Fix an output_to_verilog bug where inlined arithmetic expressions might unintentionaly truncate. - #501
Merged
Merged
Conversation
…ight unintentionaly truncate.
In Verilog, the expression `a + b` in the example below has bitwidth 6:
input[5:0] a;
input[5:0] b;
output[7:0] o;
assign o = {1'd1, a + b};
This is a major difference from PyRTL, where `a + b` has bitwidth 7. Assignment
to `o` in this example may trigger a warning, because the concatenation
expression has bitwidth 7 (`1'd1` has bitwidth 1, `a + b` has bitwidth 6),
while `o` has bitwidth 8.
Loss of the addition's carry bit can be prevented by explicitly assigning the
result to a wider-bitwidth wire:
wire[6:0] tmp;
assign tmp = a + b;
assign o = {1'd1, tmp};
This commit prevents `pyrtl.output_to_verilog` from inlining arithmetic
expressions (`+`, `-`, `*`) to avoid triggering this implicit truncation in
Verilog. We can't safely inline these arithmetic expressions because the carry
bits may be needed.
We could analyze the `GateGraph` and check if the carry bits are actually used.
For now we keep things simple and never inline arithmetic expressions.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## development #501 +/- ##
=============================================
+ Coverage 93.8% 93.8% +0.1%
=============================================
Files 30 30
Lines 7141 7143 +2
=============================================
+ Hits 6692 6694 +2
Misses 449 449 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In Verilog, the expression
a + bin the example below has bitwidth 6:This is a major difference from PyRTL, where
a + bhas bitwidth 7. Assignment tooin this example may trigger a warning, because the concatenation expression has bitwidth 7 (1'd1has bitwidth 1,a + bhas bitwidth 6), whileohas bitwidth 8.Loss of the addition's carry bit can be prevented by explicitly assigning the result to a wider-bitwidth wire:
This commit prevents
pyrtl.output_to_verilogfrom inlining arithmetic expressions (+,-,*) to avoid triggering this implicit truncation in Verilog. We can't safely inline these arithmetic expressions because the carry bits may be needed.We could analyze the
GateGraphand check if the carry bits are actually used. For now we keep things simple and never inline arithmetic expressions.