Skip to content

[Sim] Implement the lowering logic from sim.proc.print to the SV dialect#10172

Merged
fzi-hielscher merged 3 commits into
llvm:mainfrom
nanjo712:feat/lower-sim-proc-print-to-sv
Apr 16, 2026
Merged

[Sim] Implement the lowering logic from sim.proc.print to the SV dialect#10172
fzi-hielscher merged 3 commits into
llvm:mainfrom
nanjo712:feat/lower-sim-proc-print-to-sv

Conversation

@nanjo712
Copy link
Copy Markdown
Contributor

@nanjo712 nanjo712 commented Apr 10, 2026

This PR lowers sim.proc.print to SystemVerilog sv.fwrite.

It only handles the procedural form of print operations. Non-procedural sim.print is expected to be converted first, for example by --sim-proceduralize.

Split from #10146 .

AI-assisted-by: OpenAI ChatGPT

@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from d2a978d to 5358f98 Compare April 10, 2026 10:54
@nanjo712 nanjo712 marked this pull request as ready for review April 10, 2026 11:07
Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
Copy link
Copy Markdown
Contributor

@fzi-hielscher fzi-hielscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is generally not recommended to traverse def-use-chains within the dialect conversion framework, so an OpConversionPattern is not ideal for lowering the print operations. We should perform the lowering of sim.proc.print before doing the dialect conversion. I would suggest to:

  1. Find and convert the print operations
  2. Run Region DCE on the bodies of the affected hw.triggered ops to remove the dangling formatting ops.
  3. Run the dialect conversion

Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch 2 times, most recently from 62e153a to ee209ff Compare April 10, 2026 14:53
@nanjo712 nanjo712 changed the title [SimToSV] Implement sim.proc.print lowering logic [Sim] Implement the lowering logic from sim.proc.print to the SV dialect Apr 10, 2026
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch 3 times, most recently from 19f96ea to 4e6adb2 Compare April 10, 2026 15:34
Comment thread lib/Conversion/SimToSV/LowerPrintFormattedProcToSV.cpp Outdated
Comment thread test/Conversion/SimToSV/lower-print-formatted-proc-to-sv-errors.mlir Outdated
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch 2 times, most recently from 672d519 to 3911e4b Compare April 10, 2026 15:45
@nanjo712 nanjo712 requested a review from fzi-hielscher April 10, 2026 15:57
Copy link
Copy Markdown
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really amazing effort @nanjo712 🥳, thanks for pushing this forward! One thing I noticed: you had this neatly tucked into the SimToSV conversion earlier, which feels like the right spot to convert a sim.proc.print op to sv.fwrite. Would it make sense to go back to that? That would eventually allow us to have SimTransforms not link against the SV dialect anymore -- all the patterns that touch both Sim and SV would live in the SimToSV conversion. WDYT?

@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch 2 times, most recently from 91262c9 to e314a09 Compare April 10, 2026 17:02
@nanjo712
Copy link
Copy Markdown
Contributor Author

Really amazing effort @nanjo712 🥳, thanks for pushing this forward! One thing I noticed: you had this neatly tucked into the SimToSV conversion earlier, which feels like the right spot to convert a sim.proc.print op to sv.fwrite. Would it make sense to go back to that? That would eventually allow us to have SimTransforms not link against the SV dialect anymore -- all the patterns that touch both Sim and SV would live in the SimToSV conversion. WDYT?

That sounds reasonable. I must have misunderstood some of the previous reviews, sorry.

@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from e314a09 to d0dae92 Compare April 10, 2026 17:17
@fzi-hielscher
Copy link
Copy Markdown
Contributor

The hw.triggered should be lower to SV dialect first as sv.fwrite is only allowed to appear in the procedure block of sv.

Yeah.. I now remember the problem with the SV procedural region trait : #7314 🫤
If you do the ProcPrint to FWrite conversion as part of the same pass but before applyPartialConversion does this still cause a verification error? Since I've posted that PR, LLHD has also gained its own procedural region trait. Do you think we could unify the LLHD and SV flavor in the HW dialect and add it to hw.triggered, @fabianschuiki?

@fabianschuiki
Copy link
Copy Markdown
Contributor

It would be great to have a unified trait for that! I wonder whether this could live somewhere in CIRCTSupport or so, since it seems to be mostly orthogonal to the actual dialects. WDYT @fzi-hielscher?

@uenoku
Copy link
Copy Markdown
Member

uenoku commented Apr 10, 2026

Having a common trait would be great 🙏 I encountered this a few times in the past and it was really annoying to avoid.

@nanjo712
Copy link
Copy Markdown
Contributor Author

