Skip to content

Commit 9479f9d

Browse files
authored
Merge pull request #2353 from verilog-to-routing/fix_read_rr_graph_diff_x_y_chann
Fix Seg Fault error when reading in an RR Graph with different x-y segment types
2 parents 5a6249a + bbb7b4d commit 9479f9d

File tree

1 file changed

+61
-13
lines changed

1 file changed

+61
-13
lines changed

libs/librrgraph/src/io/rr_graph_uxsdcxx_serializer.h

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
325325
, is_flat_(is_flat) {
326326
// Initialize internal data
327327
init_side_map();
328+
init_segment_inf_x_y();
328329
}
329330

330331
/* A truth table to help understand the conversion from VPR side mask to uxsd side code
@@ -381,6 +382,58 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
381382
side_map_[(1 << TOP) | (1 << RIGHT) | (1 << BOTTOM) | (1 << LEFT)] = uxsd::enum_loc_side::TOP_RIGHT_BOTTOM_LEFT;
382383
}
383384

385+
/**
386+
* @brief This function separates the segments in segment_inf_ based on whether their parallel axis
387+
* is X or Y, and it stores them in segment_inf_x_ and segment_inf_y_.
388+
*/
389+
void init_segment_inf_x_y(){
390+
391+
/* Create a temp copy to convert from vtr::vector to std::vector
392+
* This is required because the ``alloc_and_load_rr_indexed_data()`` function supports only std::vector data
393+
* type for ``rr_segments``
394+
* Note that this is a dirty fix (to avoid massive code changes)
395+
* TODO: The ``alloc_and_load_rr_indexed_data()`` function should embrace ``vtr::vector`` for ``rr_segments``
396+
*/
397+
std::vector<t_segment_inf> rr_segs;
398+
rr_segs.reserve(segment_inf_.size());
399+
for (auto& rr_seg : segment_inf_) {
400+
rr_segs.push_back(rr_seg);
401+
}
402+
403+
t_unified_to_parallel_seg_index seg_index_map;
404+
segment_inf_x_ = get_parallel_segs(rr_segs, seg_index_map, X_AXIS);
405+
segment_inf_y_ = get_parallel_segs(rr_segs, seg_index_map, Y_AXIS);
406+
407+
}
408+
409+
/**
410+
* @brief Search for a segment with a matching segment ID and return its position index
411+
* in the list of segments along the corresponding axis (X or Y).
412+
* @param segment_id The ID of the segment to search for.
413+
* @param axis The axis along which to search for the segment (X or Y).
414+
* @return int The position index of the matching segment.
415+
*/
416+
int find_segment_index_along_axis(int segment_id, e_parallel_axis axis) const {
417+
const std::vector<t_segment_inf>* segment_inf_vec_ptr;
418+
419+
if (axis == X_AXIS)
420+
segment_inf_vec_ptr = &segment_inf_x_;
421+
else
422+
segment_inf_vec_ptr = &segment_inf_y_;
423+
424+
for(std::vector<t_segment_inf>::size_type i=0; i < (*segment_inf_vec_ptr).size(); i++){
425+
if((*segment_inf_vec_ptr)[i].seg_index == segment_id)
426+
return static_cast<int>(i);
427+
}
428+
429+
if (axis == X_AXIS)
430+
VTR_LOG_ERROR("Segment ID %d not found in the list of segments along X axis.\n", segment_id);
431+
else
432+
VTR_LOG_ERROR("Segment ID %d not found in the list of segments along Y axis.\n", segment_id);
433+
434+
return -1;
435+
}
436+
384437
public:
385438
void start_load(const std::function<void(const char*)>* report_error_in) final {
386439
// report_error_in should be invoked if RrGraphSerializer encounters
@@ -748,10 +801,12 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
748801
if (GRAPH_GLOBAL == graph_type_) {
749802
rr_graph_builder_->set_node_cost_index(node_id, RRIndexedDataId(0));
750803
} else if (rr_graph.node_type(node.id()) == CHANX) {
751-
rr_graph_builder_->set_node_cost_index(node_id, RRIndexedDataId(CHANX_COST_INDEX_START + segment_id));
804+
int seg_ind_x = find_segment_index_along_axis(segment_id, X_AXIS);
805+
rr_graph_builder_->set_node_cost_index(node_id, RRIndexedDataId(CHANX_COST_INDEX_START + seg_ind_x));
752806
seg_index_[rr_graph.node_cost_index(node.id())] = segment_id;
753807
} else if (rr_graph.node_type(node.id()) == CHANY) {
754-
rr_graph_builder_->set_node_cost_index(node_id, RRIndexedDataId(CHANX_COST_INDEX_START + segment_inf_.size() + segment_id));
808+
int seg_ind_y = find_segment_index_along_axis(segment_id, Y_AXIS);
809+
rr_graph_builder_->set_node_cost_index(node_id, RRIndexedDataId(CHANX_COST_INDEX_START + segment_inf_x_.size() + seg_ind_y));
755810
seg_index_[rr_graph.node_cost_index(node.id())] = segment_id;
756811
}
757812
return inode;
@@ -884,7 +939,7 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
884939

885940
inline void* init_rr_graph_rr_nodes(void*& /*ctx*/) final {
886941
rr_nodes_->clear();
887-
seg_index_.resize(CHANX_COST_INDEX_START + 2 * segment_inf_.size(), -1);
942+
seg_index_.resize(CHANX_COST_INDEX_START + segment_inf_x_.size() + segment_inf_y_.size(), -1);
888943
return nullptr;
889944
}
890945
inline void finish_rr_graph_rr_nodes(void*& /*ctx*/) final {
@@ -1612,22 +1667,13 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
16121667
process_rr_node_indices();
16131668

16141669
rr_graph_builder_->init_fan_in();
1615-
/* Create a temp copy to convert from vtr::vector to std::vector
1616-
* This is required because the ``alloc_and_load_rr_indexed_data()`` function supports only std::vector data
1617-
* type for ``rr_segments``
1618-
* Note that this is a dirty fix (to avoid massive code changes)
1619-
* TODO: The ``alloc_and_load_rr_indexed_data()`` function should embrace ``vtr::vector`` for ``rr_segments``
1620-
*/
1670+
16211671
std::vector<t_segment_inf> temp_rr_segs;
16221672
temp_rr_segs.reserve(segment_inf_.size());
16231673
for (auto& rr_seg : segment_inf_) {
16241674
temp_rr_segs.push_back(rr_seg);
16251675
}
16261676

1627-
t_unified_to_parallel_seg_index seg_index_map;
1628-
auto segment_inf_x_ = get_parallel_segs(temp_rr_segs, seg_index_map, X_AXIS);
1629-
auto segment_inf_y_ = get_parallel_segs(temp_rr_segs, seg_index_map, Y_AXIS);
1630-
16311677
alloc_and_load_rr_indexed_data(
16321678
*rr_graph_,
16331679
grid_,
@@ -1972,6 +2018,8 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
19722018
* the methods following routines:rr_graph_rr_nodes and init_node_segment according to the changes in
19732019
* rr_graph_indexed_data.cpp */
19742020
const vtr::vector<RRSegmentId, t_segment_inf>& segment_inf_;
2021+
std::vector<t_segment_inf> segment_inf_x_; // [num_segs_along_x_axis-1:0] - vector of segment information for segments along the x-axis.
2022+
std::vector<t_segment_inf> segment_inf_y_; // [num_segs_along_y_axis-1:0] - vector of segment information for segments along the y-axis.
19752023
const std::vector<t_physical_tile_type>& physical_tile_types_;
19762024
const DeviceGrid& grid_;
19772025
MetadataStorage<int>* rr_node_metadata_;

0 commit comments

Comments
 (0)