aco: don't create v_mad_f32 on GFX10.3
[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
29 #include <iostream>
30 #include <sstream>
31
32 static radv_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
46 static void validate(aco::Program *program)
47 {
48 if (!(aco::debug_flags & aco::DEBUG_VALIDATE))
49 return;
50
51 bool is_valid = aco::validate(program, stderr);
52 assert(is_valid);
53 }
54
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 /* Instruction Selection */
70 if (args->is_gs_copy_shader)
71 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
72 else
73 aco::select_program(program.get(), shader_count, shaders, &config, args);
74 if (args->options->dump_preoptir) {
75 std::cerr << "After Instruction Selection:\n";
76 aco_print_program(program.get(), stderr);
77 }
78
79 /* Phi lowering */
80 aco::lower_phis(program.get());
81 aco::dominator_tree(program.get());
82 validate(program.get());
83
84 /* Optimization */
85 aco::value_numbering(program.get());
86 aco::optimize(program.get());
87
88 /* cleanup and exec mask handling */
89 aco::setup_reduce_temp(program.get());
90 aco::insert_exec_mask(program.get());
91 validate(program.get());
92
93 /* spilling and scheduling */
94 aco::live live_vars = aco::live_var_analysis(program.get(), args->options);
95 aco::spill(program.get(), live_vars, args->options);
96
97 std::string llvm_ir;
98 if (args->options->record_ir) {
99 char *data = NULL;
100 size_t size = 0;
101 FILE *f = open_memstream(&data, &size);
102 if (f) {
103 aco_print_program(program.get(), f);
104 fputc(0, f);
105 fclose(f);
106 }
107
108 llvm_ir = std::string(data, data + size);
109 free(data);
110 }
111
112 if (program->collect_statistics)
113 aco::collect_presched_stats(program.get());
114 aco::schedule_program(program.get(), live_vars);
115 validate(program.get());
116
117 /* Register Allocation */
118 aco::register_allocation(program.get(), live_vars.live_out);
119 if (args->options->dump_shader) {
120 std::cerr << "After RA:\n";
121 aco_print_program(program.get(), stderr);
122 }
123
124 if (aco::validate_ra(program.get(), args->options, stderr)) {
125 std::cerr << "Program after RA validation failure:\n";
126 aco_print_program(program.get(), stderr);
127 abort();
128 }
129
130 validate(program.get());
131
132 /* Lower to HW Instructions */
133 aco::ssa_elimination(program.get());
134 aco::lower_to_hw_instr(program.get());
135
136 /* Insert Waitcnt */
137 aco::insert_wait_states(program.get());
138 aco::insert_NOPs(program.get());
139
140 if (program->collect_statistics)
141 aco::collect_preasm_stats(program.get());
142
143 /* Assembly */
144 std::vector<uint32_t> code;
145 unsigned exec_size = aco::emit_program(program.get(), code);
146
147 if (program->collect_statistics)
148 aco::collect_postasm_stats(program.get(), code);
149
150 bool get_disasm = args->options->dump_shader || args->options->record_ir;
151
152 size_t size = llvm_ir.size();
153
154 std::string disasm;
155 if (get_disasm) {
156 std::ostringstream stream;
157 aco::print_asm(program.get(), code, exec_size / 4u, stream);
158 stream << '\0';
159 disasm = stream.str();
160 size += disasm.size();
161 }
162
163 size_t stats_size = 0;
164 if (program->collect_statistics)
165 stats_size = sizeof(radv_compiler_statistics) + aco::num_statistics * sizeof(uint32_t);
166 size += stats_size;
167
168 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
169 /* We need to calloc to prevent unintialized data because this will be used
170 * directly for the disk cache. Uninitialized data can appear because of
171 * padding in the struct or because legacy_binary->data can be at an offset
172 * from the start less than sizeof(radv_shader_binary_legacy). */
173 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
174
175 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
176 legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
177 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
178 legacy_binary->base.total_size = size;
179
180 if (program->collect_statistics) {
181 radv_compiler_statistics *statistics = (radv_compiler_statistics *)legacy_binary->data;
182 statistics->count = aco::num_statistics;
183 statistics->infos = statistic_infos;
184 memcpy(statistics->values, program->statistics, aco::num_statistics * sizeof(uint32_t));
185 }
186 legacy_binary->stats_size = stats_size;
187
188 memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(), code.size() * sizeof(uint32_t));
189 legacy_binary->exec_size = exec_size;
190 legacy_binary->code_size = code.size() * sizeof(uint32_t);
191
192 legacy_binary->config = config;
193 legacy_binary->disasm_size = 0;
194 legacy_binary->ir_size = llvm_ir.size();
195
196 llvm_ir.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size, llvm_ir.size());
197
198 if (get_disasm) {
199 disasm.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size + llvm_ir.size(), disasm.size());
200 legacy_binary->disasm_size = disasm.size();
201 }
202
203 *binary = (radv_shader_binary*) legacy_binary;
204 }