aco: fix printing ASM on GFX6-7 if clrxdisasm is not found
[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 #if LLVM_VERSION_MAJOR >= 11
9 #include <llvm/MC/MCDisassembler/MCDisassembler.h>
10 #endif
11
12 namespace aco {
13
14 /* LLVM disassembler only supports GFX8+, try to disassemble with CLRXdisasm
15 * for GFX6-GFX7 if found on the system, this is better than nothing.
16 */
17 void print_asm_gfx6_gfx7(Program *program, std::vector<uint32_t>& binary,
18 std::ostream& out)
19 {
20 char path[] = "/tmp/fileXXXXXX";
21 char line[2048], command[128];
22 const char *gpu_type;
23 FILE *p;
24 int fd;
25
26 /* Dump the binary into a temporary file. */
27 fd = mkstemp(path);
28 if (fd < 0)
29 return;
30
31 for (uint32_t w : binary)
32 {
33 if (write(fd, &w, sizeof(w)) == -1)
34 goto fail;
35 }
36
37 /* Determine the GPU type for CLRXdisasm. Use the family for GFX6 chips
38 * because it doesn't allow to use gfx600 directly.
39 */
40 switch (program->chip_class) {
41 case GFX6:
42 switch (program->family) {
43 case CHIP_TAHITI:
44 gpu_type = "tahiti";
45 break;
46 case CHIP_PITCAIRN:
47 gpu_type = "pitcairn";
48 break;
49 case CHIP_VERDE:
50 gpu_type = "capeverde";
51 break;
52 case CHIP_OLAND:
53 gpu_type = "oland";
54 break;
55 case CHIP_HAINAN:
56 gpu_type = "hainan";
57 break;
58 default:
59 unreachable("Invalid GFX6 family!");
60 }
61 break;
62 case GFX7:
63 gpu_type = "gfx700";
64 break;
65 default:
66 unreachable("Invalid chip class!");
67 }
68
69 sprintf(command, "clrxdisasm --gpuType=%s -r %s", gpu_type, path);
70
71 p = popen(command, "r");
72 if (!p || errno == ENOENT) {
73 out << "clrxdisasm not found\n";
74 } else {
75 while (fgets(line, sizeof(line), p))
76 out << line;
77 pclose(p);
78 }
79
80 fail:
81 close(fd);
82 unlink(path);
83 }
84
85 void print_asm(Program *program, std::vector<uint32_t>& binary,
86 unsigned exec_size, std::ostream& out)
87 {
88 if (program->chip_class <= GFX7) {
89 print_asm_gfx6_gfx7(program, binary, out);
90 return;
91 }
92
93 std::vector<bool> referenced_blocks(program->blocks.size());
94 referenced_blocks[0] = true;
95 for (Block& block : program->blocks) {
96 for (unsigned succ : block.linear_succs)
97 referenced_blocks[succ] = true;
98 }
99
100 #if LLVM_VERSION_MAJOR >= 11
101 std::vector<llvm::SymbolInfoTy> symbols;
102 #else
103 std::vector<std::tuple<uint64_t, llvm::StringRef, uint8_t>> symbols;
104 #endif
105 std::vector<std::array<char,16>> block_names;
106 block_names.reserve(program->blocks.size());
107 for (Block& block : program->blocks) {
108 if (!referenced_blocks[block.index])
109 continue;
110 std::array<char, 16> name;
111 sprintf(name.data(), "BB%u", block.index);
112 block_names.push_back(name);
113 symbols.emplace_back(block.offset * 4, llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
114 }
115
116 const char *features = "";
117 if (program->chip_class >= GFX10 && program->wave_size == 64) {
118 features = "+wavefrontsize64";
119 }
120
121 LLVMDisasmContextRef disasm = LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
122 ac_get_llvm_processor_name(program->family),
123 features,
124 &symbols, 0, NULL, NULL);
125
126 char outline[1024];
127 size_t pos = 0;
128 bool invalid = false;
129 unsigned next_block = 0;
130 while (pos < exec_size) {
131 while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
132 if (referenced_blocks[next_block])
133 out << "BB" << std::dec << next_block << ":" << std::endl;
134 next_block++;
135 }
136
137 /* mask out src2 on v_writelane_b32 */
138 if (((program->chip_class == GFX8 || program->chip_class == GFX9) && (binary[pos] & 0xffff8000) == 0xd28a0000) ||
139 (program->chip_class >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7610000)) {
140 binary[pos+1] = binary[pos+1] & 0xF803FFFF;
141 }
142
143 size_t l = LLVMDisasmInstruction(disasm, (uint8_t *) &binary[pos],
144 (exec_size - pos) * sizeof(uint32_t), pos * 4,
145 outline, sizeof(outline));
146
147 size_t new_pos;
148 const int align_width = 60;
149 if (!l &&
150 ((program->chip_class >= GFX9 && (binary[pos] & 0xffff8000) == 0xd1348000) || /* v_add_u32_e64 + clamp */
151 (program->chip_class >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
152 (program->chip_class <= GFX9 && (binary[pos] & 0xffff8000) == 0xd1268000)) /* v_add_u16_e64 + clamp */) {
153 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tinteger addition + clamp";
154 bool has_literal = program->chip_class >= GFX10 &&
155 (((binary[pos+1] & 0x1ff) == 0xff) || (((binary[pos+1] >> 9) & 0x1ff) == 0xff));
156 new_pos = pos + 2 + has_literal;
157 } else if (program->chip_class >= GFX10 && l == 4 && ((binary[pos] & 0xfe0001ff) == 0x020000f9)) {
158 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tv_cndmask_b32 + sdwa";
159 new_pos = pos + 2;
160 } else if (!l) {
161 out << std::left << std::setw(align_width) << std::setfill(' ') << "(invalid instruction)";
162 new_pos = pos + 1;
163 invalid = true;
164 } else {
165 out << std::left << std::setw(align_width) << std::setfill(' ') << outline;
166 assert(l % 4 == 0);
167 new_pos = pos + l / 4;
168 }
169 out << std::right;
170
171 out << " ;";
172 for (; pos < new_pos; pos++)
173 out << " " << std::setfill('0') << std::setw(8) << std::hex << binary[pos];
174 out << std::endl;
175 }
176 out << std::setfill(' ') << std::setw(0) << std::dec;
177 assert(next_block == program->blocks.size());
178
179 LLVMDisasmDispose(disasm);
180
181 if (program->constant_data.size()) {
182 out << std::endl << "/* constant data */" << std::endl;
183 for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
184 out << '[' << std::setw(6) << std::setfill('0') << std::dec << i << ']';
185 unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
186 for (unsigned j = 0; j < line_size; j += 4) {
187 unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
188 uint32_t v = 0;
189 memcpy(&v, &program->constant_data[i + j], size);
190 out << " " << std::setw(8) << std::setfill('0') << std::hex << v;
191 }
192 out << std::endl;
193 }
194 }
195
196 out << std::setfill(' ') << std::setw(0) << std::dec;
197
198 if (invalid) {
199 /* Invalid instructions usually lead to GPU hangs, which can make
200 * getting the actual invalid instruction hard. Abort here so that we
201 * can find the problem.
202 */
203 abort();
204 }
205 }
206
207 }