17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2018 Google
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#include "aco_interface.h"
267ec681f3Smrg
277ec681f3Smrg#include "aco_ir.h"
287ec681f3Smrg
297ec681f3Smrg#include "vulkan/radv_shader.h"
307ec681f3Smrg#include "vulkan/radv_shader_args.h"
317ec681f3Smrg
327ec681f3Smrg#include "util/memstream.h"
337ec681f3Smrg
347ec681f3Smrg#include <array>
357ec681f3Smrg#include <iostream>
367ec681f3Smrg#include <vector>
377ec681f3Smrg
387ec681f3Smrgstatic const std::array<aco_compiler_statistic_info, aco::num_statistics> statistic_infos = []()
397ec681f3Smrg{
407ec681f3Smrg   std::array<aco_compiler_statistic_info, aco::num_statistics> ret{};
417ec681f3Smrg   ret[aco::statistic_hash] =
427ec681f3Smrg      aco_compiler_statistic_info{"Hash", "CRC32 hash of code and constant data"};
437ec681f3Smrg   ret[aco::statistic_instructions] =
447ec681f3Smrg      aco_compiler_statistic_info{"Instructions", "Instruction count"};
457ec681f3Smrg   ret[aco::statistic_copies] =
467ec681f3Smrg      aco_compiler_statistic_info{"Copies", "Copy instructions created for pseudo-instructions"};
477ec681f3Smrg   ret[aco::statistic_branches] = aco_compiler_statistic_info{"Branches", "Branch instructions"};
487ec681f3Smrg   ret[aco::statistic_latency] =
497ec681f3Smrg      aco_compiler_statistic_info{"Latency", "Issue cycles plus stall cycles"};
507ec681f3Smrg   ret[aco::statistic_inv_throughput] = aco_compiler_statistic_info{
517ec681f3Smrg      "Inverse Throughput", "Estimated busy cycles to execute one wave"};
527ec681f3Smrg   ret[aco::statistic_vmem_clauses] = aco_compiler_statistic_info{
537ec681f3Smrg      "VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"};
547ec681f3Smrg   ret[aco::statistic_smem_clauses] = aco_compiler_statistic_info{
557ec681f3Smrg      "SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"};
567ec681f3Smrg   ret[aco::statistic_sgpr_presched] =
577ec681f3Smrg      aco_compiler_statistic_info{"Pre-Sched SGPRs", "SGPR usage before scheduling"};
587ec681f3Smrg   ret[aco::statistic_vgpr_presched] =
597ec681f3Smrg      aco_compiler_statistic_info{"Pre-Sched VGPRs", "VGPR usage before scheduling"};
607ec681f3Smrg   return ret;
617ec681f3Smrg}();
627ec681f3Smrg
637ec681f3Smrgconst unsigned aco_num_statistics = aco::num_statistics;
647ec681f3Smrgconst aco_compiler_statistic_info* aco_statistic_infos = statistic_infos.data();
657ec681f3Smrg
667ec681f3Smrgstatic void
677ec681f3Smrgvalidate(aco::Program* program)
687ec681f3Smrg{
697ec681f3Smrg   if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
707ec681f3Smrg      return;
717ec681f3Smrg
727ec681f3Smrg   ASSERTED bool is_valid = aco::validate_ir(program);
737ec681f3Smrg   assert(is_valid);
747ec681f3Smrg}
757ec681f3Smrg
767ec681f3Smrgvoid
777ec681f3Smrgaco_compile_shader(unsigned shader_count, struct nir_shader* const* shaders,
787ec681f3Smrg                   struct radv_shader_binary** binary, const struct radv_shader_args* args)
797ec681f3Smrg{
807ec681f3Smrg   aco::init();
817ec681f3Smrg
827ec681f3Smrg   ac_shader_config config = {0};
837ec681f3Smrg   std::unique_ptr<aco::Program> program{new aco::Program};
847ec681f3Smrg
857ec681f3Smrg   program->collect_statistics = args->options->record_stats;
867ec681f3Smrg   if (program->collect_statistics)
877ec681f3Smrg      memset(program->statistics, 0, sizeof(program->statistics));
887ec681f3Smrg
897ec681f3Smrg   program->debug.func = args->options->debug.func;
907ec681f3Smrg   program->debug.private_data = args->options->debug.private_data;
917ec681f3Smrg
927ec681f3Smrg   /* Instruction Selection */
937ec681f3Smrg   if (args->is_gs_copy_shader)
947ec681f3Smrg      aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
957ec681f3Smrg   else if (args->is_trap_handler_shader)
967ec681f3Smrg      aco::select_trap_handler_shader(program.get(), shaders[0], &config, args);
977ec681f3Smrg   else
987ec681f3Smrg      aco::select_program(program.get(), shader_count, shaders, &config, args);
997ec681f3Smrg   if (args->options->dump_preoptir)
1007ec681f3Smrg      aco_print_program(program.get(), stderr);
1017ec681f3Smrg
1027ec681f3Smrg   aco::live live_vars;
1037ec681f3Smrg   if (!args->is_trap_handler_shader) {
1047ec681f3Smrg      /* Phi lowering */
1057ec681f3Smrg      aco::lower_phis(program.get());
1067ec681f3Smrg      aco::dominator_tree(program.get());
1077ec681f3Smrg      validate(program.get());
1087ec681f3Smrg
1097ec681f3Smrg      /* Optimization */
1107ec681f3Smrg      if (!args->options->key.optimisations_disabled) {
1117ec681f3Smrg         if (!(aco::debug_flags & aco::DEBUG_NO_VN))
1127ec681f3Smrg            aco::value_numbering(program.get());
1137ec681f3Smrg         if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
1147ec681f3Smrg            aco::optimize(program.get());
1157ec681f3Smrg      }
1167ec681f3Smrg
1177ec681f3Smrg      /* cleanup and exec mask handling */
1187ec681f3Smrg      aco::setup_reduce_temp(program.get());
1197ec681f3Smrg      aco::insert_exec_mask(program.get());
1207ec681f3Smrg      validate(program.get());
1217ec681f3Smrg
1227ec681f3Smrg      /* spilling and scheduling */
1237ec681f3Smrg      live_vars = aco::live_var_analysis(program.get());
1247ec681f3Smrg      aco::spill(program.get(), live_vars);
1257ec681f3Smrg   }
1267ec681f3Smrg
1277ec681f3Smrg   std::string llvm_ir;
1287ec681f3Smrg   if (args->options->record_ir) {
1297ec681f3Smrg      char* data = NULL;
1307ec681f3Smrg      size_t size = 0;
1317ec681f3Smrg      u_memstream mem;
1327ec681f3Smrg      if (u_memstream_open(&mem, &data, &size)) {
1337ec681f3Smrg         FILE* const memf = u_memstream_get(&mem);
1347ec681f3Smrg         aco_print_program(program.get(), memf);
1357ec681f3Smrg         fputc(0, memf);
1367ec681f3Smrg         u_memstream_close(&mem);
1377ec681f3Smrg      }
1387ec681f3Smrg
1397ec681f3Smrg      llvm_ir = std::string(data, data + size);
1407ec681f3Smrg      free(data);
1417ec681f3Smrg   }
1427ec681f3Smrg
1437ec681f3Smrg   if (program->collect_statistics)
1447ec681f3Smrg      aco::collect_presched_stats(program.get());
1457ec681f3Smrg
1467ec681f3Smrg   if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && args->options->dump_shader)
1477ec681f3Smrg      aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);
1487ec681f3Smrg
1497ec681f3Smrg   if (!args->is_trap_handler_shader) {
1507ec681f3Smrg      if (!args->options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_SCHED))
1517ec681f3Smrg         aco::schedule_program(program.get(), live_vars);
1527ec681f3Smrg      validate(program.get());
1537ec681f3Smrg
1547ec681f3Smrg      /* Register Allocation */
1557ec681f3Smrg      aco::register_allocation(program.get(), live_vars.live_out);
1567ec681f3Smrg
1577ec681f3Smrg      if (aco::validate_ra(program.get())) {
1587ec681f3Smrg         aco_print_program(program.get(), stderr);
1597ec681f3Smrg         abort();
1607ec681f3Smrg      } else if (args->options->dump_shader) {
1617ec681f3Smrg         aco_print_program(program.get(), stderr);
1627ec681f3Smrg      }
1637ec681f3Smrg
1647ec681f3Smrg      validate(program.get());
1657ec681f3Smrg
1667ec681f3Smrg      /* Optimization */
1677ec681f3Smrg      if (!args->options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {
1687ec681f3Smrg         aco::optimize_postRA(program.get());
1697ec681f3Smrg         validate(program.get());
1707ec681f3Smrg      }
1717ec681f3Smrg
1727ec681f3Smrg      aco::ssa_elimination(program.get());
1737ec681f3Smrg   }
1747ec681f3Smrg
1757ec681f3Smrg   /* Lower to HW Instructions */
1767ec681f3Smrg   aco::lower_to_hw_instr(program.get());
1777ec681f3Smrg
1787ec681f3Smrg   /* Insert Waitcnt */
1797ec681f3Smrg   aco::insert_wait_states(program.get());
1807ec681f3Smrg   aco::insert_NOPs(program.get());
1817ec681f3Smrg
1827ec681f3Smrg   if (program->chip_class >= GFX10)
1837ec681f3Smrg      aco::form_hard_clauses(program.get());
1847ec681f3Smrg
1857ec681f3Smrg   if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))
1867ec681f3Smrg      aco::collect_preasm_stats(program.get());
1877ec681f3Smrg
1887ec681f3Smrg   /* Assembly */
1897ec681f3Smrg   std::vector<uint32_t> code;
1907ec681f3Smrg   unsigned exec_size = aco::emit_program(program.get(), code);
1917ec681f3Smrg
1927ec681f3Smrg   if (program->collect_statistics)
1937ec681f3Smrg      aco::collect_postasm_stats(program.get(), code);
1947ec681f3Smrg
1957ec681f3Smrg   bool get_disasm = args->options->dump_shader || args->options->record_ir;
1967ec681f3Smrg
1977ec681f3Smrg   size_t size = llvm_ir.size();
1987ec681f3Smrg
1997ec681f3Smrg   std::string disasm;
2007ec681f3Smrg   if (get_disasm) {
2017ec681f3Smrg      if (check_print_asm_support(program.get())) {
2027ec681f3Smrg         char* data = NULL;
2037ec681f3Smrg         size_t disasm_size = 0;
2047ec681f3Smrg         struct u_memstream mem;
2057ec681f3Smrg         if (u_memstream_open(&mem, &data, &disasm_size)) {
2067ec681f3Smrg            FILE* const memf = u_memstream_get(&mem);
2077ec681f3Smrg            aco::print_asm(program.get(), code, exec_size / 4u, memf);
2087ec681f3Smrg            fputc(0, memf);
2097ec681f3Smrg            u_memstream_close(&mem);
2107ec681f3Smrg         }
2117ec681f3Smrg
2127ec681f3Smrg         disasm = std::string(data, data + disasm_size);
2137ec681f3Smrg         size += disasm_size;
2147ec681f3Smrg         free(data);
2157ec681f3Smrg      } else {
2167ec681f3Smrg         disasm = "Shader disassembly is not supported in the current configuration"
2177ec681f3Smrg#ifndef LLVM_AVAILABLE
2187ec681f3Smrg                  " (LLVM not available)"
2197ec681f3Smrg#endif
2207ec681f3Smrg                  ".\n";
2217ec681f3Smrg         size += disasm.length();
2227ec681f3Smrg      }
2237ec681f3Smrg   }
2247ec681f3Smrg
2257ec681f3Smrg   size_t stats_size = 0;
2267ec681f3Smrg   if (program->collect_statistics)
2277ec681f3Smrg      stats_size = aco::num_statistics * sizeof(uint32_t);
2287ec681f3Smrg   size += stats_size;
2297ec681f3Smrg
2307ec681f3Smrg   size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
2317ec681f3Smrg   /* We need to calloc to prevent unintialized data because this will be used
2327ec681f3Smrg    * directly for the disk cache. Uninitialized data can appear because of
2337ec681f3Smrg    * padding in the struct or because legacy_binary->data can be at an offset
2347ec681f3Smrg    * from the start less than sizeof(radv_shader_binary_legacy). */
2357ec681f3Smrg   radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*)calloc(size, 1);
2367ec681f3Smrg
2377ec681f3Smrg   legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
2387ec681f3Smrg   legacy_binary->base.stage = shaders[shader_count - 1]->info.stage;
2397ec681f3Smrg   legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
2407ec681f3Smrg   legacy_binary->base.total_size = size;
2417ec681f3Smrg
2427ec681f3Smrg   if (program->collect_statistics)
2437ec681f3Smrg      memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
2447ec681f3Smrg   legacy_binary->stats_size = stats_size;
2457ec681f3Smrg
2467ec681f3Smrg   memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(),
2477ec681f3Smrg          code.size() * sizeof(uint32_t));
2487ec681f3Smrg   legacy_binary->exec_size = exec_size;
2497ec681f3Smrg   legacy_binary->code_size = code.size() * sizeof(uint32_t);
2507ec681f3Smrg
2517ec681f3Smrg   legacy_binary->base.config = config;
2527ec681f3Smrg   legacy_binary->disasm_size = 0;
2537ec681f3Smrg   legacy_binary->ir_size = llvm_ir.size();
2547ec681f3Smrg
2557ec681f3Smrg   llvm_ir.copy((char*)legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size,
2567ec681f3Smrg                llvm_ir.size());
2577ec681f3Smrg
2587ec681f3Smrg   if (get_disasm) {
2597ec681f3Smrg      disasm.copy((char*)legacy_binary->data + legacy_binary->stats_size +
2607ec681f3Smrg                     legacy_binary->code_size + llvm_ir.size(),
2617ec681f3Smrg                  disasm.size());
2627ec681f3Smrg      legacy_binary->disasm_size = disasm.size();
2637ec681f3Smrg   }
2647ec681f3Smrg
2657ec681f3Smrg   *binary = (radv_shader_binary*)legacy_binary;
2667ec681f3Smrg}
2677ec681f3Smrg
2687ec681f3Smrgvoid
2697ec681f3Smrgaco_compile_vs_prolog(const struct radv_vs_prolog_key* key, struct radv_prolog_binary** binary,
2707ec681f3Smrg                      const struct radv_shader_args* args)
2717ec681f3Smrg{
2727ec681f3Smrg   aco::init();
2737ec681f3Smrg
2747ec681f3Smrg   /* create program */
2757ec681f3Smrg   ac_shader_config config = {0};
2767ec681f3Smrg   std::unique_ptr<aco::Program> program{new aco::Program};
2777ec681f3Smrg   program->collect_statistics = false;
2787ec681f3Smrg   program->debug.func = NULL;
2797ec681f3Smrg   program->debug.private_data = NULL;
2807ec681f3Smrg
2817ec681f3Smrg   /* create IR */
2827ec681f3Smrg   unsigned num_preserved_sgprs;
2837ec681f3Smrg   aco::select_vs_prolog(program.get(), key, &config, args, &num_preserved_sgprs);
2847ec681f3Smrg   aco::insert_NOPs(program.get());
2857ec681f3Smrg
2867ec681f3Smrg   if (args->options->dump_shader)
2877ec681f3Smrg      aco_print_program(program.get(), stderr);
2887ec681f3Smrg
2897ec681f3Smrg   /* assembly */
2907ec681f3Smrg   std::vector<uint32_t> code;
2917ec681f3Smrg   code.reserve(align(program->blocks[0].instructions.size() * 2, 16));
2927ec681f3Smrg   unsigned exec_size = aco::emit_program(program.get(), code);
2937ec681f3Smrg
2947ec681f3Smrg   if (args->options->dump_shader) {
2957ec681f3Smrg      aco::print_asm(program.get(), code, exec_size / 4u, stderr);
2967ec681f3Smrg      fprintf(stderr, "\n");
2977ec681f3Smrg   }
2987ec681f3Smrg
2997ec681f3Smrg   /* copy into binary */
3007ec681f3Smrg   size_t size = code.size() * sizeof(uint32_t) + sizeof(radv_prolog_binary);
3017ec681f3Smrg   radv_prolog_binary* prolog_binary = (radv_prolog_binary*)calloc(size, 1);
3027ec681f3Smrg
3037ec681f3Smrg   prolog_binary->num_sgprs = config.num_sgprs;
3047ec681f3Smrg   prolog_binary->num_vgprs = config.num_vgprs;
3057ec681f3Smrg   prolog_binary->num_preserved_sgprs = num_preserved_sgprs;
3067ec681f3Smrg   prolog_binary->code_size = code.size() * sizeof(uint32_t);
3077ec681f3Smrg   memcpy(prolog_binary->data, code.data(), prolog_binary->code_size);
3087ec681f3Smrg
3097ec681f3Smrg   *binary = prolog_binary;
3107ec681f3Smrg}
311