Skip to content

Commit 39cb3fa

Browse files
remove preemption scratchpad from .pad
Signed-off-by: Himanshu Choudhary <Himanshu.Choudhary@amd.com>
1 parent 299eac8 commit 39cb3fa

9 files changed

Lines changed: 61 additions & 37 deletions

File tree

src/cpp/common/assembler_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
115115
std::vector<std::string> m_labellist;
116116
std::map<std::string, std::vector<std::string>> m_dependent_labelmap;
117117
std::set<std::string> m_opt_opcodes;
118+
bool m_is_save_restore_op = false; // True when currently serializing a save/restore op
118119
inline std::string gen_label_name(bool makeunique, const std::shared_ptr<asm_data> data)
119120
{
120121
return makeunique ? data->get_qualify_op_name() : data->get_operation().get_name();
@@ -163,6 +164,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
163164
bool m_merged_ctrltext_elf = false;
164165

165166
HEADER_ACCESS_GET_SET(offset_type, pos);
167+
HEADER_ACCESS_GET_SET(bool, is_save_restore_op);
166168

167169
void printstate() const;
168170

src/cpp/encoder/aie2ps/aie2ps_encoder.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,6 @@
1212

1313
namespace aiebu {
1414

15-
void
16-
aie2ps_encoder::
17-
fill_scratchpad(std::shared_ptr<section_writer> padwriter, const std::map<std::string, std::shared_ptr<scratchpad_info>>& scratchpads)
18-
{
19-
for (const auto& pad : scratchpads)
20-
{
21-
const auto& content = pad.second->get_content();
22-
if (content.size())
23-
{
24-
assert((void("Pad content size and size doesnt match\n"), content.size() == pad.second->get_size()));
25-
padwriter->write_bytes(content);
26-
} else {
27-
auto size = pad.second->get_size();
28-
padwriter->write_default_bytes(size);
29-
}
30-
}
31-
}
32-
3315
void
3416
aie2ps_encoder::
3517
fill_controlpkt(std::shared_ptr<section_writer> ctrlpktwriter, const std::vector<char>& ctrlpkt)
@@ -107,12 +89,6 @@ process(std::shared_ptr<preprocessed_output> input)
10789
optimizatiom_level, nullptr, nullptr);
10890
}
10991

