[Sim] Implement the lowering logic from sim.proc.print to the SV dialect#10172
[Sim] Implement the lowering logic from sim.proc.print to the SV dialect#10172
Conversation
d2a978d to
5358f98
Compare
lib/Conversion/SimToSV/SimToSV.cpp
Outdated
| return failure(); | ||
| } | ||
|
|
||
| // Align with FIRRTLToHW: default to writing to stderr. |
There was a problem hiding this comment.
I'm not entirely sure if this is the right approach. The current purpose is to ensure that the observable behavior remains consistent if the full lowering path FIRRTL->Sim->SV is added in the future.
There was a problem hiding this comment.
I'd prefer to lower to $write instead of $fwrite if no stream is specified. We can then restore the FIRRTL behavior by adding a sim.stderr_stream operation.
There was a problem hiding this comment.
I think that sounds like a good direction. However, the current SV dialect does not seem to have an operation corresponding to $write.
For this PR, I'd prefer to preserve the existing behavior and keep lowering streamless prints to the same default error stream as the current FIRRTL-to-SV path.
Would it be okay to refine this in a follow-up PR once we have explicit support for $write on the SV side?
There was a problem hiding this comment.
I re-examined sv.fwrite, and we can simulate the behavior of $write by specifying that it outputs to stdout. I'm not entirely sure if adding an sv.write just to represent $write is worthwhile, since $fwrite already covers this capability.
I've implemented sv.write in #10179.
fzi-hielscher
left a comment
There was a problem hiding this comment.
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:
- Find and convert the print operations
- Run Region DCE on the bodies of the affected
hw.triggeredops to remove the dangling formatting ops. - Run the dialect conversion
62e153a to
ee209ff
Compare
19f96ea to
4e6adb2
Compare
| return success(); | ||
| } | ||
|
|
||
| static Operation *findProceduralRoot(Operation *op) { |
There was a problem hiding this comment.
After using proceduralize-sim, we need to lower hw.triggered to the sv dialect before we can lower sim.proc.print to sv.fwrite; otherwise, the constraints of sv.fwrite cannot be satisfied. This is why I haven't added hw.triggered to the whitelist here.
|
|
||
| // ----- | ||
|
|
||
| hw.module @unsupported_procedural_root(in %clk : i1) { |
There was a problem hiding this comment.
The hw.triggered should be lower to SV dialect first as sv.fwrite is only allowed to appear in the procedure block of sv.
672d519 to
3911e4b
Compare
fabianschuiki
left a comment
There was a problem hiding this comment.
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?
91262c9 to
e314a09
Compare
That sounds reasonable. I must have misunderstood some of the previous reviews, sorry. |
…ect. AI-assisted-by: OpenAI ChatGPT
e314a09 to
d0dae92
Compare
Yeah.. I now remember the problem with the SV procedural region trait : #7314 🫤 |
|
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? |
|
Having a common trait would be great 🙏 I encountered this a few times in the past and it was really annoying to avoid. |
This PR lowers
sim.proc.printto SystemVerilogsv.fwrite.It only handles the procedural form of print operations. Non-procedural
sim.printis expected to be converted first, for example by--sim-proceduralize.This PR does not add stream support yet.
Split from #10146 .
AI-assisted-by: OpenAI ChatGPT