Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aco_print_asm.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <array>
  2. #include <iomanip>
  3. #include "aco_ir.h"
  4. #include "llvm-c/Disassembler.h"
  5. #include "ac_llvm_util.h"
  6. #include <llvm/ADT/StringRef.h>
  7. namespace aco {
  8. void print_asm(Program *program, std::vector<uint32_t>& binary,
  9. unsigned exec_size, std::ostream& out)
  10. {
  11. std::vector<bool> referenced_blocks(program->blocks.size());
  12. referenced_blocks[0] = true;
  13. for (Block& block : program->blocks) {
  14. for (unsigned succ : block.linear_succs)
  15. referenced_blocks[succ] = true;
  16. }
  17. std::vector<std::tuple<uint64_t, llvm::StringRef, uint8_t>> symbols;
  18. std::vector<std::array<char,16>> block_names;
  19. block_names.reserve(program->blocks.size());
  20. for (Block& block : program->blocks) {
  21. if (!referenced_blocks[block.index])
  22. continue;
  23. std::array<char, 16> name;
  24. sprintf(name.data(), "BB%u", block.index);
  25. block_names.push_back(name);
  26. symbols.emplace_back(block.offset * 4, llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
  27. }
  28. const char *features = "";
  29. if (program->chip_class >= GFX10 && program->wave_size == 64) {
  30. features = "+wavefrontsize64";
  31. }
  32. LLVMDisasmContextRef disasm = LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d",
  33. ac_get_llvm_processor_name(program->family),
  34. features,
  35. &symbols, 0, NULL, NULL);
  36. char outline[1024];
  37. size_t pos = 0;
  38. bool invalid = false;
  39. unsigned next_block = 0;
  40. while (pos < exec_size) {
  41. while (next_block < program->blocks.size() && pos == program->blocks[next_block].offset) {
  42. if (referenced_blocks[next_block])
  43. out << "BB" << std::dec << next_block << ":" << std::endl;
  44. next_block++;
  45. }
  46. size_t l = LLVMDisasmInstruction(disasm, (uint8_t *) &binary[pos],
  47. (exec_size - pos) * sizeof(uint32_t), pos * 4,
  48. outline, sizeof(outline));
  49. size_t new_pos;
  50. const int align_width = 60;
  51. if (program->chip_class == GFX9 && !l && ((binary[pos] & 0xffff8000) == 0xd1348000)) { /* not actually an invalid instruction */
  52. out << std::left << std::setw(align_width) << std::setfill(' ') << "\tv_add_u32_e64 + clamp";
  53. new_pos = pos + 2;
  54. } else if (!l) {
  55. out << std::left << std::setw(align_width) << std::setfill(' ') << "(invalid instruction)";
  56. new_pos = pos + 1;
  57. invalid = true;
  58. } else {
  59. out << std::left << std::setw(align_width) << std::setfill(' ') << outline;
  60. assert(l % 4 == 0);
  61. new_pos = pos + l / 4;
  62. }
  63. out << std::right;
  64. out << " ;";
  65. for (; pos < new_pos; pos++)
  66. out << " " << std::setfill('0') << std::setw(8) << std::hex << binary[pos];
  67. out << std::endl;
  68. }
  69. out << std::setfill(' ') << std::setw(0) << std::dec;
  70. assert(next_block == program->blocks.size());
  71. LLVMDisasmDispose(disasm);
  72. if (program->constant_data.size()) {
  73. out << std::endl << "/* constant data */" << std::endl;
  74. for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
  75. out << '[' << std::setw(6) << std::setfill('0') << std::dec << i << ']';
  76. unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
  77. for (unsigned j = 0; j < line_size; j += 4) {
  78. unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
  79. uint32_t v = 0;
  80. memcpy(&v, &program->constant_data[i + j], size);
  81. out << " " << std::setw(8) << std::setfill('0') << std::hex << v;
  82. }
  83. out << std::endl;
  84. }
  85. }
  86. out << std::setfill(' ') << std::setw(0) << std::dec;
  87. if (invalid) {
  88. /* Invalid instructions usually lead to GPU hangs, which can make
  89. * getting the actual invalid instruction hard. Abort here so that we
  90. * can find the problem.
  91. */
  92. abort();
  93. }
  94. }
  95. }