glthread: don't prefix variable_data with const
[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 #include "aco_interface.h"
25 #include "aco_ir.h"
26 #include "vulkan/radv_shader.h"
27 #include "vulkan/radv_shader_args.h"
28 #include "c11/threads.h"
29 #include "util/debug.h"
30
31 #include <iostream>
32 #include <sstream>
33
34 namespace aco {
35 uint64_t debug_flags = 0;
36
37 static const struct debug_control aco_debug_options[] = {
38 {"validateir", DEBUG_VALIDATE},
39 {"validatera", DEBUG_VALIDATE_RA},
40 {"perfwarn", DEBUG_PERFWARN},
41 {NULL, 0}
42 };
43
44 static once_flag init_once_flag = ONCE_FLAG_INIT;
45
46 static void init()
47 {
48 debug_flags = parse_debug_string(getenv("ACO_DEBUG"), aco_debug_options);
49
50 #ifndef NDEBUG
51 /* enable some flags by default on debug builds */
52 debug_flags |= aco::DEBUG_VALIDATE;
53 #endif
54 }
55 }
56
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)
61 {
62 call_once(&aco::init_once_flag, aco::init);
63
64 ac_shader_config config = {0};
65 std::unique_ptr<aco::Program> program{new aco::Program};
66
67 /* Instruction Selection */
68 if (args->is_gs_copy_shader)
69 aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
70 else
71 aco::select_program(program.get(), shader_count, shaders, &config, args);
72 if (args->options->dump_preoptir) {
73 std::cerr << "After Instruction Selection:\n";
74 aco_print_program(program.get(), stderr);
75 }
76 aco::validate(program.get(), stderr);
77
78 /* Boolean phi lowering */
79 aco::lower_bool_phis(program.get());
80 //std::cerr << "After Boolean Phi Lowering:\n";
81 //aco_print_program(program.get(), stderr);
82
83 aco::dominator_tree(program.get());
84
85 /* Optimization */
86 aco::value_numbering(program.get());
87 aco::optimize(program.get());
88 aco::validate(program.get(), stderr);
89
90 aco::setup_reduce_temp(program.get());
91 aco::insert_exec_mask(program.get());
92 aco::validate(program.get(), stderr);
93
94 aco::live live_vars = aco::live_var_analysis(program.get(), args->options);
95 aco::spill(program.get(), live_vars, args->options);
96
97 //std::cerr << "Before Schedule:\n";
98 //aco_print_program(program.get(), stderr);
99 aco::schedule_program(program.get(), live_vars);
100
101 std::string llvm_ir;
102 if (args->options->record_ir) {
103 char *data = NULL;
104 size_t size = 0;
105 FILE *f = open_memstream(&data, &size);
106 if (f) {
107 aco_print_program(program.get(), f);
108 fputc(0, f);
109 fclose(f);
110 }
111
112 llvm_ir = std::string(data, data + size);
113 free(data);
114 }
115
116 /* Register Allocation */
117 aco::register_allocation(program.get(), live_vars.live_out);
118 if (args->options->dump_shader) {
119 std::cerr << "After RA:\n";
120 aco_print_program(program.get(), stderr);
121 }
122
123 if (aco::validate_ra(program.get(), args->options, stderr)) {
124 std::cerr << "Program after RA validation failure:\n";
125 aco_print_program(program.get(), stderr);
126 abort();
127 }
128
129 aco::ssa_elimination(program.get());
130 /* Lower to HW Instructions */
131 aco::lower_to_hw_instr(program.get());
132 //std::cerr << "After Eliminate Pseudo Instr:\n";
133 //aco_print_program(program.get(), stderr);
134
135 /* Insert Waitcnt */
136 aco::insert_wait_states(program.get());
137 aco::insert_NOPs(program.get());
138
139 //std::cerr << "After Insert-Waitcnt:\n";
140 //aco_print_program(program.get(), stderr);
141
142 /* Assembly */
143 std::vector<uint32_t> code;
144 unsigned exec_size = aco::emit_program(program.get(), code);
145
146 bool get_disasm = args->options->dump_shader || args->options->record_ir;
147
148 size_t size = llvm_ir.size();
149
150 std::string disasm;
151 if (get_disasm) {
152 std::ostringstream stream;
153 aco::print_asm(program.get(), code, exec_size / 4u, stream);
154 stream << '\0';
155 disasm = stream.str();
156 size += disasm.size();
157 }
158
159 size += code.size() * sizeof(uint32_t) + sizeof(radv_shader_binary_legacy);
160 /* We need to calloc to prevent unintialized data because this will be used
161 * directly for the disk cache. Uninitialized data can appear because of
162 * padding in the struct or because legacy_binary->data can be at an offset
163 * from the start less than sizeof(radv_shader_binary_legacy). */
164 radv_shader_binary_legacy* legacy_binary = (radv_shader_binary_legacy*) calloc(size, 1);
165
166 legacy_binary->base.type = RADV_BINARY_TYPE_LEGACY;
167 legacy_binary->base.stage = shaders[shader_count-1]->info.stage;
168 legacy_binary->base.is_gs_copy_shader = args->is_gs_copy_shader;
169 legacy_binary->base.total_size = size;
170
171 memcpy(legacy_binary->data, code.data(), code.size() * sizeof(uint32_t));
172 legacy_binary->exec_size = exec_size;
173 legacy_binary->code_size = code.size() * sizeof(uint32_t);
174
175 legacy_binary->config = config;
176 legacy_binary->disasm_size = 0;
177 legacy_binary->ir_size = llvm_ir.size();
178
179 llvm_ir.copy((char*) legacy_binary->data + legacy_binary->code_size, llvm_ir.size());
180
181 if (get_disasm) {
182 disasm.copy((char*) legacy_binary->data + legacy_binary->code_size + llvm_ir.size(), disasm.size());
183 legacy_binary->disasm_size = disasm.size();
184 }
185
186 *binary = (radv_shader_binary*) legacy_binary;
187 }