4 #include "llvm-c/Disassembler.h"
5 #include "ac_llvm_util.h"
7 #include <llvm/ADT/StringRef.h>
8 #if LLVM_VERSION_MAJOR >= 11
9 #include <llvm/MC/MCDisassembler/MCDisassembler.h>
14 /* LLVM disassembler only supports GFX8+, try to disassemble with CLRXdisasm
15 * for GFX6-GFX7 if found on the system, this is better than nothing.
17 void print_asm_gfx6_gfx7(Program
*program
, std::vector
<uint32_t>& binary
,
20 char path
[] = "/tmp/fileXXXXXX";
21 char line
[2048], command
[128];
26 /* Dump the binary into a temporary file. */
31 for (uint32_t w
: binary
)
33 if (write(fd
, &w
, sizeof(w
)) == -1)
37 /* Determine the GPU type for CLRXdisasm. Use the family for GFX6 chips
38 * because it doesn't allow to use gfx600 directly.
40 switch (program
->chip_class
) {
42 switch (program
->family
) {
47 gpu_type
= "pitcairn";
50 gpu_type
= "capeverde";
59 unreachable("Invalid GFX6 family!");
66 unreachable("Invalid chip class!");
69 sprintf(command
, "clrxdisasm --gpuType=%s -r %s", gpu_type
, path
);
71 p
= popen(command
, "r");
73 while (fgets(line
, sizeof(line
), p
))
83 void print_asm(Program
*program
, std::vector
<uint32_t>& binary
,
84 unsigned exec_size
, std::ostream
& out
)
86 if (program
->chip_class
<= GFX7
) {
87 print_asm_gfx6_gfx7(program
, binary
, out
);
91 std::vector
<bool> referenced_blocks(program
->blocks
.size());
92 referenced_blocks
[0] = true;
93 for (Block
& block
: program
->blocks
) {
94 for (unsigned succ
: block
.linear_succs
)
95 referenced_blocks
[succ
] = true;
98 #if LLVM_VERSION_MAJOR >= 11
99 std::vector
<llvm::SymbolInfoTy
> symbols
;
101 std::vector
<std::tuple
<uint64_t, llvm::StringRef
, uint8_t>> symbols
;
103 std::vector
<std::array
<char,16>> block_names
;
104 block_names
.reserve(program
->blocks
.size());
105 for (Block
& block
: program
->blocks
) {
106 if (!referenced_blocks
[block
.index
])
108 std::array
<char, 16> name
;
109 sprintf(name
.data(), "BB%u", block
.index
);
110 block_names
.push_back(name
);
111 symbols
.emplace_back(block
.offset
* 4, llvm::StringRef(block_names
[block_names
.size() - 1].data()), 0);
114 const char *features
= "";
115 if (program
->chip_class
>= GFX10
&& program
->wave_size
== 64) {
116 features
= "+wavefrontsize64";
119 LLVMDisasmContextRef disasm
= LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
120 ac_get_llvm_processor_name(program
->family
),
122 &symbols
, 0, NULL
, NULL
);
126 bool invalid
= false;
127 unsigned next_block
= 0;
128 while (pos
< exec_size
) {
129 while (next_block
< program
->blocks
.size() && pos
== program
->blocks
[next_block
].offset
) {
130 if (referenced_blocks
[next_block
])
131 out
<< "BB" << std::dec
<< next_block
<< ":" << std::endl
;
135 /* mask out src2 on v_writelane_b32 */
136 if (((program
->chip_class
== GFX8
|| program
->chip_class
== GFX9
) && (binary
[pos
] & 0xffff8000) == 0xd28a0000) ||
137 (program
->chip_class
== GFX10
&& (binary
[pos
] & 0xffff8000) == 0xd7610000)) {
138 binary
[pos
+1] = binary
[pos
+1] & 0xF803FFFF;
141 size_t l
= LLVMDisasmInstruction(disasm
, (uint8_t *) &binary
[pos
],
142 (exec_size
- pos
) * sizeof(uint32_t), pos
* 4,
143 outline
, sizeof(outline
));
146 const int align_width
= 60;
148 ((program
->chip_class
>= GFX9
&& (binary
[pos
] & 0xffff8000) == 0xd1348000) || /* v_add_u32_e64 + clamp */
149 (program
->chip_class
>= GFX10
&& (binary
[pos
] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
150 (program
->chip_class
<= GFX9
&& (binary
[pos
] & 0xffff8000) == 0xd1268000)) /* v_add_u16_e64 + clamp */) {
151 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "\tinteger addition + clamp";
152 bool has_literal
= program
->chip_class
>= GFX10
&&
153 (((binary
[pos
+1] & 0x1ff) == 0xff) || (((binary
[pos
+1] >> 9) & 0x1ff) == 0xff));
154 new_pos
= pos
+ 2 + has_literal
;
155 } else if (program
->chip_class
== GFX10
&& l
== 4 && ((binary
[pos
] & 0xfe0001ff) == 0x020000f9)) {
156 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "\tv_cndmask_b32 + sdwa";
159 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "(invalid instruction)";
163 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << outline
;
165 new_pos
= pos
+ l
/ 4;
170 for (; pos
< new_pos
; pos
++)
171 out
<< " " << std::setfill('0') << std::setw(8) << std::hex
<< binary
[pos
];
174 out
<< std::setfill(' ') << std::setw(0) << std::dec
;
175 assert(next_block
== program
->blocks
.size());
177 LLVMDisasmDispose(disasm
);
179 if (program
->constant_data
.size()) {
180 out
<< std::endl
<< "/* constant data */" << std::endl
;
181 for (unsigned i
= 0; i
< program
->constant_data
.size(); i
+= 32) {
182 out
<< '[' << std::setw(6) << std::setfill('0') << std::dec
<< i
<< ']';
183 unsigned line_size
= std::min
<size_t>(program
->constant_data
.size() - i
, 32);
184 for (unsigned j
= 0; j
< line_size
; j
+= 4) {
185 unsigned size
= std::min
<size_t>(program
->constant_data
.size() - (i
+ j
), 4);
187 memcpy(&v
, &program
->constant_data
[i
+ j
], size
);
188 out
<< " " << std::setw(8) << std::setfill('0') << std::hex
<< v
;
194 out
<< std::setfill(' ') << std::setw(0) << std::dec
;
197 /* Invalid instructions usually lead to GPU hangs, which can make
198 * getting the actual invalid instruction hard. Abort here so that we
199 * can find the problem.