aco: Fix combining DS additions in the optimizer.
[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 void 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 split_cnt = 0u, Temp dst = Temp())
2904 {
2905 Builder bld(ctx->program, ctx->block);
2906
2907 if (!dst.id())
2908 dst = bld.tmp(RegClass(reg_type, cnt * arr[0].size()));
2909
2910 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
2911 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
2912 instr->definitions[0] = Definition(dst);
2913
2914 for (unsigned i = 0; i < cnt; ++i) {
2915 assert(arr[i].size() == arr[0].size());
2916 allocated_vec[i] = arr[i];
2917 instr->operands[i] = Operand(arr[i]);
2918 }
2919
2920 bld.insert(std::move(instr));
2921
2922 if (split_cnt)
2923 emit_split_vector(ctx, dst, split_cnt);
2924 else
2925 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
2926
2927 return dst;
2928 }
2929
2930 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
2931 {
2932 if (const_offset >= 4096) {
2933 unsigned excess_const_offset = const_offset / 4096u * 4096u;
2934 const_offset %= 4096u;
2935
2936 if (!voffset.id())
2937 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
2938 else if (unlikely(voffset.regClass() == s1))
2939 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
2940 else if (likely(voffset.regClass() == v1))
2941 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
2942 else
2943 unreachable("Unsupported register class of voffset");
2944 }
2945
2946 return const_offset;
2947 }
2948
2949 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
2950 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
2951 {
2952 assert(vdata.id());
2953 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
2954 assert(vdata.size() >= 1 && vdata.size() <= 4);
2955
2956 Builder bld(ctx->program, ctx->block);
2957 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_store_dword + vdata.size() - 1);
2958 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
2959
2960 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
2961 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
2962 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
2963 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
2964 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
2965
2966 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
2967 }
2968
2969 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
2970 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
2971 bool allow_combining = true, bool reorder = true, bool slc = false)
2972 {
2973 Builder bld(ctx->program, ctx->block);
2974 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
2975 assert(write_mask);
2976
2977 if (elem_size_bytes == 8) {
2978 elem_size_bytes = 4;
2979 write_mask = widen_mask(write_mask, 2);
2980 }
2981
2982 while (write_mask) {
2983 int start = 0;
2984 int count = 0;
2985 u_bit_scan_consecutive_range(&write_mask, &start, &count);
2986 assert(count > 0);
2987 assert(start >= 0);
2988
2989 while (count > 0) {
2990 unsigned sub_count = allow_combining ? MIN2(count, 4) : 1;
2991 unsigned const_offset = (unsigned) start * elem_size_bytes + base_const_offset;
2992
2993 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
2994 if (unlikely(ctx->program->chip_class == GFX6 && sub_count == 3))
2995 sub_count = 2;
2996
2997 Temp elem = extract_subvector(ctx, src, start, sub_count, RegType::vgpr);
2998 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, elem, const_offset, reorder, slc);
2999
3000 count -= sub_count;
3001 start += sub_count;
3002 }
3003
3004 assert(count == 0);
3005 }
3006 }
3007
3008 Temp emit_single_mubuf_load(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset,
3009 unsigned const_offset, unsigned size_dwords, bool allow_reorder = true)
3010 {
3011 assert(size_dwords != 3 || ctx->program->chip_class != GFX6);
3012 assert(size_dwords >= 1 && size_dwords <= 4);
3013
3014 Builder bld(ctx->program, ctx->block);
3015 Temp vdata = bld.tmp(RegClass(RegType::vgpr, size_dwords));
3016 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_load_dword + size_dwords - 1);
3017 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3018
3019 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3020 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3021 Builder::Result r = bld.mubuf(op, Definition(vdata), Operand(descriptor), voffset_op, soffset_op, const_offset,
3022 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3023 /* disable_wqm */ false, /* glc */ true,
3024 /* dlc*/ ctx->program->chip_class >= GFX10, /* slc */ false);
3025
3026 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3027
3028 return vdata;
3029 }
3030
3031 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3032 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3033 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3034 {
3035 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3036 assert((num_components * elem_size_bytes / 4) == dst.size());
3037 assert(!!stride != allow_combining);
3038
3039 Builder bld(ctx->program, ctx->block);
3040 unsigned split_cnt = num_components;
3041
3042 if (elem_size_bytes == 8) {
3043 elem_size_bytes = 4;
3044 num_components *= 2;
3045 }
3046
3047 if (!stride)
3048 stride = elem_size_bytes;
3049
3050 unsigned load_size = 1;
3051 if (allow_combining) {
3052 if ((num_components % 4) == 0)
3053 load_size = 4;
3054 else if ((num_components % 3) == 0 && ctx->program->chip_class != GFX6)
3055 load_size = 3;
3056 else if ((num_components % 2) == 0)
3057 load_size = 2;
3058 }
3059
3060 unsigned num_loads = num_components / load_size;
3061 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
3062
3063 for (unsigned i = 0; i < num_loads; ++i) {
3064 unsigned const_offset = i * stride * load_size + base_const_offset;
3065 elems[i] = emit_single_mubuf_load(ctx, descriptor, voffset, soffset, const_offset, load_size, allow_reorder);
3066 }
3067
3068 create_vec_from_array(ctx, elems.data(), num_loads, RegType::vgpr, split_cnt, dst);
3069 }
3070
3071 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)
3072 {
3073 Builder bld(ctx->program, ctx->block);
3074 Temp offset = base_offset.first;
3075 unsigned const_offset = base_offset.second;
3076
3077 if (!nir_src_is_const(*off_src)) {
3078 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3079 Temp with_stride;
3080
3081 /* Calculate indirect offset with stride */
3082 if (likely(indirect_offset_arg.regClass() == v1))
3083 with_stride = bld.v_mul_imm(bld.def(v1), indirect_offset_arg, stride);
3084 else if (indirect_offset_arg.regClass() == s1)
3085 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3086 else
3087 unreachable("Unsupported register class of indirect offset");
3088
3089 /* Add to the supplied base offset */
3090 if (offset.id() == 0)
3091 offset = with_stride;
3092 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
3093 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
3094 else if (offset.size() == 1 && with_stride.size() == 1)
3095 offset = bld.vadd32(bld.def(v1), with_stride, offset);
3096 else
3097 unreachable("Unsupported register class of indirect offset");
3098 } else {
3099 unsigned const_offset_arg = nir_src_as_uint(*off_src);
3100 const_offset += const_offset_arg * stride;
3101 }
3102
3103 return std::make_pair(offset, const_offset);
3104 }
3105
3106 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
3107 {
3108 Builder bld(ctx->program, ctx->block);
3109 Temp offset;
3110
3111 if (off1.first.id() && off2.first.id()) {
3112 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
3113 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
3114 else if (off1.first.size() == 1 && off2.first.size() == 1)
3115 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
3116 else
3117 unreachable("Unsupported register class of indirect offset");
3118 } else {
3119 offset = off1.first.id() ? off1.first : off2.first;
3120 }
3121
3122 return std::make_pair(offset, off1.second + off2.second);
3123 }
3124
3125 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
3126 {
3127 Builder bld(ctx->program, ctx->block);
3128 unsigned const_offset = offs.second * multiplier;
3129
3130 if (!offs.first.id())
3131 return std::make_pair(offs.first, const_offset);
3132
3133 Temp offset = unlikely(offs.first.regClass() == s1)
3134 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
3135 : bld.v_mul_imm(bld.def(v1), offs.first, multiplier);
3136
3137 return std::make_pair(offset, const_offset);
3138 }
3139
3140 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
3141 {
3142 Builder bld(ctx->program, ctx->block);
3143
3144 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3145 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
3146 /* component is in bytes */
3147 const_offset += nir_intrinsic_component(instr) * component_stride;
3148
3149 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3150 nir_src *off_src = nir_get_io_offset_src(instr);
3151 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
3152 }
3153
3154 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
3155 {
3156 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
3157 }
3158
3159 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
3160 {
3161 Builder bld(ctx->program, ctx->block);
3162
3163 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
3164 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3165 unsigned write_mask = nir_intrinsic_write_mask(instr);
3166 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
3167
3168 if (ctx->stage == vertex_es) {
3169 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
3170 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
3171 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
3172 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
3173 } else {
3174 Temp lds_base;
3175
3176 if (ctx->stage == vertex_geometry_gs) {
3177 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
3178 unsigned itemsize = ctx->program->info->vs.es_info.esgs_itemsize;
3179 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
3180 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));
3181 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
3182 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
3183 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
3184 } else {
3185 unreachable("Invalid LS or ES stage");
3186 }
3187
3188 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
3189 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3190 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
3191
3192 }
3193 }
3194
3195 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
3196 {
3197 if (ctx->stage == vertex_vs ||
3198 ctx->stage == fragment_fs ||
3199 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
3200 unsigned write_mask = nir_intrinsic_write_mask(instr);
3201 unsigned component = nir_intrinsic_component(instr);
3202 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3203 unsigned idx = nir_intrinsic_base(instr) + component;
3204
3205 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
3206 if (off_instr->type != nir_instr_type_load_const) {
3207 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3208 nir_print_instr(off_instr, stderr);
3209 fprintf(stderr, "\n");
3210 }
3211 idx += nir_instr_as_load_const(off_instr)->value[0].u32 * 4u;
3212
3213 if (instr->src[0].ssa->bit_size == 64)
3214 write_mask = widen_mask(write_mask, 2);
3215
3216 for (unsigned i = 0; i < 8; ++i) {
3217 if (write_mask & (1 << i)) {
3218 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
3219 ctx->outputs.outputs[idx / 4u][idx % 4u] = emit_extract_vector(ctx, src, i, v1);
3220 }
3221 idx++;
3222 }
3223 } else if (ctx->stage == vertex_es ||
3224 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX)) {
3225 visit_store_ls_or_es_output(ctx, instr);
3226 } else {
3227 unreachable("Shader stage not implemented");
3228 }
3229 }
3230
3231 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
3232 {
3233 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
3234 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
3235
3236 Builder bld(ctx->program, ctx->block);
3237 Temp tmp = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
3238 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), tmp, idx, component);
3239 }
3240
3241 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
3242 {
3243 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
3244 for (unsigned i = 0; i < num_components; i++)
3245 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
3246 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
3247 assert(num_components == 4);
3248 Builder bld(ctx->program, ctx->block);
3249 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
3250 }
3251
3252 for (Operand& op : vec->operands)
3253 op = op.isUndefined() ? Operand(0u) : op;
3254
3255 vec->definitions[0] = Definition(dst);
3256 ctx->block->instructions.emplace_back(std::move(vec));
3257 emit_split_vector(ctx, dst, num_components);
3258 return;
3259 }
3260
3261 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
3262 {
3263 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3264 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
3265 unsigned idx = nir_intrinsic_base(instr);
3266 unsigned component = nir_intrinsic_component(instr);
3267 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3268
3269 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
3270 if (offset) {
3271 assert(offset->u32 == 0);
3272 } else {
3273 /* the lower 15bit of the prim_mask contain the offset into LDS
3274 * while the upper bits contain the number of prims */
3275 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
3276 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3277 Builder bld(ctx->program, ctx->block);
3278 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3279 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3280 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3281 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3282 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3283 }
3284
3285 if (instr->dest.ssa.num_components == 1) {
3286 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
3287 } else {
3288 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
3289 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
3290 {
3291 Temp tmp = {ctx->program->allocateId(), v1};
3292 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
3293 vec->operands[i] = Operand(tmp);
3294 }
3295 vec->definitions[0] = Definition(dst);
3296 ctx->block->instructions.emplace_back(std::move(vec));
3297 }
3298 }
3299
3300 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
3301 unsigned offset, unsigned stride, unsigned channels)
3302 {
3303 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
3304 if (vtx_info->chan_byte_size != 4 && channels == 3)
3305 return false;
3306 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
3307 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
3308 }
3309
3310 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
3311 unsigned offset, unsigned stride, unsigned *channels)
3312 {
3313 if (!vtx_info->chan_byte_size) {
3314 *channels = vtx_info->num_channels;
3315 return vtx_info->chan_format;
3316 }
3317
3318 unsigned num_channels = *channels;
3319 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
3320 unsigned new_channels = num_channels + 1;
3321 /* first, assume more loads is worse and try using a larger data format */
3322 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
3323 new_channels++;
3324 /* don't make the attribute potentially out-of-bounds */
3325 if (offset + new_channels * vtx_info->chan_byte_size > stride)
3326 new_channels = 5;
3327 }
3328
3329 if (new_channels == 5) {
3330 /* then try decreasing load size (at the cost of more loads) */
3331 new_channels = *channels;
3332 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
3333 new_channels--;
3334 }
3335
3336 if (new_channels < *channels)
3337 *channels = new_channels;
3338 num_channels = new_channels;
3339 }
3340
3341 switch (vtx_info->chan_format) {
3342 case V_008F0C_BUF_DATA_FORMAT_8:
3343 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
3344 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
3345 case V_008F0C_BUF_DATA_FORMAT_16:
3346 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
3347 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
3348 case V_008F0C_BUF_DATA_FORMAT_32:
3349 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
3350 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
3351 }
3352 unreachable("shouldn't reach here");
3353 return V_008F0C_BUF_DATA_FORMAT_INVALID;
3354 }
3355
3356 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3357 * so we may need to fix it up. */
3358 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
3359 {
3360 Builder bld(ctx->program, ctx->block);
3361
3362 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
3363 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
3364
3365 /* For the integer-like cases, do a natural sign extension.
3366 *
3367 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3368 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3369 * exponent.
3370 */
3371 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
3372 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
3373
3374 /* Convert back to the right type. */
3375 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
3376 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
3377 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
3378 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
3379 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
3380 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
3381 }
3382
3383 return alpha;
3384 }
3385
3386 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
3387 {
3388 Builder bld(ctx->program, ctx->block);
3389 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3390 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
3391
3392 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
3393 if (off_instr->type != nir_instr_type_load_const) {
3394 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3395 nir_print_instr(off_instr, stderr);
3396 fprintf(stderr, "\n");
3397 }
3398 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
3399
3400 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
3401
3402 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
3403 unsigned component = nir_intrinsic_component(instr);
3404 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
3405 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
3406 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
3407 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
3408
3409 unsigned dfmt = attrib_format & 0xf;
3410 unsigned nfmt = (attrib_format >> 4) & 0x7;
3411 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
3412
3413 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
3414 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
3415 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
3416 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
3417 if (post_shuffle)
3418 num_channels = MAX2(num_channels, 3);
3419
3420 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
3421 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
3422
3423 Temp index;
3424 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
3425 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
3426 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
3427 if (divisor) {
3428 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
3429 if (divisor != 1) {
3430 Temp divided = bld.tmp(v1);
3431 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
3432 index = bld.vadd32(bld.def(v1), start_instance, divided);
3433 } else {
3434 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
3435 }
3436 } else {
3437 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
3438 }
3439 } else {
3440 index = bld.vadd32(bld.def(v1),
3441 get_arg(ctx, ctx->args->ac.base_vertex),
3442 get_arg(ctx, ctx->args->ac.vertex_id));
3443 }
3444
3445 Temp channels[num_channels];
3446 unsigned channel_start = 0;
3447 bool direct_fetch = false;
3448
3449 /* skip unused channels at the start */
3450 if (vtx_info->chan_byte_size && !post_shuffle) {
3451 channel_start = ffs(mask) - 1;
3452 for (unsigned i = 0; i < channel_start; i++)
3453 channels[i] = Temp(0, s1);
3454 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
3455 num_channels = 3 - (ffs(mask) - 1);
3456 }
3457
3458 /* load channels */
3459 while (channel_start < num_channels) {
3460 unsigned fetch_size = num_channels - channel_start;
3461 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
3462 bool expanded = false;
3463
3464 /* use MUBUF when possible to avoid possible alignment issues */
3465 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
3466 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
3467 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
3468 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
3469 vtx_info->chan_byte_size == 4;
3470 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
3471 if (!use_mubuf) {
3472 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
3473 } else {
3474 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
3475 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
3476 fetch_size = 4;
3477 expanded = true;
3478 }
3479 }
3480
3481 Temp fetch_index = index;
3482 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
3483 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
3484 fetch_offset = fetch_offset % attrib_stride;
3485 }
3486
3487 Operand soffset(0u);
3488 if (fetch_offset >= 4096) {
3489 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
3490 fetch_offset %= 4096;
3491 }
3492
3493 aco_opcode opcode;
3494 switch (fetch_size) {
3495 case 1:
3496 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
3497 break;
3498 case 2:
3499 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
3500 break;
3501 case 3:
3502 assert(ctx->options->chip_class >= GFX7 ||
3503 (!use_mubuf && ctx->options->chip_class == GFX6));
3504 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
3505 break;
3506 case 4:
3507 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
3508 break;
3509 default:
3510 unreachable("Unimplemented load_input vector size");
3511 }
3512
3513 Temp fetch_dst;
3514 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
3515 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
3516 num_channels <= 3)) {
3517 direct_fetch = true;
3518 fetch_dst = dst;
3519 } else {
3520 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
3521 }
3522
3523 if (use_mubuf) {
3524 Instruction *mubuf = bld.mubuf(opcode,
3525 Definition(fetch_dst), list, fetch_index, soffset,
3526 fetch_offset, false, true).instr;
3527 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
3528 } else {
3529 Instruction *mtbuf = bld.mtbuf(opcode,
3530 Definition(fetch_dst), list, fetch_index, soffset,
3531 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
3532 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
3533 }
3534
3535 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
3536
3537 if (fetch_size == 1) {
3538 channels[channel_start] = fetch_dst;
3539 } else {
3540 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
3541 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
3542 }
3543
3544 channel_start += fetch_size;
3545 }
3546
3547 if (!direct_fetch) {
3548 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
3549 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
3550
3551 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
3552 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
3553 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
3554
3555 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3556 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
3557 unsigned num_temp = 0;
3558 for (unsigned i = 0; i < dst.size(); i++) {
3559 unsigned idx = i + component;
3560 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
3561 Temp channel = channels[swizzle[idx]];
3562 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
3563 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
3564 vec->operands[i] = Operand(channel);
3565
3566 num_temp++;
3567 elems[i] = channel;
3568 } else if (is_float && idx == 3) {
3569 vec->operands[i] = Operand(0x3f800000u);
3570 } else if (!is_float && idx == 3) {
3571 vec->operands[i] = Operand(1u);
3572 } else {
3573 vec->operands[i] = Operand(0u);
3574 }
3575 }
3576 vec->definitions[0] = Definition(dst);
3577 ctx->block->instructions.emplace_back(std::move(vec));
3578 emit_split_vector(ctx, dst, dst.size());
3579
3580 if (num_temp == dst.size())
3581 ctx->allocated_vec.emplace(dst.id(), elems);
3582 }
3583 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
3584 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
3585 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
3586 if (off_instr->type != nir_instr_type_load_const ||
3587 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
3588 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
3589 nir_print_instr(off_instr, stderr);
3590 fprintf(stderr, "\n");
3591 }
3592
3593 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3594 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
3595 if (offset) {
3596 assert(offset->u32 == 0);
3597 } else {
3598 /* the lower 15bit of the prim_mask contain the offset into LDS
3599 * while the upper bits contain the number of prims */
3600 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
3601 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3602 Builder bld(ctx->program, ctx->block);
3603 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3604 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3605 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3606 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3607 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3608 }
3609
3610 unsigned idx = nir_intrinsic_base(instr);
3611 unsigned component = nir_intrinsic_component(instr);
3612 unsigned vertex_id = 2; /* P0 */
3613
3614 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
3615 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
3616 switch (src0->u32) {
3617 case 0:
3618 vertex_id = 2; /* P0 */
3619 break;
3620 case 1:
3621 vertex_id = 0; /* P10 */
3622 break;
3623 case 2:
3624 vertex_id = 1; /* P20 */
3625 break;
3626 default:
3627 unreachable("invalid vertex index");
3628 }
3629 }
3630
3631 if (dst.size() == 1) {
3632 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
3633 } else {
3634 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3635 for (unsigned i = 0; i < dst.size(); i++)
3636 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
3637 vec->definitions[0] = Definition(dst);
3638 bld.insert(std::move(vec));
3639 }
3640
3641 } else {
3642 unreachable("Shader stage not implemented");
3643 }
3644 }
3645
3646 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
3647 {
3648 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
3649
3650 Builder bld(ctx->program, ctx->block);
3651 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
3652 Temp vertex_offset;
3653
3654 if (!nir_src_is_const(*vertex_src)) {
3655 /* better code could be created, but this case probably doesn't happen
3656 * much in practice */
3657 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
3658 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
3659 Temp elem;
3660
3661 if (ctx->stage == vertex_geometry_gs) {
3662 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
3663 if (i % 2u)
3664 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
3665 } else {
3666 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
3667 }
3668
3669 if (vertex_offset.id()) {
3670 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
3671 Operand(i), indirect_vertex);
3672 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
3673 } else {
3674 vertex_offset = elem;
3675 }
3676 }
3677
3678 if (ctx->stage == vertex_geometry_gs)
3679 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
3680 } else {
3681 unsigned vertex = nir_src_as_uint(*vertex_src);
3682 if (ctx->stage == vertex_geometry_gs)
3683 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
3684 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
3685 Operand((vertex % 2u) * 16u), Operand(16u));
3686 else
3687 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
3688 }
3689
3690 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
3691 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
3692 return offset_mul(ctx, offs, 4u);
3693 }
3694
3695 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
3696 {
3697 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
3698
3699 Builder bld(ctx->program, ctx->block);
3700 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3701 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
3702
3703 if (ctx->stage == geometry_gs) {
3704 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
3705 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
3706 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);
3707 } else if (ctx->stage == vertex_geometry_gs) {
3708 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
3709 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3710 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
3711 } else {
3712 unreachable("Unsupported GS stage.");
3713 }
3714 }
3715
3716 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
3717 {
3718 switch (ctx->shader->info.stage) {
3719 case MESA_SHADER_GEOMETRY:
3720 visit_load_gs_per_vertex_input(ctx, instr);
3721 break;
3722 default:
3723 unreachable("Unimplemented shader stage");
3724 }
3725 }
3726
3727 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
3728 {
3729 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
3730
3731 Builder bld(ctx->program, ctx->block);
3732 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3733
3734 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
3735 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
3736 Operand tes_w(0u);
3737
3738 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
3739 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
3740 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
3741 tes_w = Operand(tmp);
3742 }
3743
3744 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
3745 emit_split_vector(ctx, tess_coord, 3);
3746 }
3747
3748 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
3749 {
3750 if (ctx->program->info->need_indirect_descriptor_sets) {
3751 Builder bld(ctx->program, ctx->block);
3752 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
3753 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
3754 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
3755 }
3756
3757 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
3758 }
3759
3760
3761 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
3762 {
3763 Builder bld(ctx->program, ctx->block);
3764 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
3765 if (!ctx->divergent_vals[instr->dest.ssa.index])
3766 index = bld.as_uniform(index);
3767 unsigned desc_set = nir_intrinsic_desc_set(instr);
3768 unsigned binding = nir_intrinsic_binding(instr);
3769
3770 Temp desc_ptr;
3771 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
3772 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
3773 unsigned offset = layout->binding[binding].offset;
3774 unsigned stride;
3775 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
3776 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
3777 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
3778 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
3779 offset = pipeline_layout->push_constant_size + 16 * idx;
3780 stride = 16;
3781 } else {
3782 desc_ptr = load_desc_ptr(ctx, desc_set);
3783 stride = layout->binding[binding].size;
3784 }
3785
3786 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
3787 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
3788 if (stride != 1) {
3789 if (nir_const_index) {
3790 const_index = const_index * stride;
3791 } else if (index.type() == RegType::vgpr) {
3792 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
3793 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
3794 } else {
3795 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
3796 }
3797 }
3798 if (offset) {
3799 if (nir_const_index) {
3800 const_index = const_index + offset;
3801 } else if (index.type() == RegType::vgpr) {
3802 index = bld.vadd32(bld.def(v1), Operand(offset), index);
3803 } else {
3804 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
3805 }
3806 }
3807
3808 if (nir_const_index && const_index == 0) {
3809 index = desc_ptr;
3810 } else if (index.type() == RegType::vgpr) {
3811 index = bld.vadd32(bld.def(v1),
3812 nir_const_index ? Operand(const_index) : Operand(index),
3813 Operand(desc_ptr));
3814 } else {
3815 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3816 nir_const_index ? Operand(const_index) : Operand(index),
3817 Operand(desc_ptr));
3818 }
3819
3820 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
3821 }
3822
3823 void load_buffer(isel_context *ctx, unsigned num_components, Temp dst,
3824 Temp rsrc, Temp offset, bool glc=false, bool readonly=true)
3825 {
3826 Builder bld(ctx->program, ctx->block);
3827
3828 unsigned num_bytes = dst.size() * 4;
3829 bool dlc = glc && ctx->options->chip_class >= GFX10;
3830
3831 aco_opcode op;
3832 if (dst.type() == RegType::vgpr || (ctx->options->chip_class < GFX8 && !readonly)) {
3833 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3834 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3835 unsigned const_offset = 0;
3836
3837 Temp lower = Temp();
3838 if (num_bytes > 16) {
3839 assert(num_components == 3 || num_components == 4);
3840 op = aco_opcode::buffer_load_dwordx4;
3841 lower = bld.tmp(v4);
3842 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3843 mubuf->definitions[0] = Definition(lower);
3844 mubuf->operands[0] = Operand(rsrc);
3845 mubuf->operands[1] = vaddr;
3846 mubuf->operands[2] = soffset;
3847 mubuf->offen = (offset.type() == RegType::vgpr);
3848 mubuf->glc = glc;
3849 mubuf->dlc = dlc;
3850 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
3851 mubuf->can_reorder = readonly;
3852 bld.insert(std::move(mubuf));
3853 emit_split_vector(ctx, lower, 2);
3854 num_bytes -= 16;
3855 const_offset = 16;
3856 } else if (num_bytes == 12 && ctx->options->chip_class == GFX6) {
3857 /* GFX6 doesn't support loading vec3, expand to vec4. */
3858 num_bytes = 16;
3859 }
3860
3861 switch (num_bytes) {
3862 case 4:
3863 op = aco_opcode::buffer_load_dword;
3864 break;
3865 case 8:
3866 op = aco_opcode::buffer_load_dwordx2;
3867 break;
3868 case 12:
3869 assert(ctx->options->chip_class > GFX6);
3870 op = aco_opcode::buffer_load_dwordx3;
3871 break;
3872 case 16:
3873 op = aco_opcode::buffer_load_dwordx4;
3874 break;
3875 default:
3876 unreachable("Load SSBO not implemented for this size.");
3877 }
3878 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3879 mubuf->operands[0] = Operand(rsrc);
3880 mubuf->operands[1] = vaddr;
3881 mubuf->operands[2] = soffset;
3882 mubuf->offen = (offset.type() == RegType::vgpr);
3883 mubuf->glc = glc;
3884 mubuf->dlc = dlc;
3885 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
3886 mubuf->can_reorder = readonly;
3887 mubuf->offset = const_offset;
3888 aco_ptr<Instruction> instr = std::move(mubuf);
3889
3890 if (dst.size() > 4) {
3891 assert(lower != Temp());
3892 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
3893 instr->definitions[0] = Definition(upper);
3894 bld.insert(std::move(instr));
3895 if (dst.size() == 8)
3896 emit_split_vector(ctx, upper, 2);
3897 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
3898 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
3899 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
3900 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
3901 if (dst.size() == 8)
3902 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
3903 } else if (dst.size() == 3 && ctx->options->chip_class == GFX6) {
3904 Temp vec = bld.tmp(v4);
3905 instr->definitions[0] = Definition(vec);
3906 bld.insert(std::move(instr));
3907 emit_split_vector(ctx, vec, 4);
3908
3909 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
3910 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
3911 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
3912 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
3913 }
3914
3915 if (dst.type() == RegType::sgpr) {
3916 Temp vec = bld.tmp(RegType::vgpr, dst.size());
3917 instr->definitions[0] = Definition(vec);
3918 bld.insert(std::move(instr));
3919 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
3920 } else {
3921 instr->definitions[0] = Definition(dst);
3922 bld.insert(std::move(instr));
3923 emit_split_vector(ctx, dst, num_components);
3924 }
3925 } else {
3926 switch (num_bytes) {
3927 case 4:
3928 op = aco_opcode::s_buffer_load_dword;
3929 break;
3930 case 8:
3931 op = aco_opcode::s_buffer_load_dwordx2;
3932 break;
3933 case 12:
3934 case 16:
3935 op = aco_opcode::s_buffer_load_dwordx4;
3936 break;
3937 case 24:
3938 case 32:
3939 op = aco_opcode::s_buffer_load_dwordx8;
3940 break;
3941 default:
3942 unreachable("Load SSBO not implemented for this size.");
3943 }
3944 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3945 load->operands[0] = Operand(rsrc);
3946 load->operands[1] = Operand(bld.as_uniform(offset));
3947 assert(load->operands[1].getTemp().type() == RegType::sgpr);
3948 load->definitions[0] = Definition(dst);
3949 load->glc = glc;
3950 load->dlc = dlc;
3951 load->barrier = readonly ? barrier_none : barrier_buffer;
3952 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3953 assert(ctx->options->chip_class >= GFX8 || !glc);
3954
3955 /* trim vector */
3956 if (dst.size() == 3) {
3957 Temp vec = bld.tmp(s4);
3958 load->definitions[0] = Definition(vec);
3959 bld.insert(std::move(load));
3960 emit_split_vector(ctx, vec, 4);
3961
3962 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3963 emit_extract_vector(ctx, vec, 0, s1),
3964 emit_extract_vector(ctx, vec, 1, s1),
3965 emit_extract_vector(ctx, vec, 2, s1));
3966 } else if (dst.size() == 6) {
3967 Temp vec = bld.tmp(s8);
3968 load->definitions[0] = Definition(vec);
3969 bld.insert(std::move(load));
3970 emit_split_vector(ctx, vec, 4);
3971
3972 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
3973 emit_extract_vector(ctx, vec, 0, s2),
3974 emit_extract_vector(ctx, vec, 1, s2),
3975 emit_extract_vector(ctx, vec, 2, s2));
3976 } else {
3977 bld.insert(std::move(load));
3978 }
3979 emit_split_vector(ctx, dst, num_components);
3980 }
3981 }
3982
3983 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
3984 {
3985 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3986 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
3987
3988 Builder bld(ctx->program, ctx->block);
3989
3990 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
3991 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
3992 unsigned binding = nir_intrinsic_binding(idx_instr);
3993 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
3994
3995 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
3996 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
3997 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
3998 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
3999 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4000 if (ctx->options->chip_class >= GFX10) {
4001 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4002 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4003 S_008F0C_RESOURCE_LEVEL(1);
4004 } else {
4005 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4006 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4007 }
4008 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
4009 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
4010 Operand(0xFFFFFFFFu),
4011 Operand(desc_type));
4012 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4013 rsrc, upper_dwords);
4014 } else {
4015 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
4016 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4017 }
4018
4019 load_buffer(ctx, instr->num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa));
4020 }
4021
4022 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4023 {
4024 Builder bld(ctx->program, ctx->block);
4025 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4026
4027 unsigned offset = nir_intrinsic_base(instr);
4028 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
4029 if (index_cv && instr->dest.ssa.bit_size == 32) {
4030
4031 unsigned count = instr->dest.ssa.num_components;
4032 unsigned start = (offset + index_cv->u32) / 4u;
4033 start -= ctx->args->ac.base_inline_push_consts;
4034 if (start + count <= ctx->args->ac.num_inline_push_consts) {
4035 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4036 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4037 for (unsigned i = 0; i < count; ++i) {
4038 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
4039 vec->operands[i] = Operand{elems[i]};
4040 }
4041 vec->definitions[0] = Definition(dst);
4042 ctx->block->instructions.emplace_back(std::move(vec));
4043 ctx->allocated_vec.emplace(dst.id(), elems);
4044 return;
4045 }
4046 }
4047
4048 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
4049 if (offset != 0) // TODO check if index != 0 as well
4050 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
4051 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
4052 Temp vec = dst;
4053 bool trim = false;
4054 aco_opcode op;
4055
4056 switch (dst.size()) {
4057 case 1:
4058 op = aco_opcode::s_load_dword;
4059 break;
4060 case 2:
4061 op = aco_opcode::s_load_dwordx2;
4062 break;
4063 case 3:
4064 vec = bld.tmp(s4);
4065 trim = true;
4066 case 4:
4067 op = aco_opcode::s_load_dwordx4;
4068 break;
4069 case 6:
4070 vec = bld.tmp(s8);
4071 trim = true;
4072 case 8:
4073 op = aco_opcode::s_load_dwordx8;
4074 break;
4075 default:
4076 unreachable("unimplemented or forbidden load_push_constant.");
4077 }
4078
4079 bld.smem(op, Definition(vec), ptr, index);
4080
4081 if (trim) {
4082 emit_split_vector(ctx, vec, 4);
4083 RegClass rc = dst.size() == 3 ? s1 : s2;
4084 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4085 emit_extract_vector(ctx, vec, 0, rc),
4086 emit_extract_vector(ctx, vec, 1, rc),
4087 emit_extract_vector(ctx, vec, 2, rc));
4088
4089 }
4090 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4091 }
4092
4093 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4094 {
4095 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4096
4097 Builder bld(ctx->program, ctx->block);
4098
4099 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4100 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4101 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4102 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4103 if (ctx->options->chip_class >= GFX10) {
4104 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4105 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4106 S_008F0C_RESOURCE_LEVEL(1);
4107 } else {
4108 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4109 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4110 }
4111
4112 unsigned base = nir_intrinsic_base(instr);
4113 unsigned range = nir_intrinsic_range(instr);
4114
4115 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
4116 if (base && offset.type() == RegType::sgpr)
4117 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
4118 else if (base && offset.type() == RegType::vgpr)
4119 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
4120
4121 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4122 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
4123 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
4124 Operand(desc_type));
4125
4126 load_buffer(ctx, instr->num_components, dst, rsrc, offset);
4127 }
4128
4129 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
4130 {
4131 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
4132 ctx->cf_info.exec_potentially_empty_discard = true;
4133
4134 ctx->program->needs_exact = true;
4135
4136 // TODO: optimize uniform conditions
4137 Builder bld(ctx->program, ctx->block);
4138 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4139 assert(src.regClass() == bld.lm);
4140 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
4141 bld.pseudo(aco_opcode::p_discard_if, src);
4142 ctx->block->kind |= block_kind_uses_discard_if;
4143 return;
4144 }
4145
4146 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
4147 {
4148 Builder bld(ctx->program, ctx->block);
4149
4150 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
4151 ctx->cf_info.exec_potentially_empty_discard = true;
4152
4153 bool divergent = ctx->cf_info.parent_if.is_divergent ||
4154 ctx->cf_info.parent_loop.has_divergent_continue;
4155
4156 if (ctx->block->loop_nest_depth &&
4157 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
4158 /* we handle discards the same way as jump instructions */
4159 append_logical_end(ctx->block);
4160
4161 /* in loops, discard behaves like break */
4162 Block *linear_target = ctx->cf_info.parent_loop.exit;
4163 ctx->block->kind |= block_kind_discard;
4164
4165 if (!divergent) {
4166 /* uniform discard - loop ends here */
4167 assert(nir_instr_is_last(&instr->instr));
4168 ctx->block->kind |= block_kind_uniform;
4169 ctx->cf_info.has_branch = true;
4170 bld.branch(aco_opcode::p_branch);
4171 add_linear_edge(ctx->block->index, linear_target);
4172 return;
4173 }
4174
4175 /* we add a break right behind the discard() instructions */
4176 ctx->block->kind |= block_kind_break;
4177 unsigned idx = ctx->block->index;
4178
4179 /* remove critical edges from linear CFG */
4180 bld.branch(aco_opcode::p_branch);
4181 Block* break_block = ctx->program->create_and_insert_block();
4182 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
4183 break_block->kind |= block_kind_uniform;
4184 add_linear_edge(idx, break_block);
4185 add_linear_edge(break_block->index, linear_target);
4186 bld.reset(break_block);
4187 bld.branch(aco_opcode::p_branch);
4188
4189 Block* continue_block = ctx->program->create_and_insert_block();
4190 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
4191 add_linear_edge(idx, continue_block);
4192 append_logical_start(continue_block);
4193 ctx->block = continue_block;
4194
4195 return;
4196 }
4197
4198 /* it can currently happen that NIR doesn't remove the unreachable code */
4199 if (!nir_instr_is_last(&instr->instr)) {
4200 ctx->program->needs_exact = true;
4201 /* save exec somewhere temporarily so that it doesn't get
4202 * overwritten before the discard from outer exec masks */
4203 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
4204 bld.pseudo(aco_opcode::p_discard_if, cond);
4205 ctx->block->kind |= block_kind_uses_discard_if;
4206 return;
4207 }
4208
4209 /* This condition is incorrect for uniformly branched discards in a loop
4210 * predicated by a divergent condition, but the above code catches that case
4211 * and the discard would end up turning into a discard_if.
4212 * For example:
4213 * if (divergent) {
4214 * while (...) {
4215 * if (uniform) {
4216 * discard;
4217 * }
4218 * }
4219 * }
4220 */
4221 if (!ctx->cf_info.parent_if.is_divergent) {
4222 /* program just ends here */
4223 ctx->block->kind |= block_kind_uniform;
4224 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
4225 0 /* enabled mask */, 9 /* dest */,
4226 false /* compressed */, true/* done */, true /* valid mask */);
4227 bld.sopp(aco_opcode::s_endpgm);
4228 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
4229 } else {
4230 ctx->block->kind |= block_kind_discard;
4231 /* branch and linear edge is added by visit_if() */
4232 }
4233 }
4234
4235 enum aco_descriptor_type {
4236 ACO_DESC_IMAGE,
4237 ACO_DESC_FMASK,
4238 ACO_DESC_SAMPLER,
4239 ACO_DESC_BUFFER,
4240 ACO_DESC_PLANE_0,
4241 ACO_DESC_PLANE_1,
4242 ACO_DESC_PLANE_2,
4243 };
4244
4245 static bool
4246 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
4247 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
4248 return false;
4249 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
4250 return dim == ac_image_cube ||
4251 dim == ac_image_1darray ||
4252 dim == ac_image_2darray ||
4253 dim == ac_image_2darraymsaa;
4254 }
4255
4256 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
4257 enum aco_descriptor_type desc_type,
4258 const nir_tex_instr *tex_instr, bool image, bool write)
4259 {
4260 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
4261 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
4262 if (it != ctx->tex_desc.end())
4263 return it->second;
4264 */
4265 Temp index = Temp();
4266 bool index_set = false;
4267 unsigned constant_index = 0;
4268 unsigned descriptor_set;
4269 unsigned base_index;
4270 Builder bld(ctx->program, ctx->block);
4271
4272 if (!deref_instr) {
4273 assert(tex_instr && !image);
4274 descriptor_set = 0;
4275 base_index = tex_instr->sampler_index;
4276 } else {
4277 while(deref_instr->deref_type != nir_deref_type_var) {
4278 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
4279 if (!array_size)
4280 array_size = 1;
4281
4282 assert(deref_instr->deref_type == nir_deref_type_array);
4283 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
4284 if (const_value) {
4285 constant_index += array_size * const_value->u32;
4286 } else {
4287 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
4288 if (indirect.type() == RegType::vgpr)
4289 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
4290
4291 if (array_size != 1)
4292 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
4293
4294 if (!index_set) {
4295 index = indirect;
4296 index_set = true;
4297 } else {
4298 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
4299 }
4300 }
4301
4302 deref_instr = nir_src_as_deref(deref_instr->parent);
4303 }
4304 descriptor_set = deref_instr->var->data.descriptor_set;
4305 base_index = deref_instr->var->data.binding;
4306 }
4307
4308 Temp list = load_desc_ptr(ctx, descriptor_set);
4309 list = convert_pointer_to_64_bit(ctx, list);
4310
4311 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
4312 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
4313 unsigned offset = binding->offset;
4314 unsigned stride = binding->size;
4315 aco_opcode opcode;
4316 RegClass type;
4317
4318 assert(base_index < layout->binding_count);
4319
4320 switch (desc_type) {
4321 case ACO_DESC_IMAGE:
4322 type = s8;
4323 opcode = aco_opcode::s_load_dwordx8;
4324 break;
4325 case ACO_DESC_FMASK:
4326 type = s8;
4327 opcode = aco_opcode::s_load_dwordx8;
4328 offset += 32;
4329 break;
4330 case ACO_DESC_SAMPLER:
4331 type = s4;
4332 opcode = aco_opcode::s_load_dwordx4;
4333 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
4334 offset += radv_combined_image_descriptor_sampler_offset(binding);
4335 break;
4336 case ACO_DESC_BUFFER:
4337 type = s4;
4338 opcode = aco_opcode::s_load_dwordx4;
4339 break;
4340 case ACO_DESC_PLANE_0:
4341 case ACO_DESC_PLANE_1:
4342 type = s8;
4343 opcode = aco_opcode::s_load_dwordx8;
4344 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
4345 break;
4346 case ACO_DESC_PLANE_2:
4347 type = s4;
4348 opcode = aco_opcode::s_load_dwordx4;
4349 offset += 64;
4350 break;
4351 default:
4352 unreachable("invalid desc_type\n");
4353 }
4354
4355 offset += constant_index * stride;
4356
4357 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
4358 (!index_set || binding->immutable_samplers_equal)) {
4359 if (binding->immutable_samplers_equal)
4360 constant_index = 0;
4361
4362 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
4363 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4364 Operand(samplers[constant_index * 4 + 0]),
4365 Operand(samplers[constant_index * 4 + 1]),
4366 Operand(samplers[constant_index * 4 + 2]),
4367 Operand(samplers[constant_index * 4 + 3]));
4368 }
4369
4370 Operand off;
4371 if (!index_set) {
4372 off = bld.copy(bld.def(s1), Operand(offset));
4373 } else {
4374 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
4375 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
4376 }
4377
4378 Temp res = bld.smem(opcode, bld.def(type), list, off);
4379
4380 if (desc_type == ACO_DESC_PLANE_2) {
4381 Temp components[8];
4382 for (unsigned i = 0; i < 8; i++)
4383 components[i] = bld.tmp(s1);
4384 bld.pseudo(aco_opcode::p_split_vector,
4385 Definition(components[0]),
4386 Definition(components[1]),
4387 Definition(components[2]),
4388 Definition(components[3]),
4389 res);
4390
4391 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
4392 bld.pseudo(aco_opcode::p_split_vector,
4393 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
4394 Definition(components[4]),
4395 Definition(components[5]),
4396 Definition(components[6]),
4397 Definition(components[7]),
4398 desc2);
4399
4400 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
4401 components[0], components[1], components[2], components[3],
4402 components[4], components[5], components[6], components[7]);
4403 }
4404
4405 return res;
4406 }
4407
4408 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
4409 {
4410 switch (dim) {
4411 case GLSL_SAMPLER_DIM_BUF:
4412 return 1;
4413 case GLSL_SAMPLER_DIM_1D:
4414 return array ? 2 : 1;
4415 case GLSL_SAMPLER_DIM_2D:
4416 return array ? 3 : 2;
4417 case GLSL_SAMPLER_DIM_MS:
4418 return array ? 4 : 3;
4419 case GLSL_SAMPLER_DIM_3D:
4420 case GLSL_SAMPLER_DIM_CUBE:
4421 return 3;
4422 case GLSL_SAMPLER_DIM_RECT:
4423 case GLSL_SAMPLER_DIM_SUBPASS:
4424 return 2;
4425 case GLSL_SAMPLER_DIM_SUBPASS_MS:
4426 return 3;
4427 default:
4428 break;
4429 }
4430 return 0;
4431 }
4432
4433
4434 /* Adjust the sample index according to FMASK.
4435 *
4436 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4437 * which is the identity mapping. Each nibble says which physical sample
4438 * should be fetched to get that sample.
4439 *
4440 * For example, 0x11111100 means there are only 2 samples stored and
4441 * the second sample covers 3/4 of the pixel. When reading samples 0
4442 * and 1, return physical sample 0 (determined by the first two 0s
4443 * in FMASK), otherwise return physical sample 1.
4444 *
4445 * The sample index should be adjusted as follows:
4446 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4447 */
4448 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
4449 {
4450 Builder bld(ctx->program, ctx->block);
4451 Temp fmask = bld.tmp(v1);
4452 unsigned dim = ctx->options->chip_class >= GFX10
4453 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
4454 : 0;
4455
4456 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
4457 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
4458 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
4459 load->operands[0] = Operand(fmask_desc_ptr);
4460 load->operands[1] = Operand(s4); /* no sampler */
4461 load->operands[2] = Operand(coord);
4462 load->definitions[0] = Definition(fmask);
4463 load->glc = false;
4464 load->dlc = false;
4465 load->dmask = 0x1;
4466 load->unrm = true;
4467 load->da = da;
4468 load->dim = dim;
4469 load->can_reorder = true; /* fmask images shouldn't be modified */
4470 ctx->block->instructions.emplace_back(std::move(load));
4471
4472 Operand sample_index4;
4473 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
4474 sample_index4 = Operand(sample_index.constantValue() << 2);
4475 } else if (sample_index.regClass() == s1) {
4476 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
4477 } else {
4478 assert(sample_index.regClass() == v1);
4479 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
4480 }
4481
4482 Temp final_sample;
4483 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
4484 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
4485 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
4486 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
4487 else
4488 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
4489
4490 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4491 * resource descriptor is 0 (invalid),
4492 */
4493 Temp compare = bld.tmp(bld.lm);
4494 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
4495 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
4496
4497 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
4498
4499 /* Replace the MSAA sample index. */
4500 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
4501 }
4502
4503 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
4504 {
4505
4506 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
4507 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4508 bool is_array = glsl_sampler_type_is_array(type);
4509 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4510 assert(!add_frag_pos && "Input attachments should be lowered.");
4511 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
4512 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
4513 int count = image_type_to_components_count(dim, is_array);
4514 std::vector<Temp> coords(count);
4515 Builder bld(ctx->program, ctx->block);
4516
4517 if (is_ms) {
4518 count--;
4519 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
4520 /* get sample index */
4521 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
4522 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
4523 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
4524 std::vector<Temp> fmask_load_address;
4525 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
4526 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
4527
4528 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
4529 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
4530 } else {
4531 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
4532 }
4533 }
4534
4535 if (gfx9_1d) {
4536 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
4537 coords.resize(coords.size() + 1);
4538 coords[1] = bld.copy(bld.def(v1), Operand(0u));
4539 if (is_array)
4540 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
4541 } else {
4542 for (int i = 0; i < count; i++)
4543 coords[i] = emit_extract_vector(ctx, src0, i, v1);
4544 }
4545
4546 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
4547 instr->intrinsic == nir_intrinsic_image_deref_store) {
4548 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
4549 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
4550
4551 if (!level_zero)
4552 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
4553 }
4554
4555 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
4556 for (unsigned i = 0; i < coords.size(); i++)
4557 vec->operands[i] = Operand(coords[i]);
4558 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
4559 vec->definitions[0] = Definition(res);
4560 ctx->block->instructions.emplace_back(std::move(vec));
4561 return res;
4562 }
4563
4564
4565 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
4566 {
4567 Builder bld(ctx->program, ctx->block);
4568 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4569 const struct glsl_type *type = glsl_without_array(var->type);
4570 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4571 bool is_array = glsl_sampler_type_is_array(type);
4572 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4573
4574 if (dim == GLSL_SAMPLER_DIM_BUF) {
4575 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
4576 unsigned num_channels = util_last_bit(mask);
4577 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4578 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4579
4580 aco_opcode opcode;
4581 switch (num_channels) {
4582 case 1:
4583 opcode = aco_opcode::buffer_load_format_x;
4584 break;
4585 case 2:
4586 opcode = aco_opcode::buffer_load_format_xy;
4587 break;
4588 case 3:
4589 opcode = aco_opcode::buffer_load_format_xyz;
4590 break;
4591 case 4:
4592 opcode = aco_opcode::buffer_load_format_xyzw;
4593 break;
4594 default:
4595 unreachable(">4 channel buffer image load");
4596 }
4597 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
4598 load->operands[0] = Operand(rsrc);
4599 load->operands[1] = Operand(vindex);
4600 load->operands[2] = Operand((uint32_t) 0);
4601 Temp tmp;
4602 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4603 tmp = dst;
4604 else
4605 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
4606 load->definitions[0] = Definition(tmp);
4607 load->idxen = true;
4608 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
4609 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4610 load->barrier = barrier_image;
4611 ctx->block->instructions.emplace_back(std::move(load));
4612
4613 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
4614 return;
4615 }
4616
4617 Temp coords = get_image_coords(ctx, instr, type);
4618 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4619
4620 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
4621 unsigned num_components = util_bitcount(dmask);
4622 Temp tmp;
4623 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
4624 tmp = dst;
4625 else
4626 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
4627
4628 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
4629 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
4630
4631 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
4632 load->operands[0] = Operand(resource);
4633 load->operands[1] = Operand(s4); /* no sampler */
4634 load->operands[2] = Operand(coords);
4635 load->definitions[0] = Definition(tmp);
4636 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
4637 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
4638 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4639 load->dmask = dmask;
4640 load->unrm = true;
4641 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4642 load->barrier = barrier_image;
4643 ctx->block->instructions.emplace_back(std::move(load));
4644
4645 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
4646 return;
4647 }
4648
4649 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
4650 {
4651 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4652 const struct glsl_type *type = glsl_without_array(var->type);
4653 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4654 bool is_array = glsl_sampler_type_is_array(type);
4655 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4656
4657 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
4658
4659 if (dim == GLSL_SAMPLER_DIM_BUF) {
4660 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4661 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4662 aco_opcode opcode;
4663 switch (data.size()) {
4664 case 1:
4665 opcode = aco_opcode::buffer_store_format_x;
4666 break;
4667 case 2:
4668 opcode = aco_opcode::buffer_store_format_xy;
4669 break;
4670 case 3:
4671 opcode = aco_opcode::buffer_store_format_xyz;
4672 break;
4673 case 4:
4674 opcode = aco_opcode::buffer_store_format_xyzw;
4675 break;
4676 default:
4677 unreachable(">4 channel buffer image store");
4678 }
4679 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
4680 store->operands[0] = Operand(rsrc);
4681 store->operands[1] = Operand(vindex);
4682 store->operands[2] = Operand((uint32_t) 0);
4683 store->operands[3] = Operand(data);
4684 store->idxen = true;
4685 store->glc = glc;
4686 store->dlc = false;
4687 store->disable_wqm = true;
4688 store->barrier = barrier_image;
4689 ctx->program->needs_exact = true;
4690 ctx->block->instructions.emplace_back(std::move(store));
4691 return;
4692 }
4693
4694 assert(data.type() == RegType::vgpr);
4695 Temp coords = get_image_coords(ctx, instr, type);
4696 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4697
4698 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
4699 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
4700
4701 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
4702 store->operands[0] = Operand(resource);
4703 store->operands[1] = Operand(data);
4704 store->operands[2] = Operand(coords);
4705 store->glc = glc;
4706 store->dlc = false;
4707 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4708 store->dmask = (1 << data.size()) - 1;
4709 store->unrm = true;
4710 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4711 store->disable_wqm = true;
4712 store->barrier = barrier_image;
4713 ctx->program->needs_exact = true;
4714 ctx->block->instructions.emplace_back(std::move(store));
4715 return;
4716 }
4717
4718 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
4719 {
4720 /* return the previous value if dest is ever used */
4721 bool return_previous = false;
4722 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
4723 return_previous = true;
4724 break;
4725 }
4726 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
4727 return_previous = true;
4728 break;
4729 }
4730
4731 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4732 const struct glsl_type *type = glsl_without_array(var->type);
4733 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4734 bool is_array = glsl_sampler_type_is_array(type);
4735 Builder bld(ctx->program, ctx->block);
4736
4737 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
4738 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
4739
4740 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
4741 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
4742
4743 aco_opcode buf_op, image_op;
4744 switch (instr->intrinsic) {
4745 case nir_intrinsic_image_deref_atomic_add:
4746 buf_op = aco_opcode::buffer_atomic_add;
4747 image_op = aco_opcode::image_atomic_add;
4748 break;
4749 case nir_intrinsic_image_deref_atomic_umin:
4750 buf_op = aco_opcode::buffer_atomic_umin;
4751 image_op = aco_opcode::image_atomic_umin;
4752 break;
4753 case nir_intrinsic_image_deref_atomic_imin:
4754 buf_op = aco_opcode::buffer_atomic_smin;
4755 image_op = aco_opcode::image_atomic_smin;
4756 break;
4757 case nir_intrinsic_image_deref_atomic_umax:
4758 buf_op = aco_opcode::buffer_atomic_umax;
4759 image_op = aco_opcode::image_atomic_umax;
4760 break;
4761 case nir_intrinsic_image_deref_atomic_imax:
4762 buf_op = aco_opcode::buffer_atomic_smax;
4763 image_op = aco_opcode::image_atomic_smax;
4764 break;
4765 case nir_intrinsic_image_deref_atomic_and:
4766 buf_op = aco_opcode::buffer_atomic_and;
4767 image_op = aco_opcode::image_atomic_and;
4768 break;
4769 case nir_intrinsic_image_deref_atomic_or:
4770 buf_op = aco_opcode::buffer_atomic_or;
4771 image_op = aco_opcode::image_atomic_or;
4772 break;
4773 case nir_intrinsic_image_deref_atomic_xor:
4774 buf_op = aco_opcode::buffer_atomic_xor;
4775 image_op = aco_opcode::image_atomic_xor;
4776 break;
4777 case nir_intrinsic_image_deref_atomic_exchange:
4778 buf_op = aco_opcode::buffer_atomic_swap;
4779 image_op = aco_opcode::image_atomic_swap;
4780 break;
4781 case nir_intrinsic_image_deref_atomic_comp_swap:
4782 buf_op = aco_opcode::buffer_atomic_cmpswap;
4783 image_op = aco_opcode::image_atomic_cmpswap;
4784 break;
4785 default:
4786 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4787 }
4788
4789 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4790
4791 if (dim == GLSL_SAMPLER_DIM_BUF) {
4792 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
4793 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
4794 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4795 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
4796 mubuf->operands[0] = Operand(resource);
4797 mubuf->operands[1] = Operand(vindex);
4798 mubuf->operands[2] = Operand((uint32_t)0);
4799 mubuf->operands[3] = Operand(data);
4800 if (return_previous)
4801 mubuf->definitions[0] = Definition(dst);
4802 mubuf->offset = 0;
4803 mubuf->idxen = true;
4804 mubuf->glc = return_previous;
4805 mubuf->dlc = false; /* Not needed for atomics */
4806 mubuf->disable_wqm = true;
4807 mubuf->barrier = barrier_image;
4808 ctx->program->needs_exact = true;
4809 ctx->block->instructions.emplace_back(std::move(mubuf));
4810 return;
4811 }
4812
4813 Temp coords = get_image_coords(ctx, instr, type);
4814 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
4815 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
4816 mimg->operands[0] = Operand(resource);
4817 mimg->operands[1] = Operand(data);
4818 mimg->operands[2] = Operand(coords);
4819 if (return_previous)
4820 mimg->definitions[0] = Definition(dst);
4821 mimg->glc = return_previous;
4822 mimg->dlc = false; /* Not needed for atomics */
4823 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4824 mimg->dmask = (1 << data.size()) - 1;
4825 mimg->unrm = true;
4826 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
4827 mimg->disable_wqm = true;
4828 mimg->barrier = barrier_image;
4829 ctx->program->needs_exact = true;
4830 ctx->block->instructions.emplace_back(std::move(mimg));
4831 return;
4832 }
4833
4834 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
4835 {
4836 if (in_elements && ctx->options->chip_class == GFX8) {
4837 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
4838 Builder bld(ctx->program, ctx->block);
4839
4840 Temp size = emit_extract_vector(ctx, desc, 2, s1);
4841
4842 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
4843 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
4844
4845 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
4846 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
4847
4848 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
4849 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
4850
4851 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
4852 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
4853 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
4854 if (dst.type() == RegType::vgpr)
4855 bld.copy(Definition(dst), shr_dst);
4856
4857 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
4858 } else {
4859 emit_extract_vector(ctx, desc, 2, dst);
4860 }
4861 }
4862
4863 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
4864 {
4865 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
4866 const struct glsl_type *type = glsl_without_array(var->type);
4867 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
4868 bool is_array = glsl_sampler_type_is_array(type);
4869 Builder bld(ctx->program, ctx->block);
4870
4871 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
4872 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
4873 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
4874 }
4875
4876 /* LOD */
4877 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
4878
4879 /* Resource */
4880 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
4881
4882 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4883
4884 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
4885 mimg->operands[0] = Operand(resource);
4886 mimg->operands[1] = Operand(s4); /* no sampler */
4887 mimg->operands[2] = Operand(lod);
4888 uint8_t& dmask = mimg->dmask;
4889 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
4890 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
4891 mimg->da = glsl_sampler_type_is_array(type);
4892 mimg->can_reorder = true;
4893 Definition& def = mimg->definitions[0];
4894 ctx->block->instructions.emplace_back(std::move(mimg));
4895
4896 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
4897 glsl_sampler_type_is_array(type)) {
4898
4899 assert(instr->dest.ssa.num_components == 3);
4900 Temp tmp = {ctx->program->allocateId(), v3};
4901 def = Definition(tmp);
4902 emit_split_vector(ctx, tmp, 3);
4903
4904 /* divide 3rd value by 6 by multiplying with magic number */
4905 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
4906 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
4907
4908 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4909 emit_extract_vector(ctx, tmp, 0, v1),
4910 emit_extract_vector(ctx, tmp, 1, v1),
4911 by_6);
4912
4913 } else if (ctx->options->chip_class == GFX9 &&
4914 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
4915 glsl_sampler_type_is_array(type)) {
4916 assert(instr->dest.ssa.num_components == 2);
4917 def = Definition(dst);
4918 dmask = 0x5;
4919 } else {
4920 def = Definition(dst);
4921 }
4922
4923 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4924 }
4925
4926 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4927 {
4928 Builder bld(ctx->program, ctx->block);
4929 unsigned num_components = instr->num_components;
4930
4931 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4932 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
4933 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4934
4935 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
4936 load_buffer(ctx, num_components, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), glc, false);
4937 }
4938
4939 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
4940 {
4941 Builder bld(ctx->program, ctx->block);
4942 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
4943 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4944 unsigned writemask = nir_intrinsic_write_mask(instr);
4945 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
4946
4947 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
4948 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4949
4950 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
4951 ctx->options->chip_class >= GFX8;
4952 if (smem)
4953 offset = bld.as_uniform(offset);
4954 bool smem_nonfs = smem && ctx->stage != fragment_fs;
4955
4956 while (writemask) {
4957 int start, count;
4958 u_bit_scan_consecutive_range(&writemask, &start, &count);
4959 if (count == 3 && (smem || ctx->options->chip_class == GFX6)) {
4960 /* GFX6 doesn't support storing vec3, split it. */
4961 writemask |= 1u << (start + 2);
4962 count = 2;
4963 }
4964 int num_bytes = count * elem_size_bytes;
4965
4966 if (num_bytes > 16) {
4967 assert(elem_size_bytes == 8);
4968 writemask |= (((count - 2) << 1) - 1) << (start + 2);
4969 count = 2;
4970 num_bytes = 16;
4971 }
4972
4973 // TODO: check alignment of sub-dword stores
4974 // TODO: split 3 bytes. there is no store instruction for that
4975
4976 Temp write_data;
4977 if (count != instr->num_components) {
4978 emit_split_vector(ctx, data, instr->num_components);
4979 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4980 for (int i = 0; i < count; i++) {
4981 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
4982 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
4983 }
4984 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
4985 vec->definitions[0] = Definition(write_data);
4986 ctx->block->instructions.emplace_back(std::move(vec));
4987 } else if (!smem && data.type() != RegType::vgpr) {
4988 assert(num_bytes % 4 == 0);
4989 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
4990 } else if (smem_nonfs && data.type() == RegType::vgpr) {
4991 assert(num_bytes % 4 == 0);
4992 write_data = bld.as_uniform(data);
4993 } else {
4994 write_data = data;
4995 }
4996
4997 aco_opcode vmem_op, smem_op;
4998 switch (num_bytes) {
4999 case 4:
5000 vmem_op = aco_opcode::buffer_store_dword;
5001 smem_op = aco_opcode::s_buffer_store_dword;
5002 break;
5003 case 8:
5004 vmem_op = aco_opcode::buffer_store_dwordx2;
5005 smem_op = aco_opcode::s_buffer_store_dwordx2;
5006 break;
5007 case 12:
5008 vmem_op = aco_opcode::buffer_store_dwordx3;
5009 smem_op = aco_opcode::last_opcode;
5010 assert(!smem && ctx->options->chip_class > GFX6);
5011 break;
5012 case 16:
5013 vmem_op = aco_opcode::buffer_store_dwordx4;
5014 smem_op = aco_opcode::s_buffer_store_dwordx4;
5015 break;
5016 default:
5017 unreachable("Store SSBO not implemented for this size.");
5018 }
5019 if (ctx->stage == fragment_fs)
5020 smem_op = aco_opcode::p_fs_buffer_store_smem;
5021
5022 if (smem) {
5023 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
5024 store->operands[0] = Operand(rsrc);
5025 if (start) {
5026 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5027 offset, Operand(start * elem_size_bytes));
5028 store->operands[1] = Operand(off);
5029 } else {
5030 store->operands[1] = Operand(offset);
5031 }
5032 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
5033 store->operands[1].setFixed(m0);
5034 store->operands[2] = Operand(write_data);
5035 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5036 store->dlc = false;
5037 store->disable_wqm = true;
5038 store->barrier = barrier_buffer;
5039 ctx->block->instructions.emplace_back(std::move(store));
5040 ctx->program->wb_smem_l1_on_end = true;
5041 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
5042 ctx->block->kind |= block_kind_needs_lowering;
5043 ctx->program->needs_exact = true;
5044 }
5045 } else {
5046 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
5047 store->operands[0] = Operand(rsrc);
5048 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5049 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5050 store->operands[3] = Operand(write_data);
5051 store->offset = start * elem_size_bytes;
5052 store->offen = (offset.type() == RegType::vgpr);
5053 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5054 store->dlc = false;
5055 store->disable_wqm = true;
5056 store->barrier = barrier_buffer;
5057 ctx->program->needs_exact = true;
5058 ctx->block->instructions.emplace_back(std::move(store));
5059 }
5060 }
5061 }
5062
5063 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5064 {
5065 /* return the previous value if dest is ever used */
5066 bool return_previous = false;
5067 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5068 return_previous = true;
5069 break;
5070 }
5071 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5072 return_previous = true;
5073 break;
5074 }
5075
5076 Builder bld(ctx->program, ctx->block);
5077 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
5078
5079 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
5080 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
5081 get_ssa_temp(ctx, instr->src[3].ssa), data);
5082
5083 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
5084 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5085 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5086
5087 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5088
5089 aco_opcode op32, op64;
5090 switch (instr->intrinsic) {
5091 case nir_intrinsic_ssbo_atomic_add:
5092 op32 = aco_opcode::buffer_atomic_add;
5093 op64 = aco_opcode::buffer_atomic_add_x2;
5094 break;
5095 case nir_intrinsic_ssbo_atomic_imin:
5096 op32 = aco_opcode::buffer_atomic_smin;
5097 op64 = aco_opcode::buffer_atomic_smin_x2;
5098 break;
5099 case nir_intrinsic_ssbo_atomic_umin:
5100 op32 = aco_opcode::buffer_atomic_umin;
5101 op64 = aco_opcode::buffer_atomic_umin_x2;
5102 break;
5103 case nir_intrinsic_ssbo_atomic_imax:
5104 op32 = aco_opcode::buffer_atomic_smax;
5105 op64 = aco_opcode::buffer_atomic_smax_x2;
5106 break;
5107 case nir_intrinsic_ssbo_atomic_umax:
5108 op32 = aco_opcode::buffer_atomic_umax;
5109 op64 = aco_opcode::buffer_atomic_umax_x2;
5110 break;
5111 case nir_intrinsic_ssbo_atomic_and:
5112 op32 = aco_opcode::buffer_atomic_and;
5113 op64 = aco_opcode::buffer_atomic_and_x2;
5114 break;
5115 case nir_intrinsic_ssbo_atomic_or:
5116 op32 = aco_opcode::buffer_atomic_or;
5117 op64 = aco_opcode::buffer_atomic_or_x2;
5118 break;
5119 case nir_intrinsic_ssbo_atomic_xor:
5120 op32 = aco_opcode::buffer_atomic_xor;
5121 op64 = aco_opcode::buffer_atomic_xor_x2;
5122 break;
5123 case nir_intrinsic_ssbo_atomic_exchange:
5124 op32 = aco_opcode::buffer_atomic_swap;
5125 op64 = aco_opcode::buffer_atomic_swap_x2;
5126 break;
5127 case nir_intrinsic_ssbo_atomic_comp_swap:
5128 op32 = aco_opcode::buffer_atomic_cmpswap;
5129 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
5130 break;
5131 default:
5132 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
5133 }
5134 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5135 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5136 mubuf->operands[0] = Operand(rsrc);
5137 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5138 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5139 mubuf->operands[3] = Operand(data);
5140 if (return_previous)
5141 mubuf->definitions[0] = Definition(dst);
5142 mubuf->offset = 0;
5143 mubuf->offen = (offset.type() == RegType::vgpr);
5144 mubuf->glc = return_previous;
5145 mubuf->dlc = false; /* Not needed for atomics */
5146 mubuf->disable_wqm = true;
5147 mubuf->barrier = barrier_buffer;
5148 ctx->program->needs_exact = true;
5149 ctx->block->instructions.emplace_back(std::move(mubuf));
5150 }
5151
5152 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
5153
5154 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5155 Builder bld(ctx->program, ctx->block);
5156 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
5157 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
5158 }
5159
5160 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
5161 {
5162 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5163 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5164
5165 if (addr.type() == RegType::vgpr)
5166 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
5167 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
5168 }
5169
5170 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
5171 {
5172 Builder bld(ctx->program, ctx->block);
5173 unsigned num_components = instr->num_components;
5174 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
5175
5176 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5177 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
5178
5179 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
5180 bool dlc = glc && ctx->options->chip_class >= GFX10;
5181 aco_opcode op;
5182 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8)) {
5183 bool global = ctx->options->chip_class >= GFX9;
5184
5185 if (ctx->options->chip_class >= GFX7) {
5186 aco_opcode op;
5187 switch (num_bytes) {
5188 case 4:
5189 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
5190 break;
5191 case 8:
5192 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
5193 break;
5194 case 12:
5195 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
5196 break;
5197 case 16:
5198 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
5199 break;
5200 default:
5201 unreachable("load_global not implemented for this size.");
5202 }
5203
5204 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
5205 flat->operands[0] = Operand(addr);
5206 flat->operands[1] = Operand(s1);
5207 flat->glc = glc;
5208 flat->dlc = dlc;
5209 flat->barrier = barrier_buffer;
5210
5211 if (dst.type() == RegType::sgpr) {
5212 Temp vec = bld.tmp(RegType::vgpr, dst.size());
5213 flat->definitions[0] = Definition(vec);
5214 ctx->block->instructions.emplace_back(std::move(flat));
5215 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
5216 } else {
5217 flat->definitions[0] = Definition(dst);
5218 ctx->block->instructions.emplace_back(std::move(flat));
5219 }
5220 emit_split_vector(ctx, dst, num_components);
5221 } else {
5222 assert(ctx->options->chip_class == GFX6);
5223
5224 /* GFX6 doesn't support loading vec3, expand to vec4. */
5225 num_bytes = num_bytes == 12 ? 16 : num_bytes;
5226
5227 aco_opcode op;
5228 switch (num_bytes) {
5229 case 4:
5230 op = aco_opcode::buffer_load_dword;
5231 break;
5232 case 8:
5233 op = aco_opcode::buffer_load_dwordx2;
5234 break;
5235 case 16:
5236 op = aco_opcode::buffer_load_dwordx4;
5237 break;
5238 default:
5239 unreachable("load_global not implemented for this size.");
5240 }
5241
5242 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
5243
5244 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
5245 mubuf->operands[0] = Operand(rsrc);
5246 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
5247 mubuf->operands[2] = Operand(0u);
5248 mubuf->glc = glc;
5249 mubuf->dlc = false;
5250 mubuf->offset = 0;
5251 mubuf->addr64 = addr.type() == RegType::vgpr;
5252 mubuf->disable_wqm = false;
5253 mubuf->barrier = barrier_buffer;
5254 aco_ptr<Instruction> instr = std::move(mubuf);
5255
5256 /* expand vector */
5257 if (dst.size() == 3) {
5258 Temp vec = bld.tmp(v4);
5259 instr->definitions[0] = Definition(vec);
5260 bld.insert(std::move(instr));
5261 emit_split_vector(ctx, vec, 4);
5262
5263 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
5264 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
5265 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
5266 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
5267 }
5268
5269 if (dst.type() == RegType::sgpr) {
5270 Temp vec = bld.tmp(RegType::vgpr, dst.size());
5271 instr->definitions[0] = Definition(vec);
5272 bld.insert(std::move(instr));
5273 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
5274 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
5275 } else {
5276 instr->definitions[0] = Definition(dst);
5277 bld.insert(std::move(instr));
5278 emit_split_vector(ctx, dst, num_components);
5279 }
5280 }
5281 } else {
5282 switch (num_bytes) {
5283 case 4:
5284 op = aco_opcode::s_load_dword;
5285 break;
5286 case 8:
5287 op = aco_opcode::s_load_dwordx2;
5288 break;
5289 case 12:
5290 case 16:
5291 op = aco_opcode::s_load_dwordx4;
5292 break;
5293 default:
5294 unreachable("load_global not implemented for this size.");
5295 }
5296 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
5297 load->operands[0] = Operand(addr);
5298 load->operands[1] = Operand(0u);
5299 load->definitions[0] = Definition(dst);
5300 load->glc = glc;
5301 load->dlc = dlc;
5302 load->barrier = barrier_buffer;
5303 assert(ctx->options->chip_class >= GFX8 || !glc);
5304
5305 if (dst.size() == 3) {
5306 /* trim vector */
5307 Temp vec = bld.tmp(s4);
5308 load->definitions[0] = Definition(vec);
5309 ctx->block->instructions.emplace_back(std::move(load));
5310 emit_split_vector(ctx, vec, 4);
5311
5312 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5313 emit_extract_vector(ctx, vec, 0, s1),
5314 emit_extract_vector(ctx, vec, 1, s1),
5315 emit_extract_vector(ctx, vec, 2, s1));
5316 } else {
5317 ctx->block->instructions.emplace_back(std::move(load));
5318 }
5319 }
5320 }
5321
5322 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
5323 {
5324 Builder bld(ctx->program, ctx->block);
5325 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5326
5327 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5328 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
5329
5330 if (ctx->options->chip_class >= GFX7)
5331 addr = as_vgpr(ctx, addr);
5332
5333 unsigned writemask = nir_intrinsic_write_mask(instr);
5334 while (writemask) {
5335 int start, count;
5336 u_bit_scan_consecutive_range(&writemask, &start, &count);
5337 if (count == 3 && ctx->options->chip_class == GFX6) {
5338 /* GFX6 doesn't support storing vec3, split it. */
5339 writemask |= 1u << (start + 2);
5340 count = 2;
5341 }
5342 unsigned num_bytes = count * elem_size_bytes;
5343
5344 Temp write_data = data;
5345 if (count != instr->num_components) {
5346 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5347 for (int i = 0; i < count; i++)
5348 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
5349 write_data = bld.tmp(RegType::vgpr, count);
5350 vec->definitions[0] = Definition(write_data);
5351 ctx->block->instructions.emplace_back(std::move(vec));
5352 }
5353
5354 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5355 unsigned offset = start * elem_size_bytes;
5356
5357 if (ctx->options->chip_class >= GFX7) {
5358 if (offset > 0 && ctx->options->chip_class < GFX9) {
5359 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
5360 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
5361 Temp carry = bld.tmp(bld.lm);
5362 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
5363
5364 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
5365 Operand(offset), addr0);
5366 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
5367 Operand(0u), addr1,
5368 carry).def(1).setHint(vcc);
5369
5370 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
5371
5372 offset = 0;
5373 }
5374
5375 bool global = ctx->options->chip_class >= GFX9;
5376 aco_opcode op;
5377 switch (num_bytes) {
5378 case 4:
5379 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
5380 break;
5381 case 8:
5382 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
5383 break;
5384 case 12:
5385 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
5386 break;
5387 case 16:
5388 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
5389 break;
5390 default:
5391 unreachable("store_global not implemented for this size.");
5392 }
5393
5394 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
5395 flat->operands[0] = Operand(addr);
5396 flat->operands[1] = Operand(s1);
5397 flat->operands[2] = Operand(data);
5398 flat->glc = glc;
5399 flat->dlc = false;
5400 flat->offset = offset;
5401 flat->disable_wqm = true;
5402 flat->barrier = barrier_buffer;
5403 ctx->program->needs_exact = true;
5404 ctx->block->instructions.emplace_back(std::move(flat));
5405 } else {
5406 assert(ctx->options->chip_class == GFX6);
5407
5408 aco_opcode op;
5409 switch (num_bytes) {
5410 case 4:
5411 op = aco_opcode::buffer_store_dword;
5412 break;
5413 case 8:
5414 op = aco_opcode::buffer_store_dwordx2;
5415 break;
5416 case 16:
5417 op = aco_opcode::buffer_store_dwordx4;
5418 break;
5419 default:
5420 unreachable("store_global not implemented for this size.");
5421 }
5422
5423 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
5424
5425 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
5426 mubuf->operands[0] = Operand(rsrc);
5427 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
5428 mubuf->operands[2] = Operand(0u);
5429 mubuf->operands[3] = Operand(write_data);
5430 mubuf->glc = glc;
5431 mubuf->dlc = false;
5432 mubuf->offset = offset;
5433 mubuf->addr64 = addr.type() == RegType::vgpr;
5434 mubuf->disable_wqm = true;
5435 mubuf->barrier = barrier_buffer;
5436 ctx->program->needs_exact = true;
5437 ctx->block->instructions.emplace_back(std::move(mubuf));
5438 }
5439 }
5440 }
5441
5442 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5443 {
5444 /* return the previous value if dest is ever used */
5445 bool return_previous = false;
5446 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5447 return_previous = true;
5448 break;
5449 }
5450 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5451 return_previous = true;
5452 break;
5453 }
5454
5455 Builder bld(ctx->program, ctx->block);
5456 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
5457 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5458
5459 if (ctx->options->chip_class >= GFX7)
5460 addr = as_vgpr(ctx, addr);
5461
5462 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
5463 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
5464 get_ssa_temp(ctx, instr->src[2].ssa), data);
5465
5466 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5467
5468 aco_opcode op32, op64;
5469
5470 if (ctx->options->chip_class >= GFX7) {
5471 bool global = ctx->options->chip_class >= GFX9;
5472 switch (instr->intrinsic) {
5473 case nir_intrinsic_global_atomic_add:
5474 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
5475 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
5476 break;
5477 case nir_intrinsic_global_atomic_imin:
5478 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
5479 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
5480 break;
5481 case nir_intrinsic_global_atomic_umin:
5482 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
5483 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
5484 break;
5485 case nir_intrinsic_global_atomic_imax:
5486 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
5487 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
5488 break;
5489 case nir_intrinsic_global_atomic_umax:
5490 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
5491 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
5492 break;
5493 case nir_intrinsic_global_atomic_and:
5494 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
5495 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
5496 break;
5497 case nir_intrinsic_global_atomic_or:
5498 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
5499 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
5500 break;
5501 case nir_intrinsic_global_atomic_xor:
5502 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
5503 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
5504 break;
5505 case nir_intrinsic_global_atomic_exchange:
5506 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
5507 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
5508 break;
5509 case nir_intrinsic_global_atomic_comp_swap:
5510 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
5511 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
5512 break;
5513 default:
5514 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5515 }
5516
5517 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5518 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
5519 flat->operands[0] = Operand(addr);
5520 flat->operands[1] = Operand(s1);
5521 flat->operands[2] = Operand(data);
5522 if (return_previous)
5523 flat->definitions[0] = Definition(dst);
5524 flat->glc = return_previous;
5525 flat->dlc = false; /* Not needed for atomics */
5526 flat->offset = 0;
5527 flat->disable_wqm = true;
5528 flat->barrier = barrier_buffer;
5529 ctx->program->needs_exact = true;
5530 ctx->block->instructions.emplace_back(std::move(flat));
5531 } else {
5532 assert(ctx->options->chip_class == GFX6);
5533
5534 switch (instr->intrinsic) {
5535 case nir_intrinsic_global_atomic_add:
5536 op32 = aco_opcode::buffer_atomic_add;
5537 op64 = aco_opcode::buffer_atomic_add_x2;
5538 break;
5539 case nir_intrinsic_global_atomic_imin:
5540 op32 = aco_opcode::buffer_atomic_smin;
5541 op64 = aco_opcode::buffer_atomic_smin_x2;
5542 break;
5543 case nir_intrinsic_global_atomic_umin:
5544 op32 = aco_opcode::buffer_atomic_umin;
5545 op64 = aco_opcode::buffer_atomic_umin_x2;
5546 break;
5547 case nir_intrinsic_global_atomic_imax:
5548 op32 = aco_opcode::buffer_atomic_smax;
5549 op64 = aco_opcode::buffer_atomic_smax_x2;
5550 break;
5551 case nir_intrinsic_global_atomic_umax:
5552 op32 = aco_opcode::buffer_atomic_umax;
5553 op64 = aco_opcode::buffer_atomic_umax_x2;
5554 break;
5555 case nir_intrinsic_global_atomic_and:
5556 op32 = aco_opcode::buffer_atomic_and;
5557 op64 = aco_opcode::buffer_atomic_and_x2;
5558 break;
5559 case nir_intrinsic_global_atomic_or:
5560 op32 = aco_opcode::buffer_atomic_or;
5561 op64 = aco_opcode::buffer_atomic_or_x2;
5562 break;
5563 case nir_intrinsic_global_atomic_xor:
5564 op32 = aco_opcode::buffer_atomic_xor;
5565 op64 = aco_opcode::buffer_atomic_xor_x2;
5566 break;
5567 case nir_intrinsic_global_atomic_exchange:
5568 op32 = aco_opcode::buffer_atomic_swap;
5569 op64 = aco_opcode::buffer_atomic_swap_x2;
5570 break;
5571 case nir_intrinsic_global_atomic_comp_swap:
5572 op32 = aco_opcode::buffer_atomic_cmpswap;
5573 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
5574 break;
5575 default:
5576 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5577 }
5578
5579 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
5580
5581 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5582
5583 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5584 mubuf->operands[0] = Operand(rsrc);
5585 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
5586 mubuf->operands[2] = Operand(0u);
5587 mubuf->operands[3] = Operand(data);
5588 if (return_previous)
5589 mubuf->definitions[0] = Definition(dst);
5590 mubuf->glc = return_previous;
5591 mubuf->dlc = false;
5592 mubuf->offset = 0;
5593 mubuf->addr64 = addr.type() == RegType::vgpr;
5594 mubuf->disable_wqm = true;
5595 mubuf->barrier = barrier_buffer;
5596 ctx->program->needs_exact = true;
5597 ctx->block->instructions.emplace_back(std::move(mubuf));
5598 }
5599 }
5600
5601 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
5602 Builder bld(ctx->program, ctx->block);
5603 switch(instr->intrinsic) {
5604 case nir_intrinsic_group_memory_barrier:
5605 case nir_intrinsic_memory_barrier:
5606 bld.barrier(aco_opcode::p_memory_barrier_common);
5607 break;
5608 case nir_intrinsic_memory_barrier_buffer:
5609 bld.barrier(aco_opcode::p_memory_barrier_buffer);
5610 break;
5611 case nir_intrinsic_memory_barrier_image:
5612 bld.barrier(aco_opcode::p_memory_barrier_image);
5613 break;
5614 case nir_intrinsic_memory_barrier_tcs_patch:
5615 case nir_intrinsic_memory_barrier_shared:
5616 bld.barrier(aco_opcode::p_memory_barrier_shared);
5617 break;
5618 default:
5619 unreachable("Unimplemented memory barrier intrinsic");
5620 break;
5621 }
5622 }
5623
5624 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
5625 {
5626 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
5627 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5628 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
5629 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5630 Builder bld(ctx->program, ctx->block);
5631
5632 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5633 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
5634 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
5635 }
5636
5637 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
5638 {
5639 unsigned writemask = nir_intrinsic_write_mask(instr);
5640 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
5641 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5642 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5643 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
5644
5645 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
5646 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
5647 }
5648
5649 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5650 {
5651 unsigned offset = nir_intrinsic_base(instr);
5652 Operand m = load_lds_size_m0(ctx);
5653 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5654 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5655
5656 unsigned num_operands = 3;
5657 aco_opcode op32, op64, op32_rtn, op64_rtn;
5658 switch(instr->intrinsic) {
5659 case nir_intrinsic_shared_atomic_add:
5660 op32 = aco_opcode::ds_add_u32;
5661 op64 = aco_opcode::ds_add_u64;
5662 op32_rtn = aco_opcode::ds_add_rtn_u32;
5663 op64_rtn = aco_opcode::ds_add_rtn_u64;
5664 break;
5665 case nir_intrinsic_shared_atomic_imin:
5666 op32 = aco_opcode::ds_min_i32;
5667 op64 = aco_opcode::ds_min_i64;
5668 op32_rtn = aco_opcode::ds_min_rtn_i32;
5669 op64_rtn = aco_opcode::ds_min_rtn_i64;
5670 break;
5671 case nir_intrinsic_shared_atomic_umin:
5672 op32 = aco_opcode::ds_min_u32;
5673 op64 = aco_opcode::ds_min_u64;
5674 op32_rtn = aco_opcode::ds_min_rtn_u32;
5675 op64_rtn = aco_opcode::ds_min_rtn_u64;
5676 break;
5677 case nir_intrinsic_shared_atomic_imax:
5678 op32 = aco_opcode::ds_max_i32;
5679 op64 = aco_opcode::ds_max_i64;
5680 op32_rtn = aco_opcode::ds_max_rtn_i32;
5681 op64_rtn = aco_opcode::ds_max_rtn_i64;
5682 break;
5683 case nir_intrinsic_shared_atomic_umax:
5684 op32 = aco_opcode::ds_max_u32;
5685 op64 = aco_opcode::ds_max_u64;
5686 op32_rtn = aco_opcode::ds_max_rtn_u32;
5687 op64_rtn = aco_opcode::ds_max_rtn_u64;
5688 break;
5689 case nir_intrinsic_shared_atomic_and:
5690 op32 = aco_opcode::ds_and_b32;
5691 op64 = aco_opcode::ds_and_b64;
5692 op32_rtn = aco_opcode::ds_and_rtn_b32;
5693 op64_rtn = aco_opcode::ds_and_rtn_b64;
5694 break;
5695 case nir_intrinsic_shared_atomic_or:
5696 op32 = aco_opcode::ds_or_b32;
5697 op64 = aco_opcode::ds_or_b64;
5698 op32_rtn = aco_opcode::ds_or_rtn_b32;
5699 op64_rtn = aco_opcode::ds_or_rtn_b64;
5700 break;
5701 case nir_intrinsic_shared_atomic_xor:
5702 op32 = aco_opcode::ds_xor_b32;
5703 op64 = aco_opcode::ds_xor_b64;
5704 op32_rtn = aco_opcode::ds_xor_rtn_b32;
5705 op64_rtn = aco_opcode::ds_xor_rtn_b64;
5706 break;
5707 case nir_intrinsic_shared_atomic_exchange:
5708 op32 = aco_opcode::ds_write_b32;
5709 op64 = aco_opcode::ds_write_b64;
5710 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
5711 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
5712 break;
5713 case nir_intrinsic_shared_atomic_comp_swap:
5714 op32 = aco_opcode::ds_cmpst_b32;
5715 op64 = aco_opcode::ds_cmpst_b64;
5716 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
5717 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
5718 num_operands = 4;
5719 break;
5720 default:
5721 unreachable("Unhandled shared atomic intrinsic");
5722 }
5723
5724 /* return the previous value if dest is ever used */
5725 bool return_previous = false;
5726 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5727 return_previous = true;
5728 break;
5729 }
5730 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5731 return_previous = true;
5732 break;
5733 }
5734
5735 aco_opcode op;
5736 if (data.size() == 1) {
5737 assert(instr->dest.ssa.bit_size == 32);
5738 op = return_previous ? op32_rtn : op32;
5739 } else {
5740 assert(instr->dest.ssa.bit_size == 64);
5741 op = return_previous ? op64_rtn : op64;
5742 }
5743
5744 if (offset > 65535) {
5745 Builder bld(ctx->program, ctx->block);
5746 address = bld.vadd32(bld.def(v1), Operand(offset), address);
5747 offset = 0;
5748 }
5749
5750 aco_ptr<DS_instruction> ds;
5751 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
5752 ds->operands[0] = Operand(address);
5753 ds->operands[1] = Operand(data);
5754 if (num_operands == 4)
5755 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
5756 ds->operands[num_operands - 1] = m;
5757 ds->offset0 = offset;
5758 if (return_previous)
5759 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
5760 ctx->block->instructions.emplace_back(std::move(ds));
5761 }
5762
5763 Temp get_scratch_resource(isel_context *ctx)
5764 {
5765 Builder bld(ctx->program, ctx->block);
5766 Temp scratch_addr = ctx->program->private_segment_buffer;
5767 if (ctx->stage != compute_cs)
5768 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
5769
5770 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
5771 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
5772
5773 if (ctx->program->chip_class >= GFX10) {
5774 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5775 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5776 S_008F0C_RESOURCE_LEVEL(1);
5777 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
5778 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5779 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5780 }
5781
5782 /* older generations need element size = 16 bytes. element size removed in GFX9 */
5783 if (ctx->program->chip_class <= GFX8)
5784 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
5785
5786 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
5787 }
5788
5789 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
5790 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
5791 Builder bld(ctx->program, ctx->block);
5792 Temp rsrc = get_scratch_resource(ctx);
5793 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5794 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5795
5796 aco_opcode op;
5797 switch (dst.size()) {
5798 case 1:
5799 op = aco_opcode::buffer_load_dword;
5800 break;
5801 case 2:
5802 op = aco_opcode::buffer_load_dwordx2;
5803 break;
5804 case 3:
5805 op = aco_opcode::buffer_load_dwordx3;
5806 break;
5807 case 4:
5808 op = aco_opcode::buffer_load_dwordx4;
5809 break;
5810 case 6:
5811 case 8: {
5812 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5813 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
5814 bld.def(v4), rsrc, offset,
5815 ctx->program->scratch_offset, 0, true);
5816 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
5817 aco_opcode::buffer_load_dwordx4,
5818 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
5819 rsrc, offset, ctx->program->scratch_offset, 16, true);
5820 emit_split_vector(ctx, lower, 2);
5821 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
5822 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
5823 if (dst.size() == 8) {
5824 emit_split_vector(ctx, upper, 2);
5825 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
5826 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
5827 } else {
5828 elems[2] = upper;
5829 }
5830
5831 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
5832 Format::PSEUDO, dst.size() / 2, 1)};
5833 for (unsigned i = 0; i < dst.size() / 2; i++)
5834 vec->operands[i] = Operand(elems[i]);
5835 vec->definitions[0] = Definition(dst);
5836 bld.insert(std::move(vec));
5837 ctx->allocated_vec.emplace(dst.id(), elems);
5838 return;
5839 }
5840 default:
5841 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5842 }
5843
5844 bld.mubuf(op, Definition(dst), rsrc, offset, ctx->program->scratch_offset, 0, true);
5845 emit_split_vector(ctx, dst, instr->num_components);
5846 }
5847
5848 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
5849 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
5850 Builder bld(ctx->program, ctx->block);
5851 Temp rsrc = get_scratch_resource(ctx);
5852 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5853 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5854
5855 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5856 unsigned writemask = nir_intrinsic_write_mask(instr);
5857
5858 while (writemask) {
5859 int start, count;
5860 u_bit_scan_consecutive_range(&writemask, &start, &count);
5861 int num_bytes = count * elem_size_bytes;
5862
5863 if (num_bytes > 16) {
5864 assert(elem_size_bytes == 8);
5865 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5866 count = 2;
5867 num_bytes = 16;
5868 }
5869
5870 // TODO: check alignment of sub-dword stores
5871 // TODO: split 3 bytes. there is no store instruction for that
5872
5873 Temp write_data;
5874 if (count != instr->num_components) {
5875 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5876 for (int i = 0; i < count; i++) {
5877 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
5878 vec->operands[i] = Operand(elem);
5879 }
5880 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
5881 vec->definitions[0] = Definition(write_data);
5882 ctx->block->instructions.emplace_back(std::move(vec));
5883 } else {
5884 write_data = data;
5885 }
5886
5887 aco_opcode op;
5888 switch (num_bytes) {
5889 case 4:
5890 op = aco_opcode::buffer_store_dword;
5891 break;
5892 case 8:
5893 op = aco_opcode::buffer_store_dwordx2;
5894 break;
5895 case 12:
5896 op = aco_opcode::buffer_store_dwordx3;
5897 break;
5898 case 16:
5899 op = aco_opcode::buffer_store_dwordx4;
5900 break;
5901 default:
5902 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5903 }
5904
5905 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
5906 }
5907 }
5908
5909 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
5910 uint8_t log2_ps_iter_samples;
5911 if (ctx->program->info->ps.force_persample) {
5912 log2_ps_iter_samples =
5913 util_logbase2(ctx->options->key.fs.num_samples);
5914 } else {
5915 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
5916 }
5917
5918 /* The bit pattern matches that used by fixed function fragment
5919 * processing. */
5920 static const unsigned ps_iter_masks[] = {
5921 0xffff, /* not used */
5922 0x5555,
5923 0x1111,
5924 0x0101,
5925 0x0001,
5926 };
5927 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
5928
5929 Builder bld(ctx->program, ctx->block);
5930
5931 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5932 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
5933 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
5934 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
5935 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5936 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
5937 }
5938
5939 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
5940 Builder bld(ctx->program, ctx->block);
5941
5942 unsigned stream = nir_intrinsic_stream_id(instr);
5943 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5944 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
5945 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
5946
5947 /* get GSVS ring */
5948 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
5949
5950 unsigned num_components =
5951 ctx->program->info->gs.num_stream_output_components[stream];
5952 assert(num_components);
5953
5954 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
5955 unsigned stream_offset = 0;
5956 for (unsigned i = 0; i < stream; i++) {
5957 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
5958 stream_offset += prev_stride * ctx->program->wave_size;
5959 }
5960
5961 /* Limit on the stride field for <= GFX7. */
5962 assert(stride < (1 << 14));
5963
5964 Temp gsvs_dwords[4];
5965 for (unsigned i = 0; i < 4; i++)
5966 gsvs_dwords[i] = bld.tmp(s1);
5967 bld.pseudo(aco_opcode::p_split_vector,
5968 Definition(gsvs_dwords[0]),
5969 Definition(gsvs_dwords[1]),
5970 Definition(gsvs_dwords[2]),
5971 Definition(gsvs_dwords[3]),
5972 gsvs_ring);
5973
5974 if (stream_offset) {
5975 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
5976
5977 Temp carry = bld.tmp(s1);
5978 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
5979 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));
5980 }
5981
5982 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)));
5983 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
5984
5985 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5986 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
5987
5988 unsigned offset = 0;
5989 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
5990 if (ctx->program->info->gs.output_streams[i] != stream)
5991 continue;
5992
5993 for (unsigned j = 0; j < 4; j++) {
5994 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
5995 continue;
5996
5997 if (ctx->outputs.mask[i] & (1 << j)) {
5998 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
5999 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6000 if (const_offset >= 4096u) {
6001 if (vaddr_offset.isUndefined())
6002 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6003 else
6004 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6005 const_offset %= 4096u;
6006 }
6007
6008 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6009 mtbuf->operands[0] = Operand(gsvs_ring);
6010 mtbuf->operands[1] = vaddr_offset;
6011 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6012 mtbuf->operands[3] = Operand(ctx->outputs.outputs[i][j]);
6013 mtbuf->offen = !vaddr_offset.isUndefined();
6014 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6015 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6016 mtbuf->offset = const_offset;
6017 mtbuf->glc = true;
6018 mtbuf->slc = true;
6019 mtbuf->barrier = barrier_gs_data;
6020 mtbuf->can_reorder = true;
6021 bld.insert(std::move(mtbuf));
6022 }
6023
6024 offset += ctx->shader->info.gs.vertices_out;
6025 }
6026
6027 /* outputs for the next vertex are undefined and keeping them around can
6028 * create invalid IR with control flow */
6029 ctx->outputs.mask[i] = 0;
6030 }
6031
6032 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6033 }
6034
6035 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6036 {
6037 Builder bld(ctx->program, ctx->block);
6038
6039 if (cluster_size == 1) {
6040 return src;
6041 } if (op == nir_op_iand && cluster_size == 4) {
6042 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6043 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6044 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6045 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6046 } else if (op == nir_op_ior && cluster_size == 4) {
6047 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6048 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6049 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6050 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6051 //subgroupAnd(val) -> (exec & ~val) == 0
6052 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6053 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6054 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6055 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6056 //subgroupOr(val) -> (val & exec) != 0
6057 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6058 return bool_to_vector_condition(ctx, tmp);
6059 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6060 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6061 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6062 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6063 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6064 return bool_to_vector_condition(ctx, tmp);
6065 } else {
6066 //subgroupClustered{And,Or,Xor}(val, n) ->
6067 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6068 //cluster_offset = ~(n - 1) & lane_id
6069 //cluster_mask = ((1 << n) - 1)
6070 //subgroupClusteredAnd():
6071 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6072 //subgroupClusteredOr():
6073 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6074 //subgroupClusteredXor():
6075 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6076 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6077 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6078
6079 Temp tmp;
6080 if (op == nir_op_iand)
6081 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6082 else
6083 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6084
6085 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6086
6087 if (ctx->program->chip_class <= GFX7)
6088 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6089 else if (ctx->program->wave_size == 64)
6090 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6091 else
6092 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6093 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6094 if (cluster_mask != 0xffffffff)
6095 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6096
6097 Definition cmp_def = Definition();
6098 if (op == nir_op_iand) {
6099 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6100 } else if (op == nir_op_ior) {
6101 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6102 } else if (op == nir_op_ixor) {
6103 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
6104 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
6105 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6106 }
6107 cmp_def.setHint(vcc);
6108 return cmp_def.getTemp();
6109 }
6110 }
6111
6112 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
6113 {
6114 Builder bld(ctx->program, ctx->block);
6115
6116 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
6117 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
6118 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
6119 Temp tmp;
6120 if (op == nir_op_iand)
6121 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6122 else
6123 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
6124
6125 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
6126 Temp lo = lohi.def(0).getTemp();
6127 Temp hi = lohi.def(1).getTemp();
6128 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
6129
6130 Definition cmp_def = Definition();
6131 if (op == nir_op_iand)
6132 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6133 else if (op == nir_op_ior)
6134 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6135 else if (op == nir_op_ixor)
6136 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
6137 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
6138 cmp_def.setHint(vcc);
6139 return cmp_def.getTemp();
6140 }
6141
6142 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
6143 {
6144 Builder bld(ctx->program, ctx->block);
6145
6146 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
6147 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
6148 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
6149 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
6150 if (op == nir_op_iand)
6151 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6152 else if (op == nir_op_ior)
6153 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6154 else if (op == nir_op_ixor)
6155 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6156
6157 assert(false);
6158 return Temp();
6159 }
6160
6161 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
6162 {
6163 Builder bld(ctx->program, ctx->block);
6164 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
6165 if (src.regClass().type() == RegType::vgpr) {
6166 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
6167 } else if (src.regClass() == s1) {
6168 bld.sop1(aco_opcode::s_mov_b32, dst, src);
6169 } else if (src.regClass() == s2) {
6170 bld.sop1(aco_opcode::s_mov_b64, dst, src);
6171 } else {
6172 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6173 nir_print_instr(&instr->instr, stderr);
6174 fprintf(stderr, "\n");
6175 }
6176 }
6177
6178 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
6179 {
6180 Builder bld(ctx->program, ctx->block);
6181 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
6182 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
6183 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
6184
6185 Temp ddx_1, ddx_2, ddy_1, ddy_2;
6186 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
6187 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
6188 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
6189
6190 /* Build DD X/Y */
6191 if (ctx->program->chip_class >= GFX8) {
6192 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
6193 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
6194 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
6195 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
6196 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
6197 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
6198 } else {
6199 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
6200 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
6201 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
6202 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
6203 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
6204 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
6205 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
6206 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
6207 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
6208 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
6209 }
6210
6211 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
6212 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
6213 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
6214 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
6215 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
6216 Temp wqm1 = bld.tmp(v1);
6217 emit_wqm(ctx, tmp1, wqm1, true);
6218 Temp wqm2 = bld.tmp(v1);
6219 emit_wqm(ctx, tmp2, wqm2, true);
6220 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
6221 return;
6222 }
6223
6224 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
6225 {
6226 Builder bld(ctx->program, ctx->block);
6227 switch(instr->intrinsic) {
6228 case nir_intrinsic_load_barycentric_sample:
6229 case nir_intrinsic_load_barycentric_pixel:
6230 case nir_intrinsic_load_barycentric_centroid: {
6231 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
6232 Temp bary = Temp(0, s2);
6233 switch (mode) {
6234 case INTERP_MODE_SMOOTH:
6235 case INTERP_MODE_NONE:
6236 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
6237 bary = get_arg(ctx, ctx->args->ac.persp_center);
6238 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
6239 bary = ctx->persp_centroid;
6240 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
6241 bary = get_arg(ctx, ctx->args->ac.persp_sample);
6242 break;
6243 case INTERP_MODE_NOPERSPECTIVE:
6244 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
6245 bary = get_arg(ctx, ctx->args->ac.linear_center);
6246 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
6247 bary = ctx->linear_centroid;
6248 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
6249 bary = get_arg(ctx, ctx->args->ac.linear_sample);
6250 break;
6251 default:
6252 break;
6253 }
6254 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6255 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
6256 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
6257 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6258 Operand(p1), Operand(p2));
6259 emit_split_vector(ctx, dst, 2);
6260 break;
6261 }
6262 case nir_intrinsic_load_barycentric_model: {
6263 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
6264
6265 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6266 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
6267 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
6268 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
6269 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6270 Operand(p1), Operand(p2), Operand(p3));
6271 emit_split_vector(ctx, dst, 3);
6272 break;
6273 }
6274 case nir_intrinsic_load_barycentric_at_sample: {
6275 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
6276 switch (ctx->options->key.fs.num_samples) {
6277 case 2: sample_pos_offset += 1 << 3; break;
6278 case 4: sample_pos_offset += 3 << 3; break;
6279 case 8: sample_pos_offset += 7 << 3; break;
6280 default: break;
6281 }
6282 Temp sample_pos;
6283 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6284 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
6285 Temp private_segment_buffer = ctx->program->private_segment_buffer;
6286 if (addr.type() == RegType::sgpr) {
6287 Operand offset;
6288 if (const_addr) {
6289 sample_pos_offset += const_addr->u32 << 3;
6290 offset = Operand(sample_pos_offset);
6291 } else if (ctx->options->chip_class >= GFX9) {
6292 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
6293 } else {
6294 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
6295 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
6296 }
6297
6298 Operand off = bld.copy(bld.def(s1), Operand(offset));
6299 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
6300
6301 } else if (ctx->options->chip_class >= GFX9) {
6302 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
6303 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
6304 } else if (ctx->options->chip_class >= GFX7) {
6305 /* addr += private_segment_buffer + sample_pos_offset */
6306 Temp tmp0 = bld.tmp(s1);
6307 Temp tmp1 = bld.tmp(s1);
6308 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
6309 Definition scc_tmp = bld.def(s1, scc);
6310 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
6311 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
6312 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
6313 Temp pck0 = bld.tmp(v1);
6314 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
6315 tmp1 = as_vgpr(ctx, tmp1);
6316 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);
6317 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
6318
6319 /* sample_pos = flat_load_dwordx2 addr */
6320 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
6321 } else {
6322 assert(ctx->options->chip_class == GFX6);
6323
6324 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6325 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6326 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
6327
6328 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
6329 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
6330
6331 sample_pos = bld.tmp(v2);
6332
6333 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
6334 load->definitions[0] = Definition(sample_pos);
6335 load->operands[0] = Operand(rsrc);
6336 load->operands[1] = Operand(addr);
6337 load->operands[2] = Operand(0u);
6338 load->offset = sample_pos_offset;
6339 load->offen = 0;
6340 load->addr64 = true;
6341 load->glc = false;
6342 load->dlc = false;
6343 load->disable_wqm = false;
6344 load->barrier = barrier_none;
6345 load->can_reorder = true;
6346 ctx->block->instructions.emplace_back(std::move(load));
6347 }
6348
6349 /* sample_pos -= 0.5 */
6350 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
6351 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
6352 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
6353 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
6354 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
6355
6356 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
6357 break;
6358 }
6359 case nir_intrinsic_load_barycentric_at_offset: {
6360 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
6361 RegClass rc = RegClass(offset.type(), 1);
6362 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
6363 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
6364 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
6365 break;
6366 }
6367 case nir_intrinsic_load_front_face: {
6368 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6369 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
6370 break;
6371 }
6372 case nir_intrinsic_load_view_index: {
6373 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
6374 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6375 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
6376 break;
6377 }
6378
6379 /* fallthrough */
6380 }
6381 case nir_intrinsic_load_layer_id: {
6382 unsigned idx = nir_intrinsic_base(instr);
6383 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6384 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
6385 break;
6386 }
6387 case nir_intrinsic_load_frag_coord: {
6388 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
6389 break;
6390 }
6391 case nir_intrinsic_load_sample_pos: {
6392 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
6393 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
6394 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6395 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
6396 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
6397 break;
6398 }
6399 case nir_intrinsic_load_tess_coord:
6400 visit_load_tess_coord(ctx, instr);
6401 break;
6402 case nir_intrinsic_load_interpolated_input:
6403 visit_load_interpolated_input(ctx, instr);
6404 break;
6405 case nir_intrinsic_store_output:
6406 visit_store_output(ctx, instr);
6407 break;
6408 case nir_intrinsic_load_input:
6409 case nir_intrinsic_load_input_vertex:
6410 visit_load_input(ctx, instr);
6411 break;
6412 case nir_intrinsic_load_per_vertex_input:
6413 visit_load_per_vertex_input(ctx, instr);
6414 break;
6415 case nir_intrinsic_load_ubo:
6416 visit_load_ubo(ctx, instr);
6417 break;
6418 case nir_intrinsic_load_push_constant:
6419 visit_load_push_constant(ctx, instr);
6420 break;
6421 case nir_intrinsic_load_constant:
6422 visit_load_constant(ctx, instr);
6423 break;
6424 case nir_intrinsic_vulkan_resource_index:
6425 visit_load_resource(ctx, instr);
6426 break;
6427 case nir_intrinsic_discard:
6428 visit_discard(ctx, instr);
6429 break;
6430 case nir_intrinsic_discard_if:
6431 visit_discard_if(ctx, instr);
6432 break;
6433 case nir_intrinsic_load_shared:
6434 visit_load_shared(ctx, instr);
6435 break;
6436 case nir_intrinsic_store_shared:
6437 visit_store_shared(ctx, instr);
6438 break;
6439 case nir_intrinsic_shared_atomic_add:
6440 case nir_intrinsic_shared_atomic_imin:
6441 case nir_intrinsic_shared_atomic_umin:
6442 case nir_intrinsic_shared_atomic_imax:
6443 case nir_intrinsic_shared_atomic_umax:
6444 case nir_intrinsic_shared_atomic_and:
6445 case nir_intrinsic_shared_atomic_or:
6446 case nir_intrinsic_shared_atomic_xor:
6447 case nir_intrinsic_shared_atomic_exchange:
6448 case nir_intrinsic_shared_atomic_comp_swap:
6449 visit_shared_atomic(ctx, instr);
6450 break;
6451 case nir_intrinsic_image_deref_load:
6452 visit_image_load(ctx, instr);
6453 break;
6454 case nir_intrinsic_image_deref_store:
6455 visit_image_store(ctx, instr);
6456 break;
6457 case nir_intrinsic_image_deref_atomic_add:
6458 case nir_intrinsic_image_deref_atomic_umin:
6459 case nir_intrinsic_image_deref_atomic_imin:
6460 case nir_intrinsic_image_deref_atomic_umax:
6461 case nir_intrinsic_image_deref_atomic_imax:
6462 case nir_intrinsic_image_deref_atomic_and:
6463 case nir_intrinsic_image_deref_atomic_or:
6464 case nir_intrinsic_image_deref_atomic_xor:
6465 case nir_intrinsic_image_deref_atomic_exchange:
6466 case nir_intrinsic_image_deref_atomic_comp_swap:
6467 visit_image_atomic(ctx, instr);
6468 break;
6469 case nir_intrinsic_image_deref_size:
6470 visit_image_size(ctx, instr);
6471 break;
6472 case nir_intrinsic_load_ssbo:
6473 visit_load_ssbo(ctx, instr);
6474 break;
6475 case nir_intrinsic_store_ssbo:
6476 visit_store_ssbo(ctx, instr);
6477 break;
6478 case nir_intrinsic_load_global:
6479 visit_load_global(ctx, instr);
6480 break;
6481 case nir_intrinsic_store_global:
6482 visit_store_global(ctx, instr);
6483 break;
6484 case nir_intrinsic_global_atomic_add:
6485 case nir_intrinsic_global_atomic_imin:
6486 case nir_intrinsic_global_atomic_umin:
6487 case nir_intrinsic_global_atomic_imax:
6488 case nir_intrinsic_global_atomic_umax:
6489 case nir_intrinsic_global_atomic_and:
6490 case nir_intrinsic_global_atomic_or:
6491 case nir_intrinsic_global_atomic_xor:
6492 case nir_intrinsic_global_atomic_exchange:
6493 case nir_intrinsic_global_atomic_comp_swap:
6494 visit_global_atomic(ctx, instr);
6495 break;
6496 case nir_intrinsic_ssbo_atomic_add:
6497 case nir_intrinsic_ssbo_atomic_imin:
6498 case nir_intrinsic_ssbo_atomic_umin:
6499 case nir_intrinsic_ssbo_atomic_imax:
6500 case nir_intrinsic_ssbo_atomic_umax:
6501 case nir_intrinsic_ssbo_atomic_and:
6502 case nir_intrinsic_ssbo_atomic_or:
6503 case nir_intrinsic_ssbo_atomic_xor:
6504 case nir_intrinsic_ssbo_atomic_exchange:
6505 case nir_intrinsic_ssbo_atomic_comp_swap:
6506 visit_atomic_ssbo(ctx, instr);
6507 break;
6508 case nir_intrinsic_load_scratch:
6509 visit_load_scratch(ctx, instr);
6510 break;
6511 case nir_intrinsic_store_scratch:
6512 visit_store_scratch(ctx, instr);
6513 break;
6514 case nir_intrinsic_get_buffer_size:
6515 visit_get_buffer_size(ctx, instr);
6516 break;
6517 case nir_intrinsic_control_barrier: {
6518 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
6519 /* GFX6 only (thanks to a hw bug workaround):
6520 * The real barrier instruction isn’t needed, because an entire patch
6521 * always fits into a single wave.
6522 */
6523 break;
6524 }
6525
6526 if (ctx->shader->info.stage == MESA_SHADER_COMPUTE) {
6527 unsigned* bsize = ctx->program->info->cs.block_size;
6528 unsigned workgroup_size = bsize[0] * bsize[1] * bsize[2];
6529 if (workgroup_size > ctx->program->wave_size)
6530 bld.sopp(aco_opcode::s_barrier);
6531 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
6532 /* For each patch provided during rendering, n​ TCS shader invocations will be processed,
6533 * where n​ is the number of vertices in the output patch.
6534 */
6535 unsigned workgroup_size = ctx->tcs_num_patches * ctx->shader->info.tess.tcs_vertices_out;
6536 if (workgroup_size > ctx->program->wave_size)
6537 bld.sopp(aco_opcode::s_barrier);
6538 } else {
6539 /* We don't know the workgroup size, so always emit the s_barrier. */
6540 bld.sopp(aco_opcode::s_barrier);
6541 }
6542
6543 break;
6544 }
6545 case nir_intrinsic_memory_barrier_tcs_patch:
6546 case nir_intrinsic_group_memory_barrier:
6547 case nir_intrinsic_memory_barrier:
6548 case nir_intrinsic_memory_barrier_buffer:
6549 case nir_intrinsic_memory_barrier_image:
6550 case nir_intrinsic_memory_barrier_shared:
6551 emit_memory_barrier(ctx, instr);
6552 break;
6553 case nir_intrinsic_load_num_work_groups: {
6554 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6555 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
6556 emit_split_vector(ctx, dst, 3);
6557 break;
6558 }
6559 case nir_intrinsic_load_local_invocation_id: {
6560 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6561 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
6562 emit_split_vector(ctx, dst, 3);
6563 break;
6564 }
6565 case nir_intrinsic_load_work_group_id: {
6566 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6567 struct ac_arg *args = ctx->args->ac.workgroup_ids;
6568 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6569 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
6570 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
6571 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
6572 emit_split_vector(ctx, dst, 3);
6573 break;
6574 }
6575 case nir_intrinsic_load_local_invocation_index: {
6576 Temp id = emit_mbcnt(ctx, bld.def(v1));
6577
6578 /* The tg_size bits [6:11] contain the subgroup id,
6579 * we need this multiplied by the wave size, and then OR the thread id to it.
6580 */
6581 if (ctx->program->wave_size == 64) {
6582 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
6583 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
6584 get_arg(ctx, ctx->args->ac.tg_size));
6585 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
6586 } else {
6587 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
6588 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
6589 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
6590 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
6591 }
6592 break;
6593 }
6594 case nir_intrinsic_load_subgroup_id: {
6595 if (ctx->stage == compute_cs) {
6596 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
6597 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
6598 } else {
6599 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
6600 }
6601 break;
6602 }
6603 case nir_intrinsic_load_subgroup_invocation: {
6604 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
6605 break;
6606 }
6607 case nir_intrinsic_load_num_subgroups: {
6608 if (ctx->stage == compute_cs)
6609 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
6610 get_arg(ctx, ctx->args->ac.tg_size));
6611 else
6612 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
6613 break;
6614 }
6615 case nir_intrinsic_ballot: {
6616 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6617 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6618 Definition tmp = bld.def(dst.regClass());
6619 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
6620 if (instr->src[0].ssa->bit_size == 1) {
6621 assert(src.regClass() == bld.lm);
6622 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
6623 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
6624 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
6625 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
6626 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
6627 } else {
6628 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6629 nir_print_instr(&instr->instr, stderr);
6630 fprintf(stderr, "\n");
6631 }
6632 if (dst.size() != bld.lm.size()) {
6633 /* Wave32 with ballot size set to 64 */
6634 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
6635 }
6636 emit_wqm(ctx, tmp.getTemp(), dst);
6637 break;
6638 }
6639 case nir_intrinsic_shuffle:
6640 case nir_intrinsic_read_invocation: {
6641 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6642 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
6643 emit_uniform_subgroup(ctx, instr, src);
6644 } else {
6645 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
6646 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
6647 tid = bld.as_uniform(tid);
6648 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6649 if (src.regClass() == v1) {
6650 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
6651 } else if (src.regClass() == v2) {
6652 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6653 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6654 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
6655 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
6656 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6657 emit_split_vector(ctx, dst, 2);
6658 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
6659 assert(src.regClass() == bld.lm);
6660 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
6661 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
6662 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
6663 assert(src.regClass() == bld.lm);
6664 Temp tmp;
6665 if (ctx->program->chip_class <= GFX7)
6666 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
6667 else if (ctx->program->wave_size == 64)
6668 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
6669 else
6670 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
6671 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6672 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
6673 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
6674 } else {
6675 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6676 nir_print_instr(&instr->instr, stderr);
6677 fprintf(stderr, "\n");
6678 }
6679 }
6680 break;
6681 }
6682 case nir_intrinsic_load_sample_id: {
6683 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
6684 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6685 break;
6686 }
6687 case nir_intrinsic_load_sample_mask_in: {
6688 visit_load_sample_mask_in(ctx, instr);
6689 break;
6690 }
6691 case nir_intrinsic_read_first_invocation: {
6692 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6693 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6694 if (src.regClass() == v1) {
6695 emit_wqm(ctx,
6696 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
6697 dst);
6698 } else if (src.regClass() == v2) {
6699 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6700 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6701 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
6702 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
6703 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6704 emit_split_vector(ctx, dst, 2);
6705 } else if (instr->dest.ssa.bit_size == 1) {
6706 assert(src.regClass() == bld.lm);
6707 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
6708 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
6709 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
6710 } else if (src.regClass() == s1) {
6711 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
6712 } else if (src.regClass() == s2) {
6713 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
6714 } else {
6715 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6716 nir_print_instr(&instr->instr, stderr);
6717 fprintf(stderr, "\n");
6718 }
6719 break;
6720 }
6721 case nir_intrinsic_vote_all: {
6722 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6723 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6724 assert(src.regClass() == bld.lm);
6725 assert(dst.regClass() == bld.lm);
6726
6727 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6728 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6729 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
6730 break;
6731 }
6732 case nir_intrinsic_vote_any: {
6733 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6734 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6735 assert(src.regClass() == bld.lm);
6736 assert(dst.regClass() == bld.lm);
6737
6738 Temp tmp = bool_to_scalar_condition(ctx, src);
6739 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
6740 break;
6741 }
6742 case nir_intrinsic_reduce:
6743 case nir_intrinsic_inclusive_scan:
6744 case nir_intrinsic_exclusive_scan: {
6745 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6746 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6747 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
6748 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
6749 nir_intrinsic_cluster_size(instr) : 0;
6750 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
6751
6752 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
6753 emit_uniform_subgroup(ctx, instr, src);
6754 } else if (instr->dest.ssa.bit_size == 1) {
6755 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
6756 op = nir_op_iand;
6757 else if (op == nir_op_iadd)
6758 op = nir_op_ixor;
6759 else if (op == nir_op_umax || op == nir_op_imax)
6760 op = nir_op_ior;
6761 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
6762
6763 switch (instr->intrinsic) {
6764 case nir_intrinsic_reduce:
6765 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
6766 break;
6767 case nir_intrinsic_exclusive_scan:
6768 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
6769 break;
6770 case nir_intrinsic_inclusive_scan:
6771 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
6772 break;
6773 default:
6774 assert(false);
6775 }
6776 } else if (cluster_size == 1) {
6777 bld.copy(Definition(dst), src);
6778 } else {
6779 src = as_vgpr(ctx, src);
6780
6781 ReduceOp reduce_op;
6782 switch (op) {
6783 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
6784 CASE(iadd)
6785 CASE(imul)
6786 CASE(fadd)
6787 CASE(fmul)
6788 CASE(imin)
6789 CASE(umin)
6790 CASE(fmin)
6791 CASE(imax)
6792 CASE(umax)
6793 CASE(fmax)
6794 CASE(iand)
6795 CASE(ior)
6796 CASE(ixor)
6797 default:
6798 unreachable("unknown reduction op");
6799 #undef CASE
6800 }
6801
6802 aco_opcode aco_op;
6803 switch (instr->intrinsic) {
6804 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
6805 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
6806 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
6807 default:
6808 unreachable("unknown reduce intrinsic");
6809 }
6810
6811 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
6812 reduce->operands[0] = Operand(src);
6813 // filled in by aco_reduce_assign.cpp, used internally as part of the
6814 // reduce sequence
6815 assert(dst.size() == 1 || dst.size() == 2);
6816 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
6817 reduce->operands[2] = Operand(v1.as_linear());
6818
6819 Temp tmp_dst = bld.tmp(dst.regClass());
6820 reduce->definitions[0] = Definition(tmp_dst);
6821 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
6822 reduce->definitions[2] = Definition();
6823 reduce->definitions[3] = Definition(scc, s1);
6824 reduce->definitions[4] = Definition();
6825 reduce->reduce_op = reduce_op;
6826 reduce->cluster_size = cluster_size;
6827 ctx->block->instructions.emplace_back(std::move(reduce));
6828
6829 emit_wqm(ctx, tmp_dst, dst);
6830 }
6831 break;
6832 }
6833 case nir_intrinsic_quad_broadcast: {
6834 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6835 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6836 emit_uniform_subgroup(ctx, instr, src);
6837 } else {
6838 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6839 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
6840 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
6841
6842 if (instr->dest.ssa.bit_size == 1) {
6843 assert(src.regClass() == bld.lm);
6844 assert(dst.regClass() == bld.lm);
6845 uint32_t half_mask = 0x11111111u << lane;
6846 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
6847 Temp tmp = bld.tmp(bld.lm);
6848 bld.sop1(Builder::s_wqm, Definition(tmp),
6849 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
6850 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
6851 emit_wqm(ctx, tmp, dst);
6852 } else if (instr->dest.ssa.bit_size == 32) {
6853 if (ctx->program->chip_class >= GFX8)
6854 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
6855 else
6856 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
6857 } else if (instr->dest.ssa.bit_size == 64) {
6858 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6859 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6860 if (ctx->program->chip_class >= GFX8) {
6861 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
6862 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
6863 } else {
6864 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
6865 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
6866 }
6867 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6868 emit_split_vector(ctx, dst, 2);
6869 } else {
6870 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6871 nir_print_instr(&instr->instr, stderr);
6872 fprintf(stderr, "\n");
6873 }
6874 }
6875 break;
6876 }
6877 case nir_intrinsic_quad_swap_horizontal:
6878 case nir_intrinsic_quad_swap_vertical:
6879 case nir_intrinsic_quad_swap_diagonal:
6880 case nir_intrinsic_quad_swizzle_amd: {
6881 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6882 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6883 emit_uniform_subgroup(ctx, instr, src);
6884 break;
6885 }
6886 uint16_t dpp_ctrl = 0;
6887 switch (instr->intrinsic) {
6888 case nir_intrinsic_quad_swap_horizontal:
6889 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
6890 break;
6891 case nir_intrinsic_quad_swap_vertical:
6892 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
6893 break;
6894 case nir_intrinsic_quad_swap_diagonal:
6895 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
6896 break;
6897 case nir_intrinsic_quad_swizzle_amd:
6898 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
6899 break;
6900 default:
6901 break;
6902 }
6903 if (ctx->program->chip_class < GFX8)
6904 dpp_ctrl |= (1 << 15);
6905
6906 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6907 if (instr->dest.ssa.bit_size == 1) {
6908 assert(src.regClass() == bld.lm);
6909 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
6910 if (ctx->program->chip_class >= GFX8)
6911 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
6912 else
6913 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
6914 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
6915 emit_wqm(ctx, tmp, dst);
6916 } else if (instr->dest.ssa.bit_size == 32) {
6917 Temp tmp;
6918 if (ctx->program->chip_class >= GFX8)
6919 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
6920 else
6921 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
6922 emit_wqm(ctx, tmp, dst);
6923 } else if (instr->dest.ssa.bit_size == 64) {
6924 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6925 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6926 if (ctx->program->chip_class >= GFX8) {
6927 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
6928 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
6929 } else {
6930 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
6931 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
6932 }
6933 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6934 emit_split_vector(ctx, dst, 2);
6935 } else {
6936 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6937 nir_print_instr(&instr->instr, stderr);
6938 fprintf(stderr, "\n");
6939 }
6940 break;
6941 }
6942 case nir_intrinsic_masked_swizzle_amd: {
6943 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6944 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
6945 emit_uniform_subgroup(ctx, instr, src);
6946 break;
6947 }
6948 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6949 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
6950 if (dst.regClass() == v1) {
6951 emit_wqm(ctx,
6952 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
6953 dst);
6954 } else if (dst.regClass() == v2) {
6955 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
6956 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
6957 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
6958 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
6959 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6960 emit_split_vector(ctx, dst, 2);
6961 } else {
6962 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6963 nir_print_instr(&instr->instr, stderr);
6964 fprintf(stderr, "\n");
6965 }
6966 break;
6967 }
6968 case nir_intrinsic_write_invocation_amd: {
6969 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6970 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
6971 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
6972 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6973 if (dst.regClass() == v1) {
6974 /* src2 is ignored for writelane. RA assigns the same reg for dst */
6975 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
6976 } else if (dst.regClass() == v2) {
6977 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
6978 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
6979 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
6980 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
6981 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
6982 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
6983 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
6984 emit_split_vector(ctx, dst, 2);
6985 } else {
6986 fprintf(stderr, "Unimplemented NIR instr bit size: ");
6987 nir_print_instr(&instr->instr, stderr);
6988 fprintf(stderr, "\n");
6989 }
6990 break;
6991 }
6992 case nir_intrinsic_mbcnt_amd: {
6993 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
6994 RegClass rc = RegClass(src.type(), 1);
6995 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
6996 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
6997 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6998 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
6999 emit_wqm(ctx, wqm_tmp, dst);
7000 break;
7001 }
7002 case nir_intrinsic_load_helper_invocation: {
7003 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7004 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7005 ctx->block->kind |= block_kind_needs_lowering;
7006 ctx->program->needs_exact = true;
7007 break;
7008 }
7009 case nir_intrinsic_is_helper_invocation: {
7010 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7011 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7012 ctx->block->kind |= block_kind_needs_lowering;
7013 ctx->program->needs_exact = true;
7014 break;
7015 }
7016 case nir_intrinsic_demote:
7017 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7018
7019 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7020 ctx->cf_info.exec_potentially_empty_discard = true;
7021 ctx->block->kind |= block_kind_uses_demote;
7022 ctx->program->needs_exact = true;
7023 break;
7024 case nir_intrinsic_demote_if: {
7025 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7026 assert(src.regClass() == bld.lm);
7027 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7028 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7029
7030 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7031 ctx->cf_info.exec_potentially_empty_discard = true;
7032 ctx->block->kind |= block_kind_uses_demote;
7033 ctx->program->needs_exact = true;
7034 break;
7035 }
7036 case nir_intrinsic_first_invocation: {
7037 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7038 get_ssa_temp(ctx, &instr->dest.ssa));
7039 break;
7040 }
7041 case nir_intrinsic_shader_clock:
7042 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7043 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7044 break;
7045 case nir_intrinsic_load_vertex_id_zero_base: {
7046 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7047 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7048 break;
7049 }
7050 case nir_intrinsic_load_first_vertex: {
7051 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7052 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7053 break;
7054 }
7055 case nir_intrinsic_load_base_instance: {
7056 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7057 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7058 break;
7059 }
7060 case nir_intrinsic_load_instance_id: {
7061 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7062 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7063 break;
7064 }
7065 case nir_intrinsic_load_draw_id: {
7066 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7067 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7068 break;
7069 }
7070 case nir_intrinsic_load_invocation_id: {
7071 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7072
7073 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7074 if (ctx->options->chip_class >= GFX10)
7075 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7076 else
7077 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7078 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7079 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7080 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7081 } else {
7082 unreachable("Unsupported stage for load_invocation_id");
7083 }
7084
7085 break;
7086 }
7087 case nir_intrinsic_load_primitive_id: {
7088 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7089
7090 switch (ctx->shader->info.stage) {
7091 case MESA_SHADER_GEOMETRY:
7092 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7093 break;
7094 case MESA_SHADER_TESS_CTRL:
7095 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7096 break;
7097 case MESA_SHADER_TESS_EVAL:
7098 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7099 break;
7100 default:
7101 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7102 }
7103
7104 break;
7105 }
7106 case nir_intrinsic_load_patch_vertices_in: {
7107 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
7108 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
7109
7110 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7111 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
7112 break;
7113 }
7114 case nir_intrinsic_emit_vertex_with_counter: {
7115 visit_emit_vertex_with_counter(ctx, instr);
7116 break;
7117 }
7118 case nir_intrinsic_end_primitive_with_counter: {
7119 unsigned stream = nir_intrinsic_stream_id(instr);
7120 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
7121 break;
7122 }
7123 case nir_intrinsic_set_vertex_count: {
7124 /* unused, the HW keeps track of this for us */
7125 break;
7126 }
7127 default:
7128 fprintf(stderr, "Unimplemented intrinsic instr: ");
7129 nir_print_instr(&instr->instr, stderr);
7130 fprintf(stderr, "\n");
7131 abort();
7132
7133 break;
7134 }
7135 }
7136
7137
7138 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
7139 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
7140 enum glsl_base_type *stype)
7141 {
7142 nir_deref_instr *texture_deref_instr = NULL;
7143 nir_deref_instr *sampler_deref_instr = NULL;
7144 int plane = -1;
7145
7146 for (unsigned i = 0; i < instr->num_srcs; i++) {
7147 switch (instr->src[i].src_type) {
7148 case nir_tex_src_texture_deref:
7149 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
7150 break;
7151 case nir_tex_src_sampler_deref:
7152 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
7153 break;
7154 case nir_tex_src_plane:
7155 plane = nir_src_as_int(instr->src[i].src);
7156 break;
7157 default:
7158 break;
7159 }
7160 }
7161
7162 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
7163
7164 if (!sampler_deref_instr)
7165 sampler_deref_instr = texture_deref_instr;
7166
7167 if (plane >= 0) {
7168 assert(instr->op != nir_texop_txf_ms &&
7169 instr->op != nir_texop_samples_identical);
7170 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
7171 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
7172 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
7173 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
7174 } else if (instr->op == nir_texop_fragment_mask_fetch) {
7175 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
7176 } else {
7177 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
7178 }
7179 if (samp_ptr) {
7180 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
7181
7182 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
7183 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
7184 Builder bld(ctx->program, ctx->block);
7185
7186 /* to avoid unnecessary moves, we split and recombine sampler and image */
7187 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
7188 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
7189 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
7190 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
7191 Definition(img[2]), Definition(img[3]), Definition(img[4]),
7192 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
7193 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
7194 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
7195
7196 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
7197 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
7198 img[0], img[1], img[2], img[3],
7199 img[4], img[5], img[6], img[7]);
7200 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
7201 samp[0], samp[1], samp[2], samp[3]);
7202 }
7203 }
7204 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
7205 instr->op == nir_texop_samples_identical))
7206 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
7207 }
7208
7209 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
7210 Temp *out_ma, Temp *out_sc, Temp *out_tc)
7211 {
7212 Builder bld(ctx->program, ctx->block);
7213
7214 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
7215 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
7216 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
7217
7218 Operand neg_one(0xbf800000u);
7219 Operand one(0x3f800000u);
7220 Operand two(0x40000000u);
7221 Operand four(0x40800000u);
7222
7223 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
7224 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
7225 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
7226
7227 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
7228 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
7229 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
7230 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);
7231
7232 // select sc
7233 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
7234 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
7235 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
7236 one, is_ma_y);
7237 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
7238
7239 // select tc
7240 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
7241 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
7242 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
7243
7244 // select ma
7245 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7246 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
7247 deriv_z, is_ma_z);
7248 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
7249 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
7250 }
7251
7252 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
7253 {
7254 Builder bld(ctx->program, ctx->block);
7255 Temp ma, tc, sc, id;
7256
7257 if (is_array) {
7258 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
7259
7260 // see comment in ac_prepare_cube_coords()
7261 if (ctx->options->chip_class <= GFX8)
7262 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
7263 }
7264
7265 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7266
7267 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
7268 vop3a->operands[0] = Operand(ma);
7269 vop3a->abs[0] = true;
7270 Temp invma = bld.tmp(v1);
7271 vop3a->definitions[0] = Definition(invma);
7272 ctx->block->instructions.emplace_back(std::move(vop3a));
7273
7274 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7275 if (!is_deriv)
7276 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
7277
7278 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7279 if (!is_deriv)
7280 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
7281
7282 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
7283
7284 if (is_deriv) {
7285 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
7286 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
7287
7288 for (unsigned i = 0; i < 2; i++) {
7289 // see comment in ac_prepare_cube_coords()
7290 Temp deriv_ma;
7291 Temp deriv_sc, deriv_tc;
7292 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
7293 &deriv_ma, &deriv_sc, &deriv_tc);
7294
7295 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
7296
7297 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
7298 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
7299 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
7300 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
7301 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
7302 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
7303 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
7304 }
7305
7306 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
7307 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
7308 }
7309
7310 if (is_array)
7311 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
7312 coords.resize(3);
7313 coords[0] = sc;
7314 coords[1] = tc;
7315 coords[2] = id;
7316 }
7317
7318 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
7319 {
7320 if (vec->parent_instr->type != nir_instr_type_alu)
7321 return;
7322 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
7323 if (vec_instr->op != nir_op_vec(vec->num_components))
7324 return;
7325
7326 for (unsigned i = 0; i < vec->num_components; i++) {
7327 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
7328 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
7329 }
7330 }
7331
7332 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
7333 {
7334 Builder bld(ctx->program, ctx->block);
7335 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
7336 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
7337 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
7338 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
7339 std::vector<Temp> coords;
7340 std::vector<Temp> derivs;
7341 nir_const_value *sample_index_cv = NULL;
7342 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
7343 enum glsl_base_type stype;
7344 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
7345
7346 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
7347 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
7348 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
7349 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
7350
7351 for (unsigned i = 0; i < instr->num_srcs; i++) {
7352 switch (instr->src[i].src_type) {
7353 case nir_tex_src_coord: {
7354 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
7355 for (unsigned i = 0; i < coord.size(); i++)
7356 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
7357 break;
7358 }
7359 case nir_tex_src_bias:
7360 if (instr->op == nir_texop_txb) {
7361 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
7362 has_bias = true;
7363 }
7364 break;
7365 case nir_tex_src_lod: {
7366 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
7367
7368 if (val && val->f32 <= 0.0) {
7369 level_zero = true;
7370 } else {
7371 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
7372 has_lod = true;
7373 }
7374 break;
7375 }
7376 case nir_tex_src_comparator:
7377 if (instr->is_shadow) {
7378 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
7379 has_compare = true;
7380 }
7381 break;
7382 case nir_tex_src_offset:
7383 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
7384 get_const_vec(instr->src[i].src.ssa, const_offset);
7385 has_offset = true;
7386 break;
7387 case nir_tex_src_ddx:
7388 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
7389 has_ddx = true;
7390 break;
7391 case nir_tex_src_ddy:
7392 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
7393 has_ddy = true;
7394 break;
7395 case nir_tex_src_ms_index:
7396 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
7397 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
7398 has_sample_index = true;
7399 break;
7400 case nir_tex_src_texture_offset:
7401 case nir_tex_src_sampler_offset:
7402 default:
7403 break;
7404 }
7405 }
7406
7407 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
7408 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
7409
7410 if (instr->op == nir_texop_texture_samples) {
7411 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
7412
7413 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
7414 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
7415 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 */));
7416 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
7417
7418 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7419 samples, Operand(1u), bld.scc(is_msaa));
7420 return;
7421 }
7422
7423 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
7424 aco_ptr<Instruction> tmp_instr;
7425 Temp acc, pack = Temp();
7426
7427 uint32_t pack_const = 0;
7428 for (unsigned i = 0; i < offset.size(); i++) {
7429 if (!const_offset[i])
7430 continue;
7431 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
7432 }
7433
7434 if (offset.type() == RegType::sgpr) {
7435 for (unsigned i = 0; i < offset.size(); i++) {
7436 if (const_offset[i])
7437 continue;
7438
7439 acc = emit_extract_vector(ctx, offset, i, s1);
7440 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
7441
7442 if (i) {
7443 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
7444 }
7445
7446 if (pack == Temp()) {
7447 pack = acc;
7448 } else {
7449 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
7450 }
7451 }
7452
7453 if (pack_const && pack != Temp())
7454 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
7455 } else {
7456 for (unsigned i = 0; i < offset.size(); i++) {
7457 if (const_offset[i])
7458 continue;
7459
7460 acc = emit_extract_vector(ctx, offset, i, v1);
7461 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
7462
7463 if (i) {
7464 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
7465 }
7466
7467 if (pack == Temp()) {
7468 pack = acc;
7469 } else {
7470 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
7471 }
7472 }
7473
7474 if (pack_const && pack != Temp())
7475 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
7476 }
7477 if (pack_const && pack == Temp())
7478 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
7479 else if (pack == Temp())
7480 has_offset = false;
7481 else
7482 offset = pack;
7483 }
7484
7485 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
7486 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
7487
7488 /* pack derivatives */
7489 if (has_ddx || has_ddy) {
7490 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
7491 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
7492 Temp zero = bld.copy(bld.def(v1), Operand(0u));
7493 derivs = {ddy, zero, ddy, zero};
7494 } else {
7495 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
7496 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
7497 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
7498 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
7499 }
7500 has_derivs = true;
7501 }
7502
7503 if (instr->coord_components > 1 &&
7504 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
7505 instr->is_array &&
7506 instr->op != nir_texop_txf)
7507 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
7508
7509 if (instr->coord_components > 2 &&
7510 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
7511 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
7512 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
7513 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
7514 instr->is_array &&
7515 instr->op != nir_texop_txf &&
7516 instr->op != nir_texop_txf_ms &&
7517 instr->op != nir_texop_fragment_fetch &&
7518 instr->op != nir_texop_fragment_mask_fetch)
7519 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
7520
7521 if (ctx->options->chip_class == GFX9 &&
7522 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
7523 instr->op != nir_texop_lod && instr->coord_components) {
7524 assert(coords.size() > 0 && coords.size() < 3);
7525
7526 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
7527 Operand((uint32_t) 0) :
7528 Operand((uint32_t) 0x3f000000)));
7529 }
7530
7531 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
7532
7533 if (instr->op == nir_texop_samples_identical)
7534 resource = fmask_ptr;
7535
7536 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
7537 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
7538 instr->op != nir_texop_txs &&
7539 instr->op != nir_texop_fragment_fetch &&
7540 instr->op != nir_texop_fragment_mask_fetch) {
7541 assert(has_sample_index);
7542 Operand op(sample_index);
7543 if (sample_index_cv)
7544 op = Operand(sample_index_cv->u32);
7545 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
7546 }
7547
7548 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
7549 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
7550 Temp off = emit_extract_vector(ctx, offset, i, v1);
7551 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
7552 }
7553 has_offset = false;
7554 }
7555
7556 /* Build tex instruction */
7557 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
7558 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
7559 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
7560 : 0;
7561 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7562 Temp tmp_dst = dst;
7563
7564 /* gather4 selects the component by dmask and always returns vec4 */
7565 if (instr->op == nir_texop_tg4) {
7566 assert(instr->dest.ssa.num_components == 4);
7567 if (instr->is_shadow)
7568 dmask = 1;
7569 else
7570 dmask = 1 << instr->component;
7571 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
7572 tmp_dst = bld.tmp(v4);
7573 } else if (instr->op == nir_texop_samples_identical) {
7574 tmp_dst = bld.tmp(v1);
7575 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
7576 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
7577 }
7578
7579 aco_ptr<MIMG_instruction> tex;
7580 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
7581 if (!has_lod)
7582 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
7583
7584 bool div_by_6 = instr->op == nir_texop_txs &&
7585 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
7586 instr->is_array &&
7587 (dmask & (1 << 2));
7588 if (tmp_dst.id() == dst.id() && div_by_6)
7589 tmp_dst = bld.tmp(tmp_dst.regClass());
7590
7591 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
7592 tex->operands[0] = Operand(resource);
7593 tex->operands[1] = Operand(s4); /* no sampler */
7594 tex->operands[2] = Operand(as_vgpr(ctx,lod));
7595 if (ctx->options->chip_class == GFX9 &&
7596 instr->op == nir_texop_txs &&
7597 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
7598 instr->is_array) {
7599 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
7600 } else if (instr->op == nir_texop_query_levels) {
7601 tex->dmask = 1 << 3;
7602 } else {
7603 tex->dmask = dmask;
7604 }
7605 tex->da = da;
7606 tex->definitions[0] = Definition(tmp_dst);
7607 tex->dim = dim;
7608 tex->can_reorder = true;
7609 ctx->block->instructions.emplace_back(std::move(tex));
7610
7611 if (div_by_6) {
7612 /* divide 3rd value by 6 by multiplying with magic number */
7613 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
7614 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
7615 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
7616 assert(instr->dest.ssa.num_components == 3);
7617 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
7618 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
7619 emit_extract_vector(ctx, tmp_dst, 0, v1),
7620 emit_extract_vector(ctx, tmp_dst, 1, v1),
7621 by_6);
7622
7623 }
7624
7625 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
7626 return;
7627 }
7628
7629 Temp tg4_compare_cube_wa64 = Temp();
7630
7631 if (tg4_integer_workarounds) {
7632 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
7633 tex->operands[0] = Operand(resource);
7634 tex->operands[1] = Operand(s4); /* no sampler */
7635 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
7636 tex->dim = dim;
7637 tex->dmask = 0x3;
7638 tex->da = da;
7639 Temp size = bld.tmp(v2);
7640 tex->definitions[0] = Definition(size);
7641 tex->can_reorder = true;
7642 ctx->block->instructions.emplace_back(std::move(tex));
7643 emit_split_vector(ctx, size, size.size());
7644
7645 Temp half_texel[2];
7646 for (unsigned i = 0; i < 2; i++) {
7647 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
7648 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
7649 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
7650 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
7651 }
7652
7653 Temp new_coords[2] = {
7654 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
7655 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
7656 };
7657
7658 if (tg4_integer_cube_workaround) {
7659 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
7660 Temp desc[resource.size()];
7661 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
7662 Format::PSEUDO, 1, resource.size())};
7663 split->operands[0] = Operand(resource);
7664 for (unsigned i = 0; i < resource.size(); i++) {
7665 desc[i] = bld.tmp(s1);
7666 split->definitions[i] = Definition(desc[i]);
7667 }
7668 ctx->block->instructions.emplace_back(std::move(split));
7669
7670 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
7671 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
7672 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
7673
7674 Temp nfmt;
7675 if (stype == GLSL_TYPE_UINT) {
7676 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
7677 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
7678 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
7679 bld.scc(compare_cube_wa));
7680 } else {
7681 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
7682 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
7683 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
7684 bld.scc(compare_cube_wa));
7685 }
7686 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
7687 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
7688
7689 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
7690
7691 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
7692 Operand((uint32_t)C_008F14_NUM_FORMAT));
7693 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
7694
7695 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
7696 Format::PSEUDO, resource.size(), 1)};
7697 for (unsigned i = 0; i < resource.size(); i++)
7698 vec->operands[i] = Operand(desc[i]);
7699 resource = bld.tmp(resource.regClass());
7700 vec->definitions[0] = Definition(resource);
7701 ctx->block->instructions.emplace_back(std::move(vec));
7702
7703 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7704 new_coords[0], coords[0], tg4_compare_cube_wa64);
7705 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
7706 new_coords[1], coords[1], tg4_compare_cube_wa64);
7707 }
7708 coords[0] = new_coords[0];
7709 coords[1] = new_coords[1];
7710 }
7711
7712 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
7713 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
7714
7715 assert(coords.size() == 1);
7716 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
7717 aco_opcode op;
7718 switch (last_bit) {
7719 case 1:
7720 op = aco_opcode::buffer_load_format_x; break;
7721 case 2:
7722 op = aco_opcode::buffer_load_format_xy; break;
7723 case 3:
7724 op = aco_opcode::buffer_load_format_xyz; break;
7725 case 4:
7726 op = aco_opcode::buffer_load_format_xyzw; break;
7727 default:
7728 unreachable("Tex instruction loads more than 4 components.");
7729 }
7730
7731 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
7732 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
7733 tmp_dst = dst;
7734 else
7735 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
7736
7737 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
7738 mubuf->operands[0] = Operand(resource);
7739 mubuf->operands[1] = Operand(coords[0]);
7740 mubuf->operands[2] = Operand((uint32_t) 0);
7741 mubuf->definitions[0] = Definition(tmp_dst);
7742 mubuf->idxen = true;
7743 mubuf->can_reorder = true;
7744 ctx->block->instructions.emplace_back(std::move(mubuf));
7745
7746 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
7747 return;
7748 }
7749
7750 /* gather MIMG address components */
7751 std::vector<Temp> args;
7752 if (has_offset)
7753 args.emplace_back(offset);
7754 if (has_bias)
7755 args.emplace_back(bias);
7756 if (has_compare)
7757 args.emplace_back(compare);
7758 if (has_derivs)
7759 args.insert(args.end(), derivs.begin(), derivs.end());
7760
7761 args.insert(args.end(), coords.begin(), coords.end());
7762 if (has_sample_index)
7763 args.emplace_back(sample_index);
7764 if (has_lod)
7765 args.emplace_back(lod);
7766
7767 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
7768 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
7769 vec->definitions[0] = Definition(arg);
7770 for (unsigned i = 0; i < args.size(); i++)
7771 vec->operands[i] = Operand(args[i]);
7772 ctx->block->instructions.emplace_back(std::move(vec));
7773
7774
7775 if (instr->op == nir_texop_txf ||
7776 instr->op == nir_texop_txf_ms ||
7777 instr->op == nir_texop_samples_identical ||
7778 instr->op == nir_texop_fragment_fetch ||
7779 instr->op == nir_texop_fragment_mask_fetch) {
7780 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;
7781 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
7782 tex->operands[0] = Operand(resource);
7783 tex->operands[1] = Operand(s4); /* no sampler */
7784 tex->operands[2] = Operand(arg);
7785 tex->dim = dim;
7786 tex->dmask = dmask;
7787 tex->unrm = true;
7788 tex->da = da;
7789 tex->definitions[0] = Definition(tmp_dst);
7790 tex->can_reorder = true;
7791 ctx->block->instructions.emplace_back(std::move(tex));
7792
7793 if (instr->op == nir_texop_samples_identical) {
7794 assert(dmask == 1 && dst.regClass() == v1);
7795 assert(dst.id() != tmp_dst.id());
7796
7797 Temp tmp = bld.tmp(bld.lm);
7798 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
7799 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
7800
7801 } else {
7802 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
7803 }
7804 return;
7805 }
7806
7807 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
7808 aco_opcode opcode = aco_opcode::image_sample;
7809 if (has_offset) { /* image_sample_*_o */
7810 if (has_compare) {
7811 opcode = aco_opcode::image_sample_c_o;
7812 if (has_derivs)
7813 opcode = aco_opcode::image_sample_c_d_o;
7814 if (has_bias)
7815 opcode = aco_opcode::image_sample_c_b_o;
7816 if (level_zero)
7817 opcode = aco_opcode::image_sample_c_lz_o;
7818 if (has_lod)
7819 opcode = aco_opcode::image_sample_c_l_o;
7820 } else {
7821 opcode = aco_opcode::image_sample_o;
7822 if (has_derivs)
7823 opcode = aco_opcode::image_sample_d_o;
7824 if (has_bias)
7825 opcode = aco_opcode::image_sample_b_o;
7826 if (level_zero)
7827 opcode = aco_opcode::image_sample_lz_o;
7828 if (has_lod)
7829 opcode = aco_opcode::image_sample_l_o;
7830 }
7831 } else { /* no offset */
7832 if (has_compare) {
7833 opcode = aco_opcode::image_sample_c;
7834 if (has_derivs)
7835 opcode = aco_opcode::image_sample_c_d;
7836 if (has_bias)
7837 opcode = aco_opcode::image_sample_c_b;
7838 if (level_zero)
7839 opcode = aco_opcode::image_sample_c_lz;
7840 if (has_lod)
7841 opcode = aco_opcode::image_sample_c_l;
7842 } else {
7843 opcode = aco_opcode::image_sample;
7844 if (has_derivs)
7845 opcode = aco_opcode::image_sample_d;
7846 if (has_bias)
7847 opcode = aco_opcode::image_sample_b;
7848 if (level_zero)
7849 opcode = aco_opcode::image_sample_lz;
7850 if (has_lod)
7851 opcode = aco_opcode::image_sample_l;
7852 }
7853 }
7854
7855 if (instr->op == nir_texop_tg4) {
7856 if (has_offset) {
7857 opcode = aco_opcode::image_gather4_lz_o;
7858 if (has_compare)
7859 opcode = aco_opcode::image_gather4_c_lz_o;
7860 } else {
7861 opcode = aco_opcode::image_gather4_lz;
7862 if (has_compare)
7863 opcode = aco_opcode::image_gather4_c_lz;
7864 }
7865 } else if (instr->op == nir_texop_lod) {
7866 opcode = aco_opcode::image_get_lod;
7867 }
7868
7869 /* we don't need the bias, sample index, compare value or offset to be
7870 * computed in WQM but if the p_create_vector copies the coordinates, then it
7871 * needs to be in WQM */
7872 if (ctx->stage == fragment_fs &&
7873 !has_derivs && !has_lod && !level_zero &&
7874 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
7875 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
7876 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
7877
7878 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
7879 tex->operands[0] = Operand(resource);
7880 tex->operands[1] = Operand(sampler);
7881 tex->operands[2] = Operand(arg);
7882 tex->dim = dim;
7883 tex->dmask = dmask;
7884 tex->da = da;
7885 tex->definitions[0] = Definition(tmp_dst);
7886 tex->can_reorder = true;
7887 ctx->block->instructions.emplace_back(std::move(tex));
7888
7889 if (tg4_integer_cube_workaround) {
7890 assert(tmp_dst.id() != dst.id());
7891 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
7892
7893 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
7894 Temp val[4];
7895 for (unsigned i = 0; i < dst.size(); i++) {
7896 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
7897 Temp cvt_val;
7898 if (stype == GLSL_TYPE_UINT)
7899 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
7900 else
7901 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
7902 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
7903 }
7904 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
7905 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
7906 val[0], val[1], val[2], val[3]);
7907 }
7908 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
7909 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
7910
7911 }
7912
7913
7914 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
7915 {
7916 Temp tmp = get_ssa_temp(ctx, ssa);
7917 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
7918 return Operand(tmp.regClass());
7919 else
7920 return Operand(tmp);
7921 }
7922
7923 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
7924 {
7925 aco_ptr<Pseudo_instruction> phi;
7926 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7927 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
7928
7929 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
7930 logical |= ctx->block->kind & block_kind_merge;
7931 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
7932
7933 /* we want a sorted list of sources, since the predecessor list is also sorted */
7934 std::map<unsigned, nir_ssa_def*> phi_src;
7935 nir_foreach_phi_src(src, instr)
7936 phi_src[src->pred->index] = src->src.ssa;
7937
7938 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
7939 unsigned num_operands = 0;
7940 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size())];
7941 unsigned num_defined = 0;
7942 unsigned cur_pred_idx = 0;
7943 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
7944 if (cur_pred_idx < preds.size()) {
7945 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
7946 unsigned block = ctx->cf_info.nir_to_aco[src.first];
7947 unsigned skipped = 0;
7948 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
7949 skipped++;
7950 if (cur_pred_idx + skipped < preds.size()) {
7951 for (unsigned i = 0; i < skipped; i++)
7952 operands[num_operands++] = Operand(dst.regClass());
7953 cur_pred_idx += skipped;
7954 } else {
7955 continue;
7956 }
7957 }
7958 cur_pred_idx++;
7959 Operand op = get_phi_operand(ctx, src.second);
7960 operands[num_operands++] = op;
7961 num_defined += !op.isUndefined();
7962 }
7963 /* handle block_kind_continue_or_break at loop exit blocks */
7964 while (cur_pred_idx++ < preds.size())
7965 operands[num_operands++] = Operand(dst.regClass());
7966
7967 if (num_defined == 0) {
7968 Builder bld(ctx->program, ctx->block);
7969 if (dst.regClass() == s1) {
7970 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
7971 } else if (dst.regClass() == v1) {
7972 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
7973 } else {
7974 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
7975 for (unsigned i = 0; i < dst.size(); i++)
7976 vec->operands[i] = Operand(0u);
7977 vec->definitions[0] = Definition(dst);
7978 ctx->block->instructions.emplace_back(std::move(vec));
7979 }
7980 return;
7981 }
7982
7983 /* we can use a linear phi in some cases if one src is undef */
7984 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
7985 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
7986
7987 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
7988 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
7989 assert(invert->kind & block_kind_invert);
7990
7991 unsigned then_block = invert->linear_preds[0];
7992
7993 Block* insert_block = NULL;
7994 for (unsigned i = 0; i < num_operands; i++) {
7995 Operand op = operands[i];
7996 if (op.isUndefined())
7997 continue;
7998 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
7999 phi->operands[0] = op;
8000 break;
8001 }
8002 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8003 phi->operands[1] = Operand(dst.regClass());
8004 phi->definitions[0] = Definition(dst);
8005 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8006 return;
8007 }
8008
8009 /* try to scalarize vector phis */
8010 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8011 // TODO: scalarize linear phis on divergent ifs
8012 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8013 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8014 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8015 Operand src = operands[i];
8016 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8017 can_scalarize = false;
8018 }
8019 if (can_scalarize) {
8020 unsigned num_components = instr->dest.ssa.num_components;
8021 assert(dst.size() % num_components == 0);
8022 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8023
8024 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8025 for (unsigned k = 0; k < num_components; k++) {
8026 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8027 for (unsigned i = 0; i < num_operands; i++) {
8028 Operand src = operands[i];
8029 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8030 }
8031 Temp phi_dst = {ctx->program->allocateId(), rc};
8032 phi->definitions[0] = Definition(phi_dst);
8033 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8034 new_vec[k] = phi_dst;
8035 vec->operands[k] = Operand(phi_dst);
8036 }
8037 vec->definitions[0] = Definition(dst);
8038 ctx->block->instructions.emplace_back(std::move(vec));
8039 ctx->allocated_vec.emplace(dst.id(), new_vec);
8040 return;
8041 }
8042 }
8043
8044 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8045 for (unsigned i = 0; i < num_operands; i++)
8046 phi->operands[i] = operands[i];
8047 phi->definitions[0] = Definition(dst);
8048 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8049 }
8050
8051
8052 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8053 {
8054 Temp dst = get_ssa_temp(ctx, &instr->def);
8055
8056 assert(dst.type() == RegType::sgpr);
8057
8058 if (dst.size() == 1) {
8059 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8060 } else {
8061 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8062 for (unsigned i = 0; i < dst.size(); i++)
8063 vec->operands[i] = Operand(0u);
8064 vec->definitions[0] = Definition(dst);
8065 ctx->block->instructions.emplace_back(std::move(vec));
8066 }
8067 }
8068
8069 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8070 {
8071 Builder bld(ctx->program, ctx->block);
8072 Block *logical_target;
8073 append_logical_end(ctx->block);
8074 unsigned idx = ctx->block->index;
8075
8076 switch (instr->type) {
8077 case nir_jump_break:
8078 logical_target = ctx->cf_info.parent_loop.exit;
8079 add_logical_edge(idx, logical_target);
8080 ctx->block->kind |= block_kind_break;
8081
8082 if (!ctx->cf_info.parent_if.is_divergent &&
8083 !ctx->cf_info.parent_loop.has_divergent_continue) {
8084 /* uniform break - directly jump out of the loop */
8085 ctx->block->kind |= block_kind_uniform;
8086 ctx->cf_info.has_branch = true;
8087 bld.branch(aco_opcode::p_branch);
8088 add_linear_edge(idx, logical_target);
8089 return;
8090 }
8091 ctx->cf_info.parent_loop.has_divergent_branch = true;
8092 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8093 break;
8094 case nir_jump_continue:
8095 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8096 add_logical_edge(idx, logical_target);
8097 ctx->block->kind |= block_kind_continue;
8098
8099 if (ctx->cf_info.parent_if.is_divergent) {
8100 /* for potential uniform breaks after this continue,
8101 we must ensure that they are handled correctly */
8102 ctx->cf_info.parent_loop.has_divergent_continue = true;
8103 ctx->cf_info.parent_loop.has_divergent_branch = true;
8104 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8105 } else {
8106 /* uniform continue - directly jump to the loop header */
8107 ctx->block->kind |= block_kind_uniform;
8108 ctx->cf_info.has_branch = true;
8109 bld.branch(aco_opcode::p_branch);
8110 add_linear_edge(idx, logical_target);
8111 return;
8112 }
8113 break;
8114 default:
8115 fprintf(stderr, "Unknown NIR jump instr: ");
8116 nir_print_instr(&instr->instr, stderr);
8117 fprintf(stderr, "\n");
8118 abort();
8119 }
8120
8121 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
8122 ctx->cf_info.exec_potentially_empty_break = true;
8123 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
8124 }
8125
8126 /* remove critical edges from linear CFG */
8127 bld.branch(aco_opcode::p_branch);
8128 Block* break_block = ctx->program->create_and_insert_block();
8129 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8130 break_block->kind |= block_kind_uniform;
8131 add_linear_edge(idx, break_block);
8132 /* the loop_header pointer might be invalidated by this point */
8133 if (instr->type == nir_jump_continue)
8134 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8135 add_linear_edge(break_block->index, logical_target);
8136 bld.reset(break_block);
8137 bld.branch(aco_opcode::p_branch);
8138
8139 Block* continue_block = ctx->program->create_and_insert_block();
8140 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8141 add_linear_edge(idx, continue_block);
8142 append_logical_start(continue_block);
8143 ctx->block = continue_block;
8144 return;
8145 }
8146
8147 void visit_block(isel_context *ctx, nir_block *block)
8148 {
8149 nir_foreach_instr(instr, block) {
8150 switch (instr->type) {
8151 case nir_instr_type_alu:
8152 visit_alu_instr(ctx, nir_instr_as_alu(instr));
8153 break;
8154 case nir_instr_type_load_const:
8155 visit_load_const(ctx, nir_instr_as_load_const(instr));
8156 break;
8157 case nir_instr_type_intrinsic:
8158 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
8159 break;
8160 case nir_instr_type_tex:
8161 visit_tex(ctx, nir_instr_as_tex(instr));
8162 break;
8163 case nir_instr_type_phi:
8164 visit_phi(ctx, nir_instr_as_phi(instr));
8165 break;
8166 case nir_instr_type_ssa_undef:
8167 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
8168 break;
8169 case nir_instr_type_deref:
8170 break;
8171 case nir_instr_type_jump:
8172 visit_jump(ctx, nir_instr_as_jump(instr));
8173 break;
8174 default:
8175 fprintf(stderr, "Unknown NIR instr type: ");
8176 nir_print_instr(instr, stderr);
8177 fprintf(stderr, "\n");
8178 //abort();
8179 }
8180 }
8181
8182 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8183 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
8184 }
8185
8186
8187
8188 static void visit_loop(isel_context *ctx, nir_loop *loop)
8189 {
8190 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
8191 append_logical_end(ctx->block);
8192 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
8193 Builder bld(ctx->program, ctx->block);
8194 bld.branch(aco_opcode::p_branch);
8195 unsigned loop_preheader_idx = ctx->block->index;
8196
8197 Block loop_exit = Block();
8198 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8199 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
8200
8201 Block* loop_header = ctx->program->create_and_insert_block();
8202 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
8203 loop_header->kind |= block_kind_loop_header;
8204 add_edge(loop_preheader_idx, loop_header);
8205 ctx->block = loop_header;
8206
8207 /* emit loop body */
8208 unsigned loop_header_idx = loop_header->index;
8209 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
8210 append_logical_start(ctx->block);
8211 visit_cf_list(ctx, &loop->body);
8212
8213 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
8214 if (!ctx->cf_info.has_branch) {
8215 append_logical_end(ctx->block);
8216 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
8217 /* Discards can result in code running with an empty exec mask.
8218 * This would result in divergent breaks not ever being taken. As a
8219 * workaround, break the loop when the loop mask is empty instead of
8220 * always continuing. */
8221 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
8222 unsigned block_idx = ctx->block->index;
8223
8224 /* create helper blocks to avoid critical edges */
8225 Block *break_block = ctx->program->create_and_insert_block();
8226 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8227 break_block->kind = block_kind_uniform;
8228 bld.reset(break_block);
8229 bld.branch(aco_opcode::p_branch);
8230 add_linear_edge(block_idx, break_block);
8231 add_linear_edge(break_block->index, &loop_exit);
8232
8233 Block *continue_block = ctx->program->create_and_insert_block();
8234 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8235 continue_block->kind = block_kind_uniform;
8236 bld.reset(continue_block);
8237 bld.branch(aco_opcode::p_branch);
8238 add_linear_edge(block_idx, continue_block);
8239 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
8240
8241 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8242 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
8243 ctx->block = &ctx->program->blocks[block_idx];
8244 } else {
8245 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
8246 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8247 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
8248 else
8249 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
8250 }
8251
8252 bld.reset(ctx->block);
8253 bld.branch(aco_opcode::p_branch);
8254 }
8255
8256 /* fixup phis in loop header from unreachable blocks */
8257 if (ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch) {
8258 bool linear = ctx->cf_info.has_branch;
8259 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
8260 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
8261 if ((logical && instr->opcode == aco_opcode::p_phi) ||
8262 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
8263 /* the last operand should be the one that needs to be removed */
8264 instr->operands.pop_back();
8265 } else if (!is_phi(instr)) {
8266 break;
8267 }
8268 }
8269 }
8270
8271 ctx->cf_info.has_branch = false;
8272
8273 // TODO: if the loop has not a single exit, we must add one °°
8274 /* emit loop successor block */
8275 ctx->block = ctx->program->insert_block(std::move(loop_exit));
8276 append_logical_start(ctx->block);
8277
8278 #if 0
8279 // TODO: check if it is beneficial to not branch on continues
8280 /* trim linear phis in loop header */
8281 for (auto&& instr : loop_entry->instructions) {
8282 if (instr->opcode == aco_opcode::p_linear_phi) {
8283 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
8284 new_phi->definitions[0] = instr->definitions[0];
8285 for (unsigned i = 0; i < new_phi->operands.size(); i++)
8286 new_phi->operands[i] = instr->operands[i];
8287 /* check that the remaining operands are all the same */
8288 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
8289 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
8290 instr.swap(new_phi);
8291 } else if (instr->opcode == aco_opcode::p_phi) {
8292 continue;
8293 } else {
8294 break;
8295 }
8296 }
8297 #endif
8298 }
8299
8300 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
8301 {
8302 ic->cond = cond;
8303
8304 append_logical_end(ctx->block);
8305 ctx->block->kind |= block_kind_branch;
8306
8307 /* branch to linear then block */
8308 assert(cond.regClass() == ctx->program->lane_mask);
8309 aco_ptr<Pseudo_branch_instruction> branch;
8310 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
8311 branch->operands[0] = Operand(cond);
8312 ctx->block->instructions.push_back(std::move(branch));
8313
8314 ic->BB_if_idx = ctx->block->index;
8315 ic->BB_invert = Block();
8316 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8317 /* Invert blocks are intentionally not marked as top level because they
8318 * are not part of the logical cfg. */
8319 ic->BB_invert.kind |= block_kind_invert;
8320 ic->BB_endif = Block();
8321 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8322 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
8323
8324 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
8325 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
8326 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
8327 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
8328 ctx->cf_info.parent_if.is_divergent = true;
8329
8330 /* divergent branches use cbranch_execz */
8331 ctx->cf_info.exec_potentially_empty_discard = false;
8332 ctx->cf_info.exec_potentially_empty_break = false;
8333 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8334
8335 /** emit logical then block */
8336 Block* BB_then_logical = ctx->program->create_and_insert_block();
8337 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8338 add_edge(ic->BB_if_idx, BB_then_logical);
8339 ctx->block = BB_then_logical;
8340 append_logical_start(BB_then_logical);
8341 }
8342
8343 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
8344 {
8345 Block *BB_then_logical = ctx->block;
8346 append_logical_end(BB_then_logical);
8347 /* branch from logical then block to invert block */
8348 aco_ptr<Pseudo_branch_instruction> branch;
8349 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8350 BB_then_logical->instructions.emplace_back(std::move(branch));
8351 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
8352 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8353 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
8354 BB_then_logical->kind |= block_kind_uniform;
8355 assert(!ctx->cf_info.has_branch);
8356 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
8357 ctx->cf_info.parent_loop.has_divergent_branch = false;
8358
8359 /** emit linear then block */
8360 Block* BB_then_linear = ctx->program->create_and_insert_block();
8361 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8362 BB_then_linear->kind |= block_kind_uniform;
8363 add_linear_edge(ic->BB_if_idx, BB_then_linear);
8364 /* branch from linear then block to invert block */
8365 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8366 BB_then_linear->instructions.emplace_back(std::move(branch));
8367 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
8368
8369 /** emit invert merge block */
8370 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
8371 ic->invert_idx = ctx->block->index;
8372
8373 /* branch to linear else block (skip else) */
8374 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
8375 branch->operands[0] = Operand(ic->cond);
8376 ctx->block->instructions.push_back(std::move(branch));
8377
8378 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
8379 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
8380 ic->exec_potentially_empty_break_depth_old =
8381 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
8382 /* divergent branches use cbranch_execz */
8383 ctx->cf_info.exec_potentially_empty_discard = false;
8384 ctx->cf_info.exec_potentially_empty_break = false;
8385 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8386
8387 /** emit logical else block */
8388 Block* BB_else_logical = ctx->program->create_and_insert_block();
8389 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8390 add_logical_edge(ic->BB_if_idx, BB_else_logical);
8391 add_linear_edge(ic->invert_idx, BB_else_logical);
8392 ctx->block = BB_else_logical;
8393 append_logical_start(BB_else_logical);
8394 }
8395
8396 static void end_divergent_if(isel_context *ctx, if_context *ic)
8397 {
8398 Block *BB_else_logical = ctx->block;
8399 append_logical_end(BB_else_logical);
8400
8401 /* branch from logical else block to endif block */
8402 aco_ptr<Pseudo_branch_instruction> branch;
8403 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8404 BB_else_logical->instructions.emplace_back(std::move(branch));
8405 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
8406 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8407 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
8408 BB_else_logical->kind |= block_kind_uniform;
8409
8410 assert(!ctx->cf_info.has_branch);
8411 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
8412
8413
8414 /** emit linear else block */
8415 Block* BB_else_linear = ctx->program->create_and_insert_block();
8416 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8417 BB_else_linear->kind |= block_kind_uniform;
8418 add_linear_edge(ic->invert_idx, BB_else_linear);
8419
8420 /* branch from linear else block to endif block */
8421 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8422 BB_else_linear->instructions.emplace_back(std::move(branch));
8423 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
8424
8425
8426 /** emit endif merge block */
8427 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
8428 append_logical_start(ctx->block);
8429
8430
8431 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
8432 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
8433 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
8434 ctx->cf_info.exec_potentially_empty_break_depth =
8435 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
8436 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
8437 !ctx->cf_info.parent_if.is_divergent) {
8438 ctx->cf_info.exec_potentially_empty_break = false;
8439 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8440 }
8441 /* uniform control flow never has an empty exec-mask */
8442 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
8443 ctx->cf_info.exec_potentially_empty_discard = false;
8444 ctx->cf_info.exec_potentially_empty_break = false;
8445 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
8446 }
8447 }
8448
8449 static void visit_if(isel_context *ctx, nir_if *if_stmt)
8450 {
8451 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
8452 Builder bld(ctx->program, ctx->block);
8453 aco_ptr<Pseudo_branch_instruction> branch;
8454
8455 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
8456 /**
8457 * Uniform conditionals are represented in the following way*) :
8458 *
8459 * The linear and logical CFG:
8460 * BB_IF
8461 * / \
8462 * BB_THEN (logical) BB_ELSE (logical)
8463 * \ /
8464 * BB_ENDIF
8465 *
8466 * *) Exceptions may be due to break and continue statements within loops
8467 * If a break/continue happens within uniform control flow, it branches
8468 * to the loop exit/entry block. Otherwise, it branches to the next
8469 * merge block.
8470 **/
8471 append_logical_end(ctx->block);
8472 ctx->block->kind |= block_kind_uniform;
8473
8474 /* emit branch */
8475 assert(cond.regClass() == bld.lm);
8476 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
8477 cond = bool_to_scalar_condition(ctx, cond);
8478
8479 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
8480 branch->operands[0] = Operand(cond);
8481 branch->operands[0].setFixed(scc);
8482 ctx->block->instructions.emplace_back(std::move(branch));
8483
8484 unsigned BB_if_idx = ctx->block->index;
8485 Block BB_endif = Block();
8486 BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
8487 BB_endif.kind |= ctx->block->kind & block_kind_top_level;
8488
8489 /** emit then block */
8490 Block* BB_then = ctx->program->create_and_insert_block();
8491 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8492 add_edge(BB_if_idx, BB_then);
8493 append_logical_start(BB_then);
8494 ctx->block = BB_then;
8495 visit_cf_list(ctx, &if_stmt->then_list);
8496 BB_then = ctx->block;
8497 bool then_branch = ctx->cf_info.has_branch;
8498 bool then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
8499
8500 if (!then_branch) {
8501 append_logical_end(BB_then);
8502 /* branch from then block to endif block */
8503 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8504 BB_then->instructions.emplace_back(std::move(branch));
8505 add_linear_edge(BB_then->index, &BB_endif);
8506 if (!then_branch_divergent)
8507 add_logical_edge(BB_then->index, &BB_endif);
8508 BB_then->kind |= block_kind_uniform;
8509 }
8510
8511 ctx->cf_info.has_branch = false;
8512 ctx->cf_info.parent_loop.has_divergent_branch = false;
8513
8514 /** emit else block */
8515 Block* BB_else = ctx->program->create_and_insert_block();
8516 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8517 add_edge(BB_if_idx, BB_else);
8518 append_logical_start(BB_else);
8519 ctx->block = BB_else;
8520 visit_cf_list(ctx, &if_stmt->else_list);
8521 BB_else = ctx->block;
8522
8523 if (!ctx->cf_info.has_branch) {
8524 append_logical_end(BB_else);
8525 /* branch from then block to endif block */
8526 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
8527 BB_else->instructions.emplace_back(std::move(branch));
8528 add_linear_edge(BB_else->index, &BB_endif);
8529 if (!ctx->cf_info.parent_loop.has_divergent_branch)
8530 add_logical_edge(BB_else->index, &BB_endif);
8531 BB_else->kind |= block_kind_uniform;
8532 }
8533
8534 ctx->cf_info.has_branch &= then_branch;
8535 ctx->cf_info.parent_loop.has_divergent_branch &= then_branch_divergent;
8536
8537 /** emit endif merge block */
8538 if (!ctx->cf_info.has_branch) {
8539 ctx->block = ctx->program->insert_block(std::move(BB_endif));
8540 append_logical_start(ctx->block);
8541 }
8542 } else { /* non-uniform condition */
8543 /**
8544 * To maintain a logical and linear CFG without critical edges,
8545 * non-uniform conditionals are represented in the following way*) :
8546 *
8547 * The linear CFG:
8548 * BB_IF
8549 * / \
8550 * BB_THEN (logical) BB_THEN (linear)
8551 * \ /
8552 * BB_INVERT (linear)
8553 * / \
8554 * BB_ELSE (logical) BB_ELSE (linear)
8555 * \ /
8556 * BB_ENDIF
8557 *
8558 * The logical CFG:
8559 * BB_IF
8560 * / \
8561 * BB_THEN (logical) BB_ELSE (logical)
8562 * \ /
8563 * BB_ENDIF
8564 *
8565 * *) Exceptions may be due to break and continue statements within loops
8566 **/
8567
8568 if_context ic;
8569
8570 begin_divergent_if_then(ctx, &ic, cond);
8571 visit_cf_list(ctx, &if_stmt->then_list);
8572
8573 begin_divergent_if_else(ctx, &ic);
8574 visit_cf_list(ctx, &if_stmt->else_list);
8575
8576 end_divergent_if(ctx, &ic);
8577 }
8578 }
8579
8580 static void visit_cf_list(isel_context *ctx,
8581 struct exec_list *list)
8582 {
8583 foreach_list_typed(nir_cf_node, node, node, list) {
8584 switch (node->type) {
8585 case nir_cf_node_block:
8586 visit_block(ctx, nir_cf_node_as_block(node));
8587 break;
8588 case nir_cf_node_if:
8589 visit_if(ctx, nir_cf_node_as_if(node));
8590 break;
8591 case nir_cf_node_loop:
8592 visit_loop(ctx, nir_cf_node_as_loop(node));
8593 break;
8594 default:
8595 unreachable("unimplemented cf list type");
8596 }
8597 }
8598 }
8599
8600 static void export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
8601 {
8602 int offset = ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
8603 uint64_t mask = ctx->outputs.mask[slot];
8604 if (!is_pos && !mask)
8605 return;
8606 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
8607 return;
8608 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
8609 exp->enabled_mask = mask;
8610 for (unsigned i = 0; i < 4; ++i) {
8611 if (mask & (1 << i))
8612 exp->operands[i] = Operand(ctx->outputs.outputs[slot][i]);
8613 else
8614 exp->operands[i] = Operand(v1);
8615 }
8616 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
8617 * Setting valid_mask=1 prevents it and has no other effect.
8618 */
8619 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
8620 exp->done = false;
8621 exp->compressed = false;
8622 if (is_pos)
8623 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
8624 else
8625 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
8626 ctx->block->instructions.emplace_back(std::move(exp));
8627 }
8628
8629 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
8630 {
8631 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
8632 exp->enabled_mask = 0;
8633 for (unsigned i = 0; i < 4; ++i)
8634 exp->operands[i] = Operand(v1);
8635 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
8636 exp->operands[0] = Operand(ctx->outputs.outputs[VARYING_SLOT_PSIZ][0]);
8637 exp->enabled_mask |= 0x1;
8638 }
8639 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
8640 exp->operands[2] = Operand(ctx->outputs.outputs[VARYING_SLOT_LAYER][0]);
8641 exp->enabled_mask |= 0x4;
8642 }
8643 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
8644 if (ctx->options->chip_class < GFX9) {
8645 exp->operands[3] = Operand(ctx->outputs.outputs[VARYING_SLOT_VIEWPORT][0]);
8646 exp->enabled_mask |= 0x8;
8647 } else {
8648 Builder bld(ctx->program, ctx->block);
8649
8650 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
8651 Operand(ctx->outputs.outputs[VARYING_SLOT_VIEWPORT][0]));
8652 if (exp->operands[2].isTemp())
8653 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
8654
8655 exp->operands[2] = Operand(out);
8656 exp->enabled_mask |= 0x4;
8657 }
8658 }
8659 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
8660 exp->done = false;
8661 exp->compressed = false;
8662 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
8663 ctx->block->instructions.emplace_back(std::move(exp));
8664 }
8665
8666 static void create_vs_exports(isel_context *ctx)
8667 {
8668 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
8669
8670 if (outinfo->export_prim_id) {
8671 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
8672 ctx->outputs.outputs[VARYING_SLOT_PRIMITIVE_ID][0] = get_arg(ctx, ctx->args->vs_prim_id);
8673 }
8674
8675 if (ctx->options->key.has_multiview_view_index) {
8676 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
8677 ctx->outputs.outputs[VARYING_SLOT_LAYER][0] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
8678 }
8679
8680 /* the order these position exports are created is important */
8681 int next_pos = 0;
8682 export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
8683 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
8684 export_vs_psiz_layer_viewport(ctx, &next_pos);
8685 }
8686 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
8687 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
8688 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
8689 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
8690
8691 if (ctx->export_clip_dists) {
8692 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
8693 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
8694 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
8695 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
8696 }
8697
8698 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
8699 if (i < VARYING_SLOT_VAR0 && i != VARYING_SLOT_LAYER &&
8700 i != VARYING_SLOT_PRIMITIVE_ID)
8701 continue;
8702
8703 export_vs_varying(ctx, i, false, NULL);
8704 }
8705 }
8706
8707 static void export_fs_mrt_z(isel_context *ctx)
8708 {
8709 Builder bld(ctx->program, ctx->block);
8710 unsigned enabled_channels = 0;
8711 bool compr = false;
8712 Operand values[4];
8713
8714 for (unsigned i = 0; i < 4; ++i) {
8715 values[i] = Operand(v1);
8716 }
8717
8718 /* Both stencil and sample mask only need 16-bits. */
8719 if (!ctx->program->info->ps.writes_z &&
8720 (ctx->program->info->ps.writes_stencil ||
8721 ctx->program->info->ps.writes_sample_mask)) {
8722 compr = true; /* COMPR flag */
8723
8724 if (ctx->program->info->ps.writes_stencil) {
8725 /* Stencil should be in X[23:16]. */
8726 values[0] = Operand(ctx->outputs.outputs[FRAG_RESULT_STENCIL][0]);
8727 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
8728 enabled_channels |= 0x3;
8729 }
8730
8731 if (ctx->program->info->ps.writes_sample_mask) {
8732 /* SampleMask should be in Y[15:0]. */
8733 values[1] = Operand(ctx->outputs.outputs[FRAG_RESULT_SAMPLE_MASK][0]);
8734 enabled_channels |= 0xc;
8735 }
8736 } else {
8737 if (ctx->program->info->ps.writes_z) {
8738 values[0] = Operand(ctx->outputs.outputs[FRAG_RESULT_DEPTH][0]);
8739 enabled_channels |= 0x1;
8740 }
8741
8742 if (ctx->program->info->ps.writes_stencil) {
8743 values[1] = Operand(ctx->outputs.outputs[FRAG_RESULT_STENCIL][0]);
8744 enabled_channels |= 0x2;
8745 }
8746
8747 if (ctx->program->info->ps.writes_sample_mask) {
8748 values[2] = Operand(ctx->outputs.outputs[FRAG_RESULT_SAMPLE_MASK][0]);
8749 enabled_channels |= 0x4;
8750 }
8751 }
8752
8753 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
8754 * writemask component.
8755 */
8756 if (ctx->options->chip_class == GFX6 &&
8757 ctx->options->family != CHIP_OLAND &&
8758 ctx->options->family != CHIP_HAINAN) {
8759 enabled_channels |= 0x1;
8760 }
8761
8762 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
8763 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
8764 }
8765
8766 static void export_fs_mrt_color(isel_context *ctx, int slot)
8767 {
8768 Builder bld(ctx->program, ctx->block);
8769 unsigned write_mask = ctx->outputs.mask[slot];
8770 Operand values[4];
8771
8772 for (unsigned i = 0; i < 4; ++i) {
8773 if (write_mask & (1 << i)) {
8774 values[i] = Operand(ctx->outputs.outputs[slot][i]);
8775 } else {
8776 values[i] = Operand(v1);
8777 }
8778 }
8779
8780 unsigned target, col_format;
8781 unsigned enabled_channels = 0;
8782 aco_opcode compr_op = (aco_opcode)0;
8783
8784 slot -= FRAG_RESULT_DATA0;
8785 target = V_008DFC_SQ_EXP_MRT + slot;
8786 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
8787
8788 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
8789 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
8790
8791 switch (col_format)
8792 {
8793 case V_028714_SPI_SHADER_ZERO:
8794 enabled_channels = 0; /* writemask */
8795 target = V_008DFC_SQ_EXP_NULL;
8796 break;
8797
8798 case V_028714_SPI_SHADER_32_R:
8799 enabled_channels = 1;
8800 break;
8801
8802 case V_028714_SPI_SHADER_32_GR:
8803 enabled_channels = 0x3;
8804 break;
8805
8806 case V_028714_SPI_SHADER_32_AR:
8807 if (ctx->options->chip_class >= GFX10) {
8808 /* Special case: on GFX10, the outputs are different for 32_AR */
8809 enabled_channels = 0x3;
8810 values[1] = values[3];
8811 values[3] = Operand(v1);
8812 } else {
8813 enabled_channels = 0x9;
8814 }
8815 break;
8816
8817 case V_028714_SPI_SHADER_FP16_ABGR:
8818 enabled_channels = 0x5;
8819 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
8820 break;
8821
8822 case V_028714_SPI_SHADER_UNORM16_ABGR:
8823 enabled_channels = 0x5;
8824 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
8825 break;
8826
8827 case V_028714_SPI_SHADER_SNORM16_ABGR:
8828 enabled_channels = 0x5;
8829 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
8830 break;
8831
8832 case V_028714_SPI_SHADER_UINT16_ABGR: {
8833 enabled_channels = 0x5;
8834 compr_op = aco_opcode::v_cvt_pk_u16_u32;
8835 if (is_int8 || is_int10) {
8836 /* clamp */
8837 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
8838 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
8839
8840 for (unsigned i = 0; i < 4; i++) {
8841 if ((write_mask >> i) & 1) {
8842 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
8843 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
8844 values[i]);
8845 }
8846 }
8847 }
8848 break;
8849 }
8850
8851 case V_028714_SPI_SHADER_SINT16_ABGR:
8852 enabled_channels = 0x5;
8853 compr_op = aco_opcode::v_cvt_pk_i16_i32;
8854 if (is_int8 || is_int10) {
8855 /* clamp */
8856 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
8857 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
8858 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
8859 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
8860
8861 for (unsigned i = 0; i < 4; i++) {
8862 if ((write_mask >> i) & 1) {
8863 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
8864 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
8865 values[i]);
8866 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
8867 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
8868 values[i]);
8869 }
8870 }
8871 }
8872 break;
8873
8874 case V_028714_SPI_SHADER_32_ABGR:
8875 enabled_channels = 0xF;
8876 break;
8877
8878 default:
8879 break;
8880 }
8881
8882 if (target == V_008DFC_SQ_EXP_NULL)
8883 return;
8884
8885 if ((bool) compr_op) {
8886 for (int i = 0; i < 2; i++) {
8887 /* check if at least one of the values to be compressed is enabled */
8888 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
8889 if (enabled) {
8890 enabled_channels |= enabled << (i*2);
8891 values[i] = bld.vop3(compr_op, bld.def(v1),
8892 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
8893 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
8894 } else {
8895 values[i] = Operand(v1);
8896 }
8897 }
8898 values[2] = Operand(v1);
8899 values[3] = Operand(v1);
8900 } else {
8901 for (int i = 0; i < 4; i++)
8902 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
8903 }
8904
8905 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
8906 enabled_channels, target, (bool) compr_op);
8907 }
8908
8909 static void create_fs_exports(isel_context *ctx)
8910 {
8911 /* Export depth, stencil and sample mask. */
8912 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
8913 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
8914 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK]) {
8915 export_fs_mrt_z(ctx);
8916 }
8917
8918 /* Export all color render targets. */
8919 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i) {
8920 if (ctx->outputs.mask[i])
8921 export_fs_mrt_color(ctx, i);
8922 }
8923 }
8924
8925 static void emit_stream_output(isel_context *ctx,
8926 Temp const *so_buffers,
8927 Temp const *so_write_offset,
8928 const struct radv_stream_output *output)
8929 {
8930 unsigned num_comps = util_bitcount(output->component_mask);
8931 unsigned writemask = (1 << num_comps) - 1;
8932 unsigned loc = output->location;
8933 unsigned buf = output->buffer;
8934
8935 assert(num_comps && num_comps <= 4);
8936 if (!num_comps || num_comps > 4)
8937 return;
8938
8939 unsigned start = ffs(output->component_mask) - 1;
8940
8941 Temp out[4];
8942 bool all_undef = true;
8943 assert(ctx->stage == vertex_vs || ctx->stage == gs_copy_vs);
8944 for (unsigned i = 0; i < num_comps; i++) {
8945 out[i] = ctx->outputs.outputs[loc][start + i];
8946 all_undef = all_undef && !out[i].id();
8947 }
8948 if (all_undef)
8949 return;
8950
8951 while (writemask) {
8952 int start, count;
8953 u_bit_scan_consecutive_range(&writemask, &start, &count);
8954 if (count == 3 && ctx->options->chip_class == GFX6) {
8955 /* GFX6 doesn't support storing vec3, split it. */
8956 writemask |= 1u << (start + 2);
8957 count = 2;
8958 }
8959
8960 unsigned offset = output->offset + start * 4;
8961
8962 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
8963 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
8964 for (int i = 0; i < count; ++i)
8965 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
8966 vec->definitions[0] = Definition(write_data);
8967 ctx->block->instructions.emplace_back(std::move(vec));
8968
8969 aco_opcode opcode;
8970 switch (count) {
8971 case 1:
8972 opcode = aco_opcode::buffer_store_dword;
8973 break;
8974 case 2:
8975 opcode = aco_opcode::buffer_store_dwordx2;
8976 break;
8977 case 3:
8978 opcode = aco_opcode::buffer_store_dwordx3;
8979 break;
8980 case 4:
8981 opcode = aco_opcode::buffer_store_dwordx4;
8982 break;
8983 default:
8984 unreachable("Unsupported dword count.");
8985 }
8986
8987 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
8988 store->operands[0] = Operand(so_buffers[buf]);
8989 store->operands[1] = Operand(so_write_offset[buf]);
8990 store->operands[2] = Operand((uint32_t) 0);
8991 store->operands[3] = Operand(write_data);
8992 if (offset > 4095) {
8993 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
8994 Builder bld(ctx->program, ctx->block);
8995 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
8996 } else {
8997 store->offset = offset;
8998 }
8999 store->offen = true;
9000 store->glc = true;
9001 store->dlc = false;
9002 store->slc = true;
9003 store->can_reorder = true;
9004 ctx->block->instructions.emplace_back(std::move(store));
9005 }
9006 }
9007
9008 static void emit_streamout(isel_context *ctx, unsigned stream)
9009 {
9010 Builder bld(ctx->program, ctx->block);
9011
9012 Temp so_buffers[4];
9013 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
9014 for (unsigned i = 0; i < 4; i++) {
9015 unsigned stride = ctx->program->info->so.strides[i];
9016 if (!stride)
9017 continue;
9018
9019 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
9020 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
9021 }
9022
9023 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
9024 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
9025
9026 Temp tid = emit_mbcnt(ctx, bld.def(v1));
9027
9028 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
9029
9030 if_context ic;
9031 begin_divergent_if_then(ctx, &ic, can_emit);
9032
9033 bld.reset(ctx->block);
9034
9035 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
9036
9037 Temp so_write_offset[4];
9038
9039 for (unsigned i = 0; i < 4; i++) {
9040 unsigned stride = ctx->program->info->so.strides[i];
9041 if (!stride)
9042 continue;
9043
9044 if (stride == 1) {
9045 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
9046 get_arg(ctx, ctx->args->streamout_write_idx),
9047 get_arg(ctx, ctx->args->streamout_offset[i]));
9048 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
9049
9050 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
9051 } else {
9052 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
9053 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
9054 get_arg(ctx, ctx->args->streamout_offset[i]));
9055 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
9056 }
9057 }
9058
9059 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
9060 struct radv_stream_output *output =
9061 &ctx->program->info->so.outputs[i];
9062 if (stream != output->stream)
9063 continue;
9064
9065 emit_stream_output(ctx, so_buffers, so_write_offset, output);
9066 }
9067
9068 begin_divergent_if_else(ctx, &ic);
9069 end_divergent_if(ctx, &ic);
9070 }
9071
9072 } /* end namespace */
9073
9074 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
9075 {
9076 /* Split all arguments except for the first (ring_offsets) and the last
9077 * (exec) so that the dead channels don't stay live throughout the program.
9078 */
9079 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
9080 if (startpgm->definitions[i].regClass().size() > 1) {
9081 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
9082 startpgm->definitions[i].regClass().size());
9083 }
9084 }
9085 }
9086
9087 void handle_bc_optimize(isel_context *ctx)
9088 {
9089 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
9090 Builder bld(ctx->program, ctx->block);
9091 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
9092 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
9093 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
9094 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
9095 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
9096 if (uses_center && uses_centroid) {
9097 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
9098 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
9099
9100 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
9101 Temp new_coord[2];
9102 for (unsigned i = 0; i < 2; i++) {
9103 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
9104 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
9105 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9106 persp_centroid, persp_center, sel);
9107 }
9108 ctx->persp_centroid = bld.tmp(v2);
9109 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
9110 Operand(new_coord[0]), Operand(new_coord[1]));
9111 emit_split_vector(ctx, ctx->persp_centroid, 2);
9112 }
9113
9114 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
9115 Temp new_coord[2];
9116 for (unsigned i = 0; i < 2; i++) {
9117 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
9118 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
9119 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
9120 linear_centroid, linear_center, sel);
9121 }
9122 ctx->linear_centroid = bld.tmp(v2);
9123 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
9124 Operand(new_coord[0]), Operand(new_coord[1]));
9125 emit_split_vector(ctx, ctx->linear_centroid, 2);
9126 }
9127 }
9128 }
9129
9130 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
9131 {
9132 Program *program = ctx->program;
9133
9134 unsigned float_controls = shader->info.float_controls_execution_mode;
9135
9136 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
9137 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
9138 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
9139 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
9140 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
9141
9142 program->next_fp_mode.must_flush_denorms32 =
9143 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
9144 program->next_fp_mode.must_flush_denorms16_64 =
9145 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
9146 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
9147
9148 program->next_fp_mode.care_about_round32 =
9149 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
9150
9151 program->next_fp_mode.care_about_round16_64 =
9152 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
9153 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
9154
9155 /* default to preserving fp16 and fp64 denorms, since it's free */
9156 if (program->next_fp_mode.must_flush_denorms16_64)
9157 program->next_fp_mode.denorm16_64 = 0;
9158 else
9159 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
9160
9161 /* preserving fp32 denorms is expensive, so only do it if asked */
9162 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
9163 program->next_fp_mode.denorm32 = fp_denorm_keep;
9164 else
9165 program->next_fp_mode.denorm32 = 0;
9166
9167 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
9168 program->next_fp_mode.round32 = fp_round_tz;
9169 else
9170 program->next_fp_mode.round32 = fp_round_ne;
9171
9172 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
9173 program->next_fp_mode.round16_64 = fp_round_tz;
9174 else
9175 program->next_fp_mode.round16_64 = fp_round_ne;
9176
9177 ctx->block->fp_mode = program->next_fp_mode;
9178 }
9179
9180 void cleanup_cfg(Program *program)
9181 {
9182 /* create linear_succs/logical_succs */
9183 for (Block& BB : program->blocks) {
9184 for (unsigned idx : BB.linear_preds)
9185 program->blocks[idx].linear_succs.emplace_back(BB.index);
9186 for (unsigned idx : BB.logical_preds)
9187 program->blocks[idx].logical_succs.emplace_back(BB.index);
9188 }
9189 }
9190
9191 void select_program(Program *program,
9192 unsigned shader_count,
9193 struct nir_shader *const *shaders,
9194 ac_shader_config* config,
9195 struct radv_shader_args *args)
9196 {
9197 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
9198
9199 for (unsigned i = 0; i < shader_count; i++) {
9200 nir_shader *nir = shaders[i];
9201 init_context(&ctx, nir);
9202
9203 setup_fp_mode(&ctx, nir);
9204
9205 if (!i) {
9206 /* needs to be after init_context() for FS */
9207 Pseudo_instruction *startpgm = add_startpgm(&ctx);
9208 append_logical_start(ctx.block);
9209 split_arguments(&ctx, startpgm);
9210 }
9211
9212 if_context ic;
9213 if (shader_count >= 2) {
9214 Builder bld(ctx.program, ctx.block);
9215 Temp count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), get_arg(&ctx, args->merged_wave_info), Operand((8u << 16) | (i * 8u)));
9216 Temp thread_id = emit_mbcnt(&ctx, bld.def(v1));
9217 Temp cond = bld.vopc(aco_opcode::v_cmp_gt_u32, bld.hint_vcc(bld.def(bld.lm)), count, thread_id);
9218
9219 begin_divergent_if_then(&ctx, &ic, cond);
9220 }
9221
9222 if (i) {
9223 Builder bld(ctx.program, ctx.block);
9224
9225 bld.barrier(aco_opcode::p_memory_barrier_shared);
9226 bld.sopp(aco_opcode::s_barrier);
9227
9228 if (ctx.stage == vertex_geometry_gs) {
9229 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));
9230 }
9231 } else if (ctx.stage == geometry_gs)
9232 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
9233
9234 if (ctx.stage == fragment_fs)
9235 handle_bc_optimize(&ctx);
9236
9237 nir_function_impl *func = nir_shader_get_entrypoint(nir);
9238 visit_cf_list(&ctx, &func->body);
9239
9240 if (ctx.program->info->so.num_outputs && ctx.stage == vertex_vs)
9241 emit_streamout(&ctx, 0);
9242
9243 if (ctx.stage == vertex_vs) {
9244 create_vs_exports(&ctx);
9245 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
9246 Builder bld(ctx.program, ctx.block);
9247 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
9248 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
9249 }
9250
9251 if (ctx.stage == fragment_fs)
9252 create_fs_exports(&ctx);
9253
9254 if (shader_count >= 2) {
9255 begin_divergent_if_else(&ctx, &ic);
9256 end_divergent_if(&ctx, &ic);
9257 }
9258
9259 ralloc_free(ctx.divergent_vals);
9260 }
9261
9262 program->config->float_mode = program->blocks[0].fp_mode.val;
9263
9264 append_logical_end(ctx.block);
9265 ctx.block->kind |= block_kind_uniform | block_kind_export_end;
9266 Builder bld(ctx.program, ctx.block);
9267 if (ctx.program->wb_smem_l1_on_end)
9268 bld.smem(aco_opcode::s_dcache_wb, false);
9269 bld.sopp(aco_opcode::s_endpgm);
9270
9271 cleanup_cfg(program);
9272 }
9273
9274 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
9275 ac_shader_config* config,
9276 struct radv_shader_args *args)
9277 {
9278 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
9279
9280 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
9281 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
9282 program->next_fp_mode.must_flush_denorms32 = false;
9283 program->next_fp_mode.must_flush_denorms16_64 = false;
9284 program->next_fp_mode.care_about_round32 = false;
9285 program->next_fp_mode.care_about_round16_64 = false;
9286 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
9287 program->next_fp_mode.denorm32 = 0;
9288 program->next_fp_mode.round32 = fp_round_ne;
9289 program->next_fp_mode.round16_64 = fp_round_ne;
9290 ctx.block->fp_mode = program->next_fp_mode;
9291
9292 add_startpgm(&ctx);
9293 append_logical_start(ctx.block);
9294
9295 Builder bld(ctx.program, ctx.block);
9296
9297 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
9298
9299 Operand stream_id(0u);
9300 if (args->shader_info->so.num_outputs)
9301 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
9302 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
9303
9304 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
9305
9306 std::stack<Block> endif_blocks;
9307
9308 for (unsigned stream = 0; stream < 4; stream++) {
9309 if (stream_id.isConstant() && stream != stream_id.constantValue())
9310 continue;
9311
9312 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
9313 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
9314 continue;
9315
9316 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
9317
9318 unsigned BB_if_idx = ctx.block->index;
9319 Block BB_endif = Block();
9320 if (!stream_id.isConstant()) {
9321 /* begin IF */
9322 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
9323 append_logical_end(ctx.block);
9324 ctx.block->kind |= block_kind_uniform;
9325 bld.branch(aco_opcode::p_cbranch_z, cond);
9326
9327 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
9328
9329 ctx.block = ctx.program->create_and_insert_block();
9330 add_edge(BB_if_idx, ctx.block);
9331 bld.reset(ctx.block);
9332 append_logical_start(ctx.block);
9333 }
9334
9335 unsigned offset = 0;
9336 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9337 if (args->shader_info->gs.output_streams[i] != stream)
9338 continue;
9339
9340 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
9341 unsigned length = util_last_bit(output_usage_mask);
9342 for (unsigned j = 0; j < length; ++j) {
9343 if (!(output_usage_mask & (1 << j)))
9344 continue;
9345
9346 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
9347 Temp voffset = vtx_offset;
9348 if (const_offset >= 4096u) {
9349 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
9350 const_offset %= 4096u;
9351 }
9352
9353 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
9354 mubuf->definitions[0] = bld.def(v1);
9355 mubuf->operands[0] = Operand(gsvs_ring);
9356 mubuf->operands[1] = Operand(voffset);
9357 mubuf->operands[2] = Operand(0u);
9358 mubuf->offen = true;
9359 mubuf->offset = const_offset;
9360 mubuf->glc = true;
9361 mubuf->slc = true;
9362 mubuf->dlc = args->options->chip_class >= GFX10;
9363 mubuf->barrier = barrier_none;
9364 mubuf->can_reorder = true;
9365
9366 ctx.outputs.mask[i] |= 1 << j;
9367 ctx.outputs.outputs[i][j] = mubuf->definitions[0].getTemp();
9368
9369 bld.insert(std::move(mubuf));
9370
9371 offset++;
9372 }
9373 }
9374
9375 if (args->shader_info->so.num_outputs) {
9376 emit_streamout(&ctx, stream);
9377 bld.reset(ctx.block);
9378 }
9379
9380 if (stream == 0) {
9381 create_vs_exports(&ctx);
9382 ctx.block->kind |= block_kind_export_end;
9383 }
9384
9385 if (!stream_id.isConstant()) {
9386 append_logical_end(ctx.block);
9387
9388 /* branch from then block to endif block */
9389 bld.branch(aco_opcode::p_branch);
9390 add_edge(ctx.block->index, &BB_endif);
9391 ctx.block->kind |= block_kind_uniform;
9392
9393 /* emit else block */
9394 ctx.block = ctx.program->create_and_insert_block();
9395 add_edge(BB_if_idx, ctx.block);
9396 bld.reset(ctx.block);
9397 append_logical_start(ctx.block);
9398
9399 endif_blocks.push(std::move(BB_endif));
9400 }
9401 }
9402
9403 while (!endif_blocks.empty()) {
9404 Block BB_endif = std::move(endif_blocks.top());
9405 endif_blocks.pop();
9406
9407 Block *BB_else = ctx.block;
9408
9409 append_logical_end(BB_else);
9410 /* branch from else block to endif block */
9411 bld.branch(aco_opcode::p_branch);
9412 add_edge(BB_else->index, &BB_endif);
9413 BB_else->kind |= block_kind_uniform;
9414
9415 /** emit endif merge block */
9416 ctx.block = program->insert_block(std::move(BB_endif));
9417 bld.reset(ctx.block);
9418 append_logical_start(ctx.block);
9419 }
9420
9421 program->config->float_mode = program->blocks[0].fp_mode.val;
9422
9423 append_logical_end(ctx.block);
9424 ctx.block->kind |= block_kind_uniform;
9425 bld.sopp(aco_opcode::s_endpgm);
9426
9427 cleanup_cfg(program);
9428 }
9429 }