|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright (c) 2025, The OpenROAD Authors |
| 3 | + |
| 4 | +// Regression test: write_verilog -remove_cells must not drop hierarchical |
| 5 | +// module instances. |
| 6 | +// |
| 7 | +// Background: |
| 8 | +// CellSet uses CellIdLess which compares cells by network_->id(cell). |
| 9 | +// Before the fix, ConcreteCell used raw sequential id_ (0,1,2,...) while |
| 10 | +// dbModule used (db_id << 4) | 0x8 (24,40,56,...). These ID spaces |
| 11 | +// overlapped, so CellSet::contains could treat a dbModule as equal to a |
| 12 | +// ConcreteCell, causing writeChild to silently drop hierarchical instances. |
| 13 | +// |
| 14 | +// The fix tags ConcreteCell IDs with CONCRETE_OBJECT_ID (0xF) so they |
| 15 | +// never collide with dbModule IDs (tag 0x8). |
| 16 | + |
| 17 | +#include <cstdio> |
| 18 | +#include <fstream> |
| 19 | +#include <iterator> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "gtest/gtest.h" |
| 23 | +#include "sta/Network.hh" |
| 24 | +#include "sta/NetworkClass.hh" |
| 25 | +#include "sta/PatternMatch.hh" |
| 26 | +#include "sta/VerilogWriter.hh" |
| 27 | +#include "tst/IntegratedFixture.h" |
| 28 | + |
| 29 | +namespace sta { |
| 30 | + |
| 31 | +class TestWriteVerilog : public tst::IntegratedFixture |
| 32 | +{ |
| 33 | + public: |
| 34 | + TestWriteVerilog() |
| 35 | + : tst::IntegratedFixture(tst::IntegratedFixture::Technology::Nangate45, |
| 36 | + "_main/src/dbSta/test/") |
| 37 | + { |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +// Verifies that write_verilog -remove_cells does not drop hierarchical |
| 42 | +// instances when a liberty cell is removed. |
| 43 | +// |
| 44 | +// Puts a liberty cell (INV_X1) in remove_cells, then verifies that the |
| 45 | +// hierarchical instance (sub_inst) is still present in the output. |
| 46 | +// Before the fix, this could fail when the ConcreteCell's raw id_ |
| 47 | +// collided with the dbModule's computed id. |
| 48 | +TEST_F(TestWriteVerilog, RemoveCellsIdCollision) |
| 49 | +{ |
| 50 | + const std::string test_name = "TestWriteVerilog_RemoveCellsIdCollision"; |
| 51 | + |
| 52 | + // Read a simple hierarchical design: top -> sub_mod -> INV_X1 |
| 53 | + readVerilogAndSetup(test_name + "_pre.v", /*init_default_sdc=*/false); |
| 54 | + |
| 55 | + Network* network = sta_->network(); |
| 56 | + Instance* top_inst = network->topInstance(); |
| 57 | + |
| 58 | + // Find the hierarchical instance and its Cell* (which is a dbModule*) |
| 59 | + Cell* hier_cell = nullptr; |
| 60 | + std::string hier_inst_name; |
| 61 | + InstanceChildIterator* child_iter = network->childIterator(top_inst); |
| 62 | + while (child_iter->hasNext()) { |
| 63 | + Instance* child = child_iter->next(); |
| 64 | + if (network->isHierarchical(child)) { |
| 65 | + hier_cell = network->cell(child); |
| 66 | + hier_inst_name = network->name(child); |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + delete child_iter; |
| 71 | + ASSERT_NE(hier_cell, nullptr) << "No hierarchical instance found in design"; |
| 72 | + |
| 73 | + ObjectId hier_id = network->id(hier_cell); |
| 74 | + |
| 75 | + // Verify no ConcreteCell shares the same id as the hier module. |
| 76 | + // After the fix, ConcreteCell IDs use tag 0xF while dbModule IDs |
| 77 | + // use tag 0x8, so no collision is possible. |
| 78 | + PatternMatch match_all("*", |
| 79 | + /*is_regexp=*/false, |
| 80 | + /*nocase=*/false, |
| 81 | + sta_->tclInterp()); |
| 82 | + |
| 83 | + LibraryIterator* lib_iter = network->libraryIterator(); |
| 84 | + while (lib_iter->hasNext()) { |
| 85 | + Library* lib = lib_iter->next(); |
| 86 | + CellSeq lib_cells = network->findCellsMatching(lib, &match_all); |
| 87 | + for (Cell* cell : lib_cells) { |
| 88 | + if (cell != hier_cell) { |
| 89 | + EXPECT_NE(network->id(cell), hier_id) |
| 90 | + << "ObjectId collision between ConcreteCell '" |
| 91 | + << network->name(cell) << "' and dbModule '" |
| 92 | + << network->name(hier_cell) << "' — tagging should prevent this"; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + delete lib_iter; |
| 97 | + |
| 98 | + // Find INV_X1 liberty cell to use in remove_cells |
| 99 | + Cell* inv_cell = nullptr; |
| 100 | + LibraryIterator* lib_iter2 = network->libraryIterator(); |
| 101 | + while (lib_iter2->hasNext()) { |
| 102 | + Library* lib = lib_iter2->next(); |
| 103 | + PatternMatch inv_match("INV_X1", |
| 104 | + /*is_regexp=*/false, |
| 105 | + /*nocase=*/false, |
| 106 | + sta_->tclInterp()); |
| 107 | + CellSeq cells = network->findCellsMatching(lib, &inv_match); |
| 108 | + if (cells.empty() == false) { |
| 109 | + inv_cell = cells[0]; |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + delete lib_iter2; |
| 114 | + ASSERT_NE(inv_cell, nullptr) << "INV_X1 not found in libraries"; |
| 115 | + |
| 116 | + // Put INV_X1 in remove_cells and write verilog |
| 117 | + CellSeq remove_cells; |
| 118 | + remove_cells.push_back(inv_cell); |
| 119 | + |
| 120 | + const std::string output_file = test_name + "_out.v"; |
| 121 | + writeVerilog(output_file.c_str(), false, &remove_cells, network); |
| 122 | + |
| 123 | + // Read the output |
| 124 | + std::ifstream ifs(output_file); |
| 125 | + ASSERT_TRUE(ifs.is_open()) << "Failed to open output file: " << output_file; |
| 126 | + std::string content((std::istreambuf_iterator<char>(ifs)), |
| 127 | + std::istreambuf_iterator<char>()); |
| 128 | + ifs.close(); |
| 129 | + |
| 130 | + // The hierarchical instance must be present in the output. |
| 131 | + // Only leaf INV_X1 instances should be removed, not hierarchical modules. |
| 132 | + EXPECT_NE(content.find(hier_inst_name), std::string::npos) |
| 133 | + << "Hierarchical instance '" << hier_inst_name |
| 134 | + << "' was incorrectly removed from write_verilog output.\n" |
| 135 | + << " write_verilog -remove_cells should never drop hierarchical " |
| 136 | + "instances."; |
| 137 | + |
| 138 | + // Clean up |
| 139 | + std::remove(output_file.c_str()); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace sta |
0 commit comments