Skip to content

Commit

Permalink
Use non-resizable dynamic array in gcNet and gcPin
Browse files Browse the repository at this point in the history
Resizing these arrays could lead to dangling pointers and hard to find
bugs.

Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
  • Loading branch information
kbieganski committed Oct 24, 2024
1 parent 3a35d15 commit 6135fe3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 68 deletions.
26 changes: 26 additions & 0 deletions src/drt/src/db/dynarray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <vector>

template <typename T>
class dynarray
{
public:
dynarray() = default;
dynarray(std::vector<T>&& data) : data(std::move(data)) {}
dynarray(size_t size) : data(size) {}
T& operator[](size_t idx) { return data[idx]; }
const T& operator[](size_t idx) const { return data[idx]; }
auto begin() { return data.begin(); }
auto begin() const { return data.begin(); }
auto end() { return data.end(); }
auto end() const { return data.end(); }
size_t size() const { return data.size(); }
void clear() { data.clear(); }
T& front() { return data.front(); }
const T& front() const { return data.front(); }
T& back() { return data.back(); }
const T& back() const { return data.back(); }

private:
std::vector<T> data;
};
65 changes: 37 additions & 28 deletions src/drt/src/db/gcObj/gcNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <memory>

#include "db/dynarray.h"
#include "db/gcObj/gcBlockObject.h"
#include "db/gcObj/gcPin.h"
#include "db/obj/frBlockage.h"
Expand Down Expand Up @@ -74,22 +75,39 @@ class gcNet : public gcBlockObject
routeRectangles_[layerNum].push_back(rect);
}
}
void addPin(const gtl::polygon_90_with_holes_data<frCoord>& shape,
frLayerNum layerNum)
void initPins()
{
gcPin pin(shape, layerNum, this);
pin.setId(pins_[layerNum].size());
pins_[layerNum].push_back(std::move(pin));
}
void addPin(const gtl::rectangle_data<frCoord>& rect, frLayerNum layerNum)
{
gtl::polygon_90_with_holes_data<frCoord> shape;
std::vector<frCoord> coords
= {gtl::xl(rect), gtl::yl(rect), gtl::xh(rect), gtl::yh(rect)};
shape.set_compact(coords.begin(), coords.end());
gcPin pin(shape, layerNum, this);
pin.setId(pins_[layerNum].size());
pins_[layerNum].push_back(std::move(pin));
for (int i = 0; i < pins_.size(); i++) {
std::vector<gcPin> pins;
// init pin from polygons
gtl::polygon_90_set_data<frCoord> layerPolys;
std::vector<gtl::polygon_90_with_holes_data<frCoord>> polys;
polys.clear();
using gtl::operators::operator+=;
layerPolys += getPolygons(i, false);
layerPolys += getPolygons(i, true);
layerPolys.get(polys);
for (auto& poly : polys) {
pins.emplace_back(poly, i, this);
pins.back().setId(pins.size());
}
// init pin from rectangles
const auto addPin = ([&](const gtl::rectangle_data<frCoord>& rect) {
gtl::polygon_90_with_holes_data<frCoord> shape;
std::vector<frCoord> coords
= {gtl::xl(rect), gtl::yl(rect), gtl::xh(rect), gtl::yh(rect)};
shape.set_compact(coords.begin(), coords.end());
pins.emplace_back(shape, i, this);
pins.back().setId(pins.size());
});
for (auto& rect : getRectangles(i, false)) {
addPin(rect);
}
for (auto& rect : getRectangles(i, true)) {
addPin(rect);
}
pins_[i] = std::move(pins);
}
}
void setOwner(frBlockObject* in) { owner_ = in; }
void clear()
Expand Down Expand Up @@ -143,14 +161,8 @@ class gcNet : public gcBlockObject
}
return routeRectangles_[layerNum];
}
std::vector<std::vector<gcPin>>& getPins()
{
return pins_;
}
std::vector<gcPin>& getPins(frLayerNum layerNum)
{
return pins_[layerNum];
}
std::vector<dynarray<gcPin>>& getPins() { return pins_; }
dynarray<gcPin>& getPins(frLayerNum layerNum) { return pins_[layerNum]; }
bool hasOwner() const { return owner_; }
frBlockObject* getOwner() const { return owner_; }
bool isBlockage() const
Expand Down Expand Up @@ -219,10 +231,7 @@ class gcNet : public gcBlockObject
sp.setRect(bx);
specialSpacingRects.push_back(std::move(sp));
}
std::vector<gcRect>& getSpecialSpcRects()
{
return specialSpacingRects;
}
std::vector<gcRect>& getSpecialSpcRects() { return specialSpacingRects; }
bool hasPolyCornerAt(frCoord x, frCoord y, frLayerNum ln) const
{
for (auto& pin : pins_[ln]) {
Expand Down Expand Up @@ -259,7 +268,7 @@ class gcNet : public gcBlockObject
fixedRectangles_; // only cut layer
std::vector<std::vector<gtl::rectangle_data<frCoord>>>
routeRectangles_; // only cut layer
std::vector<std::vector<gcPin>> pins_;
std::vector<dynarray<gcPin>> pins_;
frBlockObject* owner_{nullptr};
std::vector<std::vector<Rect>> taperedRects; //(only routing layer)
std::vector<std::vector<Rect>> nonTaperedRects; //(only routing layer)
Expand Down
18 changes: 7 additions & 11 deletions src/drt/src/db/gcObj/gcPin.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#pragma once

#include "db/dynarray.h"
#include "db/gcObj/gcShape.h"

namespace drt {
Expand Down Expand Up @@ -58,21 +59,16 @@ class gcPin : public gcBlockObject
// getters
gcPolygon* getPolygon() { return &polygon_; }
const gcPolygon* getPolygon() const { return &polygon_; }
std::vector<std::vector<gcSegment>>& getPolygonEdges()
std::vector<dynarray<gcSegment>>& getPolygonEdges() { return polygon_edges_; }
const std::vector<dynarray<gcSegment>>& getPolygonEdges() const
{
return polygon_edges_;
}
const std::vector<std::vector<gcSegment>>& getPolygonEdges()
const
{
return polygon_edges_;
}
std::vector<std::vector<gcCorner>>& getPolygonCorners()
std::vector<dynarray<gcCorner>>& getPolygonCorners()
{
return polygon_corners_;
}
const std::vector<std::vector<gcCorner>>& getPolygonCorners()
const
const std::vector<dynarray<gcCorner>>& getPolygonCorners() const
{
return polygon_corners_;
}
Expand All @@ -90,8 +86,8 @@ class gcPin : public gcBlockObject
gcPolygon polygon_;
gcNet* net_{nullptr};
// assisting structures
std::vector<std::vector<gcSegment>> polygon_edges_;
std::vector<std::vector<gcCorner>> polygon_corners_;
std::vector<dynarray<gcSegment>> polygon_edges_;
std::vector<dynarray<gcCorner>> polygon_corners_;
std::vector<gcRect> max_rectangles_;
};
} // namespace drt
1 change: 0 additions & 1 deletion src/drt/src/gc/FlexGC_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class FlexGCWorker::Impl
void initDRWorker();
void initNets();
void initNet(gcNet* net);
void initNet_pins_polygon(gcNet* net);
void initNet_pins_polygonEdges(gcNet* net);
void initNet_pins_polygonEdges_getFixedPolygonEdges(
gcNet* net,
Expand Down
29 changes: 1 addition & 28 deletions src/drt/src/gc/FlexGC_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,33 +457,6 @@ void FlexGCWorker::Impl::initNetsFromDesign(const frDesign* design)
}
}

