Skip to content

Commit 1880d26

Browse files
remove preemption scratchpad from .pad
Signed-off-by: Himanshu Choudhary <Himanshu.Choudhary@amd.com>
1 parent 321a83f commit 1880d26

8 files changed

Lines changed: 66 additions & 36 deletions

File tree

src/cpp/common/assembler_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
113113
std::vector<std::string> m_labellist;
114114
std::map<std::string, std::vector<std::string>> m_dependent_labelmap;
115115
std::set<std::string> m_opt_opcodes;
116+
bool m_is_save_restore_op = false; // True when currently serializing a save/restore op
116117
inline std::string gen_label_name(bool makeunique, const std::shared_ptr<asm_data> data)
117118
{
118119
return makeunique ? data->get_file() + ":" + data->get_operation()->get_name() : data->get_operation()->get_name();
@@ -159,6 +160,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
159160
std::map<uint32_t, std::string>& m_ctrlpkt_id_map;
160161

161162
HEADER_ACCESS_GET_SET(offset_type, pos);
163+
HEADER_ACCESS_GET_SET(bool, is_save_restore_op);
162164

163165
void printstate() const;
164166

src/cpp/encoder/aie2ps/aie2ps_encoder.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,6 @@
1010

1111
namespace aiebu {
1212

13-
void
14-
aie2ps_encoder::
15-
fill_scratchpad(std::shared_ptr<section_writer> padwriter, const std::map<std::string, std::shared_ptr<scratchpad_info>>& scratchpads)
16-
{
17-
for (const auto& pad : scratchpads)
18-
{
19-
const auto& content = pad.second->get_content();
20-
if (content.size())
21-
{
22-
assert((void("Pad content size and size doesnt match\n"), content.size() == pad.second->get_size()));
23-
padwriter->write_bytes(content);
24-
} else {
25-
auto size = pad.second->get_size();
26-
std::vector<uint8_t> zeros(size, 0x00);
27-
padwriter->write_bytes(zeros);
28-
}
29-
}
30-
}
3113

3214
void
3315
aie2ps_encoder::
@@ -66,16 +48,10 @@ process(std::shared_ptr<preprocessed_output> input)
6648

6749
// for each colnum encode each page
6850
for (const auto& coldata: totalcoldata) {
69-
auto colnum = coldata.first;
7051
for (auto& lpage : coldata.second->m_pages)
7152
page_writer(lpage, coldata.second->m_scratchpad, coldata.second->m_labelpageindex,
7253
ctrlpkt_id_map, optimizatiom_level);
7354

74-
if (coldata.second->m_scratchpad.size()) {
75-
auto padwriter = std::make_shared<section_writer>(get_PadSectionName(colnum), code_section::data);
76-
fill_scratchpad(padwriter, coldata.second->m_scratchpad);
77-
twriter.push_back(padwriter);
78-
}
7955

8056
for (const auto& pair : ctrlpkt_id_map) {
8157
auto ctrlpktwriter = std::make_shared<section_writer>(pair.second, code_section::data);
@@ -180,6 +156,7 @@ page_writer(page& lpage, std::map<std::string, std::shared_ptr<scratchpad_info>>
180156
if (text->isOpcode())
181157
{
182158
page_state->set_pos(textwriter->tell() - offset);
159+
page_state->set_is_save_restore_op(text->get_is_save_restore()); // Track if this is save/restore op
183160
std::vector<uint8_t> ret = (*m_isa)[name]->serializer(text->get_operation()->get_args())
184161
->serialize(page_state, tsym, colnum, pagenum);
185162
textwriter->write_bytes(ret);
@@ -218,6 +195,15 @@ page_writer(page& lpage, std::map<std::string, std::shared_ptr<scratchpad_info>>
218195
for (auto& arg : spad.second)
219196
{
220197
offset = page_state->parse_num_arg(arg);
198+
// Log patch information
199+
log_info() << "Patching scratchpad: label=" << spad.first
200+
<< ", arg=" << arg
201+
<< ", offset=" << offset
202+
<< ", base=" << page_state->m_scratchpad[spad.first.substr(1)]->get_base()
203+
<< ", offset=" << page_state->m_scratchpad[spad.first.substr(1)]->get_offset()
204+
<< ", patch=" << (page_state->m_scratchpad[spad.first.substr(1)]->get_base() +
205+
page_state->m_scratchpad[spad.first.substr(1)]->get_offset())
206+
<< std::endl;
221207
patch57(textwriter, datawriter, offset + static_cast<offset_type>(page_header.size()),
222208
page_state->m_scratchpad[spad.first.substr(1)]->get_base() + page_state->m_scratchpad[spad.first.substr(1)]->get_offset());
223209
}
@@ -243,6 +229,12 @@ patch57(const std::shared_ptr<section_writer> textwriter, std::shared_ptr<sectio
243229
uint64_t bd2 = datawriter->read_word(offset + 2*4); // NOLINT
244230
uint64_t bd8 = datawriter->read_word(offset + 8*4); // NOLINT
245231
uint64_t arg = ((bd8 & 0x1FF) << 48) + ((bd2 & 0xFFFF) << 32) + (bd1 & 0xFFFFFFFF); // NOLINT
232+
// Add log for debugging patching
233+
log_info() << "aie2ps_encoder::patch57: offset=" << offset
234+
<< ", patch=0x" << std::hex << patch
235+
<< ", arg=0x" << std::hex << arg
236+
<< ", after patch=0x" << std::hex << patch + arg
237+
<< std::dec << std::endl;
246238
patch = arg + patch;
247239
datawriter->write_word_at(offset + 1*4, patch & 0xFFFFFFFF); // NOLINT
248240
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 {
@@ -38,6 +39,13 @@ class aie4_encoder : public aie2ps_encoder
3839
uint64_t bd0 = datawriter->read_word(offset);
3940
uint64_t bd1 = datawriter->read_word(offset + 1*4); // NOLINT
4041
uint64_t arg = (bd1 & 0xFFFFFFFF) + ((bd0 & 0x1FFFFFF) << 32); // NOLINT
42+
43+
// Add log for debugging patching
44+
log_info() << "aie4_encoder::patch57: offset=" << offset
45+
<< ", patch=0x" << std::hex << patch
46+
<< ", arg=0x" << std::hex << arg
47+
<< ", after patch=0x" << std::hex << patch + arg
48+
<< std::dec << std::endl;
4149
patch = arg + patch;
4250
datawriter->write_word_at(offset + 1*4, patch & 0xFFFFFFFF); // NOLINT
4351
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
@@ -105,6 +105,8 @@ serialize(std::shared_ptr<assembler_state> state, std::vector<symbol>& symbols,
105105
{
106106
if (state->m_ctrlpkt_id_map.find(val) != state->m_ctrlpkt_id_map.end())
107107
sval = state->m_ctrlpkt_id_map[val];
108+
else if (val == offset_type_marker && state->get_is_save_restore_op())
109+
sval = "scratch-pad-mem"; // For save/restore routine, use "scratch-pad-mem" as arg name
108110
else if (val == offset_type_marker)
109111
sval = "control-code-" + std::to_string(colnum);
110112

src/cpp/preprocessor/aie2ps/aie2ps_preprocessor.h

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

130+
offset_type preemption_scratchpad = 0;
130131
for (auto col: collist)
131132
{
132133
std::vector<page> pages;
133134
int relative_page_index = 0;
134-
int pad_size = 0;
135135
auto& label_page_index = parser->getcollabelpageindex(col);
136136
auto& scratchpad = parser->getcolscratchpad(col);
137137
auto& coldata = parser->get_col_asmdata(col);
@@ -148,10 +148,8 @@ class aie2ps_preprocessor: public preprocessor
148148

149149
for (auto& pad : scratchpad)
150150
{
151-
pad_size = (((pad_size + 3) >> 2) << 2); // round off to next multiple of 4
152-
pad.second->set_offset(pad_size);
153-
pad.second->set_base(PAGE_SIZE * relative_page_index);
154-
pad_size += pad.second->get_size();
151+
pad.second->set_offset(preemption_scratchpad);
152+
preemption_scratchpad += pad.second->get_size();
155153
}
156154

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

src/cpp/preprocessor/asm/asm_parser.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ std::vector<uint32_t>
109109
asm_parser::
110110
get_col_list()
111111
{
112-
// get col list
112+
// get col list (sorted order since unordered_map doesn't preserve insertion order)
113113
std::vector<uint32_t> keys;
114114

115115
std::transform(
116116
m_col.begin(),
117117
m_col.end(),
118118
std::back_inserter(keys),
119119
[](const std::unordered_map<uint32_t, col_data>::value_type &pair){return pair.first;});
120+
std::sort(keys.begin(), keys.end());
120121
return keys;
121122
}
122123

@@ -223,7 +224,7 @@ parse_lines(const std::vector<char>& data, std::string& file)
223224
} else
224225
insert_col_asmdata(std::make_shared<asm_data>(std::make_shared<operation>(sm[1].str(), ""),
225226
operation_type::label, code_section::unknown, 0,
226-
(uint32_t)-1, linenumber, line, file));
227+
(uint32_t)-1, linenumber, line, file, is_save_restore_routine()));
227228
continue;
228229
}
229230
// check for operation
@@ -307,7 +308,7 @@ parse_lines(const std::vector<char>& data, std::string& file)
307308

308309
insert_col_asmdata(std::make_shared<asm_data>(std::make_shared<operation>(op_name, arg_str),
309310
operation_type::op, code_section::unknown, 0, (uint32_t)-1,
310-
linenumber, line, file));
311+
linenumber, line, file, is_save_restore_routine()));
311312
if (!op_name.compare("eof"))
312313
set_data_state(true);
313314
}
@@ -991,12 +992,14 @@ asm_parser::inject_hintmap_save_restore(int col,
991992

992993
m_current_col = col;
993994
set_data_state(false);
995+
set_save_restore_routine(true); // Mark as save/restore routine
994996
parse_lines(save_chars, save_file_mod);
995997
pop_data_state();
996998

997999
set_data_state(false);
9981000
parse_lines(restore_chars, restore_file_mod);
9991001
pop_data_state();
1002+
set_save_restore_routine(false); // Clear save/restore routine flag
10001003
}
10011004

10021005
// ---------------------------------------------------------------------------
@@ -1120,12 +1123,14 @@ asm_parser::inject_default_save_restore(int col,
11201123

11211124
m_current_col = col;
11221125
set_data_state(false);
1126+
set_save_restore_routine(true); // Mark as save/restore routine
11231127
parse_lines(save_chars, save_file_mod);
11241128
pop_data_state();
11251129

11261130
set_data_state(false);
11271131
parse_lines(restore_chars, restore_file_mod);
11281132
pop_data_state();
1133+
set_save_restore_routine(false); // Clear save/restore routine flag
11291134
}
11301135

11311136
// ---------------------------------------------------------------------------
@@ -1377,8 +1382,13 @@ operate(std::shared_ptr<asm_parser> parserptr, const smatch& sm)
13771382
m_parserptr = parserptr;
13781383
verify_match(sm, error::error_code::invalid_asm, ".setpad directive requires arguments\n");
13791384

1380-
std::vector<std::string> args = splitoption(sm[2].str().c_str(), ',');
1385+
// .setpad should only be part of save/restore routines
1386+
if (!m_parserptr->should_skip_setpad_in_save_restore()) {
1387+
log_warn() << "Warning: Directive \"" << sm[0].str() << "\" found outside save/restore routine for target: "
1388+
<< m_parserptr->get_target_type() << "\n";
1389+
}
13811390

1391+
std::vector<std::string> args = splitoption(sm[2].str().c_str(), ',');
13821392
add_scratchpad(args[0], args[1]);
13831393
}
13841394

src/cpp/preprocessor/asm/asm_parser.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,15 @@ class asm_data
196196
std::string m_line;
197197
std::string m_file;
198198
int m_annotation_index = -1;
199+
bool m_is_save_restore = false; // True if this instruction is from save/restore routine
199200

200201
public:
201202
asm_data() = default;
202203
asm_data(std::shared_ptr<operation> op, operation_type optype,
203204
code_section sec, offset_type size, uint32_t pgnum,
204-
uint32_t ln, std::string line, std::string file)
205+
uint32_t ln, std::string line, std::string file, bool is_save_restore = false)
205206
:m_op(op), m_optype(optype), m_section(sec), m_size(size),
206-
m_pagenum(pgnum), m_linenumber(ln), m_line(line), m_file(file) {}
207+
m_pagenum(pgnum), m_linenumber(ln), m_line(line), m_file(file), m_is_save_restore(is_save_restore) {}
207208

208209
asm_data( asm_data* a)
209210
{
@@ -215,6 +216,7 @@ class asm_data
215216
a->m_linenumber = m_linenumber;
216217
a->m_line = m_line;
217218
a->m_file = m_file;
219+
a->m_is_save_restore = m_is_save_restore;
218220
}
219221

220222
HEADER_ACCESS_GET_SET(code_section, section);
@@ -223,6 +225,7 @@ class asm_data
223225
HEADER_ACCESS_GET_SET(uint32_t, linenumber);
224226
HEADER_ACCESS_GET_SET(std::string, file);
225227
HEADER_ACCESS_GET_SET(std::string, line);
228+
HEADER_ACCESS_GET_SET(bool, is_save_restore);
226229
bool isLabel() { return m_optype == operation_type::label; }
227230
bool isOpcode() { return m_optype == operation_type::op; }
228231
bool isAnnotation() { return m_optype == operation_type::annotation; }
@@ -346,6 +349,7 @@ class asm_parser: public std::enable_shared_from_this<asm_parser>
346349
std::map<int, std::vector<std::string>> m_preempt_hintmaps; // group -> vector of hintmap_labels (multiple PREEMPT opcodes per group)
347350
std::map<std::string, std::pair<std::string, std::string>> m_hintmap_labels; // hintmap_label -> (save_label, restore_label)
348351
std::set<int> m_preempt_without_hintmap; // groups that have PREEMPT opcodes without hintmaps
352+
bool m_is_save_restore_routine = false; // True when parsing save/restore routine files
349353

350354
// One unique scratchpad region: all hintmap labels that share the same scratchbase+size
351355
struct hintmap_group_entry {
@@ -425,6 +429,20 @@ class asm_parser: public std::enable_shared_from_this<asm_parser>
425429

426430
bool is_multi_column_mode() const { return m_preempt_labels.size() > 1; }
427431

432+
// Check if currently parsing save/restore routine
433+
bool is_save_restore_routine() const { return m_is_save_restore_routine; }
434+
void set_save_restore_routine(bool val) { m_is_save_restore_routine = val; }
435+
436+
// Check if we should skip setpad for this target in save/restore routine
437+
bool should_skip_setpad_in_save_restore() const {
438+
return m_is_save_restore_routine;
439+
}
440+
441+
// Check if we should use scratch-pad section for save/restore APPLY_OFFSET_57
442+
//bool should_use_scratchpad_section_for_save_restore() const {
443+
// return m_is_save_restore_routine;
444+
//}
445+
428446
// Record preempt label for current group (called when PREEMPT opcode is hit)
429447
// Label naming: save_N / restore_N where N = index (group/2 + 1)
430448
// 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-
96b00cbda9f5061f2cfef83b22c2e01d
1+
7605802954688a5e7d1fff4198e6ef59

0 commit comments

Comments
 (0)