• 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 #ifdef LLVM_AVAILABLE
28 #if defined(_MSC_VER) && defined(restrict)
29 #undef restrict
30 #endif
31 #include "llvm/ac_llvm_util.h"
32 
33 #include "llvm-c/Disassembler.h"
34 #include <llvm/ADT/StringRef.h>
35 #include <llvm/MC/MCDisassembler/MCDisassembler.h>
36 #endif
37 
38 #include <array>
39 #include <iomanip>
40 #include <vector>
41 
42 namespace aco {
43 namespace {
44 
45 /**
46  * Determines the GPU type to use for CLRXdisasm
47  */
48 const char*
to_clrx_device_name(chip_class cc,radeon_family family)49 to_clrx_device_name(chip_class cc, radeon_family family)
50 {
51    switch (cc) {
52    case GFX6:
53       switch (family) {
54       case CHIP_TAHITI: return "tahiti";
55       case CHIP_PITCAIRN: return "pitcairn";
56       case CHIP_VERDE: return "capeverde";
57       case CHIP_OLAND: return "oland";
58       case CHIP_HAINAN: return "hainan";
59       default: return nullptr;
60       }
61    case GFX7:
62       switch (family) {
63       case CHIP_BONAIRE: return "bonaire";
64       case CHIP_KAVERI: return "gfx700";
65       case CHIP_HAWAII: return "hawaii";
66       default: return nullptr;
67       }
68    case GFX8:
69       switch (family) {
70       case CHIP_TONGA: return "tonga";
71       case CHIP_ICELAND: return "iceland";
72       case CHIP_CARRIZO: return "carrizo";
73       case CHIP_FIJI: return "fiji";
74       case CHIP_STONEY: return "stoney";
75       case CHIP_POLARIS10: return "polaris10";
76       case CHIP_POLARIS11: return "polaris11";
77       case CHIP_POLARIS12: return "polaris12";
78       case CHIP_VEGAM: return "polaris11";
79       default: return nullptr;
80       }
81    case GFX9:
82       switch (family) {
83       case CHIP_VEGA10: return "vega10";
84       case CHIP_VEGA12: return "vega12";
85       case CHIP_VEGA20: return "vega20";
86       case CHIP_RAVEN: return "raven";
87       default: return nullptr;
88       }
89    case GFX10:
90       switch (family) {
91       case CHIP_NAVI10: return "gfx1010";
92       case CHIP_NAVI12: return "gfx1011";
93       default: return nullptr;
94       }
95    case GFX10_3:
96       return nullptr;
97    default: unreachable("Invalid chip class!"); return nullptr;
98    }
99 }
100 
101 bool
print_asm_clrx(Program * program,std::vector<uint32_t> & binary,FILE * output)102 print_asm_clrx(Program* program, std::vector<uint32_t>& binary, FILE* output)
103 {
104 #ifdef _WIN32
105    return true;
106 #else
107    char path[] = "/tmp/fileXXXXXX";
108    char line[2048], command[128];
109    FILE* p;
110    int fd;
111 
112    const char* gpu_type = to_clrx_device_name(program->chip_class, program->family);
113 
114    /* Dump the binary into a temporary file. */
115    fd = mkstemp(path);
116    if (fd < 0)
117       return true;
118 
119    for (uint32_t w : binary) {
120       if (write(fd, &w, sizeof(w)) == -1)
121          goto fail;
122    }
123 
124    sprintf(command, "clrxdisasm --gpuType=%s -r %s", gpu_type, path);
125 
126    p = popen(command, "r");
127    if (p) {
128       if (!fgets(line, sizeof(line), p)) {
129          fprintf(output, "clrxdisasm not found\n");
130          pclose(p);
131          goto fail;
132       }
133 
134       do {
135          fputs(line, output);
136       } while (fgets(line, sizeof(line), p));
137 
138       pclose(p);
139    }
140 
141    return false;
142 
143 fail:
144    close(fd);
145    unlink(path);
146    return true;
147 #endif
148 }
149 
150 #ifdef LLVM_AVAILABLE
151 std::pair<bool, size_t>
disasm_instr(chip_class chip,LLVMDisasmContextRef disasm,uint32_t * binary,unsigned exec_size,size_t pos,char * outline,unsigned outline_size)152 disasm_instr(chip_class chip, LLVMDisasmContextRef disasm, uint32_t* binary, unsigned exec_size,
153              size_t pos, char* outline, unsigned outline_size)
154 {
155    /* mask out src2 on v_writelane_b32 */
156    if (((chip == GFX8 || chip == GFX9) && (binary[pos] & 0xffff8000) == 0xd28a0000) ||
157        (chip >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7610000)) {
158       binary[pos + 1] = binary[pos + 1] & 0xF803FFFF;
159    }
160 
161    size_t l =
162       LLVMDisasmInstruction(disasm, (uint8_t*)&binary[pos], (exec_size - pos) * sizeof(uint32_t),
163                             pos * 4, outline, outline_size);
164 
165    if (chip >= GFX10 && l == 8 && ((binary[pos] & 0xffff0000) == 0xd7610000) &&
166        ((binary[pos + 1] & 0x1ff) == 0xff)) {
167       /* v_writelane with literal uses 3 dwords but llvm consumes only 2 */
168       l += 4;
169    }
170 
171    bool invalid = false;
172    size_t size;
173    if (!l &&
174        ((chip >= GFX9 && (binary[pos] & 0xffff8000) == 0xd1348000) ||  /* v_add_u32_e64 + clamp */
175         (chip >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
176         (chip <= GFX9 && (binary[pos] & 0xffff8000) == 0xd1268000) ||  /* v_add_u16_e64 + clamp */
177         (chip >= GFX10 && (binary[pos] & 0xffff8000) == 0xd76d8000) || /* v_add3_u32 + clamp */
178         (chip == GFX9 && (binary[pos] & 0xffff8000) == 0xd1ff8000)) /* v_add3_u32 + clamp */) {
179       strcpy(outline, "\tinteger addition + clamp");
180       bool has_literal = chip >= GFX10 && (((binary[pos + 1] & 0x1ff) == 0xff) ||
181                                            (((binary[pos + 1] >> 9) & 0x1ff) == 0xff));
182       size = 2 + has_literal;
183    } else if (chip >= GFX10 && l == 4 && ((binary[pos] & 0xfe0001ff) == 0x020000f9)) {
184       strcpy(outline, "\tv_cndmask_b32 + sdwa");
185       size = 2;
186    } else if (!l) {
187       strcpy(outline, "(invalid instruction)");
188       size = 1;
189       invalid = true;
190    } else {
191       assert(l % 4 == 0);
192       size = l / 4;
193    }
194 
195    return std::make_pair(invalid, size);
196 }
197 
198 bool
print_asm_llvm(Program * program,std::vector<uint32_t> & binary,unsigned exec_size,FILE * output)199 print_asm_llvm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
200 {
201    std::vector<bool> referenced_blocks(program->blocks.size());
202    referenced_blocks[0] = true;
203    for (Block& block : program->blocks) {
204       for (unsigned succ : block.linear_succs)
205          referenced_blocks[succ] = true;
206    }
207 
208    std::vector<llvm::SymbolInfoTy> symbols;
209    std::vector<std::array<char, 16>> block_names;
210    block_names.reserve(program->blocks.size());
211    for (Block& block : program->blocks) {
212       if (!referenced_blocks[block.index])
213          continue;
214       std::array<char, 16> name;
215       sprintf(name.data(), "BB%u", block.index);
216       block_names.push_back(name);
217       symbols.emplace_back(block.offset * 4,
218                            llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
219    }
220 
221    const char* features = "";
222    if (program->chip_class >= GFX10 && program->wave_size == 64) {
223       features = "+wavefrontsize64";
224    }
225 
226    LLVMDisasmContextRef disasm =
227       LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d", ac_get_llvm_processor_name(program->family),
228                                   features, &symbols, 0, NULL, NULL);
229 
230    size_t pos = 0;
231    bool invalid = false;
232    unsigned next_block = 0;
233 
234    unsigned prev_size = 0;
235    unsigned prev_pos = 0;
236    unsigned repeat_count = 0;
237    while (pos < exec_size) {
238       bool new_block =
239          next_block < program->blocks.size() && pos == program->blocks[next_block].offset;
240       if (pos + prev_size <= exec_size && prev_pos != pos && !new_block &&
241           memcmp(&binary[prev_pos], &binary[pos], prev_size * 4) == 0) {
242          repeat_count++;
243          pos += prev_size;
244          continue;
245       } else {
246          if (repeat_count)
247             fprintf(output, "\t(then repeated %u times)\n", repeat_count);
248          repeat_count = 0;
249       }
250 
251       while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
252          if (referenced_blocks[next_block])
253             fprintf(output, "BB%u:\n", next_block);
254          next_block++;
255       }
256 
257       char outline[1024];
258       std::pair<bool, size_t> res = disasm_instr(program->chip_class, disasm, binary.data(),
259                                                  exec_size, pos, outline, sizeof(outline));
260       invalid |= res.first;
261 
262       fprintf(output, "%-60s ;", outline);
263 
264       for (unsigned i = 0; i < res.second; i++)
265          fprintf(output, " %.8x", binary[pos + i]);
266       fputc('\n', output);
267 
268       prev_size = res.second;
269       prev_pos = pos;
270       pos += res.second;
271    }
272    assert(next_block == program->blocks.size());
273 
274    LLVMDisasmDispose(disasm);
275 
276    if (program->constant_data.size()) {
277       fputs("\n/* constant data */\n", output);
278       for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
279          fprintf(output, "[%.6u]", i);
280          unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
281          for (unsigned j = 0; j < line_size; j += 4) {
282             unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
283             uint32_t v = 0;
284             memcpy(&v, &program->constant_data[i + j], size);
285             fprintf(output, " %.8x", v);
286          }
287          fputc('\n', output);
288       }
289    }
290 
291    return invalid;
292 }
293 #endif /* LLVM_AVAILABLE */
294 
295 } /* end namespace */
296 
297 bool
check_print_asm_support(Program * program)298 check_print_asm_support(Program* program)
299 {
300 #ifdef LLVM_AVAILABLE
301    if (program->chip_class >= GFX8) {
302       /* LLVM disassembler only supports GFX8+ */
303       return true;
304    }
305 #endif
306 
307 #ifndef _WIN32
308    /* Check if CLRX disassembler binary is available and can disassemble the program */
309    return to_clrx_device_name(program->chip_class, program->family) &&
310           system("clrxdisasm --version") == 0;
311 #else
312    return false;
313 #endif
314 }
315 
316 /* Returns true on failure */
317 bool
print_asm(Program * program,std::vector<uint32_t> & binary,unsigned exec_size,FILE * output)318 print_asm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
319 {
320 #ifdef LLVM_AVAILABLE
321    if (program->chip_class >= GFX8) {
322       return print_asm_llvm(program, binary, exec_size, output);
323    }
324 #endif
325 
326    return print_asm_clrx(program, binary, output);
327 }
328 
329 } // namespace aco
330