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"
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"},
46 static void validate(aco::Program
*program
)
48 if (!(aco::debug_flags
& aco::DEBUG_VALIDATE_IR
))
51 ASSERTED
bool is_valid
= aco::validate_ir(program
);
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
)
62 ac_shader_config config
= {0};
63 std::unique_ptr
<aco::Program
> program
{new aco::Program
};
65 program
->collect_statistics
= args
->options
->record_stats
;
66 if (program
->collect_statistics
)
67 memset(program
->statistics
, 0, sizeof(program
->statistics
));
69 program
->debug
.func
= args
->options
->debug
.func
;
70 program
->debug
.private_data
= args
->options
->debug
.private_data
;
72 /* Instruction Selection */
73 if (args
->is_gs_copy_shader
)
74 aco::select_gs_copy_shader(program
.get(), shaders
[0], &config
, args
);
75 else if (args
->is_trap_handler_shader
)
76 aco::select_trap_handler_shader(program
.get(), shaders
[0], &config
, args
);
78 aco::select_program(program
.get(), shader_count
, shaders
, &config
, args
);
79 if (args
->options
->dump_preoptir
) {
80 std::cerr
<< "After Instruction Selection:\n";
81 aco_print_program(program
.get(), stderr
);
85 if (!args
->is_trap_handler_shader
) {
87 aco::lower_phis(program
.get());
88 aco::dominator_tree(program
.get());
89 validate(program
.get());
92 if (!(aco::debug_flags
& aco::DEBUG_NO_VN
))
93 aco::value_numbering(program
.get());
94 if (!(aco::debug_flags
& aco::DEBUG_NO_OPT
))
95 aco::optimize(program
.get());
97 /* cleanup and exec mask handling */
98 aco::setup_reduce_temp(program
.get());
99 aco::insert_exec_mask(program
.get());
100 validate(program
.get());
102 /* spilling and scheduling */
103 live_vars
= aco::live_var_analysis(program
.get(), args
->options
);
104 aco::spill(program
.get(), live_vars
, args
->options
);
108 if (args
->options
->record_ir
) {
111 FILE *f
= open_memstream(&data
, &size
);
113 aco_print_program(program
.get(), f
);
118 llvm_ir
= std::string(data
, data
+ size
);
122 if (program
->collect_statistics
)
123 aco::collect_presched_stats(program
.get());
125 if (!args
->is_trap_handler_shader
) {
126 if (!(aco::debug_flags
& aco::DEBUG_NO_SCHED
))
127 aco::schedule_program(program
.get(), live_vars
);
128 validate(program
.get());
130 /* Register Allocation */
131 aco::register_allocation(program
.get(), live_vars
.live_out
);
132 if (args
->options
->dump_shader
) {
133 std::cerr
<< "After RA:\n";
134 aco_print_program(program
.get(), stderr
);
137 if (aco::validate_ra(program
.get(), args
->options
)) {
138 std::cerr
<< "Program after RA validation failure:\n";
139 aco_print_program(program
.get(), stderr
);
143 validate(program
.get());
145 aco::ssa_elimination(program
.get());
148 /* Lower to HW Instructions */
149 aco::lower_to_hw_instr(program
.get());
152 aco::insert_wait_states(program
.get());
153 aco::insert_NOPs(program
.get());
155 if (program
->collect_statistics
)
156 aco::collect_preasm_stats(program
.get());
159 std::vector
<uint32_t> code
;
160 unsigned exec_size
= aco::emit_program(program
.get(), code
);
162 if (program
->collect_statistics
)
163 aco::collect_postasm_stats(program
.get(), code
);
165 bool get_disasm
= args
->options
->dump_shader
|| args
->options
->record_ir
;
167 size_t size
= llvm_ir
.size();
171 std::ostringstream stream
;
172 aco::print_asm(program
.get(), code
, exec_size
/ 4u, stream
);
174 disasm
= stream
.str();
175 size
+= disasm
.size();
178 size_t stats_size
= 0;
179 if (program
->collect_statistics
)
180 stats_size
= sizeof(radv_compiler_statistics
) + aco::num_statistics
* sizeof(uint32_t);
183 size
+= code
.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy
);
184 /* We need to calloc to prevent unintialized data because this will be used
185 * directly for the disk cache. Uninitialized data can appear because of
186 * padding in the struct or because legacy_binary->data can be at an offset
187 * from the start less than sizeof(radv_shader_binary_legacy). */
188 radv_shader_binary_legacy
* legacy_binary
= (radv_shader_binary_legacy
*) calloc(size
, 1);
190 legacy_binary
->base
.type
= RADV_BINARY_TYPE_LEGACY
;
191 legacy_binary
->base
.stage
= shaders
[shader_count
-1]->info
.stage
;
192 legacy_binary
->base
.is_gs_copy_shader
= args
->is_gs_copy_shader
;
193 legacy_binary
->base
.total_size
= size
;
195 if (program
->collect_statistics
) {
196 radv_compiler_statistics
*statistics
= (radv_compiler_statistics
*)legacy_binary
->data
;
197 statistics
->count
= aco::num_statistics
;
198 statistics
->infos
= statistic_infos
;
199 memcpy(statistics
->values
, program
->statistics
, aco::num_statistics
* sizeof(uint32_t));
201 legacy_binary
->stats_size
= stats_size
;
203 memcpy(legacy_binary
->data
+ legacy_binary
->stats_size
, code
.data(), code
.size() * sizeof(uint32_t));
204 legacy_binary
->exec_size
= exec_size
;
205 legacy_binary
->code_size
= code
.size() * sizeof(uint32_t);
207 legacy_binary
->config
= config
;
208 legacy_binary
->disasm_size
= 0;
209 legacy_binary
->ir_size
= llvm_ir
.size();
211 llvm_ir
.copy((char*) legacy_binary
->data
+ legacy_binary
->stats_size
+ legacy_binary
->code_size
, llvm_ir
.size());
214 disasm
.copy((char*) legacy_binary
->data
+ legacy_binary
->stats_size
+ legacy_binary
->code_size
+ llvm_ir
.size(), disasm
.size());
215 legacy_binary
->disasm_size
= disasm
.size();
218 *binary
= (radv_shader_binary
*) legacy_binary
;