Skip to content

Commit 6d722f3

Browse files
return by value in parse_direct_pin_name()
1 parent 0177ba1 commit 6d722f3

File tree

5 files changed

+84
-107
lines changed

5 files changed

+84
-107
lines changed

libs/libarchfpga/src/physical_types_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ int get_sub_tile_physical_pin(int sub_tile_index,
284284
* Given that each sub_tile's port that has exactly the same name has to be equivalent
285285
* one to the other, it is indifferent which port is returned.
286286
*/
287-
t_physical_tile_port find_tile_port_by_name(t_physical_tile_type_ptr type, const char* port_name);
287+
t_physical_tile_port find_tile_port_by_name(t_physical_tile_type_ptr type, std::string_view port_name);
288288

289289
/**
290290
* @brief Returns the physical tile port given the port name and the corresponding sub tile

vpr/src/place/place_macro.cpp

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cmath>
66
#include <sstream>
77
#include <map>
8+
#include <string_view>
89

910
#include "vtr_assert.h"
1011
#include "vtr_util.h"
@@ -28,11 +29,11 @@ static bool try_combine_macros(std::vector<std::vector<ClusterBlockId>>& pl_macr
2829
* Otherwise, mark down all the pins in that port. */
2930
static void mark_direct_of_ports(int idirect,
3031
int direct_type,
31-
char* pb_type_name,
32-
char* port_name,
32+
std::string_view pb_type_name,
33+
std::string_view port_name,
3334
int end_pin_index,
3435
int start_pin_index,
35-
const char* src_string,
36+
std::string_view src_string,
3637
int line,
3738
std::vector<std::vector<int>>& idirect_from_blk_pin,
3839
std::vector<std::vector<int>>& direct_type_from_blk_pin);
@@ -50,7 +51,7 @@ static void mark_direct_of_pins(int start_pin_index,
5051
std::vector<std::vector<int>>& direct_type_from_blk_pin,
5152
int direct_type,
5253
int line,
53-
const char* src_string);
54+
std::string_view src_string);
5455

