aco: improve clrx disassembly
- remove uninteresting lines of output - remove binary offset before instructions, for easier diffing - replace generated labels with block numbers - add encoded instructions at the end of lines, like LLVM dissaembly - print constant data instead of trying to disassemble it Signed-off-by: Rhys Perry <pendingchaos02@gmail.com> Reviewed-by: Daniel Schürmann <daniel@schuermann.dev> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14042>
This commit is contained in:
parent
11eb03c44e
commit
c7fa15b381
|
@ -42,6 +42,60 @@
|
|||
namespace aco {
|
||||
namespace {
|
||||
|
||||
std::vector<bool>
|
||||
get_referenced_blocks(Program* program)
|
||||
{
|
||||
std::vector<bool> referenced_blocks(program->blocks.size());
|
||||
referenced_blocks[0] = true;
|
||||
for (Block& block : program->blocks) {
|
||||
for (unsigned succ : block.linear_succs)
|
||||
referenced_blocks[succ] = true;
|
||||
}
|
||||
return referenced_blocks;
|
||||
}
|
||||
|
||||
void
|
||||
print_block_markers(FILE* output, Program* program, const std::vector<bool>& referenced_blocks,
|
||||
unsigned* next_block, unsigned pos)
|
||||
{
|
||||
while (*next_block < program->blocks.size() && pos == program->blocks[*next_block].offset) {
|
||||
if (referenced_blocks[*next_block])
|
||||
fprintf(output, "BB%u:\n", *next_block);
|
||||
(*next_block)++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_instr(FILE* output, const std::vector<uint32_t>& binary, char* instr, unsigned size,
|
||||
unsigned pos)
|
||||
{
|
||||
fprintf(output, "%-60s ;", instr);
|
||||
|
||||
for (unsigned i = 0; i < size; i++)
|
||||
fprintf(output, " %.8x", binary[pos + i]);
|
||||
fputc('\n', output);
|
||||
}
|
||||
|
||||
void
|
||||
print_constant_data(FILE* output, Program* program)
|
||||
{
|
||||
if (program->constant_data.empty())
|
||||
return;
|
||||
|
||||
fputs("\n/* constant data */\n", output);
|
||||
for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
|
||||
fprintf(output, "[%.6u]", i);
|
||||
unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
|
||||
for (unsigned j = 0; j < line_size; j += 4) {
|
||||
unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
|
||||
uint32_t v = 0;
|
||||
memcpy(&v, &program->constant_data[i + j], size);
|
||||
fprintf(output, " %.8x", v);
|
||||
}
|
||||
fputc('\n', output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the GPU type to use for CLRXdisasm
|
||||
*/
|
||||
|
@ -99,7 +153,26 @@ to_clrx_device_name(chip_class cc, radeon_family family)
|
|||
}
|
||||
|
||||
bool
|
||||
print_asm_clrx(Program* program, std::vector<uint32_t>& binary, FILE* output)
|
||||
get_branch_target(char** output, Program* program, const std::vector<bool>& referenced_blocks,
|
||||
char** line_start)
|
||||
{
|
||||
unsigned pos;
|
||||
if (sscanf(*line_start, ".L%d_0", &pos) != 1)
|
||||
return false;
|
||||
pos /= 4;
|
||||
*line_start = strchr(*line_start, '_') + 2;
|
||||
|
||||
for (Block& block : program->blocks) {
|
||||
if (referenced_blocks[block.index] && block.offset == pos) {
|
||||
*output += sprintf(*output, "BB%u", block.index);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
print_asm_clrx(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return true;
|
||||
|
@ -116,8 +189,8 @@ print_asm_clrx(Program* program, std::vector<uint32_t>& binary, FILE* output)
|
|||
if (fd < 0)
|
||||
return true;
|
||||
|
||||
for (uint32_t w : binary) {
|
||||
if (write(fd, &w, sizeof(w)) == -1)
|
||||
for (unsigned i = 0; i < exec_size; i++) {
|
||||
if (write(fd, &binary[i], 4) == -1)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
|
@ -131,11 +204,57 @@ print_asm_clrx(Program* program, std::vector<uint32_t>& binary, FILE* output)
|
|||
goto fail;
|
||||
}
|
||||
|
||||
std::vector<bool> referenced_blocks = get_referenced_blocks(program);
|
||||
unsigned next_block = 0;
|
||||
|
||||
char prev_instr[2048];
|
||||
unsigned prev_pos = 0;
|
||||
do {
|
||||
fputs(line, output);
|
||||
char* line_start = line;
|
||||
if (strncmp(line_start, "/*", 2))
|
||||
continue;
|
||||
|
||||
unsigned pos;
|
||||
if (sscanf(line_start, "/*%x*/", &pos) != 1)
|
||||
continue;
|
||||
pos /= 4u; /* get the dword position */
|
||||
|
||||
while (strncmp(line_start, "*/", 2))
|
||||
line_start++;
|
||||
line_start += 2;
|
||||
|
||||
while (line_start[0] == ' ')
|
||||
line_start++;
|
||||
*strchr(line_start, '\n') = 0;
|
||||
|
||||
if (*line_start == 0)
|
||||
continue; /* not an instruction, only a comment */
|
||||
|
||||
if (pos != prev_pos) {
|
||||
/* Print the previous instruction, now that we know the encoding size. */
|
||||
print_instr(output, binary, prev_instr, pos - prev_pos, prev_pos);
|
||||
prev_pos = pos;
|
||||
}
|
||||
|
||||
print_block_markers(output, program, referenced_blocks, &next_block, pos);
|
||||
|
||||
char* dest = prev_instr;
|
||||
*(dest++) = '\t';
|
||||
while (*line_start) {
|
||||
if (!strncmp(line_start, ".L", 2) &&
|
||||
get_branch_target(&dest, program, referenced_blocks, &line_start))
|
||||
continue;
|
||||
*(dest++) = *(line_start++);
|
||||
}
|
||||
*(dest++) = 0;
|
||||
} while (fgets(line, sizeof(line), p));
|
||||
|
||||
if (prev_pos != exec_size)
|
||||
print_instr(output, binary, prev_instr, exec_size - prev_pos, prev_pos);
|
||||
|
||||
pclose(p);
|
||||
|
||||
print_constant_data(output, program);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -198,12 +317,7 @@ disasm_instr(chip_class chip, LLVMDisasmContextRef disasm, uint32_t* binary, uns
|
|||
bool
|
||||
print_asm_llvm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
|
||||
{
|
||||
std::vector<bool> referenced_blocks(program->blocks.size());
|
||||
referenced_blocks[0] = true;
|
||||
for (Block& block : program->blocks) {
|
||||
for (unsigned succ : block.linear_succs)
|
||||
referenced_blocks[succ] = true;
|
||||
}
|
||||
std::vector<bool> referenced_blocks = get_referenced_blocks(program);
|
||||
|
||||
std::vector<llvm::SymbolInfoTy> symbols;
|
||||
std::vector<std::array<char, 16>> block_names;
|
||||
|
@ -248,22 +362,14 @@ print_asm_llvm(Program* program, std::vector<uint32_t>& binary, unsigned exec_si
|
|||
repeat_count = 0;
|
||||
}
|
||||
|
||||
while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
|
||||
if (referenced_blocks[next_block])
|
||||
fprintf(output, "BB%u:\n", next_block);
|
||||
next_block++;
|
||||
}
|
||||
print_block_markers(output, program, referenced_blocks, &next_block, pos);
|
||||
|
||||
char outline[1024];
|
||||
std::pair<bool, size_t> res = disasm_instr(program->chip_class, disasm, binary.data(),
|
||||
exec_size, pos, outline, sizeof(outline));
|
||||
invalid |= res.first;
|
||||
|
||||
fprintf(output, "%-60s ;", outline);
|
||||
|
||||
for (unsigned i = 0; i < res.second; i++)
|
||||
fprintf(output, " %.8x", binary[pos + i]);
|
||||
fputc('\n', output);
|
||||
print_instr(output, binary, outline, res.second, pos);
|
||||
|
||||
prev_size = res.second;
|
||||
prev_pos = pos;
|
||||
|
@ -273,20 +379,7 @@ print_asm_llvm(Program* program, std::vector<uint32_t>& binary, unsigned exec_si
|
|||
|
||||
LLVMDisasmDispose(disasm);
|
||||
|
||||
if (program->constant_data.size()) {
|
||||
fputs("\n/* constant data */\n", output);
|
||||
for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
|
||||
fprintf(output, "[%.6u]", i);
|
||||
unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
|
||||
for (unsigned j = 0; j < line_size; j += 4) {
|
||||
unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
|
||||
uint32_t v = 0;
|
||||
memcpy(&v, &program->constant_data[i + j], size);
|
||||
fprintf(output, " %.8x", v);
|
||||
}
|
||||
fputc('\n', output);
|
||||
}
|
||||
}
|
||||
print_constant_data(output, program);
|
||||
|
||||
return invalid;
|
||||
}
|
||||
|
@ -323,7 +416,7 @@ print_asm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, F
|
|||
}
|
||||
#endif
|
||||
|
||||
return print_asm_clrx(program, binary, output);
|
||||
return print_asm_clrx(program, binary, exec_size, output);
|
||||
}
|
||||
|
||||
} // namespace aco
|
||||
|
|
Loading…
Reference in New Issue