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

#include <memory>
#include <utility>
#include <vector>

#include "FastRoute.h"
#include "gtest/gtest.h"
#include "odb/PtrSetMap.h"
#include "odb/db.h"
#include "odb/geom.h"
#include "stt/SteinerTreeBuilder.h"
#include "tst/db_fixture.h"
#include "utl/ServiceRegistry.h"

namespace grt {
namespace {

// Exercises FastRouteCore::getCongestionNets on a small synthetic grid.
//
// The state getCongestionNets reads (routed tree edges and 2D edge usage)
// is built entirely through the public API: addNet() registers a net and
// its (empty) Steiner tree, addTreeEdge() appends a routed edge and bumps
// graph2d_ usage, and updateEdge2DAnd3DUsage() adds raw usage to create
// overflow at chosen cells without giving any scannable net a route there.
class CongestionNetsTest : public tst::DbFixture
{
 protected:
  static constexpr int kXGrid = 10;
  static constexpr int kYGrid = 10;
  static constexpr int kNumLayers = 2;
  // Per-layer edge capacity; the 2D capacity of an edge is the sum over
  // layers.  Routes added in tests use 1 unit per edge, so they never
  // overflow on their own.
  static constexpr int kEdgeCap = 10;
  static constexpr int kOverflowUsage = kEdgeCap * kNumLayers + 5;

  void SetUp() override
  {
    odb::dbTech* tech = odb::dbTech::create(db_.get(), "tech");
    odb::dbTechLayer::create(tech, "L1", odb::dbTechLayerType::MASTERSLICE);
    odb::dbChip* chip = odb::dbChip::create(db_.get(), tech);
    block_ = odb::dbBlock::create(chip, "top");

    fastroute_ = std::make_unique<FastRouteCore>(
        db_.get(), &logger_, &registry_, &stt_builder_, /*sta=*/nullptr);
    fastroute_->setLowerLeft(0, 0);
    fastroute_->setTileSize(100);
    fastroute_->setGridsAndLayers(kXGrid, kYGrid, kNumLayers);
    fastroute_->initEdges();
    for (int layer = 1; layer <= kNumLayers; layer++) {
      for (int y = 0; y < kYGrid; y++) {
        for (int x = 0; x < kXGrid - 1; x++) {
          fastroute_->setEdgeCapacity(x, y, x + 1, y, layer, kEdgeCap);
        }
      }
      for (int y = 0; y < kYGrid - 1; y++) {
        for (int x = 0; x < kXGrid; x++) {
          fastroute_->setEdgeCapacity(x, y, x, y + 1, layer, kEdgeCap);
        }
      }
    }

    // Carrier net for injecting overflow usage.  It is a clock net with no
    // tree edges, so getCongestionNets never selects it.
    overflow_net_ = makeFrNet("__overflow_carrier", /*is_clock=*/true);
  }

  odb::dbNet* makeFrNet(const char* name, const bool is_clock = false)
  {
    odb::dbNet* db_net = odb::dbNet::create(block_, name);
    fastroute_->addNet(db_net,
                       is_clock,
                       /*is_local=*/false,
                       /*driver_idx=*/0,
                       /*cost=*/1,
                       /*min_layer=*/0,
                       /*max_layer=*/kNumLayers - 1,
                       /*slack=*/0,
                       /*edge_cost_per_layer=*/nullptr);
    return db_net;
  }

  // Routes on layer 1; coordinates must be ascending.
  void addRouteH(odb::dbNet* net, const int x1, const int x2, const int y)
  {
    fastroute_->addTreeEdge(x1, y, x2, y, /*layer=*/1, net);
  }

  void addRouteV(odb::dbNet* net, const int x, const int y1, const int y2)
  {
    fastroute_->addTreeEdge(x, y1, x, y2, /*layer=*/1, net);
  }

  void overflowH(const int x, const int y)
  {
    fastroute_->updateEdge2DAnd3DUsage(
        x, y, x + 1, y, /*layer=*/1, kOverflowUsage, overflow_net_);
  }

  void overflowV(const int x, const int y)
  {
    fastroute_->updateEdge2DAnd3DUsage(
        x, y, x, y + 1, /*layer=*/1, kOverflowUsage, overflow_net_);
  }

  odb::PtrSet<odb::dbNet> getCongestionNets()
  {
    odb::PtrSet<odb::dbNet> nets;
    fastroute_->getCongestionNets(nets);
    return nets;
  }

  size_t overflowPositionCount()
  {
    std::vector<std::pair<odb::Point, bool>> positions;
    fastroute_->getOverflowPositions(positions);
    return positions.size();
  }

