aco: print assembly with CLRXdisasm for GFX6-GFX7 if found on the system
[mesa.git] / src / amd / compiler / aco_print_asm.cpp
1 #include <array>
2 #include <iomanip>
3 #include "aco_ir.h"
4 #include "llvm-c/Disassembler.h"
5 #include "ac_llvm_util.h"
6
7 #include <llvm/ADT/StringRef.h>
8
9 namespace aco {
10
11 /* LLVM disassembler only supports GFX8+, try to disassemble with CLRXdisasm
12 * for GFX6-GFX7 if found on the system, this is better than nothing.
13 */
14 void print_asm_gfx6_gfx7(Program *program, std::vector<uint32_t>& binary,
15 std::ostream& out)
16 {
17 char path[] = "/tmp/fileXXXXXX";
18 char line[2048], command[128];
19 FILE *p;
20 int fd;
21
22 /* Dump the binary into a temporary file. */
23 fd = mkstemp(path);
24 if (fd < 0)
25 return;
26
27 for (uint32_t w : binary)
28 {
29 if (write(fd, &w, sizeof(w)) == -1)
30 goto fail;
31 }
32
33 sprintf(command, "clrxdisasm --gpuType=%s -r %s",
34 program->chip_class == GFX6 ? "gfx600" : "gfx700", path);
35
36 p = popen(command, "r");
37 if (p) {
38 while (fgets(line, sizeof(line), p))
39 out << line;
40 pclose(p);
41 }
42
43 fail:
44 close(fd);
45 unlink(path);
46 }
47
48 void print_asm(Program *program, std::vector<uint32_t>& binary,
49 unsigned exec_size, std::ostream& out)
50 {
51 if (program->chip_class <= GFX7) {
52 print_asm_gfx6_gfx7(program, binary, out);
53 return;
54 }
55
56 std::vector<bool> referenced_blocks(program->blocks.size());
57 referenced_blocks[0] = true;
58 for (Block& block : program->blocks) {
59 for (unsigned succ : block.linear_succs)
60 referenced_blocks[succ] = true;
61 }
62
63 std::vector<std::tuple<uint64_t, llvm::StringRef, uint8_t>> symbols;
64 std::vector<std::array<char,16>> block_names;
65 block_names.reserve(program->blocks.size());
66 for (Block& block : program->blocks) {
67 if (!referenced_blocks[block.index])
68 continue;
69 std::array<char, 16> name;
70 sprintf(name.data(), "BB%u", block.index);
71 block_names.push_back(name);
72 symbols.emplace_back(block.offset * 4, llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
73 }
74
75 const char *features = "";
76 if (program->chip_class >= GFX10 && program->wave_size == 64) {
77 features = "+wavefrontsize64";
78 }
79
80 LLVMDisasmContextRef disasm = LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
81 ac_get_llvm_processor_name(program->family),
82 features,
83 &symbols, 0, NULL, NULL);
84
85 char outline[1024];
86 size_t pos = 0;
87 bool invalid = false;
88 unsigned next_block = 0;
89 while (pos < exec_size) {
90 while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
91 if (referenced_blocks[next_block])
92 out << "BB" << std::dec << next_block << ":" << std::endl;
93 next_block++;
94 }
95
96 /* mask out src2 on v_writelane_b32 */
97 if (((program->chip_class == GFX8 || program->chip_class == GFX9) && (binary[pos] & 0xffff8000) == 0xd28a0000) ||
98 (program->chip_class == GFX10 && (binary[pos] & 0xffff8000) == 0xd7610000)) {
99 binary[pos+1] = binary[pos+1] & 0xF803FFFF;
100 }
101
102 size_t l = LLVMDisasmInstruction(disasm, (uint8_t *) &binary[pos],
103 (exec_size - pos) * sizeof(uint32_t), pos * 4,
104 outline, sizeof(outline));
105
106 size_t new_pos;
107 const int align_width = 60;
108 if (!l && program->chip_class == GFX9 && ((binary[pos] & 0xffff8000) == 0xd1348000)) { /* not actually an invalid instruction */
109 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tv_add_u32_e64 + clamp";
110 new_pos = pos + 2;
111 } else if (!l) {
112 out << std::left << std::setw(align_width) << std::setfill(' ') << "(invalid instruction)";
113 new_pos = pos + 1;
114 invalid = true;
115 } else {
116 out << std::left << std::setw(align_width) << std::setfill(' ') << outline;
117 assert(l % 4 == 0);
118 new_pos = pos + l / 4;
119 }
120 out << std::right;
121
122 out << " ;";
123 for (; pos < new_pos; pos++)
124 out << " " << std::setfill('0') << std::setw(8) << std::hex << binary[pos];
125 out << std::endl;
126 }
127 out << std::setfill(' ') << std::setw(0) << std::dec;
128 assert(next_block == program->blocks.size());
129
130 LLVMDisasmDispose(disasm);
131
132 if (program->constant_data.size()) {
133 out << std::endl << "/* constant data */" << std::endl;
134 for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
135 out << '[' << std::setw(6) << std::setfill('0') << std::dec << i << ']';
136 unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
137 for (unsigned j = 0; j < line_size; j += 4) {
138 unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
139 uint32_t v = 0;
140 memcpy(&v, &program->constant_data[i + j], size);
141 out << " " << std::setw(8) << std::setfill('0') << std::hex << v;
142 }
143 out << std::endl;
144 }
145 }
146
147 out << std::setfill(' ') << std::setw(0) << std::dec;
148
149 if (invalid) {
150 /* Invalid instructions usually lead to GPU hangs, which can make
151 * getting the actual invalid instruction hard. Abort here so that we
152 * can find the problem.
153 */
154 abort();
155 }
156 }
157
158 }