xref: /aosp_15_r20/external/mesa3d/src/amd/compiler/aco_print_asm.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2018 Valve Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include "aco_ir.h"
8 
9 #include "util/u_debug.h"
10 
11 #if AMD_LLVM_AVAILABLE
12 #if defined(_MSC_VER) && defined(restrict)
13 #undef restrict
14 #endif
15 #include "llvm/ac_llvm_util.h"
16 
17 #include "llvm-c/Disassembler.h"
18 #include <llvm/ADT/StringRef.h>
19 #include <llvm/MC/MCDisassembler/MCDisassembler.h>
20 #endif
21 
22 #include <array>
23 #include <iomanip>
24 #include <vector>
25 
26 namespace aco {
27 namespace {
28 
29 std::vector<bool>
get_referenced_blocks(Program * program)30 get_referenced_blocks(Program* program)
31 {
32    std::vector<bool> referenced_blocks(program->blocks.size());
33    referenced_blocks[0] = true;
34    for (Block& block : program->blocks) {
35       for (unsigned succ : block.linear_succs)
36          referenced_blocks[succ] = true;
37    }
38    return referenced_blocks;
39 }
40 
41 void
print_block_markers(FILE * output,Program * program,const std::vector<bool> & referenced_blocks,unsigned * next_block,unsigned pos)42 print_block_markers(FILE* output, Program* program, const std::vector<bool>& referenced_blocks,
43                     unsigned* next_block, unsigned pos)
44 {
45    while (*next_block < program->blocks.size() && pos == program->blocks[*next_block].offset) {
46       if (referenced_blocks[*next_block])
47          fprintf(output, "BB%u:\n", *next_block);
48       (*next_block)++;
49    }
50 }
51 
52 void
print_instr(FILE * output,const std::vector<uint32_t> & binary,char * instr,unsigned size,unsigned pos)53 print_instr(FILE* output, const std::vector<uint32_t>& binary, char* instr, unsigned size,
54             unsigned pos)
55 {
56    fprintf(output, "%-60s ;", instr);
57 
58    for (unsigned i = 0; i < size; i++)
59       fprintf(output, " %.8x", binary[pos + i]);
60    fputc('\n', output);
61 }
62 
63 void
print_constant_data(FILE * output,Program * program)64 print_constant_data(FILE* output, Program* program)
65 {
66    if (program->constant_data.empty())
67       return;
68 
69    fputs("\n/* constant data */\n", output);
70    for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
71       fprintf(output, "[%.6u]", i);
72       unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
73       for (unsigned j = 0; j < line_size; j += 4) {
74          unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
75          uint32_t v = 0;
76          memcpy(&v, &program->constant_data[i + j], size);
77          fprintf(output, " %.8x", v);
78       }
79       fputc('\n', output);
80    }
81 }
82 
83 /**
84  * Determines the GPU type to use for CLRXdisasm
85  */
86 const char*
to_clrx_device_name(amd_gfx_level gfx_level,radeon_family family)87 to_clrx_device_name(amd_gfx_level gfx_level, radeon_family family)
88 {
89    switch (gfx_level) {
90    case GFX6:
91       switch (family) {
92       case CHIP_TAHITI: return "tahiti";
93       case CHIP_PITCAIRN: return "pitcairn";
94       case CHIP_VERDE: return "capeverde";
95       case CHIP_OLAND: return "oland";
96       case CHIP_HAINAN: return "hainan";
97       default: return nullptr;
98       }
99    case GFX7:
100       switch (family) {
101       case CHIP_BONAIRE: return "bonaire";
102       case CHIP_KAVERI: return "gfx700";
103       case CHIP_HAWAII: return "hawaii";
104       default: return nullptr;
105       }
106    case GFX8:
107       switch (family) {
108       case CHIP_TONGA: return "tonga";
109       case CHIP_ICELAND: return "iceland";
110       case CHIP_CARRIZO: return "carrizo";
111       case CHIP_FIJI: return "fiji";
112       case CHIP_STONEY: return "stoney";
113       case CHIP_POLARIS10: return "polaris10";
114       case CHIP_POLARIS11: return "polaris11";
115       case CHIP_POLARIS12: return "polaris12";
116       case CHIP_VEGAM: return "polaris11";
117       default: return nullptr;
118       }
119    case GFX9:
120       switch (family) {
121       case CHIP_VEGA10: return "vega10";
122       case CHIP_VEGA12: return "vega12";
123       case CHIP_VEGA20: return "vega20";
124       case CHIP_RAVEN: return "raven";
125       default: return nullptr;
126       }
127    case GFX10:
128       switch (family) {
129       case CHIP_NAVI10: return "gfx1010";
130       case CHIP_NAVI12: return "gfx1011";
131       default: return nullptr;
132       }
133    default: return nullptr;
134    }
135 }
136 
137 bool
get_branch_target(char ** output,Program * program,const std::vector<bool> & referenced_blocks,char ** line_start)138 get_branch_target(char** output, Program* program, const std::vector<bool>& referenced_blocks,
139                   char** line_start)
140 {
141    unsigned pos;
142    if (sscanf(*line_start, ".L%d_0", &pos) != 1)
143       return false;
144    pos /= 4;
145    *line_start = strchr(*line_start, '_') + 2;
146 
147    for (Block& block : program->blocks) {
148       if (referenced_blocks[block.index] && block.offset == pos) {
149          *output += sprintf(*output, "BB%u", block.index);
150          return true;
151       }
152    }
153    return false;
154 }
155 
156 bool
print_asm_clrx(Program * program,std::vector<uint32_t> & binary,unsigned exec_size,FILE * output)157 print_asm_clrx(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
158 {
159 #ifdef _WIN32
160    return true;
161 #else
162    char path[] = "/tmp/fileXXXXXX";
163    char line[2048], command[128];
164    FILE* p;
165    int fd;
166 
167    const char* gpu_type = to_clrx_device_name(program->gfx_level, program->family);
168 
169    /* Dump the binary into a temporary file. */
170    fd = mkstemp(path);
171    if (fd < 0)
172       return true;
173 
174    for (unsigned i = 0; i < exec_size; i++) {
175       if (write(fd, &binary[i], 4) == -1)
176          goto fail;
177    }
178 
179    sprintf(command, "clrxdisasm --gpuType=%s -r %s", gpu_type, path);
180 
181    p = popen(command, "r");
182    if (p) {
183       if (!fgets(line, sizeof(line), p)) {
184          fprintf(output, "clrxdisasm not found\n");
185          pclose(p);
186          goto fail;
187       }
188 
189       std::vector<bool> referenced_blocks = get_referenced_blocks(program);
190       unsigned next_block = 0;
191 
192       char prev_instr[2048];
193       unsigned prev_pos = 0;
194       do {
195          char* line_start = line;
196          if (strncmp(line_start, "/*", 2))
197             continue;
198 
199          unsigned pos;
200          if (sscanf(line_start, "/*%x*/", &pos) != 1)
201             continue;
202          pos /= 4u; /* get the dword position */
203 
204          while (strncmp(line_start, "*/", 2))
205             line_start++;
206          line_start += 2;
207 
208          while (line_start[0] == ' ')
209             line_start++;
210          *strchr(line_start, '\n') = 0;
211 
212          if (*line_start == 0)
213             continue; /* not an instruction, only a comment */
214 
215          if (pos != prev_pos) {
216             /* Print the previous instruction, now that we know the encoding size. */
217             print_instr(output, binary, prev_instr, pos - prev_pos, prev_pos);
218             prev_pos = pos;
219          }
220 
221          print_block_markers(output, program, referenced_blocks, &next_block, pos);
222 
223          char* dest = prev_instr;
224          *(dest++) = '\t';
225          while (*line_start) {
226             if (!strncmp(line_start, ".L", 2) &&
227                 get_branch_target(&dest, program, referenced_blocks, &line_start))
228                continue;
229             *(dest++) = *(line_start++);
230          }
231          *(dest++) = 0;
232       } while (fgets(line, sizeof(line), p));
233 
234       if (prev_pos != exec_size)
235          print_instr(output, binary, prev_instr, exec_size - prev_pos, prev_pos);
236 
237       pclose(p);
238 
239       print_constant_data(output, program);
240    }
241 
242    return false;
243 
244 fail:
245    close(fd);
246    unlink(path);
247    return true;
248 #endif
249 }
250 
251 #if AMD_LLVM_AVAILABLE
252 std::pair<bool, size_t>
disasm_instr(amd_gfx_level gfx_level,LLVMDisasmContextRef disasm,uint32_t * binary,unsigned exec_size,size_t pos,char * outline,unsigned outline_size)253 disasm_instr(amd_gfx_level gfx_level, LLVMDisasmContextRef disasm, uint32_t* binary,
254              unsigned exec_size, size_t pos, char* outline, unsigned outline_size)
255 {
256    size_t l =
257       LLVMDisasmInstruction(disasm, (uint8_t*)&binary[pos], (exec_size - pos) * sizeof(uint32_t),
258                             pos * 4, outline, outline_size);
259 
260    if (gfx_level >= GFX10 && l == 8 && ((binary[pos] & 0xffff0000) == 0xd7610000) &&
261        ((binary[pos + 1] & 0x1ff) == 0xff)) {
262       /* v_writelane with literal uses 3 dwords but llvm consumes only 2 */
263       l += 4;
264    }
265 
266    bool invalid = false;
267    size_t size;
268    if (!l &&
269        ((gfx_level >= GFX9 &&
270          (binary[pos] & 0xffff8000) == 0xd1348000) || /* v_add_u32_e64 + clamp */
271         (gfx_level >= GFX10 &&
272          (binary[pos] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
273         (gfx_level <= GFX9 &&
274          (binary[pos] & 0xffff8000) == 0xd1268000) || /* v_add_u16_e64 + clamp */
275         (gfx_level >= GFX10 && (binary[pos] & 0xffff8000) == 0xd76d8000) || /* v_add3_u32 + clamp */
276         (gfx_level == GFX9 && (binary[pos] & 0xffff8000) == 0xd1ff8000)) /* v_add3_u32 + clamp */) {
277       strcpy(outline, "\tinteger addition + clamp");
278       bool has_literal = gfx_level >= GFX10 && (((binary[pos + 1] & 0x1ff) == 0xff) ||
279                                                 (((binary[pos + 1] >> 9) & 0x1ff) == 0xff));
280       size = 2 + has_literal;
281    } else if (gfx_level >= GFX10 && l == 4 && ((binary[pos] & 0xfe0001ff) == 0x020000f9)) {
282       strcpy(outline, "\tv_cndmask_b32 + sdwa");
283       size = 2;
284    } else if (!l) {
285       strcpy(outline, "(invalid instruction)");
286       size = 1;
287       invalid = true;
288    } else {
289       assert(l % 4 == 0);
290       size = l / 4;
291    }
292 
293    return std::make_pair(invalid, size);
294 }
295 
296 bool
print_asm_llvm(Program * program,std::vector<uint32_t> & binary,unsigned exec_size,FILE * output)297 print_asm_llvm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
298 {
299    std::vector<bool> referenced_blocks = get_referenced_blocks(program);
300 
301    std::vector<llvm::SymbolInfoTy> symbols;
302    std::vector<std::array<char, 16>> block_names;
303    block_names.reserve(program->blocks.size());
304    for (Block& block : program->blocks) {
305       if (!referenced_blocks[block.index])
306          continue;
307       std::array<char, 16> name;
308       sprintf(name.data(), "BB%u", block.index);
309       block_names.push_back(name);
310       symbols.emplace_back(block.offset * 4,
311                            llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
312    }
313 
314    const char* features = "";
315    if (program->gfx_level >= GFX10 && program->wave_size == 64) {
316       features = "+wavefrontsize64";
317    }
318 
319    LLVMDisasmContextRef disasm =
320       LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d", ac_get_llvm_processor_name(program->family),
321                                   features, &symbols, 0, NULL, NULL);
322 
323    size_t pos = 0;
324    bool invalid = false;
325    unsigned next_block = 0;
326 
327    unsigned prev_size = 0;
328    unsigned prev_pos = 0;
329    unsigned repeat_count = 0;
330    while (pos <= exec_size) {
331       bool new_block =
332          next_block < program->blocks.size() && pos == program->blocks[next_block].offset;
333       if (pos + prev_size <= exec_size && prev_pos != pos && !new_block &&
334           memcmp(&binary[prev_pos], &binary[pos], prev_size * 4) == 0) {
335          repeat_count++;
336          pos += prev_size;
337          continue;
338       } else {
339          if (repeat_count)
340             fprintf(output, "\t(then repeated %u times)\n", repeat_count);
341          repeat_count = 0;
342       }
343 
344       print_block_markers(output, program, referenced_blocks, &next_block, pos);
345 
346       /* For empty last block, only print block marker. */
347       if (pos == exec_size)
348          break;
349 
350       char outline[1024];
351       std::pair<bool, size_t> res = disasm_instr(program->gfx_level, disasm, binary.data(),
352                                                  exec_size, pos, outline, sizeof(outline));
353       invalid |= res.first;
354 
355       print_instr(output, binary, outline, res.second, pos);
356 
357       prev_size = res.second;
358       prev_pos = pos;
359       pos += res.second;
360    }
361    assert(next_block == program->blocks.size());
362 
363    LLVMDisasmDispose(disasm);
364 
365    print_constant_data(output, program);
366 
367    return invalid;
368 }
369 #endif /* AMD_LLVM_AVAILABLE */
370 
371 } /* end namespace */
372 
373 bool
check_print_asm_support(Program * program)374 check_print_asm_support(Program* program)
375 {
376 #if AMD_LLVM_AVAILABLE
377    if (program->gfx_level >= GFX8) {
378       /* LLVM disassembler only supports GFX8+ */
379       const char* name = ac_get_llvm_processor_name(program->family);
380       const char* triple = "amdgcn--";
381       LLVMTargetRef target = ac_get_llvm_target(triple);
382 
383       LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
384          target, triple, name, "", LLVMCodeGenLevelDefault, LLVMRelocDefault, LLVMCodeModelDefault);
385 
386       bool supported = ac_is_llvm_processor_supported(tm, name);
387       LLVMDisposeTargetMachine(tm);
388 
389       if (supported)
390          return true;
391    }
392 #endif
393 
394 #ifndef _WIN32
395    /* Check if CLRX disassembler binary is available and can disassemble the program */
396    return to_clrx_device_name(program->gfx_level, program->family) &&
397           system("clrxdisasm --version > /dev/null 2>&1") == 0;
398 #else
399    return false;
400 #endif
401 }
402 
403 /* Returns true on failure */
404 bool
print_asm(Program * program,std::vector<uint32_t> & binary,unsigned exec_size,FILE * output)405 print_asm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
406 {
407 #if AMD_LLVM_AVAILABLE
408    if (program->gfx_level >= GFX8) {
409       return print_asm_llvm(program, binary, exec_size, output);
410    }
411 #endif
412 
413    return print_asm_clrx(program, binary, exec_size, output);
414 }
415 
416 } // namespace aco
417