aco: do not print prologs disassembly if no disassembler
[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
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(const struct radv_nir_compiler_options* options,
78                    const struct radv_shader_info* info,
79                    unsigned shader_count, struct nir_shader* const* shaders,
80                    const struct radv_shader_args *args,
81                    struct radv_shader_binary** binary)
82 {
83    aco::init();
84
85    ac_shader_config config = {0};
86    std::unique_ptr<aco::Program> program{new aco::Program};
87
88    program->collect_statistics = options->record_stats;
89    if (program->collect_statistics)
90       memset(program->statistics, 0, sizeof(program->statistics));
91
92    program->debug.func = options->debug.func;
93    program->debug.private_data = options->debug.private_data;
94
95    /* Instruction Selection */
96    if (args->is_gs_copy_shader)
97       aco::select_gs_copy_shader(program.get(), shaders[0], &config, options, info, args);
98    else if (args->is_trap_handler_shader)
99       aco::select_trap_handler_shader(program.get(), shaders[0], &config, options, info, args);
100    else
101       aco::select_program(program.get(), shader_count, shaders, &config, options, info, args);
102    if (options->dump_preoptir)
103       aco_print_program(program.get(), stderr);
104
105    aco::live live_vars;
106    if (!args->is_trap_handler_shader) {
107       /* Phi lowering */
108       aco::lower_phis(program.get());
109       aco::dominator_tree(program.get());
110       validate(program.get());
111
112       /* Optimization */
113       if (!options->key.optimisations_disabled) {
114          if (!(aco::debug_flags & aco::DEBUG_NO_VN))
115             aco::value_numbering(program.get());
116          if (!(aco::debug_flags & aco::DEBUG_NO_OPT))
117             aco::optimize(program.get());
118       }
119
120       /* cleanup and exec mask handling */
121       aco::setup_reduce_temp(program.get());
122       aco::insert_exec_mask(program.get());
123       validate(program.get());
124
125       /* spilling and scheduling */
126       live_vars = aco::live_var_analysis(program.get());
127       aco::spill(program.get(), live_vars);
128    }
129
130    std::string llvm_ir;
131    if (options->record_ir) {
132       char* data = NULL;
133       size_t size = 0;
134       u_memstream mem;
135       if (u_memstream_open(&mem, &data, &size)) {
136          FILE* const memf = u_memstream_get(&mem);
137          aco_print_program(program.get(), memf);
138          fputc(0, memf);
139          u_memstream_close(&mem);
140       }
141
142       llvm_ir = std::string(data, data + size);
143       free(data);
144    }
145
146    if (program->collect_statistics)
147       aco::collect_presched_stats(program.get());
148
149    if ((aco::debug_flags & aco::DEBUG_LIVE_INFO) && options->dump_shader)
150       aco_print_program(program.get(), stderr, live_vars, aco::print_live_vars | aco::print_kill);
151
152    if (!args->is_trap_handler_shader) {
153       if (!options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_SCHED))
154          aco::schedule_program(program.get(), live_vars);
155       validate(program.get());
156
157       /* Register Allocation */
158       aco::register_allocation(program.get(), live_vars.live_out);
159
160       if (aco::validate_ra(program.get())) {
161          aco_print_program(program.get(), stderr);
162          abort();
163       } else if (options->dump_shader) {
164          aco_print_program(program.get(), stderr);
165       }
166
167       validate(program.get());
168
169       /* Optimization */
170       if (!options->key.optimisations_disabled && !(aco::debug_flags & aco::DEBUG_NO_OPT)) {
171          aco::optimize_postRA(program.get());
172          validate(program.get());
173       }
174
175       aco::ssa_elimination(program.get());
176    }
177
178    /* Lower to HW Instructions */
179    aco::lower_to_hw_instr(program.get());
180
181    /* Insert Waitcnt */
182    aco::insert_wait_states(program.get());
183    aco::insert_NOPs(program.get());
184
185    if (program->chip_class >= GFX10)
186       aco::form_hard_clauses(program.get());
187
188    if (program->collect_statistics || (aco::debug_flags & aco::DEBUG_PERF_INFO))
189       aco::collect_preasm_stats(program.get());
190
191    /* Assembly */
192    std::vector<uint32_t> code;
193    unsigned exec_size = aco::emit_program(program.get(), code);
194
195    if (program->collect_statistics)
196       aco::collect_postasm_stats(program.get(), code);
197
198    bool get_disasm = options->dump_shader || options->record_ir;
199
200    size_t size = llvm_ir.size();
201
202    std::string disasm;
203    if (get_disasm) {
204       if (check_print_asm_support(program.get())) {
205          char* data = NULL;
206          size_t disasm_size = 0;
207          struct u_memstream mem;
208          if (u_memstream_open(&mem, &data, &disasm_size)) {
209             FILE* const memf = u_memstream_get(&mem);
210             aco::print_asm(program.get(), code, exec_size / 4u, memf);
211             fputc(0, memf);
212             u_memstream_close(&mem);
213          }
214
215          disasm = std::string(data, data + disasm_size);
216          size += disasm_size;
217          free(data);
218       } else {
219          disasm = "Shader disassembly is not supported in the current configuration"
220 #ifndef LLVM_AVAILABLE
221                   " (LLVM not available)"
222 #endif
223                   ".\n";
224          size += disasm.length();
225       }
226    }
227
228    size_t stats_size = 0;
229    if (program->collect_statistics)
230       stats_size = aco::num_statistics * sizeof(uint32_t);
231    size += stats_size;
232
233    size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
234    /* We need to calloc to prevent unintialized data because this will be used
235     * directly for the disk cache. Uninitialized data can appear because of
236     * padding in the struct or because legacy_binary->data can be at an offset
237     * from the start less than sizeof(radv_shader_binary_legacy). */
238    radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*)calloc(size, 1);
239
240    legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
241    legacy_binary->base.stage = shaders[shader_count - 1]->info.stage;
242    legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
243    legacy_binary->base.total_size = size;
244
245    if (program->collect_statistics)
246       memcpy(legacy_binary->data, program->statistics, aco::num_statistics * sizeof(uint32_t));
247    legacy_binary->stats_size = stats_size;
248
249    memcpy(legacy_binary->data + legacy_binary->stats_size, code.data(),
250           code.size() * sizeof(uint32_t));
251    legacy_binary->exec_size = exec_size;
252    legacy_binary->code_size = code.size() * sizeof(uint32_t);
253
254    legacy_binary->base.config = config;
255    legacy_binary->disasm_size = 0;
256    legacy_binary->ir_size = llvm_ir.size();
257
258    llvm_ir.copy((char*)legacy_binary->data + legacy_binary->stats_size + legacy_binary->code_size,
259                 llvm_ir.size());
260
261    if (get_disasm) {
262       disasm.copy((char*)legacy_binary->data + legacy_binary->stats_size +
263                      legacy_binary->code_size + llvm_ir.size(),
264                   disasm.size());
265       legacy_binary->disasm_size = disasm.size();
266    }
267
268    *binary = (radv_shader_binary*)legacy_binary;
269 }
270
271 void
272 aco_compile_vs_prolog(const struct radv_nir_compiler_options* options,
273                       const struct radv_shader_info* info,
274                       const struct radv_vs_prolog_key* key,
275                       const struct radv_shader_args* args,
276                       struct radv_prolog_binary** binary)
277 {
278    aco::init();
279
280    /* create program */
281    ac_shader_config config = {0};
282    std::unique_ptr<aco::Program> program{new aco::Program};
283    program->collect_statistics = false;
284    program->debug.func = NULL;
285    program->debug.private_data = NULL;
286
287    /* create IR */
288    unsigned num_preserved_sgprs;
289    aco::select_vs_prolog(program.get(), key, &config, options, info, args, &num_preserved_sgprs);
290    aco::insert_NOPs(program.get());
291
292    if (options->dump_shader)
293       aco_print_program(program.get(), stderr);
294
295    /* assembly */
296    std::vector<uint32_t> code;
297    code.reserve(align(program->blocks[0].instructions.size() * 2, 16));
298    unsigned exec_size = aco::emit_program(program.get(), code);
299
300    if (options->dump_shader) {
301       if (check_print_asm_support(program.get())) {
302          aco::print_asm(program.get(), code, exec_size / 4u, stderr);
303          fprintf(stderr, "\n");
304       } else {
305          fprintf(stderr, "Shader disassembly is not supported in the current configuration"
306 #ifndef LLVM_AVAILABLE
307                          " (LLVM not available)"
308 #endif
309                          ".\n");
310       }
311    }
312
313    /* copy into binary */
314    size_t size = code.size() * sizeof(uint32_t) + sizeof(radv_prolog_binary);
315    radv_prolog_binary* prolog_binary = (radv_prolog_binary*)calloc(size, 1);
316
317    prolog_binary->num_sgprs = config.num_sgprs;
318    prolog_binary->num_vgprs = config.num_vgprs;
319    prolog_binary->num_preserved_sgprs = num_preserved_sgprs;
320    prolog_binary->code_size = code.size() * sizeof(uint32_t);
321    memcpy(prolog_binary->data, code.data(), prolog_binary->code_size);
322
323    *binary = prolog_binary;
324 }