Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/compiler/aco_interface.cpp
4550 views
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 = []()
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
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
77
aco_compile_shader(unsigned shader_count, struct nir_shader* const* shaders,
78
struct radv_shader_binary** binary, 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->disable_optimizations) {
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->disable_optimizations && !(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->disable_optimizations && !(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
char* data = NULL;
202
size_t disasm_size = 0;
203
struct u_memstream mem;
204
if (u_memstream_open(&mem, &data, &disasm_size)) {
205
FILE* const memf = u_memstream_get(&mem);
206
aco::print_asm(program.get(), code, exec_size / 4u, memf);
207
fputc(0, memf);
208
u_memstream_close(&mem);
209
}
210
211
disasm = std::string(data, data + disasm_size);
212
size += disasm_size;
213
free(data);
214
}
215
216
size_t stats_size = 0;
217
if (program->collect_statistics)
218
stats_size = aco::num_statistics * sizeof(uint32_t);
219
size += stats_size;
220
221
size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
222
/* We need to calloc to prevent unintialized data because this will be used
223
* directly for the disk cache. Uninitialized data can appear because of
224
* padding in the struct or because legacy_binary->data can be at an offset
225
* from the start less than sizeof(radv_shader_binary_legacy). */
226
radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*)calloc(size, 1);
227
228
legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
229
legacy_binary->base.stage = shaders[shader_count - 1]->info.stage;
230
legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
231
legacy_binary->base.total_size = size;
232
233
if (program->collect_statistics)
234
memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
235
legacy_binary->stats_size = stats_size;
236
237
memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(),
238
code.size() * sizeof(uint32_t));
239
legacy_binary->exec_size = exec_size;
240
legacy_binary->code_size = code.size() * sizeof(uint32_t);
241
242
legacy_binary->config = config;
243
legacy_binary->disasm_size = 0;
244
legacy_binary->ir_size = llvm_ir.size();
245
246
llvm_ir.copy((char*)legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size,
247
llvm_ir.size());
248
249
if (get_disasm) {
250
disasm.copy((char*)legacy_binary->data + legacy_binary->stats_size +
251
legacy_binary->code_size + llvm_ir.size(),
252
disasm.size());
253
legacy_binary->disasm_size = disasm.size();
254
}
255
256
*binary = (radv_shader_binary*)legacy_binary;
257
}
258
259