110-
if (coldata.second->m_scratchpad.size()) {
111-
auto padwriter = std::make_shared<section_writer>(get_PadSectionName(colnum), code_section::data);
112-
fill_scratchpad(padwriter, coldata.second->m_scratchpad);
113-
twriter.push_back(padwriter);
114-
}
115-
11692
for (const auto& pair : ctrlpkt_id_map) {
11793
auto ctrlpktwriter = std::make_shared<section_writer>(pair.second, code_section::data);
11894
fill_controlpkt(ctrlpktwriter, ctrlpkt[pair.second]);
@@ -244,6 +220,7 @@ page_writer(page& lpage, std::map<std::string, std::shared_ptr<scratchpad_info>>
244220
if (text->isOpcode())
245221
{
246222
page_state->set_pos(textwriter->tell() - text_base);
223+
page_state->set_is_save_restore_op(text->get_is_save_restore()); // Track if this is save/restore op
247224
std::vector<uint8_t> ret = (*m_isa)[name]->serializer(args)
248225
->serialize(page_state, tsym, colnum, pagenum);
249226
textwriter->write_bytes(ret);
@@ -324,6 +301,12 @@ patch57(const std::shared_ptr<section_writer> textwriter, std::shared_ptr<sectio
324301
uint64_t bd2 = datawriter->read_word(offset + 2*4); // NOLINT
325302
uint64_t bd8 = datawriter->read_word(offset + 8*4); // NOLINT
326303
uint64_t arg = ((bd8 & 0x1FF) << 48) + ((bd2 & 0xFFFF) << 32) + (bd1 & 0xFFFFFFFF); // NOLINT
304+
// Add log for debugging patching
305+
log_info() << "aie2ps_encoder::patch57: offset=" << offset
306+
<< ", patch=0x" << std::hex << patch
307+
<< ", arg=0x" << std::hex << arg
308+
<< ", after patch=0x" << std::hex << patch + arg
309+
<< std::dec << std::endl;
327310
patch = arg + patch;
328311
datawriter->write_word_at(offset + 1*4, patch & 0xFFFFFFFF); // NOLINT
329312
datawriter->write_word_at(offset + 2*4, ((patch >> 32) & 0xFFFF) | (bd2 & 0xFFFF0000)); // NOLINT

src/cpp/encoder/aie4/aie4_encoder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "writer.h"
1111
#include "aie2ps_preprocessed_output.h"
1212
#include "ops.h"
13+
#include "common/logger.h"
1314
#include "specification/aie2ps/isa.h"
1415

1516
namespace aiebu {
@@ -40,6 +41,13 @@ class aie4_encoder : public aie2ps_encoder
4041
uint64_t bd0 = datawriter->read_word(offset);
4142
uint64_t bd1 = datawriter->read_word(offset + 1*4); // NOLINT
4243
uint64_t arg = (bd1 & 0xFFFFFFFF) + ((bd0 & 0x1FFFFFF) << 32); // NOLINT
44+
45+
// Add log for debugging patching
46+
log_info() << "aie4_encoder::patch57: offset=" << offset
47+
<< ", patch=0x" << std::hex << patch
48+
<< ", arg=0x" << std::hex << arg
49+
<< ", after patch=0x" << std::hex << patch + arg
50+
<< std::dec << std::endl;
4351
patch = arg + patch;
4452
datawriter->write_word_at(offset + 1*4, patch & 0xFFFFFFFF); // NOLINT
4553
datawriter->write_word_at(offset, (((patch >> 32) & 0x1FFFFFF) | (bd0 & 0xFE000000))); // NOLINT

src/cpp/ops/ops.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ serialize(std::shared_ptr<assembler_state> state, std::vector<symbol>& symbols,
107107
{
108108
if (state->m_ctrlpkt_id_map.find(val) != state->m_ctrlpkt_id_map.end())
109109
sval = state->m_ctrlpkt_id_map[val];
110+
else if (val == offset_type_marker && state->get_is_save_restore_op())
111+
sval = "scratch-pad-mem"; // For save/restore routine, use "scratch-pad-mem" as arg name
110112
else if (val == offset_type_marker)
111113
sval = "control-code-" + std::to_string(colnum);
112114

src/cpp/preprocessor/aie2ps/aie2ps_preprocessor.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ class aie2ps_preprocessor: public preprocessor
130130
toutput->set_ctrlpkt_id_map(ctrlpkt_id_map);
131131
toutput->set_annotations(parser->get_annotations());
132132

133+
offset_type preemption_scratchpad = 0;
133134
for (auto col: collist)
134135
{
135136
std::vector<page> pages;
136137
uint32_t relative_page_index = 0;
137-
int pad_size = 0;
138138
auto& label_page_index = parser->getcollabelpageindex(col);
139139
auto& scratchpad = parser->getcolscratchpad(col);
140140
auto& coldata = parser->get_col_asmdata(col);
@@ -153,10 +153,8 @@ class aie2ps_preprocessor: public preprocessor
153153

154154
for (auto& pad : scratchpad)
155155
{
156-
pad_size = (((pad_size + 3) >> 2) << 2); // round off to next multiple of 4
157-
pad.second->set_offset(pad_size);
158-
pad.second->set_base(PAGE_SIZE * relative_page_index);
159-
pad_size += pad.second->get_size();
156+
pad.second->set_offset(preemption_scratchpad);
157+
preemption_scratchpad += pad.second->get_size();
160158
}
161159

162160
toutput->set_coldata(col, pages, scratchpad, label_page_index, tinput->get_control_packet_index());

src/cpp/preprocessor/asm/asm_parser.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ std::vector<uint32_t>
173173
asm_parser::
174174
get_col_list()
175175
{
176-
// get col list
176+
// get col list (sorted order since unordered_map doesn't preserve insertion order)
177177
std::vector<uint32_t> keys;
178178

179179
std::transform(
180180
m_col.begin(),
181181
m_col.end(),
182182
std::back_inserter(keys),
183183
[](const std::unordered_map<uint32_t, col_data>::value_type &pair){return pair.first;});
184+
std::sort(keys.begin(), keys.end());
184185
return keys;
185186
}
186187

@@ -303,7 +304,8 @@ parse_lines(const std::vector<char>& data, std::string& file)
303304
} else
304305
insert_col_asmdata(std::make_shared<asm_data>(operation(sm[1].str(), ""), operation_type::label,
305306
code_section::unknown, 0, (uint32_t)-1, linenumber,
306-
parse_file_idx));
307+
parse_file_idx, is_save_restore_routine()));
308+
307309
continue;
308310
}
309311
// check for operation
@@ -388,9 +390,10 @@ parse_lines(const std::vector<char>& data, std::string& file)
388390

389391
line = op_name + "\t" + arg_str;
390392
}
393+
391394
insert_col_asmdata(std::make_shared<asm_data>(operation(op_name, arg_str), operation_type::op,
392395
code_section::unknown, 0, (uint32_t)-1, linenumber,
393-
parse_file_idx));
396+
parse_file_idx, is_save_restore_routine()));
394397
if (!op_name.compare("eof"))
395398
set_data_state(true);
396399
}
@@ -1073,12 +1076,14 @@ asm_parser::inject_hintmap_save_restore(int col,
10731076

10741077
m_current_col = col;
10751078
set_data_state(false);
1079+
set_save_restore_routine(true); // Mark as save/restore routine
10761080
parse_lines(save_chars, save_file_mod);
10771081
pop_data_state();
10781082

10791083
set_data_state(false);
10801084
parse_lines(restore_chars, restore_file_mod);
10811085
pop_data_state();
1086+
set_save_restore_routine(false); // Clear save/restore routine flag
10821087
}
10831088

