aco: fix disassembly of writelane instructions.
[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 void print_asm(Program *program, std::vector<uint32_t>& binary,
12 unsigned exec_size, std::ostream& out)
13 {
14 std::vector<bool> referenced_blocks(program->blocks.size());
15 referenced_blocks[0] = true;
16 for (Block& block : program->blocks) {
17 for (unsigned succ : block.linear_succs)
18 referenced_blocks[succ] = true;
19 }
20
21 std::vector<std::tuple<uint64_t, llvm::StringRef, uint8_t>> symbols;
22 std::vector<std::array<char,16>> block_names;
23 block_names.reserve(program->blocks.size());
24 for (Block& block : program->blocks) {
25 if (!referenced_blocks[block.index])
26 continue;
27 std::array<char, 16> name;
28 sprintf(name.data(), "BB%u", block.index);
29 block_names.push_back(name);
30 symbols.emplace_back(block.offset * 4, llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
31 }
32
33 const char *features = "";
34 if (program->chip_class >= GFX10 && program->wave_size == 64) {
35 features = "+wavefrontsize64";
36 }
37
38 LLVMDisasmContextRef disasm = LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
39 ac_get_llvm_processor_name(program->family),
40 features,
41 &symbols, 0, NULL, NULL);
42
43 char outline[1024];
44 size_t pos = 0;
45 bool invalid = false;
46 unsigned next_block = 0;
47 while (pos < exec_size) {
48 while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
49 if (referenced_blocks[next_block])
50 out << "BB" << std::dec << next_block << ":" << std::endl;
51 next_block++;
52 }
53
54 /* mask out src2 on v_writelane_b32 */
55 if (((program->chip_class == GFX8 || program->chip_class == GFX9) && (binary[pos] & 0xffff8000) == 0xd28a0000) ||
56 (program->chip_class == GFX10 && (binary[pos] & 0xffff8000) == 0xd7610000)) {
57 binary[pos+1] = binary[pos+1] & 0xF803FFFF;
58 }
59
60 size_t l = LLVMDisasmInstruction(disasm, (uint8_t *) &binary[pos],
61 (exec_size - pos) * sizeof(uint32_t), pos * 4,
62 outline, sizeof(outline));
63
64 size_t new_pos;
65 const int align_width = 60;
66 if (!l && program->chip_class == GFX9 && ((binary[pos] & 0xffff8000) == 0xd1348000)) { /* not actually an invalid instruction */
67 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tv_add_u32_e64 + clamp";
68 new_pos = pos + 2;
69 } else if (!l) {
70 out << std::left << std::setw(align_width) << std::setfill(' ') << "(invalid instruction)";
71 new_pos = pos + 1;
72 invalid = true;
73 } else {
74 out << std::left << std::setw(align_width) << std::setfill(' ') << outline;
75 assert(l % 4 == 0);
76 new_pos = pos + l / 4;
77 }
78 out << std::right;
79
80 out << " ;";
81 for (; pos < new_pos; pos++)
82 out << " " << std::setfill('0') << std::setw(8) << std::hex << binary[pos];
83 out << std::endl;
84 }
85 out << std::setfill(' ') << std::setw(0) << std::dec;
86 assert(next_block == program->blocks.size());
87
88 LLVMDisasmDispose(disasm);
89
90 if (program->constant_data.size()) {
91 out << std::endl << "/* constant data */" << std::endl;
92 for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
93 out << '[' << std::setw(6) << std::setfill('0') << std::dec << i << ']';
94 unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
95 for (unsigned j = 0; j < line_size; j += 4) {
96 unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
97 uint32_t v = 0;
98 memcpy(&v, &program->constant_data[i + j], size);
99 out << " " << std::setw(8) << std::setfill('0') << std::hex << v;
100 }
101 out << std::endl;
102 }
103 }
104
105 out << std::setfill(' ') << std::setw(0) << std::dec;
106
107 if (invalid) {
108 /* Invalid instructions usually lead to GPU hangs, which can make
109 * getting the actual invalid instruction hard. Abort here so that we
110 * can find the problem.
111 */
112 abort();
113 }
114 }
115
116 }