4 #include "llvm-c/Disassembler.h"
5 #include "ac_llvm_util.h"
7 #include <llvm/ADT/StringRef.h>
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.
14 void print_asm_gfx6_gfx7(Program
*program
, std::vector
<uint32_t>& binary
,
17 char path
[] = "/tmp/fileXXXXXX";
18 char line
[2048], command
[128];
22 /* Dump the binary into a temporary file. */
27 for (uint32_t w
: binary
)
29 if (write(fd
, &w
, sizeof(w
)) == -1)
33 sprintf(command
, "clrxdisasm --gpuType=%s -r %s",
34 program
->chip_class
== GFX6
? "gfx600" : "gfx700", path
);
36 p
= popen(command
, "r");
38 while (fgets(line
, sizeof(line
), p
))
48 void print_asm(Program
*program
, std::vector
<uint32_t>& binary
,
49 unsigned exec_size
, std::ostream
& out
)
51 if (program
->chip_class
<= GFX7
) {
52 print_asm_gfx6_gfx7(program
, binary
, out
);
56 std::vector
<bool> referenced_blocks(program
->blocks
.size());
57 referenced_blocks
[0] = true;
58 for (Block
& block
: program
->blocks
) {
59 for (unsigned succ
: block
.linear_succs
)
60 referenced_blocks
[succ
] = true;
63 std::vector
<std::tuple
<uint64_t, llvm::StringRef
, uint8_t>> symbols
;
64 std::vector
<std::array
<char,16>> block_names
;
65 block_names
.reserve(program
->blocks
.size());
66 for (Block
& block
: program
->blocks
) {
67 if (!referenced_blocks
[block
.index
])
69 std::array
<char, 16> name
;
70 sprintf(name
.data(), "BB%u", block
.index
);
71 block_names
.push_back(name
);
72 symbols
.emplace_back(block
.offset
* 4, llvm::StringRef(block_names
[block_names
.size() - 1].data()), 0);
75 const char *features
= "";
76 if (program
->chip_class
>= GFX10
&& program
->wave_size
== 64) {
77 features
= "+wavefrontsize64";
80 LLVMDisasmContextRef disasm
= LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
81 ac_get_llvm_processor_name(program
->family
),
83 &symbols
, 0, NULL
, NULL
);
88 unsigned next_block
= 0;
89 while (pos
< exec_size
) {
90 while (next_block
< program
->blocks
.size() && pos
== program
->blocks
[next_block
].offset
) {
91 if (referenced_blocks
[next_block
])
92 out
<< "BB" << std::dec
<< next_block
<< ":" << std::endl
;
96 /* mask out src2 on v_writelane_b32 */
97 if (((program
->chip_class
== GFX8
|| program
->chip_class
== GFX9
) && (binary
[pos
] & 0xffff8000) == 0xd28a0000) ||
98 (program
->chip_class
== GFX10
&& (binary
[pos
] & 0xffff8000) == 0xd7610000)) {
99 binary
[pos
+1] = binary
[pos
+1] & 0xF803FFFF;
102 size_t l
= LLVMDisasmInstruction(disasm
, (uint8_t *) &binary
[pos
],
103 (exec_size
- pos
) * sizeof(uint32_t), pos
* 4,
104 outline
, sizeof(outline
));
107 const int align_width
= 60;
108 if (!l
&& program
->chip_class
== GFX9
&& ((binary
[pos
] & 0xffff8000) == 0xd1348000)) { /* not actually an invalid instruction */
109 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "\tv_add_u32_e64 + clamp";
112 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << "(invalid instruction)";
116 out
<< std::left
<< std::setw(align_width
) << std::setfill(' ') << outline
;
118 new_pos
= pos
+ l
/ 4;
123 for (; pos
< new_pos
; pos
++)
124 out
<< " " << std::setfill('0') << std::setw(8) << std::hex
<< binary
[pos
];
127 out
<< std::setfill(' ') << std::setw(0) << std::dec
;
128 assert(next_block
== program
->blocks
.size());
130 LLVMDisasmDispose(disasm
);
132 if (program
->constant_data
.size()) {
133 out
<< std::endl
<< "/* constant data */" << std::endl
;
134 for (unsigned i
= 0; i
< program
->constant_data
.size(); i
+= 32) {
135 out
<< '[' << std::setw(6) << std::setfill('0') << std::dec
<< i
<< ']';
136 unsigned line_size
= std::min
<size_t>(program
->constant_data
.size() - i
, 32);
137 for (unsigned j
= 0; j
< line_size
; j
+= 4) {
138 unsigned size
= std::min
<size_t>(program
->constant_data
.size() - (i
+ j
), 4);
140 memcpy(&v
, &program
->constant_data
[i
+ j
], size
);
141 out
<< " " << std::setw(8) << std::setfill('0') << std::hex
<< v
;
147 out
<< std::setfill(' ') << std::setw(0) << std::dec
;
150 /* Invalid instructions usually lead to GPU hangs, which can make
151 * getting the actual invalid instruction hard. Abort here so that we
152 * can find the problem.