2 * Copyright © 2018 Google
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:
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
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
24 #include "aco_interface.h"
26 #include "vulkan/radv_shader.h"
27 #include "vulkan/radv_shader_args.h"
28 #include "c11/threads.h"
29 #include "util/debug.h"
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"},
48 static void validate(aco::Program
*program
)
50 if (!(aco::debug_flags
& aco::DEBUG_VALIDATE
))
53 bool is_valid
= aco::validate(program
, stderr
);
57 void aco_compile_shader(unsigned shader_count
,
58 struct nir_shader
*const *shaders
,
59 struct radv_shader_binary
**binary
,
60 struct radv_shader_args
*args
)
64 ac_shader_config config
= {0};
65 std::unique_ptr
<aco::Program
> program
{new aco::Program
};
67 program
->collect_statistics
= args
->options
->record_stats
;
68 if (program
->collect_statistics
)
69 memset(program
->statistics
, 0, sizeof(program
->statistics
));
71 /* Instruction Selection */
72 if (args
->is_gs_copy_shader
)
73 aco::select_gs_copy_shader(program
.get(), shaders
[0], &config
, args
);
75 aco::select_program(program
.get(), shader_count
, shaders
, &config
, args
);
76 if (args
->options
->dump_preoptir
) {
77 std::cerr
<< "After Instruction Selection:\n";
78 aco_print_program(program
.get(), stderr
);
82 aco::lower_phis(program
.get());
83 aco::dominator_tree(program
.get());
84 validate(program
.get());
87 aco::value_numbering(program
.get());
88 aco::optimize(program
.get());
90 /* cleanup and exec mask handling */
91 aco::setup_reduce_temp(program
.get());
92 aco::insert_exec_mask(program
.get());
93 validate(program
.get());
95 /* spilling and scheduling */
96 aco::live live_vars
= aco::live_var_analysis(program
.get(), args
->options
);
97 aco::spill(program
.get(), live_vars
, args
->options
);
98 if (program
->collect_statistics
)
99 aco::collect_presched_stats(program
.get());
100 aco::schedule_program(program
.get(), live_vars
);
101 validate(program
.get());
104 if (args
->options
->record_ir
) {
107 FILE *f
= open_memstream(&data
, &size
);
109 aco_print_program(program
.get(), f
);
114 llvm_ir
= std::string(data
, data
+ size
);
118 /* Register Allocation */
119 aco::register_allocation(program
.get(), live_vars
.live_out
);
120 if (args
->options
->dump_shader
) {
121 std::cerr
<< "After RA:\n";
122 aco_print_program(program
.get(), stderr
);
125 if (aco::validate_ra(program
.get(), args
->options
, stderr
)) {
126 std::cerr
<< "Program after RA validation failure:\n";
127 aco_print_program(program
.get(), stderr
);
131 validate(program
.get());
133 /* Lower to HW Instructions */
134 aco::ssa_elimination(program
.get());
135 aco::lower_to_hw_instr(program
.get());
138 aco::insert_wait_states(program
.get());
139 aco::insert_NOPs(program
.get());
141 if (program
->collect_statistics
)
142 aco::collect_preasm_stats(program
.get());
145 std::vector
<uint32_t> code
;
146 unsigned exec_size
= aco::emit_program(program
.get(), code
);
148 if (program
->collect_statistics
)
149 aco::collect_postasm_stats(program
.get(), code
);
151 bool get_disasm
= args
->options
->dump_shader
|| args
->options
->record_ir
;
153 size_t size
= llvm_ir
.size();
157 std::ostringstream stream
;
158 aco::print_asm(program
.get(), code
, exec_size
/ 4u, stream
);
160 disasm
= stream
.str();
161 size
+= disasm
.size();
164 size_t stats_size
= 0;
165 if (program
->collect_statistics
)
166 stats_size
= sizeof(radv_compiler_statistics
) + aco::num_statistics
* sizeof(uint32_t);
169 size
+= code
.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy
);
170 /* We need to calloc to prevent unintialized data because this will be used
171 * directly for the disk cache. Uninitialized data can appear because of
172 * padding in the struct or because legacy_binary->data can be at an offset
173 * from the start less than sizeof(radv_shader_binary_legacy). */
174 radv_shader_binary_legacy
* legacy_binary
= (radv_shader_binary_legacy
*) calloc(size
, 1);
176 legacy_binary
->base
.type
= RADV_BINARY_TYPE_LEGACY
;
177 legacy_binary
->base
.stage
= shaders
[shader_count
-1]->info
.stage
;
178 legacy_binary
->base
.is_gs_copy_shader
= args
->is_gs_copy_shader
;
179 legacy_binary
->base
.total_size
= size
;
181 if (program
->collect_statistics
) {
182 radv_compiler_statistics
*statistics
= (radv_compiler_statistics
*)legacy_binary
->data
;
183 statistics
->count
= aco::num_statistics
;
184 statistics
->infos
= statistic_infos
;
185 memcpy(statistics
->values
, program
->statistics
, aco::num_statistics
* sizeof(uint32_t));
187 legacy_binary
->stats_size
= stats_size
;
189 memcpy(legacy_binary
->data
+ legacy_binary
->stats_size
, code
.data(), code
.size() * sizeof(uint32_t));
190 legacy_binary
->exec_size
= exec_size
;
191 legacy_binary
->code_size
= code
.size() * sizeof(uint32_t);
193 legacy_binary
->config
= config
;
194 legacy_binary
->disasm_size
= 0;
195 legacy_binary
->ir_size
= llvm_ir
.size();
197 llvm_ir
.copy((char*) legacy_binary
->data
+ legacy_binary
->stats_size
+ legacy_binary
->code_size
, llvm_ir
.size());
200 disasm
.copy((char*) legacy_binary
->data
+ legacy_binary
->stats_size
+ legacy_binary
->code_size
+ llvm_ir
.size(), disasm
.size());
201 legacy_binary
->disasm_size
= disasm
.size();
204 *binary
= (radv_shader_binary
*) legacy_binary
;