void FlexGCWorker::Impl::initNet_pins_polygon(gcNet* net)
{
int numLayers = getTech()->getLayers().size();
// init pin from polygons
std::vector<gtl::polygon_90_set_data<frCoord>> layerPolys(numLayers);
std::vector<gtl::polygon_90_with_holes_data<frCoord>> polys;
for (int i = 0; i < numLayers; i++) {
polys.clear();
using gtl::operators::operator+=;
layerPolys[i] += net->getPolygons(i, false);
layerPolys[i] += net->getPolygons(i, true);
layerPolys[i].get(polys);
for (auto& poly : polys) {
net->addPin(poly, i);
}
}
// init pin from rectangles
for (int i = 0; i < numLayers; i++) {
for (auto& rect : net->getRectangles(i, false)) {
net->addPin(rect, i);
}
for (auto& rect : net->getRectangles(i, true)) {
net->addPin(rect, i);
}
}
}

void FlexGCWorker::Impl::initNet_pins_polygonEdges_getFixedPolygonEdges(
gcNet* net,
std::vector<std::set<std::pair<Point, Point>>>& fixedPolygonEdges)
Expand Down Expand Up @@ -861,7 +834,7 @@ void FlexGCWorker::Impl::initNet_pins_maxRectangles(gcNet* net)

void FlexGCWorker::Impl::initNet(gcNet* net)
{
initNet_pins_polygon(net);
net->initPins();
initNet_pins_polygonEdges(net);
initNet_pins_polygonCorners(net);
initNet_pins_maxRectangles(net);
Expand Down

0 comments on commit 6135fe3

Please sign in to comment.