File size: 4,994 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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors

#include <cmath>
#include <cstdint>

#include "gtest/gtest.h"
#include "odb/db.h"
#include "timing_report.h"
#include "tst/nangate45_fixture.h"

namespace web {
namespace {

class NetLengthTest : public tst::Nangate45Fixture
{
 protected:
  void SetUp() override
  {
    block_->setDieArea(odb::Rect(0, 0, 200000, 200000));
    block_->setCoreArea(odb::Rect(0, 0, 200000, 200000));
  }

  odb::dbInst* placeInst(const char* master, const char* name, int x, int y)
  {
    odb::dbMaster* m = lib_->findMaster(master);
    EXPECT_NE(m, nullptr);
    odb::dbInst* inst = odb::dbInst::create(block_, m, name);
    inst->setLocation(x, y);
    inst->setPlacementStatus(odb::dbPlacementStatus::PLACED);
    return inst;
  }

  // Any iterm of `inst` matching `io`, falling back to the first iterm.
  static odb::dbITerm* iterm(odb::dbInst* inst, odb::dbIoType io)
  {
    odb::dbITerm* first = nullptr;
    for (odb::dbITerm* it : inst->getITerms()) {
      if (it->getSigType().isSupply()) {
        continue;
      }
      if (!first) {
        first = it;
      }
      if (it->getIoType() == io) {
        return it;
      }
    }
    return first;
  }
};

TEST_F(NetLengthTest, HpwlMatchesTermBBox)
{
  odb::dbInst* b1 = placeInst("BUF_X16", "b1", 0, 0);
  odb::dbInst* b2 = placeInst("BUF_X16", "b2", 20000, 8000);
  odb::dbNet* net = odb::dbNet::create(block_, "n1");
  net->setSigType(odb::dbSigType::SIGNAL);
  iterm(b1, odb::dbIoType::OUTPUT)->connect(net);
  iterm(b2, odb::dbIoType::INPUT)->connect(net);

  const odb::Rect bbox = net->getTermBBox();
  EXPECT_EQ(netHpwlDbu(net), bbox.dx() + bbox.dy());
  EXPECT_GT(netHpwlDbu(net), 0);
}

TEST_F(NetLengthTest, HistogramCountsSignalNetsOnly)
{
  odb::dbInst* b1 = placeInst("BUF_X16", "b1", 0, 0);
  odb::dbInst* b2 = placeInst("BUF_X16", "b2", 20000, 8000);

  odb::dbNet* sig = odb::dbNet::create(block_, "sig");
  sig->setSigType(odb::dbSigType::SIGNAL);
  iterm(b1, odb::dbIoType::OUTPUT)->connect(sig);
  iterm(b2, odb::dbIoType::INPUT)->connect(sig);

  odb::dbNet* pwr = odb::dbNet::create(block_, "VDD");
  pwr->setSigType(odb::dbSigType::POWER);

  NetLengthHistogramResult h = computeNetLengthHistogram(block_, true);
  EXPECT_EQ(h.total_nets, 1);  // supply net excluded
  EXPECT_EQ(h.length_unit, "DBU");

  int sum = 0;
  for (const auto& bin : h.bins) {
    sum += bin.count;
  }
  EXPECT_EQ(sum, h.total_nets);
}

TEST_F(NetLengthTest, UnitLabelFollowsUseDbu)
{
  EXPECT_EQ(computeNetLengthHistogram(block_, true).length_unit, "DBU");
  EXPECT_EQ(computeNetLengthHistogram(block_, false).length_unit, "µm");
}

TEST_F(NetLengthTest, EmptyBlockHasNoNets)
{
  NetLengthHistogramResult h = computeNetLengthHistogram(block_, false);
  EXPECT_EQ(h.total_nets, 0);
  EXPECT_TRUE(h.bins.empty());
}

// The bins are upper-exclusive and so is select_net_length_bin, so the
// longest net must fall strictly inside the last bin.  When it only lands
// there by clamping, the bar counts nets that double-clicking it then fails
// to select.  The bug needs the longest net to be an exact multiple of the
// snapped bin width, so drive HPWL to chosen round values rather than hoping
// a geometry sweep lands on one.
TEST_F(NetLengthTest, LongestNetIsBelowTheLastBinUpperEdge)
{
  block_->setDieArea(odb::Rect(0, 0, 400000, 400000));
  odb::dbInst* src = placeInst("BUF_X16", "src", 0, 0);
  odb::dbInst* sink = placeInst("BUF_X16", "sink", 100000, 0);
  odb::dbNet* net = odb::dbNet::create(block_, "n");
  net->setSigType(odb::dbSigType::SIGNAL);
  iterm(src, odb::dbIoType::OUTPUT)->connect(net);
  iterm(sink, odb::dbIoType::INPUT)->connect(net);

  // HPWL tracks the sink's x with a fixed offset (pin geometry plus the
  // constant dy), so solve for the placement that yields an exact length.
  const int64_t offset = netHpwlDbu(net) - 100000;

  for (const int64_t target : {10000, 20000, 50000, 100000, 200000}) {
    sink->setLocation(static_cast<int>(target - offset), 0);
    ASSERT_EQ(netHpwlDbu(net), target);

    const NetLengthHistogramResult h = computeNetLengthHistogram(block_, true);
    ASSERT_FALSE(h.bins.empty()) << "target=" << target;
    const double bin_width = h.bins.front().upper - h.bins.front().lower;
    ASSERT_GT(bin_width, 0) << "target=" << target;
    // The case that used to break: the longest net sits exactly on a bin edge.
    ASSERT_EQ(std::fmod(static_cast<double>(target), bin_width), 0.0)
        << "target=" << target << " bin_width=" << bin_width;

    EXPECT_LT(static_cast<double>(target), h.bins.back().upper)
        << "target=" << target;
    EXPECT_GT(h.bins.back().count, 0)
        << "empty trailing bar, target=" << target;

    int sum = 0;
    for (const auto& bin : h.bins) {
      sum += bin.count;
    }
    EXPECT_EQ(sum, h.total_nets) << "target=" << target;
  }
}

TEST(NetHpwlTest, NullNetIsZero)
{
  EXPECT_EQ(netHpwlDbu(nullptr), 0);
}

}  // namespace
}  // namespace web