Skip to content

Fix an output_to_verilog bug where inlined arithmetic expressions might unintentionaly truncate. - #501

Merged
fdxmw merged 2 commits into
UCSBarchlab:developmentfrom
fdxmw:verilog
Jul 28, 2026
Merged

Fix an output_to_verilog bug where inlined arithmetic expressions might unintentionaly truncate.#501
fdxmw merged 2 commits into
UCSBarchlab:developmentfrom
fdxmw:verilog

Conversation

@fdxmw

@fdxmw fdxmw commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

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.

…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

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.8%. Comparing base (82d8858) to head (0c59ea0).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fdxmw
fdxmw merged commit 9454d07 into UCSBarchlab:development Jul 28, 2026
6 checks passed
@fdxmw
fdxmw deleted the verilog branch July 28, 2026 16:49
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.

1 participant