|
| 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