2 * Copyright © 2018 Google
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "aco_interface.h"
28 #include "vulkan/radv_shader.h"
29 #include "vulkan/radv_shader_args.h"
31 #include "util/memstream.h"
37 static const std::array<aco_compiler_statistic_info, aco::num_statistics> statistic_infos = []()
39 std::array<aco_compiler_statistic_info, aco::num_statistics> ret{};
40 ret[aco::statistic_hash] = aco_compiler_statistic_info{"Hash", "CRC32 hash of code and constant data"};
41 ret[aco::statistic_instructions] = aco_compiler_statistic_info{"Instructions", "Instruction count"};
42 ret[aco::statistic_copies] = aco_compiler_statistic_info{"Copies", "Copy instructions created for pseudo-instructions"};
43 ret[aco::statistic_branches] = aco_compiler_statistic_info{"Branches", "Branch instructions"};
44 ret[aco::statistic_latency] = aco_compiler_statistic_info{"Latency", "Issue cycles plus stall cycles"};
45 ret[aco::statistic_inv_throughput] = aco_compiler_statistic_info{"Inverse Throughput", "Estimated busy cycles to execute one wave"};
46 ret[aco::statistic_vmem_clauses] = aco_compiler_statistic_info{"VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"};
47 ret[aco::statistic_smem_clauses] = aco_compiler_statistic_info{"SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"};
48 ret[aco::statistic_sgpr_presched] = aco_compiler_statistic_info{"Pre-Sched SGPRs", "SGPR usage before scheduling"};
49 ret[aco::statistic_vgpr_presched] = aco_compiler_statistic_info{"Pre-Sched VGPRs", "VGPR usage before scheduling"};
53 const unsigned aco_num_statistics = aco::num_statistics;
54 const aco_compiler_statistic_info *aco_statistic_infos = statistic_infos.data();
56 static void validate(aco::Program *program)
58 if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
61 ASSERTED bool is_valid = aco::validate_ir(program);
65 void aco_compile_shader(unsigned shader_count,
66 struct nir_shader *const *shaders,
67 struct radv_shader_binary **binary,
68 struct radv_shader_args *args)
72 ac_shader_config config = {0};
73 std::unique_ptr<aco::Program> program{new aco::Program};
75 program->collect_statistics = args->options->record_stats;
76 if (program->collect_statistics)
77 memset(program->statistics, 0, sizeof(program->statistics));
79 program->debug.func = args->options->debug.func;
80 program->debug.private_data = args->options->debug.private_data;
82 /* Instruction Selection */
83 if (args->is_gs_copy_shader)
84 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
85 else if (args->is_trap_handler_shader)
86 aco::select_trap_handler_shader(program.get(), shaders[0], &config, args);
88 aco::select_program(program.get(), shader_count, shaders, &config, args);
89 if (args->options->dump_preoptir)
90 aco_print_program(program.get(), stderr);
93 if (!args->is_trap_handler_shader) {
95 aco::lower_phis(program.get());
96 aco::dominator_tree(program.get());
97 validate(program.get());
100 if (!args->options->disable_optimizations) {
101 if (!(aco::debug_flags & aco::DEBUG_NO_VN))
102 aco::value_numbering(program.get());
103 if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
104 aco::optimize(program.get());
107 /* cleanup and exec mask handling */
108 aco::setup_reduce_temp(program.get());
109 aco::insert_exec_mask(program.get());
110 validate(program.get());
112 /* spilling and scheduling */
113 live_vars = aco::live_var_analysis(program.get());
114 aco::spill(program.get(), live_vars);
118 if (args->options->record_ir) {
122 if (u_memstream_open(&mem, &data, &size)) {
123 FILE *const memf = u_memstream_get(&mem);
124 aco_print_program(program.get(), memf);
126 u_memstream_close(&mem);
129 llvm_ir = std::string(data, data + size);
133 if (program->collect_statistics)
134 aco::collect_presched_stats(program.get());
136 if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && args->options->dump_shader)
137 aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);
139 if (!args->is_trap_handler_shader) {
140 if (!args->options->disable_optimizations &&
141 !(aco::debug_flags & aco::DEBUG_NO_SCHED))
142 aco::schedule_program(program.get(), live_vars);
143 validate(program.get());
145 /* Register Allocation */
146 aco::register_allocation(program.get(), live_vars.live_out);
148 if (aco::validate_ra(program.get())) {
149 aco_print_program(program.get(), stderr);
151 } else if (args->options->dump_shader) {
152 aco_print_program(program.get(), stderr);
155 validate(program.get());
158 if (!args->options->disable_optimizations && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {
159 aco::optimize_postRA(program.get());
160 validate(program.get());
163 aco::ssa_elimination(program.get());
166 /* Lower to HW Instructions */
167 aco::lower_to_hw_instr(program.get());
170 aco::insert_wait_states(program.get());
171 aco::insert_NOPs(program.get());
173 if (program->chip_class >= GFX10)
174 aco::form_hard_clauses(program.get());
176 if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))
177 aco::collect_preasm_stats(program.get());
180 std::vector<uint32_t> code;
181 unsigned exec_size = aco::emit_program(program.get(), code);
183 if (program->collect_statistics)
184 aco::collect_postasm_stats(program.get(), code);
186 bool get_disasm = args->options->dump_shader || args->options->record_ir;
188 size_t size = llvm_ir.size();
193 size_t disasm_size = 0;
194 struct u_memstream mem;
195 if (u_memstream_open(&mem, &data, &disasm_size)) {
196 FILE *const memf = u_memstream_get(&mem);
197 aco::print_asm(program.get(), code, exec_size / 4u, memf);
199 u_memstream_close(&mem);
202 disasm = std::string(data, data + disasm_size);
207 size_t stats_size = 0;
208 if (program->collect_statistics)
209 stats_size = aco::num_statistics * sizeof(uint32_t);
212 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
213 /* We need to calloc to prevent unintialized data because this will be used
214 * directly for the disk cache. Uninitialized data can appear because of
215 * padding in the struct or because legacy_binary->data can be at an offset
216 * from the start less than sizeof(radv_shader_binary_legacy). */
217 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
219 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
220 legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
221 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
222 legacy_binary->base.total_size = size;
224 if (program->collect_statistics)
225 memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
226 legacy_binary->stats_size = stats_size;
228 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(), code.size() * sizeof(uint32_t));
229 legacy_binary->exec_size = exec_size;
230 legacy_binary->code_size = code.size() * sizeof(uint32_t);
232 legacy_binary->config = config;
233 legacy_binary->disasm_size = 0;
234 legacy_binary->ir_size = llvm_ir.size();
236 llvm_ir.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size, llvm_ir.size());
239 disasm.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size + llvm_ir.size(), disasm.size());
240 legacy_binary->disasm_size = disasm.size();
243 *binary = (radv_shader_binary*) legacy_binary;