Fix bit_width calculation for ascending bus ranges in writeBusDcls#361
Conversation
std::abs(from - to + 1) gives the wrong result when from < to (ascending ranges, e.g. [0:10]): abs(0 - 10 + 1) = 9 instead of 11. Moving +1 outside abs fixes both ascending and descending ranges: ascending [0:10]: abs(0 - 10) + 1 = 11 (correct) descending [10:0]: abs(10 - 0) + 1 = 11 (correct) Fixes The-OpenROAD-Project#360
There was a problem hiding this comment.
Code Review
This pull request corrects the bit width calculation for bus declarations in LibertyWriter.cc by moving the increment outside the absolute difference of the range bounds. Feedback suggests adding regression tests for both ascending and descending bus ranges to ensure continued correctness, as the existing test suite does not currently verify bus declaration content.
| sta::print(stream_, " base_type : array;\n"); | ||
| sta::print(stream_, " data_type : bit;\n"); | ||
| sta::print(stream_, " bit_width : {};\n", std::abs(dcl->from() - dcl->to() + 1)); | ||
| sta::print(stream_, " bit_width : {};\n", std::abs(dcl->from() - dcl->to()) + 1); |
There was a problem hiding this comment.
While the logic change correctly addresses the bit width calculation for ascending bus ranges, it is highly recommended to include a regression test case in liberty/test/liberty_writer.tcl (or a new test file) that specifically verifies both ascending (e.g., [0:10]) and descending (e.g., [10:0]) bus ranges. The current test suite does not appear to verify the content of bus declarations, which could lead to similar regressions in the future.
Summary
Fixes #360.
writeBusDcls()inliberty/LibertyWriter.cccomputesbit_widthincorrectly for ascending bus ranges (e.g.[0:10]).Root Cause
Change
One character moved —
+1from insidestd::abs()to outside.Impact
Ascending bus ranges are generated by OpenFPGA throughout its RTL output. Without this fix,
write_timing_modelproduces Liberty files with incorrectbit_widthvalues that Yosys rejects:This blocks top-level macro assembly in OpenLane when using
EXTRA_LIBSwith OpenFPGA-generated designs on sky130A.Verification
Reproducer and confirmation that the fix resolves it is attached to issue #360.
Tested against OpenSTA 2.5.0.