716853d23ce998dcf8a5c8c7f1325babb8a644b2
[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 <stack>
29 #include <map>
30
31 #include "ac_shader_util.h"
32 #include "aco_ir.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
37
38 namespace aco {
39 namespace {
40
41 class loop_info_RAII {
42 isel_context* ctx;
43 unsigned header_idx_old;
44 Block* exit_old;
45 bool divergent_cont_old;
46 bool divergent_branch_old;
47 bool divergent_if_old;
48
49 public:
50 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
51 : ctx(ctx),
52 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
53 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
54 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
55 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
56 {
57 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
58 ctx->cf_info.parent_loop.exit = loop_exit;
59 ctx->cf_info.parent_loop.has_divergent_continue = false;
60 ctx->cf_info.parent_loop.has_divergent_branch = false;
61 ctx->cf_info.parent_if.is_divergent = false;
62 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
63 }
64
65 ~loop_info_RAII()
66 {
67 ctx->cf_info.parent_loop.header_idx = header_idx_old;
68 ctx->cf_info.parent_loop.exit = exit_old;
69 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
70 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
71 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
72 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
73 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
74 ctx->cf_info.exec_potentially_empty_discard = false;
75 }
76 };
77
78 struct if_context {
79 Temp cond;
80
81 bool divergent_old;
82 bool exec_potentially_empty_discard_old;
83 bool exec_potentially_empty_break_old;
84 uint16_t exec_potentially_empty_break_depth_old;
85
86 unsigned BB_if_idx;
87 unsigned invert_idx;
88 bool then_branch_divergent;
89 Block BB_invert;
90 Block BB_endif;
91 };
92
93 static bool visit_cf_list(struct isel_context *ctx,
94 struct exec_list *list);
95
96 static void add_logical_edge(unsigned pred_idx, Block *succ)
97 {
98 succ->logical_preds.emplace_back(pred_idx);
99 }
100
101
102 static void add_linear_edge(unsigned pred_idx, Block *succ)
103 {
104 succ->linear_preds.emplace_back(pred_idx);
105 }
106
107 static void add_edge(unsigned pred_idx, Block *succ)
108 {
109 add_logical_edge(pred_idx, succ);
110 add_linear_edge(pred_idx, succ);
111 }
112
113 static void append_logical_start(Block *b)
114 {
115 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
116 }
117
118 static void append_logical_end(Block *b)
119 {
120 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
121 }
122
123 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
124 {
125 assert(ctx->allocated[def->index].id());
126 return ctx->allocated[def->index];
127 }
128
129 Temp emit_mbcnt(isel_context *ctx, Definition dst,
130 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
131 {
132 Builder bld(ctx->program, ctx->block);
133 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
134 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
135
136 if (ctx->program->wave_size == 32) {
137 return thread_id_lo;
138 } else {
139 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
140 return thread_id_hi;
141 }
142 }
143
144 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
145 {
146 Builder bld(ctx->program, ctx->block);
147
148 if (!dst.id())
149 dst = bld.tmp(src.regClass());
150
151 assert(src.size() == dst.size());
152
153 if (ctx->stage != fragment_fs) {
154 if (!dst.id())
155 return src;
156
157 bld.copy(Definition(dst), src);
158 return dst;
159 }
160
161 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
162 ctx->program->needs_wqm |= program_needs_wqm;
163 return dst;
164 }
165
166 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
167 {
168 if (index.regClass() == s1)
169 return bld.readlane(bld.def(s1), data, index);
170
171 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
172
173 /* Currently not implemented on GFX6-7 */
174 assert(ctx->options->chip_class >= GFX8);
175
176 if (ctx->options->chip_class <= GFX9 || ctx->program->wave_size == 32) {
177 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
178 }
179
180 /* GFX10, wave64 mode:
181 * The bpermute instruction is limited to half-wave operation, which means that it can't
182 * properly support subgroup shuffle like older generations (or wave32 mode), so we
183 * emulate it here.
184 */
185 if (!ctx->has_gfx10_wave64_bpermute) {
186 ctx->has_gfx10_wave64_bpermute = true;
187 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
188 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
189 }
190
191 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
192 Temp lane_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), lane_id);
193 Temp index_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), index);
194 Temp cmp = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm, vcc), lane_is_hi, index_is_hi);
195
196 return bld.reduction(aco_opcode::p_wave64_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc),
197 bld.vcc(cmp), Operand(v2.as_linear()), index_x4, data, gfx10_wave64_bpermute);
198 }
199
200 Temp as_vgpr(isel_context *ctx, Temp val)
201 {
202 if (val.type() == RegType::sgpr) {
203 Builder bld(ctx->program, ctx->block);
204 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
205 }
206 assert(val.type() == RegType::vgpr);
207 return val;
208 }
209
210 //assumes a != 0xffffffff
211 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
212 {
213 assert(b != 0);
214 Builder bld(ctx->program, ctx->block);
215
216 if (util_is_power_of_two_or_zero(b)) {
217 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
218 return;
219 }
220
221 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
222
223 assert(info.multiplier <= 0xffffffff);
224
225 bool pre_shift = info.pre_shift != 0;
226 bool increment = info.increment != 0;
227 bool multiply = true;
228 bool post_shift = info.post_shift != 0;
229
230 if (!pre_shift && !increment && !multiply && !post_shift) {
231 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
232 return;
233 }
234
235 Temp pre_shift_dst = a;
236 if (pre_shift) {
237 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
238 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
239 }
240
241 Temp increment_dst = pre_shift_dst;
242 if (increment) {
243 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
244 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
245 }
246
247 Temp multiply_dst = increment_dst;
248 if (multiply) {
249 multiply_dst = post_shift ? bld.tmp(v1) : dst;
250 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
251 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
252 }
253
254 if (post_shift) {
255 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
256 }
257 }
258
259 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
260 {
261 Builder bld(ctx->program, ctx->block);
262 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
263 }
264
265
266 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
267 {
268 /* no need to extract the whole vector */
269 if (src.regClass() == dst_rc) {
270 assert(idx == 0);
271 return src;
272 }
273 assert(src.size() > idx);
274 Builder bld(ctx->program, ctx->block);
275 auto it = ctx->allocated_vec.find(src.id());
276 /* the size check needs to be early because elements other than 0 may be garbage */
277 if (it != ctx->allocated_vec.end() && it->second[0].size() == dst_rc.size()) {
278 if (it->second[idx].regClass() == dst_rc) {
279 return it->second[idx];
280 } else {
281 assert(dst_rc.size() == it->second[idx].regClass().size());
282 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
283 return bld.copy(bld.def(dst_rc), it->second[idx]);
284 }
285 }
286
287 if (src.size() == dst_rc.size()) {
288 assert(idx == 0);
289 return bld.copy(bld.def(dst_rc), src);
290 } else {
291 Temp dst = bld.tmp(dst_rc);
292 emit_extract_vector(ctx, src, idx, dst);
293 return dst;
294 }
295 }
296
297 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
298 {
299 if (num_components == 1)
300 return;
301 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
302 return;
303 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
304 split->operands[0] = Operand(vec_src);
305 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
306 for (unsigned i = 0; i < num_components; i++) {
307 elems[i] = {ctx->program->allocateId(), RegClass(vec_src.type(), vec_src.size() / num_components)};
308 split->definitions[i] = Definition(elems[i]);
309 }
310 ctx->block->instructions.emplace_back(std::move(split));
311 ctx->allocated_vec.emplace(vec_src.id(), elems);
312 }
313
314 /* This vector expansion uses a mask to determine which elements in the new vector
315 * come from the original vector. The other elements are undefined. */
316 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
317 {
318 emit_split_vector(ctx, vec_src, util_bitcount(mask));
319
320 if (vec_src == dst)
321 return;
322
323 Builder bld(ctx->program, ctx->block);
324 if (num_components == 1) {
325 if (dst.type() == RegType::sgpr)
326 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
327 else
328 bld.copy(Definition(dst), vec_src);
329 return;
330 }
331
332 unsigned component_size = dst.size() / num_components;
333 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
334
335 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
336 vec->definitions[0] = Definition(dst);
337 unsigned k = 0;
338 for (unsigned i = 0; i < num_components; i++) {
339 if (mask & (1 << i)) {
340 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
341 if (dst.type() == RegType::sgpr)
342 src = bld.as_uniform(src);
343 vec->operands[i] = Operand(src);
344 } else {
345 vec->operands[i] = Operand(0u);
346 }
347 elems[i] = vec->operands[i].getTemp();
348 }
349 ctx->block->instructions.emplace_back(std::move(vec));
350 ctx->allocated_vec.emplace(dst.id(), elems);
351 }
352
353 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
354 {
355 Builder bld(ctx->program, ctx->block);
356 if (!dst.id())
357 dst = bld.tmp(bld.lm);
358
359 assert(val.regClass() == s1);
360 assert(dst.regClass() == bld.lm);
361
362 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
363 }
364
365 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
366 {
367 Builder bld(ctx->program, ctx->block);
368 if (!dst.id())
369 dst = bld.tmp(s1);
370
371 assert(val.regClass() == bld.lm);
372 assert(dst.regClass() == s1);
373
374 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
375 Temp tmp = bld.tmp(s1);
376 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
377 return emit_wqm(ctx, tmp, dst);
378 }
379
380 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
381 {
382 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
383 return get_ssa_temp(ctx, src.src.ssa);
384
385 if (src.src.ssa->num_components == size) {
386 bool identity_swizzle = true;
387 for (unsigned i = 0; identity_swizzle && i < size; i++) {
388 if (src.swizzle[i] != i)
389 identity_swizzle = false;
390 }
391 if (identity_swizzle)
392 return get_ssa_temp(ctx, src.src.ssa);
393 }
394
395 Temp vec = get_ssa_temp(ctx, src.src.ssa);
396 unsigned elem_size = vec.size() / src.src.ssa->num_components;
397 assert(elem_size > 0); /* TODO: 8 and 16-bit vectors not supported */
398 assert(vec.size() % elem_size == 0);
399
400 RegClass elem_rc = RegClass(vec.type(), elem_size);
401 if (size == 1) {
402 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
403 } else {
404 assert(size <= 4);
405 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
406 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
407 for (unsigned i = 0; i < size; ++i) {
408 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
409 vec_instr->operands[i] = Operand{elems[i]};
410 }
411 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size)};
412 vec_instr->definitions[0] = Definition(dst);
413 ctx->block->instructions.emplace_back(std::move(vec_instr));
414 ctx->allocated_vec.emplace(dst.id(), elems);
415 return dst;
416 }
417 }
418
419 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
420 {
421 if (ptr.size() == 2)
422 return ptr;
423 Builder bld(ctx->program, ctx->block);
424 if (ptr.type() == RegType::vgpr)
425 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
426 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
427 ptr, Operand((unsigned)ctx->options->address32_hi));
428 }
429
430 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
431 {
432 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
433 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
434 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
435 sop2->definitions[0] = Definition(dst);
436 if (writes_scc)
437 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
438 ctx->block->instructions.emplace_back(std::move(sop2));
439 }
440
441 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
442 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
443 {
444 Builder bld(ctx->program, ctx->block);
445 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
446 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
447 if (src1.type() == RegType::sgpr) {
448 if (commutative && src0.type() == RegType::vgpr) {
449 Temp t = src0;
450 src0 = src1;
451 src1 = t;
452 } else if (src0.type() == RegType::vgpr &&
453 op != aco_opcode::v_madmk_f32 &&
454 op != aco_opcode::v_madak_f32 &&
455 op != aco_opcode::v_madmk_f16 &&
456 op != aco_opcode::v_madak_f16) {
457 /* If the instruction is not commutative, we emit a VOP3A instruction */
458 bld.vop2_e64(op, Definition(dst), src0, src1);
459 return;
460 } else {
461 src1 = bld.copy(bld.def(RegType::vgpr, src1.size()), src1); //TODO: as_vgpr
462 }
463 }
464
465 if (flush_denorms && ctx->program->chip_class < GFX9) {
466 assert(dst.size() == 1);
467 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
468 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
469 } else {
470 bld.vop2(op, Definition(dst), src0, src1);
471 }
472 }
473
474 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
475 bool flush_denorms = false)
476 {
477 Temp src0 = get_alu_src(ctx, instr->src[0]);
478 Temp src1 = get_alu_src(ctx, instr->src[1]);
479 Temp src2 = get_alu_src(ctx, instr->src[2]);
480
481 /* ensure that the instruction has at most 1 sgpr operand
482 * The optimizer will inline constants for us */
483 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
484 src0 = as_vgpr(ctx, src0);
485 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
486 src1 = as_vgpr(ctx, src1);
487 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
488 src2 = as_vgpr(ctx, src2);
489
490 Builder bld(ctx->program, ctx->block);
491 if (flush_denorms && ctx->program->chip_class < GFX9) {
492 assert(dst.size() == 1);
493 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
494 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
495 } else {
496 bld.vop3(op, Definition(dst), src0, src1, src2);
497 }
498 }
499
500 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
501 {
502 Builder bld(ctx->program, ctx->block);
503 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
504 }
505
506 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
507 {
508 Temp src0 = get_alu_src(ctx, instr->src[0]);
509 Temp src1 = get_alu_src(ctx, instr->src[1]);
510 assert(src0.size() == src1.size());
511
512 aco_ptr<Instruction> vopc;
513 if (src1.type() == RegType::sgpr) {
514 if (src0.type() == RegType::vgpr) {
515 /* to swap the operands, we might also have to change the opcode */
516 switch (op) {
517 case aco_opcode::v_cmp_lt_f32:
518 op = aco_opcode::v_cmp_gt_f32;
519 break;
520 case aco_opcode::v_cmp_ge_f32:
521 op = aco_opcode::v_cmp_le_f32;
522 break;
523 case aco_opcode::v_cmp_lt_i32:
524 op = aco_opcode::v_cmp_gt_i32;
525 break;
526 case aco_opcode::v_cmp_ge_i32:
527 op = aco_opcode::v_cmp_le_i32;
528 break;
529 case aco_opcode::v_cmp_lt_u32:
530 op = aco_opcode::v_cmp_gt_u32;
531 break;
532 case aco_opcode::v_cmp_ge_u32:
533 op = aco_opcode::v_cmp_le_u32;
534 break;
535 case aco_opcode::v_cmp_lt_f64:
536 op = aco_opcode::v_cmp_gt_f64;
537 break;
538 case aco_opcode::v_cmp_ge_f64:
539 op = aco_opcode::v_cmp_le_f64;
540 break;
541 case aco_opcode::v_cmp_lt_i64:
542 op = aco_opcode::v_cmp_gt_i64;
543 break;
544 case aco_opcode::v_cmp_ge_i64:
545 op = aco_opcode::v_cmp_le_i64;
546 break;
547 case aco_opcode::v_cmp_lt_u64:
548 op = aco_opcode::v_cmp_gt_u64;
549 break;
550 case aco_opcode::v_cmp_ge_u64:
551 op = aco_opcode::v_cmp_le_u64;
552 break;
553 default: /* eq and ne are commutative */
554 break;
555 }
556 Temp t = src0;
557 src0 = src1;
558 src1 = t;
559 } else {
560 src1 = as_vgpr(ctx, src1);
561 }
562 }
563
564 Builder bld(ctx->program, ctx->block);
565 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
566 }
567
568 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
569 {
570 Temp src0 = get_alu_src(ctx, instr->src[0]);
571 Temp src1 = get_alu_src(ctx, instr->src[1]);
572 Builder bld(ctx->program, ctx->block);
573
574 assert(dst.regClass() == bld.lm);
575 assert(src0.type() == RegType::sgpr);
576 assert(src1.type() == RegType::sgpr);
577 assert(src0.regClass() == src1.regClass());
578
579 /* Emit the SALU comparison instruction */
580 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
581 /* Turn the result into a per-lane bool */
582 bool_to_vector_condition(ctx, cmp, dst);
583 }
584
585 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
586 aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
587 {
588 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : s32_op;
589 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : v32_op;
590 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
591 bool use_valu = s_op == aco_opcode::num_opcodes ||
592 divergent_vals ||
593 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
594 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
595 aco_opcode op = use_valu ? v_op : s_op;
596 assert(op != aco_opcode::num_opcodes);
597 assert(dst.regClass() == ctx->program->lane_mask);
598
599 if (use_valu)
600 emit_vopc_instruction(ctx, instr, op, dst);
601 else
602 emit_sopc_instruction(ctx, instr, op, dst);
603 }
604
605 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
606 {
607 Builder bld(ctx->program, ctx->block);
608 Temp src0 = get_alu_src(ctx, instr->src[0]);
609 Temp src1 = get_alu_src(ctx, instr->src[1]);
610
611 assert(dst.regClass() == bld.lm);
612 assert(src0.regClass() == bld.lm);
613 assert(src1.regClass() == bld.lm);
614
615 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
616 }
617
618 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
619 {
620 Builder bld(ctx->program, ctx->block);
621 Temp cond = get_alu_src(ctx, instr->src[0]);
622 Temp then = get_alu_src(ctx, instr->src[1]);
623 Temp els = get_alu_src(ctx, instr->src[2]);
624
625 assert(cond.regClass() == bld.lm);
626
627 if (dst.type() == RegType::vgpr) {
628 aco_ptr<Instruction> bcsel;
629 if (dst.size() == 1) {
630 then = as_vgpr(ctx, then);
631 els = as_vgpr(ctx, els);
632
633 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
634 } else if (dst.size() == 2) {
635 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
636 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
637 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
638 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
639
640 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
641 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
642
643 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
644 } else {
645 fprintf(stderr, "Unimplemented NIR instr bit size: ");
646 nir_print_instr(&instr->instr, stderr);
647 fprintf(stderr, "\n");
648 }
649 return;
650 }
651
652 if (instr->dest.dest.ssa.bit_size == 1) {
653 assert(dst.regClass() == bld.lm);
654 assert(then.regClass() == bld.lm);
655 assert(els.regClass() == bld.lm);
656 }
657
658 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
659 if (dst.regClass() == s1 || dst.regClass() == s2) {
660 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
661 assert(dst.size() == then.size());
662 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
663 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
664 } else {
665 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
666 nir_print_instr(&instr->instr, stderr);
667 fprintf(stderr, "\n");
668 }
669 return;
670 }
671
672 /* divergent boolean bcsel
673 * this implements bcsel on bools: dst = s0 ? s1 : s2
674 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
675 assert(instr->dest.dest.ssa.bit_size == 1);
676
677 if (cond.id() != then.id())
678 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
679
680 if (cond.id() == els.id())
681 bld.sop1(Builder::s_mov, Definition(dst), then);
682 else
683 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
684 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
685 }
686
687 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
688 aco_opcode op, uint32_t undo)
689 {
690 /* multiply by 16777216 to handle denormals */
691 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
692 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
693 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
694 scaled = bld.vop1(op, bld.def(v1), scaled);
695 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
696
697 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
698
699 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
700 }
701
702 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
703 {
704 if (ctx->block->fp_mode.denorm32 == 0) {
705 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
706 return;
707 }
708
709 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
710 }
711
712 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
713 {
714 if (ctx->block->fp_mode.denorm32 == 0) {
715 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
716 return;
717 }
718
719 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
720 }
721
722 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
723 {
724 if (ctx->block->fp_mode.denorm32 == 0) {
725 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
726 return;
727 }
728
729 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
730 }
731
732 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
733 {
734 if (ctx->block->fp_mode.denorm32 == 0) {
735 bld.vop1(aco_opcode::v_log_f32, dst, val);
736 return;
737 }
738
739 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
740 }
741
742 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
743 {
744 if (ctx->options->chip_class >= GFX7)
745 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
746
747 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
748 /* TODO: create more efficient code! */
749 if (val.type() == RegType::sgpr)
750 val = as_vgpr(ctx, val);
751
752 /* Split the input value. */
753 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
754 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
755
756 /* Extract the exponent and compute the unbiased value. */
757 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
758
759 /* Extract the fractional part. */
760 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
761 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
762
763 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
764 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
765
766 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
767 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
768 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
769 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
770 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
771
772 /* Get the sign bit. */
773 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
774
775 /* Decide the operation to apply depending on the unbiased exponent. */
776 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
777 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
778 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
779 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
780 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
781 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
782
783 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
784 }
785
786 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
787 {
788 if (ctx->options->chip_class >= GFX7)
789 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
790
791 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
792 Temp src0 = as_vgpr(ctx, val);
793
794 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
795 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
796
797 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
798 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
799 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
800
801 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
802 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
803 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
804 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
805
806 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
807 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
808
809 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
810
811 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
812 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
813
814 return add->definitions[0].getTemp();
815 }
816
817 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
818 {
819 if (!instr->dest.dest.is_ssa) {
820 fprintf(stderr, "nir alu dst not in ssa: ");
821 nir_print_instr(&instr->instr, stderr);
822 fprintf(stderr, "\n");
823 abort();
824 }
825 Builder bld(ctx->program, ctx->block);
826 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
827 switch(instr->op) {
828 case nir_op_vec2:
829 case nir_op_vec3:
830 case nir_op_vec4: {
831 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
832 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
833 for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; ++i) {
834 elems[i] = get_alu_src(ctx, instr->src[i]);
835 vec->operands[i] = Operand{elems[i]};
836 }
837 vec->definitions[0] = Definition(dst);
838 ctx->block->instructions.emplace_back(std::move(vec));
839 ctx->allocated_vec.emplace(dst.id(), elems);
840 break;
841 }
842 case nir_op_mov: {
843 Temp src = get_alu_src(ctx, instr->src[0]);
844 aco_ptr<Instruction> mov;
845 if (dst.type() == RegType::sgpr) {
846 if (src.type() == RegType::vgpr)
847 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
848 else if (src.regClass() == s1)
849 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
850 else if (src.regClass() == s2)
851 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
852 else
853 unreachable("wrong src register class for nir_op_imov");
854 } else if (dst.regClass() == v1) {
855 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
856 } else if (dst.regClass() == v2) {
857 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
858 } else {
859 nir_print_instr(&instr->instr, stderr);
860 unreachable("Should have been lowered to scalar.");
861 }
862 break;
863 }
864 case nir_op_inot: {
865 Temp src = get_alu_src(ctx, instr->src[0]);
866 if (instr->dest.dest.ssa.bit_size == 1) {
867 assert(src.regClass() == bld.lm);
868 assert(dst.regClass() == bld.lm);
869 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
870 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
871 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
872 } else if (dst.regClass() == v1) {
873 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
874 } else if (dst.type() == RegType::sgpr) {
875 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
876 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
877 } else {
878 fprintf(stderr, "Unimplemented NIR instr bit size: ");
879 nir_print_instr(&instr->instr, stderr);
880 fprintf(stderr, "\n");
881 }
882 break;
883 }
884 case nir_op_ineg: {
885 Temp src = get_alu_src(ctx, instr->src[0]);
886 if (dst.regClass() == v1) {
887 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
888 } else if (dst.regClass() == s1) {
889 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
890 } else if (dst.size() == 2) {
891 Temp src0 = bld.tmp(dst.type(), 1);
892 Temp src1 = bld.tmp(dst.type(), 1);
893 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
894
895 if (dst.regClass() == s2) {
896 Temp carry = bld.tmp(s1);
897 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
898 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
899 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
900 } else {
901 Temp lower = bld.tmp(v1);
902 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
903 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
904 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
905 }
906 } else {
907 fprintf(stderr, "Unimplemented NIR instr bit size: ");
908 nir_print_instr(&instr->instr, stderr);
909 fprintf(stderr, "\n");
910 }
911 break;
912 }
913 case nir_op_iabs: {
914 if (dst.regClass() == s1) {
915 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
916 } else if (dst.regClass() == v1) {
917 Temp src = get_alu_src(ctx, instr->src[0]);
918 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
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_isign: {
927 Temp src = get_alu_src(ctx, instr->src[0]);
928 if (dst.regClass() == s1) {
929 Temp tmp = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
930 Temp gtz = bld.sopc(aco_opcode::s_cmp_gt_i32, bld.def(s1, scc), src, Operand(0u));
931 bld.sop2(aco_opcode::s_add_i32, Definition(dst), bld.def(s1, scc), gtz, tmp);
932 } else if (dst.regClass() == s2) {
933 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
934 Temp neqz;
935 if (ctx->program->chip_class >= GFX8)
936 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
937 else
938 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
939 /* SCC gets zero-extended to 64 bit */
940 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
941 } else if (dst.regClass() == v1) {
942 Temp tmp = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
943 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
944 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(1u), tmp, gtz);
945 } else if (dst.regClass() == v2) {
946 Temp upper = emit_extract_vector(ctx, src, 1, v1);
947 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
948 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
949 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
950 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
951 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
952 } else {
953 fprintf(stderr, "Unimplemented NIR instr bit size: ");
954 nir_print_instr(&instr->instr, stderr);
955 fprintf(stderr, "\n");
956 }
957 break;
958 }
959 case nir_op_imax: {
960 if (dst.regClass() == v1) {
961 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
962 } else if (dst.regClass() == s1) {
963 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
964 } else {
965 fprintf(stderr, "Unimplemented NIR instr bit size: ");
966 nir_print_instr(&instr->instr, stderr);
967 fprintf(stderr, "\n");
968 }
969 break;
970 }
971 case nir_op_umax: {
972 if (dst.regClass() == v1) {
973 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
974 } else if (dst.regClass() == s1) {
975 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
976 } else {
977 fprintf(stderr, "Unimplemented NIR instr bit size: ");
978 nir_print_instr(&instr->instr, stderr);
979 fprintf(stderr, "\n");
980 }
981 break;
982 }
983 case nir_op_imin: {
984 if (dst.regClass() == v1) {
985 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
986 } else if (dst.regClass() == s1) {
987 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
988 } else {
989 fprintf(stderr, "Unimplemented NIR instr bit size: ");
990 nir_print_instr(&instr->instr, stderr);
991 fprintf(stderr, "\n");
992 }
993 break;
994 }
995 case nir_op_umin: {
996 if (dst.regClass() == v1) {
997 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
998 } else if (dst.regClass() == s1) {
999 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1000 } else {
1001 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1002 nir_print_instr(&instr->instr, stderr);
1003 fprintf(stderr, "\n");
1004 }
1005 break;
1006 }
1007 case nir_op_ior: {
1008 if (instr->dest.dest.ssa.bit_size == 1) {
1009 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1010 } else if (dst.regClass() == v1) {
1011 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1012 } else if (dst.regClass() == s1) {
1013 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1014 } else if (dst.regClass() == s2) {
1015 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1016 } else {
1017 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1018 nir_print_instr(&instr->instr, stderr);
1019 fprintf(stderr, "\n");
1020 }
1021 break;
1022 }
1023 case nir_op_iand: {
1024 if (instr->dest.dest.ssa.bit_size == 1) {
1025 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1026 } else if (dst.regClass() == v1) {
1027 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1028 } else if (dst.regClass() == s1) {
1029 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1030 } else if (dst.regClass() == s2) {
1031 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1032 } else {
1033 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1034 nir_print_instr(&instr->instr, stderr);
1035 fprintf(stderr, "\n");
1036 }
1037 break;
1038 }
1039 case nir_op_ixor: {
1040 if (instr->dest.dest.ssa.bit_size == 1) {
1041 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1042 } else if (dst.regClass() == v1) {
1043 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1044 } else if (dst.regClass() == s1) {
1045 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1046 } else if (dst.regClass() == s2) {
1047 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1048 } else {
1049 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1050 nir_print_instr(&instr->instr, stderr);
1051 fprintf(stderr, "\n");
1052 }
1053 break;
1054 }
1055 case nir_op_ushr: {
1056 if (dst.regClass() == v1) {
1057 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1058 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1059 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1060 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1061 } else if (dst.regClass() == v2) {
1062 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1063 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1064 } else if (dst.regClass() == s2) {
1065 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1066 } else if (dst.regClass() == s1) {
1067 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1068 } else {
1069 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1070 nir_print_instr(&instr->instr, stderr);
1071 fprintf(stderr, "\n");
1072 }
1073 break;
1074 }
1075 case nir_op_ishl: {
1076 if (dst.regClass() == v1) {
1077 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1078 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1079 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1080 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1081 } else if (dst.regClass() == v2) {
1082 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1083 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1084 } else if (dst.regClass() == s1) {
1085 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1086 } else if (dst.regClass() == s2) {
1087 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1088 } else {
1089 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1090 nir_print_instr(&instr->instr, stderr);
1091 fprintf(stderr, "\n");
1092 }
1093 break;
1094 }
1095 case nir_op_ishr: {
1096 if (dst.regClass() == v1) {
1097 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1098 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1099 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1100 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1101 } else if (dst.regClass() == v2) {
1102 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1103 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1104 } else if (dst.regClass() == s1) {
1105 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1106 } else if (dst.regClass() == s2) {
1107 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1108 } else {
1109 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1110 nir_print_instr(&instr->instr, stderr);
1111 fprintf(stderr, "\n");
1112 }
1113 break;
1114 }
1115 case nir_op_find_lsb: {
1116 Temp src = get_alu_src(ctx, instr->src[0]);
1117 if (src.regClass() == s1) {
1118 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1119 } else if (src.regClass() == v1) {
1120 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1121 } else if (src.regClass() == s2) {
1122 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1123 } else {
1124 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1125 nir_print_instr(&instr->instr, stderr);
1126 fprintf(stderr, "\n");
1127 }
1128 break;
1129 }
1130 case nir_op_ufind_msb:
1131 case nir_op_ifind_msb: {
1132 Temp src = get_alu_src(ctx, instr->src[0]);
1133 if (src.regClass() == s1 || src.regClass() == s2) {
1134 aco_opcode op = src.regClass() == s2 ?
1135 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1136 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1137 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1138
1139 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1140 Operand(src.size() * 32u - 1u), msb_rev);
1141 Temp msb = sub.def(0).getTemp();
1142 Temp carry = sub.def(1).getTemp();
1143
1144 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1145 } else if (src.regClass() == v1) {
1146 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1147 Temp msb_rev = bld.tmp(v1);
1148 emit_vop1_instruction(ctx, instr, op, msb_rev);
1149 Temp msb = bld.tmp(v1);
1150 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1151 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
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_bitfield_reverse: {
1160 if (dst.regClass() == s1) {
1161 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1162 } else if (dst.regClass() == v1) {
1163 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1164 } else {
1165 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1166 nir_print_instr(&instr->instr, stderr);
1167 fprintf(stderr, "\n");
1168 }
1169 break;
1170 }
1171 case nir_op_iadd: {
1172 if (dst.regClass() == s1) {
1173 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1174 break;
1175 }
1176
1177 Temp src0 = get_alu_src(ctx, instr->src[0]);
1178 Temp src1 = get_alu_src(ctx, instr->src[1]);
1179 if (dst.regClass() == v1) {
1180 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1181 break;
1182 }
1183
1184 assert(src0.size() == 2 && src1.size() == 2);
1185 Temp src00 = bld.tmp(src0.type(), 1);
1186 Temp src01 = bld.tmp(dst.type(), 1);
1187 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1188 Temp src10 = bld.tmp(src1.type(), 1);
1189 Temp src11 = bld.tmp(dst.type(), 1);
1190 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1191
1192 if (dst.regClass() == s2) {
1193 Temp carry = bld.tmp(s1);
1194 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1195 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1196 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1197 } else if (dst.regClass() == v2) {
1198 Temp dst0 = bld.tmp(v1);
1199 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1200 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1201 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1202 } else {
1203 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1204 nir_print_instr(&instr->instr, stderr);
1205 fprintf(stderr, "\n");
1206 }
1207 break;
1208 }
1209 case nir_op_uadd_sat: {
1210 Temp src0 = get_alu_src(ctx, instr->src[0]);
1211 Temp src1 = get_alu_src(ctx, instr->src[1]);
1212 if (dst.regClass() == s1) {
1213 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1214 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1215 src0, src1);
1216 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1217 } else if (dst.regClass() == v1) {
1218 if (ctx->options->chip_class >= GFX9) {
1219 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1220 add->operands[0] = Operand(src0);
1221 add->operands[1] = Operand(src1);
1222 add->definitions[0] = Definition(dst);
1223 add->clamp = 1;
1224 ctx->block->instructions.emplace_back(std::move(add));
1225 } else {
1226 if (src1.regClass() != v1)
1227 std::swap(src0, src1);
1228 assert(src1.regClass() == v1);
1229 Temp tmp = bld.tmp(v1);
1230 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1231 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1232 }
1233 } else {
1234 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1235 nir_print_instr(&instr->instr, stderr);
1236 fprintf(stderr, "\n");
1237 }
1238 break;
1239 }
1240 case nir_op_uadd_carry: {
1241 Temp src0 = get_alu_src(ctx, instr->src[0]);
1242 Temp src1 = get_alu_src(ctx, instr->src[1]);
1243 if (dst.regClass() == s1) {
1244 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1245 break;
1246 }
1247 if (dst.regClass() == v1) {
1248 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1249 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1250 break;
1251 }
1252
1253 Temp src00 = bld.tmp(src0.type(), 1);
1254 Temp src01 = bld.tmp(dst.type(), 1);
1255 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1256 Temp src10 = bld.tmp(src1.type(), 1);
1257 Temp src11 = bld.tmp(dst.type(), 1);
1258 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1259 if (dst.regClass() == s2) {
1260 Temp carry = bld.tmp(s1);
1261 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1262 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1263 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1264 } else if (dst.regClass() == v2) {
1265 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1266 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1267 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1268 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1269 } else {
1270 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1271 nir_print_instr(&instr->instr, stderr);
1272 fprintf(stderr, "\n");
1273 }
1274 break;
1275 }
1276 case nir_op_isub: {
1277 if (dst.regClass() == s1) {
1278 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1279 break;
1280 }
1281
1282 Temp src0 = get_alu_src(ctx, instr->src[0]);
1283 Temp src1 = get_alu_src(ctx, instr->src[1]);
1284 if (dst.regClass() == v1) {
1285 bld.vsub32(Definition(dst), src0, src1);
1286 break;
1287 }
1288
1289 Temp src00 = bld.tmp(src0.type(), 1);
1290 Temp src01 = bld.tmp(dst.type(), 1);
1291 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1292 Temp src10 = bld.tmp(src1.type(), 1);
1293 Temp src11 = bld.tmp(dst.type(), 1);
1294 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1295 if (dst.regClass() == s2) {
1296 Temp carry = bld.tmp(s1);
1297 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1298 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1299 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1300 } else if (dst.regClass() == v2) {
1301 Temp lower = bld.tmp(v1);
1302 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1303 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1304 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1305 } else {
1306 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1307 nir_print_instr(&instr->instr, stderr);
1308 fprintf(stderr, "\n");
1309 }
1310 break;
1311 }
1312 case nir_op_usub_borrow: {
1313 Temp src0 = get_alu_src(ctx, instr->src[0]);
1314 Temp src1 = get_alu_src(ctx, instr->src[1]);
1315 if (dst.regClass() == s1) {
1316 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1317 break;
1318 } else if (dst.regClass() == v1) {
1319 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1320 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1321 break;
1322 }
1323
1324 Temp src00 = bld.tmp(src0.type(), 1);
1325 Temp src01 = bld.tmp(dst.type(), 1);
1326 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1327 Temp src10 = bld.tmp(src1.type(), 1);
1328 Temp src11 = bld.tmp(dst.type(), 1);
1329 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1330 if (dst.regClass() == s2) {
1331 Temp borrow = bld.tmp(s1);
1332 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1333 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1334 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1335 } else if (dst.regClass() == v2) {
1336 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1337 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1338 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1339 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1340 } else {
1341 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1342 nir_print_instr(&instr->instr, stderr);
1343 fprintf(stderr, "\n");
1344 }
1345 break;
1346 }
1347 case nir_op_imul: {
1348 if (dst.regClass() == v1) {
1349 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1350 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1351 } else if (dst.regClass() == s1) {
1352 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1353 } else {
1354 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1355 nir_print_instr(&instr->instr, stderr);
1356 fprintf(stderr, "\n");
1357 }
1358 break;
1359 }
1360 case nir_op_umul_high: {
1361 if (dst.regClass() == v1) {
1362 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1363 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1364 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1365 } else if (dst.regClass() == s1) {
1366 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1367 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1368 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1369 } else {
1370 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1371 nir_print_instr(&instr->instr, stderr);
1372 fprintf(stderr, "\n");
1373 }
1374 break;
1375 }
1376 case nir_op_imul_high: {
1377 if (dst.regClass() == v1) {
1378 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1379 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1380 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1381 } else if (dst.regClass() == s1) {
1382 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1383 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1384 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1385 } else {
1386 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1387 nir_print_instr(&instr->instr, stderr);
1388 fprintf(stderr, "\n");
1389 }
1390 break;
1391 }
1392 case nir_op_fmul: {
1393 if (dst.size() == 1) {
1394 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1395 } else if (dst.size() == 2) {
1396 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), get_alu_src(ctx, instr->src[0]),
1397 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1398 } else {
1399 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1400 nir_print_instr(&instr->instr, stderr);
1401 fprintf(stderr, "\n");
1402 }
1403 break;
1404 }
1405 case nir_op_fadd: {
1406 if (dst.size() == 1) {
1407 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1408 } else if (dst.size() == 2) {
1409 bld.vop3(aco_opcode::v_add_f64, Definition(dst), get_alu_src(ctx, instr->src[0]),
1410 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1411 } else {
1412 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1413 nir_print_instr(&instr->instr, stderr);
1414 fprintf(stderr, "\n");
1415 }
1416 break;
1417 }
1418 case nir_op_fsub: {
1419 Temp src0 = get_alu_src(ctx, instr->src[0]);
1420 Temp src1 = get_alu_src(ctx, instr->src[1]);
1421 if (dst.size() == 1) {
1422 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1423 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1424 else
1425 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1426 } else if (dst.size() == 2) {
1427 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1428 get_alu_src(ctx, instr->src[0]),
1429 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1430 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1431 sub->neg[1] = true;
1432 } else {
1433 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1434 nir_print_instr(&instr->instr, stderr);
1435 fprintf(stderr, "\n");
1436 }
1437 break;
1438 }
1439 case nir_op_fmax: {
1440 if (dst.size() == 1) {
1441 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1442 } else if (dst.size() == 2) {
1443 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1444 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2),
1445 get_alu_src(ctx, instr->src[0]),
1446 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1447 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1448 } else {
1449 bld.vop3(aco_opcode::v_max_f64, Definition(dst),
1450 get_alu_src(ctx, instr->src[0]),
1451 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1452 }
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_fmin: {
1461 if (dst.size() == 1) {
1462 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1463 } else if (dst.size() == 2) {
1464 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1465 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2),
1466 get_alu_src(ctx, instr->src[0]),
1467 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1468 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1469 } else {
1470 bld.vop3(aco_opcode::v_min_f64, Definition(dst),
1471 get_alu_src(ctx, instr->src[0]),
1472 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1473 }
1474 } else {
1475 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1476 nir_print_instr(&instr->instr, stderr);
1477 fprintf(stderr, "\n");
1478 }
1479 break;
1480 }
1481 case nir_op_fmax3: {
1482 if (dst.size() == 1) {
1483 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1484 } else {
1485 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1486 nir_print_instr(&instr->instr, stderr);
1487 fprintf(stderr, "\n");
1488 }
1489 break;
1490 }
1491 case nir_op_fmin3: {
1492 if (dst.size() == 1) {
1493 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1494 } else {
1495 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1496 nir_print_instr(&instr->instr, stderr);
1497 fprintf(stderr, "\n");
1498 }
1499 break;
1500 }
1501 case nir_op_fmed3: {
1502 if (dst.size() == 1) {
1503 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1504 } else {
1505 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1506 nir_print_instr(&instr->instr, stderr);
1507 fprintf(stderr, "\n");
1508 }
1509 break;
1510 }
1511 case nir_op_umax3: {
1512 if (dst.size() == 1) {
1513 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1514 } else {
1515 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1516 nir_print_instr(&instr->instr, stderr);
1517 fprintf(stderr, "\n");
1518 }
1519 break;
1520 }
1521 case nir_op_umin3: {
1522 if (dst.size() == 1) {
1523 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1524 } else {
1525 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1526 nir_print_instr(&instr->instr, stderr);
1527 fprintf(stderr, "\n");
1528 }
1529 break;
1530 }
1531 case nir_op_umed3: {
1532 if (dst.size() == 1) {
1533 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1534 } else {
1535 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1536 nir_print_instr(&instr->instr, stderr);
1537 fprintf(stderr, "\n");
1538 }
1539 break;
1540 }
1541 case nir_op_imax3: {
1542 if (dst.size() == 1) {
1543 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1544 } else {
1545 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1546 nir_print_instr(&instr->instr, stderr);
1547 fprintf(stderr, "\n");
1548 }
1549 break;
1550 }
1551 case nir_op_imin3: {
1552 if (dst.size() == 1) {
1553 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1554 } else {
1555 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1556 nir_print_instr(&instr->instr, stderr);
1557 fprintf(stderr, "\n");
1558 }
1559 break;
1560 }
1561 case nir_op_imed3: {
1562 if (dst.size() == 1) {
1563 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1564 } else {
1565 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1566 nir_print_instr(&instr->instr, stderr);
1567 fprintf(stderr, "\n");
1568 }
1569 break;
1570 }
1571 case nir_op_cube_face_coord: {
1572 Temp in = get_alu_src(ctx, instr->src[0], 3);
1573 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1574 emit_extract_vector(ctx, in, 1, v1),
1575 emit_extract_vector(ctx, in, 2, v1) };
1576 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1577 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1578 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1579 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1580 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1581 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1582 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1583 break;
1584 }
1585 case nir_op_cube_face_index: {
1586 Temp in = get_alu_src(ctx, instr->src[0], 3);
1587 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1588 emit_extract_vector(ctx, in, 1, v1),
1589 emit_extract_vector(ctx, in, 2, v1) };
1590 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1591 break;
1592 }
1593 case nir_op_bcsel: {
1594 emit_bcsel(ctx, instr, dst);
1595 break;
1596 }
1597 case nir_op_frsq: {
1598 if (dst.size() == 1) {
1599 emit_rsq(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1600 } else if (dst.size() == 2) {
1601 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1602 } else {
1603 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1604 nir_print_instr(&instr->instr, stderr);
1605 fprintf(stderr, "\n");
1606 }
1607 break;
1608 }
1609 case nir_op_fneg: {
1610 Temp src = get_alu_src(ctx, instr->src[0]);
1611 if (dst.size() == 1) {
1612 if (ctx->block->fp_mode.must_flush_denorms32)
1613 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1614 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1615 } else if (dst.size() == 2) {
1616 if (ctx->block->fp_mode.must_flush_denorms16_64)
1617 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1618 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1619 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1620 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1621 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
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_fabs: {
1630 Temp src = get_alu_src(ctx, instr->src[0]);
1631 if (dst.size() == 1) {
1632 if (ctx->block->fp_mode.must_flush_denorms32)
1633 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1634 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1635 } else if (dst.size() == 2) {
1636 if (ctx->block->fp_mode.must_flush_denorms16_64)
1637 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1638 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1639 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1640 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1641 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1642 } else {
1643 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1644 nir_print_instr(&instr->instr, stderr);
1645 fprintf(stderr, "\n");
1646 }
1647 break;
1648 }
1649 case nir_op_fsat: {
1650 Temp src = get_alu_src(ctx, instr->src[0]);
1651 if (dst.size() == 1) {
1652 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1653 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1654 // TODO: confirm that this holds under any circumstances
1655 } else if (dst.size() == 2) {
1656 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1657 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1658 vop3->clamp = true;
1659 } else {
1660 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1661 nir_print_instr(&instr->instr, stderr);
1662 fprintf(stderr, "\n");
1663 }
1664 break;
1665 }
1666 case nir_op_flog2: {
1667 if (dst.size() == 1) {
1668 emit_log2(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1669 } else {
1670 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1671 nir_print_instr(&instr->instr, stderr);
1672 fprintf(stderr, "\n");
1673 }
1674 break;
1675 }
1676 case nir_op_frcp: {
1677 if (dst.size() == 1) {
1678 emit_rcp(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1679 } else if (dst.size() == 2) {
1680 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1681 } else {
1682 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1683 nir_print_instr(&instr->instr, stderr);
1684 fprintf(stderr, "\n");
1685 }
1686 break;
1687 }
1688 case nir_op_fexp2: {
1689 if (dst.size() == 1) {
1690 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1691 } else {
1692 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1693 nir_print_instr(&instr->instr, stderr);
1694 fprintf(stderr, "\n");
1695 }
1696 break;
1697 }
1698 case nir_op_fsqrt: {
1699 if (dst.size() == 1) {
1700 emit_sqrt(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1701 } else if (dst.size() == 2) {
1702 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
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_ffract: {
1711 if (dst.size() == 1) {
1712 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1713 } else if (dst.size() == 2) {
1714 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1715 } else {
1716 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1717 nir_print_instr(&instr->instr, stderr);
1718 fprintf(stderr, "\n");
1719 }
1720 break;
1721 }
1722 case nir_op_ffloor: {
1723 if (dst.size() == 1) {
1724 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1725 } else if (dst.size() == 2) {
1726 emit_floor_f64(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1727 } else {
1728 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1729 nir_print_instr(&instr->instr, stderr);
1730 fprintf(stderr, "\n");
1731 }
1732 break;
1733 }
1734 case nir_op_fceil: {
1735 if (dst.size() == 1) {
1736 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1737 } else if (dst.size() == 2) {
1738 if (ctx->options->chip_class >= GFX7) {
1739 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1740 } else {
1741 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1742 Temp src0 = get_alu_src(ctx, instr->src[0]);
1743
1744 /* trunc = trunc(src0)
1745 * if (src0 > 0.0 && src0 != trunc)
1746 * trunc += 1.0
1747 */
1748 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
1749 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
1750 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
1751 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
1752 Temp add = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), bld.copy(bld.def(v1), Operand(0u)), bld.copy(bld.def(v1), Operand(0x3ff00000u)), cond);
1753 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
1754 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
1755 }
1756 } else {
1757 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1758 nir_print_instr(&instr->instr, stderr);
1759 fprintf(stderr, "\n");
1760 }
1761 break;
1762 }
1763 case nir_op_ftrunc: {
1764 if (dst.size() == 1) {
1765 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
1766 } else if (dst.size() == 2) {
1767 emit_trunc_f64(ctx, bld, Definition(dst), get_alu_src(ctx, instr->src[0]));
1768 } else {
1769 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1770 nir_print_instr(&instr->instr, stderr);
1771 fprintf(stderr, "\n");
1772 }
1773 break;
1774 }
1775 case nir_op_fround_even: {
1776 if (dst.size() == 1) {
1777 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
1778 } else if (dst.size() == 2) {
1779 if (ctx->options->chip_class >= GFX7) {
1780 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
1781 } else {
1782 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
1783 Temp src0 = get_alu_src(ctx, instr->src[0]);
1784
1785 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
1786 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
1787
1788 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
1789 Temp bfi = bld.vop3(aco_opcode::v_bfi_b32, bld.def(v1), bitmask, bld.copy(bld.def(v1), Operand(0x43300000u)), as_vgpr(ctx, src0_hi));
1790 Temp tmp = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), src0, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
1791 Instruction *sub = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), tmp, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
1792 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
1793 tmp = sub->definitions[0].getTemp();
1794
1795 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
1796 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
1797 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
1798 Temp cond = vop3->definitions[0].getTemp();
1799
1800 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
1801 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
1802 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
1803 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
1804
1805 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1806 }
1807 } else {
1808 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1809 nir_print_instr(&instr->instr, stderr);
1810 fprintf(stderr, "\n");
1811 }
1812 break;
1813 }
1814 case nir_op_fsin:
1815 case nir_op_fcos: {
1816 Temp src = get_alu_src(ctx, instr->src[0]);
1817 aco_ptr<Instruction> norm;
1818 if (dst.size() == 1) {
1819 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
1820 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, as_vgpr(ctx, src));
1821
1822 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1823 if (ctx->options->chip_class < GFX9)
1824 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
1825
1826 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
1827 bld.vop1(opcode, Definition(dst), tmp);
1828 } else {
1829 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1830 nir_print_instr(&instr->instr, stderr);
1831 fprintf(stderr, "\n");
1832 }
1833 break;
1834 }
1835 case nir_op_ldexp: {
1836 if (dst.size() == 1) {
1837 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst),
1838 as_vgpr(ctx, get_alu_src(ctx, instr->src[0])),
1839 get_alu_src(ctx, instr->src[1]));
1840 } else if (dst.size() == 2) {
1841 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst),
1842 as_vgpr(ctx, get_alu_src(ctx, instr->src[0])),
1843 get_alu_src(ctx, instr->src[1]));
1844 } else {
1845 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1846 nir_print_instr(&instr->instr, stderr);
1847 fprintf(stderr, "\n");
1848 }
1849 break;
1850 }
1851 case nir_op_frexp_sig: {
1852 if (dst.size() == 1) {
1853 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst),
1854 get_alu_src(ctx, instr->src[0]));
1855 } else if (dst.size() == 2) {
1856 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst),
1857 get_alu_src(ctx, instr->src[0]));
1858 } else {
1859 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1860 nir_print_instr(&instr->instr, stderr);
1861 fprintf(stderr, "\n");
1862 }
1863 break;
1864 }
1865 case nir_op_frexp_exp: {
1866 if (instr->src[0].src.ssa->bit_size == 32) {
1867 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst),
1868 get_alu_src(ctx, instr->src[0]));
1869 } else if (instr->src[0].src.ssa->bit_size == 64) {
1870 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst),
1871 get_alu_src(ctx, instr->src[0]));
1872 } else {
1873 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1874 nir_print_instr(&instr->instr, stderr);
1875 fprintf(stderr, "\n");
1876 }
1877 break;
1878 }
1879 case nir_op_fsign: {
1880 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
1881 if (dst.size() == 1) {
1882 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1883 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
1884 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1885 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
1886 } else if (dst.size() == 2) {
1887 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1888 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
1889 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
1890
1891 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1892 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
1893 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
1894
1895 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
1896 } else {
1897 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1898 nir_print_instr(&instr->instr, stderr);
1899 fprintf(stderr, "\n");
1900 }
1901 break;
1902 }
1903 case nir_op_f2f32: {
1904 if (instr->src[0].src.ssa->bit_size == 64) {
1905 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
1906 } else {
1907 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1908 nir_print_instr(&instr->instr, stderr);
1909 fprintf(stderr, "\n");
1910 }
1911 break;
1912 }
1913 case nir_op_f2f64: {
1914 if (instr->src[0].src.ssa->bit_size == 32) {
1915 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_f32, dst);
1916 } else {
1917 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1918 nir_print_instr(&instr->instr, stderr);
1919 fprintf(stderr, "\n");
1920 }
1921 break;
1922 }
1923 case nir_op_i2f32: {
1924 assert(dst.size() == 1);
1925 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_i32, dst);
1926 break;
1927 }
1928 case nir_op_i2f64: {
1929 if (instr->src[0].src.ssa->bit_size == 32) {
1930 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_i32, dst);
1931 } else if (instr->src[0].src.ssa->bit_size == 64) {
1932 Temp src = get_alu_src(ctx, instr->src[0]);
1933 RegClass rc = RegClass(src.type(), 1);
1934 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
1935 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1936 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
1937 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
1938 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
1939 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
1940
1941 } else {
1942 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1943 nir_print_instr(&instr->instr, stderr);
1944 fprintf(stderr, "\n");
1945 }
1946 break;
1947 }
1948 case nir_op_u2f32: {
1949 assert(dst.size() == 1);
1950 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_u32, dst);
1951 break;
1952 }
1953 case nir_op_u2f64: {
1954 if (instr->src[0].src.ssa->bit_size == 32) {
1955 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_u32, dst);
1956 } else if (instr->src[0].src.ssa->bit_size == 64) {
1957 Temp src = get_alu_src(ctx, instr->src[0]);
1958 RegClass rc = RegClass(src.type(), 1);
1959 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
1960 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1961 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
1962 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
1963 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
1964 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
1965 } else {
1966 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1967 nir_print_instr(&instr->instr, stderr);
1968 fprintf(stderr, "\n");
1969 }
1970 break;
1971 }
1972 case nir_op_f2i32: {
1973 Temp src = get_alu_src(ctx, instr->src[0]);
1974 if (instr->src[0].src.ssa->bit_size == 32) {
1975 if (dst.type() == RegType::vgpr)
1976 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
1977 else
1978 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
1979 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
1980
1981 } else if (instr->src[0].src.ssa->bit_size == 64) {
1982 if (dst.type() == RegType::vgpr)
1983 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
1984 else
1985 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
1986 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
1987
1988 } else {
1989 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1990 nir_print_instr(&instr->instr, stderr);
1991 fprintf(stderr, "\n");
1992 }
1993 break;
1994 }
1995 case nir_op_f2u32: {
1996 Temp src = get_alu_src(ctx, instr->src[0]);
1997 if (instr->src[0].src.ssa->bit_size == 32) {
1998 if (dst.type() == RegType::vgpr)
1999 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2000 else
2001 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2002 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2003
2004 } else if (instr->src[0].src.ssa->bit_size == 64) {
2005 if (dst.type() == RegType::vgpr)
2006 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2007 else
2008 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2009 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2010
2011 } else {
2012 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2013 nir_print_instr(&instr->instr, stderr);
2014 fprintf(stderr, "\n");
2015 }
2016 break;
2017 }
2018 case nir_op_f2i64: {
2019 Temp src = get_alu_src(ctx, instr->src[0]);
2020 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
2021 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2022 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2023 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2024 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2025 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2026 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2027 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2028 Temp new_exponent = bld.tmp(v1);
2029 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2030 if (ctx->program->chip_class >= GFX8)
2031 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2032 else
2033 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2034 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2035 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2036 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2037 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2038 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2039 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2040 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2041 Temp new_lower = bld.tmp(v1);
2042 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2043 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2044 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2045
2046 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
2047 if (src.type() == RegType::vgpr)
2048 src = bld.as_uniform(src);
2049 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2050 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2051 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2052 exponent = bld.sop2(aco_opcode::s_min_u32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2053 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2054 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2055 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2056 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2057 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2058 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2059 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2060 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2061 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2062 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2063 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2064 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2065 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2066 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2067 Temp borrow = bld.tmp(s1);
2068 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2069 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2070 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2071
2072 } else if (instr->src[0].src.ssa->bit_size == 64) {
2073 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2074 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2075 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2076 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2077 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2078 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2079 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2080 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2081 if (dst.type() == RegType::sgpr) {
2082 lower = bld.as_uniform(lower);
2083 upper = bld.as_uniform(upper);
2084 }
2085 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2086
2087 } else {
2088 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr->instr, stderr);
2090 fprintf(stderr, "\n");
2091 }
2092 break;
2093 }
2094 case nir_op_f2u64: {
2095 Temp src = get_alu_src(ctx, instr->src[0]);
2096 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
2097 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2098 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2099 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2100 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2101 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2102 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2103 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2104 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2105 Temp new_exponent = bld.tmp(v1);
2106 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2107 if (ctx->program->chip_class >= GFX8)
2108 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2109 else
2110 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2111 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2112 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2113 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2114 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2115 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2116 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2117 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2118
2119 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
2120 if (src.type() == RegType::vgpr)
2121 src = bld.as_uniform(src);
2122 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2123 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2124 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2125 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2126 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2127 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2128 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2129 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2130 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2131 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2132 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2133 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2134 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2135 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2136 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2137 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2138 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2139 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2140
2141 } else if (instr->src[0].src.ssa->bit_size == 64) {
2142 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2143 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2144 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2145 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2146 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2147 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2148 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2149 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2150 if (dst.type() == RegType::sgpr) {
2151 lower = bld.as_uniform(lower);
2152 upper = bld.as_uniform(upper);
2153 }
2154 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2155
2156 } else {
2157 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2158 nir_print_instr(&instr->instr, stderr);
2159 fprintf(stderr, "\n");
2160 }
2161 break;
2162 }
2163 case nir_op_b2f32: {
2164 Temp src = get_alu_src(ctx, instr->src[0]);
2165 assert(src.regClass() == bld.lm);
2166
2167 if (dst.regClass() == s1) {
2168 src = bool_to_scalar_condition(ctx, src);
2169 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2170 } else if (dst.regClass() == v1) {
2171 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2172 } else {
2173 unreachable("Wrong destination register class for nir_op_b2f32.");
2174 }
2175 break;
2176 }
2177 case nir_op_b2f64: {
2178 Temp src = get_alu_src(ctx, instr->src[0]);
2179 assert(src.regClass() == bld.lm);
2180
2181 if (dst.regClass() == s2) {
2182 src = bool_to_scalar_condition(ctx, src);
2183 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2184 } else if (dst.regClass() == v2) {
2185 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2186 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2187 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2188 } else {
2189 unreachable("Wrong destination register class for nir_op_b2f64.");
2190 }
2191 break;
2192 }
2193 case nir_op_i2i32: {
2194 Temp src = get_alu_src(ctx, instr->src[0]);
2195 if (instr->src[0].src.ssa->bit_size == 64) {
2196 /* we can actually just say dst = src, as it would map the lower register */
2197 emit_extract_vector(ctx, src, 0, dst);
2198 } else {
2199 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2200 nir_print_instr(&instr->instr, stderr);
2201 fprintf(stderr, "\n");
2202 }
2203 break;
2204 }
2205 case nir_op_u2u32: {
2206 Temp src = get_alu_src(ctx, instr->src[0]);
2207 if (instr->src[0].src.ssa->bit_size == 16) {
2208 if (dst.regClass() == s1) {
2209 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFFFu), src);
2210 } else {
2211 // TODO: do better with SDWA
2212 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0xFFFFu), src);
2213 }
2214 } else if (instr->src[0].src.ssa->bit_size == 64) {
2215 /* we can actually just say dst = src, as it would map the lower register */
2216 emit_extract_vector(ctx, src, 0, dst);
2217 } else {
2218 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2219 nir_print_instr(&instr->instr, stderr);
2220 fprintf(stderr, "\n");
2221 }
2222 break;
2223 }
2224 case nir_op_i2i64: {
2225 Temp src = get_alu_src(ctx, instr->src[0]);
2226 if (src.regClass() == s1) {
2227 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2228 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2229 } else if (src.regClass() == v1) {
2230 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2231 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2232 } else {
2233 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2234 nir_print_instr(&instr->instr, stderr);
2235 fprintf(stderr, "\n");
2236 }
2237 break;
2238 }
2239 case nir_op_u2u64: {
2240 Temp src = get_alu_src(ctx, instr->src[0]);
2241 if (instr->src[0].src.ssa->bit_size == 32) {
2242 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, Operand(0u));
2243 } else {
2244 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2245 nir_print_instr(&instr->instr, stderr);
2246 fprintf(stderr, "\n");
2247 }
2248 break;
2249 }
2250 case nir_op_b2i32: {
2251 Temp src = get_alu_src(ctx, instr->src[0]);
2252 assert(src.regClass() == bld.lm);
2253
2254 if (dst.regClass() == s1) {
2255 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2256 bool_to_scalar_condition(ctx, src, dst);
2257 } else if (dst.regClass() == v1) {
2258 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2259 } else {
2260 unreachable("Invalid register class for b2i32");
2261 }
2262 break;
2263 }
2264 case nir_op_i2b1: {
2265 Temp src = get_alu_src(ctx, instr->src[0]);
2266 assert(dst.regClass() == bld.lm);
2267
2268 if (src.type() == RegType::vgpr) {
2269 assert(src.regClass() == v1 || src.regClass() == v2);
2270 assert(dst.regClass() == bld.lm);
2271 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2272 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2273 } else {
2274 assert(src.regClass() == s1 || src.regClass() == s2);
2275 Temp tmp;
2276 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2277 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2278 } else {
2279 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2280 bld.scc(bld.def(s1)), Operand(0u), src);
2281 }
2282 bool_to_vector_condition(ctx, tmp, dst);
2283 }
2284 break;
2285 }
2286 case nir_op_pack_64_2x32_split: {
2287 Temp src0 = get_alu_src(ctx, instr->src[0]);
2288 Temp src1 = get_alu_src(ctx, instr->src[1]);
2289
2290 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2291 break;
2292 }
2293 case nir_op_unpack_64_2x32_split_x:
2294 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2295 break;
2296 case nir_op_unpack_64_2x32_split_y:
2297 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2298 break;
2299 case nir_op_pack_half_2x16: {
2300 Temp src = get_alu_src(ctx, instr->src[0], 2);
2301
2302 if (dst.regClass() == v1) {
2303 Temp src0 = bld.tmp(v1);
2304 Temp src1 = bld.tmp(v1);
2305 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2306 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2307 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2308 else
2309 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2310 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2311 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2312 } else {
2313 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2314 nir_print_instr(&instr->instr, stderr);
2315 fprintf(stderr, "\n");
2316 }
2317 break;
2318 }
2319 case nir_op_unpack_half_2x16_split_x: {
2320 if (dst.regClass() == v1) {
2321 Builder bld(ctx->program, ctx->block);
2322 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2323 } else {
2324 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2325 nir_print_instr(&instr->instr, stderr);
2326 fprintf(stderr, "\n");
2327 }
2328 break;
2329 }
2330 case nir_op_unpack_half_2x16_split_y: {
2331 if (dst.regClass() == v1) {
2332 Builder bld(ctx->program, ctx->block);
2333 /* TODO: use SDWA here */
2334 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2335 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2336 } else {
2337 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2338 nir_print_instr(&instr->instr, stderr);
2339 fprintf(stderr, "\n");
2340 }
2341 break;
2342 }
2343 case nir_op_fquantize2f16: {
2344 Temp src = get_alu_src(ctx, instr->src[0]);
2345 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2346 Temp f32, cmp_res;
2347
2348 if (ctx->program->chip_class >= GFX8) {
2349 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2350 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2351 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2352 } else {
2353 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2354 * so compare the result and flush to 0 if it's smaller.
2355 */
2356 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2357 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2358 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2359 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2360 cmp_res = vop3->definitions[0].getTemp();
2361 }
2362
2363 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2364 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2365 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2366 } else {
2367 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2368 }
2369 break;
2370 }
2371 case nir_op_bfm: {
2372 Temp bits = get_alu_src(ctx, instr->src[0]);
2373 Temp offset = get_alu_src(ctx, instr->src[1]);
2374
2375 if (dst.regClass() == s1) {
2376 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2377 } else if (dst.regClass() == v1) {
2378 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2379 } else {
2380 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2381 nir_print_instr(&instr->instr, stderr);
2382 fprintf(stderr, "\n");
2383 }
2384 break;
2385 }
2386 case nir_op_bitfield_select: {
2387 /* (mask & insert) | (~mask & base) */
2388 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2389 Temp insert = get_alu_src(ctx, instr->src[1]);
2390 Temp base = get_alu_src(ctx, instr->src[2]);
2391
2392 /* dst = (insert & bitmask) | (base & ~bitmask) */
2393 if (dst.regClass() == s1) {
2394 aco_ptr<Instruction> sop2;
2395 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2396 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2397 Operand lhs;
2398 if (const_insert && const_bitmask) {
2399 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2400 } else {
2401 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2402 lhs = Operand(insert);
2403 }
2404
2405 Operand rhs;
2406 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2407 if (const_base && const_bitmask) {
2408 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2409 } else {
2410 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2411 rhs = Operand(base);
2412 }
2413
2414 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2415
2416 } else if (dst.regClass() == v1) {
2417 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2418 base = as_vgpr(ctx, base);
2419 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2420 insert = as_vgpr(ctx, insert);
2421
2422 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2423
2424 } else {
2425 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2426 nir_print_instr(&instr->instr, stderr);
2427 fprintf(stderr, "\n");
2428 }
2429 break;
2430 }
2431 case nir_op_ubfe:
2432 case nir_op_ibfe: {
2433 Temp base = get_alu_src(ctx, instr->src[0]);
2434 Temp offset = get_alu_src(ctx, instr->src[1]);
2435 Temp bits = get_alu_src(ctx, instr->src[2]);
2436
2437 if (dst.type() == RegType::sgpr) {
2438 Operand extract;
2439 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2440 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2441 if (const_offset && const_bits) {
2442 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2443 extract = Operand(const_extract);
2444 } else {
2445 Operand width;
2446 if (const_bits) {
2447 width = Operand(const_bits->u32 << 16);
2448 } else {
2449 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2450 }
2451 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2452 }
2453
2454 aco_opcode opcode;
2455 if (dst.regClass() == s1) {
2456 if (instr->op == nir_op_ubfe)
2457 opcode = aco_opcode::s_bfe_u32;
2458 else
2459 opcode = aco_opcode::s_bfe_i32;
2460 } else if (dst.regClass() == s2) {
2461 if (instr->op == nir_op_ubfe)
2462 opcode = aco_opcode::s_bfe_u64;
2463 else
2464 opcode = aco_opcode::s_bfe_i64;
2465 } else {
2466 unreachable("Unsupported BFE bit size");
2467 }
2468
2469 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2470
2471 } else {
2472 aco_opcode opcode;
2473 if (dst.regClass() == v1) {
2474 if (instr->op == nir_op_ubfe)
2475 opcode = aco_opcode::v_bfe_u32;
2476 else
2477 opcode = aco_opcode::v_bfe_i32;
2478 } else {
2479 unreachable("Unsupported BFE bit size");
2480 }
2481
2482 emit_vop3a_instruction(ctx, instr, opcode, dst);
2483 }
2484 break;
2485 }
2486 case nir_op_bit_count: {
2487 Temp src = get_alu_src(ctx, instr->src[0]);
2488 if (src.regClass() == s1) {
2489 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2490 } else if (src.regClass() == v1) {
2491 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2492 } else if (src.regClass() == v2) {
2493 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2494 emit_extract_vector(ctx, src, 1, v1),
2495 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2496 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2497 } else if (src.regClass() == s2) {
2498 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2499 } else {
2500 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2501 nir_print_instr(&instr->instr, stderr);
2502 fprintf(stderr, "\n");
2503 }
2504 break;
2505 }
2506 case nir_op_flt: {
2507 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2508 break;
2509 }
2510 case nir_op_fge: {
2511 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2512 break;
2513 }
2514 case nir_op_feq: {
2515 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2516 break;
2517 }
2518 case nir_op_fne: {
2519 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2520 break;
2521 }
2522 case nir_op_ilt: {
2523 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_i32, aco_opcode::v_cmp_lt_i64, aco_opcode::s_cmp_lt_i32);
2524 break;
2525 }
2526 case nir_op_ige: {
2527 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_i32, aco_opcode::v_cmp_ge_i64, aco_opcode::s_cmp_ge_i32);
2528 break;
2529 }
2530 case nir_op_ieq: {
2531 if (instr->src[0].src.ssa->bit_size == 1)
2532 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2533 else
2534 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_i32, aco_opcode::v_cmp_eq_i64, aco_opcode::s_cmp_eq_i32,
2535 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2536 break;
2537 }
2538 case nir_op_ine: {
2539 if (instr->src[0].src.ssa->bit_size == 1)
2540 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2541 else
2542 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lg_i32, aco_opcode::v_cmp_lg_i64, aco_opcode::s_cmp_lg_i32,
2543 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2544 break;
2545 }
2546 case nir_op_ult: {
2547 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_u32, aco_opcode::v_cmp_lt_u64, aco_opcode::s_cmp_lt_u32);
2548 break;
2549 }
2550 case nir_op_uge: {
2551 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_u32, aco_opcode::v_cmp_ge_u64, aco_opcode::s_cmp_ge_u32);
2552 break;
2553 }
2554 case nir_op_fddx:
2555 case nir_op_fddy:
2556 case nir_op_fddx_fine:
2557 case nir_op_fddy_fine:
2558 case nir_op_fddx_coarse:
2559 case nir_op_fddy_coarse: {
2560 Temp src = get_alu_src(ctx, instr->src[0]);
2561 uint16_t dpp_ctrl1, dpp_ctrl2;
2562 if (instr->op == nir_op_fddx_fine) {
2563 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2564 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2565 } else if (instr->op == nir_op_fddy_fine) {
2566 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2567 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2568 } else {
2569 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2570 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2571 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2572 else
2573 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2574 }
2575
2576 Temp tmp;
2577 if (ctx->program->chip_class >= GFX8) {
2578 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2579 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2580 } else {
2581 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2582 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2583 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2584 }
2585 emit_wqm(ctx, tmp, dst, true);
2586 break;
2587 }
2588 default:
2589 fprintf(stderr, "Unknown NIR ALU instr: ");
2590 nir_print_instr(&instr->instr, stderr);
2591 fprintf(stderr, "\n");
2592 }
2593 }
2594
2595 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
2596 {
2597 Temp dst = get_ssa_temp(ctx, &instr->def);
2598
2599 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2600 // which get truncated the lsb if double and msb if int
2601 // for now, we only use s_mov_b64 with 64bit inline constants
2602 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
2603 assert(dst.type() == RegType::sgpr);
2604
2605 Builder bld(ctx->program, ctx->block);
2606
2607 if (instr->def.bit_size == 1) {
2608 assert(dst.regClass() == bld.lm);
2609 int val = instr->value[0].b ? -1 : 0;
2610 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
2611 bld.sop1(Builder::s_mov, Definition(dst), op);
2612 } else if (dst.size() == 1) {
2613 bld.copy(Definition(dst), Operand(instr->value[0].u32));
2614 } else {
2615 assert(dst.size() != 1);
2616 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
2617 if (instr->def.bit_size == 64)
2618 for (unsigned i = 0; i < dst.size(); i++)
2619 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
2620 else {
2621 for (unsigned i = 0; i < dst.size(); i++)
2622 vec->operands[i] = Operand{instr->value[i].u32};
2623 }
2624 vec->definitions[0] = Definition(dst);
2625 ctx->block->instructions.emplace_back(std::move(vec));
2626 }
2627 }
2628
2629 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
2630 {
2631 uint32_t new_mask = 0;
2632 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
2633 if (mask & (1u << i))
2634 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
2635 return new_mask;
2636 }
2637
2638 Operand load_lds_size_m0(isel_context *ctx)
2639 {
2640 /* TODO: m0 does not need to be initialized on GFX9+ */
2641 Builder bld(ctx->program, ctx->block);
2642 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
2643 }
2644
2645 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
2646 Temp address, unsigned base_offset, unsigned align)
2647 {
2648 assert(util_is_power_of_two_nonzero(align) && align >= 4);
2649
2650 Builder bld(ctx->program, ctx->block);
2651
2652 Operand m = load_lds_size_m0(ctx);
2653
2654 unsigned num_components = dst.size() * 4u / elem_size_bytes;
2655 unsigned bytes_read = 0;
2656 unsigned result_size = 0;
2657 unsigned total_bytes = num_components * elem_size_bytes;
2658 std::array<Temp, NIR_MAX_VEC_COMPONENTS> result;
2659 bool large_ds_read = ctx->options->chip_class >= GFX7;
2660 bool usable_read2 = ctx->options->chip_class >= GFX7;
2661
2662 while (bytes_read < total_bytes) {
2663 unsigned todo = total_bytes - bytes_read;
2664 bool aligned8 = bytes_read % 8 == 0 && align % 8 == 0;
2665 bool aligned16 = bytes_read % 16 == 0 && align % 16 == 0;
2666
2667 aco_opcode op = aco_opcode::last_opcode;
2668 bool read2 = false;
2669 if (todo >= 16 && aligned16 && large_ds_read) {
2670 op = aco_opcode::ds_read_b128;
2671 todo = 16;
2672 } else if (todo >= 16 && aligned8 && usable_read2) {
2673 op = aco_opcode::ds_read2_b64;
2674 read2 = true;
2675 todo = 16;
2676 } else if (todo >= 12 && aligned16 && large_ds_read) {
2677 op = aco_opcode::ds_read_b96;
2678 todo = 12;
2679 } else if (todo >= 8 && aligned8) {
2680 op = aco_opcode::ds_read_b64;
2681 todo = 8;
2682 } else if (todo >= 8 && usable_read2) {
2683 op = aco_opcode::ds_read2_b32;
2684 read2 = true;
2685 todo = 8;
2686 } else if (todo >= 4) {
2687 op = aco_opcode::ds_read_b32;
2688 todo = 4;
2689 } else {
2690 assert(false);
2691 }
2692 assert(todo % elem_size_bytes == 0);
2693 unsigned num_elements = todo / elem_size_bytes;
2694 unsigned offset = base_offset + bytes_read;
2695 unsigned max_offset = read2 ? 1019 : 65535;
2696
2697 Temp address_offset = address;
2698 if (offset > max_offset) {
2699 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
2700 offset = bytes_read;
2701 }
2702 assert(offset <= max_offset); /* bytes_read shouldn't be large enough for this to happen */
2703
2704 Temp res;
2705 if (num_components == 1 && dst.type() == RegType::vgpr)
2706 res = dst;
2707 else
2708 res = bld.tmp(RegClass(RegType::vgpr, todo / 4));
2709
2710 if (read2)
2711 res = bld.ds(op, Definition(res), address_offset, m, offset / (todo / 2), (offset / (todo / 2)) + 1);
2712 else
2713 res = bld.ds(op, Definition(res), address_offset, m, offset);
2714
2715 if (num_components == 1) {
2716 assert(todo == total_bytes);
2717 if (dst.type() == RegType::sgpr)
2718 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
2719 return dst;
2720 }
2721
2722 if (dst.type() == RegType::sgpr) {
2723 Temp new_res = bld.tmp(RegType::sgpr, res.size());
2724 expand_vector(ctx, res, new_res, res.size(), (1 << res.size()) - 1);
2725 res = new_res;
2726 }
2727
2728 if (num_elements == 1) {
2729 result[result_size++] = res;
2730 } else {
2731 assert(res != dst && res.size() % num_elements == 0);
2732 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_elements)};
2733 split->operands[0] = Operand(res);
2734 for (unsigned i = 0; i < num_elements; i++)
2735 split->definitions[i] = Definition(result[result_size++] = bld.tmp(res.type(), elem_size_bytes / 4));
2736 ctx->block->instructions.emplace_back(std::move(split));
2737 }
2738
2739 bytes_read += todo;
2740 }
2741
2742 assert(result_size == num_components && result_size > 1);
2743 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, result_size, 1)};
2744 for (unsigned i = 0; i < result_size; i++)
2745 vec->operands[i] = Operand(result[i]);
2746 vec->definitions[0] = Definition(dst);
2747 ctx->block->instructions.emplace_back(std::move(vec));
2748 ctx->allocated_vec.emplace(dst.id(), result);
2749
2750 return dst;
2751 }
2752
2753 Temp extract_subvector(isel_context *ctx, Temp data, unsigned start, unsigned size, RegType type)
2754 {
2755 if (start == 0 && size == data.size())
2756 return type == RegType::vgpr ? as_vgpr(ctx, data) : data;
2757
2758 unsigned size_hint = 1;
2759 auto it = ctx->allocated_vec.find(data.id());
2760 if (it != ctx->allocated_vec.end())
2761 size_hint = it->second[0].size();
2762 if (size % size_hint || start % size_hint)
2763 size_hint = 1;
2764
2765 start /= size_hint;
2766 size /= size_hint;
2767
2768 Temp elems[size];
2769 for (unsigned i = 0; i < size; i++)
2770 elems[i] = emit_extract_vector(ctx, data, start + i, RegClass(type, size_hint));
2771
2772 if (size == 1)
2773 return type == RegType::vgpr ? as_vgpr(ctx, elems[0]) : elems[0];
2774
2775 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
2776 for (unsigned i = 0; i < size; i++)
2777 vec->operands[i] = Operand(elems[i]);
2778 Temp res = {ctx->program->allocateId(), RegClass(type, size * size_hint)};
2779 vec->definitions[0] = Definition(res);
2780 ctx->block->instructions.emplace_back(std::move(vec));
2781 return res;
2782 }
2783
2784 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)
2785 {
2786 Builder bld(ctx->program, ctx->block);
2787 unsigned bytes_written = 0;
2788 bool large_ds_write = ctx->options->chip_class >= GFX7;
2789 bool usable_write2 = ctx->options->chip_class >= GFX7;
2790
2791 while (bytes_written < total_size * 4) {
2792 unsigned todo = total_size * 4 - bytes_written;
2793 bool aligned8 = bytes_written % 8 == 0 && align % 8 == 0;
2794 bool aligned16 = bytes_written % 16 == 0 && align % 16 == 0;
2795
2796 aco_opcode op = aco_opcode::last_opcode;
2797 bool write2 = false;
2798 unsigned size = 0;
2799 if (todo >= 16 && aligned16 && large_ds_write) {
2800 op = aco_opcode::ds_write_b128;
2801 size = 4;
2802 } else if (todo >= 16 && aligned8 && usable_write2) {
2803 op = aco_opcode::ds_write2_b64;
2804 write2 = true;
2805 size = 4;
2806 } else if (todo >= 12 && aligned16 && large_ds_write) {
2807 op = aco_opcode::ds_write_b96;
2808 size = 3;
2809 } else if (todo >= 8 && aligned8) {
2810 op = aco_opcode::ds_write_b64;
2811 size = 2;
2812 } else if (todo >= 8 && usable_write2) {
2813 op = aco_opcode::ds_write2_b32;
2814 write2 = true;
2815 size = 2;
2816 } else if (todo >= 4) {
2817 op = aco_opcode::ds_write_b32;
2818 size = 1;
2819 } else {
2820 assert(false);
2821 }
2822
2823 unsigned offset = offset0 + offset1 + bytes_written;
2824 unsigned max_offset = write2 ? 1020 : 65535;
2825 Temp address_offset = address;
2826 if (offset > max_offset) {
2827 address_offset = bld.vadd32(bld.def(v1), Operand(offset0), address_offset);
2828 offset = offset1 + bytes_written;
2829 }
2830 assert(offset <= max_offset); /* offset1 shouldn't be large enough for this to happen */
2831
2832 if (write2) {
2833 Temp val0 = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size / 2, RegType::vgpr);
2834 Temp val1 = extract_subvector(ctx, data, data_start + (bytes_written >> 2) + 1, size / 2, RegType::vgpr);
2835 bld.ds(op, address_offset, val0, val1, m, offset / size / 2, (offset / size / 2) + 1);
2836 } else {
2837 Temp val = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size, RegType::vgpr);
2838 bld.ds(op, address_offset, val, m, offset);
2839 }
2840
2841 bytes_written += size * 4;
2842 }
2843 }
2844
2845 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
2846 Temp address, unsigned base_offset, unsigned align)
2847 {
2848 assert(util_is_power_of_two_nonzero(align) && align >= 4);
2849 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
2850
2851 Operand m = load_lds_size_m0(ctx);
2852
2853 /* we need at most two stores, assuming that the writemask is at most 4 bits wide */
2854 assert(wrmask <= 0x0f);
2855 int start[2], count[2];
2856 u_bit_scan_consecutive_range(&wrmask, &start[0], &count[0]);
2857 u_bit_scan_consecutive_range(&wrmask, &start[1], &count[1]);
2858 assert(wrmask == 0);
2859
2860 /* one combined store is sufficient */
2861 if (count[0] == count[1] && (align % elem_size_bytes) == 0 && (base_offset % elem_size_bytes) == 0) {
2862 Builder bld(ctx->program, ctx->block);
2863
2864 Temp address_offset = address;
2865 if ((base_offset / elem_size_bytes) + start[1] > 255) {
2866 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
2867 base_offset = 0;
2868 }
2869
2870 assert(count[0] == 1);
2871 RegClass xtract_rc(RegType::vgpr, elem_size_bytes / 4);
2872
2873 Temp val0 = emit_extract_vector(ctx, data, start[0], xtract_rc);
2874 Temp val1 = emit_extract_vector(ctx, data, start[1], xtract_rc);
2875 aco_opcode op = elem_size_bytes == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
2876 base_offset = base_offset / elem_size_bytes;
2877 bld.ds(op, address_offset, val0, val1, m,
2878 base_offset + start[0], base_offset + start[1]);
2879 return;
2880 }
2881
2882 for (unsigned i = 0; i < 2; i++) {
2883 if (count[i] == 0)
2884 continue;
2885
2886 unsigned elem_size_words = elem_size_bytes / 4;
2887 ds_write_helper(ctx, m, address, data, start[i] * elem_size_words, count[i] * elem_size_words,
2888 base_offset, start[i] * elem_size_bytes, align);
2889 }
2890 return;
2891 }
2892
2893 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
2894 {
2895 unsigned align = 16;
2896 if (const_offset)
2897 align = std::min(align, 1u << (ffs(const_offset) - 1));
2898
2899 return align;
2900 }
2901
2902
2903 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
2904 unsigned split_cnt = 0u, Temp dst = Temp())
2905 {
2906 Builder bld(ctx->program, ctx->block);
2907 unsigned dword_size = elem_size_bytes / 4;
2908
2909 if (!dst.id())
2910 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
2911
2912 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
2913 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
2914 instr->definitions[0] = Definition(dst);
2915
2916 for (unsigned i = 0; i < cnt; ++i) {
2917 if (arr[i].id()) {
2918 assert(arr[i].size() == dword_size);
2919 allocated_vec[i] = arr[i];
2920 instr->operands[i] = Operand(arr[i]);
2921 } else {
2922 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
2923 allocated_vec[i] = zero;
2924 instr->operands[i] = Operand(zero);
2925 }
2926 }
2927
2928 bld.insert(std::move(instr));
2929
2930 if (split_cnt)
2931 emit_split_vector(ctx, dst, split_cnt);
2932 else
2933 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
2934
2935 return dst;
2936 }
2937
2938 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
2939 {
2940 if (const_offset >= 4096) {
2941 unsigned excess_const_offset = const_offset / 4096u * 4096u;
2942 const_offset %= 4096u;
2943
2944 if (!voffset.id())
2945 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
2946 else if (unlikely(voffset.regClass() == s1))
2947 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
2948 else if (likely(voffset.regClass() == v1))
2949 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
2950 else
2951 unreachable("Unsupported register class of voffset");
2952 }
2953
2954 return const_offset;
2955 }
2956
2957 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
2958 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
2959 {
2960 assert(vdata.id());
2961 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
2962 assert(vdata.size() >= 1 && vdata.size() <= 4);
2963
2964 Builder bld(ctx->program, ctx->block);
2965 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_store_dword + vdata.size() - 1);
2966 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
2967
2968 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
2969 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
2970 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
2971 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
2972 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
2973
2974 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
2975 }
2976
2977 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
2978 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
2979 bool allow_combining = true, bool reorder = true, bool slc = false)
2980 {
2981 Builder bld(ctx->program, ctx->block);
2982 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
2983 assert(write_mask);
2984
2985 if (elem_size_bytes == 8) {
2986 elem_size_bytes = 4;
2987 write_mask = widen_mask(write_mask, 2);
2988 }
2989
2990 while (write_mask) {
2991 int start = 0;
2992 int count = 0;
2993 u_bit_scan_consecutive_range(&write_mask, &start, &count);
2994 assert(count > 0);
2995 assert(start >= 0);
2996
2997 while (count > 0) {
2998 unsigned sub_count = allow_combining ? MIN2(count, 4) : 1;
2999 unsigned const_offset = (unsigned) start * elem_size_bytes + base_const_offset;
3000
3001 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
3002 if (unlikely(ctx->program->chip_class == GFX6 && sub_count == 3))
3003 sub_count = 2;
3004
3005 Temp elem = extract_subvector(ctx, src, start, sub_count, RegType::vgpr);
3006 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, elem, const_offset, reorder, slc);
3007
3008 count -= sub_count;
3009 start += sub_count;
3010 }
3011
3012 assert(count == 0);
3013 }
3014 }
3015
3016 Temp emit_single_mubuf_load(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset,
3017 unsigned const_offset, unsigned size_dwords, bool allow_reorder = true)
3018 {
3019 assert(size_dwords != 3 || ctx->program->chip_class != GFX6);
3020 assert(size_dwords >= 1 && size_dwords <= 4);
3021
3022 Builder bld(ctx->program, ctx->block);
3023 Temp vdata = bld.tmp(RegClass(RegType::vgpr, size_dwords));
3024 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_load_dword + size_dwords - 1);
3025 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3026
3027 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3028 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3029 Builder::Result r = bld.mubuf(op, Definition(vdata), Operand(descriptor), voffset_op, soffset_op, const_offset,
3030 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3031 /* disable_wqm */ false, /* glc */ true,
3032 /* dlc*/ ctx->program->chip_class >= GFX10, /* slc */ false);
3033
3034 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3035
3036 return vdata;
3037 }
3038
3039 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3040 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3041 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3042 {
3043 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3044 assert((num_components * elem_size_bytes / 4) == dst.size());
3045 assert(!!stride != allow_combining);
3046
3047 Builder bld(ctx->program, ctx->block);
3048 unsigned split_cnt = num_components;
3049
3050 if (elem_size_bytes == 8) {
3051 elem_size_bytes = 4;
3052 num_components *= 2;
3053 }
3054
3055 if (!stride)
3056 stride = elem_size_bytes;
3057
3058 unsigned load_size = 1;
3059 if (allow_combining) {
3060 if ((num_components % 4) == 0)
3061 load_size = 4;
3062 else if ((num_components % 3) == 0 && ctx->program->chip_class != GFX6)
3063 load_size = 3;
3064 else if ((num_components % 2) == 0)
3065 load_size = 2;
3066 }
3067
3068 unsigned num_loads = num_components / load_size;
3069 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
3070
3071 for (unsigned i = 0; i < num_loads; ++i) {
3072 unsigned const_offset = i * stride * load_size + base_const_offset;
3073 elems[i] = emit_single_mubuf_load(ctx, descriptor, voffset, soffset, const_offset, load_size, allow_reorder);
3074 }
3075
3076 create_vec_from_array(ctx, elems.data(), num_loads, RegType::vgpr, load_size * 4u, split_cnt, dst);
3077 }
3078
3079 std::pair<Temp, unsigned> offset_add_from_nir(isel_context *ctx, const std::pair<Temp, unsigned> &base_offset, nir_src *off_src, unsigned stride = 1u)
3080 {
3081 Builder bld(ctx->program, ctx->block);
3082 Temp offset = base_offset.first;
3083 unsigned const_offset = base_offset.second;
3084
3085 if (!nir_src_is_const(*off_src)) {
3086 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3087 Temp with_stride;
3088
3089 /* Calculate indirect offset with stride */
3090 if (likely(indirect_offset_arg.regClass() == v1))
3091 with_stride = bld.v_mul_imm(bld.def(v1), indirect_offset_arg, stride);
3092 else if (indirect_offset_arg.regClass() == s1)
3093 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3094 else
3095 unreachable("Unsupported register class of indirect offset");
3096
3097 /* Add to the supplied base offset */
3098 if (offset.id() == 0)
3099 offset = with_stride;
3100 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
3101 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
3102 else if (offset.size() == 1 && with_stride.size() == 1)
3103 offset = bld.vadd32(bld.def(v1), with_stride, offset);
3104 else
3105 unreachable("Unsupported register class of indirect offset");
3106 } else {
3107 unsigned const_offset_arg = nir_src_as_uint(*off_src);
3108 const_offset += const_offset_arg * stride;
3109 }
3110
3111 return std::make_pair(offset, const_offset);
3112 }
3113
3114 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
3115 {
3116 Builder bld(ctx->program, ctx->block);
3117 Temp offset;
3118
3119 if (off1.first.id() && off2.first.id()) {
3120 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
3121 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
3122 else if (off1.first.size() == 1 && off2.first.size() == 1)
3123 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
3124 else
3125 unreachable("Unsupported register class of indirect offset");
3126 } else {
3127 offset = off1.first.id() ? off1.first : off2.first;
3128 }
3129
3130 return std::make_pair(offset, off1.second + off2.second);
3131 }
3132
3133 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
3134 {
3135 Builder bld(ctx->program, ctx->block);
3136 unsigned const_offset = offs.second * multiplier;
3137
3138 if (!offs.first.id())
3139 return std::make_pair(offs.first, const_offset);
3140
3141 Temp offset = unlikely(offs.first.regClass() == s1)
3142 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
3143 : bld.v_mul_imm(bld.def(v1), offs.first, multiplier);
3144
3145 return std::make_pair(offset, const_offset);
3146 }
3147
3148 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
3149 {
3150 Builder bld(ctx->program, ctx->block);
3151
3152 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3153 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
3154 /* component is in bytes */
3155 const_offset += nir_intrinsic_component(instr) * component_stride;
3156
3157 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3158 nir_src *off_src = nir_get_io_offset_src(instr);
3159 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
3160 }
3161
3162 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
3163 {
3164 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
3165 }
3166
3167 Temp get_tess_rel_patch_id(isel_context *ctx)
3168 {
3169 Builder bld(ctx->program, ctx->block);
3170
3171 switch (ctx->shader->info.stage) {
3172 case MESA_SHADER_TESS_CTRL:
3173 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
3174 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
3175 case MESA_SHADER_TESS_EVAL:
3176 return get_arg(ctx, ctx->args->tes_rel_patch_id);
3177 default:
3178 unreachable("Unsupported stage in get_tess_rel_patch_id");
3179 }
3180 }
3181
3182 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3183 {
3184 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3185 Builder bld(ctx->program, ctx->block);
3186
3187 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
3188 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
3189
3190 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
3191
3192 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3193 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
3194
3195 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3196 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
3197 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
3198
3199 return offset_mul(ctx, offs, 4u);
3200 }
3201
3202 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
3203 {
3204 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3205 Builder bld(ctx->program, ctx->block);
3206
3207 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
3208 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
3209 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
3210 uint32_t output_vertex_size = num_tcs_outputs * 16;
3211 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3212 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
3213
3214 std::pair<Temp, unsigned> offs = instr
3215 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
3216 : std::make_pair(Temp(), 0u);
3217
3218 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3219 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
3220
3221 if (per_vertex) {
3222 assert(instr);
3223
3224 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3225 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
3226
3227 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
3228 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
3229 } else {
3230 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
3231 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
3232 }
3233
3234 return offs;
3235 }
3236
3237 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3238 {
3239 Builder bld(ctx->program, ctx->block);
3240
3241 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
3242 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
3243
3244 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
3245
3246 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3247 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
3248 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
3249
3250 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3251 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
3252
3253 return offs;
3254 }
3255
3256 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
3257 {
3258 Builder bld(ctx->program, ctx->block);
3259
3260 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
3261 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
3262 : ctx->args->options->key.tes.tcs_num_outputs;
3263
3264 unsigned output_vertex_size = num_tcs_outputs * 16;
3265 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3266 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
3267 unsigned attr_stride = ctx->tcs_num_patches;
3268
3269 std::pair<Temp, unsigned> offs = instr
3270 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
3271 : std::make_pair(Temp(), 0u);
3272
3273 if (const_base_offset)
3274 offs.second += const_base_offset * attr_stride;
3275
3276 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3277 Temp patch_off = bld.v_mul_imm(bld.def(v1), rel_patch_id, 16u);
3278 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
3279
3280 return offs;
3281 }
3282
3283 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
3284 {
3285 unsigned off = nir_intrinsic_base(instr) * 4u;
3286 nir_src *off_src = nir_get_io_offset_src(instr);
3287
3288 if (!nir_src_is_const(*off_src)) {
3289 *indirect = true;
3290 return false;
3291 }
3292
3293 *indirect = false;
3294 off += nir_src_as_uint(*off_src) * 16u;
3295
3296 while (mask) {
3297 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
3298 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
3299 return true;
3300 }
3301
3302 return false;
3303 }
3304
3305 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
3306 {
3307 unsigned write_mask = nir_intrinsic_write_mask(instr);
3308 unsigned component = nir_intrinsic_component(instr);
3309 unsigned idx = nir_intrinsic_base(instr) + component;
3310
3311 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
3312 if (off_instr->type != nir_instr_type_load_const)
3313 return false;
3314
3315 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3316 idx += nir_src_as_uint(instr->src[1]) * 4u;
3317
3318 if (instr->src[0].ssa->bit_size == 64)
3319 write_mask = widen_mask(write_mask, 2);
3320
3321 for (unsigned i = 0; i < 8; ++i) {
3322 if (write_mask & (1 << i)) {
3323 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
3324 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
3325 }
3326 idx++;
3327 }
3328
3329 return true;
3330 }
3331
3332 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
3333 {
3334 /* Only TCS per-vertex inputs are supported by this function.
3335 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
3336 */
3337 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
3338 return false;
3339
3340 nir_src *off_src = nir_get_io_offset_src(instr);
3341 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3342 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
3343 bool can_use_temps = nir_src_is_const(*off_src) &&
3344 vertex_index_instr->type == nir_instr_type_intrinsic &&
3345 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
3346
3347 if (!can_use_temps)
3348 return false;
3349
3350 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
3351 Temp *src = &ctx->inputs.temps[idx];
3352 Temp vec = create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u);
3353 assert(vec.size() == dst.size());
3354
3355 Builder bld(ctx->program, ctx->block);
3356 bld.copy(Definition(dst), vec);
3357 return true;
3358 }
3359
3360 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
3361 {
3362 Builder bld(ctx->program, ctx->block);
3363
3364 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
3365 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3366 unsigned write_mask = nir_intrinsic_write_mask(instr);
3367 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
3368
3369 if (ctx->tcs_in_out_eq)
3370 store_output_to_temps(ctx, instr);
3371
3372 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
3373 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
3374 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
3375 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
3376 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
3377 } else {
3378 Temp lds_base;
3379
3380 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
3381 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
3382 unsigned itemsize = ctx->stage == vertex_geometry_gs
3383 ? ctx->program->info->vs.es_info.esgs_itemsize
3384 : ctx->program->info->tes.es_info.esgs_itemsize;
3385 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
3386 Temp wave_idx = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), get_arg(ctx, ctx->args->merged_wave_info), Operand(4u << 16 | 24));
3387 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
3388 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
3389 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
3390 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
3391 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
3392 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
3393 */
3394 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
3395 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
3396 lds_base = bld.v_mul_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
3397 } else {
3398 unreachable("Invalid LS or ES stage");
3399 }
3400
3401 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
3402 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3403 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
3404 }
3405 }
3406
3407 bool should_write_tcs_patch_output_to_vmem(isel_context *ctx, nir_intrinsic_instr *instr)
3408 {
3409 unsigned off = nir_intrinsic_base(instr) * 4u;
3410 return off != ctx->tcs_tess_lvl_out_loc &&
3411 off != ctx->tcs_tess_lvl_in_loc;
3412 }
3413
3414 bool should_write_tcs_output_to_lds(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3415 {
3416 /* When none of the appropriate outputs are read, we are OK to never write to LDS */
3417 if (per_vertex ? ctx->shader->info.outputs_read == 0U : ctx->shader->info.patch_outputs_read == 0u)
3418 return false;
3419
3420 uint64_t mask = per_vertex
3421 ? ctx->shader->info.outputs_read
3422 : ctx->shader->info.patch_outputs_read;
3423 bool indirect_write;
3424 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
3425 return indirect_write || output_read;
3426 }
3427
3428 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3429 {
3430 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3431 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3432
3433 Builder bld(ctx->program, ctx->block);
3434
3435 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
3436 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3437 unsigned write_mask = nir_intrinsic_write_mask(instr);
3438
3439 /* Only write to VMEM if the output is per-vertex or it's per-patch non tess factor */
3440 bool write_to_vmem = per_vertex || should_write_tcs_patch_output_to_vmem(ctx, instr);
3441 /* Only write to LDS if the output is read by the shader, or it's per-patch tess factor */
3442 bool write_to_lds = !write_to_vmem || should_write_tcs_output_to_lds(ctx, instr, per_vertex);
3443
3444 if (write_to_vmem) {
3445 std::pair<Temp, unsigned> vmem_offs = per_vertex
3446 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
3447 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
3448
3449 Temp hs_ring_tess_offchip = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
3450 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
3451 store_vmem_mubuf(ctx, store_val, hs_ring_tess_offchip, vmem_offs.first, oc_lds, vmem_offs.second, elem_size_bytes, write_mask, true, false);
3452 }
3453
3454 if (write_to_lds) {
3455 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3456 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3457 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
3458 }
3459 }
3460
3461 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3462 {
3463 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3464 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3465
3466 Builder bld(ctx->program, ctx->block);
3467
3468 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3469 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3470 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3471 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3472
3473 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
3474 }
3475
3476 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
3477 {
3478 if (ctx->stage == vertex_vs ||
3479 ctx->stage == tess_eval_vs ||
3480 ctx->stage == fragment_fs ||
3481 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
3482 bool stored_to_temps = store_output_to_temps(ctx, instr);
3483 if (!stored_to_temps) {
3484 fprintf(stderr, "Unimplemented output offset instruction:\n");
3485 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
3486 fprintf(stderr, "\n");
3487 abort();
3488 }
3489 } else if (ctx->stage == vertex_es ||
3490 ctx->stage == vertex_ls ||
3491 ctx->stage == tess_eval_es ||
3492 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3493 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3494 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
3495 visit_store_ls_or_es_output(ctx, instr);
3496 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
3497 visit_store_tcs_output(ctx, instr, false);
3498 } else {
3499 unreachable("Shader stage not implemented");
3500 }
3501 }
3502
3503 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
3504 {
3505 visit_load_tcs_output(ctx, instr, false);
3506 }
3507
3508 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
3509 {
3510 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
3511 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
3512
3513 Builder bld(ctx->program, ctx->block);
3514 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
3515 if (ctx->program->has_16bank_lds)
3516 interp_p1.instr->operands[0].setLateKill(true);
3517 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
3518 }
3519
3520 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
3521 {
3522 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
3523 for (unsigned i = 0; i < num_components; i++)
3524 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
3525 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
3526 assert(num_components == 4);
3527 Builder bld(ctx->program, ctx->block);
3528 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
3529 }
3530
3531 for (Operand& op : vec->operands)
3532 op = op.isUndefined() ? Operand(0u) : op;
3533
3534 vec->definitions[0] = Definition(dst);
3535 ctx->block->instructions.emplace_back(std::move(vec));
3536 emit_split_vector(ctx, dst, num_components);
3537 return;
3538 }
3539
3540 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
3541 {
3542 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3543 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
3544 unsigned idx = nir_intrinsic_base(instr);
3545 unsigned component = nir_intrinsic_component(instr);
3546 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3547
3548 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
3549 if (offset) {
3550 assert(offset->u32 == 0);
3551 } else {
3552 /* the lower 15bit of the prim_mask contain the offset into LDS
3553 * while the upper bits contain the number of prims */
3554 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
3555 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3556 Builder bld(ctx->program, ctx->block);
3557 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3558 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3559 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3560 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3561 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3562 }
3563
3564 if (instr->dest.ssa.num_components == 1) {
3565 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
3566 } else {
3567 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
3568 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
3569 {
3570 Temp tmp = {ctx->program->allocateId(), v1};
3571 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
3572 vec->operands[i] = Operand(tmp);
3573 }
3574 vec->definitions[0] = Definition(dst);
3575 ctx->block->instructions.emplace_back(std::move(vec));
3576 }
3577 }
3578
3579 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
3580 unsigned offset, unsigned stride, unsigned channels)
3581 {
3582 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
3583 if (vtx_info->chan_byte_size != 4 && channels == 3)
3584 return false;
3585 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
3586 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
3587 }
3588
3589 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
3590 unsigned offset, unsigned stride, unsigned *channels)
3591 {
3592 if (!vtx_info->chan_byte_size) {
3593 *channels = vtx_info->num_channels;
3594 return vtx_info->chan_format;
3595 }
3596
3597 unsigned num_channels = *channels;
3598 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
3599 unsigned new_channels = num_channels + 1;
3600 /* first, assume more loads is worse and try using a larger data format */
3601 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
3602 new_channels++;
3603 /* don't make the attribute potentially out-of-bounds */
3604 if (offset + new_channels * vtx_info->chan_byte_size > stride)
3605 new_channels = 5;
3606 }
3607
3608 if (new_channels == 5) {
3609 /* then try decreasing load size (at the cost of more loads) */
3610 new_channels = *channels;
3611 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
3612 new_channels--;
3613 }
3614
3615 if (new_channels < *channels)
3616 *channels = new_channels;
3617 num_channels = new_channels;
3618 }
3619
3620 switch (vtx_info->chan_format) {
3621 case V_008F0C_BUF_DATA_FORMAT_8:
3622 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
3623 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
3624 case V_008F0C_BUF_DATA_FORMAT_16:
3625 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
3626 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
3627 case V_008F0C_BUF_DATA_FORMAT_32:
3628 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
3629 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
3630 }
3631 unreachable("shouldn't reach here");
3632 return V_008F0C_BUF_DATA_FORMAT_INVALID;
3633 }
3634
3635 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3636 * so we may need to fix it up. */
3637 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
3638 {
3639 Builder bld(ctx->program, ctx->block);
3640
3641 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
3642 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
3643
3644 /* For the integer-like cases, do a natural sign extension.
3645 *
3646 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3647 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3648 * exponent.
3649 */
3650 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
3651 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
3652
3653 /* Convert back to the right type. */
3654 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
3655 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
3656 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
3657 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
3658 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
3659 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
3660 }
3661
3662 return alpha;
3663 }
3664
3665 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
3666 {
3667 Builder bld(ctx->program, ctx->block);
3668 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3669 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
3670
3671 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
3672 if (off_instr->type != nir_instr_type_load_const) {
3673 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3674 nir_print_instr(off_instr, stderr);
3675 fprintf(stderr, "\n");
3676 }
3677 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
3678
3679 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
3680
3681 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
3682 unsigned component = nir_intrinsic_component(instr);
3683 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
3684 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
3685 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
3686 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
3687
3688 unsigned dfmt = attrib_format & 0xf;
3689 unsigned nfmt = (attrib_format >> 4) & 0x7;
3690 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
3691
3692 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
3693 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
3694 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
3695 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
3696 if (post_shuffle)
3697 num_channels = MAX2(num_channels, 3);
3698
3699 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
3700 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
3701
3702 Temp index;
3703 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
3704 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
3705 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
3706 if (divisor) {
3707 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
3708 if (divisor != 1) {
3709 Temp divided = bld.tmp(v1);
3710 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
3711 index = bld.vadd32(bld.def(v1), start_instance, divided);
3712 } else {
3713 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
3714 }
3715 } else {
3716 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
3717 }
3718 } else {
3719 index = bld.vadd32(bld.def(v1),
3720 get_arg(ctx, ctx->args->ac.base_vertex),
3721 get_arg(ctx, ctx->args->ac.vertex_id));
3722 }
3723
3724 Temp channels[num_channels];
3725 unsigned channel_start = 0;
3726 bool direct_fetch = false;
3727
3728 /* skip unused channels at the start */
3729 if (vtx_info->chan_byte_size && !post_shuffle) {
3730 channel_start = ffs(mask) - 1;
3731 for (unsigned i = 0; i < channel_start; i++)
3732 channels[i] = Temp(0, s1);
3733 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
3734 num_channels = 3 - (ffs(mask) - 1);
3735 }
3736
3737 /* load channels */
3738 while (channel_start < num_channels) {
3739 unsigned fetch_size = num_channels - channel_start;
3740 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
3741 bool expanded = false;
3742
3743 /* use MUBUF when possible to avoid possible alignment issues */
3744 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
3745 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
3746 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
3747 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
3748 vtx_info->chan_byte_size == 4;
3749 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
3750 if (!use_mubuf) {
3751 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
3752 } else {
3753 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
3754 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
3755 fetch_size = 4;
3756 expanded = true;
3757 }
3758 }
3759
3760 Temp fetch_index = index;
3761 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
3762 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
3763 fetch_offset = fetch_offset % attrib_stride;
3764 }
3765
3766 Operand soffset(0u);
3767 if (fetch_offset >= 4096) {
3768 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
3769 fetch_offset %= 4096;
3770 }
3771
3772 aco_opcode opcode;
3773 switch (fetch_size) {
3774 case 1:
3775 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
3776 break;
3777 case 2:
3778 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
3779 break;
3780 case 3:
3781 assert(ctx->options->chip_class >= GFX7 ||
3782 (!use_mubuf && ctx->options->chip_class == GFX6));
3783 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
3784 break;
3785 case 4:
3786 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
3787 break;
3788 default:
3789 unreachable("Unimplemented load_input vector size");
3790 }
3791
3792 Temp fetch_dst;
3793 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
3794 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
3795 num_channels <= 3)) {
3796 direct_fetch = true;
3797 fetch_dst = dst;
3798 } else {
3799 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
3800 }
3801
3802 if (use_mubuf) {
3803 Instruction *mubuf = bld.mubuf(opcode,
3804 Definition(fetch_dst), list, fetch_index, soffset,
3805 fetch_offset, false, true).instr;
3806 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
3807 } else {
3808 Instruction *mtbuf = bld.mtbuf(opcode,
3809 Definition(fetch_dst), list, fetch_index, soffset,
3810 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
3811 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
3812 }
3813
3814 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
3815
3816 if (fetch_size == 1) {
3817 channels[channel_start] = fetch_dst;
3818 } else {
3819 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
3820 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
3821 }
3822
3823 channel_start += fetch_size;
3824 }
3825
3826 if (!direct_fetch) {
3827 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
3828 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
3829
3830 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
3831 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
3832 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
3833
3834 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3835 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
3836 unsigned num_temp = 0;
3837 for (unsigned i = 0; i < dst.size(); i++) {
3838 unsigned idx = i + component;
3839 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
3840 Temp channel = channels[swizzle[idx]];
3841 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
3842 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
3843 vec->operands[i] = Operand(channel);
3844
3845 num_temp++;
3846 elems[i] = channel;
3847 } else if (is_float && idx == 3) {
3848 vec->operands[i] = Operand(0x3f800000u);
3849 } else if (!is_float && idx == 3) {
3850 vec->operands[i] = Operand(1u);
3851 } else {
3852 vec->operands[i] = Operand(0u);
3853 }
3854 }
3855 vec->definitions[0] = Definition(dst);
3856 ctx->block->instructions.emplace_back(std::move(vec));
3857 emit_split_vector(ctx, dst, dst.size());
3858
3859 if (num_temp == dst.size())
3860 ctx->allocated_vec.emplace(dst.id(), elems);
3861 }
3862 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
3863 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
3864 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
3865 if (off_instr->type != nir_instr_type_load_const ||
3866 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
3867 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3868 nir_print_instr(off_instr, stderr);
3869 fprintf(stderr, "\n");
3870 }
3871
3872 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3873 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
3874 if (offset) {
3875 assert(offset->u32 == 0);
3876 } else {
3877 /* the lower 15bit of the prim_mask contain the offset into LDS
3878 * while the upper bits contain the number of prims */
3879 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
3880 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3881 Builder bld(ctx->program, ctx->block);
3882 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3883 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3884 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3885 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3886 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3887 }
3888
3889 unsigned idx = nir_intrinsic_base(instr);
3890 unsigned component = nir_intrinsic_component(instr);
3891 unsigned vertex_id = 2; /* P0 */
3892
3893 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
3894 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
3895 switch (src0->u32) {
3896 case 0:
3897 vertex_id = 2; /* P0 */
3898 break;
3899 case 1:
3900 vertex_id = 0; /* P10 */
3901 break;
3902 case 2:
3903 vertex_id = 1; /* P20 */
3904 break;
3905 default:
3906 unreachable("invalid vertex index");
3907 }
3908 }
3909
3910 if (dst.size() == 1) {
3911 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
3912 } else {
3913 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3914 for (unsigned i = 0; i < dst.size(); i++)
3915 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
3916 vec->definitions[0] = Definition(dst);
3917 bld.insert(std::move(vec));
3918 }
3919
3920 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3921 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
3922 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
3923 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
3924 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
3925
3926 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
3927 } else {
3928 unreachable("Shader stage not implemented");
3929 }
3930 }
3931
3932 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
3933 {
3934 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
3935
3936 Builder bld(ctx->program, ctx->block);
3937 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
3938 Temp vertex_offset;
3939
3940 if (!nir_src_is_const(*vertex_src)) {
3941 /* better code could be created, but this case probably doesn't happen
3942 * much in practice */
3943 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
3944 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
3945 Temp elem;
3946
3947 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
3948 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
3949 if (i % 2u)
3950 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
3951 } else {
3952 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
3953 }
3954
3955 if (vertex_offset.id()) {
3956 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
3957 Operand(i), indirect_vertex);
3958 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
3959 } else {
3960 vertex_offset = elem;
3961 }
3962 }
3963
3964 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
3965 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
3966 } else {
3967 unsigned vertex = nir_src_as_uint(*vertex_src);
3968 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
3969 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
3970 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
3971 Operand((vertex % 2u) * 16u), Operand(16u));
3972 else
3973 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
3974 }
3975
3976 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
3977 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
3978 return offset_mul(ctx, offs, 4u);
3979 }
3980
3981 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
3982 {
3983 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
3984
3985 Builder bld(ctx->program, ctx->block);
3986 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3987 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
3988
3989 if (ctx->stage == geometry_gs) {
3990 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
3991 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
3992 load_vmem_mubuf(ctx, dst, ring, offs.first, Temp(), offs.second, elem_size_bytes, instr->dest.ssa.num_components, 4u * ctx->program->wave_size, false, true);
3993 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
3994 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
3995 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3996 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
3997 } else {
3998 unreachable("Unsupported GS stage.");
3999 }
4000 }
4001
4002 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4003 {
4004 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4005
4006 Builder bld(ctx->program, ctx->block);
4007 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4008
4009 if (load_input_from_temps(ctx, instr, dst))
4010 return;
4011
4012 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4013 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4014 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4015
4016 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4017 }
4018
4019 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4020 {
4021 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4022
4023 Builder bld(ctx->program, ctx->block);
4024
4025 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4026 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4027 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4028
4029 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4030 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4031
4032 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4033 }
4034
4035 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4036 {
4037 switch (ctx->shader->info.stage) {
4038 case MESA_SHADER_GEOMETRY:
4039 visit_load_gs_per_vertex_input(ctx, instr);
4040 break;
4041 case MESA_SHADER_TESS_CTRL:
4042 visit_load_tcs_per_vertex_input(ctx, instr);
4043 break;
4044 case MESA_SHADER_TESS_EVAL:
4045 visit_load_tes_per_vertex_input(ctx, instr);
4046 break;
4047 default:
4048 unreachable("Unimplemented shader stage");
4049 }
4050 }
4051
4052 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4053 {
4054 visit_load_tcs_output(ctx, instr, true);
4055 }
4056
4057 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4058 {
4059 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4060 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4061
4062 visit_store_tcs_output(ctx, instr, true);
4063 }
4064
4065 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
4066 {
4067 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4068
4069 Builder bld(ctx->program, ctx->block);
4070 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4071
4072 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
4073 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
4074 Operand tes_w(0u);
4075
4076 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
4077 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
4078 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
4079 tes_w = Operand(tmp);
4080 }
4081
4082 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
4083 emit_split_vector(ctx, tess_coord, 3);
4084 }
4085
4086 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
4087 {
4088 if (ctx->program->info->need_indirect_descriptor_sets) {
4089 Builder bld(ctx->program, ctx->block);
4090 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
4091 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
4092 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
4093 }
4094
4095 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
4096 }
4097
4098
4099 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
4100 {
4101 Builder bld(ctx->program, ctx->block);
4102 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
4103 if (!ctx->divergent_vals[instr->dest.ssa.index])
4104 index = bld.as_uniform(index);
4105 unsigned desc_set = nir_intrinsic_desc_set(instr);
4106 unsigned binding = nir_intrinsic_binding(instr);
4107
4108 Temp desc_ptr;
4109 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
4110 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
4111 unsigned offset = layout->binding[binding].offset;
4112 unsigned stride;
4113 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
4114 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
4115 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
4116 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
4117 offset = pipeline_layout->push_constant_size + 16 * idx;
4118 stride = 16;
4119 } else {
4120 desc_ptr = load_desc_ptr(ctx, desc_set);
4121 stride = layout->binding[binding].size;
4122 }
4123
4124 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
4125 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
4126 if (stride != 1) {
4127 if (nir_const_index) {
4128 const_index = const_index * stride;
4129 } else if (index.type() == RegType::vgpr) {
4130 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
4131 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
4132 } else {
4133 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
4134 }
4135 }
4136 if (offset) {
4137 if (nir_const_index) {
4138 const_index = const_index + offset;
4139 } else if (index.type() == RegType::vgpr) {
4140 index = bld.vadd32(bld.def(v1), Operand(offset), index);
4141 } else {
4142 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
4143 }
4144 }
4145
4146 if (nir_const_index && const_index == 0) {
4147 index = desc_ptr;
4148 } else if (index.type() == RegType::vgpr) {
4149 index = bld.vadd32(bld.def(v1),
4150 nir_const_index ? Operand(const_index) : Operand(index),
4151 Operand(desc_ptr));
4152 } else {
4153 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
4154 nir_const_index ? Operand(const_index) : Operand(index),
4155 Operand(desc_ptr));
4156 }
4157
4158 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
4159 }
4160
4161 void load_buffer(isel_context *ctx, unsigned num_components, Temp dst,
4162 Temp rsrc, Temp offset, bool glc=false, bool readonly=true)
4163 {
4164 Builder bld(ctx->program, ctx->block);
4165
4166 unsigned num_bytes = dst.size() * 4;
4167 bool dlc = glc && ctx->options->chip_class >= GFX10;
4168
4169 aco_opcode op;
4170 if (dst.type() == RegType::vgpr || (ctx->options->chip_class < GFX8 && !readonly)) {
4171 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4172 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4173 unsigned const_offset = 0;
4174
4175 Temp lower = Temp();
4176 if (num_bytes > 16) {
4177 assert(num_components == 3 || num_components == 4);
4178 op = aco_opcode::buffer_load_dwordx4;
4179 lower = bld.tmp(v4);
4180 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4181 mubuf->definitions[0] = Definition(lower);
4182 mubuf->operands[0] = Operand(rsrc);
4183 mubuf->operands[1] = vaddr;
4184 mubuf->operands[2] = soffset;
4185 mubuf->offen = (offset.type() == RegType::vgpr);
4186 mubuf->glc = glc;
4187 mubuf->dlc = dlc;
4188 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4189 mubuf->can_reorder = readonly;
4190 bld.insert(std::move(mubuf));
4191 emit_split_vector(ctx, lower, 2);
4192 num_bytes -= 16;
4193 const_offset = 16;
4194 } else if (num_bytes == 12 && ctx->options->chip_class == GFX6) {
4195 /* GFX6 doesn't support loading vec3, expand to vec4. */
4196 num_bytes = 16;
4197 }
4198
4199 switch (num_bytes) {
4200 case 4:
4201 op = aco_opcode::buffer_load_dword;
4202 break;
4203 case 8:
4204 op = aco_opcode::buffer_load_dwordx2;
4205 break;
4206 case 12:
4207 assert(ctx->options->chip_class > GFX6);
4208 op = aco_opcode::buffer_load_dwordx3;
4209 break;
4210 case 16:
4211 op = aco_opcode::buffer_load_dwordx4;
4212 break;
4213 default:
4214 unreachable("Load SSBO not implemented for this size.");
4215 }
4216 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4217 mubuf->operands[0] = Operand(rsrc);
4218 mubuf->operands[1] = vaddr;
4219 mubuf->operands[2] = soffset;
4220 mubuf->offen = (offset.type() == RegType::vgpr);
4221 mubuf->glc = glc;
4222 mubuf->dlc = dlc;
4223 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4224 mubuf->can_reorder = readonly;
4225 mubuf->offset = const_offset;
4226 aco_ptr<Instruction> instr = std::move(mubuf);
4227
4228 if (dst.size() > 4) {
4229 assert(lower != Temp());
4230 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
4231 instr->definitions[0] = Definition(upper);
4232 bld.insert(std::move(instr));
4233 if (dst.size() == 8)
4234 emit_split_vector(ctx, upper, 2);
4235 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
4236 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
4237 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
4238 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
4239 if (dst.size() == 8)
4240 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
4241 } else if (dst.size() == 3 && ctx->options->chip_class == GFX6) {
4242 Temp vec = bld.tmp(v4);
4243 instr->definitions[0] = Definition(vec);
4244 bld.insert(std::move(instr));
4245 emit_split_vector(ctx, vec, 4);
4246
4247 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
4248 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
4249 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
4250 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
4251 }
4252
4253 if (dst.type() == RegType::sgpr) {
4254 Temp vec = bld.tmp(RegType::vgpr, dst.size());
4255 instr->definitions[0] = Definition(vec);
4256 bld.insert(std::move(instr));
4257 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
4258 } else {
4259 instr->definitions[0] = Definition(dst);
4260 bld.insert(std::move(instr));
4261 emit_split_vector(ctx, dst, num_components);
4262 }
4263 } else {
4264 switch (num_bytes) {
4265 case 4:
4266 op = aco_opcode::s_buffer_load_dword;
4267 break;
4268 case 8:
4269 op = aco_opcode::s_buffer_load_dwordx2;
4270 break;
4271 case 12:
4272 case 16:
4273 op = aco_opcode::s_buffer_load_dwordx4;
4274 break;
4275 case 24:
4276 case 32:
4277 op = aco_opcode::s_buffer_load_dwordx8;
4278 break;
4279 default:
4280 unreachable("Load SSBO not implemented for this size.");
4281 }
4282 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
4283 load->operands[0] = Operand(rsrc);
4284 load->operands[1] = Operand(bld.as_uniform(offset));
4285 assert(load->operands[1].getTemp().type() == RegType::sgpr);
4286 load->definitions[0] = Definition(dst);
4287 load->glc = glc;
4288 load->dlc = dlc;
4289 load->barrier = readonly ? barrier_none : barrier_buffer;
4290 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
4291 assert(ctx->options->chip_class >= GFX8 || !glc);
4292
4293 /* trim vector */
4294 if (dst.size() == 3) {
4295 Temp vec = bld.tmp(s4);
4296 load->definitions[0] = Definition(vec);
4297 bld.insert(std::move(load));
4298 emit_split_vector(ctx, vec, 4);
4299
4300 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4301 emit_extract_vector(ctx, vec, 0, s1),
4302 emit_extract_vector(ctx, vec, 1, s1),
4303 emit_extract_vector(ctx, vec, 2, s1));
4304 } else if (dst.size() == 6) {
4305 Temp vec = bld.tmp(s8);
4306 load->definitions[0] = Definition(vec);
4307 bld.insert(std::move(load));
4308 emit_split_vector(ctx, vec, 4);
4309
4310 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4311 emit_extract_vector(ctx, vec, 0, s2),
4312 emit_extract_vector(ctx, vec, 1, s2),
4313 emit_extract_vector(ctx, vec, 2, s2));
4314 } else {
4315 bld.insert(std::move(load));
4316 }
4317 emit_split_vector(ctx, dst, num_components);
4318 }
4319 }
4320
4321 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
4322 {
4323 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4324 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
4325
4326 Builder bld(ctx->program, ctx->block);
4327
4328 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
4329 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
4330 unsigned binding = nir_intrinsic_binding(idx_instr);
4331 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
4332
4333 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
4334 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4335 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4336 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4337 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4338 if (ctx->options->chip_class >= GFX10) {
4339 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4340 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4341 S_008F0C_RESOURCE_LEVEL(1);
4342 } else {
4343 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4344 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4345 }
4346 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
4347 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
4348 Operand(0xFFFFFFFFu),
4349 Operand(desc_type));
4350 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4351 rsrc, upper_dwords);
4352 } else {
4353 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
4354 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4355 }
4356
4357 load_buffer(ctx, instr->num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa));
4358 }
4359
4360 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4361 {
4362 Builder bld(ctx->program, ctx->block);
4363 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4364
4365 unsigned offset = nir_intrinsic_base(instr);
4366 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
4367 if (index_cv && instr->dest.ssa.bit_size == 32) {
4368
4369 unsigned count = instr->dest.ssa.num_components;
4370 unsigned start = (offset + index_cv->u32) / 4u;
4371 start -= ctx->args->ac.base_inline_push_consts;
4372 if (start + count <= ctx->args->ac.num_inline_push_consts) {
4373 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4374 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4375 for (unsigned i = 0; i < count; ++i) {
4376 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
4377 vec->operands[i] = Operand{elems[i]};
4378 }
4379 vec->definitions[0] = Definition(dst);
4380 ctx->block->instructions.emplace_back(std::move(vec));
4381 ctx->allocated_vec.emplace(dst.id(), elems);
4382 return;
4383 }
4384 }
4385
4386 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
4387 if (offset != 0) // TODO check if index != 0 as well
4388 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
4389 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
4390 Temp vec = dst;
4391 bool trim = false;
4392 aco_opcode op;
4393
4394 switch (dst.size()) {
4395 case 1:
4396 op = aco_opcode::s_load_dword;
4397 break;
4398 case 2:
4399 op = aco_opcode::s_load_dwordx2;
4400 break;
4401 case 3:
4402 vec = bld.tmp(s4);
4403 trim = true;
4404 case 4:
4405 op = aco_opcode::s_load_dwordx4;
4406 break;
4407 case 6:
4408 vec = bld.tmp(s8);
4409 trim = true;
4410 case 8:
4411 op = aco_opcode::s_load_dwordx8;
4412 break;
4413 default:
4414 unreachable("unimplemented or forbidden load_push_constant.");
4415 }
4416
4417 bld.smem(op, Definition(vec), ptr, index);
4418
4419 if (trim) {
4420 emit_split_vector(ctx, vec, 4);
4421 RegClass rc = dst.size() == 3 ? s1 : s2;
4422 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4423 emit_extract_vector(ctx, vec, 0, rc),
4424 emit_extract_vector(ctx, vec, 1, rc),
4425 emit_extract_vector(ctx, vec, 2, rc));
4426
4427 }
4428 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4429 }
4430
4431 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4432 {
4433 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4434
4435 Builder bld(ctx->program, ctx->block);
4436
4437 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4438 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4439 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4440 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4441 if (ctx->options->chip_class >= GFX10) {
4442 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4443 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4444 S_008F0C_RESOURCE_LEVEL(1);
4445 } else {
4446 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4447 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4448 }
4449
4450 unsigned base = nir_intrinsic_base(instr);
4451 unsigned range = nir_intrinsic_range(instr);
4452
4453 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
4454 if (base && offset.type() == RegType::sgpr)
4455 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
4456 else if (base && offset.type() == RegType::vgpr)
4457 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
4458
4459 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4460 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
4461 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
4462 Operand(desc_type));
4463
4464 load_buffer(ctx, instr->num_components, dst, rsrc, offset);
4465 }
4466
4467 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
4468 {
4469 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
4470 ctx->cf_info.exec_potentially_empty_discard = true;
4471
4472 ctx->program->needs_exact = true;
4473
4474 // TODO: optimize uniform conditions
4475 Builder bld(ctx->program, ctx->block);
4476 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4477 assert(src.regClass() == bld.lm);
4478 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
4479 bld.pseudo(aco_opcode::p_discard_if, src);
4480 ctx->block->kind |= block_kind_uses_discard_if;
4481 return;
4482 }
4483
4484 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
4485 {
4486 Builder bld(ctx->program, ctx->block);
4487
4488 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
4489 ctx->cf_info.exec_potentially_empty_discard = true;
4490
4491 bool divergent = ctx->cf_info.parent_if.is_divergent ||
4492 ctx->cf_info.parent_loop.has_divergent_continue;
4493
4494 if (ctx->block->loop_nest_depth &&
4495 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
4496 /* we handle discards the same way as jump instructions */
4497 append_logical_end(ctx->block);
4498
4499 /* in loops, discard behaves like break */
4500 Block *linear_target = ctx->cf_info.parent_loop.exit;
4501 ctx->block->kind |= block_kind_discard;
4502
4503 if (!divergent) {
4504 /* uniform discard - loop ends here */
4505 assert(nir_instr_is_last(&instr->instr));
4506 ctx->block->kind |= block_kind_uniform;
4507 ctx->cf_info.has_branch = true;
4508 bld.branch(aco_opcode::p_branch);
4509 add_linear_edge(ctx->block->index, linear_target);
4510 return;
4511 }
4512
4513 /* we add a break right behind the discard() instructions */
4514 ctx->block->kind |= block_kind_break;
4515 unsigned idx = ctx->block->index;
4516
4517 ctx->cf_info.parent_loop.has_divergent_branch = true;
4518 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
4519
4520 /* remove critical edges from linear CFG */
4521 bld.branch(aco_opcode::p_branch);
4522 Block* break_block = ctx->program->create_and_insert_block();
4523 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
4524 break_block->kind |= block_kind_uniform;
4525 add_linear_edge(idx, break_block);
4526 add_linear_edge(break_block->index, linear_target);
4527 bld.reset(break_block);
4528 bld.branch(aco_opcode::p_branch);
4529
4530 Block* continue_block = ctx->program->create_and_insert_block();
4531 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
4532 add_linear_edge(idx, continue_block);
4533 append_logical_start(continue_block);
4534 ctx->block = continue_block;
4535
4536 return;
4537 }
4538
4539 /* it can currently happen that NIR doesn't remove the unreachable code */
4540 if (!nir_instr_is_last(&instr->instr)) {
4541 ctx->program->needs_exact = true;
4542 /* save exec somewhere temporarily so that it doesn't get
4543 * overwritten before the discard from outer exec masks */
4544 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
4545 bld.pseudo(aco_opcode::p_discard_if, cond);
4546 ctx->block->kind |= block_kind_uses_discard_if;
4547 return;
4548 }
4549
4550 /* This condition is incorrect for uniformly branched discards in a loop
4551 * predicated by a divergent condition, but the above code catches that case
4552 * and the discard would end up turning into a discard_if.
4553 * For example:
4554 * if (divergent) {
4555 * while (...) {
4556 * if (uniform) {
4557 * discard;
4558 * }
4559 * }
4560 * }
4561 */
4562 if (!ctx->cf_info.parent_if.is_divergent) {
4563 /* program just ends here */
4564 ctx->block->kind |= block_kind_uniform;
4565 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
4566 0 /* enabled mask */, 9 /* dest */,
4567 false /* compressed */, true/* done */, true /* valid mask */);
4568 bld.sopp(aco_opcode::s_endpgm);
4569 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
4570 } else {
4571 ctx->block->kind |= block_kind_discard;
4572 /* branch and linear edge is added by visit_if() */
4573 }
4574 }
4575
4576 enum aco_descriptor_type {
4577 ACO_DESC_IMAGE,
4578 ACO_DESC_FMASK,
4579 ACO_DESC_SAMPLER,
4580 ACO_DESC_BUFFER,
4581 ACO_DESC_PLANE_0,
4582 ACO_DESC_PLANE_1,
4583 ACO_DESC_PLANE_2,
4584 };
4585
4586 static bool
4587 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
4588 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
4589 return false;
4590 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
4591 return dim == ac_image_cube ||
4592 dim == ac_image_1darray ||
4593 dim == ac_image_2darray ||
4594 dim == ac_image_2darraymsaa;
4595 }
4596
4597 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
4598 enum aco_descriptor_type desc_type,
4599 const nir_tex_instr *tex_instr, bool image, bool write)
4600 {
4601 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
4602 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
4603 if (it != ctx->tex_desc.end())
4604 return it->second;
4605 */
4606 Temp index = Temp();
4607 bool index_set = false;
4608 unsigned constant_index = 0;
4609 unsigned descriptor_set;
4610 unsigned base_index;
4611 Builder bld(ctx->program, ctx->block);
4612
4613 if (!deref_instr) {
4614 assert(tex_instr && !image);
4615 descriptor_set = 0;
4616 base_index = tex_instr->sampler_index;
4617 } else {
4618 while(deref_instr->deref_type != nir_deref_type_var) {
4619 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
4620 if (!array_size)
4621 array_size = 1;
4622
4623 assert(deref_instr->deref_type == nir_deref_type_array);
4624 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
4625 if (const_value) {
4626 constant_index += array_size * const_value->u32;
4627 } else {
4628 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
4629 if (indirect.type() == RegType::vgpr)
4630 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
4631
4632 if (array_size != 1)
4633 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
4634
4635 if (!index_set) {
4636 index = indirect;
4637 index_set = true;
4638 } else {
4639 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
4640 }
4641 }
4642
4643 deref_instr = nir_src_as_deref(deref_instr->parent);
4644 }
4645 descriptor_set = deref_instr->var->data.descriptor_set;
4646 base_index = deref_instr->var->data.binding;
4647 }
4648
4649 Temp list = load_desc_ptr(ctx, descriptor_set);
4650 list = convert_pointer_to_64_bit(ctx, list);
4651
4652 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
4653 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
4654 unsigned offset = binding->offset;
4655 unsigned stride = binding->size;
4656 aco_opcode opcode;
4657 RegClass type;
4658
4659 assert(base_index < layout->binding_count);
4660
4661 switch (desc_type) {
4662 case ACO_DESC_IMAGE:
4663 type = s8;
4664 opcode = aco_opcode::s_load_dwordx8;
4665 break;
4666 case ACO_DESC_FMASK:
4667 type = s8;
4668 opcode = aco_opcode::s_load_dwordx8;
4669 offset += 32;
4670 break;
4671 case ACO_DESC_SAMPLER:
4672 type = s4;
4673 opcode = aco_opcode::s_load_dwordx4;
4674 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
4675 offset += radv_combined_image_descriptor_sampler_offset(binding);
4676 break;
4677 case ACO_DESC_BUFFER:
4678 type = s4;
4679 opcode = aco_opcode::s_load_dwordx4;
4680 break;
4681 case ACO_DESC_PLANE_0:
4682 case ACO_DESC_PLANE_1:
4683 type = s8;
4684 opcode = aco_opcode::s_load_dwordx8;
4685 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
4686 break;
4687 case ACO_DESC_PLANE_2:
4688 type = s4;
4689 opcode = aco_opcode::s_load_dwordx4;
4690 offset += 64;
4691 break;
4692 default:
4693 unreachable("invalid desc_type\n");
4694 }
4695
4696 offset += constant_index * stride;
4697
4698 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
4699 (!index_set || binding->immutable_samplers_equal)) {
4700 if (binding->immutable_samplers_equal)
4701 constant_index = 0;
4702
4703 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
4704 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4705 Operand(samplers[constant_index * 4 + 0]),
4706 Operand(samplers[constant_index * 4 + 1]),
4707 Operand(samplers[constant_index * 4 + 2]),
4708 Operand(samplers[constant_index * 4 + 3]));
4709 }
4710
4711 Operand off;
4712 if (!index_set) {
4713 off = bld.copy(bld.def(s1), Operand(offset));
4714 } else {
4715 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
4716 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
4717 }
4718
4719 Temp res = bld.smem(opcode, bld.def(type), list, off);
4720
4721 if (desc_type == ACO_DESC_PLANE_2) {
4722 Temp components[8];
4723 for (unsigned i = 0; i < 8; i++)
4724 components[i] = bld.tmp(s1);
4725 bld.pseudo(aco_opcode::p_split_vector,
4726 Definition(components[0]),
4727 Definition(components[1]),
4728 Definition(components[2]),
4729 Definition(components[3]),
4730 res);
4731
4732 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
4733 bld.pseudo(aco_opcode::p_split_vector,
4734 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
4735 Definition(components[4]),
4736 Definition(components[5]),
4737 Definition(components[6]),
4738 Definition(components[7]),
4739 desc2);
4740
4741 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
4742 components[0], components[1], components[2], components[3],
4743 components[4], components[5], components[6], components[7]);
4744 }
4745
4746 return res;
4747 }
4748
4749 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
4750 {
4751 switch (dim) {
4752 case GLSL_SAMPLER_DIM_BUF:
4753 return 1;
4754 case GLSL_SAMPLER_DIM_1D:
4755 return array ? 2 : 1;
4756 case GLSL_SAMPLER_DIM_2D:
4757 return array ? 3 : 2;
4758 case GLSL_SAMPLER_DIM_MS:
4759 return array ? 4 : 3;
4760 case GLSL_SAMPLER_DIM_3D:
4761 case GLSL_SAMPLER_DIM_CUBE:
4762 return 3;
4763 case GLSL_SAMPLER_DIM_RECT:
4764 case GLSL_SAMPLER_DIM_SUBPASS:
4765 return 2;
4766 case GLSL_SAMPLER_DIM_SUBPASS_MS:
4767 return 3;
4768 default:
4769 break;
4770 }
4771 return 0;
4772 }
4773
4774
4775 /* Adjust the sample index according to FMASK.
4776 *
4777 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4778 * which is the identity mapping. Each nibble says which physical sample
4779 * should be fetched to get that sample.
4780 *
4781 * For example, 0x11111100 means there are only 2 samples stored and
4782 * the second sample covers 3/4 of the pixel. When reading samples 0
4783 * and 1, return physical sample 0 (determined by the first two 0s
4784 * in FMASK), otherwise return physical sample 1.
4785 *
4786 * The sample index should be adjusted as follows:
4787 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4788 */
4789 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
4790 {
4791 Builder bld(ctx->program, ctx->block);
4792 Temp fmask = bld.tmp(v1);
4793 unsigned dim = ctx->options->chip_class >= GFX10
4794 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
4795 : 0;
4796
4797 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
4798 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
4799 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
4800 load->operands[0] = Operand(fmask_desc_ptr);
4801 load->operands[1] = Operand(s4); /* no sampler */
4802 load->operands[2] = Operand(coord);
4803 load->definitions[0] = Definition(fmask);
4804 load->glc = false;
4805 load->dlc = false;
4806 load->dmask = 0x1;
4807 load->unrm = true;
4808 load->da = da;
4809 load->dim = dim;
4810 load->can_reorder = true; /* fmask images shouldn't be modified */
4811 ctx->block->instructions.emplace_back(std::move(load));
4812
4813 Operand sample_index4;
4814 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
4815 sample_index4 = Operand(sample_index.constantValue() << 2);
4816 } else if (sample_index.regClass() == s1) {
4817 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
4818 } else {
4819 assert(sample_index.regClass() == v1);
4820 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
4821 }
4822
4823 Temp final_sample;
4824 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
4825 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
4826 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
4827 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
4828 else
4829 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
4830
4831 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4832 * resource descriptor is 0 (invalid),
4833 */
4834 Temp compare = bld.tmp(bld.lm);
4835 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
4836 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
4837
4838 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
4839
4840 /* Replace the MSAA sample index. */
4841 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
4842 }
4843
4844 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
4845 {
4846
4847 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
4848 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4849 bool is_array = glsl_sampler_type_is_array(type);
4850 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4851 assert(!add_frag_pos && "Input attachments should be lowered.");
4852 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4853 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
4854 int count = image_type_to_components_count(dim, is_array);
4855 std::vector<Temp> coords(count);
4856 Builder bld(ctx->program, ctx->block);
4857
4858 if (is_ms) {
4859 count--;
4860 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
4861 /* get sample index */
4862 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
4863 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
4864 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
4865 std::vector<Temp> fmask_load_address;
4866 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
4867 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
4868
4869 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
4870 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
4871 } else {
4872 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
4873 }
4874 }
4875
4876 if (gfx9_1d) {
4877 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
4878 coords.resize(coords.size() + 1);
4879 coords[1] = bld.copy(bld.def(v1), Operand(0u));
4880 if (is_array)
4881 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
4882 } else {
4883 for (int i = 0; i < count; i++)
4884 coords[i] = emit_extract_vector(ctx, src0, i, v1);
4885 }
4886
4887 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
4888 instr->intrinsic == nir_intrinsic_image_deref_store) {
4889 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
4890 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
4891
4892 if (!level_zero)
4893 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
4894 }
4895
4896 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
4897 for (unsigned i = 0; i < coords.size(); i++)
4898 vec->operands[i] = Operand(coords[i]);
4899 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
4900 vec->definitions[0] = Definition(res);
4901 ctx->block->instructions.emplace_back(std::move(vec));
4902 return res;
4903 }
4904
4905
4906 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
4907 {
4908 Builder bld(ctx->program, ctx->block);
4909 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4910 const struct glsl_type *type = glsl_without_array(var->type);
4911 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4912 bool is_array = glsl_sampler_type_is_array(type);
4913 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4914
4915 if (dim == GLSL_SAMPLER_DIM_BUF) {
4916 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
4917 unsigned num_channels = util_last_bit(mask);
4918 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4919 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4920
4921 aco_opcode opcode;
4922 switch (num_channels) {
4923 case 1:
4924 opcode = aco_opcode::buffer_load_format_x;
4925 break;
4926 case 2:
4927 opcode = aco_opcode::buffer_load_format_xy;
4928 break;
4929 case 3:
4930 opcode = aco_opcode::buffer_load_format_xyz;
4931 break;
4932 case 4:
4933 opcode = aco_opcode::buffer_load_format_xyzw;
4934 break;
4935 default:
4936 unreachable(">4 channel buffer image load");
4937 }
4938 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
4939 load->operands[0] = Operand(rsrc);
4940 load->operands[1] = Operand(vindex);
4941 load->operands[2] = Operand((uint32_t) 0);
4942 Temp tmp;
4943 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4944 tmp = dst;
4945 else
4946 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
4947 load->definitions[0] = Definition(tmp);
4948 load->idxen = true;
4949 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
4950 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4951 load->barrier = barrier_image;
4952 ctx->block->instructions.emplace_back(std::move(load));
4953
4954 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
4955 return;
4956 }
4957
4958 Temp coords = get_image_coords(ctx, instr, type);
4959 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4960
4961 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
4962 unsigned num_components = util_bitcount(dmask);
4963 Temp tmp;
4964 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4965 tmp = dst;
4966 else
4967 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
4968
4969 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
4970 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
4971
4972 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
4973 load->operands[0] = Operand(resource);
4974 load->operands[1] = Operand(s4); /* no sampler */
4975 load->operands[2] = Operand(coords);
4976 load->definitions[0] = Definition(tmp);
4977 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
4978 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4979 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4980 load->dmask = dmask;
4981 load->unrm = true;
4982 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4983 load->barrier = barrier_image;
4984 ctx->block->instructions.emplace_back(std::move(load));
4985
4986 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
4987 return;
4988 }
4989
4990 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
4991 {
4992 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4993 const struct glsl_type *type = glsl_without_array(var->type);
4994 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4995 bool is_array = glsl_sampler_type_is_array(type);
4996 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4997
4998 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
4999
5000 if (dim == GLSL_SAMPLER_DIM_BUF) {
5001 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5002 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5003 aco_opcode opcode;
5004 switch (data.size()) {
5005 case 1:
5006 opcode = aco_opcode::buffer_store_format_x;
5007 break;
5008 case 2:
5009 opcode = aco_opcode::buffer_store_format_xy;
5010 break;
5011 case 3:
5012 opcode = aco_opcode::buffer_store_format_xyz;
5013 break;
5014 case 4:
5015 opcode = aco_opcode::buffer_store_format_xyzw;
5016 break;
5017 default:
5018 unreachable(">4 channel buffer image store");
5019 }
5020 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5021 store->operands[0] = Operand(rsrc);
5022 store->operands[1] = Operand(vindex);
5023 store->operands[2] = Operand((uint32_t) 0);
5024 store->operands[3] = Operand(data);
5025 store->idxen = true;
5026 store->glc = glc;
5027 store->dlc = false;
5028 store->disable_wqm = true;
5029 store->barrier = barrier_image;
5030 ctx->program->needs_exact = true;
5031 ctx->block->instructions.emplace_back(std::move(store));
5032 return;
5033 }
5034
5035 assert(data.type() == RegType::vgpr);
5036 Temp coords = get_image_coords(ctx, instr, type);
5037 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5038
5039 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5040 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5041
5042 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5043 store->operands[0] = Operand(resource);
5044 store->operands[1] = Operand(data);
5045 store->operands[2] = Operand(coords);
5046 store->glc = glc;
5047 store->dlc = false;
5048 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5049 store->dmask = (1 << data.size()) - 1;
5050 store->unrm = true;
5051 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5052 store->disable_wqm = true;
5053 store->barrier = barrier_image;
5054 ctx->program->needs_exact = true;
5055 ctx->block->instructions.emplace_back(std::move(store));
5056 return;
5057 }
5058
5059 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5060 {
5061 /* return the previous value if dest is ever used */
5062 bool return_previous = false;
5063 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5064 return_previous = true;
5065 break;
5066 }
5067 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5068 return_previous = true;
5069 break;
5070 }
5071
5072 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5073 const struct glsl_type *type = glsl_without_array(var->type);
5074 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5075 bool is_array = glsl_sampler_type_is_array(type);
5076 Builder bld(ctx->program, ctx->block);
5077
5078 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5079 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5080
5081 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5082 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5083
5084 aco_opcode buf_op, image_op;
5085 switch (instr->intrinsic) {
5086 case nir_intrinsic_image_deref_atomic_add:
5087 buf_op = aco_opcode::buffer_atomic_add;
5088 image_op = aco_opcode::image_atomic_add;
5089 break;
5090 case nir_intrinsic_image_deref_atomic_umin:
5091 buf_op = aco_opcode::buffer_atomic_umin;
5092 image_op = aco_opcode::image_atomic_umin;
5093 break;
5094 case nir_intrinsic_image_deref_atomic_imin:
5095 buf_op = aco_opcode::buffer_atomic_smin;
5096 image_op = aco_opcode::image_atomic_smin;
5097 break;
5098 case nir_intrinsic_image_deref_atomic_umax:
5099 buf_op = aco_opcode::buffer_atomic_umax;
5100 image_op = aco_opcode::image_atomic_umax;
5101 break;
5102 case nir_intrinsic_image_deref_atomic_imax:
5103 buf_op = aco_opcode::buffer_atomic_smax;
5104 image_op = aco_opcode::image_atomic_smax;
5105 break;
5106 case nir_intrinsic_image_deref_atomic_and:
5107 buf_op = aco_opcode::buffer_atomic_and;
5108 image_op = aco_opcode::image_atomic_and;
5109 break;
5110 case nir_intrinsic_image_deref_atomic_or:
5111 buf_op = aco_opcode::buffer_atomic_or;
5112 image_op = aco_opcode::image_atomic_or;
5113 break;
5114 case nir_intrinsic_image_deref_atomic_xor:
5115 buf_op = aco_opcode::buffer_atomic_xor;
5116 image_op = aco_opcode::image_atomic_xor;
5117 break;
5118 case nir_intrinsic_image_deref_atomic_exchange:
5119 buf_op = aco_opcode::buffer_atomic_swap;
5120 image_op = aco_opcode::image_atomic_swap;
5121 break;
5122 case nir_intrinsic_image_deref_atomic_comp_swap:
5123 buf_op = aco_opcode::buffer_atomic_cmpswap;
5124 image_op = aco_opcode::image_atomic_cmpswap;
5125 break;
5126 default:
5127 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5128 }
5129
5130 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5131
5132 if (dim == GLSL_SAMPLER_DIM_BUF) {
5133 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5134 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5135 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5136 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5137 mubuf->operands[0] = Operand(resource);
5138 mubuf->operands[1] = Operand(vindex);
5139 mubuf->operands[2] = Operand((uint32_t)0);
5140 mubuf->operands[3] = Operand(data);
5141 if (return_previous)
5142 mubuf->definitions[0] = Definition(dst);
5143 mubuf->offset = 0;
5144 mubuf->idxen = true;
5145 mubuf->glc = return_previous;
5146 mubuf->dlc = false; /* Not needed for atomics */
5147 mubuf->disable_wqm = true;
5148 mubuf->barrier = barrier_image;
5149 ctx->program->needs_exact = true;
5150 ctx->block->instructions.emplace_back(std::move(mubuf));
5151 return;
5152 }
5153
5154 Temp coords = get_image_coords(ctx, instr, type);
5155 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5156 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5157 mimg->operands[0] = Operand(resource);
5158 mimg->operands[1] = Operand(data);
5159 mimg->operands[2] = Operand(coords);
5160 if (return_previous)
5161 mimg->definitions[0] = Definition(dst);
5162 mimg->glc = return_previous;
5163 mimg->dlc = false; /* Not needed for atomics */
5164 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5165 mimg->dmask = (1 << data.size()) - 1;
5166 mimg->unrm = true;
5167 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5168 mimg->disable_wqm = true;
5169 mimg->barrier = barrier_image;
5170 ctx->program->needs_exact = true;
5171 ctx->block->instructions.emplace_back(std::move(mimg));
5172 return;
5173 }
5174
5175 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
5176 {
5177 if (in_elements && ctx->options->chip_class == GFX8) {
5178 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5179 Builder bld(ctx->program, ctx->block);
5180
5181 Temp size = emit_extract_vector(ctx, desc, 2, s1);
5182
5183 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
5184 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
5185
5186 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
5187 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
5188
5189 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
5190 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
5191
5192 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
5193 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
5194 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
5195 if (dst.type() == RegType::vgpr)
5196 bld.copy(Definition(dst), shr_dst);
5197
5198 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5199 } else {
5200 emit_extract_vector(ctx, desc, 2, dst);
5201 }
5202 }
5203
5204 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
5205 {
5206 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5207 const struct glsl_type *type = glsl_without_array(var->type);
5208 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5209 bool is_array = glsl_sampler_type_is_array(type);
5210 Builder bld(ctx->program, ctx->block);
5211
5212 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
5213 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
5214 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
5215 }
5216
5217 /* LOD */
5218 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
5219
5220 /* Resource */
5221 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
5222
5223 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5224
5225 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
5226 mimg->operands[0] = Operand(resource);
5227 mimg->operands[1] = Operand(s4); /* no sampler */
5228 mimg->operands[2] = Operand(lod);
5229 uint8_t& dmask = mimg->dmask;
5230 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5231 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
5232 mimg->da = glsl_sampler_type_is_array(type);
5233 mimg->can_reorder = true;
5234 Definition& def = mimg->definitions[0];
5235 ctx->block->instructions.emplace_back(std::move(mimg));
5236
5237 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
5238 glsl_sampler_type_is_array(type)) {
5239
5240 assert(instr->dest.ssa.num_components == 3);
5241 Temp tmp = {ctx->program->allocateId(), v3};
5242 def = Definition(tmp);
5243 emit_split_vector(ctx, tmp, 3);
5244
5245 /* divide 3rd value by 6 by multiplying with magic number */
5246 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
5247 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
5248
5249 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5250 emit_extract_vector(ctx, tmp, 0, v1),
5251 emit_extract_vector(ctx, tmp, 1, v1),
5252 by_6);
5253
5254 } else if (ctx->options->chip_class == GFX9 &&
5255 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
5256 glsl_sampler_type_is_array(type)) {
5257 assert(instr->dest.ssa.num_components == 2);
5258 def = Definition(dst);
5259 dmask = 0x5;
5260 } else {
5261 def = Definition(dst);
5262 }
5263
5264 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5265 }
5266
5267 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5268 {
5269 Builder bld(ctx->program, ctx->block);
5270 unsigned num_components = instr->num_components;
5271
5272 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5273 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5274 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5275
5276 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
5277 load_buffer(ctx, num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), glc, false);
5278 }
5279
5280 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5281 {
5282 Builder bld(ctx->program, ctx->block);
5283 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
5284 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5285 unsigned writemask = nir_intrinsic_write_mask(instr);
5286 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
5287
5288 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5289 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5290
5291 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
5292 ctx->options->chip_class >= GFX8;
5293 if (smem)
5294 offset = bld.as_uniform(offset);
5295 bool smem_nonfs = smem && ctx->stage != fragment_fs;
5296
5297 while (writemask) {
5298 int start, count;
5299 u_bit_scan_consecutive_range(&writemask, &start, &count);
5300 if (count == 3 && (smem || ctx->options->chip_class == GFX6)) {
5301 /* GFX6 doesn't support storing vec3, split it. */
5302 writemask |= 1u << (start + 2);
5303 count = 2;
5304 }
5305 int num_bytes = count * elem_size_bytes;
5306
5307 if (num_bytes > 16) {
5308 assert(elem_size_bytes == 8);
5309 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5310 count = 2;
5311 num_bytes = 16;
5312 }
5313
5314 // TODO: check alignment of sub-dword stores
5315 // TODO: split 3 bytes. there is no store instruction for that
5316
5317 Temp write_data;
5318 if (count != instr->num_components) {
5319 emit_split_vector(ctx, data, instr->num_components);
5320 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5321 for (int i = 0; i < count; i++) {
5322 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
5323 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
5324 }
5325 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
5326 vec->definitions[0] = Definition(write_data);
5327 ctx->block->instructions.emplace_back(std::move(vec));
5328 } else if (!smem && data.type() != RegType::vgpr) {
5329 assert(num_bytes % 4 == 0);
5330 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
5331 } else if (smem_nonfs && data.type() == RegType::vgpr) {
5332 assert(num_bytes % 4 == 0);
5333 write_data = bld.as_uniform(data);
5334 } else {
5335 write_data = data;
5336 }
5337
5338 aco_opcode vmem_op, smem_op;
5339 switch (num_bytes) {
5340 case 4:
5341 vmem_op = aco_opcode::buffer_store_dword;
5342 smem_op = aco_opcode::s_buffer_store_dword;
5343 break;
5344 case 8:
5345 vmem_op = aco_opcode::buffer_store_dwordx2;
5346 smem_op = aco_opcode::s_buffer_store_dwordx2;
5347 break;
5348 case 12:
5349 vmem_op = aco_opcode::buffer_store_dwordx3;
5350 smem_op = aco_opcode::last_opcode;
5351 assert(!smem && ctx->options->chip_class > GFX6);
5352 break;
5353 case 16:
5354 vmem_op = aco_opcode::buffer_store_dwordx4;
5355 smem_op = aco_opcode::s_buffer_store_dwordx4;
5356 break;
5357 default:
5358 unreachable("Store SSBO not implemented for this size.");
5359 }
5360 if (ctx->stage == fragment_fs)
5361 smem_op = aco_opcode::p_fs_buffer_store_smem;
5362
5363 if (smem) {
5364 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
5365 store->operands[0] = Operand(rsrc);
5366 if (start) {
5367 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5368 offset, Operand(start * elem_size_bytes));
5369 store->operands[1] = Operand(off);
5370 } else {
5371 store->operands[1] = Operand(offset);
5372 }
5373 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
5374 store->operands[1].setFixed(m0);
5375 store->operands[2] = Operand(write_data);
5376 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5377 store->dlc = false;
5378 store->disable_wqm = true;
5379 store->barrier = barrier_buffer;
5380 ctx->block->instructions.emplace_back(std::move(store));
5381 ctx->program->wb_smem_l1_on_end = true;
5382 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
5383 ctx->block->kind |= block_kind_needs_lowering;
5384 ctx->program->needs_exact = true;
5385 }
5386 } else {
5387 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
5388 store->operands[0] = Operand(rsrc);
5389 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5390 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5391 store->operands[3] = Operand(write_data);
5392 store->offset = start * elem_size_bytes;
5393 store->offen = (offset.type() == RegType::vgpr);
5394 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5395 store->dlc = false;
5396 store->disable_wqm = true;
5397 store->barrier = barrier_buffer;
5398 ctx->program->needs_exact = true;
5399 ctx->block->instructions.emplace_back(std::move(store));
5400 }
5401 }
5402 }
5403
5404 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5405 {
5406 /* return the previous value if dest is ever used */
5407 bool return_previous = false;
5408 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5409 return_previous = true;
5410 break;
5411 }
5412 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5413 return_previous = true;
5414 break;
5415 }
5416
5417 Builder bld(ctx->program, ctx->block);
5418 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
5419
5420 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
5421 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
5422 get_ssa_temp(ctx, instr->src[3].ssa), data);
5423
5424 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
5425 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5426 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5427
5428 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5429
5430 aco_opcode op32, op64;
5431 switch (instr->intrinsic) {
5432 case nir_intrinsic_ssbo_atomic_add:
5433 op32 = aco_opcode::buffer_atomic_add;
5434 op64 = aco_opcode::buffer_atomic_add_x2;
5435 break;
5436 case nir_intrinsic_ssbo_atomic_imin:
5437 op32 = aco_opcode::buffer_atomic_smin;
5438 op64 = aco_opcode::buffer_atomic_smin_x2;
5439 break;
5440 case nir_intrinsic_ssbo_atomic_umin:
5441 op32 = aco_opcode::buffer_atomic_umin;
5442 op64 = aco_opcode::buffer_atomic_umin_x2;
5443 break;
5444 case nir_intrinsic_ssbo_atomic_imax:
5445 op32 = aco_opcode::buffer_atomic_smax;
5446 op64 = aco_opcode::buffer_atomic_smax_x2;
5447 break;
5448 case nir_intrinsic_ssbo_atomic_umax:
5449 op32 = aco_opcode::buffer_atomic_umax;
5450 op64 = aco_opcode::buffer_atomic_umax_x2;
5451 break;
5452 case nir_intrinsic_ssbo_atomic_and:
5453 op32 = aco_opcode::buffer_atomic_and;
5454 op64 = aco_opcode::buffer_atomic_and_x2;
5455 break;
5456 case nir_intrinsic_ssbo_atomic_or:
5457 op32 = aco_opcode::buffer_atomic_or;
5458 op64 = aco_opcode::buffer_atomic_or_x2;
5459 break;
5460 case nir_intrinsic_ssbo_atomic_xor:
5461 op32 = aco_opcode::buffer_atomic_xor;
5462 op64 = aco_opcode::buffer_atomic_xor_x2;
5463 break;
5464 case nir_intrinsic_ssbo_atomic_exchange:
5465 op32 = aco_opcode::buffer_atomic_swap;
5466 op64 = aco_opcode::buffer_atomic_swap_x2;
5467 break;
5468 case nir_intrinsic_ssbo_atomic_comp_swap:
5469 op32 = aco_opcode::buffer_atomic_cmpswap;
5470 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
5471 break;
5472 default:
5473 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
5474 }
5475 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5476 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5477 mubuf->operands[0] = Operand(rsrc);
5478 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5479 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5480 mubuf->operands[3] = Operand(data);
5481 if (return_previous)
5482 mubuf->definitions[0] = Definition(dst);
5483 mubuf->offset = 0;
5484 mubuf->offen = (offset.type() == RegType::vgpr);
5485 mubuf->glc = return_previous;
5486 mubuf->dlc = false; /* Not needed for atomics */
5487 mubuf->disable_wqm = true;
5488 mubuf->barrier = barrier_buffer;
5489 ctx->program->needs_exact = true;
5490 ctx->block->instructions.emplace_back(std::move(mubuf));
5491 }
5492
5493 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
5494
5495 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5496 Builder bld(ctx->program, ctx->block);
5497 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
5498 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
5499 }
5500
5501 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
5502 {
5503 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5504 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5505
5506 if (addr.type() == RegType::vgpr)
5507 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
5508 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
5509 }
5510
5511 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
5512 {
5513 Builder bld(ctx->program, ctx->block);
5514 unsigned num_components = instr->num_components;
5515 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
5516
5517 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5518 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
5519
5520 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
5521 bool dlc = glc && ctx->options->chip_class >= GFX10;
5522 aco_opcode op;
5523 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8)) {
5524 bool global = ctx->options->chip_class >= GFX9;
5525
5526 if (ctx->options->chip_class >= GFX7) {
5527 aco_opcode op;
5528 switch (num_bytes) {
5529 case 4:
5530 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
5531 break;
5532 case 8:
5533 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
5534 break;
5535 case 12:
5536 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
5537 break;
5538 case 16:
5539 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
5540 break;
5541 default:
5542 unreachable("load_global not implemented for this size.");
5543 }
5544
5545 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
5546 flat->operands[0] = Operand(addr);
5547 flat->operands[1] = Operand(s1);
5548 flat->glc = glc;
5549 flat->dlc = dlc;
5550 flat->barrier = barrier_buffer;
5551
5552 if (dst.type() == RegType::sgpr) {
5553 Temp vec = bld.tmp(RegType::vgpr, dst.size());
5554 flat->definitions[0] = Definition(vec);
5555 ctx->block->instructions.emplace_back(std::move(flat));
5556 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
5557 } else {
5558 flat->definitions[0] = Definition(dst);
5559 ctx->block->instructions.emplace_back(std::move(flat));
5560 }
5561 emit_split_vector(ctx, dst, num_components);
5562 } else {
5563 assert(ctx->options->chip_class == GFX6);
5564
5565 /* GFX6 doesn't support loading vec3, expand to vec4. */
5566 num_bytes = num_bytes == 12 ? 16 : num_bytes;
5567
5568 aco_opcode op;
5569 switch (num_bytes) {
5570 case 4:
5571 op = aco_opcode::buffer_load_dword;
5572 break;
5573 case 8:
5574 op = aco_opcode::buffer_load_dwordx2;
5575 break;
5576 case 16:
5577 op = aco_opcode::buffer_load_dwordx4;
5578 break;
5579 default:
5580 unreachable("load_global not implemented for this size.");
5581 }
5582
5583 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
5584
5585 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
5586 mubuf->operands[0] = Operand(rsrc);
5587 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
5588 mubuf->operands[2] = Operand(0u);
5589 mubuf->glc = glc;
5590 mubuf->dlc = false;
5591 mubuf->offset = 0;
5592 mubuf->addr64 = addr.type() == RegType::vgpr;
5593 mubuf->disable_wqm = false;
5594 mubuf->barrier = barrier_buffer;
5595 aco_ptr<Instruction> instr = std::move(mubuf);
5596
5597 /* expand vector */
5598 if (dst.size() == 3) {
5599 Temp vec = bld.tmp(v4);
5600 instr->definitions[0] = Definition(vec);
5601 bld.insert(std::move(instr));
5602 emit_split_vector(ctx, vec, 4);
5603
5604 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
5605 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
5606 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
5607 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
5608 }
5609
5610 if (dst.type() == RegType::sgpr) {
5611 Temp vec = bld.tmp(RegType::vgpr, dst.size());
5612 instr->definitions[0] = Definition(vec);
5613 bld.insert(std::move(instr));
5614 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
5615 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
5616 } else {
5617 instr->definitions[0] = Definition(dst);
5618 bld.insert(std::move(instr));
5619 emit_split_vector(ctx, dst, num_components);
5620 }
5621 }
5622 } else {
5623 switch (num_bytes) {
5624 case 4:
5625 op = aco_opcode::s_load_dword;
5626 break;
5627 case 8:
5628 op = aco_opcode::s_load_dwordx2;
5629 break;
5630 case 12:
5631 case 16:
5632 op = aco_opcode::s_load_dwordx4;
5633 break;
5634 default:
5635 unreachable("load_global not implemented for this size.");
5636 }
5637 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
5638 load->operands[0] = Operand(addr);
5639 load->operands[1] = Operand(0u);
5640 load->definitions[0] = Definition(dst);
5641 load->glc = glc;
5642 load->dlc = dlc;
5643 load->barrier = barrier_buffer;
5644 assert(ctx->options->chip_class >= GFX8 || !glc);
5645
5646 if (dst.size() == 3) {
5647 /* trim vector */
5648 Temp vec = bld.tmp(s4);
5649 load->definitions[0] = Definition(vec);
5650 ctx->block->instructions.emplace_back(std::move(load));
5651 emit_split_vector(ctx, vec, 4);
5652
5653 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5654 emit_extract_vector(ctx, vec, 0, s1),
5655 emit_extract_vector(ctx, vec, 1, s1),
5656 emit_extract_vector(ctx, vec, 2, s1));
5657 } else {
5658 ctx->block->instructions.emplace_back(std::move(load));
5659 }
5660 }
5661 }
5662
5663 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
5664 {
5665 Builder bld(ctx->program, ctx->block);
5666 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5667
5668 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5669 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
5670
5671 if (ctx->options->chip_class >= GFX7)
5672 addr = as_vgpr(ctx, addr);
5673
5674 unsigned writemask = nir_intrinsic_write_mask(instr);
5675 while (writemask) {
5676 int start, count;
5677 u_bit_scan_consecutive_range(&writemask, &start, &count);
5678 if (count == 3 && ctx->options->chip_class == GFX6) {
5679 /* GFX6 doesn't support storing vec3, split it. */
5680 writemask |= 1u << (start + 2);
5681 count = 2;
5682 }
5683 unsigned num_bytes = count * elem_size_bytes;
5684
5685 Temp write_data = data;
5686 if (count != instr->num_components) {
5687 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5688 for (int i = 0; i < count; i++)
5689 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
5690 write_data = bld.tmp(RegType::vgpr, count);
5691 vec->definitions[0] = Definition(write_data);
5692 ctx->block->instructions.emplace_back(std::move(vec));
5693 }
5694
5695 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5696 unsigned offset = start * elem_size_bytes;
5697
5698 if (ctx->options->chip_class >= GFX7) {
5699 if (offset > 0 && ctx->options->chip_class < GFX9) {
5700 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
5701 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
5702 Temp carry = bld.tmp(bld.lm);
5703 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
5704
5705 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
5706 Operand(offset), addr0);
5707 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
5708 Operand(0u), addr1,
5709 carry).def(1).setHint(vcc);
5710
5711 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
5712
5713 offset = 0;
5714 }
5715
5716 bool global = ctx->options->chip_class >= GFX9;
5717 aco_opcode op;
5718 switch (num_bytes) {
5719 case 4:
5720 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
5721 break;
5722 case 8:
5723 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
5724 break;
5725 case 12:
5726 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
5727 break;
5728 case 16:
5729 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
5730 break;
5731 default:
5732 unreachable("store_global not implemented for this size.");
5733 }
5734
5735 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
5736 flat->operands[0] = Operand(addr);
5737 flat->operands[1] = Operand(s1);
5738 flat->operands[2] = Operand(data);
5739 flat->glc = glc;
5740 flat->dlc = false;
5741 flat->offset = offset;
5742 flat->disable_wqm = true;
5743 flat->barrier = barrier_buffer;
5744 ctx->program->needs_exact = true;
5745 ctx->block->instructions.emplace_back(std::move(flat));
5746 } else {
5747 assert(ctx->options->chip_class == GFX6);
5748
5749 aco_opcode op;
5750 switch (num_bytes) {
5751 case 4:
5752 op = aco_opcode::buffer_store_dword;
5753 break;
5754 case 8:
5755 op = aco_opcode::buffer_store_dwordx2;
5756 break;
5757 case 16:
5758 op = aco_opcode::buffer_store_dwordx4;
5759 break;
5760 default:
5761 unreachable("store_global not implemented for this size.");
5762 }
5763
5764 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
5765
5766 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
5767 mubuf->operands[0] = Operand(rsrc);
5768 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
5769 mubuf->operands[2] = Operand(0u);
5770 mubuf->operands[3] = Operand(write_data);
5771 mubuf->glc = glc;
5772 mubuf->dlc = false;
5773 mubuf->offset = offset;
5774 mubuf->addr64 = addr.type() == RegType::vgpr;
5775 mubuf->disable_wqm = true;
5776 mubuf->barrier = barrier_buffer;
5777 ctx->program->needs_exact = true;
5778 ctx->block->instructions.emplace_back(std::move(mubuf));
5779 }
5780 }
5781 }
5782
5783 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5784 {
5785 /* return the previous value if dest is ever used */
5786 bool return_previous = false;
5787 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5788 return_previous = true;
5789 break;
5790 }
5791 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5792 return_previous = true;
5793 break;
5794 }
5795
5796 Builder bld(ctx->program, ctx->block);
5797 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
5798 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5799
5800 if (ctx->options->chip_class >= GFX7)
5801 addr = as_vgpr(ctx, addr);
5802
5803 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
5804 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
5805 get_ssa_temp(ctx, instr->src[2].ssa), data);
5806
5807 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5808
5809 aco_opcode op32, op64;
5810
5811 if (ctx->options->chip_class >= GFX7) {
5812 bool global = ctx->options->chip_class >= GFX9;
5813 switch (instr->intrinsic) {
5814 case nir_intrinsic_global_atomic_add:
5815 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
5816 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
5817 break;
5818 case nir_intrinsic_global_atomic_imin:
5819 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
5820 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
5821 break;
5822 case nir_intrinsic_global_atomic_umin:
5823 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
5824 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
5825 break;
5826 case nir_intrinsic_global_atomic_imax:
5827 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
5828 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
5829 break;
5830 case nir_intrinsic_global_atomic_umax:
5831 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
5832 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
5833 break;
5834 case nir_intrinsic_global_atomic_and:
5835 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
5836 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
5837 break;
5838 case nir_intrinsic_global_atomic_or:
5839 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
5840 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
5841 break;
5842 case nir_intrinsic_global_atomic_xor:
5843 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
5844 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
5845 break;
5846 case nir_intrinsic_global_atomic_exchange:
5847 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
5848 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
5849 break;
5850 case nir_intrinsic_global_atomic_comp_swap:
5851 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
5852 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
5853 break;
5854 default:
5855 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5856 }
5857
5858 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5859 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
5860 flat->operands[0] = Operand(addr);
5861 flat->operands[1] = Operand(s1);
5862 flat->operands[2] = Operand(data);
5863 if (return_previous)
5864 flat->definitions[0] = Definition(dst);
5865 flat->glc = return_previous;
5866 flat->dlc = false; /* Not needed for atomics */
5867 flat->offset = 0;
5868 flat->disable_wqm = true;
5869 flat->barrier = barrier_buffer;
5870 ctx->program->needs_exact = true;
5871 ctx->block->instructions.emplace_back(std::move(flat));
5872 } else {
5873 assert(ctx->options->chip_class == GFX6);
5874
5875 switch (instr->intrinsic) {
5876 case nir_intrinsic_global_atomic_add:
5877 op32 = aco_opcode::buffer_atomic_add;
5878 op64 = aco_opcode::buffer_atomic_add_x2;
5879 break;
5880 case nir_intrinsic_global_atomic_imin:
5881 op32 = aco_opcode::buffer_atomic_smin;
5882 op64 = aco_opcode::buffer_atomic_smin_x2;
5883 break;
5884 case nir_intrinsic_global_atomic_umin:
5885 op32 = aco_opcode::buffer_atomic_umin;
5886 op64 = aco_opcode::buffer_atomic_umin_x2;
5887 break;
5888 case nir_intrinsic_global_atomic_imax:
5889 op32 = aco_opcode::buffer_atomic_smax;
5890 op64 = aco_opcode::buffer_atomic_smax_x2;
5891 break;
5892 case nir_intrinsic_global_atomic_umax:
5893 op32 = aco_opcode::buffer_atomic_umax;
5894 op64 = aco_opcode::buffer_atomic_umax_x2;
5895 break;
5896 case nir_intrinsic_global_atomic_and:
5897 op32 = aco_opcode::buffer_atomic_and;
5898 op64 = aco_opcode::buffer_atomic_and_x2;
5899 break;
5900 case nir_intrinsic_global_atomic_or:
5901 op32 = aco_opcode::buffer_atomic_or;
5902 op64 = aco_opcode::buffer_atomic_or_x2;
5903 break;
5904 case nir_intrinsic_global_atomic_xor:
5905 op32 = aco_opcode::buffer_atomic_xor;
5906 op64 = aco_opcode::buffer_atomic_xor_x2;
5907 break;
5908 case nir_intrinsic_global_atomic_exchange:
5909 op32 = aco_opcode::buffer_atomic_swap;
5910 op64 = aco_opcode::buffer_atomic_swap_x2;
5911 break;
5912 case nir_intrinsic_global_atomic_comp_swap:
5913 op32 = aco_opcode::buffer_atomic_cmpswap;
5914 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
5915 break;
5916 default:
5917 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5918 }
5919
5920 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
5921
5922 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5923
5924 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5925 mubuf->operands[0] = Operand(rsrc);
5926 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
5927 mubuf->operands[2] = Operand(0u);
5928 mubuf->operands[3] = Operand(data);
5929 if (return_previous)
5930 mubuf->definitions[0] = Definition(dst);
5931 mubuf->glc = return_previous;
5932 mubuf->dlc = false;
5933 mubuf->offset = 0;
5934 mubuf->addr64 = addr.type() == RegType::vgpr;
5935 mubuf->disable_wqm = true;
5936 mubuf->barrier = barrier_buffer;
5937 ctx->program->needs_exact = true;
5938 ctx->block->instructions.emplace_back(std::move(mubuf));
5939 }
5940 }
5941
5942 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
5943 Builder bld(ctx->program, ctx->block);
5944 switch(instr->intrinsic) {
5945 case nir_intrinsic_group_memory_barrier:
5946 case nir_intrinsic_memory_barrier:
5947 bld.barrier(aco_opcode::p_memory_barrier_common);
5948 break;
5949 case nir_intrinsic_memory_barrier_buffer:
5950 bld.barrier(aco_opcode::p_memory_barrier_buffer);
5951 break;
5952 case nir_intrinsic_memory_barrier_image:
5953 bld.barrier(aco_opcode::p_memory_barrier_image);
5954 break;
5955 case nir_intrinsic_memory_barrier_tcs_patch:
5956 case nir_intrinsic_memory_barrier_shared:
5957 bld.barrier(aco_opcode::p_memory_barrier_shared);
5958 break;
5959 default:
5960 unreachable("Unimplemented memory barrier intrinsic");
5961 break;
5962 }
5963 }
5964
5965 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
5966 {
5967 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
5968 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5969 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
5970 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5971 Builder bld(ctx->program, ctx->block);
5972
5973 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5974 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
5975 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
5976 }
5977
5978 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
5979 {
5980 unsigned writemask = nir_intrinsic_write_mask(instr);
5981 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
5982 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5983 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5984 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
5985
5986 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
5987 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
5988 }
5989
5990 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5991 {
5992 unsigned offset = nir_intrinsic_base(instr);
5993 Operand m = load_lds_size_m0(ctx);
5994 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5995 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5996
5997 unsigned num_operands = 3;
5998 aco_opcode op32, op64, op32_rtn, op64_rtn;
5999 switch(instr->intrinsic) {
6000 case nir_intrinsic_shared_atomic_add:
6001 op32 = aco_opcode::ds_add_u32;
6002 op64 = aco_opcode::ds_add_u64;
6003 op32_rtn = aco_opcode::ds_add_rtn_u32;
6004 op64_rtn = aco_opcode::ds_add_rtn_u64;
6005 break;
6006 case nir_intrinsic_shared_atomic_imin:
6007 op32 = aco_opcode::ds_min_i32;
6008 op64 = aco_opcode::ds_min_i64;
6009 op32_rtn = aco_opcode::ds_min_rtn_i32;
6010 op64_rtn = aco_opcode::ds_min_rtn_i64;
6011 break;
6012 case nir_intrinsic_shared_atomic_umin:
6013 op32 = aco_opcode::ds_min_u32;
6014 op64 = aco_opcode::ds_min_u64;
6015 op32_rtn = aco_opcode::ds_min_rtn_u32;
6016 op64_rtn = aco_opcode::ds_min_rtn_u64;
6017 break;
6018 case nir_intrinsic_shared_atomic_imax:
6019 op32 = aco_opcode::ds_max_i32;
6020 op64 = aco_opcode::ds_max_i64;
6021 op32_rtn = aco_opcode::ds_max_rtn_i32;
6022 op64_rtn = aco_opcode::ds_max_rtn_i64;
6023 break;
6024 case nir_intrinsic_shared_atomic_umax:
6025 op32 = aco_opcode::ds_max_u32;
6026 op64 = aco_opcode::ds_max_u64;
6027 op32_rtn = aco_opcode::ds_max_rtn_u32;
6028 op64_rtn = aco_opcode::ds_max_rtn_u64;
6029 break;
6030 case nir_intrinsic_shared_atomic_and:
6031 op32 = aco_opcode::ds_and_b32;
6032 op64 = aco_opcode::ds_and_b64;
6033 op32_rtn = aco_opcode::ds_and_rtn_b32;
6034 op64_rtn = aco_opcode::ds_and_rtn_b64;
6035 break;
6036 case nir_intrinsic_shared_atomic_or:
6037 op32 = aco_opcode::ds_or_b32;
6038 op64 = aco_opcode::ds_or_b64;
6039 op32_rtn = aco_opcode::ds_or_rtn_b32;
6040 op64_rtn = aco_opcode::ds_or_rtn_b64;
6041 break;
6042 case nir_intrinsic_shared_atomic_xor:
6043 op32 = aco_opcode::ds_xor_b32;
6044 op64 = aco_opcode::ds_xor_b64;
6045 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6046 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6047 break;
6048 case nir_intrinsic_shared_atomic_exchange:
6049 op32 = aco_opcode::ds_write_b32;
6050 op64 = aco_opcode::ds_write_b64;
6051 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6052 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6053 break;
6054 case nir_intrinsic_shared_atomic_comp_swap:
6055 op32 = aco_opcode::ds_cmpst_b32;
6056 op64 = aco_opcode::ds_cmpst_b64;
6057 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6058 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6059 num_operands = 4;
6060 break;
6061 default:
6062 unreachable("Unhandled shared atomic intrinsic");
6063 }
6064
6065 /* return the previous value if dest is ever used */
6066 bool return_previous = false;
6067 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6068 return_previous = true;
6069 break;
6070 }
6071 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6072 return_previous = true;
6073 break;
6074 }
6075
6076 aco_opcode op;
6077 if (data.size() == 1) {
6078 assert(instr->dest.ssa.bit_size == 32);
6079 op = return_previous ? op32_rtn : op32;
6080 } else {
6081 assert(instr->dest.ssa.bit_size == 64);
6082 op = return_previous ? op64_rtn : op64;
6083 }
6084
6085 if (offset > 65535) {
6086 Builder bld(ctx->program, ctx->block);
6087 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6088 offset = 0;
6089 }
6090
6091 aco_ptr<DS_instruction> ds;
6092 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6093 ds->operands[0] = Operand(address);
6094 ds->operands[1] = Operand(data);
6095 if (num_operands == 4)
6096 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6097 ds->operands[num_operands - 1] = m;
6098 ds->offset0 = offset;
6099 if (return_previous)
6100 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6101 ctx->block->instructions.emplace_back(std::move(ds));
6102 }
6103
6104 Temp get_scratch_resource(isel_context *ctx)
6105 {
6106 Builder bld(ctx->program, ctx->block);
6107 Temp scratch_addr = ctx->program->private_segment_buffer;
6108 if (ctx->stage != compute_cs)
6109 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6110
6111 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6112 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6113
6114 if (ctx->program->chip_class >= GFX10) {
6115 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6116 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6117 S_008F0C_RESOURCE_LEVEL(1);
6118 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6119 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6120 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6121 }
6122
6123 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6124 if (ctx->program->chip_class <= GFX8)
6125 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6126
6127 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6128 }
6129
6130 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6131 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
6132 Builder bld(ctx->program, ctx->block);
6133 Temp rsrc = get_scratch_resource(ctx);
6134 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6135 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6136
6137 aco_opcode op;
6138 switch (dst.size()) {
6139 case 1:
6140 op = aco_opcode::buffer_load_dword;
6141 break;
6142 case 2:
6143 op = aco_opcode::buffer_load_dwordx2;
6144 break;
6145 case 3:
6146 op = aco_opcode::buffer_load_dwordx3;
6147 break;
6148 case 4:
6149 op = aco_opcode::buffer_load_dwordx4;
6150 break;
6151 case 6:
6152 case 8: {
6153 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
6154 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
6155 bld.def(v4), rsrc, offset,
6156 ctx->program->scratch_offset, 0, true);
6157 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
6158 aco_opcode::buffer_load_dwordx4,
6159 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
6160 rsrc, offset, ctx->program->scratch_offset, 16, true);
6161 emit_split_vector(ctx, lower, 2);
6162 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
6163 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
6164 if (dst.size() == 8) {
6165 emit_split_vector(ctx, upper, 2);
6166 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
6167 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
6168 } else {
6169 elems[2] = upper;
6170 }
6171
6172 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
6173 Format::PSEUDO, dst.size() / 2, 1)};
6174 for (unsigned i = 0; i < dst.size() / 2; i++)
6175 vec->operands[i] = Operand(elems[i]);
6176 vec->definitions[0] = Definition(dst);
6177 bld.insert(std::move(vec));
6178 ctx->allocated_vec.emplace(dst.id(), elems);
6179 return;
6180 }
6181 default:
6182 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
6183 }
6184
6185 bld.mubuf(op, Definition(dst), rsrc, offset, ctx->program->scratch_offset, 0, true);
6186 emit_split_vector(ctx, dst, instr->num_components);
6187 }
6188
6189 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6190 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
6191 Builder bld(ctx->program, ctx->block);
6192 Temp rsrc = get_scratch_resource(ctx);
6193 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6194 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6195
6196 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6197 unsigned writemask = nir_intrinsic_write_mask(instr);
6198
6199 while (writemask) {
6200 int start, count;
6201 u_bit_scan_consecutive_range(&writemask, &start, &count);
6202 int num_bytes = count * elem_size_bytes;
6203
6204 if (num_bytes > 16) {
6205 assert(elem_size_bytes == 8);
6206 writemask |= (((count - 2) << 1) - 1) << (start + 2);
6207 count = 2;
6208 num_bytes = 16;
6209 }
6210
6211 // TODO: check alignment of sub-dword stores
6212 // TODO: split 3 bytes. there is no store instruction for that
6213
6214 Temp write_data;
6215 if (count != instr->num_components) {
6216 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6217 for (int i = 0; i < count; i++) {
6218 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
6219 vec->operands[i] = Operand(elem);
6220 }
6221 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
6222 vec->definitions[0] = Definition(write_data);
6223 ctx->block->instructions.emplace_back(std::move(vec));
6224 } else {
6225 write_data = data;
6226 }
6227
6228 aco_opcode op;
6229 switch (num_bytes) {
6230 case 4:
6231 op = aco_opcode::buffer_store_dword;
6232 break;
6233 case 8:
6234 op = aco_opcode::buffer_store_dwordx2;
6235 break;
6236 case 12:
6237 op = aco_opcode::buffer_store_dwordx3;
6238 break;
6239 case 16:
6240 op = aco_opcode::buffer_store_dwordx4;
6241 break;
6242 default:
6243 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
6244 }
6245
6246 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
6247 }
6248 }
6249
6250 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6251 uint8_t log2_ps_iter_samples;
6252 if (ctx->program->info->ps.force_persample) {
6253 log2_ps_iter_samples =
6254 util_logbase2(ctx->options->key.fs.num_samples);
6255 } else {
6256 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6257 }
6258
6259 /* The bit pattern matches that used by fixed function fragment
6260 * processing. */
6261 static const unsigned ps_iter_masks[] = {
6262 0xffff, /* not used */
6263 0x5555,
6264 0x1111,
6265 0x0101,
6266 0x0001,
6267 };
6268 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6269
6270 Builder bld(ctx->program, ctx->block);
6271
6272 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6273 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6274 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6275 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6276 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6277 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6278 }
6279
6280 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6281 Builder bld(ctx->program, ctx->block);
6282
6283 unsigned stream = nir_intrinsic_stream_id(instr);
6284 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6285 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6286 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6287
6288 /* get GSVS ring */
6289 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6290
6291 unsigned num_components =
6292 ctx->program->info->gs.num_stream_output_components[stream];
6293 assert(num_components);
6294
6295 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6296 unsigned stream_offset = 0;
6297 for (unsigned i = 0; i < stream; i++) {
6298 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6299 stream_offset += prev_stride * ctx->program->wave_size;
6300 }
6301
6302 /* Limit on the stride field for <= GFX7. */
6303 assert(stride < (1 << 14));
6304
6305 Temp gsvs_dwords[4];
6306 for (unsigned i = 0; i < 4; i++)
6307 gsvs_dwords[i] = bld.tmp(s1);
6308 bld.pseudo(aco_opcode::p_split_vector,
6309 Definition(gsvs_dwords[0]),
6310 Definition(gsvs_dwords[1]),
6311 Definition(gsvs_dwords[2]),
6312 Definition(gsvs_dwords[3]),
6313 gsvs_ring);
6314
6315 if (stream_offset) {
6316 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6317
6318 Temp carry = bld.tmp(s1);
6319 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6320 gsvs_dwords[1] = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), gsvs_dwords[1], Operand(0u), bld.scc(carry));
6321 }
6322
6323 gsvs_dwords[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), gsvs_dwords[1], Operand(S_008F04_STRIDE(stride)));
6324 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6325
6326 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6327 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6328
6329 unsigned offset = 0;
6330 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6331 if (ctx->program->info->gs.output_streams[i] != stream)
6332 continue;
6333
6334 for (unsigned j = 0; j < 4; j++) {
6335 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6336 continue;
6337
6338 if (ctx->outputs.mask[i] & (1 << j)) {
6339 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6340 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6341 if (const_offset >= 4096u) {
6342 if (vaddr_offset.isUndefined())
6343 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6344 else
6345 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6346 const_offset %= 4096u;
6347 }
6348
6349 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6350 mtbuf->operands[0] = Operand(gsvs_ring);
6351 mtbuf->operands[1] = vaddr_offset;
6352 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6353 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6354 mtbuf->offen = !vaddr_offset.isUndefined();
6355 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6356 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6357 mtbuf->offset = const_offset;
6358 mtbuf->glc = true;
6359 mtbuf->slc = true;
6360 mtbuf->barrier = barrier_gs_data;
6361 mtbuf->can_reorder = true;
6362 bld.insert(std::move(mtbuf));
6363 }
6364
6365 offset += ctx->shader->info.gs.vertices_out;
6366 }
6367
6368 /* outputs for the next vertex are undefined and keeping them around can
6369 * create invalid IR with control flow */
6370 ctx->outputs.mask[i] = 0;
6371 }
6372
6373 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6374 }
6375
6376 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6377 {
6378 Builder bld(ctx->program, ctx->block);
6379
6380 if (cluster_size == 1) {
6381 return src;
6382 } if (op == nir_op_iand && cluster_size == 4) {
6383 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6384 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6385 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6386 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6387 } else if (op == nir_op_ior && cluster_size == 4) {
6388 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6389 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6390 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6391 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6392 //subgroupAnd(val) -> (exec & ~val) == 0
6393 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6394 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6395 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6396 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6397 //subgroupOr(val) -> (val & exec) != 0
6398 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6399 return bool_to_vector_condition(ctx, tmp);
6400 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6401 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6402 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6403 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6404 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6405 return bool_to_vector_condition(ctx, tmp);
6406 } else {
6407 //subgroupClustered{And,Or,Xor}(val, n) ->
6408 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6409 //cluster_offset = ~(n - 1) & lane_id
6410 //cluster_mask = ((1 << n) - 1)
6411 //subgroupClusteredAnd():
6412 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6413 //subgroupClusteredOr():
6414 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6415 //subgroupClusteredXor():
6416 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6417 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6418 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6419
6420 Temp tmp;
6421 if (op == nir_op_iand)
6422 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6423 else
6424 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6425
6426 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6427
6428 if (ctx->program->chip_class <= GFX7)
6429 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6430 else if (ctx->program->wave_size == 64)
6431 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6432 else
6433 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6434 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6435 if (cluster_mask != 0xffffffff)
6436 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6437
6438 Definition cmp_def = Definition();
6439 if (op == nir_op_iand) {
6440 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6441 } else if (op == nir_op_ior) {
6442 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6443 } else if (op == nir_op_ixor) {
6444 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
6445 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
6446 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6447 }
6448 cmp_def.setHint(vcc);
6449 return cmp_def.getTemp();
6450 }
6451 }
6452
6453 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
6454 {
6455 Builder bld(ctx->program, ctx->block);
6456
6457 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
6458 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
6459 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
6460 Temp tmp;
6461 if (op == nir_op_iand)
6462 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6463 else
6464 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
6465
6466 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
6467 Temp lo = lohi.def(0).getTemp();
6468 Temp hi = lohi.def(1).getTemp();
6469 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
6470
6471 Definition cmp_def = Definition();
6472 if (op == nir_op_iand)
6473 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6474 else if (op == nir_op_ior)
6475 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6476 else if (op == nir_op_ixor)
6477 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
6478 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
6479 cmp_def.setHint(vcc);
6480 return cmp_def.getTemp();
6481 }
6482
6483 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
6484 {
6485 Builder bld(ctx->program, ctx->block);
6486
6487 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
6488 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
6489 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
6490 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
6491 if (op == nir_op_iand)
6492 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6493 else if (op == nir_op_ior)
6494 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6495 else if (op == nir_op_ixor)
6496 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6497
6498 assert(false);
6499 return Temp();
6500 }
6501
6502 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
6503 {
6504 Builder bld(ctx->program, ctx->block);
6505 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
6506 if (src.regClass().type() == RegType::vgpr) {
6507 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
6508 } else if (src.regClass() == s1) {
6509 bld.sop1(aco_opcode::s_mov_b32, dst, src);
6510 } else if (src.regClass() == s2) {
6511 bld.sop1(aco_opcode::s_mov_b64, dst, src);
6512 } else {
6513 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6514 nir_print_instr(&instr->instr, stderr);
6515 fprintf(stderr, "\n");
6516 }
6517 }
6518
6519 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
6520 {
6521 Builder bld(ctx->program, ctx->block);
6522 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
6523 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
6524 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
6525
6526 Temp ddx_1, ddx_2, ddy_1, ddy_2;
6527 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
6528 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
6529 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
6530
6531 /* Build DD X/Y */
6532 if (ctx->program->chip_class >= GFX8) {
6533 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
6534 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
6535 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
6536 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
6537 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
6538 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
6539 } else {
6540 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
6541 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
6542 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
6543 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
6544 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
6545 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
6546 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
6547 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
6548 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
6549 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
6550 }
6551
6552 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
6553 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
6554 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
6555 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
6556 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
6557 Temp wqm1 = bld.tmp(v1);
6558 emit_wqm(ctx, tmp1, wqm1, true);
6559 Temp wqm2 = bld.tmp(v1);
6560 emit_wqm(ctx, tmp2, wqm2, true);
6561 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
6562 return;
6563 }
6564
6565 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
6566 {
6567 Builder bld(ctx->program, ctx->block);
6568 switch(instr->intrinsic) {
6569 case nir_intrinsic_load_barycentric_sample:
6570 case nir_intrinsic_load_barycentric_pixel:
6571 case nir_intrinsic_load_barycentric_centroid: {
6572 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
6573 Temp bary = Temp(0, s2);
6574 switch (mode) {
6575 case INTERP_MODE_SMOOTH:
6576 case INTERP_MODE_NONE:
6577 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
6578 bary = get_arg(ctx, ctx->args->ac.persp_center);
6579 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
6580 bary = ctx->persp_centroid;
6581 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
6582 bary = get_arg(ctx, ctx->args->ac.persp_sample);
6583 break;
6584 case INTERP_MODE_NOPERSPECTIVE:
6585 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
6586 bary = get_arg(ctx, ctx->args->ac.linear_center);
6587 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
6588 bary = ctx->linear_centroid;
6589 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
6590 bary = get_arg(ctx, ctx->args->ac.linear_sample);
6591 break;
6592 default:
6593 break;
6594 }
6595 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6596 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
6597 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
6598 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6599 Operand(p1), Operand(p2));
6600 emit_split_vector(ctx, dst, 2);
6601 break;
6602 }
6603 case nir_intrinsic_load_barycentric_model: {
6604 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
6605
6606 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6607 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
6608 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
6609 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
6610 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6611 Operand(p1), Operand(p2), Operand(p3));
6612 emit_split_vector(ctx, dst, 3);
6613 break;
6614 }
6615 case nir_intrinsic_load_barycentric_at_sample: {
6616 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
6617 switch (ctx->options->key.fs.num_samples) {
6618 case 2: sample_pos_offset += 1 << 3; break;
6619 case 4: sample_pos_offset += 3 << 3; break;
6620 case 8: sample_pos_offset += 7 << 3; break;
6621 default: break;
6622 }
6623 Temp sample_pos;
6624 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6625 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
6626 Temp private_segment_buffer = ctx->program->private_segment_buffer;
6627 if (addr.type() == RegType::sgpr) {
6628 Operand offset;
6629 if (const_addr) {
6630 sample_pos_offset += const_addr->u32 << 3;
6631 offset = Operand(sample_pos_offset);
6632 } else if (ctx->options->chip_class >= GFX9) {
6633 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
6634 } else {
6635 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
6636 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
6637 }
6638
6639 Operand off = bld.copy(bld.def(s1), Operand(offset));
6640 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
6641
6642 } else if (ctx->options->chip_class >= GFX9) {
6643 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
6644 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
6645 } else if (ctx->options->chip_class >= GFX7) {
6646 /* addr += private_segment_buffer + sample_pos_offset */
6647 Temp tmp0 = bld.tmp(s1);
6648 Temp tmp1 = bld.tmp(s1);
6649 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
6650 Definition scc_tmp = bld.def(s1, scc);
6651 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
6652 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
6653 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
6654 Temp pck0 = bld.tmp(v1);
6655 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
6656 tmp1 = as_vgpr(ctx, tmp1);
6657 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);
6658 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
6659
6660 /* sample_pos = flat_load_dwordx2 addr */
6661 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
6662 } else {
6663 assert(ctx->options->chip_class == GFX6);
6664
6665 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6666 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6667 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
6668
6669 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
6670 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
6671
6672 sample_pos = bld.tmp(v2);
6673
6674 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
6675 load->definitions[0] = Definition(sample_pos);
6676 load->operands[0] = Operand(rsrc);
6677 load->operands[1] = Operand(addr);
6678 load->operands[2] = Operand(0u);
6679 load->offset = sample_pos_offset;
6680 load->offen = 0;
6681 load->addr64 = true;
6682 load->glc = false;
6683 load->dlc = false;
6684 load->disable_wqm = false;
6685 load->barrier = barrier_none;
6686 load->can_reorder = true;
6687 ctx->block->instructions.emplace_back(std::move(load));
6688 }
6689
6690 /* sample_pos -= 0.5 */
6691 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
6692 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
6693 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
6694 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
6695 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
6696
6697 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
6698 break;
6699 }
6700 case nir_intrinsic_load_barycentric_at_offset: {
6701 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
6702 RegClass rc = RegClass(offset.type(), 1);
6703 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
6704 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
6705 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
6706 break;
6707 }
6708 case nir_intrinsic_load_front_face: {
6709 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6710 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
6711 break;
6712 }
6713 case nir_intrinsic_load_view_index: {
6714 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
6715 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6716 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
6717 break;
6718 }
6719
6720 /* fallthrough */
6721 }
6722 case nir_intrinsic_load_layer_id: {
6723 unsigned idx = nir_intrinsic_base(instr);
6724 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6725 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
6726 break;
6727 }
6728 case nir_intrinsic_load_frag_coord: {
6729 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
6730 break;
6731 }
6732 case nir_intrinsic_load_sample_pos: {
6733 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
6734 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
6735 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6736 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
6737 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
6738 break;
6739 }
6740 case nir_intrinsic_load_tess_coord:
6741 visit_load_tess_coord(ctx, instr);
6742 break;
6743 case nir_intrinsic_load_interpolated_input:
6744 visit_load_interpolated_input(ctx, instr);
6745 break;
6746 case nir_intrinsic_store_output:
6747 visit_store_output(ctx, instr);
6748 break;
6749 case nir_intrinsic_load_input:
6750 case nir_intrinsic_load_input_vertex:
6751 visit_load_input(ctx, instr);
6752 break;
6753 case nir_intrinsic_load_output:
6754 visit_load_output(ctx, instr);
6755 break;
6756 case nir_intrinsic_load_per_vertex_input:
6757 visit_load_per_vertex_input(ctx, instr);
6758 break;
6759 case nir_intrinsic_load_per_vertex_output:
6760 visit_load_per_vertex_output(ctx, instr);
6761 break;
6762 case nir_intrinsic_store_per_vertex_output:
6763 visit_store_per_vertex_output(ctx, instr);
6764 break;
6765 case nir_intrinsic_load_ubo:
6766 visit_load_ubo(ctx, instr);
6767 break;
6768 case nir_intrinsic_load_push_constant:
6769 visit_load_push_constant(ctx, instr);
6770 break;
6771 case nir_intrinsic_load_constant:
6772 visit_load_constant(ctx, instr);
6773 break;
6774 case nir_intrinsic_vulkan_resource_index:
6775 visit_load_resource(ctx, instr);
6776 break;
6777 case nir_intrinsic_discard:
6778 visit_discard(ctx, instr);
6779 break;
6780 case nir_intrinsic_discard_if:
6781 visit_discard_if(ctx, instr);
6782 break;
6783 case nir_intrinsic_load_shared:
6784 visit_load_shared(ctx, instr);
6785 break;
6786 case nir_intrinsic_store_shared:
6787 visit_store_shared(ctx, instr);
6788 break;
6789 case nir_intrinsic_shared_atomic_add:
6790 case nir_intrinsic_shared_atomic_imin:
6791 case nir_intrinsic_shared_atomic_umin:
6792 case nir_intrinsic_shared_atomic_imax:
6793 case nir_intrinsic_shared_atomic_umax:
6794 case nir_intrinsic_shared_atomic_and:
6795 case nir_intrinsic_shared_atomic_or:
6796 case nir_intrinsic_shared_atomic_xor:
6797 case nir_intrinsic_shared_atomic_exchange:
6798 case nir_intrinsic_shared_atomic_comp_swap:
6799 visit_shared_atomic(ctx, instr);
6800 break;
6801 case nir_intrinsic_image_deref_load:
6802 visit_image_load(ctx, instr);
6803 break;
6804 case nir_intrinsic_image_deref_store:
6805 visit_image_store(ctx, instr);
6806 break;
6807 case nir_intrinsic_image_deref_atomic_add:
6808 case nir_intrinsic_image_deref_atomic_umin:
6809 case nir_intrinsic_image_deref_atomic_imin:
6810 case nir_intrinsic_image_deref_atomic_umax:
6811 case nir_intrinsic_image_deref_atomic_imax:
6812 case nir_intrinsic_image_deref_atomic_and:
6813 case nir_intrinsic_image_deref_atomic_or:
6814 case nir_intrinsic_image_deref_atomic_xor:
6815 case nir_intrinsic_image_deref_atomic_exchange:
6816 case nir_intrinsic_image_deref_atomic_comp_swap:
6817 visit_image_atomic(ctx, instr);
6818 break;
6819 case nir_intrinsic_image_deref_size:
6820 visit_image_size(ctx, instr);
6821 break;
6822 case nir_intrinsic_load_ssbo:
6823 visit_load_ssbo(ctx, instr);
6824 break;
6825 case nir_intrinsic_store_ssbo:
6826 visit_store_ssbo(ctx, instr);
6827 break;
6828 case nir_intrinsic_load_global:
6829 visit_load_global(ctx, instr);
6830 break;
6831 case nir_intrinsic_store_global:
6832 visit_store_global(ctx, instr);
6833 break;
6834 case nir_intrinsic_global_atomic_add:
6835 case nir_intrinsic_global_atomic_imin:
6836 case nir_intrinsic_global_atomic_umin:
6837 case nir_intrinsic_global_atomic_imax:
6838 case nir_intrinsic_global_atomic_umax:
6839 case nir_intrinsic_global_atomic_and:
6840 case nir_intrinsic_global_atomic_or:
6841 case nir_intrinsic_global_atomic_xor:
6842 case nir_intrinsic_global_atomic_exchange:
6843 case nir_intrinsic_global_atomic_comp_swap:
6844 visit_global_atomic(ctx, instr);
6845 break;
6846 case nir_intrinsic_ssbo_atomic_add:
6847 case nir_intrinsic_ssbo_atomic_imin:
6848 case nir_intrinsic_ssbo_atomic_umin:
6849 case nir_intrinsic_ssbo_atomic_imax:
6850 case nir_intrinsic_ssbo_atomic_umax:
6851 case nir_intrinsic_ssbo_atomic_and:
6852 case nir_intrinsic_ssbo_atomic_or:
6853 case nir_intrinsic_ssbo_atomic_xor:
6854 case nir_intrinsic_ssbo_atomic_exchange:
6855 case nir_intrinsic_ssbo_atomic_comp_swap:
6856 visit_atomic_ssbo(ctx, instr);
6857 break;
6858 case nir_intrinsic_load_scratch:
6859 visit_load_scratch(ctx, instr);
6860 break;
6861 case nir_intrinsic_store_scratch:
6862 visit_store_scratch(ctx, instr);
6863 break;
6864 case nir_intrinsic_get_buffer_size:
6865 visit_get_buffer_size(ctx, instr);
6866 break;
6867 case nir_intrinsic_control_barrier: {
6868 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
6869 /* GFX6 only (thanks to a hw bug workaround):
6870 * The real barrier instruction isn’t needed, because an entire patch
6871 * always fits into a single wave.
6872 */
6873 break;
6874 }
6875
6876 if (ctx->program->workgroup_size > ctx->program->wave_size)
6877 bld.sopp(aco_opcode::s_barrier);
6878
6879 break;
6880 }
6881 case nir_intrinsic_memory_barrier_tcs_patch:
6882 case nir_intrinsic_group_memory_barrier:
6883 case nir_intrinsic_memory_barrier:
6884 case nir_intrinsic_memory_barrier_buffer:
6885 case nir_intrinsic_memory_barrier_image:
6886 case nir_intrinsic_memory_barrier_shared:
6887 emit_memory_barrier(ctx, instr);
6888 break;
6889 case nir_intrinsic_load_num_work_groups: {
6890 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6891 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
6892 emit_split_vector(ctx, dst, 3);
6893 break;
6894 }
6895 case nir_intrinsic_load_local_invocation_id: {
6896 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6897 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
6898 emit_split_vector(ctx, dst, 3);
6899 break;
6900 }
6901 case nir_intrinsic_load_work_group_id: {
6902 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6903 struct ac_arg *args = ctx->args->ac.workgroup_ids;
6904 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6905 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
6906 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
6907 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
6908 emit_split_vector(ctx, dst, 3);
6909 break;
6910 }
6911 case nir_intrinsic_load_local_invocation_index: {
6912 Temp id = emit_mbcnt(ctx, bld.def(v1));
6913
6914 /* The tg_size bits [6:11] contain the subgroup id,
6915 * we need this multiplied by the wave size, and then OR the thread id to it.
6916 */
6917 if (ctx->program->wave_size == 64) {
6918 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
6919 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
6920 get_arg(ctx, ctx->args->ac.tg_size));
6921 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
6922 } else {
6923 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
6924 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
6925 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
6926 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
6927 }
6928 break;
6929 }
6930 case nir_intrinsic_load_subgroup_id: {
6931 if (ctx->stage == compute_cs) {
6932 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
6933 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
6934 } else {
6935 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
6936 }
6937 break;
6938 }
6939 case nir_intrinsic_load_subgroup_invocation: {
6940 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
6941 break;
6942 }
6943 case nir_intrinsic_load_num_subgroups: {
6944 if (ctx->stage == compute_cs)
6945 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
6946 get_arg(ctx, ctx->args->ac.tg_size));
6947 else
6948 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
6949 break;
6950 }
6951 case nir_intrinsic_ballot: {
6952 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6953 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6954 Definition tmp = bld.def(dst.regClass());
6955 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
6956 if (instr->src[0].ssa->bit_size == 1) {
6957 assert(src.regClass() == bld.lm);
6958 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
6959 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
6960 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
6961 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
6962 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
6963 } else {
6964 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6965 nir_print_instr(&instr->instr, stderr);
6966 fprintf(stderr, "\n");
6967 }
6968 if (dst.size() != bld.lm.size()) {
6969 /* Wave32 with ballot size set to 64 */
6970 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
6971 }
6972 emit_wqm(ctx, tmp.getTemp(), dst);
6973 break;
6974 }
6975 case nir_intrinsic_shuffle:
6976 case nir_intrinsic_read_invocation: {
6977 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6978 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
6979 emit_uniform_subgroup(ctx, instr, src);
6980 } else {
6981 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
6982 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
6983 tid = bld.as_uniform(tid);
6984 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6985 if (src.regClass() == v1) {
6986 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
6987 } else if (src.regClass() == v2) {
6988 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6989 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6990 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
6991 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
6992 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6993 emit_split_vector(ctx, dst, 2);
6994 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
6995 assert(src.regClass() == bld.lm);
6996 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
6997 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
6998 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
6999 assert(src.regClass() == bld.lm);
7000 Temp tmp;
7001 if (ctx->program->chip_class <= GFX7)
7002 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7003 else if (ctx->program->wave_size == 64)
7004 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7005 else
7006 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7007 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7008 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7009 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7010 } else {
7011 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7012 nir_print_instr(&instr->instr, stderr);
7013 fprintf(stderr, "\n");
7014 }
7015 }
7016 break;
7017 }
7018 case nir_intrinsic_load_sample_id: {
7019 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7020 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7021 break;
7022 }
7023 case nir_intrinsic_load_sample_mask_in: {
7024 visit_load_sample_mask_in(ctx, instr);
7025 break;
7026 }
7027 case nir_intrinsic_read_first_invocation: {
7028 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7029 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7030 if (src.regClass() == v1) {
7031 emit_wqm(ctx,
7032 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7033 dst);
7034 } else if (src.regClass() == v2) {
7035 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7036 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7037 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7038 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7039 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7040 emit_split_vector(ctx, dst, 2);
7041 } else if (instr->dest.ssa.bit_size == 1) {
7042 assert(src.regClass() == bld.lm);
7043 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7044 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7045 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7046 } else if (src.regClass() == s1) {
7047 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7048 } else if (src.regClass() == s2) {
7049 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7050 } else {
7051 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7052 nir_print_instr(&instr->instr, stderr);
7053 fprintf(stderr, "\n");
7054 }
7055 break;
7056 }
7057 case nir_intrinsic_vote_all: {
7058 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7059 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7060 assert(src.regClass() == bld.lm);
7061 assert(dst.regClass() == bld.lm);
7062
7063 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7064 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7065 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7066 break;
7067 }
7068 case nir_intrinsic_vote_any: {
7069 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7070 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7071 assert(src.regClass() == bld.lm);
7072 assert(dst.regClass() == bld.lm);
7073
7074 Temp tmp = bool_to_scalar_condition(ctx, src);
7075 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7076 break;
7077 }
7078 case nir_intrinsic_reduce:
7079 case nir_intrinsic_inclusive_scan:
7080 case nir_intrinsic_exclusive_scan: {
7081 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7082 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7083 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7084 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7085 nir_intrinsic_cluster_size(instr) : 0;
7086 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7087
7088 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7089 emit_uniform_subgroup(ctx, instr, src);
7090 } else if (instr->dest.ssa.bit_size == 1) {
7091 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7092 op = nir_op_iand;
7093 else if (op == nir_op_iadd)
7094 op = nir_op_ixor;
7095 else if (op == nir_op_umax || op == nir_op_imax)
7096 op = nir_op_ior;
7097 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7098
7099 switch (instr->intrinsic) {
7100 case nir_intrinsic_reduce:
7101 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7102 break;
7103 case nir_intrinsic_exclusive_scan:
7104 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7105 break;
7106 case nir_intrinsic_inclusive_scan:
7107 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7108 break;
7109 default:
7110 assert(false);
7111 }
7112 } else if (cluster_size == 1) {
7113 bld.copy(Definition(dst), src);
7114 } else {
7115 src = as_vgpr(ctx, src);
7116
7117 ReduceOp reduce_op;
7118 switch (op) {
7119 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7120 CASE(iadd)
7121 CASE(imul)
7122 CASE(fadd)
7123 CASE(fmul)
7124 CASE(imin)
7125 CASE(umin)
7126 CASE(fmin)
7127 CASE(imax)
7128 CASE(umax)
7129 CASE(fmax)
7130 CASE(iand)
7131 CASE(ior)
7132 CASE(ixor)
7133 default:
7134 unreachable("unknown reduction op");
7135 #undef CASE
7136 }
7137
7138 aco_opcode aco_op;
7139 switch (instr->intrinsic) {
7140 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7141 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7142 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7143 default:
7144 unreachable("unknown reduce intrinsic");
7145 }
7146
7147 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7148 reduce->operands[0] = Operand(src);
7149 // filled in by aco_reduce_assign.cpp, used internally as part of the
7150 // reduce sequence
7151 assert(dst.size() == 1 || dst.size() == 2);
7152 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7153 reduce->operands[2] = Operand(v1.as_linear());
7154
7155 Temp tmp_dst = bld.tmp(dst.regClass());
7156 reduce->definitions[0] = Definition(tmp_dst);
7157 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7158 reduce->definitions[2] = Definition();
7159 reduce->definitions[3] = Definition(scc, s1);
7160 reduce->definitions[4] = Definition();
7161 reduce->reduce_op = reduce_op;
7162 reduce->cluster_size = cluster_size;
7163 ctx->block->instructions.emplace_back(std::move(reduce));
7164
7165 emit_wqm(ctx, tmp_dst, dst);
7166 }
7167 break;
7168 }
7169 case nir_intrinsic_quad_broadcast: {
7170 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7171 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7172 emit_uniform_subgroup(ctx, instr, src);
7173 } else {
7174 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7175 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7176 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7177
7178 if (instr->dest.ssa.bit_size == 1) {
7179 assert(src.regClass() == bld.lm);
7180 assert(dst.regClass() == bld.lm);
7181 uint32_t half_mask = 0x11111111u << lane;
7182 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7183 Temp tmp = bld.tmp(bld.lm);
7184 bld.sop1(Builder::s_wqm, Definition(tmp),
7185 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7186 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7187 emit_wqm(ctx, tmp, dst);
7188 } else if (instr->dest.ssa.bit_size == 32) {
7189 if (ctx->program->chip_class >= GFX8)
7190 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7191 else
7192 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7193 } else if (instr->dest.ssa.bit_size == 64) {
7194 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7195 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7196 if (ctx->program->chip_class >= GFX8) {
7197 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7198 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7199 } else {
7200 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7201 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7202 }
7203 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7204 emit_split_vector(ctx, dst, 2);
7205 } else {
7206 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7207 nir_print_instr(&instr->instr, stderr);
7208 fprintf(stderr, "\n");
7209 }
7210 }
7211 break;
7212 }
7213 case nir_intrinsic_quad_swap_horizontal:
7214 case nir_intrinsic_quad_swap_vertical:
7215 case nir_intrinsic_quad_swap_diagonal:
7216 case nir_intrinsic_quad_swizzle_amd: {
7217 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7218 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7219 emit_uniform_subgroup(ctx, instr, src);
7220 break;
7221 }
7222 uint16_t dpp_ctrl = 0;
7223 switch (instr->intrinsic) {
7224 case nir_intrinsic_quad_swap_horizontal:
7225 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7226 break;
7227 case nir_intrinsic_quad_swap_vertical:
7228 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7229 break;
7230 case nir_intrinsic_quad_swap_diagonal:
7231 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7232 break;
7233 case nir_intrinsic_quad_swizzle_amd:
7234 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7235 break;
7236 default:
7237 break;
7238 }
7239 if (ctx->program->chip_class < GFX8)
7240 dpp_ctrl |= (1 << 15);
7241
7242 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7243 if (instr->dest.ssa.bit_size == 1) {
7244 assert(src.regClass() == bld.lm);
7245 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7246 if (ctx->program->chip_class >= GFX8)
7247 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7248 else
7249 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7250 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7251 emit_wqm(ctx, tmp, dst);
7252 } else if (instr->dest.ssa.bit_size == 32) {
7253 Temp tmp;
7254 if (ctx->program->chip_class >= GFX8)
7255 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7256 else
7257 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7258 emit_wqm(ctx, tmp, dst);
7259 } else if (instr->dest.ssa.bit_size == 64) {
7260 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7261 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7262 if (ctx->program->chip_class >= GFX8) {
7263 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7264 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7265 } else {
7266 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7267 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7268 }
7269 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7270 emit_split_vector(ctx, dst, 2);
7271 } else {
7272 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7273 nir_print_instr(&instr->instr, stderr);
7274 fprintf(stderr, "\n");
7275 }
7276 break;
7277 }
7278 case nir_intrinsic_masked_swizzle_amd: {
7279 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7280 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7281 emit_uniform_subgroup(ctx, instr, src);
7282 break;
7283 }
7284 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7285 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7286 if (dst.regClass() == v1) {
7287 emit_wqm(ctx,
7288 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7289 dst);
7290 } else if (dst.regClass() == v2) {
7291 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7292 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7293 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7294 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7295 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7296 emit_split_vector(ctx, dst, 2);
7297 } else {
7298 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7299 nir_print_instr(&instr->instr, stderr);
7300 fprintf(stderr, "\n");
7301 }
7302 break;
7303 }
7304 case nir_intrinsic_write_invocation_amd: {
7305 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7306 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7307 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7308 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7309 if (dst.regClass() == v1) {
7310 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7311 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7312 } else if (dst.regClass() == v2) {
7313 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7314 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7315 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7316 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7317 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7318 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7319 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7320 emit_split_vector(ctx, dst, 2);
7321 } else {
7322 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7323 nir_print_instr(&instr->instr, stderr);
7324 fprintf(stderr, "\n");
7325 }
7326 break;
7327 }
7328 case nir_intrinsic_mbcnt_amd: {
7329 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7330 RegClass rc = RegClass(src.type(), 1);
7331 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7332 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7333 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7334 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7335 emit_wqm(ctx, wqm_tmp, dst);
7336 break;
7337 }
7338 case nir_intrinsic_load_helper_invocation: {
7339 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7340 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7341 ctx->block->kind |= block_kind_needs_lowering;
7342 ctx->program->needs_exact = true;
7343 break;
7344 }
7345 case nir_intrinsic_is_helper_invocation: {
7346 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7347 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7348 ctx->block->kind |= block_kind_needs_lowering;
7349 ctx->program->needs_exact = true;
7350 break;
7351 }
7352 case nir_intrinsic_demote:
7353 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7354
7355 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7356 ctx->cf_info.exec_potentially_empty_discard = true;
7357 ctx->block->kind |= block_kind_uses_demote;
7358 ctx->program->needs_exact = true;
7359 break;
7360 case nir_intrinsic_demote_if: {
7361 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7362 assert(src.regClass() == bld.lm);
7363 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7364 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7365
7366 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7367 ctx->cf_info.exec_potentially_empty_discard = true;
7368 ctx->block->kind |= block_kind_uses_demote;
7369 ctx->program->needs_exact = true;
7370 break;
7371 }
7372 case nir_intrinsic_first_invocation: {
7373 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7374 get_ssa_temp(ctx, &instr->dest.ssa));
7375 break;
7376 }
7377 case nir_intrinsic_shader_clock:
7378 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7379 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7380 break;
7381 case nir_intrinsic_load_vertex_id_zero_base: {
7382 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7383 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7384 break;
7385 }
7386 case nir_intrinsic_load_first_vertex: {
7387 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7388 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7389 break;
7390 }
7391 case nir_intrinsic_load_base_instance: {
7392 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7393 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7394 break;
7395 }
7396 case nir_intrinsic_load_instance_id: {
7397 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7398 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7399 break;
7400 }
7401 case nir_intrinsic_load_draw_id: {
7402 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7403 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7404 break;
7405 }
7406 case nir_intrinsic_load_invocation_id: {
7407 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7408
7409 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7410 if (ctx->options->chip_class >= GFX10)
7411 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7412 else
7413 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7414 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7415 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7416 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7417 } else {
7418 unreachable("Unsupported stage for load_invocation_id");
7419 }
7420
7421 break;
7422 }
7423 case nir_intrinsic_load_primitive_id: {
7424 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7425
7426 switch (ctx->shader->info.stage) {
7427 case MESA_SHADER_GEOMETRY:
7428 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7429 break;
7430 case MESA_SHADER_TESS_CTRL:
7431 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7432 break;
7433 case MESA_SHADER_TESS_EVAL:
7434 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7435 break;
7436 default:
7437 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7438 }
7439
7440 break;
7441 }
7442 case nir_intrinsic_load_patch_vertices_in: {
7443 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
7444 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
7445
7446 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7447 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
7448 break;
7449 }
7450 case nir_intrinsic_emit_vertex_with_counter: {
7451 visit_emit_vertex_with_counter(ctx, instr);
7452 break;
7453 }
7454 case nir_intrinsic_end_primitive_with_counter: {
7455 unsigned stream = nir_intrinsic_stream_id(instr);
7456 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
7457 break;
7458 }
7459 case nir_intrinsic_set_vertex_count: {
7460 /* unused, the HW keeps track of this for us */
7461 break;
7462 }
7463 default:
7464 fprintf(stderr, "Unimplemented intrinsic instr: ");
7465 nir_print_instr(&instr->instr, stderr);
7466 fprintf(stderr, "\n");
7467 abort();
7468
7469 break;
7470 }
7471 }
7472
7473
7474 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
7475 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
7476 enum glsl_base_type *stype)
7477 {
7478 nir_deref_instr *texture_deref_instr = NULL;
7479 nir_deref_instr *sampler_deref_instr = NULL;
7480 int plane = -1;
7481
7482 for (unsigned i = 0; i < instr->num_srcs; i++) {
7483 switch (instr->src[i].src_type) {
7484 case nir_tex_src_texture_deref:
7485 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
7486 break;
7487 case nir_tex_src_sampler_deref:
7488 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
7489 break;
7490 case nir_tex_src_plane:
7491 plane = nir_src_as_int(instr->src[i].src);
7492 break;
7493 default:
7494 break;
7495 }
7496 }
7497
7498 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
7499
7500 if (!sampler_deref_instr)
7501 sampler_deref_instr = texture_deref_instr;
7502
7503 if (plane >= 0) {
7504 assert(instr->op != nir_texop_txf_ms &&
7505 instr->op != nir_texop_samples_identical);
7506 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
7507 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
7508 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
7509 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
7510 } else if (instr->op == nir_texop_fragment_mask_fetch) {
7511 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
7512 } else {
7513 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
7514 }
7515 if (samp_ptr) {
7516 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
7517
7518 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
7519 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
7520 Builder bld(ctx->program, ctx->block);
7521
7522 /* to avoid unnecessary moves, we split and recombine sampler and image */
7523 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
7524 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
7525 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
7526 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
7527 Definition(img[2]), Definition(img[3]), Definition(img[4]),
7528 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
7529 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
7530 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
7531
7532 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
7533 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
7534 img[0], img[1], img[2], img[3],
7535 img[4], img[5], img[6], img[7]);
7536 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
7537 samp[0], samp[1], samp[2], samp[3]);
7538 }
7539 }
7540 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
7541 instr->op == nir_texop_samples_identical))
7542 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
7543 }
7544
7545 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
7546 Temp *out_ma, Temp *out_sc, Temp *out_tc)
7547 {
7548 Builder bld(ctx->program, ctx->block);
7549
7550 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
7551 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
7552 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
7553
7554 Operand neg_one(0xbf800000u);
7555 Operand one(0x3f800000u);
7556 Operand two(0x40000000u);
7557 Operand four(0x40800000u);
7558
7559 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
7560 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
7561 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
7562
7563 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
7564 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
7565 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
7566 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);
7567
7568 // select sc
7569 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
7570 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
7571 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
7572 one, is_ma_y);
7573 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
7574
7575 // select tc
7576 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
7577 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
7578 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
7579
7580 // select ma
7581 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7582 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
7583 deriv_z, is_ma_z);
7584 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
7585 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
7586 }
7587
7588 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
7589 {
7590 Builder bld(ctx->program, ctx->block);
7591 Temp ma, tc, sc, id;
7592
7593 if (is_array) {
7594 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
7595
7596 // see comment in ac_prepare_cube_coords()
7597 if (ctx->options->chip_class <= GFX8)
7598 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
7599 }
7600
7601 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7602
7603 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
7604 vop3a->operands[0] = Operand(ma);
7605 vop3a->abs[0] = true;
7606 Temp invma = bld.tmp(v1);
7607 vop3a->definitions[0] = Definition(invma);
7608 ctx->block->instructions.emplace_back(std::move(vop3a));
7609
7610 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7611 if (!is_deriv)
7612 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
7613
7614 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7615 if (!is_deriv)
7616 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
7617
7618 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7619
7620 if (is_deriv) {
7621 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
7622 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
7623
7624 for (unsigned i = 0; i < 2; i++) {
7625 // see comment in ac_prepare_cube_coords()
7626 Temp deriv_ma;
7627 Temp deriv_sc, deriv_tc;
7628 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
7629 &deriv_ma, &deriv_sc, &deriv_tc);
7630
7631 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
7632
7633 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
7634 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
7635 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
7636 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
7637 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
7638 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
7639 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
7640 }
7641
7642 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
7643 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
7644 }
7645
7646 if (is_array)
7647 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
7648 coords.resize(3);
7649 coords[0] = sc;
7650 coords[1] = tc;
7651 coords[2] = id;
7652 }
7653
7654 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
7655 {
7656 if (vec->parent_instr->type != nir_instr_type_alu)
7657 return;
7658 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
7659 if (vec_instr->op != nir_op_vec(vec->num_components))
7660 return;
7661
7662 for (unsigned i = 0; i < vec->num_components; i++) {
7663 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
7664 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
7665 }
7666 }
7667
7668 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
7669 {
7670 Builder bld(ctx->program, ctx->block);
7671 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
7672 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
7673 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
7674 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
7675 std::vector<Temp> coords;
7676 std::vector<Temp> derivs;
7677 nir_const_value *sample_index_cv = NULL;
7678 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
7679 enum glsl_base_type stype;
7680 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
7681
7682 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
7683 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
7684 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
7685 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
7686
7687 for (unsigned i = 0; i < instr->num_srcs; i++) {
7688 switch (instr->src[i].src_type) {
7689 case nir_tex_src_coord: {
7690 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
7691 for (unsigned i = 0; i < coord.size(); i++)
7692 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
7693 break;
7694 }
7695 case nir_tex_src_bias:
7696 if (instr->op == nir_texop_txb) {
7697 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
7698 has_bias = true;
7699 }
7700 break;
7701 case nir_tex_src_lod: {
7702 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
7703
7704 if (val && val->f32 <= 0.0) {
7705 level_zero = true;
7706 } else {
7707 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
7708 has_lod = true;
7709 }
7710 break;
7711 }
7712 case nir_tex_src_comparator:
7713 if (instr->is_shadow) {
7714 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
7715 has_compare = true;
7716 }
7717 break;
7718 case nir_tex_src_offset:
7719 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
7720 get_const_vec(instr->src[i].src.ssa, const_offset);
7721 has_offset = true;
7722 break;
7723 case nir_tex_src_ddx:
7724 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
7725 has_ddx = true;
7726 break;
7727 case nir_tex_src_ddy:
7728 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
7729 has_ddy = true;
7730 break;
7731 case nir_tex_src_ms_index:
7732 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
7733 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
7734 has_sample_index = true;
7735 break;
7736 case nir_tex_src_texture_offset:
7737 case nir_tex_src_sampler_offset:
7738 default:
7739 break;
7740 }
7741 }
7742
7743 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
7744 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
7745
7746 if (instr->op == nir_texop_texture_samples) {
7747 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
7748
7749 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
7750 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
7751 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 */));
7752 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
7753
7754 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7755 samples, Operand(1u), bld.scc(is_msaa));
7756 return;
7757 }
7758
7759 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
7760 aco_ptr<Instruction> tmp_instr;
7761 Temp acc, pack = Temp();
7762
7763 uint32_t pack_const = 0;
7764 for (unsigned i = 0; i < offset.size(); i++) {
7765 if (!const_offset[i])
7766 continue;
7767 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
7768 }
7769
7770 if (offset.type() == RegType::sgpr) {
7771 for (unsigned i = 0; i < offset.size(); i++) {
7772 if (const_offset[i])
7773 continue;
7774
7775 acc = emit_extract_vector(ctx, offset, i, s1);
7776 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
7777
7778 if (i) {
7779 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
7780 }
7781
7782 if (pack == Temp()) {
7783 pack = acc;
7784 } else {
7785 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
7786 }
7787 }
7788
7789 if (pack_const && pack != Temp())
7790 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
7791 } else {
7792 for (unsigned i = 0; i < offset.size(); i++) {
7793 if (const_offset[i])
7794 continue;
7795
7796 acc = emit_extract_vector(ctx, offset, i, v1);
7797 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
7798
7799 if (i) {
7800 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
7801 }
7802
7803 if (pack == Temp()) {
7804 pack = acc;
7805 } else {
7806 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
7807 }
7808 }
7809
7810 if (pack_const && pack != Temp())
7811 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
7812 }
7813 if (pack_const && pack == Temp())
7814 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
7815 else if (pack == Temp())
7816 has_offset = false;
7817 else
7818 offset = pack;
7819 }
7820
7821 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
7822 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
7823
7824 /* pack derivatives */
7825 if (has_ddx || has_ddy) {
7826 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
7827 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
7828 Temp zero = bld.copy(bld.def(v1), Operand(0u));
7829 derivs = {ddy, zero, ddy, zero};
7830 } else {
7831 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
7832 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
7833 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
7834 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
7835 }
7836 has_derivs = true;
7837 }
7838
7839 if (instr->coord_components > 1 &&
7840 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
7841 instr->is_array &&
7842 instr->op != nir_texop_txf)
7843 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
7844
7845 if (instr->coord_components > 2 &&
7846 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
7847 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
7848 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
7849 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
7850 instr->is_array &&
7851 instr->op != nir_texop_txf &&
7852 instr->op != nir_texop_txf_ms &&
7853 instr->op != nir_texop_fragment_fetch &&
7854 instr->op != nir_texop_fragment_mask_fetch)
7855 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
7856
7857 if (ctx->options->chip_class == GFX9 &&
7858 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
7859 instr->op != nir_texop_lod && instr->coord_components) {
7860 assert(coords.size() > 0 && coords.size() < 3);
7861
7862 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
7863 Operand((uint32_t) 0) :
7864 Operand((uint32_t) 0x3f000000)));
7865 }
7866
7867 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
7868
7869 if (instr->op == nir_texop_samples_identical)
7870 resource = fmask_ptr;
7871
7872 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
7873 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
7874 instr->op != nir_texop_txs &&
7875 instr->op != nir_texop_fragment_fetch &&
7876 instr->op != nir_texop_fragment_mask_fetch) {
7877 assert(has_sample_index);
7878 Operand op(sample_index);
7879 if (sample_index_cv)
7880 op = Operand(sample_index_cv->u32);
7881 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
7882 }
7883
7884 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
7885 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
7886 Temp off = emit_extract_vector(ctx, offset, i, v1);
7887 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
7888 }
7889 has_offset = false;
7890 }
7891
7892 /* Build tex instruction */
7893 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
7894 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
7895 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
7896 : 0;
7897 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7898 Temp tmp_dst = dst;
7899
7900 /* gather4 selects the component by dmask and always returns vec4 */
7901 if (instr->op == nir_texop_tg4) {
7902 assert(instr->dest.ssa.num_components == 4);
7903 if (instr->is_shadow)
7904 dmask = 1;
7905 else
7906 dmask = 1 << instr->component;
7907 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
7908 tmp_dst = bld.tmp(v4);
7909 } else if (instr->op == nir_texop_samples_identical) {
7910 tmp_dst = bld.tmp(v1);
7911 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
7912 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
7913 }
7914
7915 aco_ptr<MIMG_instruction> tex;
7916 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
7917 if (!has_lod)
7918 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
7919
7920 bool div_by_6 = instr->op == nir_texop_txs &&
7921 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
7922 instr->is_array &&
7923 (dmask & (1 << 2));
7924 if (tmp_dst.id() == dst.id() && div_by_6)
7925 tmp_dst = bld.tmp(tmp_dst.regClass());
7926
7927 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
7928 tex->operands[0] = Operand(resource);
7929 tex->operands[1] = Operand(s4); /* no sampler */
7930 tex->operands[2] = Operand(as_vgpr(ctx,lod));
7931 if (ctx->options->chip_class == GFX9 &&
7932 instr->op == nir_texop_txs &&
7933 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
7934 instr->is_array) {
7935 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
7936 } else if (instr->op == nir_texop_query_levels) {
7937 tex->dmask = 1 << 3;
7938 } else {
7939 tex->dmask = dmask;
7940 }
7941 tex->da = da;
7942 tex->definitions[0] = Definition(tmp_dst);
7943 tex->dim = dim;
7944 tex->can_reorder = true;
7945 ctx->block->instructions.emplace_back(std::move(tex));
7946
7947 if (div_by_6) {
7948 /* divide 3rd value by 6 by multiplying with magic number */
7949 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
7950 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
7951 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
7952 assert(instr->dest.ssa.num_components == 3);
7953 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
7954 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
7955 emit_extract_vector(ctx, tmp_dst, 0, v1),
7956 emit_extract_vector(ctx, tmp_dst, 1, v1),
7957 by_6);
7958
7959 }
7960
7961 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
7962 return;
7963 }
7964
7965 Temp tg4_compare_cube_wa64 = Temp();
7966
7967 if (tg4_integer_workarounds) {
7968 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
7969 tex->operands[0] = Operand(resource);
7970 tex->operands[1] = Operand(s4); /* no sampler */
7971 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
7972 tex->dim = dim;
7973 tex->dmask = 0x3;
7974 tex->da = da;
7975 Temp size = bld.tmp(v2);
7976 tex->definitions[0] = Definition(size);
7977 tex->can_reorder = true;
7978 ctx->block->instructions.emplace_back(std::move(tex));
7979 emit_split_vector(ctx, size, size.size());
7980
7981 Temp half_texel[2];
7982 for (unsigned i = 0; i < 2; i++) {
7983 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
7984 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
7985 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
7986 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
7987 }
7988
7989 Temp new_coords[2] = {
7990 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
7991 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
7992 };
7993
7994 if (tg4_integer_cube_workaround) {
7995 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
7996 Temp desc[resource.size()];
7997 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
7998 Format::PSEUDO, 1, resource.size())};
7999 split->operands[0] = Operand(resource);
8000 for (unsigned i = 0; i < resource.size(); i++) {
8001 desc[i] = bld.tmp(s1);
8002 split->definitions[i] = Definition(desc[i]);
8003 }
8004 ctx->block->instructions.emplace_back(std::move(split));
8005
8006 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8007 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8008 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8009
8010 Temp nfmt;
8011 if (stype == GLSL_TYPE_UINT) {
8012 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8013 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8014 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8015 bld.scc(compare_cube_wa));
8016 } else {
8017 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8018 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8019 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8020 bld.scc(compare_cube_wa));
8021 }
8022 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8023 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8024
8025 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8026
8027 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8028 Operand((uint32_t)C_008F14_NUM_FORMAT));
8029 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8030
8031 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8032 Format::PSEUDO, resource.size(), 1)};
8033 for (unsigned i = 0; i < resource.size(); i++)
8034 vec->operands[i] = Operand(desc[i]);
8035 resource = bld.tmp(resource.regClass());
8036 vec->definitions[0] = Definition(resource);
8037 ctx->block->instructions.emplace_back(std::move(vec));
8038
8039 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8040 new_coords[0], coords[0], tg4_compare_cube_wa64);
8041 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8042 new_coords[1], coords[1], tg4_compare_cube_wa64);
8043 }
8044 coords[0] = new_coords[0];
8045 coords[1] = new_coords[1];
8046 }
8047
8048 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8049 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8050
8051 assert(coords.size() == 1);
8052 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8053 aco_opcode op;
8054 switch (last_bit) {
8055 case 1:
8056 op = aco_opcode::buffer_load_format_x; break;
8057 case 2:
8058 op = aco_opcode::buffer_load_format_xy; break;
8059 case 3:
8060 op = aco_opcode::buffer_load_format_xyz; break;
8061 case 4:
8062 op = aco_opcode::buffer_load_format_xyzw; break;
8063 default:
8064 unreachable("Tex instruction loads more than 4 components.");
8065 }
8066
8067 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8068 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8069 tmp_dst = dst;
8070 else
8071 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8072
8073 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8074 mubuf->operands[0] = Operand(resource);
8075 mubuf->operands[1] = Operand(coords[0]);
8076 mubuf->operands[2] = Operand((uint32_t) 0);
8077 mubuf->definitions[0] = Definition(tmp_dst);
8078 mubuf->idxen = true;
8079 mubuf->can_reorder = true;
8080 ctx->block->instructions.emplace_back(std::move(mubuf));
8081
8082 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8083 return;
8084 }
8085
8086 /* gather MIMG address components */
8087 std::vector<Temp> args;
8088 if (has_offset)
8089 args.emplace_back(offset);
8090 if (has_bias)
8091 args.emplace_back(bias);
8092 if (has_compare)
8093 args.emplace_back(compare);
8094 if (has_derivs)
8095 args.insert(args.end(), derivs.begin(), derivs.end());
8096
8097 args.insert(args.end(), coords.begin(), coords.end());
8098 if (has_sample_index)
8099 args.emplace_back(sample_index);
8100 if (has_lod)
8101 args.emplace_back(lod);
8102
8103 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8104 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8105 vec->definitions[0] = Definition(arg);
8106 for (unsigned i = 0; i < args.size(); i++)
8107 vec->operands[i] = Operand(args[i]);
8108 ctx->block->instructions.emplace_back(std::move(vec));
8109
8110
8111 if (instr->op == nir_texop_txf ||
8112 instr->op == nir_texop_txf_ms ||
8113 instr->op == nir_texop_samples_identical ||
8114 instr->op == nir_texop_fragment_fetch ||
8115 instr->op == nir_texop_fragment_mask_fetch) {
8116 aco_opcode op = level_zero || instr->sampler_dim == GLSL_SAMPLER_DIM_MS || instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS ? aco_opcode::image_load : aco_opcode::image_load_mip;
8117 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8118 tex->operands[0] = Operand(resource);
8119 tex->operands[1] = Operand(s4); /* no sampler */
8120 tex->operands[2] = Operand(arg);
8121 tex->dim = dim;
8122 tex->dmask = dmask;
8123 tex->unrm = true;
8124 tex->da = da;
8125 tex->definitions[0] = Definition(tmp_dst);
8126 tex->can_reorder = true;
8127 ctx->block->instructions.emplace_back(std::move(tex));
8128
8129 if (instr->op == nir_texop_samples_identical) {
8130 assert(dmask == 1 && dst.regClass() == v1);
8131 assert(dst.id() != tmp_dst.id());
8132
8133 Temp tmp = bld.tmp(bld.lm);
8134 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8135 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8136
8137 } else {
8138 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8139 }
8140 return;
8141 }
8142
8143 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8144 aco_opcode opcode = aco_opcode::image_sample;
8145 if (has_offset) { /* image_sample_*_o */
8146 if (has_compare) {
8147 opcode = aco_opcode::image_sample_c_o;
8148 if (has_derivs)
8149 opcode = aco_opcode::image_sample_c_d_o;
8150 if (has_bias)
8151 opcode = aco_opcode::image_sample_c_b_o;
8152 if (level_zero)
8153 opcode = aco_opcode::image_sample_c_lz_o;
8154 if (has_lod)
8155 opcode = aco_opcode::image_sample_c_l_o;
8156 } else {
8157 opcode = aco_opcode::image_sample_o;
8158 if (has_derivs)
8159 opcode = aco_opcode::image_sample_d_o;
8160 if (has_bias)
8161 opcode = aco_opcode::image_sample_b_o;
8162 if (level_zero)
8163 opcode = aco_opcode::image_sample_lz_o;
8164 if (has_lod)
8165 opcode = aco_opcode::image_sample_l_o;
8166 }
8167 } else { /* no offset */
8168 if (has_compare) {
8169 opcode = aco_opcode::image_sample_c;
8170 if (has_derivs)
8171 opcode = aco_opcode::image_sample_c_d;
8172 if (has_bias)
8173 opcode = aco_opcode::image_sample_c_b;
8174 if (level_zero)
8175 opcode = aco_opcode::image_sample_c_lz;
8176 if (has_lod)
8177 opcode = aco_opcode::image_sample_c_l;
8178 } else {
8179 opcode = aco_opcode::image_sample;
8180 if (has_derivs)
8181 opcode = aco_opcode::image_sample_d;
8182 if (has_bias)
8183 opcode = aco_opcode::image_sample_b;
8184 if (level_zero)
8185 opcode = aco_opcode::image_sample_lz;
8186 if (has_lod)
8187 opcode = aco_opcode::image_sample_l;
8188 }
8189 }
8190
8191 if (instr->op == nir_texop_tg4) {
8192 if (has_offset) {
8193 opcode = aco_opcode::image_gather4_lz_o;
8194 if (has_compare)
8195 opcode = aco_opcode::image_gather4_c_lz_o;
8196 } else {
8197 opcode = aco_opcode::image_gather4_lz;
8198 if (has_compare)
8199 opcode = aco_opcode::image_gather4_c_lz;
8200 }
8201 } else if (instr->op == nir_texop_lod) {
8202 opcode = aco_opcode::image_get_lod;
8203 }
8204
8205 /* we don't need the bias, sample index, compare value or offset to be
8206 * computed in WQM but if the p_create_vector copies the coordinates, then it
8207 * needs to be in WQM */
8208 if (ctx->stage == fragment_fs &&
8209 !has_derivs && !has_lod && !level_zero &&
8210 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8211 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8212 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8213
8214 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8215 tex->operands[0] = Operand(resource);
8216 tex->operands[1] = Operand(sampler);
8217 tex->operands[2] = Operand(arg);
8218 tex->dim = dim;
8219 tex->dmask = dmask;
8220 tex->da = da;
8221 tex->definitions[0] = Definition(tmp_dst);
8222 tex->can_reorder = true;
8223 ctx->block->instructions.emplace_back(std::move(tex));
8224
8225 if (tg4_integer_cube_workaround) {
8226 assert(tmp_dst.id() != dst.id());
8227 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8228
8229 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8230 Temp val[4];
8231 for (unsigned i = 0; i < dst.size(); i++) {
8232 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8233 Temp cvt_val;
8234 if (stype == GLSL_TYPE_UINT)
8235 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8236 else
8237 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8238 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8239 }
8240 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8241 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8242 val[0], val[1], val[2], val[3]);
8243 }
8244 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8245 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8246
8247 }
8248
8249
8250 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8251 {
8252 Temp tmp = get_ssa_temp(ctx, ssa);
8253 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8254 return Operand(tmp.regClass());
8255 else
8256 return Operand(tmp);
8257 }
8258
8259 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8260 {
8261 aco_ptr<Pseudo_instruction> phi;
8262 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8263 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8264
8265 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8266 logical |= ctx->block->kind & block_kind_merge;
8267 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8268
8269 /* we want a sorted list of sources, since the predecessor list is also sorted */
8270 std::map<unsigned, nir_ssa_def*> phi_src;
8271 nir_foreach_phi_src(src, instr)
8272 phi_src[src->pred->index] = src->src.ssa;
8273
8274 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8275 unsigned num_operands = 0;
8276 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8277 unsigned num_defined = 0;
8278 unsigned cur_pred_idx = 0;
8279 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8280 if (cur_pred_idx < preds.size()) {
8281 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8282 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8283 unsigned skipped = 0;
8284 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8285 skipped++;
8286 if (cur_pred_idx + skipped < preds.size()) {
8287 for (unsigned i = 0; i < skipped; i++)
8288 operands[num_operands++] = Operand(dst.regClass());
8289 cur_pred_idx += skipped;
8290 } else {
8291 continue;
8292 }
8293 }
8294 /* Handle missing predecessors at the end. This shouldn't happen with loop
8295 * headers and we can't ignore these sources for loop header phis. */
8296 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8297 continue;
8298 cur_pred_idx++;
8299 Operand op = get_phi_operand(ctx, src.second);
8300 operands[num_operands++] = op;
8301 num_defined += !op.isUndefined();
8302 }
8303 /* handle block_kind_continue_or_break at loop exit blocks */
8304 while (cur_pred_idx++ < preds.size())
8305 operands[num_operands++] = Operand(dst.regClass());
8306
8307 /* If the loop ends with a break, still add a linear continue edge in case
8308 * that break is divergent or continue_or_break is used. We'll either remove
8309 * this operand later in visit_loop() if it's not necessary or replace the
8310 * undef with something correct. */
8311 if (!logical && ctx->block->kind & block_kind_loop_header) {
8312 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8313 nir_block *last = nir_loop_last_block(loop);
8314 if (last->successors[0] != instr->instr.block)
8315 operands[num_operands++] = Operand(RegClass());
8316 }
8317
8318 if (num_defined == 0) {
8319 Builder bld(ctx->program, ctx->block);
8320 if (dst.regClass() == s1) {
8321 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8322 } else if (dst.regClass() == v1) {
8323 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8324 } else {
8325 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8326 for (unsigned i = 0; i < dst.size(); i++)
8327 vec->operands[i] = Operand(0u);
8328 vec->definitions[0] = Definition(dst);
8329 ctx->block->instructions.emplace_back(std::move(vec));
8330 }
8331 return;
8332 }
8333
8334 /* we can use a linear phi in some cases if one src is undef */
8335 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8336 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8337
8338 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8339 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8340 assert(invert->kind & block_kind_invert);
8341
8342 unsigned then_block = invert->linear_preds[0];
8343
8344 Block* insert_block = NULL;
8345 for (unsigned i = 0; i < num_operands; i++) {
8346 Operand op = operands[i];
8347 if (op.isUndefined())
8348 continue;
8349 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8350 phi->operands[0] = op;
8351 break;
8352 }
8353 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8354 phi->operands[1] = Operand(dst.regClass());
8355 phi->definitions[0] = Definition(dst);
8356 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8357 return;
8358 }
8359
8360 /* try to scalarize vector phis */
8361 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8362 // TODO: scalarize linear phis on divergent ifs
8363 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8364 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8365 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8366 Operand src = operands[i];
8367 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8368 can_scalarize = false;
8369 }
8370 if (can_scalarize) {
8371 unsigned num_components = instr->dest.ssa.num_components;
8372 assert(dst.size() % num_components == 0);
8373 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8374
8375 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8376 for (unsigned k = 0; k < num_components; k++) {
8377 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8378 for (unsigned i = 0; i < num_operands; i++) {
8379 Operand src = operands[i];
8380 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8381 }
8382 Temp phi_dst = {ctx->program->allocateId(), rc};
8383 phi->definitions[0] = Definition(phi_dst);
8384 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8385 new_vec[k] = phi_dst;
8386 vec->operands[k] = Operand(phi_dst);
8387 }
8388 vec->definitions[0] = Definition(dst);
8389 ctx->block->instructions.emplace_back(std::move(vec));
8390 ctx->allocated_vec.emplace(dst.id(), new_vec);
8391 return;
8392 }
8393 }
8394
8395 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8396 for (unsigned i = 0; i < num_operands; i++)
8397 phi->operands[i] = operands[i];
8398 phi->definitions[0] = Definition(dst);
8399 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8400 }
8401
8402
8403 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8404 {
8405 Temp dst = get_ssa_temp(ctx, &instr->def);
8406
8407 assert(dst.type() == RegType::sgpr);
8408
8409 if (dst.size() == 1) {
8410 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8411 } else {
8412 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8413 for (unsigned i = 0; i < dst.size(); i++)
8414 vec->operands[i] = Operand(0u);
8415 vec->definitions[0] = Definition(dst);
8416 ctx->block->instructions.emplace_back(std::move(vec));
8417 }
8418 }
8419
8420 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8421 {
8422 Builder bld(ctx->program, ctx->block);
8423 Block *logical_target;
8424 append_logical_end(ctx->block);
8425 unsigned idx = ctx->block->index;
8426
8427 switch (instr->type) {
8428 case nir_jump_break:
8429 logical_target = ctx->cf_info.parent_loop.exit;
8430 add_logical_edge(idx, logical_target);
8431 ctx->block->kind |= block_kind_break;
8432
8433 if (!ctx->cf_info.parent_if.is_divergent &&
8434 !ctx->cf_info.parent_loop.has_divergent_continue) {
8435 /* uniform break - directly jump out of the loop */
8436 ctx->block->kind |= block_kind_uniform;
8437 ctx->cf_info.has_branch = true;
8438 bld.branch(aco_opcode::p_branch);
8439 add_linear_edge(idx, logical_target);
8440 return;
8441 }
8442 ctx->cf_info.parent_loop.has_divergent_branch = true;
8443 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8444 break;
8445 case nir_jump_continue:
8446 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8447 add_logical_edge(idx, logical_target);
8448 ctx->block->kind |= block_kind_continue;
8449
8450 if (ctx->cf_info.parent_if.is_divergent) {
8451 /* for potential uniform breaks after this continue,
8452 we must ensure that they are handled correctly */
8453 ctx->cf_info.parent_loop.has_divergent_continue = true;
8454 ctx->cf_info.parent_loop.has_divergent_branch = true;
8455 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8456 } else {
8457 /* uniform continue - directly jump to the loop header */
8458 ctx->block->kind |= block_kind_uniform;
8459 ctx->cf_info.has_branch = true;
8460 bld.branch(aco_opcode::p_branch);
8461 add_linear_edge(idx, logical_target);
8462 return;
8463 }
8464 break;
8465 default:
8466 fprintf(stderr, "Unknown NIR jump instr: ");
8467 nir_print_instr(&instr->instr, stderr);
8468 fprintf(stderr, "\n");
8469 abort();
8470 }
8471
8472 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
8473 ctx->cf_info.exec_potentially_empty_break = true;
8474 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
8475 }
8476
8477 /* remove critical edges from linear CFG */
8478 bld.branch(aco_opcode::p_branch);
8479 Block* break_block = ctx->program->create_and_insert_block();
8480 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8481 break_block->kind |= block_kind_uniform;
8482 add_linear_edge(idx, break_block);
8483 /* the loop_header pointer might be invalidated by this point */
8484 if (instr->type == nir_jump_continue)
8485 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8486 add_linear_edge(break_block->index, logical_target);
8487 bld.reset(break_block);
8488 bld.branch(aco_opcode::p_branch);
8489
8490 Block* continue_block = ctx->program->create_and_insert_block();
8491 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8492 add_linear_edge(idx, continue_block);
8493 append_logical_start(continue_block);
8494 ctx->block = continue_block;
8495 return;
8496 }
8497
8498 void visit_block(isel_context *ctx, nir_block *block)
8499 {
8500 nir_foreach_instr(instr, block) {
8501 switch (instr->type) {
8502 case nir_instr_type_alu:
8503 visit_alu_instr(ctx, nir_instr_as_alu(instr));
8504 break;
8505 case nir_instr_type_load_const:
8506 visit_load_const(ctx, nir_instr_as_load_const(instr));
8507 break;
8508 case nir_instr_type_intrinsic:
8509 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
8510 break;
8511 case nir_instr_type_tex:
8512 visit_tex(ctx, nir_instr_as_tex(instr));
8513 break;
8514 case nir_instr_type_phi:
8515 visit_phi(ctx, nir_instr_as_phi(instr));
8516 break;
8517 case nir_instr_type_ssa_undef:
8518 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
8519 break;
8520 case nir_instr_type_deref:
8521 break;
8522 case nir_instr_type_jump:
8523 visit_jump(ctx, nir_instr_as_jump(instr));
8524 break;
8525 default:
8526 fprintf(stderr, "Unknown NIR instr type: ");
8527 nir_print_instr(instr, stderr);
8528 fprintf(stderr, "\n");
8529 //abort();
8530 }
8531 }
8532
8533 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8534 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
8535 }
8536
8537
8538
8539 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
8540 aco_ptr<Instruction>& header_phi, Operand *vals)
8541 {
8542 vals[0] = Operand(header_phi->definitions[0].getTemp());
8543 RegClass rc = vals[0].regClass();
8544
8545 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
8546
8547 unsigned next_pred = 1;
8548
8549 for (unsigned idx = first + 1; idx <= last; idx++) {
8550 Block& block = ctx->program->blocks[idx];
8551 if (block.loop_nest_depth != loop_nest_depth) {
8552 vals[idx - first] = vals[idx - 1 - first];
8553 continue;
8554 }
8555
8556 if (block.kind & block_kind_continue) {
8557 vals[idx - first] = header_phi->operands[next_pred];
8558 next_pred++;
8559 continue;
8560 }
8561
8562 bool all_same = true;
8563 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
8564 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
8565
8566 Operand val;
8567 if (all_same) {
8568 val = vals[block.linear_preds[0] - first];
8569 } else {
8570 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
8571 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
8572 for (unsigned i = 0; i < block.linear_preds.size(); i++)
8573 phi->operands[i] = vals[block.linear_preds[i] - first];
8574 val = Operand(Temp(ctx->program->allocateId(), rc));
8575 phi->definitions[0] = Definition(val.getTemp());
8576 block.instructions.emplace(block.instructions.begin(), std::move(phi));
8577 }
8578 vals[idx - first] = val;
8579 }
8580
8581 return vals[last - first];
8582 }
8583
8584 static void visit_loop(isel_context *ctx, nir_loop *loop)
8585 {
8586 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
8587 append_logical_end(ctx->block);
8588 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
8589 Builder bld(ctx->program, ctx->block);
8590 bld.branch(aco_opcode::p_branch);
8591 unsigned loop_preheader_idx = ctx->block->index;
8592
8593 Block loop_exit = Block();
8594 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8595 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
8596
8597 Block* loop_header = ctx->program->create_and_insert_block();
8598 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
8599 loop_header->kind |= block_kind_loop_header;
8600 add_edge(loop_preheader_idx, loop_header);
8601 ctx->block = loop_header;
8602
8603 /* emit loop body */
8604 unsigned loop_header_idx = loop_header->index;
8605 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
8606 append_logical_start(ctx->block);
8607 bool unreachable = visit_cf_list(ctx, &loop->body);
8608
8609 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
8610 if (!ctx->cf_info.has_branch) {
8611 append_logical_end(ctx->block);
8612 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
8613 /* Discards can result in code running with an empty exec mask.
8614 * This would result in divergent breaks not ever being taken. As a
8615 * workaround, break the loop when the loop mask is empty instead of
8616 * always continuing. */
8617 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
8618 unsigned block_idx = ctx->block->index;
8619
8620 /* create helper blocks to avoid critical edges */
8621 Block *break_block = ctx->program->create_and_insert_block();
8622 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8623 break_block->kind = block_kind_uniform;
8624 bld.reset(break_block);
8625 bld.branch(aco_opcode::p_branch);
8626 add_linear_edge(block_idx, break_block);
8627 add_linear_edge(break_block->index, &loop_exit);
8628
8629 Block *continue_block = ctx->program->create_and_insert_block();
8630 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8631 continue_block->kind = block_kind_uniform;
8632 bld.reset(continue_block);
8633 bld.branch(aco_opcode::p_branch);
8634 add_linear_edge(block_idx, continue_block);
8635 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
8636
8637 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8638 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
8639 ctx->block = &ctx->program->blocks[block_idx];
8640 } else {
8641 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
8642 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8643 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
8644 else
8645 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
8646 }
8647
8648 bld.reset(ctx->block);
8649 bld.branch(aco_opcode::p_branch);
8650 }
8651
8652 /* Fixup phis in loop header from unreachable blocks.
8653 * has_branch/has_divergent_branch also indicates if the loop ends with a
8654 * break/continue instruction, but we don't emit those if unreachable=true */
8655 if (unreachable) {
8656 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
8657 bool linear = ctx->cf_info.has_branch;
8658 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
8659 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
8660 if ((logical && instr->opcode == aco_opcode::p_phi) ||
8661 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
8662 /* the last operand should be the one that needs to be removed */
8663 instr->operands.pop_back();
8664 } else if (!is_phi(instr)) {
8665 break;
8666 }
8667 }
8668 }
8669
8670 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
8671 * and the previous one shouldn't both happen at once because a break in the
8672 * merge block would get CSE'd */
8673 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
8674 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
8675 Operand vals[num_vals];
8676 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
8677 if (instr->opcode == aco_opcode::p_linear_phi) {
8678 if (ctx->cf_info.has_branch)
8679 instr->operands.pop_back();
8680 else
8681 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
8682 } else if (!is_phi(instr)) {
8683 break;
8684 }
8685 }
8686 }
8687
8688 ctx->cf_info.has_branch = false;
8689
8690 // TODO: if the loop has not a single exit, we must add one °°
8691 /* emit loop successor block */
8692 ctx->block = ctx->program->insert_block(std::move(loop_exit));
8693 append_logical_start(ctx->block);
8694
8695 #if 0
8696 // TODO: check if it is beneficial to not branch on continues
8697 /* trim linear phis in loop header */
8698 for (auto&& instr : loop_entry->instructions) {
8699 if (instr->opcode == aco_opcode::p_linear_phi) {
8700 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
8701 new_phi->definitions[0] = instr->definitions[0];
8702 for (unsigned i = 0; i < new_phi->operands.size(); i++)
8703 new_phi->operands[i] = instr->operands[i];
8704 /* check that the remaining operands are all the same */
8705 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
8706 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
8707 instr.swap(new_phi);
8708 } else if (instr->opcode == aco_opcode::p_phi) {
8709 continue;
8710 } else {
8711 break;
8712 }
8713 }
8714 #endif
8715 }
8716
8717 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
8718 {
8719 ic->cond = cond;
8720
8721 append_logical_end(ctx->block);
8722 ctx->block->kind |= block_kind_branch;
8723
8724 /* branch to linear then block */
8725 assert(cond.regClass() == ctx->program->lane_mask);
8726 aco_ptr<Pseudo_branch_instruction> branch;
8727 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
8728 branch->operands[0] = Operand(cond);
8729 ctx->block->instructions.push_back(std::move(branch));
8730
8731 ic->BB_if_idx = ctx->block->index;
8732 ic->BB_invert = Block();
8733 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8734 /* Invert blocks are intentionally not marked as top level because they
8735 * are not part of the logical cfg. */
8736 ic->BB_invert.kind |= block_kind_invert;
8737 ic->BB_endif = Block();
8738 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8739 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
8740
8741 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
8742 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
8743 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
8744 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
8745 ctx->cf_info.parent_if.is_divergent = true;
8746
8747 /* divergent branches use cbranch_execz */
8748 ctx->cf_info.exec_potentially_empty_discard = false;
8749 ctx->cf_info.exec_potentially_empty_break = false;
8750 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8751
8752 /** emit logical then block */
8753 Block* BB_then_logical = ctx->program->create_and_insert_block();
8754 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8755 add_edge(ic->BB_if_idx, BB_then_logical);
8756 ctx->block = BB_then_logical;
8757 append_logical_start(BB_then_logical);
8758 }
8759
8760 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
8761 {
8762 Block *BB_then_logical = ctx->block;
8763 append_logical_end(BB_then_logical);
8764 /* branch from logical then block to invert block */
8765 aco_ptr<Pseudo_branch_instruction> branch;
8766 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8767 BB_then_logical->instructions.emplace_back(std::move(branch));
8768 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
8769 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8770 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
8771 BB_then_logical->kind |= block_kind_uniform;
8772 assert(!ctx->cf_info.has_branch);
8773 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
8774 ctx->cf_info.parent_loop.has_divergent_branch = false;
8775
8776 /** emit linear then block */
8777 Block* BB_then_linear = ctx->program->create_and_insert_block();
8778 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8779 BB_then_linear->kind |= block_kind_uniform;
8780 add_linear_edge(ic->BB_if_idx, BB_then_linear);
8781 /* branch from linear then block to invert block */
8782 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8783 BB_then_linear->instructions.emplace_back(std::move(branch));
8784 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
8785
8786 /** emit invert merge block */
8787 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
8788 ic->invert_idx = ctx->block->index;
8789
8790 /* branch to linear else block (skip else) */
8791 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
8792 branch->operands[0] = Operand(ic->cond);
8793 ctx->block->instructions.push_back(std::move(branch));
8794
8795 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
8796 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
8797 ic->exec_potentially_empty_break_depth_old =
8798 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
8799 /* divergent branches use cbranch_execz */
8800 ctx->cf_info.exec_potentially_empty_discard = false;
8801 ctx->cf_info.exec_potentially_empty_break = false;
8802 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8803
8804 /** emit logical else block */
8805 Block* BB_else_logical = ctx->program->create_and_insert_block();
8806 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8807 add_logical_edge(ic->BB_if_idx, BB_else_logical);
8808 add_linear_edge(ic->invert_idx, BB_else_logical);
8809 ctx->block = BB_else_logical;
8810 append_logical_start(BB_else_logical);
8811 }
8812
8813 static void end_divergent_if(isel_context *ctx, if_context *ic)
8814 {
8815 Block *BB_else_logical = ctx->block;
8816 append_logical_end(BB_else_logical);
8817
8818 /* branch from logical else block to endif block */
8819 aco_ptr<Pseudo_branch_instruction> branch;
8820 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8821 BB_else_logical->instructions.emplace_back(std::move(branch));
8822 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
8823 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8824 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
8825 BB_else_logical->kind |= block_kind_uniform;
8826
8827 assert(!ctx->cf_info.has_branch);
8828 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
8829
8830
8831 /** emit linear else block */
8832 Block* BB_else_linear = ctx->program->create_and_insert_block();
8833 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8834 BB_else_linear->kind |= block_kind_uniform;
8835 add_linear_edge(ic->invert_idx, BB_else_linear);
8836
8837 /* branch from linear else block to endif block */
8838 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8839 BB_else_linear->instructions.emplace_back(std::move(branch));
8840 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
8841
8842
8843 /** emit endif merge block */
8844 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
8845 append_logical_start(ctx->block);
8846
8847
8848 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
8849 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
8850 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
8851 ctx->cf_info.exec_potentially_empty_break_depth =
8852 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
8853 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
8854 !ctx->cf_info.parent_if.is_divergent) {
8855 ctx->cf_info.exec_potentially_empty_break = false;
8856 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8857 }
8858 /* uniform control flow never has an empty exec-mask */
8859 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
8860 ctx->cf_info.exec_potentially_empty_discard = false;
8861 ctx->cf_info.exec_potentially_empty_break = false;
8862 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8863 }
8864 }
8865
8866 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
8867 {
8868 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
8869 Builder bld(ctx->program, ctx->block);
8870 aco_ptr<Pseudo_branch_instruction> branch;
8871
8872 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
8873 /**
8874 * Uniform conditionals are represented in the following way*) :
8875 *
8876 * The linear and logical CFG:
8877 * BB_IF
8878 * / \
8879 * BB_THEN (logical) BB_ELSE (logical)
8880 * \ /
8881 * BB_ENDIF
8882 *
8883 * *) Exceptions may be due to break and continue statements within loops
8884 * If a break/continue happens within uniform control flow, it branches
8885 * to the loop exit/entry block. Otherwise, it branches to the next
8886 * merge block.
8887 **/
8888 append_logical_end(ctx->block);
8889 ctx->block->kind |= block_kind_uniform;
8890
8891 /* emit branch */
8892 assert(cond.regClass() == bld.lm);
8893 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
8894 cond = bool_to_scalar_condition(ctx, cond);
8895
8896 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
8897 branch->operands[0] = Operand(cond);
8898 branch->operands[0].setFixed(scc);
8899 ctx->block->instructions.emplace_back(std::move(branch));
8900
8901 unsigned BB_if_idx = ctx->block->index;
8902 Block BB_endif = Block();
8903 BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8904 BB_endif.kind |= ctx->block->kind & block_kind_top_level;
8905
8906 /** emit then block */
8907 Block* BB_then = ctx->program->create_and_insert_block();
8908 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8909 add_edge(BB_if_idx, BB_then);
8910 append_logical_start(BB_then);
8911 ctx->block = BB_then;
8912 visit_cf_list(ctx, &if_stmt->then_list);
8913 BB_then = ctx->block;
8914 bool then_branch = ctx->cf_info.has_branch;
8915 bool then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
8916
8917 if (!then_branch) {
8918 append_logical_end(BB_then);
8919 /* branch from then block to endif block */
8920 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8921 BB_then->instructions.emplace_back(std::move(branch));
8922 add_linear_edge(BB_then->index, &BB_endif);
8923 if (!then_branch_divergent)
8924 add_logical_edge(BB_then->index, &BB_endif);
8925 BB_then->kind |= block_kind_uniform;
8926 }
8927
8928 ctx->cf_info.has_branch = false;
8929 ctx->cf_info.parent_loop.has_divergent_branch = false;
8930
8931 /** emit else block */
8932 Block* BB_else = ctx->program->create_and_insert_block();
8933 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8934 add_edge(BB_if_idx, BB_else);
8935 append_logical_start(BB_else);
8936 ctx->block = BB_else;
8937 visit_cf_list(ctx, &if_stmt->else_list);
8938 BB_else = ctx->block;
8939
8940 if (!ctx->cf_info.has_branch) {
8941 append_logical_end(BB_else);
8942 /* branch from then block to endif block */
8943 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8944 BB_else->instructions.emplace_back(std::move(branch));
8945 add_linear_edge(BB_else->index, &BB_endif);
8946 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8947 add_logical_edge(BB_else->index, &BB_endif);
8948 BB_else->kind |= block_kind_uniform;
8949 }
8950
8951 ctx->cf_info.has_branch &= then_branch;
8952 ctx->cf_info.parent_loop.has_divergent_branch &= then_branch_divergent;
8953
8954 /** emit endif merge block */
8955 if (!ctx->cf_info.has_branch) {
8956 ctx->block = ctx->program->insert_block(std::move(BB_endif));
8957 append_logical_start(ctx->block);
8958 }
8959 return !ctx->cf_info.has_branch;
8960 } else { /* non-uniform condition */
8961 /**
8962 * To maintain a logical and linear CFG without critical edges,
8963 * non-uniform conditionals are represented in the following way*) :
8964 *
8965 * The linear CFG:
8966 * BB_IF
8967 * / \
8968 * BB_THEN (logical) BB_THEN (linear)
8969 * \ /
8970 * BB_INVERT (linear)
8971 * / \
8972 * BB_ELSE (logical) BB_ELSE (linear)
8973 * \ /
8974 * BB_ENDIF
8975 *
8976 * The logical CFG:
8977 * BB_IF
8978 * / \
8979 * BB_THEN (logical) BB_ELSE (logical)
8980 * \ /
8981 * BB_ENDIF
8982 *
8983 * *) Exceptions may be due to break and continue statements within loops
8984 **/
8985
8986 if_context ic;
8987
8988 begin_divergent_if_then(ctx, &ic, cond);
8989 visit_cf_list(ctx, &if_stmt->then_list);
8990
8991 begin_divergent_if_else(ctx, &ic);
8992 visit_cf_list(ctx, &if_stmt->else_list);
8993
8994 end_divergent_if(ctx, &ic);
8995
8996 return true;
8997 }
8998 }
8999
9000 static bool visit_cf_list(isel_context *ctx,
9001 struct exec_list *list)
9002 {
9003 foreach_list_typed(nir_cf_node, node, node, list) {
9004 switch (node->type) {
9005 case nir_cf_node_block:
9006 visit_block(ctx, nir_cf_node_as_block(node));
9007 break;
9008 case nir_cf_node_if:
9009 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9010 return true;
9011 break;
9012 case nir_cf_node_loop:
9013 visit_loop(ctx, nir_cf_node_as_loop(node));
9014 break;
9015 default:
9016 unreachable("unimplemented cf list type");
9017 }
9018 }
9019 return false;
9020 }
9021
9022 static void create_null_export(isel_context *ctx)
9023 {
9024 /* Some shader stages always need to have exports.
9025 * So when there is none, we need to add a null export.
9026 */
9027
9028 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9029 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9030 Builder bld(ctx->program, ctx->block);
9031 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9032 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9033 }
9034
9035 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9036 {
9037 assert(ctx->stage == vertex_vs ||
9038 ctx->stage == tess_eval_vs ||
9039 ctx->stage == gs_copy_vs);
9040
9041 int offset = ctx->stage == tess_eval_vs
9042 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9043 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9044 uint64_t mask = ctx->outputs.mask[slot];
9045 if (!is_pos && !mask)
9046 return false;
9047 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9048 return false;
9049 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9050 exp->enabled_mask = mask;
9051 for (unsigned i = 0; i < 4; ++i) {
9052 if (mask & (1 << i))
9053 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9054 else
9055 exp->operands[i] = Operand(v1);
9056 }
9057 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9058 * Setting valid_mask=1 prevents it and has no other effect.
9059 */
9060 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9061 exp->done = false;
9062 exp->compressed = false;
9063 if (is_pos)
9064 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9065 else
9066 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9067 ctx->block->instructions.emplace_back(std::move(exp));
9068
9069 return true;
9070 }
9071
9072 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9073 {
9074 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9075 exp->enabled_mask = 0;
9076 for (unsigned i = 0; i < 4; ++i)
9077 exp->operands[i] = Operand(v1);
9078 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9079 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9080 exp->enabled_mask |= 0x1;
9081 }
9082 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9083 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9084 exp->enabled_mask |= 0x4;
9085 }
9086 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9087 if (ctx->options->chip_class < GFX9) {
9088 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9089 exp->enabled_mask |= 0x8;
9090 } else {
9091 Builder bld(ctx->program, ctx->block);
9092
9093 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9094 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9095 if (exp->operands[2].isTemp())
9096 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9097
9098 exp->operands[2] = Operand(out);
9099 exp->enabled_mask |= 0x4;
9100 }
9101 }
9102 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9103 exp->done = false;
9104 exp->compressed = false;
9105 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9106 ctx->block->instructions.emplace_back(std::move(exp));
9107 }
9108
9109 static void create_vs_exports(isel_context *ctx)
9110 {
9111 assert(ctx->stage == vertex_vs ||
9112 ctx->stage == tess_eval_vs ||
9113 ctx->stage == gs_copy_vs);
9114
9115 radv_vs_output_info *outinfo = ctx->stage == tess_eval_vs
9116 ? &ctx->program->info->tes.outinfo
9117 : &ctx->program->info->vs.outinfo;
9118
9119 if (outinfo->export_prim_id) {
9120 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9121 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9122 }
9123
9124 if (ctx->options->key.has_multiview_view_index) {
9125 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9126 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9127 }
9128
9129 /* the order these position exports are created is important */
9130 int next_pos = 0;
9131 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9132 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9133 export_vs_psiz_layer_viewport(ctx, &next_pos);
9134 exported_pos = true;
9135 }
9136 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9137 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9138 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9139 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9140
9141 if (ctx->export_clip_dists) {
9142 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9143 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9144 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9145 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9146 }
9147
9148 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9149 if (i < VARYING_SLOT_VAR0 && i != VARYING_SLOT_LAYER &&
9150 i != VARYING_SLOT_PRIMITIVE_ID)
9151 continue;
9152
9153 export_vs_varying(ctx, i, false, NULL);
9154 }
9155
9156 if (!exported_pos)
9157 create_null_export(ctx);
9158 }
9159
9160 static bool export_fs_mrt_z(isel_context *ctx)
9161 {
9162 Builder bld(ctx->program, ctx->block);
9163 unsigned enabled_channels = 0;
9164 bool compr = false;
9165 Operand values[4];
9166
9167 for (unsigned i = 0; i < 4; ++i) {
9168 values[i] = Operand(v1);
9169 }
9170
9171 /* Both stencil and sample mask only need 16-bits. */
9172 if (!ctx->program->info->ps.writes_z &&
9173 (ctx->program->info->ps.writes_stencil ||
9174 ctx->program->info->ps.writes_sample_mask)) {
9175 compr = true; /* COMPR flag */
9176
9177 if (ctx->program->info->ps.writes_stencil) {
9178 /* Stencil should be in X[23:16]. */
9179 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9180 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9181 enabled_channels |= 0x3;
9182 }
9183
9184 if (ctx->program->info->ps.writes_sample_mask) {
9185 /* SampleMask should be in Y[15:0]. */
9186 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9187 enabled_channels |= 0xc;
9188 }
9189 } else {
9190 if (ctx->program->info->ps.writes_z) {
9191 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9192 enabled_channels |= 0x1;
9193 }
9194
9195 if (ctx->program->info->ps.writes_stencil) {
9196 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9197 enabled_channels |= 0x2;
9198 }
9199
9200 if (ctx->program->info->ps.writes_sample_mask) {
9201 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9202 enabled_channels |= 0x4;
9203 }
9204 }
9205
9206 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9207 * writemask component.
9208 */
9209 if (ctx->options->chip_class == GFX6 &&
9210 ctx->options->family != CHIP_OLAND &&
9211 ctx->options->family != CHIP_HAINAN) {
9212 enabled_channels |= 0x1;
9213 }
9214
9215 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9216 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9217
9218 return true;
9219 }
9220
9221 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9222 {
9223 Builder bld(ctx->program, ctx->block);
9224 unsigned write_mask = ctx->outputs.mask[slot];
9225 Operand values[4];
9226
9227 for (unsigned i = 0; i < 4; ++i) {
9228 if (write_mask & (1 << i)) {
9229 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9230 } else {
9231 values[i] = Operand(v1);
9232 }
9233 }
9234
9235 unsigned target, col_format;
9236 unsigned enabled_channels = 0;
9237 aco_opcode compr_op = (aco_opcode)0;
9238
9239 slot -= FRAG_RESULT_DATA0;
9240 target = V_008DFC_SQ_EXP_MRT + slot;
9241 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9242
9243 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9244 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9245
9246 switch (col_format)
9247 {
9248 case V_028714_SPI_SHADER_ZERO:
9249 enabled_channels = 0; /* writemask */
9250 target = V_008DFC_SQ_EXP_NULL;
9251 break;
9252
9253 case V_028714_SPI_SHADER_32_R:
9254 enabled_channels = 1;
9255 break;
9256
9257 case V_028714_SPI_SHADER_32_GR:
9258 enabled_channels = 0x3;
9259 break;
9260
9261 case V_028714_SPI_SHADER_32_AR:
9262 if (ctx->options->chip_class >= GFX10) {
9263 /* Special case: on GFX10, the outputs are different for 32_AR */
9264 enabled_channels = 0x3;
9265 values[1] = values[3];
9266 values[3] = Operand(v1);
9267 } else {
9268 enabled_channels = 0x9;
9269 }
9270 break;
9271
9272 case V_028714_SPI_SHADER_FP16_ABGR:
9273 enabled_channels = 0x5;
9274 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9275 break;
9276
9277 case V_028714_SPI_SHADER_UNORM16_ABGR:
9278 enabled_channels = 0x5;
9279 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9280 break;
9281
9282 case V_028714_SPI_SHADER_SNORM16_ABGR:
9283 enabled_channels = 0x5;
9284 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9285 break;
9286
9287 case V_028714_SPI_SHADER_UINT16_ABGR: {
9288 enabled_channels = 0x5;
9289 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9290 if (is_int8 || is_int10) {
9291 /* clamp */
9292 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9293 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9294
9295 for (unsigned i = 0; i < 4; i++) {
9296 if ((write_mask >> i) & 1) {
9297 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9298 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9299 values[i]);
9300 }
9301 }
9302 }
9303 break;
9304 }
9305
9306 case V_028714_SPI_SHADER_SINT16_ABGR:
9307 enabled_channels = 0x5;
9308 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9309 if (is_int8 || is_int10) {
9310 /* clamp */
9311 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9312 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9313 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9314 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9315
9316 for (unsigned i = 0; i < 4; i++) {
9317 if ((write_mask >> i) & 1) {
9318 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9319 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9320 values[i]);
9321 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9322 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9323 values[i]);
9324 }
9325 }
9326 }
9327 break;
9328
9329 case V_028714_SPI_SHADER_32_ABGR:
9330 enabled_channels = 0xF;
9331 break;
9332
9333 default:
9334 break;
9335 }
9336
9337 if (target == V_008DFC_SQ_EXP_NULL)
9338 return false;
9339
9340 if ((bool) compr_op) {
9341 for (int i = 0; i < 2; i++) {
9342 /* check if at least one of the values to be compressed is enabled */
9343 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9344 if (enabled) {
9345 enabled_channels |= enabled << (i*2);
9346 values[i] = bld.vop3(compr_op, bld.def(v1),
9347 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9348 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9349 } else {
9350 values[i] = Operand(v1);
9351 }
9352 }
9353 values[2] = Operand(v1);
9354 values[3] = Operand(v1);
9355 } else {
9356 for (int i = 0; i < 4; i++)
9357 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
9358 }
9359
9360 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9361 enabled_channels, target, (bool) compr_op);
9362 return true;
9363 }
9364
9365 static void create_fs_exports(isel_context *ctx)
9366 {
9367 bool exported = false;
9368
9369 /* Export depth, stencil and sample mask. */
9370 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
9371 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
9372 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
9373 exported |= export_fs_mrt_z(ctx);
9374
9375 /* Export all color render targets. */
9376 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
9377 if (ctx->outputs.mask[i])
9378 exported |= export_fs_mrt_color(ctx, i);
9379
9380 if (!exported)
9381 create_null_export(ctx);
9382 }
9383
9384 static void write_tcs_tess_factors(isel_context *ctx)
9385 {
9386 unsigned outer_comps;
9387 unsigned inner_comps;
9388
9389 switch (ctx->args->options->key.tcs.primitive_mode) {
9390 case GL_ISOLINES:
9391 outer_comps = 2;
9392 inner_comps = 0;
9393 break;
9394 case GL_TRIANGLES:
9395 outer_comps = 3;
9396 inner_comps = 1;
9397 break;
9398 case GL_QUADS:
9399 outer_comps = 4;
9400 inner_comps = 2;
9401 break;
9402 default:
9403 return;
9404 }
9405
9406 Builder bld(ctx->program, ctx->block);
9407
9408 bld.barrier(aco_opcode::p_memory_barrier_shared);
9409 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
9410 bld.sopp(aco_opcode::s_barrier);
9411
9412 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
9413 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
9414
9415 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
9416 if_context ic_invocation_id_is_zero;
9417 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
9418 bld.reset(ctx->block);
9419
9420 Temp hs_ring_tess_factor = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_FACTOR * 16u));
9421
9422 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
9423 unsigned stride = inner_comps + outer_comps;
9424 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
9425 Temp tf_inner_vec;
9426 Temp tf_outer_vec;
9427 Temp out[6];
9428 assert(stride <= (sizeof(out) / sizeof(Temp)));
9429
9430 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
9431 // LINES reversal
9432 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
9433 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
9434 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
9435 } else {
9436 tf_outer_vec = load_lds(ctx, 4, bld.tmp(RegClass(RegType::vgpr, outer_comps)), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
9437 tf_inner_vec = load_lds(ctx, 4, bld.tmp(RegClass(RegType::vgpr, inner_comps)), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_in_loc, lds_align);
9438
9439 for (unsigned i = 0; i < outer_comps; ++i)
9440 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
9441 for (unsigned i = 0; i < inner_comps; ++i)
9442 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
9443 }
9444
9445 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
9446 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
9447 Temp byte_offset = bld.v_mul_imm(bld.def(v1), rel_patch_id, stride * 4u);
9448 unsigned tf_const_offset = 0;
9449
9450 if (ctx->program->chip_class <= GFX8) {
9451 Temp rel_patch_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), rel_patch_id);
9452 if_context ic_rel_patch_id_is_zero;
9453 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
9454 bld.reset(ctx->block);
9455
9456 /* Store the dynamic HS control word. */
9457 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
9458 bld.mubuf(aco_opcode::buffer_store_dword,
9459 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
9460 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
9461 /* disable_wqm */ false, /* glc */ true);
9462 tf_const_offset += 4;
9463
9464 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
9465 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
9466 bld.reset(ctx->block);
9467 }
9468
9469 assert(stride == 2 || stride == 4 || stride == 6);
9470 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
9471 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
9472
9473 /* Store to offchip for TES to read - only if TES reads them */
9474 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
9475 Temp hs_ring_tess_offchip = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
9476 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
9477
9478 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
9479 store_vmem_mubuf(ctx, tf_outer_vec, hs_ring_tess_offchip, vmem_offs_outer.first, oc_lds, vmem_offs_outer.second, 4, (1 << outer_comps) - 1, true, false);
9480
9481 if (likely(inner_comps)) {
9482 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
9483 store_vmem_mubuf(ctx, tf_inner_vec, hs_ring_tess_offchip, vmem_offs_inner.first, oc_lds, vmem_offs_inner.second, 4, (1 << inner_comps) - 1, true, false);
9484 }
9485 }
9486
9487 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
9488 end_divergent_if(ctx, &ic_invocation_id_is_zero);
9489 }
9490
9491 static void emit_stream_output(isel_context *ctx,
9492 Temp const *so_buffers,
9493 Temp const *so_write_offset,
9494 const struct radv_stream_output *output)
9495 {
9496 unsigned num_comps = util_bitcount(output->component_mask);
9497 unsigned writemask = (1 << num_comps) - 1;
9498 unsigned loc = output->location;
9499 unsigned buf = output->buffer;
9500
9501 assert(num_comps && num_comps <= 4);
9502 if (!num_comps || num_comps > 4)
9503 return;
9504
9505 unsigned start = ffs(output->component_mask) - 1;
9506
9507 Temp out[4];
9508 bool all_undef = true;
9509 assert(ctx->stage == vertex_vs || ctx->stage == gs_copy_vs);
9510 for (unsigned i = 0; i < num_comps; i++) {
9511 out[i] = ctx->outputs.temps[loc * 4 + start + i];
9512 all_undef = all_undef && !out[i].id();
9513 }
9514 if (all_undef)
9515 return;
9516
9517 while (writemask) {
9518 int start, count;
9519 u_bit_scan_consecutive_range(&writemask, &start, &count);
9520 if (count == 3 && ctx->options->chip_class == GFX6) {
9521 /* GFX6 doesn't support storing vec3, split it. */
9522 writemask |= 1u << (start + 2);
9523 count = 2;
9524 }
9525
9526 unsigned offset = output->offset + start * 4;
9527
9528 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
9529 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
9530 for (int i = 0; i < count; ++i)
9531 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
9532 vec->definitions[0] = Definition(write_data);
9533 ctx->block->instructions.emplace_back(std::move(vec));
9534
9535 aco_opcode opcode;
9536 switch (count) {
9537 case 1:
9538 opcode = aco_opcode::buffer_store_dword;
9539 break;
9540 case 2:
9541 opcode = aco_opcode::buffer_store_dwordx2;
9542 break;
9543 case 3:
9544 opcode = aco_opcode::buffer_store_dwordx3;
9545 break;
9546 case 4:
9547 opcode = aco_opcode::buffer_store_dwordx4;
9548 break;
9549 default:
9550 unreachable("Unsupported dword count.");
9551 }
9552
9553 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
9554 store->operands[0] = Operand(so_buffers[buf]);
9555 store->operands[1] = Operand(so_write_offset[buf]);
9556 store->operands[2] = Operand((uint32_t) 0);
9557 store->operands[3] = Operand(write_data);
9558 if (offset > 4095) {
9559 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
9560 Builder bld(ctx->program, ctx->block);
9561 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
9562 } else {
9563 store->offset = offset;
9564 }
9565 store->offen = true;
9566 store->glc = true;
9567 store->dlc = false;
9568 store->slc = true;
9569 store->can_reorder = true;
9570 ctx->block->instructions.emplace_back(std::move(store));
9571 }
9572 }
9573
9574 static void emit_streamout(isel_context *ctx, unsigned stream)
9575 {
9576 Builder bld(ctx->program, ctx->block);
9577
9578 Temp so_buffers[4];
9579 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
9580 for (unsigned i = 0; i < 4; i++) {
9581 unsigned stride = ctx->program->info->so.strides[i];
9582 if (!stride)
9583 continue;
9584
9585 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
9586 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
9587 }
9588
9589 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
9590 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
9591
9592 Temp tid = emit_mbcnt(ctx, bld.def(v1));
9593
9594 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
9595
9596 if_context ic;
9597 begin_divergent_if_then(ctx, &ic, can_emit);
9598
9599 bld.reset(ctx->block);
9600
9601 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
9602
9603 Temp so_write_offset[4];
9604
9605 for (unsigned i = 0; i < 4; i++) {
9606 unsigned stride = ctx->program->info->so.strides[i];
9607 if (!stride)
9608 continue;
9609
9610 if (stride == 1) {
9611 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
9612 get_arg(ctx, ctx->args->streamout_write_idx),
9613 get_arg(ctx, ctx->args->streamout_offset[i]));
9614 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
9615
9616 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
9617 } else {
9618 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
9619 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
9620 get_arg(ctx, ctx->args->streamout_offset[i]));
9621 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
9622 }
9623 }
9624
9625 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
9626 struct radv_stream_output *output =
9627 &ctx->program->info->so.outputs[i];
9628 if (stream != output->stream)
9629 continue;
9630
9631 emit_stream_output(ctx, so_buffers, so_write_offset, output);
9632 }
9633
9634 begin_divergent_if_else(ctx, &ic);
9635 end_divergent_if(ctx, &ic);
9636 }
9637
9638 } /* end namespace */
9639
9640 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
9641 {
9642 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
9643 Builder bld(ctx->program, ctx->block);
9644 constexpr unsigned hs_idx = 1u;
9645 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
9646 get_arg(ctx, ctx->args->merged_wave_info),
9647 Operand((8u << 16) | (hs_idx * 8u)));
9648 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
9649
9650 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
9651
9652 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9653 get_arg(ctx, ctx->args->rel_auto_id),
9654 get_arg(ctx, ctx->args->ac.instance_id),
9655 ls_has_nonzero_hs_threads);
9656 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9657 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
9658 get_arg(ctx, ctx->args->rel_auto_id),
9659 ls_has_nonzero_hs_threads);
9660 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9661 get_arg(ctx, ctx->args->ac.tcs_patch_id),
9662 get_arg(ctx, ctx->args->ac.vertex_id),
9663 ls_has_nonzero_hs_threads);
9664
9665 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
9666 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
9667 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
9668 }
9669
9670 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
9671 {
9672 /* Split all arguments except for the first (ring_offsets) and the last
9673 * (exec) so that the dead channels don't stay live throughout the program.
9674 */
9675 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
9676 if (startpgm->definitions[i].regClass().size() > 1) {
9677 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
9678 startpgm->definitions[i].regClass().size());
9679 }
9680 }
9681 }
9682
9683 void handle_bc_optimize(isel_context *ctx)
9684 {
9685 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
9686 Builder bld(ctx->program, ctx->block);
9687 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
9688 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
9689 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
9690 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
9691 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
9692 if (uses_center && uses_centroid) {
9693 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
9694 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
9695
9696 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
9697 Temp new_coord[2];
9698 for (unsigned i = 0; i < 2; i++) {
9699 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
9700 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
9701 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9702 persp_centroid, persp_center, sel);
9703 }
9704 ctx->persp_centroid = bld.tmp(v2);
9705 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
9706 Operand(new_coord[0]), Operand(new_coord[1]));
9707 emit_split_vector(ctx, ctx->persp_centroid, 2);
9708 }
9709
9710 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
9711 Temp new_coord[2];
9712 for (unsigned i = 0; i < 2; i++) {
9713 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
9714 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
9715 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9716 linear_centroid, linear_center, sel);
9717 }
9718 ctx->linear_centroid = bld.tmp(v2);
9719 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
9720 Operand(new_coord[0]), Operand(new_coord[1]));
9721 emit_split_vector(ctx, ctx->linear_centroid, 2);
9722 }
9723 }
9724 }
9725
9726 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
9727 {
9728 Program *program = ctx->program;
9729
9730 unsigned float_controls = shader->info.float_controls_execution_mode;
9731
9732 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
9733 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
9734 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
9735 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
9736 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
9737
9738 program->next_fp_mode.must_flush_denorms32 =
9739 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
9740 program->next_fp_mode.must_flush_denorms16_64 =
9741 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
9742 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
9743
9744 program->next_fp_mode.care_about_round32 =
9745 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
9746
9747 program->next_fp_mode.care_about_round16_64 =
9748 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
9749 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
9750
9751 /* default to preserving fp16 and fp64 denorms, since it's free */
9752 if (program->next_fp_mode.must_flush_denorms16_64)
9753 program->next_fp_mode.denorm16_64 = 0;
9754 else
9755 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
9756
9757 /* preserving fp32 denorms is expensive, so only do it if asked */
9758 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
9759 program->next_fp_mode.denorm32 = fp_denorm_keep;
9760 else
9761 program->next_fp_mode.denorm32 = 0;
9762
9763 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
9764 program->next_fp_mode.round32 = fp_round_tz;
9765 else
9766 program->next_fp_mode.round32 = fp_round_ne;
9767
9768 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
9769 program->next_fp_mode.round16_64 = fp_round_tz;
9770 else
9771 program->next_fp_mode.round16_64 = fp_round_ne;
9772
9773 ctx->block->fp_mode = program->next_fp_mode;
9774 }
9775
9776 void cleanup_cfg(Program *program)
9777 {
9778 /* create linear_succs/logical_succs */
9779 for (Block& BB : program->blocks) {
9780 for (unsigned idx : BB.linear_preds)
9781 program->blocks[idx].linear_succs.emplace_back(BB.index);
9782 for (unsigned idx : BB.logical_preds)
9783 program->blocks[idx].logical_succs.emplace_back(BB.index);
9784 }
9785 }
9786
9787 void select_program(Program *program,
9788 unsigned shader_count,
9789 struct nir_shader *const *shaders,
9790 ac_shader_config* config,
9791 struct radv_shader_args *args)
9792 {
9793 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
9794 if_context ic_merged_wave_info;
9795
9796 for (unsigned i = 0; i < shader_count; i++) {
9797 nir_shader *nir = shaders[i];
9798 init_context(&ctx, nir);
9799
9800 setup_fp_mode(&ctx, nir);
9801
9802 if (!i) {
9803 /* needs to be after init_context() for FS */
9804 Pseudo_instruction *startpgm = add_startpgm(&ctx);
9805 append_logical_start(ctx.block);
9806
9807 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
9808 fix_ls_vgpr_init_bug(&ctx, startpgm);
9809
9810 split_arguments(&ctx, startpgm);
9811 }
9812
9813 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
9814 nir_function_impl *func = nir_shader_get_entrypoint(nir);
9815 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
9816 ((nir->info.stage == MESA_SHADER_VERTEX &&
9817 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
9818 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
9819 ctx.stage == tess_eval_geometry_gs));
9820
9821 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : (shader_count >= 2 && !empty_shader);
9822 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
9823 if (check_merged_wave_info) {
9824 Builder bld(ctx.program, ctx.block);
9825
9826 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
9827 Temp count = i == 0 ? get_arg(&ctx, args->merged_wave_info)
9828 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
9829 get_arg(&ctx, args->merged_wave_info), Operand(i * 8u));
9830
9831 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
9832 Temp cond;
9833
9834 if (ctx.program->wave_size == 64) {
9835 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
9836 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
9837 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
9838 } else {
9839 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
9840 cond = emit_extract_vector(&ctx, mask, 0, bld.lm);
9841 }
9842
9843 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
9844 }
9845
9846 if (i) {
9847 Builder bld(ctx.program, ctx.block);
9848
9849 bld.barrier(aco_opcode::p_memory_barrier_shared);
9850 bld.sopp(aco_opcode::s_barrier);
9851
9852 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
9853 ctx.gs_wave_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1, m0), bld.def(s1, scc), get_arg(&ctx, args->merged_wave_info), Operand((8u << 16) | 16u));
9854 }
9855 } else if (ctx.stage == geometry_gs)
9856 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
9857
9858 if (ctx.stage == fragment_fs)
9859 handle_bc_optimize(&ctx);
9860
9861 visit_cf_list(&ctx, &func->body);
9862
9863 if (ctx.program->info->so.num_outputs && (ctx.stage == vertex_vs || ctx.stage == tess_eval_vs))
9864 emit_streamout(&ctx, 0);
9865
9866 if (ctx.stage == vertex_vs || ctx.stage == tess_eval_vs) {
9867 create_vs_exports(&ctx);
9868 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
9869 Builder bld(ctx.program, ctx.block);
9870 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
9871 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
9872 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
9873 write_tcs_tess_factors(&ctx);
9874 }
9875
9876 if (ctx.stage == fragment_fs)
9877 create_fs_exports(&ctx);
9878
9879 if (endif_merged_wave_info) {
9880 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
9881 end_divergent_if(&ctx, &ic_merged_wave_info);
9882 }
9883
9884 ralloc_free(ctx.divergent_vals);
9885
9886 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
9887 /* Outputs of the previous stage are inputs to the next stage */
9888 ctx.inputs = ctx.outputs;
9889 ctx.outputs = shader_io_state();
9890 }
9891 }
9892
9893 program->config->float_mode = program->blocks[0].fp_mode.val;
9894
9895 append_logical_end(ctx.block);
9896 ctx.block->kind |= block_kind_uniform | block_kind_export_end;
9897 Builder bld(ctx.program, ctx.block);
9898 if (ctx.program->wb_smem_l1_on_end)
9899 bld.smem(aco_opcode::s_dcache_wb, false);
9900 bld.sopp(aco_opcode::s_endpgm);
9901
9902 cleanup_cfg(program);
9903 }
9904
9905 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
9906 ac_shader_config* config,
9907 struct radv_shader_args *args)
9908 {
9909 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
9910
9911 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
9912 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
9913 program->next_fp_mode.must_flush_denorms32 = false;
9914 program->next_fp_mode.must_flush_denorms16_64 = false;
9915 program->next_fp_mode.care_about_round32 = false;
9916 program->next_fp_mode.care_about_round16_64 = false;
9917 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
9918 program->next_fp_mode.denorm32 = 0;
9919 program->next_fp_mode.round32 = fp_round_ne;
9920 program->next_fp_mode.round16_64 = fp_round_ne;
9921 ctx.block->fp_mode = program->next_fp_mode;
9922
9923 add_startpgm(&ctx);
9924 append_logical_start(ctx.block);
9925
9926 Builder bld(ctx.program, ctx.block);
9927
9928 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
9929
9930 Operand stream_id(0u);
9931 if (args->shader_info->so.num_outputs)
9932 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
9933 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
9934
9935 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
9936
9937 std::stack<Block> endif_blocks;
9938
9939 for (unsigned stream = 0; stream < 4; stream++) {
9940 if (stream_id.isConstant() && stream != stream_id.constantValue())
9941 continue;
9942
9943 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
9944 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
9945 continue;
9946
9947 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
9948
9949 unsigned BB_if_idx = ctx.block->index;
9950 Block BB_endif = Block();
9951 if (!stream_id.isConstant()) {
9952 /* begin IF */
9953 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
9954 append_logical_end(ctx.block);
9955 ctx.block->kind |= block_kind_uniform;
9956 bld.branch(aco_opcode::p_cbranch_z, cond);
9957
9958 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
9959
9960 ctx.block = ctx.program->create_and_insert_block();
9961 add_edge(BB_if_idx, ctx.block);
9962 bld.reset(ctx.block);
9963 append_logical_start(ctx.block);
9964 }
9965
9966 unsigned offset = 0;
9967 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9968 if (args->shader_info->gs.output_streams[i] != stream)
9969 continue;
9970
9971 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
9972 unsigned length = util_last_bit(output_usage_mask);
9973 for (unsigned j = 0; j < length; ++j) {
9974 if (!(output_usage_mask & (1 << j)))
9975 continue;
9976
9977 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
9978 Temp voffset = vtx_offset;
9979 if (const_offset >= 4096u) {
9980 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
9981 const_offset %= 4096u;
9982 }
9983
9984 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
9985 mubuf->definitions[0] = bld.def(v1);
9986 mubuf->operands[0] = Operand(gsvs_ring);
9987 mubuf->operands[1] = Operand(voffset);
9988 mubuf->operands[2] = Operand(0u);
9989 mubuf->offen = true;
9990 mubuf->offset = const_offset;
9991 mubuf->glc = true;
9992 mubuf->slc = true;
9993 mubuf->dlc = args->options->chip_class >= GFX10;
9994 mubuf->barrier = barrier_none;
9995 mubuf->can_reorder = true;
9996
9997 ctx.outputs.mask[i] |= 1 << j;
9998 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
9999
10000 bld.insert(std::move(mubuf));
10001
10002 offset++;
10003 }
10004 }
10005
10006 if (args->shader_info->so.num_outputs) {
10007 emit_streamout(&ctx, stream);
10008 bld.reset(ctx.block);
10009 }
10010
10011 if (stream == 0) {
10012 create_vs_exports(&ctx);
10013 ctx.block->kind |= block_kind_export_end;
10014 }
10015
10016 if (!stream_id.isConstant()) {
10017 append_logical_end(ctx.block);
10018
10019 /* branch from then block to endif block */
10020 bld.branch(aco_opcode::p_branch);
10021 add_edge(ctx.block->index, &BB_endif);
10022 ctx.block->kind |= block_kind_uniform;
10023
10024 /* emit else block */
10025 ctx.block = ctx.program->create_and_insert_block();
10026 add_edge(BB_if_idx, ctx.block);
10027 bld.reset(ctx.block);
10028 append_logical_start(ctx.block);
10029
10030 endif_blocks.push(std::move(BB_endif));
10031 }
10032 }
10033
10034 while (!endif_blocks.empty()) {
10035 Block BB_endif = std::move(endif_blocks.top());
10036 endif_blocks.pop();
10037
10038 Block *BB_else = ctx.block;
10039
10040 append_logical_end(BB_else);
10041 /* branch from else block to endif block */
10042 bld.branch(aco_opcode::p_branch);
10043 add_edge(BB_else->index, &BB_endif);
10044 BB_else->kind |= block_kind_uniform;
10045
10046 /** emit endif merge block */
10047 ctx.block = program->insert_block(std::move(BB_endif));
10048 bld.reset(ctx.block);
10049 append_logical_start(ctx.block);
10050 }
10051
10052 program->config->float_mode = program->blocks[0].fp_mode.val;
10053
10054 append_logical_end(ctx.block);
10055 ctx.block->kind |= block_kind_uniform;
10056 bld.sopp(aco_opcode::s_endpgm);
10057
10058 cleanup_cfg(program);
10059 }
10060 }