2a0a500a7b69c9e2803e2c26ac05af86ce295749
[mesa.git] / src / amd / compiler / aco_ir.cpp
1 /*
2 * Copyright © 2020 Valve Corporation
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_ir.h"
25 #include "vulkan/radv_shader.h"
26 #include "c11/threads.h"
27 #include "util/debug.h"
28
29 namespace aco {
30
31 uint64_t debug_flags = 0;
32
33 static const struct debug_control aco_debug_options[] = {
34 {"validateir", DEBUG_VALIDATE_IR},
35 {"validatera", DEBUG_VALIDATE_RA},
36 {"perfwarn", DEBUG_PERFWARN},
37 {NULL, 0}
38 };
39
40 static once_flag init_once_flag = ONCE_FLAG_INIT;
41
42 static void init_once()
43 {
44 debug_flags = parse_debug_string(getenv("ACO_DEBUG"), aco_debug_options);
45
46 #ifndef NDEBUG
47 /* enable some flags by default on debug builds */
48 debug_flags |= aco::DEBUG_VALIDATE_IR;
49 #endif
50 }
51
52 void init()
53 {
54 call_once(&init_once_flag, init_once);
55 }
56
57 void init_program(Program *program, Stage stage, struct radv_shader_info *info,
58 enum chip_class chip_class, enum radeon_family family,
59 ac_shader_config *config)
60 {
61 program->stage = stage;
62 program->config = config;
63 program->info = info;
64 program->chip_class = chip_class;
65 if (family == CHIP_UNKNOWN) {
66 switch (chip_class) {
67 case GFX6:
68 program->family = CHIP_TAHITI;
69 break;
70 case GFX7:
71 program->family = CHIP_BONAIRE;
72 break;
73 case GFX8:
74 program->family = CHIP_POLARIS10;
75 break;
76 case GFX9:
77 program->family = CHIP_VEGA10;
78 break;
79 case GFX10:
80 program->family = CHIP_NAVI10;
81 break;
82 default:
83 program->family = CHIP_UNKNOWN;
84 break;
85 }
86 } else {
87 program->family = family;
88 }
89 program->wave_size = info->wave_size;
90 program->lane_mask = program->wave_size == 32 ? s1 : s2;
91
92 program->lds_alloc_granule = chip_class >= GFX7 ? 512 : 256;
93 program->lds_limit = chip_class >= GFX7 ? 65536 : 32768;
94 /* apparently gfx702 also has 16-bank LDS but I can't find a family for that */
95 program->has_16bank_lds = family == CHIP_KABINI || family == CHIP_STONEY;
96
97 program->vgpr_limit = 256;
98 program->vgpr_alloc_granule = 3;
99
100 if (chip_class >= GFX10) {
101 program->physical_sgprs = 2560; /* doesn't matter as long as it's at least 128 * 20 */
102 program->sgpr_alloc_granule = 127;
103 program->sgpr_limit = 106;
104 if (chip_class >= GFX10_3)
105 program->vgpr_alloc_granule = program->wave_size == 32 ? 15 : 7;
106 else
107 program->vgpr_alloc_granule = program->wave_size == 32 ? 7 : 3;
108 } else if (program->chip_class >= GFX8) {
109 program->physical_sgprs = 800;
110 program->sgpr_alloc_granule = 15;
111 if (family == CHIP_TONGA || family == CHIP_ICELAND)
112 program->sgpr_limit = 94; /* workaround hardware bug */
113 else
114 program->sgpr_limit = 102;
115 } else {
116 program->physical_sgprs = 512;
117 program->sgpr_alloc_granule = 7;
118 program->sgpr_limit = 104;
119 }
120
121 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
122 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
123 program->next_fp_mode.must_flush_denorms32 = false;
124 program->next_fp_mode.must_flush_denorms16_64 = false;
125 program->next_fp_mode.care_about_round32 = false;
126 program->next_fp_mode.care_about_round16_64 = false;
127 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
128 program->next_fp_mode.denorm32 = 0;
129 program->next_fp_mode.round16_64 = fp_round_ne;
130 program->next_fp_mode.round32 = fp_round_ne;
131 }
132
133 memory_sync_info get_sync_info(const Instruction* instr)
134 {
135 switch (instr->format) {
136 case Format::SMEM:
137 return static_cast<const SMEM_instruction*>(instr)->sync;
138 case Format::MUBUF:
139 return static_cast<const MUBUF_instruction*>(instr)->sync;
140 case Format::MIMG:
141 return static_cast<const MIMG_instruction*>(instr)->sync;
142 case Format::MTBUF:
143 return static_cast<const MTBUF_instruction*>(instr)->sync;
144 case Format::FLAT:
145 case Format::GLOBAL:
146 case Format::SCRATCH:
147 return static_cast<const FLAT_instruction*>(instr)->sync;
148 case Format::DS:
149 return static_cast<const DS_instruction*>(instr)->sync;
150 default:
151 return memory_sync_info();
152 }
153 }
154
155 bool can_use_SDWA(chip_class chip, const aco_ptr<Instruction>& instr)
156 {
157 if (!instr->isVALU())
158 return false;
159
160 if (chip < GFX8 || instr->isDPP())
161 return false;
162
163 if (instr->isSDWA())
164 return true;
165
166 if (instr->isVOP3()) {
167 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(instr.get());
168 if (instr->format == Format::VOP3)
169 return false;
170 if (vop3->clamp && instr->format == asVOP3(Format::VOPC) && chip != GFX8)
171 return false;
172 if (vop3->omod && chip < GFX9)
173 return false;
174
175 //TODO: return true if we know we will use vcc
176 if (instr->definitions.size() >= 2)
177 return false;
178
179 for (unsigned i = 1; i < instr->operands.size(); i++) {
180 if (instr->operands[i].isLiteral())
181 return false;
182 if (chip < GFX9 && !instr->operands[i].isOfType(RegType::vgpr))
183 return false;
184 }
185 }
186
187 if (!instr->operands.empty()) {
188 if (instr->operands[0].isLiteral())
189 return false;
190 if (chip < GFX9 && !instr->operands[0].isOfType(RegType::vgpr))
191 return false;
192 }
193
194 bool is_mac = instr->opcode == aco_opcode::v_mac_f32 ||
195 instr->opcode == aco_opcode::v_mac_f16 ||
196 instr->opcode == aco_opcode::v_fmac_f32 ||
197 instr->opcode == aco_opcode::v_fmac_f16;
198
199 if (chip != GFX8 && is_mac)
200 return false;
201
202 //TODO: return true if we know we will use vcc
203 if ((unsigned)instr->format & (unsigned)Format::VOPC)
204 return false;
205 if (instr->operands.size() >= 3 && !is_mac)
206 return false;
207
208 return instr->opcode != aco_opcode::v_madmk_f32 &&
209 instr->opcode != aco_opcode::v_madak_f32 &&
210 instr->opcode != aco_opcode::v_madmk_f16 &&
211 instr->opcode != aco_opcode::v_madak_f16 &&
212 instr->opcode != aco_opcode::v_readfirstlane_b32 &&
213 instr->opcode != aco_opcode::v_clrexcp &&
214 instr->opcode != aco_opcode::v_swap_b32;
215 }
216
217 /* updates "instr" and returns the old instruction (or NULL if no update was needed) */
218 aco_ptr<Instruction> convert_to_SDWA(chip_class chip, aco_ptr<Instruction>& instr)
219 {
220 if (instr->isSDWA())
221 return NULL;
222
223 aco_ptr<Instruction> tmp = std::move(instr);
224 Format format = (Format)(((uint16_t)tmp->format & ~(uint16_t)Format::VOP3) | (uint16_t)Format::SDWA);
225 instr.reset(create_instruction<SDWA_instruction>(tmp->opcode, format, tmp->operands.size(), tmp->definitions.size()));
226 std::copy(tmp->operands.cbegin(), tmp->operands.cend(), instr->operands.begin());
227 std::copy(tmp->definitions.cbegin(), tmp->definitions.cend(), instr->definitions.begin());
228
229 SDWA_instruction *sdwa = static_cast<SDWA_instruction*>(instr.get());
230
231 if (tmp->isVOP3()) {
232 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(tmp.get());
233 memcpy(sdwa->neg, vop3->neg, sizeof(sdwa->neg));
234 memcpy(sdwa->abs, vop3->abs, sizeof(sdwa->abs));
235 sdwa->omod = vop3->omod;
236 sdwa->clamp = vop3->clamp;
237 }
238
239 for (unsigned i = 0; i < instr->operands.size(); i++) {
240 switch (instr->operands[i].bytes()) {
241 case 1:
242 sdwa->sel[i] = sdwa_ubyte;
243 break;
244 case 2:
245 sdwa->sel[i] = sdwa_uword;
246 break;
247 case 4:
248 sdwa->sel[i] = sdwa_udword;
249 break;
250 }
251 }
252 switch (instr->definitions[0].bytes()) {
253 case 1:
254 sdwa->dst_sel = sdwa_ubyte;
255 sdwa->dst_preserve = true;
256 break;
257 case 2:
258 sdwa->dst_sel = sdwa_uword;
259 sdwa->dst_preserve = true;
260 break;
261 case 4:
262 sdwa->dst_sel = sdwa_udword;
263 break;
264 }
265
266 if (instr->definitions[0].getTemp().type() == RegType::sgpr && chip == GFX8)
267 instr->definitions[0].setFixed(vcc);
268 if (instr->definitions.size() >= 2)
269 instr->definitions[1].setFixed(vcc);
270 if (instr->operands.size() >= 3)
271 instr->operands[2].setFixed(vcc);
272
273 return tmp;
274 }
275
276 bool can_use_opsel(chip_class chip, aco_opcode op, int idx, bool high)
277 {
278 /* opsel is only GFX9+ */
279 if ((high || idx == -1) && chip < GFX9)
280 return false;
281
282 switch (op) {
283 case aco_opcode::v_div_fixup_f16:
284 case aco_opcode::v_fma_f16:
285 case aco_opcode::v_mad_f16:
286 case aco_opcode::v_mad_u16:
287 case aco_opcode::v_mad_i16:
288 case aco_opcode::v_med3_f16:
289 case aco_opcode::v_med3_i16:
290 case aco_opcode::v_med3_u16:
291 case aco_opcode::v_min3_f16:
292 case aco_opcode::v_min3_i16:
293 case aco_opcode::v_min3_u16:
294 case aco_opcode::v_max3_f16:
295 case aco_opcode::v_max3_i16:
296 case aco_opcode::v_max3_u16:
297 case aco_opcode::v_max_u16_e64:
298 case aco_opcode::v_max_i16_e64:
299 case aco_opcode::v_min_u16_e64:
300 case aco_opcode::v_min_i16_e64:
301 case aco_opcode::v_add_i16:
302 case aco_opcode::v_sub_i16:
303 case aco_opcode::v_add_u16_e64:
304 case aco_opcode::v_sub_u16_e64:
305 case aco_opcode::v_cvt_pknorm_i16_f16:
306 case aco_opcode::v_cvt_pknorm_u16_f16:
307 case aco_opcode::v_lshlrev_b16_e64:
308 case aco_opcode::v_lshrrev_b16_e64:
309 case aco_opcode::v_ashrrev_i16_e64:
310 case aco_opcode::v_mul_lo_u16_e64:
311 return true;
312 case aco_opcode::v_pack_b32_f16:
313 return idx != -1;
314 case aco_opcode::v_mad_u32_u16:
315 case aco_opcode::v_mad_i32_i16:
316 return idx >= 0 && idx < 2;
317 default:
318 return false;
319 }
320 }
321
322 }