Skip to content

Commit f9a281c

Browse files
authored
Merge pull request #10835 from The-OpenROAD-Project-staging/secure-issue1737-hold-buffer-count-fix
rsz: fix hold-buffer count reset on repair_timing -hold rollback
2 parents 20d015b + 1f30b01 commit f9a281c

10 files changed

Lines changed: 476 additions & 250 deletions

‎src/rsz/src/RecoverPower.cc‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ bool RecoverPower::recoverPower(const float recover_power_percent, bool verbose)
154154
if (better) {
155155
failed_move_threshold = 0;
156156
resizer_->journalEnd();
157+
resize_count_++;
157158
debugPrint(logger_,
158159
RSZ,
159160
"recover_power",
@@ -178,6 +179,7 @@ bool RecoverPower::recoverPower(const float recover_power_percent, bool verbose)
178179
"power recovery",
179180
failed_move_threshold_limit_);
180181
resizer_->journalEnd();
182+
resize_count_++;
181183
break;
182184
}
183185
resizer_->journalRestore();
@@ -229,6 +231,10 @@ sta::Vertex* RecoverPower::recoverPower(const sta::Pin* end_pin)
229231
est::IncrementalParasiticsGuard guard(estimate_parasitics_);
230232
drvr_vertex = recoverPower(path, slack);
231233
}
234+
// No accept/reject journal here: a returned vertex is one kept downsize.
235+
if (drvr_vertex != nullptr) {
236+
resize_count_++;
237+
}
232238

233239
if (resize_count_ > 0) {
234240
logger_->info(RSZ, 3111, "Resized {} instances.", resize_count_);
@@ -360,7 +366,7 @@ bool RecoverPower::downsizeDrvr(const sta::Path* drvr_path,
360366
drvr_port->libertyCell()->name(),
361367
downsize->name());
362368
if (resizer_->replaceCell(drvr, downsize, true)) {
363-
resize_count_++;
369+
// Counted by the caller only if the move is kept (journalEnd).
364370
return true;
365371
}
366372
}

‎src/rsz/src/RepairHold.cc‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ bool RepairHold::repairHold(sta::VertexSeq& ends,
392392
sta::Slack worst_slack;
393393
findHoldViolations(ends, hold_margin, worst_slack, hold_failures);
394394
inserted_buffer_count_ = 0;
395+
resize_count_ = 0;
395396
if (!hold_failures.empty()) {
396397
logger_->info(RSZ,
397398
46,
@@ -659,11 +660,11 @@ void RepairHold::repairEndHold(sta::Vertex* end_vertex,
659660
sta::RiseFallBoth::riseFall(),
660661
sta_->scenes(),
661662
max_);
662-
makeHoldDelay(path_vertex,
663-
load_pins,
664-
loads_have_out_port,
665-
buffer_cell,
666-
buffer_loc);
663+
const HoldMoveStats move = makeHoldDelay(path_vertex,
664+
load_pins,
665+
loads_have_out_port,
666+
buffer_cell,
667+
buffer_loc);
667668
sta::Slew slew_after = sta_->slew(path_vertex,
668669
sta::RiseFallBoth::riseFall(),
669670
sta_->scenes(),
@@ -677,9 +678,10 @@ void RepairHold::repairEndHold(sta::Vertex* end_vertex,
677678
&& sta::fuzzyLess(setup_slack_after, setup_slack_before)
678679
&& setup_slack_after < setup_margin)) {
679680
resizer_->journalRestore();
680-
inserted_buffer_count_ = 0;
681681
} else {
682682
resizer_->journalEnd();
683+
inserted_buffer_count_ += move.buffers;
684+
resize_count_ += move.resizes;
683685
}
684686
}
685687
}
@@ -709,12 +711,14 @@ void RepairHold::mergeInto(Slacks& from, Slacks& result)
709711
= max(result[fall_index_][max_index_], from[fall_index_][max_index_]);
710712
}
711713

