aco: flush denorms after fmin/fmax on pre-GFX9
[mesa.git] / src / amd / compiler / aco_instruction_selection.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <algorithm>
27 #include <array>
28 #include <map>
29
30 #include "ac_shader_util.h"
31 #include "aco_ir.h"
32 #include "aco_builder.h"
33 #include "aco_interface.h"
34 #include "aco_instruction_selection_setup.cpp"
35 #include "util/fast_idiv_by_const.h"
36
37 namespace aco {
38 namespace {
39
40 class loop_info_RAII {
41 isel_context* ctx;
42 unsigned header_idx_old;
43 Block* exit_old;
44 bool divergent_cont_old;
45 bool divergent_branch_old;
46 bool divergent_if_old;
47
48 public:
49 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
50 : ctx(ctx),
51 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
52 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
53 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
54 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
55 {
56 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
57 ctx->cf_info.parent_loop.exit = loop_exit;
58 ctx->cf_info.parent_loop.has_divergent_continue = false;
59 ctx->cf_info.parent_loop.has_divergent_branch = false;
60 ctx->cf_info.parent_if.is_divergent = false;
61 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
62 }
63
64 ~loop_info_RAII()
65 {
66 ctx->cf_info.parent_loop.header_idx = header_idx_old;
67 ctx->cf_info.parent_loop.exit = exit_old;
68 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
69 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
70 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
71 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
72 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
73 ctx->cf_info.exec_potentially_empty = false;
74 }
75 };
76
77 struct if_context {
78 Temp cond;
79
80 bool divergent_old;
81 bool exec_potentially_empty_old;
82
83 unsigned BB_if_idx;
84 unsigned invert_idx;
85 bool then_branch_divergent;
86 Block BB_invert;
87 Block BB_endif;
88 };
89
90 static void visit_cf_list(struct isel_context *ctx,
91 struct exec_list *list);
92
93 static void add_logical_edge(unsigned pred_idx, Block *succ)
94 {
95 succ->logical_preds.emplace_back(pred_idx);
96 }
97
98
99 static void add_linear_edge(unsigned pred_idx, Block *succ)
100 {
101 succ->linear_preds.emplace_back(pred_idx);
102 }
103
104 static void add_edge(unsigned pred_idx, Block *succ)
105 {
106 add_logical_edge(pred_idx, succ);
107 add_linear_edge(pred_idx, succ);
108 }
109
110 static void append_logical_start(Block *b)
111 {
112 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
113 }
114
115 static void append_logical_end(Block *b)
116 {
117 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
118 }
119
120 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
121 {
122 assert(ctx->allocated[def->index].id());
123 return ctx->allocated[def->index];
124 }
125
126 Temp emit_mbcnt(isel_context *ctx, Definition dst,
127 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
128 {
129 Builder bld(ctx->program, ctx->block);
130 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
131 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
132
133 if (ctx->program->wave_size == 32) {
134 return thread_id_lo;
135 } else {
136 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
137 return thread_id_hi;
138 }
139 }
140
141 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
142 {
143 Builder bld(ctx->program, ctx->block);
144
145 if (!dst.id())
146 dst = bld.tmp(src.regClass());
147
148 assert(src.size() == dst.size());
149
150 if (ctx->stage != fragment_fs) {
151 if (!dst.id())
152 return src;
153
154 bld.copy(Definition(dst), src);
155 return dst;
156 }
157
158 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
159 ctx->program->needs_wqm |= program_needs_wqm;
160 return dst;
161 }
162
163 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
164 {
165 if (index.regClass() == s1)
166 return bld.readlane(bld.def(s1), data, index);
167
168 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
169
170 /* Currently not implemented on GFX6-7 */
171 assert(ctx->options->chip_class >= GFX8);
172
173 if (ctx->options->chip_class <= GFX9 || ctx->program->wave_size == 32) {
174 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
175 }
176
177 /* GFX10, wave64 mode:
178 * The bpermute instruction is limited to half-wave operation, which means that it can't
179 * properly support subgroup shuffle like older generations (or wave32 mode), so we
180 * emulate it here.
181 */
182 if (!ctx->has_gfx10_wave64_bpermute) {
183 ctx->has_gfx10_wave64_bpermute = true;
184 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
185 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
186 }
187
188 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
189 Temp lane_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), lane_id);
190 Temp index_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), index);
191 Temp cmp = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(s2, vcc), lane_is_hi, index_is_hi);
192
193 return bld.reduction(aco_opcode::p_wave64_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc),
194 bld.vcc(cmp), Operand(v2.as_linear()), index_x4, data, gfx10_wave64_bpermute);
195 }
196
197 Temp as_vgpr(isel_context *ctx, Temp val)
198 {
199 if (val.type() == RegType::sgpr) {
200 Builder bld(ctx->program, ctx->block);
201 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
202 }
203 assert(val.type() == RegType::vgpr);
204 return val;
205 }
206
207 //assumes a != 0xffffffff
208 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
209 {
210 assert(b != 0);
211 Builder bld(ctx->program, ctx->block);
212
213 if (util_is_power_of_two_or_zero(b)) {
214 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
215 return;
216 }
217
218 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
219
220 assert(info.multiplier <= 0xffffffff);
221
222 bool pre_shift = info.pre_shift != 0;
223 bool increment = info.increment != 0;
224 bool multiply = true;
225 bool post_shift = info.post_shift != 0;
226
227 if (!pre_shift && !increment && !multiply && !post_shift) {
228 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
229 return;
230 }
231
232 Temp pre_shift_dst = a;
233 if (pre_shift) {
234 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
235 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
236 }
237
238 Temp increment_dst = pre_shift_dst;
239 if (increment) {
240 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
241 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
242 }
243
244 Temp multiply_dst = increment_dst;
245 if (multiply) {
246 multiply_dst = post_shift ? bld.tmp(v1) : dst;
247 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
248 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
249 }
250
251 if (post_shift) {
252 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
253 }
254 }
255
256 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
257 {
258 Builder bld(ctx->program, ctx->block);
259 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
260 }
261
262
263 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
264 {
265 /* no need to extract the whole vector */
266 if (src.regClass() == dst_rc) {
267 assert(idx == 0);
268 return src;
269 }
270 assert(src.size() > idx);
271 Builder bld(ctx->program, ctx->block);
272 auto it = ctx->allocated_vec.find(src.id());
273 /* the size check needs to be early because elements other than 0 may be garbage */
274 if (it != ctx->allocated_vec.end() && it->second[0].size() == dst_rc.size()) {
275 if (it->second[idx].regClass() == dst_rc) {
276 return it->second[idx];
277 } else {
278 assert(dst_rc.size() == it->second[idx].regClass().size());
279 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
280 return bld.copy(bld.def(dst_rc), it->second[idx]);
281 }
282 }
283
284 if (src.size() == dst_rc.size()) {
285 assert(idx == 0);
286 return bld.copy(bld.def(dst_rc), src);
287 } else {
288 Temp dst = bld.tmp(dst_rc);
289 emit_extract_vector(ctx, src, idx, dst);
290 return dst;
291 }
292 }
293
294 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
295 {
296 if (num_components == 1)
297 return;
298 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
299 return;
300 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
301 split->operands[0] = Operand(vec_src);
302 std::array<Temp,4> elems;
303 for (unsigned i = 0; i < num_components; i++) {
304 elems[i] = {ctx->program->allocateId(), RegClass(vec_src.type(), vec_src.size() / num_components)};
305 split->definitions[i] = Definition(elems[i]);
306 }
307 ctx->block->instructions.emplace_back(std::move(split));
308 ctx->allocated_vec.emplace(vec_src.id(), elems);
309 }
310
311 /* This vector expansion uses a mask to determine which elements in the new vector
312 * come from the original vector. The other elements are undefined. */
313 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
314 {
315 emit_split_vector(ctx, vec_src, util_bitcount(mask));
316
317 if (vec_src == dst)
318 return;
319
320 Builder bld(ctx->program, ctx->block);
321 if (num_components == 1) {
322 if (dst.type() == RegType::sgpr)
323 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
324 else
325 bld.copy(Definition(dst), vec_src);
326 return;
327 }
328
329 unsigned component_size = dst.size() / num_components;
330 std::array<Temp,4> elems;
331
332 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
333 vec->definitions[0] = Definition(dst);
334 unsigned k = 0;
335 for (unsigned i = 0; i < num_components; i++) {
336 if (mask & (1 << i)) {
337 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
338 if (dst.type() == RegType::sgpr)
339 src = bld.as_uniform(src);
340 vec->operands[i] = Operand(src);
341 } else {
342 vec->operands[i] = Operand(0u);
343 }
344 elems[i] = vec->operands[i].getTemp();
345 }
346 ctx->block->instructions.emplace_back(std::move(vec));
347 ctx->allocated_vec.emplace(dst.id(), elems);
348 }
349
350 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
351 {
352 Builder bld(ctx->program, ctx->block);
353 if (!dst.id())
354 dst = bld.tmp(bld.lm);
355
356 assert(val.regClass() == s1);
357 assert(dst.regClass() == bld.lm);
358
359 return bld.sop2(Builder::s_cselect, bld.hint_vcc(Definition(dst)), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
360 }
361
362 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
363 {
364 Builder bld(ctx->program, ctx->block);
365 if (!dst.id())
366 dst = bld.tmp(s1);
367
368 assert(val.regClass() == bld.lm);
369 assert(dst.regClass() == s1);
370
371 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
372 Temp tmp = bld.tmp(s1);
373 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
374 return emit_wqm(ctx, tmp, dst);
375 }
376
377 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
378 {
379 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
380 return get_ssa_temp(ctx, src.src.ssa);
381
382 if (src.src.ssa->num_components == size) {
383 bool identity_swizzle = true;
384 for (unsigned i = 0; identity_swizzle && i < size; i++) {
385 if (src.swizzle[i] != i)
386 identity_swizzle = false;
387 }
388 if (identity_swizzle)
389 return get_ssa_temp(ctx, src.src.ssa);
390 }
391
392 Temp vec = get_ssa_temp(ctx, src.src.ssa);
393 unsigned elem_size = vec.size() / src.src.ssa->num_components;
394 assert(elem_size > 0); /* TODO: 8 and 16-bit vectors not supported */
395 assert(vec.size() % elem_size == 0);
396
397 RegClass elem_rc = RegClass(vec.type(), elem_size);
398 if (size == 1) {
399 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
400 } else {
401 assert(size <= 4);
402 std::array<Temp,4> elems;
403 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
404 for (unsigned i = 0; i < size; ++i) {
405 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
406 vec_instr->operands[i] = Operand{elems[i]};
407 }
408 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size)};
409 vec_instr->definitions[0] = Definition(dst);
410 ctx->block->instructions.emplace_back(std::move(vec_instr));
411 ctx->allocated_vec.emplace(dst.id(), elems);
412 return dst;
413 }
414 }
415
416 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
417 {
418 if (ptr.size() == 2)
419 return ptr;
420 Builder bld(ctx->program, ctx->block);
421 if (ptr.type() == RegType::vgpr)
422 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
423 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
424 ptr, Operand((unsigned)ctx->options->address32_hi));
425 }
426
427 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
428 {
429 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
430 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
431 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
432 sop2->definitions[0] = Definition(dst);
433 if (writes_scc)
434 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
435 ctx->block->instructions.emplace_back(std::move(sop2));
436 }
437
438 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
439 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
440 {
441 Builder bld(ctx->program, ctx->block);
442 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
443 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
444 if (src1.type() == RegType::sgpr) {
445 if (commutative && src0.type() == RegType::vgpr) {
446 Temp t = src0;
447 src0 = src1;
448 src1 = t;
449 } else if (src0.type() == RegType::vgpr &&
450 op != aco_opcode::v_madmk_f32 &&
451 op != aco_opcode::v_madak_f32 &&
452 op != aco_opcode::v_madmk_f16 &&
453 op != aco_opcode::v_madak_f16) {
454 /* If the instruction is not commutative, we emit a VOP3A instruction */
455 bld.vop2_e64(op, Definition(dst), src0, src1);
456 return;
457 } else {
458 src1 = bld.copy(bld.def(RegType::vgpr, src1.size()), src1); //TODO: as_vgpr
459 }
460 }
461
462 if (flush_denorms && ctx->program->chip_class < GFX9) {
463 assert(dst.size() == 1);
464 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
465 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
466 } else {
467 bld.vop2(op, Definition(dst), src0, src1);
468 }
469 }
470
471 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
472 bool flush_denorms = false)
473 {
474 Temp src0 = get_alu_src(ctx, instr->src[0]);
475 Temp src1 = get_alu_src(ctx, instr->src[1]);
476 Temp src2 = get_alu_src(ctx, instr->src[2]);
477
478 /* ensure that the instruction has at most 1 sgpr operand
479 * The optimizer will inline constants for us */
480 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
481 src0 = as_vgpr(ctx, src0);
482 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
483 src1 = as_vgpr(ctx, src1);
484 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
485 src2 = as_vgpr(ctx, src2);
486
487 Builder bld(ctx->program, ctx->block);
488 if (flush_denorms && ctx->program->chip_class < GFX9) {
489 assert(dst.size() == 1);
490 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
491 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
492 } else {
493 bld.vop3(op, Definition(dst), src0, src1, src2);
494 }
495 }
496
497 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
498 {
499 Builder bld(ctx->program, ctx->block);
500 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
501 }
502
503 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
504 {
505 Temp src0 = get_alu_src(ctx, instr->src[0]);
506 Temp src1 = get_alu_src(ctx, instr->src[1]);
507 assert(src0.size() == src1.size());
508
509 aco_ptr<Instruction> vopc;
510 if (src1.type() == RegType::sgpr) {
511 if (src0.type() == RegType::vgpr) {
512 /* to swap the operands, we might also have to change the opcode */
513 switch (op) {
514 case aco_opcode::v_cmp_lt_f32:
515 op = aco_opcode::v_cmp_gt_f32;
516 break;
517 case aco_opcode::v_cmp_ge_f32:
518 op = aco_opcode::v_cmp_le_f32;
519 break;
520 case aco_opcode::v_cmp_lt_i32:
521 op = aco_opcode::v_cmp_gt_i32;
522 break;
523 case aco_opcode::v_cmp_ge_i32:
524 op = aco_opcode::v_cmp_le_i32;
525 break;
526 case aco_opcode::v_cmp_lt_u32:
527 op = aco_opcode::v_cmp_gt_u32;
528 break;
529 case aco_opcode::v_cmp_ge_u32:
530 op = aco_opcode::v_cmp_le_u32;
531 break;
532 case aco_opcode::v_cmp_lt_f64:
533 op = aco_opcode::v_cmp_gt_f64;
534 break;
535 case aco_opcode::v_cmp_ge_f64:
536 op = aco_opcode::v_cmp_le_f64;
537 break;
538 case aco_opcode::v_cmp_lt_i64:
539 op = aco_opcode::v_cmp_gt_i64;
540 break;
541 case aco_opcode::v_cmp_ge_i64:
542 op = aco_opcode::v_cmp_le_i64;
543 break;
544 case aco_opcode::v_cmp_lt_u64:
545 op = aco_opcode::v_cmp_gt_u64;
546 break;
547 case aco_opcode::v_cmp_ge_u64:
548 op = aco_opcode::v_cmp_le_u64;
549 break;
550 default: /* eq and ne are commutative */
551 break;
552 }
553 Temp t = src0;
554 src0 = src1;
555 src1 = t;
556 } else {
557 src1 = as_vgpr(ctx, src1);
558 }
559 }
560
561 Builder bld(ctx->program, ctx->block);
562 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
563 }
564
565 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
566 {
567 Temp src0 = get_alu_src(ctx, instr->src[0]);
568 Temp src1 = get_alu_src(ctx, instr->src[1]);
569 Builder bld(ctx->program, ctx->block);
570
571 assert(dst.regClass() == bld.lm);
572 assert(src0.type() == RegType::sgpr);
573 assert(src1.type() == RegType::sgpr);
574 assert(src0.regClass() == src1.regClass());
575
576 /* Emit the SALU comparison instruction */
577 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
578 /* Turn the result into a per-lane bool */
579 bool_to_vector_condition(ctx, cmp, dst);
580 }
581
582 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
583 aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
584 {
585 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : s32_op;
586 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : v32_op;
587 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
588 bool use_valu = s_op == aco_opcode::num_opcodes ||
589 divergent_vals ||
590 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
591 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
592 aco_opcode op = use_valu ? v_op : s_op;
593 assert(op != aco_opcode::num_opcodes);
594
595 if (use_valu)
596 emit_vopc_instruction(ctx, instr, op, dst);
597 else
598 emit_sopc_instruction(ctx, instr, op, dst);
599 }
600
601 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
602 {
603 Builder bld(ctx->program, ctx->block);
604 Temp src0 = get_alu_src(ctx, instr->src[0]);
605 Temp src1 = get_alu_src(ctx, instr->src[1]);
606
607 assert(dst.regClass() == bld.lm);
608 assert(src0.regClass() == bld.lm);
609 assert(src1.regClass() == bld.lm);
610
611 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
612 }
613
614 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
615 {
616 Builder bld(ctx->program, ctx->block);
617 Temp cond = get_alu_src(ctx, instr->src[0]);
618 Temp then = get_alu_src(ctx, instr->src[1]);
619 Temp els = get_alu_src(ctx, instr->src[2]);
620
621 assert(cond.regClass() == bld.lm);
622
623 if (dst.type() == RegType::vgpr) {
624 aco_ptr<Instruction> bcsel;
625 if (dst.size() == 1) {
626 then = as_vgpr(ctx, then);
627 els = as_vgpr(ctx, els);
628
629 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
630 } else if (dst.size() == 2) {
631 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
632 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
633 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
634 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
635
636 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
637 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
638
639 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
640 } else {
641 fprintf(stderr, "Unimplemented NIR instr bit size: ");
642 nir_print_instr(&instr->instr, stderr);
643 fprintf(stderr, "\n");
644 }
645 return;
646 }
647
648 if (instr->dest.dest.ssa.bit_size == 1) {
649 assert(dst.regClass() == bld.lm);
650 assert(then.regClass() == bld.lm);
651 assert(els.regClass() == bld.lm);
652 }
653
654 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
655 if (dst.regClass() == s1 || dst.regClass() == s2) {
656 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
657 assert(dst.size() == then.size());
658 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
659 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
660 } else {
661 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
662 nir_print_instr(&instr->instr, stderr);
663 fprintf(stderr, "\n");
664 }
665 return;
666 }
667
668 /* divergent boolean bcsel
669 * this implements bcsel on bools: dst = s0 ? s1 : s2
670 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
671 assert(instr->dest.dest.ssa.bit_size == 1);
672
673 if (cond.id() != then.id())
674 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
675
676 if (cond.id() == els.id())
677 bld.sop1(Builder::s_mov, Definition(dst), then);
678 else
679 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
680 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
681 }
682
683 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
684 aco_opcode op, uint32_t undo)
685 {
686 /* multiply by 16777216 to handle denormals */
687 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
688 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
689 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
690 scaled = bld.vop1(op, bld.def(v1), scaled);
691 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
692
693 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
694
695 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
696 }
697
698 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
699 {
700 if (ctx->block->fp_mode.denorm32 == 0) {
701 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
702 return;
703 }
704
705 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
706 }
707
708 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
709 {
710 if (ctx->block->fp_mode.denorm32 == 0) {
711 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
712 return;
713 }
714
715 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
716 }
717
718 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
719 {
720 if (ctx->block->fp_mode.denorm32 == 0) {
721 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
722 return;
723 }
724
725 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
726 }
727
728 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
729 {
730 if (ctx->block->fp_mode.denorm32 == 0) {
731 bld.vop1(aco_opcode::v_log_f32, dst, val);
732 return;
733 }
734
735 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
736 }
737
738 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
739 {
740 if (!instr->dest.dest.is_ssa) {
741 fprintf(stderr, "nir alu dst not in ssa: ");
742 nir_print_instr(&instr->instr, stderr);
743 fprintf(stderr, "\n");
744 abort();
745 }
746 Builder bld(ctx->program, ctx->block);
747 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
748 switch(instr->op) {
749 case nir_op_vec2:
750 case nir_op_vec3:
751 case nir_op_vec4: {
752 std::array<Temp,4> elems;
753 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
754 for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; ++i) {
755 elems[i] = get_alu_src(ctx, instr->src[i]);
756 vec->operands[i] = Operand{elems[i]};
757 }
758 vec->definitions[0] = Definition(dst);
759 ctx->block->instructions.emplace_back(std::move(vec));
760 ctx->allocated_vec.emplace(dst.id(), elems);
761 break;
762 }
763 case nir_op_mov: {
764 Temp src = get_alu_src(ctx, instr->src[0]);
765 aco_ptr<Instruction> mov;
766 if (dst.type() == RegType::sgpr) {
767 if (src.type() == RegType::vgpr)
768 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
769 else if (src.regClass() == s1)
770 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
771 else if (src.regClass() == s2)
772 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
773 else
774 unreachable("wrong src register class for nir_op_imov");
775 } else if (dst.regClass() == v1) {
776 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
777 } else if (dst.regClass() == v2) {
778 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
779 } else {
780 nir_print_instr(&instr->instr, stderr);
781 unreachable("Should have been lowered to scalar.");
782 }
783 break;
784 }
785 case nir_op_inot: {
786 Temp src = get_alu_src(ctx, instr->src[0]);
787 if (instr->dest.dest.ssa.bit_size == 1) {
788 assert(src.regClass() == bld.lm);
789 assert(dst.regClass() == bld.lm);
790 bld.sop2(Builder::s_andn2, Definition(dst), bld.def(s1, scc), Operand(exec, bld.lm), src);
791 } else if (dst.regClass() == v1) {
792 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
793 } else if (dst.type() == RegType::sgpr) {
794 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
795 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
796 } else {
797 fprintf(stderr, "Unimplemented NIR instr bit size: ");
798 nir_print_instr(&instr->instr, stderr);
799 fprintf(stderr, "\n");
800 }
801 break;
802 }
803 case nir_op_ineg: {
804 Temp src = get_alu_src(ctx, instr->src[0]);
805 if (dst.regClass() == v1) {
806 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
807 } else if (dst.regClass() == s1) {
808 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
809 } else if (dst.size() == 2) {
810 Temp src0 = bld.tmp(dst.type(), 1);
811 Temp src1 = bld.tmp(dst.type(), 1);
812 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
813
814 if (dst.regClass() == s2) {
815 Temp carry = bld.tmp(s1);
816 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
817 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
818 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
819 } else {
820 Temp lower = bld.tmp(v1);
821 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
822 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
823 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
824 }
825 } else {
826 fprintf(stderr, "Unimplemented NIR instr bit size: ");
827 nir_print_instr(&instr->instr, stderr);
828 fprintf(stderr, "\n");
829 }
830 break;
831 }
832 case nir_op_iabs: {
833 if (dst.regClass() == s1) {
834 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
835 } else if (dst.regClass() == v1) {
836 Temp src = get_alu_src(ctx, instr->src[0]);
837 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
838 } else {
839 fprintf(stderr, "Unimplemented NIR instr bit size: ");
840 nir_print_instr(&instr->instr, stderr);
841 fprintf(stderr, "\n");
842 }
843 break;
844 }
845 case nir_op_isign: {
846 Temp src = get_alu_src(ctx, instr->src[0]);
847 if (dst.regClass() == s1) {
848 Temp tmp = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
849 Temp gtz = bld.sopc(aco_opcode::s_cmp_gt_i32, bld.def(s1, scc), src, Operand(0u));
850 bld.sop2(aco_opcode::s_add_i32, Definition(dst), bld.def(s1, scc), gtz, tmp);
851 } else if (dst.regClass() == s2) {
852 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
853 Temp neqz;
854 if (ctx->program->chip_class >= GFX8)
855 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
856 else
857 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
858 /* SCC gets zero-extended to 64 bit */
859 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
860 } else if (dst.regClass() == v1) {
861 Temp tmp = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
862 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
863 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(1u), tmp, gtz);
864 } else if (dst.regClass() == v2) {
865 Temp upper = emit_extract_vector(ctx, src, 1, v1);
866 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
867 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
868 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
869 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
870 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
871 } else {
872 fprintf(stderr, "Unimplemented NIR instr bit size: ");
873 nir_print_instr(&instr->instr, stderr);
874 fprintf(stderr, "\n");
875 }
876 break;
877 }
878 case nir_op_imax: {
879 if (dst.regClass() == v1) {
880 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
881 } else if (dst.regClass() == s1) {
882 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
883 } else {
884 fprintf(stderr, "Unimplemented NIR instr bit size: ");
885 nir_print_instr(&instr->instr, stderr);
886 fprintf(stderr, "\n");
887 }
888 break;
889 }
890 case nir_op_umax: {
891 if (dst.regClass() == v1) {
892 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
893 } else if (dst.regClass() == s1) {
894 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
895 } else {
896 fprintf(stderr, "Unimplemented NIR instr bit size: ");
897 nir_print_instr(&instr->instr, stderr);
898 fprintf(stderr, "\n");
899 }
900 break;
901 }
902 case nir_op_imin: {
903 if (dst.regClass() == v1) {
904 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
905 } else if (dst.regClass() == s1) {
906 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
907 } else {
908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
909 nir_print_instr(&instr->instr, stderr);
910 fprintf(stderr, "\n");
911 }
912 break;
913 }
914 case nir_op_umin: {
915 if (dst.regClass() == v1) {
916 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
917 } else if (dst.regClass() == s1) {
918 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
919 } else {
920 fprintf(stderr, "Unimplemented NIR instr bit size: ");
921 nir_print_instr(&instr->instr, stderr);
922 fprintf(stderr, "\n");
923 }
924 break;
925 }
926 case nir_op_ior: {
927 if (instr->dest.dest.ssa.bit_size == 1) {
928 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
929 } else if (dst.regClass() == v1) {
930 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
931 } else if (dst.regClass() == s1) {
932 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
933 } else if (dst.regClass() == s2) {
934 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
935 } else {
936 fprintf(stderr, "Unimplemented NIR instr bit size: ");
937 nir_print_instr(&instr->instr, stderr);
938 fprintf(stderr, "\n");
939 }
940 break;
941 }
942 case nir_op_iand: {
943 if (instr->dest.dest.ssa.bit_size == 1) {
944 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
945 } else if (dst.regClass() == v1) {
946 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
947 } else if (dst.regClass() == s1) {
948 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
949 } else if (dst.regClass() == s2) {
950 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
951 } else {
952 fprintf(stderr, "Unimplemented NIR instr bit size: ");
953 nir_print_instr(&instr->instr, stderr);
954 fprintf(stderr, "\n");
955 }
956 break;
957 }
958 case nir_op_ixor: {
959 if (instr->dest.dest.ssa.bit_size == 1) {
960 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
961 } else if (dst.regClass() == v1) {
962 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
963 } else if (dst.regClass() == s1) {
964 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
965 } else if (dst.regClass() == s2) {
966 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
967 } else {
968 fprintf(stderr, "Unimplemented NIR instr bit size: ");
969 nir_print_instr(&instr->instr, stderr);
970 fprintf(stderr, "\n");
971 }
972 break;
973 }
974 case nir_op_ushr: {
975 if (dst.regClass() == v1) {
976 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
977 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
978 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
979 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
980 } else if (dst.regClass() == v2) {
981 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
982 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
983 } else if (dst.regClass() == s2) {
984 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
985 } else if (dst.regClass() == s1) {
986 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
987 } else {
988 fprintf(stderr, "Unimplemented NIR instr bit size: ");
989 nir_print_instr(&instr->instr, stderr);
990 fprintf(stderr, "\n");
991 }
992 break;
993 }
994 case nir_op_ishl: {
995 if (dst.regClass() == v1) {
996 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
997 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
998 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
999 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1000 } else if (dst.regClass() == v2) {
1001 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1002 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1003 } else if (dst.regClass() == s1) {
1004 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1005 } else if (dst.regClass() == s2) {
1006 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1007 } else {
1008 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1009 nir_print_instr(&instr->instr, stderr);
1010 fprintf(stderr, "\n");
1011 }
1012 break;
1013 }
1014 case nir_op_ishr: {
1015 if (dst.regClass() == v1) {
1016 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1017 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1018 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1019 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1020 } else if (dst.regClass() == v2) {
1021 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1022 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1023 } else if (dst.regClass() == s1) {
1024 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1025 } else if (dst.regClass() == s2) {
1026 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1027 } else {
1028 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1029 nir_print_instr(&instr->instr, stderr);
1030 fprintf(stderr, "\n");
1031 }
1032 break;
1033 }
1034 case nir_op_find_lsb: {
1035 Temp src = get_alu_src(ctx, instr->src[0]);
1036 if (src.regClass() == s1) {
1037 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1038 } else if (src.regClass() == v1) {
1039 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1040 } else if (src.regClass() == s2) {
1041 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1042 } else {
1043 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1044 nir_print_instr(&instr->instr, stderr);
1045 fprintf(stderr, "\n");
1046 }
1047 break;
1048 }
1049 case nir_op_ufind_msb:
1050 case nir_op_ifind_msb: {
1051 Temp src = get_alu_src(ctx, instr->src[0]);
1052 if (src.regClass() == s1 || src.regClass() == s2) {
1053 aco_opcode op = src.regClass() == s2 ?
1054 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1055 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1056 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1057
1058 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1059 Operand(src.size() * 32u - 1u), msb_rev);
1060 Temp msb = sub.def(0).getTemp();
1061 Temp carry = sub.def(1).getTemp();
1062
1063 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, carry);
1064 } else if (src.regClass() == v1) {
1065 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1066 Temp msb_rev = bld.tmp(v1);
1067 emit_vop1_instruction(ctx, instr, op, msb_rev);
1068 Temp msb = bld.tmp(v1);
1069 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1070 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1071 } else {
1072 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1073 nir_print_instr(&instr->instr, stderr);
1074 fprintf(stderr, "\n");
1075 }
1076 break;
1077 }
1078 case nir_op_bitfield_reverse: {
1079 if (dst.regClass() == s1) {
1080 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1081 } else if (dst.regClass() == v1) {
1082 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1083 } else {
1084 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1085 nir_print_instr(&instr->instr, stderr);
1086 fprintf(stderr, "\n");
1087 }
1088 break;
1089 }
1090 case nir_op_iadd: {
1091 if (dst.regClass() == s1) {
1092 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1093 break;
1094 }
1095
1096 Temp src0 = get_alu_src(ctx, instr->src[0]);
1097 Temp src1 = get_alu_src(ctx, instr->src[1]);
1098 if (dst.regClass() == v1) {
1099 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1100 break;
1101 }
1102
1103 assert(src0.size() == 2 && src1.size() == 2);
1104 Temp src00 = bld.tmp(src0.type(), 1);
1105 Temp src01 = bld.tmp(dst.type(), 1);
1106 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1107 Temp src10 = bld.tmp(src1.type(), 1);
1108 Temp src11 = bld.tmp(dst.type(), 1);
1109 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1110
1111 if (dst.regClass() == s2) {
1112 Temp carry = bld.tmp(s1);
1113 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1114 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1115 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1116 } else if (dst.regClass() == v2) {
1117 Temp dst0 = bld.tmp(v1);
1118 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1119 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1120 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1121 } else {
1122 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1123 nir_print_instr(&instr->instr, stderr);
1124 fprintf(stderr, "\n");
1125 }
1126 break;
1127 }
1128 case nir_op_uadd_sat: {
1129 Temp src0 = get_alu_src(ctx, instr->src[0]);
1130 Temp src1 = get_alu_src(ctx, instr->src[1]);
1131 if (dst.regClass() == s1) {
1132 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1133 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1134 src0, src1);
1135 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1136 } else if (dst.regClass() == v1) {
1137 if (ctx->options->chip_class >= GFX9) {
1138 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1139 add->operands[0] = Operand(src0);
1140 add->operands[1] = Operand(src1);
1141 add->definitions[0] = Definition(dst);
1142 add->clamp = 1;
1143 ctx->block->instructions.emplace_back(std::move(add));
1144 } else {
1145 if (src1.regClass() != v1)
1146 std::swap(src0, src1);
1147 assert(src1.regClass() == v1);
1148 Temp tmp = bld.tmp(v1);
1149 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1150 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1151 }
1152 } else {
1153 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1154 nir_print_instr(&instr->instr, stderr);
1155 fprintf(stderr, "\n");
1156 }
1157 break;
1158 }
1159 case nir_op_uadd_carry: {
1160 Temp src0 = get_alu_src(ctx, instr->src[0]);
1161 Temp src1 = get_alu_src(ctx, instr->src[1]);
1162 if (dst.regClass() == s1) {
1163 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1164 break;
1165 }
1166 if (dst.regClass() == v1) {
1167 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1168 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1169 break;
1170 }
1171
1172 Temp src00 = bld.tmp(src0.type(), 1);
1173 Temp src01 = bld.tmp(dst.type(), 1);
1174 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1175 Temp src10 = bld.tmp(src1.type(), 1);
1176 Temp src11 = bld.tmp(dst.type(), 1);
1177 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1178 if (dst.regClass() == s2) {
1179 Temp carry = bld.tmp(s1);
1180 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1181 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1182 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1183 } else if (dst.regClass() == v2) {
1184 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1185 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1186 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1187 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1188 } else {
1189 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1190 nir_print_instr(&instr->instr, stderr);
1191 fprintf(stderr, "\n");
1192 }
1193 break;
1194 }
1195 case nir_op_isub: {
1196 if (dst.regClass() == s1) {
1197 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1198 break;
1199 }
1200
1201 Temp src0 = get_alu_src(ctx, instr->src[0]);
1202 Temp src1 = get_alu_src(ctx, instr->src[1]);
1203 if (dst.regClass() == v1) {
1204 bld.vsub32(Definition(dst), src0, src1);
1205 break;
1206 }
1207
1208 Temp src00 = bld.tmp(src0.type(), 1);
1209 Temp src01 = bld.tmp(dst.type(), 1);
1210 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1211 Temp src10 = bld.tmp(src1.type(), 1);
1212 Temp src11 = bld.tmp(dst.type(), 1);
1213 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1214 if (dst.regClass() == s2) {
1215 Temp carry = bld.tmp(s1);
1216 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1217 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1218 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1219 } else if (dst.regClass() == v2) {
1220 Temp lower = bld.tmp(v1);
1221 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1222 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1223 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1224 } else {
1225 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1226 nir_print_instr(&instr->instr, stderr);
1227 fprintf(stderr, "\n");
1228 }
1229 break;
1230 }
1231 case nir_op_usub_borrow: {
1232 Temp src0 = get_alu_src(ctx, instr->src[0]);
1233 Temp src1 = get_alu_src(ctx, instr->src[1]);
1234 if (dst.regClass() == s1) {
1235 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1236 break;
1237 } else if (dst.regClass() == v1) {
1238 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1239 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1240 break;
1241 }
1242
1243 Temp src00 = bld.tmp(src0.type(), 1);
1244 Temp src01 = bld.tmp(dst.type(), 1);
1245 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1246 Temp src10 = bld.tmp(src1.type(), 1);
1247 Temp src11 = bld.tmp(dst.type(), 1);
1248 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1249 if (dst.regClass() == s2) {
1250 Temp borrow = bld.tmp(s1);
1251 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1252 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1253 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1254 } else if (dst.regClass() == v2) {
1255 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1256 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1257 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1258 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1259 } else {
1260 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1261 nir_print_instr(&instr->instr, stderr);
1262 fprintf(stderr, "\n");
1263 }
1264 break;
1265 }
1266 case nir_op_imul: {
1267 if (dst.regClass() == v1) {
1268 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1269 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1270 } else if (dst.regClass() == s1) {
1271 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1272 } else {
1273 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1274 nir_print_instr(&instr->instr, stderr);
1275 fprintf(stderr, "\n");
1276 }
1277 break;
1278 }
1279 case nir_op_umul_high: {
1280 if (dst.regClass() == v1) {
1281 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1282 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1283 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1284 } else if (dst.regClass() == s1) {
1285 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1286 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1287 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1288 } else {
1289 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1290 nir_print_instr(&instr->instr, stderr);
1291 fprintf(stderr, "\n");
1292 }
1293 break;
1294 }
1295 case nir_op_imul_high: {
1296 if (dst.regClass() == v1) {
1297 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1298 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1299 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1300 } else if (dst.regClass() == s1) {
1301 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1302 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1303 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1304 } else {
1305 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1306 nir_print_instr(&instr->instr, stderr);
1307 fprintf(stderr, "\n");
1308 }
1309 break;
1310 }
1311 case nir_op_fmul: {
1312 if (dst.size() == 1) {
1313 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1314 } else if (dst.size() == 2) {
1315 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), get_alu_src(ctx, instr->src[0]),
1316 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1317 } else {
1318 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1319 nir_print_instr(&instr->instr, stderr);
1320 fprintf(stderr, "\n");
1321 }
1322 break;
1323 }
1324 case nir_op_fadd: {
1325 if (dst.size() == 1) {
1326 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1327 } else if (dst.size() == 2) {
1328 bld.vop3(aco_opcode::v_add_f64, Definition(dst), get_alu_src(ctx, instr->src[0]),
1329 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1330 } else {
1331 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1332 nir_print_instr(&instr->instr, stderr);
1333 fprintf(stderr, "\n");
1334 }
1335 break;
1336 }
1337 case nir_op_fsub: {
1338 Temp src0 = get_alu_src(ctx, instr->src[0]);
1339 Temp src1 = get_alu_src(ctx, instr->src[1]);
1340 if (dst.size() == 1) {
1341 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1342 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1343 else
1344 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1345 } else if (dst.size() == 2) {
1346 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1347 get_alu_src(ctx, instr->src[0]),
1348 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1349 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1350 sub->neg[1] = true;
1351 } else {
1352 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1353 nir_print_instr(&instr->instr, stderr);
1354 fprintf(stderr, "\n");
1355 }
1356 break;
1357 }
1358 case nir_op_fmax: {
1359 if (dst.size() == 1) {
1360 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1361 } else if (dst.size() == 2) {
1362 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1363 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2),
1364 get_alu_src(ctx, instr->src[0]),
1365 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1366 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1367 } else {
1368 bld.vop3(aco_opcode::v_max_f64, Definition(dst),
1369 get_alu_src(ctx, instr->src[0]),
1370 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1371 }
1372 } else {
1373 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1374 nir_print_instr(&instr->instr, stderr);
1375 fprintf(stderr, "\n");
1376 }
1377 break;
1378 }
1379 case nir_op_fmin: {
1380 if (dst.size() == 1) {
1381 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1382 } else if (dst.size() == 2) {
1383 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1384 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2),
1385 get_alu_src(ctx, instr->src[0]),
1386 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1387 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1388 } else {
1389 bld.vop3(aco_opcode::v_min_f64, Definition(dst),
1390 get_alu_src(ctx, instr->src[0]),
1391 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1392 }
1393 } else {
1394 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1395 nir_print_instr(&instr->instr, stderr);
1396 fprintf(stderr, "\n");
1397 }
1398 break;
1399 }
1400 case nir_op_fmax3: {
1401 if (dst.size() == 1) {
1402 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1403 } else {
1404 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1405 nir_print_instr(&instr->instr, stderr);
1406 fprintf(stderr, "\n");
1407 }
1408 break;
1409 }
1410 case nir_op_fmin3: {
1411 if (dst.size() == 1) {
1412 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1413 } else {
1414 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1415 nir_print_instr(&instr->instr, stderr);
1416 fprintf(stderr, "\n");
1417 }
1418 break;
1419 }
1420 case nir_op_fmed3: {
1421 if (dst.size() == 1) {
1422 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1423 } else {
1424 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1425 nir_print_instr(&instr->instr, stderr);
1426 fprintf(stderr, "\n");
1427 }
1428 break;
1429 }
1430 case nir_op_umax3: {
1431 if (dst.size() == 1) {
1432 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1433 } else {
1434 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1435 nir_print_instr(&instr->instr, stderr);
1436 fprintf(stderr, "\n");
1437 }
1438 break;
1439 }
1440 case nir_op_umin3: {
1441 if (dst.size() == 1) {
1442 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1443 } else {
1444 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1445 nir_print_instr(&instr->instr, stderr);
1446 fprintf(stderr, "\n");
1447 }
1448 break;
1449 }
1450 case nir_op_umed3: {
1451 if (dst.size() == 1) {
1452 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1453 } else {
1454 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1455 nir_print_instr(&instr->instr, stderr);
1456 fprintf(stderr, "\n");
1457 }
1458 break;
1459 }
1460 case nir_op_imax3: {
1461 if (dst.size() == 1) {
1462 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1463 } else {
1464 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1465 nir_print_instr(&instr->instr, stderr);
1466 fprintf(stderr, "\n");
1467 }
1468 break;
1469 }
1470 case nir_op_imin3: {
1471 if (dst.size() == 1) {
1472 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1473 } else {
1474 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1475 nir_print_instr(&instr->instr, stderr);
1476 fprintf(stderr, "\n");
1477 }
1478 break;
1479 }
1480 case nir_op_imed3: {
1481 if (dst.size() == 1) {
1482 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1483 } else {
1484 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1485 nir_print_instr(&instr->instr, stderr);
1486 fprintf(stderr, "\n");
1487 }
1488 break;
1489 }
1490 case nir_op_cube_face_coord: {
1491 Temp in = get_alu_src(ctx, instr->src[0], 3);
1492 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1493 emit_extract_vector(ctx, in, 1, v1),
1494 emit_extract_vector(ctx, in, 2, v1) };
1495 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1496 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1497 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1498 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1499 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1500 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1501 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1502 break;
1503 }
1504 case nir_op_cube_face_index: {
1505 Temp in = get_alu_src(ctx, instr->src[0], 3);
1506 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1507 emit_extract_vector(ctx, in, 1, v1),
1508 emit_extract_vector(ctx, in, 2, v1) };
1509 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1510 break;
1511 }
1512 case nir_op_bcsel: {
1513 emit_bcsel(ctx, instr, dst);
1514 break;
1515 }
1516 case nir_op_frsq: {
1517 if (dst.size() == 1) {
1518 emit_rsq(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1519 } else if (dst.size() == 2) {
1520 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1521 } else {
1522 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1523 nir_print_instr(&instr->instr, stderr);
1524 fprintf(stderr, "\n");
1525 }
1526 break;
1527 }
1528 case nir_op_fneg: {
1529 Temp src = get_alu_src(ctx, instr->src[0]);
1530 if (dst.size() == 1) {
1531 if (ctx->block->fp_mode.must_flush_denorms32)
1532 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1533 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1534 } else if (dst.size() == 2) {
1535 if (ctx->block->fp_mode.must_flush_denorms16_64)
1536 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1537 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1538 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1539 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1540 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1541 } else {
1542 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1543 nir_print_instr(&instr->instr, stderr);
1544 fprintf(stderr, "\n");
1545 }
1546 break;
1547 }
1548 case nir_op_fabs: {
1549 Temp src = get_alu_src(ctx, instr->src[0]);
1550 if (dst.size() == 1) {
1551 if (ctx->block->fp_mode.must_flush_denorms32)
1552 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1553 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1554 } else if (dst.size() == 2) {
1555 if (ctx->block->fp_mode.must_flush_denorms16_64)
1556 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1557 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1558 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1559 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1560 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1561 } else {
1562 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1563 nir_print_instr(&instr->instr, stderr);
1564 fprintf(stderr, "\n");
1565 }
1566 break;
1567 }
1568 case nir_op_fsat: {
1569 Temp src = get_alu_src(ctx, instr->src[0]);
1570 if (dst.size() == 1) {
1571 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1572 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1573 // TODO: confirm that this holds under any circumstances
1574 } else if (dst.size() == 2) {
1575 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1576 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1577 vop3->clamp = true;
1578 } else {
1579 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1580 nir_print_instr(&instr->instr, stderr);
1581 fprintf(stderr, "\n");
1582 }
1583 break;
1584 }
1585 case nir_op_flog2: {
1586 if (dst.size() == 1) {
1587 emit_log2(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1588 } else {
1589 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1590 nir_print_instr(&instr->instr, stderr);
1591 fprintf(stderr, "\n");
1592 }
1593 break;
1594 }
1595 case nir_op_frcp: {
1596 if (dst.size() == 1) {
1597 emit_rcp(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1598 } else if (dst.size() == 2) {
1599 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1600 } else {
1601 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1602 nir_print_instr(&instr->instr, stderr);
1603 fprintf(stderr, "\n");
1604 }
1605 break;
1606 }
1607 case nir_op_fexp2: {
1608 if (dst.size() == 1) {
1609 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1610 } else {
1611 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1612 nir_print_instr(&instr->instr, stderr);
1613 fprintf(stderr, "\n");
1614 }
1615 break;
1616 }
1617 case nir_op_fsqrt: {
1618 if (dst.size() == 1) {
1619 emit_sqrt(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1620 } else if (dst.size() == 2) {
1621 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1622 } else {
1623 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1624 nir_print_instr(&instr->instr, stderr);
1625 fprintf(stderr, "\n");
1626 }
1627 break;
1628 }
1629 case nir_op_ffract: {
1630 if (dst.size() == 1) {
1631 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1632 } else if (dst.size() == 2) {
1633 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1634 } else {
1635 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1636 nir_print_instr(&instr->instr, stderr);
1637 fprintf(stderr, "\n");
1638 }
1639 break;
1640 }
1641 case nir_op_ffloor: {
1642 if (dst.size() == 1) {
1643 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1644 } else if (dst.size() == 2) {
1645 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f64, dst);
1646 } else {
1647 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1648 nir_print_instr(&instr->instr, stderr);
1649 fprintf(stderr, "\n");
1650 }
1651 break;
1652 }
1653 case nir_op_fceil: {
1654 if (dst.size() == 1) {
1655 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1656 } else if (dst.size() == 2) {
1657 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1658 } else {
1659 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1660 nir_print_instr(&instr->instr, stderr);
1661 fprintf(stderr, "\n");
1662 }
1663 break;
1664 }
1665 case nir_op_ftrunc: {
1666 if (dst.size() == 1) {
1667 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
1668 } else if (dst.size() == 2) {
1669 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f64, dst);
1670 } else {
1671 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1672 nir_print_instr(&instr->instr, stderr);
1673 fprintf(stderr, "\n");
1674 }
1675 break;
1676 }
1677 case nir_op_fround_even: {
1678 if (dst.size() == 1) {
1679 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
1680 } else if (dst.size() == 2) {
1681 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
1682 } else {
1683 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1684 nir_print_instr(&instr->instr, stderr);
1685 fprintf(stderr, "\n");
1686 }
1687 break;
1688 }
1689 case nir_op_fsin:
1690 case nir_op_fcos: {
1691 Temp src = get_alu_src(ctx, instr->src[0]);
1692 aco_ptr<Instruction> norm;
1693 if (dst.size() == 1) {
1694 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
1695 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, as_vgpr(ctx, src));
1696
1697 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1698 if (ctx->options->chip_class < GFX9)
1699 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
1700
1701 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
1702 bld.vop1(opcode, Definition(dst), tmp);
1703 } else {
1704 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1705 nir_print_instr(&instr->instr, stderr);
1706 fprintf(stderr, "\n");
1707 }
1708 break;
1709 }
1710 case nir_op_ldexp: {
1711 if (dst.size() == 1) {
1712 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst),
1713 as_vgpr(ctx, get_alu_src(ctx, instr->src[0])),
1714 get_alu_src(ctx, instr->src[1]));
1715 } else if (dst.size() == 2) {
1716 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst),
1717 as_vgpr(ctx, get_alu_src(ctx, instr->src[0])),
1718 get_alu_src(ctx, instr->src[1]));
1719 } else {
1720 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1721 nir_print_instr(&instr->instr, stderr);
1722 fprintf(stderr, "\n");
1723 }
1724 break;
1725 }
1726 case nir_op_frexp_sig: {
1727 if (dst.size() == 1) {
1728 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst),
1729 get_alu_src(ctx, instr->src[0]));
1730 } else if (dst.size() == 2) {
1731 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst),
1732 get_alu_src(ctx, instr->src[0]));
1733 } else {
1734 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1735 nir_print_instr(&instr->instr, stderr);
1736 fprintf(stderr, "\n");
1737 }
1738 break;
1739 }
1740 case nir_op_frexp_exp: {
1741 if (instr->src[0].src.ssa->bit_size == 32) {
1742 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst),
1743 get_alu_src(ctx, instr->src[0]));
1744 } else if (instr->src[0].src.ssa->bit_size == 64) {
1745 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst),
1746 get_alu_src(ctx, instr->src[0]));
1747 } else {
1748 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1749 nir_print_instr(&instr->instr, stderr);
1750 fprintf(stderr, "\n");
1751 }
1752 break;
1753 }
1754 case nir_op_fsign: {
1755 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
1756 if (dst.size() == 1) {
1757 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1758 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
1759 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1760 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
1761 } else if (dst.size() == 2) {
1762 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1763 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
1764 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
1765
1766 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1767 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
1768 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
1769
1770 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
1771 } else {
1772 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1773 nir_print_instr(&instr->instr, stderr);
1774 fprintf(stderr, "\n");
1775 }
1776 break;
1777 }
1778 case nir_op_f2f32: {
1779 if (instr->src[0].src.ssa->bit_size == 64) {
1780 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
1781 } else {
1782 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1783 nir_print_instr(&instr->instr, stderr);
1784 fprintf(stderr, "\n");
1785 }
1786 break;
1787 }
1788 case nir_op_f2f64: {
1789 if (instr->src[0].src.ssa->bit_size == 32) {
1790 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_f32, dst);
1791 } else {
1792 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1793 nir_print_instr(&instr->instr, stderr);
1794 fprintf(stderr, "\n");
1795 }
1796 break;
1797 }
1798 case nir_op_i2f32: {
1799 assert(dst.size() == 1);
1800 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_i32, dst);
1801 break;
1802 }
1803 case nir_op_i2f64: {
1804 if (instr->src[0].src.ssa->bit_size == 32) {
1805 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_i32, dst);
1806 } else if (instr->src[0].src.ssa->bit_size == 64) {
1807 Temp src = get_alu_src(ctx, instr->src[0]);
1808 RegClass rc = RegClass(src.type(), 1);
1809 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
1810 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1811 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
1812 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
1813 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
1814 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
1815
1816 } else {
1817 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1818 nir_print_instr(&instr->instr, stderr);
1819 fprintf(stderr, "\n");
1820 }
1821 break;
1822 }
1823 case nir_op_u2f32: {
1824 assert(dst.size() == 1);
1825 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_u32, dst);
1826 break;
1827 }
1828 case nir_op_u2f64: {
1829 if (instr->src[0].src.ssa->bit_size == 32) {
1830 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_u32, dst);
1831 } else if (instr->src[0].src.ssa->bit_size == 64) {
1832 Temp src = get_alu_src(ctx, instr->src[0]);
1833 RegClass rc = RegClass(src.type(), 1);
1834 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
1835 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1836 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
1837 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
1838 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
1839 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
1840 } else {
1841 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1842 nir_print_instr(&instr->instr, stderr);
1843 fprintf(stderr, "\n");
1844 }
1845 break;
1846 }
1847 case nir_op_f2i32: {
1848 Temp src = get_alu_src(ctx, instr->src[0]);
1849 if (instr->src[0].src.ssa->bit_size == 32) {
1850 if (dst.type() == RegType::vgpr)
1851 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
1852 else
1853 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
1854 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
1855
1856 } else if (instr->src[0].src.ssa->bit_size == 64) {
1857 if (dst.type() == RegType::vgpr)
1858 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
1859 else
1860 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
1861 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
1862
1863 } else {
1864 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1865 nir_print_instr(&instr->instr, stderr);
1866 fprintf(stderr, "\n");
1867 }
1868 break;
1869 }
1870 case nir_op_f2u32: {
1871 Temp src = get_alu_src(ctx, instr->src[0]);
1872 if (instr->src[0].src.ssa->bit_size == 32) {
1873 if (dst.type() == RegType::vgpr)
1874 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
1875 else
1876 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
1877 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
1878
1879 } else if (instr->src[0].src.ssa->bit_size == 64) {
1880 if (dst.type() == RegType::vgpr)
1881 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
1882 else
1883 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
1884 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
1885
1886 } else {
1887 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1888 nir_print_instr(&instr->instr, stderr);
1889 fprintf(stderr, "\n");
1890 }
1891 break;
1892 }
1893 case nir_op_f2i64: {
1894 Temp src = get_alu_src(ctx, instr->src[0]);
1895 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
1896 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
1897 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
1898 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
1899 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
1900 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
1901 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
1902 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
1903 Temp new_exponent = bld.tmp(v1);
1904 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
1905 if (ctx->program->chip_class >= GFX8)
1906 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
1907 else
1908 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
1909 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
1910 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
1911 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
1912 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
1913 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
1914 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
1915 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
1916 Temp new_lower = bld.tmp(v1);
1917 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
1918 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
1919 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
1920
1921 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
1922 if (src.type() == RegType::vgpr)
1923 src = bld.as_uniform(src);
1924 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
1925 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
1926 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
1927 exponent = bld.sop2(aco_opcode::s_min_u32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
1928 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
1929 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
1930 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
1931 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
1932 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
1933 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
1934 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
1935 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
1936 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
1937 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
1938 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
1939 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
1940 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
1941 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
1942 Temp borrow = bld.tmp(s1);
1943 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
1944 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
1945 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1946
1947 } else if (instr->src[0].src.ssa->bit_size == 64) {
1948 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
1949 Temp trunc = bld.vop1(aco_opcode::v_trunc_f64, bld.def(v2), src);
1950 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
1951 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
1952 Temp floor = bld.vop1(aco_opcode::v_floor_f64, bld.def(v2), mul);
1953 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
1954 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
1955 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
1956 if (dst.type() == RegType::sgpr) {
1957 lower = bld.as_uniform(lower);
1958 upper = bld.as_uniform(upper);
1959 }
1960 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1961
1962 } else {
1963 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1964 nir_print_instr(&instr->instr, stderr);
1965 fprintf(stderr, "\n");
1966 }
1967 break;
1968 }
1969 case nir_op_f2u64: {
1970 Temp src = get_alu_src(ctx, instr->src[0]);
1971 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
1972 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
1973 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
1974 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
1975 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
1976 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
1977 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
1978 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
1979 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
1980 Temp new_exponent = bld.tmp(v1);
1981 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
1982 if (ctx->program->chip_class >= GFX8)
1983 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
1984 else
1985 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
1986 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
1987 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
1988 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
1989 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
1990 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
1991 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
1992 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1993
1994 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
1995 if (src.type() == RegType::vgpr)
1996 src = bld.as_uniform(src);
1997 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
1998 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
1999 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2000 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2001 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2002 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2003 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2004 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2005 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2006 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2007 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2008 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2009 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2010 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2011 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2012 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2013 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2014 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2015
2016 } else if (instr->src[0].src.ssa->bit_size == 64) {
2017 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2018 Temp trunc = bld.vop1(aco_opcode::v_trunc_f64, bld.def(v2), src);
2019 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2020 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2021 Temp floor = bld.vop1(aco_opcode::v_floor_f64, bld.def(v2), mul);
2022 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2023 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2024 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2025 if (dst.type() == RegType::sgpr) {
2026 lower = bld.as_uniform(lower);
2027 upper = bld.as_uniform(upper);
2028 }
2029 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2030
2031 } else {
2032 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2033 nir_print_instr(&instr->instr, stderr);
2034 fprintf(stderr, "\n");
2035 }
2036 break;
2037 }
2038 case nir_op_b2f32: {
2039 Temp src = get_alu_src(ctx, instr->src[0]);
2040 assert(src.regClass() == bld.lm);
2041
2042 if (dst.regClass() == s1) {
2043 src = bool_to_scalar_condition(ctx, src);
2044 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2045 } else if (dst.regClass() == v1) {
2046 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2047 } else {
2048 unreachable("Wrong destination register class for nir_op_b2f32.");
2049 }
2050 break;
2051 }
2052 case nir_op_b2f64: {
2053 Temp src = get_alu_src(ctx, instr->src[0]);
2054 assert(src.regClass() == bld.lm);
2055
2056 if (dst.regClass() == s2) {
2057 src = bool_to_scalar_condition(ctx, src);
2058 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2059 } else if (dst.regClass() == v2) {
2060 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2061 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2062 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2063 } else {
2064 unreachable("Wrong destination register class for nir_op_b2f64.");
2065 }
2066 break;
2067 }
2068 case nir_op_i2i32: {
2069 Temp src = get_alu_src(ctx, instr->src[0]);
2070 if (instr->src[0].src.ssa->bit_size == 64) {
2071 /* we can actually just say dst = src, as it would map the lower register */
2072 emit_extract_vector(ctx, src, 0, dst);
2073 } else {
2074 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2075 nir_print_instr(&instr->instr, stderr);
2076 fprintf(stderr, "\n");
2077 }
2078 break;
2079 }
2080 case nir_op_u2u32: {
2081 Temp src = get_alu_src(ctx, instr->src[0]);
2082 if (instr->src[0].src.ssa->bit_size == 16) {
2083 if (dst.regClass() == s1) {
2084 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFFFu), src);
2085 } else {
2086 // TODO: do better with SDWA
2087 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0xFFFFu), src);
2088 }
2089 } else if (instr->src[0].src.ssa->bit_size == 64) {
2090 /* we can actually just say dst = src, as it would map the lower register */
2091 emit_extract_vector(ctx, src, 0, dst);
2092 } else {
2093 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2094 nir_print_instr(&instr->instr, stderr);
2095 fprintf(stderr, "\n");
2096 }
2097 break;
2098 }
2099 case nir_op_i2i64: {
2100 Temp src = get_alu_src(ctx, instr->src[0]);
2101 if (src.regClass() == s1) {
2102 Temp high = bld.sopc(aco_opcode::s_ashr_i32, bld.def(s1, scc), src, Operand(31u));
2103 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2104 } else if (src.regClass() == v1) {
2105 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2106 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2107 } else {
2108 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2109 nir_print_instr(&instr->instr, stderr);
2110 fprintf(stderr, "\n");
2111 }
2112 break;
2113 }
2114 case nir_op_u2u64: {
2115 Temp src = get_alu_src(ctx, instr->src[0]);
2116 if (instr->src[0].src.ssa->bit_size == 32) {
2117 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, Operand(0u));
2118 } else {
2119 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2120 nir_print_instr(&instr->instr, stderr);
2121 fprintf(stderr, "\n");
2122 }
2123 break;
2124 }
2125 case nir_op_b2i32: {
2126 Temp src = get_alu_src(ctx, instr->src[0]);
2127 assert(src.regClass() == bld.lm);
2128
2129 if (dst.regClass() == s1) {
2130 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2131 bool_to_scalar_condition(ctx, src, dst);
2132 } else if (dst.regClass() == v1) {
2133 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2134 } else {
2135 unreachable("Invalid register class for b2i32");
2136 }
2137 break;
2138 }
2139 case nir_op_i2b1: {
2140 Temp src = get_alu_src(ctx, instr->src[0]);
2141 assert(dst.regClass() == bld.lm);
2142
2143 if (src.type() == RegType::vgpr) {
2144 assert(src.regClass() == v1 || src.regClass() == v2);
2145 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2146 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2147 } else {
2148 assert(src.regClass() == s1 || src.regClass() == s2);
2149 Temp tmp;
2150 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2151 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2152 } else {
2153 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2154 bld.scc(bld.def(s1)), Operand(0u), src);
2155 }
2156 bool_to_vector_condition(ctx, tmp, dst);
2157 }
2158 break;
2159 }
2160 case nir_op_pack_64_2x32_split: {
2161 Temp src0 = get_alu_src(ctx, instr->src[0]);
2162 Temp src1 = get_alu_src(ctx, instr->src[1]);
2163
2164 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2165 break;
2166 }
2167 case nir_op_unpack_64_2x32_split_x:
2168 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2169 break;
2170 case nir_op_unpack_64_2x32_split_y:
2171 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2172 break;
2173 case nir_op_pack_half_2x16: {
2174 Temp src = get_alu_src(ctx, instr->src[0], 2);
2175
2176 if (dst.regClass() == v1) {
2177 Temp src0 = bld.tmp(v1);
2178 Temp src1 = bld.tmp(v1);
2179 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2180 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2181 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2182 else
2183 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2184 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2185 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2186 } else {
2187 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2188 nir_print_instr(&instr->instr, stderr);
2189 fprintf(stderr, "\n");
2190 }
2191 break;
2192 }
2193 case nir_op_unpack_half_2x16_split_x: {
2194 if (dst.regClass() == v1) {
2195 Builder bld(ctx->program, ctx->block);
2196 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2197 } else {
2198 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2199 nir_print_instr(&instr->instr, stderr);
2200 fprintf(stderr, "\n");
2201 }
2202 break;
2203 }
2204 case nir_op_unpack_half_2x16_split_y: {
2205 if (dst.regClass() == v1) {
2206 Builder bld(ctx->program, ctx->block);
2207 /* TODO: use SDWA here */
2208 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2209 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2210 } else {
2211 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2212 nir_print_instr(&instr->instr, stderr);
2213 fprintf(stderr, "\n");
2214 }
2215 break;
2216 }
2217 case nir_op_fquantize2f16: {
2218 Temp src = get_alu_src(ctx, instr->src[0]);
2219 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2220 Temp f32, cmp_res;
2221
2222 if (ctx->program->chip_class >= GFX8) {
2223 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2224 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2225 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2226 } else {
2227 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2228 * so compare the result and flush to 0 if it's smaller.
2229 */
2230 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2231 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2232 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(s2)), f32, smallest);
2233 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2234 cmp_res = vop3->definitions[0].getTemp();
2235 }
2236
2237 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2238 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2239 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2240 } else {
2241 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2242 }
2243 break;
2244 }
2245 case nir_op_bfm: {
2246 Temp bits = get_alu_src(ctx, instr->src[0]);
2247 Temp offset = get_alu_src(ctx, instr->src[1]);
2248
2249 if (dst.regClass() == s1) {
2250 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2251 } else if (dst.regClass() == v1) {
2252 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2253 } else {
2254 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2255 nir_print_instr(&instr->instr, stderr);
2256 fprintf(stderr, "\n");
2257 }
2258 break;
2259 }
2260 case nir_op_bitfield_select: {
2261 /* (mask & insert) | (~mask & base) */
2262 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2263 Temp insert = get_alu_src(ctx, instr->src[1]);
2264 Temp base = get_alu_src(ctx, instr->src[2]);
2265
2266 /* dst = (insert & bitmask) | (base & ~bitmask) */
2267 if (dst.regClass() == s1) {
2268 aco_ptr<Instruction> sop2;
2269 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2270 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2271 Operand lhs;
2272 if (const_insert && const_bitmask) {
2273 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2274 } else {
2275 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2276 lhs = Operand(insert);
2277 }
2278
2279 Operand rhs;
2280 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2281 if (const_base && const_bitmask) {
2282 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2283 } else {
2284 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2285 rhs = Operand(base);
2286 }
2287
2288 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2289
2290 } else if (dst.regClass() == v1) {
2291 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2292 base = as_vgpr(ctx, base);
2293 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2294 insert = as_vgpr(ctx, insert);
2295
2296 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2297
2298 } else {
2299 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2300 nir_print_instr(&instr->instr, stderr);
2301 fprintf(stderr, "\n");
2302 }
2303 break;
2304 }
2305 case nir_op_ubfe:
2306 case nir_op_ibfe: {
2307 Temp base = get_alu_src(ctx, instr->src[0]);
2308 Temp offset = get_alu_src(ctx, instr->src[1]);
2309 Temp bits = get_alu_src(ctx, instr->src[2]);
2310
2311 if (dst.type() == RegType::sgpr) {
2312 Operand extract;
2313 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2314 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2315 if (const_offset && const_bits) {
2316 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2317 extract = Operand(const_extract);
2318 } else {
2319 Operand width;
2320 if (const_bits) {
2321 width = Operand(const_bits->u32 << 16);
2322 } else {
2323 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2324 }
2325 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2326 }
2327
2328 aco_opcode opcode;
2329 if (dst.regClass() == s1) {
2330 if (instr->op == nir_op_ubfe)
2331 opcode = aco_opcode::s_bfe_u32;
2332 else
2333 opcode = aco_opcode::s_bfe_i32;
2334 } else if (dst.regClass() == s2) {
2335 if (instr->op == nir_op_ubfe)
2336 opcode = aco_opcode::s_bfe_u64;
2337 else
2338 opcode = aco_opcode::s_bfe_i64;
2339 } else {
2340 unreachable("Unsupported BFE bit size");
2341 }
2342
2343 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2344
2345 } else {
2346 aco_opcode opcode;
2347 if (dst.regClass() == v1) {
2348 if (instr->op == nir_op_ubfe)
2349 opcode = aco_opcode::v_bfe_u32;
2350 else
2351 opcode = aco_opcode::v_bfe_i32;
2352 } else {
2353 unreachable("Unsupported BFE bit size");
2354 }
2355
2356 emit_vop3a_instruction(ctx, instr, opcode, dst);
2357 }
2358 break;
2359 }
2360 case nir_op_bit_count: {
2361 Temp src = get_alu_src(ctx, instr->src[0]);
2362 if (src.regClass() == s1) {
2363 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2364 } else if (src.regClass() == v1) {
2365 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2366 } else if (src.regClass() == v2) {
2367 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2368 emit_extract_vector(ctx, src, 1, v1),
2369 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2370 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2371 } else if (src.regClass() == s2) {
2372 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2373 } else {
2374 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2375 nir_print_instr(&instr->instr, stderr);
2376 fprintf(stderr, "\n");
2377 }
2378 break;
2379 }
2380 case nir_op_flt: {
2381 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2382 break;
2383 }
2384 case nir_op_fge: {
2385 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2386 break;
2387 }
2388 case nir_op_feq: {
2389 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2390 break;
2391 }
2392 case nir_op_fne: {
2393 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2394 break;
2395 }
2396 case nir_op_ilt: {
2397 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_i32, aco_opcode::v_cmp_lt_i64, aco_opcode::s_cmp_lt_i32);
2398 break;
2399 }
2400 case nir_op_ige: {
2401 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_i32, aco_opcode::v_cmp_ge_i64, aco_opcode::s_cmp_ge_i32);
2402 break;
2403 }
2404 case nir_op_ieq: {
2405 if (instr->src[0].src.ssa->bit_size == 1)
2406 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2407 else
2408 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_i32, aco_opcode::v_cmp_eq_i64, aco_opcode::s_cmp_eq_i32,
2409 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2410 break;
2411 }
2412 case nir_op_ine: {
2413 if (instr->src[0].src.ssa->bit_size == 1)
2414 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2415 else
2416 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lg_i32, aco_opcode::v_cmp_lg_i64, aco_opcode::s_cmp_lg_i32,
2417 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2418 break;
2419 }
2420 case nir_op_ult: {
2421 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_u32, aco_opcode::v_cmp_lt_u64, aco_opcode::s_cmp_lt_u32);
2422 break;
2423 }
2424 case nir_op_uge: {
2425 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_u32, aco_opcode::v_cmp_ge_u64, aco_opcode::s_cmp_ge_u32);
2426 break;
2427 }
2428 case nir_op_fddx:
2429 case nir_op_fddy:
2430 case nir_op_fddx_fine:
2431 case nir_op_fddy_fine:
2432 case nir_op_fddx_coarse:
2433 case nir_op_fddy_coarse: {
2434 Temp src = get_alu_src(ctx, instr->src[0]);
2435 uint16_t dpp_ctrl1, dpp_ctrl2;
2436 if (instr->op == nir_op_fddx_fine) {
2437 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2438 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2439 } else if (instr->op == nir_op_fddy_fine) {
2440 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2441 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2442 } else {
2443 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2444 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2445 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2446 else
2447 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2448 }
2449
2450 Temp tmp;
2451 if (ctx->program->chip_class >= GFX8) {
2452 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2453 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2454 } else {
2455 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2456 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2457 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2458 }
2459 emit_wqm(ctx, tmp, dst, true);
2460 break;
2461 }
2462 default:
2463 fprintf(stderr, "Unknown NIR ALU instr: ");
2464 nir_print_instr(&instr->instr, stderr);
2465 fprintf(stderr, "\n");
2466 }
2467 }
2468
2469 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
2470 {
2471 Temp dst = get_ssa_temp(ctx, &instr->def);
2472
2473 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2474 // which get truncated the lsb if double and msb if int
2475 // for now, we only use s_mov_b64 with 64bit inline constants
2476 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
2477 assert(dst.type() == RegType::sgpr);
2478
2479 Builder bld(ctx->program, ctx->block);
2480
2481 if (instr->def.bit_size == 1) {
2482 assert(dst.regClass() == bld.lm);
2483 int val = instr->value[0].b ? -1 : 0;
2484 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
2485 bld.sop1(Builder::s_mov, Definition(dst), op);
2486 } else if (dst.size() == 1) {
2487 bld.copy(Definition(dst), Operand(instr->value[0].u32));
2488 } else {
2489 assert(dst.size() != 1);
2490 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
2491 if (instr->def.bit_size == 64)
2492 for (unsigned i = 0; i < dst.size(); i++)
2493 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
2494 else {
2495 for (unsigned i = 0; i < dst.size(); i++)
2496 vec->operands[i] = Operand{instr->value[i].u32};
2497 }
2498 vec->definitions[0] = Definition(dst);
2499 ctx->block->instructions.emplace_back(std::move(vec));
2500 }
2501 }
2502
2503 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
2504 {
2505 uint32_t new_mask = 0;
2506 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
2507 if (mask & (1u << i))
2508 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
2509 return new_mask;
2510 }
2511
2512 void visit_store_vs_output(isel_context *ctx, nir_intrinsic_instr *instr)
2513 {
2514 /* This wouldn't work inside control flow or with indirect offsets but
2515 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2516
2517 unsigned write_mask = nir_intrinsic_write_mask(instr);
2518 unsigned component = nir_intrinsic_component(instr);
2519 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
2520 unsigned idx = nir_intrinsic_base(instr) + component;
2521
2522 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
2523 if (off_instr->type != nir_instr_type_load_const) {
2524 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
2525 nir_print_instr(off_instr, stderr);
2526 fprintf(stderr, "\n");
2527 }
2528 idx += nir_instr_as_load_const(off_instr)->value[0].u32 * 4u;
2529
2530 if (instr->src[0].ssa->bit_size == 64)
2531 write_mask = widen_mask(write_mask, 2);
2532
2533 for (unsigned i = 0; i < 8; ++i) {
2534 if (write_mask & (1 << i)) {
2535 ctx->vs_output.mask[idx / 4u] |= 1 << (idx % 4u);
2536 ctx->vs_output.outputs[idx / 4u][idx % 4u] = emit_extract_vector(ctx, src, i, v1);
2537 }
2538 idx++;
2539 }
2540 }
2541
2542 void visit_store_fs_output(isel_context *ctx, nir_intrinsic_instr *instr)
2543 {
2544 Builder bld(ctx->program, ctx->block);
2545 unsigned write_mask = nir_intrinsic_write_mask(instr);
2546 Operand values[4];
2547 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
2548 for (unsigned i = 0; i < 4; ++i) {
2549 if (write_mask & (1 << i)) {
2550 Temp tmp = emit_extract_vector(ctx, src, i, v1);
2551 values[i] = Operand(tmp);
2552 } else {
2553 values[i] = Operand(v1);
2554 }
2555 }
2556
2557 unsigned index = nir_intrinsic_base(instr) / 4;
2558 unsigned target, col_format;
2559 unsigned enabled_channels = 0xF;
2560 aco_opcode compr_op = (aco_opcode)0;
2561
2562 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
2563 assert(offset && "Non-const offsets on exports not yet supported");
2564 index += offset->u32;
2565
2566 assert(index != FRAG_RESULT_COLOR);
2567
2568 /* Unlike vertex shader exports, it's fine to use multiple exports to
2569 * export separate channels of one target. So shaders which export both
2570 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2571 * TODO: combine the exports in those cases and create better code
2572 */
2573
2574 if (index == FRAG_RESULT_SAMPLE_MASK) {
2575
2576 if (ctx->program->info->ps.writes_z) {
2577 target = V_008DFC_SQ_EXP_MRTZ;
2578 enabled_channels = 0x4;
2579 col_format = (unsigned) -1;
2580
2581 values[2] = values[0];
2582 values[0] = Operand(v1);
2583 } else {
2584 bld.exp(aco_opcode::exp, Operand(v1), Operand(values[0]), Operand(v1), Operand(v1),
2585 0xc, V_008DFC_SQ_EXP_MRTZ, true);
2586 return;
2587 }
2588
2589 } else if (index == FRAG_RESULT_DEPTH) {
2590
2591 target = V_008DFC_SQ_EXP_MRTZ;
2592 enabled_channels = 0x1;
2593 col_format = (unsigned) -1;
2594
2595 } else if (index == FRAG_RESULT_STENCIL) {
2596
2597 if (ctx->program->info->ps.writes_z) {
2598 target = V_008DFC_SQ_EXP_MRTZ;
2599 enabled_channels = 0x2;
2600 col_format = (unsigned) -1;
2601
2602 values[1] = values[0];
2603 values[0] = Operand(v1);
2604 } else {
2605 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
2606 bld.exp(aco_opcode::exp, values[0], Operand(v1), Operand(v1), Operand(v1),
2607 0x3, V_008DFC_SQ_EXP_MRTZ, true);
2608 return;
2609 }
2610
2611 } else {
2612 index -= FRAG_RESULT_DATA0;
2613 target = V_008DFC_SQ_EXP_MRT + index;
2614 col_format = (ctx->options->key.fs.col_format >> (4 * index)) & 0xf;
2615 }
2616 bool is_int8 = (ctx->options->key.fs.is_int8 >> index) & 1;
2617 bool is_int10 = (ctx->options->key.fs.is_int10 >> index) & 1;
2618
2619 switch (col_format)
2620 {
2621 case V_028714_SPI_SHADER_ZERO:
2622 enabled_channels = 0; /* writemask */
2623 target = V_008DFC_SQ_EXP_NULL;
2624 break;
2625
2626 case V_028714_SPI_SHADER_32_R:
2627 enabled_channels = 1;
2628 break;
2629
2630 case V_028714_SPI_SHADER_32_GR:
2631 enabled_channels = 0x3;
2632 break;
2633
2634 case V_028714_SPI_SHADER_32_AR:
2635 if (ctx->options->chip_class >= GFX10) {
2636 /* Special case: on GFX10, the outputs are different for 32_AR */
2637 enabled_channels = 0x3;
2638 values[1] = values[3];
2639 values[3] = Operand(v1);
2640 } else {
2641 enabled_channels = 0x9;
2642 }
2643 break;
2644
2645 case V_028714_SPI_SHADER_FP16_ABGR:
2646 enabled_channels = 0x5;
2647 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
2648 break;
2649
2650 case V_028714_SPI_SHADER_UNORM16_ABGR:
2651 enabled_channels = 0x5;
2652 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
2653 break;
2654
2655 case V_028714_SPI_SHADER_SNORM16_ABGR:
2656 enabled_channels = 0x5;
2657 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
2658 break;
2659
2660 case V_028714_SPI_SHADER_UINT16_ABGR: {
2661 enabled_channels = 0x5;
2662 compr_op = aco_opcode::v_cvt_pk_u16_u32;
2663 if (is_int8 || is_int10) {
2664 /* clamp */
2665 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
2666 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
2667
2668 for (unsigned i = 0; i < 4; i++) {
2669 if ((write_mask >> i) & 1) {
2670 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
2671 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
2672 values[i]);
2673 }
2674 }
2675 }
2676 break;
2677 }
2678
2679 case V_028714_SPI_SHADER_SINT16_ABGR:
2680 enabled_channels = 0x5;
2681 compr_op = aco_opcode::v_cvt_pk_i16_i32;
2682 if (is_int8 || is_int10) {
2683 /* clamp */
2684 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
2685 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
2686 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
2687 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
2688
2689 for (unsigned i = 0; i < 4; i++) {
2690 if ((write_mask >> i) & 1) {
2691 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
2692 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
2693 values[i]);
2694 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
2695 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
2696 values[i]);
2697 }
2698 }
2699 }
2700 break;
2701
2702 case V_028714_SPI_SHADER_32_ABGR:
2703 enabled_channels = 0xF;
2704 break;
2705
2706 default:
2707 break;
2708 }
2709
2710 if (target == V_008DFC_SQ_EXP_NULL)
2711 return;
2712
2713 if ((bool) compr_op) {
2714 for (int i = 0; i < 2; i++) {
2715 /* check if at least one of the values to be compressed is enabled */
2716 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
2717 if (enabled) {
2718 enabled_channels |= enabled << (i*2);
2719 values[i] = bld.vop3(compr_op, bld.def(v1),
2720 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
2721 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
2722 } else {
2723 values[i] = Operand(v1);
2724 }
2725 }
2726 values[2] = Operand(v1);
2727 values[3] = Operand(v1);
2728 } else {
2729 for (int i = 0; i < 4; i++)
2730 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
2731 }
2732
2733 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
2734 enabled_channels, target, (bool) compr_op);
2735 }
2736
2737 Operand load_lds_size_m0(isel_context *ctx)
2738 {
2739 /* TODO: m0 does not need to be initialized on GFX9+ */
2740 Builder bld(ctx->program, ctx->block);
2741 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
2742 }
2743
2744 void load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
2745 Temp address, unsigned base_offset, unsigned align)
2746 {
2747 assert(util_is_power_of_two_nonzero(align) && align >= 4);
2748
2749 Builder bld(ctx->program, ctx->block);
2750
2751 Operand m = load_lds_size_m0(ctx);
2752
2753 unsigned num_components = dst.size() * 4u / elem_size_bytes;
2754 unsigned bytes_read = 0;
2755 unsigned result_size = 0;
2756 unsigned total_bytes = num_components * elem_size_bytes;
2757 std::array<Temp, 4> result;
2758
2759 while (bytes_read < total_bytes) {
2760 unsigned todo = total_bytes - bytes_read;
2761 bool aligned8 = bytes_read % 8 == 0 && align % 8 == 0;
2762 bool aligned16 = bytes_read % 16 == 0 && align % 16 == 0;
2763
2764 aco_opcode op = aco_opcode::last_opcode;
2765 bool read2 = false;
2766 if (todo >= 16 && aligned16) {
2767 op = aco_opcode::ds_read_b128;
2768 todo = 16;
2769 } else if (todo >= 16 && aligned8) {
2770 op = aco_opcode::ds_read2_b64;
2771 read2 = true;
2772 todo = 16;
2773 } else if (todo >= 12 && aligned16) {
2774 op = aco_opcode::ds_read_b96;
2775 todo = 12;
2776 } else if (todo >= 8 && aligned8) {
2777 op = aco_opcode::ds_read_b64;
2778 todo = 8;
2779 } else if (todo >= 8) {
2780 op = aco_opcode::ds_read2_b32;
2781 read2 = true;
2782 todo = 8;
2783 } else if (todo >= 4) {
2784 op = aco_opcode::ds_read_b32;
2785 todo = 4;
2786 } else {
2787 assert(false);
2788 }
2789 assert(todo % elem_size_bytes == 0);
2790 unsigned num_elements = todo / elem_size_bytes;
2791 unsigned offset = base_offset + bytes_read;
2792 unsigned max_offset = read2 ? 1019 : 65535;
2793
2794 Temp address_offset = address;
2795 if (offset > max_offset) {
2796 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
2797 offset = bytes_read;
2798 }
2799 assert(offset <= max_offset); /* bytes_read shouldn't be large enough for this to happen */
2800
2801 Temp res;
2802 if (num_components == 1 && dst.type() == RegType::vgpr)
2803 res = dst;
2804 else
2805 res = bld.tmp(RegClass(RegType::vgpr, todo / 4));
2806
2807 if (read2)
2808 res = bld.ds(op, Definition(res), address_offset, m, offset >> 2, (offset >> 2) + 1);
2809 else
2810 res = bld.ds(op, Definition(res), address_offset, m, offset);
2811
2812 if (num_components == 1) {
2813 assert(todo == total_bytes);
2814 if (dst.type() == RegType::sgpr)
2815 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
2816 return;
2817 }
2818
2819 if (dst.type() == RegType::sgpr)
2820 res = bld.as_uniform(res);
2821
2822 if (num_elements == 1) {
2823 result[result_size++] = res;
2824 } else {
2825 assert(res != dst && res.size() % num_elements == 0);
2826 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_elements)};
2827 split->operands[0] = Operand(res);
2828 for (unsigned i = 0; i < num_elements; i++)
2829 split->definitions[i] = Definition(result[result_size++] = bld.tmp(res.type(), elem_size_bytes / 4));
2830 ctx->block->instructions.emplace_back(std::move(split));
2831 }
2832
2833 bytes_read += todo;
2834 }
2835
2836 assert(result_size == num_components && result_size > 1);
2837 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, result_size, 1)};
2838 for (unsigned i = 0; i < result_size; i++)
2839 vec->operands[i] = Operand(result[i]);
2840 vec->definitions[0] = Definition(dst);
2841 ctx->block->instructions.emplace_back(std::move(vec));
2842 ctx->allocated_vec.emplace(dst.id(), result);
2843 }
2844
2845 Temp extract_subvector(isel_context *ctx, Temp data, unsigned start, unsigned size, RegType type)
2846 {
2847 if (start == 0 && size == data.size())
2848 return type == RegType::vgpr ? as_vgpr(ctx, data) : data;
2849
2850 unsigned size_hint = 1;
2851 auto it = ctx->allocated_vec.find(data.id());
2852 if (it != ctx->allocated_vec.end())
2853 size_hint = it->second[0].size();
2854 if (size % size_hint || start % size_hint)
2855 size_hint = 1;
2856
2857 start /= size_hint;
2858 size /= size_hint;
2859
2860 Temp elems[size];
2861 for (unsigned i = 0; i < size; i++)
2862 elems[i] = emit_extract_vector(ctx, data, start + i, RegClass(type, size_hint));
2863
2864 if (size == 1)
2865 return type == RegType::vgpr ? as_vgpr(ctx, elems[0]) : elems[0];
2866
2867 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
2868 for (unsigned i = 0; i < size; i++)
2869 vec->operands[i] = Operand(elems[i]);
2870 Temp res = {ctx->program->allocateId(), RegClass(type, size * size_hint)};
2871 vec->definitions[0] = Definition(res);
2872 ctx->block->instructions.emplace_back(std::move(vec));
2873 return res;
2874 }
2875
2876 void ds_write_helper(isel_context *ctx, Operand m, Temp address, Temp data, unsigned data_start, unsigned total_size, unsigned offset0, unsigned offset1, unsigned align)
2877 {
2878 Builder bld(ctx->program, ctx->block);
2879 unsigned bytes_written = 0;
2880 while (bytes_written < total_size * 4) {
2881 unsigned todo = total_size * 4 - bytes_written;
2882 bool aligned8 = bytes_written % 8 == 0 && align % 8 == 0;
2883 bool aligned16 = bytes_written % 16 == 0 && align % 16 == 0;
2884
2885 aco_opcode op = aco_opcode::last_opcode;
2886 bool write2 = false;
2887 unsigned size = 0;
2888 if (todo >= 16 && aligned16) {
2889 op = aco_opcode::ds_write_b128;
2890 size = 4;
2891 } else if (todo >= 16 && aligned8) {
2892 op = aco_opcode::ds_write2_b64;
2893 write2 = true;
2894 size = 4;
2895 } else if (todo >= 12 && aligned16) {
2896 op = aco_opcode::ds_write_b96;
2897 size = 3;
2898 } else if (todo >= 8 && aligned8) {
2899 op = aco_opcode::ds_write_b64;
2900 size = 2;
2901 } else if (todo >= 8) {
2902 op = aco_opcode::ds_write2_b32;
2903 write2 = true;
2904 size = 2;
2905 } else if (todo >= 4) {
2906 op = aco_opcode::ds_write_b32;
2907 size = 1;
2908 } else {
2909 assert(false);
2910 }
2911
2912 unsigned offset = offset0 + offset1 + bytes_written;
2913 unsigned max_offset = write2 ? 1020 : 65535;
2914 Temp address_offset = address;
2915 if (offset > max_offset) {
2916 address_offset = bld.vadd32(bld.def(v1), Operand(offset0), address_offset);
2917 offset = offset1 + bytes_written;
2918 }
2919 assert(offset <= max_offset); /* offset1 shouldn't be large enough for this to happen */
2920
2921 if (write2) {
2922 Temp val0 = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size / 2, RegType::vgpr);
2923 Temp val1 = extract_subvector(ctx, data, data_start + (bytes_written >> 2) + 1, size / 2, RegType::vgpr);
2924 bld.ds(op, address_offset, val0, val1, m, offset >> 2, (offset >> 2) + 1);
2925 } else {
2926 Temp val = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size, RegType::vgpr);
2927 bld.ds(op, address_offset, val, m, offset);
2928 }
2929
2930 bytes_written += size * 4;
2931 }
2932 }
2933
2934 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
2935 Temp address, unsigned base_offset, unsigned align)
2936 {
2937 assert(util_is_power_of_two_nonzero(align) && align >= 4);
2938
2939 Operand m = load_lds_size_m0(ctx);
2940
2941 /* we need at most two stores for 32bit variables */
2942 int start[2], count[2];
2943 u_bit_scan_consecutive_range(&wrmask, &start[0], &count[0]);
2944 u_bit_scan_consecutive_range(&wrmask, &start[1], &count[1]);
2945 assert(wrmask == 0);
2946
2947 /* one combined store is sufficient */
2948 if (count[0] == count[1]) {
2949 Builder bld(ctx->program, ctx->block);
2950
2951 Temp address_offset = address;
2952 if ((base_offset >> 2) + start[1] > 255) {
2953 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
2954 base_offset = 0;
2955 }
2956
2957 assert(count[0] == 1);
2958 Temp val0 = emit_extract_vector(ctx, data, start[0], v1);
2959 Temp val1 = emit_extract_vector(ctx, data, start[1], v1);
2960 aco_opcode op = elem_size_bytes == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
2961 base_offset = base_offset / elem_size_bytes;
2962 bld.ds(op, address_offset, val0, val1, m,
2963 base_offset + start[0], base_offset + start[1]);
2964 return;
2965 }
2966
2967 for (unsigned i = 0; i < 2; i++) {
2968 if (count[i] == 0)
2969 continue;
2970
2971 unsigned elem_size_words = elem_size_bytes / 4;
2972 ds_write_helper(ctx, m, address, data, start[i] * elem_size_words, count[i] * elem_size_words,
2973 base_offset, start[i] * elem_size_bytes, align);
2974 }
2975 return;
2976 }
2977
2978 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
2979 {
2980 if (ctx->stage == vertex_vs) {
2981 visit_store_vs_output(ctx, instr);
2982 } else if (ctx->stage == fragment_fs) {
2983 visit_store_fs_output(ctx, instr);
2984 } else {
2985 unreachable("Shader stage not implemented");
2986 }
2987 }
2988
2989 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
2990 {
2991 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
2992 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
2993
2994 Builder bld(ctx->program, ctx->block);
2995 Temp tmp = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
2996 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), tmp, idx, component);
2997 }
2998
2999 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
3000 {
3001 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
3002 for (unsigned i = 0; i < num_components; i++)
3003 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
3004 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
3005 assert(num_components == 4);
3006 Builder bld(ctx->program, ctx->block);
3007 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
3008 }
3009
3010 for (Operand& op : vec->operands)
3011 op = op.isUndefined() ? Operand(0u) : op;
3012
3013 vec->definitions[0] = Definition(dst);
3014 ctx->block->instructions.emplace_back(std::move(vec));
3015 emit_split_vector(ctx, dst, num_components);
3016 return;
3017 }
3018
3019 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
3020 {
3021 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3022 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
3023 unsigned idx = nir_intrinsic_base(instr);
3024 unsigned component = nir_intrinsic_component(instr);
3025 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3026
3027 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
3028 if (offset) {
3029 assert(offset->u32 == 0);
3030 } else {
3031 /* the lower 15bit of the prim_mask contain the offset into LDS
3032 * while the upper bits contain the number of prims */
3033 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
3034 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3035 Builder bld(ctx->program, ctx->block);
3036 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3037 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3038 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3039 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3040 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3041 }
3042
3043 if (instr->dest.ssa.num_components == 1) {
3044 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
3045 } else {
3046 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
3047 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
3048 {
3049 Temp tmp = {ctx->program->allocateId(), v1};
3050 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
3051 vec->operands[i] = Operand(tmp);
3052 }
3053 vec->definitions[0] = Definition(dst);
3054 ctx->block->instructions.emplace_back(std::move(vec));
3055 }
3056 }
3057
3058 unsigned get_num_channels_from_data_format(unsigned data_format)
3059 {
3060 switch (data_format) {
3061 case V_008F0C_BUF_DATA_FORMAT_8:
3062 case V_008F0C_BUF_DATA_FORMAT_16:
3063 case V_008F0C_BUF_DATA_FORMAT_32:
3064 return 1;
3065 case V_008F0C_BUF_DATA_FORMAT_8_8:
3066 case V_008F0C_BUF_DATA_FORMAT_16_16:
3067 case V_008F0C_BUF_DATA_FORMAT_32_32:
3068 return 2;
3069 case V_008F0C_BUF_DATA_FORMAT_10_11_11:
3070 case V_008F0C_BUF_DATA_FORMAT_11_11_10:
3071 case V_008F0C_BUF_DATA_FORMAT_32_32_32:
3072 return 3;
3073 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8:
3074 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2:
3075 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10:
3076 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16:
3077 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32:
3078 return 4;
3079 default:
3080 break;
3081 }
3082
3083 return 4;
3084 }
3085
3086 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3087 * so we may need to fix it up. */
3088 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
3089 {
3090 Builder bld(ctx->program, ctx->block);
3091
3092 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
3093 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
3094
3095 /* For the integer-like cases, do a natural sign extension.
3096 *
3097 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3098 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3099 * exponent.
3100 */
3101 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
3102 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
3103
3104 /* Convert back to the right type. */
3105 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
3106 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
3107 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
3108 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
3109 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
3110 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
3111 }
3112
3113 return alpha;
3114 }
3115
3116 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
3117 {
3118 Builder bld(ctx->program, ctx->block);
3119 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3120 if (ctx->stage & sw_vs) {
3121
3122 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
3123 if (off_instr->type != nir_instr_type_load_const) {
3124 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3125 nir_print_instr(off_instr, stderr);
3126 fprintf(stderr, "\n");
3127 }
3128 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
3129
3130 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
3131
3132 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
3133 unsigned component = nir_intrinsic_component(instr);
3134 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
3135 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
3136 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
3137 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
3138
3139 unsigned dfmt = attrib_format & 0xf;
3140
3141 unsigned nfmt = (attrib_format >> 4) & 0x7;
3142 unsigned num_dfmt_channels = get_num_channels_from_data_format(dfmt);
3143 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
3144 unsigned num_channels = MIN2(util_last_bit(mask), num_dfmt_channels);
3145 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
3146 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
3147 if (post_shuffle)
3148 num_channels = MAX2(num_channels, 3);
3149
3150 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, Operand(attrib_binding * 16u));
3151
3152 Temp index;
3153 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
3154 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
3155 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
3156 if (divisor) {
3157 ctx->needs_instance_id = true;
3158 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
3159 if (divisor != 1) {
3160 Temp divided = bld.tmp(v1);
3161 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
3162 index = bld.vadd32(bld.def(v1), start_instance, divided);
3163 } else {
3164 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
3165 }
3166 } else {
3167 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
3168 }
3169 } else {
3170 index = bld.vadd32(bld.def(v1),
3171 get_arg(ctx, ctx->args->ac.base_vertex),
3172 get_arg(ctx, ctx->args->ac.vertex_id));
3173 }
3174
3175 if (attrib_stride != 0 && attrib_offset > attrib_stride) {
3176 index = bld.vadd32(bld.def(v1), Operand(attrib_offset / attrib_stride), index);
3177 attrib_offset = attrib_offset % attrib_stride;
3178 }
3179
3180 Operand soffset(0u);
3181 if (attrib_offset >= 4096) {
3182 soffset = bld.copy(bld.def(s1), Operand(attrib_offset));
3183 attrib_offset = 0;
3184 }
3185
3186 aco_opcode opcode;
3187 switch (num_channels) {
3188 case 1:
3189 opcode = aco_opcode::tbuffer_load_format_x;
3190 break;
3191 case 2:
3192 opcode = aco_opcode::tbuffer_load_format_xy;
3193 break;
3194 case 3:
3195 opcode = aco_opcode::tbuffer_load_format_xyz;
3196 break;
3197 case 4:
3198 opcode = aco_opcode::tbuffer_load_format_xyzw;
3199 break;
3200 default:
3201 unreachable("Unimplemented load_input vector size");
3202 }
3203
3204 Temp tmp = post_shuffle || num_channels != dst.size() || alpha_adjust != RADV_ALPHA_ADJUST_NONE || component ? bld.tmp(RegType::vgpr, num_channels) : dst;
3205
3206 aco_ptr<MTBUF_instruction> mubuf{create_instruction<MTBUF_instruction>(opcode, Format::MTBUF, 3, 1)};
3207 mubuf->operands[0] = Operand(index);
3208 mubuf->operands[1] = Operand(list);
3209 mubuf->operands[2] = soffset;
3210 mubuf->definitions[0] = Definition(tmp);
3211 mubuf->idxen = true;
3212 mubuf->can_reorder = true;
3213 mubuf->dfmt = dfmt;
3214 mubuf->nfmt = nfmt;
3215 assert(attrib_offset < 4096);
3216 mubuf->offset = attrib_offset;
3217 ctx->block->instructions.emplace_back(std::move(mubuf));
3218
3219 emit_split_vector(ctx, tmp, tmp.size());
3220
3221 if (tmp.id() != dst.id()) {
3222 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
3223 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
3224
3225 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
3226 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
3227 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
3228
3229 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3230 for (unsigned i = 0; i < dst.size(); i++) {
3231 unsigned idx = i + component;
3232 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE && num_channels >= 4) {
3233 Temp alpha = emit_extract_vector(ctx, tmp, swizzle[3], v1);
3234 vec->operands[3] = Operand(adjust_vertex_fetch_alpha(ctx, alpha_adjust, alpha));
3235 } else if (idx < num_channels) {
3236 vec->operands[i] = Operand(emit_extract_vector(ctx, tmp, swizzle[idx], v1));
3237 } else if (is_float && idx == 3) {
3238 vec->operands[i] = Operand(0x3f800000u);
3239 } else if (!is_float && idx == 3) {
3240 vec->operands[i] = Operand(1u);
3241 } else {
3242 vec->operands[i] = Operand(0u);
3243 }
3244 }
3245 vec->definitions[0] = Definition(dst);
3246 ctx->block->instructions.emplace_back(std::move(vec));
3247 emit_split_vector(ctx, dst, dst.size());
3248 }
3249
3250 } else if (ctx->stage == fragment_fs) {
3251 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
3252 if (off_instr->type != nir_instr_type_load_const ||
3253 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
3254 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3255 nir_print_instr(off_instr, stderr);
3256 fprintf(stderr, "\n");
3257 }
3258
3259 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3260 nir_const_value* offset = nir_src_as_const_value(instr->src[0]);
3261 if (offset) {
3262 assert(offset->u32 == 0);
3263 } else {
3264 /* the lower 15bit of the prim_mask contain the offset into LDS
3265 * while the upper bits contain the number of prims */
3266 Temp offset_src = get_ssa_temp(ctx, instr->src[0].ssa);
3267 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3268 Builder bld(ctx->program, ctx->block);
3269 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3270 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3271 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3272 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3273 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3274 }
3275
3276 unsigned idx = nir_intrinsic_base(instr);
3277 unsigned component = nir_intrinsic_component(instr);
3278
3279 if (dst.size() == 1) {
3280 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(2u), bld.m0(prim_mask), idx, component);
3281 } else {
3282 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3283 for (unsigned i = 0; i < dst.size(); i++)
3284 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(2u), bld.m0(prim_mask), idx, component + i);
3285 vec->definitions[0] = Definition(dst);
3286 bld.insert(std::move(vec));
3287 }
3288
3289 } else {
3290 unreachable("Shader stage not implemented");
3291 }
3292 }
3293
3294 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
3295 {
3296 if (ctx->program->info->need_indirect_descriptor_sets) {
3297 Builder bld(ctx->program, ctx->block);
3298 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
3299 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, Operand(desc_set << 2));//, false, false, false);
3300 }
3301
3302 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
3303 }
3304
3305
3306 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
3307 {
3308 Builder bld(ctx->program, ctx->block);
3309 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
3310 if (!ctx->divergent_vals[instr->dest.ssa.index])
3311 index = bld.as_uniform(index);
3312 unsigned desc_set = nir_intrinsic_desc_set(instr);
3313 unsigned binding = nir_intrinsic_binding(instr);
3314
3315 Temp desc_ptr;
3316 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
3317 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
3318 unsigned offset = layout->binding[binding].offset;
3319 unsigned stride;
3320 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
3321 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
3322 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
3323 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
3324 offset = pipeline_layout->push_constant_size + 16 * idx;
3325 stride = 16;
3326 } else {
3327 desc_ptr = load_desc_ptr(ctx, desc_set);
3328 stride = layout->binding[binding].size;
3329 }
3330
3331 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
3332 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
3333 if (stride != 1) {
3334 if (nir_const_index) {
3335 const_index = const_index * stride;
3336 } else if (index.type() == RegType::vgpr) {
3337 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
3338 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
3339 } else {
3340 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
3341 }
3342 }
3343 if (offset) {
3344 if (nir_const_index) {
3345 const_index = const_index + offset;
3346 } else if (index.type() == RegType::vgpr) {
3347 index = bld.vadd32(bld.def(v1), Operand(offset), index);
3348 } else {
3349 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
3350 }
3351 }
3352
3353 if (nir_const_index && const_index == 0) {
3354 index = desc_ptr;
3355 } else if (index.type() == RegType::vgpr) {
3356 index = bld.vadd32(bld.def(v1),
3357 nir_const_index ? Operand(const_index) : Operand(index),
3358 Operand(desc_ptr));
3359 } else {
3360 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3361 nir_const_index ? Operand(const_index) : Operand(index),
3362 Operand(desc_ptr));
3363 }
3364
3365 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
3366 }
3367
3368 void load_buffer(isel_context *ctx, unsigned num_components, Temp dst,
3369 Temp rsrc, Temp offset, bool glc=false, bool readonly=true)
3370 {
3371 Builder bld(ctx->program, ctx->block);
3372
3373 unsigned num_bytes = dst.size() * 4;
3374 bool dlc = glc && ctx->options->chip_class >= GFX10;
3375
3376 aco_opcode op;
3377 if (dst.type() == RegType::vgpr || (ctx->options->chip_class < GFX8 && !readonly)) {
3378 if (ctx->options->chip_class < GFX8)
3379 offset = as_vgpr(ctx, offset);
3380
3381 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3382 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3383 unsigned const_offset = 0;
3384
3385 Temp lower = Temp();
3386 if (num_bytes > 16) {
3387 assert(num_components == 3 || num_components == 4);
3388 op = aco_opcode::buffer_load_dwordx4;
3389 lower = bld.tmp(v4);
3390 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3391 mubuf->definitions[0] = Definition(lower);
3392 mubuf->operands[0] = vaddr;
3393 mubuf->operands[1] = Operand(rsrc);
3394 mubuf->operands[2] = soffset;
3395 mubuf->offen = (offset.type() == RegType::vgpr);
3396 mubuf->glc = glc;
3397 mubuf->dlc = dlc;
3398 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
3399 mubuf->can_reorder = readonly;
3400 bld.insert(std::move(mubuf));
3401 emit_split_vector(ctx, lower, 2);
3402 num_bytes -= 16;
3403 const_offset = 16;
3404 }
3405
3406 switch (num_bytes) {
3407 case 4:
3408 op = aco_opcode::buffer_load_dword;
3409 break;
3410 case 8:
3411 op = aco_opcode::buffer_load_dwordx2;
3412 break;
3413 case 12:
3414 op = aco_opcode::buffer_load_dwordx3;
3415 break;
3416 case 16:
3417 op = aco_opcode::buffer_load_dwordx4;
3418 break;
3419 default:
3420 unreachable("Load SSBO not implemented for this size.");
3421 }
3422 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3423 mubuf->operands[0] = vaddr;
3424 mubuf->operands[1] = Operand(rsrc);
3425 mubuf->operands[2] = soffset;
3426 mubuf->offen = (offset.type() == RegType::vgpr);
3427 mubuf->glc = glc;
3428 mubuf->dlc = dlc;
3429 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
3430 mubuf->can_reorder = readonly;
3431 mubuf->offset = const_offset;
3432 aco_ptr<Instruction> instr = std::move(mubuf);
3433
3434 if (dst.size() > 4) {
3435 assert(lower != Temp());
3436 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
3437 instr->definitions[0] = Definition(upper);
3438 bld.insert(std::move(instr));
3439 if (dst.size() == 8)
3440 emit_split_vector(ctx, upper, 2);
3441 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
3442 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
3443 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
3444 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
3445 if (dst.size() == 8)
3446 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
3447 }
3448
3449 if (dst.type() == RegType::sgpr) {
3450 Temp vec = bld.tmp(RegType::vgpr, dst.size());
3451 instr->definitions[0] = Definition(vec);
3452 bld.insert(std::move(instr));
3453 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
3454 } else {
3455 instr->definitions[0] = Definition(dst);
3456 bld.insert(std::move(instr));
3457 }
3458 } else {
3459 switch (num_bytes) {
3460 case 4:
3461 op = aco_opcode::s_buffer_load_dword;
3462 break;
3463 case 8:
3464 op = aco_opcode::s_buffer_load_dwordx2;
3465 break;
3466 case 12:
3467 case 16:
3468 op = aco_opcode::s_buffer_load_dwordx4;
3469 break;
3470 case 24:
3471 case 32:
3472 op = aco_opcode::s_buffer_load_dwordx8;
3473 break;
3474 default:
3475 unreachable("Load SSBO not implemented for this size.");
3476 }
3477 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3478 load->operands[0] = Operand(rsrc);
3479 load->operands[1] = Operand(bld.as_uniform(offset));
3480 assert(load->operands[1].getTemp().type() == RegType::sgpr);
3481 load->definitions[0] = Definition(dst);
3482 load->glc = glc;
3483 load->dlc = dlc;
3484 load->barrier = readonly ? barrier_none : barrier_buffer;
3485 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3486 assert(ctx->options->chip_class >= GFX8 || !glc);
3487
3488 /* trim vector */
3489 if (dst.size() == 3) {
3490 Temp vec = bld.tmp(s4);
3491 load->definitions[0] = Definition(vec);
3492 bld.insert(std::move(load));
3493 emit_split_vector(ctx, vec, 4);
3494
3495 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3496 emit_extract_vector(ctx, vec, 0, s1),
3497 emit_extract_vector(ctx, vec, 1, s1),
3498 emit_extract_vector(ctx, vec, 2, s1));
3499 } else if (dst.size() == 6) {
3500 Temp vec = bld.tmp(s8);
3501 load->definitions[0] = Definition(vec);
3502 bld.insert(std::move(load));
3503 emit_split_vector(ctx, vec, 4);
3504
3505 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3506 emit_extract_vector(ctx, vec, 0, s2),
3507 emit_extract_vector(ctx, vec, 1, s2),
3508 emit_extract_vector(ctx, vec, 2, s2));
3509 } else {
3510 bld.insert(std::move(load));
3511 }
3512
3513 }
3514 emit_split_vector(ctx, dst, num_components);
3515 }
3516
3517 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
3518 {
3519 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3520 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
3521
3522 Builder bld(ctx->program, ctx->block);
3523
3524 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
3525 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
3526 unsigned binding = nir_intrinsic_binding(idx_instr);
3527 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
3528
3529 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
3530 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
3531 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
3532 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
3533 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
3534 if (ctx->options->chip_class >= GFX10) {
3535 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
3536 S_008F0C_OOB_SELECT(3) |
3537 S_008F0C_RESOURCE_LEVEL(1);
3538 } else {
3539 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3540 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3541 }
3542 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
3543 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
3544 Operand(0xFFFFFFFFu),
3545 Operand(desc_type));
3546 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
3547 rsrc, upper_dwords);
3548 } else {
3549 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
3550 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
3551 }
3552
3553 load_buffer(ctx, instr->num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa));
3554 }
3555
3556 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
3557 {
3558 Builder bld(ctx->program, ctx->block);
3559 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3560
3561 unsigned offset = nir_intrinsic_base(instr);
3562 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
3563 if (index_cv && instr->dest.ssa.bit_size == 32) {
3564
3565 unsigned count = instr->dest.ssa.num_components;
3566 unsigned start = (offset + index_cv->u32) / 4u;
3567 start -= ctx->args->ac.base_inline_push_consts;
3568 if (start + count <= ctx->args->ac.num_inline_push_consts) {
3569 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
3570 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
3571 for (unsigned i = 0; i < count; ++i) {
3572 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
3573 vec->operands[i] = Operand{elems[i]};
3574 }
3575 vec->definitions[0] = Definition(dst);
3576 ctx->block->instructions.emplace_back(std::move(vec));
3577 ctx->allocated_vec.emplace(dst.id(), elems);
3578 return;
3579 }
3580 }
3581
3582 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
3583 if (offset != 0) // TODO check if index != 0 as well
3584 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
3585 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
3586 Temp vec = dst;
3587 bool trim = false;
3588 aco_opcode op;
3589
3590 switch (dst.size()) {
3591 case 1:
3592 op = aco_opcode::s_load_dword;
3593 break;
3594 case 2:
3595 op = aco_opcode::s_load_dwordx2;
3596 break;
3597 case 3:
3598 vec = bld.tmp(s4);
3599 trim = true;
3600 case 4:
3601 op = aco_opcode::s_load_dwordx4;
3602 break;
3603 case 6:
3604 vec = bld.tmp(s8);
3605 trim = true;
3606 case 8:
3607 op = aco_opcode::s_load_dwordx8;
3608 break;
3609 default:
3610 unreachable("unimplemented or forbidden load_push_constant.");
3611 }
3612
3613 bld.smem(op, Definition(vec), ptr, index);
3614
3615 if (trim) {
3616 emit_split_vector(ctx, vec, 4);
3617 RegClass rc = dst.size() == 3 ? s1 : s2;
3618 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3619 emit_extract_vector(ctx, vec, 0, rc),
3620 emit_extract_vector(ctx, vec, 1, rc),
3621 emit_extract_vector(ctx, vec, 2, rc));
3622
3623 }
3624 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
3625 }
3626
3627 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
3628 {
3629 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3630
3631 Builder bld(ctx->program, ctx->block);
3632
3633 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
3634 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
3635 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
3636 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
3637 if (ctx->options->chip_class >= GFX10) {
3638 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
3639 S_008F0C_OOB_SELECT(3) |
3640 S_008F0C_RESOURCE_LEVEL(1);
3641 } else {
3642 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3643 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3644 }
3645
3646 unsigned base = nir_intrinsic_base(instr);
3647 unsigned range = nir_intrinsic_range(instr);
3648
3649 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
3650 if (base && offset.type() == RegType::sgpr)
3651 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
3652 else if (base && offset.type() == RegType::vgpr)
3653 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
3654
3655 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
3656 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
3657 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
3658 Operand(desc_type));
3659
3660 load_buffer(ctx, instr->num_components, dst, rsrc, offset);
3661 }
3662
3663 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
3664 {
3665 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
3666 ctx->cf_info.exec_potentially_empty = true;
3667
3668 ctx->program->needs_exact = true;
3669
3670 // TODO: optimize uniform conditions
3671 Builder bld(ctx->program, ctx->block);
3672 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3673 assert(src.regClass() == bld.lm);
3674 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
3675 bld.pseudo(aco_opcode::p_discard_if, src);
3676 ctx->block->kind |= block_kind_uses_discard_if;
3677 return;
3678 }
3679
3680 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
3681 {
3682 Builder bld(ctx->program, ctx->block);
3683
3684 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
3685 ctx->cf_info.exec_potentially_empty = true;
3686
3687 bool divergent = ctx->cf_info.parent_if.is_divergent ||
3688 ctx->cf_info.parent_loop.has_divergent_continue;
3689
3690 if (ctx->block->loop_nest_depth &&
3691 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
3692 /* we handle discards the same way as jump instructions */
3693 append_logical_end(ctx->block);
3694
3695 /* in loops, discard behaves like break */
3696 Block *linear_target = ctx->cf_info.parent_loop.exit;
3697 ctx->block->kind |= block_kind_discard;
3698
3699 if (!divergent) {
3700 /* uniform discard - loop ends here */
3701 assert(nir_instr_is_last(&instr->instr));
3702 ctx->block->kind |= block_kind_uniform;
3703 ctx->cf_info.has_branch = true;
3704 bld.branch(aco_opcode::p_branch);
3705 add_linear_edge(ctx->block->index, linear_target);
3706 return;
3707 }
3708
3709 /* we add a break right behind the discard() instructions */
3710 ctx->block->kind |= block_kind_break;
3711 unsigned idx = ctx->block->index;
3712
3713 /* remove critical edges from linear CFG */
3714 bld.branch(aco_opcode::p_branch);
3715 Block* break_block = ctx->program->create_and_insert_block();
3716 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
3717 break_block->kind |= block_kind_uniform;
3718 add_linear_edge(idx, break_block);
3719 add_linear_edge(break_block->index, linear_target);
3720 bld.reset(break_block);
3721 bld.branch(aco_opcode::p_branch);
3722
3723 Block* continue_block = ctx->program->create_and_insert_block();
3724 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
3725 add_linear_edge(idx, continue_block);
3726 append_logical_start(continue_block);
3727 ctx->block = continue_block;
3728
3729 return;
3730 }
3731
3732 /* it can currently happen that NIR doesn't remove the unreachable code */
3733 if (!nir_instr_is_last(&instr->instr)) {
3734 ctx->program->needs_exact = true;
3735 /* save exec somewhere temporarily so that it doesn't get
3736 * overwritten before the discard from outer exec masks */
3737 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
3738 bld.pseudo(aco_opcode::p_discard_if, cond);
3739 ctx->block->kind |= block_kind_uses_discard_if;
3740 return;
3741 }
3742
3743 /* This condition is incorrect for uniformly branched discards in a loop
3744 * predicated by a divergent condition, but the above code catches that case
3745 * and the discard would end up turning into a discard_if.
3746 * For example:
3747 * if (divergent) {
3748 * while (...) {
3749 * if (uniform) {
3750 * discard;
3751 * }
3752 * }
3753 * }
3754 */
3755 if (!ctx->cf_info.parent_if.is_divergent) {
3756 /* program just ends here */
3757 ctx->block->kind |= block_kind_uniform;
3758 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
3759 0 /* enabled mask */, 9 /* dest */,
3760 false /* compressed */, true/* done */, true /* valid mask */);
3761 bld.sopp(aco_opcode::s_endpgm);
3762 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3763 } else {
3764 ctx->block->kind |= block_kind_discard;
3765 /* branch and linear edge is added by visit_if() */
3766 }
3767 }
3768
3769 enum aco_descriptor_type {
3770 ACO_DESC_IMAGE,
3771 ACO_DESC_FMASK,
3772 ACO_DESC_SAMPLER,
3773 ACO_DESC_BUFFER,
3774 ACO_DESC_PLANE_0,
3775 ACO_DESC_PLANE_1,
3776 ACO_DESC_PLANE_2,
3777 };
3778
3779 static bool
3780 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
3781 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
3782 return false;
3783 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
3784 return dim == ac_image_cube ||
3785 dim == ac_image_1darray ||
3786 dim == ac_image_2darray ||
3787 dim == ac_image_2darraymsaa;
3788 }
3789
3790 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
3791 enum aco_descriptor_type desc_type,
3792 const nir_tex_instr *tex_instr, bool image, bool write)
3793 {
3794 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3795 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3796 if (it != ctx->tex_desc.end())
3797 return it->second;
3798 */
3799 Temp index = Temp();
3800 bool index_set = false;
3801 unsigned constant_index = 0;
3802 unsigned descriptor_set;
3803 unsigned base_index;
3804 Builder bld(ctx->program, ctx->block);
3805
3806 if (!deref_instr) {
3807 assert(tex_instr && !image);
3808 descriptor_set = 0;
3809 base_index = tex_instr->sampler_index;
3810 } else {
3811 while(deref_instr->deref_type != nir_deref_type_var) {
3812 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
3813 if (!array_size)
3814 array_size = 1;
3815
3816 assert(deref_instr->deref_type == nir_deref_type_array);
3817 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
3818 if (const_value) {
3819 constant_index += array_size * const_value->u32;
3820 } else {
3821 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
3822 if (indirect.type() == RegType::vgpr)
3823 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
3824
3825 if (array_size != 1)
3826 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
3827
3828 if (!index_set) {
3829 index = indirect;
3830 index_set = true;
3831 } else {
3832 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
3833 }
3834 }
3835
3836 deref_instr = nir_src_as_deref(deref_instr->parent);
3837 }
3838 descriptor_set = deref_instr->var->data.descriptor_set;
3839 base_index = deref_instr->var->data.binding;
3840 }
3841
3842 Temp list = load_desc_ptr(ctx, descriptor_set);
3843 list = convert_pointer_to_64_bit(ctx, list);
3844
3845 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
3846 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
3847 unsigned offset = binding->offset;
3848 unsigned stride = binding->size;
3849 aco_opcode opcode;
3850 RegClass type;
3851
3852 assert(base_index < layout->binding_count);
3853
3854 switch (desc_type) {
3855 case ACO_DESC_IMAGE:
3856 type = s8;
3857 opcode = aco_opcode::s_load_dwordx8;
3858 break;
3859 case ACO_DESC_FMASK:
3860 type = s8;
3861 opcode = aco_opcode::s_load_dwordx8;
3862 offset += 32;
3863 break;
3864 case ACO_DESC_SAMPLER:
3865 type = s4;
3866 opcode = aco_opcode::s_load_dwordx4;
3867 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
3868 offset += radv_combined_image_descriptor_sampler_offset(binding);
3869 break;
3870 case ACO_DESC_BUFFER:
3871 type = s4;
3872 opcode = aco_opcode::s_load_dwordx4;
3873 break;
3874 case ACO_DESC_PLANE_0:
3875 case ACO_DESC_PLANE_1:
3876 type = s8;
3877 opcode = aco_opcode::s_load_dwordx8;
3878 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
3879 break;
3880 case ACO_DESC_PLANE_2:
3881 type = s4;
3882 opcode = aco_opcode::s_load_dwordx4;
3883 offset += 64;
3884 break;
3885 default:
3886 unreachable("invalid desc_type\n");
3887 }
3888
3889 offset += constant_index * stride;
3890
3891 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
3892 (!index_set || binding->immutable_samplers_equal)) {
3893 if (binding->immutable_samplers_equal)
3894 constant_index = 0;
3895
3896 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
3897 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
3898 Operand(samplers[constant_index * 4 + 0]),
3899 Operand(samplers[constant_index * 4 + 1]),
3900 Operand(samplers[constant_index * 4 + 2]),
3901 Operand(samplers[constant_index * 4 + 3]));
3902 }
3903
3904 Operand off;
3905 if (!index_set) {
3906 off = Operand(offset);
3907 } else {
3908 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
3909 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
3910 }
3911
3912 Temp res = bld.smem(opcode, bld.def(type), list, off);
3913
3914 if (desc_type == ACO_DESC_PLANE_2) {
3915 Temp components[8];
3916 for (unsigned i = 0; i < 8; i++)
3917 components[i] = bld.tmp(s1);
3918 bld.pseudo(aco_opcode::p_split_vector,
3919 Definition(components[0]),
3920 Definition(components[1]),
3921 Definition(components[2]),
3922 Definition(components[3]),
3923 res);
3924
3925 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
3926 bld.pseudo(aco_opcode::p_split_vector,
3927 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
3928 Definition(components[4]),
3929 Definition(components[5]),
3930 Definition(components[6]),
3931 Definition(components[7]),
3932 desc2);
3933
3934 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
3935 components[0], components[1], components[2], components[3],
3936 components[4], components[5], components[6], components[7]);
3937 }
3938
3939 return res;
3940 }
3941
3942 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
3943 {
3944 switch (dim) {
3945 case GLSL_SAMPLER_DIM_BUF:
3946 return 1;
3947 case GLSL_SAMPLER_DIM_1D:
3948 return array ? 2 : 1;
3949 case GLSL_SAMPLER_DIM_2D:
3950 return array ? 3 : 2;
3951 case GLSL_SAMPLER_DIM_MS:
3952 return array ? 4 : 3;
3953 case GLSL_SAMPLER_DIM_3D:
3954 case GLSL_SAMPLER_DIM_CUBE:
3955 return 3;
3956 case GLSL_SAMPLER_DIM_RECT:
3957 case GLSL_SAMPLER_DIM_SUBPASS:
3958 return 2;
3959 case GLSL_SAMPLER_DIM_SUBPASS_MS:
3960 return 3;
3961 default:
3962 break;
3963 }
3964 return 0;
3965 }
3966
3967
3968 /* Adjust the sample index according to FMASK.
3969 *
3970 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3971 * which is the identity mapping. Each nibble says which physical sample
3972 * should be fetched to get that sample.
3973 *
3974 * For example, 0x11111100 means there are only 2 samples stored and
3975 * the second sample covers 3/4 of the pixel. When reading samples 0
3976 * and 1, return physical sample 0 (determined by the first two 0s
3977 * in FMASK), otherwise return physical sample 1.
3978 *
3979 * The sample index should be adjusted as follows:
3980 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3981 */
3982 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, Temp coords, Operand sample_index, Temp fmask_desc_ptr)
3983 {
3984 Builder bld(ctx->program, ctx->block);
3985 Temp fmask = bld.tmp(v1);
3986 unsigned dim = ctx->options->chip_class >= GFX10
3987 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
3988 : 0;
3989
3990 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 2, 1)};
3991 load->operands[0] = Operand(coords);
3992 load->operands[1] = Operand(fmask_desc_ptr);
3993 load->definitions[0] = Definition(fmask);
3994 load->glc = false;
3995 load->dlc = false;
3996 load->dmask = 0x1;
3997 load->unrm = true;
3998 load->da = da;
3999 load->dim = dim;
4000 load->can_reorder = true; /* fmask images shouldn't be modified */
4001 ctx->block->instructions.emplace_back(std::move(load));
4002
4003 Operand sample_index4;
4004 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
4005 sample_index4 = Operand(sample_index.constantValue() << 2);
4006 } else if (sample_index.regClass() == s1) {
4007 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
4008 } else {
4009 assert(sample_index.regClass() == v1);
4010 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
4011 }
4012
4013 Temp final_sample;
4014 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
4015 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
4016 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
4017 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
4018 else
4019 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
4020
4021 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4022 * resource descriptor is 0 (invalid),
4023 */
4024 Temp compare = bld.tmp(bld.lm);
4025 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
4026 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
4027
4028 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
4029
4030 /* Replace the MSAA sample index. */
4031 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
4032 }
4033
4034 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
4035 {
4036
4037 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
4038 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4039 bool is_array = glsl_sampler_type_is_array(type);
4040 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4041 assert(!add_frag_pos && "Input attachments should be lowered.");
4042 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4043 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
4044 int count = image_type_to_components_count(dim, is_array);
4045 std::vector<Operand> coords(count);
4046
4047 if (is_ms) {
4048 Operand sample_index;
4049 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
4050 if (sample_cv)
4051 sample_index = Operand(sample_cv->u32);
4052 else
4053 sample_index = Operand(emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[2].ssa), 0, v1));
4054
4055 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
4056 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, is_array ? 3 : 2, 1)};
4057 for (unsigned i = 0; i < vec->operands.size(); i++)
4058 vec->operands[i] = Operand(emit_extract_vector(ctx, src0, i, v1));
4059 Temp fmask_load_address = {ctx->program->allocateId(), is_array ? v3 : v2};
4060 vec->definitions[0] = Definition(fmask_load_address);
4061 ctx->block->instructions.emplace_back(std::move(vec));
4062
4063 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
4064 sample_index = Operand(adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr));
4065 }
4066 count--;
4067 coords[count] = sample_index;
4068 }
4069
4070 if (count == 1 && !gfx9_1d)
4071 return emit_extract_vector(ctx, src0, 0, v1);
4072
4073 if (gfx9_1d) {
4074 coords[0] = Operand(emit_extract_vector(ctx, src0, 0, v1));
4075 coords.resize(coords.size() + 1);
4076 coords[1] = Operand((uint32_t) 0);
4077 if (is_array)
4078 coords[2] = Operand(emit_extract_vector(ctx, src0, 1, v1));
4079 } else {
4080 for (int i = 0; i < count; i++)
4081 coords[i] = Operand(emit_extract_vector(ctx, src0, i, v1));
4082 }
4083
4084 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
4085 for (unsigned i = 0; i < coords.size(); i++)
4086 vec->operands[i] = coords[i];
4087 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
4088 vec->definitions[0] = Definition(res);
4089 ctx->block->instructions.emplace_back(std::move(vec));
4090 return res;
4091 }
4092
4093
4094 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
4095 {
4096 Builder bld(ctx->program, ctx->block);
4097 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4098 const struct glsl_type *type = glsl_without_array(var->type);
4099 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4100 bool is_array = glsl_sampler_type_is_array(type);
4101 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4102
4103 if (dim == GLSL_SAMPLER_DIM_BUF) {
4104 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
4105 unsigned num_channels = util_last_bit(mask);
4106 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4107 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4108
4109 aco_opcode opcode;
4110 switch (num_channels) {
4111 case 1:
4112 opcode = aco_opcode::buffer_load_format_x;
4113 break;
4114 case 2:
4115 opcode = aco_opcode::buffer_load_format_xy;
4116 break;
4117 case 3:
4118 opcode = aco_opcode::buffer_load_format_xyz;
4119 break;
4120 case 4:
4121 opcode = aco_opcode::buffer_load_format_xyzw;
4122 break;
4123 default:
4124 unreachable(">4 channel buffer image load");
4125 }
4126 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
4127 load->operands[0] = Operand(vindex);
4128 load->operands[1] = Operand(rsrc);
4129 load->operands[2] = Operand((uint32_t) 0);
4130 Temp tmp;
4131 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4132 tmp = dst;
4133 else
4134 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
4135 load->definitions[0] = Definition(tmp);
4136 load->idxen = true;
4137 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
4138 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4139 load->barrier = barrier_image;
4140 ctx->block->instructions.emplace_back(std::move(load));
4141
4142 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
4143 return;
4144 }
4145
4146 Temp coords = get_image_coords(ctx, instr, type);
4147 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4148
4149 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
4150 unsigned num_components = util_bitcount(dmask);
4151 Temp tmp;
4152 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4153 tmp = dst;
4154 else
4155 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
4156
4157 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 2, 1)};
4158 load->operands[0] = Operand(coords);
4159 load->operands[1] = Operand(resource);
4160 load->definitions[0] = Definition(tmp);
4161 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
4162 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4163 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4164 load->dmask = dmask;
4165 load->unrm = true;
4166 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4167 load->barrier = barrier_image;
4168 ctx->block->instructions.emplace_back(std::move(load));
4169
4170 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
4171 return;
4172 }
4173
4174 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
4175 {
4176 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4177 const struct glsl_type *type = glsl_without_array(var->type);
4178 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4179 bool is_array = glsl_sampler_type_is_array(type);
4180 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4181
4182 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
4183
4184 if (dim == GLSL_SAMPLER_DIM_BUF) {
4185 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4186 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4187 aco_opcode opcode;
4188 switch (data.size()) {
4189 case 1:
4190 opcode = aco_opcode::buffer_store_format_x;
4191 break;
4192 case 2:
4193 opcode = aco_opcode::buffer_store_format_xy;
4194 break;
4195 case 3:
4196 opcode = aco_opcode::buffer_store_format_xyz;
4197 break;
4198 case 4:
4199 opcode = aco_opcode::buffer_store_format_xyzw;
4200 break;
4201 default:
4202 unreachable(">4 channel buffer image store");
4203 }
4204 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
4205 store->operands[0] = Operand(vindex);
4206 store->operands[1] = Operand(rsrc);
4207 store->operands[2] = Operand((uint32_t) 0);
4208 store->operands[3] = Operand(data);
4209 store->idxen = true;
4210 store->glc = glc;
4211 store->dlc = false;
4212 store->disable_wqm = true;
4213 store->barrier = barrier_image;
4214 ctx->program->needs_exact = true;
4215 ctx->block->instructions.emplace_back(std::move(store));
4216 return;
4217 }
4218
4219 assert(data.type() == RegType::vgpr);
4220 Temp coords = get_image_coords(ctx, instr, type);
4221 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4222
4223 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(aco_opcode::image_store, Format::MIMG, 4, 0)};
4224 store->operands[0] = Operand(coords);
4225 store->operands[1] = Operand(resource);
4226 store->operands[2] = Operand(s4);
4227 store->operands[3] = Operand(data);
4228 store->glc = glc;
4229 store->dlc = false;
4230 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4231 store->dmask = (1 << data.size()) - 1;
4232 store->unrm = true;
4233 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4234 store->disable_wqm = true;
4235 store->barrier = barrier_image;
4236 ctx->program->needs_exact = true;
4237 ctx->block->instructions.emplace_back(std::move(store));
4238 return;
4239 }
4240
4241 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4242 {
4243 /* return the previous value if dest is ever used */
4244 bool return_previous = false;
4245 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4246 return_previous = true;
4247 break;
4248 }
4249 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4250 return_previous = true;
4251 break;
4252 }
4253
4254 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4255 const struct glsl_type *type = glsl_without_array(var->type);
4256 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4257 bool is_array = glsl_sampler_type_is_array(type);
4258 Builder bld(ctx->program, ctx->block);
4259
4260 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4261 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
4262
4263 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
4264 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
4265
4266 aco_opcode buf_op, image_op;
4267 switch (instr->intrinsic) {
4268 case nir_intrinsic_image_deref_atomic_add:
4269 buf_op = aco_opcode::buffer_atomic_add;
4270 image_op = aco_opcode::image_atomic_add;
4271 break;
4272 case nir_intrinsic_image_deref_atomic_umin:
4273 buf_op = aco_opcode::buffer_atomic_umin;
4274 image_op = aco_opcode::image_atomic_umin;
4275 break;
4276 case nir_intrinsic_image_deref_atomic_imin:
4277 buf_op = aco_opcode::buffer_atomic_smin;
4278 image_op = aco_opcode::image_atomic_smin;
4279 break;
4280 case nir_intrinsic_image_deref_atomic_umax:
4281 buf_op = aco_opcode::buffer_atomic_umax;
4282 image_op = aco_opcode::image_atomic_umax;
4283 break;
4284 case nir_intrinsic_image_deref_atomic_imax:
4285 buf_op = aco_opcode::buffer_atomic_smax;
4286 image_op = aco_opcode::image_atomic_smax;
4287 break;
4288 case nir_intrinsic_image_deref_atomic_and:
4289 buf_op = aco_opcode::buffer_atomic_and;
4290 image_op = aco_opcode::image_atomic_and;
4291 break;
4292 case nir_intrinsic_image_deref_atomic_or:
4293 buf_op = aco_opcode::buffer_atomic_or;
4294 image_op = aco_opcode::image_atomic_or;
4295 break;
4296 case nir_intrinsic_image_deref_atomic_xor:
4297 buf_op = aco_opcode::buffer_atomic_xor;
4298 image_op = aco_opcode::image_atomic_xor;
4299 break;
4300 case nir_intrinsic_image_deref_atomic_exchange:
4301 buf_op = aco_opcode::buffer_atomic_swap;
4302 image_op = aco_opcode::image_atomic_swap;
4303 break;
4304 case nir_intrinsic_image_deref_atomic_comp_swap:
4305 buf_op = aco_opcode::buffer_atomic_cmpswap;
4306 image_op = aco_opcode::image_atomic_cmpswap;
4307 break;
4308 default:
4309 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4310 }
4311
4312 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4313
4314 if (dim == GLSL_SAMPLER_DIM_BUF) {
4315 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4316 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4317 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4318 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
4319 mubuf->operands[0] = Operand(vindex);
4320 mubuf->operands[1] = Operand(resource);
4321 mubuf->operands[2] = Operand((uint32_t)0);
4322 mubuf->operands[3] = Operand(data);
4323 if (return_previous)
4324 mubuf->definitions[0] = Definition(dst);
4325 mubuf->offset = 0;
4326 mubuf->idxen = true;
4327 mubuf->glc = return_previous;
4328 mubuf->dlc = false; /* Not needed for atomics */
4329 mubuf->disable_wqm = true;
4330 mubuf->barrier = barrier_image;
4331 ctx->program->needs_exact = true;
4332 ctx->block->instructions.emplace_back(std::move(mubuf));
4333 return;
4334 }
4335
4336 Temp coords = get_image_coords(ctx, instr, type);
4337 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4338 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 4, return_previous ? 1 : 0)};
4339 mimg->operands[0] = Operand(coords);
4340 mimg->operands[1] = Operand(resource);
4341 mimg->operands[2] = Operand(s4); /* no sampler */
4342 mimg->operands[3] = Operand(data);
4343 if (return_previous)
4344 mimg->definitions[0] = Definition(dst);
4345 mimg->glc = return_previous;
4346 mimg->dlc = false; /* Not needed for atomics */
4347 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4348 mimg->dmask = (1 << data.size()) - 1;
4349 mimg->unrm = true;
4350 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4351 mimg->disable_wqm = true;
4352 mimg->barrier = barrier_image;
4353 ctx->program->needs_exact = true;
4354 ctx->block->instructions.emplace_back(std::move(mimg));
4355 return;
4356 }
4357
4358 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
4359 {
4360 if (in_elements && ctx->options->chip_class == GFX8) {
4361 Builder bld(ctx->program, ctx->block);
4362
4363 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
4364 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
4365 stride = bld.vop1(aco_opcode::v_cvt_f32_ubyte0, bld.def(v1), stride);
4366 stride = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), stride);
4367
4368 Temp size = emit_extract_vector(ctx, desc, 2, s1);
4369 size = bld.vop1(aco_opcode::v_cvt_f32_u32, bld.def(v1), size);
4370
4371 Temp res = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), size, stride);
4372 res = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), res);
4373 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
4374
4375 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4376 /* idea
4377 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4378 * in case 12 (or 3?), we have to divide by 3:
4379 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4380 * use v_mul_hi_u32 with magic number to divide
4381 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4382 * disable v_skip
4383 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4384 */
4385
4386 } else {
4387 emit_extract_vector(ctx, desc, 2, dst);
4388 }
4389 }
4390
4391 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
4392 {
4393 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4394 const struct glsl_type *type = glsl_without_array(var->type);
4395 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4396 bool is_array = glsl_sampler_type_is_array(type);
4397 Builder bld(ctx->program, ctx->block);
4398
4399 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
4400 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
4401 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
4402 }
4403
4404 /* LOD */
4405 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
4406
4407 /* Resource */
4408 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
4409
4410 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4411
4412 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 2, 1)};
4413 mimg->operands[0] = Operand(lod);
4414 mimg->operands[1] = Operand(resource);
4415 unsigned& dmask = mimg->dmask;
4416 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4417 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
4418 mimg->da = glsl_sampler_type_is_array(type);
4419 mimg->can_reorder = true;
4420 Definition& def = mimg->definitions[0];
4421 ctx->block->instructions.emplace_back(std::move(mimg));
4422
4423 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
4424 glsl_sampler_type_is_array(type)) {
4425
4426 assert(instr->dest.ssa.num_components == 3);
4427 Temp tmp = {ctx->program->allocateId(), v3};
4428 def = Definition(tmp);
4429 emit_split_vector(ctx, tmp, 3);
4430
4431 /* divide 3rd value by 6 by multiplying with magic number */
4432 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
4433 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
4434
4435 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4436 emit_extract_vector(ctx, tmp, 0, v1),
4437 emit_extract_vector(ctx, tmp, 1, v1),
4438 by_6);
4439
4440 } else if (ctx->options->chip_class == GFX9 &&
4441 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
4442 glsl_sampler_type_is_array(type)) {
4443 assert(instr->dest.ssa.num_components == 2);
4444 def = Definition(dst);
4445 dmask = 0x5;
4446 } else {
4447 def = Definition(dst);
4448 }
4449
4450 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4451 }
4452
4453 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4454 {
4455 Builder bld(ctx->program, ctx->block);
4456 unsigned num_components = instr->num_components;
4457
4458 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4459 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4460 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4461
4462 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
4463 load_buffer(ctx, num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), glc, false);
4464 }
4465
4466 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4467 {
4468 Builder bld(ctx->program, ctx->block);
4469 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
4470 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4471 unsigned writemask = nir_intrinsic_write_mask(instr);
4472
4473 Temp offset;
4474 if (ctx->options->chip_class < GFX8)
4475 offset = as_vgpr(ctx,get_ssa_temp(ctx, instr->src[2].ssa));
4476 else
4477 offset = get_ssa_temp(ctx, instr->src[2].ssa);
4478
4479 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4480 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4481
4482 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
4483 ctx->options->chip_class >= GFX8;
4484 if (smem)
4485 offset = bld.as_uniform(offset);
4486 bool smem_nonfs = smem && ctx->stage != fragment_fs;
4487
4488 while (writemask) {
4489 int start, count;
4490 u_bit_scan_consecutive_range(&writemask, &start, &count);
4491 if (count == 3 && smem) {
4492 writemask |= 1u << (start + 2);
4493 count = 2;
4494 }
4495 int num_bytes = count * elem_size_bytes;
4496
4497 if (num_bytes > 16) {
4498 assert(elem_size_bytes == 8);
4499 writemask |= (((count - 2) << 1) - 1) << (start + 2);
4500 count = 2;
4501 num_bytes = 16;
4502 }
4503
4504 // TODO: check alignment of sub-dword stores
4505 // TODO: split 3 bytes. there is no store instruction for that
4506
4507 Temp write_data;
4508 if (count != instr->num_components) {
4509 emit_split_vector(ctx, data, instr->num_components);
4510 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4511 for (int i = 0; i < count; i++) {
4512 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
4513 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
4514 }
4515 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
4516 vec->definitions[0] = Definition(write_data);
4517 ctx->block->instructions.emplace_back(std::move(vec));
4518 } else if (!smem && data.type() != RegType::vgpr) {
4519 assert(num_bytes % 4 == 0);
4520 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
4521 } else if (smem_nonfs && data.type() == RegType::vgpr) {
4522 assert(num_bytes % 4 == 0);
4523 write_data = bld.as_uniform(data);
4524 } else {
4525 write_data = data;
4526 }
4527
4528 aco_opcode vmem_op, smem_op;
4529 switch (num_bytes) {
4530 case 4:
4531 vmem_op = aco_opcode::buffer_store_dword;
4532 smem_op = aco_opcode::s_buffer_store_dword;
4533 break;
4534 case 8:
4535 vmem_op = aco_opcode::buffer_store_dwordx2;
4536 smem_op = aco_opcode::s_buffer_store_dwordx2;
4537 break;
4538 case 12:
4539 vmem_op = aco_opcode::buffer_store_dwordx3;
4540 smem_op = aco_opcode::last_opcode;
4541 assert(!smem);
4542 break;
4543 case 16:
4544 vmem_op = aco_opcode::buffer_store_dwordx4;
4545 smem_op = aco_opcode::s_buffer_store_dwordx4;
4546 break;
4547 default:
4548 unreachable("Store SSBO not implemented for this size.");
4549 }
4550 if (ctx->stage == fragment_fs)
4551 smem_op = aco_opcode::p_fs_buffer_store_smem;
4552
4553 if (smem) {
4554 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
4555 store->operands[0] = Operand(rsrc);
4556 if (start) {
4557 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
4558 offset, Operand(start * elem_size_bytes));
4559 store->operands[1] = Operand(off);
4560 } else {
4561 store->operands[1] = Operand(offset);
4562 }
4563 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
4564 store->operands[1].setFixed(m0);
4565 store->operands[2] = Operand(write_data);
4566 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
4567 store->dlc = false;
4568 store->disable_wqm = true;
4569 store->barrier = barrier_buffer;
4570 ctx->block->instructions.emplace_back(std::move(store));
4571 ctx->program->wb_smem_l1_on_end = true;
4572 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
4573 ctx->block->kind |= block_kind_needs_lowering;
4574 ctx->program->needs_exact = true;
4575 }
4576 } else {
4577 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
4578 store->operands[0] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4579 store->operands[1] = Operand(rsrc);
4580 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4581 store->operands[3] = Operand(write_data);
4582 store->offset = start * elem_size_bytes;
4583 store->offen = (offset.type() == RegType::vgpr);
4584 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
4585 store->dlc = false;
4586 store->disable_wqm = true;
4587 store->barrier = barrier_buffer;
4588 ctx->program->needs_exact = true;
4589 ctx->block->instructions.emplace_back(std::move(store));
4590 }
4591 }
4592 }
4593
4594 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4595 {
4596 /* return the previous value if dest is ever used */
4597 bool return_previous = false;
4598 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4599 return_previous = true;
4600 break;
4601 }
4602 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4603 return_previous = true;
4604 break;
4605 }
4606
4607 Builder bld(ctx->program, ctx->block);
4608 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
4609
4610 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
4611 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
4612 get_ssa_temp(ctx, instr->src[3].ssa), data);
4613
4614 Temp offset;
4615 if (ctx->options->chip_class < GFX8)
4616 offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4617 else
4618 offset = get_ssa_temp(ctx, instr->src[1].ssa);
4619
4620 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4621 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4622
4623 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4624
4625 aco_opcode op32, op64;
4626 switch (instr->intrinsic) {
4627 case nir_intrinsic_ssbo_atomic_add:
4628 op32 = aco_opcode::buffer_atomic_add;
4629 op64 = aco_opcode::buffer_atomic_add_x2;
4630 break;
4631 case nir_intrinsic_ssbo_atomic_imin:
4632 op32 = aco_opcode::buffer_atomic_smin;
4633 op64 = aco_opcode::buffer_atomic_smin_x2;
4634 break;
4635 case nir_intrinsic_ssbo_atomic_umin:
4636 op32 = aco_opcode::buffer_atomic_umin;
4637 op64 = aco_opcode::buffer_atomic_umin_x2;
4638 break;
4639 case nir_intrinsic_ssbo_atomic_imax:
4640 op32 = aco_opcode::buffer_atomic_smax;
4641 op64 = aco_opcode::buffer_atomic_smax_x2;
4642 break;
4643 case nir_intrinsic_ssbo_atomic_umax:
4644 op32 = aco_opcode::buffer_atomic_umax;
4645 op64 = aco_opcode::buffer_atomic_umax_x2;
4646 break;
4647 case nir_intrinsic_ssbo_atomic_and:
4648 op32 = aco_opcode::buffer_atomic_and;
4649 op64 = aco_opcode::buffer_atomic_and_x2;
4650 break;
4651 case nir_intrinsic_ssbo_atomic_or:
4652 op32 = aco_opcode::buffer_atomic_or;
4653 op64 = aco_opcode::buffer_atomic_or_x2;
4654 break;
4655 case nir_intrinsic_ssbo_atomic_xor:
4656 op32 = aco_opcode::buffer_atomic_xor;
4657 op64 = aco_opcode::buffer_atomic_xor_x2;
4658 break;
4659 case nir_intrinsic_ssbo_atomic_exchange:
4660 op32 = aco_opcode::buffer_atomic_swap;
4661 op64 = aco_opcode::buffer_atomic_swap_x2;
4662 break;
4663 case nir_intrinsic_ssbo_atomic_comp_swap:
4664 op32 = aco_opcode::buffer_atomic_cmpswap;
4665 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
4666 break;
4667 default:
4668 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4669 }
4670 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
4671 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
4672 mubuf->operands[0] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4673 mubuf->operands[1] = Operand(rsrc);
4674 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4675 mubuf->operands[3] = Operand(data);
4676 if (return_previous)
4677 mubuf->definitions[0] = Definition(dst);
4678 mubuf->offset = 0;
4679 mubuf->offen = (offset.type() == RegType::vgpr);
4680 mubuf->glc = return_previous;
4681 mubuf->dlc = false; /* Not needed for atomics */
4682 mubuf->disable_wqm = true;
4683 mubuf->barrier = barrier_buffer;
4684 ctx->program->needs_exact = true;
4685 ctx->block->instructions.emplace_back(std::move(mubuf));
4686 }
4687
4688 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
4689
4690 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4691 Builder bld(ctx->program, ctx->block);
4692 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
4693 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
4694 }
4695
4696 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
4697 {
4698 Builder bld(ctx->program, ctx->block);
4699 unsigned num_components = instr->num_components;
4700 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
4701
4702 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4703 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
4704
4705 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
4706 bool dlc = glc && ctx->options->chip_class >= GFX10;
4707 aco_opcode op;
4708 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8)) {
4709 bool global = ctx->options->chip_class >= GFX9;
4710 aco_opcode op;
4711 switch (num_bytes) {
4712 case 4:
4713 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
4714 break;
4715 case 8:
4716 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
4717 break;
4718 case 12:
4719 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
4720 break;
4721 case 16:
4722 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
4723 break;
4724 default:
4725 unreachable("load_global not implemented for this size.");
4726 }
4727 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
4728 flat->operands[0] = Operand(addr);
4729 flat->operands[1] = Operand(s1);
4730 flat->glc = glc;
4731 flat->dlc = dlc;
4732 flat->barrier = barrier_buffer;
4733
4734 if (dst.type() == RegType::sgpr) {
4735 Temp vec = bld.tmp(RegType::vgpr, dst.size());
4736 flat->definitions[0] = Definition(vec);
4737 ctx->block->instructions.emplace_back(std::move(flat));
4738 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
4739 } else {
4740 flat->definitions[0] = Definition(dst);
4741 ctx->block->instructions.emplace_back(std::move(flat));
4742 }
4743 emit_split_vector(ctx, dst, num_components);
4744 } else {
4745 switch (num_bytes) {
4746 case 4:
4747 op = aco_opcode::s_load_dword;
4748 break;
4749 case 8:
4750 op = aco_opcode::s_load_dwordx2;
4751 break;
4752 case 12:
4753 case 16:
4754 op = aco_opcode::s_load_dwordx4;
4755 break;
4756 default:
4757 unreachable("load_global not implemented for this size.");
4758 }
4759 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
4760 load->operands[0] = Operand(addr);
4761 load->operands[1] = Operand(0u);
4762 load->definitions[0] = Definition(dst);
4763 load->glc = glc;
4764 load->dlc = dlc;
4765 load->barrier = barrier_buffer;
4766 assert(ctx->options->chip_class >= GFX8 || !glc);
4767
4768 if (dst.size() == 3) {
4769 /* trim vector */
4770 Temp vec = bld.tmp(s4);
4771 load->definitions[0] = Definition(vec);
4772 ctx->block->instructions.emplace_back(std::move(load));
4773 emit_split_vector(ctx, vec, 4);
4774
4775 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4776 emit_extract_vector(ctx, vec, 0, s1),
4777 emit_extract_vector(ctx, vec, 1, s1),
4778 emit_extract_vector(ctx, vec, 2, s1));
4779 } else {
4780 ctx->block->instructions.emplace_back(std::move(load));
4781 }
4782 }
4783 }
4784
4785 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
4786 {
4787 Builder bld(ctx->program, ctx->block);
4788 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4789
4790 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4791 Temp addr = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4792
4793 unsigned writemask = nir_intrinsic_write_mask(instr);
4794 while (writemask) {
4795 int start, count;
4796 u_bit_scan_consecutive_range(&writemask, &start, &count);
4797 unsigned num_bytes = count * elem_size_bytes;
4798
4799 Temp write_data = data;
4800 if (count != instr->num_components) {
4801 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4802 for (int i = 0; i < count; i++)
4803 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
4804 write_data = bld.tmp(RegType::vgpr, count);
4805 vec->definitions[0] = Definition(write_data);
4806 ctx->block->instructions.emplace_back(std::move(vec));
4807 }
4808
4809 unsigned offset = start * elem_size_bytes;
4810 if (offset > 0 && ctx->options->chip_class < GFX9) {
4811 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
4812 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
4813 Temp carry = bld.tmp(bld.lm);
4814 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
4815
4816 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
4817 Operand(offset), addr0);
4818 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
4819 Operand(0u), addr1,
4820 carry).def(1).setHint(vcc);
4821
4822 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
4823
4824 offset = 0;
4825 }
4826
4827 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
4828 bool global = ctx->options->chip_class >= GFX9;
4829 aco_opcode op;
4830 switch (num_bytes) {
4831 case 4:
4832 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
4833 break;
4834 case 8:
4835 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
4836 break;
4837 case 12:
4838 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
4839 break;
4840 case 16:
4841 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
4842 break;
4843 default:
4844 unreachable("store_global not implemented for this size.");
4845 }
4846 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
4847 flat->operands[0] = Operand(addr);
4848 flat->operands[1] = Operand(s1);
4849 flat->operands[2] = Operand(data);
4850 flat->glc = glc;
4851 flat->dlc = false;
4852 flat->offset = offset;
4853 flat->disable_wqm = true;
4854 flat->barrier = barrier_buffer;
4855 ctx->program->needs_exact = true;
4856 ctx->block->instructions.emplace_back(std::move(flat));
4857 }
4858 }
4859
4860 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4861 {
4862 /* return the previous value if dest is ever used */
4863 bool return_previous = false;
4864 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4865 return_previous = true;
4866 break;
4867 }
4868 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4869 return_previous = true;
4870 break;
4871 }
4872
4873 Builder bld(ctx->program, ctx->block);
4874 Temp addr = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4875 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4876
4877 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
4878 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
4879 get_ssa_temp(ctx, instr->src[2].ssa), data);
4880
4881 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4882
4883 bool global = ctx->options->chip_class >= GFX9;
4884 aco_opcode op32, op64;
4885 switch (instr->intrinsic) {
4886 case nir_intrinsic_global_atomic_add:
4887 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
4888 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
4889 break;
4890 case nir_intrinsic_global_atomic_imin:
4891 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
4892 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
4893 break;
4894 case nir_intrinsic_global_atomic_umin:
4895 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
4896 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
4897 break;
4898 case nir_intrinsic_global_atomic_imax:
4899 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
4900 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
4901 break;
4902 case nir_intrinsic_global_atomic_umax:
4903 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
4904 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
4905 break;
4906 case nir_intrinsic_global_atomic_and:
4907 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
4908 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
4909 break;
4910 case nir_intrinsic_global_atomic_or:
4911 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
4912 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
4913 break;
4914 case nir_intrinsic_global_atomic_xor:
4915 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
4916 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
4917 break;
4918 case nir_intrinsic_global_atomic_exchange:
4919 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
4920 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
4921 break;
4922 case nir_intrinsic_global_atomic_comp_swap:
4923 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
4924 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
4925 break;
4926 default:
4927 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
4928 }
4929 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
4930 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
4931 flat->operands[0] = Operand(addr);
4932 flat->operands[1] = Operand(s1);
4933 flat->operands[2] = Operand(data);
4934 if (return_previous)
4935 flat->definitions[0] = Definition(dst);
4936 flat->glc = return_previous;
4937 flat->dlc = false; /* Not needed for atomics */
4938 flat->offset = 0;
4939 flat->disable_wqm = true;
4940 flat->barrier = barrier_buffer;
4941 ctx->program->needs_exact = true;
4942 ctx->block->instructions.emplace_back(std::move(flat));
4943 }
4944
4945 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
4946 Builder bld(ctx->program, ctx->block);
4947 switch(instr->intrinsic) {
4948 case nir_intrinsic_group_memory_barrier:
4949 case nir_intrinsic_memory_barrier:
4950 bld.barrier(aco_opcode::p_memory_barrier_all);
4951 break;
4952 case nir_intrinsic_memory_barrier_atomic_counter:
4953 bld.barrier(aco_opcode::p_memory_barrier_atomic);
4954 break;
4955 case nir_intrinsic_memory_barrier_buffer:
4956 bld.barrier(aco_opcode::p_memory_barrier_buffer);
4957 break;
4958 case nir_intrinsic_memory_barrier_image:
4959 bld.barrier(aco_opcode::p_memory_barrier_image);
4960 break;
4961 case nir_intrinsic_memory_barrier_shared:
4962 bld.barrier(aco_opcode::p_memory_barrier_shared);
4963 break;
4964 default:
4965 unreachable("Unimplemented memory barrier intrinsic");
4966 break;
4967 }
4968 }
4969
4970 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
4971 {
4972 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4973 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4974 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
4975 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4976 Builder bld(ctx->program, ctx->block);
4977
4978 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4979 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
4980 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
4981 }
4982
4983 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
4984 {
4985 unsigned writemask = nir_intrinsic_write_mask(instr);
4986 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
4987 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4988 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4989 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
4990
4991 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
4992 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
4993 }
4994
4995 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4996 {
4997 unsigned offset = nir_intrinsic_base(instr);
4998 Operand m = load_lds_size_m0(ctx);
4999 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5000 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5001
5002 unsigned num_operands = 3;
5003 aco_opcode op32, op64, op32_rtn, op64_rtn;
5004 switch(instr->intrinsic) {
5005 case nir_intrinsic_shared_atomic_add:
5006 op32 = aco_opcode::ds_add_u32;
5007 op64 = aco_opcode::ds_add_u64;
5008 op32_rtn = aco_opcode::ds_add_rtn_u32;
5009 op64_rtn = aco_opcode::ds_add_rtn_u64;
5010 break;
5011 case nir_intrinsic_shared_atomic_imin:
5012 op32 = aco_opcode::ds_min_i32;
5013 op64 = aco_opcode::ds_min_i64;
5014 op32_rtn = aco_opcode::ds_min_rtn_i32;
5015 op64_rtn = aco_opcode::ds_min_rtn_i64;
5016 break;
5017 case nir_intrinsic_shared_atomic_umin:
5018 op32 = aco_opcode::ds_min_u32;
5019 op64 = aco_opcode::ds_min_u64;
5020 op32_rtn = aco_opcode::ds_min_rtn_u32;
5021 op64_rtn = aco_opcode::ds_min_rtn_u64;
5022 break;
5023 case nir_intrinsic_shared_atomic_imax:
5024 op32 = aco_opcode::ds_max_i32;
5025 op64 = aco_opcode::ds_max_i64;
5026 op32_rtn = aco_opcode::ds_max_rtn_i32;
5027 op64_rtn = aco_opcode::ds_max_rtn_i64;
5028 break;
5029 case nir_intrinsic_shared_atomic_umax:
5030 op32 = aco_opcode::ds_max_u32;
5031 op64 = aco_opcode::ds_max_u64;
5032 op32_rtn = aco_opcode::ds_max_rtn_u32;
5033 op64_rtn = aco_opcode::ds_max_rtn_u64;
5034 break;
5035 case nir_intrinsic_shared_atomic_and:
5036 op32 = aco_opcode::ds_and_b32;
5037 op64 = aco_opcode::ds_and_b64;
5038 op32_rtn = aco_opcode::ds_and_rtn_b32;
5039 op64_rtn = aco_opcode::ds_and_rtn_b64;
5040 break;
5041 case nir_intrinsic_shared_atomic_or:
5042 op32 = aco_opcode::ds_or_b32;
5043 op64 = aco_opcode::ds_or_b64;
5044 op32_rtn = aco_opcode::ds_or_rtn_b32;
5045 op64_rtn = aco_opcode::ds_or_rtn_b64;
5046 break;
5047 case nir_intrinsic_shared_atomic_xor:
5048 op32 = aco_opcode::ds_xor_b32;
5049 op64 = aco_opcode::ds_xor_b64;
5050 op32_rtn = aco_opcode::ds_xor_rtn_b32;
5051 op64_rtn = aco_opcode::ds_xor_rtn_b64;
5052 break;
5053 case nir_intrinsic_shared_atomic_exchange:
5054 op32 = aco_opcode::ds_write_b32;
5055 op64 = aco_opcode::ds_write_b64;
5056 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
5057 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
5058 break;
5059 case nir_intrinsic_shared_atomic_comp_swap:
5060 op32 = aco_opcode::ds_cmpst_b32;
5061 op64 = aco_opcode::ds_cmpst_b64;
5062 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
5063 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
5064 num_operands = 4;
5065 break;
5066 default:
5067 unreachable("Unhandled shared atomic intrinsic");
5068 }
5069
5070 /* return the previous value if dest is ever used */
5071 bool return_previous = false;
5072 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5073 return_previous = true;
5074 break;
5075 }
5076 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5077 return_previous = true;
5078 break;
5079 }
5080
5081 aco_opcode op;
5082 if (data.size() == 1) {
5083 assert(instr->dest.ssa.bit_size == 32);
5084 op = return_previous ? op32_rtn : op32;
5085 } else {
5086 assert(instr->dest.ssa.bit_size == 64);
5087 op = return_previous ? op64_rtn : op64;
5088 }
5089
5090 if (offset > 65535) {
5091 Builder bld(ctx->program, ctx->block);
5092 address = bld.vadd32(bld.def(v1), Operand(offset), address);
5093 offset = 0;
5094 }
5095
5096 aco_ptr<DS_instruction> ds;
5097 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
5098 ds->operands[0] = Operand(address);
5099 ds->operands[1] = Operand(data);
5100 if (num_operands == 4)
5101 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
5102 ds->operands[num_operands - 1] = m;
5103 ds->offset0 = offset;
5104 if (return_previous)
5105 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
5106 ctx->block->instructions.emplace_back(std::move(ds));
5107 }
5108
5109 Temp get_scratch_resource(isel_context *ctx)
5110 {
5111 Builder bld(ctx->program, ctx->block);
5112 Temp scratch_addr = ctx->program->private_segment_buffer;
5113 if (ctx->stage != compute_cs)
5114 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
5115
5116 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
5117 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
5118
5119 if (ctx->program->chip_class >= GFX10) {
5120 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5121 S_008F0C_OOB_SELECT(3) |
5122 S_008F0C_RESOURCE_LEVEL(1);
5123 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
5124 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5125 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5126 }
5127
5128 /* older generations need element size = 16 bytes. element size removed in GFX9 */
5129 if (ctx->program->chip_class <= GFX8)
5130 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
5131
5132 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
5133 }
5134
5135 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
5136 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
5137 Builder bld(ctx->program, ctx->block);
5138 Temp rsrc = get_scratch_resource(ctx);
5139 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5140 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5141
5142 aco_opcode op;
5143 switch (dst.size()) {
5144 case 1:
5145 op = aco_opcode::buffer_load_dword;
5146 break;
5147 case 2:
5148 op = aco_opcode::buffer_load_dwordx2;
5149 break;
5150 case 3:
5151 op = aco_opcode::buffer_load_dwordx3;
5152 break;
5153 case 4:
5154 op = aco_opcode::buffer_load_dwordx4;
5155 break;
5156 case 6:
5157 case 8: {
5158 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5159 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
5160 bld.def(v4), offset, rsrc,
5161 ctx->program->scratch_offset, 0, true);
5162 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
5163 aco_opcode::buffer_load_dwordx4,
5164 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
5165 offset, rsrc, ctx->program->scratch_offset, 16, true);
5166 emit_split_vector(ctx, lower, 2);
5167 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
5168 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
5169 if (dst.size() == 8) {
5170 emit_split_vector(ctx, upper, 2);
5171 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
5172 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
5173 } else {
5174 elems[2] = upper;
5175 }
5176
5177 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
5178 Format::PSEUDO, dst.size() / 2, 1)};
5179 for (unsigned i = 0; i < dst.size() / 2; i++)
5180 vec->operands[i] = Operand(elems[i]);
5181 vec->definitions[0] = Definition(dst);
5182 bld.insert(std::move(vec));
5183 ctx->allocated_vec.emplace(dst.id(), elems);
5184 return;
5185 }
5186 default:
5187 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5188 }
5189
5190 bld.mubuf(op, Definition(dst), offset, rsrc, ctx->program->scratch_offset, 0, true);
5191 emit_split_vector(ctx, dst, instr->num_components);
5192 }
5193
5194 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
5195 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
5196 Builder bld(ctx->program, ctx->block);
5197 Temp rsrc = get_scratch_resource(ctx);
5198 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5199 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5200
5201 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5202 unsigned writemask = nir_intrinsic_write_mask(instr);
5203
5204 while (writemask) {
5205 int start, count;
5206 u_bit_scan_consecutive_range(&writemask, &start, &count);
5207 int num_bytes = count * elem_size_bytes;
5208
5209 if (num_bytes > 16) {
5210 assert(elem_size_bytes == 8);
5211 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5212 count = 2;
5213 num_bytes = 16;
5214 }
5215
5216 // TODO: check alignment of sub-dword stores
5217 // TODO: split 3 bytes. there is no store instruction for that
5218
5219 Temp write_data;
5220 if (count != instr->num_components) {
5221 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5222 for (int i = 0; i < count; i++) {
5223 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
5224 vec->operands[i] = Operand(elem);
5225 }
5226 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
5227 vec->definitions[0] = Definition(write_data);
5228 ctx->block->instructions.emplace_back(std::move(vec));
5229 } else {
5230 write_data = data;
5231 }
5232
5233 aco_opcode op;
5234 switch (num_bytes) {
5235 case 4:
5236 op = aco_opcode::buffer_store_dword;
5237 break;
5238 case 8:
5239 op = aco_opcode::buffer_store_dwordx2;
5240 break;
5241 case 12:
5242 op = aco_opcode::buffer_store_dwordx3;
5243 break;
5244 case 16:
5245 op = aco_opcode::buffer_store_dwordx4;
5246 break;
5247 default:
5248 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5249 }
5250
5251 bld.mubuf(op, offset, rsrc, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
5252 }
5253 }
5254
5255 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
5256 uint8_t log2_ps_iter_samples;
5257 if (ctx->program->info->ps.force_persample) {
5258 log2_ps_iter_samples =
5259 util_logbase2(ctx->options->key.fs.num_samples);
5260 } else {
5261 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
5262 }
5263
5264 /* The bit pattern matches that used by fixed function fragment
5265 * processing. */
5266 static const unsigned ps_iter_masks[] = {
5267 0xffff, /* not used */
5268 0x5555,
5269 0x1111,
5270 0x0101,
5271 0x0001,
5272 };
5273 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
5274
5275 Builder bld(ctx->program, ctx->block);
5276
5277 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5278 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
5279 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
5280 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
5281 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5282 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
5283 }
5284
5285 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
5286 {
5287 Builder bld(ctx->program, ctx->block);
5288
5289 if (cluster_size == 1) {
5290 return src;
5291 } if (op == nir_op_iand && cluster_size == 4) {
5292 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5293 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
5294 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
5295 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
5296 } else if (op == nir_op_ior && cluster_size == 4) {
5297 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5298 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
5299 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
5300 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
5301 //subgroupAnd(val) -> (exec & ~val) == 0
5302 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
5303 return bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(0u), Operand(-1u), bld.scc(tmp));
5304 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
5305 //subgroupOr(val) -> (val & exec) != 0
5306 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
5307 return bool_to_vector_condition(ctx, tmp);
5308 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
5309 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5310 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5311 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
5312 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
5313 return bool_to_vector_condition(ctx, tmp);
5314 } else {
5315 //subgroupClustered{And,Or,Xor}(val, n) ->
5316 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
5317 //cluster_offset = ~(n - 1) & lane_id
5318 //cluster_mask = ((1 << n) - 1)
5319 //subgroupClusteredAnd():
5320 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5321 //subgroupClusteredOr():
5322 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5323 //subgroupClusteredXor():
5324 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5325 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
5326 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
5327
5328 Temp tmp;
5329 if (op == nir_op_iand)
5330 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5331 else
5332 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5333
5334 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
5335
5336 if (ctx->program->chip_class <= GFX7)
5337 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
5338 else if (ctx->program->wave_size == 64)
5339 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
5340 else
5341 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
5342 tmp = emit_extract_vector(ctx, tmp, 0, v1);
5343 if (cluster_mask != 0xffffffff)
5344 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
5345
5346 Definition cmp_def = Definition();
5347 if (op == nir_op_iand) {
5348 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
5349 } else if (op == nir_op_ior) {
5350 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
5351 } else if (op == nir_op_ixor) {
5352 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
5353 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
5354 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
5355 }
5356 cmp_def.setHint(vcc);
5357 return cmp_def.getTemp();
5358 }
5359 }
5360
5361 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
5362 {
5363 Builder bld(ctx->program, ctx->block);
5364
5365 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5366 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5367 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5368 Temp tmp;
5369 if (op == nir_op_iand)
5370 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
5371 else
5372 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
5373
5374 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
5375 Temp lo = lohi.def(0).getTemp();
5376 Temp hi = lohi.def(1).getTemp();
5377 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
5378
5379 Definition cmp_def = Definition();
5380 if (op == nir_op_iand)
5381 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
5382 else if (op == nir_op_ior)
5383 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
5384 else if (op == nir_op_ixor)
5385 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
5386 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
5387 cmp_def.setHint(vcc);
5388 return cmp_def.getTemp();
5389 }
5390
5391 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
5392 {
5393 Builder bld(ctx->program, ctx->block);
5394
5395 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5396 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5397 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5398 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
5399 if (op == nir_op_iand)
5400 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
5401 else if (op == nir_op_ior)
5402 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
5403 else if (op == nir_op_ixor)
5404 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
5405
5406 assert(false);
5407 return Temp();
5408 }
5409
5410 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
5411 {
5412 Builder bld(ctx->program, ctx->block);
5413 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
5414 if (src.regClass().type() == RegType::vgpr) {
5415 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
5416 } else if (src.regClass() == s1) {
5417 bld.sop1(aco_opcode::s_mov_b32, dst, src);
5418 } else if (src.regClass() == s2) {
5419 bld.sop1(aco_opcode::s_mov_b64, dst, src);
5420 } else {
5421 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5422 nir_print_instr(&instr->instr, stderr);
5423 fprintf(stderr, "\n");
5424 }
5425 }
5426
5427 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
5428 {
5429 Builder bld(ctx->program, ctx->block);
5430 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
5431 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
5432 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
5433
5434 Temp ddx_1, ddx_2, ddy_1, ddy_2;
5435 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
5436 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
5437 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
5438
5439 /* Build DD X/Y */
5440 if (ctx->program->chip_class >= GFX8) {
5441 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
5442 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
5443 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
5444 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
5445 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
5446 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
5447 } else {
5448 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
5449 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
5450 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
5451 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
5452 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
5453 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
5454 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
5455 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
5456 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
5457 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
5458 }
5459
5460 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5461 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
5462 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
5463 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
5464 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
5465 Temp wqm1 = bld.tmp(v1);
5466 emit_wqm(ctx, tmp1, wqm1, true);
5467 Temp wqm2 = bld.tmp(v1);
5468 emit_wqm(ctx, tmp2, wqm2, true);
5469 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
5470 return;
5471 }
5472
5473 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
5474 {
5475 Builder bld(ctx->program, ctx->block);
5476 switch(instr->intrinsic) {
5477 case nir_intrinsic_load_barycentric_sample:
5478 case nir_intrinsic_load_barycentric_pixel:
5479 case nir_intrinsic_load_barycentric_centroid: {
5480 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
5481 Temp bary = Temp(0, s2);
5482 switch (mode) {
5483 case INTERP_MODE_SMOOTH:
5484 case INTERP_MODE_NONE:
5485 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
5486 bary = get_arg(ctx, ctx->args->ac.persp_center);
5487 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
5488 bary = ctx->persp_centroid;
5489 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
5490 bary = get_arg(ctx, ctx->args->ac.persp_sample);
5491 break;
5492 case INTERP_MODE_NOPERSPECTIVE:
5493 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
5494 bary = get_arg(ctx, ctx->args->ac.linear_center);
5495 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
5496 bary = ctx->linear_centroid;
5497 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
5498 bary = get_arg(ctx, ctx->args->ac.linear_sample);
5499 break;
5500 default:
5501 break;
5502 }
5503 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5504 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
5505 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
5506 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5507 Operand(p1), Operand(p2));
5508 emit_split_vector(ctx, dst, 2);
5509 break;
5510 }
5511 case nir_intrinsic_load_barycentric_at_sample: {
5512 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
5513 switch (ctx->options->key.fs.num_samples) {
5514 case 2: sample_pos_offset += 1 << 3; break;
5515 case 4: sample_pos_offset += 3 << 3; break;
5516 case 8: sample_pos_offset += 7 << 3; break;
5517 default: break;
5518 }
5519 Temp sample_pos;
5520 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
5521 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
5522 Temp private_segment_buffer = ctx->program->private_segment_buffer;
5523 if (addr.type() == RegType::sgpr) {
5524 Operand offset;
5525 if (const_addr) {
5526 sample_pos_offset += const_addr->u32 << 3;
5527 offset = Operand(sample_pos_offset);
5528 } else if (ctx->options->chip_class >= GFX9) {
5529 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
5530 } else {
5531 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
5532 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
5533 }
5534 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, Operand(offset));
5535
5536 } else if (ctx->options->chip_class >= GFX9) {
5537 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
5538 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
5539 } else {
5540 /* addr += private_segment_buffer + sample_pos_offset */
5541 Temp tmp0 = bld.tmp(s1);
5542 Temp tmp1 = bld.tmp(s1);
5543 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
5544 Definition scc_tmp = bld.def(s1, scc);
5545 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
5546 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
5547 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
5548 Temp pck0 = bld.tmp(v1);
5549 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
5550 tmp1 = as_vgpr(ctx, tmp1);
5551 Temp pck1 = bld.vop2_e64(aco_opcode::v_addc_co_u32, bld.def(v1), bld.hint_vcc(bld.def(bld.lm)), tmp1, Operand(0u), carry);
5552 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
5553
5554 /* sample_pos = flat_load_dwordx2 addr */
5555 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
5556 }
5557
5558 /* sample_pos -= 0.5 */
5559 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
5560 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
5561 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
5562 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
5563 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
5564
5565 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
5566 break;
5567 }
5568 case nir_intrinsic_load_barycentric_at_offset: {
5569 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5570 RegClass rc = RegClass(offset.type(), 1);
5571 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
5572 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
5573 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
5574 break;
5575 }
5576 case nir_intrinsic_load_front_face: {
5577 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5578 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
5579 break;
5580 }
5581 case nir_intrinsic_load_view_index:
5582 case nir_intrinsic_load_layer_id: {
5583 if (instr->intrinsic == nir_intrinsic_load_view_index && (ctx->stage & sw_vs)) {
5584 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5585 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
5586 break;
5587 }
5588
5589 unsigned idx = nir_intrinsic_base(instr);
5590 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5591 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
5592 break;
5593 }
5594 case nir_intrinsic_load_frag_coord: {
5595 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
5596 break;
5597 }
5598 case nir_intrinsic_load_sample_pos: {
5599 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
5600 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
5601 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5602 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
5603 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
5604 break;
5605 }
5606 case nir_intrinsic_load_interpolated_input:
5607 visit_load_interpolated_input(ctx, instr);
5608 break;
5609 case nir_intrinsic_store_output:
5610 visit_store_output(ctx, instr);
5611 break;
5612 case nir_intrinsic_load_input:
5613 visit_load_input(ctx, instr);
5614 break;
5615 case nir_intrinsic_load_ubo:
5616 visit_load_ubo(ctx, instr);
5617 break;
5618 case nir_intrinsic_load_push_constant:
5619 visit_load_push_constant(ctx, instr);
5620 break;
5621 case nir_intrinsic_load_constant:
5622 visit_load_constant(ctx, instr);
5623 break;
5624 case nir_intrinsic_vulkan_resource_index:
5625 visit_load_resource(ctx, instr);
5626 break;
5627 case nir_intrinsic_discard:
5628 visit_discard(ctx, instr);
5629 break;
5630 case nir_intrinsic_discard_if:
5631 visit_discard_if(ctx, instr);
5632 break;
5633 case nir_intrinsic_load_shared:
5634 visit_load_shared(ctx, instr);
5635 break;
5636 case nir_intrinsic_store_shared:
5637 visit_store_shared(ctx, instr);
5638 break;
5639 case nir_intrinsic_shared_atomic_add:
5640 case nir_intrinsic_shared_atomic_imin:
5641 case nir_intrinsic_shared_atomic_umin:
5642 case nir_intrinsic_shared_atomic_imax:
5643 case nir_intrinsic_shared_atomic_umax:
5644 case nir_intrinsic_shared_atomic_and:
5645 case nir_intrinsic_shared_atomic_or:
5646 case nir_intrinsic_shared_atomic_xor:
5647 case nir_intrinsic_shared_atomic_exchange:
5648 case nir_intrinsic_shared_atomic_comp_swap:
5649 visit_shared_atomic(ctx, instr);
5650 break;
5651 case nir_intrinsic_image_deref_load:
5652 visit_image_load(ctx, instr);
5653 break;
5654 case nir_intrinsic_image_deref_store:
5655 visit_image_store(ctx, instr);
5656 break;
5657 case nir_intrinsic_image_deref_atomic_add:
5658 case nir_intrinsic_image_deref_atomic_umin:
5659 case nir_intrinsic_image_deref_atomic_imin:
5660 case nir_intrinsic_image_deref_atomic_umax:
5661 case nir_intrinsic_image_deref_atomic_imax:
5662 case nir_intrinsic_image_deref_atomic_and:
5663 case nir_intrinsic_image_deref_atomic_or:
5664 case nir_intrinsic_image_deref_atomic_xor:
5665 case nir_intrinsic_image_deref_atomic_exchange:
5666 case nir_intrinsic_image_deref_atomic_comp_swap:
5667 visit_image_atomic(ctx, instr);
5668 break;
5669 case nir_intrinsic_image_deref_size:
5670 visit_image_size(ctx, instr);
5671 break;
5672 case nir_intrinsic_load_ssbo:
5673 visit_load_ssbo(ctx, instr);
5674 break;
5675 case nir_intrinsic_store_ssbo:
5676 visit_store_ssbo(ctx, instr);
5677 break;
5678 case nir_intrinsic_load_global:
5679 visit_load_global(ctx, instr);
5680 break;
5681 case nir_intrinsic_store_global:
5682 visit_store_global(ctx, instr);
5683 break;
5684 case nir_intrinsic_global_atomic_add:
5685 case nir_intrinsic_global_atomic_imin:
5686 case nir_intrinsic_global_atomic_umin:
5687 case nir_intrinsic_global_atomic_imax:
5688 case nir_intrinsic_global_atomic_umax:
5689 case nir_intrinsic_global_atomic_and:
5690 case nir_intrinsic_global_atomic_or:
5691 case nir_intrinsic_global_atomic_xor:
5692 case nir_intrinsic_global_atomic_exchange:
5693 case nir_intrinsic_global_atomic_comp_swap:
5694 visit_global_atomic(ctx, instr);
5695 break;
5696 case nir_intrinsic_ssbo_atomic_add:
5697 case nir_intrinsic_ssbo_atomic_imin:
5698 case nir_intrinsic_ssbo_atomic_umin:
5699 case nir_intrinsic_ssbo_atomic_imax:
5700 case nir_intrinsic_ssbo_atomic_umax:
5701 case nir_intrinsic_ssbo_atomic_and:
5702 case nir_intrinsic_ssbo_atomic_or:
5703 case nir_intrinsic_ssbo_atomic_xor:
5704 case nir_intrinsic_ssbo_atomic_exchange:
5705 case nir_intrinsic_ssbo_atomic_comp_swap:
5706 visit_atomic_ssbo(ctx, instr);
5707 break;
5708 case nir_intrinsic_load_scratch:
5709 visit_load_scratch(ctx, instr);
5710 break;
5711 case nir_intrinsic_store_scratch:
5712 visit_store_scratch(ctx, instr);
5713 break;
5714 case nir_intrinsic_get_buffer_size:
5715 visit_get_buffer_size(ctx, instr);
5716 break;
5717 case nir_intrinsic_barrier: {
5718 unsigned* bsize = ctx->program->info->cs.block_size;
5719 unsigned workgroup_size = bsize[0] * bsize[1] * bsize[2];
5720 if (workgroup_size > ctx->program->wave_size)
5721 bld.sopp(aco_opcode::s_barrier);
5722 break;
5723 }
5724 case nir_intrinsic_group_memory_barrier:
5725 case nir_intrinsic_memory_barrier:
5726 case nir_intrinsic_memory_barrier_atomic_counter:
5727 case nir_intrinsic_memory_barrier_buffer:
5728 case nir_intrinsic_memory_barrier_image:
5729 case nir_intrinsic_memory_barrier_shared:
5730 emit_memory_barrier(ctx, instr);
5731 break;
5732 case nir_intrinsic_load_num_work_groups: {
5733 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5734 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
5735 emit_split_vector(ctx, dst, 3);
5736 break;
5737 }
5738 case nir_intrinsic_load_local_invocation_id: {
5739 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5740 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
5741 emit_split_vector(ctx, dst, 3);
5742 break;
5743 }
5744 case nir_intrinsic_load_work_group_id: {
5745 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5746 struct ac_arg *args = ctx->args->ac.workgroup_ids;
5747 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5748 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
5749 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
5750 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
5751 emit_split_vector(ctx, dst, 3);
5752 break;
5753 }
5754 case nir_intrinsic_load_local_invocation_index: {
5755 Temp id = emit_mbcnt(ctx, bld.def(v1));
5756
5757 /* The tg_size bits [6:11] contain the subgroup id,
5758 * we need this multiplied by the wave size, and then OR the thread id to it.
5759 */
5760 if (ctx->program->wave_size == 64) {
5761 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
5762 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
5763 get_arg(ctx, ctx->args->ac.tg_size));
5764 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
5765 } else {
5766 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
5767 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
5768 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
5769 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
5770 }
5771 break;
5772 }
5773 case nir_intrinsic_load_subgroup_id: {
5774 if (ctx->stage == compute_cs) {
5775 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
5776 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
5777 } else {
5778 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
5779 }
5780 break;
5781 }
5782 case nir_intrinsic_load_subgroup_invocation: {
5783 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
5784 break;
5785 }
5786 case nir_intrinsic_load_num_subgroups: {
5787 if (ctx->stage == compute_cs)
5788 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
5789 get_arg(ctx, ctx->args->ac.tg_size));
5790 else
5791 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
5792 break;
5793 }
5794 case nir_intrinsic_ballot: {
5795 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5796 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5797 Definition tmp = bld.def(dst.regClass());
5798 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
5799 if (instr->src[0].ssa->bit_size == 1) {
5800 assert(src.regClass() == bld.lm);
5801 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
5802 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
5803 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
5804 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
5805 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
5806 } else {
5807 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5808 nir_print_instr(&instr->instr, stderr);
5809 fprintf(stderr, "\n");
5810 }
5811 if (dst.size() != bld.lm.size()) {
5812 /* Wave32 with ballot size set to 64 */
5813 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
5814 }
5815 emit_wqm(ctx, tmp.getTemp(), dst);
5816 break;
5817 }
5818 case nir_intrinsic_shuffle:
5819 case nir_intrinsic_read_invocation: {
5820 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5821 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
5822 emit_uniform_subgroup(ctx, instr, src);
5823 } else {
5824 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
5825 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
5826 tid = bld.as_uniform(tid);
5827 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5828 if (src.regClass() == v1) {
5829 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
5830 } else if (src.regClass() == v2) {
5831 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
5832 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
5833 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
5834 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
5835 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
5836 emit_split_vector(ctx, dst, 2);
5837 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
5838 assert(src.regClass() == bld.lm);
5839 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
5840 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
5841 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
5842 assert(src.regClass() == bld.lm);
5843 Temp tmp;
5844 if (ctx->program->chip_class <= GFX7)
5845 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
5846 else if (ctx->program->wave_size == 64)
5847 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
5848 else
5849 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
5850 tmp = emit_extract_vector(ctx, tmp, 0, v1);
5851 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
5852 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
5853 } else {
5854 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5855 nir_print_instr(&instr->instr, stderr);
5856 fprintf(stderr, "\n");
5857 }
5858 }
5859 break;
5860 }
5861 case nir_intrinsic_load_sample_id: {
5862 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5863 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
5864 break;
5865 }
5866 case nir_intrinsic_load_sample_mask_in: {
5867 visit_load_sample_mask_in(ctx, instr);
5868 break;
5869 }
5870 case nir_intrinsic_read_first_invocation: {
5871 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5872 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5873 if (src.regClass() == v1) {
5874 emit_wqm(ctx,
5875 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
5876 dst);
5877 } else if (src.regClass() == v2) {
5878 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
5879 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
5880 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
5881 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
5882 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
5883 emit_split_vector(ctx, dst, 2);
5884 } else if (instr->dest.ssa.bit_size == 1) {
5885 assert(src.regClass() == bld.lm);
5886 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
5887 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
5888 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
5889 } else if (src.regClass() == s1) {
5890 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
5891 } else if (src.regClass() == s2) {
5892 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
5893 } else {
5894 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5895 nir_print_instr(&instr->instr, stderr);
5896 fprintf(stderr, "\n");
5897 }
5898 break;
5899 }
5900 case nir_intrinsic_vote_all: {
5901 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5902 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5903 assert(src.regClass() == bld.lm);
5904 assert(dst.regClass() == bld.lm);
5905
5906 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
5907 Temp val = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(0u), Operand(-1u), bld.scc(tmp));
5908 emit_wqm(ctx, val, dst);
5909 break;
5910 }
5911 case nir_intrinsic_vote_any: {
5912 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5913 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5914 assert(src.regClass() == bld.lm);
5915 assert(dst.regClass() == bld.lm);
5916
5917 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
5918 Temp val = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), Operand(0u), bld.scc(tmp));
5919 emit_wqm(ctx, val, dst);
5920 break;
5921 }
5922 case nir_intrinsic_reduce:
5923 case nir_intrinsic_inclusive_scan:
5924 case nir_intrinsic_exclusive_scan: {
5925 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5926 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5927 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
5928 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
5929 nir_intrinsic_cluster_size(instr) : 0;
5930 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
5931
5932 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
5933 emit_uniform_subgroup(ctx, instr, src);
5934 } else if (instr->dest.ssa.bit_size == 1) {
5935 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
5936 op = nir_op_iand;
5937 else if (op == nir_op_iadd)
5938 op = nir_op_ixor;
5939 else if (op == nir_op_umax || op == nir_op_imax)
5940 op = nir_op_ior;
5941 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
5942
5943 switch (instr->intrinsic) {
5944 case nir_intrinsic_reduce:
5945 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
5946 break;
5947 case nir_intrinsic_exclusive_scan:
5948 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
5949 break;
5950 case nir_intrinsic_inclusive_scan:
5951 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
5952 break;
5953 default:
5954 assert(false);
5955 }
5956 } else if (cluster_size == 1) {
5957 bld.copy(Definition(dst), src);
5958 } else {
5959 src = as_vgpr(ctx, src);
5960
5961 ReduceOp reduce_op;
5962 switch (op) {
5963 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5964 CASE(iadd)
5965 CASE(imul)
5966 CASE(fadd)
5967 CASE(fmul)
5968 CASE(imin)
5969 CASE(umin)
5970 CASE(fmin)
5971 CASE(imax)
5972 CASE(umax)
5973 CASE(fmax)
5974 CASE(iand)
5975 CASE(ior)
5976 CASE(ixor)
5977 default:
5978 unreachable("unknown reduction op");
5979 #undef CASE
5980 }
5981
5982 aco_opcode aco_op;
5983 switch (instr->intrinsic) {
5984 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
5985 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
5986 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
5987 default:
5988 unreachable("unknown reduce intrinsic");
5989 }
5990
5991 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
5992 reduce->operands[0] = Operand(src);
5993 // filled in by aco_reduce_assign.cpp, used internally as part of the
5994 // reduce sequence
5995 assert(dst.size() == 1 || dst.size() == 2);
5996 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
5997 reduce->operands[2] = Operand(v1.as_linear());
5998
5999 Temp tmp_dst = bld.tmp(dst.regClass());
6000 reduce->definitions[0] = Definition(tmp_dst);
6001 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
6002 reduce->definitions[2] = Definition();
6003 reduce->definitions[3] = Definition(scc, s1);
6004 reduce->definitions[4] = Definition();
6005 reduce->reduce_op = reduce_op;
6006 reduce->cluster_size = cluster_size;
6007 ctx->block->instructions.emplace_back(std::move(reduce));
6008
6009 emit_wqm(ctx, tmp_dst, dst);
6010 }
6011 break;
6012 }
6013 case nir_intrinsic_quad_broadcast: {
6014 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6015 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6016 emit_uniform_subgroup(ctx, instr, src);
6017 } else {
6018 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6019 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
6020 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
6021
6022 if (instr->dest.ssa.bit_size == 1) {
6023 assert(src.regClass() == bld.lm);
6024 assert(dst.regClass() == bld.lm);
6025 uint32_t half_mask = 0x11111111u << lane;
6026 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
6027 Temp tmp = bld.tmp(bld.lm);
6028 bld.sop1(Builder::s_wqm, Definition(tmp),
6029 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
6030 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
6031 emit_wqm(ctx, tmp, dst);
6032 } else if (instr->dest.ssa.bit_size == 32) {
6033 if (ctx->program->chip_class >= GFX8)
6034 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
6035 else
6036 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
6037 } else if (instr->dest.ssa.bit_size == 64) {
6038 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6039 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6040 if (ctx->program->chip_class >= GFX8) {
6041 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
6042 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
6043 } else {
6044 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
6045 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
6046 }
6047 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6048 emit_split_vector(ctx, dst, 2);
6049 } else {
6050 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6051 nir_print_instr(&instr->instr, stderr);
6052 fprintf(stderr, "\n");
6053 }
6054 }
6055 break;
6056 }
6057 case nir_intrinsic_quad_swap_horizontal:
6058 case nir_intrinsic_quad_swap_vertical:
6059 case nir_intrinsic_quad_swap_diagonal:
6060 case nir_intrinsic_quad_swizzle_amd: {
6061 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6062 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6063 emit_uniform_subgroup(ctx, instr, src);
6064 break;
6065 }
6066 uint16_t dpp_ctrl = 0;
6067 switch (instr->intrinsic) {
6068 case nir_intrinsic_quad_swap_horizontal:
6069 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
6070 break;
6071 case nir_intrinsic_quad_swap_vertical:
6072 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
6073 break;
6074 case nir_intrinsic_quad_swap_diagonal:
6075 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
6076 break;
6077 case nir_intrinsic_quad_swizzle_amd:
6078 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
6079 break;
6080 default:
6081 break;
6082 }
6083 if (ctx->program->chip_class < GFX8)
6084 dpp_ctrl |= (1 << 15);
6085
6086 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6087 if (instr->dest.ssa.bit_size == 1) {
6088 assert(src.regClass() == bld.lm);
6089 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
6090 if (ctx->program->chip_class >= GFX8)
6091 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
6092 else
6093 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
6094 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
6095 emit_wqm(ctx, tmp, dst);
6096 } else if (instr->dest.ssa.bit_size == 32) {
6097 Temp tmp;
6098 if (ctx->program->chip_class >= GFX8)
6099 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
6100 else
6101 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
6102 emit_wqm(ctx, tmp, dst);
6103 } else if (instr->dest.ssa.bit_size == 64) {
6104 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6105 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6106 if (ctx->program->chip_class >= GFX8) {
6107 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
6108 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
6109 } else {
6110 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
6111 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
6112 }
6113 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6114 emit_split_vector(ctx, dst, 2);
6115 } else {
6116 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6117 nir_print_instr(&instr->instr, stderr);
6118 fprintf(stderr, "\n");
6119 }
6120 break;
6121 }
6122 case nir_intrinsic_masked_swizzle_amd: {
6123 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6124 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6125 emit_uniform_subgroup(ctx, instr, src);
6126 break;
6127 }
6128 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6129 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
6130 if (dst.regClass() == v1) {
6131 emit_wqm(ctx,
6132 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
6133 dst);
6134 } else if (dst.regClass() == v2) {
6135 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6136 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6137 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
6138 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
6139 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6140 emit_split_vector(ctx, dst, 2);
6141 } else {
6142 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6143 nir_print_instr(&instr->instr, stderr);
6144 fprintf(stderr, "\n");
6145 }
6146 break;
6147 }
6148 case nir_intrinsic_write_invocation_amd: {
6149 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6150 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
6151 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
6152 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6153 if (dst.regClass() == v1) {
6154 /* src2 is ignored for writelane. RA assigns the same reg for dst */
6155 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
6156 } else if (dst.regClass() == v2) {
6157 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
6158 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
6159 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
6160 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
6161 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
6162 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
6163 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6164 emit_split_vector(ctx, dst, 2);
6165 } else {
6166 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6167 nir_print_instr(&instr->instr, stderr);
6168 fprintf(stderr, "\n");
6169 }
6170 break;
6171 }
6172 case nir_intrinsic_mbcnt_amd: {
6173 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6174 RegClass rc = RegClass(src.type(), 1);
6175 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
6176 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
6177 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6178 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
6179 emit_wqm(ctx, wqm_tmp, dst);
6180 break;
6181 }
6182 case nir_intrinsic_load_helper_invocation: {
6183 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6184 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
6185 ctx->block->kind |= block_kind_needs_lowering;
6186 ctx->program->needs_exact = true;
6187 break;
6188 }
6189 case nir_intrinsic_is_helper_invocation: {
6190 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6191 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
6192 ctx->block->kind |= block_kind_needs_lowering;
6193 ctx->program->needs_exact = true;
6194 break;
6195 }
6196 case nir_intrinsic_demote:
6197 bld.pseudo(aco_opcode::p_demote_to_helper);
6198 ctx->block->kind |= block_kind_uses_demote;
6199 ctx->program->needs_exact = true;
6200 break;
6201 case nir_intrinsic_demote_if: {
6202 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6203 assert(src.regClass() == bld.lm);
6204 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6205 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
6206 ctx->block->kind |= block_kind_uses_demote;
6207 ctx->program->needs_exact = true;
6208 break;
6209 }
6210 case nir_intrinsic_first_invocation: {
6211 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
6212 get_ssa_temp(ctx, &instr->dest.ssa));
6213 break;
6214 }
6215 case nir_intrinsic_shader_clock:
6216 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
6217 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
6218 break;
6219 case nir_intrinsic_load_vertex_id_zero_base: {
6220 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6221 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
6222 break;
6223 }
6224 case nir_intrinsic_load_first_vertex: {
6225 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6226 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
6227 break;
6228 }
6229 case nir_intrinsic_load_base_instance: {
6230 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6231 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
6232 break;
6233 }
6234 case nir_intrinsic_load_instance_id: {
6235 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6236 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
6237 break;
6238 }
6239 case nir_intrinsic_load_draw_id: {
6240 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6241 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
6242 break;
6243 }
6244 default:
6245 fprintf(stderr, "Unimplemented intrinsic instr: ");
6246 nir_print_instr(&instr->instr, stderr);
6247 fprintf(stderr, "\n");
6248 abort();
6249
6250 break;
6251 }
6252 }
6253
6254
6255 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
6256 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
6257 enum glsl_base_type *stype)
6258 {
6259 nir_deref_instr *texture_deref_instr = NULL;
6260 nir_deref_instr *sampler_deref_instr = NULL;
6261 int plane = -1;
6262
6263 for (unsigned i = 0; i < instr->num_srcs; i++) {
6264 switch (instr->src[i].src_type) {
6265 case nir_tex_src_texture_deref:
6266 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
6267 break;
6268 case nir_tex_src_sampler_deref:
6269 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
6270 break;
6271 case nir_tex_src_plane:
6272 plane = nir_src_as_int(instr->src[i].src);
6273 break;
6274 default:
6275 break;
6276 }
6277 }
6278
6279 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
6280
6281 if (!sampler_deref_instr)
6282 sampler_deref_instr = texture_deref_instr;
6283
6284 if (plane >= 0) {
6285 assert(instr->op != nir_texop_txf_ms &&
6286 instr->op != nir_texop_samples_identical);
6287 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
6288 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
6289 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
6290 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
6291 } else {
6292 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
6293 }
6294 if (samp_ptr) {
6295 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
6296
6297 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
6298 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
6299 Builder bld(ctx->program, ctx->block);
6300
6301 /* to avoid unnecessary moves, we split and recombine sampler and image */
6302 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
6303 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
6304 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
6305 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
6306 Definition(img[2]), Definition(img[3]), Definition(img[4]),
6307 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
6308 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
6309 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
6310
6311 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
6312 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
6313 img[0], img[1], img[2], img[3],
6314 img[4], img[5], img[6], img[7]);
6315 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6316 samp[0], samp[1], samp[2], samp[3]);
6317 }
6318 }
6319 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
6320 instr->op == nir_texop_samples_identical))
6321 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
6322 }
6323
6324 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
6325 Temp *out_ma, Temp *out_sc, Temp *out_tc)
6326 {
6327 Builder bld(ctx->program, ctx->block);
6328
6329 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
6330 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
6331 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
6332
6333 Operand neg_one(0xbf800000u);
6334 Operand one(0x3f800000u);
6335 Operand two(0x40000000u);
6336 Operand four(0x40800000u);
6337
6338 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
6339 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
6340 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
6341
6342 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
6343 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(s2), two, id);
6344 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
6345 Temp is_not_ma_x = bld.sop2(aco_opcode::s_or_b64, bld.hint_vcc(bld.def(bld.lm)), bld.def(s1, scc), is_ma_z, is_ma_y);
6346
6347 // select sc
6348 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
6349 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
6350 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
6351 one, is_ma_y);
6352 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
6353
6354 // select tc
6355 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
6356 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
6357 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
6358
6359 // select ma
6360 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
6361 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
6362 deriv_z, is_ma_z);
6363 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
6364 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
6365 }
6366
6367 void prepare_cube_coords(isel_context *ctx, Temp* coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
6368 {
6369 Builder bld(ctx->program, ctx->block);
6370 Temp coord_args[4], ma, tc, sc, id;
6371 for (unsigned i = 0; i < (is_array ? 4 : 3); i++)
6372 coord_args[i] = emit_extract_vector(ctx, *coords, i, v1);
6373
6374 if (is_array) {
6375 coord_args[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coord_args[3]);
6376
6377 // see comment in ac_prepare_cube_coords()
6378 if (ctx->options->chip_class <= GFX8)
6379 coord_args[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coord_args[3]);
6380 }
6381
6382 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6383
6384 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
6385 vop3a->operands[0] = Operand(ma);
6386 vop3a->abs[0] = true;
6387 Temp invma = bld.tmp(v1);
6388 vop3a->definitions[0] = Definition(invma);
6389 ctx->block->instructions.emplace_back(std::move(vop3a));
6390
6391 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6392 if (!is_deriv)
6393 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
6394
6395 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6396 if (!is_deriv)
6397 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
6398
6399 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6400
6401 if (is_deriv) {
6402 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
6403 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
6404
6405 for (unsigned i = 0; i < 2; i++) {
6406 // see comment in ac_prepare_cube_coords()
6407 Temp deriv_ma;
6408 Temp deriv_sc, deriv_tc;
6409 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
6410 &deriv_ma, &deriv_sc, &deriv_tc);
6411
6412 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
6413
6414 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
6415 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
6416 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
6417 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
6418 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
6419 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
6420 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
6421 }
6422
6423 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
6424 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
6425 }
6426
6427 if (is_array)
6428 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coord_args[3], id, Operand(0x41000000u/*8.0*/));
6429 *coords = bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), sc, tc, id);
6430
6431 }
6432
6433 Temp apply_round_slice(isel_context *ctx, Temp coords, unsigned idx)
6434 {
6435 Temp coord_vec[3];
6436 for (unsigned i = 0; i < coords.size(); i++)
6437 coord_vec[i] = emit_extract_vector(ctx, coords, i, v1);
6438
6439 Builder bld(ctx->program, ctx->block);
6440 coord_vec[idx] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coord_vec[idx]);
6441
6442 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
6443 for (unsigned i = 0; i < coords.size(); i++)
6444 vec->operands[i] = Operand(coord_vec[i]);
6445 Temp res = bld.tmp(RegType::vgpr, coords.size());
6446 vec->definitions[0] = Definition(res);
6447 ctx->block->instructions.emplace_back(std::move(vec));
6448 return res;
6449 }
6450
6451 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
6452 {
6453 if (vec->parent_instr->type != nir_instr_type_alu)
6454 return;
6455 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
6456 if (vec_instr->op != nir_op_vec(vec->num_components))
6457 return;
6458
6459 for (unsigned i = 0; i < vec->num_components; i++) {
6460 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
6461 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
6462 }
6463 }
6464
6465 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
6466 {
6467 Builder bld(ctx->program, ctx->block);
6468 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
6469 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
6470 Temp resource, sampler, fmask_ptr, bias = Temp(), coords, compare = Temp(), sample_index = Temp(),
6471 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(), derivs = Temp();
6472 nir_const_value *sample_index_cv = NULL;
6473 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
6474 enum glsl_base_type stype;
6475 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
6476
6477 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
6478 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
6479 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
6480 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
6481
6482 for (unsigned i = 0; i < instr->num_srcs; i++) {
6483 switch (instr->src[i].src_type) {
6484 case nir_tex_src_coord:
6485 coords = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[i].src.ssa));
6486 break;
6487 case nir_tex_src_bias:
6488 if (instr->op == nir_texop_txb) {
6489 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
6490 has_bias = true;
6491 }
6492 break;
6493 case nir_tex_src_lod: {
6494 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
6495
6496 if (val && val->f32 <= 0.0) {
6497 level_zero = true;
6498 } else {
6499 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
6500 has_lod = true;
6501 }
6502 break;
6503 }
6504 case nir_tex_src_comparator:
6505 if (instr->is_shadow) {
6506 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
6507 has_compare = true;
6508 }
6509 break;
6510 case nir_tex_src_offset:
6511 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
6512 get_const_vec(instr->src[i].src.ssa, const_offset);
6513 has_offset = true;
6514 break;
6515 case nir_tex_src_ddx:
6516 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
6517 has_ddx = true;
6518 break;
6519 case nir_tex_src_ddy:
6520 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
6521 has_ddy = true;
6522 break;
6523 case nir_tex_src_ms_index:
6524 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
6525 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
6526 has_sample_index = true;
6527 break;
6528 case nir_tex_src_texture_offset:
6529 case nir_tex_src_sampler_offset:
6530 default:
6531 break;
6532 }
6533 }
6534 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6535 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
6536 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
6537
6538 if (instr->op == nir_texop_texture_samples) {
6539 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
6540
6541 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
6542 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
6543 Temp type = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(28u | 4u<<16 /* offset=28, width=4 */));
6544 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
6545
6546 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6547 samples, Operand(1u), bld.scc(is_msaa));
6548 return;
6549 }
6550
6551 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
6552 aco_ptr<Instruction> tmp_instr;
6553 Temp acc, pack = Temp();
6554
6555 uint32_t pack_const = 0;
6556 for (unsigned i = 0; i < offset.size(); i++) {
6557 if (!const_offset[i])
6558 continue;
6559 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
6560 }
6561
6562 if (offset.type() == RegType::sgpr) {
6563 for (unsigned i = 0; i < offset.size(); i++) {
6564 if (const_offset[i])
6565 continue;
6566
6567 acc = emit_extract_vector(ctx, offset, i, s1);
6568 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
6569
6570 if (i) {
6571 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
6572 }
6573
6574 if (pack == Temp()) {
6575 pack = acc;
6576 } else {
6577 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
6578 }
6579 }
6580
6581 if (pack_const && pack != Temp())
6582 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
6583 } else {
6584 for (unsigned i = 0; i < offset.size(); i++) {
6585 if (const_offset[i])
6586 continue;
6587
6588 acc = emit_extract_vector(ctx, offset, i, v1);
6589 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
6590
6591 if (i) {
6592 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
6593 }
6594
6595 if (pack == Temp()) {
6596 pack = acc;
6597 } else {
6598 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
6599 }
6600 }
6601
6602 if (pack_const && pack != Temp())
6603 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
6604 }
6605 if (pack_const && pack == Temp())
6606 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
6607 else if (pack == Temp())
6608 has_offset = false;
6609 else
6610 offset = pack;
6611 }
6612
6613 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
6614 prepare_cube_coords(ctx, &coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
6615
6616 /* pack derivatives */
6617 if (has_ddx || has_ddy) {
6618 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
6619 derivs = bld.pseudo(aco_opcode::p_create_vector, bld.def(v4),
6620 ddx, Operand(0u), ddy, Operand(0u));
6621 } else {
6622 derivs = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, ddx.size() + ddy.size()), ddx, ddy);
6623 }
6624 has_derivs = true;
6625 }
6626
6627 if (instr->coord_components > 1 &&
6628 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
6629 instr->is_array &&
6630 instr->op != nir_texop_txf)
6631 coords = apply_round_slice(ctx, coords, 1);
6632
6633 if (instr->coord_components > 2 &&
6634 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
6635 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
6636 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
6637 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
6638 instr->is_array &&
6639 instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms)
6640 coords = apply_round_slice(ctx, coords, 2);
6641
6642 if (ctx->options->chip_class == GFX9 &&
6643 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
6644 instr->op != nir_texop_lod && instr->coord_components) {
6645 assert(coords.size() > 0 && coords.size() < 3);
6646
6647 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size() + 1, 1)};
6648 vec->operands[0] = Operand(emit_extract_vector(ctx, coords, 0, v1));
6649 vec->operands[1] = instr->op == nir_texop_txf ? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6650 if (coords.size() > 1)
6651 vec->operands[2] = Operand(emit_extract_vector(ctx, coords, 1, v1));
6652 coords = bld.tmp(RegType::vgpr, coords.size() + 1);
6653 vec->definitions[0] = Definition(coords);
6654 ctx->block->instructions.emplace_back(std::move(vec));
6655 }
6656
6657 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
6658
6659 if (instr->op == nir_texop_samples_identical)
6660 resource = fmask_ptr;
6661
6662 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
6663 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
6664 instr->op != nir_texop_txs) {
6665 assert(has_sample_index);
6666 Operand op(sample_index);
6667 if (sample_index_cv)
6668 op = Operand(sample_index_cv->u32);
6669 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
6670 }
6671
6672 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
6673 Temp split_coords[coords.size()];
6674 emit_split_vector(ctx, coords, coords.size());
6675 for (unsigned i = 0; i < coords.size(); i++)
6676 split_coords[i] = emit_extract_vector(ctx, coords, i, v1);
6677
6678 unsigned i = 0;
6679 for (; i < std::min(offset.size(), instr->coord_components); i++) {
6680 Temp off = emit_extract_vector(ctx, offset, i, v1);
6681 split_coords[i] = bld.vadd32(bld.def(v1), split_coords[i], off);
6682 }
6683
6684 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
6685 for (unsigned i = 0; i < coords.size(); i++)
6686 vec->operands[i] = Operand(split_coords[i]);
6687 coords = bld.tmp(coords.regClass());
6688 vec->definitions[0] = Definition(coords);
6689 ctx->block->instructions.emplace_back(std::move(vec));
6690
6691 has_offset = false;
6692 }
6693
6694 /* Build tex instruction */
6695 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
6696 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
6697 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
6698 : 0;
6699 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6700 Temp tmp_dst = dst;
6701
6702 /* gather4 selects the component by dmask and always returns vec4 */
6703 if (instr->op == nir_texop_tg4) {
6704 assert(instr->dest.ssa.num_components == 4);
6705 if (instr->is_shadow)
6706 dmask = 1;
6707 else
6708 dmask = 1 << instr->component;
6709 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
6710 tmp_dst = bld.tmp(v4);
6711 } else if (instr->op == nir_texop_samples_identical) {
6712 tmp_dst = bld.tmp(v1);
6713 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
6714 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
6715 }
6716
6717 aco_ptr<MIMG_instruction> tex;
6718 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
6719 if (!has_lod)
6720 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6721
6722 bool div_by_6 = instr->op == nir_texop_txs &&
6723 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
6724 instr->is_array &&
6725 (dmask & (1 << 2));
6726 if (tmp_dst.id() == dst.id() && div_by_6)
6727 tmp_dst = bld.tmp(tmp_dst.regClass());
6728
6729 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 2, 1));
6730 tex->operands[0] = Operand(as_vgpr(ctx,lod));
6731 tex->operands[1] = Operand(resource);
6732 if (ctx->options->chip_class == GFX9 &&
6733 instr->op == nir_texop_txs &&
6734 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
6735 instr->is_array) {
6736 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
6737 } else if (instr->op == nir_texop_query_levels) {
6738 tex->dmask = 1 << 3;
6739 } else {
6740 tex->dmask = dmask;
6741 }
6742 tex->da = da;
6743 tex->definitions[0] = Definition(tmp_dst);
6744 tex->dim = dim;
6745 tex->can_reorder = true;
6746 ctx->block->instructions.emplace_back(std::move(tex));
6747
6748 if (div_by_6) {
6749 /* divide 3rd value by 6 by multiplying with magic number */
6750 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
6751 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6752 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
6753 assert(instr->dest.ssa.num_components == 3);
6754 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
6755 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
6756 emit_extract_vector(ctx, tmp_dst, 0, v1),
6757 emit_extract_vector(ctx, tmp_dst, 1, v1),
6758 by_6);
6759
6760 }
6761
6762 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
6763 return;
6764 }
6765
6766 Temp tg4_compare_cube_wa64 = Temp();
6767
6768 if (tg4_integer_workarounds) {
6769 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 2, 1));
6770 tex->operands[0] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6771 tex->operands[1] = Operand(resource);
6772 tex->dim = dim;
6773 tex->dmask = 0x3;
6774 tex->da = da;
6775 Temp size = bld.tmp(v2);
6776 tex->definitions[0] = Definition(size);
6777 tex->can_reorder = true;
6778 ctx->block->instructions.emplace_back(std::move(tex));
6779 emit_split_vector(ctx, size, size.size());
6780
6781 Temp half_texel[2];
6782 for (unsigned i = 0; i < 2; i++) {
6783 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
6784 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
6785 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
6786 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
6787 }
6788
6789 Temp orig_coords[2] = {
6790 emit_extract_vector(ctx, coords, 0, v1),
6791 emit_extract_vector(ctx, coords, 1, v1)};
6792 Temp new_coords[2] = {
6793 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), orig_coords[0], half_texel[0]),
6794 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), orig_coords[1], half_texel[1])
6795 };
6796
6797 if (tg4_integer_cube_workaround) {
6798 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6799 Temp desc[resource.size()];
6800 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
6801 Format::PSEUDO, 1, resource.size())};
6802 split->operands[0] = Operand(resource);
6803 for (unsigned i = 0; i < resource.size(); i++) {
6804 desc[i] = bld.tmp(s1);
6805 split->definitions[i] = Definition(desc[i]);
6806 }
6807 ctx->block->instructions.emplace_back(std::move(split));
6808
6809 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
6810 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
6811 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
6812
6813 Temp nfmt;
6814 if (stype == GLSL_TYPE_UINT) {
6815 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
6816 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
6817 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
6818 bld.scc(compare_cube_wa));
6819 } else {
6820 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
6821 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
6822 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
6823 bld.scc(compare_cube_wa));
6824 }
6825 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
6826 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
6827
6828 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
6829
6830 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
6831 Operand((uint32_t)C_008F14_NUM_FORMAT));
6832 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
6833
6834 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
6835 Format::PSEUDO, resource.size(), 1)};
6836 for (unsigned i = 0; i < resource.size(); i++)
6837 vec->operands[i] = Operand(desc[i]);
6838 resource = bld.tmp(resource.regClass());
6839 vec->definitions[0] = Definition(resource);
6840 ctx->block->instructions.emplace_back(std::move(vec));
6841
6842 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
6843 new_coords[0], orig_coords[0], tg4_compare_cube_wa64);
6844 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
6845 new_coords[1], orig_coords[1], tg4_compare_cube_wa64);
6846 }
6847
6848 if (coords.size() == 3) {
6849 coords = bld.pseudo(aco_opcode::p_create_vector, bld.def(v3),
6850 new_coords[0], new_coords[1],
6851 emit_extract_vector(ctx, coords, 2, v1));
6852 } else {
6853 assert(coords.size() == 2);
6854 coords = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2),
6855 new_coords[0], new_coords[1]);
6856 }
6857 }
6858
6859 std::vector<Operand> args;
6860 if (has_offset)
6861 args.emplace_back(Operand(offset));
6862 if (has_bias)
6863 args.emplace_back(Operand(bias));
6864 if (has_compare)
6865 args.emplace_back(Operand(compare));
6866 if (has_derivs)
6867 args.emplace_back(Operand(derivs));
6868 args.emplace_back(Operand(coords));
6869 if (has_sample_index)
6870 args.emplace_back(Operand(sample_index));
6871 if (has_lod)
6872 args.emplace_back(lod);
6873
6874 Temp arg;
6875 if (args.size() > 1) {
6876 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
6877 unsigned size = 0;
6878 for (unsigned i = 0; i < args.size(); i++) {
6879 size += args[i].size();
6880 vec->operands[i] = args[i];
6881 }
6882 RegClass rc = RegClass(RegType::vgpr, size);
6883 Temp tmp = bld.tmp(rc);
6884 vec->definitions[0] = Definition(tmp);
6885 ctx->block->instructions.emplace_back(std::move(vec));
6886 arg = tmp;
6887 } else {
6888 assert(args[0].isTemp());
6889 arg = as_vgpr(ctx, args[0].getTemp());
6890 }
6891
6892 /* we don't need the bias, sample index, compare value or offset to be
6893 * computed in WQM but if the p_create_vector copies the coordinates, then it
6894 * needs to be in WQM */
6895 if (!(has_ddx && has_ddy) && !has_lod && !level_zero &&
6896 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
6897 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
6898 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
6899
6900 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
6901 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6902
6903 assert(coords.size() == 1);
6904 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
6905 aco_opcode op;
6906 switch (last_bit) {
6907 case 1:
6908 op = aco_opcode::buffer_load_format_x; break;
6909 case 2:
6910 op = aco_opcode::buffer_load_format_xy; break;
6911 case 3:
6912 op = aco_opcode::buffer_load_format_xyz; break;
6913 case 4:
6914 op = aco_opcode::buffer_load_format_xyzw; break;
6915 default:
6916 unreachable("Tex instruction loads more than 4 components.");
6917 }
6918
6919 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6920 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
6921 tmp_dst = dst;
6922 else
6923 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
6924
6925 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
6926 mubuf->operands[0] = Operand(coords);
6927 mubuf->operands[1] = Operand(resource);
6928 mubuf->operands[2] = Operand((uint32_t) 0);
6929 mubuf->definitions[0] = Definition(tmp_dst);
6930 mubuf->idxen = true;
6931 mubuf->can_reorder = true;
6932 ctx->block->instructions.emplace_back(std::move(mubuf));
6933
6934 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
6935 return;
6936 }
6937
6938
6939 if (instr->op == nir_texop_txf ||
6940 instr->op == nir_texop_txf_ms ||
6941 instr->op == nir_texop_samples_identical) {
6942 aco_opcode op = level_zero || instr->sampler_dim == GLSL_SAMPLER_DIM_MS ? aco_opcode::image_load : aco_opcode::image_load_mip;
6943 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 2, 1));
6944 tex->operands[0] = Operand(arg);
6945 tex->operands[1] = Operand(resource);
6946 tex->dim = dim;
6947 tex->dmask = dmask;
6948 tex->unrm = true;
6949 tex->da = da;
6950 tex->definitions[0] = Definition(tmp_dst);
6951 tex->can_reorder = true;
6952 ctx->block->instructions.emplace_back(std::move(tex));
6953
6954 if (instr->op == nir_texop_samples_identical) {
6955 assert(dmask == 1 && dst.regClass() == v1);
6956 assert(dst.id() != tmp_dst.id());
6957
6958 Temp tmp = bld.tmp(bld.lm);
6959 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
6960 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
6961
6962 } else {
6963 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
6964 }
6965 return;
6966 }
6967
6968 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6969 aco_opcode opcode = aco_opcode::image_sample;
6970 if (has_offset) { /* image_sample_*_o */
6971 if (has_compare) {
6972 opcode = aco_opcode::image_sample_c_o;
6973 if (has_derivs)
6974 opcode = aco_opcode::image_sample_c_d_o;
6975 if (has_bias)
6976 opcode = aco_opcode::image_sample_c_b_o;
6977 if (level_zero)
6978 opcode = aco_opcode::image_sample_c_lz_o;
6979 if (has_lod)
6980 opcode = aco_opcode::image_sample_c_l_o;
6981 } else {
6982 opcode = aco_opcode::image_sample_o;
6983 if (has_derivs)
6984 opcode = aco_opcode::image_sample_d_o;
6985 if (has_bias)
6986 opcode = aco_opcode::image_sample_b_o;
6987 if (level_zero)
6988 opcode = aco_opcode::image_sample_lz_o;
6989 if (has_lod)
6990 opcode = aco_opcode::image_sample_l_o;
6991 }
6992 } else { /* no offset */
6993 if (has_compare) {
6994 opcode = aco_opcode::image_sample_c;
6995 if (has_derivs)
6996 opcode = aco_opcode::image_sample_c_d;
6997 if (has_bias)
6998 opcode = aco_opcode::image_sample_c_b;
6999 if (level_zero)
7000 opcode = aco_opcode::image_sample_c_lz;
7001 if (has_lod)
7002 opcode = aco_opcode::image_sample_c_l;
7003 } else {
7004 opcode = aco_opcode::image_sample;
7005 if (has_derivs)
7006 opcode = aco_opcode::image_sample_d;
7007 if (has_bias)
7008 opcode = aco_opcode::image_sample_b;
7009 if (level_zero)
7010 opcode = aco_opcode::image_sample_lz;
7011 if (has_lod)
7012 opcode = aco_opcode::image_sample_l;
7013 }
7014 }
7015
7016 if (instr->op == nir_texop_tg4) {
7017 if (has_offset) {
7018 opcode = aco_opcode::image_gather4_lz_o;
7019 if (has_compare)
7020 opcode = aco_opcode::image_gather4_c_lz_o;
7021 } else {
7022 opcode = aco_opcode::image_gather4_lz;
7023 if (has_compare)
7024 opcode = aco_opcode::image_gather4_c_lz;
7025 }
7026 } else if (instr->op == nir_texop_lod) {
7027 opcode = aco_opcode::image_get_lod;
7028 }
7029
7030 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
7031 tex->operands[0] = Operand(arg);
7032 tex->operands[1] = Operand(resource);
7033 tex->operands[2] = Operand(sampler);
7034 tex->dim = dim;
7035 tex->dmask = dmask;
7036 tex->da = da;
7037 tex->definitions[0] = Definition(tmp_dst);
7038 tex->can_reorder = true;
7039 ctx->block->instructions.emplace_back(std::move(tex));
7040
7041 if (tg4_integer_cube_workaround) {
7042 assert(tmp_dst.id() != dst.id());
7043 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
7044
7045 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
7046 Temp val[4];
7047 for (unsigned i = 0; i < dst.size(); i++) {
7048 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
7049 Temp cvt_val;
7050 if (stype == GLSL_TYPE_UINT)
7051 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
7052 else
7053 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
7054 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
7055 }
7056 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
7057 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
7058 val[0], val[1], val[2], val[3]);
7059 }
7060 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
7061 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
7062
7063 }
7064
7065
7066 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
7067 {
7068 Temp tmp = get_ssa_temp(ctx, ssa);
7069 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
7070 return Operand(tmp.regClass());
7071 else
7072 return Operand(tmp);
7073 }
7074
7075 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
7076 {
7077 aco_ptr<Pseudo_instruction> phi;
7078 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7079 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
7080
7081 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
7082 logical |= ctx->block->kind & block_kind_merge;
7083 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
7084
7085 /* we want a sorted list of sources, since the predecessor list is also sorted */
7086 std::map<unsigned, nir_ssa_def*> phi_src;
7087 nir_foreach_phi_src(src, instr)
7088 phi_src[src->pred->index] = src->src.ssa;
7089
7090 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
7091 unsigned num_operands = 0;
7092 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size())];
7093 unsigned num_defined = 0;
7094 unsigned cur_pred_idx = 0;
7095 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
7096 if (cur_pred_idx < preds.size()) {
7097 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
7098 unsigned block = ctx->cf_info.nir_to_aco[src.first];
7099 unsigned skipped = 0;
7100 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
7101 skipped++;
7102 if (cur_pred_idx + skipped < preds.size()) {
7103 for (unsigned i = 0; i < skipped; i++)
7104 operands[num_operands++] = Operand(dst.regClass());
7105 cur_pred_idx += skipped;
7106 } else {
7107 continue;
7108 }
7109 }
7110 cur_pred_idx++;
7111 Operand op = get_phi_operand(ctx, src.second);
7112 operands[num_operands++] = op;
7113 num_defined += !op.isUndefined();
7114 }
7115 /* handle block_kind_continue_or_break at loop exit blocks */
7116 while (cur_pred_idx++ < preds.size())
7117 operands[num_operands++] = Operand(dst.regClass());
7118
7119 if (num_defined == 0) {
7120 Builder bld(ctx->program, ctx->block);
7121 if (dst.regClass() == s1) {
7122 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
7123 } else if (dst.regClass() == v1) {
7124 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
7125 } else {
7126 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
7127 for (unsigned i = 0; i < dst.size(); i++)
7128 vec->operands[i] = Operand(0u);
7129 vec->definitions[0] = Definition(dst);
7130 ctx->block->instructions.emplace_back(std::move(vec));
7131 }
7132 return;
7133 }
7134
7135 /* we can use a linear phi in some cases if one src is undef */
7136 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
7137 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
7138
7139 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
7140 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
7141 assert(invert->kind & block_kind_invert);
7142
7143 unsigned then_block = invert->linear_preds[0];
7144
7145 Block* insert_block = NULL;
7146 for (unsigned i = 0; i < num_operands; i++) {
7147 Operand op = operands[i];
7148 if (op.isUndefined())
7149 continue;
7150 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
7151 phi->operands[0] = op;
7152 break;
7153 }
7154 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
7155 phi->operands[1] = Operand(dst.regClass());
7156 phi->definitions[0] = Definition(dst);
7157 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
7158 return;
7159 }
7160
7161 /* try to scalarize vector phis */
7162 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
7163 // TODO: scalarize linear phis on divergent ifs
7164 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
7165 std::array<Temp, 4> new_vec;
7166 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
7167 Operand src = operands[i];
7168 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
7169 can_scalarize = false;
7170 }
7171 if (can_scalarize) {
7172 unsigned num_components = instr->dest.ssa.num_components;
7173 assert(dst.size() % num_components == 0);
7174 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
7175
7176 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
7177 for (unsigned k = 0; k < num_components; k++) {
7178 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
7179 for (unsigned i = 0; i < num_operands; i++) {
7180 Operand src = operands[i];
7181 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
7182 }
7183 Temp phi_dst = {ctx->program->allocateId(), rc};
7184 phi->definitions[0] = Definition(phi_dst);
7185 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
7186 new_vec[k] = phi_dst;
7187 vec->operands[k] = Operand(phi_dst);
7188 }
7189 vec->definitions[0] = Definition(dst);
7190 ctx->block->instructions.emplace_back(std::move(vec));
7191 ctx->allocated_vec.emplace(dst.id(), new_vec);
7192 return;
7193 }
7194 }
7195
7196 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
7197 for (unsigned i = 0; i < num_operands; i++)
7198 phi->operands[i] = operands[i];
7199 phi->definitions[0] = Definition(dst);
7200 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
7201 }
7202
7203
7204 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
7205 {
7206 Temp dst = get_ssa_temp(ctx, &instr->def);
7207
7208 assert(dst.type() == RegType::sgpr);
7209
7210 if (dst.size() == 1) {
7211 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
7212 } else {
7213 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
7214 for (unsigned i = 0; i < dst.size(); i++)
7215 vec->operands[i] = Operand(0u);
7216 vec->definitions[0] = Definition(dst);
7217 ctx->block->instructions.emplace_back(std::move(vec));
7218 }
7219 }
7220
7221 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
7222 {
7223 Builder bld(ctx->program, ctx->block);
7224 Block *logical_target;
7225 append_logical_end(ctx->block);
7226 unsigned idx = ctx->block->index;
7227
7228 switch (instr->type) {
7229 case nir_jump_break:
7230 logical_target = ctx->cf_info.parent_loop.exit;
7231 add_logical_edge(idx, logical_target);
7232 ctx->block->kind |= block_kind_break;
7233
7234 if (!ctx->cf_info.parent_if.is_divergent &&
7235 !ctx->cf_info.parent_loop.has_divergent_continue) {
7236 /* uniform break - directly jump out of the loop */
7237 ctx->block->kind |= block_kind_uniform;
7238 ctx->cf_info.has_branch = true;
7239 bld.branch(aco_opcode::p_branch);
7240 add_linear_edge(idx, logical_target);
7241 return;
7242 }
7243 ctx->cf_info.parent_loop.has_divergent_branch = true;
7244 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
7245 break;
7246 case nir_jump_continue:
7247 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
7248 add_logical_edge(idx, logical_target);
7249 ctx->block->kind |= block_kind_continue;
7250
7251 if (ctx->cf_info.parent_if.is_divergent) {
7252 /* for potential uniform breaks after this continue,
7253 we must ensure that they are handled correctly */
7254 ctx->cf_info.parent_loop.has_divergent_continue = true;
7255 ctx->cf_info.parent_loop.has_divergent_branch = true;
7256 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
7257 } else {
7258 /* uniform continue - directly jump to the loop header */
7259 ctx->block->kind |= block_kind_uniform;
7260 ctx->cf_info.has_branch = true;
7261 bld.branch(aco_opcode::p_branch);
7262 add_linear_edge(idx, logical_target);
7263 return;
7264 }
7265 break;
7266 default:
7267 fprintf(stderr, "Unknown NIR jump instr: ");
7268 nir_print_instr(&instr->instr, stderr);
7269 fprintf(stderr, "\n");
7270 abort();
7271 }
7272
7273 /* remove critical edges from linear CFG */
7274 bld.branch(aco_opcode::p_branch);
7275 Block* break_block = ctx->program->create_and_insert_block();
7276 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7277 break_block->kind |= block_kind_uniform;
7278 add_linear_edge(idx, break_block);
7279 /* the loop_header pointer might be invalidated by this point */
7280 if (instr->type == nir_jump_continue)
7281 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
7282 add_linear_edge(break_block->index, logical_target);
7283 bld.reset(break_block);
7284 bld.branch(aco_opcode::p_branch);
7285
7286 Block* continue_block = ctx->program->create_and_insert_block();
7287 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7288 add_linear_edge(idx, continue_block);
7289 append_logical_start(continue_block);
7290 ctx->block = continue_block;
7291 return;
7292 }
7293
7294 void visit_block(isel_context *ctx, nir_block *block)
7295 {
7296 nir_foreach_instr(instr, block) {
7297 switch (instr->type) {
7298 case nir_instr_type_alu:
7299 visit_alu_instr(ctx, nir_instr_as_alu(instr));
7300 break;
7301 case nir_instr_type_load_const:
7302 visit_load_const(ctx, nir_instr_as_load_const(instr));
7303 break;
7304 case nir_instr_type_intrinsic:
7305 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
7306 break;
7307 case nir_instr_type_tex:
7308 visit_tex(ctx, nir_instr_as_tex(instr));
7309 break;
7310 case nir_instr_type_phi:
7311 visit_phi(ctx, nir_instr_as_phi(instr));
7312 break;
7313 case nir_instr_type_ssa_undef:
7314 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
7315 break;
7316 case nir_instr_type_deref:
7317 break;
7318 case nir_instr_type_jump:
7319 visit_jump(ctx, nir_instr_as_jump(instr));
7320 break;
7321 default:
7322 fprintf(stderr, "Unknown NIR instr type: ");
7323 nir_print_instr(instr, stderr);
7324 fprintf(stderr, "\n");
7325 //abort();
7326 }
7327 }
7328
7329 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7330 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
7331 }
7332
7333
7334
7335 static void visit_loop(isel_context *ctx, nir_loop *loop)
7336 {
7337 append_logical_end(ctx->block);
7338 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
7339 Builder bld(ctx->program, ctx->block);
7340 bld.branch(aco_opcode::p_branch);
7341 unsigned loop_preheader_idx = ctx->block->index;
7342
7343 Block loop_exit = Block();
7344 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7345 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
7346
7347 Block* loop_header = ctx->program->create_and_insert_block();
7348 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
7349 loop_header->kind |= block_kind_loop_header;
7350 add_edge(loop_preheader_idx, loop_header);
7351 ctx->block = loop_header;
7352
7353 /* emit loop body */
7354 unsigned loop_header_idx = loop_header->index;
7355 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
7356 append_logical_start(ctx->block);
7357 visit_cf_list(ctx, &loop->body);
7358
7359 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7360 if (!ctx->cf_info.has_branch) {
7361 append_logical_end(ctx->block);
7362 if (ctx->cf_info.exec_potentially_empty) {
7363 /* Discards can result in code running with an empty exec mask.
7364 * This would result in divergent breaks not ever being taken. As a
7365 * workaround, break the loop when the loop mask is empty instead of
7366 * always continuing. */
7367 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
7368 unsigned block_idx = ctx->block->index;
7369
7370 /* create helper blocks to avoid critical edges */
7371 Block *break_block = ctx->program->create_and_insert_block();
7372 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7373 break_block->kind = block_kind_uniform;
7374 bld.reset(break_block);
7375 bld.branch(aco_opcode::p_branch);
7376 add_linear_edge(block_idx, break_block);
7377 add_linear_edge(break_block->index, &loop_exit);
7378
7379 Block *continue_block = ctx->program->create_and_insert_block();
7380 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7381 continue_block->kind = block_kind_uniform;
7382 bld.reset(continue_block);
7383 bld.branch(aco_opcode::p_branch);
7384 add_linear_edge(block_idx, continue_block);
7385 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
7386
7387 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
7388 ctx->block = &ctx->program->blocks[block_idx];
7389 } else {
7390 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
7391 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7392 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
7393 else
7394 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
7395 }
7396
7397 bld.reset(ctx->block);
7398 bld.branch(aco_opcode::p_branch);
7399 }
7400
7401 /* fixup phis in loop header from unreachable blocks */
7402 if (ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch) {
7403 bool linear = ctx->cf_info.has_branch;
7404 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
7405 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
7406 if ((logical && instr->opcode == aco_opcode::p_phi) ||
7407 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
7408 /* the last operand should be the one that needs to be removed */
7409 instr->operands.pop_back();
7410 } else if (!is_phi(instr)) {
7411 break;
7412 }
7413 }
7414 }
7415
7416 ctx->cf_info.has_branch = false;
7417
7418 // TODO: if the loop has not a single exit, we must add one °°
7419 /* emit loop successor block */
7420 ctx->block = ctx->program->insert_block(std::move(loop_exit));
7421 append_logical_start(ctx->block);
7422
7423 #if 0
7424 // TODO: check if it is beneficial to not branch on continues
7425 /* trim linear phis in loop header */
7426 for (auto&& instr : loop_entry->instructions) {
7427 if (instr->opcode == aco_opcode::p_linear_phi) {
7428 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
7429 new_phi->definitions[0] = instr->definitions[0];
7430 for (unsigned i = 0; i < new_phi->operands.size(); i++)
7431 new_phi->operands[i] = instr->operands[i];
7432 /* check that the remaining operands are all the same */
7433 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
7434 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
7435 instr.swap(new_phi);
7436 } else if (instr->opcode == aco_opcode::p_phi) {
7437 continue;
7438 } else {
7439 break;
7440 }
7441 }
7442 #endif
7443 }
7444
7445 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
7446 {
7447 ic->cond = cond;
7448
7449 append_logical_end(ctx->block);
7450 ctx->block->kind |= block_kind_branch;
7451
7452 /* branch to linear then block */
7453 assert(cond.regClass() == ctx->program->lane_mask);
7454 aco_ptr<Pseudo_branch_instruction> branch;
7455 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
7456 branch->operands[0] = Operand(cond);
7457 ctx->block->instructions.push_back(std::move(branch));
7458
7459 ic->BB_if_idx = ctx->block->index;
7460 ic->BB_invert = Block();
7461 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7462 /* Invert blocks are intentionally not marked as top level because they
7463 * are not part of the logical cfg. */
7464 ic->BB_invert.kind |= block_kind_invert;
7465 ic->BB_endif = Block();
7466 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7467 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
7468
7469 ic->exec_potentially_empty_old = ctx->cf_info.exec_potentially_empty;
7470 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
7471 ctx->cf_info.parent_if.is_divergent = true;
7472 ctx->cf_info.exec_potentially_empty = false; /* divergent branches use cbranch_execz */
7473
7474 /** emit logical then block */
7475 Block* BB_then_logical = ctx->program->create_and_insert_block();
7476 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7477 add_edge(ic->BB_if_idx, BB_then_logical);
7478 ctx->block = BB_then_logical;
7479 append_logical_start(BB_then_logical);
7480 }
7481
7482 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
7483 {
7484 Block *BB_then_logical = ctx->block;
7485 append_logical_end(BB_then_logical);
7486 /* branch from logical then block to invert block */
7487 aco_ptr<Pseudo_branch_instruction> branch;
7488 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7489 BB_then_logical->instructions.emplace_back(std::move(branch));
7490 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
7491 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7492 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
7493 BB_then_logical->kind |= block_kind_uniform;
7494 assert(!ctx->cf_info.has_branch);
7495 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
7496 ctx->cf_info.parent_loop.has_divergent_branch = false;
7497
7498 /** emit linear then block */
7499 Block* BB_then_linear = ctx->program->create_and_insert_block();
7500 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7501 BB_then_linear->kind |= block_kind_uniform;
7502 add_linear_edge(ic->BB_if_idx, BB_then_linear);
7503 /* branch from linear then block to invert block */
7504 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7505 BB_then_linear->instructions.emplace_back(std::move(branch));
7506 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
7507
7508 /** emit invert merge block */
7509 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
7510 ic->invert_idx = ctx->block->index;
7511
7512 /* branch to linear else block (skip else) */
7513 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
7514 branch->operands[0] = Operand(ic->cond);
7515 ctx->block->instructions.push_back(std::move(branch));
7516
7517 ic->exec_potentially_empty_old |= ctx->cf_info.exec_potentially_empty;
7518 ctx->cf_info.exec_potentially_empty = false; /* divergent branches use cbranch_execz */
7519
7520 /** emit logical else block */
7521 Block* BB_else_logical = ctx->program->create_and_insert_block();
7522 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7523 add_logical_edge(ic->BB_if_idx, BB_else_logical);
7524 add_linear_edge(ic->invert_idx, BB_else_logical);
7525 ctx->block = BB_else_logical;
7526 append_logical_start(BB_else_logical);
7527 }
7528
7529 static void end_divergent_if(isel_context *ctx, if_context *ic)
7530 {
7531 Block *BB_else_logical = ctx->block;
7532 append_logical_end(BB_else_logical);
7533
7534 /* branch from logical else block to endif block */
7535 aco_ptr<Pseudo_branch_instruction> branch;
7536 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7537 BB_else_logical->instructions.emplace_back(std::move(branch));
7538 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
7539 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7540 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
7541 BB_else_logical->kind |= block_kind_uniform;
7542
7543 assert(!ctx->cf_info.has_branch);
7544 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
7545
7546
7547 /** emit linear else block */
7548 Block* BB_else_linear = ctx->program->create_and_insert_block();
7549 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7550 BB_else_linear->kind |= block_kind_uniform;
7551 add_linear_edge(ic->invert_idx, BB_else_linear);
7552
7553 /* branch from linear else block to endif block */
7554 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7555 BB_else_linear->instructions.emplace_back(std::move(branch));
7556 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
7557
7558
7559 /** emit endif merge block */
7560 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
7561 append_logical_start(ctx->block);
7562
7563
7564 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
7565 ctx->cf_info.exec_potentially_empty |= ic->exec_potentially_empty_old;
7566 /* uniform control flow never has an empty exec-mask */
7567 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
7568 ctx->cf_info.exec_potentially_empty = false;
7569 }
7570
7571 static void visit_if(isel_context *ctx, nir_if *if_stmt)
7572 {
7573 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
7574 Builder bld(ctx->program, ctx->block);
7575 aco_ptr<Pseudo_branch_instruction> branch;
7576
7577 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
7578 /**
7579 * Uniform conditionals are represented in the following way*) :
7580 *
7581 * The linear and logical CFG:
7582 * BB_IF
7583 * / \
7584 * BB_THEN (logical) BB_ELSE (logical)
7585 * \ /
7586 * BB_ENDIF
7587 *
7588 * *) Exceptions may be due to break and continue statements within loops
7589 * If a break/continue happens within uniform control flow, it branches
7590 * to the loop exit/entry block. Otherwise, it branches to the next
7591 * merge block.
7592 **/
7593 append_logical_end(ctx->block);
7594 ctx->block->kind |= block_kind_uniform;
7595
7596 /* emit branch */
7597 assert(cond.regClass() == bld.lm);
7598 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7599 cond = bool_to_scalar_condition(ctx, cond);
7600
7601 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
7602 branch->operands[0] = Operand(cond);
7603 branch->operands[0].setFixed(scc);
7604 ctx->block->instructions.emplace_back(std::move(branch));
7605
7606 unsigned BB_if_idx = ctx->block->index;
7607 Block BB_endif = Block();
7608 BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7609 BB_endif.kind |= ctx->block->kind & block_kind_top_level;
7610
7611 /** emit then block */
7612 Block* BB_then = ctx->program->create_and_insert_block();
7613 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7614 add_edge(BB_if_idx, BB_then);
7615 append_logical_start(BB_then);
7616 ctx->block = BB_then;
7617 visit_cf_list(ctx, &if_stmt->then_list);
7618 BB_then = ctx->block;
7619 bool then_branch = ctx->cf_info.has_branch;
7620 bool then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
7621
7622 if (!then_branch) {
7623 append_logical_end(BB_then);
7624 /* branch from then block to endif block */
7625 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7626 BB_then->instructions.emplace_back(std::move(branch));
7627 add_linear_edge(BB_then->index, &BB_endif);
7628 if (!then_branch_divergent)
7629 add_logical_edge(BB_then->index, &BB_endif);
7630 BB_then->kind |= block_kind_uniform;
7631 }
7632
7633 ctx->cf_info.has_branch = false;
7634 ctx->cf_info.parent_loop.has_divergent_branch = false;
7635
7636 /** emit else block */
7637 Block* BB_else = ctx->program->create_and_insert_block();
7638 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7639 add_edge(BB_if_idx, BB_else);
7640 append_logical_start(BB_else);
7641 ctx->block = BB_else;
7642 visit_cf_list(ctx, &if_stmt->else_list);
7643 BB_else = ctx->block;
7644
7645 if (!ctx->cf_info.has_branch) {
7646 append_logical_end(BB_else);
7647 /* branch from then block to endif block */
7648 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7649 BB_else->instructions.emplace_back(std::move(branch));
7650 add_linear_edge(BB_else->index, &BB_endif);
7651 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7652 add_logical_edge(BB_else->index, &BB_endif);
7653 BB_else->kind |= block_kind_uniform;
7654 }
7655
7656 ctx->cf_info.has_branch &= then_branch;
7657 ctx->cf_info.parent_loop.has_divergent_branch &= then_branch_divergent;
7658
7659 /** emit endif merge block */
7660 if (!ctx->cf_info.has_branch) {
7661 ctx->block = ctx->program->insert_block(std::move(BB_endif));
7662 append_logical_start(ctx->block);
7663 }
7664 } else { /* non-uniform condition */
7665 /**
7666 * To maintain a logical and linear CFG without critical edges,
7667 * non-uniform conditionals are represented in the following way*) :
7668 *
7669 * The linear CFG:
7670 * BB_IF
7671 * / \
7672 * BB_THEN (logical) BB_THEN (linear)
7673 * \ /
7674 * BB_INVERT (linear)
7675 * / \
7676 * BB_ELSE (logical) BB_ELSE (linear)
7677 * \ /
7678 * BB_ENDIF
7679 *
7680 * The logical CFG:
7681 * BB_IF
7682 * / \
7683 * BB_THEN (logical) BB_ELSE (logical)
7684 * \ /
7685 * BB_ENDIF
7686 *
7687 * *) Exceptions may be due to break and continue statements within loops
7688 **/
7689
7690 if_context ic;
7691
7692 begin_divergent_if_then(ctx, &ic, cond);
7693 visit_cf_list(ctx, &if_stmt->then_list);
7694
7695 begin_divergent_if_else(ctx, &ic);
7696 visit_cf_list(ctx, &if_stmt->else_list);
7697
7698 end_divergent_if(ctx, &ic);
7699 }
7700 }
7701
7702 static void visit_cf_list(isel_context *ctx,
7703 struct exec_list *list)
7704 {
7705 foreach_list_typed(nir_cf_node, node, node, list) {
7706 switch (node->type) {
7707 case nir_cf_node_block:
7708 visit_block(ctx, nir_cf_node_as_block(node));
7709 break;
7710 case nir_cf_node_if:
7711 visit_if(ctx, nir_cf_node_as_if(node));
7712 break;
7713 case nir_cf_node_loop:
7714 visit_loop(ctx, nir_cf_node_as_loop(node));
7715 break;
7716 default:
7717 unreachable("unimplemented cf list type");
7718 }
7719 }
7720 }
7721
7722 static void export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
7723 {
7724 int offset = ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
7725 uint64_t mask = ctx->vs_output.mask[slot];
7726 if (!is_pos && !mask)
7727 return;
7728 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
7729 return;
7730 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
7731 exp->enabled_mask = mask;
7732 for (unsigned i = 0; i < 4; ++i) {
7733 if (mask & (1 << i))
7734 exp->operands[i] = Operand(ctx->vs_output.outputs[slot][i]);
7735 else
7736 exp->operands[i] = Operand(v1);
7737 }
7738 exp->valid_mask = false;
7739 exp->done = false;
7740 exp->compressed = false;
7741 if (is_pos)
7742 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
7743 else
7744 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
7745 ctx->block->instructions.emplace_back(std::move(exp));
7746 }
7747
7748 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
7749 {
7750 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
7751 exp->enabled_mask = 0;
7752 for (unsigned i = 0; i < 4; ++i)
7753 exp->operands[i] = Operand(v1);
7754 if (ctx->vs_output.mask[VARYING_SLOT_PSIZ]) {
7755 exp->operands[0] = Operand(ctx->vs_output.outputs[VARYING_SLOT_PSIZ][0]);
7756 exp->enabled_mask |= 0x1;
7757 }
7758 if (ctx->vs_output.mask[VARYING_SLOT_LAYER]) {
7759 exp->operands[2] = Operand(ctx->vs_output.outputs[VARYING_SLOT_LAYER][0]);
7760 exp->enabled_mask |= 0x4;
7761 }
7762 if (ctx->vs_output.mask[VARYING_SLOT_VIEWPORT]) {
7763 if (ctx->options->chip_class < GFX9) {
7764 exp->operands[3] = Operand(ctx->vs_output.outputs[VARYING_SLOT_VIEWPORT][0]);
7765 exp->enabled_mask |= 0x8;
7766 } else {
7767 Builder bld(ctx->program, ctx->block);
7768
7769 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
7770 Operand(ctx->vs_output.outputs[VARYING_SLOT_VIEWPORT][0]));
7771 if (exp->operands[2].isTemp())
7772 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
7773
7774 exp->operands[2] = Operand(out);
7775 exp->enabled_mask |= 0x4;
7776 }
7777 }
7778 exp->valid_mask = false;
7779 exp->done = false;
7780 exp->compressed = false;
7781 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
7782 ctx->block->instructions.emplace_back(std::move(exp));
7783 }
7784
7785 static void create_vs_exports(isel_context *ctx)
7786 {
7787 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
7788
7789 if (outinfo->export_prim_id) {
7790 ctx->vs_output.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
7791 ctx->vs_output.outputs[VARYING_SLOT_PRIMITIVE_ID][0] = get_arg(ctx, ctx->args->vs_prim_id);
7792 }
7793
7794 if (ctx->options->key.has_multiview_view_index) {
7795 ctx->vs_output.mask[VARYING_SLOT_LAYER] |= 0x1;
7796 ctx->vs_output.outputs[VARYING_SLOT_LAYER][0] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
7797 }
7798
7799 /* the order these position exports are created is important */
7800 int next_pos = 0;
7801 export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
7802 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
7803 export_vs_psiz_layer_viewport(ctx, &next_pos);
7804 }
7805 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
7806 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
7807 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
7808 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
7809
7810 if (ctx->options->key.vs_common_out.export_clip_dists) {
7811 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
7812 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
7813 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
7814 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
7815 }
7816
7817 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
7818 if (i < VARYING_SLOT_VAR0 && i != VARYING_SLOT_LAYER &&
7819 i != VARYING_SLOT_PRIMITIVE_ID)
7820 continue;
7821
7822 export_vs_varying(ctx, i, false, NULL);
7823 }
7824 }
7825
7826 static void emit_stream_output(isel_context *ctx,
7827 Temp const *so_buffers,
7828 Temp const *so_write_offset,
7829 const struct radv_stream_output *output)
7830 {
7831 unsigned num_comps = util_bitcount(output->component_mask);
7832 unsigned loc = output->location;
7833 unsigned buf = output->buffer;
7834 unsigned offset = output->offset;
7835
7836 assert(num_comps && num_comps <= 4);
7837 if (!num_comps || num_comps > 4)
7838 return;
7839
7840 unsigned start = ffs(output->component_mask) - 1;
7841
7842 Temp out[4];
7843 bool all_undef = true;
7844 assert(ctx->stage == vertex_vs);
7845 for (unsigned i = 0; i < num_comps; i++) {
7846 out[i] = ctx->vs_output.outputs[loc][start + i];
7847 all_undef = all_undef && !out[i].id();
7848 }
7849 if (all_undef)
7850 return;
7851
7852 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_comps)};
7853 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_comps, 1)};
7854 for (unsigned i = 0; i < num_comps; ++i)
7855 vec->operands[i] = (ctx->vs_output.mask[loc] & 1 << i) ? Operand(out[i]) : Operand(0u);
7856 vec->definitions[0] = Definition(write_data);
7857 ctx->block->instructions.emplace_back(std::move(vec));
7858
7859 aco_opcode opcode;
7860 switch (num_comps) {
7861 case 1:
7862 opcode = aco_opcode::buffer_store_dword;
7863 break;
7864 case 2:
7865 opcode = aco_opcode::buffer_store_dwordx2;
7866 break;
7867 case 3:
7868 opcode = aco_opcode::buffer_store_dwordx3;
7869 break;
7870 case 4:
7871 opcode = aco_opcode::buffer_store_dwordx4;
7872 break;
7873 }
7874
7875 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
7876 store->operands[0] = Operand(so_write_offset[buf]);
7877 store->operands[1] = Operand(so_buffers[buf]);
7878 store->operands[2] = Operand((uint32_t) 0);
7879 store->operands[3] = Operand(write_data);
7880 if (offset > 4095) {
7881 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7882 Builder bld(ctx->program, ctx->block);
7883 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
7884 } else {
7885 store->offset = offset;
7886 }
7887 store->offen = true;
7888 store->glc = true;
7889 store->dlc = false;
7890 store->slc = true;
7891 store->can_reorder = true;
7892 ctx->block->instructions.emplace_back(std::move(store));
7893 }
7894
7895 static void emit_streamout(isel_context *ctx, unsigned stream)
7896 {
7897 Builder bld(ctx->program, ctx->block);
7898
7899 Temp so_buffers[4];
7900 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
7901 for (unsigned i = 0; i < 4; i++) {
7902 unsigned stride = ctx->program->info->so.strides[i];
7903 if (!stride)
7904 continue;
7905
7906 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, Operand(i * 16u));
7907 }
7908
7909 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7910 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
7911
7912 Temp tid = emit_mbcnt(ctx, bld.def(v1));
7913
7914 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(s2), so_vtx_count, tid);
7915
7916 if_context ic;
7917 begin_divergent_if_then(ctx, &ic, can_emit);
7918
7919 bld.reset(ctx->block);
7920
7921 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
7922
7923 Temp so_write_offset[4];
7924
7925 for (unsigned i = 0; i < 4; i++) {
7926 unsigned stride = ctx->program->info->so.strides[i];
7927 if (!stride)
7928 continue;
7929
7930 if (stride == 1) {
7931 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
7932 get_arg(ctx, ctx->args->streamout_write_idx),
7933 get_arg(ctx, ctx->args->streamout_offset[i]));
7934 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
7935
7936 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
7937 } else {
7938 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
7939 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
7940 get_arg(ctx, ctx->args->streamout_offset[i]));
7941 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
7942 }
7943 }
7944
7945 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
7946 struct radv_stream_output *output =
7947 &ctx->program->info->so.outputs[i];
7948 if (stream != output->stream)
7949 continue;
7950
7951 emit_stream_output(ctx, so_buffers, so_write_offset, output);
7952 }
7953
7954 begin_divergent_if_else(ctx, &ic);
7955 end_divergent_if(ctx, &ic);
7956 }
7957
7958 } /* end namespace */
7959
7960 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
7961 {
7962 /* Split all arguments except for the first (ring_offsets) and the last
7963 * (exec) so that the dead channels don't stay live throughout the program.
7964 */
7965 for (unsigned i = 1; i < startpgm->definitions.size() - 1; i++) {
7966 if (startpgm->definitions[i].regClass().size() > 1) {
7967 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
7968 startpgm->definitions[i].regClass().size());
7969 }
7970 }
7971 }
7972
7973 void handle_bc_optimize(isel_context *ctx)
7974 {
7975 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7976 Builder bld(ctx->program, ctx->block);
7977 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
7978 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
7979 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
7980 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
7981 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
7982 if (uses_center && uses_centroid) {
7983 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
7984 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
7985
7986 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
7987 Temp new_coord[2];
7988 for (unsigned i = 0; i < 2; i++) {
7989 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
7990 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
7991 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7992 persp_centroid, persp_center, sel);
7993 }
7994 ctx->persp_centroid = bld.tmp(v2);
7995 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
7996 Operand(new_coord[0]), Operand(new_coord[1]));
7997 emit_split_vector(ctx, ctx->persp_centroid, 2);
7998 }
7999
8000 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
8001 Temp new_coord[2];
8002 for (unsigned i = 0; i < 2; i++) {
8003 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
8004 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
8005 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8006 linear_centroid, linear_center, sel);
8007 }
8008 ctx->linear_centroid = bld.tmp(v2);
8009 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
8010 Operand(new_coord[0]), Operand(new_coord[1]));
8011 emit_split_vector(ctx, ctx->linear_centroid, 2);
8012 }
8013 }
8014 }
8015
8016 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
8017 {
8018 Program *program = ctx->program;
8019
8020 unsigned float_controls = shader->info.float_controls_execution_mode;
8021
8022 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
8023 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
8024 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
8025 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
8026 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
8027
8028 program->next_fp_mode.must_flush_denorms32 =
8029 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
8030 program->next_fp_mode.must_flush_denorms16_64 =
8031 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
8032 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
8033
8034 program->next_fp_mode.care_about_round32 =
8035 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
8036
8037 program->next_fp_mode.care_about_round16_64 =
8038 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
8039 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
8040
8041 /* default to preserving fp16 and fp64 denorms, since it's free */
8042 if (program->next_fp_mode.must_flush_denorms16_64)
8043 program->next_fp_mode.denorm16_64 = 0;
8044 else
8045 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
8046
8047 /* preserving fp32 denorms is expensive, so only do it if asked */
8048 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
8049 program->next_fp_mode.denorm32 = fp_denorm_keep;
8050 else
8051 program->next_fp_mode.denorm32 = 0;
8052
8053 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
8054 program->next_fp_mode.round32 = fp_round_tz;
8055 else
8056 program->next_fp_mode.round32 = fp_round_ne;
8057
8058 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
8059 program->next_fp_mode.round16_64 = fp_round_tz;
8060 else
8061 program->next_fp_mode.round16_64 = fp_round_ne;
8062
8063 ctx->block->fp_mode = program->next_fp_mode;
8064 }
8065
8066 void select_program(Program *program,
8067 unsigned shader_count,
8068 struct nir_shader *const *shaders,
8069 ac_shader_config* config,
8070 struct radv_shader_args *args)
8071 {
8072 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args);
8073
8074 for (unsigned i = 0; i < shader_count; i++) {
8075 nir_shader *nir = shaders[i];
8076 init_context(&ctx, nir);
8077
8078 setup_fp_mode(&ctx, nir);
8079
8080 if (!i) {
8081 /* needs to be after init_context() for FS */
8082 Pseudo_instruction *startpgm = add_startpgm(&ctx);
8083 append_logical_start(ctx.block);
8084 split_arguments(&ctx, startpgm);
8085 }
8086
8087 if_context ic;
8088 if (shader_count >= 2) {
8089 Builder bld(ctx.program, ctx.block);
8090 Temp count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), ctx.merged_wave_info, Operand((8u << 16) | (i * 8u)));
8091 Temp thread_id = emit_mbcnt(&ctx, bld.def(v1));
8092 Temp cond = bld.vopc(aco_opcode::v_cmp_gt_u32, bld.hint_vcc(bld.def(bld.lm)), count, thread_id);
8093
8094 begin_divergent_if_then(&ctx, &ic, cond);
8095 }
8096
8097 if (i) {
8098 Builder bld(ctx.program, ctx.block);
8099 bld.barrier(aco_opcode::p_memory_barrier_shared); //TODO: different barriers are needed for different stages
8100 bld.sopp(aco_opcode::s_barrier);
8101 }
8102
8103 if (ctx.stage == fragment_fs)
8104 handle_bc_optimize(&ctx);
8105
8106 nir_function_impl *func = nir_shader_get_entrypoint(nir);
8107 visit_cf_list(&ctx, &func->body);
8108
8109 if (ctx.program->info->so.num_outputs/*&& !ctx->is_gs_copy_shader */)
8110 emit_streamout(&ctx, 0);
8111
8112 if (ctx.stage == vertex_vs)
8113 create_vs_exports(&ctx);
8114
8115 if (shader_count >= 2) {
8116 begin_divergent_if_else(&ctx, &ic);
8117 end_divergent_if(&ctx, &ic);
8118 }
8119
8120 ralloc_free(ctx.divergent_vals);
8121 }
8122
8123 program->config->float_mode = program->blocks[0].fp_mode.val;
8124
8125 append_logical_end(ctx.block);
8126 ctx.block->kind |= block_kind_uniform;
8127 Builder bld(ctx.program, ctx.block);
8128 if (ctx.program->wb_smem_l1_on_end)
8129 bld.smem(aco_opcode::s_dcache_wb, false);
8130 bld.sopp(aco_opcode::s_endpgm);
8131
8132 /* cleanup CFG */
8133 for (Block& BB : program->blocks) {
8134 for (unsigned idx : BB.linear_preds)
8135 program->blocks[idx].linear_succs.emplace_back(BB.index);
8136 for (unsigned idx : BB.logical_preds)
8137 program->blocks[idx].logical_succs.emplace_back(BB.index);
8138 }
8139 }
8140 }