-
Notifications
You must be signed in to change notification settings - Fork 943
Expand file tree
/
Copy pathgrid.cpp
More file actions
1991 lines (1751 loc) · 56.4 KB
/
Copy pathgrid.cpp
File metadata and controls
1991 lines (1751 loc) · 56.4 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022-2025, The OpenROAD Authors
#include "grid.h"
#include <algorithm>
#include <array>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "boost/geometry/geometry.hpp"
#include "connect.h"
#include "domain.h"
#include "odb/db.h"
#include "odb/dbShape.h"
#include "odb/dbTransform.h"
#include "odb/dbTypes.h"
#include "odb/isotropy.h"
#include "power_cells.h"
#include "rings.h"
#include "shape.h"
#include "straps.h"
#include "techlayer.h"
#include "utl/Logger.h"
#include "via.h"
namespace pdn {
namespace bgi = boost::geometry::index;
Grid::Grid(VoltageDomain* domain,
const std::string& name,
bool starts_with_power,
const std::vector<odb::dbTechLayer*>& generate_obstructions)
: domain_(domain), name_(name), starts_with_power_(starts_with_power)
{
obstruction_layers_ = generate_obstructions;
}
Grid::~Grid() = default;
odb::dbBlock* Grid::getBlock() const
{
return domain_->getBlock();
}
utl::Logger* Grid::getLogger() const
{
return domain_->getLogger();
}
std::vector<odb::dbNet*> Grid::getNets(bool starts_with_power) const
{
return domain_->getNets(starts_with_power);
}
std::string Grid::typeToString(Type type)
{
switch (type) {
case kCore:
return "Core";
case kInstance:
return "Instance";
case kExisting:
return "Existing";
}
return "Unknown";
}
void Grid::addRing(std::unique_ptr<Rings> ring)
{
if (ring != nullptr) {
ring->setGrid(this);
ring->checkLayerSpecifications();
rings_.push_back(std::move(ring));
}
}
void Grid::addStrap(std::unique_ptr<Straps> strap)
{
if (strap != nullptr) {
strap->setGrid(this);
strap->checkLayerSpecifications();
straps_.push_back(std::move(strap));
}
}
void Grid::addConnect(std::unique_ptr<Connect> connect)
{
if (connect != nullptr) {
for (const auto& conn : connect_) {
if (conn->getLowerLayer() == connect->getLowerLayer()
&& conn->getUpperLayer() == connect->getUpperLayer()) {
getLogger()->error(
utl::PDN,
186,
"Connect between layers {} and {} already exists in \"{}\".",
connect->getLowerLayer()->getName(),
connect->getUpperLayer()->getName(),
getLongName());
}
}
connect->setGrid(this);
connect_.push_back(std::move(connect));
}
}
void Grid::makeShapes(const Shape::ShapeTreeMap& global_shapes,
const Shape::ObstructionTreeMap& obstructions)
{
auto* logger = getLogger();
logger->info(utl::PDN, 1, "Inserting grid: {}", getLongName());
// copy obstructions
Shape::ObstructionTreeMap local_obstructions = obstructions;
Shape::ShapeTreeMap local_shapes = global_shapes;
// make shapes
std::vector<GridComponent*> deferred;
for (auto* component : getGridComponents()) {
if (!component->make(local_shapes, local_obstructions)) {
debugPrint(logger,
utl::PDN,
"Make",
2,
"Deferring shape creation for component in \"{}\".",
getName());
deferred.push_back(component);
}
}
// make deferred components
for (auto* component : deferred) {
component->make(local_shapes, local_obstructions);
}
// refine shapes
bool modified = false;
do {
modified = false;
for (auto* component : getGridComponents()) {
// attempt to refine shapes
const bool comp_modified
= component->refineShapes(local_shapes, local_obstructions);
modified |= comp_modified;
}
} while (modified);
Shape::ShapeTreeMap all_shapes = global_shapes;
// insert power switches
if (switched_power_cell_ != nullptr) {
switched_power_cell_->build();
for (const auto& [layer, cell_shapes] : switched_power_cell_->getShapes()) {
auto& layer_shapes = all_shapes[layer];
layer_shapes.insert(cell_shapes.begin(), cell_shapes.end());
}
}
// Remove any poorly formed shapes
cleanupShapes();
// make vias
makeVias(all_shapes, obstructions, local_obstructions);
// find and repair disconnected channels
RepairChannelStraps::repairGridChannels(
this,
all_shapes,
local_obstructions,
allow_repair_channels_,
domain_->getPDNGen()->getDebugRenderer());
}
void Grid::makeRoutingObstructions(odb::dbBlock* block) const
{
if (obstruction_layers_.empty()) {
return;
}
const auto shapes = getShapes();
for (auto* layer : obstruction_layers_) {
auto itr = shapes.find(layer);
if (itr == shapes.end()) {
continue;
}
TechLayer techlayer(layer);
techlayer.populateGrid(block);
const bool is_horizontal
= layer->getDirection() == odb::dbTechLayerDir::HORIZONTAL;
const int min_width = techlayer.getMinWidth();
const int min_spacing = techlayer.getSpacing(0);
std::vector<ShapePtr> all_shapes;
for (const auto& shape_value : itr->second) {
all_shapes.push_back(shape_value);
}
// sort shapes so they get written to db in the same order. Shapes
// are non-overlapping so comparing one corner should be a total order.
std::ranges::sort(all_shapes, [](const auto& l, const auto& r) {
auto lc = l->getRect().ll();
auto rc = r->getRect().ll();
return lc < rc;
});
for (const auto& shape : all_shapes) {
const auto& rect = shape->getRect();
// bloat to block routing based on spacing
const int width = is_horizontal ? rect.dy() : rect.dx();
const int length = is_horizontal ? rect.dx() : rect.dy();
const int width_spacing = techlayer.getSpacing(width, length);
const int length_spacing = techlayer.getSpacing(length, width);
int delta_x = is_horizontal ? length_spacing : width_spacing;
int delta_y = is_horizontal ? width_spacing : length_spacing;
if (is_horizontal) {
delta_x -= min_spacing;
delta_y += min_width;
} else {
delta_x += min_width;
delta_y -= min_spacing;
}
const odb::Rect obs(rect.xMin() - delta_x,
rect.yMin() - delta_y,
rect.xMax() + delta_x,
rect.yMax() + delta_y);
if (techlayer.hasGrid()) {
std::vector<int> grid = techlayer.getGrid();
std::erase_if(grid, [&obs, is_horizontal](int pos) {
if (is_horizontal) {
return obs.yMin() > pos || pos > obs.yMax();
}
return obs.xMin() > pos || pos > obs.xMax();
});
// add by tracks
const int min_width = techlayer.getMinWidth();
const int half0_min_width = min_width / 2;
const int half1_min_width = min_width - half0_min_width;
for (int track : grid) {
const int low = track - half0_min_width;
const int high = track + half1_min_width;
odb::Rect new_obs = obs;
if (is_horizontal) {
new_obs.set_ylo(low);
new_obs.set_yhi(high);
} else {
new_obs.set_xlo(low);
new_obs.set_xhi(high);
}
odb::dbObstruction::create(block,
layer,
new_obs.xMin(),
new_obs.yMin(),
new_obs.xMax(),
new_obs.yMax());
}
} else {
// add blob
odb::dbObstruction::create(
block, layer, obs.xMin(), obs.yMin(), obs.xMax(), obs.yMax());
}
}
}
}
bool Grid::repairVias(const Shape::ShapeTreeMap& global_shapes,
Shape::ObstructionTreeMap& obstructions)
{
debugPrint(getLogger(),
utl::PDN,
"ViaRepair",
1,
"Start via repair: {}",
getLongName());
// find vias that do not overlap completely
// attempt to extend straps to fit (if owned by grid)
Shape::ShapeTreeMap search_shapes = getShapes();
odb::Rect search_area = getDomainBoundary();
for (const auto& [layer, shapes] : search_shapes) {
for (const auto& shape : shapes) {
search_area.merge(shape->getRect());
}
}
// populate shapes and obstructions
for (auto& [layer, layer_global_shape] : global_shapes) {
auto& shapes = search_shapes[layer];
for (auto it = layer_global_shape.qbegin(bgi::intersects(search_area));
it != layer_global_shape.qend();
it++) {
shapes.insert(*it);
}
}
auto obs_filter = [this](const ShapePtr& other) -> bool {
if (other->shapeType() != Shape::kGridObs) {
return true;
}
const GridObsShape* shape = static_cast<GridObsShape*>(other.get());
return !shape->belongsTo(this);
};
// When rings are present, shapes must not be extended beyond the ring
// boundary. Without this guard, extendTo() can push stripes through the
// padframe when a via sits right at the ring edge. For ring-less grids the
// obstruction-based check inside extendTo() is sufficient.
const std::optional<odb::Rect> allowed_area
= rings_.empty() ? std::nullopt : std::make_optional(getRingArea());
std::map<Shape*, std::unique_ptr<Shape>> replace_shapes;
for (const auto& via : vias_) {
// ensure shapes belong to something
const auto& lower_shape = via->getLowerShape();
if (lower_shape->getGridComponent() == nullptr) {
continue;
}
const auto& upper_shape = via->getUpperShape();
if (upper_shape->getGridComponent() == nullptr) {
continue;
}
// ensure atleast one shape belongs to this grid
const bool lower_belongs_to_grid
= lower_shape->getGridComponent()->getGrid() == this;
const bool upper_belongs_to_grid
= upper_shape->getGridComponent()->getGrid() == this;
if (!lower_belongs_to_grid && !upper_belongs_to_grid) {
continue;
}
if (lower_belongs_to_grid && lower_shape->isModifiable()) {
Shape* extend_test = lower_shape.get();
auto find_replace = replace_shapes.find(extend_test);
if (find_replace != replace_shapes.end()) {
extend_test = find_replace->second.get();
}
auto new_lower
= extend_test->extendTo(upper_shape->getRect(),
search_shapes[extend_test->getLayer()],
obstructions[extend_test->getLayer()],
lower_shape.get(),
obs_filter);
const bool lower_in_bounds
= !allowed_area || allowed_area->contains(new_lower->getRect());
if (new_lower != nullptr && lower_in_bounds) {
replace_shapes[lower_shape.get()] = std::move(new_lower);
}
}
if (upper_belongs_to_grid && upper_shape->isModifiable()) {
Shape* extend_test = upper_shape.get();
auto find_replace = replace_shapes.find(extend_test);
if (find_replace != replace_shapes.end()) {
extend_test = find_replace->second.get();
}
auto new_upper
= extend_test->extendTo(lower_shape->getRect(),
search_shapes[extend_test->getLayer()],
obstructions[extend_test->getLayer()],
upper_shape.get(),
obs_filter);
const bool upper_in_bounds
= !allowed_area || allowed_area->contains(new_upper->getRect());
if (new_upper != nullptr && upper_in_bounds) {
replace_shapes[upper_shape.get()] = std::move(new_upper);
}
}
}
for (auto& [old_shape, new_shape] : replace_shapes) {
auto* component = old_shape->getGridComponent();
component->replaceShape(old_shape, std::move(new_shape));
}
debugPrint(getLogger(),
utl::PDN,
"ViaRepair",
1,
"End via repair: {}",
getLongName());
return !replace_shapes.empty();
}
Shape::ShapeTreeMap Grid::getShapes() const
{
ShapeVectorMap shapes;
for (auto* component : getGridComponents()) {
for (const auto& [layer, component_shapes] : component->getShapes()) {
shapes[layer].insert(shapes[layer].end(),
component_shapes.begin(),
component_shapes.end());
}
}
return Shape::convertVectorToTree(shapes);
}
odb::Rect Grid::getDomainArea() const
{
return domain_->getDomainArea();
}
odb::Rect Grid::getDomainBoundary() const
{
return getDomainArea();
}
odb::Rect Grid::getGridArea() const
{
if (getBlock() == nullptr) {
return odb::Rect();
}
odb::Rect rect = getBlock()->getDieArea();
return rect;
}
odb::Rect Grid::getGridBoundary() const
{
return getGridArea();
}
odb::Rect Grid::getRingArea() const
{
if (getBlock() == nullptr) {
return odb::Rect();
}
// get the outline of the rings
odb::Rect rect = getDomainBoundary();
for (const auto& ring : rings_) {
for (const auto& [layer, shapes] : ring->getShapes()) {
for (const auto& shape : shapes) {
const odb::Rect& ring_shape = shape->getRect();
if (ring_shape.dx() == ring_shape.dy()) {
rect.merge(ring_shape);
} else if (ring_shape.dx() > ring_shape.dy()) {
if (ring_shape.yMin() < rect.yMin()) {
rect.set_ylo(ring_shape.yMin());
}
if (ring_shape.yMax() > rect.yMax()) {
rect.set_yhi(ring_shape.yMax());
}
} else {
if (ring_shape.xMin() < rect.xMin()) {
rect.set_xlo(ring_shape.xMin());
}
if (ring_shape.xMax() > rect.xMax()) {
rect.set_xhi(ring_shape.xMax());
}
}
}
}
}
return rect;
}
void Grid::report() const
{
auto* logger = getLogger();
logger->report("Grid name: {}", getLongName());
logger->report("Type: {}", typeToString(type()));
if (!rings_.empty()) {
logger->report("Rings:");
for (const auto& ring : rings_) {
ring->report();
}
}
if (!straps_.empty()) {
logger->report("Straps:");
for (const auto& strap : straps_) {
strap->report();
}
}
if (!connect_.empty()) {
std::vector<Connect*> connect;
connect.reserve(connect_.size());
for (const auto& conn : connect_) {
connect.push_back(conn.get());
}
std::ranges::sort(connect, [](const Connect* l, const Connect* r) {
int l_lower = l->getLowerLayer()->getRoutingLevel();
int l_upper = l->getUpperLayer()->getRoutingLevel();
int r_lower = r->getLowerLayer()->getRoutingLevel();
int r_upper = r->getUpperLayer()->getRoutingLevel();
return std::tie(l_lower, l_upper) < std::tie(r_lower, r_upper);
});
logger->report("Connect:");
for (Connect* conn : connect) {
conn->report();
}
}
if (!pin_layers_.empty()) {
std::string layers;
for (auto* layer : pin_layers_) {
if (!layers.empty()) {
layers += " ";
}
layers += layer->getName();
}
logger->report("Pin layers: {}", layers);
}
if (!obstruction_layers_.empty()) {
std::string layers;
for (auto* layer : pin_layers_) {
if (!layers.empty()) {
layers += " ";
}
layers += layer->getName();
}
logger->report("Routing obstruction layers: {}", layers);
}
if (switched_power_cell_ != nullptr) {
switched_power_cell_->report();
}
}
void Grid::getIntersections(std::vector<ViaPtr>& shape_intersections,
const Shape::ShapeTreeMap& search_shapes) const
{
debugPrint(getLogger(),
utl::PDN,
"Via",
1,
"Getting via intersections in \"{}\" - start",
name_);
Shape::ShapeTreeMap shapes = search_shapes;
// Populate with additional shapes from grid components
for (auto* comp : getGridComponents()) {
comp->getConnectableShapes(shapes);
}
// loop over connect statements
for (const auto& connect : connect_) {
odb::dbTechLayer* lower_layer = connect->getLowerLayer();
odb::dbTechLayer* upper_layer = connect->getUpperLayer();
// check if both layers have shapes
if (!shapes.contains(lower_layer)) {
continue;
}
if (!shapes.contains(upper_layer)) {
continue;
}
const auto& lower_shapes = shapes.at(lower_layer);
const auto& upper_shapes = shapes.at(upper_layer);
debugPrint(getLogger(),
utl::PDN,
"Via",
2,
"Getting via intersections in \"{}\" - layers {} ({} shapes) - "
"{} ({} shapes)",
name_,
lower_layer->getName(),
lower_shapes.size(),
upper_layer->getName(),
upper_shapes.size());
// loop over lower layer shapes
for (const auto& lower_shape : lower_shapes) {
auto* lower_net = lower_shape->getNet();
// check for intersections in higher layer shapes
for (auto it = upper_shapes.qbegin(
bgi::intersects(lower_shape->getRect())
&& bgi::satisfies([lower_net](const auto& other) {
// not the same net, so ignore
return lower_net == other->getNet();
}));
it != upper_shapes.qend();
it++) {
const auto& upper_shape = *it;
if (!lower_shape->getRect().overlaps(upper_shape->getRect())) {
// no overlap, so ignore
continue;
}
const odb::Rect via_rect
= lower_shape->getRect().intersect(upper_shape->getRect());
auto* via = new Via(connect.get(),
lower_shape->getNet(),
via_rect,
lower_shape,
upper_shape);
shape_intersections.push_back(ViaPtr(via));
}
}
}
debugPrint(getLogger(),
utl::PDN,
"Via",
1,
"Getting via intersections in \"{}\" - end",
name_);
}
void Grid::resetShapes()
{
vias_.clear();
std::set<GridComponent*> remove;
for (auto* component : getGridComponents()) {
component->clearShapes();
if (component->isAutoInserted()) {
remove.insert(component);
}
}
for (auto* component : remove) {
removeGridComponent(component);
}
for (const auto& connect : connect_) {
connect->clearShapes();
}
}
void Grid::ripup()
{
if (switched_power_cell_ != nullptr) {
switched_power_cell_->ripup();
}
}
void Grid::checkSetup() const
{
// check if follow pins have connect statements
std::set<odb::dbTechLayer*> follow_pin_layers;
for (const auto& strap : straps_) {
if (strap->type() == Straps::kFollowpin) {
follow_pin_layers.insert(strap->getLayer());
}
}
if (follow_pin_layers.empty()) {
return;
}
std::set<Connect*> follow_pin_connect;
for (auto* lower : follow_pin_layers) {
for (const auto& connect : connect_) {
if (connect->getLowerLayer() != lower) {
continue;
}
for (auto* upper : follow_pin_layers) {
if (connect->getUpperLayer() == upper) {
follow_pin_connect.insert(connect.get());
break;
}
}
}
}
if (follow_pin_layers.size() > 1 && follow_pin_connect.empty()) {
// found no connect statements between followpins
getLogger()->error(utl::PDN,
192,
"There are multiple ({}) followpin definitions in {}, "
"but no connect statements between them.",
follow_pin_layers.size(),
getName());
}
if (follow_pin_layers.size() - 1 != follow_pin_connect.size()) {
getLogger()->error(utl::PDN,
193,
"There are only ({}) followpin connect statements when "
"{} is/are required.",
follow_pin_connect.size(),
follow_pin_layers.size() - 1);
}
for (auto* connect0 : follow_pin_connect) {
for (auto* connect1 : follow_pin_connect) {
if (connect0 == connect1) {
continue;
}
// ensure order of connects is consistent
const int c0_lower = connect0->getLowerLayer()->getRoutingLevel();
const int c0_upper = connect0->getUpperLayer()->getRoutingLevel();
const int c1_lower = connect1->getLowerLayer()->getRoutingLevel();
const int c1_upper = connect1->getUpperLayer()->getRoutingLevel();
if (std::tie(c0_lower, c0_upper) > std::tie(c1_lower, c1_upper)) {
std::swap(connect0, connect1);
}
if (connect0->overlaps(connect1) || connect1->overlaps(connect0)) {
getLogger()->error(utl::PDN,
194,
"Connect statements for followpins overlap between "
"layers: {} -> {} and {} -> {}",
connect0->getLowerLayer()->getName(),
connect0->getUpperLayer()->getName(),
connect1->getLowerLayer()->getName(),
connect1->getUpperLayer()->getName());
}
}
}
// Check connectivity
std::set<odb::dbTechLayer*> check_layers;
for (const auto& ring : rings_) {
for (auto* layer : ring->getLayers()) {
check_layers.insert(layer);
}
}
for (const auto& strap : straps_) {
check_layers.insert(strap->getLayer());
}
// Check that pin layers actually exists in stack
for (auto* layer : pin_layers_) {
if (check_layers.find(layer) == check_layers.end()) {
getLogger()->error(utl::PDN,
111,
"Pin layer {} is not a valid shape in {}",
layer->getName(),
name_);
}
}
// add instance layers
const auto nets_vec = getNets();
const std::set<odb::dbNet*> nets(nets_vec.begin(), nets_vec.end());
for (auto* inst : getInstances()) {
if (!inst->isFixed()) {
continue;
}
for (auto* iterm : inst->getITerms()) {
if (nets.find(iterm->getNet()) != nets.end()) {
for (const auto& [layer, shape] : iterm->getGeometries()) {
check_layers.insert(layer);
}
}
}
}
if (domain_->hasSwitchedPower()) {
for (const auto& powercell :
domain_->getPDNGen()->getSwitchedPowerCells()) {
for (auto* mterm : powercell->getMaster()->getMTerms()) {
for (auto* mpin : mterm->getMPins()) {
for (auto* box : mpin->getGeometry()) {
auto* layer = box->getTechLayer();
if (layer) {
check_layers.insert(layer);
}
}
}
}
}
}
// add bterms and exisiting routing
for (auto* net : nets) {
for (auto* swire : net->getSWires()) {
for (auto* box : swire->getWires()) {
auto* layer = box->getTechLayer();
if (layer) {
check_layers.insert(layer);
}
}
}
for (auto* bterm : net->getBTerms()) {
for (auto* bpin : bterm->getBPins()) {
if (!bpin->getPlacementStatus().isFixed()) {
continue;
}
for (auto* box : bpin->getBoxes()) {
auto* layer = box->getTechLayer();
if (layer) {
check_layers.insert(layer);
}
}
}
}
}
// Check that connect statement actually point to something
for (const auto& connect : connect_) {
if (check_layers.find(connect->getLowerLayer()) == check_layers.end()) {
getLogger()->error(utl::PDN,
112,
"Cannot find shapes to connect to on {}",
connect->getLowerLayer()->getName());
}
if (check_layers.find(connect->getUpperLayer()) == check_layers.end()) {
getLogger()->error(utl::PDN,
113,
"Cannot find shapes to connect to on {}",
connect->getUpperLayer()->getName());
}
}
}
void Grid::getObstructions(Shape::ObstructionTreeMap& obstructions) const
{
for (const auto& [layer, shapes] : getShapes()) {
auto& obs = obstructions[layer];
obs.insert(shapes.begin(), shapes.end());
}
}
void Grid::makeVias(const Shape::ShapeTreeMap& global_shapes,
const Shape::ObstructionTreeMap& obstructions,
Shape::ObstructionTreeMap& local_obstructions)
{
makeVias(global_shapes, obstructions);
// repair vias that are only partially overlapping straps
if (repairVias(global_shapes, local_obstructions)) {
// rebuild vias since shapes changed
makeVias(global_shapes, obstructions);
}
}
void Grid::makeVias(const Shape::ShapeTreeMap& global_shapes,
const Shape::ObstructionTreeMap& obstructions)
{
debugPrint(
getLogger(), utl::PDN, "Make", 1, "Making vias in \"{}\" - start", name_);
Shape::ShapeTreeMap search_shapes = getShapes();
odb::Rect search_area = getDomainBoundary();
for (const auto& [layer, shapes] : search_shapes) {
for (const auto& shape : shapes) {
search_area.merge(shape->getRect());
}
}
// populate shapes and obstructions
for (auto& [layer, layer_global_shape] : global_shapes) {
auto& shapes = search_shapes[layer];
for (auto it = layer_global_shape.qbegin(bgi::intersects(search_area));
it != layer_global_shape.qend();
it++) {
shapes.insert(*it);
}
}
Shape::ObstructionTreeMap search_obstructions = obstructions;
for (const auto& [layer, shapes] : search_shapes) {
auto& obs = search_obstructions[layer];
for (auto& search_shape : shapes) {
obs.insert(search_shape);
}
}
// get possible vias
std::vector<ViaPtr> vias;
getIntersections(vias, search_shapes);
auto remove_set_of_vias = [&vias](std::set<ViaPtr>& remove_vias) {
std::erase_if(vias,
[&](const ViaPtr& via) { return remove_vias.contains(via); });
remove_vias.clear();
};
std::set<ViaPtr> remove_vias;
// remove vias with obstructions in their stack
for (const auto& via : vias) {
for (auto* layer : via->getConnect()->getIntermediteLayers()) {
const auto& search_obs = search_obstructions[layer];
if (search_obs.qbegin(
bgi::intersects(via->getArea())
&& bgi::satisfies([this, layer](const ShapePtr& other) -> bool {
if (other->shapeType() != Shape::kGridObs) {
return true;
}
// only consider obstructions on routing layers as blocking
// for grid obstructions
if (layer->getType() != odb::dbTechLayerType::ROUTING) {
return false;
}
const GridObsShape* shape
= static_cast<GridObsShape*>(other.get());
return !shape->belongsTo(this);
}))
!= search_obs.qend()) {
remove_vias.insert(via);
via->markFailed(FailedViaReason::kObstructed);
break;
}
}
}
debugPrint(getLogger(),
utl::PDN,
"Via",
2,
"Removing {} vias due to obstructions.",
remove_vias.size());
remove_set_of_vias(remove_vias);
// Remove overlapping vias and keep largest
Via::ViaTree overlapping_via_tree;
for (const auto& via : vias) {
overlapping_via_tree.insert(via);
}
for (const auto& via : vias) {
if (via->isFailed()) {
continue;
}
if (overlapping_via_tree.qbegin(
bgi::intersects(via->getArea())
&& bgi::satisfies([&via](const ViaPtr& other) -> bool {
if (via == other) {
// ignore the same via
return false;
}
if (other->isFailed()) {
return false;
}
if (via->getLowerLayer() != other->getLowerLayer()) {
return false;
}
if (via->getUpperLayer() != other->getUpperLayer()) {
return false;
}
// Remove the smaller of the two vias
return via->getArea().area() <= other->getArea().area();
}))
!= overlapping_via_tree.qend()) {
remove_vias.insert(via);
via->markFailed(FailedViaReason::kOverlapping);
}
}
debugPrint(getLogger(),
utl::PDN,
"Via",
2,
"Removing {} vias due to overlaps.",
remove_vias.size());
remove_set_of_vias(remove_vias);
// build via tree
vias_.clear();
for (auto& via : vias) {
vias_.insert(via);
via->getLowerShape()->addVia(via);
via->getUpperShape()->addVia(via);
}
debugPrint(
getLogger(), utl::PDN, "Make", 1, "Making vias in \"{}\" - end", name_);
}
void Grid::getVias(std::vector<ViaPtr>& vias) const
{
for (const auto& via : vias_) {
vias.push_back(via);
}
}
void Grid::removeVia(const ViaPtr& via)
{
auto find
= std::ranges::find_if(vias_,
[via](const auto& other) { return via == other; });
if (find != vias_.end()) {
vias_.remove(*find);
}
}
void Grid::removeInvalidVias()
{
std::vector<ViaPtr> remove_vias;
for (const auto& via : vias_) {
if (!via->isValid()) {
remove_vias.push_back(via);
}
}
for (const auto& remove_via : remove_vias) {
vias_.remove(remove_via);
}
}
std::vector<GridComponent*> Grid::getGridComponents() const
{
std::vector<GridComponent*> components;
components.reserve(rings_.size() + straps_.size());
for (const auto& ring : rings_) {