aco: Remember VS/TCS output driver locations.
[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 &&
141 ((program->chip_class >= GFX9 && (binary[pos] & 0xffff8000) == 0xd1348000) || /* v_add_u32_e64 + clamp */
142 (program->chip_class >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
143 (program->chip_class <= GFX9 && (binary[pos] & 0xffff8000) == 0xd1268000)) /* v_add_u16_e64 + clamp */) {
144 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tinteger addition + clamp";
145 bool has_literal = program->chip_class >= GFX10 &&
146 (((binary[pos+1] & 0x1ff) == 0xff) || (((binary[pos+1] >> 9) & 0x1ff) == 0xff));
147 new_pos = pos + 2 + has_literal;
148 } else if (program->chip_class == GFX10 && l == 4 && ((binary[pos] & 0xfe0001ff) == 0x020000f9)) {
149 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tv_cndmask_b32 + sdwa";
150 new_pos = pos + 2;
151 } else if (!l) {
152 out << std::left << std::setw(align_width) << std::setfill(' ') << "(invalid instruction)";
153 new_pos = pos + 1;
154 invalid = true;
155 } else {
156 out << std::left << std::setw(align_width) << std::setfill(' ') << outline;
157 assert(l % 4 == 0);
158 new_pos = pos + l / 4;
159 }
160 out << std::right;
161
162 out << " ;";
163 for (; pos < new_pos; pos++)
164 out << " " << std::setfill('0') << std::setw(8) << std::hex << binary[pos];
165 out << std::endl;
166 }
167 out << std::setfill(' ') << std::setw(0) << std::dec;
168 assert(next_block == program->blocks.size());
169
170 LLVMDisasmDispose(disasm);
171
172 if (program->constant_data.size()) {
173 out << std::endl << "/* constant data */" << std::endl;
174 for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
175 out << '[' << std::setw(6) << std::setfill('0') << std::dec << i << ']';
176 unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
177 for (unsigned j = 0; j < line_size; j += 4) {
178 unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
179 uint32_t v = 0;
180 memcpy(&v, &program->constant_data[i + j], size);
181 out << " " << std::setw(8) << std::setfill('0') << std::hex << v;
182 }
183 out << std::endl;
184 }
185 }
186
187 out << std::setfill(' ') << std::setw(0) << std::dec;
188
189 if (invalid) {
190 /* Invalid instructions usually lead to GPU hangs, which can make
191 * getting the actual invalid instruction hard. Abort here so that we
192 * can find the problem.
193 */
194 abort();
195 }
196 }
197
198 }