Path: blob/21.2-virgl/src/amd/compiler/aco_interface.cpp
4550 views
/*1* Copyright © 2018 Google2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22*/2324#include "aco_interface.h"2526#include "aco_ir.h"2728#include "vulkan/radv_shader.h"29#include "vulkan/radv_shader_args.h"3031#include "util/memstream.h"3233#include <array>34#include <iostream>35#include <vector>3637static const std::array<aco_compiler_statistic_info, aco::num_statistics> statistic_infos = []()38{39std::array<aco_compiler_statistic_info, aco::num_statistics> ret{};40ret[aco::statistic_hash] =41aco_compiler_statistic_info{"Hash", "CRC32 hash of code and constant data"};42ret[aco::statistic_instructions] =43aco_compiler_statistic_info{"Instructions", "Instruction count"};44ret[aco::statistic_copies] =45aco_compiler_statistic_info{"Copies", "Copy instructions created for pseudo-instructions"};46ret[aco::statistic_branches] = aco_compiler_statistic_info{"Branches", "Branch instructions"};47ret[aco::statistic_latency] =48aco_compiler_statistic_info{"Latency", "Issue cycles plus stall cycles"};49ret[aco::statistic_inv_throughput] = aco_compiler_statistic_info{50"Inverse Throughput", "Estimated busy cycles to execute one wave"};51ret[aco::statistic_vmem_clauses] = aco_compiler_statistic_info{52"VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"};53ret[aco::statistic_smem_clauses] = aco_compiler_statistic_info{54"SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"};55ret[aco::statistic_sgpr_presched] =56aco_compiler_statistic_info{"Pre-Sched SGPRs", "SGPR usage before scheduling"};57ret[aco::statistic_vgpr_presched] =58aco_compiler_statistic_info{"Pre-Sched VGPRs", "VGPR usage before scheduling"};59return ret;60}();6162const unsigned aco_num_statistics = aco::num_statistics;63const aco_compiler_statistic_info* aco_statistic_infos = statistic_infos.data();6465static void66validate(aco::Program* program)67{68if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))69return;7071ASSERTED bool is_valid = aco::validate_ir(program);72assert(is_valid);73}7475void76aco_compile_shader(unsigned shader_count, struct nir_shader* const* shaders,77struct radv_shader_binary** binary, struct radv_shader_args* args)78{79aco::init();8081ac_shader_config config = {0};82std::unique_ptr<aco::Program> program{new aco::Program};8384program->collect_statistics = args->options->record_stats;85if (program->collect_statistics)86memset(program->statistics, 0, sizeof(program->statistics));8788program->debug.func = args->options->debug.func;89program->debug.private_data = args->options->debug.private_data;9091/* Instruction Selection */92if (args->is_gs_copy_shader)93aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);94else if (args->is_trap_handler_shader)95aco::select_trap_handler_shader(program.get(), shaders[0], &config, args);96else97aco::select_program(program.get(), shader_count, shaders, &config, args);98if (args->options->dump_preoptir)99aco_print_program(program.get(), stderr);100101aco::live live_vars;102if (!args->is_trap_handler_shader) {103/* Phi lowering */104aco::lower_phis(program.get());105aco::dominator_tree(program.get());106validate(program.get());107108/* Optimization */109if (!args->options->disable_optimizations) {110if (!(aco::debug_flags & aco::DEBUG_NO_VN))111aco::value_numbering(program.get());112if (!(aco::debug_flags & aco::DEBUG_NO_OPT))113aco::optimize(program.get());114}115116/* cleanup and exec mask handling */117aco::setup_reduce_temp(program.get());118aco::insert_exec_mask(program.get());119validate(program.get());120121/* spilling and scheduling */122live_vars = aco::live_var_analysis(program.get());123aco::spill(program.get(), live_vars);124}125126std::string llvm_ir;127if (args->options->record_ir) {128char* data = NULL;129size_t size = 0;130u_memstream mem;131if (u_memstream_open(&mem, &data, &size)) {132FILE* const memf = u_memstream_get(&mem);133aco_print_program(program.get(), memf);134fputc(0, memf);135u_memstream_close(&mem);136}137138llvm_ir = std::string(data, data + size);139free(data);140}141142if (program->collect_statistics)143aco::collect_presched_stats(program.get());144145if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && args->options->dump_shader)146aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);147148if (!args->is_trap_handler_shader) {149if (!args->options->disable_optimizations && !(aco::debug_flags & aco::DEBUG_NO_SCHED))150aco::schedule_program(program.get(), live_vars);151validate(program.get());152153/* Register Allocation */154aco::register_allocation(program.get(), live_vars.live_out);155156if (aco::validate_ra(program.get())) {157aco_print_program(program.get(), stderr);158abort();159} else if (args->options->dump_shader) {160aco_print_program(program.get(), stderr);161}162163validate(program.get());164165/* Optimization */166if (!args->options->disable_optimizations && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {167aco::optimize_postRA(program.get());168validate(program.get());169}170171aco::ssa_elimination(program.get());172}173174/* Lower to HW Instructions */175aco::lower_to_hw_instr(program.get());176177/* Insert Waitcnt */178aco::insert_wait_states(program.get());179aco::insert_NOPs(program.get());180181if (program->chip_class >= GFX10)182aco::form_hard_clauses(program.get());183184if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))185aco::collect_preasm_stats(program.get());186187/* Assembly */188std::vector<uint32_t> code;189unsigned exec_size = aco::emit_program(program.get(), code);190191if (program->collect_statistics)192aco::collect_postasm_stats(program.get(), code);193194bool get_disasm = args->options->dump_shader || args->options->record_ir;195196size_t size = llvm_ir.size();197198std::string disasm;199if (get_disasm) {200char* data = NULL;201size_t disasm_size = 0;202struct u_memstream mem;203if (u_memstream_open(&mem, &data, &disasm_size)) {204FILE* const memf = u_memstream_get(&mem);205aco::print_asm(program.get(), code, exec_size / 4u, memf);206fputc(0, memf);207u_memstream_close(&mem);208}209210disasm = std::string(data, data + disasm_size);211size += disasm_size;212free(data);213}214215size_t stats_size = 0;216if (program->collect_statistics)217stats_size = aco::num_statistics * sizeof(uint32_t);218size += stats_size;219220size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);221/* We need to calloc to prevent unintialized data because this will be used222* directly for the disk cache. Uninitialized data can appear because of223* padding in the struct or because legacy_binary->data can be at an offset224* from the start less than sizeof(radv_shader_binary_legacy). */225radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*)calloc(size, 1);226227legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;228legacy_binary->base.stage = shaders[shader_count - 1]->info.stage;229legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;230legacy_binary->base.total_size = size;231232if (program->collect_statistics)233memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));234legacy_binary->stats_size = stats_size;235236memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(),237code.size() * sizeof(uint32_t));238legacy_binary->exec_size = exec_size;239legacy_binary->code_size = code.size() * sizeof(uint32_t);240241legacy_binary->config = config;242legacy_binary->disasm_size = 0;243legacy_binary->ir_size = llvm_ir.size();244245llvm_ir.copy((char*)legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size,246llvm_ir.size());247248if (get_disasm) {249disasm.copy((char*)legacy_binary->data + legacy_binary->stats_size +250legacy_binary->code_size + llvm_ir.size(),251disasm.size());252legacy_binary->disasm_size = disasm.size();253}254255*binary = (radv_shader_binary*)legacy_binary;256}257258259