712-
void RepairHold::makeHoldDelay(sta::Vertex* drvr,
713-
sta::PinSeq& load_pins,
714-
bool loads_have_out_port, // top level port
715-
sta::LibertyCell* buffer_cell,
716-
const odb::Point& loc)
714+
RepairHold::HoldMoveStats RepairHold::makeHoldDelay(
715+
sta::Vertex* drvr,
716+
sta::PinSeq& load_pins,
717+
bool loads_have_out_port, // top level port
718+
sta::LibertyCell* buffer_cell,
719+
const odb::Point& loc)
717720
{
721+
HoldMoveStats stats;
718722
sta::Instance* buffer = nullptr;
719723
sta::Pin* buffer_out_pin = nullptr;
720724

@@ -735,7 +739,7 @@ void RepairHold::makeHoldDelay(sta::Vertex* drvr,
735739
}
736740
}
737741
if (load_pins_set.empty()) {
738-
return;
742+
return stats;
739743
}
740744

741745
buffer = resizer_->insertBufferBeforeLoads(
@@ -746,17 +750,16 @@ void RepairHold::makeHoldDelay(sta::Vertex* drvr,
746750
3009,
747751
"insert_buffer failed on drvr_pin '{}'.",
748752
drvr_pin_name.empty() ? "<unknown>" : drvr_pin_name);
749-
return;
753+
return stats;
750754
}
755+
stats.buffers = 1;
751756

752757
odb::dbInst* new_buffer = db_network_->staToDb(buffer);
753758
debugPrint(
754759
logger_, RSZ, "repair_hold", 3, " insert {}", new_buffer->getName());
755760

756761
buffer_out_pin = db_network_->dbToSta(new_buffer->getFirstOutput());
757762

758-
inserted_buffer_count_++;
759-
760763
// Update RC and delay. Resize if necessary
761764
sta::Vertex* buffer_out_vertex = graph_->pinDrvrVertex(buffer_out_pin);
762765
estimate_parasitics_->updateParasitics();
@@ -765,8 +768,9 @@ void RepairHold::makeHoldDelay(sta::Vertex* drvr,
765768
if (!checkMaxSlewCap(buffer_out_pin)
766769
&& resizer_->resizeToTargetSlew(buffer_out_pin)) {
767770
estimate_parasitics_->updateParasitics();
768-
resize_count_++;
771+
stats.resizes = 1;
769772
}
773+
return stats;
770774
}
771775

772776
bool RepairHold::checkMaxSlewCap(const sta::Pin* drvr_pin)

‎src/rsz/src/RepairHold.hh‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ class RepairHold : public sta::dbStaState
9191
double setup_margin,
9292
double hold_margin,
9393
bool allow_setup_violations);
94-
void makeHoldDelay(sta::Vertex* drvr,
95-
sta::PinSeq& load_pins,
96-
bool loads_have_out_port,
97-
sta::LibertyCell* buffer_cell,
98-
const odb::Point& loc);
94+
// Buffers/resizes from one hold-fix attempt; committed by repairEndHold.
95+
struct HoldMoveStats
96+
{
97+
int buffers = 0;
98+
int resizes = 0;
99+
};
100+
HoldMoveStats makeHoldDelay(sta::Vertex* drvr,
101+
sta::PinSeq& load_pins,
102+
bool loads_have_out_port,
103+
sta::LibertyCell* buffer_cell,
104+
const odb::Point& loc);
99105
bool checkMaxSlewCap(const sta::Pin* drvr_pin);
100106
void mergeInit(Slacks& slacks);
101107
void mergeInto(Slacks& from, Slacks& result);

‎src/rsz/test/BUILD‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,36 @@ cc_test(
597597
],
598598
)
599599

