radv, aco: collect statistics if requested but executables are not
[mesa.git] / src / amd / compiler / aco_interface.cpp
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 "vulkan/radv_shader.h"
27 #include "vulkan/radv_shader_args.h"
28 #include "c11/threads.h"
29 #include "util/debug.h"
30
31 #include <iostream>
32 #include <sstream>
33
34 namespace aco {
35 uint64_t debug_flags = 0;
36
37 static const struct debug_control aco_debug_options[] = {
38 {"validateir", DEBUG_VALIDATE},
39 {"validatera", DEBUG_VALIDATE_RA},
40 {"perfwarn", DEBUG_PERFWARN},
41 {NULL, 0}
42 };
43
44 static once_flag init_once_flag = ONCE_FLAG_INIT;
45
46 static void init()
47 {
48 debug_flags = parse_debug_string(getenv("ACO_DEBUG"), aco_debug_options);
49
50 #ifndef NDEBUG
51 /* enable some flags by default on debug builds */
52 debug_flags |= aco::DEBUG_VALIDATE;
53 #endif
54 }
55 }
56
57 static radv_compiler_statistic_info statistic_infos[] = {
58 [aco::statistic_hash] = {"Hash", "CRC32 hash of code and constant data"},
59 [aco::statistic_instructions] = {"Instructions", "Instruction count"},
60 [aco::statistic_copies] = {"Copies", "Copy instructions created for pseudo-instructions"},
61 [aco::statistic_branches] = {"Branches", "Branch instructions"},
62 [aco::statistic_cycles] = {"Busy Cycles", "Estimate of busy cycles"},
63 [aco::statistic_vmem_clauses] = {"VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"},
64 [aco::statistic_smem_clauses] = {"SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"},
65 [aco::statistic_vmem_score] = {"VMEM Score", "Average VMEM def-use distances"},
66 [aco::statistic_smem_score] = {"SMEM Score", "Average SMEM def-use distances"},
67 [aco::statistic_sgpr_presched] = {"Pre-Sched SGPRs", "SGPR usage before scheduling"},
68 [aco::statistic_vgpr_presched] = {"Pre-Sched VGPRs", "VGPR usage before scheduling"},
69 };
70
71 void aco_compile_shader(unsigned shader_count,
72 struct nir_shader *const *shaders,
73 struct radv_shader_binary **binary,
74 struct radv_shader_args *args)
75 {
76 call_once(&aco::init_once_flag, aco::init);
77
78 ac_shader_config config = {0};
79 std::unique_ptr<aco::Program> program{new aco::Program};
80
81 program->collect_statistics = args->options->record_stats;
82 if (program->collect_statistics)
83 memset(program->statistics, 0, sizeof(program->statistics));
84
85 /* Instruction Selection */
86 if (args->is_gs_copy_shader)
87 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
88 else
89 aco::select_program(program.get(), shader_count, shaders, &config, args);
90 if (args->options->dump_preoptir) {
91 std::cerr << "After Instruction Selection:\n";
92 aco_print_program(program.get(), stderr);
93 }
94 aco::validate(program.get(), stderr);
95
96 /* Boolean phi lowering */
97 aco::lower_bool_phis(program.get());
98 //std::cerr << "After Boolean Phi Lowering:\n";
99 //aco_print_program(program.get(), stderr);
100
101 aco::dominator_tree(program.get());
102
103 /* Optimization */
104 aco::value_numbering(program.get());
105 aco::optimize(program.get());
106 aco::validate(program.get(), stderr);
107
108 aco::setup_reduce_temp(program.get());
109 aco::insert_exec_mask(program.get());
110 aco::validate(program.get(), stderr);
111
112 aco::live live_vars = aco::live_var_analysis(program.get(), args->options);
113 aco::spill(program.get(), live_vars, args->options);
114
115 if (program->collect_statistics)
116 aco::collect_presched_stats(program.get());
117
118 //std::cerr << "Before Schedule:\n";
119 //aco_print_program(program.get(), stderr);
120 aco::schedule_program(program.get(), live_vars);
121
122 std::string llvm_ir;
123 if (args->options->record_ir) {
124 char *data = NULL;
125 size_t size = 0;
126 FILE *f = open_memstream(&data, &size);
127 if (f) {
128 aco_print_program(program.get(), f);
129 fputc(0, f);
130 fclose(f);
131 }
132
133 llvm_ir = std::string(data, data + size);
134 free(data);
135 }
136
137 /* Register Allocation */
138 aco::register_allocation(program.get(), live_vars.live_out);
139 if (args->options->dump_shader) {
140 std::cerr << "After RA:\n";
141 aco_print_program(program.get(), stderr);
142 }
143
144 if (aco::validate_ra(program.get(), args->options, stderr)) {
145 std::cerr << "Program after RA validation failure:\n";
146 aco_print_program(program.get(), stderr);
147 abort();
148 }
149
150 aco::ssa_elimination(program.get());
151 /* Lower to HW Instructions */
152 aco::lower_to_hw_instr(program.get());
153 //std::cerr << "After Eliminate Pseudo Instr:\n";
154 //aco_print_program(program.get(), stderr);
155
156 /* Insert Waitcnt */
157 aco::insert_wait_states(program.get());
158 aco::insert_NOPs(program.get());
159
160 //std::cerr << "After Insert-Waitcnt:\n";
161 //aco_print_program(program.get(), stderr);
162
163 if (program->collect_statistics)
164 aco::collect_preasm_stats(program.get());
165
166 /* Assembly */
167 std::vector<uint32_t> code;
168 unsigned exec_size = aco::emit_program(program.get(), code);
169
170 if (program->collect_statistics)
171 aco::collect_postasm_stats(program.get(), code);
172
173 bool get_disasm = args->options->dump_shader || args->options->record_ir;
174
175 size_t size = llvm_ir.size();
176
177 std::string disasm;
178 if (get_disasm) {
179 std::ostringstream stream;
180 aco::print_asm(program.get(), code, exec_size / 4u, stream);
181 stream << '\0';
182 disasm = stream.str();
183 size += disasm.size();
184 }
185
186 size_t stats_size = 0;
187 if (program->collect_statistics)
188 stats_size = sizeof(radv_compiler_statistics) + aco::num_statistics * sizeof(uint32_t);
189 size += stats_size;
190
191 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
192 /* We need to calloc to prevent unintialized data because this will be used
193 * directly for the disk cache. Uninitialized data can appear because of
194 * padding in the struct or because legacy_binary->data can be at an offset
195 * from the start less than sizeof(radv_shader_binary_legacy). */
196 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
197
198 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
199 legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
200 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
201 legacy_binary->base.total_size = size;
202
203 if (program->collect_statistics) {
204 radv_compiler_statistics *statistics = (radv_compiler_statistics *)legacy_binary->data;
205 statistics->count = aco::num_statistics;
206 statistics->infos = statistic_infos;
207 memcpy(statistics->values, program->statistics, aco::num_statistics * sizeof(uint32_t));
208 }
209 legacy_binary->stats_size = stats_size;
210
211 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(), code.size() * sizeof(uint32_t));
212 legacy_binary->exec_size = exec_size;
213 legacy_binary->code_size = code.size() * sizeof(uint32_t);
214
215 legacy_binary->config = config;
216 legacy_binary->disasm_size = 0;
217 legacy_binary->ir_size = llvm_ir.size();
218
219 llvm_ir.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size, llvm_ir.size());
220
221 if (get_disasm) {
222 disasm.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size + llvm_ir.size(), disasm.size());
223 legacy_binary->disasm_size = disasm.size();
224 }
225
226 *binary = (radv_shader_binary*) legacy_binary;
227 }