aco: add missing Licenses and remove Authors from files
[platform/upstream/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
25 #include "aco_interface.h"
26 #include "aco_ir.h"
27
28 #include "vulkan/radv_shader.h"
29 #include "vulkan/radv_shader_args.h"
30
31 #include "util/memstream.h"
32
33 #include <array>
34 #include <iostream>
35 #include <vector>
36
37 static const std::array<aco_compiler_statistic_info, aco::num_statistics> statistic_infos = []()
38 {
39    std::array<aco_compiler_statistic_info, aco::num_statistics> ret{};
40    ret[aco::statistic_hash] = aco_compiler_statistic_info{"Hash", "CRC32 hash of code and constant data"};
41    ret[aco::statistic_instructions] = aco_compiler_statistic_info{"Instructions", "Instruction count"};
42    ret[aco::statistic_copies] = aco_compiler_statistic_info{"Copies", "Copy instructions created for pseudo-instructions"};
43    ret[aco::statistic_branches] = aco_compiler_statistic_info{"Branches", "Branch instructions"};
44    ret[aco::statistic_latency] = aco_compiler_statistic_info{"Latency", "Issue cycles plus stall cycles"};
45    ret[aco::statistic_inv_throughput] = aco_compiler_statistic_info{"Inverse Throughput", "Estimated busy cycles to execute one wave"};
46    ret[aco::statistic_vmem_clauses] = aco_compiler_statistic_info{"VMEM Clause", "Number of VMEM clauses (includes 1-sized clauses)"};
47    ret[aco::statistic_smem_clauses] = aco_compiler_statistic_info{"SMEM Clause", "Number of SMEM clauses (includes 1-sized clauses)"};
48    ret[aco::statistic_sgpr_presched] = aco_compiler_statistic_info{"Pre-Sched SGPRs", "SGPR usage before scheduling"};
49    ret[aco::statistic_vgpr_presched] = aco_compiler_statistic_info{"Pre-Sched VGPRs", "VGPR usage before scheduling"};
50    return ret;
51 }();
52
53 const unsigned aco_num_statistics = aco::num_statistics;
54 const aco_compiler_statistic_info *aco_statistic_infos = statistic_infos.data();
55
56 static void validate(aco::Program *program)
57 {
58    if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
59       return;
60
61    ASSERTED bool is_valid = aco::validate_ir(program);
62    assert(is_valid);
63 }
64
65 void aco_compile_shader(unsigned shader_count,
66                         struct nir_shader *const *shaders,
67                         struct radv_shader_binary **binary,
68                         struct radv_shader_args *args)
69 {
70    aco::init();
71
72    ac_shader_config config = {0};
73    std::unique_ptr<aco::Program> program{new aco::Program};
74
75    program->collect_statistics = args->options->record_stats;
76    if (program->collect_statistics)
77       memset(program->statistics, 0, sizeof(program->statistics));
78
79    program->debug.func = args->options->debug.func;
80    program->debug.private_data = args->options->debug.private_data;
81
82    /* Instruction Selection */
83    if (args->is_gs_copy_shader)
84       aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
85    else if (args->is_trap_handler_shader)
86       aco::select_trap_handler_shader(program.get(), shaders[0], &config, args);
87    else
88       aco::select_program(program.get(), shader_count, shaders, &config, args);
89    if (args->options->dump_preoptir)
90       aco_print_program(program.get(), stderr);
91
92    aco::live live_vars;
93    if (!args->is_trap_handler_shader) {
94       /* Phi lowering */
95       aco::lower_phis(program.get());
96       aco::dominator_tree(program.get());
97       validate(program.get());
98
99       /* Optimization */
100       if (!args->options->disable_optimizations) {
101          if (!(aco::debug_flags & aco::DEBUG_NO_VN))
102             aco::value_numbering(program.get());
103          if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
104             aco::optimize(program.get());
105       }
106
107       /* cleanup and exec mask handling */
108       aco::setup_reduce_temp(program.get());
109       aco::insert_exec_mask(program.get());
110       validate(program.get());
111
112       /* spilling and scheduling */
113       live_vars = aco::live_var_analysis(program.get());
114       aco::spill(program.get(), live_vars);
115    }
116
117    std::string llvm_ir;
118    if (args->options->record_ir) {
119       char *data = NULL;
120       size_t size = 0;
121       u_memstream mem;
122       if (u_memstream_open(&mem, &data, &size)) {
123          FILE *const memf = u_memstream_get(&mem);
124          aco_print_program(program.get(), memf);
125          fputc(0, memf);
126          u_memstream_close(&mem);
127       }
128
129       llvm_ir = std::string(data, data + size);
130       free(data);
131    }
132
133    if (program->collect_statistics)
134       aco::collect_presched_stats(program.get());
135
136    if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && args->options->dump_shader)
137       aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);
138
139    if (!args->is_trap_handler_shader) {
140       if (!args->options->disable_optimizations &&
141           !(aco::debug_flags & aco::DEBUG_NO_SCHED))
142          aco::schedule_program(program.get(), live_vars);
143       validate(program.get());
144
145       /* Register Allocation */
146       aco::register_allocation(program.get(), live_vars.live_out);
147
148       if (aco::validate_ra(program.get())) {
149          aco_print_program(program.get(), stderr);
150          abort();
151       } else if (args->options->dump_shader) {
152          aco_print_program(program.get(), stderr);
153       }
154
155       validate(program.get());
156
157       /* Optimization */
158       if (!args->options->disable_optimizations && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {
159          aco::optimize_postRA(program.get());
160          validate(program.get());
161       }
162
163       aco::ssa_elimination(program.get());
164    }
165
166    /* Lower to HW Instructions */
167    aco::lower_to_hw_instr(program.get());
168
169    /* Insert Waitcnt */
170    aco::insert_wait_states(program.get());
171    aco::insert_NOPs(program.get());
172
173    if (program->chip_class >= GFX10)
174       aco::form_hard_clauses(program.get());
175
176    if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))
177       aco::collect_preasm_stats(program.get());
178
179    /* Assembly */
180    std::vector<uint32_t> code;
181    unsigned exec_size = aco::emit_program(program.get(), code);
182
183    if (program->collect_statistics)
184       aco::collect_postasm_stats(program.get(), code);
185
186    bool get_disasm = args->options->dump_shader || args->options->record_ir;
187
188    size_t size = llvm_ir.size();
189
190    std::string disasm;
191    if (get_disasm) {
192       char *data = NULL;
193       size_t disasm_size = 0;
194       struct u_memstream mem;
195       if (u_memstream_open(&mem, &data, &disasm_size)) {
196          FILE *const memf = u_memstream_get(&mem);
197          aco::print_asm(program.get(), code, exec_size / 4u, memf);
198          fputc(0, memf);
199          u_memstream_close(&mem);
200       }
201
202       disasm = std::string(data, data + disasm_size);
203       size += disasm_size;
204       free(data);
205    }
206
207    size_t stats_size = 0;
208    if (program->collect_statistics)
209       stats_size = aco::num_statistics * sizeof(uint32_t);
210    size += stats_size;
211
212    size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
213    /* We need to calloc to prevent unintialized data because this will be used
214     * directly for the disk cache. Uninitialized data can appear because of
215     * padding in the struct or because legacy_binary->data can be at an offset
216     * from the start less than sizeof(radv_shader_binary_legacy). */
217    radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
218
219    legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
220    legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
221    legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
222    legacy_binary->base.total_size = size;
223
224    if (program->collect_statistics)
225       memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
226    legacy_binary->stats_size = stats_size;
227
228    memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(), code.size() * sizeof(uint32_t));
229    legacy_binary->exec_size = exec_size;
230    legacy_binary->code_size = code.size() * sizeof(uint32_t);
231
232    legacy_binary->config = config;
233    legacy_binary->disasm_size = 0;
234    legacy_binary->ir_size = llvm_ir.size();
235
236    llvm_ir.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size, llvm_ir.size());
237
238    if (get_disasm) {
239       disasm.copy((char*) legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size + llvm_ir.size(), disasm.size());
240       legacy_binary->disasm_size = disasm.size();
241    }
242
243    *binary = (radv_shader_binary*) legacy_binary;
244 }