aco: use soffset for MUBUF instructions on SI/CI
[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 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3379 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3380 unsigned const_offset = 0;
3381
3382 Temp lower = Temp();
3383 if (num_bytes > 16) {
3384 assert(num_components == 3 || num_components == 4);
3385 op = aco_opcode::buffer_load_dwordx4;
3386 lower = bld.tmp(v4);
3387 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3388 mubuf->definitions[0] = Definition(lower);
3389 mubuf->operands[0] = vaddr;
3390 mubuf->operands[1] = Operand(rsrc);
3391 mubuf->operands[2] = soffset;
3392 mubuf->offen = (offset.type() == RegType::vgpr);
3393 mubuf->glc = glc;
3394 mubuf->dlc = dlc;
3395 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
3396 mubuf->can_reorder = readonly;
3397 bld.insert(std::move(mubuf));
3398 emit_split_vector(ctx, lower, 2);
3399 num_bytes -= 16;
3400 const_offset = 16;
3401 }
3402
3403 switch (num_bytes) {
3404 case 4:
3405 op = aco_opcode::buffer_load_dword;
3406 break;
3407 case 8:
3408 op = aco_opcode::buffer_load_dwordx2;
3409 break;
3410 case 12:
3411 op = aco_opcode::buffer_load_dwordx3;
3412 break;
3413 case 16:
3414 op = aco_opcode::buffer_load_dwordx4;
3415 break;
3416 default:
3417 unreachable("Load SSBO not implemented for this size.");
3418 }
3419 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3420 mubuf->operands[0] = vaddr;
3421 mubuf->operands[1] = Operand(rsrc);
3422 mubuf->operands[2] = soffset;
3423 mubuf->offen = (offset.type() == RegType::vgpr);
3424 mubuf->glc = glc;
3425 mubuf->dlc = dlc;
3426 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
3427 mubuf->can_reorder = readonly;
3428 mubuf->offset = const_offset;
3429 aco_ptr<Instruction> instr = std::move(mubuf);
3430
3431 if (dst.size() > 4) {
3432 assert(lower != Temp());
3433 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
3434 instr->definitions[0] = Definition(upper);
3435 bld.insert(std::move(instr));
3436 if (dst.size() == 8)
3437 emit_split_vector(ctx, upper, 2);
3438 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
3439 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
3440 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
3441 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
3442 if (dst.size() == 8)
3443 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
3444 }
3445
3446 if (dst.type() == RegType::sgpr) {
3447 Temp vec = bld.tmp(RegType::vgpr, dst.size());
3448 instr->definitions[0] = Definition(vec);
3449 bld.insert(std::move(instr));
3450 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
3451 } else {
3452 instr->definitions[0] = Definition(dst);
3453 bld.insert(std::move(instr));
3454 }
3455 } else {
3456 switch (num_bytes) {
3457 case 4:
3458 op = aco_opcode::s_buffer_load_dword;
3459 break;
3460 case 8:
3461 op = aco_opcode::s_buffer_load_dwordx2;
3462 break;
3463 case 12:
3464 case 16:
3465 op = aco_opcode::s_buffer_load_dwordx4;
3466 break;
3467 case 24:
3468 case 32:
3469 op = aco_opcode::s_buffer_load_dwordx8;
3470 break;
3471 default:
3472 unreachable("Load SSBO not implemented for this size.");
3473 }
3474 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3475 load->operands[0] = Operand(rsrc);
3476 load->operands[1] = Operand(bld.as_uniform(offset));
3477 assert(load->operands[1].getTemp().type() == RegType::sgpr);
3478 load->definitions[0] = Definition(dst);
3479 load->glc = glc;
3480 load->dlc = dlc;
3481 load->barrier = readonly ? barrier_none : barrier_buffer;
3482 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3483 assert(ctx->options->chip_class >= GFX8 || !glc);
3484
3485 /* trim vector */
3486 if (dst.size() == 3) {
3487 Temp vec = bld.tmp(s4);
3488 load->definitions[0] = Definition(vec);
3489 bld.insert(std::move(load));
3490 emit_split_vector(ctx, vec, 4);
3491
3492 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3493 emit_extract_vector(ctx, vec, 0, s1),
3494 emit_extract_vector(ctx, vec, 1, s1),
3495 emit_extract_vector(ctx, vec, 2, s1));
3496 } else if (dst.size() == 6) {
3497 Temp vec = bld.tmp(s8);
3498 load->definitions[0] = Definition(vec);
3499 bld.insert(std::move(load));
3500 emit_split_vector(ctx, vec, 4);
3501
3502 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3503 emit_extract_vector(ctx, vec, 0, s2),
3504 emit_extract_vector(ctx, vec, 1, s2),
3505 emit_extract_vector(ctx, vec, 2, s2));
3506 } else {
3507 bld.insert(std::move(load));
3508 }
3509
3510 }
3511 emit_split_vector(ctx, dst, num_components);
3512 }
3513
3514 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
3515 {
3516 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3517 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
3518
3519 Builder bld(ctx->program, ctx->block);
3520
3521 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
3522 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
3523 unsigned binding = nir_intrinsic_binding(idx_instr);
3524 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
3525
3526 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
3527 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
3528 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
3529 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
3530 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
3531 if (ctx->options->chip_class >= GFX10) {
3532 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
3533 S_008F0C_OOB_SELECT(3) |
3534 S_008F0C_RESOURCE_LEVEL(1);
3535 } else {
3536 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3537 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3538 }
3539 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
3540 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
3541 Operand(0xFFFFFFFFu),
3542 Operand(desc_type));
3543 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
3544 rsrc, upper_dwords);
3545 } else {
3546 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
3547 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
3548 }
3549
3550 load_buffer(ctx, instr->num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa));
3551 }
3552
3553 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
3554 {
3555 Builder bld(ctx->program, ctx->block);
3556 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3557
3558 unsigned offset = nir_intrinsic_base(instr);
3559 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
3560 if (index_cv && instr->dest.ssa.bit_size == 32) {
3561
3562 unsigned count = instr->dest.ssa.num_components;
3563 unsigned start = (offset + index_cv->u32) / 4u;
3564 start -= ctx->args->ac.base_inline_push_consts;
3565 if (start + count <= ctx->args->ac.num_inline_push_consts) {
3566 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
3567 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
3568 for (unsigned i = 0; i < count; ++i) {
3569 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
3570 vec->operands[i] = Operand{elems[i]};
3571 }
3572 vec->definitions[0] = Definition(dst);
3573 ctx->block->instructions.emplace_back(std::move(vec));
3574 ctx->allocated_vec.emplace(dst.id(), elems);
3575 return;
3576 }
3577 }
3578
3579 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
3580 if (offset != 0) // TODO check if index != 0 as well
3581 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
3582 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
3583 Temp vec = dst;
3584 bool trim = false;
3585 aco_opcode op;
3586
3587 switch (dst.size()) {
3588 case 1:
3589 op = aco_opcode::s_load_dword;
3590 break;
3591 case 2:
3592 op = aco_opcode::s_load_dwordx2;
3593 break;
3594 case 3:
3595 vec = bld.tmp(s4);
3596 trim = true;
3597 case 4:
3598 op = aco_opcode::s_load_dwordx4;
3599 break;
3600 case 6:
3601 vec = bld.tmp(s8);
3602 trim = true;
3603 case 8:
3604 op = aco_opcode::s_load_dwordx8;
3605 break;
3606 default:
3607 unreachable("unimplemented or forbidden load_push_constant.");
3608 }
3609
3610 bld.smem(op, Definition(vec), ptr, index);
3611
3612 if (trim) {
3613 emit_split_vector(ctx, vec, 4);
3614 RegClass rc = dst.size() == 3 ? s1 : s2;
3615 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3616 emit_extract_vector(ctx, vec, 0, rc),
3617 emit_extract_vector(ctx, vec, 1, rc),
3618 emit_extract_vector(ctx, vec, 2, rc));
3619
3620 }
3621 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
3622 }
3623
3624 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
3625 {
3626 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3627
3628 Builder bld(ctx->program, ctx->block);
3629
3630 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
3631 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
3632 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
3633 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
3634 if (ctx->options->chip_class >= GFX10) {
3635 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
3636 S_008F0C_OOB_SELECT(3) |
3637 S_008F0C_RESOURCE_LEVEL(1);
3638 } else {
3639 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3640 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3641 }
3642
3643 unsigned base = nir_intrinsic_base(instr);
3644 unsigned range = nir_intrinsic_range(instr);
3645
3646 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
3647 if (base && offset.type() == RegType::sgpr)
3648 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
3649 else if (base && offset.type() == RegType::vgpr)
3650 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
3651
3652 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
3653 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
3654 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
3655 Operand(desc_type));
3656
3657 load_buffer(ctx, instr->num_components, dst, rsrc, offset);
3658 }
3659
3660 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
3661 {
3662 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
3663 ctx->cf_info.exec_potentially_empty = true;
3664
3665 ctx->program->needs_exact = true;
3666
3667 // TODO: optimize uniform conditions
3668 Builder bld(ctx->program, ctx->block);
3669 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3670 assert(src.regClass() == bld.lm);
3671 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
3672 bld.pseudo(aco_opcode::p_discard_if, src);
3673 ctx->block->kind |= block_kind_uses_discard_if;
3674 return;
3675 }
3676
3677 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
3678 {
3679 Builder bld(ctx->program, ctx->block);
3680
3681 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
3682 ctx->cf_info.exec_potentially_empty = true;
3683
3684 bool divergent = ctx->cf_info.parent_if.is_divergent ||
3685 ctx->cf_info.parent_loop.has_divergent_continue;
3686
3687 if (ctx->block->loop_nest_depth &&
3688 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
3689 /* we handle discards the same way as jump instructions */
3690 append_logical_end(ctx->block);
3691
3692 /* in loops, discard behaves like break */
3693 Block *linear_target = ctx->cf_info.parent_loop.exit;
3694 ctx->block->kind |= block_kind_discard;
3695
3696 if (!divergent) {
3697 /* uniform discard - loop ends here */
3698 assert(nir_instr_is_last(&instr->instr));
3699 ctx->block->kind |= block_kind_uniform;
3700 ctx->cf_info.has_branch = true;
3701 bld.branch(aco_opcode::p_branch);
3702 add_linear_edge(ctx->block->index, linear_target);
3703 return;
3704 }
3705
3706 /* we add a break right behind the discard() instructions */
3707 ctx->block->kind |= block_kind_break;
3708 unsigned idx = ctx->block->index;
3709
3710 /* remove critical edges from linear CFG */
3711 bld.branch(aco_opcode::p_branch);
3712 Block* break_block = ctx->program->create_and_insert_block();
3713 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
3714 break_block->kind |= block_kind_uniform;
3715 add_linear_edge(idx, break_block);
3716 add_linear_edge(break_block->index, linear_target);
3717 bld.reset(break_block);
3718 bld.branch(aco_opcode::p_branch);
3719
3720 Block* continue_block = ctx->program->create_and_insert_block();
3721 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
3722 add_linear_edge(idx, continue_block);
3723 append_logical_start(continue_block);
3724 ctx->block = continue_block;
3725
3726 return;
3727 }
3728
3729 /* it can currently happen that NIR doesn't remove the unreachable code */
3730 if (!nir_instr_is_last(&instr->instr)) {
3731 ctx->program->needs_exact = true;
3732 /* save exec somewhere temporarily so that it doesn't get
3733 * overwritten before the discard from outer exec masks */
3734 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
3735 bld.pseudo(aco_opcode::p_discard_if, cond);
3736 ctx->block->kind |= block_kind_uses_discard_if;
3737 return;
3738 }
3739
3740 /* This condition is incorrect for uniformly branched discards in a loop
3741 * predicated by a divergent condition, but the above code catches that case
3742 * and the discard would end up turning into a discard_if.
3743 * For example:
3744 * if (divergent) {
3745 * while (...) {
3746 * if (uniform) {
3747 * discard;
3748 * }
3749 * }
3750 * }
3751 */
3752 if (!ctx->cf_info.parent_if.is_divergent) {
3753 /* program just ends here */
3754 ctx->block->kind |= block_kind_uniform;
3755 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
3756 0 /* enabled mask */, 9 /* dest */,
3757 false /* compressed */, true/* done */, true /* valid mask */);
3758 bld.sopp(aco_opcode::s_endpgm);
3759 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3760 } else {
3761 ctx->block->kind |= block_kind_discard;
3762 /* branch and linear edge is added by visit_if() */
3763 }
3764 }
3765
3766 enum aco_descriptor_type {
3767 ACO_DESC_IMAGE,
3768 ACO_DESC_FMASK,
3769 ACO_DESC_SAMPLER,
3770 ACO_DESC_BUFFER,
3771 ACO_DESC_PLANE_0,
3772 ACO_DESC_PLANE_1,
3773 ACO_DESC_PLANE_2,
3774 };
3775
3776 static bool
3777 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
3778 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
3779 return false;
3780 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
3781 return dim == ac_image_cube ||
3782 dim == ac_image_1darray ||
3783 dim == ac_image_2darray ||
3784 dim == ac_image_2darraymsaa;
3785 }
3786
3787 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
3788 enum aco_descriptor_type desc_type,
3789 const nir_tex_instr *tex_instr, bool image, bool write)
3790 {
3791 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3792 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3793 if (it != ctx->tex_desc.end())
3794 return it->second;
3795 */
3796 Temp index = Temp();
3797 bool index_set = false;
3798 unsigned constant_index = 0;
3799 unsigned descriptor_set;
3800 unsigned base_index;
3801 Builder bld(ctx->program, ctx->block);
3802
3803 if (!deref_instr) {
3804 assert(tex_instr && !image);
3805 descriptor_set = 0;
3806 base_index = tex_instr->sampler_index;
3807 } else {
3808 while(deref_instr->deref_type != nir_deref_type_var) {
3809 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
3810 if (!array_size)
3811 array_size = 1;
3812
3813 assert(deref_instr->deref_type == nir_deref_type_array);
3814 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
3815 if (const_value) {
3816 constant_index += array_size * const_value->u32;
3817 } else {
3818 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
3819 if (indirect.type() == RegType::vgpr)
3820 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
3821
3822 if (array_size != 1)
3823 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
3824
3825 if (!index_set) {
3826 index = indirect;
3827 index_set = true;
3828 } else {
3829 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
3830 }
3831 }
3832
3833 deref_instr = nir_src_as_deref(deref_instr->parent);
3834 }
3835 descriptor_set = deref_instr->var->data.descriptor_set;
3836 base_index = deref_instr->var->data.binding;
3837 }
3838
3839 Temp list = load_desc_ptr(ctx, descriptor_set);
3840 list = convert_pointer_to_64_bit(ctx, list);
3841
3842 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
3843 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
3844 unsigned offset = binding->offset;
3845 unsigned stride = binding->size;
3846 aco_opcode opcode;
3847 RegClass type;
3848
3849 assert(base_index < layout->binding_count);
3850
3851 switch (desc_type) {
3852 case ACO_DESC_IMAGE:
3853 type = s8;
3854 opcode = aco_opcode::s_load_dwordx8;
3855 break;
3856 case ACO_DESC_FMASK:
3857 type = s8;
3858 opcode = aco_opcode::s_load_dwordx8;
3859 offset += 32;
3860 break;
3861 case ACO_DESC_SAMPLER:
3862 type = s4;
3863 opcode = aco_opcode::s_load_dwordx4;
3864 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
3865 offset += radv_combined_image_descriptor_sampler_offset(binding);
3866 break;
3867 case ACO_DESC_BUFFER:
3868 type = s4;
3869 opcode = aco_opcode::s_load_dwordx4;
3870 break;
3871 case ACO_DESC_PLANE_0:
3872 case ACO_DESC_PLANE_1:
3873 type = s8;
3874 opcode = aco_opcode::s_load_dwordx8;
3875 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
3876 break;
3877 case ACO_DESC_PLANE_2:
3878 type = s4;
3879 opcode = aco_opcode::s_load_dwordx4;
3880 offset += 64;
3881 break;
3882 default:
3883 unreachable("invalid desc_type\n");
3884 }
3885
3886 offset += constant_index * stride;
3887
3888 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
3889 (!index_set || binding->immutable_samplers_equal)) {
3890 if (binding->immutable_samplers_equal)
3891 constant_index = 0;
3892
3893 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
3894 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
3895 Operand(samplers[constant_index * 4 + 0]),
3896 Operand(samplers[constant_index * 4 + 1]),
3897 Operand(samplers[constant_index * 4 + 2]),
3898 Operand(samplers[constant_index * 4 + 3]));
3899 }
3900
3901 Operand off;
3902 if (!index_set) {
3903 off = Operand(offset);
3904 } else {
3905 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
3906 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
3907 }
3908
3909 Temp res = bld.smem(opcode, bld.def(type), list, off);
3910
3911 if (desc_type == ACO_DESC_PLANE_2) {
3912 Temp components[8];
3913 for (unsigned i = 0; i < 8; i++)
3914 components[i] = bld.tmp(s1);
3915 bld.pseudo(aco_opcode::p_split_vector,
3916 Definition(components[0]),
3917 Definition(components[1]),
3918 Definition(components[2]),
3919 Definition(components[3]),
3920 res);
3921
3922 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
3923 bld.pseudo(aco_opcode::p_split_vector,
3924 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
3925 Definition(components[4]),
3926 Definition(components[5]),
3927 Definition(components[6]),
3928 Definition(components[7]),
3929 desc2);
3930
3931 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
3932 components[0], components[1], components[2], components[3],
3933 components[4], components[5], components[6], components[7]);
3934 }
3935
3936 return res;
3937 }
3938
3939 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
3940 {
3941 switch (dim) {
3942 case GLSL_SAMPLER_DIM_BUF:
3943 return 1;
3944 case GLSL_SAMPLER_DIM_1D:
3945 return array ? 2 : 1;
3946 case GLSL_SAMPLER_DIM_2D:
3947 return array ? 3 : 2;
3948 case GLSL_SAMPLER_DIM_MS:
3949 return array ? 4 : 3;
3950 case GLSL_SAMPLER_DIM_3D:
3951 case GLSL_SAMPLER_DIM_CUBE:
3952 return 3;
3953 case GLSL_SAMPLER_DIM_RECT:
3954 case GLSL_SAMPLER_DIM_SUBPASS:
3955 return 2;
3956 case GLSL_SAMPLER_DIM_SUBPASS_MS:
3957 return 3;
3958 default:
3959 break;
3960 }
3961 return 0;
3962 }
3963
3964
3965 /* Adjust the sample index according to FMASK.
3966 *
3967 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3968 * which is the identity mapping. Each nibble says which physical sample
3969 * should be fetched to get that sample.
3970 *
3971 * For example, 0x11111100 means there are only 2 samples stored and
3972 * the second sample covers 3/4 of the pixel. When reading samples 0
3973 * and 1, return physical sample 0 (determined by the first two 0s
3974 * in FMASK), otherwise return physical sample 1.
3975 *
3976 * The sample index should be adjusted as follows:
3977 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3978 */
3979 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, Temp coords, Operand sample_index, Temp fmask_desc_ptr)
3980 {
3981 Builder bld(ctx->program, ctx->block);
3982 Temp fmask = bld.tmp(v1);
3983 unsigned dim = ctx->options->chip_class >= GFX10
3984 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
3985 : 0;
3986
3987 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 2, 1)};
3988 load->operands[0] = Operand(coords);
3989 load->operands[1] = Operand(fmask_desc_ptr);
3990 load->definitions[0] = Definition(fmask);
3991 load->glc = false;
3992 load->dlc = false;
3993 load->dmask = 0x1;
3994 load->unrm = true;
3995 load->da = da;
3996 load->dim = dim;
3997 load->can_reorder = true; /* fmask images shouldn't be modified */
3998 ctx->block->instructions.emplace_back(std::move(load));
3999
4000 Operand sample_index4;
4001 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
4002 sample_index4 = Operand(sample_index.constantValue() << 2);
4003 } else if (sample_index.regClass() == s1) {
4004 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
4005 } else {
4006 assert(sample_index.regClass() == v1);
4007 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
4008 }
4009
4010 Temp final_sample;
4011 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
4012 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
4013 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
4014 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
4015 else
4016 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
4017
4018 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4019 * resource descriptor is 0 (invalid),
4020 */
4021 Temp compare = bld.tmp(bld.lm);
4022 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
4023 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
4024
4025 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
4026
4027 /* Replace the MSAA sample index. */
4028 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
4029 }
4030
4031 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
4032 {
4033
4034 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
4035 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4036 bool is_array = glsl_sampler_type_is_array(type);
4037 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4038 assert(!add_frag_pos && "Input attachments should be lowered.");
4039 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4040 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
4041 int count = image_type_to_components_count(dim, is_array);
4042 std::vector<Operand> coords(count);
4043
4044 if (is_ms) {
4045 Operand sample_index;
4046 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
4047 if (sample_cv)
4048 sample_index = Operand(sample_cv->u32);
4049 else
4050 sample_index = Operand(emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[2].ssa), 0, v1));
4051
4052 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
4053 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, is_array ? 3 : 2, 1)};
4054 for (unsigned i = 0; i < vec->operands.size(); i++)
4055 vec->operands[i] = Operand(emit_extract_vector(ctx, src0, i, v1));
4056 Temp fmask_load_address = {ctx->program->allocateId(), is_array ? v3 : v2};
4057 vec->definitions[0] = Definition(fmask_load_address);
4058 ctx->block->instructions.emplace_back(std::move(vec));
4059
4060 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
4061 sample_index = Operand(adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr));
4062 }
4063 count--;
4064 coords[count] = sample_index;
4065 }
4066
4067 if (count == 1 && !gfx9_1d)
4068 return emit_extract_vector(ctx, src0, 0, v1);
4069
4070 if (gfx9_1d) {
4071 coords[0] = Operand(emit_extract_vector(ctx, src0, 0, v1));
4072 coords.resize(coords.size() + 1);
4073 coords[1] = Operand((uint32_t) 0);
4074 if (is_array)
4075 coords[2] = Operand(emit_extract_vector(ctx, src0, 1, v1));
4076 } else {
4077 for (int i = 0; i < count; i++)
4078 coords[i] = Operand(emit_extract_vector(ctx, src0, i, v1));
4079 }
4080
4081 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
4082 for (unsigned i = 0; i < coords.size(); i++)
4083 vec->operands[i] = coords[i];
4084 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
4085 vec->definitions[0] = Definition(res);
4086 ctx->block->instructions.emplace_back(std::move(vec));
4087 return res;
4088 }
4089
4090
4091 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
4092 {
4093 Builder bld(ctx->program, ctx->block);
4094 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4095 const struct glsl_type *type = glsl_without_array(var->type);
4096 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4097 bool is_array = glsl_sampler_type_is_array(type);
4098 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4099
4100 if (dim == GLSL_SAMPLER_DIM_BUF) {
4101 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
4102 unsigned num_channels = util_last_bit(mask);
4103 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4104 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4105
4106 aco_opcode opcode;
4107 switch (num_channels) {
4108 case 1:
4109 opcode = aco_opcode::buffer_load_format_x;
4110 break;
4111 case 2:
4112 opcode = aco_opcode::buffer_load_format_xy;
4113 break;
4114 case 3:
4115 opcode = aco_opcode::buffer_load_format_xyz;
4116 break;
4117 case 4:
4118 opcode = aco_opcode::buffer_load_format_xyzw;
4119 break;
4120 default:
4121 unreachable(">4 channel buffer image load");
4122 }
4123 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
4124 load->operands[0] = Operand(vindex);
4125 load->operands[1] = Operand(rsrc);
4126 load->operands[2] = Operand((uint32_t) 0);
4127 Temp tmp;
4128 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4129 tmp = dst;
4130 else
4131 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
4132 load->definitions[0] = Definition(tmp);
4133 load->idxen = true;
4134 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
4135 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4136 load->barrier = barrier_image;
4137 ctx->block->instructions.emplace_back(std::move(load));
4138
4139 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
4140 return;
4141 }
4142
4143 Temp coords = get_image_coords(ctx, instr, type);
4144 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4145
4146 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
4147 unsigned num_components = util_bitcount(dmask);
4148 Temp tmp;
4149 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4150 tmp = dst;
4151 else
4152 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
4153
4154 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 2, 1)};
4155 load->operands[0] = Operand(coords);
4156 load->operands[1] = Operand(resource);
4157 load->definitions[0] = Definition(tmp);
4158 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
4159 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4160 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4161 load->dmask = dmask;
4162 load->unrm = true;
4163 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4164 load->barrier = barrier_image;
4165 ctx->block->instructions.emplace_back(std::move(load));
4166
4167 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
4168 return;
4169 }
4170
4171 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
4172 {
4173 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4174 const struct glsl_type *type = glsl_without_array(var->type);
4175 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4176 bool is_array = glsl_sampler_type_is_array(type);
4177 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4178
4179 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
4180
4181 if (dim == GLSL_SAMPLER_DIM_BUF) {
4182 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4183 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4184 aco_opcode opcode;
4185 switch (data.size()) {
4186 case 1:
4187 opcode = aco_opcode::buffer_store_format_x;
4188 break;
4189 case 2:
4190 opcode = aco_opcode::buffer_store_format_xy;
4191 break;
4192 case 3:
4193 opcode = aco_opcode::buffer_store_format_xyz;
4194 break;
4195 case 4:
4196 opcode = aco_opcode::buffer_store_format_xyzw;
4197 break;
4198 default:
4199 unreachable(">4 channel buffer image store");
4200 }
4201 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
4202 store->operands[0] = Operand(vindex);
4203 store->operands[1] = Operand(rsrc);
4204 store->operands[2] = Operand((uint32_t) 0);
4205 store->operands[3] = Operand(data);
4206 store->idxen = true;
4207 store->glc = glc;
4208 store->dlc = false;
4209 store->disable_wqm = true;
4210 store->barrier = barrier_image;
4211 ctx->program->needs_exact = true;
4212 ctx->block->instructions.emplace_back(std::move(store));
4213 return;
4214 }
4215
4216 assert(data.type() == RegType::vgpr);
4217 Temp coords = get_image_coords(ctx, instr, type);
4218 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4219
4220 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(aco_opcode::image_store, Format::MIMG, 4, 0)};
4221 store->operands[0] = Operand(coords);
4222 store->operands[1] = Operand(resource);
4223 store->operands[2] = Operand(s4);
4224 store->operands[3] = Operand(data);
4225 store->glc = glc;
4226 store->dlc = false;
4227 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4228 store->dmask = (1 << data.size()) - 1;
4229 store->unrm = true;
4230 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4231 store->disable_wqm = true;
4232 store->barrier = barrier_image;
4233 ctx->program->needs_exact = true;
4234 ctx->block->instructions.emplace_back(std::move(store));
4235 return;
4236 }
4237
4238 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4239 {
4240 /* return the previous value if dest is ever used */
4241 bool return_previous = false;
4242 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4243 return_previous = true;
4244 break;
4245 }
4246 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4247 return_previous = true;
4248 break;
4249 }
4250
4251 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4252 const struct glsl_type *type = glsl_without_array(var->type);
4253 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4254 bool is_array = glsl_sampler_type_is_array(type);
4255 Builder bld(ctx->program, ctx->block);
4256
4257 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4258 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
4259
4260 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
4261 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
4262
4263 aco_opcode buf_op, image_op;
4264 switch (instr->intrinsic) {
4265 case nir_intrinsic_image_deref_atomic_add:
4266 buf_op = aco_opcode::buffer_atomic_add;
4267 image_op = aco_opcode::image_atomic_add;
4268 break;
4269 case nir_intrinsic_image_deref_atomic_umin:
4270 buf_op = aco_opcode::buffer_atomic_umin;
4271 image_op = aco_opcode::image_atomic_umin;
4272 break;
4273 case nir_intrinsic_image_deref_atomic_imin:
4274 buf_op = aco_opcode::buffer_atomic_smin;
4275 image_op = aco_opcode::image_atomic_smin;
4276 break;
4277 case nir_intrinsic_image_deref_atomic_umax:
4278 buf_op = aco_opcode::buffer_atomic_umax;
4279 image_op = aco_opcode::image_atomic_umax;
4280 break;
4281 case nir_intrinsic_image_deref_atomic_imax:
4282 buf_op = aco_opcode::buffer_atomic_smax;
4283 image_op = aco_opcode::image_atomic_smax;
4284 break;
4285 case nir_intrinsic_image_deref_atomic_and:
4286 buf_op = aco_opcode::buffer_atomic_and;
4287 image_op = aco_opcode::image_atomic_and;
4288 break;
4289 case nir_intrinsic_image_deref_atomic_or:
4290 buf_op = aco_opcode::buffer_atomic_or;
4291 image_op = aco_opcode::image_atomic_or;
4292 break;
4293 case nir_intrinsic_image_deref_atomic_xor:
4294 buf_op = aco_opcode::buffer_atomic_xor;
4295 image_op = aco_opcode::image_atomic_xor;
4296 break;
4297 case nir_intrinsic_image_deref_atomic_exchange:
4298 buf_op = aco_opcode::buffer_atomic_swap;
4299 image_op = aco_opcode::image_atomic_swap;
4300 break;
4301 case nir_intrinsic_image_deref_atomic_comp_swap:
4302 buf_op = aco_opcode::buffer_atomic_cmpswap;
4303 image_op = aco_opcode::image_atomic_cmpswap;
4304 break;
4305 default:
4306 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4307 }
4308
4309 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4310
4311 if (dim == GLSL_SAMPLER_DIM_BUF) {
4312 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4313 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4314 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4315 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
4316 mubuf->operands[0] = Operand(vindex);
4317 mubuf->operands[1] = Operand(resource);
4318 mubuf->operands[2] = Operand((uint32_t)0);
4319 mubuf->operands[3] = Operand(data);
4320 if (return_previous)
4321 mubuf->definitions[0] = Definition(dst);
4322 mubuf->offset = 0;
4323 mubuf->idxen = true;
4324 mubuf->glc = return_previous;
4325 mubuf->dlc = false; /* Not needed for atomics */
4326 mubuf->disable_wqm = true;
4327 mubuf->barrier = barrier_image;
4328 ctx->program->needs_exact = true;
4329 ctx->block->instructions.emplace_back(std::move(mubuf));
4330 return;
4331 }
4332
4333 Temp coords = get_image_coords(ctx, instr, type);
4334 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4335 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 4, return_previous ? 1 : 0)};
4336 mimg->operands[0] = Operand(coords);
4337 mimg->operands[1] = Operand(resource);
4338 mimg->operands[2] = Operand(s4); /* no sampler */
4339 mimg->operands[3] = Operand(data);
4340 if (return_previous)
4341 mimg->definitions[0] = Definition(dst);
4342 mimg->glc = return_previous;
4343 mimg->dlc = false; /* Not needed for atomics */
4344 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4345 mimg->dmask = (1 << data.size()) - 1;
4346 mimg->unrm = true;
4347 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4348 mimg->disable_wqm = true;
4349 mimg->barrier = barrier_image;
4350 ctx->program->needs_exact = true;
4351 ctx->block->instructions.emplace_back(std::move(mimg));
4352 return;
4353 }
4354
4355 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
4356 {
4357 if (in_elements && ctx->options->chip_class == GFX8) {
4358 Builder bld(ctx->program, ctx->block);
4359
4360 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
4361 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
4362 stride = bld.vop1(aco_opcode::v_cvt_f32_ubyte0, bld.def(v1), stride);
4363 stride = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), stride);
4364
4365 Temp size = emit_extract_vector(ctx, desc, 2, s1);
4366 size = bld.vop1(aco_opcode::v_cvt_f32_u32, bld.def(v1), size);
4367
4368 Temp res = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), size, stride);
4369 res = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), res);
4370 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
4371
4372 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4373 /* idea
4374 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4375 * in case 12 (or 3?), we have to divide by 3:
4376 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4377 * use v_mul_hi_u32 with magic number to divide
4378 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4379 * disable v_skip
4380 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4381 */
4382
4383 } else {
4384 emit_extract_vector(ctx, desc, 2, dst);
4385 }
4386 }
4387
4388 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
4389 {
4390 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4391 const struct glsl_type *type = glsl_without_array(var->type);
4392 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4393 bool is_array = glsl_sampler_type_is_array(type);
4394 Builder bld(ctx->program, ctx->block);
4395
4396 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
4397 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
4398 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
4399 }
4400
4401 /* LOD */
4402 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
4403
4404 /* Resource */
4405 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
4406
4407 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4408
4409 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 2, 1)};
4410 mimg->operands[0] = Operand(lod);
4411 mimg->operands[1] = Operand(resource);
4412 unsigned& dmask = mimg->dmask;
4413 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4414 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
4415 mimg->da = glsl_sampler_type_is_array(type);
4416 mimg->can_reorder = true;
4417 Definition& def = mimg->definitions[0];
4418 ctx->block->instructions.emplace_back(std::move(mimg));
4419
4420 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
4421 glsl_sampler_type_is_array(type)) {
4422
4423 assert(instr->dest.ssa.num_components == 3);
4424 Temp tmp = {ctx->program->allocateId(), v3};
4425 def = Definition(tmp);
4426 emit_split_vector(ctx, tmp, 3);
4427
4428 /* divide 3rd value by 6 by multiplying with magic number */
4429 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
4430 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
4431
4432 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4433 emit_extract_vector(ctx, tmp, 0, v1),
4434 emit_extract_vector(ctx, tmp, 1, v1),
4435 by_6);
4436
4437 } else if (ctx->options->chip_class == GFX9 &&
4438 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
4439 glsl_sampler_type_is_array(type)) {
4440 assert(instr->dest.ssa.num_components == 2);
4441 def = Definition(dst);
4442 dmask = 0x5;
4443 } else {
4444 def = Definition(dst);
4445 }
4446
4447 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4448 }
4449
4450 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4451 {
4452 Builder bld(ctx->program, ctx->block);
4453 unsigned num_components = instr->num_components;
4454
4455 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4456 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4457 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4458
4459 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
4460 load_buffer(ctx, num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), glc, false);
4461 }
4462
4463 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4464 {
4465 Builder bld(ctx->program, ctx->block);
4466 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
4467 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4468 unsigned writemask = nir_intrinsic_write_mask(instr);
4469 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
4470
4471 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4472 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4473
4474 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
4475 ctx->options->chip_class >= GFX8;
4476 if (smem)
4477 offset = bld.as_uniform(offset);
4478 bool smem_nonfs = smem && ctx->stage != fragment_fs;
4479
4480 while (writemask) {
4481 int start, count;
4482 u_bit_scan_consecutive_range(&writemask, &start, &count);
4483 if (count == 3 && smem) {
4484 writemask |= 1u << (start + 2);
4485 count = 2;
4486 }
4487 int num_bytes = count * elem_size_bytes;
4488
4489 if (num_bytes > 16) {
4490 assert(elem_size_bytes == 8);
4491 writemask |= (((count - 2) << 1) - 1) << (start + 2);
4492 count = 2;
4493 num_bytes = 16;
4494 }
4495
4496 // TODO: check alignment of sub-dword stores
4497 // TODO: split 3 bytes. there is no store instruction for that
4498
4499 Temp write_data;
4500 if (count != instr->num_components) {
4501 emit_split_vector(ctx, data, instr->num_components);
4502 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4503 for (int i = 0; i < count; i++) {
4504 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
4505 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
4506 }
4507 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
4508 vec->definitions[0] = Definition(write_data);
4509 ctx->block->instructions.emplace_back(std::move(vec));
4510 } else if (!smem && data.type() != RegType::vgpr) {
4511 assert(num_bytes % 4 == 0);
4512 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
4513 } else if (smem_nonfs && data.type() == RegType::vgpr) {
4514 assert(num_bytes % 4 == 0);
4515 write_data = bld.as_uniform(data);
4516 } else {
4517 write_data = data;
4518 }
4519
4520 aco_opcode vmem_op, smem_op;
4521 switch (num_bytes) {
4522 case 4:
4523 vmem_op = aco_opcode::buffer_store_dword;
4524 smem_op = aco_opcode::s_buffer_store_dword;
4525 break;
4526 case 8:
4527 vmem_op = aco_opcode::buffer_store_dwordx2;
4528 smem_op = aco_opcode::s_buffer_store_dwordx2;
4529 break;
4530 case 12:
4531 vmem_op = aco_opcode::buffer_store_dwordx3;
4532 smem_op = aco_opcode::last_opcode;
4533 assert(!smem);
4534 break;
4535 case 16:
4536 vmem_op = aco_opcode::buffer_store_dwordx4;
4537 smem_op = aco_opcode::s_buffer_store_dwordx4;
4538 break;
4539 default:
4540 unreachable("Store SSBO not implemented for this size.");
4541 }
4542 if (ctx->stage == fragment_fs)
4543 smem_op = aco_opcode::p_fs_buffer_store_smem;
4544
4545 if (smem) {
4546 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
4547 store->operands[0] = Operand(rsrc);
4548 if (start) {
4549 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
4550 offset, Operand(start * elem_size_bytes));
4551 store->operands[1] = Operand(off);
4552 } else {
4553 store->operands[1] = Operand(offset);
4554 }
4555 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
4556 store->operands[1].setFixed(m0);
4557 store->operands[2] = Operand(write_data);
4558 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
4559 store->dlc = false;
4560 store->disable_wqm = true;
4561 store->barrier = barrier_buffer;
4562 ctx->block->instructions.emplace_back(std::move(store));
4563 ctx->program->wb_smem_l1_on_end = true;
4564 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
4565 ctx->block->kind |= block_kind_needs_lowering;
4566 ctx->program->needs_exact = true;
4567 }
4568 } else {
4569 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
4570 store->operands[0] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4571 store->operands[1] = Operand(rsrc);
4572 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4573 store->operands[3] = Operand(write_data);
4574 store->offset = start * elem_size_bytes;
4575 store->offen = (offset.type() == RegType::vgpr);
4576 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
4577 store->dlc = false;
4578 store->disable_wqm = true;
4579 store->barrier = barrier_buffer;
4580 ctx->program->needs_exact = true;
4581 ctx->block->instructions.emplace_back(std::move(store));
4582 }
4583 }
4584 }
4585
4586 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4587 {
4588 /* return the previous value if dest is ever used */
4589 bool return_previous = false;
4590 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4591 return_previous = true;
4592 break;
4593 }
4594 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4595 return_previous = true;
4596 break;
4597 }
4598
4599 Builder bld(ctx->program, ctx->block);
4600 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
4601
4602 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
4603 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
4604 get_ssa_temp(ctx, instr->src[3].ssa), data);
4605
4606 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
4607 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4608 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4609
4610 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4611
4612 aco_opcode op32, op64;
4613 switch (instr->intrinsic) {
4614 case nir_intrinsic_ssbo_atomic_add:
4615 op32 = aco_opcode::buffer_atomic_add;
4616 op64 = aco_opcode::buffer_atomic_add_x2;
4617 break;
4618 case nir_intrinsic_ssbo_atomic_imin:
4619 op32 = aco_opcode::buffer_atomic_smin;
4620 op64 = aco_opcode::buffer_atomic_smin_x2;
4621 break;
4622 case nir_intrinsic_ssbo_atomic_umin:
4623 op32 = aco_opcode::buffer_atomic_umin;
4624 op64 = aco_opcode::buffer_atomic_umin_x2;
4625 break;
4626 case nir_intrinsic_ssbo_atomic_imax:
4627 op32 = aco_opcode::buffer_atomic_smax;
4628 op64 = aco_opcode::buffer_atomic_smax_x2;
4629 break;
4630 case nir_intrinsic_ssbo_atomic_umax:
4631 op32 = aco_opcode::buffer_atomic_umax;
4632 op64 = aco_opcode::buffer_atomic_umax_x2;
4633 break;
4634 case nir_intrinsic_ssbo_atomic_and:
4635 op32 = aco_opcode::buffer_atomic_and;
4636 op64 = aco_opcode::buffer_atomic_and_x2;
4637 break;
4638 case nir_intrinsic_ssbo_atomic_or:
4639 op32 = aco_opcode::buffer_atomic_or;
4640 op64 = aco_opcode::buffer_atomic_or_x2;
4641 break;
4642 case nir_intrinsic_ssbo_atomic_xor:
4643 op32 = aco_opcode::buffer_atomic_xor;
4644 op64 = aco_opcode::buffer_atomic_xor_x2;
4645 break;
4646 case nir_intrinsic_ssbo_atomic_exchange:
4647 op32 = aco_opcode::buffer_atomic_swap;
4648 op64 = aco_opcode::buffer_atomic_swap_x2;
4649 break;
4650 case nir_intrinsic_ssbo_atomic_comp_swap:
4651 op32 = aco_opcode::buffer_atomic_cmpswap;
4652 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
4653 break;
4654 default:
4655 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4656 }
4657 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
4658 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
4659 mubuf->operands[0] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4660 mubuf->operands[1] = Operand(rsrc);
4661 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4662 mubuf->operands[3] = Operand(data);
4663 if (return_previous)
4664 mubuf->definitions[0] = Definition(dst);
4665 mubuf->offset = 0;
4666 mubuf->offen = (offset.type() == RegType::vgpr);
4667 mubuf->glc = return_previous;
4668 mubuf->dlc = false; /* Not needed for atomics */
4669 mubuf->disable_wqm = true;
4670 mubuf->barrier = barrier_buffer;
4671 ctx->program->needs_exact = true;
4672 ctx->block->instructions.emplace_back(std::move(mubuf));
4673 }
4674
4675 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
4676
4677 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4678 Builder bld(ctx->program, ctx->block);
4679 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
4680 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
4681 }
4682
4683 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
4684 {
4685 Builder bld(ctx->program, ctx->block);
4686 unsigned num_components = instr->num_components;
4687 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
4688
4689 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4690 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
4691
4692 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
4693 bool dlc = glc && ctx->options->chip_class >= GFX10;
4694 aco_opcode op;
4695 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8)) {
4696 bool global = ctx->options->chip_class >= GFX9;
4697 aco_opcode op;
4698 switch (num_bytes) {
4699 case 4:
4700 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
4701 break;
4702 case 8:
4703 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
4704 break;
4705 case 12:
4706 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
4707 break;
4708 case 16:
4709 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
4710 break;
4711 default:
4712 unreachable("load_global not implemented for this size.");
4713 }
4714 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
4715 flat->operands[0] = Operand(addr);
4716 flat->operands[1] = Operand(s1);
4717 flat->glc = glc;
4718 flat->dlc = dlc;
4719 flat->barrier = barrier_buffer;
4720
4721 if (dst.type() == RegType::sgpr) {
4722 Temp vec = bld.tmp(RegType::vgpr, dst.size());
4723 flat->definitions[0] = Definition(vec);
4724 ctx->block->instructions.emplace_back(std::move(flat));
4725 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
4726 } else {
4727 flat->definitions[0] = Definition(dst);
4728 ctx->block->instructions.emplace_back(std::move(flat));
4729 }
4730 emit_split_vector(ctx, dst, num_components);
4731 } else {
4732 switch (num_bytes) {
4733 case 4:
4734 op = aco_opcode::s_load_dword;
4735 break;
4736 case 8:
4737 op = aco_opcode::s_load_dwordx2;
4738 break;
4739 case 12:
4740 case 16:
4741 op = aco_opcode::s_load_dwordx4;
4742 break;
4743 default:
4744 unreachable("load_global not implemented for this size.");
4745 }
4746 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
4747 load->operands[0] = Operand(addr);
4748 load->operands[1] = Operand(0u);
4749 load->definitions[0] = Definition(dst);
4750 load->glc = glc;
4751 load->dlc = dlc;
4752 load->barrier = barrier_buffer;
4753 assert(ctx->options->chip_class >= GFX8 || !glc);
4754
4755 if (dst.size() == 3) {
4756 /* trim vector */
4757 Temp vec = bld.tmp(s4);
4758 load->definitions[0] = Definition(vec);
4759 ctx->block->instructions.emplace_back(std::move(load));
4760 emit_split_vector(ctx, vec, 4);
4761
4762 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4763 emit_extract_vector(ctx, vec, 0, s1),
4764 emit_extract_vector(ctx, vec, 1, s1),
4765 emit_extract_vector(ctx, vec, 2, s1));
4766 } else {
4767 ctx->block->instructions.emplace_back(std::move(load));
4768 }
4769 }
4770 }
4771
4772 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
4773 {
4774 Builder bld(ctx->program, ctx->block);
4775 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4776
4777 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4778 Temp addr = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4779
4780 unsigned writemask = nir_intrinsic_write_mask(instr);
4781 while (writemask) {
4782 int start, count;
4783 u_bit_scan_consecutive_range(&writemask, &start, &count);
4784 unsigned num_bytes = count * elem_size_bytes;
4785
4786 Temp write_data = data;
4787 if (count != instr->num_components) {
4788 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4789 for (int i = 0; i < count; i++)
4790 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
4791 write_data = bld.tmp(RegType::vgpr, count);
4792 vec->definitions[0] = Definition(write_data);
4793 ctx->block->instructions.emplace_back(std::move(vec));
4794 }
4795
4796 unsigned offset = start * elem_size_bytes;
4797 if (offset > 0 && ctx->options->chip_class < GFX9) {
4798 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
4799 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
4800 Temp carry = bld.tmp(bld.lm);
4801 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
4802
4803 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
4804 Operand(offset), addr0);
4805 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
4806 Operand(0u), addr1,
4807 carry).def(1).setHint(vcc);
4808
4809 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
4810
4811 offset = 0;
4812 }
4813
4814 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
4815 bool global = ctx->options->chip_class >= GFX9;
4816 aco_opcode op;
4817 switch (num_bytes) {
4818 case 4:
4819 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
4820 break;
4821 case 8:
4822 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
4823 break;
4824 case 12:
4825 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
4826 break;
4827 case 16:
4828 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
4829 break;
4830 default:
4831 unreachable("store_global not implemented for this size.");
4832 }
4833 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
4834 flat->operands[0] = Operand(addr);
4835 flat->operands[1] = Operand(s1);
4836 flat->operands[2] = Operand(data);
4837 flat->glc = glc;
4838 flat->dlc = false;
4839 flat->offset = offset;
4840 flat->disable_wqm = true;
4841 flat->barrier = barrier_buffer;
4842 ctx->program->needs_exact = true;
4843 ctx->block->instructions.emplace_back(std::move(flat));
4844 }
4845 }
4846
4847 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4848 {
4849 /* return the previous value if dest is ever used */
4850 bool return_previous = false;
4851 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4852 return_previous = true;
4853 break;
4854 }
4855 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4856 return_previous = true;
4857 break;
4858 }
4859
4860 Builder bld(ctx->program, ctx->block);
4861 Temp addr = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4862 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4863
4864 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
4865 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
4866 get_ssa_temp(ctx, instr->src[2].ssa), data);
4867
4868 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4869
4870 bool global = ctx->options->chip_class >= GFX9;
4871 aco_opcode op32, op64;
4872 switch (instr->intrinsic) {
4873 case nir_intrinsic_global_atomic_add:
4874 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
4875 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
4876 break;
4877 case nir_intrinsic_global_atomic_imin:
4878 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
4879 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
4880 break;
4881 case nir_intrinsic_global_atomic_umin:
4882 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
4883 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
4884 break;
4885 case nir_intrinsic_global_atomic_imax:
4886 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
4887 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
4888 break;
4889 case nir_intrinsic_global_atomic_umax:
4890 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
4891 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
4892 break;
4893 case nir_intrinsic_global_atomic_and:
4894 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
4895 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
4896 break;
4897 case nir_intrinsic_global_atomic_or:
4898 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
4899 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
4900 break;
4901 case nir_intrinsic_global_atomic_xor:
4902 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
4903 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
4904 break;
4905 case nir_intrinsic_global_atomic_exchange:
4906 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
4907 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
4908 break;
4909 case nir_intrinsic_global_atomic_comp_swap:
4910 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
4911 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
4912 break;
4913 default:
4914 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
4915 }
4916 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
4917 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
4918 flat->operands[0] = Operand(addr);
4919 flat->operands[1] = Operand(s1);
4920 flat->operands[2] = Operand(data);
4921 if (return_previous)
4922 flat->definitions[0] = Definition(dst);
4923 flat->glc = return_previous;
4924 flat->dlc = false; /* Not needed for atomics */
4925 flat->offset = 0;
4926 flat->disable_wqm = true;
4927 flat->barrier = barrier_buffer;
4928 ctx->program->needs_exact = true;
4929 ctx->block->instructions.emplace_back(std::move(flat));
4930 }
4931
4932 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
4933 Builder bld(ctx->program, ctx->block);
4934 switch(instr->intrinsic) {
4935 case nir_intrinsic_group_memory_barrier:
4936 case nir_intrinsic_memory_barrier:
4937 bld.barrier(aco_opcode::p_memory_barrier_all);
4938 break;
4939 case nir_intrinsic_memory_barrier_atomic_counter:
4940 bld.barrier(aco_opcode::p_memory_barrier_atomic);
4941 break;
4942 case nir_intrinsic_memory_barrier_buffer:
4943 bld.barrier(aco_opcode::p_memory_barrier_buffer);
4944 break;
4945 case nir_intrinsic_memory_barrier_image:
4946 bld.barrier(aco_opcode::p_memory_barrier_image);
4947 break;
4948 case nir_intrinsic_memory_barrier_shared:
4949 bld.barrier(aco_opcode::p_memory_barrier_shared);
4950 break;
4951 default:
4952 unreachable("Unimplemented memory barrier intrinsic");
4953 break;
4954 }
4955 }
4956
4957 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
4958 {
4959 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4960 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4961 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
4962 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4963 Builder bld(ctx->program, ctx->block);
4964
4965 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4966 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
4967 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
4968 }
4969
4970 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
4971 {
4972 unsigned writemask = nir_intrinsic_write_mask(instr);
4973 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
4974 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4975 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4976 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
4977
4978 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
4979 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
4980 }
4981
4982 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4983 {
4984 unsigned offset = nir_intrinsic_base(instr);
4985 Operand m = load_lds_size_m0(ctx);
4986 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4987 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4988
4989 unsigned num_operands = 3;
4990 aco_opcode op32, op64, op32_rtn, op64_rtn;
4991 switch(instr->intrinsic) {
4992 case nir_intrinsic_shared_atomic_add:
4993 op32 = aco_opcode::ds_add_u32;
4994 op64 = aco_opcode::ds_add_u64;
4995 op32_rtn = aco_opcode::ds_add_rtn_u32;
4996 op64_rtn = aco_opcode::ds_add_rtn_u64;
4997 break;
4998 case nir_intrinsic_shared_atomic_imin:
4999 op32 = aco_opcode::ds_min_i32;
5000 op64 = aco_opcode::ds_min_i64;
5001 op32_rtn = aco_opcode::ds_min_rtn_i32;
5002 op64_rtn = aco_opcode::ds_min_rtn_i64;
5003 break;
5004 case nir_intrinsic_shared_atomic_umin:
5005 op32 = aco_opcode::ds_min_u32;
5006 op64 = aco_opcode::ds_min_u64;
5007 op32_rtn = aco_opcode::ds_min_rtn_u32;
5008 op64_rtn = aco_opcode::ds_min_rtn_u64;
5009 break;
5010 case nir_intrinsic_shared_atomic_imax:
5011 op32 = aco_opcode::ds_max_i32;
5012 op64 = aco_opcode::ds_max_i64;
5013 op32_rtn = aco_opcode::ds_max_rtn_i32;
5014 op64_rtn = aco_opcode::ds_max_rtn_i64;
5015 break;
5016 case nir_intrinsic_shared_atomic_umax:
5017 op32 = aco_opcode::ds_max_u32;
5018 op64 = aco_opcode::ds_max_u64;
5019 op32_rtn = aco_opcode::ds_max_rtn_u32;
5020 op64_rtn = aco_opcode::ds_max_rtn_u64;
5021 break;
5022 case nir_intrinsic_shared_atomic_and:
5023 op32 = aco_opcode::ds_and_b32;
5024 op64 = aco_opcode::ds_and_b64;
5025 op32_rtn = aco_opcode::ds_and_rtn_b32;
5026 op64_rtn = aco_opcode::ds_and_rtn_b64;
5027 break;
5028 case nir_intrinsic_shared_atomic_or:
5029 op32 = aco_opcode::ds_or_b32;
5030 op64 = aco_opcode::ds_or_b64;
5031 op32_rtn = aco_opcode::ds_or_rtn_b32;
5032 op64_rtn = aco_opcode::ds_or_rtn_b64;
5033 break;
5034 case nir_intrinsic_shared_atomic_xor:
5035 op32 = aco_opcode::ds_xor_b32;
5036 op64 = aco_opcode::ds_xor_b64;
5037 op32_rtn = aco_opcode::ds_xor_rtn_b32;
5038 op64_rtn = aco_opcode::ds_xor_rtn_b64;
5039 break;
5040 case nir_intrinsic_shared_atomic_exchange:
5041 op32 = aco_opcode::ds_write_b32;
5042 op64 = aco_opcode::ds_write_b64;
5043 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
5044 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
5045 break;
5046 case nir_intrinsic_shared_atomic_comp_swap:
5047 op32 = aco_opcode::ds_cmpst_b32;
5048 op64 = aco_opcode::ds_cmpst_b64;
5049 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
5050 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
5051 num_operands = 4;
5052 break;
5053 default:
5054 unreachable("Unhandled shared atomic intrinsic");
5055 }
5056
5057 /* return the previous value if dest is ever used */
5058 bool return_previous = false;
5059 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5060 return_previous = true;
5061 break;
5062 }
5063 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5064 return_previous = true;
5065 break;
5066 }
5067
5068 aco_opcode op;
5069 if (data.size() == 1) {
5070 assert(instr->dest.ssa.bit_size == 32);
5071 op = return_previous ? op32_rtn : op32;
5072 } else {
5073 assert(instr->dest.ssa.bit_size == 64);
5074 op = return_previous ? op64_rtn : op64;
5075 }
5076
5077 if (offset > 65535) {
5078 Builder bld(ctx->program, ctx->block);
5079 address = bld.vadd32(bld.def(v1), Operand(offset), address);
5080 offset = 0;
5081 }
5082
5083 aco_ptr<DS_instruction> ds;
5084 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
5085 ds->operands[0] = Operand(address);
5086 ds->operands[1] = Operand(data);
5087 if (num_operands == 4)
5088 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
5089 ds->operands[num_operands - 1] = m;
5090 ds->offset0 = offset;
5091 if (return_previous)
5092 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
5093 ctx->block->instructions.emplace_back(std::move(ds));
5094 }
5095
5096 Temp get_scratch_resource(isel_context *ctx)
5097 {
5098 Builder bld(ctx->program, ctx->block);
5099 Temp scratch_addr = ctx->program->private_segment_buffer;
5100 if (ctx->stage != compute_cs)
5101 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
5102
5103 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
5104 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
5105
5106 if (ctx->program->chip_class >= GFX10) {
5107 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5108 S_008F0C_OOB_SELECT(3) |
5109 S_008F0C_RESOURCE_LEVEL(1);
5110 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
5111 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5112 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5113 }
5114
5115 /* older generations need element size = 16 bytes. element size removed in GFX9 */
5116 if (ctx->program->chip_class <= GFX8)
5117 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
5118
5119 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
5120 }
5121
5122 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
5123 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
5124 Builder bld(ctx->program, ctx->block);
5125 Temp rsrc = get_scratch_resource(ctx);
5126 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5127 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5128
5129 aco_opcode op;
5130 switch (dst.size()) {
5131 case 1:
5132 op = aco_opcode::buffer_load_dword;
5133 break;
5134 case 2:
5135 op = aco_opcode::buffer_load_dwordx2;
5136 break;
5137 case 3:
5138 op = aco_opcode::buffer_load_dwordx3;
5139 break;
5140 case 4:
5141 op = aco_opcode::buffer_load_dwordx4;
5142 break;
5143 case 6:
5144 case 8: {
5145 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5146 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
5147 bld.def(v4), offset, rsrc,
5148 ctx->program->scratch_offset, 0, true);
5149 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
5150 aco_opcode::buffer_load_dwordx4,
5151 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
5152 offset, rsrc, ctx->program->scratch_offset, 16, true);
5153 emit_split_vector(ctx, lower, 2);
5154 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
5155 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
5156 if (dst.size() == 8) {
5157 emit_split_vector(ctx, upper, 2);
5158 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
5159 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
5160 } else {
5161 elems[2] = upper;
5162 }
5163
5164 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
5165 Format::PSEUDO, dst.size() / 2, 1)};
5166 for (unsigned i = 0; i < dst.size() / 2; i++)
5167 vec->operands[i] = Operand(elems[i]);
5168 vec->definitions[0] = Definition(dst);
5169 bld.insert(std::move(vec));
5170 ctx->allocated_vec.emplace(dst.id(), elems);
5171 return;
5172 }
5173 default:
5174 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5175 }
5176
5177 bld.mubuf(op, Definition(dst), offset, rsrc, ctx->program->scratch_offset, 0, true);
5178 emit_split_vector(ctx, dst, instr->num_components);
5179 }
5180
5181 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
5182 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
5183 Builder bld(ctx->program, ctx->block);
5184 Temp rsrc = get_scratch_resource(ctx);
5185 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5186 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5187
5188 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5189 unsigned writemask = nir_intrinsic_write_mask(instr);
5190
5191 while (writemask) {
5192 int start, count;
5193 u_bit_scan_consecutive_range(&writemask, &start, &count);
5194 int num_bytes = count * elem_size_bytes;
5195
5196 if (num_bytes > 16) {
5197 assert(elem_size_bytes == 8);
5198 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5199 count = 2;
5200 num_bytes = 16;
5201 }
5202
5203 // TODO: check alignment of sub-dword stores
5204 // TODO: split 3 bytes. there is no store instruction for that
5205
5206 Temp write_data;
5207 if (count != instr->num_components) {
5208 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5209 for (int i = 0; i < count; i++) {
5210 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
5211 vec->operands[i] = Operand(elem);
5212 }
5213 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
5214 vec->definitions[0] = Definition(write_data);
5215 ctx->block->instructions.emplace_back(std::move(vec));
5216 } else {
5217 write_data = data;
5218 }
5219
5220 aco_opcode op;
5221 switch (num_bytes) {
5222 case 4:
5223 op = aco_opcode::buffer_store_dword;
5224 break;
5225 case 8:
5226 op = aco_opcode::buffer_store_dwordx2;
5227 break;
5228 case 12:
5229 op = aco_opcode::buffer_store_dwordx3;
5230 break;
5231 case 16:
5232 op = aco_opcode::buffer_store_dwordx4;
5233 break;
5234 default:
5235 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5236 }
5237
5238 bld.mubuf(op, offset, rsrc, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
5239 }
5240 }
5241
5242 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
5243 uint8_t log2_ps_iter_samples;
5244 if (ctx->program->info->ps.force_persample) {
5245 log2_ps_iter_samples =
5246 util_logbase2(ctx->options->key.fs.num_samples);
5247 } else {
5248 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
5249 }
5250
5251 /* The bit pattern matches that used by fixed function fragment
5252 * processing. */
5253 static const unsigned ps_iter_masks[] = {
5254 0xffff, /* not used */
5255 0x5555,
5256 0x1111,
5257 0x0101,
5258 0x0001,
5259 };
5260 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
5261
5262 Builder bld(ctx->program, ctx->block);
5263
5264 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5265 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
5266 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
5267 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
5268 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5269 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
5270 }
5271
5272 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
5273 {
5274 Builder bld(ctx->program, ctx->block);
5275
5276 if (cluster_size == 1) {
5277 return src;
5278 } if (op == nir_op_iand && cluster_size == 4) {
5279 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5280 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
5281 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
5282 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
5283 } else if (op == nir_op_ior && cluster_size == 4) {
5284 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5285 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
5286 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
5287 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
5288 //subgroupAnd(val) -> (exec & ~val) == 0
5289 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
5290 return bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(0u), Operand(-1u), bld.scc(tmp));
5291 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
5292 //subgroupOr(val) -> (val & exec) != 0
5293 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
5294 return bool_to_vector_condition(ctx, tmp);
5295 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
5296 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5297 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5298 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
5299 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
5300 return bool_to_vector_condition(ctx, tmp);
5301 } else {
5302 //subgroupClustered{And,Or,Xor}(val, n) ->
5303 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
5304 //cluster_offset = ~(n - 1) & lane_id
5305 //cluster_mask = ((1 << n) - 1)
5306 //subgroupClusteredAnd():
5307 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5308 //subgroupClusteredOr():
5309 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5310 //subgroupClusteredXor():
5311 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5312 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
5313 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
5314
5315 Temp tmp;
5316 if (op == nir_op_iand)
5317 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5318 else
5319 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5320
5321 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
5322
5323 if (ctx->program->chip_class <= GFX7)
5324 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
5325 else if (ctx->program->wave_size == 64)
5326 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
5327 else
5328 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
5329 tmp = emit_extract_vector(ctx, tmp, 0, v1);
5330 if (cluster_mask != 0xffffffff)
5331 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
5332
5333 Definition cmp_def = Definition();
5334 if (op == nir_op_iand) {
5335 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
5336 } else if (op == nir_op_ior) {
5337 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
5338 } else if (op == nir_op_ixor) {
5339 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
5340 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
5341 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
5342 }
5343 cmp_def.setHint(vcc);
5344 return cmp_def.getTemp();
5345 }
5346 }
5347
5348 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
5349 {
5350 Builder bld(ctx->program, ctx->block);
5351
5352 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5353 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5354 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5355 Temp tmp;
5356 if (op == nir_op_iand)
5357 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
5358 else
5359 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
5360
5361 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
5362 Temp lo = lohi.def(0).getTemp();
5363 Temp hi = lohi.def(1).getTemp();
5364 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
5365
5366 Definition cmp_def = Definition();
5367 if (op == nir_op_iand)
5368 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
5369 else if (op == nir_op_ior)
5370 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
5371 else if (op == nir_op_ixor)
5372 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
5373 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
5374 cmp_def.setHint(vcc);
5375 return cmp_def.getTemp();
5376 }
5377
5378 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
5379 {
5380 Builder bld(ctx->program, ctx->block);
5381
5382 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5383 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5384 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5385 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
5386 if (op == nir_op_iand)
5387 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
5388 else if (op == nir_op_ior)
5389 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
5390 else if (op == nir_op_ixor)
5391 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
5392
5393 assert(false);
5394 return Temp();
5395 }
5396
5397 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
5398 {
5399 Builder bld(ctx->program, ctx->block);
5400 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
5401 if (src.regClass().type() == RegType::vgpr) {
5402 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
5403 } else if (src.regClass() == s1) {
5404 bld.sop1(aco_opcode::s_mov_b32, dst, src);
5405 } else if (src.regClass() == s2) {
5406 bld.sop1(aco_opcode::s_mov_b64, dst, src);
5407 } else {
5408 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5409 nir_print_instr(&instr->instr, stderr);
5410 fprintf(stderr, "\n");
5411 }
5412 }
5413
5414 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
5415 {
5416 Builder bld(ctx->program, ctx->block);
5417 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
5418 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
5419 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
5420
5421 Temp ddx_1, ddx_2, ddy_1, ddy_2;
5422 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
5423 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
5424 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
5425
5426 /* Build DD X/Y */
5427 if (ctx->program->chip_class >= GFX8) {
5428 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
5429 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
5430 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
5431 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
5432 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
5433 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
5434 } else {
5435 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
5436 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
5437 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
5438 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
5439 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
5440 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
5441 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
5442 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
5443 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
5444 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
5445 }
5446
5447 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5448 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
5449 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
5450 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
5451 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
5452 Temp wqm1 = bld.tmp(v1);
5453 emit_wqm(ctx, tmp1, wqm1, true);
5454 Temp wqm2 = bld.tmp(v1);
5455 emit_wqm(ctx, tmp2, wqm2, true);
5456 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
5457 return;
5458 }
5459
5460 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
5461 {
5462 Builder bld(ctx->program, ctx->block);
5463 switch(instr->intrinsic) {
5464 case nir_intrinsic_load_barycentric_sample:
5465 case nir_intrinsic_load_barycentric_pixel:
5466 case nir_intrinsic_load_barycentric_centroid: {
5467 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
5468 Temp bary = Temp(0, s2);
5469 switch (mode) {
5470 case INTERP_MODE_SMOOTH:
5471 case INTERP_MODE_NONE:
5472 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
5473 bary = get_arg(ctx, ctx->args->ac.persp_center);
5474 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
5475 bary = ctx->persp_centroid;
5476 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
5477 bary = get_arg(ctx, ctx->args->ac.persp_sample);
5478 break;
5479 case INTERP_MODE_NOPERSPECTIVE:
5480 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
5481 bary = get_arg(ctx, ctx->args->ac.linear_center);
5482 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
5483 bary = ctx->linear_centroid;
5484 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
5485 bary = get_arg(ctx, ctx->args->ac.linear_sample);
5486 break;
5487 default:
5488 break;
5489 }
5490 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5491 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
5492 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
5493 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5494 Operand(p1), Operand(p2));
5495 emit_split_vector(ctx, dst, 2);
5496 break;
5497 }
5498 case nir_intrinsic_load_barycentric_at_sample: {
5499 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
5500 switch (ctx->options->key.fs.num_samples) {
5501 case 2: sample_pos_offset += 1 << 3; break;
5502 case 4: sample_pos_offset += 3 << 3; break;
5503 case 8: sample_pos_offset += 7 << 3; break;
5504 default: break;
5505 }
5506 Temp sample_pos;
5507 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
5508 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
5509 Temp private_segment_buffer = ctx->program->private_segment_buffer;
5510 if (addr.type() == RegType::sgpr) {
5511 Operand offset;
5512 if (const_addr) {
5513 sample_pos_offset += const_addr->u32 << 3;
5514 offset = Operand(sample_pos_offset);
5515 } else if (ctx->options->chip_class >= GFX9) {
5516 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
5517 } else {
5518 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
5519 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
5520 }
5521 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, Operand(offset));
5522
5523 } else if (ctx->options->chip_class >= GFX9) {
5524 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
5525 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
5526 } else {
5527 /* addr += private_segment_buffer + sample_pos_offset */
5528 Temp tmp0 = bld.tmp(s1);
5529 Temp tmp1 = bld.tmp(s1);
5530 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
5531 Definition scc_tmp = bld.def(s1, scc);
5532 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
5533 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
5534 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
5535 Temp pck0 = bld.tmp(v1);
5536 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
5537 tmp1 = as_vgpr(ctx, tmp1);
5538 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);
5539 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
5540
5541 /* sample_pos = flat_load_dwordx2 addr */
5542 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
5543 }
5544
5545 /* sample_pos -= 0.5 */
5546 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
5547 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
5548 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
5549 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
5550 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
5551
5552 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
5553 break;
5554 }
5555 case nir_intrinsic_load_barycentric_at_offset: {
5556 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5557 RegClass rc = RegClass(offset.type(), 1);
5558 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
5559 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
5560 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
5561 break;
5562 }
5563 case nir_intrinsic_load_front_face: {
5564 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5565 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
5566 break;
5567 }
5568 case nir_intrinsic_load_view_index:
5569 case nir_intrinsic_load_layer_id: {
5570 if (instr->intrinsic == nir_intrinsic_load_view_index && (ctx->stage & sw_vs)) {
5571 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5572 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
5573 break;
5574 }
5575
5576 unsigned idx = nir_intrinsic_base(instr);
5577 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5578 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
5579 break;
5580 }
5581 case nir_intrinsic_load_frag_coord: {
5582 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
5583 break;
5584 }
5585 case nir_intrinsic_load_sample_pos: {
5586 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
5587 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
5588 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5589 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
5590 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
5591 break;
5592 }
5593 case nir_intrinsic_load_interpolated_input:
5594 visit_load_interpolated_input(ctx, instr);
5595 break;
5596 case nir_intrinsic_store_output:
5597 visit_store_output(ctx, instr);
5598 break;
5599 case nir_intrinsic_load_input:
5600 visit_load_input(ctx, instr);
5601 break;
5602 case nir_intrinsic_load_ubo:
5603 visit_load_ubo(ctx, instr);
5604 break;
5605 case nir_intrinsic_load_push_constant:
5606 visit_load_push_constant(ctx, instr);
5607 break;
5608 case nir_intrinsic_load_constant:
5609 visit_load_constant(ctx, instr);
5610 break;
5611 case nir_intrinsic_vulkan_resource_index:
5612 visit_load_resource(ctx, instr);
5613 break;
5614 case nir_intrinsic_discard:
5615 visit_discard(ctx, instr);
5616 break;
5617 case nir_intrinsic_discard_if:
5618 visit_discard_if(ctx, instr);
5619 break;
5620 case nir_intrinsic_load_shared:
5621 visit_load_shared(ctx, instr);
5622 break;
5623 case nir_intrinsic_store_shared:
5624 visit_store_shared(ctx, instr);
5625 break;
5626 case nir_intrinsic_shared_atomic_add:
5627 case nir_intrinsic_shared_atomic_imin:
5628 case nir_intrinsic_shared_atomic_umin:
5629 case nir_intrinsic_shared_atomic_imax:
5630 case nir_intrinsic_shared_atomic_umax:
5631 case nir_intrinsic_shared_atomic_and:
5632 case nir_intrinsic_shared_atomic_or:
5633 case nir_intrinsic_shared_atomic_xor:
5634 case nir_intrinsic_shared_atomic_exchange:
5635 case nir_intrinsic_shared_atomic_comp_swap:
5636 visit_shared_atomic(ctx, instr);
5637 break;
5638 case nir_intrinsic_image_deref_load:
5639 visit_image_load(ctx, instr);
5640 break;
5641 case nir_intrinsic_image_deref_store:
5642 visit_image_store(ctx, instr);
5643 break;
5644 case nir_intrinsic_image_deref_atomic_add:
5645 case nir_intrinsic_image_deref_atomic_umin:
5646 case nir_intrinsic_image_deref_atomic_imin:
5647 case nir_intrinsic_image_deref_atomic_umax:
5648 case nir_intrinsic_image_deref_atomic_imax:
5649 case nir_intrinsic_image_deref_atomic_and:
5650 case nir_intrinsic_image_deref_atomic_or:
5651 case nir_intrinsic_image_deref_atomic_xor:
5652 case nir_intrinsic_image_deref_atomic_exchange:
5653 case nir_intrinsic_image_deref_atomic_comp_swap:
5654 visit_image_atomic(ctx, instr);
5655 break;
5656 case nir_intrinsic_image_deref_size:
5657 visit_image_size(ctx, instr);
5658 break;
5659 case nir_intrinsic_load_ssbo:
5660 visit_load_ssbo(ctx, instr);
5661 break;
5662 case nir_intrinsic_store_ssbo:
5663 visit_store_ssbo(ctx, instr);
5664 break;
5665 case nir_intrinsic_load_global:
5666 visit_load_global(ctx, instr);
5667 break;
5668 case nir_intrinsic_store_global:
5669 visit_store_global(ctx, instr);
5670 break;
5671 case nir_intrinsic_global_atomic_add:
5672 case nir_intrinsic_global_atomic_imin:
5673 case nir_intrinsic_global_atomic_umin:
5674 case nir_intrinsic_global_atomic_imax:
5675 case nir_intrinsic_global_atomic_umax:
5676 case nir_intrinsic_global_atomic_and:
5677 case nir_intrinsic_global_atomic_or:
5678 case nir_intrinsic_global_atomic_xor:
5679 case nir_intrinsic_global_atomic_exchange:
5680 case nir_intrinsic_global_atomic_comp_swap:
5681 visit_global_atomic(ctx, instr);
5682 break;
5683 case nir_intrinsic_ssbo_atomic_add:
5684 case nir_intrinsic_ssbo_atomic_imin:
5685 case nir_intrinsic_ssbo_atomic_umin:
5686 case nir_intrinsic_ssbo_atomic_imax:
5687 case nir_intrinsic_ssbo_atomic_umax:
5688 case nir_intrinsic_ssbo_atomic_and:
5689 case nir_intrinsic_ssbo_atomic_or:
5690 case nir_intrinsic_ssbo_atomic_xor:
5691 case nir_intrinsic_ssbo_atomic_exchange:
5692 case nir_intrinsic_ssbo_atomic_comp_swap:
5693 visit_atomic_ssbo(ctx, instr);
5694 break;
5695 case nir_intrinsic_load_scratch:
5696 visit_load_scratch(ctx, instr);
5697 break;
5698 case nir_intrinsic_store_scratch:
5699 visit_store_scratch(ctx, instr);
5700 break;
5701 case nir_intrinsic_get_buffer_size:
5702 visit_get_buffer_size(ctx, instr);
5703 break;
5704 case nir_intrinsic_barrier: {
5705 unsigned* bsize = ctx->program->info->cs.block_size;
5706 unsigned workgroup_size = bsize[0] * bsize[1] * bsize[2];
5707 if (workgroup_size > ctx->program->wave_size)
5708 bld.sopp(aco_opcode::s_barrier);
5709 break;
5710 }
5711 case nir_intrinsic_group_memory_barrier:
5712 case nir_intrinsic_memory_barrier:
5713 case nir_intrinsic_memory_barrier_atomic_counter:
5714 case nir_intrinsic_memory_barrier_buffer:
5715 case nir_intrinsic_memory_barrier_image:
5716 case nir_intrinsic_memory_barrier_shared:
5717 emit_memory_barrier(ctx, instr);
5718 break;
5719 case nir_intrinsic_load_num_work_groups: {
5720 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5721 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
5722 emit_split_vector(ctx, dst, 3);
5723 break;
5724 }
5725 case nir_intrinsic_load_local_invocation_id: {
5726 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5727 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
5728 emit_split_vector(ctx, dst, 3);
5729 break;
5730 }
5731 case nir_intrinsic_load_work_group_id: {
5732 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5733 struct ac_arg *args = ctx->args->ac.workgroup_ids;
5734 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5735 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
5736 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
5737 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
5738 emit_split_vector(ctx, dst, 3);
5739 break;
5740 }
5741 case nir_intrinsic_load_local_invocation_index: {
5742 Temp id = emit_mbcnt(ctx, bld.def(v1));
5743
5744 /* The tg_size bits [6:11] contain the subgroup id,
5745 * we need this multiplied by the wave size, and then OR the thread id to it.
5746 */
5747 if (ctx->program->wave_size == 64) {
5748 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
5749 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
5750 get_arg(ctx, ctx->args->ac.tg_size));
5751 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
5752 } else {
5753 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
5754 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
5755 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
5756 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
5757 }
5758 break;
5759 }
5760 case nir_intrinsic_load_subgroup_id: {
5761 if (ctx->stage == compute_cs) {
5762 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
5763 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
5764 } else {
5765 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
5766 }
5767 break;
5768 }
5769 case nir_intrinsic_load_subgroup_invocation: {
5770 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
5771 break;
5772 }
5773 case nir_intrinsic_load_num_subgroups: {
5774 if (ctx->stage == compute_cs)
5775 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
5776 get_arg(ctx, ctx->args->ac.tg_size));
5777 else
5778 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
5779 break;
5780 }
5781 case nir_intrinsic_ballot: {
5782 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5783 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5784 Definition tmp = bld.def(dst.regClass());
5785 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
5786 if (instr->src[0].ssa->bit_size == 1) {
5787 assert(src.regClass() == bld.lm);
5788 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
5789 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
5790 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
5791 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
5792 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
5793 } else {
5794 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5795 nir_print_instr(&instr->instr, stderr);
5796 fprintf(stderr, "\n");
5797 }
5798 if (dst.size() != bld.lm.size()) {
5799 /* Wave32 with ballot size set to 64 */
5800 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
5801 }
5802 emit_wqm(ctx, tmp.getTemp(), dst);
5803 break;
5804 }
5805 case nir_intrinsic_shuffle:
5806 case nir_intrinsic_read_invocation: {
5807 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5808 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
5809 emit_uniform_subgroup(ctx, instr, src);
5810 } else {
5811 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
5812 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
5813 tid = bld.as_uniform(tid);
5814 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5815 if (src.regClass() == v1) {
5816 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
5817 } else if (src.regClass() == v2) {
5818 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
5819 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
5820 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
5821 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
5822 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
5823 emit_split_vector(ctx, dst, 2);
5824 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
5825 assert(src.regClass() == bld.lm);
5826 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
5827 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
5828 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
5829 assert(src.regClass() == bld.lm);
5830 Temp tmp;
5831 if (ctx->program->chip_class <= GFX7)
5832 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
5833 else if (ctx->program->wave_size == 64)
5834 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
5835 else
5836 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
5837 tmp = emit_extract_vector(ctx, tmp, 0, v1);
5838 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
5839 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
5840 } else {
5841 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5842 nir_print_instr(&instr->instr, stderr);
5843 fprintf(stderr, "\n");
5844 }
5845 }
5846 break;
5847 }
5848 case nir_intrinsic_load_sample_id: {
5849 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
5850 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
5851 break;
5852 }
5853 case nir_intrinsic_load_sample_mask_in: {
5854 visit_load_sample_mask_in(ctx, instr);
5855 break;
5856 }
5857 case nir_intrinsic_read_first_invocation: {
5858 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5859 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5860 if (src.regClass() == v1) {
5861 emit_wqm(ctx,
5862 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
5863 dst);
5864 } else if (src.regClass() == v2) {
5865 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
5866 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
5867 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
5868 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
5869 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
5870 emit_split_vector(ctx, dst, 2);
5871 } else if (instr->dest.ssa.bit_size == 1) {
5872 assert(src.regClass() == bld.lm);
5873 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
5874 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
5875 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
5876 } else if (src.regClass() == s1) {
5877 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
5878 } else if (src.regClass() == s2) {
5879 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
5880 } else {
5881 fprintf(stderr, "Unimplemented NIR instr bit size: ");
5882 nir_print_instr(&instr->instr, stderr);
5883 fprintf(stderr, "\n");
5884 }
5885 break;
5886 }
5887 case nir_intrinsic_vote_all: {
5888 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5889 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5890 assert(src.regClass() == bld.lm);
5891 assert(dst.regClass() == bld.lm);
5892
5893 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
5894 Temp val = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(0u), Operand(-1u), bld.scc(tmp));
5895 emit_wqm(ctx, val, dst);
5896 break;
5897 }
5898 case nir_intrinsic_vote_any: {
5899 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5900 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5901 assert(src.regClass() == bld.lm);
5902 assert(dst.regClass() == bld.lm);
5903
5904 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
5905 Temp val = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), Operand(0u), bld.scc(tmp));
5906 emit_wqm(ctx, val, dst);
5907 break;
5908 }
5909 case nir_intrinsic_reduce:
5910 case nir_intrinsic_inclusive_scan:
5911 case nir_intrinsic_exclusive_scan: {
5912 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5913 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5914 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
5915 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
5916 nir_intrinsic_cluster_size(instr) : 0;
5917 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
5918
5919 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
5920 emit_uniform_subgroup(ctx, instr, src);
5921 } else if (instr->dest.ssa.bit_size == 1) {
5922 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
5923 op = nir_op_iand;
5924 else if (op == nir_op_iadd)
5925 op = nir_op_ixor;
5926 else if (op == nir_op_umax || op == nir_op_imax)
5927 op = nir_op_ior;
5928 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
5929
5930 switch (instr->intrinsic) {
5931 case nir_intrinsic_reduce:
5932 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
5933 break;
5934 case nir_intrinsic_exclusive_scan:
5935 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
5936 break;
5937 case nir_intrinsic_inclusive_scan:
5938 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
5939 break;
5940 default:
5941 assert(false);
5942 }
5943 } else if (cluster_size == 1) {
5944 bld.copy(Definition(dst), src);
5945 } else {
5946 src = as_vgpr(ctx, src);
5947
5948 ReduceOp reduce_op;
5949 switch (op) {
5950 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5951 CASE(iadd)
5952 CASE(imul)
5953 CASE(fadd)
5954 CASE(fmul)
5955 CASE(imin)
5956 CASE(umin)
5957 CASE(fmin)
5958 CASE(imax)
5959 CASE(umax)
5960 CASE(fmax)
5961 CASE(iand)
5962 CASE(ior)
5963 CASE(ixor)
5964 default:
5965 unreachable("unknown reduction op");
5966 #undef CASE
5967 }
5968
5969 aco_opcode aco_op;
5970 switch (instr->intrinsic) {
5971 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
5972 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
5973 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
5974 default:
5975 unreachable("unknown reduce intrinsic");
5976 }
5977
5978 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
5979 reduce->operands[0] = Operand(src);
5980 // filled in by aco_reduce_assign.cpp, used internally as part of the
5981 // reduce sequence
5982 assert(dst.size() == 1 || dst.size() == 2);
5983 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
5984 reduce->operands[2] = Operand(v1.as_linear());
5985
5986 Temp tmp_dst = bld.tmp(dst.regClass());
5987 reduce->definitions[0] = Definition(tmp_dst);
5988 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
5989 reduce->definitions[2] = Definition();
5990 reduce->definitions[3] = Definition(scc, s1);
5991 reduce->definitions[4] = Definition();
5992 reduce->reduce_op = reduce_op;
5993 reduce->cluster_size = cluster_size;
5994 ctx->block->instructions.emplace_back(std::move(reduce));
5995
5996 emit_wqm(ctx, tmp_dst, dst);
5997 }
5998 break;
5999 }
6000 case nir_intrinsic_quad_broadcast: {
6001 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6002 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6003 emit_uniform_subgroup(ctx, instr, src);
6004 } else {
6005 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6006 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
6007 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
6008
6009 if (instr->dest.ssa.bit_size == 1) {
6010 assert(src.regClass() == bld.lm);
6011 assert(dst.regClass() == bld.lm);
6012 uint32_t half_mask = 0x11111111u << lane;
6013 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
6014 Temp tmp = bld.tmp(bld.lm);
6015 bld.sop1(Builder::s_wqm, Definition(tmp),
6016 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
6017 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
6018 emit_wqm(ctx, tmp, dst);
6019 } else if (instr->dest.ssa.bit_size == 32) {
6020 if (ctx->program->chip_class >= GFX8)
6021 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
6022 else
6023 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
6024 } else if (instr->dest.ssa.bit_size == 64) {
6025 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6026 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6027 if (ctx->program->chip_class >= GFX8) {
6028 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
6029 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
6030 } else {
6031 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
6032 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
6033 }
6034 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6035 emit_split_vector(ctx, dst, 2);
6036 } else {
6037 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6038 nir_print_instr(&instr->instr, stderr);
6039 fprintf(stderr, "\n");
6040 }
6041 }
6042 break;
6043 }
6044 case nir_intrinsic_quad_swap_horizontal:
6045 case nir_intrinsic_quad_swap_vertical:
6046 case nir_intrinsic_quad_swap_diagonal:
6047 case nir_intrinsic_quad_swizzle_amd: {
6048 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6049 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6050 emit_uniform_subgroup(ctx, instr, src);
6051 break;
6052 }
6053 uint16_t dpp_ctrl = 0;
6054 switch (instr->intrinsic) {
6055 case nir_intrinsic_quad_swap_horizontal:
6056 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
6057 break;
6058 case nir_intrinsic_quad_swap_vertical:
6059 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
6060 break;
6061 case nir_intrinsic_quad_swap_diagonal:
6062 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
6063 break;
6064 case nir_intrinsic_quad_swizzle_amd:
6065 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
6066 break;
6067 default:
6068 break;
6069 }
6070 if (ctx->program->chip_class < GFX8)
6071 dpp_ctrl |= (1 << 15);
6072
6073 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6074 if (instr->dest.ssa.bit_size == 1) {
6075 assert(src.regClass() == bld.lm);
6076 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
6077 if (ctx->program->chip_class >= GFX8)
6078 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
6079 else
6080 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
6081 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
6082 emit_wqm(ctx, tmp, dst);
6083 } else if (instr->dest.ssa.bit_size == 32) {
6084 Temp tmp;
6085 if (ctx->program->chip_class >= GFX8)
6086 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
6087 else
6088 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
6089 emit_wqm(ctx, tmp, dst);
6090 } else if (instr->dest.ssa.bit_size == 64) {
6091 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6092 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6093 if (ctx->program->chip_class >= GFX8) {
6094 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
6095 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
6096 } else {
6097 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
6098 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
6099 }
6100 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6101 emit_split_vector(ctx, dst, 2);
6102 } else {
6103 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6104 nir_print_instr(&instr->instr, stderr);
6105 fprintf(stderr, "\n");
6106 }
6107 break;
6108 }
6109 case nir_intrinsic_masked_swizzle_amd: {
6110 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6111 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6112 emit_uniform_subgroup(ctx, instr, src);
6113 break;
6114 }
6115 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6116 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
6117 if (dst.regClass() == v1) {
6118 emit_wqm(ctx,
6119 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
6120 dst);
6121 } else if (dst.regClass() == v2) {
6122 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6123 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6124 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
6125 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
6126 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6127 emit_split_vector(ctx, dst, 2);
6128 } else {
6129 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6130 nir_print_instr(&instr->instr, stderr);
6131 fprintf(stderr, "\n");
6132 }
6133 break;
6134 }
6135 case nir_intrinsic_write_invocation_amd: {
6136 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6137 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
6138 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
6139 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6140 if (dst.regClass() == v1) {
6141 /* src2 is ignored for writelane. RA assigns the same reg for dst */
6142 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
6143 } else if (dst.regClass() == v2) {
6144 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
6145 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
6146 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
6147 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
6148 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
6149 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
6150 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6151 emit_split_vector(ctx, dst, 2);
6152 } else {
6153 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6154 nir_print_instr(&instr->instr, stderr);
6155 fprintf(stderr, "\n");
6156 }
6157 break;
6158 }
6159 case nir_intrinsic_mbcnt_amd: {
6160 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6161 RegClass rc = RegClass(src.type(), 1);
6162 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
6163 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
6164 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6165 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
6166 emit_wqm(ctx, wqm_tmp, dst);
6167 break;
6168 }
6169 case nir_intrinsic_load_helper_invocation: {
6170 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6171 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
6172 ctx->block->kind |= block_kind_needs_lowering;
6173 ctx->program->needs_exact = true;
6174 break;
6175 }
6176 case nir_intrinsic_is_helper_invocation: {
6177 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6178 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
6179 ctx->block->kind |= block_kind_needs_lowering;
6180 ctx->program->needs_exact = true;
6181 break;
6182 }
6183 case nir_intrinsic_demote:
6184 bld.pseudo(aco_opcode::p_demote_to_helper);
6185 ctx->block->kind |= block_kind_uses_demote;
6186 ctx->program->needs_exact = true;
6187 break;
6188 case nir_intrinsic_demote_if: {
6189 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6190 assert(src.regClass() == bld.lm);
6191 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6192 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
6193 ctx->block->kind |= block_kind_uses_demote;
6194 ctx->program->needs_exact = true;
6195 break;
6196 }
6197 case nir_intrinsic_first_invocation: {
6198 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
6199 get_ssa_temp(ctx, &instr->dest.ssa));
6200 break;
6201 }
6202 case nir_intrinsic_shader_clock:
6203 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
6204 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
6205 break;
6206 case nir_intrinsic_load_vertex_id_zero_base: {
6207 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6208 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
6209 break;
6210 }
6211 case nir_intrinsic_load_first_vertex: {
6212 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6213 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
6214 break;
6215 }
6216 case nir_intrinsic_load_base_instance: {
6217 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6218 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
6219 break;
6220 }
6221 case nir_intrinsic_load_instance_id: {
6222 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6223 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
6224 break;
6225 }
6226 case nir_intrinsic_load_draw_id: {
6227 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6228 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
6229 break;
6230 }
6231 default:
6232 fprintf(stderr, "Unimplemented intrinsic instr: ");
6233 nir_print_instr(&instr->instr, stderr);
6234 fprintf(stderr, "\n");
6235 abort();
6236
6237 break;
6238 }
6239 }
6240
6241
6242 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
6243 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
6244 enum glsl_base_type *stype)
6245 {
6246 nir_deref_instr *texture_deref_instr = NULL;
6247 nir_deref_instr *sampler_deref_instr = NULL;
6248 int plane = -1;
6249
6250 for (unsigned i = 0; i < instr->num_srcs; i++) {
6251 switch (instr->src[i].src_type) {
6252 case nir_tex_src_texture_deref:
6253 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
6254 break;
6255 case nir_tex_src_sampler_deref:
6256 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
6257 break;
6258 case nir_tex_src_plane:
6259 plane = nir_src_as_int(instr->src[i].src);
6260 break;
6261 default:
6262 break;
6263 }
6264 }
6265
6266 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
6267
6268 if (!sampler_deref_instr)
6269 sampler_deref_instr = texture_deref_instr;
6270
6271 if (plane >= 0) {
6272 assert(instr->op != nir_texop_txf_ms &&
6273 instr->op != nir_texop_samples_identical);
6274 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
6275 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
6276 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
6277 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
6278 } else {
6279 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
6280 }
6281 if (samp_ptr) {
6282 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
6283
6284 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
6285 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
6286 Builder bld(ctx->program, ctx->block);
6287
6288 /* to avoid unnecessary moves, we split and recombine sampler and image */
6289 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
6290 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
6291 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
6292 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
6293 Definition(img[2]), Definition(img[3]), Definition(img[4]),
6294 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
6295 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
6296 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
6297
6298 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
6299 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
6300 img[0], img[1], img[2], img[3],
6301 img[4], img[5], img[6], img[7]);
6302 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6303 samp[0], samp[1], samp[2], samp[3]);
6304 }
6305 }
6306 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
6307 instr->op == nir_texop_samples_identical))
6308 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
6309 }
6310
6311 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
6312 Temp *out_ma, Temp *out_sc, Temp *out_tc)
6313 {
6314 Builder bld(ctx->program, ctx->block);
6315
6316 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
6317 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
6318 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
6319
6320 Operand neg_one(0xbf800000u);
6321 Operand one(0x3f800000u);
6322 Operand two(0x40000000u);
6323 Operand four(0x40800000u);
6324
6325 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
6326 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
6327 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
6328
6329 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
6330 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(s2), two, id);
6331 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
6332 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);
6333
6334 // select sc
6335 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
6336 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
6337 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
6338 one, is_ma_y);
6339 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
6340
6341 // select tc
6342 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
6343 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
6344 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
6345
6346 // select ma
6347 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
6348 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
6349 deriv_z, is_ma_z);
6350 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
6351 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
6352 }
6353
6354 void prepare_cube_coords(isel_context *ctx, Temp* coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
6355 {
6356 Builder bld(ctx->program, ctx->block);
6357 Temp coord_args[4], ma, tc, sc, id;
6358 for (unsigned i = 0; i < (is_array ? 4 : 3); i++)
6359 coord_args[i] = emit_extract_vector(ctx, *coords, i, v1);
6360
6361 if (is_array) {
6362 coord_args[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coord_args[3]);
6363
6364 // see comment in ac_prepare_cube_coords()
6365 if (ctx->options->chip_class <= GFX8)
6366 coord_args[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coord_args[3]);
6367 }
6368
6369 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6370
6371 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
6372 vop3a->operands[0] = Operand(ma);
6373 vop3a->abs[0] = true;
6374 Temp invma = bld.tmp(v1);
6375 vop3a->definitions[0] = Definition(invma);
6376 ctx->block->instructions.emplace_back(std::move(vop3a));
6377
6378 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6379 if (!is_deriv)
6380 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
6381
6382 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6383 if (!is_deriv)
6384 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
6385
6386 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coord_args[0], coord_args[1], coord_args[2]);
6387
6388 if (is_deriv) {
6389 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
6390 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
6391
6392 for (unsigned i = 0; i < 2; i++) {
6393 // see comment in ac_prepare_cube_coords()
6394 Temp deriv_ma;
6395 Temp deriv_sc, deriv_tc;
6396 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
6397 &deriv_ma, &deriv_sc, &deriv_tc);
6398
6399 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
6400
6401 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
6402 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
6403 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
6404 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
6405 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
6406 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
6407 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
6408 }
6409
6410 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
6411 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
6412 }
6413
6414 if (is_array)
6415 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coord_args[3], id, Operand(0x41000000u/*8.0*/));
6416 *coords = bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), sc, tc, id);
6417
6418 }
6419
6420 Temp apply_round_slice(isel_context *ctx, Temp coords, unsigned idx)
6421 {
6422 Temp coord_vec[3];
6423 for (unsigned i = 0; i < coords.size(); i++)
6424 coord_vec[i] = emit_extract_vector(ctx, coords, i, v1);
6425
6426 Builder bld(ctx->program, ctx->block);
6427 coord_vec[idx] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coord_vec[idx]);
6428
6429 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
6430 for (unsigned i = 0; i < coords.size(); i++)
6431 vec->operands[i] = Operand(coord_vec[i]);
6432 Temp res = bld.tmp(RegType::vgpr, coords.size());
6433 vec->definitions[0] = Definition(res);
6434 ctx->block->instructions.emplace_back(std::move(vec));
6435 return res;
6436 }
6437
6438 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
6439 {
6440 if (vec->parent_instr->type != nir_instr_type_alu)
6441 return;
6442 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
6443 if (vec_instr->op != nir_op_vec(vec->num_components))
6444 return;
6445
6446 for (unsigned i = 0; i < vec->num_components; i++) {
6447 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
6448 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
6449 }
6450 }
6451
6452 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
6453 {
6454 Builder bld(ctx->program, ctx->block);
6455 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
6456 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
6457 Temp resource, sampler, fmask_ptr, bias = Temp(), coords, compare = Temp(), sample_index = Temp(),
6458 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(), derivs = Temp();
6459 nir_const_value *sample_index_cv = NULL;
6460 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
6461 enum glsl_base_type stype;
6462 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
6463
6464 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
6465 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
6466 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
6467 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
6468
6469 for (unsigned i = 0; i < instr->num_srcs; i++) {
6470 switch (instr->src[i].src_type) {
6471 case nir_tex_src_coord:
6472 coords = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[i].src.ssa));
6473 break;
6474 case nir_tex_src_bias:
6475 if (instr->op == nir_texop_txb) {
6476 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
6477 has_bias = true;
6478 }
6479 break;
6480 case nir_tex_src_lod: {
6481 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
6482
6483 if (val && val->f32 <= 0.0) {
6484 level_zero = true;
6485 } else {
6486 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
6487 has_lod = true;
6488 }
6489 break;
6490 }
6491 case nir_tex_src_comparator:
6492 if (instr->is_shadow) {
6493 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
6494 has_compare = true;
6495 }
6496 break;
6497 case nir_tex_src_offset:
6498 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
6499 get_const_vec(instr->src[i].src.ssa, const_offset);
6500 has_offset = true;
6501 break;
6502 case nir_tex_src_ddx:
6503 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
6504 has_ddx = true;
6505 break;
6506 case nir_tex_src_ddy:
6507 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
6508 has_ddy = true;
6509 break;
6510 case nir_tex_src_ms_index:
6511 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
6512 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
6513 has_sample_index = true;
6514 break;
6515 case nir_tex_src_texture_offset:
6516 case nir_tex_src_sampler_offset:
6517 default:
6518 break;
6519 }
6520 }
6521 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6522 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
6523 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
6524
6525 if (instr->op == nir_texop_texture_samples) {
6526 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
6527
6528 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
6529 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
6530 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 */));
6531 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
6532
6533 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6534 samples, Operand(1u), bld.scc(is_msaa));
6535 return;
6536 }
6537
6538 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
6539 aco_ptr<Instruction> tmp_instr;
6540 Temp acc, pack = Temp();
6541
6542 uint32_t pack_const = 0;
6543 for (unsigned i = 0; i < offset.size(); i++) {
6544 if (!const_offset[i])
6545 continue;
6546 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
6547 }
6548
6549 if (offset.type() == RegType::sgpr) {
6550 for (unsigned i = 0; i < offset.size(); i++) {
6551 if (const_offset[i])
6552 continue;
6553
6554 acc = emit_extract_vector(ctx, offset, i, s1);
6555 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
6556
6557 if (i) {
6558 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
6559 }
6560
6561 if (pack == Temp()) {
6562 pack = acc;
6563 } else {
6564 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
6565 }
6566 }
6567
6568 if (pack_const && pack != Temp())
6569 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
6570 } else {
6571 for (unsigned i = 0; i < offset.size(); i++) {
6572 if (const_offset[i])
6573 continue;
6574
6575 acc = emit_extract_vector(ctx, offset, i, v1);
6576 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
6577
6578 if (i) {
6579 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
6580 }
6581
6582 if (pack == Temp()) {
6583 pack = acc;
6584 } else {
6585 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
6586 }
6587 }
6588
6589 if (pack_const && pack != Temp())
6590 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
6591 }
6592 if (pack_const && pack == Temp())
6593 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
6594 else if (pack == Temp())
6595 has_offset = false;
6596 else
6597 offset = pack;
6598 }
6599
6600 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
6601 prepare_cube_coords(ctx, &coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
6602
6603 /* pack derivatives */
6604 if (has_ddx || has_ddy) {
6605 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
6606 derivs = bld.pseudo(aco_opcode::p_create_vector, bld.def(v4),
6607 ddx, Operand(0u), ddy, Operand(0u));
6608 } else {
6609 derivs = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, ddx.size() + ddy.size()), ddx, ddy);
6610 }
6611 has_derivs = true;
6612 }
6613
6614 if (instr->coord_components > 1 &&
6615 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
6616 instr->is_array &&
6617 instr->op != nir_texop_txf)
6618 coords = apply_round_slice(ctx, coords, 1);
6619
6620 if (instr->coord_components > 2 &&
6621 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
6622 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
6623 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
6624 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
6625 instr->is_array &&
6626 instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms)
6627 coords = apply_round_slice(ctx, coords, 2);
6628
6629 if (ctx->options->chip_class == GFX9 &&
6630 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
6631 instr->op != nir_texop_lod && instr->coord_components) {
6632 assert(coords.size() > 0 && coords.size() < 3);
6633
6634 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size() + 1, 1)};
6635 vec->operands[0] = Operand(emit_extract_vector(ctx, coords, 0, v1));
6636 vec->operands[1] = instr->op == nir_texop_txf ? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6637 if (coords.size() > 1)
6638 vec->operands[2] = Operand(emit_extract_vector(ctx, coords, 1, v1));
6639 coords = bld.tmp(RegType::vgpr, coords.size() + 1);
6640 vec->definitions[0] = Definition(coords);
6641 ctx->block->instructions.emplace_back(std::move(vec));
6642 }
6643
6644 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
6645
6646 if (instr->op == nir_texop_samples_identical)
6647 resource = fmask_ptr;
6648
6649 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
6650 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
6651 instr->op != nir_texop_txs) {
6652 assert(has_sample_index);
6653 Operand op(sample_index);
6654 if (sample_index_cv)
6655 op = Operand(sample_index_cv->u32);
6656 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
6657 }
6658
6659 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
6660 Temp split_coords[coords.size()];
6661 emit_split_vector(ctx, coords, coords.size());
6662 for (unsigned i = 0; i < coords.size(); i++)
6663 split_coords[i] = emit_extract_vector(ctx, coords, i, v1);
6664
6665 unsigned i = 0;
6666 for (; i < std::min(offset.size(), instr->coord_components); i++) {
6667 Temp off = emit_extract_vector(ctx, offset, i, v1);
6668 split_coords[i] = bld.vadd32(bld.def(v1), split_coords[i], off);
6669 }
6670
6671 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
6672 for (unsigned i = 0; i < coords.size(); i++)
6673 vec->operands[i] = Operand(split_coords[i]);
6674 coords = bld.tmp(coords.regClass());
6675 vec->definitions[0] = Definition(coords);
6676 ctx->block->instructions.emplace_back(std::move(vec));
6677
6678 has_offset = false;
6679 }
6680
6681 /* Build tex instruction */
6682 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
6683 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
6684 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
6685 : 0;
6686 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6687 Temp tmp_dst = dst;
6688
6689 /* gather4 selects the component by dmask and always returns vec4 */
6690 if (instr->op == nir_texop_tg4) {
6691 assert(instr->dest.ssa.num_components == 4);
6692 if (instr->is_shadow)
6693 dmask = 1;
6694 else
6695 dmask = 1 << instr->component;
6696 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
6697 tmp_dst = bld.tmp(v4);
6698 } else if (instr->op == nir_texop_samples_identical) {
6699 tmp_dst = bld.tmp(v1);
6700 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
6701 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
6702 }
6703
6704 aco_ptr<MIMG_instruction> tex;
6705 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
6706 if (!has_lod)
6707 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6708
6709 bool div_by_6 = instr->op == nir_texop_txs &&
6710 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
6711 instr->is_array &&
6712 (dmask & (1 << 2));
6713 if (tmp_dst.id() == dst.id() && div_by_6)
6714 tmp_dst = bld.tmp(tmp_dst.regClass());
6715
6716 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 2, 1));
6717 tex->operands[0] = Operand(as_vgpr(ctx,lod));
6718 tex->operands[1] = Operand(resource);
6719 if (ctx->options->chip_class == GFX9 &&
6720 instr->op == nir_texop_txs &&
6721 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
6722 instr->is_array) {
6723 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
6724 } else if (instr->op == nir_texop_query_levels) {
6725 tex->dmask = 1 << 3;
6726 } else {
6727 tex->dmask = dmask;
6728 }
6729 tex->da = da;
6730 tex->definitions[0] = Definition(tmp_dst);
6731 tex->dim = dim;
6732 tex->can_reorder = true;
6733 ctx->block->instructions.emplace_back(std::move(tex));
6734
6735 if (div_by_6) {
6736 /* divide 3rd value by 6 by multiplying with magic number */
6737 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
6738 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6739 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
6740 assert(instr->dest.ssa.num_components == 3);
6741 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
6742 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
6743 emit_extract_vector(ctx, tmp_dst, 0, v1),
6744 emit_extract_vector(ctx, tmp_dst, 1, v1),
6745 by_6);
6746
6747 }
6748
6749 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
6750 return;
6751 }
6752
6753 Temp tg4_compare_cube_wa64 = Temp();
6754
6755 if (tg4_integer_workarounds) {
6756 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 2, 1));
6757 tex->operands[0] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6758 tex->operands[1] = Operand(resource);
6759 tex->dim = dim;
6760 tex->dmask = 0x3;
6761 tex->da = da;
6762 Temp size = bld.tmp(v2);
6763 tex->definitions[0] = Definition(size);
6764 tex->can_reorder = true;
6765 ctx->block->instructions.emplace_back(std::move(tex));
6766 emit_split_vector(ctx, size, size.size());
6767
6768 Temp half_texel[2];
6769 for (unsigned i = 0; i < 2; i++) {
6770 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
6771 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
6772 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
6773 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
6774 }
6775
6776 Temp orig_coords[2] = {
6777 emit_extract_vector(ctx, coords, 0, v1),
6778 emit_extract_vector(ctx, coords, 1, v1)};
6779 Temp new_coords[2] = {
6780 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), orig_coords[0], half_texel[0]),
6781 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), orig_coords[1], half_texel[1])
6782 };
6783
6784 if (tg4_integer_cube_workaround) {
6785 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6786 Temp desc[resource.size()];
6787 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
6788 Format::PSEUDO, 1, resource.size())};
6789 split->operands[0] = Operand(resource);
6790 for (unsigned i = 0; i < resource.size(); i++) {
6791 desc[i] = bld.tmp(s1);
6792 split->definitions[i] = Definition(desc[i]);
6793 }
6794 ctx->block->instructions.emplace_back(std::move(split));
6795
6796 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
6797 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
6798 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
6799
6800 Temp nfmt;
6801 if (stype == GLSL_TYPE_UINT) {
6802 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
6803 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
6804 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
6805 bld.scc(compare_cube_wa));
6806 } else {
6807 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
6808 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
6809 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
6810 bld.scc(compare_cube_wa));
6811 }
6812 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
6813 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
6814
6815 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
6816
6817 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
6818 Operand((uint32_t)C_008F14_NUM_FORMAT));
6819 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
6820
6821 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
6822 Format::PSEUDO, resource.size(), 1)};
6823 for (unsigned i = 0; i < resource.size(); i++)
6824 vec->operands[i] = Operand(desc[i]);
6825 resource = bld.tmp(resource.regClass());
6826 vec->definitions[0] = Definition(resource);
6827 ctx->block->instructions.emplace_back(std::move(vec));
6828
6829 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
6830 new_coords[0], orig_coords[0], tg4_compare_cube_wa64);
6831 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
6832 new_coords[1], orig_coords[1], tg4_compare_cube_wa64);
6833 }
6834
6835 if (coords.size() == 3) {
6836 coords = bld.pseudo(aco_opcode::p_create_vector, bld.def(v3),
6837 new_coords[0], new_coords[1],
6838 emit_extract_vector(ctx, coords, 2, v1));
6839 } else {
6840 assert(coords.size() == 2);
6841 coords = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2),
6842 new_coords[0], new_coords[1]);
6843 }
6844 }
6845
6846 std::vector<Operand> args;
6847 if (has_offset)
6848 args.emplace_back(Operand(offset));
6849 if (has_bias)
6850 args.emplace_back(Operand(bias));
6851 if (has_compare)
6852 args.emplace_back(Operand(compare));
6853 if (has_derivs)
6854 args.emplace_back(Operand(derivs));
6855 args.emplace_back(Operand(coords));
6856 if (has_sample_index)
6857 args.emplace_back(Operand(sample_index));
6858 if (has_lod)
6859 args.emplace_back(lod);
6860
6861 Temp arg;
6862 if (args.size() > 1) {
6863 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
6864 unsigned size = 0;
6865 for (unsigned i = 0; i < args.size(); i++) {
6866 size += args[i].size();
6867 vec->operands[i] = args[i];
6868 }
6869 RegClass rc = RegClass(RegType::vgpr, size);
6870 Temp tmp = bld.tmp(rc);
6871 vec->definitions[0] = Definition(tmp);
6872 ctx->block->instructions.emplace_back(std::move(vec));
6873 arg = tmp;
6874 } else {
6875 assert(args[0].isTemp());
6876 arg = as_vgpr(ctx, args[0].getTemp());
6877 }
6878
6879 /* we don't need the bias, sample index, compare value or offset to be
6880 * computed in WQM but if the p_create_vector copies the coordinates, then it
6881 * needs to be in WQM */
6882 if (!(has_ddx && has_ddy) && !has_lod && !level_zero &&
6883 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
6884 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
6885 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
6886
6887 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
6888 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6889
6890 assert(coords.size() == 1);
6891 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
6892 aco_opcode op;
6893 switch (last_bit) {
6894 case 1:
6895 op = aco_opcode::buffer_load_format_x; break;
6896 case 2:
6897 op = aco_opcode::buffer_load_format_xy; break;
6898 case 3:
6899 op = aco_opcode::buffer_load_format_xyz; break;
6900 case 4:
6901 op = aco_opcode::buffer_load_format_xyzw; break;
6902 default:
6903 unreachable("Tex instruction loads more than 4 components.");
6904 }
6905
6906 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6907 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
6908 tmp_dst = dst;
6909 else
6910 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
6911
6912 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
6913 mubuf->operands[0] = Operand(coords);
6914 mubuf->operands[1] = Operand(resource);
6915 mubuf->operands[2] = Operand((uint32_t) 0);
6916 mubuf->definitions[0] = Definition(tmp_dst);
6917 mubuf->idxen = true;
6918 mubuf->can_reorder = true;
6919 ctx->block->instructions.emplace_back(std::move(mubuf));
6920
6921 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
6922 return;
6923 }
6924
6925
6926 if (instr->op == nir_texop_txf ||
6927 instr->op == nir_texop_txf_ms ||
6928 instr->op == nir_texop_samples_identical) {
6929 aco_opcode op = level_zero || instr->sampler_dim == GLSL_SAMPLER_DIM_MS ? aco_opcode::image_load : aco_opcode::image_load_mip;
6930 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 2, 1));
6931 tex->operands[0] = Operand(arg);
6932 tex->operands[1] = Operand(resource);
6933 tex->dim = dim;
6934 tex->dmask = dmask;
6935 tex->unrm = true;
6936 tex->da = da;
6937 tex->definitions[0] = Definition(tmp_dst);
6938 tex->can_reorder = true;
6939 ctx->block->instructions.emplace_back(std::move(tex));
6940
6941 if (instr->op == nir_texop_samples_identical) {
6942 assert(dmask == 1 && dst.regClass() == v1);
6943 assert(dst.id() != tmp_dst.id());
6944
6945 Temp tmp = bld.tmp(bld.lm);
6946 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
6947 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
6948
6949 } else {
6950 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
6951 }
6952 return;
6953 }
6954
6955 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6956 aco_opcode opcode = aco_opcode::image_sample;
6957 if (has_offset) { /* image_sample_*_o */
6958 if (has_compare) {
6959 opcode = aco_opcode::image_sample_c_o;
6960 if (has_derivs)
6961 opcode = aco_opcode::image_sample_c_d_o;
6962 if (has_bias)
6963 opcode = aco_opcode::image_sample_c_b_o;
6964 if (level_zero)
6965 opcode = aco_opcode::image_sample_c_lz_o;
6966 if (has_lod)
6967 opcode = aco_opcode::image_sample_c_l_o;
6968 } else {
6969 opcode = aco_opcode::image_sample_o;
6970 if (has_derivs)
6971 opcode = aco_opcode::image_sample_d_o;
6972 if (has_bias)
6973 opcode = aco_opcode::image_sample_b_o;
6974 if (level_zero)
6975 opcode = aco_opcode::image_sample_lz_o;
6976 if (has_lod)
6977 opcode = aco_opcode::image_sample_l_o;
6978 }
6979 } else { /* no offset */
6980 if (has_compare) {
6981 opcode = aco_opcode::image_sample_c;
6982 if (has_derivs)
6983 opcode = aco_opcode::image_sample_c_d;
6984 if (has_bias)
6985 opcode = aco_opcode::image_sample_c_b;
6986 if (level_zero)
6987 opcode = aco_opcode::image_sample_c_lz;
6988 if (has_lod)
6989 opcode = aco_opcode::image_sample_c_l;
6990 } else {
6991 opcode = aco_opcode::image_sample;
6992 if (has_derivs)
6993 opcode = aco_opcode::image_sample_d;
6994 if (has_bias)
6995 opcode = aco_opcode::image_sample_b;
6996 if (level_zero)
6997 opcode = aco_opcode::image_sample_lz;
6998 if (has_lod)
6999 opcode = aco_opcode::image_sample_l;
7000 }
7001 }
7002
7003 if (instr->op == nir_texop_tg4) {
7004 if (has_offset) {
7005 opcode = aco_opcode::image_gather4_lz_o;
7006 if (has_compare)
7007 opcode = aco_opcode::image_gather4_c_lz_o;
7008 } else {
7009 opcode = aco_opcode::image_gather4_lz;
7010 if (has_compare)
7011 opcode = aco_opcode::image_gather4_c_lz;
7012 }
7013 } else if (instr->op == nir_texop_lod) {
7014 opcode = aco_opcode::image_get_lod;
7015 }
7016
7017 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
7018 tex->operands[0] = Operand(arg);
7019 tex->operands[1] = Operand(resource);
7020 tex->operands[2] = Operand(sampler);
7021 tex->dim = dim;
7022 tex->dmask = dmask;
7023 tex->da = da;
7024 tex->definitions[0] = Definition(tmp_dst);
7025 tex->can_reorder = true;
7026 ctx->block->instructions.emplace_back(std::move(tex));
7027
7028 if (tg4_integer_cube_workaround) {
7029 assert(tmp_dst.id() != dst.id());
7030 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
7031
7032 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
7033 Temp val[4];
7034 for (unsigned i = 0; i < dst.size(); i++) {
7035 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
7036 Temp cvt_val;
7037 if (stype == GLSL_TYPE_UINT)
7038 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
7039 else
7040 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
7041 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
7042 }
7043 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
7044 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
7045 val[0], val[1], val[2], val[3]);
7046 }
7047 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
7048 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
7049
7050 }
7051
7052
7053 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
7054 {
7055 Temp tmp = get_ssa_temp(ctx, ssa);
7056 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
7057 return Operand(tmp.regClass());
7058 else
7059 return Operand(tmp);
7060 }
7061
7062 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
7063 {
7064 aco_ptr<Pseudo_instruction> phi;
7065 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7066 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
7067
7068 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
7069 logical |= ctx->block->kind & block_kind_merge;
7070 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
7071
7072 /* we want a sorted list of sources, since the predecessor list is also sorted */
7073 std::map<unsigned, nir_ssa_def*> phi_src;
7074 nir_foreach_phi_src(src, instr)
7075 phi_src[src->pred->index] = src->src.ssa;
7076
7077 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
7078 unsigned num_operands = 0;
7079 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size())];
7080 unsigned num_defined = 0;
7081 unsigned cur_pred_idx = 0;
7082 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
7083 if (cur_pred_idx < preds.size()) {
7084 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
7085 unsigned block = ctx->cf_info.nir_to_aco[src.first];
7086 unsigned skipped = 0;
7087 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
7088 skipped++;
7089 if (cur_pred_idx + skipped < preds.size()) {
7090 for (unsigned i = 0; i < skipped; i++)
7091 operands[num_operands++] = Operand(dst.regClass());
7092 cur_pred_idx += skipped;
7093 } else {
7094 continue;
7095 }
7096 }
7097 cur_pred_idx++;
7098 Operand op = get_phi_operand(ctx, src.second);
7099 operands[num_operands++] = op;
7100 num_defined += !op.isUndefined();
7101 }
7102 /* handle block_kind_continue_or_break at loop exit blocks */
7103 while (cur_pred_idx++ < preds.size())
7104 operands[num_operands++] = Operand(dst.regClass());
7105
7106 if (num_defined == 0) {
7107 Builder bld(ctx->program, ctx->block);
7108 if (dst.regClass() == s1) {
7109 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
7110 } else if (dst.regClass() == v1) {
7111 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
7112 } else {
7113 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
7114 for (unsigned i = 0; i < dst.size(); i++)
7115 vec->operands[i] = Operand(0u);
7116 vec->definitions[0] = Definition(dst);
7117 ctx->block->instructions.emplace_back(std::move(vec));
7118 }
7119 return;
7120 }
7121
7122 /* we can use a linear phi in some cases if one src is undef */
7123 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
7124 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
7125
7126 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
7127 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
7128 assert(invert->kind & block_kind_invert);
7129
7130 unsigned then_block = invert->linear_preds[0];
7131
7132 Block* insert_block = NULL;
7133 for (unsigned i = 0; i < num_operands; i++) {
7134 Operand op = operands[i];
7135 if (op.isUndefined())
7136 continue;
7137 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
7138 phi->operands[0] = op;
7139 break;
7140 }
7141 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
7142 phi->operands[1] = Operand(dst.regClass());
7143 phi->definitions[0] = Definition(dst);
7144 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
7145 return;
7146 }
7147
7148 /* try to scalarize vector phis */
7149 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
7150 // TODO: scalarize linear phis on divergent ifs
7151 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
7152 std::array<Temp, 4> new_vec;
7153 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
7154 Operand src = operands[i];
7155 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
7156 can_scalarize = false;
7157 }
7158 if (can_scalarize) {
7159 unsigned num_components = instr->dest.ssa.num_components;
7160 assert(dst.size() % num_components == 0);
7161 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
7162
7163 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
7164 for (unsigned k = 0; k < num_components; k++) {
7165 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
7166 for (unsigned i = 0; i < num_operands; i++) {
7167 Operand src = operands[i];
7168 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
7169 }
7170 Temp phi_dst = {ctx->program->allocateId(), rc};
7171 phi->definitions[0] = Definition(phi_dst);
7172 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
7173 new_vec[k] = phi_dst;
7174 vec->operands[k] = Operand(phi_dst);
7175 }
7176 vec->definitions[0] = Definition(dst);
7177 ctx->block->instructions.emplace_back(std::move(vec));
7178 ctx->allocated_vec.emplace(dst.id(), new_vec);
7179 return;
7180 }
7181 }
7182
7183 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
7184 for (unsigned i = 0; i < num_operands; i++)
7185 phi->operands[i] = operands[i];
7186 phi->definitions[0] = Definition(dst);
7187 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
7188 }
7189
7190
7191 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
7192 {
7193 Temp dst = get_ssa_temp(ctx, &instr->def);
7194
7195 assert(dst.type() == RegType::sgpr);
7196
7197 if (dst.size() == 1) {
7198 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
7199 } else {
7200 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
7201 for (unsigned i = 0; i < dst.size(); i++)
7202 vec->operands[i] = Operand(0u);
7203 vec->definitions[0] = Definition(dst);
7204 ctx->block->instructions.emplace_back(std::move(vec));
7205 }
7206 }
7207
7208 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
7209 {
7210 Builder bld(ctx->program, ctx->block);
7211 Block *logical_target;
7212 append_logical_end(ctx->block);
7213 unsigned idx = ctx->block->index;
7214
7215 switch (instr->type) {
7216 case nir_jump_break:
7217 logical_target = ctx->cf_info.parent_loop.exit;
7218 add_logical_edge(idx, logical_target);
7219 ctx->block->kind |= block_kind_break;
7220
7221 if (!ctx->cf_info.parent_if.is_divergent &&
7222 !ctx->cf_info.parent_loop.has_divergent_continue) {
7223 /* uniform break - directly jump out of the loop */
7224 ctx->block->kind |= block_kind_uniform;
7225 ctx->cf_info.has_branch = true;
7226 bld.branch(aco_opcode::p_branch);
7227 add_linear_edge(idx, logical_target);
7228 return;
7229 }
7230 ctx->cf_info.parent_loop.has_divergent_branch = true;
7231 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
7232 break;
7233 case nir_jump_continue:
7234 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
7235 add_logical_edge(idx, logical_target);
7236 ctx->block->kind |= block_kind_continue;
7237
7238 if (ctx->cf_info.parent_if.is_divergent) {
7239 /* for potential uniform breaks after this continue,
7240 we must ensure that they are handled correctly */
7241 ctx->cf_info.parent_loop.has_divergent_continue = true;
7242 ctx->cf_info.parent_loop.has_divergent_branch = true;
7243 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
7244 } else {
7245 /* uniform continue - directly jump to the loop header */
7246 ctx->block->kind |= block_kind_uniform;
7247 ctx->cf_info.has_branch = true;
7248 bld.branch(aco_opcode::p_branch);
7249 add_linear_edge(idx, logical_target);
7250 return;
7251 }
7252 break;
7253 default:
7254 fprintf(stderr, "Unknown NIR jump instr: ");
7255 nir_print_instr(&instr->instr, stderr);
7256 fprintf(stderr, "\n");
7257 abort();
7258 }
7259
7260 /* remove critical edges from linear CFG */
7261 bld.branch(aco_opcode::p_branch);
7262 Block* break_block = ctx->program->create_and_insert_block();
7263 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7264 break_block->kind |= block_kind_uniform;
7265 add_linear_edge(idx, break_block);
7266 /* the loop_header pointer might be invalidated by this point */
7267 if (instr->type == nir_jump_continue)
7268 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
7269 add_linear_edge(break_block->index, logical_target);
7270 bld.reset(break_block);
7271 bld.branch(aco_opcode::p_branch);
7272
7273 Block* continue_block = ctx->program->create_and_insert_block();
7274 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7275 add_linear_edge(idx, continue_block);
7276 append_logical_start(continue_block);
7277 ctx->block = continue_block;
7278 return;
7279 }
7280
7281 void visit_block(isel_context *ctx, nir_block *block)
7282 {
7283 nir_foreach_instr(instr, block) {
7284 switch (instr->type) {
7285 case nir_instr_type_alu:
7286 visit_alu_instr(ctx, nir_instr_as_alu(instr));
7287 break;
7288 case nir_instr_type_load_const:
7289 visit_load_const(ctx, nir_instr_as_load_const(instr));
7290 break;
7291 case nir_instr_type_intrinsic:
7292 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
7293 break;
7294 case nir_instr_type_tex:
7295 visit_tex(ctx, nir_instr_as_tex(instr));
7296 break;
7297 case nir_instr_type_phi:
7298 visit_phi(ctx, nir_instr_as_phi(instr));
7299 break;
7300 case nir_instr_type_ssa_undef:
7301 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
7302 break;
7303 case nir_instr_type_deref:
7304 break;
7305 case nir_instr_type_jump:
7306 visit_jump(ctx, nir_instr_as_jump(instr));
7307 break;
7308 default:
7309 fprintf(stderr, "Unknown NIR instr type: ");
7310 nir_print_instr(instr, stderr);
7311 fprintf(stderr, "\n");
7312 //abort();
7313 }
7314 }
7315
7316 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7317 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
7318 }
7319
7320
7321
7322 static void visit_loop(isel_context *ctx, nir_loop *loop)
7323 {
7324 append_logical_end(ctx->block);
7325 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
7326 Builder bld(ctx->program, ctx->block);
7327 bld.branch(aco_opcode::p_branch);
7328 unsigned loop_preheader_idx = ctx->block->index;
7329
7330 Block loop_exit = Block();
7331 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7332 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
7333
7334 Block* loop_header = ctx->program->create_and_insert_block();
7335 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
7336 loop_header->kind |= block_kind_loop_header;
7337 add_edge(loop_preheader_idx, loop_header);
7338 ctx->block = loop_header;
7339
7340 /* emit loop body */
7341 unsigned loop_header_idx = loop_header->index;
7342 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
7343 append_logical_start(ctx->block);
7344 visit_cf_list(ctx, &loop->body);
7345
7346 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7347 if (!ctx->cf_info.has_branch) {
7348 append_logical_end(ctx->block);
7349 if (ctx->cf_info.exec_potentially_empty) {
7350 /* Discards can result in code running with an empty exec mask.
7351 * This would result in divergent breaks not ever being taken. As a
7352 * workaround, break the loop when the loop mask is empty instead of
7353 * always continuing. */
7354 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
7355 unsigned block_idx = ctx->block->index;
7356
7357 /* create helper blocks to avoid critical edges */
7358 Block *break_block = ctx->program->create_and_insert_block();
7359 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7360 break_block->kind = block_kind_uniform;
7361 bld.reset(break_block);
7362 bld.branch(aco_opcode::p_branch);
7363 add_linear_edge(block_idx, break_block);
7364 add_linear_edge(break_block->index, &loop_exit);
7365
7366 Block *continue_block = ctx->program->create_and_insert_block();
7367 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7368 continue_block->kind = block_kind_uniform;
7369 bld.reset(continue_block);
7370 bld.branch(aco_opcode::p_branch);
7371 add_linear_edge(block_idx, continue_block);
7372 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
7373
7374 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
7375 ctx->block = &ctx->program->blocks[block_idx];
7376 } else {
7377 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
7378 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7379 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
7380 else
7381 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
7382 }
7383
7384 bld.reset(ctx->block);
7385 bld.branch(aco_opcode::p_branch);
7386 }
7387
7388 /* fixup phis in loop header from unreachable blocks */
7389 if (ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch) {
7390 bool linear = ctx->cf_info.has_branch;
7391 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
7392 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
7393 if ((logical && instr->opcode == aco_opcode::p_phi) ||
7394 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
7395 /* the last operand should be the one that needs to be removed */
7396 instr->operands.pop_back();
7397 } else if (!is_phi(instr)) {
7398 break;
7399 }
7400 }
7401 }
7402
7403 ctx->cf_info.has_branch = false;
7404
7405 // TODO: if the loop has not a single exit, we must add one °°
7406 /* emit loop successor block */
7407 ctx->block = ctx->program->insert_block(std::move(loop_exit));
7408 append_logical_start(ctx->block);
7409
7410 #if 0
7411 // TODO: check if it is beneficial to not branch on continues
7412 /* trim linear phis in loop header */
7413 for (auto&& instr : loop_entry->instructions) {
7414 if (instr->opcode == aco_opcode::p_linear_phi) {
7415 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
7416 new_phi->definitions[0] = instr->definitions[0];
7417 for (unsigned i = 0; i < new_phi->operands.size(); i++)
7418 new_phi->operands[i] = instr->operands[i];
7419 /* check that the remaining operands are all the same */
7420 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
7421 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
7422 instr.swap(new_phi);
7423 } else if (instr->opcode == aco_opcode::p_phi) {
7424 continue;
7425 } else {
7426 break;
7427 }
7428 }
7429 #endif
7430 }
7431
7432 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
7433 {
7434 ic->cond = cond;
7435
7436 append_logical_end(ctx->block);
7437 ctx->block->kind |= block_kind_branch;
7438
7439 /* branch to linear then block */
7440 assert(cond.regClass() == ctx->program->lane_mask);
7441 aco_ptr<Pseudo_branch_instruction> branch;
7442 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
7443 branch->operands[0] = Operand(cond);
7444 ctx->block->instructions.push_back(std::move(branch));
7445
7446 ic->BB_if_idx = ctx->block->index;
7447 ic->BB_invert = Block();
7448 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7449 /* Invert blocks are intentionally not marked as top level because they
7450 * are not part of the logical cfg. */
7451 ic->BB_invert.kind |= block_kind_invert;
7452 ic->BB_endif = Block();
7453 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7454 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
7455
7456 ic->exec_potentially_empty_old = ctx->cf_info.exec_potentially_empty;
7457 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
7458 ctx->cf_info.parent_if.is_divergent = true;
7459 ctx->cf_info.exec_potentially_empty = false; /* divergent branches use cbranch_execz */
7460
7461 /** emit logical then block */
7462 Block* BB_then_logical = ctx->program->create_and_insert_block();
7463 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7464 add_edge(ic->BB_if_idx, BB_then_logical);
7465 ctx->block = BB_then_logical;
7466 append_logical_start(BB_then_logical);
7467 }
7468
7469 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
7470 {
7471 Block *BB_then_logical = ctx->block;
7472 append_logical_end(BB_then_logical);
7473 /* branch from logical then block to invert block */
7474 aco_ptr<Pseudo_branch_instruction> branch;
7475 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7476 BB_then_logical->instructions.emplace_back(std::move(branch));
7477 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
7478 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7479 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
7480 BB_then_logical->kind |= block_kind_uniform;
7481 assert(!ctx->cf_info.has_branch);
7482 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
7483 ctx->cf_info.parent_loop.has_divergent_branch = false;
7484
7485 /** emit linear then block */
7486 Block* BB_then_linear = ctx->program->create_and_insert_block();
7487 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7488 BB_then_linear->kind |= block_kind_uniform;
7489 add_linear_edge(ic->BB_if_idx, BB_then_linear);
7490 /* branch from linear then block to invert block */
7491 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7492 BB_then_linear->instructions.emplace_back(std::move(branch));
7493 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
7494
7495 /** emit invert merge block */
7496 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
7497 ic->invert_idx = ctx->block->index;
7498
7499 /* branch to linear else block (skip else) */
7500 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
7501 branch->operands[0] = Operand(ic->cond);
7502 ctx->block->instructions.push_back(std::move(branch));
7503
7504 ic->exec_potentially_empty_old |= ctx->cf_info.exec_potentially_empty;
7505 ctx->cf_info.exec_potentially_empty = false; /* divergent branches use cbranch_execz */
7506
7507 /** emit logical else block */
7508 Block* BB_else_logical = ctx->program->create_and_insert_block();
7509 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7510 add_logical_edge(ic->BB_if_idx, BB_else_logical);
7511 add_linear_edge(ic->invert_idx, BB_else_logical);
7512 ctx->block = BB_else_logical;
7513 append_logical_start(BB_else_logical);
7514 }
7515
7516 static void end_divergent_if(isel_context *ctx, if_context *ic)
7517 {
7518 Block *BB_else_logical = ctx->block;
7519 append_logical_end(BB_else_logical);
7520
7521 /* branch from logical else block to endif block */
7522 aco_ptr<Pseudo_branch_instruction> branch;
7523 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7524 BB_else_logical->instructions.emplace_back(std::move(branch));
7525 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
7526 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7527 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
7528 BB_else_logical->kind |= block_kind_uniform;
7529
7530 assert(!ctx->cf_info.has_branch);
7531 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
7532
7533
7534 /** emit linear else block */
7535 Block* BB_else_linear = ctx->program->create_and_insert_block();
7536 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7537 BB_else_linear->kind |= block_kind_uniform;
7538 add_linear_edge(ic->invert_idx, BB_else_linear);
7539
7540 /* branch from linear else block to endif block */
7541 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7542 BB_else_linear->instructions.emplace_back(std::move(branch));
7543 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
7544
7545
7546 /** emit endif merge block */
7547 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
7548 append_logical_start(ctx->block);
7549
7550
7551 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
7552 ctx->cf_info.exec_potentially_empty |= ic->exec_potentially_empty_old;
7553 /* uniform control flow never has an empty exec-mask */
7554 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
7555 ctx->cf_info.exec_potentially_empty = false;
7556 }
7557
7558 static void visit_if(isel_context *ctx, nir_if *if_stmt)
7559 {
7560 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
7561 Builder bld(ctx->program, ctx->block);
7562 aco_ptr<Pseudo_branch_instruction> branch;
7563
7564 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
7565 /**
7566 * Uniform conditionals are represented in the following way*) :
7567 *
7568 * The linear and logical CFG:
7569 * BB_IF
7570 * / \
7571 * BB_THEN (logical) BB_ELSE (logical)
7572 * \ /
7573 * BB_ENDIF
7574 *
7575 * *) Exceptions may be due to break and continue statements within loops
7576 * If a break/continue happens within uniform control flow, it branches
7577 * to the loop exit/entry block. Otherwise, it branches to the next
7578 * merge block.
7579 **/
7580 append_logical_end(ctx->block);
7581 ctx->block->kind |= block_kind_uniform;
7582
7583 /* emit branch */
7584 assert(cond.regClass() == bld.lm);
7585 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7586 cond = bool_to_scalar_condition(ctx, cond);
7587
7588 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
7589 branch->operands[0] = Operand(cond);
7590 branch->operands[0].setFixed(scc);
7591 ctx->block->instructions.emplace_back(std::move(branch));
7592
7593 unsigned BB_if_idx = ctx->block->index;
7594 Block BB_endif = Block();
7595 BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
7596 BB_endif.kind |= ctx->block->kind & block_kind_top_level;
7597
7598 /** emit then block */
7599 Block* BB_then = ctx->program->create_and_insert_block();
7600 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7601 add_edge(BB_if_idx, BB_then);
7602 append_logical_start(BB_then);
7603 ctx->block = BB_then;
7604 visit_cf_list(ctx, &if_stmt->then_list);
7605 BB_then = ctx->block;
7606 bool then_branch = ctx->cf_info.has_branch;
7607 bool then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
7608
7609 if (!then_branch) {
7610 append_logical_end(BB_then);
7611 /* branch from then block to endif block */
7612 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7613 BB_then->instructions.emplace_back(std::move(branch));
7614 add_linear_edge(BB_then->index, &BB_endif);
7615 if (!then_branch_divergent)
7616 add_logical_edge(BB_then->index, &BB_endif);
7617 BB_then->kind |= block_kind_uniform;
7618 }
7619
7620 ctx->cf_info.has_branch = false;
7621 ctx->cf_info.parent_loop.has_divergent_branch = false;
7622
7623 /** emit else block */
7624 Block* BB_else = ctx->program->create_and_insert_block();
7625 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
7626 add_edge(BB_if_idx, BB_else);
7627 append_logical_start(BB_else);
7628 ctx->block = BB_else;
7629 visit_cf_list(ctx, &if_stmt->else_list);
7630 BB_else = ctx->block;
7631
7632 if (!ctx->cf_info.has_branch) {
7633 append_logical_end(BB_else);
7634 /* branch from then block to endif block */
7635 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
7636 BB_else->instructions.emplace_back(std::move(branch));
7637 add_linear_edge(BB_else->index, &BB_endif);
7638 if (!ctx->cf_info.parent_loop.has_divergent_branch)
7639 add_logical_edge(BB_else->index, &BB_endif);
7640 BB_else->kind |= block_kind_uniform;
7641 }
7642
7643 ctx->cf_info.has_branch &= then_branch;
7644 ctx->cf_info.parent_loop.has_divergent_branch &= then_branch_divergent;
7645
7646 /** emit endif merge block */
7647 if (!ctx->cf_info.has_branch) {
7648 ctx->block = ctx->program->insert_block(std::move(BB_endif));
7649 append_logical_start(ctx->block);
7650 }
7651 } else { /* non-uniform condition */
7652 /**
7653 * To maintain a logical and linear CFG without critical edges,
7654 * non-uniform conditionals are represented in the following way*) :
7655 *
7656 * The linear CFG:
7657 * BB_IF
7658 * / \
7659 * BB_THEN (logical) BB_THEN (linear)
7660 * \ /
7661 * BB_INVERT (linear)
7662 * / \
7663 * BB_ELSE (logical) BB_ELSE (linear)
7664 * \ /
7665 * BB_ENDIF
7666 *
7667 * The logical CFG:
7668 * BB_IF
7669 * / \
7670 * BB_THEN (logical) BB_ELSE (logical)
7671 * \ /
7672 * BB_ENDIF
7673 *
7674 * *) Exceptions may be due to break and continue statements within loops
7675 **/
7676
7677 if_context ic;
7678
7679 begin_divergent_if_then(ctx, &ic, cond);
7680 visit_cf_list(ctx, &if_stmt->then_list);
7681
7682 begin_divergent_if_else(ctx, &ic);
7683 visit_cf_list(ctx, &if_stmt->else_list);
7684
7685 end_divergent_if(ctx, &ic);
7686 }
7687 }
7688
7689 static void visit_cf_list(isel_context *ctx,
7690 struct exec_list *list)
7691 {
7692 foreach_list_typed(nir_cf_node, node, node, list) {
7693 switch (node->type) {
7694 case nir_cf_node_block:
7695 visit_block(ctx, nir_cf_node_as_block(node));
7696 break;
7697 case nir_cf_node_if:
7698 visit_if(ctx, nir_cf_node_as_if(node));
7699 break;
7700 case nir_cf_node_loop:
7701 visit_loop(ctx, nir_cf_node_as_loop(node));
7702 break;
7703 default:
7704 unreachable("unimplemented cf list type");
7705 }
7706 }
7707 }
7708
7709 static void export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
7710 {
7711 int offset = ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
7712 uint64_t mask = ctx->vs_output.mask[slot];
7713 if (!is_pos && !mask)
7714 return;
7715 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
7716 return;
7717 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
7718 exp->enabled_mask = mask;
7719 for (unsigned i = 0; i < 4; ++i) {
7720 if (mask & (1 << i))
7721 exp->operands[i] = Operand(ctx->vs_output.outputs[slot][i]);
7722 else
7723 exp->operands[i] = Operand(v1);
7724 }
7725 exp->valid_mask = false;
7726 exp->done = false;
7727 exp->compressed = false;
7728 if (is_pos)
7729 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
7730 else
7731 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
7732 ctx->block->instructions.emplace_back(std::move(exp));
7733 }
7734
7735 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
7736 {
7737 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
7738 exp->enabled_mask = 0;
7739 for (unsigned i = 0; i < 4; ++i)
7740 exp->operands[i] = Operand(v1);
7741 if (ctx->vs_output.mask[VARYING_SLOT_PSIZ]) {
7742 exp->operands[0] = Operand(ctx->vs_output.outputs[VARYING_SLOT_PSIZ][0]);
7743 exp->enabled_mask |= 0x1;
7744 }
7745 if (ctx->vs_output.mask[VARYING_SLOT_LAYER]) {
7746 exp->operands[2] = Operand(ctx->vs_output.outputs[VARYING_SLOT_LAYER][0]);
7747 exp->enabled_mask |= 0x4;
7748 }
7749 if (ctx->vs_output.mask[VARYING_SLOT_VIEWPORT]) {
7750 if (ctx->options->chip_class < GFX9) {
7751 exp->operands[3] = Operand(ctx->vs_output.outputs[VARYING_SLOT_VIEWPORT][0]);
7752 exp->enabled_mask |= 0x8;
7753 } else {
7754 Builder bld(ctx->program, ctx->block);
7755
7756 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
7757 Operand(ctx->vs_output.outputs[VARYING_SLOT_VIEWPORT][0]));
7758 if (exp->operands[2].isTemp())
7759 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
7760
7761 exp->operands[2] = Operand(out);
7762 exp->enabled_mask |= 0x4;
7763 }
7764 }
7765 exp->valid_mask = false;
7766 exp->done = false;
7767 exp->compressed = false;
7768 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
7769 ctx->block->instructions.emplace_back(std::move(exp));
7770 }
7771
7772 static void create_vs_exports(isel_context *ctx)
7773 {
7774 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
7775
7776 if (outinfo->export_prim_id) {
7777 ctx->vs_output.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
7778 ctx->vs_output.outputs[VARYING_SLOT_PRIMITIVE_ID][0] = get_arg(ctx, ctx->args->vs_prim_id);
7779 }
7780
7781 if (ctx->options->key.has_multiview_view_index) {
7782 ctx->vs_output.mask[VARYING_SLOT_LAYER] |= 0x1;
7783 ctx->vs_output.outputs[VARYING_SLOT_LAYER][0] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
7784 }
7785
7786 /* the order these position exports are created is important */
7787 int next_pos = 0;
7788 export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
7789 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
7790 export_vs_psiz_layer_viewport(ctx, &next_pos);
7791 }
7792 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
7793 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
7794 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
7795 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
7796
7797 if (ctx->options->key.vs_common_out.export_clip_dists) {
7798 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
7799 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
7800 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
7801 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
7802 }
7803
7804 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
7805 if (i < VARYING_SLOT_VAR0 && i != VARYING_SLOT_LAYER &&
7806 i != VARYING_SLOT_PRIMITIVE_ID)
7807 continue;
7808
7809 export_vs_varying(ctx, i, false, NULL);
7810 }
7811 }
7812
7813 static void emit_stream_output(isel_context *ctx,
7814 Temp const *so_buffers,
7815 Temp const *so_write_offset,
7816 const struct radv_stream_output *output)
7817 {
7818 unsigned num_comps = util_bitcount(output->component_mask);
7819 unsigned loc = output->location;
7820 unsigned buf = output->buffer;
7821 unsigned offset = output->offset;
7822
7823 assert(num_comps && num_comps <= 4);
7824 if (!num_comps || num_comps > 4)
7825 return;
7826
7827 unsigned start = ffs(output->component_mask) - 1;
7828
7829 Temp out[4];
7830 bool all_undef = true;
7831 assert(ctx->stage == vertex_vs);
7832 for (unsigned i = 0; i < num_comps; i++) {
7833 out[i] = ctx->vs_output.outputs[loc][start + i];
7834 all_undef = all_undef && !out[i].id();
7835 }
7836 if (all_undef)
7837 return;
7838
7839 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_comps)};
7840 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_comps, 1)};
7841 for (unsigned i = 0; i < num_comps; ++i)
7842 vec->operands[i] = (ctx->vs_output.mask[loc] & 1 << i) ? Operand(out[i]) : Operand(0u);
7843 vec->definitions[0] = Definition(write_data);
7844 ctx->block->instructions.emplace_back(std::move(vec));
7845
7846 aco_opcode opcode;
7847 switch (num_comps) {
7848 case 1:
7849 opcode = aco_opcode::buffer_store_dword;
7850 break;
7851 case 2:
7852 opcode = aco_opcode::buffer_store_dwordx2;
7853 break;
7854 case 3:
7855 opcode = aco_opcode::buffer_store_dwordx3;
7856 break;
7857 case 4:
7858 opcode = aco_opcode::buffer_store_dwordx4;
7859 break;
7860 }
7861
7862 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
7863 store->operands[0] = Operand(so_write_offset[buf]);
7864 store->operands[1] = Operand(so_buffers[buf]);
7865 store->operands[2] = Operand((uint32_t) 0);
7866 store->operands[3] = Operand(write_data);
7867 if (offset > 4095) {
7868 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7869 Builder bld(ctx->program, ctx->block);
7870 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
7871 } else {
7872 store->offset = offset;
7873 }
7874 store->offen = true;
7875 store->glc = true;
7876 store->dlc = false;
7877 store->slc = true;
7878 store->can_reorder = true;
7879 ctx->block->instructions.emplace_back(std::move(store));
7880 }
7881
7882 static void emit_streamout(isel_context *ctx, unsigned stream)
7883 {
7884 Builder bld(ctx->program, ctx->block);
7885
7886 Temp so_buffers[4];
7887 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
7888 for (unsigned i = 0; i < 4; i++) {
7889 unsigned stride = ctx->program->info->so.strides[i];
7890 if (!stride)
7891 continue;
7892
7893 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, Operand(i * 16u));
7894 }
7895
7896 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7897 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
7898
7899 Temp tid = emit_mbcnt(ctx, bld.def(v1));
7900
7901 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(s2), so_vtx_count, tid);
7902
7903 if_context ic;
7904 begin_divergent_if_then(ctx, &ic, can_emit);
7905
7906 bld.reset(ctx->block);
7907
7908 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
7909
7910 Temp so_write_offset[4];
7911
7912 for (unsigned i = 0; i < 4; i++) {
7913 unsigned stride = ctx->program->info->so.strides[i];
7914 if (!stride)
7915 continue;
7916
7917 if (stride == 1) {
7918 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
7919 get_arg(ctx, ctx->args->streamout_write_idx),
7920 get_arg(ctx, ctx->args->streamout_offset[i]));
7921 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
7922
7923 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
7924 } else {
7925 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
7926 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
7927 get_arg(ctx, ctx->args->streamout_offset[i]));
7928 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
7929 }
7930 }
7931
7932 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
7933 struct radv_stream_output *output =
7934 &ctx->program->info->so.outputs[i];
7935 if (stream != output->stream)
7936 continue;
7937
7938 emit_stream_output(ctx, so_buffers, so_write_offset, output);
7939 }
7940
7941 begin_divergent_if_else(ctx, &ic);
7942 end_divergent_if(ctx, &ic);
7943 }
7944
7945 } /* end namespace */
7946
7947 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
7948 {
7949 /* Split all arguments except for the first (ring_offsets) and the last
7950 * (exec) so that the dead channels don't stay live throughout the program.
7951 */
7952 for (unsigned i = 1; i < startpgm->definitions.size() - 1; i++) {
7953 if (startpgm->definitions[i].regClass().size() > 1) {
7954 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
7955 startpgm->definitions[i].regClass().size());
7956 }
7957 }
7958 }
7959
7960 void handle_bc_optimize(isel_context *ctx)
7961 {
7962 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7963 Builder bld(ctx->program, ctx->block);
7964 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
7965 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
7966 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
7967 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
7968 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
7969 if (uses_center && uses_centroid) {
7970 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
7971 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
7972
7973 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
7974 Temp new_coord[2];
7975 for (unsigned i = 0; i < 2; i++) {
7976 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
7977 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
7978 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7979 persp_centroid, persp_center, sel);
7980 }
7981 ctx->persp_centroid = bld.tmp(v2);
7982 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
7983 Operand(new_coord[0]), Operand(new_coord[1]));
7984 emit_split_vector(ctx, ctx->persp_centroid, 2);
7985 }
7986
7987 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
7988 Temp new_coord[2];
7989 for (unsigned i = 0; i < 2; i++) {
7990 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
7991 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
7992 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7993 linear_centroid, linear_center, sel);
7994 }
7995 ctx->linear_centroid = bld.tmp(v2);
7996 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
7997 Operand(new_coord[0]), Operand(new_coord[1]));
7998 emit_split_vector(ctx, ctx->linear_centroid, 2);
7999 }
8000 }
8001 }
8002
8003 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
8004 {
8005 Program *program = ctx->program;
8006
8007 unsigned float_controls = shader->info.float_controls_execution_mode;
8008
8009 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
8010 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
8011 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
8012 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
8013 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
8014
8015 program->next_fp_mode.must_flush_denorms32 =
8016 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
8017 program->next_fp_mode.must_flush_denorms16_64 =
8018 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
8019 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
8020
8021 program->next_fp_mode.care_about_round32 =
8022 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
8023
8024 program->next_fp_mode.care_about_round16_64 =
8025 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
8026 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
8027
8028 /* default to preserving fp16 and fp64 denorms, since it's free */
8029 if (program->next_fp_mode.must_flush_denorms16_64)
8030 program->next_fp_mode.denorm16_64 = 0;
8031 else
8032 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
8033
8034 /* preserving fp32 denorms is expensive, so only do it if asked */
8035 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
8036 program->next_fp_mode.denorm32 = fp_denorm_keep;
8037 else
8038 program->next_fp_mode.denorm32 = 0;
8039
8040 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
8041 program->next_fp_mode.round32 = fp_round_tz;
8042 else
8043 program->next_fp_mode.round32 = fp_round_ne;
8044
8045 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
8046 program->next_fp_mode.round16_64 = fp_round_tz;
8047 else
8048 program->next_fp_mode.round16_64 = fp_round_ne;
8049
8050 ctx->block->fp_mode = program->next_fp_mode;
8051 }
8052
8053 void select_program(Program *program,
8054 unsigned shader_count,
8055 struct nir_shader *const *shaders,
8056 ac_shader_config* config,
8057 struct radv_shader_args *args)
8058 {
8059 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args);
8060
8061 for (unsigned i = 0; i < shader_count; i++) {
8062 nir_shader *nir = shaders[i];
8063 init_context(&ctx, nir);
8064
8065 setup_fp_mode(&ctx, nir);
8066
8067 if (!i) {
8068 /* needs to be after init_context() for FS */
8069 Pseudo_instruction *startpgm = add_startpgm(&ctx);
8070 append_logical_start(ctx.block);
8071 split_arguments(&ctx, startpgm);
8072 }
8073
8074 if_context ic;
8075 if (shader_count >= 2) {
8076 Builder bld(ctx.program, ctx.block);
8077 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)));
8078 Temp thread_id = emit_mbcnt(&ctx, bld.def(v1));
8079 Temp cond = bld.vopc(aco_opcode::v_cmp_gt_u32, bld.hint_vcc(bld.def(bld.lm)), count, thread_id);
8080
8081 begin_divergent_if_then(&ctx, &ic, cond);
8082 }
8083
8084 if (i) {
8085 Builder bld(ctx.program, ctx.block);
8086 bld.barrier(aco_opcode::p_memory_barrier_shared); //TODO: different barriers are needed for different stages
8087 bld.sopp(aco_opcode::s_barrier);
8088 }
8089
8090 if (ctx.stage == fragment_fs)
8091 handle_bc_optimize(&ctx);
8092
8093 nir_function_impl *func = nir_shader_get_entrypoint(nir);
8094 visit_cf_list(&ctx, &func->body);
8095
8096 if (ctx.program->info->so.num_outputs/*&& !ctx->is_gs_copy_shader */)
8097 emit_streamout(&ctx, 0);
8098
8099 if (ctx.stage == vertex_vs)
8100 create_vs_exports(&ctx);
8101
8102 if (shader_count >= 2) {
8103 begin_divergent_if_else(&ctx, &ic);
8104 end_divergent_if(&ctx, &ic);
8105 }
8106
8107 ralloc_free(ctx.divergent_vals);
8108 }
8109
8110 program->config->float_mode = program->blocks[0].fp_mode.val;
8111
8112 append_logical_end(ctx.block);
8113 ctx.block->kind |= block_kind_uniform;
8114 Builder bld(ctx.program, ctx.block);
8115 if (ctx.program->wb_smem_l1_on_end)
8116 bld.smem(aco_opcode::s_dcache_wb, false);
8117 bld.sopp(aco_opcode::s_endpgm);
8118
8119 /* cleanup CFG */
8120 for (Block& BB : program->blocks) {
8121 for (unsigned idx : BB.linear_preds)
8122 program->blocks[idx].linear_succs.emplace_back(BB.index);
8123 for (unsigned idx : BB.logical_preds)
8124 program->blocks[idx].logical_succs.emplace_back(BB.index);
8125 }
8126 }
8127 }