Skip to content

Commit 06d63af

Browse files
authored
Minor perf improvements related to number parsing (#266)
* Minor perf improvements related to number parsing Signed-off-by: Sonal Santan <sonal.santan@amd.com> * Move the parse_num_arg() handler table to the class itself since one such instance is needed per class object Signed-off-by: Sonal Santan <sonal.santan@amd.com> * Use std::vector's built-in resize method to grow and fill scratchpad Signed-off-by: Sonal Santan <sonal.santan@amd.com> * Resolve a type conversion warning Signed-off-by: Sonal Santan <sonal.santan@amd.com> --------- Signed-off-by: Sonal Santan <sonal.santan@amd.com>
1 parent 3d3c790 commit 06d63af

4 files changed

Lines changed: 51 additions & 42 deletions

File tree

src/cpp/common/assembler_state.cpp

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
#include "utils.h"
66

7+
#include "regex_wrapper.h"
78
#include "aiebu/aiebu_error.h"
89

9-
#include <functional>
10-
#include <unordered_map>
11-
1210
namespace aiebu {
1311

1412
assembler_state::
@@ -161,54 +159,34 @@ process(bool makeunique)
161159
* 3. If string is hex number string (start with "0x"): it return decimal equavalent
162160
* 4. If string is numeric string: it will convert to decimal
163161
*/
164-
uint32_t assembler_state::parse_num_arg(const std::string& str) {
165-
const std::unordered_map<std::string, std::function<uint32_t(const std::string&)>> handlers = {
166-
{"@", [this](const std::string& s) -> uint32_t {
167-
//If string start with '@': it can be either pad name or label name
168-
auto key = s.substr(1);
169-
if (m_scratchpad.find(key) != m_scratchpad.end())
170-
return m_scratchpad[key]->get_base() + m_scratchpad[key]->get_offset();
171-
if (m_labelmap.find(key) != m_labelmap.end())
172-
return m_labelmap[key]->get_pos();
173-
throw error(error::error_code::invalid_asm, "Label " + key + " not present in label map\n");
174-
}},
175-
{"s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
176-
{"mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
177-
{"mem_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
178-
{"mem_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
179-
{"shim_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
180-
{"shim_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
181-
{"tile_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
182-
{"tile_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
183-
{"shim_ctrl_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }}
184-
};
185-
162+
uint32_t assembler_state::parse_num_arg(const std::string &str) const {
186163
// check if its pad/label/actor
187164
for (const auto& [prefix, handler] : handlers) {
188165
if (str.rfind(prefix) == 0) {
189166
return handler(str);
190167
}
191168
}
192169

193-
if (str.rfind("tile_") == 0)
170+
static const regex kTileRe{R"(^tile_(\d+)_(\d+)$)"};
171+
smatch m;
172+
if ((str[0] == 't') && regex_match(str, m, kTileRe) && (m.size() == 3))
194173
{
195-
// Parse and return perticular col and row 32bit base address eg: tile_0_1
196-
constexpr static size_t col_start = 5;
197-
constexpr static size_t len_of_underscore = 1;
198174
constexpr static size_t row_mask = 0x1F;
199175
constexpr static size_t col_mask = 0x7F;
200176
constexpr static size_t col_shift = 5;
201-
size_t row_start = col_start + len_of_underscore + str.substr(col_start).rfind("_");
202-
uint32_t col = std::stoi(str.substr(col_start));
203-
uint32_t row = std::stoi(str.substr(row_start));
177+
const uint32_t col = std::stoi(m[1]);
178+
const uint32_t row = std::stoi(m[2]);
204179
return (((col & col_mask) << col_shift) | (row & row_mask));
205-
} else if (str.rfind("0x") == 0)
180+
}
181+
else if (str.rfind("0x") == 0)
206182
{ //parse hex string
207-
return std::stoul(str.substr(2), nullptr , HEX_BASE);
208-
} else if (is_number(str))
183+
return std::stoul(str, nullptr , HEX_BASE);
184+
}
185+
else if (is_number(str))
209186
{ //parse numeric string
210187
return std::stoul(str);
211-
} else {
188+
}
189+
else {
212190
throw symbol_exception();
213191
}
214192
}

src/cpp/common/assembler_state.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <memory>
1616
#include <map>
1717
#include <set>
18+
#include <functional>
19+
#include <unordered_map>
1820

1921
namespace aiebu {
2022

@@ -145,6 +147,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
145147
assembler_state(const assembler_state& rhs) = default;
146148
assembler_state& operator=(const assembler_state& rhs) = delete;
147149
assembler_state(assembler_state &&s) = default;
150+
148151
public:
149152
std::shared_ptr<std::map<std::string, std::shared_ptr<isa_op>>> m_isa;
150153
std::vector<std::shared_ptr<asm_data>>& m_data;
@@ -182,7 +185,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
182185
return (m_scratchpad.find(label) != m_scratchpad.end());
183186
}
184187

185-
uint32_t parse_num_arg(const std::string& str);
188+
uint32_t parse_num_arg(const std::string& str) const;
186189

187190
void process(bool makeunique);
188191

@@ -246,6 +249,29 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
246249
virtual symbol::patch_schema get_shim_dma_patching() const = 0;
247250
virtual symbol::patch_schema get_control_packet_patching() const = 0;
248251
virtual ~assembler_state() = default;
252+
253+
private:
254+
const std::unordered_map<std::string, std::function<uint32_t(const std::string&)>> handlers = {
255+
{"@", [this](const std::string& s) -> uint32_t {
256+
//If string start with '@': it can be either pad name or label name
257+
auto key = s.substr(1);
258+
if (m_scratchpad.find(key) != m_scratchpad.end())
259+
return m_scratchpad[key]->get_base() + m_scratchpad[key]->get_offset();
260+
if (m_labelmap.find(key) != m_labelmap.end())
261+
return m_labelmap[key]->get_pos();
262+
throw error(error::error_code::invalid_asm, "Label " + key + " not present in label map\n");
263+
}},
264+
{"s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
265+
{"mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
266+
{"mem_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
267+
{"mem_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
268+
{"shim_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
269+
{"shim_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
270+
{"tile_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
271+
{"tile_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
272+
{"shim_ctrl_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }}
273+
};
274+
249275
};
250276

251277

src/cpp/common/writer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class section_writer: public writer
5656
m_data.insert(m_data.end(), bytes.begin(), bytes.end());
5757
}
5858

59+
void
60+
write_default_bytes(unsigned int count)
61+
{
62+
m_data.resize(m_data.size() + count, 0x0);
63+
}
64+
5965
virtual void reserve(size_t capacity);
6066

6167
virtual uint32_t read_word(offset_type offset) const;

src/cpp/encoder/aie2ps/aie2ps_encoder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ fill_scratchpad(std::shared_ptr<section_writer> padwriter, const std::map<std::s
2424
padwriter->write_bytes(content);
2525
} else {
2626
auto size = pad.second->get_size();
27-
std::vector<uint8_t> zeros(size, 0x00);
28-
padwriter->write_bytes(zeros);
27+
padwriter->write_default_bytes(size);
2928
}
3029
}
3130
}
@@ -75,14 +74,14 @@ process(std::shared_ptr<preprocessed_output> input)
7574
if (coldata.second->m_scratchpad.size()) {
7675
auto padwriter = std::make_shared<section_writer>(get_PadSectionName(colnum), code_section::data);
7776
fill_scratchpad(padwriter, coldata.second->m_scratchpad);
78-
twriter.push_back(padwriter);
77+
twriter.push_back(padwriter);
7978
}
8079

8180
for (const auto& pair : ctrlpkt_id_map) {
8281
auto ctrlpktwriter = std::make_shared<section_writer>(pair.second, code_section::data);
8382
fill_controlpkt(ctrlpktwriter, ctrlpkt[pair.second]);
8483
fill_control_packet_symbols(ctrlpktwriter, totalsyms);
85-
twriter.push_back(ctrlpktwriter);
84+
twriter.push_back(ctrlpktwriter);
8685
}
8786
}
8887
// Report (only if log level is info or higher)
@@ -225,7 +224,7 @@ page_writer(page& lpage, std::map<std::string, std::shared_ptr<scratchpad_info>>
225224
std::vector<uint8_t> ret = (*m_isa)[name]->serializer(args)
226225
->serialize(page_state, dsym, colnum, pagenum);
227226
datawriter->write_bytes(ret);
228-
} else
227+
} else
229228
throw error(error::error_code::internal_error, "Invalid operation: " + name + " in DATA section !!!");
230229
}
231230

0 commit comments

Comments
 (0)