4 #include "llvm-c/Disassembler.h"
5 #include "ac_llvm_util.h"
7 #include <llvm/ADT/StringRef.h>
11 void print_asm(Program
*program
, std::vector
<uint32_t>& binary
,
12 unsigned exec_size
, std::ostream
& out
)
14 if (program
->chip_class
<= GFX7
) {
15 out
<< "Disassembly for this GPU currently not supported." << std::endl
;
18 std::vector
<bool> referenced_blocks(program
->blocks
.size());
19 referenced_blocks
[0] = true;
20 for (Block
& block
: program
->blocks
) {
21 for (unsigned succ
: block
.linear_succs
)
22 referenced_blocks
[succ
] = true;
25 std::vector
<std::tuple
<uint64_t, llvm::StringRef
, uint8_t>> symbols
;
26 std::vector
<std::array
<char,16>> block_names
;
27 block_names
.reserve(program
->blocks
.size());
28 for (Block
& block
: program
->blocks
) {
29 if (!referenced_blocks
[block
.index
])
31 std::array
<char, 16> name
;
32 sprintf(name
.data(), "BB%u", block
.index
);
33 block_names
.push_back(name
);
34 symbols
.emplace_back(block
.offset
* 4, llvm::StringRef(block_names
[block_names
.size() - 1].data()), 0);
37 const char *features
= "";
38 if (program
->chip_class
>= GFX10
&& program
->wave_size
== 64) {
39 features
= "+wavefrontsize64";
42 LLVMDisasmContextRef disasm
= LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
43 ac_get_llvm_processor_name(program
->family
),
45 &symbols
, 0, NULL
, NULL
);
50 unsigned next_block
= 0;
51 while (pos
< exec_size
) {
52 while (next_block
< program
->blocks
.size() && pos
== program
->blocks
[next_block
].offset
) {
53 if (referenced_blocks
[next_block
])
54 out
<< "BB" << std::dec
<< next_block
<< ":" << std::endl
;
58 /* mask out src2 on v_writelane_b32 */
59 if (((program
->chip_class
== GFX8
|| program
->chip_class
== GFX9
) && (binary
[pos
] & 0xffff8000) == 0xd28a0000) ||
60 (program
->chip_class
== GFX10
&& (binary
[pos
] & 0xffff8000) == 0xd7610000)) {
61 binary
[pos
+1] = binary
[pos
+1] & 0xF803FFFF;
64 size_t l
= LLVMDisasmInstruction(disasm
, (uint8_t *) &binary
[pos
],
65 (exec_size
- pos
) * sizeof(uint32_t), pos
* 4,
66 outline
, sizeof(outline
));
69 const int align_width
= 60;
70 if (!l
&& program
->chip_class
== GFX9
&& ((binary
[pos
] & 0xffff8000) == 0xd1348000)) { /* not actually an invalid instruction */
71 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "\tv_add_u32_e64 + clamp";
74 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "(invalid instruction)";
78 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << outline
;
80 new_pos
= pos
+ l
/ 4;
85 for (; pos
< new_pos
; pos
++)
86 out
<< " " << std::setfill('0') << std::setw(8) << std::hex
<< binary
[pos
];
89 out
<< std::setfill(' ') << std::setw(0) << std::dec
;
90 assert(next_block
== program
->blocks
.size());
92 LLVMDisasmDispose(disasm
);
94 if (program
->constant_data
.size()) {
95 out
<< std::endl
<< "/* constant data */" << std::endl
;
96 for (unsigned i
= 0; i
< program
->constant_data
.size(); i
+= 32) {
97 out
<< '[' << std::setw(6) << std::setfill('0') << std::dec
<< i
<< ']';
98 unsigned line_size
= std::min
<size_t>(program
->constant_data
.size() - i
, 32);
99 for (unsigned j
= 0; j
< line_size
; j
+= 4) {
100 unsigned size
= std::min
<size_t>(program
->constant_data
.size() - (i
+ j
), 4);
102 memcpy(&v
, &program
->constant_data
[i
+ j
], size
);
103 out
<< " " << std::setw(8) << std::setfill('0') << std::hex
<< v
;
109 out
<< std::setfill(' ') << std::setw(0) << std::dec
;
112 /* Invalid instructions usually lead to GPU hangs, which can make
113 * getting the actual invalid instruction hard. Abort here so that we
114 * can find the problem.