10841089
// ---------------------------------------------------------------------------
@@ -1202,12 +1207,14 @@ asm_parser::inject_default_save_restore(int col,
12021207

12031208
m_current_col = col;
12041209
set_data_state(false);
1210+
set_save_restore_routine(true); // Mark as save/restore routine
12051211
parse_lines(save_chars, save_file_mod);
12061212
pop_data_state();
12071213

12081214
set_data_state(false);
12091215
parse_lines(restore_chars, restore_file_mod);
12101216
pop_data_state();
1217+
set_save_restore_routine(false); // Clear save/restore routine flag
12111218
}
12121219

12131220
// ---------------------------------------------------------------------------
@@ -1468,14 +1475,20 @@ operate(std::shared_ptr<asm_parser> parserptr,
14681475
void
14691476
pad_directive::
14701477
operate(std::shared_ptr<asm_parser> parserptr,
1471-
const std::string& /*directive_line*/,
1478+
const std::string& directive_line,
14721479
const std::string& args_tail)
14731480
{
14741481
m_parserptr = parserptr;
14751482
verify_nonempty_args(args_tail, error::error_code::invalid_asm, ".setpad directive requires arguments\n");
14761483

14771484
std::vector<std::string> args = splitoption(args_tail.c_str(), ',');
14781485

1486+
// .setpad should only be part of save/restore routines
1487+
if (!m_parserptr->should_skip_setpad_in_save_restore()) {
1488+
log_warn() << "Warning: Directive \"" << directive_line << "\" found outside save/restore routine for target: "
1489+
<< m_parserptr->get_target_type() << "\n";
1490+
}
1491+
14791492
add_scratchpad(args[0], args[1]);
14801493
}
14811494

src/cpp/preprocessor/asm/asm_parser.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ class asm_data
269269
// m_file replaced with a 32-bit index into a thread_local intern table.
270270
uint32_t m_file_idx;
271271
int m_annotation_index = -1;
272+
bool m_is_save_restore = false; // True if this instruction is from save/restore routine
272273

273274
public:
274275
asm_data() = default;
@@ -282,17 +283,18 @@ class asm_data
282283
*/
283284
asm_data(operation op, operation_type optype,
284285
code_section sec, offset_type size, uint32_t pgnum,
285-
uint32_t ln, uint32_t file_idx)
286+
uint32_t ln, uint32_t file_idx, bool is_save_restore = false)
286287
:m_op(std::move(op)), m_optype(optype), m_section(sec), m_size(size),
287288
m_pagenum(pgnum), m_linenumber(ln),
288-
m_file_idx(file_idx) {}
289+
m_file_idx(file_idx), m_is_save_restore(is_save_restore) {}
289290

