1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "aco_ir.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifdef LLVM_AVAILABLE
28bf215546Sopenharmony_ci#if defined(_MSC_VER) && defined(restrict)
29bf215546Sopenharmony_ci#undef restrict
30bf215546Sopenharmony_ci#endif
31bf215546Sopenharmony_ci#include "llvm/ac_llvm_util.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "llvm-c/Disassembler.h"
34bf215546Sopenharmony_ci#include <llvm/ADT/StringRef.h>
35bf215546Sopenharmony_ci#include <llvm/MC/MCDisassembler/MCDisassembler.h>
36bf215546Sopenharmony_ci#endif
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include <array>
39bf215546Sopenharmony_ci#include <iomanip>
40bf215546Sopenharmony_ci#include <vector>
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cinamespace aco {
43bf215546Sopenharmony_cinamespace {
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistd::vector<bool>
46bf215546Sopenharmony_ciget_referenced_blocks(Program* program)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   std::vector<bool> referenced_blocks(program->blocks.size());
49bf215546Sopenharmony_ci   referenced_blocks[0] = true;
50bf215546Sopenharmony_ci   for (Block& block : program->blocks) {
51bf215546Sopenharmony_ci      for (unsigned succ : block.linear_succs)
52bf215546Sopenharmony_ci         referenced_blocks[succ] = true;
53bf215546Sopenharmony_ci   }
54bf215546Sopenharmony_ci   return referenced_blocks;
55bf215546Sopenharmony_ci}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_civoid
58bf215546Sopenharmony_ciprint_block_markers(FILE* output, Program* program, const std::vector<bool>& referenced_blocks,
59bf215546Sopenharmony_ci                    unsigned* next_block, unsigned pos)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   while (*next_block < program->blocks.size() && pos == program->blocks[*next_block].offset) {
62bf215546Sopenharmony_ci      if (referenced_blocks[*next_block])
63bf215546Sopenharmony_ci         fprintf(output, "BB%u:\n", *next_block);
64bf215546Sopenharmony_ci      (*next_block)++;
65bf215546Sopenharmony_ci   }
66bf215546Sopenharmony_ci}
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_civoid
69bf215546Sopenharmony_ciprint_instr(FILE* output, const std::vector<uint32_t>& binary, char* instr, unsigned size,
70bf215546Sopenharmony_ci            unsigned pos)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   fprintf(output, "%-60s ;", instr);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   for (unsigned i = 0; i < size; i++)
75bf215546Sopenharmony_ci      fprintf(output, " %.8x", binary[pos + i]);
76bf215546Sopenharmony_ci   fputc('\n', output);
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_civoid
80bf215546Sopenharmony_ciprint_constant_data(FILE* output, Program* program)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   if (program->constant_data.empty())
83bf215546Sopenharmony_ci      return;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   fputs("\n/* constant data */\n", output);
86bf215546Sopenharmony_ci   for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
87bf215546Sopenharmony_ci      fprintf(output, "[%.6u]", i);
88bf215546Sopenharmony_ci      unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
89bf215546Sopenharmony_ci      for (unsigned j = 0; j < line_size; j += 4) {
90bf215546Sopenharmony_ci         unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
91bf215546Sopenharmony_ci         uint32_t v = 0;
92bf215546Sopenharmony_ci         memcpy(&v, &program->constant_data[i + j], size);
93bf215546Sopenharmony_ci         fprintf(output, " %.8x", v);
94bf215546Sopenharmony_ci      }
95bf215546Sopenharmony_ci      fputc('\n', output);
96bf215546Sopenharmony_ci   }
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci/**
100bf215546Sopenharmony_ci * Determines the GPU type to use for CLRXdisasm
101bf215546Sopenharmony_ci */
102bf215546Sopenharmony_ciconst char*
103bf215546Sopenharmony_cito_clrx_device_name(amd_gfx_level gfx_level, radeon_family family)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   switch (gfx_level) {
106bf215546Sopenharmony_ci   case GFX6:
107bf215546Sopenharmony_ci      switch (family) {
108bf215546Sopenharmony_ci      case CHIP_TAHITI: return "tahiti";
109bf215546Sopenharmony_ci      case CHIP_PITCAIRN: return "pitcairn";
110bf215546Sopenharmony_ci      case CHIP_VERDE: return "capeverde";
111bf215546Sopenharmony_ci      case CHIP_OLAND: return "oland";
112bf215546Sopenharmony_ci      case CHIP_HAINAN: return "hainan";
113bf215546Sopenharmony_ci      default: return nullptr;
114bf215546Sopenharmony_ci      }
115bf215546Sopenharmony_ci   case GFX7:
116bf215546Sopenharmony_ci      switch (family) {
117bf215546Sopenharmony_ci      case CHIP_BONAIRE: return "bonaire";
118bf215546Sopenharmony_ci      case CHIP_KAVERI: return "gfx700";
119bf215546Sopenharmony_ci      case CHIP_HAWAII: return "hawaii";
120bf215546Sopenharmony_ci      default: return nullptr;
121bf215546Sopenharmony_ci      }
122bf215546Sopenharmony_ci   case GFX8:
123bf215546Sopenharmony_ci      switch (family) {
124bf215546Sopenharmony_ci      case CHIP_TONGA: return "tonga";
125bf215546Sopenharmony_ci      case CHIP_ICELAND: return "iceland";
126bf215546Sopenharmony_ci      case CHIP_CARRIZO: return "carrizo";
127bf215546Sopenharmony_ci      case CHIP_FIJI: return "fiji";
128bf215546Sopenharmony_ci      case CHIP_STONEY: return "stoney";
129bf215546Sopenharmony_ci      case CHIP_POLARIS10: return "polaris10";
130bf215546Sopenharmony_ci      case CHIP_POLARIS11: return "polaris11";
131bf215546Sopenharmony_ci      case CHIP_POLARIS12: return "polaris12";
132bf215546Sopenharmony_ci      case CHIP_VEGAM: return "polaris11";
133bf215546Sopenharmony_ci      default: return nullptr;
134bf215546Sopenharmony_ci      }
135bf215546Sopenharmony_ci   case GFX9:
136bf215546Sopenharmony_ci      switch (family) {
137bf215546Sopenharmony_ci      case CHIP_VEGA10: return "vega10";
138bf215546Sopenharmony_ci      case CHIP_VEGA12: return "vega12";
139bf215546Sopenharmony_ci      case CHIP_VEGA20: return "vega20";
140bf215546Sopenharmony_ci      case CHIP_RAVEN: return "raven";
141bf215546Sopenharmony_ci      default: return nullptr;
142bf215546Sopenharmony_ci      }
143bf215546Sopenharmony_ci   case GFX10:
144bf215546Sopenharmony_ci      switch (family) {
145bf215546Sopenharmony_ci      case CHIP_NAVI10: return "gfx1010";
146bf215546Sopenharmony_ci      case CHIP_NAVI12: return "gfx1011";
147bf215546Sopenharmony_ci      default: return nullptr;
148bf215546Sopenharmony_ci      }
149bf215546Sopenharmony_ci   case GFX10_3:
150bf215546Sopenharmony_ci   case GFX11: return nullptr;
151bf215546Sopenharmony_ci   default: unreachable("Invalid chip class!"); return nullptr;
152bf215546Sopenharmony_ci   }
153bf215546Sopenharmony_ci}
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_cibool
156bf215546Sopenharmony_ciget_branch_target(char** output, Program* program, const std::vector<bool>& referenced_blocks,
157bf215546Sopenharmony_ci                  char** line_start)
158bf215546Sopenharmony_ci{
159bf215546Sopenharmony_ci   unsigned pos;
160bf215546Sopenharmony_ci   if (sscanf(*line_start, ".L%d_0", &pos) != 1)
161bf215546Sopenharmony_ci      return false;
162bf215546Sopenharmony_ci   pos /= 4;
163bf215546Sopenharmony_ci   *line_start = strchr(*line_start, '_') + 2;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   for (Block& block : program->blocks) {
166bf215546Sopenharmony_ci      if (referenced_blocks[block.index] && block.offset == pos) {
167bf215546Sopenharmony_ci         *output += sprintf(*output, "BB%u", block.index);
168bf215546Sopenharmony_ci         return true;
169bf215546Sopenharmony_ci      }
170bf215546Sopenharmony_ci   }
171bf215546Sopenharmony_ci   return false;
172bf215546Sopenharmony_ci}
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_cibool
175bf215546Sopenharmony_ciprint_asm_clrx(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
176bf215546Sopenharmony_ci{
177bf215546Sopenharmony_ci#ifdef _WIN32
178bf215546Sopenharmony_ci   return true;
179bf215546Sopenharmony_ci#else
180bf215546Sopenharmony_ci   char path[] = "/tmp/fileXXXXXX";
181bf215546Sopenharmony_ci   char line[2048], command[128];
182bf215546Sopenharmony_ci   FILE* p;
183bf215546Sopenharmony_ci   int fd;
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   const char* gpu_type = to_clrx_device_name(program->gfx_level, program->family);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   /* Dump the binary into a temporary file. */
188bf215546Sopenharmony_ci   fd = mkstemp(path);
189bf215546Sopenharmony_ci   if (fd < 0)
190bf215546Sopenharmony_ci      return true;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   for (unsigned i = 0; i < exec_size; i++) {
193bf215546Sopenharmony_ci      if (write(fd, &binary[i], 4) == -1)
194bf215546Sopenharmony_ci         goto fail;
195bf215546Sopenharmony_ci   }
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   sprintf(command, "clrxdisasm --gpuType=%s -r %s", gpu_type, path);
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   p = popen(command, "r");
200bf215546Sopenharmony_ci   if (p) {
201bf215546Sopenharmony_ci      if (!fgets(line, sizeof(line), p)) {
202bf215546Sopenharmony_ci         fprintf(output, "clrxdisasm not found\n");
203bf215546Sopenharmony_ci         pclose(p);
204bf215546Sopenharmony_ci         goto fail;
205bf215546Sopenharmony_ci      }
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci      std::vector<bool> referenced_blocks = get_referenced_blocks(program);
208bf215546Sopenharmony_ci      unsigned next_block = 0;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci      char prev_instr[2048];
211bf215546Sopenharmony_ci      unsigned prev_pos = 0;
212bf215546Sopenharmony_ci      do {
213bf215546Sopenharmony_ci         char* line_start = line;
214bf215546Sopenharmony_ci         if (strncmp(line_start, "/*", 2))
215bf215546Sopenharmony_ci            continue;
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci         unsigned pos;
218bf215546Sopenharmony_ci         if (sscanf(line_start, "/*%x*/", &pos) != 1)
219bf215546Sopenharmony_ci            continue;
220bf215546Sopenharmony_ci         pos /= 4u; /* get the dword position */
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci         while (strncmp(line_start, "*/", 2))
223bf215546Sopenharmony_ci            line_start++;
224bf215546Sopenharmony_ci         line_start += 2;
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci         while (line_start[0] == ' ')
227bf215546Sopenharmony_ci            line_start++;
228bf215546Sopenharmony_ci         *strchr(line_start, '\n') = 0;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci         if (*line_start == 0)
231bf215546Sopenharmony_ci            continue; /* not an instruction, only a comment */
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci         if (pos != prev_pos) {
234bf215546Sopenharmony_ci            /* Print the previous instruction, now that we know the encoding size. */
235bf215546Sopenharmony_ci            print_instr(output, binary, prev_instr, pos - prev_pos, prev_pos);
236bf215546Sopenharmony_ci            prev_pos = pos;
237bf215546Sopenharmony_ci         }
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci         print_block_markers(output, program, referenced_blocks, &next_block, pos);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci         char* dest = prev_instr;
242bf215546Sopenharmony_ci         *(dest++) = '\t';
243bf215546Sopenharmony_ci         while (*line_start) {
244bf215546Sopenharmony_ci            if (!strncmp(line_start, ".L", 2) &&
245bf215546Sopenharmony_ci                get_branch_target(&dest, program, referenced_blocks, &line_start))
246bf215546Sopenharmony_ci               continue;
247bf215546Sopenharmony_ci            *(dest++) = *(line_start++);
248bf215546Sopenharmony_ci         }
249bf215546Sopenharmony_ci         *(dest++) = 0;
250bf215546Sopenharmony_ci      } while (fgets(line, sizeof(line), p));
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci      if (prev_pos != exec_size)
253bf215546Sopenharmony_ci         print_instr(output, binary, prev_instr, exec_size - prev_pos, prev_pos);
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci      pclose(p);
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci      print_constant_data(output, program);
258bf215546Sopenharmony_ci   }
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci   return false;
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_cifail:
263bf215546Sopenharmony_ci   close(fd);
264bf215546Sopenharmony_ci   unlink(path);
265bf215546Sopenharmony_ci   return true;
266bf215546Sopenharmony_ci#endif
267bf215546Sopenharmony_ci}
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci#ifdef LLVM_AVAILABLE
270bf215546Sopenharmony_cistd::pair<bool, size_t>
271bf215546Sopenharmony_cidisasm_instr(amd_gfx_level gfx_level, LLVMDisasmContextRef disasm, uint32_t* binary,
272bf215546Sopenharmony_ci             unsigned exec_size, size_t pos, char* outline, unsigned outline_size)
273bf215546Sopenharmony_ci{
274bf215546Sopenharmony_ci   size_t l =
275bf215546Sopenharmony_ci      LLVMDisasmInstruction(disasm, (uint8_t*)&binary[pos], (exec_size - pos) * sizeof(uint32_t),
276bf215546Sopenharmony_ci                            pos * 4, outline, outline_size);
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   if (gfx_level >= GFX10 && l == 8 && ((binary[pos] & 0xffff0000) == 0xd7610000) &&
279bf215546Sopenharmony_ci       ((binary[pos + 1] & 0x1ff) == 0xff)) {
280bf215546Sopenharmony_ci      /* v_writelane with literal uses 3 dwords but llvm consumes only 2 */
281bf215546Sopenharmony_ci      l += 4;
282bf215546Sopenharmony_ci   }
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci   bool invalid = false;
285bf215546Sopenharmony_ci   size_t size;
286bf215546Sopenharmony_ci   if (!l &&
287bf215546Sopenharmony_ci       ((gfx_level >= GFX9 &&
288bf215546Sopenharmony_ci         (binary[pos] & 0xffff8000) == 0xd1348000) || /* v_add_u32_e64 + clamp */
289bf215546Sopenharmony_ci        (gfx_level >= GFX10 &&
290bf215546Sopenharmony_ci         (binary[pos] & 0xffff8000) == 0xd7038000) || /* v_add_u16_e64 + clamp */
291bf215546Sopenharmony_ci        (gfx_level <= GFX9 &&
292bf215546Sopenharmony_ci         (binary[pos] & 0xffff8000) == 0xd1268000) || /* v_add_u16_e64 + clamp */
293bf215546Sopenharmony_ci        (gfx_level >= GFX10 && (binary[pos] & 0xffff8000) == 0xd76d8000) || /* v_add3_u32 + clamp */
294bf215546Sopenharmony_ci        (gfx_level == GFX9 && (binary[pos] & 0xffff8000) == 0xd1ff8000)) /* v_add3_u32 + clamp */) {
295bf215546Sopenharmony_ci      strcpy(outline, "\tinteger addition + clamp");
296bf215546Sopenharmony_ci      bool has_literal = gfx_level >= GFX10 && (((binary[pos + 1] & 0x1ff) == 0xff) ||
297bf215546Sopenharmony_ci                                                (((binary[pos + 1] >> 9) & 0x1ff) == 0xff));
298bf215546Sopenharmony_ci      size = 2 + has_literal;
299bf215546Sopenharmony_ci   } else if (gfx_level >= GFX10 && l == 4 && ((binary[pos] & 0xfe0001ff) == 0x020000f9)) {
300bf215546Sopenharmony_ci      strcpy(outline, "\tv_cndmask_b32 + sdwa");
301bf215546Sopenharmony_ci      size = 2;
302bf215546Sopenharmony_ci   } else if (!l) {
303bf215546Sopenharmony_ci      strcpy(outline, "(invalid instruction)");
304bf215546Sopenharmony_ci      size = 1;
305bf215546Sopenharmony_ci      invalid = true;
306bf215546Sopenharmony_ci   } else {
307bf215546Sopenharmony_ci      assert(l % 4 == 0);
308bf215546Sopenharmony_ci      size = l / 4;
309bf215546Sopenharmony_ci   }
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci#if LLVM_VERSION_MAJOR <= 14
312bf215546Sopenharmony_ci   /* See: https://github.com/GPUOpen-Tools/radeon_gpu_profiler/issues/65 and
313bf215546Sopenharmony_ci    * https://github.com/llvm/llvm-project/issues/38652
314bf215546Sopenharmony_ci    */
315bf215546Sopenharmony_ci   if (invalid) {
316bf215546Sopenharmony_ci      /* do nothing */
317bf215546Sopenharmony_ci   } else if (gfx_level == GFX9 && (binary[pos] & 0xfc024000) == 0xc0024000) {
318bf215546Sopenharmony_ci      /* SMEM with IMM=1 and SOE=1: LLVM ignores SOFFSET */
319bf215546Sopenharmony_ci      size_t len = strlen(outline);
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci      char imm[16] = {0};
322bf215546Sopenharmony_ci      while (outline[--len] != ' ') ;
323bf215546Sopenharmony_ci      strncpy(imm, outline + len + 1, sizeof(imm) - 1);
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci      snprintf(outline + len, outline_size - len, " s%u offset:%s", binary[pos + 1] >> 25, imm);
326bf215546Sopenharmony_ci   } else if (gfx_level >= GFX10 && (binary[pos] & 0xfc000000) == 0xf4000000 &&
327bf215546Sopenharmony_ci              (binary[pos + 1] & 0xfe000000) != 0xfa000000) {
328bf215546Sopenharmony_ci      /* SMEM non-NULL SOFFSET: LLVM ignores OFFSET */
329bf215546Sopenharmony_ci      uint32_t offset = binary[pos + 1] & 0x1fffff;
330bf215546Sopenharmony_ci      if (offset) {
331bf215546Sopenharmony_ci         size_t len = strlen(outline);
332bf215546Sopenharmony_ci         snprintf(outline + len, outline_size - len, " offset:0x%x", offset);
333bf215546Sopenharmony_ci      }
334bf215546Sopenharmony_ci   }
335bf215546Sopenharmony_ci#endif
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci   return std::make_pair(invalid, size);
338bf215546Sopenharmony_ci}
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_cibool
341bf215546Sopenharmony_ciprint_asm_llvm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
342bf215546Sopenharmony_ci{
343bf215546Sopenharmony_ci   std::vector<bool> referenced_blocks = get_referenced_blocks(program);
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci   std::vector<llvm::SymbolInfoTy> symbols;
346bf215546Sopenharmony_ci   std::vector<std::array<char, 16>> block_names;
347bf215546Sopenharmony_ci   block_names.reserve(program->blocks.size());
348bf215546Sopenharmony_ci   for (Block& block : program->blocks) {
349bf215546Sopenharmony_ci      if (!referenced_blocks[block.index])
350bf215546Sopenharmony_ci         continue;
351bf215546Sopenharmony_ci      std::array<char, 16> name;
352bf215546Sopenharmony_ci      sprintf(name.data(), "BB%u", block.index);
353bf215546Sopenharmony_ci      block_names.push_back(name);
354bf215546Sopenharmony_ci      symbols.emplace_back(block.offset * 4,
355bf215546Sopenharmony_ci                           llvm::StringRef(block_names[block_names.size() - 1].data()), 0);
356bf215546Sopenharmony_ci   }
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci   const char* features = "";
359bf215546Sopenharmony_ci   if (program->gfx_level >= GFX10 && program->wave_size == 64) {
360bf215546Sopenharmony_ci      features = "+wavefrontsize64";
361bf215546Sopenharmony_ci   }
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci   LLVMDisasmContextRef disasm =
364bf215546Sopenharmony_ci      LLVMCreateDisasmCPUFeatures("amdgcn-mesa-mesa3d", ac_get_llvm_processor_name(program->family),
365bf215546Sopenharmony_ci                                  features, &symbols, 0, NULL, NULL);
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   size_t pos = 0;
368bf215546Sopenharmony_ci   bool invalid = false;
369bf215546Sopenharmony_ci   unsigned next_block = 0;
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   unsigned prev_size = 0;
372bf215546Sopenharmony_ci   unsigned prev_pos = 0;
373bf215546Sopenharmony_ci   unsigned repeat_count = 0;
374bf215546Sopenharmony_ci   while (pos < exec_size) {
375bf215546Sopenharmony_ci      bool new_block =
376bf215546Sopenharmony_ci         next_block < program->blocks.size() && pos == program->blocks[next_block].offset;
377bf215546Sopenharmony_ci      if (pos + prev_size <= exec_size && prev_pos != pos && !new_block &&
378bf215546Sopenharmony_ci          memcmp(&binary[prev_pos], &binary[pos], prev_size * 4) == 0) {
379bf215546Sopenharmony_ci         repeat_count++;
380bf215546Sopenharmony_ci         pos += prev_size;
381bf215546Sopenharmony_ci         continue;
382bf215546Sopenharmony_ci      } else {
383bf215546Sopenharmony_ci         if (repeat_count)
384bf215546Sopenharmony_ci            fprintf(output, "\t(then repeated %u times)\n", repeat_count);
385bf215546Sopenharmony_ci         repeat_count = 0;
386bf215546Sopenharmony_ci      }
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci      print_block_markers(output, program, referenced_blocks, &next_block, pos);
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_ci      char outline[1024];
391bf215546Sopenharmony_ci      std::pair<bool, size_t> res = disasm_instr(program->gfx_level, disasm, binary.data(),
392bf215546Sopenharmony_ci                                                 exec_size, pos, outline, sizeof(outline));
393bf215546Sopenharmony_ci      invalid |= res.first;
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci      print_instr(output, binary, outline, res.second, pos);
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci      prev_size = res.second;
398bf215546Sopenharmony_ci      prev_pos = pos;
399bf215546Sopenharmony_ci      pos += res.second;
400bf215546Sopenharmony_ci   }
401bf215546Sopenharmony_ci   assert(next_block == program->blocks.size());
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci   LLVMDisasmDispose(disasm);
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci   print_constant_data(output, program);
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci   return invalid;
408bf215546Sopenharmony_ci}
409bf215546Sopenharmony_ci#endif /* LLVM_AVAILABLE */
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci} /* end namespace */
412bf215546Sopenharmony_ci
413bf215546Sopenharmony_cibool
414bf215546Sopenharmony_cicheck_print_asm_support(Program* program)
415bf215546Sopenharmony_ci{
416bf215546Sopenharmony_ci#ifdef LLVM_AVAILABLE
417bf215546Sopenharmony_ci   if (program->gfx_level >= GFX8) {
418bf215546Sopenharmony_ci      /* LLVM disassembler only supports GFX8+ */
419bf215546Sopenharmony_ci      const char* name = ac_get_llvm_processor_name(program->family);
420bf215546Sopenharmony_ci      const char* triple = "amdgcn--";
421bf215546Sopenharmony_ci      LLVMTargetRef target = ac_get_llvm_target(triple);
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci      LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
424bf215546Sopenharmony_ci         target, triple, name, "", LLVMCodeGenLevelDefault, LLVMRelocDefault, LLVMCodeModelDefault);
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci      bool supported = ac_is_llvm_processor_supported(tm, name);
427bf215546Sopenharmony_ci      LLVMDisposeTargetMachine(tm);
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci      if (supported)
430bf215546Sopenharmony_ci         return true;
431bf215546Sopenharmony_ci   }
432bf215546Sopenharmony_ci#endif
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci#ifndef _WIN32
435bf215546Sopenharmony_ci   /* Check if CLRX disassembler binary is available and can disassemble the program */
436bf215546Sopenharmony_ci   return to_clrx_device_name(program->gfx_level, program->family) &&
437bf215546Sopenharmony_ci          system("clrxdisasm --version") == 0;
438bf215546Sopenharmony_ci#else
439bf215546Sopenharmony_ci   return false;
440bf215546Sopenharmony_ci#endif
441bf215546Sopenharmony_ci}
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci/* Returns true on failure */
444bf215546Sopenharmony_cibool
445bf215546Sopenharmony_ciprint_asm(Program* program, std::vector<uint32_t>& binary, unsigned exec_size, FILE* output)
446bf215546Sopenharmony_ci{
447bf215546Sopenharmony_ci#ifdef LLVM_AVAILABLE
448bf215546Sopenharmony_ci   if (program->gfx_level >= GFX8) {
449bf215546Sopenharmony_ci      return print_asm_llvm(program, binary, exec_size, output);
450bf215546Sopenharmony_ci   }
451bf215546Sopenharmony_ci#endif
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci   return print_asm_clrx(program, binary, exec_size, output);
454bf215546Sopenharmony_ci}
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci} // namespace aco
457