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"
29 #include "vulkan/radv_shader.h"
30 #include "vulkan/radv_shader_args.h"
32 #include "util/memstream.h"
38 static const std::array<aco_compiler_statistic_info, aco::num_statistics> statistic_infos = []()
40 std::array<aco_compiler_statistic_info, aco::num_statistics> ret{};
41 ret[aco::statistic_hash] =
42 aco_compiler_statistic_info{"Hash", "CRC32 hash of code and constant data"};
43 ret[aco::statistic_instructions] =
44 aco_compiler_statistic_info{"Instructions", "Instruction count"};
45 ret[aco::statistic_copies] =
46 aco_compiler_statistic_info{"Copies", "Copy instructions created for pseudo-instructions"};
47 ret[aco::statistic_branches] = aco_compiler_statistic_info{"Branches", "Branch instructions"};
48 ret[aco::statistic_latency] =
49 aco_compiler_statistic_info{"Latency", "Issue cycles plus stall cycles"};
50 ret[aco::statistic_inv_throughput] = aco_compiler_statistic_info{
51 "Inverse Throughput", "Estimated busy cycles to execute one wave"};
52 ret[aco::statistic_vmem_clauses] = aco_compiler_statistic_info{
53 "VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"};
54 ret[aco::statistic_smem_clauses] = aco_compiler_statistic_info{
55 "SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"};
56 ret[aco::statistic_sgpr_presched] =
57 aco_compiler_statistic_info{"Pre-Sched SGPRs", "SGPR usage before scheduling"};
58 ret[aco::statistic_vgpr_presched] =
59 aco_compiler_statistic_info{"Pre-Sched VGPRs", "VGPR usage before scheduling"};
63 const unsigned aco_num_statistics = aco::num_statistics;
64 const aco_compiler_statistic_info* aco_statistic_infos = statistic_infos.data();
67 validate(aco::Program* program)
69 if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
72 ASSERTED bool is_valid = aco::validate_ir(program);
77 aco_compile_shader(const struct radv_nir_compiler_options* options,
78 const struct radv_shader_info* info,
79 unsigned shader_count, struct nir_shader* const* shaders,
80 const struct radv_shader_args *args,
81 struct radv_shader_binary** binary)
85 ac_shader_config config = {0};
86 std::unique_ptr<aco::Program> program{new aco::Program};
88 program->collect_statistics = options->record_stats;
89 if (program->collect_statistics)
90 memset(program->statistics, 0, sizeof(program->statistics));
92 program->debug.func = options->debug.func;
93 program->debug.private_data = options->debug.private_data;
95 /* Instruction Selection */
96 if (args->is_gs_copy_shader)
97 aco::select_gs_copy_shader(program.get(), shaders[0], &config, options, info, args);
98 else if (args->is_trap_handler_shader)
99 aco::select_trap_handler_shader(program.get(), shaders[0], &config, options, info, args);
101 aco::select_program(program.get(), shader_count, shaders, &config, options, info, args);
102 if (options->dump_preoptir)
103 aco_print_program(program.get(), stderr);
106 if (!args->is_trap_handler_shader) {
108 aco::lower_phis(program.get());
109 aco::dominator_tree(program.get());
110 validate(program.get());
113 if (!options->key.optimisations_disabled) {
114 if (!(aco::debug_flags & aco::DEBUG_NO_VN))
115 aco::value_numbering(program.get());
116 if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
117 aco::optimize(program.get());
120 /* cleanup and exec mask handling */
121 aco::setup_reduce_temp(program.get());
122 aco::insert_exec_mask(program.get());
123 validate(program.get());
125 /* spilling and scheduling */
126 live_vars = aco::live_var_analysis(program.get());
127 aco::spill(program.get(), live_vars);
131 if (options->record_ir) {
135 if (u_memstream_open(&mem, &data, &size)) {
136 FILE* const memf = u_memstream_get(&mem);
137 aco_print_program(program.get(), memf);
139 u_memstream_close(&mem);
142 llvm_ir = std::string(data, data + size);
146 if (program->collect_statistics)
147 aco::collect_presched_stats(program.get());
149 if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && options->dump_shader)
150 aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);
152 if (!args->is_trap_handler_shader) {
153 if (!options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_SCHED))
154 aco::schedule_program(program.get(), live_vars);
155 validate(program.get());
157 /* Register Allocation */
158 aco::register_allocation(program.get(), live_vars.live_out);
160 if (aco::validate_ra(program.get())) {
161 aco_print_program(program.get(), stderr);
163 } else if (options->dump_shader) {
164 aco_print_program(program.get(), stderr);
167 validate(program.get());
170 if (!options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {
171 aco::optimize_postRA(program.get());
172 validate(program.get());
175 aco::ssa_elimination(program.get());
178 /* Lower to HW Instructions */
179 aco::lower_to_hw_instr(program.get());
182 aco::insert_wait_states(program.get());
183 aco::insert_NOPs(program.get());
185 if (program->chip_class >= GFX10)
186 aco::form_hard_clauses(program.get());
188 if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))
189 aco::collect_preasm_stats(program.get());
192 std::vector<uint32_t> code;
193 unsigned exec_size = aco::emit_program(program.get(), code);
195 if (program->collect_statistics)
196 aco::collect_postasm_stats(program.get(), code);
198 bool get_disasm = options->dump_shader || options->record_ir;
200 size_t size = llvm_ir.size();
204 if (check_print_asm_support(program.get())) {
206 size_t disasm_size = 0;
207 struct u_memstream mem;
208 if (u_memstream_open(&mem, &data, &disasm_size)) {
209 FILE* const memf = u_memstream_get(&mem);
210 aco::print_asm(program.get(), code, exec_size / 4u, memf);
212 u_memstream_close(&mem);
215 disasm = std::string(data, data + disasm_size);
219 disasm = "Shader disassembly is not supported in the current configuration"
220 #ifndef LLVM_AVAILABLE
221 " (LLVM not available)"
224 size += disasm.length();
228 size_t stats_size = 0;
229 if (program->collect_statistics)
230 stats_size = aco::num_statistics * sizeof(uint32_t);
233 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
234 /* We need to calloc to prevent unintialized data because this will be used
235 * directly for the disk cache. Uninitialized data can appear because of
236 * padding in the struct or because legacy_binary->data can be at an offset
237 * from the start less than sizeof(radv_shader_binary_legacy). */
238 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*)calloc(size, 1);
240 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
241 legacy_binary->base.stage = shaders[shader_count - 1]->info.stage;
242 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
243 legacy_binary->base.total_size = size;
245 if (program->collect_statistics)
246 memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
247 legacy_binary->stats_size = stats_size;
249 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(),
250 code.size() * sizeof(uint32_t));
251 legacy_binary->exec_size = exec_size;
252 legacy_binary->code_size = code.size() * sizeof(uint32_t);
254 legacy_binary->base.config = config;
255 legacy_binary->disasm_size = 0;
256 legacy_binary->ir_size = llvm_ir.size();
258 llvm_ir.copy((char*)legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size,
262 disasm.copy((char*)legacy_binary->data + legacy_binary->stats_size +
263 legacy_binary->code_size + llvm_ir.size(),
265 legacy_binary->disasm_size = disasm.size();
268 *binary = (radv_shader_binary*)legacy_binary;
272 aco_compile_vs_prolog(const struct radv_nir_compiler_options* options,
273 const struct radv_shader_info* info,
274 const struct radv_vs_prolog_key* key,
275 const struct radv_shader_args* args,
276 struct radv_prolog_binary** binary)
281 ac_shader_config config = {0};
282 std::unique_ptr<aco::Program> program{new aco::Program};
283 program->collect_statistics = false;
284 program->debug.func = NULL;
285 program->debug.private_data = NULL;
288 unsigned num_preserved_sgprs;
289 aco::select_vs_prolog(program.get(), key, &config, options, info, args, &num_preserved_sgprs);
290 aco::insert_NOPs(program.get());
292 if (options->dump_shader)
293 aco_print_program(program.get(), stderr);
296 std::vector<uint32_t> code;
297 code.reserve(align(program->blocks[0].instructions.size() * 2, 16));
298 unsigned exec_size = aco::emit_program(program.get(), code);
300 if (options->dump_shader) {
301 if (check_print_asm_support(program.get())) {
302 aco::print_asm(program.get(), code, exec_size / 4u, stderr);
303 fprintf(stderr, "\n");
305 fprintf(stderr, "Shader disassembly is not supported in the current configuration"
306 #ifndef LLVM_AVAILABLE
307 " (LLVM not available)"
313 /* copy into binary */
314 size_t size = code.size() * sizeof(uint32_t) + sizeof(radv_prolog_binary);
315 radv_prolog_binary* prolog_binary = (radv_prolog_binary*)calloc(size, 1);
317 prolog_binary->num_sgprs = config.num_sgprs;
318 prolog_binary->num_vgprs = config.num_vgprs;
319 prolog_binary->num_preserved_sgprs = num_preserved_sgprs;
320 prolog_binary->code_size = code.size() * sizeof(uint32_t);
321 memcpy(prolog_binary->data, code.data(), prolog_binary->code_size);
323 *binary = prolog_binary;