Skip to content

Fix bit_width calculation for ascending bus ranges in writeBusDcls#361

Open
JayPankajPatel wants to merge 1 commit into
The-OpenROAD-Project:masterfrom
JayPankajPatel:fix/write-timing-model-ascending-bus-bit-width
Open

Fix bit_width calculation for ascending bus ranges in writeBusDcls#361
JayPankajPatel wants to merge 1 commit into
The-OpenROAD-Project:masterfrom
JayPankajPatel:fix/write-timing-model-ascending-bus-bit-width

Conversation

@JayPankajPatel
Copy link
Copy Markdown

Summary

Fixes #360.

writeBusDcls() in liberty/LibertyWriter.cc computes bit_width incorrectly for ascending bus ranges (e.g. [0:10]).

Root Cause

// before — +1 inside abs(), wrong for ascending ranges
std::abs(dcl->from() - dcl->to() + 1)
// ascending  [0:10]:  abs(0  - 10 + 1) = abs(-9) = 9   ← WRONG
// descending [10:0]:  abs(10 - 0  + 1) = 11             ← correct

// after — +1 outside abs(), correct for both
std::abs(dcl->from() - dcl->to()) + 1
// ascending  [0:10]:  abs(0  - 10) + 1 = 11  ✓
// descending [10:0]:  abs(10 - 0)  + 1 = 11  ✓

Change

One character moved — +1 from inside std::abs() to outside.

Impact

Ascending bus ranges are generated by OpenFPGA throughout its RTL output. Without this fix, write_timing_model produces Liberty files with incorrect bit_width values that Yosys rejects:

ERROR: Incompatible array type 'chanx_left_in': bit_width=9, bit_from=0, bit_to=10.

This blocks top-level macro assembly in OpenLane when using EXTRA_LIBS with 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.

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
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 11, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread liberty/LibertyWriter.cc
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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

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.

write_timing_model: incorrect bit_width for ascending bus ranges (off-by-one in std::abs)

2 participants