2021-06-09 17:54:53 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2018 Valve Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-06-09 15:40:03 +02:00
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
#include "aco_ir.h"
|
2021-06-10 11:33:15 +02:00
|
|
|
|
|
|
|
|
#include "llvm/ac_llvm_util.h"
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2021-06-09 15:40:03 +02:00
|
|
|
#include "llvm-c/Disassembler.h"
|
2019-09-17 13:22:17 +02:00
|
|
|
#include <llvm/ADT/StringRef.h>
|
2020-05-15 21:31:35 +01:00
|
|
|
#include <llvm/MC/MCDisassembler/MCDisassembler.h>
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2021-06-09 15:40:03 +02:00
|
|
|
#include <array>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
namespace aco {
|
2020-10-15 16:03:00 +01:00
|
|
|
namespace {
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2020-01-17 09:49:44 +01:00
|
|
|
/* LLVM disassembler only supports GFX8+, try to disassemble with CLRXdisasm
|
|
|
|
|
* for GFX6-GFX7 if found on the system, this is better than nothing.
|
|
|
|
|
*/
|
2020-10-02 11:06:50 +02:00
|
|
|
bool print_asm_gfx6_gfx7(Program *program, std::vector<uint32_t>& binary,
|
2020-10-15 16:38:13 +01:00
|
|
|
FILE *output)
|
2020-01-17 09:49:44 +01:00
|
|
|
{
|
2020-11-26 22:09:15 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
return true;
|
|
|
|
|
#else
|
2020-01-17 09:49:44 +01:00
|
|
|
char path[] = "/tmp/fileXXXXXX";
|
|
|
|
|
char line[2048], command[128];
|
2020-01-20 18:41:00 +01:00
|
|
|
const char *gpu_type;
|
2020-01-17 09:49:44 +01:00
|
|
|
FILE *p;
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
/* Dump the binary into a temporary file. */
|
|
|
|
|
fd = mkstemp(path);
|
|
|
|
|
if (fd < 0)
|
2020-10-02 11:06:50 +02:00
|
|
|
return true;
|
2020-01-17 09:49:44 +01:00
|
|
|
|
|
|
|
|
for (uint32_t w : binary)
|
|
|
|
|
{
|
|
|
|
|
if (write(fd, &w, sizeof(w)) == -1)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 18:41:00 +01:00
|
|
|
/* Determine the GPU type for CLRXdisasm. Use the family for GFX6 chips
|
|
|
|
|
* because it doesn't allow to use gfx600 directly.
|
|
|
|
|
*/
|
|
|
|
|
switch (program->chip_class) {
|
|
|
|
|
case GFX6:
|
|
|
|
|
switch (program->family) {
|
|
|
|
|
case CHIP_TAHITI:
|
|
|
|
|
gpu_type = "tahiti";
|
|
|
|
|
break;
|
|
|
|
|
case CHIP_PITCAIRN:
|
|
|
|
|
gpu_type = "pitcairn";
|
|
|
|
|
break;
|
|
|
|
|
case CHIP_VERDE:
|
|
|
|
|
gpu_type = "capeverde";
|
|
|
|
|
break;
|
|
|
|
|
case CHIP_OLAND:
|
|
|
|
|
gpu_type = "oland";
|
|
|
|
|
break;
|
|
|
|
|
case CHIP_HAINAN:
|
|
|
|
|
gpu_type = "hainan";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
unreachable("Invalid GFX6 family!");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GFX7:
|
|
|
|
|
gpu_type = "gfx700";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
unreachable("Invalid chip class!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sprintf(command, "clrxdisasm --gpuType=%s -r %s", gpu_type, path);
|
2020-01-17 09:49:44 +01:00
|
|
|
|
|
|
|
|
p = popen(command, "r");
|
2020-06-22 13:33:21 +02:00
|
|
|
if (p) {
|
|
|
|
|
if (!fgets(line, sizeof(line), p)) {
|
2020-10-15 16:38:13 +01:00
|
|
|
fprintf(output, "clrxdisasm not found\n");
|
2020-06-22 13:33:21 +02:00
|
|
|
pclose(p);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
2020-10-15 16:38:13 +01:00
|
|
|
fputs(line, output);
|
2020-06-22 13:33:21 +02:00
|
|
|
} while (fgets(line, sizeof(line), p));
|
|
|
|
|
|
2020-01-17 09:49:44 +01:00
|
|
|
pclose(p);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 11:06:50 +02:00
|
|
|
return false;
|
|
|
|
|
|
2020-01-17 09:49:44 +01:00
|
|
|
fail:
|
|
|
|
|
close(fd);
|
|
|
|
|
unlink(path);
|
2020-10-02 11:06:50 +02:00
|
|
|
return true;
|
2020-11-26 22:09:15 -08:00
|
|
|
#endif
|
2020-01-17 09:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-15 16:03:00 +01:00
|
|
|
std::pair<bool, size_t> disasm_instr(chip_class chip, LLVMDisasmContextRef disasm,
|
|
|
|
|
uint32_t *binary, unsigned exec_size, size_t pos,
|
2020-10-15 16:38:13 +01:00
|
|
|
char *outline, unsigned outline_size)
|
2020-10-15 16:03:00 +01:00
|
|
|
{
|
|
|
|
|
/* mask out src2 on v_writelane_b32 */
|
|
|
|
|
if (((chip == GFX8 || chip == GFX9) && (binary[pos] & 0xffff8000) == 0xd28a0000) ||
|
|
|
|
|
(chip >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7610000)) {
|
|
|
|
|
binary[pos+1] = binary[pos+1] & 0xF803FFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t l = LLVMDisasmInstruction(disasm, (uint8_t *) &binary[pos],
|
|
|
|
|
(exec_size - pos) * sizeof(uint32_t), pos * 4,
|
2020-10-15 16:38:13 +01:00
|
|
|
outline, outline_size);
|
2020-10-15 16:03:00 +01:00
|
|
|
|
|
|
|
|
if (chip >= GFX10 && l == 8 &&
|
|
|
|
|
((binary[pos] & 0xffff0000) == 0xd7610000) &&
|
|
|
|
|
((binary[pos + 1] & 0x1ff) == 0xff)) {
|
|
|
|
|
/* v_writelane with literal uses 3 dwords but llvm consumes only 2 */
|
|
|
|
|
l += 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool invalid = false;
|
|
|
|
|
size_t size;
|
|
|
|
|
if (!l &&
|
|
|
|
|
((chip >= GFX9 && (binary[pos] & 0xffff8000) == 0xd1348000) || /* v_add_u32_e64 + clamp */
|
|
|
|
|
(chip >= GFX10 && (binary[pos] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
|
|
|
|
|
(chip <= GFX9 && (binary[pos] & 0xffff8000) == 0xd1268000) || /* v_add_u16_e64 + clamp */
|
|
|
|
|
(chip >= GFX10 && (binary[pos] & 0xffff8000) == 0xd76d8000) || /* v_add3_u32 + clamp */
|
|
|
|
|
(chip == GFX9 && (binary[pos] & 0xffff8000) == 0xd1ff8000)) /* v_add3_u32 + clamp */) {
|
2020-10-15 16:38:13 +01:00
|
|
|
strcpy(outline, "\tinteger addition + clamp");
|
2020-10-15 16:03:00 +01:00
|
|
|
bool has_literal = chip >= GFX10 &&
|
|
|
|
|
(((binary[pos+1] & 0x1ff) == 0xff) || (((binary[pos+1] >> 9) & 0x1ff) == 0xff));
|
|
|
|
|
size = 2 + has_literal;
|
|
|
|
|
} else if (chip >= GFX10 && l == 4 && ((binary[pos] & 0xfe0001ff) == 0x020000f9)) {
|
2020-10-15 16:38:13 +01:00
|
|
|
strcpy(outline, "\tv_cndmask_b32 + sdwa");
|
2020-10-15 16:03:00 +01:00
|
|
|
size = 2;
|
|
|
|
|
} else if (!l) {
|
2020-10-15 16:38:13 +01:00
|
|
|
strcpy(outline, "(invalid instruction)");
|
2020-10-15 16:03:00 +01:00
|
|
|
size = 1;
|
|
|
|
|
invalid = true;
|
|
|
|
|
} else {
|
|
|
|
|
assert(l % 4 == 0);
|
|
|
|
|
size = l / 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::make_pair(invalid, size);
|
|
|
|
|
}
|
|
|
|
|
} /* end namespace */
|
|
|
|
|
|
2020-10-02 11:06:50 +02:00
|
|
|
bool print_asm(Program *program, std::vector<uint32_t>& binary,
|
2020-10-15 16:38:13 +01:00
|
|
|
unsigned exec_size, FILE *output)
|
2019-09-17 13:22:17 +02:00
|
|
|
{
|
2019-11-15 16:29:32 +01:00
|
|
|
if (program->chip_class <= GFX7) {
|
2020-10-02 11:06:50 +02:00
|
|
|
/* Do not abort if clrxdisasm isn't found. */
|
2020-10-15 16:38:13 +01:00
|
|
|
print_asm_gfx6_gfx7(program, binary, output);
|
2020-10-02 11:06:50 +02:00
|
|
|
return false;
|
2019-11-15 16:29:32 +01:00
|
|
|
}
|
2020-01-17 09:49:44 +01:00
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
std::vector<bool> referenced_blocks(program->blocks.size());
|
|
|
|
|
referenced_blocks[0] = true;
|
|
|
|
|
for (Block& block : program->blocks) {
|
|
|
|
|
for (unsigned succ : block.linear_succs)
|
|
|
|
|
referenced_blocks[succ] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 21:31:35 +01:00
|
|
|
std::vector<llvm::SymbolInfoTy> symbols;
|
2019-09-17 13:22:17 +02:00
|
|
|
std::vector<std::array<char,16>> block_names;
|
|
|
|
|
block_names.reserve(program->blocks.size());
|
|
|
|
|
for (Block& block : program->blocks) {
|
|
|
|
|
if (!referenced_blocks[block.index])
|
|
|
|
|
continue;
|
|
|
|
|
std::array<char, 16> name;
|
|
|
|
|
sprintf(name.data(), "BB%u", block.index);
|
|
|
|
|
block_names.push_back(name);
|
|
|
|
|
symbols.emplace_back(block.offset * 4, llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-21 17:58:08 +02:00
|
|
|
const char *features = "";
|
|
|
|
|
if (program->chip_class >= GFX10 && program->wave_size == 64) {
|
|
|
|
|
features = "+wavefrontsize64";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LLVMDisasmContextRef disasm = LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
|
|
|
|
|
ac_get_llvm_processor_name(program->family),
|
|
|
|
|
features,
|
|
|
|
|
&symbols, 0, NULL, NULL);
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
bool invalid = false;
|
|
|
|
|
unsigned next_block = 0;
|
2020-10-15 16:10:55 +01:00
|
|
|
|
|
|
|
|
unsigned prev_size = 0;
|
|
|
|
|
unsigned prev_pos = 0;
|
|
|
|
|
unsigned repeat_count = 0;
|
2019-09-17 13:22:17 +02:00
|
|
|
while (pos < exec_size) {
|
2020-10-15 16:10:55 +01:00
|
|
|
bool new_block = next_block < program->blocks.size() && pos == program->blocks[next_block].offset;
|
|
|
|
|
if (pos + prev_size <= exec_size && prev_pos != pos && !new_block &&
|
|
|
|
|
memcmp(&binary[prev_pos], &binary[pos], prev_size * 4) == 0) {
|
|
|
|
|
repeat_count++;
|
|
|
|
|
pos += prev_size;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
2020-10-15 16:38:13 +01:00
|
|
|
if (repeat_count)
|
|
|
|
|
fprintf(output, "\t(then repeated %u times)\n", repeat_count);
|
2020-10-15 16:10:55 +01:00
|
|
|
repeat_count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
|
|
|
|
|
if (referenced_blocks[next_block])
|
2020-10-15 16:38:13 +01:00
|
|
|
fprintf(output, "BB%u:\n", next_block);
|
2019-09-17 13:22:17 +02:00
|
|
|
next_block++;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 16:38:13 +01:00
|
|
|
char outline[1024];
|
2020-10-15 16:03:00 +01:00
|
|
|
std::pair<bool, size_t> res = disasm_instr(
|
2020-10-15 16:38:13 +01:00
|
|
|
program->chip_class, disasm, binary.data(), exec_size, pos, outline, sizeof(outline));
|
2020-10-15 16:03:00 +01:00
|
|
|
invalid |= res.first;
|
2020-09-07 20:20:50 +01:00
|
|
|
|
2020-10-15 16:38:13 +01:00
|
|
|
fprintf(output, "%-60s ;", outline);
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2020-10-15 16:10:55 +01:00
|
|
|
for (unsigned i = 0; i < res.second; i++)
|
2020-10-15 16:38:13 +01:00
|
|
|
fprintf(output, " %.8x", binary[pos + i]);
|
|
|
|
|
fputc('\n', output);
|
2020-08-05 14:59:01 +01:00
|
|
|
|
2020-10-15 16:10:55 +01:00
|
|
|
prev_size = res.second;
|
|
|
|
|
prev_pos = pos;
|
|
|
|
|
pos += res.second;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
assert(next_block == program->blocks.size());
|
|
|
|
|
|
|
|
|
|
LLVMDisasmDispose(disasm);
|
|
|
|
|
|
|
|
|
|
if (program->constant_data.size()) {
|
2020-10-15 16:38:13 +01:00
|
|
|
fputs("\n/* constant data */\n", output);
|
2019-09-17 13:22:17 +02:00
|
|
|
for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
|
2020-10-15 16:38:13 +01:00
|
|
|
fprintf(output, "[%.6u]", i);
|
2019-09-17 13:22:17 +02:00
|
|
|
unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
|
|
|
|
|
for (unsigned j = 0; j < line_size; j += 4) {
|
|
|
|
|
unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
|
|
|
|
|
uint32_t v = 0;
|
|
|
|
|
memcpy(&v, &program->constant_data[i + j], size);
|
2020-10-15 16:38:13 +01:00
|
|
|
fprintf(output, " %.8x", v);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
2020-10-15 16:38:13 +01:00
|
|
|
fputc('\n', output);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 11:06:50 +02:00
|
|
|
return invalid;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|