aco: move some setup code into helpers
[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 static radv_compiler_statistic_info statistic_infos[] = {
35 [aco::statistic_hash] = {"Hash", "CRC32 hash of code and constant data"},
36 [aco::statistic_instructions] = {"Instructions", "Instruction count"},
37 [aco::statistic_copies] = {"Copies", "Copy instructions created for pseudo-instructions"},
38 [aco::statistic_branches] = {"Branches", "Branch instructions"},
39 [aco::statistic_cycles] = {"Busy Cycles", "Estimate of busy cycles"},
40 [aco::statistic_vmem_clauses] = {"VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"},
41 [aco::statistic_smem_clauses] = {"SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"},
42 [aco::statistic_vmem_score] = {"VMEM Score", "Average VMEM def-use distances"},
43 [aco::statistic_smem_score] = {"SMEM Score", "Average SMEM def-use distances"},
44 [aco::statistic_sgpr_presched] = {"Pre-Sched SGPRs", "SGPR usage before scheduling"},
45 [aco::statistic_vgpr_presched] = {"Pre-Sched VGPRs", "VGPR usage before scheduling"},
46 };
47
48 void aco_compile_shader(unsigned shader_count,
49 struct nir_shader *const *shaders,
50 struct radv_shader_binary **binary,
51 struct radv_shader_args *args)
52 {
53 aco::init();
54
55 ac_shader_config config = {0};
56 std::unique_ptr<aco::Program> program{new aco::Program};
57
58 program->collect_statistics = args->options->record_stats;
59 if (program->collect_statistics)
60 memset(program->statistics, 0, sizeof(program->statistics));
61
62 /* Instruction Selection */
63 if (args->is_gs_copy_shader)
64 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
65 else
66 aco::select_program(program.get(), shader_count, shaders, &config, args);
67 if (args->options->dump_preoptir) {
68 std::cerr << "After Instruction Selection:\n";
69 aco_print_program(program.get(), stderr);
70 }
71
72 /* Phi lowering */
73 aco::lower_phis(program.get());
74 aco::dominator_tree(program.get());
75 aco::validate(program.get(), stderr);
76
77 /* Optimization */
78 aco::value_numbering(program.get());
79 aco::optimize(program.get());
80
81 /* cleanup and exec mask handling */
82 aco::setup_reduce_temp(program.get());
83 aco::insert_exec_mask(program.get());
84 aco::validate(program.get(), stderr);
85
86 /* spilling and scheduling */
87 aco::live live_vars = aco::live_var_analysis(program.get(), args->options);
88 aco::spill(program.get(), live_vars, args->options);
89 if (program->collect_statistics)
90 aco::collect_presched_stats(program.get());
91 aco::schedule_program(program.get(), live_vars);
92 aco::validate(program.get(), stderr);
93
94 std::string llvm_ir;
95 if (args->options->record_ir) {
96 char *data = NULL;
97 size_t size = 0;
98 FILE *f = open_memstream(&data, &size);
99 if (f) {
100 aco_print_program(program.get(), f);
101 fputc(0, f);
102 fclose(f);
103 }
104
105 llvm_ir = std::string(data, data + size);
106 free(data);
107 }
108
109 /* Register Allocation */
110 aco::register_allocation(program.get(), live_vars.live_out);
111 if (args->options->dump_shader) {
112 std::cerr << "After RA:\n";
113 aco_print_program(program.get(), stderr);
114 }
115
116 if (aco::validate_ra(program.get(), args->options, stderr)) {
117 std::cerr << "Program after RA validation failure:\n";
118 aco_print_program(program.get(), stderr);
119 abort();
120 }
121
122 aco::validate(program.get(), stderr);
123
124 /* Lower to HW Instructions */
125 aco::ssa_elimination(program.get());
126 aco::lower_to_hw_instr(program.get());
127
128 /* Insert Waitcnt */
129 aco::insert_wait_states(program.get());
130 aco::insert_NOPs(program.get());
131
132 if (program->collect_statistics)
133 aco::collect_preasm_stats(program.get());
134
135 /* Assembly */
136 std::vector<uint32_t> code;
137 unsigned exec_size = aco::emit_program(program.get(), code);
138
139 if (program->collect_statistics)
140 aco::collect_postasm_stats(program.get(), code);
141
142 bool get_disasm = args->options->dump_shader || args->options->record_ir;
143
144 size_t size = llvm_ir.size();
145
146 std::string disasm;
147 if (get_disasm) {
148 std::ostringstream stream;
149 aco::print_asm(program.get(), code, exec_size / 4u, stream);
150 stream << '\0';
151 disasm = stream.str();
152 size += disasm.size();
153 }
154
155 size_t stats_size = 0;
156 if (program->collect_statistics)
157 stats_size = sizeof(radv_compiler_statistics) + aco::num_statistics * sizeof(uint32_t);
158 size += stats_size;
159
160 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
161 /* We need to calloc to prevent unintialized data because this will be used
162 * directly for the disk cache. Uninitialized data can appear because of
163 * padding in the struct or because legacy_binary->data can be at an offset
164 * from the start less than sizeof(radv_shader_binary_legacy). */
165 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
166
167 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
168 legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
169 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
170 legacy_binary->base.total_size = size;
171
172 if (program->collect_statistics) {
173 radv_compiler_statistics *statistics = (radv_compiler_statistics *)legacy_binary->data;
174 statistics->count = aco::num_statistics;
175 statistics->infos = statistic_infos;
176 memcpy(statistics->values, program->statistics, aco::num_statistics * sizeof(uint32_t));
177 }
178 legacy_binary->stats_size = stats_size;
179
180 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(), code.size() * sizeof(uint32_t));
181 legacy_binary->exec_size = exec_size;
182 legacy_binary->code_size = code.size() * sizeof(uint32_t);
183
184 legacy_binary->config = config;
185 legacy_binary->disasm_size = 0;
186 legacy_binary->ir_size = llvm_ir.size();
187
188 llvm_ir.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size, llvm_ir.size());
189
190 if (get_disasm) {
191 disasm.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size + llvm_ir.size(), disasm.size());
192 legacy_binary->disasm_size = disasm.size();
193 }
194
195 *binary = (radv_shader_binary*) legacy_binary;
196 }