290291
// Rule of Zero: implicit copy/move keeps insert_col_asmdata / vector growth cheap.
291292

292293
HEADER_ACCESS_GET_SET(code_section, section);
293294
HEADER_ACCESS_GET_SET(offset_type, size);
294295
HEADER_ACCESS_GET_SET(pageid_type, pagenum);
295296
HEADER_ACCESS_GET_SET(uint32_t, linenumber);
297+
296298
const std::string& get_file() const { return detail::lookup_filename(m_file_idx); }
297299
uint32_t get_file_idx() const { return m_file_idx; }
298300
// Qualify the operation's own name as a label-map key (e.g. "0:start_job").
@@ -322,6 +324,7 @@ class asm_data
322324
return n + ' ' + a;
323325
}
324326

327+
HEADER_ACCESS_GET_SET(bool, is_save_restore);
325328
bool isLabel() const { return m_optype == operation_type::label; }
326329
bool isOpcode() const { return m_optype == operation_type::op; }
327330
bool isAnnotation() const { return m_optype == operation_type::annotation; }
@@ -478,6 +481,7 @@ class asm_parser: public std::enable_shared_from_this<asm_parser>
478481
std::map<int, std::vector<std::string>> m_preempt_hintmaps; // group -> vector of hintmap_labels (multiple PREEMPT opcodes per group)
479482
std::map<std::string, std::pair<std::string, std::string>> m_hintmap_labels; // hintmap_label -> (save_label, restore_label)
480483
std::set<int> m_preempt_without_hintmap; // groups that have PREEMPT opcodes without hintmaps
484+
bool m_is_save_restore_routine = false; // True when parsing save/restore routine files
481485

482486
// One unique scratchpad region: all hintmap labels that share the same scratchbase+size
483487
struct hintmap_group_entry {
@@ -557,6 +561,20 @@ class asm_parser: public std::enable_shared_from_this<asm_parser>
557561

558562
bool is_multi_column_mode() const { return m_preempt_labels.size() > 1; }
559563

564+
// Check if currently parsing save/restore routine
565+
bool is_save_restore_routine() const { return m_is_save_restore_routine; }
566+
void set_save_restore_routine(bool val) { m_is_save_restore_routine = val; }
567+
568+
// Check if we should skip setpad for this target in save/restore routine
569+
bool should_skip_setpad_in_save_restore() const {
570+
return m_is_save_restore_routine;
571+
}
572+
573+
// Check if we should use scratch-pad section for save/restore APPLY_OFFSET_57
574+
//bool should_use_scratchpad_section_for_save_restore() const {
575+
// return m_is_save_restore_routine;
576+
//}
577+
560578
// Record preempt label for current group (called when PREEMPT opcode is hit)
561579
// Label naming: save_N / restore_N where N = index (group/2 + 1)
562580
// group 0 -> save_1, group 2 -> save_2, group 4 -> save_3
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58f4172515858e1c790a38aab7bfa6b9
1+
36b5e14897a92d5f0c2c1ae965ffc8e7
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7f27c182bdcad298d09f563cea160922
1+
71abde9f51feb4706acbe8f56ede9787

0 commit comments

Comments
 (0)