Skip to content

Commit c14a0cf

Browse files
committed
grt: fix minmax related lifetime issue
Lesson learned the hard way auto [min_x, max_x] = std::minmax(node->x(), child->x()); if x() returns an int this does not work! minmax returns a pair of const int& that then go out of scope leaving min_x/max_x dangling. Lifetime extension does not apply to the destructed binding. Gemini and chatgpt both failed to figure this out without me essentially explaining it to them (repeatedly). auto [min_x, max_x] = std::minmax({node->x(), child->x()}); does work as the initializer list form returns a pair of ints which get copied. The joys of c++... Signed-off-by: Matt Liberty <[email protected]>
1 parent 75a8d40 commit c14a0cf

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/grt/src/cugr/src/CUGR.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ NetRouteMap CUGR::getRoutes()
204204
routing_tree, [&](const std::shared_ptr<GRTreeNode>& node) {
205205
for (const auto& child : node->children) {
206206
if (node->getLayerIdx() == child->getLayerIdx()) {
207-
auto [min_x, max_x] = std::minmax(node->x(), child->x());
208-
auto [min_y, max_y] = std::minmax(node->y(), child->y());
207+
auto [min_x, max_x] = std::minmax({node->x(), child->x()});
208+
auto [min_y, max_y] = std::minmax({node->y(), child->y()});
209209
GSegment segment(
210210
grid_graph_->getGridline(0, min_x) + half_gcell_size,
211211
grid_graph_->getGridline(1, min_y) + half_gcell_size,

src/grt/src/cugr/src/GridGraph.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ CostT GridGraph::getWireCost(const int layer_index,
306306
assert(u[1 - direction] == v[1 - direction]);
307307
CostT cost = 0;
308308
if (direction == MetalLayer::H) {
309-
const auto [l, h] = std::minmax(u.x(), v.x());
309+
const auto [l, h] = std::minmax({u.x(), v.x()});
310310
for (int x = l; x < h; x++) {
311311
cost += getWireCost(layer_index, {x, u.y()});
312312
}
313313
} else {
314-
const auto [l, h] = std::minmax(u.y(), v.y());
314+
const auto [l, h] = std::minmax({u.y(), v.y()});
315315
for (int y = l; y < h; y++) {
316316
cost += getWireCost(layer_index, {u.x(), y});
317317
}
@@ -471,14 +471,14 @@ void GridGraph::commitTree(const std::shared_ptr<GRTreeNode>& tree,
471471
if (node->getLayerIdx() == child->getLayerIdx()) {
472472
unsigned direction = layer_directions_[node->getLayerIdx()];
473473
if (direction == MetalLayer::H) {
474-
assert(node->y == child->y);
475-
const auto [l, h] = std::minmax(node->x(), child->x());
474+
assert(node->y() == child->y());
475+
const auto [l, h] = std::minmax({node->x(), child->x()});
476476
for (int x = l; x < h; x++) {
477477
commitWire(node->getLayerIdx(), {x, node->y()}, reverse);
478478
}
479479
} else {
480-
assert(node->x == child->x);
481-
const auto [l, h] = std::minmax(node->y(), child->y());
480+
assert(node->x() == child->x());
481+
const auto [l, h] = std::minmax({node->y(), child->y()});
482482
for (int y = l; y < h; y++) {
483483
commitWire(node->getLayerIdx(), {node->x(), y}, reverse);
484484
}
@@ -502,16 +502,16 @@ int GridGraph::checkOverflow(const int layer_index,
502502
int num = 0;
503503
unsigned direction = layer_directions_[layer_index];
504504
if (direction == MetalLayer::H) {
505-
assert(u.y == v.y);
506-
const auto [l, h] = std::minmax(u.x(), v.x());
505+
assert(u.y() == v.y());
506+
const auto [l, h] = std::minmax({u.x(), v.x()});
507507
for (int x = l; x < h; x++) {
508508
if (checkOverflow(layer_index, x, u.y())) {
509509
num++;
510510
}
511511
}
512512
} else {
513-
assert(u.x == v.x);
514-
const auto [l, h] = std::minmax(u.y(), v.y());
513+
assert(u.x() == v.x());
514+
const auto [l, h] = std::minmax({u.y(), v.y()});
515515
for (int y = l; y < h; y++) {
516516
if (checkOverflow(layer_index, u.x(), y)) {
517517
num++;
@@ -717,14 +717,14 @@ void GridGraph::updateWireCostView(
717717
if (node->getLayerIdx() == child->getLayerIdx()) {
718718
unsigned direction = getLayerDirection(node->getLayerIdx());
719719
if (direction == MetalLayer::H) {
720-
assert(node->y == child->y);
720+
assert(node->y() == child->y());
721721
int l = std::min(node->x(), child->x()),
722722
h = std::max(node->x(), child->x());
723723
for (int x = l; x < h; x++) {
724724
update(direction, x, node->y());
725725
}
726726
} else {
727-
assert(node->x == child->x);
727+
assert(node->x() == child->x());
728728
int l = std::min(node->y(), child->y()),
729729
h = std::max(node->y(), child->y());
730730
for (int y = l; y < h; y++) {

src/grt/src/cugr/src/GridGraph.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,16 @@ class GridGraphView : public std::vector<std::vector<std::vector<Type>>>
167167
public:
168168
bool check(const PointT& u, const PointT& v) const
169169
{
170-
assert(u.x == v.x || u.y == v.y);
170+
assert(u.x() == v.x() || u.y() == v.y());
171171
if (u.y() == v.y()) {
172-
const auto [l, h] = std::minmax(u.x(), v.x());
172+
const auto [l, h] = std::minmax({u.x(), v.x()});
173173
for (int x = l; x < h; x++) {
174174
if ((*this)[MetalLayer::H][x][u.y()]) {
175175
return true;
176176
}
177177
}
178178
} else {
179-
const auto [l, h] = std::minmax(u.y(), v.y());
179+
const auto [l, h] = std::minmax({u.y(), v.y()});
180180
for (int y = l; y < h; y++) {
181181
if ((*this)[MetalLayer::V][u.x()][y]) {
182182
return true;
@@ -188,15 +188,15 @@ class GridGraphView : public std::vector<std::vector<std::vector<Type>>>
188188

189189
Type sum(const PointT& u, const PointT& v) const
190190
{
191-
assert(u.x == v.x || u.y == v.y);
191+
assert(u.x() == v.x() || u.y() == v.y());
192192
Type res = 0;
193193
if (u.y() == v.y()) {
194-
const auto [l, h] = std::minmax(u.x(), v.x());
194+
const auto [l, h] = std::minmax({u.x(), v.x()});
195195
for (int x = l; x < h; x++) {
196196
res += (*this)[MetalLayer::H][x][u.y()];
197197
}
198198
} else {
199-
const auto [l, h] = std::minmax(u.y(), v.y());
199+
const auto [l, h] = std::minmax({u.y(), v.y()});
200200
for (int y = l; y < h; y++) {
201201
res += (*this)[MetalLayer::V][u.x()][y];
202202
}

0 commit comments

Comments
 (0)