The hw.triggered should be lower to SV dialect first as sv.fwrite is only allowed to appear in the procedure block of sv.

Yeah.. I now remember the problem with the SV procedural region trait : #7314 🫤 If you do the ProcPrint to FWrite conversion as part of the same pass but before applyPartialConversion does this still cause a verification error? Since I've posted that PR, LLHD has also gained its own procedural region trait. Do you think we could unify the LLHD and SV flavor in the HW dialect and add it to hw.triggered, @fabianschuiki?

Thanks for following up on this with #10180 and #10195.

Given that, I plan to wait for #10195 to land before continuing this PR. Once that happens, I'll update the checking logic here to follow that design instead of preserving the current ad hoc restrictions.

@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from dbf232d to 7ac0123 Compare April 14, 2026 08:30
Copy link
Copy Markdown
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split get_file lowering? I think it's better to get plain format string done first.

@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from e57cf9e to f18f480 Compare April 15, 2026 09:35
@nanjo712
Copy link
Copy Markdown
Contributor Author

Could you split get_file lowering? I think it's better to get plain format string done first.

This sounds reasonable. I have reduced the current PR to only handle the range without specified output streams. We can focus on this issue first.

@nanjo712 nanjo712 requested a review from uenoku April 15, 2026 09:44
Copy link
Copy Markdown
Contributor

@fzi-hielscher fzi-hielscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pursuing this, @nanjo712 👍

Comment thread lib/Conversion/SimToSV/CMakeLists.txt Outdated
Comment thread lib/Conversion/SimToSV/LowerPrintFormattedProcToSV.cpp Outdated
Comment thread lib/Conversion/SimToSV/LowerPrintFormattedProcToSV.cpp Outdated
Comment thread lib/Conversion/SimToSV/LowerPrintFormattedProcToSV.cpp Outdated
Comment thread lib/Conversion/SimToSV/LowerPrintFormattedProcToSV.cpp Outdated
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch 2 times, most recently from 4b2f3ad to 1c83edc Compare April 15, 2026 12:30
@nanjo712 nanjo712 requested a review from fzi-hielscher April 15, 2026 12:44
Copy link
Copy Markdown
Contributor

@fzi-hielscher fzi-hielscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The conversion LGTM. Could you change the sv.* ops in the test to HW and SCF ops, so they align with how the pass will be used in practice?

Comment thread test/Conversion/SimToSV/lower-print-formatted-proc-to-sv.mlir Outdated
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from 1c83edc to 7fc794c Compare April 15, 2026 14:59
@nanjo712
Copy link
Copy Markdown
Contributor Author

Could you change the sv.* ops in the test to HW and SCF ops, so they align with how the pass will be used in practice?

That's resonable. Done.

@nanjo712 nanjo712 requested a review from fzi-hielscher April 15, 2026 15:03
Comment thread test/Conversion/SimToSV/lower-print-formatted-proc-to-sv.mlir Outdated
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from 7fc794c to 712394b Compare April 15, 2026 16:08
@nanjo712 nanjo712 requested a review from fzi-hielscher April 15, 2026 16:14
Copy link
Copy Markdown
Contributor

@fzi-hielscher fzi-hielscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. 🥳 Thanks again for putting in the work.
Just one small thing and a request for the future: If you are making incremental changes due to review comments, please add commits to the PR instead of force-pushing the single commit. The commits get squashed before merging, and it helps me (and others) a lot to track which parts have changed and need re-review.

Comment thread test/Conversion/SimToSV/lower-print-formatted-proc-to-sv-errors.mlir Outdated
Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
Copy link
Copy Markdown
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nanjo712
Copy link
Copy Markdown
Contributor Author

nanjo712 commented Apr 16, 2026

Thank you for your detailed review!

If you are making incremental changes due to review comments, please add commits to the PR instead of force-pushing the single commit. The commits get squashed before merging, and it helps me (and others) a lot to track which parts have changed and need re-review.

I understand. I wasn't very familiar with the squash mechanism before, sorry for any inconvenience it may have caused.

@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from 24cde9e to 610adc7 Compare April 16, 2026 02:53
@nanjo712 nanjo712 force-pushed the feat/lower-sim-proc-print-to-sv branch from 610adc7 to 328cf80 Compare April 16, 2026 02:53
Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
Comment thread lib/Conversion/SimToSV/SimToSV.cpp Outdated
@fzi-hielscher fzi-hielscher merged commit 07ba639 into llvm:main Apr 16, 2026
6 checks passed
@nanjo712 nanjo712 deleted the feat/lower-sim-proc-print-to-sv branch April 16, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants