1
1
import io
2
+ import pathlib
2
3
import pytest
4
+ import tempfile
5
+ import textwrap
3
6
from typing import Optional
4
7
5
8
import circt
@@ -43,7 +46,7 @@ def test_bad_input():
43
46
44
47
@pytest .mark .parametrize ("style" , ("plain" , "wrapInAtSquareBracket" , "none" ))
45
48
def test_location_info_style (style ):
46
- ir = (
49
+ ir = textwrap . dedent (
47
50
"""
48
51
module attributes {{circt.loweringOptions = "locationInfoStyle={style}"}} {{
49
52
hw.module @M() -> () {{}}
@@ -57,16 +60,16 @@ def test_location_info_style(style):
57
60
line = ostream .readline ().rstrip ()
58
61
expected = "module M();"
59
62
if style == "plain" :
60
- expected += " // -:3:11 "
63
+ expected += " // -:3:3 "
61
64
elif style == "wrapInAtSquareBracket" :
62
- expected += " // @[-:3:11 ]"
65
+ expected += " // @[-:3:3 ]"
63
66
assert line == expected
64
67
65
68
66
69
@pytest .mark .parametrize ("explicit_bitcast" , (False , True ))
67
70
def test_explicit_bitcast (explicit_bitcast ):
68
71
explicit_bitcast_attr = ",explicitBitcast" if explicit_bitcast else ""
69
- ir = (
72
+ ir = textwrap . dedent (
70
73
"""
71
74
module attributes {{circt.loweringOptions = "locationInfoStyle=none{explicit_bitcast_attr}"}} {{
72
75
hw.module @M(%a: i8, %b: i8) -> (y: i8) {{
@@ -98,7 +101,7 @@ def test_disallow_expression_inlining_in_ports(disallow_expression_inlining_in_p
98
101
else ""
99
102
)
100
103
101
- ir = (
104
+ ir = textwrap . dedent (
102
105
"""
103
106
module attributes {{circt.loweringOptions = "locationInfoStyle=none{disallow_expression_inlining_in_ports_attr}"}} {{
104
107
hw.module.extern @Foo(%I: i1) -> (O: i1)
@@ -137,7 +140,7 @@ def test_omit_version_comment(omit_version_comment):
137
140
else ""
138
141
)
139
142
140
- ir = (
143
+ ir = textwrap . dedent (
141
144
"""
142
145
module attributes {{circt.loweringOptions = "locationInfoStyle=none{omit_version_comment_attr}"}} {{
143
146
hw.module @M() -> () {{}}
@@ -156,22 +159,39 @@ def test_omit_version_comment(omit_version_comment):
156
159
assert first .startswith ("// Generated by" )
157
160
158
161
159
- def test_split_verilog ():
160
- ir = (
162
+ @pytest .mark .parametrize ("specify_output_file" , (False , True ))
163
+ def test_split_verilog (specify_output_file ):
164
+ ir = textwrap .dedent (
161
165
"""
162
166
module attributes {{circt.loweringOptions = "locationInfoStyle=none"}} {{
163
- hw.module @M() -> () attributes {{output_file = {output_file}} } {{}}
167
+ hw.module @M() -> () {attribute_string } {{}}
164
168
}}
165
169
"""
166
170
)
167
- output_file = "tests/test_backend/test_mlir/build/test_mlir_to_verilog_split_verilog.sv"
168
- ir = ir .format (output_file = f"#hw.output_file<\" { output_file } \" >" )
169
- _ , ostream = _run_test (ir , split_verilog = True )
170
- ostream .seek (0 )
171
- assert not ostream .readline () # output expected to be empty
172
-
173
- # Now read ostream from the expcted output file.
174
- ostream = open (output_file )
175
- ostream .readline () # skip header
176
- assert ostream .readline ().rstrip () == "module M();"
177
- assert ostream .readline ().rstrip () == "endmodule"
171
+ with tempfile .TemporaryDirectory () as tempdir :
172
+ output_file = f"{ tempdir } /outfile.sv"
173
+ if specify_output_file :
174
+ attribute_string = (
175
+ f"attributes "
176
+ f"{{output_file = #hw.output_file<\" { output_file } \" >}}"
177
+ )
178
+ else :
179
+ attribute_string = ""
180
+ ir = ir .format (attribute_string = attribute_string )
181
+ opts = {"split_verilog" : True }
182
+ if not specify_output_file :
183
+ opts ["split_verilog_directory" ] = tempdir
184
+ _ , ostream = _run_test (ir , ** opts )
185
+ ostream .seek (0 )
186
+ # We expect the output to be empty due to split verilog.
187
+ assert not ostream .readline ()
188
+
189
+ # Now read ostream from the expcted output file. If the output file is
190
+ # not specificed explicitly, then it goes into <split verilog
191
+ # directory>/<module name>.sv (in this case, <tempdir>/M.sv).
192
+ if not specify_output_file :
193
+ output_file = f"{ tempdir } /M.sv"
194
+ with open (output_file , "r" ) as ostream :
195
+ ostream .readline () # skip header
196
+ assert ostream .readline ().rstrip () == "module M();"
197
+ assert ostream .readline ().rstrip () == "endmodule"
0 commit comments