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