11import io
2+ import pathlib
23import pytest
4+ import tempfile
5+ import textwrap
36from typing import Optional
47
58import circt
@@ -43,7 +46,7 @@ def test_bad_input():
4346
4447@pytest .mark .parametrize ("style" , ("plain" , "wrapInAtSquareBracket" , "none" ))
4548def test_location_info_style (style ):
46- ir = (
49+ ir = textwrap . dedent (
4750 """
4851 module attributes {{circt.loweringOptions = "locationInfoStyle={style}"}} {{
4952 hw.module @M() -> () {{}}
@@ -57,16 +60,16 @@ def test_location_info_style(style):
5760 line = ostream .readline ().rstrip ()
5861 expected = "module M();"
5962 if style == "plain" :
60- expected += " // -:3:11 "
63+ expected += " // -:3:3 "
6164 elif style == "wrapInAtSquareBracket" :
62- expected += " // @[-:3:11 ]"
65+ expected += " // @[-:3:3 ]"
6366 assert line == expected
6467
6568
6669@pytest .mark .parametrize ("explicit_bitcast" , (False , True ))
6770def test_explicit_bitcast (explicit_bitcast ):
6871 explicit_bitcast_attr = ",explicitBitcast" if explicit_bitcast else ""
69- ir = (
72+ ir = textwrap . dedent (
7073 """
7174 module attributes {{circt.loweringOptions = "locationInfoStyle=none{explicit_bitcast_attr}"}} {{
7275 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
98101 else ""
99102 )
100103
101- ir = (
104+ ir = textwrap . dedent (
102105 """
103106 module attributes {{circt.loweringOptions = "locationInfoStyle=none{disallow_expression_inlining_in_ports_attr}"}} {{
104107 hw.module.extern @Foo(%I: i1) -> (O: i1)
@@ -137,7 +140,7 @@ def test_omit_version_comment(omit_version_comment):
137140 else ""
138141 )
139142
140- ir = (
143+ ir = textwrap . dedent (
141144 """
142145 module attributes {{circt.loweringOptions = "locationInfoStyle=none{omit_version_comment_attr}"}} {{
143146 hw.module @M() -> () {{}}
@@ -156,22 +159,39 @@ def test_omit_version_comment(omit_version_comment):
156159 assert first .startswith ("// Generated by" )
157160
158161
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 (
161165 """
162166 module attributes {{circt.loweringOptions = "locationInfoStyle=none"}} {{
163- hw.module @M() -> () attributes {{output_file = {output_file}} } {{}}
167+ hw.module @M() -> () {attribute_string } {{}}
164168 }}
165169 """
166170 )
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