1 /*
2 * Copyright © 2018 Google
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include "aco_interface.h"
26
27 #include "aco_ir.h"
28
29 #include "vulkan/radv_shader.h"
30 #include "vulkan/radv_shader_args.h"
31
32 #include "util/memstream.h"
33
34 #include <array>
35 #include <iostream>
36 #include <vector>
37
38 static const std::array<aco_compiler_statistic_info, aco::num_statistics> statistic_infos = []()
__anon5fdcbb460102() 39 {
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"};
60 return ret;
61 }();
62
63 const unsigned aco_num_statistics = aco::num_statistics;
64 const aco_compiler_statistic_info* aco_statistic_infos = statistic_infos.data();
65
66 static void
validate(aco::Program * program)67 validate(aco::Program* program)
68 {
69 if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
70 return;
71
72 ASSERTED bool is_valid = aco::validate_ir(program);
73 assert(is_valid);
74 }
75
76 void
aco_compile_shader(unsigned shader_count,struct nir_shader * const * shaders,struct radv_shader_binary ** binary,const struct radv_shader_args * args)77 aco_compile_shader(unsigned shader_count, struct nir_shader* const* shaders,
78 struct radv_shader_binary** binary, const struct radv_shader_args* args)
79 {
80 aco::init();
81
82 ac_shader_config config = {0};
83 std::unique_ptr<aco::Program> program{new aco::Program};
84
85 program->collect_statistics = args->options->record_stats;
86 if (program->collect_statistics)
87 memset(program->statistics, 0, sizeof(program->statistics));
88
89 program->debug.func = args->options->debug.func;
90 program->debug.private_data = args->options->debug.private_data;
91
92 /* Instruction Selection */
93 if (args->is_gs_copy_shader)
94 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
95 else if (args->is_trap_handler_shader)
96 aco::select_trap_handler_shader(program.get(), shaders[0], &config, args);
97 else
98 aco::select_program(program.get(), shader_count, shaders, &config, args);
99 if (args->options->dump_preoptir)
100 aco_print_program(program.get(), stderr);
101
102 aco::live live_vars;
103 if (!args->is_trap_handler_shader) {
104 /* Phi lowering */
105 aco::lower_phis(program.get());
106 aco::dominator_tree(program.get());
107 validate(program.get());
108
109 /* Optimization */
110 if (!args->options->key.optimisations_disabled) {
111 if (!(aco::debug_flags & aco::DEBUG_NO_VN))
112 aco::value_numbering(program.get());
113 if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
114 aco::optimize(program.get());
115 }
116
117 /* cleanup and exec mask handling */
118 aco::setup_reduce_temp(program.get());
119 aco::insert_exec_mask(program.get());
120 validate(program.get());
121
122 /* spilling and scheduling */
123 live_vars = aco::live_var_analysis(program.get());
124 aco::spill(program.get(), live_vars);
125 }
126
127 std::string llvm_ir;
128 if (args->options->record_ir) {
129 char* data = NULL;
130 size_t size = 0;
131 u_memstream mem;
132 if (u_memstream_open(&mem, &data, &size)) {
133 FILE* const memf = u_memstream_get(&mem);
134 aco_print_program(program.get(), memf);
135 fputc(0, memf);
136 u_memstream_close(&mem);
137 }
138
139 llvm_ir = std::string(data, data + size);
140 free(data);
141 }
142
143 if (program->collect_statistics)
144 aco::collect_presched_stats(program.get());
145
146 if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && args->options->dump_shader)
147 aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);
148
149 if (!args->is_trap_handler_shader) {
150 if (!args->options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_SCHED))
151 aco::schedule_program(program.get(), live_vars);
152 validate(program.get());
153
154 /* Register Allocation */
155 aco::register_allocation(program.get(), live_vars.live_out);
156
157 if (aco::validate_ra(program.get())) {
158 aco_print_program(program.get(), stderr);
159 abort();
160 } else if (args->options->dump_shader) {
161 aco_print_program(program.get(), stderr);
162 }
163
164 validate(program.get());
165
166 /* Optimization */
167 if (!args->options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {
168 aco::optimize_postRA(program.get());
169 validate(program.get());
170 }
171
172 aco::ssa_elimination(program.get());
173 }
174
175 /* Lower to HW Instructions */
176 aco::lower_to_hw_instr(program.get());
177
178 /* Insert Waitcnt */
179 aco::insert_wait_states(program.get());
180 aco::insert_NOPs(program.get());
181
182 if (program->chip_class >= GFX10)
183 aco::form_hard_clauses(program.get());
184
185 if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))
186 aco::collect_preasm_stats(program.get());
187
188 /* Assembly */
189 std::vector<uint32_t> code;
190 unsigned exec_size = aco::emit_program(program.get(), code);
191
192 if (program->collect_statistics)
193 aco::collect_postasm_stats(program.get(), code);
194
195 bool get_disasm = args->options->dump_shader || args->options->record_ir;
196
197 size_t size = llvm_ir.size();
198
199 std::string disasm;
200 if (get_disasm) {
201 if (check_print_asm_support(program.get())) {
202 char* data = NULL;
203 size_t disasm_size = 0;
204 struct u_memstream mem;
205 if (u_memstream_open(&mem, &data, &disasm_size)) {
206 FILE* const memf = u_memstream_get(&mem);
207 aco::print_asm(program.get(), code, exec_size / 4u, memf);
208 fputc(0, memf);
209 u_memstream_close(&mem);
210 }
211
212 disasm = std::string(data, data + disasm_size);
213 size += disasm_size;
214 free(data);
215 } else {
216 disasm = "Shader disassembly is not supported in the current configuration"
217 #ifndef LLVM_AVAILABLE
218 " (LLVM not available)"
219 #endif
220 ".\n";
221 size += disasm.length();
222 }
223 }
224
225 size_t stats_size = 0;
226 if (program->collect_statistics)
227 stats_size = aco::num_statistics * sizeof(uint32_t);
228 size += stats_size;
229
230 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
231 /* We need to calloc to prevent unintialized data because this will be used
232 * directly for the disk cache. Uninitialized data can appear because of
233 * padding in the struct or because legacy_binary->data can be at an offset
234 * from the start less than sizeof(radv_shader_binary_legacy). */
235 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*)calloc(size, 1);
236
237 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
238 legacy_binary->base.stage = shaders[shader_count - 1]->info.stage;
239 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
240 legacy_binary->base.total_size = size;
241
242 if (program->collect_statistics)
243 memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
244 legacy_binary->stats_size = stats_size;
245
246 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(),
247 code.size() * sizeof(uint32_t));
248 legacy_binary->exec_size = exec_size;
249 legacy_binary->code_size = code.size() * sizeof(uint32_t);
250
251 legacy_binary->base.config = config;
252 legacy_binary->disasm_size = 0;
253 legacy_binary->ir_size = llvm_ir.size();
254
255 llvm_ir.copy((char*)legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size,
256 llvm_ir.size());
257
258 if (get_disasm) {
259 disasm.copy((char*)legacy_binary->data + legacy_binary->stats_size +
260 legacy_binary->code_size + llvm_ir.size(),
261 disasm.size());
262 legacy_binary->disasm_size = disasm.size();
263 }
264
265 *binary = (radv_shader_binary*)legacy_binary;
266 }
267
268 void
aco_compile_vs_prolog(const struct radv_vs_prolog_key * key,struct radv_prolog_binary ** binary,const struct radv_shader_args * args)269 aco_compile_vs_prolog(const struct radv_vs_prolog_key* key, struct radv_prolog_binary** binary,
270 const struct radv_shader_args* args)
271 {
272 aco::init();
273
274 /* create program */
275 ac_shader_config config = {0};
276 std::unique_ptr<aco::Program> program{new aco::Program};
277 program->collect_statistics = false;
278 program->debug.func = NULL;
279 program->debug.private_data = NULL;
280
281 /* create IR */
282 unsigned num_preserved_sgprs;
283 aco::select_vs_prolog(program.get(), key, &config, args, &num_preserved_sgprs);
284 aco::insert_NOPs(program.get());
285
286 if (args->options->dump_shader)
287 aco_print_program(program.get(), stderr);
288
289 /* assembly */
290 std::vector<uint32_t> code;
291 code.reserve(align(program->blocks[0].instructions.size() * 2, 16));
292 unsigned exec_size = aco::emit_program(program.get(), code);
293
294 if (args->options->dump_shader) {
295 aco::print_asm(program.get(), code, exec_size / 4u, stderr);
296 fprintf(stderr, "\n");
297 }
298
299 /* copy into binary */
300 size_t size = code.size() * sizeof(uint32_t) + sizeof(radv_prolog_binary);
301 radv_prolog_binary* prolog_binary = (radv_prolog_binary*)calloc(size, 1);
302
303 prolog_binary->num_sgprs = config.num_sgprs;
304 prolog_binary->num_vgprs = config.num_vgprs;
305 prolog_binary->num_preserved_sgprs = num_preserved_sgprs;
306 prolog_binary->code_size = code.size() * sizeof(uint32_t);
307 memcpy(prolog_binary->data, code.data(), prolog_binary->code_size);
308
309 *binary = prolog_binary;
310 }
311