diff --git a/openroad/dpl/BUILD b/openroad/dpl/BUILD index e828951497f..cdc6b7abbb8 100644 --- a/openroad/dpl/BUILD +++ b/openroad/dpl/BUILD @@ -24,6 +24,7 @@ cc_library( "src/Opendp.cpp", "src/OptMirror.cpp", "src/Optdp.cpp", + "src/PinAccessRepair.cpp", "src/Place.cpp", "src/PlacementDRC.cpp", "src/PlacementDRC.h", @@ -90,6 +91,7 @@ cc_library( "//openroad/dpl:__subpackages__", ], deps = [ + "//openroad/drt:pin_access_service", "//openroad/odb/src/db", "//openroad/utl", "@boost.container_hash", @@ -140,6 +142,7 @@ cc_library( deps = [ ":dpl_impl", "//:ord", + "//openroad/drt:pin_access_service", "//openroad/odb/src/db", "//openroad/utl", "//openroad/web:core", diff --git a/openroad/dpl/README.md b/openroad/dpl/README.md index 2e8e96054f0..363fab3f39f 100644 --- a/openroad/dpl/README.md +++ b/openroad/dpl/README.md @@ -82,6 +82,25 @@ Run `global_connect` after buffer insertion or resizing and before `detailed_placement -pdn_aware` when global connection rules define the supply bindings. +### Repair Pin Access + +The `repair_pin_access` command moves standard cells with unusable pin access to +nearby legal sites. Run successful `pin_access` in the current session first. +DRT checks each candidate against routing rules and fixed supply geometry. +The command preserves orientation, placement constraints, and usable access in +previously healthy cells. It returns the number of unresolved cells. + +```tcl +repair_pin_access + [-max_displacement {sites rows}] + [-max_rounds count] +``` + +| Option | Description | +| ----- | ----- | +| `-max_displacement` | Nonnegative displacement from the input placement, in sites and rows. Default: `{20 2}`. | +| `-max_rounds` | Positive maximum number of improving move batches. Default: `3`. | + ### Set Placement Padding The `set_placement_padding` command sets left and right padding in multiples diff --git a/openroad/dpl/include/dpl/Opendp.h b/openroad/dpl/include/dpl/Opendp.h index 06e277b5bec..165417a5371 100644 --- a/openroad/dpl/include/dpl/Opendp.h +++ b/openroad/dpl/include/dpl/Opendp.h @@ -28,6 +28,10 @@ #include "odb/dbTypes.h" #include "odb/geom.h" +namespace drt { +class PinAccessService; +} + namespace odb { class dbBlockCallBackObj; } @@ -126,6 +130,10 @@ class Opendp bool pdn_aware = false); // C++ equivalent of `detailed_placement -quiet`. void detailedPlacementQuiet(); + int repairPinAccess(drt::PinAccessService* pa, + int max_displacement_x, + int max_displacement_y, + int max_rounds); void reportLegalizationStats() const; void reportLegalizationSummary(double runtime); @@ -220,6 +228,8 @@ class Opendp friend class Graphics; friend class CellPlaceOrderLess; friend class NegotiationLegalizer; + friend class PinAccessRepair; + friend class PinAccessRepairTest_ReleasesGridPixels_Test; void findDisplacementStats(); DbuPt pointOffMacro(const Node& cell); void convertDbToCell(odb::dbInst* db_inst, Node& cell); @@ -264,7 +274,8 @@ class Opendp GridX x, GridY y, GridX x_end, - GridY y_end) const; + GridY y_end, + const odb::dbOrientType* candidate_orient = nullptr) const; bool checkMasterSym(unsigned masterSym, unsigned cellOri) const; bool checkRowPowerCompatible(const Node* cell, GridY y) const; bool ripUpAndReplace(Node* cell); diff --git a/openroad/dpl/src/Opendp.i b/openroad/dpl/src/Opendp.i index eb51d468bcb..a80e1eba82e 100644 --- a/openroad/dpl/src/Opendp.i +++ b/openroad/dpl/src/Opendp.i @@ -10,7 +10,9 @@ #include "graphics/Graphics.h" #include "graphics/DplObserver.h" #include "dpl/Opendp.h" +#include "drt/PinAccessService.h" #include "utl/Logger.h" +#include "utl/ServiceRegistry.h" %} @@ -23,6 +25,17 @@ namespace dpl { +int +repair_pin_access_cmd(int max_displacement_x, + int max_displacement_y, + int max_rounds) +{ + auto* openroad = ord::OpenRoad::openRoad(); + auto* pa = openroad->getServiceRegistry()->find(); + return openroad->getOpendp()->repairPinAccess( + pa, max_displacement_x, max_displacement_y, max_rounds); +} + void detailed_placement_cmd(int max_displacment_x, int max_displacment_y, diff --git a/openroad/dpl/src/Opendp.tcl b/openroad/dpl/src/Opendp.tcl index 36149c19c0c..57e044a2de5 100644 --- a/openroad/dpl/src/Opendp.tcl +++ b/openroad/dpl/src/Opendp.tcl @@ -95,6 +95,38 @@ proc detailed_placement { args } { } } +sta::define_cmd_args "repair_pin_access" {[-max_displacement {sites rows}] \ + [-max_rounds count]} + +proc repair_pin_access { args } { + sta::parse_key_args "repair_pin_access" args \ + keys {-max_displacement -max_rounds} flags {} + sta::check_argc_eq0 "repair_pin_access" $args + + set max_displacement_x 20 + set max_displacement_y 2 + if { [info exists keys(-max_displacement)] } { + if { [llength $keys(-max_displacement)] != 2 } { + utl::error DPL 350 "-max_displacement requires {sites rows}." + } + lassign $keys(-max_displacement) max_displacement_x max_displacement_y + } + foreach value [list $max_displacement_x $max_displacement_y] { + if { ![string is integer -strict $value] || $value < 0 } { + utl::error DPL 349 "-max_displacement requires nonnegative integers." + } + } + set max_rounds 3 + if { [info exists keys(-max_rounds)] } { + set max_rounds $keys(-max_rounds) + } + if { ![string is integer -strict $max_rounds] || $max_rounds < 1 } { + utl::error DPL 351 "-max_rounds requires a positive integer." + } + return [dpl::repair_pin_access_cmd \ + $max_displacement_x $max_displacement_y $max_rounds] +} + sta::define_cmd_args "set_placement_padding" { -global|-masters masters|-instances insts\ [-right site_count]\ [-left site_count] \ diff --git a/openroad/dpl/src/PinAccessRepair.cpp b/openroad/dpl/src/PinAccessRepair.cpp new file mode 100644 index 00000000000..45797bd2228 --- /dev/null +++ b/openroad/dpl/src/PinAccessRepair.cpp @@ -0,0 +1,272 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2026, The OpenROAD Authors + +#include +#include +#include +#include +#include +#include +#include + +#include "dpl/Opendp.h" +#include "drt/PinAccessService.h" +#include "infrastructure/Grid.h" +#include "infrastructure/Objects.h" +#include "infrastructure/network.h" +#include "odb/dbTypes.h" +#include "utl/Logger.h" + +namespace dpl { + +class PinAccessRepair +{ + public: + PinAccessRepair(Opendp& dp, drt::PinAccessService* pa, int dx, int dy) + : dp_(dp), pa_(pa), max_displacement_x_(dx), max_displacement_y_(dy) + { + } + + int run(int max_rounds) + { + initialize(); + auto failures = scan(); + for (int round = 0; round < max_rounds && !failures.empty(); ++round) { + std::vector moves; + try { + for (Node* cell : failures) { + search(cell, moves); + } + if (moves.empty()) { + break; + } + for (const Move& move : moves) { + const odb::Point location = dp_.getDplLocation(move.cell); + move.cell->getDbInst()->setLocation(location.x(), location.y()); + } + pa_->updateDirtyPinAccess(); + // A move can change access for neighbors outside the moved batch. + auto next = scan(); + const bool healthy_preserved = std::ranges::includes( + failures, next, [](const Node* lhs, const Node* rhs) { + return lhs->getDbInst()->getId() < rhs->getDbInst()->getId(); + }); + if (next.size() < failures.size() && healthy_preserved) { + failures = std::move(next); + continue; + } + } catch (...) { + const auto error = std::current_exception(); + restorePlacement(moves); + try { + pa_->updateDirtyPinAccess(); + } catch (const std::exception& rollback_error) { + dp_.logger_->warn(utl::DPL, + 513, + "Pin access repair restored placement, but pin " + "access refresh failed: {}", + rollback_error.what()); + } + std::rethrow_exception(error); + } + restorePlacement(moves); + pa_->updateDirtyPinAccess(); + break; + } + if (!failures.empty()) { + dp_.logger_->warn(utl::DPL, + 514, + "Pin access repair stopped with {} unresolved cells.", + failures.size()); + } + return failures.size(); + } + + private: + struct Move + { + Node* cell; + GridPt old; + odb::Point location; + odb::dbOrientType orient; + }; + + void initialize() + { + dp_.initGrid(true); + dp_.groupInitPixels2(); + dp_.groupInitPixels(); + dp_.groupAssignCellRegions(); + dp_.setFixedGridCells(); + for (const auto& node : dp_.network_->getNodes()) { + if (node->getType() == Node::CELL) { + if (!node->isFixed()) { + dp_.grid_->paintPixel(node.get()); + } + if (node->isStdCell()) { + cells_.push_back(node.get()); + } + } + } + std::ranges::sort(cells_, [](const Node* lhs, const Node* rhs) { + return lhs->getDbInst()->getId() < rhs->getDbInst()->getId(); + }); + } + + std::vector scan() + { + std::vector instances; + instances.reserve(cells_.size()); + for (Node* cell : cells_) { + instances.push_back(cell->getDbInst()); + } + std::vector failures; + for (auto* inst : pa_->findPinAccessFailures(instances)) { + failures.push_back(dp_.network_->getNode(inst)); + } + return failures; + } + + std::vector> candidateSites(Node* cell) + { + const int x0 = dp_.grid_->gridX(cell->getOrigLeft()).v; + const int y0 = dp_.grid_->gridSnapDownY(cell->getOrigBottom()).v; + std::vector> candidates; + // Grid-bounded results fit in int; widen the displacement arithmetic. + const int xlow = std::max(0, int64_t{x0} - max_displacement_x_); + const int xhigh = std::min(dp_.grid_->getRowSiteCount().v - 1, + int64_t{x0} + max_displacement_x_); + const int ylow = std::max(0, int64_t{y0} - max_displacement_y_); + const int yhigh = std::min(dp_.grid_->getRowCount().v - 1, + int64_t{y0} + max_displacement_y_); + for (int y = ylow; y <= yhigh; ++y) { + for (int x = xlow; x <= xhigh; ++x) { + const int64_t distance + = std::abs(int64_t{x} - dp_.grid_->gridX(cell).v) + * dp_.grid_->getSiteWidth().v + + std::abs(int64_t{dp_.grid_->gridYToDbu(GridY{y}).v} + - cell->getBottom().v); + candidates.emplace_back(distance, y, x); + } + } + std::ranges::sort(candidates); + return candidates; + } + + bool legalCandidate(Node* cell, GridX gx, GridY gy, odb::dbOrientType orient) + { + const auto site_orient + = dp_.grid_->getSiteOrientation(gx, gy, cell->getSite()); + return site_orient + && (*site_orient == orient + || (!orient.isRightAngleRotation() + && *site_orient == orient.flipY())) + && dp_.checkPixels(cell, + gx, + gy, + gx + dp_.grid_->gridWidth(cell), + dp_.grid_->gridEndY(dp_.grid_->gridYToDbu(gy) + + cell->getHeight()), + &orient); + } + + void search(Node* cell, std::vector& moves) + { + if (cell->isFixed()) { + return; + } + auto* inst = cell->getDbInst(); + const GridPt old{dp_.grid_->gridX(cell), dp_.grid_->gridSnapDownY(cell)}; + const auto orient = cell->getOrient(); + const auto candidates = candidateSites(cell); + // Record the active trial so query errors restore the entire batch. + moves.push_back({ + .cell = cell, + .old = old, + .location = inst->getLocation(), + .orient = orient, + }); + dp_.unplaceCell(cell); + for (const auto& candidate : candidates) { + const GridX gx{std::get<2>(candidate)}; + const GridY gy{std::get<1>(candidate)}; + if (gx == old.x && gy == old.y) { + continue; + } + if (!legalCandidate(cell, gx, gy, orient)) { + continue; + } + const odb::Point location( + dp_.core_.xMin() + gx.v * dp_.grid_->getSiteWidth().v, + dp_.core_.yMin() + dp_.grid_->gridYToDbu(gy).v); + if (!pa_->hasUsablePinAccess(inst, location)) { + continue; + } + dp_.placeCell(cell, gx, gy); + cell->setOrient(orient); + return; + } + dp_.placeCell(cell, old.x, old.y); + cell->setOrient(orient); + moves.pop_back(); + } + + void restorePlacement(const std::vector& moves) + { + // A later move may occupy an earlier cell's source site. + for (const Move& move : moves) { + dp_.unplaceCell(move.cell); + } + for (const Move& move : moves) { + dp_.placeCell(move.cell, move.old.x, move.old.y); + move.cell->setOrient(move.orient); + move.cell->getDbInst()->setLocation(move.location.x(), move.location.y()); + } + } + + Opendp& dp_; + drt::PinAccessService* pa_; + int max_displacement_x_; + int max_displacement_y_; + std::vector cells_; +}; + +int Opendp::repairPinAccess(drt::PinAccessService* pa, + const int max_displacement_x, + const int max_displacement_y, + const int max_rounds) +{ + if (pa == nullptr || !pa->hasPinAccess()) { + logger_->error( + utl::DPL, + 510, + "repair_pin_access requires successful current live pin access."); + } + if (max_displacement_x < 0 || max_displacement_y < 0 || max_rounds < 1) { + logger_->error(utl::DPL, + 511, + "Pin access repair requires nonnegative displacement and " + "positive rounds."); + } + if (db_->getChip() == nullptr || db_->getChip()->getBlock() == nullptr) { + logger_->error(utl::DPL, 512, "Pin access repair requires a loaded block."); + } + if (db_->getChip()->getBlock()->getRows().empty()) { + logger_->error(utl::DPL, 515, "Pin access repair requires placement rows."); + } + pa->updateDirtyPinAccess(); + try { + // Fixed physical cells can lie outside placement rows. + checkPlacement(false, "", false, true); + const int remaining + = PinAccessRepair(*this, pa, max_displacement_x, max_displacement_y) + .run(max_rounds); + grid_->releasePixels(); + return remaining; + } catch (...) { + grid_->releasePixels(); + throw; + } +} + +} // namespace dpl diff --git a/openroad/dpl/src/Place.cpp b/openroad/dpl/src/Place.cpp index d4b052ef520..8878a7c7d85 100644 --- a/openroad/dpl/src/Place.cpp +++ b/openroad/dpl/src/Place.cpp @@ -1035,7 +1035,8 @@ bool Opendp::checkPixels(const Node* cell, const GridX x, const GridY y, const GridX x_end, - const GridY y_end) const + const GridY y_end, + const odb::dbOrientType* candidate_orient) const { if (x_end > grid_->getRowSiteCount()) { return false; @@ -1052,12 +1053,18 @@ bool Opendp::checkPixels(const Node* cell, if (pixel == nullptr || pixel->cell || !pixel->is_valid || (cell->inGroup() && pixel->group != cell->getGroup()) || (!cell->inGroup() && pixel->group) - || (first_row && !grid_->getSiteOrientation(x1, y1, site))) { + || (first_row && x1 != x + && !grid_->getSiteOrientation(x1, y1, site))) { return false; } } } + const auto site_orient = grid_->getSiteOrientation(x, y, site); + if (!site_orient) { + return false; + } + if (disallow_one_site_gaps_) { // here we need to check for abutting first, if there is an abutting // cell then we continue as there is nothing wrong with it if there is @@ -1106,7 +1113,8 @@ bool Opendp::checkPixels(const Node* cell, } } - const auto orient = grid_->getSiteOrientation(x, y, site).value(); + const auto orient + = candidate_orient == nullptr ? site_orient.value() : *candidate_orient; // Check for symmetry auto* dbMaster = cell->getDbInst()->getMaster(); diff --git a/openroad/dpl/test/BUILD b/openroad/dpl/test/BUILD index 9d4a16d294d..c3a1b76364e 100644 --- a/openroad/dpl/test/BUILD +++ b/openroad/dpl/test/BUILD @@ -110,6 +110,9 @@ PASSFAIL_TESTS = [ "pdn_via_cut", "pdn_via_euclidean", "pdn_via_mirror", + "repair_pin_access", + "repair_pin_access_failure", + "repair_pin_access_phase", ] ALL_TESTS = COMPULSORY_TESTS + PASSFAIL_TESTS @@ -333,6 +336,12 @@ filegroup( "regions3": [ "Nangate45/fake_macros.lef", ], + "repair_pin_access": ["repair_pin_access_tech.lef"], + "repair_pin_access_failure": ["repair_pin_access_tech.lef"], + "repair_pin_access_phase": [ + "repair_pin_access_tech.lef", + "repair_pin_access_failure.def", + ], }.get(test_name, []), ) for test_name in ALL_TESTS @@ -352,7 +361,10 @@ filegroup( cc_test( name = "dpl_unittest", - srcs = ["dpl_test.cc"], + srcs = [ + "PinAccessRepairTest.cc", + "dpl_test.cc", + ], data = [ "extra.lef", "gcd_replace.def", @@ -360,6 +372,7 @@ cc_test( deps = [ "//openroad/dpl", "//openroad/dpl:dpl_impl", + "//openroad/drt:pin_access_service", "//openroad/odb/src/db", "//openroad/odb/src/lefin", "//openroad/tst", diff --git a/openroad/dpl/test/PinAccessRepairTest.cc b/openroad/dpl/test/PinAccessRepairTest.cc new file mode 100644 index 00000000000..5ba21305b78 --- /dev/null +++ b/openroad/dpl/test/PinAccessRepairTest.cc @@ -0,0 +1,287 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2026, The OpenROAD Authors + +#include +#include + +#include "dpl/Opendp.h" +#include "drt/PinAccessService.h" +#include "gtest/gtest.h" +#include "infrastructure/Grid.h" +#include "odb/db.h" +#include "tst/nangate45_fixture.h" + +namespace dpl { +namespace { + +class RepairService : public drt::PinAccessService +{ + public: + bool hasPinAccess() const override { return ready; } + bool hasUsablePinAccess(odb::dbInst* inst, + const odb::Point& location) const override + { + return usable(inst, location); + } + std::vector findPinAccessFailures( + const std::vector& instances) const override + { + std::vector failures; + for (auto* inst : instances) { + if (!hasUsablePinAccess(inst, inst->getLocation())) { + failures.push_back(inst); + } + } + return failures; + } + void updateDirtyPinAccess() override + { + ++updates; + if (refresh) { + refresh(); + } + } + bool ready = true; + int updates = 0; + std::function usable; + std::function refresh; +}; + +class PinAccessRepairTest : public tst::Nangate45Fixture +{ + protected: + static constexpr int kSite = 380; + static constexpr int kHeight = 2800; + static constexpr int kX = 10 * kSite; + + void SetUp() override + { + auto* site = lib_->findSite("FreePDK45_38x28_10R_NP_162NW_34O"); + ASSERT_NE(site, nullptr); + block_->setDieArea({0, 0, 32 * kSite, kHeight}); + odb::dbRow::create(block_, + "ROW", + site, + 0, + 0, + odb::dbOrientType::R0, + odb::dbRowDir::HORIZONTAL, + 32, + kSite); + block_->setCoreArea(block_->computeCoreArea()); + auto* master = odb::dbMaster::create(lib_, "REPAIR_CELL"); + master->setType(odb::dbMasterType::CORE); + master->setWidth(kSite); + master->setHeight(kHeight); + master->setSite(site); + master->setSymmetryY(); + master->setFrozen(); + inst_ = makeCell("target", kX); + service_.usable = [&](odb::dbInst* inst, const odb::Point& location) { + return inst != inst_ || location.x() != kX; + }; + } + + odb::dbInst* makeCell(const char* name, int x) + { + return makeInst(block_, + lib_->findMaster("REPAIR_CELL"), + name, + {.location = {x, 0}, + .status = odb::dbPlacementStatus::PLACED, + .iterms = {}}); + } + + int repair(int dx = 1, int rounds = 3) + { + return dp_.repairPinAccess(&service_, dx, 0, rounds); + } + + odb::dbInst* inst_; + RepairService service_; + Opendp dp_{db_.get(), &logger_}; +}; + +TEST_F(PinAccessRepairTest, RequiresLivePinAccess) +{ + service_.ready = false; + EXPECT_THROW(repair(), std::runtime_error); + EXPECT_THROW(dp_.repairPinAccess(nullptr, 1, 0, 1), std::runtime_error); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(service_.updates, 0); +} + +TEST_F(PinAccessRepairTest, UsesNearestLegalSiteAndPreservesOrientation) +{ + inst_->setOrient(odb::dbOrientType::MY); + inst_->setLocation(kX, 0); + EXPECT_EQ(repair(), 0); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX - kSite, 0)); + EXPECT_EQ(inst_->getOrient(), odb::dbOrientType::MY); + EXPECT_EQ(service_.updates, 2); + EXPECT_EQ(repair(), 0); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX - kSite, 0)); +} + +TEST_F(PinAccessRepairTest, VerticalMovesRespectRowOrientationAndDisplacement) +{ + auto* site = inst_->getMaster()->getSite(); + block_->setDieArea({0, 0, 32 * kSite, 3 * kHeight}); + for (int row = 1; row <= 2; ++row) { + odb::dbRow::create(block_, + row == 1 ? "ROW1" : "ROW2", + site, + 0, + row * kHeight, + row == 1 ? odb::dbOrientType::MX : odb::dbOrientType::R0, + odb::dbRowDir::HORIZONTAL, + 32, + kSite); + } + block_->setCoreArea(block_->computeCoreArea()); + service_.usable = [](odb::dbInst*, const odb::Point& location) { + return location.y() != 0; + }; + EXPECT_EQ(dp_.repairPinAccess(&service_, 0, 1, 3), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(dp_.repairPinAccess(&service_, 0, 2, 3), 0); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 2 * kHeight)); + EXPECT_EQ(inst_->getOrient(), odb::dbOrientType::R0); +} + +TEST_F(PinAccessRepairTest, FixedCellsAndHealthyCellsStayPut) +{ + inst_->setPlacementStatus(odb::dbPlacementStatus::FIRM); + auto* healthy = makeCell("healthy", kX + 2 * kSite); + EXPECT_EQ(repair(), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(healthy->getLocation(), odb::Point(kX + 2 * kSite, 0)); +} + +TEST_F(PinAccessRepairTest, ZeroDisplacementAndMissingAccessPreservePlacement) +{ + EXPECT_EQ(repair(0), 1); + service_.usable = [](odb::dbInst*, const odb::Point&) { return false; }; + EXPECT_EQ(repair(), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); +} + +TEST_F(PinAccessRepairTest, PlacementOccupancyBoundsCandidateSearch) +{ + auto* left = makeCell("left", kX - kSite); + auto* right = makeCell("right", kX + kSite); + left->setPlacementStatus(odb::dbPlacementStatus::FIRM); + EXPECT_EQ(repair(), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(left->getLocation(), odb::Point(kX - kSite, 0)); + EXPECT_EQ(right->getLocation(), odb::Point(kX + kSite, 0)); +} + +TEST_F(PinAccessRepairTest, PaddingBoundsCandidateSearch) +{ + makeCell("left", kX - 2 * kSite); + makeCell("right", kX + 2 * kSite); + dp_.setPaddingGlobal(1, 0); + EXPECT_EQ(repair(), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); +} + +TEST_F(PinAccessRepairTest, RegionBoundsCandidateSearch) +{ + auto* region = odb::dbRegion::create(block_, "region"); + odb::dbBox::create(region, kX, 0, kX + kSite, kHeight); + auto* group = odb::dbGroup::create(region, "group"); + group->addInst(inst_); + EXPECT_EQ(repair(), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); +} + +TEST_F(PinAccessRepairTest, RejectsIllegalInputPlacement) +{ + inst_->setLocation(kX + kSite / 2, 0); + EXPECT_THROW(repair(), std::runtime_error); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX + kSite / 2, 0)); +} + +TEST_F(PinAccessRepairTest, AcceptsNonemptyProperSubset) +{ + auto* right = makeCell("right", kX + 4 * kSite); + auto* left = makeCell("left", kX - 4 * kSite); + right->setPlacementStatus(odb::dbPlacementStatus::FIRM); + left->setPlacementStatus(odb::dbPlacementStatus::FIRM); + service_.usable = [&](odb::dbInst* inst, const odb::Point& location) { + return inst == inst_ && location.x() != kX; + }; + EXPECT_EQ(repair(), 2); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX - kSite, 0)); + EXPECT_EQ(right->getLocation(), odb::Point(kX + 4 * kSite, 0)); + EXPECT_EQ(left->getLocation(), odb::Point(kX - 4 * kSite, 0)); +} + +TEST_F(PinAccessRepairTest, RejectsNewHealthyNeighborFailure) +{ + auto* second = makeCell("second", kX + 6 * kSite); + auto* healthy = makeCell("healthy", kX + 3 * kSite); + service_.usable = [&](odb::dbInst* inst, const odb::Point& location) { + if (inst == healthy) { + return inst_->getLocation().x() == kX; + } + return location.x() != (inst == second ? kX + 6 * kSite : kX); + }; + EXPECT_EQ(repair(), 2); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(second->getLocation(), odb::Point(kX + 6 * kSite, 0)); + EXPECT_EQ(healthy->getLocation(), odb::Point(kX + 3 * kSite, 0)); + EXPECT_EQ(service_.updates, 3); +} + +TEST_F(PinAccessRepairTest, RejectsNonimprovingBatch) +{ + service_.usable = [&](odb::dbInst*, const odb::Point& location) { + return inst_->getLocation().x() == kX && location.x() != kX; + }; + EXPECT_EQ(repair(), 1); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(service_.updates, 3); +} + +TEST_F(PinAccessRepairTest, QueryExceptionRestoresActiveTrial) +{ + service_.usable = [&](odb::dbInst*, const odb::Point& location) { + if (location.x() != kX) { + throw std::runtime_error("query failed"); + } + return false; + }; + EXPECT_THROW(repair(), std::runtime_error); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); +} + +TEST_F(PinAccessRepairTest, RefreshExceptionRestoresPlacement) +{ + service_.refresh = [&] { + if (service_.updates == 2) { + throw std::runtime_error("refresh failed"); + } + }; + EXPECT_THROW(repair(), std::runtime_error); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); + EXPECT_EQ(service_.updates, 3); +} + +TEST_F(PinAccessRepairTest, RejectsInvalidOptions) +{ + EXPECT_THROW(repair(-1), std::runtime_error); + EXPECT_THROW(repair(1, 0), std::runtime_error); + EXPECT_EQ(inst_->getLocation(), odb::Point(kX, 0)); +} + +} // namespace + +TEST_F(PinAccessRepairTest, ReleasesGridPixels) +{ + EXPECT_EQ(repair(), 0); + EXPECT_TRUE(dp_.grid_->pixelsReleased()); +} + +} // namespace dpl diff --git a/openroad/dpl/test/dpl_readme_msgs_check.ok b/openroad/dpl/test/dpl_readme_msgs_check.ok index 8121a052850..e3cbbbc0970 100644 --- a/openroad/dpl/test/dpl_readme_msgs_check.ok +++ b/openroad/dpl/test/dpl_readme_msgs_check.ok @@ -1,5 +1,5 @@ README.md -Names: 7, Desc: 7, Syn: 7, Options: 7, Args: 7 +Names: 8, Desc: 8, Syn: 8, Options: 8, Args: 8 Global Examples: None Global See Also: None Man2 successfully compiled. diff --git a/openroad/dpl/test/repair_pin_access.def b/openroad/dpl/test/repair_pin_access.def new file mode 100644 index 00000000000..7070f571ad8 --- /dev/null +++ b/openroad/dpl/test/repair_pin_access.def @@ -0,0 +1,35 @@ +VERSION 5.8 ; +DIVIDERCHAR "/" ; +BUSBITCHARS "[]" ; +DESIGN pdn_first_hop ; +UNITS DISTANCE MICRONS 1000 ; +DIEAREA ( 0 0 ) ( 8000 1000 ) ; +ROW ROW_0 core_site 0 0 N DO 20 BY 1 STEP 400 0 ; +TRACKS X 0 DO 40 STEP 200 LAYER M1 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M1 ; +TRACKS X 0 DO 20 STEP 400 LAYER M2 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M2 ; +TRACKS X 0 DO 40 STEP 200 LAYER M3 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M3 ; +COMPONENTS 2 ; + - u phase_cell + PLACED ( 0 0 ) N ; + - v phase_cell + PLACED ( 4000 0 ) N ; +END COMPONENTS +PINS 2 ; + - OUT + NET n + DIRECTION OUTPUT + USE SIGNAL + + PORT + LAYER M2 ( -50 -100 ) ( 50 100 ) + + PLACED ( 1600 500 ) N ; + - OUT2 + NET n2 + DIRECTION OUTPUT + USE SIGNAL + + PORT + LAYER M2 ( -50 -100 ) ( 50 100 ) + + PLACED ( 5600 500 ) N ; +END PINS +NETS 2 ; + - n ( PIN OUT ) ( u A ) + USE SIGNAL ; + - n2 ( PIN OUT2 ) ( v A ) + USE SIGNAL ; +END NETS +SPECIALNETS 1 ; + - VDD + USE POWER + + ROUTED M2 0 + SHAPE STRIPE ( 400 500 ) V23 + NEW M2 0 + SHAPE STRIPE ( 4400 500 ) V23 ; +END SPECIALNETS +END DESIGN diff --git a/openroad/dpl/test/repair_pin_access.lef b/openroad/dpl/test/repair_pin_access.lef new file mode 100644 index 00000000000..37490b086dd --- /dev/null +++ b/openroad/dpl/test/repair_pin_access.lef @@ -0,0 +1,24 @@ +VERSION 5.8 ; +BUSBITCHARS "[]" ; +DIVIDERCHAR "/" ; +SITE core_site + CLASS CORE ; + SYMMETRY Y ; + SIZE 0.4 BY 1.0 ; +END core_site +MACRO phase_cell + CLASS CORE ; + ORIGIN 0 0 ; + SIZE 1.2 BY 1.0 ; + SYMMETRY X Y ; + SITE core_site ; + PIN A + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M1 ; + RECT 0.35 0.4 0.45 0.6 ; + END + END A +END phase_cell +END LIBRARY diff --git a/openroad/dpl/test/repair_pin_access.tcl b/openroad/dpl/test/repair_pin_access.tcl new file mode 100644 index 00000000000..a663c460260 --- /dev/null +++ b/openroad/dpl/test/repair_pin_access.tcl @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2026, The OpenROAD Authors + +source "helpers.tcl" +set_thread_count 2 +read_lef repair_pin_access_tech.lef +read_lef repair_pin_access.lef +read_def repair_pin_access.def +set_routing_layers -signal M2-M3 +check_placement +set block [ord::get_db_block] +set inst [$block findInst u] +set second_inst [$block findInst v] + +proc full_pin_access { } { + pin_access -via_access_layer M1 -via_in_pin_bottom_layer M1 \ + -via_in_pin_top_layer M1 -min_access_points 1 -verbose 0 +} + +check_error "reject before pin access" {repair_pin_access} DPL-0510 +check "missing PA preserves placement" {$inst getLocation} "0 0" +full_pin_access +check "zero displacement preserves unresolved cells" { + repair_pin_access -max_displacement {0 0} +} 2 +check "zero displacement preserves placement" {$inst getLocation} "0 0" +check "repair resolves fixed supply escape conflicts" {repair_pin_access} 0 +set repaired_location [$inst getLocation] +set second_location [$second_inst getLocation] +check "repair moves first affected cell" {expr {$repaired_location ne "0 0"}} 1 +check "repair moves second affected cell" {expr {$second_location ne "4000 0"}} 1 +check_placement +lassign $repaired_location ix iy +set aps [[$inst findITerm A] getPrefAccessPoints] +check "refreshed preferred witness" {llength $aps} 1 +lassign [[lindex $aps 0] getPoint] ax ay +check "first-hop witness clears fixed via" { + expr {abs($ix + $ax - 400) >= 220 || abs($iy + $ay - 500) >= 220} +} 1 +check "clean repair result" {repair_pin_access} 0 +check "clean repair preserves placement" {$inst getLocation} $repaired_location +check "clean repair preserves second cell" {$second_inst getLocation} $second_location + +$inst setLocation 0 0 +$inst setPlacementStatus FIRM +full_pin_access +check "fixed conflict remains unresolved" {repair_pin_access} 1 +check "fixed instance does not move" {$inst getLocation} "0 0" + +foreach {arguments error_id} { + {-max_displacement 1} DPL-0350 + {-max_displacement {1 -1}} DPL-0349 + {-max_displacement {1 1.5}} DPL-0349 + {-max_rounds 0} DPL-0351 + {-max_rounds wrong} DPL-0351 +} { + check_error "invalid repair arguments $arguments" {repair_pin_access {*}$arguments} $error_id +} + +set saved_db [make_result_file repair_pin_access.odb] +write_db $saved_db +odb::dbChip_destroy [[ord::get_db] getChip] +read_db $saved_db +check_error "reject loaded stale pin access" {repair_pin_access} DPL-0510 +exit_summary diff --git a/openroad/dpl/test/repair_pin_access_failure.def b/openroad/dpl/test/repair_pin_access_failure.def new file mode 100644 index 00000000000..c1b70a1f16f --- /dev/null +++ b/openroad/dpl/test/repair_pin_access_failure.def @@ -0,0 +1,35 @@ +VERSION 5.8 ; +DIVIDERCHAR "/" ; +BUSBITCHARS "[]" ; +DESIGN pdn_first_hop ; +UNITS DISTANCE MICRONS 1000 ; +DIEAREA ( 0 0 ) ( 8000 1000 ) ; +ROW ROW_0 core_site 0 0 N DO 80 BY 1 STEP 100 0 ; +TRACKS X 0 DO 40 STEP 200 LAYER M1 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M1 ; +TRACKS X 0 DO 20 STEP 400 LAYER M2 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M2 ; +TRACKS X 0 DO 40 STEP 200 LAYER M3 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M3 ; +COMPONENTS 2 ; + - u phase_cell + PLACED ( 400 0 ) N ; + - v phase_cell + PLACED ( 4400 0 ) N ; +END COMPONENTS +PINS 2 ; + - OUT + NET n + DIRECTION OUTPUT + USE SIGNAL + + PORT + LAYER M2 ( -50 -100 ) ( 50 100 ) + + PLACED ( 1600 500 ) N ; + - OUT2 + NET n2 + DIRECTION OUTPUT + USE SIGNAL + + PORT + LAYER M2 ( -50 -100 ) ( 50 100 ) + + PLACED ( 5600 500 ) N ; +END PINS +NETS 2 ; + - n ( PIN OUT ) ( u A ) + USE SIGNAL ; + - n2 ( PIN OUT2 ) ( v A ) + USE SIGNAL ; +END NETS +SPECIALNETS 1 ; + - VDD + USE POWER + + ROUTED M2 0 + SHAPE STRIPE ( 800 500 ) V23 + NEW M2 0 + SHAPE STRIPE ( 4800 500 ) V23 ; +END SPECIALNETS +END DESIGN diff --git a/openroad/dpl/test/repair_pin_access_failure.lef b/openroad/dpl/test/repair_pin_access_failure.lef new file mode 100644 index 00000000000..3a34d10cade --- /dev/null +++ b/openroad/dpl/test/repair_pin_access_failure.lef @@ -0,0 +1,32 @@ +VERSION 5.8 ; +BUSBITCHARS "[]" ; +DIVIDERCHAR "/" ; +SITE core_site + CLASS CORE ; + SYMMETRY Y ; + SIZE 0.1 BY 1.0 ; +END core_site +MACRO phase_cell + CLASS CORE ; + ORIGIN 0 0 ; + SIZE 1.2 BY 1.0 ; + SYMMETRY X Y ; + SITE core_site ; + PIN A + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M1 ; + RECT 0.35 0.4 0.45 0.6 ; + END + END A + PIN B + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M1 ; + RECT 0.75 0.2 1.05 0.4 ; + END + END B +END phase_cell +END LIBRARY diff --git a/openroad/dpl/test/repair_pin_access_failure.tcl b/openroad/dpl/test/repair_pin_access_failure.tcl new file mode 100644 index 00000000000..ae4160dffc0 --- /dev/null +++ b/openroad/dpl/test/repair_pin_access_failure.tcl @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2026, The OpenROAD Authors + +source "helpers.tcl" +set_thread_count 2 +read_lef repair_pin_access_tech.lef +read_lef repair_pin_access_failure.lef +read_def repair_pin_access_failure.def +set_routing_layers -signal M2-M3 +check_placement +pin_access -via_access_layer M1 -via_in_pin_bottom_layer M1 \ + -via_in_pin_top_layer M1 -min_access_points 1 -verbose 0 +set block [ord::get_db_block] +set inst [$block findInst u] +set initial_ap [lindex [[$inst findITerm A] getPrefAccessPoints] 0] +set initial_point [$initial_ap getPoint] + +# Off-phase sites cannot provide an AP for the narrow pin. +check "bounded failure rejects sites without native access" { + repair_pin_access -max_displacement {3 0} +} 2 +check "bounded failure preserves first cell" {$inst getLocation} "400 0" +check "bounded failure preserves second cell" {[$block findInst v] getLocation} "4400 0" +check "candidate queries preserve published preferred AP" { + [lindex [[$inst findITerm A] getPrefAccessPoints] 0] getPoint +} $initial_point +check "repeated bounded failure remains usable" { + repair_pin_access -max_displacement {3 0} +} 2 +check_placement +exit_summary diff --git a/openroad/dpl/test/repair_pin_access_phase.lef b/openroad/dpl/test/repair_pin_access_phase.lef new file mode 100644 index 00000000000..394057f9723 --- /dev/null +++ b/openroad/dpl/test/repair_pin_access_phase.lef @@ -0,0 +1,24 @@ +VERSION 5.8 ; +BUSBITCHARS "[]" ; +DIVIDERCHAR "/" ; +SITE core_site + CLASS CORE ; + SYMMETRY Y ; + SIZE 0.1 BY 1.0 ; +END core_site +MACRO phase_cell + CLASS CORE ; + ORIGIN 0 0 ; + SIZE 1.2 BY 1.0 ; + SYMMETRY X Y ; + SITE core_site ; + PIN A + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M1 ; + RECT 0.15 0.4 0.65 0.6 ; + END + END A +END phase_cell +END LIBRARY diff --git a/openroad/dpl/test/repair_pin_access_phase.tcl b/openroad/dpl/test/repair_pin_access_phase.tcl new file mode 100644 index 00000000000..8be66d76063 --- /dev/null +++ b/openroad/dpl/test/repair_pin_access_phase.tcl @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2026, The OpenROAD Authors + +source "helpers.tcl" +set_thread_count 2 +read_lef repair_pin_access_tech.lef +read_lef repair_pin_access_phase.lef +read_def repair_pin_access_failure.def +set_routing_layers -signal M2-M3 +check_placement +pin_access -via_access_layer M1 -via_in_pin_bottom_layer M1 \ + -via_in_pin_top_layer M1 -min_access_points 1 -verbose 0 + +set inst [[ord::get_db_block] findInst u] +set initial_ap [lindex [[$inst findITerm A] getPrefAccessPoints] 0] +set initial_point [$initial_ap getPoint] +check "phase-changing repair resolves conflicts" { + repair_pin_access -max_displacement {3 0} +} 0 +check_placement +lassign [$inst getLocation] ix iy +check "repair creates a new track phase" {expr {$ix % 400 != 0}} 1 +set refreshed_ap [lindex [[$inst findITerm A] getPrefAccessPoints] 0] +check "native AP uses the new class" {expr {[$refreshed_ap getPoint] ne $initial_point}} 1 +lassign [$refreshed_ap getPoint] ax ay +check "refreshed phase clears fixed supply via" { + expr {abs($ix + $ax - 800) >= 220 || abs($iy + $ay - 500) >= 220} +} 1 +check "phase-changing repair is stable" {repair_pin_access} 0 +exit_summary diff --git a/openroad/dpl/test/repair_pin_access_tech.lef b/openroad/dpl/test/repair_pin_access_tech.lef new file mode 100644 index 00000000000..bbc2929e717 --- /dev/null +++ b/openroad/dpl/test/repair_pin_access_tech.lef @@ -0,0 +1,64 @@ +VERSION 5.8 ; +BUSBITCHARS "[]" ; +DIVIDERCHAR "/" ; +UNITS + DATABASE MICRONS 1000 ; +END UNITS +MANUFACTURINGGRID 0.01 ; +PROPERTYDEFINITIONS + LAYER LEF58_RIGHTWAYONGRIDONLY STRING ; +END PROPERTYDEFINITIONS +LAYER masterslice + TYPE MASTERSLICE ; +END masterslice +LAYER v0 + TYPE CUT ; + SPACING 0.04 ; +END v0 +LAYER M1 + TYPE ROUTING ; + DIRECTION HORIZONTAL ; + PITCH 0.2 ; + WIDTH 0.1 ; + SPACING 0.1 ; +END M1 +LAYER V1 + TYPE CUT ; + SPACING 0.04 ; +END V1 +LAYER M2 + TYPE ROUTING ; + DIRECTION VERTICAL ; + PITCH 0.4 ; + WIDTH 0.1 ; + SPACING 0.1 ; + PROPERTY LEF58_RIGHTWAYONGRIDONLY "RIGHTWAYONGRIDONLY ;" ; +END M2 +VIA V12 DEFAULT + LAYER M1 ; + RECT -0.05 -0.05 0.05 0.05 ; + LAYER V1 ; + RECT -0.02 -0.02 0.02 0.02 ; + LAYER M2 ; + RECT -0.05 -0.05 0.05 0.05 ; +END V12 +LAYER V2 + TYPE CUT ; + SPACING 0.04 ; +END V2 +LAYER M3 + TYPE ROUTING ; + DIRECTION HORIZONTAL ; + PITCH 0.2 ; + WIDTH 0.1 ; + SPACING 0.1 ; +END M3 +VIA V23 DEFAULT + LAYER M2 ; + RECT -0.07 -0.07 0.07 0.07 ; + LAYER V2 ; + RECT -0.02 -0.02 0.02 0.02 ; + LAYER M3 ; + RECT -0.05 -0.05 0.05 0.05 ; +END V23 +END LIBRARY diff --git a/openroad/drt/include/drt/PinAccessService.h b/openroad/drt/include/drt/PinAccessService.h index 334a697875b..badf8011ad6 100644 --- a/openroad/drt/include/drt/PinAccessService.h +++ b/openroad/drt/include/drt/PinAccessService.h @@ -3,6 +3,13 @@ #pragma once +#include + +namespace odb { +class dbInst; +class Point; +} // namespace odb + namespace drt { // Service interface published by the detailed router. Consumers look @@ -13,6 +20,18 @@ class PinAccessService public: virtual ~PinAccessService() = default; + // Successful full pin access is available for the current live block. + virtual bool hasPinAccess() const = 0; + // Check required pins at the candidate lower-left without changing state. + // Tests native PA escapes and signal-pin supply overlap, not full placement + // DRC. + virtual bool hasUsablePinAccess(odb::dbInst* inst, + const odb::Point& candidate_location) const + = 0; + // Return failures at current lower-left locations, preserving input order. + virtual std::vector findPinAccessFailures( + const std::vector& instances) const = 0; + // Recompute pin access for any instances that have become dirty. virtual void updateDirtyPinAccess() = 0; }; diff --git a/openroad/drt/include/drt/TritonRoute.h b/openroad/drt/include/drt/TritonRoute.h index 574ffe08346..b5e948e08f4 100644 --- a/openroad/drt/include/drt/TritonRoute.h +++ b/openroad/drt/include/drt/TritonRoute.h @@ -20,6 +20,7 @@ namespace odb { class dbDatabase; +class dbBlock; class dbInst; class dbBTerm; class dbNet; @@ -91,6 +92,11 @@ class TritonRoute : public PinAccessService ~TritonRoute() override; void updateDirtyPinAccess() override; + bool hasPinAccess() const override; + bool hasUsablePinAccess(odb::dbInst* inst, + const odb::Point& candidate_location) const override; + std::vector findPinAccessFailures( + const std::vector& instances) const override; void initGraphics(std::unique_ptr graphics_factory); @@ -156,7 +162,7 @@ class TritonRoute : public PinAccessService void debugSingleWorker(const std::string& dumpDir, const std::string& drcRpt); void updateGlobals(const char* file_name); void resetDb(const char* file_name); - void clearDesign(); + void clearDesign(bool detach_callback = true); void updateDesign(const std::vector& updates, int num_threads); void updateDesign(const std::string& path, int num_threads); void addWorkerResults( @@ -211,6 +217,8 @@ class TritonRoute : public PinAccessService unsigned int cloud_sz_{0}; std::optional dist_pool_; std::unique_ptr pa_{nullptr}; + bool pa_rebuild_required_{false}; + odb::dbBlock* pa_block_{nullptr}; std::unique_ptr graphics_factory_{nullptr}; void initDesign(); diff --git a/openroad/drt/src/DesignCallBack.cpp b/openroad/drt/src/DesignCallBack.cpp index cd8bced3eae..d51b63055f1 100644 --- a/openroad/drt/src/DesignCallBack.cpp +++ b/openroad/drt/src/DesignCallBack.cpp @@ -10,6 +10,11 @@ namespace drt { +void DesignCallBack::inDbBlockClear(odb::dbBlock*) +{ + router_->clearDesign(false); +} + void DesignCallBack::inDbInstCreate(odb::dbInst* db_inst) { auto design = router_->getDesign(); diff --git a/openroad/drt/src/DesignCallBack.h b/openroad/drt/src/DesignCallBack.h index a55768ab952..9a5729c1594 100644 --- a/openroad/drt/src/DesignCallBack.h +++ b/openroad/drt/src/DesignCallBack.h @@ -11,6 +11,7 @@ class DesignCallBack : public odb::dbBlockCallBackObj { public: DesignCallBack(TritonRoute* router) : router_(router) {} + void inDbBlockClear(odb::dbBlock*) override; void inDbInstCreate(odb::dbInst*) override; void inDbPreMoveInst(odb::dbInst* inst) override; void inDbPostMoveInst(odb::dbInst* inst) override; diff --git a/openroad/drt/src/TritonRoute.cpp b/openroad/drt/src/TritonRoute.cpp index 9b61f145da1..3e1a5b9b8fd 100644 --- a/openroad/drt/src/TritonRoute.cpp +++ b/openroad/drt/src/TritonRoute.cpp @@ -4,6 +4,8 @@ #include "drt/TritonRoute.h" #include +#include +#include #include #include #include @@ -61,6 +63,7 @@ #include "utl/Logger.h" #include "utl/ScopedTemporaryFile.h" #include "utl/ServiceRegistry.h" +#include "utl/exception.h" #include "utl/timer.h" using odb::dbTechLayerType; @@ -93,9 +96,68 @@ TritonRoute::~TritonRoute() service_registry_->withdraw(this); } +bool TritonRoute::hasPinAccess() const +{ + return pa_block_ && db_callback_->hasOwner() && db_->getChip() + && pa_block_ == db_->getChip()->getBlock() && pa_ + && !pa_rebuild_required_; +} + +bool TritonRoute::hasUsablePinAccess(odb::dbInst* inst, + const odb::Point& candidate_location) const +{ + return hasPinAccess() && inst && inst->getBlock() == pa_block_ + && pa_->hasUsablePinAccess(inst, candidate_location); +} + +std::vector TritonRoute::findPinAccessFailures( + const std::vector& instances) const +{ + if (instances.empty() || !hasPinAccess()) { + return instances; + } + std::vector locations(instances.size()); + std::vector failures(instances.size()); + for (size_t i = 0; i < instances.size(); ++i) { + auto* inst = instances[i]; + failures[i] = !inst || inst->getBlock() != pa_block_; + if (!failures[i]) { + locations[i] = inst->getLocation(); + } + } + const int num_threads = std::min( + std::max(1, router_cfg_->MAX_THREADS), instances.size()); + std::vector> workers; + workers.reserve(num_threads); + for (int i = 0; i < num_threads; ++i) { + workers.push_back(pa_->makeQueryWorker()); + } + utl::ThreadException exception; +#pragma omp parallel for num_threads(num_threads) schedule(dynamic) + for (size_t i = 0; i < instances.size(); ++i) { + try { + if (!failures[i]) { + failures[i] = !workers[omp_get_thread_num()]->hasUsablePinAccess( + instances[i], locations[i]); + } + } catch (...) { + exception.capture(); + } + } + exception.rethrow(); + std::vector result; + for (size_t i = 0; i < instances.size(); ++i) { + if (failures[i]) { + result.push_back(instances[i]); + } + } + return result; +} + void TritonRoute::updateDirtyPinAccess() { - if (design_ == nullptr || design_->getTopBlock() == nullptr) { + if (!pa_rebuild_required_ + && (design_ == nullptr || design_->getTopBlock() == nullptr)) { return; } updateDirtyPAData(); @@ -359,7 +421,7 @@ void TritonRoute::resetDb(const char* file_name) logger_->error( DRT, 9954, "odb file {} is invalid: {}", file_name, f.what()); } - design_ = std::make_unique(logger_, router_cfg_.get()); + clearDesign(); initDesign(); if (!db_->getChip()->getBlock()->getAccessPoints().empty()) { initGuide(); @@ -368,8 +430,14 @@ void TritonRoute::resetDb(const char* file_name) } } -void TritonRoute::clearDesign() +void TritonRoute::clearDesign(bool detach_callback) { + pa_.reset(); + pa_block_ = nullptr; + pa_rebuild_required_ = false; + if (detach_callback) { + db_callback_->removeOwner(); + } design_ = std::make_unique(logger_, router_cfg_.get()); } @@ -583,6 +651,7 @@ bool TritonRoute::initGuide() } void TritonRoute::initDesign() { + pa_block_ = nullptr; if (db_ == nullptr || db_->getChip() == nullptr || db_->getChip()->getBlock() == nullptr) { logger_->error(utl::DRT, 151, "Database, chip or block not initialized."); @@ -1129,6 +1198,9 @@ void TritonRoute::pinAccess(const std::vector& target_insts) pa_->main(); io::Writer writer(getDesign(), logger_); writer.updateDb(db_, router_cfg_.get(), true); + if (target_insts.empty()) { + pa_block_ = db_->getChip()->getBlock(); + } } void TritonRoute::deleteInstancePAData(frInst* inst, bool delete_inst) @@ -1156,11 +1228,21 @@ void TritonRoute::addAvoidViaDefPA(const frViaDef* via_def) } void TritonRoute::updateDirtyPAData() { - if (pa_) { - design_->getTopBlock()->removeDeletedObjects(); - pa_->updateDirtyInsts(); - io::Writer writer(getDesign(), logger_); - writer.updateDb(getDb(), getRouterConfiguration(), true); + try { + if (pa_rebuild_required_) { + pinAccess(); + return; + } + if (pa_) { + design_->getTopBlock()->removeDeletedObjects(); + pa_->updateDirtyInsts(); + io::Writer writer(getDesign(), logger_); + writer.updateDb(getDb(), getRouterConfiguration(), true); + } + } catch (...) { + pa_rebuild_required_ = true; + pa_block_ = nullptr; + throw; } } diff --git a/openroad/drt/src/db/obj/frInst.h b/openroad/drt/src/db/obj/frInst.h index dc9d66e7014..8cccf4667c2 100644 --- a/openroad/drt/src/db/obj/frInst.h +++ b/openroad/drt/src/db/obj/frInst.h @@ -89,7 +89,10 @@ class frInst : public frRef xform_ = xformIn; } odb::dbInst* getDBInst() const { return db_inst_; } - odb::dbTransform getDBTransform() const { return db_inst_->getTransform(); } + odb::dbTransform getDBTransform() const + { + return db_inst_ ? db_inst_->getTransform() : xform_; + } /* from frPinFig * hasPin diff --git a/openroad/drt/src/gc/FlexGC.cpp b/openroad/drt/src/gc/FlexGC.cpp index 31ef2de9c6e..a7ea3db8373 100644 --- a/openroad/drt/src/gc/FlexGC.cpp +++ b/openroad/drt/src/gc/FlexGC.cpp @@ -255,6 +255,12 @@ void FlexGCWorker::resetTargetNet() impl_->targetDRNet_ = nullptr; } +void FlexGCWorker::setReplacementInst(frInst* source, frInst* candidate) +{ + impl_->replacement_source_ = source; + impl_->replacement_candidate_ = candidate; +} + void FlexGCWorker::addTargetObj(frBlockObject* in) { impl_->targetObjs_.insert(in); diff --git a/openroad/drt/src/gc/FlexGC.h b/openroad/drt/src/gc/FlexGC.h index 8134b2c703e..4973d443a2d 100644 --- a/openroad/drt/src/gc/FlexGC.h +++ b/openroad/drt/src/gc/FlexGC.h @@ -40,6 +40,7 @@ class FlexGCWorker gcNet* getTargetNet(); void resetTargetNet(); void addTargetObj(frBlockObject* in); + void setReplacementInst(frInst* source, frInst* candidate); void setTargetObjs(const std::set& targetObjs); void setIgnoreDB(); void setIgnoreMinArea(); diff --git a/openroad/drt/src/gc/FlexGC_impl.h b/openroad/drt/src/gc/FlexGC_impl.h index d54eb42cdd9..2f841fa641d 100644 --- a/openroad/drt/src/gc/FlexGC_impl.h +++ b/openroad/drt/src/gc/FlexGC_impl.h @@ -156,6 +156,8 @@ class FlexGCWorker::Impl // for pin prep std::set targetObjs_; + frInst* replacement_source_ = nullptr; + frInst* replacement_candidate_ = nullptr; bool ignoreDB_; bool ignoreMinArea_; bool ignoreLongSideEOL_; diff --git a/openroad/drt/src/gc/FlexGC_init.cpp b/openroad/drt/src/gc/FlexGC_init.cpp index 6ae29142b98..4be58053f79 100644 --- a/openroad/drt/src/gc/FlexGC_init.cpp +++ b/openroad/drt/src/gc/FlexGC_init.cpp @@ -148,6 +148,13 @@ void FlexGCWorker::Impl::initObj(const odb::Rect& box, bool FlexGCWorker::Impl::initDesign_skipObj(frBlockObject* obj) { + if (replacement_source_) { + return (obj->typeId() == frcInstTerm + && static_cast(obj)->getInst() == replacement_source_) + || (obj->typeId() == frcInstBlockage + && static_cast(obj)->getInst() + == replacement_source_); + } if (targetObjs_.empty()) { return false; } @@ -199,6 +206,50 @@ void FlexGCWorker::Impl::initDesign(const frDesign* design, bool skipDR) initObj(box, i, obj, true); } } + // The live region query has no geometry for the trial placement. + if (replacement_candidate_) { + const auto transform = replacement_candidate_->getTransform(); + const auto import_pin = [&](const auto* pin, frBlockObject* owner) { + for (const auto& figure : pin->getFigs()) { + const auto layer = static_cast(figure.get())->getLayerNum(); + if (figure->typeId() == frcPolygon) { + std::vector> points; + for (auto point : + static_cast(figure.get())->getPoints()) { + transform.apply(point); + points.emplace_back(point.x(), point.y()); + } + gtl::polygon_90_data polygon; + polygon.set(points.begin(), points.end()); + gtl::polygon_90_set_data polygons; + polygons.insert(polygon); + std::vector> rectangles; + polygons.get_rectangles(rectangles); + for (const auto& rect : rectangles) { + const odb::Rect box( + gtl::xl(rect), gtl::yl(rect), gtl::xh(rect), gtl::yh(rect)); + if (box.intersects(extBox)) { + initObj(box, layer, owner, true); + } + } + } else { + auto box = figure->getBBox(); + transform.apply(box); + if (box.intersects(extBox)) { + initObj(box, layer, owner, true); + } + } + } + }; + for (const auto& term : replacement_candidate_->getInstTerms()) { + for (const auto& pin : term->getTerm()->getPins()) { + import_pin(pin.get(), term.get()); + } + } + for (const auto& blockage : replacement_candidate_->getInstBlockages()) { + import_pin(blockage->getBlockage()->getPin(), blockage.get()); + } + } // init all dr objs from design if (getDRWorker() || skipDR) { return; diff --git a/openroad/drt/src/pa/FlexPA.cpp b/openroad/drt/src/pa/FlexPA.cpp index b9a015eb8a3..bce69f16524 100644 --- a/openroad/drt/src/pa/FlexPA.cpp +++ b/openroad/drt/src/pa/FlexPA.cpp @@ -76,6 +76,15 @@ FlexPA::FlexPA(frDesign* in, // must be out-of-line due to the unique_ptr FlexPA::~FlexPA() = default; +std::unique_ptr FlexPA::makeQueryWorker() const +{ + auto worker = std::make_unique(design_, logger_, dist_, router_cfg_); + worker->track_coords_ = track_coords_; + worker->layer_num_to_via_defs_ = layer_num_to_via_defs_; + worker->avoid_via_defs_ = avoid_via_defs_; + return worker; +} + void FlexPA::setDebug(std::unique_ptr pa_graphics) { graphics_ = std::move(pa_graphics); @@ -164,16 +173,22 @@ void FlexPA::updateDirtyInsts() unique_inst_patterns_[unique_class] = std::vector>(); } + ThreadException unique_class_exception; #pragma omp parallel for schedule(dynamic) for (auto& unique_class : dirty_unique_classes_vec) { - initSkipInstTerm(unique_class); - auto candidate_inst = unique_class->getFirstInst(); - genInstAccessPoints(candidate_inst); - revertAccessPoints(candidate_inst); - if (isStdCell(candidate_inst)) { - prepPatternInst(candidate_inst); + try { + initSkipInstTerm(unique_class); + auto candidate_inst = unique_class->getFirstInst(); + genInstAccessPoints(candidate_inst); + revertAccessPoints(candidate_inst); + if (isStdCell(candidate_inst)) { + prepPatternInst(candidate_inst); + } + } catch (...) { + unique_class_exception.capture(); } } + unique_class_exception.rethrow(); for (auto& inst : dirty_insts_) { addToInstsSet(inst); } diff --git a/openroad/drt/src/pa/FlexPA.h b/openroad/drt/src/pa/FlexPA.h index b55710e566b..31cfd13fd17 100644 --- a/openroad/drt/src/pa/FlexPA.h +++ b/openroad/drt/src/pa/FlexPA.h @@ -127,13 +127,24 @@ class FlexPA void addToInstsSet(frInst* inst); int main(); + bool hasUsablePinAccess(odb::dbInst* inst, + const odb::Point& candidate_location); + std::unique_ptr makeQueryWorker() const; private: + bool signalPinsOverlapSupply(frInst* candidate); + frDesign* design_; utl::Logger* logger_; dst::Distributed* dist_; RouterConfiguration* router_cfg_; + struct QueryContext + { + frInst* source; + }; + QueryContext* query_ = nullptr; + std::unique_ptr graphics_; std::string debugPinName_; @@ -223,6 +234,11 @@ class FlexPA template int genPinAccess(T* pin, frInstTerm* inst_term = nullptr); + template + std::vector> generatePinAccess( + T* pin, + frInstTerm* inst_term); + /** * @brief determines if the current access points are enough to say PA is done * with this pin. diff --git a/openroad/drt/src/pa/FlexPA_acc_point.cpp b/openroad/drt/src/pa/FlexPA_acc_point.cpp index 84f2d8aadd3..f435fb76fc5 100644 --- a/openroad/drt/src/pa/FlexPA_acc_point.cpp +++ b/openroad/drt/src/pa/FlexPA_acc_point.cpp @@ -15,6 +15,7 @@ #include "db/infra/frSegStyle.h" #include "db/obj/frAccess.h" #include "db/obj/frBlockObject.h" +#include "db/obj/frInstBlockage.h" #include "db/obj/frInstTerm.h" #include "db/obj/frShape.h" #include "db/obj/frVia.h" @@ -22,7 +23,9 @@ #include "db/tech/frViaDef.h" #include "frBaseTypes.h" #include "frProfileTask.h" +#include "frRegionQuery.h" #include "gc/FlexGC.h" +#include "odb/db.h" #include "odb/dbTransform.h" #include "odb/dbTypes.h" #include "odb/geom.h" @@ -374,11 +377,11 @@ void FlexPA::createMultipleAccessPoints( } } const int new_aps_size = aps.size() - aps_size_before; - if (inst_term && isStdCell(inst_term->getInst())) { + if (!query_ && inst_term && isStdCell(inst_term->getInst())) { #pragma omp atomic std_cell_pin_gen_ap_cnt_ += new_aps_size; } - if (inst_term && isMacroCell(inst_term->getInst())) { + if (!query_ && inst_term && isMacroCell(inst_term->getInst())) { #pragma omp atomic macro_cell_pin_gen_ap_cnt_ += new_aps_size; } @@ -726,7 +729,10 @@ bool FlexPA::isPlanarViolationFree(frAccessPoint* ap, tmp_box.bloat(extension, ext_box); design_rule_checker.setExtBox(ext_box); design_rule_checker.setDrcBox(ext_box); - if (inst_term) { + if (query_) { + design_rule_checker.setReplacementInst(query_->source, + inst_term->getInst()); + } else if (inst_term) { design_rule_checker.addTargetObj(inst_term->getInst()); } else { design_rule_checker.addTargetObj(pin->getTerm()); @@ -754,7 +760,7 @@ bool FlexPA::isPlanarViolationFree(frAccessPoint* ap, design_rule_checker.initPA1(); design_rule_checker.main(); - if (graphics_) { + if (graphics_ && !query_) { graphics_->setPlanarAP(ap, ps, design_rule_checker.getMarkers()); } @@ -1080,7 +1086,10 @@ bool FlexPA::isViaViolationFree(frAccessPoint* ap, auto pin_net = pin_term->getNet(); design_rule_checker.setExtBox(ext_box); design_rule_checker.setDrcBox(ext_box); - if (inst_term) { + if (query_) { + design_rule_checker.setReplacementInst(query_->source, + inst_term->getInst()); + } else if (inst_term) { auto* it_net = inst_term->getNet(); if (!it_net || !it_net->hasNDR() || it_net->autoTaperEnabled(router_cfg_->AUTO_TAPER_NDR_NETS)) { @@ -1118,7 +1127,7 @@ bool FlexPA::isViaViolationFree(frAccessPoint* ap, const bool no_drv = design_rule_checker.getMarkers().empty(); - if (graphics_) { + if (graphics_ && !query_) { graphics_->setViaAP(ap, via, design_rule_checker.getMarkers()); } return no_drv; @@ -1317,7 +1326,7 @@ bool FlexPA::genPinAccessCostBounded( } filterMultipleAPAccesses( new_aps, pin_shapes, pin, inst_term, is_std_cell_pin); - if (graphics_) { + if (graphics_ && !query_) { graphics_->setAPs(new_aps, lower_type, upper_type); } for (auto& ap : new_aps) { @@ -1411,19 +1420,30 @@ FlexPA::mergePinShapes(T* pin, frInstTerm* inst_term, const bool is_shrink) template int FlexPA::genPinAccess(T* pin, frInstTerm* inst_term) { - // IO term pin always only have one access const int pin_access_idx = inst_term ? inst_term->getInst()->getPinAccessIdx() : 0; - if (pin->getPinAccess(pin_access_idx)->getNumAccessPoints() > 0) { - pin->getPinAccess(pin_access_idx)->clearAccessPoints(); + auto access = pin->getPinAccess(pin_access_idx); + access->clearAccessPoints(); + auto aps = generatePinAccess(pin, inst_term); + updatePinStats(aps, inst_term); + const int count = aps.size(); + for (auto& ap : aps) { + access->addAccessPoint(std::move(ap)); } + return count; +} +template +std::vector> FlexPA::generatePinAccess( + T* pin, + frInstTerm* inst_term) +{ // aps are after xform // before checkPoints, ap->hasAccess(dir) indicates whether to check drc std::vector> aps; std::set> apset; - if (graphics_) { + if (graphics_ && !query_) { UniqueClass* inst_class = nullptr; if (inst_term) { inst_class = unique_insts_.getUniqueClass(inst_term->getInst()); @@ -1508,12 +1528,149 @@ int FlexPA::genPinAccess(T* pin, frInstTerm* inst_term) ap->sortViaDefs(cost_map); } - updatePinStats(aps, inst_term); - // write to pa - for (auto& ap : aps) { - pin->getPinAccess(pin_access_idx)->addAccessPoint(std::move(ap)); + return aps; +} + +bool FlexPA::signalPinsOverlapSupply(frInst* candidate) +{ + // Native PA suppresses fixed/fixed shorts, so check all signal-pin copper + // first. + for (const auto& term : candidate->getInstTerms()) { + if (term->getTerm()->getType().isSupply()) { + continue; + } + for (const auto& pin : term->getTerm()->getPins()) { + auto shapes = mergePinShapes(pin.get(), term.get()); + for (int layer = 0; std::cmp_less(layer, shapes.size()); ++layer) { + if (!getTech()->getLayer(layer)->isRoutable()) { + continue; + } + std::vector> rectangles; + shapes[layer].get_rectangles(rectangles); + for (const auto& rect : rectangles) { + const odb::Rect copper( + gtl::xl(rect), gtl::yl(rect), gtl::xh(rect), gtl::yh(rect)); + frRegionQuery::Objects objects; + getDesign()->getRegionQuery()->query(copper, layer, objects); + for (const auto& [box, object] : objects) { + if (object->typeId() != frcPathSeg && object->typeId() != frcVia + && object->typeId() != frcPatchWire) { + continue; + } + const auto net = static_cast(object)->getNet(); + if (net && net->getType().isSupply() && net != term->getNet() + && copper.intersects(box)) { + return true; + } + } + } + } + } } - return aps.size(); + return false; +} + +bool FlexPA::hasUsablePinAccess(odb::dbInst* db_inst, + const odb::Point& candidate_location) +{ + auto source = getDesign()->getTopBlock()->findInst(db_inst); + if (!source || source->isToBeDeleted()) { + return false; + } + auto transform = db_inst->getTransform(); + const auto location = db_inst->getLocation(); + const auto origin = transform.getOffset(); + transform.setOffset( + odb::Point(origin.x() + candidate_location.x() - location.x(), + origin.y() + candidate_location.y() - location.y())); + frInst candidate(source->getName(), source->getMaster(), nullptr); + candidate.setTransform(transform); + for (const auto& term : source->getInstTerms()) { + auto trial = std::make_unique(&candidate, term->getTerm()); + trial->addToNet(term->getNet()); + trial->setIndexInOwner(term->getIndexInOwner()); + trial->setStubborn(term->isStubborn()); + candidate.addInstTerm(std::move(trial)); + } + for (const auto& blockage : source->getInstBlockages()) { + candidate.addInstBlockage( + std::make_unique(&candidate, blockage->getBlockage())); + } + if (signalPinsOverlapSupply(&candidate)) { + return false; + } + QueryContext context{source}; + struct QueryScope + { + QueryContext*& slot; + ~QueryScope() { slot = nullptr; } + } scope{query_}; + query_ = &context; + const auto latest = source->getLatestPATransform(); + const bool reuse = source->hasPinAccessIdx() + && latest.getOrient() == transform.getOrient() + && latest.getOffset() == candidate_location; + for (const auto& term : candidate.getInstTerms()) { + if (isSkipInstTermLocal(term.get())) { + continue; + } + bool usable = false; + for (const auto& pin : term->getTerm()->getPins()) { + if (reuse) { + auto shapes = mergePinShapes(pin.get(), term.get()); + std::vector>> polygons( + shapes.size()); + for (size_t layer = 0; layer < shapes.size(); ++layer) { + shapes[layer].get_polygons(polygons[layer]); + } + for (const auto& stored : + pin->getPinAccess(source->getPinAccessIdx())->getAccessPoints()) { + frAccessPoint ap(*stored); + const odb::dbTransform shift(candidate_location); + auto point = ap.getPoint(); + shift.apply(point); + ap.setPoint(point); + for (auto& segment : ap.getPathSegs()) { + segment.move(shift); + } + const auto layer = ap.getLayerNum(); + if (!isStdCellTerm(term.get()) + || layer > router_cfg_->VIA_ACCESS_LAYERNUM) { + usable = validateAPForPlanarAccess( + &ap, polygons, pin.get(), term.get()); + } + if (!usable) { + for (const auto& vias : stored->getAllViaDefs()) { + for (auto via_def : vias) { + frVia via(via_def, point); + if (checkViaPlanarAccess( + &ap, &via, pin.get(), term.get(), polygons[layer])) { + usable = true; + break; + } + } + if (usable) { + break; + } + } + } + if (usable) { + break; + } + } + } + if (!usable) { + usable = !generatePinAccess(pin.get(), term.get()).empty(); + } + if (usable) { + break; + } + } + if (!usable) { + return false; + } + } + return true; } void FlexPA::genInstAccessPoints(frInst* unique_inst) diff --git a/openroad/drt/test/BUILD b/openroad/drt/test/BUILD index 944ff3a9b42..1c6bc683bb6 100644 --- a/openroad/drt/test/BUILD +++ b/openroad/drt/test/BUILD @@ -290,3 +290,23 @@ cc_test( "@googletest//:gtest_main", ], ) + +cc_test( + name = "incremental_pin_access_unittest", + srcs = ["IncrementalPinAccessTest.cpp"], + data = [ + "incremental_pin_access.def", + "incremental_pin_access.lef", + "incremental_pin_access_tech.lef", + ], + deps = [ + "//openroad/drt:drt_impl", + "//openroad/dst", + "//openroad/odb/src/db", + "//openroad/odb/src/defin", + "//openroad/tst:db_fixture", + "//openroad/utl", + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) diff --git a/openroad/drt/test/IncrementalPinAccessTest.cpp b/openroad/drt/test/IncrementalPinAccessTest.cpp new file mode 100644 index 00000000000..b620e1901a0 --- /dev/null +++ b/openroad/drt/test/IncrementalPinAccessTest.cpp @@ -0,0 +1,449 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2026, The OpenROAD Authors + +#include +#include +#include +#include + +#include "AbstractGraphicsFactory.h" +#include "drt-global.h" +#include "drt/TritonRoute.h" +#include "dst/Distributed.h" +#include "frDesign.h" +#include "gtest/gtest.h" +#include "odb/db.h" +#include "odb/defin.h" +#include "tst/db_fixture.h" +#include "tst/logger_checks.h" +#include "utl/ServiceRegistry.h" + +namespace drt { +namespace { + +class NoGraphics : public AbstractGraphicsFactory +{ + public: + void reset(frDebugSettings*, + frDesign*, + odb::dbDatabase*, + utl::Logger*, + RouterConfiguration*) override + { + } + bool guiActive() override { return false; } + std::unique_ptr makeUniqueDRGraphics() override + { + return nullptr; + } + std::unique_ptr makeUniqueTAGraphics() override + { + return nullptr; + } + std::unique_ptr makeUniquePAGraphics() override + { + return nullptr; + } +}; + +class IncrementalPinAccessTest : public tst::DbFixture +{ + protected: + void SetUp() override + { + auto tech = loadTechLef( + "test", "_main/openroad/drt/test/incremental_pin_access_tech.lef"); + ASSERT_NE(tech, nullptr); + auto lib = loadLibLef( + tech, "test", "_main/openroad/drt/test/incremental_pin_access.lef"); + ASSERT_NE(lib, nullptr); + ASSERT_NE( + readDef("_main/openroad/drt/test/incremental_pin_access.def", {lib}), + nullptr); + router_.initGraphics(std::make_unique()); + ParamStruct params; + params.viaAccessLayer = "M1"; + params.viaInPinBottomLayer = "M1"; + params.viaInPinTopLayer = "M1"; + params.minAccessPoints = 1; + params.verbose = 0; + params.num_threads = 2; + router_.setParams(params); + router_.getRouterConfiguration()->BOTTOM_ROUTING_LAYER = 4; + router_.getRouterConfiguration()->TOP_ROUTING_LAYER = 6; + } + + odb::dbInst* makeNeighbor() + { + auto block = db_->getChip()->getBlock(); + auto power = block->findNet("VDD"); + while (!power->getSWires().empty()) { + odb::dbSWire::destroy(*power->getSWires().begin()); + } + auto master = db_->findLib("test")->findMaster("neighbor_cell"); + auto neighbor = odb::dbInst::create(block, master, "neighbor"); + neighbor->setLocation(400, 0); + neighbor->setPlacementStatus(odb::dbPlacementStatus::PLACED); + return neighbor; + } + + dst::Distributed dist_{&logger_}; + utl::ServiceRegistry services_{&logger_}; + TritonRoute router_{db_.get(), &logger_, &services_, &dist_, nullptr}; +}; + +TEST_F(IncrementalPinAccessTest, QueryPreservesPublishedAccess) +{ + auto inst = db_->getChip()->getBlock()->findInst("u"); + EXPECT_FALSE(router_.hasPinAccess()); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, inst->getLocation())); + ASSERT_NO_THROW(router_.pinAccess()); + ASSERT_TRUE(router_.hasPinAccess()); + auto term = inst->findITerm("A"); + const auto preferred = term->getPrefAccessPoints(); + const auto catalogue = term->getAccessPoints(); + const auto location = inst->getLocation(); + for (int repeat = 0; repeat < 2; ++repeat) { + EXPECT_FALSE(router_.hasUsablePinAccess(inst, location)); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, odb::Point(300, 0))); + EXPECT_EQ(inst->getLocation(), location); + EXPECT_EQ(term->getPrefAccessPoints(), preferred); + EXPECT_EQ(term->getAccessPoints(), catalogue); + } + db_->getChip()->getBlock()->clear(); + EXPECT_FALSE(router_.hasPinAccess()); +} + +TEST_F(IncrementalPinAccessTest, BulkQueryMatchesScalarAndPreservesAccess) +{ + auto* block = db_->getChip()->getBlock(); + auto* healthy = block->findInst("u"); + auto* failing = block->findInst("v"); + healthy->setOrient(odb::dbOrientType::MY); + healthy->setLocation(400, 0); + ASSERT_NO_THROW(router_.pinAccess()); + ASSERT_TRUE(router_.hasUsablePinAccess(healthy, healthy->getLocation())); + ASSERT_FALSE(router_.hasUsablePinAccess(failing, failing->getLocation())); + auto* child = odb::dbBlock::create(block, "child"); + auto* foreign = odb::dbInst::create(child, healthy->getMaster(), "u"); + std::vector instances; + for (int i = 0; i < 8; ++i) { + instances.insert(instances.end(), {healthy, failing, nullptr, foreign}); + } + std::vector expected; + for (auto* inst : instances) { + if (!router_.hasUsablePinAccess( + inst, inst ? inst->getLocation() : odb::Point())) { + expected.push_back(inst); + } + } + auto* healthy_term = healthy->findITerm("A"); + auto* failing_term = failing->findITerm("A"); + const auto healthy_preferred = healthy_term->getPrefAccessPoints(); + const auto healthy_catalogue = healthy_term->getAccessPoints(); + const auto failing_preferred = failing_term->getPrefAccessPoints(); + const auto failing_catalogue = failing_term->getAccessPoints(); + for (const int threads : {1, 2, 16}) { + router_.getRouterConfiguration()->MAX_THREADS = threads; + for (int repeat = 0; repeat < 2; ++repeat) { + EXPECT_EQ(router_.findPinAccessFailures(instances), expected); + EXPECT_TRUE(router_.findPinAccessFailures({}).empty()); + EXPECT_TRUE(router_.hasPinAccess()); + EXPECT_EQ(healthy->getLocation(), odb::Point(400, 0)); + EXPECT_EQ(healthy->getOrient(), odb::dbOrientType::MY); + EXPECT_EQ(failing->getLocation(), odb::Point(4400, 0)); + EXPECT_EQ(healthy_term->getPrefAccessPoints(), healthy_preferred); + EXPECT_EQ(healthy_term->getAccessPoints(), healthy_catalogue); + EXPECT_EQ(failing_term->getPrefAccessPoints(), failing_preferred); + EXPECT_EQ(failing_term->getAccessPoints(), failing_catalogue); + EXPECT_TRUE(router_.hasUsablePinAccess(healthy, healthy->getLocation())); + EXPECT_FALSE(router_.hasUsablePinAccess(failing, failing->getLocation())); + } + } +} + +TEST_F(IncrementalPinAccessTest, BulkQueryRejectsUnavailablePinAccess) +{ + auto* block = db_->getChip()->getBlock(); + auto* inst = block->findInst("u"); + const std::vector instances{inst, nullptr, inst}; + EXPECT_EQ(router_.findPinAccessFailures(instances), instances); + EXPECT_TRUE(router_.findPinAccessFailures({}).empty()); + ASSERT_NO_THROW(router_.pinAccess()); + EXPECT_LOGGER_ERROR(router_.main(), "DRT-0047"); + EXPECT_EQ(router_.findPinAccessFailures(instances), instances); +} + +TEST_F(IncrementalPinAccessTest, QueryRejectsSupplyOverlapOnAlternativeMPin) +{ + auto block = db_->getChip()->getBlock(); + auto inst = block->findInst("u"); + auto power = block->findNet("VDD"); + while (!power->getSWires().empty()) { + odb::dbSWire::destroy(*power->getSWires().begin()); + } + auto term = inst->findITerm("A"); + auto pin = odb::dbMPin::create(term->getMTerm()); + auto layer = block->getTech()->findLayer("M1"); + odb::dbBox::create(pin, layer, 750, 600, 850, 800); + auto wire = odb::dbSWire::create(power, odb::dbWireType::ROUTED); + ASSERT_NE(odb::dbSBox::create( + wire, layer, 750, 680, 850, 720, odb::dbWireShapeType::STRIPE), + nullptr); + ASSERT_NO_THROW(router_.pinAccess()); + const auto preferred = term->getPrefAccessPoints(); + const auto catalogue = term->getAccessPoints(); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, inst->getLocation())); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); + EXPECT_EQ(inst->getLocation(), odb::Point(400, 0)); + EXPECT_EQ(term->getPrefAccessPoints(), preferred); + EXPECT_EQ(term->getAccessPoints(), catalogue); +} + +TEST_F(IncrementalPinAccessTest, QueryChecksPDNCuts) +{ + auto block = db_->getChip()->getBlock(); + auto power = block->findNet("VDD"); + while (!power->getSWires().empty()) { + odb::dbSWire::destroy(*power->getSWires().begin()); + } + auto wire = odb::dbSWire::create(power, odb::dbWireType::ROUTED); + ASSERT_NE(odb::dbSBox::create(wire, + block->getTech()->findLayer("V1"), + 780, + 480, + 820, + 520, + odb::dbWireShapeType::STRIPE), + nullptr); + ASSERT_NO_THROW(router_.pinAccess()); + auto inst = block->findInst("u"); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, inst->getLocation())); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); +} + +TEST_F(IncrementalPinAccessTest, QueryChecksFirstHopSegments) +{ + auto block = db_->getChip()->getBlock(); + auto power = block->findNet("VDD"); + while (!power->getSWires().empty()) { + odb::dbSWire::destroy(*power->getSWires().begin()); + } + auto wire = odb::dbSWire::create(power, odb::dbWireType::ROUTED); + const odb::Rect obstacles[]{{550, 450, 650, 550}, + {950, 450, 1050, 550}, + {750, 250, 850, 350}, + {750, 650, 850, 750}}; + for (const auto& box : obstacles) { + ASSERT_NE(odb::dbSBox::create(wire, + block->getTech()->findLayer("M2"), + box.xMin(), + box.yMin(), + box.xMax(), + box.yMax(), + odb::dbWireShapeType::STRIPE), + nullptr); + } + ASSERT_NO_THROW(router_.pinAccess()); + auto inst = block->findInst("u"); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, inst->getLocation())); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); +} + +TEST_F(IncrementalPinAccessTest, QueryUsesMirroredLowerLeft) +{ + auto inst = db_->getChip()->getBlock()->findInst("u"); + inst->setOrient(odb::dbOrientType::MY); + inst->setLocation(400, 0); + ASSERT_NO_THROW(router_.pinAccess()); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, odb::Point(400, 0))); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); + EXPECT_EQ(inst->getLocation(), odb::Point(400, 0)); + EXPECT_EQ(inst->getOrient(), odb::dbOrientType::MY); +} + +TEST_F(IncrementalPinAccessTest, QueryUsesAlternativeMPin) +{ + auto inst = db_->getChip()->getBlock()->findInst("u"); + auto pin = odb::dbMPin::create(inst->findITerm("A")->getMTerm()); + odb::dbBox::create(pin, + inst->getMaster()->getLib()->getTech()->findLayer("M1"), + 750, + 600, + 850, + 800); + ASSERT_NO_THROW(router_.pinAccess()); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, inst->getLocation())); +} + +TEST_F(IncrementalPinAccessTest, QueryMovesOwnObstructions) +{ + auto inst = db_->getChip()->getBlock()->findInst("u"); + auto master = inst->getMaster(); + odb::dbBox::create( + master, master->getLib()->getTech()->findLayer("M2"), -50, 400, 50, 600); + ASSERT_NO_THROW(router_.pinAccess()); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); +} + +TEST_F(IncrementalPinAccessTest, QueryChecksNeighborPins) +{ + auto block = db_->getChip()->getBlock(); + auto inst = block->findInst("u"); + auto neighbor = makeNeighbor(); + ASSERT_NO_THROW(router_.pinAccess()); + neighbor->findITerm("A")->connect(block->findNet("n2")); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, inst->getLocation())); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); +} + +TEST_F(IncrementalPinAccessTest, QueryAcceptsSameNetNeighbor) +{ + auto block = db_->getChip()->getBlock(); + auto inst = block->findInst("u"); + auto neighbor = makeNeighbor(); + ASSERT_NO_THROW(router_.pinAccess()); + neighbor->findITerm("A")->connect(block->findNet("n")); + EXPECT_TRUE(router_.hasUsablePinAccess(inst, inst->getLocation())); +} + +TEST_F(IncrementalPinAccessTest, ReinitializationInvalidatesReadiness) +{ + ASSERT_NO_THROW(router_.pinAccess()); + ASSERT_TRUE(router_.hasPinAccess()); + EXPECT_LOGGER_ERROR(router_.main(), "DRT-0047"); + EXPECT_FALSE(router_.hasPinAccess()); + auto inst = db_->getChip()->getBlock()->findInst("u"); + EXPECT_FALSE(router_.hasUsablePinAccess(inst, odb::Point(0, 0))); +} + +TEST_F(IncrementalPinAccessTest, FailedRefreshRebuildsOnRetry) +{ + auto block = db_->getChip()->getBlock(); + auto inst = block->findInst("u"); + auto unmoved = block->findInst("v"); + auto term = unmoved->findITerm("B"); + // B precedes A in the LEF to exercise partial regeneration. + unmoved->findITerm("A")->disconnect(); + term->connect(block->findNet("n2")); + unmoved->setLocation(6300, 0); + ASSERT_NO_THROW(router_.pinAccess()); + inst->setLocation(700, 0); + EXPECT_LOGGER_ERROR(router_.updateDirtyPAData(), "DRT-0073"); + inst->setLocation(900, 0); + EXPECT_LOGGER_ERROR(router_.updateDirtyPAData(), "DRT-0073"); + inst->setLocation(400, 0); + ASSERT_NO_THROW(router_.updateDirtyPAData()); + const auto points = inst->findITerm("A")->getPrefAccessPoints(); + ASSERT_EQ(points.size(), 1); + EXPECT_EQ(points.front()->getPoint(), odb::Point(400, 500)); + EXPECT_EQ(unmoved->getLocation(), odb::Point(6300, 0)); + const auto preferred = term->getPrefAccessPoints(); + ASSERT_EQ(preferred.size(), 1); + EXPECT_EQ(preferred.front()->getPoint(), odb::Point(900, 300)); + const auto catalogue = term->getAccessPoints(); + ASSERT_EQ(catalogue.size(), 1); + ASSERT_FALSE(catalogue.begin()->second.empty()); + bool preferred_in_catalogue = false; + for (auto point : catalogue.begin()->second) { + preferred_in_catalogue |= point == preferred.front(); + EXPECT_TRUE(odb::Rect(750, 200, 1050, 400).intersects(point->getPoint())); + } + EXPECT_TRUE(preferred_in_catalogue); +} + +TEST_F(IncrementalPinAccessTest, RefreshPreservesUnmovedClassMember) +{ + auto block = db_->getChip()->getBlock(); + auto unmoved = block->findInst("v"); + auto term = unmoved->findITerm("A"); + ASSERT_NO_THROW(router_.pinAccess()); + block->findInst("u")->findITerm("B")->connect(block->findNet("n2")); + ASSERT_NO_THROW(router_.updateDirtyPAData()); + EXPECT_EQ(unmoved->getLocation(), odb::Point(4400, 0)); + const auto preferred = term->getPrefAccessPoints(); + ASSERT_EQ(preferred.size(), 1); + EXPECT_EQ(preferred.front()->getPoint(), odb::Point(400, 500)); + const auto catalogue = term->getAccessPoints(); + ASSERT_EQ(catalogue.size(), 1); + ASSERT_FALSE(catalogue.begin()->second.empty()); + bool preferred_in_catalogue = false; + for (auto point : catalogue.begin()->second) { + preferred_in_catalogue |= point == preferred.front(); + EXPECT_TRUE(odb::Rect(350, 400, 450, 600).intersects(point->getPoint())); + } + EXPECT_TRUE(preferred_in_catalogue); +} + +TEST_F(IncrementalPinAccessTest, ResetDbDiscardsPinAccess) +{ + ASSERT_TRUE(db_->getChip()->getBlock()->getAccessPoints().empty()); + const auto odb_path = testing::TempDir() + "incremental_pin_access.odb"; + { + std::ofstream stream(odb_path, std::ios::binary); + db_->write(stream); + } + ASSERT_NO_THROW(router_.pinAccess()); + odb::dbChip::destroy(db_->getChip()); + ASSERT_NO_THROW(router_.resetDb(odb_path.c_str())); + std::remove(odb_path.c_str()); + auto block = db_->getChip()->getBlock(); + ASSERT_TRUE(block->getAccessPoints().empty()); + auto inst = block->findInst("u"); + inst->setLocation(800, 0); + ASSERT_NO_THROW(router_.updateDirtyPinAccess()); + EXPECT_TRUE(block->getAccessPoints().empty()); + ASSERT_NO_THROW(router_.pinAccess()); + const auto points = inst->findITerm("A")->getPrefAccessPoints(); + ASSERT_EQ(points.size(), 1); + EXPECT_EQ(points.front()->getPoint(), odb::Point(400, 500)); +} + +TEST_F(IncrementalPinAccessTest, BlockClearDiscardsDesign) +{ + auto block = db_->getChip()->getBlock(); + auto master = block->findInst("u")->getMaster(); + ASSERT_NO_THROW(router_.pinAccess()); + block->findInst("u")->setLocation(800, 0); + block->clear(); + ASSERT_EQ(router_.getDesign()->getTopBlock(), nullptr); + EXPECT_NO_THROW(router_.updateDirtyPinAccess()); + + struct Cell + { + const char* instance; + const char* net; + const char* port; + }; + const Cell cells[]{{"u", "n", "OUT"}, {"v", "n2", "OUT2"}}; + // FLOORPLAN updates existing objects; recreate the cleared connections first. + for (const auto& cell : cells) { + auto inst = odb::dbInst::create(block, master, cell.instance); + auto net = odb::dbNet::create(block, cell.net); + inst->findITerm("A")->connect(net); + odb::dbBTerm::create(net, cell.port); + } + auto power = odb::dbNet::create(block, "VDD"); + power->setSigType(odb::dbSigType::POWER); + power->setSpecial(); + std::vector libs{db_->findLib("test")}; + odb::defin def(db_.get(), &logger_, odb::defin::FLOORPLAN); + ASSERT_TRUE(def.readChip( + libs, + getFilePath("_main/openroad/drt/test/incremental_pin_access.def").c_str(), + db_->getChip())); + ASSERT_EQ(router_.getDesign()->getTopBlock(), nullptr); + ASSERT_NO_THROW(router_.pinAccess()); + for (const auto& cell : cells) { + const auto points + = block->findInst(cell.instance)->findITerm("A")->getPrefAccessPoints(); + ASSERT_EQ(points.size(), 1); + EXPECT_EQ(points.front()->getPoint(), odb::Point(400, 500)); + } + block->clear(); + EXPECT_EQ(router_.getDesign()->getTopBlock(), nullptr); +} + +} // namespace +} // namespace drt diff --git a/openroad/drt/test/incremental_pin_access.def b/openroad/drt/test/incremental_pin_access.def new file mode 100644 index 00000000000..c1b70a1f16f --- /dev/null +++ b/openroad/drt/test/incremental_pin_access.def @@ -0,0 +1,35 @@ +VERSION 5.8 ; +DIVIDERCHAR "/" ; +BUSBITCHARS "[]" ; +DESIGN pdn_first_hop ; +UNITS DISTANCE MICRONS 1000 ; +DIEAREA ( 0 0 ) ( 8000 1000 ) ; +ROW ROW_0 core_site 0 0 N DO 80 BY 1 STEP 100 0 ; +TRACKS X 0 DO 40 STEP 200 LAYER M1 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M1 ; +TRACKS X 0 DO 20 STEP 400 LAYER M2 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M2 ; +TRACKS X 0 DO 40 STEP 200 LAYER M3 ; +TRACKS Y 100 DO 5 STEP 200 LAYER M3 ; +COMPONENTS 2 ; + - u phase_cell + PLACED ( 400 0 ) N ; + - v phase_cell + PLACED ( 4400 0 ) N ; +END COMPONENTS +PINS 2 ; + - OUT + NET n + DIRECTION OUTPUT + USE SIGNAL + + PORT + LAYER M2 ( -50 -100 ) ( 50 100 ) + + PLACED ( 1600 500 ) N ; + - OUT2 + NET n2 + DIRECTION OUTPUT + USE SIGNAL + + PORT + LAYER M2 ( -50 -100 ) ( 50 100 ) + + PLACED ( 5600 500 ) N ; +END PINS +NETS 2 ; + - n ( PIN OUT ) ( u A ) + USE SIGNAL ; + - n2 ( PIN OUT2 ) ( v A ) + USE SIGNAL ; +END NETS +SPECIALNETS 1 ; + - VDD + USE POWER + + ROUTED M2 0 + SHAPE STRIPE ( 800 500 ) V23 + NEW M2 0 + SHAPE STRIPE ( 4800 500 ) V23 ; +END SPECIALNETS +END DESIGN diff --git a/openroad/drt/test/incremental_pin_access.lef b/openroad/drt/test/incremental_pin_access.lef new file mode 100644 index 00000000000..b74b43b88bb --- /dev/null +++ b/openroad/drt/test/incremental_pin_access.lef @@ -0,0 +1,47 @@ +VERSION 5.8 ; +BUSBITCHARS "[]" ; +DIVIDERCHAR "/" ; +SITE core_site + CLASS CORE ; + SYMMETRY Y ; + SIZE 0.1 BY 1.0 ; +END core_site +MACRO phase_cell + CLASS CORE ; + ORIGIN 0 0 ; + SIZE 1.2 BY 1.0 ; + SYMMETRY X Y ; + SITE core_site ; + PIN B + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M1 ; + RECT 0.75 0.2 1.05 0.4 ; + END + END B + PIN A + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M1 ; + RECT 0.35 0.4 0.45 0.6 ; + END + END A +END phase_cell +MACRO neighbor_cell + CLASS CORE ; + ORIGIN 0 0 ; + SIZE 1.2 BY 1.0 ; + SYMMETRY X Y ; + SITE core_site ; + PIN A + DIRECTION INPUT ; + USE SIGNAL ; + PORT + LAYER M2 ; + RECT 0.35 0.4 0.45 0.6 ; + END + END A +END neighbor_cell +END LIBRARY diff --git a/openroad/drt/test/incremental_pin_access_tech.lef b/openroad/drt/test/incremental_pin_access_tech.lef new file mode 100644 index 00000000000..bbc2929e717 --- /dev/null +++ b/openroad/drt/test/incremental_pin_access_tech.lef @@ -0,0 +1,64 @@ +VERSION 5.8 ; +BUSBITCHARS "[]" ; +DIVIDERCHAR "/" ; +UNITS + DATABASE MICRONS 1000 ; +END UNITS +MANUFACTURINGGRID 0.01 ; +PROPERTYDEFINITIONS + LAYER LEF58_RIGHTWAYONGRIDONLY STRING ; +END PROPERTYDEFINITIONS +LAYER masterslice + TYPE MASTERSLICE ; +END masterslice +LAYER v0 + TYPE CUT ; + SPACING 0.04 ; +END v0 +LAYER M1 + TYPE ROUTING ; + DIRECTION HORIZONTAL ; + PITCH 0.2 ; + WIDTH 0.1 ; + SPACING 0.1 ; +END M1 +LAYER V1 + TYPE CUT ; + SPACING 0.04 ; +END V1 +LAYER M2 + TYPE ROUTING ; + DIRECTION VERTICAL ; + PITCH 0.4 ; + WIDTH 0.1 ; + SPACING 0.1 ; + PROPERTY LEF58_RIGHTWAYONGRIDONLY "RIGHTWAYONGRIDONLY ;" ; +END M2 +VIA V12 DEFAULT + LAYER M1 ; + RECT -0.05 -0.05 0.05 0.05 ; + LAYER V1 ; + RECT -0.02 -0.02 0.02 0.02 ; + LAYER M2 ; + RECT -0.05 -0.05 0.05 0.05 ; +END V12 +LAYER V2 + TYPE CUT ; + SPACING 0.04 ; +END V2 +LAYER M3 + TYPE ROUTING ; + DIRECTION HORIZONTAL ; + PITCH 0.2 ; + WIDTH 0.1 ; + SPACING 0.1 ; +END M3 +VIA V23 DEFAULT + LAYER M2 ; + RECT -0.07 -0.07 0.07 0.07 ; + LAYER V2 ; + RECT -0.02 -0.02 0.02 0.02 ; + LAYER M3 ; + RECT -0.05 -0.05 0.05 0.05 ; +END V23 +END LIBRARY diff --git a/openroad/odb/include/odb/dbBlockCallBackObj.h b/openroad/odb/include/odb/dbBlockCallBackObj.h index c1326e88748..aa7ad9c30bb 100644 --- a/openroad/odb/include/odb/dbBlockCallBackObj.h +++ b/openroad/odb/include/odb/dbBlockCallBackObj.h @@ -201,6 +201,8 @@ class dbBlockCallBackObj virtual void inDbBlockStreamOutBefore(dbBlock*) {} virtual void inDbBlockStreamOutAfter(dbBlock*) {} virtual void inDbBlockReadNetsBefore(dbBlock*) {} + // Called before clearing contents, with callback registration retained. + virtual void inDbBlockClear(dbBlock*) {} virtual void inDbBlockSetDieArea(dbBlock*) {} virtual void inDbBlockSetCoreArea(dbBlock*) {} diff --git a/openroad/odb/src/db/dbBlock.cpp b/openroad/odb/src/db/dbBlock.cpp index 5479ad14f69..67e1a6fc476 100644 --- a/openroad/odb/src/db/dbBlock.cpp +++ b/openroad/odb/src/db/dbBlock.cpp @@ -74,8 +74,11 @@ #include "dbIsolation.h" #include "dbJournal.h" #include "dbLevelShifter.h" +#include "dbLib.h" #include "dbLogicPort.h" +#include "dbMPin.h" #include "dbMarkerCategory.h" +#include "dbMaster.h" #include "dbModBTerm.h" #include "dbModITerm.h" #include "dbModInst.h" @@ -124,6 +127,7 @@ #include "odb/db.h" #include "odb/dbBlockCallBackObj.h" #include "odb/dbExtControl.h" +#include "odb/dbId.h" #include "odb/dbObject.h" #include "odb/dbSet.h" #include "odb/dbShape.h" @@ -507,7 +511,37 @@ _dbBlock::~_dbBlock() void dbBlock::clear() { _dbBlock* block = (_dbBlock*) this; + for (auto callback : block->callbacks_) { + callback->inDbBlockClear(this); + } _dbDatabase* db = block->getDatabase(); + // Master pins outlive the block contents and must not retain their AP IDs. + for (auto access_point : getAccessPoints()) { + const auto* ap = static_cast<_dbAccessPoint*>(access_point->getImpl()); + if (!ap->mpin_.isValid() || !db->lib_tbl_->validId(ap->lib_)) { + continue; + } + const auto* lib = db->lib_tbl_->getPtr(ap->lib_); + if (!lib->master_tbl_->validId(ap->master_)) { + continue; + } + const auto* master = lib->master_tbl_->getPtr(ap->master_); + if (master->mpin_tbl_->validId(ap->mpin_)) { + auto& pin_aps = master->mpin_tbl_->getPtr(ap->mpin_)->aps_; + for (auto& aps : pin_aps) { + const auto pos + = std::ranges::find(aps, dbId<_dbAccessPoint>(ap->getOID())); + if (pos != aps.end()) { + aps.erase(pos); + break; + } + } + if (std::ranges::all_of(pin_aps, + [](const auto& aps) { return aps.empty(); })) { + pin_aps.clear(); + } + } + } _dbBlock* parent = (_dbBlock*) getParent(); _dbChip* chip = (_dbChip*) getChip(); diff --git a/openroad/odb/test/cpp/TestAccessPoint.cpp b/openroad/odb/test/cpp/TestAccessPoint.cpp index 3aaf5677ef0..4dd726fa9fa 100644 --- a/openroad/odb/test/cpp/TestAccessPoint.cpp +++ b/openroad/odb/test/cpp/TestAccessPoint.cpp @@ -121,5 +121,95 @@ TEST_F(SimpleDbFixture, SwapMasterClearsAndInvalidatesAccessPoints) EXPECT_TRUE(swapped_iterm->getAccessPoints().empty()); } +TEST_F(SimpleDbFixture, BlockClearUnlinksMasterAccessPoints) +{ + createSimpleDB(); + auto block = db_->getChip()->getBlock(); + auto master = db_->findMaster("and2"); + auto term = master->findMTerm("a"); + auto pin = dbMPin::create(term); + auto ap = dbAccessPoint::create(block, pin, 0); + dbAccessPoint::create(block, pin, 0); + dbAccessPoint::create(block, pin, 2); + for (const auto* name : {"i1", "i2"}) { + auto inst = dbInst::create(block, master, name); + inst->setPinAccessIdx(0); + inst->getITerm(term)->setAccessPoint(pin, ap); + ASSERT_EQ(inst->getITerm(term)->getPrefAccessPoints().size(), 1); + } + auto net = dbNet::create(block, "n1"); + auto port = dbBTerm::create(net, "IN1"); + auto bpin = dbBPin::create(port); + dbAccessPoint::create(bpin); + ASSERT_EQ(block->getAccessPoints().size(), 4); + ASSERT_EQ(pin->getPinAccess().at(0).size(), 2); + ASSERT_EQ(pin->getPinAccess().at(2).size(), 1); + + block->clear(); + + EXPECT_EQ(db_->getChip()->getBlock(), block); + EXPECT_TRUE(block->getAccessPoints().empty()); + EXPECT_TRUE(pin->getPinAccess().empty()); + + auto inst = dbInst::create(block, master, "i1"); + auto fresh_ap = dbAccessPoint::create(block, pin, 0); + inst->setPinAccessIdx(0); + inst->getITerm(term)->setAccessPoint(pin, fresh_ap); + const auto fresh_access = pin->getPinAccess(); + ASSERT_EQ(fresh_access.size(), 1); + ASSERT_EQ(fresh_access.at(0).size(), 1); + EXPECT_EQ(fresh_access.at(0).front(), fresh_ap); + const auto preferred = inst->getITerm(term)->getPrefAccessPoints(); + ASSERT_EQ(preferred.size(), 1); + EXPECT_EQ(preferred.front(), fresh_ap); +} + +TEST_F(SimpleDbFixture, BlockClearPreservesUnrelatedMasterAccessPoints) +{ + createSimpleDB(); + auto block = db_->getChip()->getBlock(); + auto master = db_->findMaster("and2"); + auto pin = dbMPin::create(master->findMTerm("a")); + auto surviving_ap = dbAccessPoint::create(block, pin, 0); + auto child = dbBlock::create(block, "child"); + auto other_pin = dbMPin::create(master->findMTerm("o")); + dbAccessPoint::create(child, other_pin, 0); + auto removed_ap = dbAccessPoint::create(child, pin, 2); + ASSERT_NE(surviving_ap->getId(), removed_ap->getId()); + + child->clear(); + + const auto access = pin->getPinAccess(); + ASSERT_EQ(access.size(), 3); + ASSERT_EQ(access.at(0).size(), 1); + EXPECT_EQ(access.at(0).front(), surviving_ap); + EXPECT_TRUE(access.at(1).empty()); + EXPECT_TRUE(access.at(2).empty()); + EXPECT_TRUE(other_pin->getPinAccess().empty()); + EXPECT_TRUE(child->getAccessPoints().empty()); +} + +TEST_F(SimpleDbFixture, BlockClearIgnoresDeletedAccessPointOwners) +{ + createSimpleDB(); + auto block = db_->getChip()->getBlock(); + auto master = db_->findMaster("and2"); + auto pin = dbMPin::create(master->findMTerm("a")); + dbAccessPoint::create(block, pin, 0); + dbMaster::destroy(master); + + auto lib = dbLib::create(db_.get(), "removed_lib", db_->getTech(), ','); + auto removed_master + = createMaster1X1(lib, "removed_master", 1000, 1000, "a", "o"); + auto removed_pin = dbMPin::create(removed_master->findMTerm("a")); + dbAccessPoint::create(block, removed_pin, 0); + dbLib::destroy(lib); + ASSERT_EQ(block->getAccessPoints().size(), 2); + + block->clear(); + + EXPECT_TRUE(block->getAccessPoints().empty()); +} + } // namespace } // namespace odb diff --git a/openroad/odb/test/cpp/TestCallBacks.cpp b/openroad/odb/test/cpp/TestCallBacks.cpp index f37b3c8fe63..b568f981aa7 100644 --- a/openroad/odb/test/cpp/TestCallBacks.cpp +++ b/openroad/odb/test/cpp/TestCallBacks.cpp @@ -16,6 +16,43 @@ class CallbackFixture : public SimpleDbFixture CallBack cb_; }; +TEST_F(CallbackFixture, test_block_clear) +{ + class ClearCallback : public dbBlockCallBackObj + { + public: + int clears = 0; + int creates = 0; + dbBlock* expected_block = nullptr; + + void inDbBlockClear(dbBlock* block) override + { + ++clears; + EXPECT_EQ(block, expected_block); + EXPECT_TRUE(hasOwner()); + EXPECT_NE(block->findNet("before_clear"), nullptr); + dbNet::create(block, "during_clear"); + } + + void inDbNetCreate(dbNet*) override { ++creates; } + } callback; + + createSimpleDB(); + dbBlock* block = db_->getChip()->getBlock(); + dbNet::create(block, "before_clear"); + callback.expected_block = block; + callback.addOwner(block); + block->clear(); + EXPECT_EQ(callback.clears, 1); + EXPECT_EQ(callback.creates, 1); + EXPECT_EQ(db_->getChip()->getBlock(), block); + EXPECT_TRUE(callback.hasOwner()); + EXPECT_EQ(block->findNet("before_clear"), nullptr); + EXPECT_EQ(block->findNet("during_clear"), nullptr); + dbNet::create(block, "after_clear"); + EXPECT_EQ(callback.creates, 2); +} + TEST_F(CallbackFixture, test_inst_and_iterm) { createSimpleDB();