  odb::dbBlock* block_ = nullptr;
  utl::ServiceRegistry registry_{&logger_};
  stt::SteinerTreeBuilder stt_builder_{&logger_};
  std::unique_ptr<FastRouteCore> fastroute_;
  odb::dbNet* overflow_net_ = nullptr;
};

TEST_F(CongestionNetsTest, FindsNetsCrossingOverflowAtRadiusZero)
{
  odb::dbNet* h_net = makeFrNet("h_net");
  addRouteH(h_net, 2, 5, 3);
  odb::dbNet* v_net = makeFrNet("v_net");
  addRouteV(v_net, 7, 2, 5);

  overflowH(3, 3);
  overflowV(7, 3);
  EXPECT_EQ(overflowPositionCount(), 2u);

  const auto nets = getCongestionNets();
  EXPECT_EQ(nets.size(), 2u);
  EXPECT_EQ(nets.count(h_net), 1u);
  EXPECT_EQ(nets.count(v_net), 1u);
}

TEST_F(CongestionNetsTest, HorizontalOverflowIgnoresVerticalOnlyNet)
{
  odb::dbNet* v_net = makeFrNet("v_net");
  addRouteV(v_net, 5, 3, 7);

  // Horizontal overflow on a cell the vertical route passes through.
  overflowH(5, 5);

  EXPECT_TRUE(getCongestionNets().empty());
}

TEST_F(CongestionNetsTest, VerticalOverflowIgnoresHorizontalOnlyNet)
{
  odb::dbNet* h_net = makeFrNet("h_net");
  addRouteH(h_net, 3, 7, 5);

  overflowV(5, 5);

  EXPECT_TRUE(getCongestionNets().empty());
}

TEST_F(CongestionNetsTest, ClockNetsExcluded)
{
  odb::dbNet* clk_net = makeFrNet("clk", /*is_clock=*/true);
  addRouteH(clk_net, 2, 6, 4);

  overflowH(3, 4);

  EXPECT_TRUE(getCongestionNets().empty());
}

TEST_F(CongestionNetsTest, NoOverflowFindsNothing)
{
  odb::dbNet* h_net = makeFrNet("h_net");
  addRouteH(h_net, 2, 6, 4);

  EXPECT_EQ(overflowPositionCount(), 0u);
  EXPECT_TRUE(getCongestionNets().empty());
}

TEST_F(CongestionNetsTest, RadiusExpandsUntilNetFound)
{
  // Route at Chebyshev distance 2 from the overflow cell: only reachable
  // once the search radius expands to 2.
  odb::dbNet* net = makeFrNet("net");
  addRouteH(net, 4, 7, 7);

  overflowH(5, 5);

  const auto nets = getCongestionNets();
  EXPECT_EQ(nets.size(), 1u);
  EXPECT_EQ(nets.count(net), 1u);
}

TEST_F(CongestionNetsTest, NetsBeyondMaxRadiusNotFound)
{
  // Chebyshev distance 6 exceeds the maximum search radius of 4.
  odb::dbNet* far_net = makeFrNet("far_net");
  addRouteH(far_net, 2, 6, 8);

  overflowH(3, 2);

  EXPECT_TRUE(getCongestionNets().empty());
}

TEST_F(CongestionNetsTest, RadiusStopsExpandingOnceNetsFound)
{
  odb::dbNet* near_net = makeFrNet("near_net");
  addRouteH(near_net, 2, 6, 4);
  odb::dbNet* next_net = makeFrNet("next_net");
  addRouteH(next_net, 2, 6, 5);

  overflowH(3, 4);

  // near_net is found at radius 0, so the radius never grows to reach
  // next_net one row away.
  const auto nets = getCongestionNets();
  EXPECT_EQ(nets.size(), 1u);
  EXPECT_EQ(nets.count(near_net), 1u);
}

TEST_F(CongestionNetsTest, AlreadyAddedNetsDontStopRadiusExpansion)
{
  odb::dbNet* seeded_net = makeFrNet("seeded_net");
  addRouteH(seeded_net, 2, 6, 4);
  odb::dbNet* found_net = makeFrNet("found_net");
  addRouteH(found_net, 2, 6, 7);

  overflowH(3, 4);

  // seeded_net crosses the overflow but is already in the set; the radius
  // keeps expanding until found_net (distance 3) is picked up.
  odb::PtrSet<odb::dbNet> nets;
  nets.insert(seeded_net);
  fastroute_->getCongestionNets(nets);
  EXPECT_EQ(nets.size(), 2u);
  EXPECT_EQ(nets.count(found_net), 1u);
}

TEST_F(CongestionNetsTest, AllNetsOnOverflowCellFound)
{
  odb::dbNet* net_a = makeFrNet("net_a");
  addRouteH(net_a, 2, 6, 4);
  odb::dbNet* net_b = makeFrNet("net_b");
  addRouteH(net_b, 3, 7, 4);

  overflowH(4, 4);

  const auto nets = getCongestionNets();
  EXPECT_EQ(nets.size(), 2u);
  EXPECT_EQ(nets.count(net_a), 1u);
  EXPECT_EQ(nets.count(net_b), 1u);
}

}  // namespace
}  // namespace grt