5556
const std::vector<t_pl_macro>& PlaceMacros::macros() const {
5657
return pl_macros_;
@@ -393,18 +394,6 @@ int PlaceMacros::get_imacro_from_iblk(ClusterBlockId iblk) const {
393394
void PlaceMacros::alloc_and_load_idirect_from_blk_pin_(const std::vector<t_direct_inf>& directs) {
394395
const auto& device_ctx = g_vpr_ctx.device();
395396

396-
constexpr size_t MAX_STRING_LEN = 512;
397-
398-
char to_pb_type_name[MAX_STRING_LEN + 1];
399-
char to_port_name[MAX_STRING_LEN + 1];
400-
char from_pb_type_name[MAX_STRING_LEN + 1];
401-
char from_port_name[MAX_STRING_LEN + 1];
402-
403-
int to_start_pin_index = -1;
404-
int to_end_pin_index = -1;
405-
int from_start_pin_index = -1;
406-
int from_end_pin_index = -1;
407-
408397
// Allocate and initialize the values to OPEN (-1).
409398
idirect_from_blk_pin_.resize(device_ctx.physical_tile_types.size());
410399
direct_type_from_blk_pin_.resize(device_ctx.physical_tile_types.size());
@@ -421,27 +410,27 @@ void PlaceMacros::alloc_and_load_idirect_from_blk_pin_(const std::vector<t_direc
421410
// Go through directs and find pins with possible direct connections
422411
for (size_t idirect = 0; idirect < directs.size(); idirect++) {
423412
// Parse out the pb_type and port name, possibly pin_indices from from_pin
424-
parse_direct_pin_name(directs[idirect].from_pin.c_str(), directs[idirect].line,
425-
&from_end_pin_index, &from_start_pin_index, from_pb_type_name, from_port_name);
413+
auto [from_end_pin_index, from_start_pin_index, from_pb_type_name, from_port_name] = parse_direct_pin_name(directs[idirect].from_pin,
414+
directs[idirect].line);
426415

427416
// Parse out the pb_type and port name, possibly pin_indices from to_pin
428-
parse_direct_pin_name(directs[idirect].to_pin.c_str(), directs[idirect].line,
429-
&to_end_pin_index, &to_start_pin_index, to_pb_type_name, to_port_name);
417+
auto [to_end_pin_index, to_start_pin_index, to_pb_type_name, to_port_name] = parse_direct_pin_name(directs[idirect].to_pin,
418+
directs[idirect].line);
430419

431-
/* Now I have all the data that I need, I could go through all the block pins *
432-
* in all the blocks to find all the pins that could have possible direct *
433-
* connections. Mark all down all those pins with the idirect the pins belong *
434-
* to and whether it is a source or a sink of the direct connection. */
420+
/* Now I have all the data that I need, I could go through all the block pins
421+
* in all the blocks to find all the pins that could have possible direct
422+
* connections. Mark all down all those pins with the idirect the pins belong
423+
* to and whether it is a source or a sink of the direct connection. */
435424

436425
// Find blocks with the same name as from_pb_type_name and from_port_name
437426
mark_direct_of_ports(idirect, SOURCE, from_pb_type_name, from_port_name,
438-
from_end_pin_index, from_start_pin_index, directs[idirect].from_pin.c_str(),
427+
from_end_pin_index, from_start_pin_index, directs[idirect].from_pin,
439428
directs[idirect].line,
440429
idirect_from_blk_pin_, direct_type_from_blk_pin_);
441430

442431
// Then, find blocks with the same name as to_pb_type_name and from_port_name
443432
mark_direct_of_ports(idirect, SINK, to_pb_type_name, to_port_name,
444-
to_end_pin_index, to_start_pin_index, directs[idirect].to_pin.c_str(),
433+
to_end_pin_index, to_start_pin_index, directs[idirect].to_pin,
445434
directs[idirect].line,
446435
idirect_from_blk_pin_, direct_type_from_blk_pin_);
447436

@@ -450,11 +439,11 @@ void PlaceMacros::alloc_and_load_idirect_from_blk_pin_(const std::vector<t_direc
450439

451440
static void mark_direct_of_ports(int idirect,
452441
int direct_type,
453-
char* pb_type_name,
454-
char* port_name,
442+
std::string_view pb_type_name,
443+
std::string_view port_name,
455444
int end_pin_index,
456445
int start_pin_index,
457-
const char* src_string,
446+
std::string_view src_string,
458447
int line,
459448
std::vector<std::vector<int>>& idirect_from_blk_pin,
460449
std::vector<std::vector<int>>& direct_type_from_blk_pin) {
@@ -470,14 +459,14 @@ static void mark_direct_of_ports(int idirect,
470459
for (int itype = 1; itype < (int)device_ctx.physical_tile_types.size(); itype++) {
471460
auto& physical_tile = device_ctx.physical_tile_types[itype];
472461
// Find blocks with the same pb_type_name
473-
if (strcmp(physical_tile.name, pb_type_name) == 0) {
462+
if (pb_type_name == physical_tile.name ) {
474463
int num_sub_tiles = physical_tile.sub_tiles.size();
475464
for (int isub_tile = 0; isub_tile < num_sub_tiles; isub_tile++) {
476465
auto& ports = physical_tile.sub_tiles[isub_tile].ports;
477466
int num_ports = ports.size();
478467
for (int iport = 0; iport < num_ports; iport++) {
479468
// Find ports with the same port_name
480-
if (strcmp(ports[iport].name, port_name) == 0) {
469+
if (port_name == ports[iport].name ) {
481470
int num_port_pins = ports[iport].num_pins;
482471

483472
// Check whether the end_pin_index is valid
@@ -517,7 +506,7 @@ static void mark_direct_of_pins(int start_pin_index,
517506
std::vector<std::vector<int>>& direct_type_from_blk_pin,
518507
int direct_type,
519508
int line,
520-
const char* src_string) {
509+
std::string_view src_string) {
521510
/* Mark the pin entry in idirect_from_blk_pin with idirect and the pin entry in *
522511
* direct_type_from_blk_pin with direct_type from start_pin_index to *
523512
* end_pin_index. */

vpr/src/route/rr_graph.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4347,8 +4347,6 @@ static void build_unidir_rr_opins(RRGraphBuilder& rr_graph_builder,
43474347
*/
43484348
static t_clb_to_clb_directs* alloc_and_load_clb_to_clb_directs(const std::vector<t_direct_inf>& directs, int delayless_switch) {
43494349
t_clb_to_clb_directs* clb_to_clb_directs;
4350-
char *tile_name, *port_name;
4351-
int start_pin_index, end_pin_index;
43524350
t_physical_tile_type_ptr physical_tile = nullptr;
43534351
t_physical_tile_port tile_port;
43544352

@@ -4357,9 +4355,6 @@ static t_clb_to_clb_directs* alloc_and_load_clb_to_clb_directs(const std::vector
43574355
const int num_directs = directs.size();
43584356
clb_to_clb_directs = new t_clb_to_clb_directs[num_directs];
43594357

4360-
tile_name = nullptr;
4361-
port_name = nullptr;
4362-
43634358
for (int i = 0; i < num_directs; i++) {
43644359
//clb_to_clb_directs[i].from_clb_type;
43654360
clb_to_clb_directs[i].from_clb_pin_start_index = 0;
@@ -4369,23 +4364,20 @@ static t_clb_to_clb_directs* alloc_and_load_clb_to_clb_directs(const std::vector
43694364
clb_to_clb_directs[i].to_clb_pin_end_index = 0;
43704365
clb_to_clb_directs[i].switch_index = 0;
43714366

4372-
tile_name = new char[directs[i].from_pin.length() + directs[i].to_pin.length()];
4373-
port_name = new char[directs[i].from_pin.length() + directs[i].to_pin.length()];
4374-
43754367
// Load from pins
43764368
// Parse out the pb_type name, port name, and pin range
4377-
parse_direct_pin_name(directs[i].from_pin.c_str(), directs[i].line, &start_pin_index, &end_pin_index, tile_name, port_name);
4369+
auto [start_pin_index, end_pin_index, tile_name, port_name] = parse_direct_pin_name(directs[i].from_pin, directs[i].line);
43784370

43794371
// Figure out which type, port, and pin is used
4380-
for (const auto& type : device_ctx.physical_tile_types) {
4381-
if (strcmp(type.name, tile_name) == 0) {
4372+
for (const t_physical_tile_type& type : device_ctx.physical_tile_types) {
4373+
if (tile_name == type.name) {
43824374
physical_tile = &type;
43834375
break;
43844376
}
43854377
}
43864378

43874379
if (physical_tile == nullptr) {
4388-
VPR_THROW(VPR_ERROR_ARCH, "Unable to find block %s.\n", tile_name);
4380+
VPR_THROW(VPR_ERROR_ARCH, "Unable to find block %s.\n", tile_name.c_str());
43894381
}
43904382

43914383
clb_to_clb_directs[i].from_clb_type = physical_tile;
@@ -4405,18 +4397,18 @@ static t_clb_to_clb_directs* alloc_and_load_clb_to_clb_directs(const std::vector
44054397

44064398
// Load to pins
44074399
// Parse out the pb_type name, port name, and pin range
4408-
parse_direct_pin_name(directs[i].to_pin.c_str(), directs[i].line, &start_pin_index, &end_pin_index, tile_name, port_name);
4400+
std::tie(start_pin_index, end_pin_index, tile_name, port_name) = parse_direct_pin_name(directs[i].to_pin, directs[i].line);
44094401

44104402
// Figure out which type, port, and pin is used
4411-
for (const auto& type : device_ctx.physical_tile_types) {
4412-
if (strcmp(type.name, tile_name) == 0) {
4403+
for (const t_physical_tile_type& type : device_ctx.physical_tile_types) {
4404+
if (tile_name == type.name ) {
44134405
physical_tile = &type;
44144406
break;
44154407
}
44164408
}
44174409

44184410
if (physical_tile == nullptr) {
4419-
VPR_THROW(VPR_ERROR_ARCH, "Unable to find block %s.\n", tile_name);
4411+
VPR_THROW(VPR_ERROR_ARCH, "Unable to find block %s.\n", tile_name.c_str());
44204412
}
44214413

44224414
clb_to_clb_directs[i].to_clb_type = physical_tile;
@@ -4447,8 +4439,6 @@ static t_clb_to_clb_directs* alloc_and_load_clb_to_clb_directs(const std::vector
44474439
//Use the delayless switch by default
44484440
clb_to_clb_directs[i].switch_index = delayless_switch;
44494441
}
4450-
delete[] tile_name;
4451-
delete[] port_name;
44524442
}
44534443

44544444
return clb_to_clb_directs;

vpr/src/util/vpr_utils.cpp

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,39 +1602,29 @@ void free_pb_stats(t_pb* pb) {
16021602
* load placement macros in place_macro.c *
16031603
* *
16041604
***************************************************************************************/
1605-
1606-
void parse_direct_pin_name(const char* src_string, int line, int* start_pin_index, int* end_pin_index, char* pb_type_name, char* port_name) {
1607-
/* Parses out the pb_type_name and port_name from the direct passed in. *
1608-
* If the start_pin_index and end_pin_index is specified, parse them too. *
1609-
* Return the values parsed by reference. */
1610-
1611-
char source_string[MAX_STRING_LEN + 1];
1612-
int ichar, match_count;
1605+
std::tuple<int, int, std::string, std::string> parse_direct_pin_name(std::string_view src_string, int line) {
16131606

16141607
if (vtr::split(src_string).size() > 1) {
16151608
VPR_THROW(VPR_ERROR_ARCH,
16161609
"Only a single port pin range specification allowed for direct connect (was: '%s')", src_string);
16171610
}
16181611

16191612
// parse out the pb_type and port name, possibly pin_indices
1620-
const char* find_format = strstr(src_string, "[");
1621-
if (find_format == nullptr) {
1613+
if (src_string.find('[') == std::string_view::npos) {
16221614
/* Format "pb_type_name.port_name" */
1623-
*start_pin_index = *end_pin_index = -1;
1615+
const int start_pin_index = -1;
1616+
const int end_pin_index = -1;
16241617

1625-
if (strlen(src_string) + 1 <= MAX_STRING_LEN + 1) {
1626-
strcpy(source_string, src_string);
1627-
} else {
1628-
VPR_FATAL_ERROR(VPR_ERROR_ARCH,
1629-
"Pin name exceeded buffer size of %zu characters", MAX_STRING_LEN + 1);
1630-
}
1631-
for (ichar = 0; ichar < (int)(strlen(source_string)); ichar++) {
1632-
if (source_string[ichar] == '.')
1633-
source_string[ichar] = ' ';
1634-
}
1618+
std::string source_string{src_string};
1619+
// replace '.' characters with space
1620+
std::replace(source_string.begin(), source_string.end(), '.', ' ');
1621+
1622+
std::istringstream source_iss(source_string);
1623+
std::string pb_type_name, port_name;
16351624

1636-
match_count = sscanf(source_string, "%s %s", pb_type_name, port_name);
1637-
if (match_count != 2) {
1625+
if (source_iss >> pb_type_name >> port_name) {
1626+
return {start_pin_index, end_pin_index, pb_type_name, port_name};
1627+
} else {
16381628
VTR_LOG_ERROR(
16391629
"[LINE %d] Invalid pin - %s, name should be in the format "
16401630
"\"pb_type_name\".\"port_name\" or \"pb_type_name\".\"port_name[end_pin_index:start_pin_index]\". "
@@ -1644,41 +1634,44 @@ void parse_direct_pin_name(const char* src_string, int line, int* start_pin_inde
16441634
}
16451635
} else {
16461636
/* Format "pb_type_name.port_name[end_pin_index:start_pin_index]" */
1647-
strcpy(source_string, src_string);
1648-
for (ichar = 0; ichar < (int)(strlen(source_string)); ichar++) {
1649-
//Need white space between the components when using %s with
1650-
//sscanf
1651-
if (source_string[ichar] == '.')
1652-
source_string[ichar] = ' ';
1653-
if (source_string[ichar] == '[')
1654-
source_string[ichar] = ' ';
1655-
}
1656-
1657-
match_count = sscanf(source_string, "%s %s %d:%d]",
1658-
pb_type_name, port_name,
1659-
end_pin_index, start_pin_index);
1660-
if (match_count != 4) {
1637+
std::string source_string{src_string};
1638+
1639+
// Replace '.' and '[' characters with ' '
1640+
std::replace_if(source_string.begin(), source_string.end(),
1641+
[](char c) { return c == '.' || c == '['; },
1642+
' ');
1643+
1644+
std::istringstream source_iss(source_string);
1645+
int start_pin_index, end_pin_index;
1646+
std::string pb_type_name, port_name;
1647+
1648+
if (source_iss >> pb_type_name >> port_name >> end_pin_index >> start_pin_index) {
1649+
1650+
if (end_pin_index < 0 || start_pin_index < 0) {
1651+
VTR_LOG_ERROR(
1652+
"[LINE %d] Invalid pin - %s, the pin_index in "
1653+
"[end_pin_index:start_pin_index] should not be a negative value.\n",
1654+
line, src_string);
1655+
exit(1);
1656+
}
1657+
1658+
if (end_pin_index < start_pin_index) {
1659+
VTR_LOG_ERROR(
1660+
"[LINE %d] Invalid from_pin - %s, the end_pin_index in "
1661+
"[end_pin_index:start_pin_index] should not be less than start_pin_index.\n",
1662+
line, src_string);
1663+
exit(1);
1664+
}
1665+
1666+
return {start_pin_index, end_pin_index, pb_type_name, port_name};
1667+
} else {
16611668
VTR_LOG_ERROR(
16621669
"[LINE %d] Invalid pin - %s, name should be in the format "
16631670
"\"pb_type_name\".\"port_name\" or \"pb_type_name\".\"port_name[end_pin_index:start_pin_index]\". "
16641671
"The end_pin_index and start_pin_index can be the same.\n",
16651672
line, src_string);
16661673
exit(1);
16671674
}
1668-
if (*end_pin_index < 0 || *start_pin_index < 0) {
1669-
VTR_LOG_ERROR(
1670-
"[LINE %d] Invalid pin - %s, the pin_index in "
1671-
"[end_pin_index:start_pin_index] should not be a negative value.\n",
1672-
line, src_string);
1673-
exit(1);
1674-
}
1675-
if (*end_pin_index < *start_pin_index) {
1676-
VTR_LOG_ERROR(
1677-
"[LINE %d] Invalid from_pin - %s, the end_pin_index in "
1678-
"[end_pin_index:start_pin_index] should not be less than start_pin_index.\n",
1679-
line, src_string);
1680-
exit(1);
1681-
}
16821675
}
16831676
}
16841677

@@ -1824,10 +1817,10 @@ int get_atom_pin_class_num(const AtomPinId atom_pin_id) {
18241817
return get_class_num_from_pin_physical_num(physical_type, pin_physical_num);
18251818
}
18261819

1827-
t_physical_tile_port find_tile_port_by_name(t_physical_tile_type_ptr type, const char* port_name) {
1828-
for (const auto& sub_tile : type->sub_tiles) {
1829-
for (const auto& port : sub_tile.ports) {
1830-
if (0 == strcmp(port.name, port_name)) {
1820+
t_physical_tile_port find_tile_port_by_name(t_physical_tile_type_ptr type, std::string_view port_name) {
1821+
for (const t_sub_tile& sub_tile : type->sub_tiles) {
1822+
for (const t_physical_tile_port& port : sub_tile.ports) {
1823+
if (port_name == port.name) {
18311824
return port;
18321825
}
18331826
}

vpr/src/util/vpr_utils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ t_pin_range get_pb_pins(t_physical_tile_type_ptr physical_type,
216216
float compute_primitive_base_cost(const t_pb_graph_node* primitive);
217217
int num_ext_inputs_atom_block(AtomBlockId blk_id);
218218

219-
void parse_direct_pin_name(const char* src_string, int line, int* start_pin_index, int* end_pin_index, char* pb_type_name, char* port_name);
219+
/**
220+
* @brief Parses out the pb_type_name and port_name from the direct passed in.
221+
* If the start_pin_index and end_pin_index is specified, parse them too. *
222+
* @return (start_pin_index, end_pin_index, pb_type_name, port_name)
223+
*/
224+
std::tuple<int, int, std::string, std::string> parse_direct_pin_name(std::string_view src_string, int line);
220225

221226
void free_pb_stats(t_pb* pb);
222227
void free_pb(t_pb* pb);

0 commit comments

Comments
 (0)