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 #include "aco_interface.h"
25 #include "aco_ir.h"
26 #include "util/memstream.h"
27 #include "vulkan/radv_shader.h"
28 #include "vulkan/radv_shader_args.h"
29
30 #include <iostream>
31
32 static aco_compiler_statistic_info statistic_infos[] = {
33 [aco::statistic_hash] = {"Hash", "CRC32 hash of code and constant data"},
34 [aco::statistic_instructions] = {"Instructions", "Instruction count"},
35 [aco::statistic_copies] = {"Copies", "Copy instructions created for pseudo-instructions"},
36 [aco::statistic_branches] = {"Branches", "Branch instructions"},
37 [aco::statistic_cycles] = {"Busy Cycles", "Estimate of busy cycles"},
38 [aco::statistic_vmem_clauses] = {"VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"},
39 [aco::statistic_smem_clauses] = {"SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"},
40 [aco::statistic_vmem_score] = {"VMEM Score", "Average VMEM def-use distances"},
41 [aco::statistic_smem_score] = {"SMEM Score", "Average SMEM def-use distances"},
42 [aco::statistic_sgpr_presched] = {"Pre-Sched SGPRs", "SGPR usage before scheduling"},
43 [aco::statistic_vgpr_presched] = {"Pre-Sched VGPRs", "VGPR usage before scheduling"},
44 };
45
validate(aco::Program * program)46 static void validate(aco::Program *program)
47 {
48 if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
49 return;
50
51 ASSERTED bool is_valid = aco::validate_ir(program);
52 assert(is_valid);
53 }
54
aco_compile_shader(unsigned shader_count,struct nir_shader * const * shaders,struct radv_shader_binary ** binary,struct radv_shader_args * args)55 void aco_compile_shader(unsigned shader_count,
56 struct nir_shader *const *shaders,
57 struct radv_shader_binary **binary,
58 struct radv_shader_args *args)
59 {
60 aco::init();
61
62 ac_shader_config config = {0};
63 std::unique_ptr<aco::Program> program{new aco::Program};
64
65 program->collect_statistics = args->options->record_stats;
66 if (program->collect_statistics)
67 memset(program->statistics, 0, sizeof(program->statistics));
68
69 program->debug.func = args->options->debug.func;
70 program->debug.private_data = args->options->debug.private_data;
71
72 /* Instruction Selection */
73 if (args->is_gs_copy_shader)
74 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
75 else if (args->is_trap_handler_shader)
76 aco::select_trap_handler_shader(program.get(), shaders[0], &config, args);
77 else
78 aco::select_program(program.get(), shader_count, shaders, &config, args);
79 if (args->options->dump_preoptir) {
80 std::cerr << "After Instruction Selection:\n";
81 aco_print_program(program.get(), stderr);
82 }
83
84 aco::live live_vars;
85 if (!args->is_trap_handler_shader) {
86 /* Phi lowering */
87 aco::lower_phis(program.get());
88 aco::dominator_tree(program.get());
89 validate(program.get());
90
91 /* Optimization */
92 if (!args->options->disable_optimizations) {
93 if (!(aco::debug_flags & aco::DEBUG_NO_VN))
94 aco::value_numbering(program.get());
95 if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
96 aco::optimize(program.get());
97 }
98
99 /* cleanup and exec mask handling */
100 aco::setup_reduce_temp(program.get());
101 aco::insert_exec_mask(program.get());
102 validate(program.get());
103
104 /* spilling and scheduling */
105 live_vars = aco::live_var_analysis(program.get());
106 aco::spill(program.get(), live_vars);
107 }
108
109 std::string llvm_ir;
110 if (args->options->record_ir) {
111 char *data = NULL;
112 size_t size = 0;
113 u_memstream mem;
114 if (u_memstream_open(&mem, &data, &size)) {
115 FILE *const memf = u_memstream_get(&mem);
116 aco_print_program(program.get(), memf);
117 fputc(0, memf);
118 u_memstream_close(&mem);
119 }
120
121 llvm_ir = std::string(data, data + size);
122 free(data);
123 }
124
125 if (program->collect_statistics)
126 aco::collect_presched_stats(program.get());
127
128 if (!args->is_trap_handler_shader) {
129 if (!args->options->disable_optimizations &&
130 !(aco::debug_flags & aco::DEBUG_NO_SCHED))
131 aco::schedule_program(program.get(), live_vars);
132 validate(program.get());
133
134 /* Register Allocation */
135 aco::register_allocation(program.get(), live_vars.live_out);
136 if (args->options->dump_shader) {
137 std::cerr << "After RA:\n";
138 aco_print_program(program.get(), stderr);
139 }
140
141 if (aco::validate_ra(program.get())) {
142 std::cerr << "Program after RA validation failure:\n";
143 aco_print_program(program.get(), stderr);
144 abort();
145 }
146
147 validate(program.get());
148
149 aco::ssa_elimination(program.get());
150 }
151
152 /* Lower to HW Instructions */
153 aco::lower_to_hw_instr(program.get());
154
155 /* Insert Waitcnt */
156 aco::insert_wait_states(program.get());
157 aco::insert_NOPs(program.get());
158
159 if (program->chip_class >= GFX10)
160 aco::form_hard_clauses(program.get());
161
162 if (program->collect_statistics)
163 aco::collect_preasm_stats(program.get());
164
165 /* Assembly */
166 std::vector<uint32_t> code;
167 unsigned exec_size = aco::emit_program(program.get(), code);
168
169 if (program->collect_statistics)
170 aco::collect_postasm_stats(program.get(), code);
171
172 bool get_disasm = args->options->dump_shader || args->options->record_ir;
173
174 size_t size = llvm_ir.size();
175
176 std::string disasm;
177 if (get_disasm) {
178 char *data = NULL;
179 size_t disasm_size = 0;
180 FILE *f = open_memstream(&data, &disasm_size);
181 if (f) {
182 bool fail = aco::print_asm(program.get(), code, exec_size / 4u, f);
183 fputc(0, f);
184 fclose(f);
185
186 if (fail) {
187 fprintf(stderr, "Failed to disassemble program:\n");
188 aco_print_program(program.get(), stderr);
189 fputs(data, stderr);
190 abort();
191 }
192 }
193
194 disasm = std::string(data, data + disasm_size);
195 size += disasm_size;
196 free(data);
197 }
198
199 size_t stats_size = 0;
200 if (program->collect_statistics)
201 stats_size = sizeof(aco_compiler_statistics) + aco::num_statistics * sizeof(uint32_t);
202 size += stats_size;
203
204 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
205 /* We need to calloc to prevent unintialized data because this will be used
206 * directly for the disk cache. Uninitialized data can appear because of
207 * padding in the struct or because legacy_binary->data can be at an offset
208 * from the start less than sizeof(radv_shader_binary_legacy). */
209 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
210
211 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
212 legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
213 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
214 legacy_binary->base.total_size = size;
215
216 if (program->collect_statistics) {
217 aco_compiler_statistics *statistics = (aco_compiler_statistics *)legacy_binary->data;
218 statistics->count = aco::num_statistics;
219 statistics->infos = statistic_infos;
220 memcpy(statistics->values, program->statistics, aco::num_statistics * sizeof(uint32_t));
221 }
222 legacy_binary->stats_size = stats_size;
223
224 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(), code.size() * sizeof(uint32_t));
225 legacy_binary->exec_size = exec_size;
226 legacy_binary->code_size = code.size() * sizeof(uint32_t);
227
228 legacy_binary->config = config;
229 legacy_binary->disasm_size = 0;
230 legacy_binary->ir_size = llvm_ir.size();
231
232 llvm_ir.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size, llvm_ir.size());
233
234 if (get_disasm) {
235 disasm.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size + llvm_ir.size(), disasm.size());
236 legacy_binary->disasm_size = disasm.size();
237 }
238
239 *binary = (radv_shader_binary*) legacy_binary;
240 }
241