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