aco: Set GFX10 dimensionality on the instructions that need it.
[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 size_t l = LLVMDisasmInstruction(disasm, (uint8_t *) &binary[pos],
55 (exec_size - pos) * sizeof(uint32_t), pos * 4,
56 outline, sizeof(outline));
57
58 size_t new_pos;
59 const int align_width = 60;
60 if (program->chip_class == GFX9 && !l && ((binary[pos] & 0xffff8000) == 0xd1348000)) { /* not actually an invalid instruction */
61 out << std::left << std::setw(align_width) << std::setfill(' ') << "\tv_add_u32_e64 + clamp";
62 new_pos = pos + 2;
63 } else if (!l) {
64 out << std::left << std::setw(align_width) << std::setfill(' ') << "(invalid instruction)";
65 new_pos = pos + 1;
66 invalid = true;
67 } else {
68 out << std::left << std::setw(align_width) << std::setfill(' ') << outline;
69 assert(l % 4 == 0);
70 new_pos = pos + l / 4;
71 }
72 out << std::right;
73
74 out << " ;";
75 for (; pos < new_pos; pos++)
76 out << " " << std::setfill('0') << std::setw(8) << std::hex << binary[pos];
77 out << std::endl;
78 }
79 out << std::setfill(' ') << std::setw(0) << std::dec;
80 assert(next_block == program->blocks.size());
81
82 LLVMDisasmDispose(disasm);
83
84 if (program->constant_data.size()) {
85 out << std::endl << "/* constant data */" << std::endl;
86 for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
87 out << '[' << std::setw(6) << std::setfill('0') << std::dec << i << ']';
88 unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
89 for (unsigned j = 0; j < line_size; j += 4) {
90 unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
91 uint32_t v = 0;
92 memcpy(&v, &program->constant_data[i + j], size);
93 out << " " << std::setw(8) << std::setfill('0') << std::hex << v;
94 }
95 out << std::endl;
96 }
97 }
98
99 out << std::setfill(' ') << std::setw(0) << std::dec;
100
101 if (invalid) {
102 /* Invalid instructions usually lead to GPU hangs, which can make
103 * getting the actual invalid instruction hard. Abort here so that we
104 * can find the problem.
105 */
106 abort();
107 }
108 }
109
110 }