Skip to content

Commit cbe5665

Browse files
pass by ref and range based loops
1 parent 66fa02d commit cbe5665

24 files changed

+156
-102
lines changed

libs/libvtrutil/src/vtr_util.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static int cont; /* line continued? (used by strtok)*/
2626
*
2727
* The split strings (excluding the delimiters) are returned
2828
*/
29-
std::vector<std::string> split(const char* text, const std::string delims) {
29+
std::vector<std::string> split(const char* text, const std::string& delims) {
3030
if (text) {
3131
std::string text_str(text);
3232
return split(text_str, delims);
@@ -39,7 +39,7 @@ std::vector<std::string> split(const char* text, const std::string delims) {
3939
*
4040
* The split strings (excluding the delimiters) are returned
4141
*/
42-
std::vector<std::string> split(const std::string& text, const std::string delims) {
42+
std::vector<std::string> split(const std::string& text, const std::string& delims) {
4343
std::vector<std::string> tokens;
4444

4545
std::string curr_tok;
@@ -102,7 +102,7 @@ std::string replace_all(const std::string& input, const std::string& search, con
102102
}
103103

104104
///@brief Retruns true if str starts with prefix
105-
bool starts_with(std::string str, std::string prefix) {
105+
bool starts_with(const std::string& str, const std::string& prefix) {
106106
return str.find(prefix) == 0;
107107
}
108108

@@ -461,8 +461,8 @@ bool file_exists(const char* filename) {
461461
*
462462
* Returns true if the extension is correct, and false otherwise.
463463
*/
464-
bool check_file_name_extension(std::string file_name,
465-
std::string file_extension) {
464+
bool check_file_name_extension(const std::string& file_name,
465+
const std::string& file_extension) {
466466
auto ext = std::filesystem::path(file_name).extension();
467467
return ext == file_extension;
468468
}

libs/libvtrutil/src/vtr_util.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace vtr {
1414
*
1515
* The split strings (excluding the delimiters) are returned
1616
*/
17-
std::vector<std::string> split(const char* text, const std::string delims = " \t\n");
18-
std::vector<std::string> split(const std::string& text, const std::string delims = " \t\n");
17+
std::vector<std::string> split(const char* text, const std::string& delims = " \t\n");
18+
std::vector<std::string> split(const std::string& text, const std::string& delims = " \t\n");
1919

2020
///@brief Returns 'input' with the first instance of 'search' replaced with 'replace'
2121
std::string replace_first(const std::string& input, const std::string& search, const std::string& replace);
@@ -24,7 +24,7 @@ std::string replace_first(const std::string& input, const std::string& search, c
2424
std::string replace_all(const std::string& input, const std::string& search, const std::string& replace);
2525

2626
///@brief Retruns true if str starts with prefix
27-
bool starts_with(std::string str, std::string prefix);
27+
bool starts_with(const std::string& str, const std::string& prefix);
2828

2929
///@brief Returns a std::string formatted using a printf-style format string
3030
std::string string_fmt(const char* fmt, ...);
@@ -69,7 +69,7 @@ double atod(const std::string& value);
6969
*/
7070
int get_file_line_number_of_last_opened_file();
7171
bool file_exists(const char* filename);
72-
bool check_file_name_extension(std::string file_name, std::string file_extension);
72+
bool check_file_name_extension(const std::string& file_name, const std::string& file_extension);
7373

7474
extern std::string out_file_prefix;
7575

vpr/src/base/SetupGrid.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ using vtr::t_formula_data;
3131

3232
static DeviceGrid auto_size_device_grid(const std::vector<t_grid_def>& grid_layouts, const std::map<t_logical_block_type_ptr, size_t>& minimum_instance_counts, float maximum_device_utilization);
3333
static std::vector<t_logical_block_type_ptr> grid_overused_resources(const DeviceGrid& grid, std::map<t_logical_block_type_ptr, size_t> instance_counts);
34-
static bool grid_satisfies_instance_counts(const DeviceGrid& grid, std::map<t_logical_block_type_ptr, size_t> instance_counts, float maximum_utilization);
35-
static DeviceGrid build_device_grid(const t_grid_def& grid_def, size_t width, size_t height, bool warn_out_of_range = true, std::vector<t_logical_block_type_ptr> limiting_resources = std::vector<t_logical_block_type_ptr>());
34+
static bool grid_satisfies_instance_counts(const DeviceGrid& grid, const std::map<t_logical_block_type_ptr, size_t>& instance_counts, float maximum_utilization);
35+
static DeviceGrid build_device_grid(const t_grid_def& grid_def, size_t width, size_t height, bool warn_out_of_range = true, const std::vector<t_logical_block_type_ptr>& limiting_resources = std::vector<t_logical_block_type_ptr>());
3636

3737
static void CheckGrid(const DeviceGrid& grid);
3838

@@ -46,7 +46,7 @@ static void set_grid_block_type(int priority,
4646
const t_metadata_dict* meta);
4747

4848
///@brief Create the device grid based on resource requirements
49-
DeviceGrid create_device_grid(std::string layout_name, const std::vector<t_grid_def>& grid_layouts, const std::map<t_logical_block_type_ptr, size_t>& minimum_instance_counts, float target_device_utilization) {
49+
DeviceGrid create_device_grid(const std::string& layout_name, const std::vector<t_grid_def>& grid_layouts, const std::map<t_logical_block_type_ptr, size_t>& minimum_instance_counts, float target_device_utilization) {
5050
if (layout_name == "auto") {
5151
//Auto-size the device
5252
//
@@ -78,9 +78,9 @@ DeviceGrid create_device_grid(std::string layout_name, const std::vector<t_grid_
7878
}
7979

8080
///@brief Create the device grid based on dimensions
81-
DeviceGrid create_device_grid(std::string layout_name, const std::vector<t_grid_def>& grid_layouts, size_t width, size_t height) {
81+
DeviceGrid create_device_grid(const std::string& layout_name, const std::vector<t_grid_def>& grid_layouts, size_t width, size_t height) {
8282
if (layout_name == "auto") {
83-
VTR_ASSERT(grid_layouts.size() > 0);
83+
VTR_ASSERT(!grid_layouts.empty());
8484
//Auto-size
8585
if (grid_layouts[0].grid_type == GridDefType::AUTO) {
8686
//Auto layout of the specified dimensions
@@ -145,7 +145,7 @@ DeviceGrid create_device_grid(std::string layout_name, const std::vector<t_grid_
145145
* If an auto grid layouts are specified, the smallest dynamicly sized grid is picked
146146
*/
147147
static DeviceGrid auto_size_device_grid(const std::vector<t_grid_def>& grid_layouts, const std::map<t_logical_block_type_ptr, size_t>& minimum_instance_counts, float maximum_device_utilization) {
148-
VTR_ASSERT(grid_layouts.size() > 0);
148+
VTR_ASSERT(!grid_layouts.empty());
149149

150150
DeviceGrid grid;
151151

@@ -281,6 +281,7 @@ static std::vector<t_logical_block_type_ptr> grid_overused_resources(const Devic
281281

282282
//Sort so we allocate logical blocks with the fewest equivalent sites first (least flexible)
283283
std::vector<const t_logical_block_type*> logical_block_types;
284+
logical_block_types.reserve(device_ctx.logical_block_types.size());
284285
for (auto& block_type : device_ctx.logical_block_types) {
285286
logical_block_types.push_back(&block_type);
286287
}
@@ -316,7 +317,7 @@ static std::vector<t_logical_block_type_ptr> grid_overused_resources(const Devic
316317
return overused_resources;
317318
}
318319

319-
static bool grid_satisfies_instance_counts(const DeviceGrid& grid, std::map<t_logical_block_type_ptr, size_t> instance_counts, float maximum_utilization) {
320+
static bool grid_satisfies_instance_counts(const DeviceGrid& grid, const std::map<t_logical_block_type_ptr, size_t>& instance_counts, float maximum_utilization) {
320321
//Are the resources satisified?
321322
auto overused_resources = grid_overused_resources(grid, instance_counts);
322323

@@ -335,7 +336,7 @@ static bool grid_satisfies_instance_counts(const DeviceGrid& grid, std::map<t_lo
335336
}
336337

337338
///@brief Build the specified device grid
338-
static DeviceGrid build_device_grid(const t_grid_def& grid_def, size_t grid_width, size_t grid_height, bool warn_out_of_range, const std::vector<t_logical_block_type_ptr> limiting_resources) {
339+
static DeviceGrid build_device_grid(const t_grid_def& grid_def, size_t grid_width, size_t grid_height, bool warn_out_of_range, const std::vector<t_logical_block_type_ptr>& limiting_resources) {
339340
if (grid_def.grid_type == GridDefType::FIXED) {
340341
if (grid_def.width != int(grid_width) || grid_def.height != int(grid_height)) {
341342
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
@@ -754,7 +755,7 @@ static void CheckGrid(const DeviceGrid& grid) {
754755
}
755756
}
756757

757-
float calculate_device_utilization(const DeviceGrid& grid, std::map<t_logical_block_type_ptr, size_t> instance_counts) {
758+
float calculate_device_utilization(const DeviceGrid& grid, const std::map<t_logical_block_type_ptr, size_t>& instance_counts) {
758759
//Record the resources of the grid
759760
std::map<t_physical_tile_type_ptr, size_t> grid_resources;
760761
for (int layer_num = 0; layer_num < grid.get_num_layers(); ++layer_num) {

vpr/src/base/SetupGrid.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@
1313
#include "physical_types.h"
1414

1515
///@brief Find the device satisfying the specified minimum resources
16-
DeviceGrid create_device_grid(std::string layout_name,
16+
DeviceGrid create_device_grid(const std::string& layout_name,
1717
const std::vector<t_grid_def>& grid_layouts,
1818
const std::map<t_logical_block_type_ptr, size_t>& minimum_instance_counts,
1919
float target_device_utilization);
2020

2121
///@brief Find the device close in size to the specified dimensions
22-
DeviceGrid create_device_grid(std::string layout_name, const std::vector<t_grid_def>& grid_layouts, size_t min_width, size_t min_height);
22+
DeviceGrid create_device_grid(const std::string& layout_name,
23+
const std::vector<t_grid_def>& grid_layouts,
24+
size_t min_width,
25+
size_t min_height);
2326

2427
/**
2528
* @brief Calculate the device utilization
2629
*
2730
* Calculate the device utilization (i.e. fraction of used grid tiles)
2831
* foor the specified grid and resource requirements
2932
*/
30-
float calculate_device_utilization(const DeviceGrid& grid, std::map<t_logical_block_type_ptr, size_t> instance_counts);
33+
float calculate_device_utilization(const DeviceGrid& grid, const std::map<t_logical_block_type_ptr, size_t>& instance_counts);
3134

3235
/**
3336
* @brief Returns the effective size of the device

vpr/src/base/atom_netlist.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ AtomBlockId AtomNetlist::find_atom_pin_driver(const AtomBlockId blk_id, const t_
115115
return AtomBlockId::INVALID();
116116
}
117117

118-
std::unordered_set<std::string> AtomNetlist::net_aliases(const std::string net_name) const {
118+
std::unordered_set<std::string> AtomNetlist::net_aliases(const std::string& net_name) const {
119119
auto net_id = find_net(net_name);
120120
VTR_ASSERT(net_id != AtomNetId::INVALID());
121121

@@ -137,7 +137,7 @@ std::unordered_set<std::string> AtomNetlist::net_aliases(const std::string net_n
137137
* Mutators
138138
*
139139
*/
140-
AtomBlockId AtomNetlist::create_block(const std::string name, const t_model* model, const TruthTable truth_table) {
140+
AtomBlockId AtomNetlist::create_block(const std::string& name, const t_model* model, const TruthTable& truth_table) {
141141
AtomBlockId blk_id = Netlist::create_block(name);
142142

143143
//Initialize the data
@@ -205,7 +205,7 @@ AtomPinId AtomNetlist::create_pin(const AtomPortId port_id, BitIndex port_bit, c
205205
return pin_id;
206206
}
207207

208-
AtomNetId AtomNetlist::create_net(const std::string name) {
208+
AtomNetId AtomNetlist::create_net(const std::string& name) {
209209
AtomNetId net_id = Netlist::create_net(name);
210210

211211
//Check post-conditions: size
@@ -214,11 +214,11 @@ AtomNetId AtomNetlist::create_net(const std::string name) {
214214
return net_id;
215215
}
216216

217-
AtomNetId AtomNetlist::add_net(const std::string name, AtomPinId driver, std::vector<AtomPinId> sinks) {
217+
AtomNetId AtomNetlist::add_net(const std::string& name, AtomPinId driver, std::vector<AtomPinId> sinks) {
218218
return Netlist::add_net(name, driver, sinks);
219219
}
220220

221-
void AtomNetlist::add_net_alias(const std::string net_name, const std::string alias_net_name) {
221+
void AtomNetlist::add_net_alias(const std::string& net_name, const std::string& alias_net_name) {
222222
auto net_id = find_net(net_name);
223223
VTR_ASSERT(net_id != AtomNetId::INVALID());
224224

vpr/src/base/atom_netlist.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class AtomNetlist : public Netlist<AtomBlockId, AtomPortId, AtomPinId, AtomNetId
157157
*
158158
* @param net_name name of the net from which the aliases are extracted
159159
*/
160-
std::unordered_set<std::string> net_aliases(const std::string net_name) const;
160+
std::unordered_set<std::string> net_aliases(const std::string& net_name) const;
161161

162162
public: //Public Mutators
163163
/*
@@ -173,7 +173,7 @@ class AtomNetlist : public Netlist<AtomBlockId, AtomPortId, AtomPinId, AtomNetId
173173
* The truth_table is optional and only relevant for LUTs (where it describes the logic function)
174174
* and Flip-Flops/latches (where it consists of a single entry defining the initial state).
175175
*/
176-
AtomBlockId create_block(const std::string name, const t_model* model, const TruthTable truth_table = TruthTable());
176+
AtomBlockId create_block(const std::string& name, const t_model* model, const TruthTable& truth_table = TruthTable());
177177

178178
/**
179179
* @brief Create or return an existing port in the netlist
@@ -199,7 +199,7 @@ class AtomNetlist : public Netlist<AtomBlockId, AtomPortId, AtomPinId, AtomNetId
199199
*
200200
* @param name The unique name of the net
201201
*/
202-
AtomNetId create_net(const std::string name); //An empty or existing net
202+
AtomNetId create_net(const std::string& name); //An empty or existing net
203203

204204
/**
205205
* @brief Create a completely specified net from specified driver and sinks
@@ -208,7 +208,7 @@ class AtomNetlist : public Netlist<AtomBlockId, AtomPortId, AtomPinId, AtomNetId
208208
* @param driver The net's driver pin
209209
* @param sinks The net's sink pins
210210
*/
211-
AtomNetId add_net(const std::string name, AtomPinId driver, std::vector<AtomPinId> sinks);
211+
AtomNetId add_net(const std::string& name, AtomPinId driver, std::vector<AtomPinId> sinks);
212212

213213
/**
214214
* @brief Adds a value to the net aliases set for a given net name in the net_aliases_map.
@@ -218,7 +218,7 @@ class AtomNetlist : public Netlist<AtomBlockId, AtomPortId, AtomPinId, AtomNetId
218218
* @param net_name The net to be added to the map
219219
* @param alias_net_name The alias of the assigned clock net id
220220
*/
221-
void add_net_alias(const std::string net_name, std::string alias_net_name);
221+
void add_net_alias(const std::string& net_name, const std::string& alias_net_name);
222222

223223
private: //Private members
224224
/*

vpr/src/base/constraints_load.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "constraints_load.h"
22

3-
void echo_constraints(char* filename, VprConstraints constraints) {
3+
void echo_constraints(char* filename, const VprConstraints& constraints) {
44
FILE* fp;
55
fp = vtr::fopen(filename, "w");
66

vpr/src/base/constraints_load.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
#include "vtr_vector.h"
99

1010
///@brief Used to print vpr's floorplanning constraints to an echo file "vpr_constraints.echo"
11-
void echo_constraints(char* filename, VprConstraints constraints);
11+
void echo_constraints(char* filename, const VprConstraints& constraints);
1212

1313
#endif

vpr/src/base/setup_noc.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void setup_noc(const t_arch& arch) {
3333
} else if (noc_router_tiles.size() > arch.noc->router_list.size()) // check whether the noc topology information provided is using all the routers in the FPGA
3434
{
3535
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "The Provided NoC topology information in the architecture file uses less number of routers than what is available in the FPGA device.");
36-
} else if (noc_router_tiles.size() == 0) // case where no physical router tiles were found
36+
} else if (noc_router_tiles.empty()) // case where no physical router tiles were found
3737
{
3838
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "No physical NoC routers were found on the FPGA device. Either the provided name for the physical router tile was incorrect or the FPGA device has no routers.");
3939
}
@@ -58,7 +58,7 @@ void setup_noc(const t_arch& arch) {
5858
return;
5959
}
6060

61-
void identify_and_store_noc_router_tile_positions(const DeviceGrid& device_grid, std::vector<t_noc_router_tile_position>& noc_router_tiles, std::string noc_router_tile_name) {
61+
void identify_and_store_noc_router_tile_positions(const DeviceGrid& device_grid, std::vector<t_noc_router_tile_position>& noc_router_tiles, const std::string& noc_router_tile_name) {
6262
const int num_layers = device_grid.get_num_layers();
6363
int curr_tile_width;
6464
int curr_tile_height;
@@ -173,10 +173,10 @@ void create_noc_routers(const t_noc_inf& noc_info, NocStorage* noc_model, std::v
173173
error_case_physical_router_index_2 = INVALID_PHYSICAL_ROUTER_INDEX;
174174

175175
// determine the physical router tile that is closest to the current user described router in the arch file
176-
for (auto physical_router = noc_router_tiles.begin(); physical_router != noc_router_tiles.end(); physical_router++) {
176+
for (auto & physical_router : noc_router_tiles) {
177177
// get the position of the current physical router tile on the FPGA device
178-
curr_physical_router_pos_x = physical_router->tile_centroid_x;
179-
curr_physical_router_pos_y = physical_router->tile_centroid_y;
178+
curr_physical_router_pos_x = physical_router.tile_centroid_x;
179+
curr_physical_router_pos_y = physical_router.tile_centroid_y;
180180

181181
// use euclidean distance to calculate the length between the current user described router and the physical router
182182
curr_calculated_distance = sqrt(pow(abs(curr_physical_router_pos_x - curr_logical_router_position_x), 2.0) + pow(abs(curr_physical_router_pos_y - curr_logical_router_position_y), 2.0));
@@ -237,14 +237,14 @@ void create_noc_links(const t_noc_inf* noc_info, NocStorage* noc_model) {
237237
noc_model->make_room_for_noc_router_link_list();
238238

239239
// go through each router and add its outgoing links to the NoC
240-
for (auto router = noc_info->router_list.begin(); router != noc_info->router_list.end(); router++) {
240+
for (const auto & router : noc_info->router_list) {
241241
// get the converted id of the current source router
242-
source_router = noc_model->convert_router_id(router->id);
242+
source_router = noc_model->convert_router_id(router.id);
243243

244244
// go through all the routers connected to the current one and add links to the noc
245-
for (auto conn_router_id = router->connection_list.begin(); conn_router_id != router->connection_list.end(); conn_router_id++) {
245+
for (int conn_router_id : router.connection_list) {
246246
// get the converted id of the currently connected sink router
247-
sink_router = noc_model->convert_router_id(*conn_router_id);
247+
sink_router = noc_model->convert_router_id(conn_router_id);
248248

249249
// add the link to the Noc
250250
noc_model->add_link(source_router, sink_router);

vpr/src/base/setup_noc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void setup_noc(const t_arch& arch);
8989
* tile in the FPGA architecture description
9090
* file.
9191
*/
92-
void identify_and_store_noc_router_tile_positions(const DeviceGrid& device_grid, std::vector<t_noc_router_tile_position>& list_of_noc_router_tiles, std::string noc_router_tile_name);
92+
void identify_and_store_noc_router_tile_positions(const DeviceGrid& device_grid, std::vector<t_noc_router_tile_position>& list_of_noc_router_tiles, const std::string& noc_router_tile_name);
9393

9494
/**
9595
* @brief Creates NoC routers and adds them to the NoC model based

vpr/src/base/vpr_constraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Partition& VprConstraints::get_mutable_partition(PartitionId part_id) {
4141
std::vector<AtomBlockId> VprConstraints::get_part_atoms(PartitionId part_id) const {
4242
std::vector<AtomBlockId> part_atoms;
4343

44-
for (auto& it : constrained_atoms) {
44+
for (const auto& it : constrained_atoms) {
4545
if (it.second == part_id) {
4646
part_atoms.push_back(it.first);
4747
}

vpr/src/base/vpr_constraints_reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void load_vpr_constraints_file(const char* read_vpr_constraints_name) {
3535
auto& floorplanning_ctx = g_vpr_ctx.mutable_floorplanning();
3636
floorplanning_ctx.constraints = reader.constraints_;
3737

38-
VprConstraints ctx_constraints = floorplanning_ctx.constraints;
38+
const auto& ctx_constraints = floorplanning_ctx.constraints;
3939

4040
if (getEchoEnabled() && isEchoFileEnabled(E_ECHO_VPR_CONSTRAINTS)) {
4141
echo_constraints(getEchoFileName(E_ECHO_VPR_CONSTRAINTS), ctx_constraints);

0 commit comments

Comments
 (0)