Skip to content

Commit ccb2396

Browse files
Merge pull request #2932 from AmirhosseinPoolad/refactor_atom_pb_from_packing
Remove usage of atom to pb lookup from packing
2 parents c6802d6 + f77c3c7 commit ccb2396

26 files changed

+347
-239
lines changed

utils/fasm/src/fasm.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ static AtomNetId _find_atom_input_logical_net(const t_pb* atom, const t_pb_route
343343

344344
static LogicVec lut_outputs(const t_pb* atom_pb, size_t num_inputs, const t_pb_routes &pb_route) {
345345
auto& atom_ctx = g_vpr_ctx.atom();
346-
AtomBlockId block_id = atom_ctx.lookup().pb_atom(atom_pb);
346+
AtomBlockId block_id = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom_pb);
347347
const auto& truth_table = atom_ctx.netlist().block_truth_table(block_id);
348-
auto ports = atom_ctx.netlist().block_input_ports(atom_ctx.lookup().pb_atom(atom_pb));
348+
auto ports = atom_ctx.netlist().block_input_ports(atom_ctx.lookup().atom_pb_bimap().pb_atom(atom_pb));
349349

350350
const t_pb_graph_node* gnode = atom_pb->pb_graph_node;
351351

@@ -537,7 +537,7 @@ static const t_pb_routes &find_pb_route(const t_pb* pb) {
537537
void FasmWriterVisitor::check_for_param(const t_pb *atom) {
538538
auto& atom_ctx = g_vpr_ctx.atom();
539539

540-
auto atom_blk_id = atom_ctx.lookup().pb_atom(atom);
540+
auto atom_blk_id = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom);
541541
if (atom_blk_id == AtomBlockId::INVALID()) {
542542
return;
543543
}
@@ -592,7 +592,7 @@ void FasmWriterVisitor::check_for_param(const t_pb *atom) {
592592
void FasmWriterVisitor::check_for_lut(const t_pb* atom) {
593593
auto& atom_ctx = g_vpr_ctx.atom();
594594

595-
auto atom_blk_id = atom_ctx.lookup().pb_atom(atom);
595+
auto atom_blk_id = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom);
596596
if (atom_blk_id == AtomBlockId::INVALID()) {
597597
return;
598598
}

vpr/src/base/atom_lookup.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,6 @@
44
#include "vtr_optional.h"
55

66
#include "atom_lookup.h"
7-
/*
8-
* PB
9-
*/
10-
const t_pb* AtomLookup::atom_pb(const AtomBlockId blk_id) const {
11-
auto iter = atom_to_pb_.find(blk_id);
12-
if (iter == atom_to_pb_.end()) {
13-
//Not found
14-
return nullptr;
15-
}
16-
return iter->second;
17-
}
18-
19-
AtomBlockId AtomLookup::pb_atom(const t_pb* pb) const {
20-
auto iter = atom_to_pb_.find(pb);
21-
if (iter == atom_to_pb_.inverse_end()) {
22-
//Not found
23-
return AtomBlockId::INVALID();
24-
}
25-
return iter->second;
26-
}
27-
28-
const t_pb_graph_node* AtomLookup::atom_pb_graph_node(const AtomBlockId blk_id) const {
29-
const t_pb* pb = atom_pb(blk_id);
30-
if (pb) {
31-
//Found
32-
return pb->pb_graph_node;
33-
}
34-
return nullptr;
35-
}
36-
37-
void AtomLookup::set_atom_pb(const AtomBlockId blk_id, const t_pb* pb) {
38-
//If either of blk_id or pb are not valid,
39-
//remove any mapping
40-
41-
if (!blk_id && pb) {
42-
//Remove
43-
atom_to_pb_.erase(pb);
44-
} else if (blk_id && !pb) {
45-
//Remove
46-
atom_to_pb_.erase(blk_id);
47-
} else if (blk_id && pb) {
48-
//If both are valid store the mapping
49-
atom_to_pb_.update(blk_id, pb);
50-
}
51-
}
527

538
/*
549
* PB Pins

vpr/src/base/atom_lookup.h

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "tatum/TimingGraphFwd.hpp"
1515

1616
#include "vtr_optional.h"
17+
#include "atom_pb_bimap.h"
1718

1819
/**
1920
* @brief The AtomLookup class describes the mapping between components in the AtomNetlist
@@ -31,23 +32,45 @@ class AtomLookup {
3132
*/
3233

3334
/**
34-
* @brief Returns the leaf pb associated with the atom blk_id
35-
* @note this is the lowest level pb which corresponds directly to the atom block
35+
* @brief Sets the atom to pb bimap access lock to value.
36+
* If set to true, access to the bimap is prohibited and will result in failing assertions.
37+
*
38+
* @param value Value to set to lock to
3639
*/
37-
const t_pb* atom_pb(const AtomBlockId blk_id) const;
38-
39-
///@brief Returns the atom block id associated with pb
40-
AtomBlockId pb_atom(const t_pb* pb) const;
41-
42-
///@brief Conveneince wrapper around atom_pb to access the associated graph node
43-
const t_pb_graph_node* atom_pb_graph_node(const AtomBlockId blk_id) const;
40+
inline void set_atom_pb_bimap_lock(bool value) {
41+
VTR_ASSERT_SAFE_MSG(lock_atom_pb_bimap_ != value, "Double locking or unlocking the atom pb bimap lock");
42+
lock_atom_pb_bimap_ = value;
43+
}
44+
45+
/// @brief Gets the current atom to pb bimap lock value.
46+
inline bool atom_pb_bimap_islocked() const { return lock_atom_pb_bimap_; }
47+
48+
// All accesses, mutable or immutable, to the atom to pb bimap
49+
// will result in failing assertions if the lock is set to true.
50+
// This is done to make sure there is only a single source of
51+
// data in places that are supposed to use a local data structure
52+
// instead of the global context.
53+
54+
/// @brief Returns a mutable reference to the atom to pb bimap, provided that access to it is unlocked. It will result in a crash otherwise.
55+
/// @return Mutable reference to the atom pb bimap.
56+
inline AtomPBBimap& mutable_atom_pb_bimap() {
57+
VTR_ASSERT(!lock_atom_pb_bimap_);
58+
return atom_to_pb_bimap_;
59+
}
60+
61+
/// @brief Returns an immutable reference to the atom to pb bimap, provided that access to it is unlocked. It will result in a crash otherwise.
62+
/// @return Immutable reference to the atom pb bimap.
63+
inline const AtomPBBimap& atom_pb_bimap() const {
64+
VTR_ASSERT(!lock_atom_pb_bimap_);
65+
return atom_to_pb_bimap_;
66+
}
4467

4568
/**
46-
* @brief Sets the bidirectional mapping between an atom and pb
47-
*
48-
* If either blk_id or pb are not valid any, existing mapping is removed
69+
* @brief Set atom to pb bimap
70+
*
71+
* @param atom_to_pb Reference to AtomPBBimab to be copied from
4972
*/
50-
void set_atom_pb(const AtomBlockId blk_id, const t_pb* pb);
73+
void set_atom_to_pb_bimap(const AtomPBBimap& atom_to_pb) { atom_to_pb_bimap_ = atom_to_pb; }
5174

5275
/*
5376
* PB Pins
@@ -112,7 +135,12 @@ class AtomLookup {
112135

113136
private: //Types
114137
private:
115-
vtr::bimap<AtomBlockId, const t_pb*, vtr::linear_map, std::unordered_map> atom_to_pb_;
138+
/**
139+
* @brief Allows or disallows access to the AtomPBBimap data.
140+
* Useful to make sure global context is not accessed in places you don't want it to.
141+
*/
142+
bool lock_atom_pb_bimap_ = false;
143+
AtomPBBimap atom_to_pb_bimap_;
116144

117145
vtr::vector_map<AtomPinId, const t_pb_graph_pin*> atom_pin_to_pb_graph_pin_;
118146

vpr/src/base/clustered_netlist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "clustered_netlist.h"
2-
2+
#include "globals.h"
33
#include "physical_types_util.h"
44
#include "vtr_assert.h"
55

@@ -171,7 +171,7 @@ ClusterNetId ClusteredNetlist::create_net(const std::string& name) {
171171

172172
void ClusteredNetlist::remove_block_impl(const ClusterBlockId blk_id) {
173173
//Remove & invalidate pointers
174-
free_pb(block_pbs_[blk_id]);
174+
free_pb(block_pbs_[blk_id], g_vpr_ctx.mutable_atom().mutable_lookup().mutable_atom_pb_bimap());
175175
delete block_pbs_[blk_id];
176176
block_pbs_.insert(blk_id, NULL);
177177
block_types_.insert(blk_id, NULL);

vpr/src/base/load_flat_place.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void print_flat_cluster(FILE* fp,
7171
// Print a line for each atom.
7272
for (AtomBlockId atom : atoms_lookup[blk_id]) {
7373
// Get the atom pb graph node.
74-
t_pb_graph_node* atom_pbgn = atom_ctx.lookup().atom_pb(atom)->pb_graph_node;
74+
t_pb_graph_node* atom_pbgn = atom_ctx.lookup().atom_pb_bimap().atom_pb(atom)->pb_graph_node;
7575

7676
// Print the flat placement information for this atom.
7777
fprintf(fp, "%s %d %d %d %d %d #%zu %s\n",

vpr/src/base/netlist_writer.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ class NetlistWriterVisitor : public NetlistVisitor {
859859
void visit_atom_impl(const t_pb* atom) override {
860860
auto& atom_ctx = g_vpr_ctx.atom();
861861

862-
auto atom_pb = atom_ctx.lookup().pb_atom(atom);
862+
auto atom_pb = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom);
863863
if (atom_pb == AtomBlockId::INVALID()) {
864864
return;
865865
}
@@ -1787,7 +1787,7 @@ class NetlistWriterVisitor : public NetlistVisitor {
17871787
}
17881788

17891789
auto& atom_ctx = g_vpr_ctx.atom();
1790-
AtomBlockId blk_id = atom_ctx.lookup().pb_atom(atom);
1790+
AtomBlockId blk_id = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom);
17911791
for (auto param : atom_ctx.netlist().block_params(blk_id)) {
17921792
params[param.first] = param.second;
17931793
}
@@ -1809,7 +1809,7 @@ class NetlistWriterVisitor : public NetlistVisitor {
18091809
tatum::NodeId find_tnode(const t_pb* atom, int cluster_pin_idx) {
18101810
auto& atom_ctx = g_vpr_ctx.atom();
18111811

1812-
AtomBlockId blk_id = atom_ctx.lookup().pb_atom(atom);
1812+
AtomBlockId blk_id = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom);
18131813
ClusterBlockId clb_index = atom_ctx.lookup().atom_clb(blk_id);
18141814

18151815
auto key = std::make_pair(clb_index, cluster_pin_idx);
@@ -1840,7 +1840,7 @@ class NetlistWriterVisitor : public NetlistVisitor {
18401840
const t_pb* atom) { //LUT primitive
18411841
auto& atom_ctx = g_vpr_ctx.atom();
18421842

1843-
const t_model* model = atom_ctx.netlist().block_model(atom_ctx.lookup().pb_atom(atom));
1843+
const t_model* model = atom_ctx.netlist().block_model(atom_ctx.lookup().atom_pb_bimap().pb_atom(atom));
18441844
VTR_ASSERT(model->name == std::string(MODEL_NAMES));
18451845

18461846
#ifdef DEBUG_LUT_MASK
@@ -1851,7 +1851,7 @@ class NetlistWriterVisitor : public NetlistVisitor {
18511851
std::vector<int> permute = determine_lut_permutation(num_inputs, atom);
18521852

18531853
//Retrieve the truth table
1854-
const auto& truth_table = atom_ctx.netlist().block_truth_table(atom_ctx.lookup().pb_atom(atom));
1854+
const auto& truth_table = atom_ctx.netlist().block_truth_table(atom_ctx.lookup().atom_pb_bimap().pb_atom(atom));
18551855

18561856
//Apply the permutation
18571857
auto permuted_truth_table = permute_truth_table(truth_table, num_inputs, permute);
@@ -1896,7 +1896,7 @@ class NetlistWriterVisitor : public NetlistVisitor {
18961896
//
18971897
//We walk through the logical inputs to this atom (i.e. in the original truth table/netlist)
18981898
//and find the corresponding input in the implementation atom (i.e. in the current netlist)
1899-
auto ports = atom_ctx.netlist().block_input_ports(atom_ctx.lookup().pb_atom(atom_pb));
1899+
auto ports = atom_ctx.netlist().block_input_ports(atom_ctx.lookup().atom_pb_bimap().pb_atom(atom_pb));
19001900
if (ports.size() == 1) {
19011901
const t_pb_graph_node* gnode = atom_pb->pb_graph_node;
19021902
VTR_ASSERT(gnode->num_input_ports == 1);
@@ -2144,7 +2144,7 @@ class MergedNetlistWriterVisitor : public NetlistWriterVisitor {
21442144
void visit_atom_impl(const t_pb* atom) override {
21452145
auto& atom_ctx = g_vpr_ctx.atom();
21462146

2147-
auto atom_pb = atom_ctx.lookup().pb_atom(atom);
2147+
auto atom_pb = atom_ctx.lookup().atom_pb_bimap().pb_atom(atom);
21482148
if (atom_pb == AtomBlockId::INVALID()) {
21492149
return;
21502150
}

vpr/src/base/read_netlist.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ ClusteredNetlist read_netlist(const char* net_file,
177177

178178
//Reset atom/pb mapping (it is reloaded from the packed netlist file)
179179
for (auto blk_id : atom_ctx.netlist().blocks())
180-
atom_ctx.mutable_lookup().set_atom_pb(blk_id, nullptr);
180+
atom_ctx.mutable_lookup().mutable_atom_pb_bimap().set_atom_pb(blk_id, nullptr);
181181

182182
//Count the number of blocks for allocation
183183
bcount = pugiutil::count_children(top, "block", loc_data, pugiutil::ReqOpt::OPTIONAL);
@@ -197,7 +197,7 @@ ClusteredNetlist read_netlist(const char* net_file,
197197

198198
/* Error check */
199199
for (auto blk_id : atom_ctx.netlist().blocks()) {
200-
if (atom_ctx.lookup().atom_pb(blk_id) == nullptr) {
200+
if (atom_ctx.lookup().atom_pb_bimap().atom_pb(blk_id) == nullptr) {
201201
VPR_FATAL_ERROR(VPR_ERROR_NET_F,
202202
".blif file and .net file do not match, .net file missing atom %s.\n",
203203
atom_ctx.netlist().block_name(blk_id).c_str());
@@ -319,7 +319,7 @@ static void processComplexBlock(pugi::xml_node clb_block,
319319
}
320320

321321
//Parse all pbs and CB internal nets
322-
atom_ctx.mutable_lookup().set_atom_pb(AtomBlockId::INVALID(), clb_nlist->block_pb(index));
322+
atom_ctx.mutable_lookup().mutable_atom_pb_bimap().set_atom_pb(AtomBlockId::INVALID(), clb_nlist->block_pb(index));
323323

324324
clb_nlist->block_pb(index)->pb_graph_node = clb_nlist->block_type(index)->pb_graph_head;
325325
clb_nlist->block_pb(index)->pb_route = alloc_pb_route(clb_nlist->block_pb(index)->pb_graph_node);
@@ -474,7 +474,7 @@ static void processPb(pugi::xml_node Parent, const ClusterBlockId index, t_pb* p
474474

475475
//Update atom netlist mapping
476476
VTR_ASSERT(blk_id);
477-
atom_ctx.mutable_lookup().set_atom_pb(blk_id, pb);
477+
atom_ctx.mutable_lookup().mutable_atom_pb_bimap().set_atom_pb(blk_id, pb);
478478
atom_ctx.mutable_lookup().set_atom_clb(blk_id, index);
479479

480480
auto atom_attrs = atom_ctx.netlist().block_attrs(blk_id);
@@ -542,7 +542,7 @@ static void processPb(pugi::xml_node Parent, const ClusterBlockId index, t_pb* p
542542
pb->child_pbs[i][pb_index].name = vtr::strdup(name.value());
543543

544544
/* Parse all pbs and CB internal nets*/
545-
atom_ctx.mutable_lookup().set_atom_pb(AtomBlockId::INVALID(), &pb->child_pbs[i][pb_index]);
545+
atom_ctx.mutable_lookup().mutable_atom_pb_bimap().set_atom_pb(AtomBlockId::INVALID(), &pb->child_pbs[i][pb_index]);
546546

547547
auto mode = child.attribute("mode");
548548
pb->child_pbs[i][pb_index].mode = 0;
@@ -564,7 +564,7 @@ static void processPb(pugi::xml_node Parent, const ClusterBlockId index, t_pb* p
564564
} else {
565565
/* physical block has no used primitives but it may have used routing */
566566
pb->child_pbs[i][pb_index].name = nullptr;
567-
atom_ctx.mutable_lookup().set_atom_pb(AtomBlockId::INVALID(), &pb->child_pbs[i][pb_index]);
567+
atom_ctx.mutable_lookup().mutable_atom_pb_bimap().set_atom_pb(AtomBlockId::INVALID(), &pb->child_pbs[i][pb_index]);
568568

569569
auto lookahead1 = pugiutil::get_first_child(child, "outputs", loc_data, pugiutil::OPTIONAL);
570570
if (lookahead1) {
@@ -1180,7 +1180,7 @@ static void load_atom_pin_mapping(const ClusteredNetlist& clb_nlist) {
11801180
auto& atom_ctx = g_vpr_ctx.atom();
11811181

11821182
for (const AtomBlockId blk : atom_ctx.netlist().blocks()) {
1183-
const t_pb* pb = atom_ctx.lookup().atom_pb(blk);
1183+
const t_pb* pb = atom_ctx.lookup().atom_pb_bimap().atom_pb(blk);
11841184
VTR_ASSERT_MSG(pb, "Atom block must have a matching PB");
11851185

11861186
const t_pb_graph_node* gnode = pb->pb_graph_node;
@@ -1250,7 +1250,7 @@ void set_atom_pin_mapping(const ClusteredNetlist& clb_nlist, const AtomBlockId a
12501250
return;
12511251
}
12521252

1253-
const t_pb* atom_pb = atom_ctx.lookup().atom_pb(atom_blk);
1253+
const t_pb* atom_pb = atom_ctx.lookup().atom_pb_bimap().atom_pb(atom_blk);
12541254

12551255
//This finds the index within the atom port to which the current gpin
12561256
//is mapped. Note that this accounts for any applied pin rotations

vpr/src/draw/draw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ ezgl::point2d atom_pin_draw_coord(AtomPinId pin) {
797797

798798
AtomBlockId blk = atom_ctx.netlist().pin_block(pin);
799799
ClusterBlockId clb_index = atom_ctx.lookup().atom_clb(blk);
800-
const t_pb_graph_node* pg_gnode = atom_ctx.lookup().atom_pb_graph_node(blk);
800+
const t_pb_graph_node* pg_gnode = atom_ctx.lookup().atom_pb_bimap().atom_pb_graph_node(blk);
801801

802802
t_draw_coords* draw_coords = get_draw_coords_vars();
803803
ezgl::rectangle pb_bbox = draw_coords->get_absolute_pb_bbox(clb_index,

vpr/src/draw/draw_floorplanning.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ void draw_constrained_atoms(ezgl::renderer* g) {
152152
auto atoms = constraints.get_part_atoms((PartitionId)partitionID);
153153

154154
for (const AtomBlockId atom_id : atoms) {
155-
if (atom_ctx.lookup().atom_pb(atom_id) != nullptr) {
156-
const t_pb* pb = atom_ctx.lookup().atom_pb(atom_id);
155+
if (atom_ctx.lookup().atom_pb_bimap().atom_pb(atom_id) != nullptr) {
156+
const t_pb* pb = atom_ctx.lookup().atom_pb_bimap().atom_pb(atom_id);
157157
auto color = kelly_max_contrast_colors_no_black[partitionID % (kelly_max_contrast_colors_no_black.size())];
158158
ClusterBlockId clb_index = atom_ctx.lookup().atom_clb(atom_id);
159159
auto type = cluster_ctx.clb_nlist.block_type(clb_index);
@@ -310,7 +310,7 @@ static GtkTreeModel* create_and_fill_model() {
310310
-1);
311311

312312
for (AtomBlockId const_atom : atoms) {
313-
std::string atom_name = (atom_ctx.lookup().atom_pb(const_atom))->name;
313+
std::string atom_name = (atom_ctx.lookup().atom_pb_bimap().atom_pb(const_atom))->name;
314314
gtk_tree_store_append(store, &child_iter, &iter);
315315
gtk_tree_store_set(store, &child_iter,
316316
COL_NAME, atom_name.c_str(),

0 commit comments

Comments
 (0)