File size: 13,184 Bytes
f61bac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023-2025, The OpenROAD Authors

#include <cassert>
#include <cstdio>
#include <string>

#include "db_sta/dbNetwork.hh"
#include "gtest/gtest.h"
#include "odb/db.h"
#include "odb/dbTypes.h"
#include "sta/Graph.hh"
#include "sta/NetworkClass.hh"
#include "sta/Path.hh"
#include "sta/SdcClass.hh"
#include "sta/Sta.hh"
#include "tst/IntegratedFixture.h"

namespace sta {

class TestDbSta : public tst::IntegratedFixture
{
 protected:
  TestDbSta()
      : tst::IntegratedFixture(tst::IntegratedFixture::Technology::kNangate45,
                               "_main/src/dbSta/test/")
  {
  }
};

TEST_F(TestDbSta, TestHierarchyConnectivity)
{
  std::string test_name = "TestDbSta_0";
  readVerilogAndSetup(test_name + ".v");

  Pin* sta_hier_pin = db_network_->findPin("sub_inst/mod_in");
  ASSERT_NE(sta_hier_pin, nullptr);

  odb::dbModNet* modnet = block_->findModNet("sub_inst/mod_in");
  ASSERT_NE(modnet, nullptr);

  odb::dbNet* dbnet = block_->findNet("net2");
  ASSERT_NE(dbnet, nullptr);

  Net* sta_modnet = db_network_->dbToSta(modnet);
  Net* sta_net = db_network_->dbToSta(dbnet);

  odb::dbInst* buf_inst = block_->findInst("buf");
  ASSERT_NE(buf_inst, nullptr);

  // Sanity check
  db_network_->checkAxioms();

  // Check connectivity b/w Net* and Pin*
  bool bool_return;
  bool_return = db_network_->isConnected(sta_modnet, sta_hier_pin);
  ASSERT_TRUE(bool_return);

  bool_return = db_network_->isConnected(sta_net, sta_hier_pin);
  ASSERT_TRUE(bool_return);

  // Check connectivity b/w Net* and Net*
  bool_return = db_network_->isConnected(sta_modnet, sta_net);
  ASSERT_TRUE(bool_return);

  bool_return = db_network_->isConnected(sta_net, sta_modnet);
  ASSERT_TRUE(bool_return);

  // Check Network::highestNetAbove(Net* net)
  odb::dbNet* dbnet_out2 = block_->findNet("out2");
  ASSERT_NE(dbnet_out2, nullptr);
  Net* sta_dbnet_out2 = db_network_->dbToSta(dbnet_out2);
  ASSERT_NE(sta_dbnet_out2, nullptr);
  Net* sta_highest_net = db_network_->highestNetAbove(sta_dbnet_out2);
  ASSERT_EQ(sta_highest_net, sta_dbnet_out2);

  odb::dbModNet* modnet_mod_out = block_->findModNet("sub_inst/mod_out");
  ASSERT_NE(modnet_mod_out, nullptr);
  Net* sta_modnet_mod_out = db_network_->dbToSta(modnet_mod_out);
  ASSERT_NE(sta_modnet_mod_out, nullptr);
  odb::dbModNet* modnet_out2 = block_->findModNet("out2");
  ASSERT_NE(modnet_out2, nullptr);
  Net* sta_modnet_out2 = db_network_->dbToSta(modnet_out2);
  ASSERT_NE(sta_modnet_out2, nullptr);
  // highestNetAbove on a mod net now returns the associated flat dbNet, not the
  // highest mod net, so that parasitic externality checks work correctly.
  Net* sta_highest_modnet_out
      = db_network_->highestNetAbove(sta_modnet_mod_out);
  ASSERT_EQ(sta_highest_modnet_out, sta_dbnet_out2);

  // Regression for PR #3194: dbNetwork::highestConnectedNet on a hier
  // dbModNet must return the associated flat dbNet, not the modnet wrapper
  // itself. Parasitics::findParasiticNet uses this as the lookup key into
  // the parasitic network map. SPEF is a flat format so parasitics are
  // keyed by flat dbNet; returning the modnet wrapper caused a cache miss
  // and silently degraded delay calculation to LumpedCap.
  const Net* sta_highest_connected_modnet
      = db_network_->highestConnectedNet(sta_modnet_mod_out);
  ASSERT_EQ(sta_highest_connected_modnet, sta_dbnet_out2);

  // Flat dbNet input: highestConnectedNet is idempotent.
  const Net* sta_highest_connected_flat
      = db_network_->highestConnectedNet(sta_dbnet_out2);
  ASSERT_EQ(sta_highest_connected_flat, sta_dbnet_out2);

  // Check get_ports -of_object Net*
  NetTermIterator* term_iter = db_network_->termIterator(sta_dbnet_out2);
  while (term_iter->hasNext()) {
    Term* term = term_iter->next();
    Pin* pin = db_network_->pin(term);
    Port* port = db_network_->port(pin);
    ASSERT_EQ(db_network_->name(port), block_->findBTerm("out2")->getName());
  }

  // Check dbBTerm::getITerm()
  odb::dbBTerm* bterm_clk = block_->findBTerm("in1");
  ASSERT_NE(bterm_clk, nullptr);
  // There is no related dbITerm for a dbBTerm
  ASSERT_EQ(bterm_clk->getITerm(), nullptr);
}

// Coverage for dbNetwork::findFlatDbNet overloads. Verifies that a flat
// dbNet can be reached from any of: a leaf iterm pin, a top-level bterm
// pin, a hierarchical moditerm pin, a flat dbNet, and a hierarchical
// dbModNet. Pair this with the FlatNet test, which characterizes the
// non-hierarchy-resolving flatNet overloads.
TEST_F(TestDbSta, FindFlatDbNet)
{
  std::string test_name = "TestDbSta_0";
  readVerilogAndSetup(test_name + ".v");

  odb::dbNet* dbnet_net2 = block_->findNet("net2");
  ASSERT_NE(dbnet_net2, nullptr);

  odb::dbModNet* modnet_mod_in = block_->findModNet("sub_inst/mod_in");
  ASSERT_NE(modnet_mod_in, nullptr);
  // The hier net for sub_inst/mod_in shares the flat net "net2".
  ASSERT_EQ(modnet_mod_in->findRelatedNet(), dbnet_net2);

  // 1. findFlatDbNet(Pin*) on an iterm pin (buf/Z drives net2).
  odb::dbInst* buf_inst = block_->findInst("buf");
  ASSERT_NE(buf_inst, nullptr);
  Pin* iterm_pin = db_network_->dbToSta(buf_inst->findITerm("Z"));
  ASSERT_NE(iterm_pin, nullptr);
  EXPECT_EQ(db_network_->findFlatDbNet(iterm_pin), dbnet_net2);

  // 2. findFlatDbNet(Pin*) on a top-level bterm pin (in1).
  odb::dbBTerm* bterm_in1 = block_->findBTerm("in1");
  ASSERT_NE(bterm_in1, nullptr);
  Pin* bterm_pin = db_network_->dbToSta(bterm_in1);
  ASSERT_NE(bterm_pin, nullptr);
  EXPECT_EQ(db_network_->findFlatDbNet(bterm_pin), bterm_in1->getNet());

  // 3. findFlatDbNet(Pin*) on a moditerm pin (sub_inst/mod_in).
  Pin* moditerm_pin = db_network_->findPin("sub_inst/mod_in");
  ASSERT_NE(moditerm_pin, nullptr);
  EXPECT_EQ(db_network_->findFlatDbNet(moditerm_pin), dbnet_net2);

  // 4. findFlatDbNet(Net*) on a flat dbNet returns the same dbNet.
  Net* sta_dbnet = db_network_->dbToSta(dbnet_net2);
  EXPECT_EQ(db_network_->findFlatDbNet(sta_dbnet), dbnet_net2);

  // 5. findFlatDbNet(Net*) on a dbModNet resolves to the related flat
  // dbNet.
  Net* sta_modnet = db_network_->dbToSta(modnet_mod_in);
  EXPECT_EQ(db_network_->findFlatDbNet(sta_modnet), dbnet_net2);

  // 6. findFlatDbNet(Net*) on null returns null.
  EXPECT_EQ(db_network_->findFlatDbNet(static_cast<Net*>(nullptr)), nullptr);
}

// Characterization tests for dbNetwork::flatNet in the presence of
// hierarchy. flatNet does NOT walk the hierarchy: a hierarchical input
// (moditerm Pin*, dbModNet Net*) yields nullptr even when a related flat
// dbNet exists. Callers that need hierarchical resolution must use
// findFlatDbNet. These expectations pin down the current behavior so any
// future change to flatNet is intentional and reviewed.
TEST_F(TestDbSta, FlatNet)
{
  std::string test_name = "TestDbSta_0";
  readVerilogAndSetup(test_name + ".v");

  odb::dbNet* dbnet_net2 = block_->findNet("net2");
  ASSERT_NE(dbnet_net2, nullptr);

  odb::dbModNet* modnet_mod_in = block_->findModNet("sub_inst/mod_in");
  ASSERT_NE(modnet_mod_in, nullptr);
  // Sanity: the hierarchical net for sub_inst/mod_in shares flat net "net2".
  ASSERT_EQ(modnet_mod_in->findRelatedNet(), dbnet_net2);

  // 1. flatNet(Pin*) on a leaf iterm pin returns the directly attached
  // flat dbNet.
  odb::dbInst* buf_inst = block_->findInst("buf");
  ASSERT_NE(buf_inst, nullptr);
  Pin* iterm_pin = db_network_->dbToSta(buf_inst->findITerm("Z"));
  ASSERT_NE(iterm_pin, nullptr);
  EXPECT_EQ(db_network_->flatNet(iterm_pin), dbnet_net2);

  // 2. flatNet(Pin*) on a top-level bterm pin returns the directly
  // attached flat dbNet.
  odb::dbBTerm* bterm_in1 = block_->findBTerm("in1");
  ASSERT_NE(bterm_in1, nullptr);
  Pin* bterm_pin = db_network_->dbToSta(bterm_in1);
  ASSERT_NE(bterm_pin, nullptr);
  EXPECT_EQ(db_network_->flatNet(bterm_pin), bterm_in1->getNet());

  // 3. flatNet(Pin*) on a hierarchical moditerm pin returns nullptr —
  // moditerms attach only to modnets, and flatNet does not resolve through
  // the modnet. Callers needing the related flat dbNet must use
  // findFlatDbNet instead.
  Pin* moditerm_pin = db_network_->findPin("sub_inst/mod_in");
  ASSERT_NE(moditerm_pin, nullptr);
  EXPECT_EQ(db_network_->flatNet(moditerm_pin), nullptr);

  // 4. flatNet(Net*) on a flat dbNet returns the same dbNet.
  Net* sta_dbnet = db_network_->dbToSta(dbnet_net2);
  EXPECT_EQ(db_network_->flatNet(sta_dbnet), dbnet_net2);

  // 5. flatNet(Net*) on a hierarchical dbModNet returns nullptr — flatNet
  // does not resolve through the modnet to its related flat dbNet.
  Net* sta_modnet = db_network_->dbToSta(modnet_mod_in);
  EXPECT_EQ(db_network_->flatNet(sta_modnet), nullptr);

  // 6. flatNet(Net*) on null returns null.
  EXPECT_EQ(db_network_->flatNet(static_cast<Net*>(nullptr)), nullptr);

  // 7. flatNet(Term*) on a top-level bterm returns its dbNet.
  Term* bterm_term = db_network_->dbToStaTerm(bterm_in1);
  ASSERT_NE(bterm_term, nullptr);
  EXPECT_EQ(db_network_->flatNet(bterm_term), bterm_in1->getNet());
}

// Reassociating a moditerm pin must preserve its modnet connection and not
// trip ORD-2026 in connectPin. moditerms only attach to modnets, so the
// flat-net leg of reassociatePinConnection must resolve to nullptr for
// these pins. Regression for the hierarchical repair_tie_fanout flow.
TEST_F(TestDbSta, ReassociateModITermPin)
{
  std::string test_name = "TestDbSta_0";
  readVerilogAndSetup(test_name + ".v");

  Pin* moditerm_pin = db_network_->findPin("sub_inst/mod_in");
  ASSERT_NE(moditerm_pin, nullptr);

  odb::dbITerm* iterm = nullptr;
  odb::dbBTerm* bterm = nullptr;
  odb::dbModITerm* moditerm = nullptr;
  db_network_->staToDb(moditerm_pin, iterm, bterm, moditerm);
  ASSERT_EQ(iterm, nullptr);
  ASSERT_EQ(bterm, nullptr);
  ASSERT_NE(moditerm, nullptr);

  odb::dbModNet* original_modnet = moditerm->getModNet();
  ASSERT_NE(original_modnet, nullptr);
  // The pin's modnet has a related flat dbNet, so a buggy reassociate that
  // forwarded findFlatDbNet's result into connectPin would trigger ORD-2026.
  ASSERT_NE(db_network_->findFlatDbNet(moditerm_pin), nullptr);

  db_network_->reassociatePinConnection(moditerm_pin);

  EXPECT_EQ(moditerm->getModNet(), original_modnet);
  EXPECT_TRUE(db_network_->isConnected(db_network_->dbToSta(original_modnet),
                                       moditerm_pin));
  db_network_->checkAxioms();
}

// Regression for #10210 (stale Path* dereference in rsz).
//
// Topology (TestDbSta_StalePrevPath.v):
//   clk -> b1(BUF) -> inv1(INV) -> nd1(NAND2) -> out1
//                                   nd1/A2 <- in2
//
// Flow:
//   1. Capture drvr_path at nd1/ZN and snapshot prevPath() pointer + pin name
//   2. Delete upstream b1 + updateTiming -> free
//   3. Add a fresh BUF + clock + updateTiming -> recycle
//   4. Assert the captured Path's prev slot has been recycled: pin()
//      decodes to data that belongs to a different instance than nd1's
//      real input.
TEST_F(TestDbSta, StalePrevPath)
{
  const auto* test_info = testing::UnitTest::GetInstance()->current_test_info();
  const std::string test_name
      = std::string(test_info->test_suite_name()) + "_" + test_info->name();
  readVerilogAndSetup(test_name + ".v");
  sta_->updateTiming(true);

  Network* network = sta_->network();

  Instance* nd1 = db_network_->dbToSta(block_->findInst("nd1"));
  Path* drvr_path = sta_->vertexWorstArrivalPath(
      sta_->ensureGraph()->pinDrvrVertex(network->findPin(nd1, "ZN")),
      MinMax::max());
  ASSERT_NE(drvr_path, nullptr);
  ASSERT_EQ(network->pathName(drvr_path->pin(sta_.get())), "nd1/ZN");
  const Path* pre_addr = drvr_path->prevPath();
  ASSERT_NE(pre_addr, nullptr);
  const std::string pre_pin_name = network->pathName(pre_addr->pin(sta_.get()));

  // 2. Free upstream Path[] slots.
  sta_->deleteInstance(db_network_->dbToSta(block_->findInst("b1")));
  sta_->updateTiming(true);

  // 3. Recycle freed slots via a single fresh BUF driven by a new clock.
  odb::dbNet* in3_net = odb::dbNet::create(block_, "in3");
  odb::dbBTerm* new_bt = odb::dbBTerm::create(in3_net, "in3");
  new_bt->setIoType(odb::dbIoType::INPUT);
  odb::dbNet* nfan_net = odb::dbNet::create(block_, "nfan");
  odb::dbInst* bnew
      = odb::dbInst::create(block_, db_->findMaster("BUF_X1"), "bnew");
  bnew->findITerm("A")->connect(in3_net);
  bnew->findITerm("Z")->connect(nfan_net);

  PinSet clk2_pins(db_network_);
  clk2_pins.insert(db_network_->dbToSta(new_bt));
  FloatSeq clk2_waveform = {0.0f, 0.1f};
  sta_->makeClock(
      "clk2", clk2_pins, false, 0.2f, clk2_waveform, "", sta_->cmdMode());
  sta_->updateTiming(true);

  // 4. Staleness evidence. Pointer address is same but pin name has changed.
  const Path* post_addr = drvr_path->prevPath();
  const std::string post_pin_name
      = post_addr ? network->pathName(post_addr->pin(sta_.get()))
                  : std::string("<null>");

  EXPECT_EQ(pre_addr, post_addr)
      << "stale-pointer signature: prev_path_ address unchanged";
  EXPECT_NE(pre_pin_name, post_pin_name)
      << "but slot content should differ after free+reuse. before="
      << pre_pin_name << " after=" << post_pin_name;
}

}  // namespace sta