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