600+
cc_test(
601+
name = "TestRepairHoldRollback",
602+
srcs = ["cpp/TestRepairHoldRollback.cc"],
603+
copts = ["-Isrc/rsz/src"],
604+
data = [
605+
"Nangate45/Nangate45.lef",
606+
"Nangate45/Nangate45_typ.lib",
607+
"gcd_nangate45_placed.def",
608+
],
609+
features = ["-layering_check"],
610+
deps = [
611+
"//src/ant",
612+
"//src/dbSta",
613+
"//src/dbSta:dbNetwork",
614+
"//src/dpl",
615+
"//src/est",
616+
"//src/grt",
617+
"//src/odb/src/db",
618+
"//src/odb/src/defin",
619+
"//src/rsz",
620+
"//src/sta:opensta_lib",
621+
"//src/stt",
622+
"//src/tst",
623+
"//src/tst:integrated_fixture",
624+
"//src/utl",
625+
"@googletest//:gtest",
626+
"@googletest//:gtest_main",
627+
],
628+
)
629+
600630
py_library(
601631
name = "helpers",
602632
srcs = ["helpers.py"],

‎src/rsz/test/cpp/CMakeLists.txt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,32 @@ gtest_discover_tests(TestResizerMt
201201

202202
add_dependencies(build_and_test TestResizerMt
203203
)
204+
205+
add_executable(TestRepairHoldRollback TestRepairHoldRollback.cc)
206+
target_link_libraries(TestRepairHoldRollback
207+
OpenSTA
208+
GTest::gtest
209+
GTest::gtest_main
210+
GTest::gmock
211+
dbSta_lib
212+
utl_lib
213+
rsz_lib
214+
grt_lib
215+
dpl_lib
216+
stt_lib
217+
odb
218+
tst_integrated_fixture
219+
${TCL_LIBRARY}
220+
)
221+
222+
target_include_directories(TestRepairHoldRollback
223+
PRIVATE
224+
${PROJECT_SOURCE_DIR}/src/rsz/src
225+
)
226+
227+
gtest_discover_tests(TestRepairHoldRollback
228+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..
229+
)
230+
231+
add_dependencies(build_and_test TestRepairHoldRollback
232+
)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright (c) 2026-2026, The OpenROAD Authors
3+
4+
// Regression for the hold-buffer counter on a rolled-back insertion.
5+
//
6+
// repairEndHold() opens an ECO journal around every hold-buffer insertion so
7+
// a fix that makes setup worse can be undone with journalRestore(). The old
8+
// code reset inserted_buffer_count_ to 0 on that rollback instead of leaving
9+
// the already-committed buffers counted. journalRestore() only undoes the one
10+
// insertion just attempted, so wiping the whole count under-reports every
11+
// buffer kept earlier in the run. That corrupts the "Inserted N hold buffers"
12+
// line, the per-iteration table, the design__instance__count__hold_buffer
13+
// metric, and defeats the max_buffer_percent safety valve (which compares
14+
// against the count that was just zeroed).
15+
//
16+
// The gcd design at a tight 0.5ns clock is setup-critical: several hold fixes
17+
// commit, then a later fix is rolled back because it pushes the worst setup
18+
// slack negative. With the bug, holdBufferCount() collapses to the handful of
19+
// buffers committed after the last rollback while the netlist still holds all
20+
// of them; with the fix the count matches the buffers actually in the block.
21+
// The invariant asserted here - reported count == buffers physically added -
22+
// holds for any rollback pattern on correct code and only breaks when a
23+
// rollback wipes the count.
24+
25+
#include <string>
26+
#include <vector>
27+
28+
#include "db_sta/dbNetwork.hh"
29+
#include "db_sta/dbSta.hh"
30+
#include "est/EstimateParasitics.h"
31+
#include "gtest/gtest.h"
32+
#include "odb/db.h"
33+
#include "odb/dbSet.h"
34+
#include "odb/defin.h"
35+
#include "rsz/Resizer.hh"
36+
#include "sta/Clock.hh"
37+
#include "sta/Graph.hh"
38+
#include "sta/Liberty.hh"
39+
#include "sta/MinMax.hh"
40+
#include "sta/Mode.hh"
41+
#include "sta/NetworkClass.hh"
42+
#include "sta/Sdc.hh"
43+
#include "sta/Transition.hh"
44+
#include "sta/Units.hh"
45+
#include "tst/IntegratedFixture.h"
46+
47+
namespace rsz {
48+
49+
class RepairHoldRollbackTest : public tst::IntegratedFixture
50+
{
51+
protected:
52+
RepairHoldRollbackTest()
53+
: tst::IntegratedFixture(tst::IntegratedFixture::Technology::kNangate45,
54+
"_main/src/rsz/test/")
55+
{
56+
}
57+
58+
void SetUp() override
59+
{
60+
// Read the placed gcd design directly from DEF (no high-level placement
61+
// commands). It is already used by the repair_hold9 Tcl test.
62+
odb::dbChip* chip = odb::dbChip::create(db_.get(), db_->getTech());
63+
odb::defin def_reader(db_.get(), &logger_, odb::defin::DEFAULT);
64+
std::vector<odb::dbLib*> search_libs;
65+
for (odb::dbLib* lib : db_->getLibs()) {
66+
search_libs.push_back(lib);
67+
}
68+
def_reader.readChip(
69+
search_libs,
70+
getFilePath("_main/src/rsz/test/gcd_nangate45_placed.def").c_str(),
71+
chip);
72+
block_ = db_->getChip()->getBlock();
73+
sta_->postReadDef(block_);
74+
75+
makeTightClock();
76+
77+
sta_->ensureGraph();
78+
sta_->ensureLevelized();
79+
resizer_.initBlock();
80+
ep_.estimateWireParasitics();
81+
}
82+
83+
// A single "clk" clock with a deliberately tight 0.5ns period so the design
84+
// is setup-critical and hold fixes contend with setup slack.
85+
void makeTightClock()
86+
{
87+
sta::Cell* top_cell = db_network_->cell(db_network_->topInstance());
88+
ASSERT_NE(top_cell, nullptr);
89+
sta::Port* clk_port = db_network_->findPort(top_cell, "clk");
90+
ASSERT_NE(clk_port, nullptr);
91+
sta::Pin* clk_pin
92+
= db_network_->findPin(db_network_->topInstance(), clk_port);
93+
sta::PinSet clk_pins(db_network_);
94+
clk_pins.insert(clk_pin);
95+
96+
const double period = sta_->units()->timeUnit()->userToSta(0.5);
97+
sta::FloatSeq waveform;
98+
waveform.push_back(0);
99+
waveform.push_back(period / 2.0);
100+
sta_->makeClock("clk",
101+
clk_pins,
102+
/*add_to_pins=*/false,
103+
period,
104+
waveform,
105+
/*comment=*/"",
106+
sta_->cmdMode());
107+
}
108+
109+
// Number of buffer instances currently in the block.
110+
int bufferInstCount() const
111+
{
112+
int count = 0;
113+
for (odb::dbInst* inst : block_->getInsts()) {
114+
sta::LibertyCell* cell
115+
= db_network_->libertyCell(db_network_->dbToSta(inst));
116+
if (cell != nullptr && cell->isBuffer()) {
117+
++count;
118+
}
119+
}
120+
return count;
121+
}
122+
};
123+
124+
TEST_F(RepairHoldRollbackTest, HoldBufferCountSurvivesRollback)
125+
{
126+
const int buffers_before = bufferInstCount();
127+
128+
// Large hold margin on a setup-critical design forces some hold fixes to be
129+
// rolled back after others have committed. max_buffer_percent is set high so
130+
// the (correctly counted) buffers never trip the RSZ-0060 safety valve.
131+
const double hold_margin = sta_->units()->timeUnit()->userToSta(0.25);
132+
resizer_.repairHold(/*setup_margin=*/0.0,
133+
hold_margin,
134+
/*allow_setup_violations=*/false,
135+
/*max_buffer_percent=*/1.0,
136+
/*max_passes=*/10000,
137+
/*max_iterations=*/-1,
138+
/*match_cell_footprint=*/false,
139+
/*verbose=*/false);
140+
141+
const int buffers_added = bufferInstCount() - buffers_before;
142+
143+
// The run must actually insert hold buffers, otherwise it exercises nothing.
144+
EXPECT_GT(buffers_added, 0);
145+
146+
// The reported count must equal the buffers actually left in the netlist.
147+
// The old code zeroed inserted_buffer_count_ on a rolled-back insertion, so
148+
// this reported far fewer buffers than are physically present.
149+
EXPECT_EQ(resizer_.holdBufferCount(), buffers_added);
150+
}
151+
152+
} // namespace rsz

0 commit comments

Comments
 (0)