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