File size: 14,305 Bytes
5cdf637 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | // Copyright 2024 The OpenROAD Authors
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <cstdio>
#include <string>
#include "db_sta/dbNetwork.hh"
#include "gtest/gtest.h"
#include "odb/db.h"
#include "odb/dbTypes.h"
#include "tst/fixture.h"
namespace odb {
class TestSwapMasterFixture : public tst::Fixture
{
protected:
void SetUp() override
{
const std::string nangate_lef
= getFilePath("_main/test/Nangate45/Nangate45.lef");
const std::string nangate_lib
= getFilePath("_main/test/Nangate45/Nangate45_typ.lib");
lib_ = loadTechAndLib("Nangate45", "Nangate45", nangate_lef);
readLiberty(nangate_lib);
odb::dbChip* chip = odb::dbChip::create(db_.get(), db_->getTech());
block_ = odb::dbBlock::create(chip, "top");
block_->setDieArea(odb::Rect(0, 0, 1000, 1000));
// register proper callbacks for timer like read_def
sta_->postReadDef(block_);
// Enable hierarchy
auto* db_network = sta_->getDbNetwork();
db_network->setHierarchy();
db_network->setBlock(block_);
}
odb::dbLib* lib_ = nullptr;
odb::dbChip* chip_ = nullptr;
odb::dbBlock* block_ = nullptr;
};
TEST_F(TestSwapMasterFixture, ComplexSwap)
{
// 1. Setup: Get masters for various cells from Nangate45
auto* inv_master = lib_->findMaster("INV_X1");
auto* nand_master = lib_->findMaster("NAND2_X1");
auto* buf_master = lib_->findMaster("BUF_X1");
auto* nor_master = lib_->findMaster("NOR2_X1");
ASSERT_NE(inv_master, nullptr);
ASSERT_NE(nand_master, nullptr);
ASSERT_NE(buf_master, nullptr);
ASSERT_NE(nor_master, nullptr);
// Top module netlist (before swap):
//
// +--------------------------------+
// | |
// top_in1 --| in1 |
// | module_A |
// top_in2 --| in2 (inst_1) out1 |-- top_out1
// | |
// --| unconnected_in |
// | |
// | out2 |--
// | |
// +--------------------------------+
// module_A netlist:
//
// +-------------+
// in1 ---| INV(inv1_a) |---+
// +-------------+ | +---------------+
// +---| A1 |
// | NAND2(nand1_a)|--- out1
// +---| A2 |
// +-------------+ | +---------------+
// in2 ---| INV(inv2_a) |---+
// +-------------+
//
// unconnected_in ---
//
// --- out2 (unconnected)
//
// This module is logically equivalent to a 2-input OR gate.
// out1 = NOT( (NOT in1) AND (NOT in2) ) = in1 OR in2
// module_B netlist:
//
// +------------+
// in1 ---| BUF(buf1_b)|---+
// +------------+ | +-------------+ +-----------------+
// +---| A1 | | |
// | NOR2(nor1_b)|--| A INV(inv_out_b)|--- out1
// +---| A2 | | |
// +------------+ | +-------------+ +-----------------+
// in2 ---| BUF(buf2_b)|---+
// +------------+
//
// unconnected_in ---
//
// --- out2
//
// This module is logically equivalent to a 2-input OR gate.
// out1 = NOT( NOT( (in1) OR (in2) ) ) = in1 OR in2
//
// module_A and module_B have the same port signature and are logically
// equivalent, but have different internal logic and cell types. This makes
// them swappable at the module instance level.
// Create module_A: inverters and a NAND gate
auto* module_a = odb::dbModule::create(block_, "module_A");
auto* modbterm_a_in1 = odb::dbModBTerm::create(module_a, "in1");
modbterm_a_in1->setIoType(odb::dbIoType::INPUT);
auto* modbterm_a_in2 = odb::dbModBTerm::create(module_a, "in2");
modbterm_a_in2->setIoType(odb::dbIoType::INPUT);
auto* modbterm_a_unconnected_in
= odb::dbModBTerm::create(module_a, "unconnected_in");
modbterm_a_unconnected_in->setIoType(odb::dbIoType::INPUT);
auto* modbterm_a_out1 = odb::dbModBTerm::create(module_a, "out1");
modbterm_a_out1->setIoType(odb::dbIoType::OUTPUT);
auto* modbterm_a_out2 = odb::dbModBTerm::create(module_a, "out2");
modbterm_a_out2->setIoType(odb::dbIoType::OUTPUT);
auto* inv1_a
= odb::dbInst::create(block_, inv_master, "inv1_a", false, module_a);
auto* inv2_a
= odb::dbInst::create(block_, inv_master, "inv2_a", false, module_a);
auto* nand1_a
= odb::dbInst::create(block_, nand_master, "nand1_a", false, module_a);
auto* net_in1_a = odb::dbModNet::create(module_a, "net_in1");
modbterm_a_in1->connect(net_in1_a);
inv1_a->findITerm("A")->connect(net_in1_a);
auto* net_in2_a = odb::dbModNet::create(module_a, "net_in2");
modbterm_a_in2->connect(net_in2_a);
inv2_a->findITerm("A")->connect(net_in2_a);
auto* net_n1_a = odb::dbModNet::create(module_a, "net_n1");
inv1_a->findITerm("ZN")->connect(net_n1_a);
nand1_a->findITerm("A1")->connect(net_n1_a);
auto* net_n2_a = odb::dbModNet::create(module_a, "net_n2");
inv2_a->findITerm("ZN")->connect(net_n2_a);
nand1_a->findITerm("A2")->connect(net_n2_a);
auto* net_out1_a = odb::dbModNet::create(module_a, "net_out1");
modbterm_a_out1->connect(net_out1_a);
nand1_a->findITerm("ZN")->connect(net_out1_a);
// unconnected_in and out2 are not connected internally in module_A
// Create module_B: buffers and a NOR gate (compatible port signature)
auto* module_b = odb::dbModule::create(block_, "module_B");
auto* modbterm_b_in1 = odb::dbModBTerm::create(module_b, "in1");
modbterm_b_in1->setIoType(odb::dbIoType::INPUT);
auto* modbterm_b_in2 = odb::dbModBTerm::create(module_b, "in2");
modbterm_b_in2->setIoType(odb::dbIoType::INPUT);
auto* modbterm_b_unconnected_in
= odb::dbModBTerm::create(module_b, "unconnected_in");
modbterm_b_unconnected_in->setIoType(odb::dbIoType::INPUT);
auto* modbterm_b_out1 = odb::dbModBTerm::create(module_b, "out1");
modbterm_b_out1->setIoType(odb::dbIoType::OUTPUT);
auto* modbterm_b_out2 = odb::dbModBTerm::create(module_b, "out2");
modbterm_b_out2->setIoType(odb::dbIoType::OUTPUT);
auto* buf1_b
= odb::dbInst::create(block_, buf_master, "buf1_b", false, module_b);
auto* buf2_b
= odb::dbInst::create(block_, buf_master, "buf2_b", false, module_b);
auto* nor1_b
= odb::dbInst::create(block_, nor_master, "nor1_b", false, module_b);
auto* inv_out_b
= odb::dbInst::create(block_, inv_master, "inv_out_b", false, module_b);
auto* net_in1_b = odb::dbModNet::create(module_b, "net_in1");
modbterm_b_in1->connect(net_in1_b);
buf1_b->findITerm("A")->connect(net_in1_b);
auto* net_in2_b = odb::dbModNet::create(module_b, "net_in2");
modbterm_b_in2->connect(net_in2_b);
buf2_b->findITerm("A")->connect(net_in2_b);
auto* net_n1_b = odb::dbModNet::create(module_b, "net_n1");
buf1_b->findITerm("Z")->connect(net_n1_b);
nor1_b->findITerm("A1")->connect(net_n1_b);
auto* net_n2_b = odb::dbModNet::create(module_b, "net_n2");
buf2_b->findITerm("Z")->connect(net_n2_b);
nor1_b->findITerm("A2")->connect(net_n2_b);
auto* net_nor_out_b = odb::dbModNet::create(module_b, "net_nor_out");
nor1_b->findITerm("ZN")->connect(net_nor_out_b);
inv_out_b->findITerm("A")->connect(net_nor_out_b);
auto* net_out1_b = odb::dbModNet::create(module_b, "net_out1");
modbterm_b_out1->connect(net_out1_b);
inv_out_b->findITerm("ZN")->connect(net_out1_b);
// unconnected_in and out2 are not connected internally in module_B
// Instantiate module_A in the top module
auto* top_module = block_->getTopModule();
auto* inst_1 = odb::dbModInst::create(top_module, module_a, "inst_1");
// Create ModITerms for the instance and connect them
auto* moditerm_in1 = odb::dbModITerm::create(inst_1, "in1");
moditerm_in1->setChildModBTerm(modbterm_a_in1);
modbterm_a_in1->setParentModITerm(moditerm_in1);
auto* moditerm_in2 = odb::dbModITerm::create(inst_1, "in2");
moditerm_in2->setChildModBTerm(modbterm_a_in2);
modbterm_a_in2->setParentModITerm(moditerm_in2);
auto* moditerm_unconnected_in
= odb::dbModITerm::create(inst_1, "unconnected_in");
moditerm_unconnected_in->setChildModBTerm(modbterm_a_unconnected_in);
modbterm_a_unconnected_in->setParentModITerm(moditerm_unconnected_in);
auto* moditerm_out1 = odb::dbModITerm::create(inst_1, "out1");
moditerm_out1->setChildModBTerm(modbterm_a_out1);
modbterm_a_out1->setParentModITerm(moditerm_out1);
auto* moditerm_out2 = odb::dbModITerm::create(inst_1, "out2");
moditerm_out2->setChildModBTerm(modbterm_a_out2);
modbterm_a_out2->setParentModITerm(moditerm_out2);
// Create and connect top-level ports and nets
auto* net_top_in1 = odb::dbNet::create(block_, "top_in1");
auto* top_in1 = odb::dbBTerm::create(net_top_in1, "top_in1");
top_in1->setIoType(odb::dbIoType::INPUT);
auto* net_top_in2 = odb::dbNet::create(block_, "top_in2");
auto* top_in2 = odb::dbBTerm::create(net_top_in2, "top_in2");
top_in2->setIoType(odb::dbIoType::INPUT);
auto* net_top_out1 = odb::dbNet::create(block_, "top_out1");
auto* top_out1 = odb::dbBTerm::create(net_top_out1, "top_out1");
top_out1->setIoType(odb::dbIoType::OUTPUT);
auto* mod_net_in1 = odb::dbModNet::create(top_module, "mod_net_in1");
top_in1->connect(mod_net_in1);
moditerm_in1->connect(mod_net_in1);
auto* mod_net_in2 = odb::dbModNet::create(top_module, "mod_net_in2");
top_in2->connect(mod_net_in2);
moditerm_in2->connect(mod_net_in2);
auto* mod_net_out1 = odb::dbModNet::create(top_module, "mod_net_out1");
top_out1->connect(mod_net_out1);
moditerm_out1->connect(mod_net_out1);
// inst_1/out2 and inst_1/unconnected_in are intentionally not connected at
// the top level to increase test coverage.
// Pre-swap checks
ASSERT_EQ(inst_1->getMaster(), module_a);
ASSERT_EQ(module_a->getInsts().size(), 3);
ASSERT_NE(block_->findModule("module_A"), nullptr);
// Dump module_A contents for easier debugging
printf("--- Dumping module_A (%s) contents ---\n", module_a->getName());
printf("Instances:\n");
for (auto* inst : module_a->getInsts()) {
printf(" - %s (Master: %s)\n",
inst->getName().c_str(),
inst->getMaster()->getName().c_str());
}
printf("Module Instances:\n");
for (auto* mod_inst : module_a->getModInsts()) {
printf(" - %s (Master: %s)\n",
mod_inst->getName(),
mod_inst->getMaster()->getName());
}
printf("Module Nets:\n");
for (auto* mod_net : module_a->getModNets()) {
printf(" - %s\n", mod_net->getName().c_str());
}
printf("--- End of dump ---\n");
// 2. Action
auto* new_inst_1 = inst_1->swapMaster(module_b);
// 3. Verification
ASSERT_NE(new_inst_1, nullptr);
EXPECT_STREQ(new_inst_1->getName(), "inst_1");
// Master checks
auto* new_master = new_inst_1->getMaster();
ASSERT_NE(new_master, nullptr);
EXPECT_NE(new_master, module_b); // Should be a unique copy
EXPECT_EQ(new_master->getInsts().size(), 4);
// Dump new_master contents for easier debugging
printf("--- Dumping new_master (%s) contents ---\n", new_master->getName());
printf("Instances:\n");
for (auto* inst : new_master->getInsts()) {
printf(" - %s (Master: %s)\n",
inst->getName().c_str(),
inst->getMaster()->getName().c_str());
}
printf("Module Instances:\n");
for (auto* mod_inst : new_master->getModInsts()) {
printf(" - %s (Master: %s)\n",
mod_inst->getName(),
mod_inst->getMaster()->getName());
}
printf("Module Nets:\n");
for (auto* mod_net : new_master->getModNets()) {
printf(" - %s\n", mod_net->getName().c_str());
}
printf("--- End of dump ---\n");
// Check internal instances of the new master by name
auto* new_buf1 = new_master->findDbInst("inst_1/buf1_b");
ASSERT_NE(new_buf1, nullptr);
EXPECT_EQ(new_buf1->getMaster(), buf_master);
auto* new_buf2 = new_master->findDbInst("inst_1/buf2_b");
ASSERT_NE(new_buf2, nullptr);
EXPECT_EQ(new_buf2->getMaster(), buf_master);
auto* new_nor1 = new_master->findDbInst("inst_1/nor1_b");
ASSERT_NE(new_nor1, nullptr);
EXPECT_EQ(new_nor1->getMaster(), nor_master);
// Check backup module
// - module_A still exists in the child block of the top block.
// - swapMaster() moves the old master to a child block as a backup.
ASSERT_NE(block_->findModule("module_A"), nullptr);
// Port and connection checks
auto* new_moditerm_in1 = new_inst_1->findModITerm("in1");
ASSERT_NE(new_moditerm_in1, nullptr);
EXPECT_EQ(new_moditerm_in1->getModNet(), mod_net_in1);
auto* new_moditerm_out1 = new_inst_1->findModITerm("out1");
ASSERT_NE(new_moditerm_out1, nullptr);
EXPECT_EQ(new_moditerm_out1->getModNet(), mod_net_out1);
// Check unconnected ports still exist
auto* new_moditerm_out2 = new_inst_1->findModITerm("out2");
ASSERT_NE(new_moditerm_out2, nullptr);
EXPECT_EQ(new_moditerm_out2->getModNet(), nullptr); // Was not connected
auto* new_moditerm_unconnected_in
= new_inst_1->findModITerm("unconnected_in");
ASSERT_NE(new_moditerm_unconnected_in, nullptr);
EXPECT_EQ(new_moditerm_unconnected_in->getModNet(),
nullptr); // Was not connected
// Check internal connectivity of the new master
auto* new_master_in1 = new_master->findModBTerm("in1");
ASSERT_NE(new_master_in1, nullptr);
auto* new_master_in1_net = new_master_in1->getModNet();
ASSERT_NE(new_master_in1_net, nullptr);
EXPECT_EQ(new_master_in1_net->getITerms().size(), 1);
EXPECT_EQ((*new_master_in1_net->getITerms().begin())->getInst()->getMaster(),
buf_master);
auto* new_master_unconnected_in = new_master->findModBTerm("unconnected_in");
ASSERT_NE(new_master_unconnected_in, nullptr);
EXPECT_EQ(new_master_unconnected_in->getModNet(),
nullptr); // Was not connected internally
}
} // namespace odb |