aco: fix adjusting the sample index with FMASK if value is negative
[mesa.git] / src / amd / compiler / aco_instruction_selection.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <algorithm>
27 #include <array>
28 #include <stack>
29 #include <map>
30
31 #include "ac_shader_util.h"
32 #include "aco_ir.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
37
38 namespace aco {
39 namespace {
40
41 class loop_info_RAII {
42 isel_context* ctx;
43 unsigned header_idx_old;
44 Block* exit_old;
45 bool divergent_cont_old;
46 bool divergent_branch_old;
47 bool divergent_if_old;
48
49 public:
50 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
51 : ctx(ctx),
52 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
53 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
54 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
55 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
56 {
57 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
58 ctx->cf_info.parent_loop.exit = loop_exit;
59 ctx->cf_info.parent_loop.has_divergent_continue = false;
60 ctx->cf_info.parent_loop.has_divergent_branch = false;
61 ctx->cf_info.parent_if.is_divergent = false;
62 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
63 }
64
65 ~loop_info_RAII()
66 {
67 ctx->cf_info.parent_loop.header_idx = header_idx_old;
68 ctx->cf_info.parent_loop.exit = exit_old;
69 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
70 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
71 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
72 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
73 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
74 ctx->cf_info.exec_potentially_empty_discard = false;
75 }
76 };
77
78 struct if_context {
79 Temp cond;
80
81 bool divergent_old;
82 bool exec_potentially_empty_discard_old;
83 bool exec_potentially_empty_break_old;
84 uint16_t exec_potentially_empty_break_depth_old;
85
86 unsigned BB_if_idx;
87 unsigned invert_idx;
88 bool uniform_has_then_branch;
89 bool then_branch_divergent;
90 Block BB_invert;
91 Block BB_endif;
92 };
93
94 static bool visit_cf_list(struct isel_context *ctx,
95 struct exec_list *list);
96
97 static void add_logical_edge(unsigned pred_idx, Block *succ)
98 {
99 succ->logical_preds.emplace_back(pred_idx);
100 }
101
102
103 static void add_linear_edge(unsigned pred_idx, Block *succ)
104 {
105 succ->linear_preds.emplace_back(pred_idx);
106 }
107
108 static void add_edge(unsigned pred_idx, Block *succ)
109 {
110 add_logical_edge(pred_idx, succ);
111 add_linear_edge(pred_idx, succ);
112 }
113
114 static void append_logical_start(Block *b)
115 {
116 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
117 }
118
119 static void append_logical_end(Block *b)
120 {
121 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
122 }
123
124 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
125 {
126 assert(ctx->allocated[def->index].id());
127 return ctx->allocated[def->index];
128 }
129
130 Temp emit_mbcnt(isel_context *ctx, Definition dst,
131 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
132 {
133 Builder bld(ctx->program, ctx->block);
134 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
135 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
136
137 if (ctx->program->wave_size == 32) {
138 return thread_id_lo;
139 } else {
140 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 }
143 }
144
145 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
146 {
147 Builder bld(ctx->program, ctx->block);
148
149 if (!dst.id())
150 dst = bld.tmp(src.regClass());
151
152 assert(src.size() == dst.size());
153
154 if (ctx->stage != fragment_fs) {
155 if (!dst.id())
156 return src;
157
158 bld.copy(Definition(dst), src);
159 return dst;
160 }
161
162 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
163 ctx->program->needs_wqm |= program_needs_wqm;
164 return dst;
165 }
166
167 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
168 {
169 if (index.regClass() == s1)
170 return bld.readlane(bld.def(s1), data, index);
171
172 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
173
174 /* Currently not implemented on GFX6-7 */
175 assert(ctx->options->chip_class >= GFX8);
176
177 if (ctx->options->chip_class <= GFX9 || ctx->program->wave_size == 32) {
178 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
179 }
180
181 /* GFX10, wave64 mode:
182 * The bpermute instruction is limited to half-wave operation, which means that it can't
183 * properly support subgroup shuffle like older generations (or wave32 mode), so we
184 * emulate it here.
185 */
186 if (!ctx->has_gfx10_wave64_bpermute) {
187 ctx->has_gfx10_wave64_bpermute = true;
188 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
189 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
190 }
191
192 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
193 Temp lane_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), lane_id);
194 Temp index_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), index);
195 Temp cmp = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm, vcc), lane_is_hi, index_is_hi);
196
197 return bld.reduction(aco_opcode::p_wave64_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc),
198 bld.vcc(cmp), Operand(v2.as_linear()), index_x4, data, gfx10_wave64_bpermute);
199 }
200
201 Temp as_vgpr(isel_context *ctx, Temp val)
202 {
203 if (val.type() == RegType::sgpr) {
204 Builder bld(ctx->program, ctx->block);
205 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
206 }
207 assert(val.type() == RegType::vgpr);
208 return val;
209 }
210
211 //assumes a != 0xffffffff
212 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
213 {
214 assert(b != 0);
215 Builder bld(ctx->program, ctx->block);
216
217 if (util_is_power_of_two_or_zero(b)) {
218 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
219 return;
220 }
221
222 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
223
224 assert(info.multiplier <= 0xffffffff);
225
226 bool pre_shift = info.pre_shift != 0;
227 bool increment = info.increment != 0;
228 bool multiply = true;
229 bool post_shift = info.post_shift != 0;
230
231 if (!pre_shift && !increment && !multiply && !post_shift) {
232 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
233 return;
234 }
235
236 Temp pre_shift_dst = a;
237 if (pre_shift) {
238 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
239 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
240 }
241
242 Temp increment_dst = pre_shift_dst;
243 if (increment) {
244 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
245 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
246 }
247
248 Temp multiply_dst = increment_dst;
249 if (multiply) {
250 multiply_dst = post_shift ? bld.tmp(v1) : dst;
251 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
252 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
253 }
254
255 if (post_shift) {
256 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
257 }
258 }
259
260 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
261 {
262 Builder bld(ctx->program, ctx->block);
263 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
264 }
265
266
267 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
268 {
269 /* no need to extract the whole vector */
270 if (src.regClass() == dst_rc) {
271 assert(idx == 0);
272 return src;
273 }
274
275 assert(src.bytes() > (idx * dst_rc.bytes()));
276 Builder bld(ctx->program, ctx->block);
277 auto it = ctx->allocated_vec.find(src.id());
278 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
279 if (it->second[idx].regClass() == dst_rc) {
280 return it->second[idx];
281 } else {
282 assert(!dst_rc.is_subdword());
283 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
284 return bld.copy(bld.def(dst_rc), it->second[idx]);
285 }
286 }
287
288 if (dst_rc.is_subdword())
289 src = as_vgpr(ctx, src);
290
291 if (src.bytes() == dst_rc.bytes()) {
292 assert(idx == 0);
293 return bld.copy(bld.def(dst_rc), src);
294 } else {
295 Temp dst = bld.tmp(dst_rc);
296 emit_extract_vector(ctx, src, idx, dst);
297 return dst;
298 }
299 }
300
301 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
302 {
303 if (num_components == 1)
304 return;
305 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
306 return;
307 RegClass rc;
308 if (num_components > vec_src.size()) {
309 if (vec_src.type() == RegType::sgpr) {
310 /* should still help get_alu_src() */
311 emit_split_vector(ctx, vec_src, vec_src.size());
312 return;
313 }
314 /* sub-dword split */
315 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
316 } else {
317 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
318 }
319 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
320 split->operands[0] = Operand(vec_src);
321 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
322 for (unsigned i = 0; i < num_components; i++) {
323 elems[i] = {ctx->program->allocateId(), rc};
324 split->definitions[i] = Definition(elems[i]);
325 }
326 ctx->block->instructions.emplace_back(std::move(split));
327 ctx->allocated_vec.emplace(vec_src.id(), elems);
328 }
329
330 /* This vector expansion uses a mask to determine which elements in the new vector
331 * come from the original vector. The other elements are undefined. */
332 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
333 {
334 emit_split_vector(ctx, vec_src, util_bitcount(mask));
335
336 if (vec_src == dst)
337 return;
338
339 Builder bld(ctx->program, ctx->block);
340 if (num_components == 1) {
341 if (dst.type() == RegType::sgpr)
342 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
343 else
344 bld.copy(Definition(dst), vec_src);
345 return;
346 }
347
348 unsigned component_size = dst.size() / num_components;
349 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
350
351 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
352 vec->definitions[0] = Definition(dst);
353 unsigned k = 0;
354 for (unsigned i = 0; i < num_components; i++) {
355 if (mask & (1 << i)) {
356 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
357 if (dst.type() == RegType::sgpr)
358 src = bld.as_uniform(src);
359 vec->operands[i] = Operand(src);
360 } else {
361 vec->operands[i] = Operand(0u);
362 }
363 elems[i] = vec->operands[i].getTemp();
364 }
365 ctx->block->instructions.emplace_back(std::move(vec));
366 ctx->allocated_vec.emplace(dst.id(), elems);
367 }
368
369 /* adjust misaligned small bit size loads */
370 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
371 {
372 Builder bld(ctx->program, ctx->block);
373 Operand shift;
374 Temp select = Temp();
375 if (offset.isConstant()) {
376 assert(offset.constantValue() && offset.constantValue() < 4);
377 shift = Operand(offset.constantValue() * 8);
378 } else {
379 /* bit_offset = 8 * (offset & 0x3) */
380 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
381 select = bld.tmp(s1);
382 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
383 }
384
385 if (vec.size() == 1) {
386 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
387 } else if (vec.size() == 2) {
388 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
389 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
390 if (tmp == dst)
391 emit_split_vector(ctx, dst, 2);
392 else
393 emit_extract_vector(ctx, tmp, 0, dst);
394 } else if (vec.size() == 4) {
395 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
396 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
397 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
398 if (select != Temp())
399 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), select);
400 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
401 Temp mid = bld.tmp(s1);
402 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
403 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
404 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
405 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
406 emit_split_vector(ctx, dst, 2);
407 }
408 }
409
410 /* this function trims subdword vectors:
411 * if dst is vgpr - split the src and create a shrunk version according to the mask.
412 * if dst is sgpr - split the src, but move the original to sgpr. */
413 void trim_subdword_vector(isel_context *ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
414 {
415 assert(vec_src.type() == RegType::vgpr);
416 emit_split_vector(ctx, vec_src, num_components);
417
418 Builder bld(ctx->program, ctx->block);
419 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
420 unsigned component_size = vec_src.bytes() / num_components;
421 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
422
423 unsigned k = 0;
424 for (unsigned i = 0; i < num_components; i++) {
425 if (mask & (1 << i))
426 elems[k++] = emit_extract_vector(ctx, vec_src, i, rc);
427 }
428
429 if (dst.type() == RegType::vgpr) {
430 assert(dst.bytes() == k * component_size);
431 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, k, 1)};
432 for (unsigned i = 0; i < k; i++)
433 vec->operands[i] = Operand(elems[i]);
434 vec->definitions[0] = Definition(dst);
435 bld.insert(std::move(vec));
436 } else {
437 // TODO: alignbyte if mask doesn't start with 1?
438 assert(mask & 1);
439 assert(dst.size() == vec_src.size());
440 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
441 }
442 ctx->allocated_vec.emplace(dst.id(), elems);
443 }
444
445 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
446 {
447 Builder bld(ctx->program, ctx->block);
448 if (!dst.id())
449 dst = bld.tmp(bld.lm);
450
451 assert(val.regClass() == s1);
452 assert(dst.regClass() == bld.lm);
453
454 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
455 }
456
457 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
458 {
459 Builder bld(ctx->program, ctx->block);
460 if (!dst.id())
461 dst = bld.tmp(s1);
462
463 assert(val.regClass() == bld.lm);
464 assert(dst.regClass() == s1);
465
466 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
467 Temp tmp = bld.tmp(s1);
468 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
469 return emit_wqm(ctx, tmp, dst);
470 }
471
472 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
473 {
474 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
475 return get_ssa_temp(ctx, src.src.ssa);
476
477 if (src.src.ssa->num_components == size) {
478 bool identity_swizzle = true;
479 for (unsigned i = 0; identity_swizzle && i < size; i++) {
480 if (src.swizzle[i] != i)
481 identity_swizzle = false;
482 }
483 if (identity_swizzle)
484 return get_ssa_temp(ctx, src.src.ssa);
485 }
486
487 Temp vec = get_ssa_temp(ctx, src.src.ssa);
488 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
489 assert(elem_size > 0);
490 assert(vec.bytes() % elem_size == 0);
491
492 if (elem_size < 4 && vec.type() == RegType::sgpr) {
493 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
494 assert(size == 1);
495 unsigned swizzle = src.swizzle[0];
496 if (vec.size() > 1) {
497 assert(src.src.ssa->bit_size == 16);
498 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
499 swizzle = swizzle & 1;
500 }
501 if (swizzle == 0)
502 return vec;
503
504 Temp dst{ctx->program->allocateId(), s1};
505 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
506 bfe->operands[0] = Operand(vec);
507 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
508 bfe->definitions[0] = Definition(dst);
509 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
510 ctx->block->instructions.emplace_back(std::move(bfe));
511 return dst;
512 }
513
514 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
515 if (size == 1) {
516 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
517 } else {
518 assert(size <= 4);
519 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
520 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
521 for (unsigned i = 0; i < size; ++i) {
522 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
523 vec_instr->operands[i] = Operand{elems[i]};
524 }
525 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
526 vec_instr->definitions[0] = Definition(dst);
527 ctx->block->instructions.emplace_back(std::move(vec_instr));
528 ctx->allocated_vec.emplace(dst.id(), elems);
529 return dst;
530 }
531 }
532
533 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
534 {
535 if (ptr.size() == 2)
536 return ptr;
537 Builder bld(ctx->program, ctx->block);
538 if (ptr.type() == RegType::vgpr)
539 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
540 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
541 ptr, Operand((unsigned)ctx->options->address32_hi));
542 }
543
544 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
545 {
546 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
547 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
548 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
549 sop2->definitions[0] = Definition(dst);
550 if (writes_scc)
551 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
552 ctx->block->instructions.emplace_back(std::move(sop2));
553 }
554
555 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
556 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
557 {
558 Builder bld(ctx->program, ctx->block);
559 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
560 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
561 if (src1.type() == RegType::sgpr) {
562 if (commutative && src0.type() == RegType::vgpr) {
563 Temp t = src0;
564 src0 = src1;
565 src1 = t;
566 } else {
567 src1 = as_vgpr(ctx, src1);
568 }
569 }
570
571 if (flush_denorms && ctx->program->chip_class < GFX9) {
572 assert(dst.size() == 1);
573 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
574 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
575 } else {
576 bld.vop2(op, Definition(dst), src0, src1);
577 }
578 }
579
580 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
581 bool flush_denorms = false)
582 {
583 Temp src0 = get_alu_src(ctx, instr->src[0]);
584 Temp src1 = get_alu_src(ctx, instr->src[1]);
585 Temp src2 = get_alu_src(ctx, instr->src[2]);
586
587 /* ensure that the instruction has at most 1 sgpr operand
588 * The optimizer will inline constants for us */
589 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
590 src0 = as_vgpr(ctx, src0);
591 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
592 src1 = as_vgpr(ctx, src1);
593 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
594 src2 = as_vgpr(ctx, src2);
595
596 Builder bld(ctx->program, ctx->block);
597 if (flush_denorms && ctx->program->chip_class < GFX9) {
598 assert(dst.size() == 1);
599 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
600 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
601 } else {
602 bld.vop3(op, Definition(dst), src0, src1, src2);
603 }
604 }
605
606 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
607 {
608 Builder bld(ctx->program, ctx->block);
609 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
610 }
611
612 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
613 {
614 Temp src0 = get_alu_src(ctx, instr->src[0]);
615 Temp src1 = get_alu_src(ctx, instr->src[1]);
616 assert(src0.size() == src1.size());
617
618 aco_ptr<Instruction> vopc;
619 if (src1.type() == RegType::sgpr) {
620 if (src0.type() == RegType::vgpr) {
621 /* to swap the operands, we might also have to change the opcode */
622 switch (op) {
623 case aco_opcode::v_cmp_lt_f16:
624 op = aco_opcode::v_cmp_gt_f16;
625 break;
626 case aco_opcode::v_cmp_ge_f16:
627 op = aco_opcode::v_cmp_le_f16;
628 break;
629 case aco_opcode::v_cmp_lt_i16:
630 op = aco_opcode::v_cmp_gt_i16;
631 break;
632 case aco_opcode::v_cmp_ge_i16:
633 op = aco_opcode::v_cmp_le_i16;
634 break;
635 case aco_opcode::v_cmp_lt_u16:
636 op = aco_opcode::v_cmp_gt_u16;
637 break;
638 case aco_opcode::v_cmp_ge_u16:
639 op = aco_opcode::v_cmp_le_u16;
640 break;
641 case aco_opcode::v_cmp_lt_f32:
642 op = aco_opcode::v_cmp_gt_f32;
643 break;
644 case aco_opcode::v_cmp_ge_f32:
645 op = aco_opcode::v_cmp_le_f32;
646 break;
647 case aco_opcode::v_cmp_lt_i32:
648 op = aco_opcode::v_cmp_gt_i32;
649 break;
650 case aco_opcode::v_cmp_ge_i32:
651 op = aco_opcode::v_cmp_le_i32;
652 break;
653 case aco_opcode::v_cmp_lt_u32:
654 op = aco_opcode::v_cmp_gt_u32;
655 break;
656 case aco_opcode::v_cmp_ge_u32:
657 op = aco_opcode::v_cmp_le_u32;
658 break;
659 case aco_opcode::v_cmp_lt_f64:
660 op = aco_opcode::v_cmp_gt_f64;
661 break;
662 case aco_opcode::v_cmp_ge_f64:
663 op = aco_opcode::v_cmp_le_f64;
664 break;
665 case aco_opcode::v_cmp_lt_i64:
666 op = aco_opcode::v_cmp_gt_i64;
667 break;
668 case aco_opcode::v_cmp_ge_i64:
669 op = aco_opcode::v_cmp_le_i64;
670 break;
671 case aco_opcode::v_cmp_lt_u64:
672 op = aco_opcode::v_cmp_gt_u64;
673 break;
674 case aco_opcode::v_cmp_ge_u64:
675 op = aco_opcode::v_cmp_le_u64;
676 break;
677 default: /* eq and ne are commutative */
678 break;
679 }
680 Temp t = src0;
681 src0 = src1;
682 src1 = t;
683 } else {
684 src1 = as_vgpr(ctx, src1);
685 }
686 }
687
688 Builder bld(ctx->program, ctx->block);
689 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
690 }
691
692 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
693 {
694 Temp src0 = get_alu_src(ctx, instr->src[0]);
695 Temp src1 = get_alu_src(ctx, instr->src[1]);
696 Builder bld(ctx->program, ctx->block);
697
698 assert(dst.regClass() == bld.lm);
699 assert(src0.type() == RegType::sgpr);
700 assert(src1.type() == RegType::sgpr);
701 assert(src0.regClass() == src1.regClass());
702
703 /* Emit the SALU comparison instruction */
704 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
705 /* Turn the result into a per-lane bool */
706 bool_to_vector_condition(ctx, cmp, dst);
707 }
708
709 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
710 aco_opcode v16_op, aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
711 {
712 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : instr->src[0].src.ssa->bit_size == 32 ? s32_op : aco_opcode::num_opcodes;
713 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : instr->src[0].src.ssa->bit_size == 32 ? v32_op : v16_op;
714 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
715 bool use_valu = s_op == aco_opcode::num_opcodes ||
716 divergent_vals ||
717 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
718 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
719 aco_opcode op = use_valu ? v_op : s_op;
720 assert(op != aco_opcode::num_opcodes);
721 assert(dst.regClass() == ctx->program->lane_mask);
722
723 if (use_valu)
724 emit_vopc_instruction(ctx, instr, op, dst);
725 else
726 emit_sopc_instruction(ctx, instr, op, dst);
727 }
728
729 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
730 {
731 Builder bld(ctx->program, ctx->block);
732 Temp src0 = get_alu_src(ctx, instr->src[0]);
733 Temp src1 = get_alu_src(ctx, instr->src[1]);
734
735 assert(dst.regClass() == bld.lm);
736 assert(src0.regClass() == bld.lm);
737 assert(src1.regClass() == bld.lm);
738
739 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
740 }
741
742 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
743 {
744 Builder bld(ctx->program, ctx->block);
745 Temp cond = get_alu_src(ctx, instr->src[0]);
746 Temp then = get_alu_src(ctx, instr->src[1]);
747 Temp els = get_alu_src(ctx, instr->src[2]);
748
749 assert(cond.regClass() == bld.lm);
750
751 if (dst.type() == RegType::vgpr) {
752 aco_ptr<Instruction> bcsel;
753 if (dst.regClass() == v2b) {
754 then = as_vgpr(ctx, then);
755 els = as_vgpr(ctx, els);
756
757 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), els, then, cond);
758 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
759 } else if (dst.regClass() == v1) {
760 then = as_vgpr(ctx, then);
761 els = as_vgpr(ctx, els);
762
763 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
764 } else if (dst.regClass() == v2) {
765 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
766 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
767 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
768 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
769
770 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
771 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
772
773 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
774 } else {
775 fprintf(stderr, "Unimplemented NIR instr bit size: ");
776 nir_print_instr(&instr->instr, stderr);
777 fprintf(stderr, "\n");
778 }
779 return;
780 }
781
782 if (instr->dest.dest.ssa.bit_size == 1) {
783 assert(dst.regClass() == bld.lm);
784 assert(then.regClass() == bld.lm);
785 assert(els.regClass() == bld.lm);
786 }
787
788 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
789 if (dst.regClass() == s1 || dst.regClass() == s2) {
790 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
791 assert(dst.size() == then.size());
792 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
793 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
794 } else {
795 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
796 nir_print_instr(&instr->instr, stderr);
797 fprintf(stderr, "\n");
798 }
799 return;
800 }
801
802 /* divergent boolean bcsel
803 * this implements bcsel on bools: dst = s0 ? s1 : s2
804 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
805 assert(instr->dest.dest.ssa.bit_size == 1);
806
807 if (cond.id() != then.id())
808 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
809
810 if (cond.id() == els.id())
811 bld.sop1(Builder::s_mov, Definition(dst), then);
812 else
813 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
814 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
815 }
816
817 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
818 aco_opcode op, uint32_t undo)
819 {
820 /* multiply by 16777216 to handle denormals */
821 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
822 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
823 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
824 scaled = bld.vop1(op, bld.def(v1), scaled);
825 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
826
827 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
828
829 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
830 }
831
832 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
833 {
834 if (ctx->block->fp_mode.denorm32 == 0) {
835 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
836 return;
837 }
838
839 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
840 }
841
842 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
843 {
844 if (ctx->block->fp_mode.denorm32 == 0) {
845 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
846 return;
847 }
848
849 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
850 }
851
852 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
853 {
854 if (ctx->block->fp_mode.denorm32 == 0) {
855 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
856 return;
857 }
858
859 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
860 }
861
862 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
863 {
864 if (ctx->block->fp_mode.denorm32 == 0) {
865 bld.vop1(aco_opcode::v_log_f32, dst, val);
866 return;
867 }
868
869 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
870 }
871
872 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
873 {
874 if (ctx->options->chip_class >= GFX7)
875 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
876
877 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
878 /* TODO: create more efficient code! */
879 if (val.type() == RegType::sgpr)
880 val = as_vgpr(ctx, val);
881
882 /* Split the input value. */
883 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
884 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
885
886 /* Extract the exponent and compute the unbiased value. */
887 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
888
889 /* Extract the fractional part. */
890 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
891 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
892
893 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
894 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
895
896 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
897 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
898 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
899 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
900 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
901
902 /* Get the sign bit. */
903 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
904
905 /* Decide the operation to apply depending on the unbiased exponent. */
906 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
907 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
908 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
909 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
910 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
911 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
912
913 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
914 }
915
916 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
917 {
918 if (ctx->options->chip_class >= GFX7)
919 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
920
921 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
922 Temp src0 = as_vgpr(ctx, val);
923
924 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
925 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
926
927 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
928 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
929 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
930
931 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
932 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
933 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
934 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
935
936 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
937 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
938
939 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
940
941 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
942 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
943
944 return add->definitions[0].getTemp();
945 }
946
947 Temp convert_int(Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
948 if (!dst.id()) {
949 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
950 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
951 else
952 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
953 }
954
955 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
956 return bld.copy(Definition(dst), src);
957 else if (dst.bytes() < src.bytes())
958 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
959
960 Temp tmp = dst;
961 if (dst_bits == 64)
962 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
963
964 if (tmp == src) {
965 } else if (src.regClass() == s1) {
966 if (is_signed)
967 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
968 else
969 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
970 } else {
971 assert(src_bits != 8 || src.regClass() == v1b);
972 assert(src_bits != 16 || src.regClass() == v2b);
973 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
974 sdwa->operands[0] = Operand(src);
975 sdwa->definitions[0] = Definition(tmp);
976 if (is_signed)
977 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
978 else
979 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
980 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
981 bld.insert(std::move(sdwa));
982 }
983
984 if (dst_bits == 64) {
985 if (is_signed && dst.regClass() == s2) {
986 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
987 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
988 } else if (is_signed && dst.regClass() == v2) {
989 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
990 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
991 } else {
992 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
993 }
994 }
995
996 return dst;
997 }
998
999 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1000 {
1001 if (!instr->dest.dest.is_ssa) {
1002 fprintf(stderr, "nir alu dst not in ssa: ");
1003 nir_print_instr(&instr->instr, stderr);
1004 fprintf(stderr, "\n");
1005 abort();
1006 }
1007 Builder bld(ctx->program, ctx->block);
1008 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1009 switch(instr->op) {
1010 case nir_op_vec2:
1011 case nir_op_vec3:
1012 case nir_op_vec4: {
1013 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1014 unsigned num = instr->dest.dest.ssa.num_components;
1015 for (unsigned i = 0; i < num; ++i)
1016 elems[i] = get_alu_src(ctx, instr->src[i]);
1017
1018 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1019 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1020 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1021 for (unsigned i = 0; i < num; ++i) {
1022 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1023 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1024 else
1025 vec->operands[i] = Operand{elems[i]};
1026 }
1027 vec->definitions[0] = Definition(dst);
1028 ctx->block->instructions.emplace_back(std::move(vec));
1029 ctx->allocated_vec.emplace(dst.id(), elems);
1030 } else {
1031 // TODO: that is a bit suboptimal..
1032 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1033 for (unsigned i = 0; i < num - 1; ++i)
1034 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1035 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1036 for (unsigned i = 0; i < num; ++i) {
1037 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1038 if (bit % 32 == 0) {
1039 elems[bit / 32] = elems[i];
1040 } else {
1041 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1042 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1043 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1044 }
1045 }
1046 if (dst.size() == 1)
1047 bld.copy(Definition(dst), elems[0]);
1048 else
1049 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1050 }
1051 break;
1052 }
1053 case nir_op_mov: {
1054 Temp src = get_alu_src(ctx, instr->src[0]);
1055 aco_ptr<Instruction> mov;
1056 if (dst.type() == RegType::sgpr) {
1057 if (src.type() == RegType::vgpr)
1058 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1059 else if (src.regClass() == s1)
1060 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1061 else if (src.regClass() == s2)
1062 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1063 else
1064 unreachable("wrong src register class for nir_op_imov");
1065 } else if (dst.regClass() == v1) {
1066 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1067 } else if (dst.regClass() == v2) {
1068 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1069 } else {
1070 nir_print_instr(&instr->instr, stderr);
1071 unreachable("Should have been lowered to scalar.");
1072 }
1073 break;
1074 }
1075 case nir_op_inot: {
1076 Temp src = get_alu_src(ctx, instr->src[0]);
1077 if (instr->dest.dest.ssa.bit_size == 1) {
1078 assert(src.regClass() == bld.lm);
1079 assert(dst.regClass() == bld.lm);
1080 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1081 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1082 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1083 } else if (dst.regClass() == v1) {
1084 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1085 } else if (dst.type() == RegType::sgpr) {
1086 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1087 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1088 } else {
1089 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1090 nir_print_instr(&instr->instr, stderr);
1091 fprintf(stderr, "\n");
1092 }
1093 break;
1094 }
1095 case nir_op_ineg: {
1096 Temp src = get_alu_src(ctx, instr->src[0]);
1097 if (dst.regClass() == v1) {
1098 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1099 } else if (dst.regClass() == s1) {
1100 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1101 } else if (dst.size() == 2) {
1102 Temp src0 = bld.tmp(dst.type(), 1);
1103 Temp src1 = bld.tmp(dst.type(), 1);
1104 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1105
1106 if (dst.regClass() == s2) {
1107 Temp carry = bld.tmp(s1);
1108 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1109 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1110 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1111 } else {
1112 Temp lower = bld.tmp(v1);
1113 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1114 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1115 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1116 }
1117 } else {
1118 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1119 nir_print_instr(&instr->instr, stderr);
1120 fprintf(stderr, "\n");
1121 }
1122 break;
1123 }
1124 case nir_op_iabs: {
1125 if (dst.regClass() == s1) {
1126 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1127 } else if (dst.regClass() == v1) {
1128 Temp src = get_alu_src(ctx, instr->src[0]);
1129 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
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_isign: {
1138 Temp src = get_alu_src(ctx, instr->src[0]);
1139 if (dst.regClass() == s1) {
1140 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1141 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1142 } else if (dst.regClass() == s2) {
1143 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1144 Temp neqz;
1145 if (ctx->program->chip_class >= GFX8)
1146 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1147 else
1148 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1149 /* SCC gets zero-extended to 64 bit */
1150 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1151 } else if (dst.regClass() == v1) {
1152 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1153 } else if (dst.regClass() == v2) {
1154 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1155 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1156 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1157 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1158 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1159 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1160 } else {
1161 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1162 nir_print_instr(&instr->instr, stderr);
1163 fprintf(stderr, "\n");
1164 }
1165 break;
1166 }
1167 case nir_op_imax: {
1168 if (dst.regClass() == v1) {
1169 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1170 } else if (dst.regClass() == s1) {
1171 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1172 } else {
1173 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1174 nir_print_instr(&instr->instr, stderr);
1175 fprintf(stderr, "\n");
1176 }
1177 break;
1178 }
1179 case nir_op_umax: {
1180 if (dst.regClass() == v1) {
1181 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1182 } else if (dst.regClass() == s1) {
1183 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1184 } else {
1185 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1186 nir_print_instr(&instr->instr, stderr);
1187 fprintf(stderr, "\n");
1188 }
1189 break;
1190 }
1191 case nir_op_imin: {
1192 if (dst.regClass() == v1) {
1193 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1194 } else if (dst.regClass() == s1) {
1195 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1196 } else {
1197 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1198 nir_print_instr(&instr->instr, stderr);
1199 fprintf(stderr, "\n");
1200 }
1201 break;
1202 }
1203 case nir_op_umin: {
1204 if (dst.regClass() == v1) {
1205 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1206 } else if (dst.regClass() == s1) {
1207 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1208 } else {
1209 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1210 nir_print_instr(&instr->instr, stderr);
1211 fprintf(stderr, "\n");
1212 }
1213 break;
1214 }
1215 case nir_op_ior: {
1216 if (instr->dest.dest.ssa.bit_size == 1) {
1217 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1218 } else if (dst.regClass() == v1) {
1219 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1220 } else if (dst.regClass() == s1) {
1221 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1222 } else if (dst.regClass() == s2) {
1223 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1224 } else {
1225 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1226 nir_print_instr(&instr->instr, stderr);
1227 fprintf(stderr, "\n");
1228 }
1229 break;
1230 }
1231 case nir_op_iand: {
1232 if (instr->dest.dest.ssa.bit_size == 1) {
1233 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1234 } else if (dst.regClass() == v1) {
1235 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1236 } else if (dst.regClass() == s1) {
1237 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1238 } else if (dst.regClass() == s2) {
1239 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1240 } else {
1241 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1242 nir_print_instr(&instr->instr, stderr);
1243 fprintf(stderr, "\n");
1244 }
1245 break;
1246 }
1247 case nir_op_ixor: {
1248 if (instr->dest.dest.ssa.bit_size == 1) {
1249 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1250 } else if (dst.regClass() == v1) {
1251 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1252 } else if (dst.regClass() == s1) {
1253 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1254 } else if (dst.regClass() == s2) {
1255 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1256 } else {
1257 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1258 nir_print_instr(&instr->instr, stderr);
1259 fprintf(stderr, "\n");
1260 }
1261 break;
1262 }
1263 case nir_op_ushr: {
1264 if (dst.regClass() == v1) {
1265 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1266 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1267 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1268 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1269 } else if (dst.regClass() == v2) {
1270 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1271 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1272 } else if (dst.regClass() == s2) {
1273 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1274 } else if (dst.regClass() == s1) {
1275 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1276 } else {
1277 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1278 nir_print_instr(&instr->instr, stderr);
1279 fprintf(stderr, "\n");
1280 }
1281 break;
1282 }
1283 case nir_op_ishl: {
1284 if (dst.regClass() == v1) {
1285 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1286 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1287 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1288 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1289 } else if (dst.regClass() == v2) {
1290 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1291 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1292 } else if (dst.regClass() == s1) {
1293 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1294 } else if (dst.regClass() == s2) {
1295 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1296 } else {
1297 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1298 nir_print_instr(&instr->instr, stderr);
1299 fprintf(stderr, "\n");
1300 }
1301 break;
1302 }
1303 case nir_op_ishr: {
1304 if (dst.regClass() == v1) {
1305 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1306 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1307 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1308 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1309 } else if (dst.regClass() == v2) {
1310 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1311 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1312 } else if (dst.regClass() == s1) {
1313 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1314 } else if (dst.regClass() == s2) {
1315 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1316 } else {
1317 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1318 nir_print_instr(&instr->instr, stderr);
1319 fprintf(stderr, "\n");
1320 }
1321 break;
1322 }
1323 case nir_op_find_lsb: {
1324 Temp src = get_alu_src(ctx, instr->src[0]);
1325 if (src.regClass() == s1) {
1326 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1327 } else if (src.regClass() == v1) {
1328 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1329 } else if (src.regClass() == s2) {
1330 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1331 } else {
1332 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1333 nir_print_instr(&instr->instr, stderr);
1334 fprintf(stderr, "\n");
1335 }
1336 break;
1337 }
1338 case nir_op_ufind_msb:
1339 case nir_op_ifind_msb: {
1340 Temp src = get_alu_src(ctx, instr->src[0]);
1341 if (src.regClass() == s1 || src.regClass() == s2) {
1342 aco_opcode op = src.regClass() == s2 ?
1343 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1344 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1345 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1346
1347 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1348 Operand(src.size() * 32u - 1u), msb_rev);
1349 Temp msb = sub.def(0).getTemp();
1350 Temp carry = sub.def(1).getTemp();
1351
1352 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1353 } else if (src.regClass() == v1) {
1354 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1355 Temp msb_rev = bld.tmp(v1);
1356 emit_vop1_instruction(ctx, instr, op, msb_rev);
1357 Temp msb = bld.tmp(v1);
1358 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1359 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1360 } else {
1361 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1362 nir_print_instr(&instr->instr, stderr);
1363 fprintf(stderr, "\n");
1364 }
1365 break;
1366 }
1367 case nir_op_bitfield_reverse: {
1368 if (dst.regClass() == s1) {
1369 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1370 } else if (dst.regClass() == v1) {
1371 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1372 } else {
1373 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1374 nir_print_instr(&instr->instr, stderr);
1375 fprintf(stderr, "\n");
1376 }
1377 break;
1378 }
1379 case nir_op_iadd: {
1380 if (dst.regClass() == s1) {
1381 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1382 break;
1383 }
1384
1385 Temp src0 = get_alu_src(ctx, instr->src[0]);
1386 Temp src1 = get_alu_src(ctx, instr->src[1]);
1387 if (dst.regClass() == v1) {
1388 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1389 break;
1390 }
1391
1392 assert(src0.size() == 2 && src1.size() == 2);
1393 Temp src00 = bld.tmp(src0.type(), 1);
1394 Temp src01 = bld.tmp(dst.type(), 1);
1395 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1396 Temp src10 = bld.tmp(src1.type(), 1);
1397 Temp src11 = bld.tmp(dst.type(), 1);
1398 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1399
1400 if (dst.regClass() == s2) {
1401 Temp carry = bld.tmp(s1);
1402 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1403 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1404 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1405 } else if (dst.regClass() == v2) {
1406 Temp dst0 = bld.tmp(v1);
1407 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1408 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1409 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1410 } else {
1411 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1412 nir_print_instr(&instr->instr, stderr);
1413 fprintf(stderr, "\n");
1414 }
1415 break;
1416 }
1417 case nir_op_uadd_sat: {
1418 Temp src0 = get_alu_src(ctx, instr->src[0]);
1419 Temp src1 = get_alu_src(ctx, instr->src[1]);
1420 if (dst.regClass() == s1) {
1421 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1422 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1423 src0, src1);
1424 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1425 } else if (dst.regClass() == v1) {
1426 if (ctx->options->chip_class >= GFX9) {
1427 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1428 add->operands[0] = Operand(src0);
1429 add->operands[1] = Operand(src1);
1430 add->definitions[0] = Definition(dst);
1431 add->clamp = 1;
1432 ctx->block->instructions.emplace_back(std::move(add));
1433 } else {
1434 if (src1.regClass() != v1)
1435 std::swap(src0, src1);
1436 assert(src1.regClass() == v1);
1437 Temp tmp = bld.tmp(v1);
1438 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1439 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1440 }
1441 } else {
1442 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1443 nir_print_instr(&instr->instr, stderr);
1444 fprintf(stderr, "\n");
1445 }
1446 break;
1447 }
1448 case nir_op_uadd_carry: {
1449 Temp src0 = get_alu_src(ctx, instr->src[0]);
1450 Temp src1 = get_alu_src(ctx, instr->src[1]);
1451 if (dst.regClass() == s1) {
1452 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1453 break;
1454 }
1455 if (dst.regClass() == v1) {
1456 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1457 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1458 break;
1459 }
1460
1461 Temp src00 = bld.tmp(src0.type(), 1);
1462 Temp src01 = bld.tmp(dst.type(), 1);
1463 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1464 Temp src10 = bld.tmp(src1.type(), 1);
1465 Temp src11 = bld.tmp(dst.type(), 1);
1466 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1467 if (dst.regClass() == s2) {
1468 Temp carry = bld.tmp(s1);
1469 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1470 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1471 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1472 } else if (dst.regClass() == v2) {
1473 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1474 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1475 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1476 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1477 } else {
1478 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1479 nir_print_instr(&instr->instr, stderr);
1480 fprintf(stderr, "\n");
1481 }
1482 break;
1483 }
1484 case nir_op_isub: {
1485 if (dst.regClass() == s1) {
1486 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1487 break;
1488 }
1489
1490 Temp src0 = get_alu_src(ctx, instr->src[0]);
1491 Temp src1 = get_alu_src(ctx, instr->src[1]);
1492 if (dst.regClass() == v1) {
1493 bld.vsub32(Definition(dst), src0, src1);
1494 break;
1495 }
1496
1497 Temp src00 = bld.tmp(src0.type(), 1);
1498 Temp src01 = bld.tmp(dst.type(), 1);
1499 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1500 Temp src10 = bld.tmp(src1.type(), 1);
1501 Temp src11 = bld.tmp(dst.type(), 1);
1502 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1503 if (dst.regClass() == s2) {
1504 Temp carry = bld.tmp(s1);
1505 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1506 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1507 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1508 } else if (dst.regClass() == v2) {
1509 Temp lower = bld.tmp(v1);
1510 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1511 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1512 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1513 } else {
1514 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1515 nir_print_instr(&instr->instr, stderr);
1516 fprintf(stderr, "\n");
1517 }
1518 break;
1519 }
1520 case nir_op_usub_borrow: {
1521 Temp src0 = get_alu_src(ctx, instr->src[0]);
1522 Temp src1 = get_alu_src(ctx, instr->src[1]);
1523 if (dst.regClass() == s1) {
1524 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1525 break;
1526 } else if (dst.regClass() == v1) {
1527 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1528 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1529 break;
1530 }
1531
1532 Temp src00 = bld.tmp(src0.type(), 1);
1533 Temp src01 = bld.tmp(dst.type(), 1);
1534 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1535 Temp src10 = bld.tmp(src1.type(), 1);
1536 Temp src11 = bld.tmp(dst.type(), 1);
1537 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1538 if (dst.regClass() == s2) {
1539 Temp borrow = bld.tmp(s1);
1540 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1541 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1542 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1543 } else if (dst.regClass() == v2) {
1544 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1545 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1546 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1547 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1548 } else {
1549 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1550 nir_print_instr(&instr->instr, stderr);
1551 fprintf(stderr, "\n");
1552 }
1553 break;
1554 }
1555 case nir_op_imul: {
1556 if (dst.regClass() == v1) {
1557 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1558 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1559 } else if (dst.regClass() == s1) {
1560 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1561 } else {
1562 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1563 nir_print_instr(&instr->instr, stderr);
1564 fprintf(stderr, "\n");
1565 }
1566 break;
1567 }
1568 case nir_op_umul_high: {
1569 if (dst.regClass() == v1) {
1570 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1571 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1572 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1573 } else if (dst.regClass() == s1) {
1574 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1575 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1576 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1577 } else {
1578 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1579 nir_print_instr(&instr->instr, stderr);
1580 fprintf(stderr, "\n");
1581 }
1582 break;
1583 }
1584 case nir_op_imul_high: {
1585 if (dst.regClass() == v1) {
1586 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1587 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1588 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1589 } else if (dst.regClass() == s1) {
1590 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1591 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1592 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1593 } else {
1594 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1595 nir_print_instr(&instr->instr, stderr);
1596 fprintf(stderr, "\n");
1597 }
1598 break;
1599 }
1600 case nir_op_fmul: {
1601 Temp src0 = get_alu_src(ctx, instr->src[0]);
1602 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1603 if (dst.regClass() == v2b) {
1604 Temp tmp = bld.tmp(v1);
1605 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, tmp, true);
1606 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1607 } else if (dst.regClass() == v1) {
1608 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1609 } else if (dst.regClass() == v2) {
1610 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1611 } else {
1612 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1613 nir_print_instr(&instr->instr, stderr);
1614 fprintf(stderr, "\n");
1615 }
1616 break;
1617 }
1618 case nir_op_fadd: {
1619 Temp src0 = get_alu_src(ctx, instr->src[0]);
1620 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1621 if (dst.regClass() == v2b) {
1622 Temp tmp = bld.tmp(v1);
1623 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, tmp, true);
1624 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1625 } else if (dst.regClass() == v1) {
1626 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1627 } else if (dst.regClass() == v2) {
1628 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1629 } else {
1630 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1631 nir_print_instr(&instr->instr, stderr);
1632 fprintf(stderr, "\n");
1633 }
1634 break;
1635 }
1636 case nir_op_fsub: {
1637 Temp src0 = get_alu_src(ctx, instr->src[0]);
1638 Temp src1 = get_alu_src(ctx, instr->src[1]);
1639 if (dst.regClass() == v2b) {
1640 Temp tmp = bld.tmp(v1);
1641 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1642 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, tmp, false);
1643 else
1644 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, tmp, true);
1645 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1646 } else if (dst.regClass() == v1) {
1647 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1648 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1649 else
1650 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1651 } else if (dst.regClass() == v2) {
1652 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1653 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1654 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1655 sub->neg[1] = true;
1656 } else {
1657 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1658 nir_print_instr(&instr->instr, stderr);
1659 fprintf(stderr, "\n");
1660 }
1661 break;
1662 }
1663 case nir_op_fmax: {
1664 Temp src0 = get_alu_src(ctx, instr->src[0]);
1665 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1666 if (dst.regClass() == v2b) {
1667 // TODO: check fp_mode.must_flush_denorms16_64
1668 Temp tmp = bld.tmp(v1);
1669 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, tmp, true);
1670 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1671 } else if (dst.regClass() == v1) {
1672 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1673 } else if (dst.regClass() == v2) {
1674 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1675 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1676 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1677 } else {
1678 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1679 }
1680 } else {
1681 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1682 nir_print_instr(&instr->instr, stderr);
1683 fprintf(stderr, "\n");
1684 }
1685 break;
1686 }
1687 case nir_op_fmin: {
1688 Temp src0 = get_alu_src(ctx, instr->src[0]);
1689 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1690 if (dst.regClass() == v2b) {
1691 // TODO: check fp_mode.must_flush_denorms16_64
1692 Temp tmp = bld.tmp(v1);
1693 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, tmp, true);
1694 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1695 } else if (dst.regClass() == v1) {
1696 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1697 } else if (dst.regClass() == v2) {
1698 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1699 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1700 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1701 } else {
1702 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1703 }
1704 } else {
1705 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1706 nir_print_instr(&instr->instr, stderr);
1707 fprintf(stderr, "\n");
1708 }
1709 break;
1710 }
1711 case nir_op_fmax3: {
1712 if (dst.regClass() == v2b) {
1713 Temp tmp = bld.tmp(v1);
1714 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, tmp, false);
1715 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1716 } else if (dst.regClass() == v1) {
1717 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1718 } else {
1719 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1720 nir_print_instr(&instr->instr, stderr);
1721 fprintf(stderr, "\n");
1722 }
1723 break;
1724 }
1725 case nir_op_fmin3: {
1726 if (dst.regClass() == v2b) {
1727 Temp tmp = bld.tmp(v1);
1728 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, tmp, false);
1729 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1730 } else if (dst.regClass() == v1) {
1731 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1732 } else {
1733 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1734 nir_print_instr(&instr->instr, stderr);
1735 fprintf(stderr, "\n");
1736 }
1737 break;
1738 }
1739 case nir_op_fmed3: {
1740 if (dst.regClass() == v2b) {
1741 Temp tmp = bld.tmp(v1);
1742 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, tmp, false);
1743 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1744 } else if (dst.regClass() == v1) {
1745 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1746 } else {
1747 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1748 nir_print_instr(&instr->instr, stderr);
1749 fprintf(stderr, "\n");
1750 }
1751 break;
1752 }
1753 case nir_op_umax3: {
1754 if (dst.size() == 1) {
1755 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1756 } else {
1757 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1758 nir_print_instr(&instr->instr, stderr);
1759 fprintf(stderr, "\n");
1760 }
1761 break;
1762 }
1763 case nir_op_umin3: {
1764 if (dst.size() == 1) {
1765 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1766 } else {
1767 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1768 nir_print_instr(&instr->instr, stderr);
1769 fprintf(stderr, "\n");
1770 }
1771 break;
1772 }
1773 case nir_op_umed3: {
1774 if (dst.size() == 1) {
1775 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1776 } else {
1777 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1778 nir_print_instr(&instr->instr, stderr);
1779 fprintf(stderr, "\n");
1780 }
1781 break;
1782 }
1783 case nir_op_imax3: {
1784 if (dst.size() == 1) {
1785 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1786 } else {
1787 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1788 nir_print_instr(&instr->instr, stderr);
1789 fprintf(stderr, "\n");
1790 }
1791 break;
1792 }
1793 case nir_op_imin3: {
1794 if (dst.size() == 1) {
1795 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1796 } else {
1797 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1798 nir_print_instr(&instr->instr, stderr);
1799 fprintf(stderr, "\n");
1800 }
1801 break;
1802 }
1803 case nir_op_imed3: {
1804 if (dst.size() == 1) {
1805 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1806 } else {
1807 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1808 nir_print_instr(&instr->instr, stderr);
1809 fprintf(stderr, "\n");
1810 }
1811 break;
1812 }
1813 case nir_op_cube_face_coord: {
1814 Temp in = get_alu_src(ctx, instr->src[0], 3);
1815 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1816 emit_extract_vector(ctx, in, 1, v1),
1817 emit_extract_vector(ctx, in, 2, v1) };
1818 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1819 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1820 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1821 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1822 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1823 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1824 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1825 break;
1826 }
1827 case nir_op_cube_face_index: {
1828 Temp in = get_alu_src(ctx, instr->src[0], 3);
1829 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1830 emit_extract_vector(ctx, in, 1, v1),
1831 emit_extract_vector(ctx, in, 2, v1) };
1832 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1833 break;
1834 }
1835 case nir_op_bcsel: {
1836 emit_bcsel(ctx, instr, dst);
1837 break;
1838 }
1839 case nir_op_frsq: {
1840 Temp src = get_alu_src(ctx, instr->src[0]);
1841 if (dst.regClass() == v2b) {
1842 Temp tmp = bld.vop1(aco_opcode::v_rsq_f16, bld.def(v1), src);
1843 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1844 } else if (dst.regClass() == v1) {
1845 emit_rsq(ctx, bld, Definition(dst), src);
1846 } else if (dst.regClass() == v2) {
1847 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1848 } else {
1849 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1850 nir_print_instr(&instr->instr, stderr);
1851 fprintf(stderr, "\n");
1852 }
1853 break;
1854 }
1855 case nir_op_fneg: {
1856 Temp src = get_alu_src(ctx, instr->src[0]);
1857 if (dst.regClass() == v2b) {
1858 Temp tmp = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x8000u), as_vgpr(ctx, src));
1859 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1860 } else if (dst.regClass() == v1) {
1861 if (ctx->block->fp_mode.must_flush_denorms32)
1862 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1863 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1864 } else if (dst.regClass() == v2) {
1865 if (ctx->block->fp_mode.must_flush_denorms16_64)
1866 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1867 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1868 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1869 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1870 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1871 } else {
1872 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1873 nir_print_instr(&instr->instr, stderr);
1874 fprintf(stderr, "\n");
1875 }
1876 break;
1877 }
1878 case nir_op_fabs: {
1879 Temp src = get_alu_src(ctx, instr->src[0]);
1880 if (dst.regClass() == v2b) {
1881 Temp tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFu), as_vgpr(ctx, src));
1882 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1883 } else if (dst.regClass() == v1) {
1884 if (ctx->block->fp_mode.must_flush_denorms32)
1885 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1886 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1887 } else if (dst.regClass() == v2) {
1888 if (ctx->block->fp_mode.must_flush_denorms16_64)
1889 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1890 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1891 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1892 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1893 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1894 } else {
1895 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1896 nir_print_instr(&instr->instr, stderr);
1897 fprintf(stderr, "\n");
1898 }
1899 break;
1900 }
1901 case nir_op_fsat: {
1902 Temp src = get_alu_src(ctx, instr->src[0]);
1903 if (dst.regClass() == v2b) {
1904 Temp tmp = bld.vop3(aco_opcode::v_med3_f16, bld.def(v1), Operand(0u), Operand(0x3f800000u), src);
1905 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1906 } else if (dst.regClass() == v1) {
1907 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1908 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1909 // TODO: confirm that this holds under any circumstances
1910 } else if (dst.regClass() == v2) {
1911 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1912 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1913 vop3->clamp = true;
1914 } else {
1915 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1916 nir_print_instr(&instr->instr, stderr);
1917 fprintf(stderr, "\n");
1918 }
1919 break;
1920 }
1921 case nir_op_flog2: {
1922 Temp src = get_alu_src(ctx, instr->src[0]);
1923 if (dst.regClass() == v2b) {
1924 Temp tmp = bld.vop1(aco_opcode::v_log_f16, bld.def(v1), src);
1925 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1926 } else if (dst.regClass() == v1) {
1927 emit_log2(ctx, bld, Definition(dst), src);
1928 } else {
1929 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1930 nir_print_instr(&instr->instr, stderr);
1931 fprintf(stderr, "\n");
1932 }
1933 break;
1934 }
1935 case nir_op_frcp: {
1936 Temp src = get_alu_src(ctx, instr->src[0]);
1937 if (dst.regClass() == v2b) {
1938 Temp tmp = bld.vop1(aco_opcode::v_rcp_f16, bld.def(v1), src);
1939 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1940 } else if (dst.regClass() == v1) {
1941 emit_rcp(ctx, bld, Definition(dst), src);
1942 } else if (dst.regClass() == v2) {
1943 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1944 } else {
1945 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1946 nir_print_instr(&instr->instr, stderr);
1947 fprintf(stderr, "\n");
1948 }
1949 break;
1950 }
1951 case nir_op_fexp2: {
1952 if (dst.regClass() == v2b) {
1953 Temp src = get_alu_src(ctx, instr->src[0]);
1954 Temp tmp = bld.vop1(aco_opcode::v_exp_f16, bld.def(v1), src);
1955 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1956 } else if (dst.regClass() == v1) {
1957 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1958 } else {
1959 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1960 nir_print_instr(&instr->instr, stderr);
1961 fprintf(stderr, "\n");
1962 }
1963 break;
1964 }
1965 case nir_op_fsqrt: {
1966 Temp src = get_alu_src(ctx, instr->src[0]);
1967 if (dst.regClass() == v2b) {
1968 Temp tmp = bld.vop1(aco_opcode::v_sqrt_f16, bld.def(v1), src);
1969 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1970 } else if (dst.regClass() == v1) {
1971 emit_sqrt(ctx, bld, Definition(dst), src);
1972 } else if (dst.regClass() == v2) {
1973 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1974 } else {
1975 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1976 nir_print_instr(&instr->instr, stderr);
1977 fprintf(stderr, "\n");
1978 }
1979 break;
1980 }
1981 case nir_op_ffract: {
1982 if (dst.regClass() == v2b) {
1983 Temp src = get_alu_src(ctx, instr->src[0]);
1984 Temp tmp = bld.vop1(aco_opcode::v_fract_f16, bld.def(v1), src);
1985 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1986 } else if (dst.regClass() == v1) {
1987 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1988 } else if (dst.regClass() == v2) {
1989 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1990 } else {
1991 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1992 nir_print_instr(&instr->instr, stderr);
1993 fprintf(stderr, "\n");
1994 }
1995 break;
1996 }
1997 case nir_op_ffloor: {
1998 Temp src = get_alu_src(ctx, instr->src[0]);
1999 if (dst.regClass() == v2b) {
2000 Temp tmp = bld.vop1(aco_opcode::v_floor_f16, bld.def(v1), src);
2001 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2002 } else if (dst.regClass() == v1) {
2003 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
2004 } else if (dst.regClass() == v2) {
2005 emit_floor_f64(ctx, bld, Definition(dst), src);
2006 } else {
2007 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2008 nir_print_instr(&instr->instr, stderr);
2009 fprintf(stderr, "\n");
2010 }
2011 break;
2012 }
2013 case nir_op_fceil: {
2014 Temp src0 = get_alu_src(ctx, instr->src[0]);
2015 if (dst.regClass() == v2b) {
2016 Temp tmp = bld.vop1(aco_opcode::v_ceil_f16, bld.def(v1), src0);
2017 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2018 } else if (dst.regClass() == v1) {
2019 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2020 } else if (dst.regClass() == v2) {
2021 if (ctx->options->chip_class >= GFX7) {
2022 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2023 } else {
2024 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2025 /* trunc = trunc(src0)
2026 * if (src0 > 0.0 && src0 != trunc)
2027 * trunc += 1.0
2028 */
2029 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2030 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2031 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2032 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2033 Temp add = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), bld.copy(bld.def(v1), Operand(0u)), bld.copy(bld.def(v1), Operand(0x3ff00000u)), cond);
2034 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2035 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2036 }
2037 } else {
2038 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2039 nir_print_instr(&instr->instr, stderr);
2040 fprintf(stderr, "\n");
2041 }
2042 break;
2043 }
2044 case nir_op_ftrunc: {
2045 Temp src = get_alu_src(ctx, instr->src[0]);
2046 if (dst.regClass() == v2b) {
2047 Temp tmp = bld.vop1(aco_opcode::v_trunc_f16, bld.def(v1), src);
2048 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2049 } else if (dst.regClass() == v1) {
2050 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2051 } else if (dst.regClass() == v2) {
2052 emit_trunc_f64(ctx, bld, Definition(dst), src);
2053 } else {
2054 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2055 nir_print_instr(&instr->instr, stderr);
2056 fprintf(stderr, "\n");
2057 }
2058 break;
2059 }
2060 case nir_op_fround_even: {
2061 Temp src0 = get_alu_src(ctx, instr->src[0]);
2062 if (dst.regClass() == v2b) {
2063 Temp tmp = bld.vop1(aco_opcode::v_rndne_f16, bld.def(v1), src0);
2064 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2065 } else if (dst.regClass() == v1) {
2066 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2067 } else if (dst.regClass() == v2) {
2068 if (ctx->options->chip_class >= GFX7) {
2069 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2070 } else {
2071 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2072 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2073 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2074
2075 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2076 Temp bfi = bld.vop3(aco_opcode::v_bfi_b32, bld.def(v1), bitmask, bld.copy(bld.def(v1), Operand(0x43300000u)), as_vgpr(ctx, src0_hi));
2077 Temp tmp = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), src0, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
2078 Instruction *sub = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), tmp, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
2079 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2080 tmp = sub->definitions[0].getTemp();
2081
2082 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2083 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2084 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2085 Temp cond = vop3->definitions[0].getTemp();
2086
2087 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2088 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2089 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2090 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2091
2092 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2093 }
2094 } else {
2095 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2096 nir_print_instr(&instr->instr, stderr);
2097 fprintf(stderr, "\n");
2098 }
2099 break;
2100 }
2101 case nir_op_fsin:
2102 case nir_op_fcos: {
2103 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2104 aco_ptr<Instruction> norm;
2105 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2106 if (dst.regClass() == v2b) {
2107 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2108 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2109 tmp = bld.vop1(opcode, bld.def(v1), tmp);
2110 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2111 } else if (dst.regClass() == v1) {
2112 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2113
2114 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2115 if (ctx->options->chip_class < GFX9)
2116 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2117
2118 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2119 bld.vop1(opcode, Definition(dst), tmp);
2120 } else {
2121 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2122 nir_print_instr(&instr->instr, stderr);
2123 fprintf(stderr, "\n");
2124 }
2125 break;
2126 }
2127 case nir_op_ldexp: {
2128 Temp src0 = get_alu_src(ctx, instr->src[0]);
2129 Temp src1 = get_alu_src(ctx, instr->src[1]);
2130 if (dst.regClass() == v2b) {
2131 Temp tmp = bld.tmp(v1);
2132 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, tmp, false);
2133 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2134 } else if (dst.regClass() == v1) {
2135 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2136 } else if (dst.regClass() == v2) {
2137 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2138 } else {
2139 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2140 nir_print_instr(&instr->instr, stderr);
2141 fprintf(stderr, "\n");
2142 }
2143 break;
2144 }
2145 case nir_op_frexp_sig: {
2146 Temp src = get_alu_src(ctx, instr->src[0]);
2147 if (dst.regClass() == v2b) {
2148 Temp tmp = bld.vop1(aco_opcode::v_frexp_mant_f16, bld.def(v1), src);
2149 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2150 } else if (dst.regClass() == v1) {
2151 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2152 } else if (dst.regClass() == v2) {
2153 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2154 } else {
2155 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2156 nir_print_instr(&instr->instr, stderr);
2157 fprintf(stderr, "\n");
2158 }
2159 break;
2160 }
2161 case nir_op_frexp_exp: {
2162 Temp src = get_alu_src(ctx, instr->src[0]);
2163 if (instr->src[0].src.ssa->bit_size == 16) {
2164 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2165 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2166 convert_int(bld, tmp, 8, 32, true, dst);
2167 } else if (instr->src[0].src.ssa->bit_size == 32) {
2168 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2169 } else if (instr->src[0].src.ssa->bit_size == 64) {
2170 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2171 } else {
2172 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2173 nir_print_instr(&instr->instr, stderr);
2174 fprintf(stderr, "\n");
2175 }
2176 break;
2177 }
2178 case nir_op_fsign: {
2179 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2180 if (dst.regClass() == v2b) {
2181 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2182 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2183 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2184 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2185 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2186 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), minus_one, src, cond);
2187 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2188 } else if (dst.regClass() == v1) {
2189 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2190 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2191 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2192 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2193 } else if (dst.regClass() == v2) {
2194 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2195 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2196 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2197
2198 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2199 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2200 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2201
2202 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2203 } else {
2204 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2205 nir_print_instr(&instr->instr, stderr);
2206 fprintf(stderr, "\n");
2207 }
2208 break;
2209 }
2210 case nir_op_f2f16:
2211 case nir_op_f2f16_rtne: {
2212 Temp src = get_alu_src(ctx, instr->src[0]);
2213 if (instr->src[0].src.ssa->bit_size == 64)
2214 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2215 src = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2216 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2217 break;
2218 }
2219 case nir_op_f2f16_rtz: {
2220 Temp src = get_alu_src(ctx, instr->src[0]);
2221 if (instr->src[0].src.ssa->bit_size == 64)
2222 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2223 src = bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, bld.def(v1), src, Operand(0u));
2224 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2225 break;
2226 }
2227 case nir_op_f2f32: {
2228 if (instr->src[0].src.ssa->bit_size == 16) {
2229 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2230 } else if (instr->src[0].src.ssa->bit_size == 64) {
2231 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2232 } else {
2233 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2234 nir_print_instr(&instr->instr, stderr);
2235 fprintf(stderr, "\n");
2236 }
2237 break;
2238 }
2239 case nir_op_f2f64: {
2240 Temp src = get_alu_src(ctx, instr->src[0]);
2241 if (instr->src[0].src.ssa->bit_size == 16)
2242 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2243 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2244 break;
2245 }
2246 case nir_op_i2f16: {
2247 assert(dst.regClass() == v2b);
2248 Temp src = get_alu_src(ctx, instr->src[0]);
2249 if (instr->src[0].src.ssa->bit_size == 8)
2250 src = convert_int(bld, src, 8, 16, true);
2251 Temp tmp = bld.vop1(aco_opcode::v_cvt_f16_i16, bld.def(v1), src);
2252 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2253 break;
2254 }
2255 case nir_op_i2f32: {
2256 assert(dst.size() == 1);
2257 Temp src = get_alu_src(ctx, instr->src[0]);
2258 if (instr->src[0].src.ssa->bit_size <= 16)
2259 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2260 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2261 break;
2262 }
2263 case nir_op_i2f64: {
2264 if (instr->src[0].src.ssa->bit_size <= 32) {
2265 Temp src = get_alu_src(ctx, instr->src[0]);
2266 if (instr->src[0].src.ssa->bit_size <= 16)
2267 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2268 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2269 } else if (instr->src[0].src.ssa->bit_size == 64) {
2270 Temp src = get_alu_src(ctx, instr->src[0]);
2271 RegClass rc = RegClass(src.type(), 1);
2272 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2273 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2274 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2275 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2276 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2277 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2278
2279 } else {
2280 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2281 nir_print_instr(&instr->instr, stderr);
2282 fprintf(stderr, "\n");
2283 }
2284 break;
2285 }
2286 case nir_op_u2f16: {
2287 assert(dst.regClass() == v2b);
2288 Temp src = get_alu_src(ctx, instr->src[0]);
2289 if (instr->src[0].src.ssa->bit_size == 8)
2290 src = convert_int(bld, src, 8, 16, false);
2291 Temp tmp = bld.vop1(aco_opcode::v_cvt_f16_u16, bld.def(v1), src);
2292 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2293 break;
2294 }
2295 case nir_op_u2f32: {
2296 assert(dst.size() == 1);
2297 Temp src = get_alu_src(ctx, instr->src[0]);
2298 if (instr->src[0].src.ssa->bit_size == 8) {
2299 //TODO: we should use v_cvt_f32_ubyte1/v_cvt_f32_ubyte2/etc depending on the register assignment
2300 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2301 } else {
2302 if (instr->src[0].src.ssa->bit_size == 16)
2303 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2304 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2305 }
2306 break;
2307 }
2308 case nir_op_u2f64: {
2309 if (instr->src[0].src.ssa->bit_size <= 32) {
2310 Temp src = get_alu_src(ctx, instr->src[0]);
2311 if (instr->src[0].src.ssa->bit_size <= 16)
2312 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2313 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2314 } else if (instr->src[0].src.ssa->bit_size == 64) {
2315 Temp src = get_alu_src(ctx, instr->src[0]);
2316 RegClass rc = RegClass(src.type(), 1);
2317 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2318 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2319 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2320 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2321 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2322 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2323 } else {
2324 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2325 nir_print_instr(&instr->instr, stderr);
2326 fprintf(stderr, "\n");
2327 }
2328 break;
2329 }
2330 case nir_op_f2i8:
2331 case nir_op_f2i16: {
2332 Temp src = get_alu_src(ctx, instr->src[0]);
2333 if (instr->src[0].src.ssa->bit_size == 16)
2334 src = bld.vop1(aco_opcode::v_cvt_i16_f16, bld.def(v1), src);
2335 else if (instr->src[0].src.ssa->bit_size == 32)
2336 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2337 else
2338 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2339
2340 if (dst.type() == RegType::vgpr)
2341 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2342 else
2343 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2344 break;
2345 }
2346 case nir_op_f2u8:
2347 case nir_op_f2u16: {
2348 Temp src = get_alu_src(ctx, instr->src[0]);
2349 if (instr->src[0].src.ssa->bit_size == 16)
2350 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2351 else if (instr->src[0].src.ssa->bit_size == 32)
2352 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2353 else
2354 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2355
2356 if (dst.type() == RegType::vgpr)
2357 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2358 else
2359 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2360 break;
2361 }
2362 case nir_op_f2i32: {
2363 Temp src = get_alu_src(ctx, instr->src[0]);
2364 if (instr->src[0].src.ssa->bit_size == 16) {
2365 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2366 if (dst.type() == RegType::vgpr) {
2367 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2368 } else {
2369 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2370 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2371 }
2372 } else if (instr->src[0].src.ssa->bit_size == 32) {
2373 if (dst.type() == RegType::vgpr)
2374 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2375 else
2376 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2377 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2378
2379 } else if (instr->src[0].src.ssa->bit_size == 64) {
2380 if (dst.type() == RegType::vgpr)
2381 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2382 else
2383 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2384 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2385
2386 } else {
2387 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2388 nir_print_instr(&instr->instr, stderr);
2389 fprintf(stderr, "\n");
2390 }
2391 break;
2392 }
2393 case nir_op_f2u32: {
2394 Temp src = get_alu_src(ctx, instr->src[0]);
2395 if (instr->src[0].src.ssa->bit_size == 16) {
2396 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2397 if (dst.type() == RegType::vgpr) {
2398 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2399 } else {
2400 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2401 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2402 }
2403 } else if (instr->src[0].src.ssa->bit_size == 32) {
2404 if (dst.type() == RegType::vgpr)
2405 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2406 else
2407 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2408 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2409
2410 } else if (instr->src[0].src.ssa->bit_size == 64) {
2411 if (dst.type() == RegType::vgpr)
2412 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2413 else
2414 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2415 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2416
2417 } else {
2418 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2419 nir_print_instr(&instr->instr, stderr);
2420 fprintf(stderr, "\n");
2421 }
2422 break;
2423 }
2424 case nir_op_f2i64: {
2425 Temp src = get_alu_src(ctx, instr->src[0]);
2426 if (instr->src[0].src.ssa->bit_size == 16)
2427 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2428
2429 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2430 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2431 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2432 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2433 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2434 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2435 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2436 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2437 Temp new_exponent = bld.tmp(v1);
2438 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2439 if (ctx->program->chip_class >= GFX8)
2440 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2441 else
2442 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2443 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2444 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2445 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2446 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2447 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2448 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2449 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2450 Temp new_lower = bld.tmp(v1);
2451 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2452 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2453 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2454
2455 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2456 if (src.type() == RegType::vgpr)
2457 src = bld.as_uniform(src);
2458 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2459 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2460 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2461 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2462 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2463 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2464 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2465 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2466 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2467 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2468 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2469 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2470 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2471 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2472 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2473 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2474 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2475 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2476 Temp borrow = bld.tmp(s1);
2477 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2478 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2479 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2480
2481 } else if (instr->src[0].src.ssa->bit_size == 64) {
2482 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2483 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2484 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2485 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2486 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2487 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2488 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2489 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2490 if (dst.type() == RegType::sgpr) {
2491 lower = bld.as_uniform(lower);
2492 upper = bld.as_uniform(upper);
2493 }
2494 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2495
2496 } else {
2497 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2498 nir_print_instr(&instr->instr, stderr);
2499 fprintf(stderr, "\n");
2500 }
2501 break;
2502 }
2503 case nir_op_f2u64: {
2504 Temp src = get_alu_src(ctx, instr->src[0]);
2505 if (instr->src[0].src.ssa->bit_size == 16)
2506 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2507
2508 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2509 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2510 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2511 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2512 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2513 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2514 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2515 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2516 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2517 Temp new_exponent = bld.tmp(v1);
2518 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2519 if (ctx->program->chip_class >= GFX8)
2520 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2521 else
2522 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2523 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2524 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2525 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2526 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2527 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2528 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2529 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2530
2531 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2532 if (src.type() == RegType::vgpr)
2533 src = bld.as_uniform(src);
2534 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2535 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2536 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2537 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2538 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2539 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2540 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2541 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2542 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2543 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2544 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2545 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2546 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2547 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2548 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2549 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2550 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2551 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2552
2553 } else if (instr->src[0].src.ssa->bit_size == 64) {
2554 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2555 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2556 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2557 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2558 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2559 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2560 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2561 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2562 if (dst.type() == RegType::sgpr) {
2563 lower = bld.as_uniform(lower);
2564 upper = bld.as_uniform(upper);
2565 }
2566 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2567
2568 } else {
2569 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2570 nir_print_instr(&instr->instr, stderr);
2571 fprintf(stderr, "\n");
2572 }
2573 break;
2574 }
2575 case nir_op_b2f16: {
2576 Temp src = get_alu_src(ctx, instr->src[0]);
2577 assert(src.regClass() == bld.lm);
2578
2579 if (dst.regClass() == s1) {
2580 src = bool_to_scalar_condition(ctx, src);
2581 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2582 } else if (dst.regClass() == v2b) {
2583 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2584 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2585 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2586 } else {
2587 unreachable("Wrong destination register class for nir_op_b2f16.");
2588 }
2589 break;
2590 }
2591 case nir_op_b2f32: {
2592 Temp src = get_alu_src(ctx, instr->src[0]);
2593 assert(src.regClass() == bld.lm);
2594
2595 if (dst.regClass() == s1) {
2596 src = bool_to_scalar_condition(ctx, src);
2597 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2598 } else if (dst.regClass() == v1) {
2599 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2600 } else {
2601 unreachable("Wrong destination register class for nir_op_b2f32.");
2602 }
2603 break;
2604 }
2605 case nir_op_b2f64: {
2606 Temp src = get_alu_src(ctx, instr->src[0]);
2607 assert(src.regClass() == bld.lm);
2608
2609 if (dst.regClass() == s2) {
2610 src = bool_to_scalar_condition(ctx, src);
2611 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2612 } else if (dst.regClass() == v2) {
2613 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2614 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2615 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2616 } else {
2617 unreachable("Wrong destination register class for nir_op_b2f64.");
2618 }
2619 break;
2620 }
2621 case nir_op_i2i8:
2622 case nir_op_i2i16:
2623 case nir_op_i2i32:
2624 case nir_op_i2i64: {
2625 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2626 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2627 break;
2628 }
2629 case nir_op_u2u8:
2630 case nir_op_u2u16:
2631 case nir_op_u2u32:
2632 case nir_op_u2u64: {
2633 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2634 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2635 break;
2636 }
2637 case nir_op_b2b32:
2638 case nir_op_b2i32: {
2639 Temp src = get_alu_src(ctx, instr->src[0]);
2640 assert(src.regClass() == bld.lm);
2641
2642 if (dst.regClass() == s1) {
2643 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2644 bool_to_scalar_condition(ctx, src, dst);
2645 } else if (dst.regClass() == v1) {
2646 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2647 } else {
2648 unreachable("Invalid register class for b2i32");
2649 }
2650 break;
2651 }
2652 case nir_op_b2b1:
2653 case nir_op_i2b1: {
2654 Temp src = get_alu_src(ctx, instr->src[0]);
2655 assert(dst.regClass() == bld.lm);
2656
2657 if (src.type() == RegType::vgpr) {
2658 assert(src.regClass() == v1 || src.regClass() == v2);
2659 assert(dst.regClass() == bld.lm);
2660 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2661 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2662 } else {
2663 assert(src.regClass() == s1 || src.regClass() == s2);
2664 Temp tmp;
2665 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2666 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2667 } else {
2668 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2669 bld.scc(bld.def(s1)), Operand(0u), src);
2670 }
2671 bool_to_vector_condition(ctx, tmp, dst);
2672 }
2673 break;
2674 }
2675 case nir_op_pack_64_2x32_split: {
2676 Temp src0 = get_alu_src(ctx, instr->src[0]);
2677 Temp src1 = get_alu_src(ctx, instr->src[1]);
2678
2679 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2680 break;
2681 }
2682 case nir_op_unpack_64_2x32_split_x:
2683 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2684 break;
2685 case nir_op_unpack_64_2x32_split_y:
2686 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2687 break;
2688 case nir_op_unpack_32_2x16_split_x:
2689 if (dst.type() == RegType::vgpr) {
2690 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2691 } else {
2692 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2693 }
2694 break;
2695 case nir_op_unpack_32_2x16_split_y:
2696 if (dst.type() == RegType::vgpr) {
2697 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2698 } else {
2699 bld.sop2(aco_opcode::s_bfe_u32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]), Operand(uint32_t(16 << 16 | 16)));
2700 }
2701 break;
2702 case nir_op_pack_32_2x16_split: {
2703 Temp src0 = get_alu_src(ctx, instr->src[0]);
2704 Temp src1 = get_alu_src(ctx, instr->src[1]);
2705 if (dst.regClass() == v1) {
2706 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2707 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2708 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2709 } else {
2710 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2711 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2712 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2713 }
2714 break;
2715 }
2716 case nir_op_pack_half_2x16: {
2717 Temp src = get_alu_src(ctx, instr->src[0], 2);
2718
2719 if (dst.regClass() == v1) {
2720 Temp src0 = bld.tmp(v1);
2721 Temp src1 = bld.tmp(v1);
2722 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2723 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2724 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2725 else
2726 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2727 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2728 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2729 } else {
2730 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2731 nir_print_instr(&instr->instr, stderr);
2732 fprintf(stderr, "\n");
2733 }
2734 break;
2735 }
2736 case nir_op_unpack_half_2x16_split_x: {
2737 if (dst.regClass() == v1) {
2738 Builder bld(ctx->program, ctx->block);
2739 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2740 } else {
2741 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2742 nir_print_instr(&instr->instr, stderr);
2743 fprintf(stderr, "\n");
2744 }
2745 break;
2746 }
2747 case nir_op_unpack_half_2x16_split_y: {
2748 if (dst.regClass() == v1) {
2749 Builder bld(ctx->program, ctx->block);
2750 /* TODO: use SDWA here */
2751 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2752 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2753 } else {
2754 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2755 nir_print_instr(&instr->instr, stderr);
2756 fprintf(stderr, "\n");
2757 }
2758 break;
2759 }
2760 case nir_op_fquantize2f16: {
2761 Temp src = get_alu_src(ctx, instr->src[0]);
2762 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2763 Temp f32, cmp_res;
2764
2765 if (ctx->program->chip_class >= GFX8) {
2766 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2767 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2768 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2769 } else {
2770 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2771 * so compare the result and flush to 0 if it's smaller.
2772 */
2773 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2774 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2775 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2776 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2777 cmp_res = vop3->definitions[0].getTemp();
2778 }
2779
2780 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2781 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2782 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2783 } else {
2784 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2785 }
2786 break;
2787 }
2788 case nir_op_bfm: {
2789 Temp bits = get_alu_src(ctx, instr->src[0]);
2790 Temp offset = get_alu_src(ctx, instr->src[1]);
2791
2792 if (dst.regClass() == s1) {
2793 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2794 } else if (dst.regClass() == v1) {
2795 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2796 } else {
2797 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2798 nir_print_instr(&instr->instr, stderr);
2799 fprintf(stderr, "\n");
2800 }
2801 break;
2802 }
2803 case nir_op_bitfield_select: {
2804 /* (mask & insert) | (~mask & base) */
2805 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2806 Temp insert = get_alu_src(ctx, instr->src[1]);
2807 Temp base = get_alu_src(ctx, instr->src[2]);
2808
2809 /* dst = (insert & bitmask) | (base & ~bitmask) */
2810 if (dst.regClass() == s1) {
2811 aco_ptr<Instruction> sop2;
2812 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2813 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2814 Operand lhs;
2815 if (const_insert && const_bitmask) {
2816 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2817 } else {
2818 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2819 lhs = Operand(insert);
2820 }
2821
2822 Operand rhs;
2823 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2824 if (const_base && const_bitmask) {
2825 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2826 } else {
2827 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2828 rhs = Operand(base);
2829 }
2830
2831 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2832
2833 } else if (dst.regClass() == v1) {
2834 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2835 base = as_vgpr(ctx, base);
2836 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2837 insert = as_vgpr(ctx, insert);
2838
2839 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2840
2841 } else {
2842 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2843 nir_print_instr(&instr->instr, stderr);
2844 fprintf(stderr, "\n");
2845 }
2846 break;
2847 }
2848 case nir_op_ubfe:
2849 case nir_op_ibfe: {
2850 Temp base = get_alu_src(ctx, instr->src[0]);
2851 Temp offset = get_alu_src(ctx, instr->src[1]);
2852 Temp bits = get_alu_src(ctx, instr->src[2]);
2853
2854 if (dst.type() == RegType::sgpr) {
2855 Operand extract;
2856 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2857 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2858 if (const_offset && const_bits) {
2859 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2860 extract = Operand(const_extract);
2861 } else {
2862 Operand width;
2863 if (const_bits) {
2864 width = Operand(const_bits->u32 << 16);
2865 } else {
2866 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2867 }
2868 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2869 }
2870
2871 aco_opcode opcode;
2872 if (dst.regClass() == s1) {
2873 if (instr->op == nir_op_ubfe)
2874 opcode = aco_opcode::s_bfe_u32;
2875 else
2876 opcode = aco_opcode::s_bfe_i32;
2877 } else if (dst.regClass() == s2) {
2878 if (instr->op == nir_op_ubfe)
2879 opcode = aco_opcode::s_bfe_u64;
2880 else
2881 opcode = aco_opcode::s_bfe_i64;
2882 } else {
2883 unreachable("Unsupported BFE bit size");
2884 }
2885
2886 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2887
2888 } else {
2889 aco_opcode opcode;
2890 if (dst.regClass() == v1) {
2891 if (instr->op == nir_op_ubfe)
2892 opcode = aco_opcode::v_bfe_u32;
2893 else
2894 opcode = aco_opcode::v_bfe_i32;
2895 } else {
2896 unreachable("Unsupported BFE bit size");
2897 }
2898
2899 emit_vop3a_instruction(ctx, instr, opcode, dst);
2900 }
2901 break;
2902 }
2903 case nir_op_bit_count: {
2904 Temp src = get_alu_src(ctx, instr->src[0]);
2905 if (src.regClass() == s1) {
2906 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2907 } else if (src.regClass() == v1) {
2908 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2909 } else if (src.regClass() == v2) {
2910 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2911 emit_extract_vector(ctx, src, 1, v1),
2912 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2913 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2914 } else if (src.regClass() == s2) {
2915 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2916 } else {
2917 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2918 nir_print_instr(&instr->instr, stderr);
2919 fprintf(stderr, "\n");
2920 }
2921 break;
2922 }
2923 case nir_op_flt: {
2924 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2925 break;
2926 }
2927 case nir_op_fge: {
2928 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2929 break;
2930 }
2931 case nir_op_feq: {
2932 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2933 break;
2934 }
2935 case nir_op_fne: {
2936 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2937 break;
2938 }
2939 case nir_op_ilt: {
2940 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_i16, aco_opcode::v_cmp_lt_i32, aco_opcode::v_cmp_lt_i64, aco_opcode::s_cmp_lt_i32);
2941 break;
2942 }
2943 case nir_op_ige: {
2944 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_i16, aco_opcode::v_cmp_ge_i32, aco_opcode::v_cmp_ge_i64, aco_opcode::s_cmp_ge_i32);
2945 break;
2946 }
2947 case nir_op_ieq: {
2948 if (instr->src[0].src.ssa->bit_size == 1)
2949 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2950 else
2951 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_i16, aco_opcode::v_cmp_eq_i32, aco_opcode::v_cmp_eq_i64, aco_opcode::s_cmp_eq_i32,
2952 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2953 break;
2954 }
2955 case nir_op_ine: {
2956 if (instr->src[0].src.ssa->bit_size == 1)
2957 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2958 else
2959 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lg_i16, aco_opcode::v_cmp_lg_i32, aco_opcode::v_cmp_lg_i64, aco_opcode::s_cmp_lg_i32,
2960 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2961 break;
2962 }
2963 case nir_op_ult: {
2964 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_u16, aco_opcode::v_cmp_lt_u32, aco_opcode::v_cmp_lt_u64, aco_opcode::s_cmp_lt_u32);
2965 break;
2966 }
2967 case nir_op_uge: {
2968 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_u16, aco_opcode::v_cmp_ge_u32, aco_opcode::v_cmp_ge_u64, aco_opcode::s_cmp_ge_u32);
2969 break;
2970 }
2971 case nir_op_fddx:
2972 case nir_op_fddy:
2973 case nir_op_fddx_fine:
2974 case nir_op_fddy_fine:
2975 case nir_op_fddx_coarse:
2976 case nir_op_fddy_coarse: {
2977 Temp src = get_alu_src(ctx, instr->src[0]);
2978 uint16_t dpp_ctrl1, dpp_ctrl2;
2979 if (instr->op == nir_op_fddx_fine) {
2980 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2981 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2982 } else if (instr->op == nir_op_fddy_fine) {
2983 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2984 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2985 } else {
2986 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2987 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2988 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2989 else
2990 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2991 }
2992
2993 Temp tmp;
2994 if (ctx->program->chip_class >= GFX8) {
2995 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2996 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2997 } else {
2998 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2999 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3000 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3001 }
3002 emit_wqm(ctx, tmp, dst, true);
3003 break;
3004 }
3005 default:
3006 fprintf(stderr, "Unknown NIR ALU instr: ");
3007 nir_print_instr(&instr->instr, stderr);
3008 fprintf(stderr, "\n");
3009 }
3010 }
3011
3012 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3013 {
3014 Temp dst = get_ssa_temp(ctx, &instr->def);
3015
3016 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3017 // which get truncated the lsb if double and msb if int
3018 // for now, we only use s_mov_b64 with 64bit inline constants
3019 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3020 assert(dst.type() == RegType::sgpr);
3021
3022 Builder bld(ctx->program, ctx->block);
3023
3024 if (instr->def.bit_size == 1) {
3025 assert(dst.regClass() == bld.lm);
3026 int val = instr->value[0].b ? -1 : 0;
3027 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3028 bld.sop1(Builder::s_mov, Definition(dst), op);
3029 } else if (instr->def.bit_size == 8) {
3030 /* ensure that the value is correctly represented in the low byte of the register */
3031 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3032 } else if (instr->def.bit_size == 16) {
3033 /* ensure that the value is correctly represented in the low half of the register */
3034 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3035 } else if (dst.size() == 1) {
3036 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3037 } else {
3038 assert(dst.size() != 1);
3039 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3040 if (instr->def.bit_size == 64)
3041 for (unsigned i = 0; i < dst.size(); i++)
3042 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3043 else {
3044 for (unsigned i = 0; i < dst.size(); i++)
3045 vec->operands[i] = Operand{instr->value[i].u32};
3046 }
3047 vec->definitions[0] = Definition(dst);
3048 ctx->block->instructions.emplace_back(std::move(vec));
3049 }
3050 }
3051
3052 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3053 {
3054 uint32_t new_mask = 0;
3055 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3056 if (mask & (1u << i))
3057 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3058 return new_mask;
3059 }
3060
3061 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst)
3062 {
3063 Builder bld(ctx->program, ctx->block);
3064 if (offset.isTemp()) {
3065 Temp tmp[3] = {vec, vec, vec};
3066
3067 if (vec.size() == 3) {
3068 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
3069 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
3070 } else if (vec.size() == 2) {
3071 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
3072 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
3073 }
3074 for (unsigned i = 0; i < dst.size(); i++)
3075 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
3076
3077 vec = tmp[0];
3078 if (dst.size() == 2)
3079 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
3080
3081 offset = Operand(0u);
3082 }
3083
3084 if (vec.bytes() == dst.bytes() && offset.constantValue() == 0)
3085 bld.copy(Definition(dst), vec);
3086 else
3087 trim_subdword_vector(ctx, vec, dst, vec.bytes(), ((1 << dst.bytes()) - 1) << offset.constantValue());
3088 }
3089
3090 struct LoadEmitInfo {
3091 Operand offset;
3092 Temp dst;
3093 unsigned num_components;
3094 unsigned component_size;
3095 Temp resource = Temp(0, s1);
3096 unsigned component_stride = 0;
3097 unsigned const_offset = 0;
3098 unsigned align_mul = 0;
3099 unsigned align_offset = 0;
3100
3101 bool glc = false;
3102 unsigned swizzle_component_size = 0;
3103 barrier_interaction barrier = barrier_none;
3104 bool can_reorder = true;
3105 Temp soffset = Temp(0, s1);
3106 };
3107
3108 using LoadCallback = Temp(*)(
3109 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3110 unsigned align, unsigned const_offset, Temp dst_hint);
3111
3112 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3113 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3114 {
3115 unsigned load_size = info->num_components * info->component_size;
3116 unsigned component_size = info->component_size;
3117
3118 unsigned num_vals = 0;
3119 Temp vals[info->dst.bytes()];
3120
3121 unsigned const_offset = info->const_offset;
3122
3123 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3124 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3125
3126 unsigned bytes_read = 0;
3127 while (bytes_read < load_size) {
3128 unsigned bytes_needed = load_size - bytes_read;
3129
3130 /* add buffer for unaligned loads */
3131 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3132
3133 if (byte_align) {
3134 if ((bytes_needed > 2 || !supports_8bit_16bit_loads) && byte_align_loads) {
3135 if (info->component_stride) {
3136 assert(supports_8bit_16bit_loads && "unimplemented");
3137 bytes_needed = 2;
3138 byte_align = 0;
3139 } else {
3140 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3141 bytes_needed = align(bytes_needed, 4);
3142 }
3143 } else {
3144 byte_align = 0;
3145 }
3146 }
3147
3148 if (info->swizzle_component_size)
3149 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3150 if (info->component_stride)
3151 bytes_needed = MIN2(bytes_needed, info->component_size);
3152
3153 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3154
3155 /* reduce constant offset */
3156 Operand offset = info->offset;
3157 unsigned reduced_const_offset = const_offset;
3158 bool remove_const_offset_completely = need_to_align_offset;
3159 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3160 unsigned to_add = const_offset;
3161 if (remove_const_offset_completely) {
3162 reduced_const_offset = 0;
3163 } else {
3164 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3165 reduced_const_offset %= max_const_offset_plus_one;
3166 }
3167 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3168 if (offset.isConstant()) {
3169 offset = Operand(offset.constantValue() + to_add);
3170 } else if (offset_tmp.regClass() == s1) {
3171 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3172 offset_tmp, Operand(to_add));
3173 } else if (offset_tmp.regClass() == v1) {
3174 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3175 } else {
3176 Temp lo = bld.tmp(offset_tmp.type(), 1);
3177 Temp hi = bld.tmp(offset_tmp.type(), 1);
3178 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3179
3180 if (offset_tmp.regClass() == s2) {
3181 Temp carry = bld.tmp(s1);
3182 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3183 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3184 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3185 } else {
3186 Temp new_lo = bld.tmp(v1);
3187 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3188 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3189 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3190 }
3191 }
3192 }
3193
3194 /* align offset down if needed */
3195 Operand aligned_offset = offset;
3196 if (need_to_align_offset) {
3197 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3198 if (offset.isConstant()) {
3199 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3200 } else if (offset_tmp.regClass() == s1) {
3201 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3202 } else if (offset_tmp.regClass() == s2) {
3203 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3204 } else if (offset_tmp.regClass() == v1) {
3205 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3206 } else if (offset_tmp.regClass() == v2) {
3207 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3208 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3209 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3210 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3211 }
3212 }
3213 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3214 bld.copy(bld.def(s1), aligned_offset);
3215
3216 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3217 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3218 reduced_const_offset, byte_align ? Temp() : info->dst);
3219
3220 /* shift result right if needed */
3221 if (byte_align) {
3222 Operand align((uint32_t)byte_align);
3223 if (byte_align == -1) {
3224 if (offset.isConstant())
3225 align = Operand(offset.constantValue() % 4u);
3226 else if (offset.size() == 2)
3227 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3228 else
3229 align = offset;
3230 }
3231
3232 if (align.isTemp() || align.constantValue()) {
3233 assert(val.bytes() >= load_size && "unimplemented");
3234 Temp new_val = bld.tmp(RegClass::get(val.type(), load_size));
3235 if (val.type() == RegType::sgpr)
3236 byte_align_scalar(ctx, val, align, new_val);
3237 else
3238 byte_align_vector(ctx, val, align, new_val);
3239 val = new_val;
3240 }
3241 }
3242
3243 /* add result to list and advance */
3244 if (info->component_stride) {
3245 assert(val.bytes() == info->component_size && "unimplemented");
3246 const_offset += info->component_stride;
3247 align_offset = (align_offset + info->component_stride) % align_mul;
3248 } else {
3249 const_offset += val.bytes();
3250 align_offset = (align_offset + val.bytes()) % align_mul;
3251 }
3252 bytes_read += val.bytes();
3253 vals[num_vals++] = val;
3254 }
3255
3256 /* the callback wrote directly to dst */
3257 if (vals[0] == info->dst) {
3258 assert(num_vals == 1);
3259 emit_split_vector(ctx, info->dst, info->num_components);
3260 return;
3261 }
3262
3263 /* create array of components */
3264 unsigned components_split = 0;
3265 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3266 bool has_vgprs = false;
3267 for (unsigned i = 0; i < num_vals;) {
3268 Temp tmp[num_vals];
3269 unsigned num_tmps = 0;
3270 unsigned tmp_size = 0;
3271 RegType reg_type = RegType::sgpr;
3272 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3273 if (vals[i].type() == RegType::vgpr)
3274 reg_type = RegType::vgpr;
3275 tmp_size += vals[i].bytes();
3276 tmp[num_tmps++] = vals[i++];
3277 }
3278 if (num_tmps > 1) {
3279 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3280 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3281 for (unsigned i = 0; i < num_vals; i++)
3282 vec->operands[i] = Operand(tmp[i]);
3283 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3284 vec->definitions[0] = Definition(tmp[0]);
3285 bld.insert(std::move(vec));
3286 }
3287
3288 if (tmp[0].bytes() % component_size) {
3289 /* trim tmp[0] */
3290 assert(i == num_vals);
3291 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3292 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3293 }
3294
3295 RegClass elem_rc = RegClass::get(reg_type, component_size);
3296
3297 unsigned start = components_split;
3298
3299 if (tmp_size == elem_rc.bytes()) {
3300 allocated_vec[components_split++] = tmp[0];
3301 } else {
3302 assert(tmp_size % elem_rc.bytes() == 0);
3303 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3304 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3305 for (unsigned i = 0; i < split->definitions.size(); i++) {
3306 Temp component = bld.tmp(elem_rc);
3307 allocated_vec[components_split++] = component;
3308 split->definitions[i] = Definition(component);
3309 }
3310 split->operands[0] = Operand(tmp[0]);
3311 bld.insert(std::move(split));
3312 }
3313
3314 /* try to p_as_uniform early so we can create more optimizable code and
3315 * also update allocated_vec */
3316 for (unsigned j = start; j < components_split; j++) {
3317 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3318 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3319 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3320 }
3321 }
3322
3323 /* concatenate components and p_as_uniform() result if needed */
3324 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3325 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3326
3327 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3328
3329 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3330 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3331 for (unsigned i = 0; i < info->num_components; i++)
3332 vec->operands[i] = Operand(allocated_vec[i]);
3333 if (padding_bytes)
3334 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3335 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3336 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3337 vec->definitions[0] = Definition(tmp);
3338 bld.insert(std::move(vec));
3339 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3340 } else {
3341 vec->definitions[0] = Definition(info->dst);
3342 bld.insert(std::move(vec));
3343 }
3344 }
3345
3346 Operand load_lds_size_m0(Builder& bld)
3347 {
3348 /* TODO: m0 does not need to be initialized on GFX9+ */
3349 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3350 }
3351
3352 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3353 Temp offset, unsigned bytes_needed,
3354 unsigned align, unsigned const_offset,
3355 Temp dst_hint)
3356 {
3357 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3358
3359 Operand m = load_lds_size_m0(bld);
3360
3361 bool large_ds_read = bld.program->chip_class >= GFX7;
3362 bool usable_read2 = bld.program->chip_class >= GFX7;
3363
3364 bool read2 = false;
3365 unsigned size = 0;
3366 aco_opcode op;
3367 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3368 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3369 size = 16;
3370 op = aco_opcode::ds_read_b128;
3371 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3372 size = 16;
3373 read2 = true;
3374 op = aco_opcode::ds_read2_b64;
3375 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3376 size = 12;
3377 op = aco_opcode::ds_read_b96;
3378 } else if (bytes_needed >= 8 && align % 8 == 0) {
3379 size = 8;
3380 op = aco_opcode::ds_read_b64;
3381 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3382 size = 8;
3383 read2 = true;
3384 op = aco_opcode::ds_read2_b32;
3385 } else if (bytes_needed >= 4 && align % 4 == 0) {
3386 size = 4;
3387 op = aco_opcode::ds_read_b32;
3388 } else if (bytes_needed >= 2 && align % 2 == 0) {
3389 size = 2;
3390 op = aco_opcode::ds_read_u16;
3391 } else {
3392 size = 1;
3393 op = aco_opcode::ds_read_u8;
3394 }
3395
3396 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3397 if (const_offset >= max_offset_plus_one) {
3398 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3399 const_offset %= max_offset_plus_one;
3400 }
3401
3402 if (read2)
3403 const_offset /= (size / 2u);
3404
3405 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3406 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3407 if (read2)
3408 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3409 else
3410 bld.ds(op, Definition(val), offset, m, const_offset);
3411
3412 if (size < 4)
3413 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3414
3415 return val;
3416 }
3417
3418 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3419
3420 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3421 Temp offset, unsigned bytes_needed,
3422 unsigned align, unsigned const_offset,
3423 Temp dst_hint)
3424 {
3425 unsigned size = 0;
3426 aco_opcode op;
3427 if (bytes_needed <= 4) {
3428 size = 1;
3429 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3430 } else if (bytes_needed <= 8) {
3431 size = 2;
3432 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3433 } else if (bytes_needed <= 16) {
3434 size = 4;
3435 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3436 } else if (bytes_needed <= 32) {
3437 size = 8;
3438 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3439 } else {
3440 size = 16;
3441 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3442 }
3443 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3444 if (info->resource.id()) {
3445 load->operands[0] = Operand(info->resource);
3446 load->operands[1] = Operand(offset);
3447 } else {
3448 load->operands[0] = Operand(offset);
3449 load->operands[1] = Operand(0u);
3450 }
3451 RegClass rc(RegType::sgpr, size);
3452 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3453 load->definitions[0] = Definition(val);
3454 load->glc = info->glc;
3455 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3456 load->barrier = info->barrier;
3457 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3458 bld.insert(std::move(load));
3459 return val;
3460 }
3461
3462 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3463
3464 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3465 Temp offset, unsigned bytes_needed,
3466 unsigned align_, unsigned const_offset,
3467 Temp dst_hint)
3468 {
3469 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3470 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3471
3472 if (info->soffset.id()) {
3473 if (soffset.isTemp())
3474 vaddr = bld.copy(bld.def(v1), soffset);
3475 soffset = Operand(info->soffset);
3476 }
3477
3478 unsigned bytes_size = 0;
3479 aco_opcode op;
3480 if (bytes_needed == 1) {
3481 bytes_size = 1;
3482 op = aco_opcode::buffer_load_ubyte;
3483 } else if (bytes_needed == 2) {
3484 bytes_size = 2;
3485 op = aco_opcode::buffer_load_ushort;
3486 } else if (bytes_needed <= 4) {
3487 bytes_size = 4;
3488 op = aco_opcode::buffer_load_dword;
3489 } else if (bytes_needed <= 8) {
3490 bytes_size = 8;
3491 op = aco_opcode::buffer_load_dwordx2;
3492 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3493 bytes_size = 12;
3494 op = aco_opcode::buffer_load_dwordx3;
3495 } else {
3496 bytes_size = 16;
3497 op = aco_opcode::buffer_load_dwordx4;
3498 }
3499 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3500 mubuf->operands[0] = Operand(info->resource);
3501 mubuf->operands[1] = vaddr;
3502 mubuf->operands[2] = soffset;
3503 mubuf->offen = (offset.type() == RegType::vgpr);
3504 mubuf->glc = info->glc;
3505 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3506 mubuf->barrier = info->barrier;
3507 mubuf->can_reorder = info->can_reorder;
3508 mubuf->offset = const_offset;
3509 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3510 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3511 mubuf->definitions[0] = Definition(val);
3512 bld.insert(std::move(mubuf));
3513
3514 if (bytes_size < 4)
3515 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3516
3517 return val;
3518 }
3519
3520 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3521
3522 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3523 {
3524 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3525 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3526
3527 if (addr.type() == RegType::vgpr)
3528 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3529 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3530 }
3531
3532 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3533 Temp offset, unsigned bytes_needed,
3534 unsigned align_, unsigned const_offset,
3535 Temp dst_hint)
3536 {
3537 unsigned bytes_size = 0;
3538 bool mubuf = bld.program->chip_class == GFX6;
3539 bool global = bld.program->chip_class >= GFX9;
3540 aco_opcode op;
3541 if (bytes_needed == 1) {
3542 bytes_size = 1;
3543 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3544 } else if (bytes_needed == 2) {
3545 bytes_size = 2;
3546 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3547 } else if (bytes_needed <= 4) {
3548 bytes_size = 4;
3549 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3550 } else if (bytes_needed <= 8) {
3551 bytes_size = 8;
3552 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3553 } else if (bytes_needed <= 12 && !mubuf) {
3554 bytes_size = 12;
3555 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3556 } else {
3557 bytes_size = 16;
3558 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3559 }
3560 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3561 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3562 if (mubuf) {
3563 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3564 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3565 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3566 mubuf->operands[2] = Operand(0u);
3567 mubuf->glc = info->glc;
3568 mubuf->dlc = false;
3569 mubuf->offset = 0;
3570 mubuf->addr64 = offset.type() == RegType::vgpr;
3571 mubuf->disable_wqm = false;
3572 mubuf->barrier = info->barrier;
3573 mubuf->definitions[0] = Definition(val);
3574 bld.insert(std::move(mubuf));
3575 } else {
3576 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3577
3578 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3579 flat->operands[0] = Operand(offset);
3580 flat->operands[1] = Operand(s1);
3581 flat->glc = info->glc;
3582 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3583 flat->barrier = info->barrier;
3584 flat->offset = 0u;
3585 flat->definitions[0] = Definition(val);
3586 bld.insert(std::move(flat));
3587 }
3588
3589 if (bytes_size < 4)
3590 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3591
3592 return val;
3593 }
3594
3595 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3596
3597 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3598 Temp address, unsigned base_offset, unsigned align)
3599 {
3600 assert(util_is_power_of_two_nonzero(align));
3601
3602 Builder bld(ctx->program, ctx->block);
3603
3604 unsigned num_components = dst.bytes() / elem_size_bytes;
3605 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3606 info.align_mul = align;
3607 info.align_offset = 0;
3608 info.barrier = barrier_shared;
3609 info.can_reorder = false;
3610 info.const_offset = base_offset;
3611 emit_lds_load(ctx, bld, &info);
3612
3613 return dst;
3614 }
3615
3616 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3617 {
3618 if (!count)
3619 return;
3620
3621 Builder bld(ctx->program, ctx->block);
3622
3623 ASSERTED bool is_subdword = false;
3624 for (unsigned i = 0; i < count; i++)
3625 is_subdword |= offsets[i] % 4;
3626 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3627 assert(!is_subdword || dst_type == RegType::vgpr);
3628
3629 /* count == 1 fast path */
3630 if (count == 1) {
3631 if (dst_type == RegType::sgpr)
3632 dst[0] = bld.as_uniform(src);
3633 else
3634 dst[0] = as_vgpr(ctx, src);
3635 return;
3636 }
3637
3638 for (unsigned i = 0; i < count - 1; i++)
3639 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3640 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3641
3642 if (is_subdword && src.type() == RegType::sgpr) {
3643 src = as_vgpr(ctx, src);
3644 } else {
3645 /* use allocated_vec if possible */
3646 auto it = ctx->allocated_vec.find(src.id());
3647 if (it != ctx->allocated_vec.end()) {
3648 unsigned total_size = 0;
3649 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3650 total_size += it->second[i].bytes();
3651 if (total_size != src.bytes())
3652 goto split;
3653
3654 unsigned elem_size = it->second[0].bytes();
3655
3656 for (unsigned i = 0; i < count; i++) {
3657 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3658 goto split;
3659 }
3660
3661 for (unsigned i = 0; i < count; i++) {
3662 unsigned start_idx = offsets[i] / elem_size;
3663 unsigned op_count = dst[i].bytes() / elem_size;
3664 if (op_count == 1) {
3665 if (dst_type == RegType::sgpr)
3666 dst[i] = bld.as_uniform(it->second[start_idx]);
3667 else
3668 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3669 continue;
3670 }
3671
3672 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3673 for (unsigned j = 0; j < op_count; j++) {
3674 Temp tmp = it->second[start_idx + j];
3675 if (dst_type == RegType::sgpr)
3676 tmp = bld.as_uniform(tmp);
3677 vec->operands[j] = Operand(tmp);
3678 }
3679 vec->definitions[0] = Definition(dst[i]);
3680 bld.insert(std::move(vec));
3681 }
3682 return;
3683 }
3684 }
3685
3686 if (dst_type == RegType::sgpr)
3687 src = bld.as_uniform(src);
3688
3689 split:
3690 /* just split it */
3691 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3692 split->operands[0] = Operand(src);
3693 for (unsigned i = 0; i < count; i++)
3694 split->definitions[i] = Definition(dst[i]);
3695 bld.insert(std::move(split));
3696 }
3697
3698 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3699 int *start, int *count)
3700 {
3701 unsigned start_elem = ffs(todo_mask) - 1;
3702 bool skip = !(mask & (1 << start_elem));
3703 if (skip)
3704 mask = ~mask & todo_mask;
3705
3706 mask &= todo_mask;
3707
3708 u_bit_scan_consecutive_range(&mask, start, count);
3709
3710 return !skip;
3711 }
3712
3713 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3714 {
3715 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3716 }
3717
3718 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3719 Temp address, unsigned base_offset, unsigned align)
3720 {
3721 assert(util_is_power_of_two_nonzero(align));
3722 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3723
3724 Builder bld(ctx->program, ctx->block);
3725 bool large_ds_write = ctx->options->chip_class >= GFX7;
3726 bool usable_write2 = ctx->options->chip_class >= GFX7;
3727
3728 unsigned write_count = 0;
3729 Temp write_datas[32];
3730 unsigned offsets[32];
3731 aco_opcode opcodes[32];
3732
3733 wrmask = widen_mask(wrmask, elem_size_bytes);
3734
3735 uint32_t todo = u_bit_consecutive(0, data.bytes());
3736 while (todo) {
3737 int offset, bytes;
3738 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3739 offsets[write_count] = offset;
3740 opcodes[write_count] = aco_opcode::num_opcodes;
3741 write_count++;
3742 advance_write_mask(&todo, offset, bytes);
3743 continue;
3744 }
3745
3746 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3747 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3748 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3749 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3750
3751 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3752 aco_opcode op = aco_opcode::num_opcodes;
3753 if (bytes >= 16 && aligned16 && large_ds_write) {
3754 op = aco_opcode::ds_write_b128;
3755 bytes = 16;
3756 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3757 op = aco_opcode::ds_write_b96;
3758 bytes = 12;
3759 } else if (bytes >= 8 && aligned8) {
3760 op = aco_opcode::ds_write_b64;
3761 bytes = 8;
3762 } else if (bytes >= 4 && aligned4) {
3763 op = aco_opcode::ds_write_b32;
3764 bytes = 4;
3765 } else if (bytes >= 2 && aligned2) {
3766 op = aco_opcode::ds_write_b16;
3767 bytes = 2;
3768 } else if (bytes >= 1) {
3769 op = aco_opcode::ds_write_b8;
3770 bytes = 1;
3771 } else {
3772 assert(false);
3773 }
3774
3775 offsets[write_count] = offset;
3776 opcodes[write_count] = op;
3777 write_count++;
3778 advance_write_mask(&todo, offset, bytes);
3779 }
3780
3781 Operand m = load_lds_size_m0(bld);
3782
3783 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3784
3785 for (unsigned i = 0; i < write_count; i++) {
3786 aco_opcode op = opcodes[i];
3787 if (op == aco_opcode::num_opcodes)
3788 continue;
3789
3790 Temp data = write_datas[i];
3791
3792 unsigned second = write_count;
3793 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3794 for (second = i + 1; second < write_count; second++) {
3795 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3796 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3797 opcodes[second] = aco_opcode::num_opcodes;
3798 break;
3799 }
3800 }
3801 }
3802
3803 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3804 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3805
3806 unsigned inline_offset = base_offset + offsets[i];
3807 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3808 Temp address_offset = address;
3809 if (inline_offset > max_offset) {
3810 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3811 inline_offset = offsets[i];
3812 }
3813 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3814
3815 if (write2) {
3816 Temp second_data = write_datas[second];
3817 inline_offset /= data.bytes();
3818 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3819 } else {
3820 bld.ds(op, address_offset, data, m, inline_offset);
3821 }
3822 }
3823 }
3824
3825 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3826 {
3827 unsigned align = 16;
3828 if (const_offset)
3829 align = std::min(align, 1u << (ffs(const_offset) - 1));
3830
3831 return align;
3832 }
3833
3834
3835 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3836 {
3837 switch (bytes) {
3838 case 1:
3839 assert(!smem);
3840 return aco_opcode::buffer_store_byte;
3841 case 2:
3842 assert(!smem);
3843 return aco_opcode::buffer_store_short;
3844 case 4:
3845 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3846 case 8:
3847 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3848 case 12:
3849 assert(!smem);
3850 return aco_opcode::buffer_store_dwordx3;
3851 case 16:
3852 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3853 }
3854 unreachable("Unexpected store size");
3855 return aco_opcode::num_opcodes;
3856 }
3857
3858 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3859 Temp data, unsigned writemask, int swizzle_element_size,
3860 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3861 {
3862 unsigned write_count_with_skips = 0;
3863 bool skips[16];
3864
3865 /* determine how to split the data */
3866 unsigned todo = u_bit_consecutive(0, data.bytes());
3867 while (todo) {
3868 int offset, bytes;
3869 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3870 offsets[write_count_with_skips] = offset;
3871 if (skips[write_count_with_skips]) {
3872 advance_write_mask(&todo, offset, bytes);
3873 write_count_with_skips++;
3874 continue;
3875 }
3876
3877 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3878 * larger than swizzle_element_size */
3879 bytes = MIN2(bytes, swizzle_element_size);
3880 if (bytes % 4)
3881 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3882
3883 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3884 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3885 bytes = 8;
3886
3887 /* dword or larger stores have to be dword-aligned */
3888 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3889 unsigned align_offset = instr ? nir_intrinsic_align_mul(instr) : 0;
3890 bool dword_aligned = (align_offset + offset) % 4 == 0 && align_mul % 4 == 0;
3891 if (bytes >= 4 && !dword_aligned)
3892 bytes = MIN2(bytes, 2);
3893
3894 advance_write_mask(&todo, offset, bytes);
3895 write_count_with_skips++;
3896 }
3897
3898 /* actually split data */
3899 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3900
3901 /* remove skips */
3902 for (unsigned i = 0; i < write_count_with_skips; i++) {
3903 if (skips[i])
3904 continue;
3905 write_datas[*write_count] = write_datas[i];
3906 offsets[*write_count] = offsets[i];
3907 (*write_count)++;
3908 }
3909 }
3910
3911 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3912 unsigned split_cnt = 0u, Temp dst = Temp())
3913 {
3914 Builder bld(ctx->program, ctx->block);
3915 unsigned dword_size = elem_size_bytes / 4;
3916
3917 if (!dst.id())
3918 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3919
3920 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3921 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3922 instr->definitions[0] = Definition(dst);
3923
3924 for (unsigned i = 0; i < cnt; ++i) {
3925 if (arr[i].id()) {
3926 assert(arr[i].size() == dword_size);
3927 allocated_vec[i] = arr[i];
3928 instr->operands[i] = Operand(arr[i]);
3929 } else {
3930 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3931 allocated_vec[i] = zero;
3932 instr->operands[i] = Operand(zero);
3933 }
3934 }
3935
3936 bld.insert(std::move(instr));
3937
3938 if (split_cnt)
3939 emit_split_vector(ctx, dst, split_cnt);
3940 else
3941 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3942
3943 return dst;
3944 }
3945
3946 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3947 {
3948 if (const_offset >= 4096) {
3949 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3950 const_offset %= 4096u;
3951
3952 if (!voffset.id())
3953 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3954 else if (unlikely(voffset.regClass() == s1))
3955 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3956 else if (likely(voffset.regClass() == v1))
3957 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3958 else
3959 unreachable("Unsupported register class of voffset");
3960 }
3961
3962 return const_offset;
3963 }
3964
3965 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3966 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3967 {
3968 assert(vdata.id());
3969 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3970 assert(vdata.size() >= 1 && vdata.size() <= 4);
3971
3972 Builder bld(ctx->program, ctx->block);
3973 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
3974 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3975
3976 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3977 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3978 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3979 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3980 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3981
3982 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3983 }
3984
3985 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3986 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3987 bool allow_combining = true, bool reorder = true, bool slc = false)
3988 {
3989 Builder bld(ctx->program, ctx->block);
3990 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3991 assert(write_mask);
3992 write_mask = widen_mask(write_mask, elem_size_bytes);
3993
3994 unsigned write_count = 0;
3995 Temp write_datas[32];
3996 unsigned offsets[32];
3997 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
3998 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
3999
4000 for (unsigned i = 0; i < write_count; i++) {
4001 unsigned const_offset = offsets[i] + base_const_offset;
4002 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc);
4003 }
4004 }
4005
4006 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4007 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4008 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4009 {
4010 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
4011 assert((num_components * elem_size_bytes / 4) == dst.size());
4012 assert(!!stride != allow_combining);
4013
4014 Builder bld(ctx->program, ctx->block);
4015
4016 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4017 info.component_stride = allow_combining ? 0 : stride;
4018 info.glc = true;
4019 info.swizzle_component_size = allow_combining ? 0 : 4;
4020 info.align_mul = MIN2(elem_size_bytes, 4);
4021 info.align_offset = 0;
4022 info.soffset = soffset;
4023 info.const_offset = base_const_offset;
4024 emit_mubuf_load(ctx, bld, &info);
4025 }
4026
4027 std::pair<Temp, unsigned> offset_add_from_nir(isel_context *ctx, const std::pair<Temp, unsigned> &base_offset, nir_src *off_src, unsigned stride = 1u)
4028 {
4029 Builder bld(ctx->program, ctx->block);
4030 Temp offset = base_offset.first;
4031 unsigned const_offset = base_offset.second;
4032
4033 if (!nir_src_is_const(*off_src)) {
4034 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4035 Temp with_stride;
4036
4037 /* Calculate indirect offset with stride */
4038 if (likely(indirect_offset_arg.regClass() == v1))
4039 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4040 else if (indirect_offset_arg.regClass() == s1)
4041 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4042 else
4043 unreachable("Unsupported register class of indirect offset");
4044
4045 /* Add to the supplied base offset */
4046 if (offset.id() == 0)
4047 offset = with_stride;
4048 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4049 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4050 else if (offset.size() == 1 && with_stride.size() == 1)
4051 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4052 else
4053 unreachable("Unsupported register class of indirect offset");
4054 } else {
4055 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4056 const_offset += const_offset_arg * stride;
4057 }
4058
4059 return std::make_pair(offset, const_offset);
4060 }
4061
4062 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4063 {
4064 Builder bld(ctx->program, ctx->block);
4065 Temp offset;
4066
4067 if (off1.first.id() && off2.first.id()) {
4068 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4069 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4070 else if (off1.first.size() == 1 && off2.first.size() == 1)
4071 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4072 else
4073 unreachable("Unsupported register class of indirect offset");
4074 } else {
4075 offset = off1.first.id() ? off1.first : off2.first;
4076 }
4077
4078 return std::make_pair(offset, off1.second + off2.second);
4079 }
4080
4081 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4082 {
4083 Builder bld(ctx->program, ctx->block);
4084 unsigned const_offset = offs.second * multiplier;
4085
4086 if (!offs.first.id())
4087 return std::make_pair(offs.first, const_offset);
4088
4089 Temp offset = unlikely(offs.first.regClass() == s1)
4090 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4091 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4092
4093 return std::make_pair(offset, const_offset);
4094 }
4095
4096 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4097 {
4098 Builder bld(ctx->program, ctx->block);
4099
4100 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4101 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4102 /* component is in bytes */
4103 const_offset += nir_intrinsic_component(instr) * component_stride;
4104
4105 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4106 nir_src *off_src = nir_get_io_offset_src(instr);
4107 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4108 }
4109
4110 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4111 {
4112 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4113 }
4114
4115 Temp get_tess_rel_patch_id(isel_context *ctx)
4116 {
4117 Builder bld(ctx->program, ctx->block);
4118
4119 switch (ctx->shader->info.stage) {
4120 case MESA_SHADER_TESS_CTRL:
4121 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4122 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4123 case MESA_SHADER_TESS_EVAL:
4124 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4125 default:
4126 unreachable("Unsupported stage in get_tess_rel_patch_id");
4127 }
4128 }
4129
4130 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4131 {
4132 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4133 Builder bld(ctx->program, ctx->block);
4134
4135 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4136 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4137
4138 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4139
4140 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4141 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4142
4143 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4144 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4145 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4146
4147 return offset_mul(ctx, offs, 4u);
4148 }
4149
4150 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4151 {
4152 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4153 Builder bld(ctx->program, ctx->block);
4154
4155 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4156 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
4157 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
4158 uint32_t output_vertex_size = num_tcs_outputs * 16;
4159 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4160 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
4161
4162 std::pair<Temp, unsigned> offs = instr
4163 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4164 : std::make_pair(Temp(), 0u);
4165
4166 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4167 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4168
4169 if (per_vertex) {
4170 assert(instr);
4171
4172 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4173 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4174
4175 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4176 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4177 } else {
4178 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4179 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4180 }
4181
4182 return offs;
4183 }
4184
4185 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4186 {
4187 Builder bld(ctx->program, ctx->block);
4188
4189 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4190 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4191
4192 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4193
4194 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4195 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4196 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4197
4198 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4199 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4200
4201 return offs;
4202 }
4203
4204 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4205 {
4206 Builder bld(ctx->program, ctx->block);
4207
4208 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
4209 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
4210 : ctx->args->options->key.tes.tcs_num_outputs;
4211
4212 unsigned output_vertex_size = num_tcs_outputs * 16;
4213 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4214 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4215 unsigned attr_stride = ctx->tcs_num_patches;
4216
4217 std::pair<Temp, unsigned> offs = instr
4218 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4219 : std::make_pair(Temp(), 0u);
4220
4221 if (const_base_offset)
4222 offs.second += const_base_offset * attr_stride;
4223
4224 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4225 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4226 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4227
4228 return offs;
4229 }
4230
4231 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4232 {
4233 if (mask == 0)
4234 return false;
4235
4236 unsigned off = nir_intrinsic_base(instr) * 4u;
4237 nir_src *off_src = nir_get_io_offset_src(instr);
4238
4239 if (!nir_src_is_const(*off_src)) {
4240 *indirect = true;
4241 return false;
4242 }
4243
4244 *indirect = false;
4245 off += nir_src_as_uint(*off_src) * 16u;
4246
4247 while (mask) {
4248 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
4249 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
4250 return true;
4251 }
4252
4253 return false;
4254 }
4255
4256 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4257 {
4258 unsigned write_mask = nir_intrinsic_write_mask(instr);
4259 unsigned component = nir_intrinsic_component(instr);
4260 unsigned idx = nir_intrinsic_base(instr) + component;
4261
4262 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4263 if (off_instr->type != nir_instr_type_load_const)
4264 return false;
4265
4266 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4267 idx += nir_src_as_uint(instr->src[1]) * 4u;
4268
4269 if (instr->src[0].ssa->bit_size == 64)
4270 write_mask = widen_mask(write_mask, 2);
4271
4272 for (unsigned i = 0; i < 8; ++i) {
4273 if (write_mask & (1 << i)) {
4274 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4275 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
4276 }
4277 idx++;
4278 }
4279
4280 return true;
4281 }
4282
4283 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4284 {
4285 /* Only TCS per-vertex inputs are supported by this function.
4286 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4287 */
4288 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4289 return false;
4290
4291 nir_src *off_src = nir_get_io_offset_src(instr);
4292 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4293 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4294 bool can_use_temps = nir_src_is_const(*off_src) &&
4295 vertex_index_instr->type == nir_instr_type_intrinsic &&
4296 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4297
4298 if (!can_use_temps)
4299 return false;
4300
4301 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4302 Temp *src = &ctx->inputs.temps[idx];
4303 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4304
4305 return true;
4306 }
4307
4308 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4309 {
4310 Builder bld(ctx->program, ctx->block);
4311
4312 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4313 /* When the TCS only reads this output directly and for the same vertices as its invocation id, it is unnecessary to store the VS output to LDS. */
4314 bool indirect_write;
4315 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4316 if (temp_only_input && !indirect_write)
4317 return;
4318 }
4319
4320 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4321 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4322 unsigned write_mask = nir_intrinsic_write_mask(instr);
4323 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4324
4325 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4326 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4327 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4328 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4329 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4330 } else {
4331 Temp lds_base;
4332
4333 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4334 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4335 unsigned itemsize = ctx->stage == vertex_geometry_gs
4336 ? ctx->program->info->vs.es_info.esgs_itemsize
4337 : ctx->program->info->tes.es_info.esgs_itemsize;
4338 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4339 Temp wave_idx = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), get_arg(ctx, ctx->args->merged_wave_info), Operand(4u << 16 | 24));
4340 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4341 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4342 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4343 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4344 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4345 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4346 */
4347 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
4348 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4349 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
4350 } else {
4351 unreachable("Invalid LS or ES stage");
4352 }
4353
4354 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4355 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4356 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4357 }
4358 }
4359
4360 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4361 {
4362 if (per_vertex)
4363 return false;
4364
4365 unsigned off = nir_intrinsic_base(instr) * 4u;
4366 return off == ctx->tcs_tess_lvl_out_loc ||
4367 off == ctx->tcs_tess_lvl_in_loc;
4368
4369 }
4370
4371 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4372 {
4373 uint64_t mask = per_vertex
4374 ? ctx->program->info->tcs.tes_inputs_read
4375 : ctx->program->info->tcs.tes_patch_inputs_read;
4376
4377 bool indirect_write = false;
4378 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4379 return indirect_write || output_read_by_tes;
4380 }
4381
4382 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4383 {
4384 uint64_t mask = per_vertex
4385 ? ctx->shader->info.outputs_read
4386 : ctx->shader->info.patch_outputs_read;
4387
4388 bool indirect_write = false;
4389 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4390 return indirect_write || output_read;
4391 }
4392
4393 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4394 {
4395 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4396 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4397
4398 Builder bld(ctx->program, ctx->block);
4399
4400 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4401 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4402 unsigned write_mask = nir_intrinsic_write_mask(instr);
4403
4404 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4405 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4406 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4407
4408 if (write_to_vmem) {
4409 std::pair<Temp, unsigned> vmem_offs = per_vertex
4410 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4411 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4412
4413 Temp hs_ring_tess_offchip = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4414 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4415 store_vmem_mubuf(ctx, store_val, hs_ring_tess_offchip, vmem_offs.first, oc_lds, vmem_offs.second, elem_size_bytes, write_mask, true, false);
4416 }
4417
4418 if (write_to_lds) {
4419 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4420 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4421 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4422 }
4423 }
4424
4425 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4426 {
4427 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4428 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4429
4430 Builder bld(ctx->program, ctx->block);
4431
4432 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4433 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4434 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4435 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4436
4437 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4438 }
4439
4440 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4441 {
4442 if (ctx->stage == vertex_vs ||
4443 ctx->stage == tess_eval_vs ||
4444 ctx->stage == fragment_fs ||
4445 ctx->stage == ngg_vertex_gs ||
4446 ctx->stage == ngg_tess_eval_gs ||
4447 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4448 bool stored_to_temps = store_output_to_temps(ctx, instr);
4449 if (!stored_to_temps) {
4450 fprintf(stderr, "Unimplemented output offset instruction:\n");
4451 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4452 fprintf(stderr, "\n");
4453 abort();
4454 }
4455 } else if (ctx->stage == vertex_es ||
4456 ctx->stage == vertex_ls ||
4457 ctx->stage == tess_eval_es ||
4458 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4459 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4460 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4461 visit_store_ls_or_es_output(ctx, instr);
4462 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4463 visit_store_tcs_output(ctx, instr, false);
4464 } else {
4465 unreachable("Shader stage not implemented");
4466 }
4467 }
4468
4469 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4470 {
4471 visit_load_tcs_output(ctx, instr, false);
4472 }
4473
4474 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4475 {
4476 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4477 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4478
4479 Builder bld(ctx->program, ctx->block);
4480 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
4481 if (ctx->program->has_16bank_lds)
4482 interp_p1.instr->operands[0].setLateKill(true);
4483 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
4484 }
4485
4486 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4487 {
4488 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4489 for (unsigned i = 0; i < num_components; i++)
4490 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4491 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4492 assert(num_components == 4);
4493 Builder bld(ctx->program, ctx->block);
4494 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4495 }
4496
4497 for (Operand& op : vec->operands)
4498 op = op.isUndefined() ? Operand(0u) : op;
4499
4500 vec->definitions[0] = Definition(dst);
4501 ctx->block->instructions.emplace_back(std::move(vec));
4502 emit_split_vector(ctx, dst, num_components);
4503 return;
4504 }
4505
4506 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4507 {
4508 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4509 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4510 unsigned idx = nir_intrinsic_base(instr);
4511 unsigned component = nir_intrinsic_component(instr);
4512 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4513
4514 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4515 if (offset) {
4516 assert(offset->u32 == 0);
4517 } else {
4518 /* the lower 15bit of the prim_mask contain the offset into LDS
4519 * while the upper bits contain the number of prims */
4520 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4521 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4522 Builder bld(ctx->program, ctx->block);
4523 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4524 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4525 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4526 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4527 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4528 }
4529
4530 if (instr->dest.ssa.num_components == 1) {
4531 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4532 } else {
4533 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4534 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4535 {
4536 Temp tmp = {ctx->program->allocateId(), v1};
4537 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4538 vec->operands[i] = Operand(tmp);
4539 }
4540 vec->definitions[0] = Definition(dst);
4541 ctx->block->instructions.emplace_back(std::move(vec));
4542 }
4543 }
4544
4545 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4546 unsigned offset, unsigned stride, unsigned channels)
4547 {
4548 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4549 if (vtx_info->chan_byte_size != 4 && channels == 3)
4550 return false;
4551 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4552 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4553 }
4554
4555 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4556 unsigned offset, unsigned stride, unsigned *channels)
4557 {
4558 if (!vtx_info->chan_byte_size) {
4559 *channels = vtx_info->num_channels;
4560 return vtx_info->chan_format;
4561 }
4562
4563 unsigned num_channels = *channels;
4564 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4565 unsigned new_channels = num_channels + 1;
4566 /* first, assume more loads is worse and try using a larger data format */
4567 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4568 new_channels++;
4569 /* don't make the attribute potentially out-of-bounds */
4570 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4571 new_channels = 5;
4572 }
4573
4574 if (new_channels == 5) {
4575 /* then try decreasing load size (at the cost of more loads) */
4576 new_channels = *channels;
4577 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4578 new_channels--;
4579 }
4580
4581 if (new_channels < *channels)
4582 *channels = new_channels;
4583 num_channels = new_channels;
4584 }
4585
4586 switch (vtx_info->chan_format) {
4587 case V_008F0C_BUF_DATA_FORMAT_8:
4588 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4589 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4590 case V_008F0C_BUF_DATA_FORMAT_16:
4591 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4592 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4593 case V_008F0C_BUF_DATA_FORMAT_32:
4594 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4595 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4596 }
4597 unreachable("shouldn't reach here");
4598 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4599 }
4600
4601 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4602 * so we may need to fix it up. */
4603 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4604 {
4605 Builder bld(ctx->program, ctx->block);
4606
4607 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4608 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4609
4610 /* For the integer-like cases, do a natural sign extension.
4611 *
4612 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4613 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4614 * exponent.
4615 */
4616 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4617 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4618
4619 /* Convert back to the right type. */
4620 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4621 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4622 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4623 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4624 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4625 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4626 }
4627
4628 return alpha;
4629 }
4630
4631 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4632 {
4633 Builder bld(ctx->program, ctx->block);
4634 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4635 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4636
4637 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4638 if (off_instr->type != nir_instr_type_load_const) {
4639 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4640 nir_print_instr(off_instr, stderr);
4641 fprintf(stderr, "\n");
4642 }
4643 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4644
4645 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4646
4647 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4648 unsigned component = nir_intrinsic_component(instr);
4649 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4650 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4651 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4652 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4653
4654 unsigned dfmt = attrib_format & 0xf;
4655 unsigned nfmt = (attrib_format >> 4) & 0x7;
4656 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4657
4658 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4659 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4660 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4661 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4662 if (post_shuffle)
4663 num_channels = MAX2(num_channels, 3);
4664
4665 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4666 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4667
4668 Temp index;
4669 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4670 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4671 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4672 if (divisor) {
4673 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4674 if (divisor != 1) {
4675 Temp divided = bld.tmp(v1);
4676 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4677 index = bld.vadd32(bld.def(v1), start_instance, divided);
4678 } else {
4679 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4680 }
4681 } else {
4682 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4683 }
4684 } else {
4685 index = bld.vadd32(bld.def(v1),
4686 get_arg(ctx, ctx->args->ac.base_vertex),
4687 get_arg(ctx, ctx->args->ac.vertex_id));
4688 }
4689
4690 Temp channels[num_channels];
4691 unsigned channel_start = 0;
4692 bool direct_fetch = false;
4693
4694 /* skip unused channels at the start */
4695 if (vtx_info->chan_byte_size && !post_shuffle) {
4696 channel_start = ffs(mask) - 1;
4697 for (unsigned i = 0; i < channel_start; i++)
4698 channels[i] = Temp(0, s1);
4699 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4700 num_channels = 3 - (ffs(mask) - 1);
4701 }
4702
4703 /* load channels */
4704 while (channel_start < num_channels) {
4705 unsigned fetch_size = num_channels - channel_start;
4706 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4707 bool expanded = false;
4708
4709 /* use MUBUF when possible to avoid possible alignment issues */
4710 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4711 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4712 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4713 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4714 vtx_info->chan_byte_size == 4;
4715 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4716 if (!use_mubuf) {
4717 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4718 } else {
4719 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4720 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4721 fetch_size = 4;
4722 expanded = true;
4723 }
4724 }
4725
4726 Temp fetch_index = index;
4727 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4728 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4729 fetch_offset = fetch_offset % attrib_stride;
4730 }
4731
4732 Operand soffset(0u);
4733 if (fetch_offset >= 4096) {
4734 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4735 fetch_offset %= 4096;
4736 }
4737
4738 aco_opcode opcode;
4739 switch (fetch_size) {
4740 case 1:
4741 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4742 break;
4743 case 2:
4744 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4745 break;
4746 case 3:
4747 assert(ctx->options->chip_class >= GFX7 ||
4748 (!use_mubuf && ctx->options->chip_class == GFX6));
4749 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4750 break;
4751 case 4:
4752 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4753 break;
4754 default:
4755 unreachable("Unimplemented load_input vector size");
4756 }
4757
4758 Temp fetch_dst;
4759 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4760 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4761 num_channels <= 3)) {
4762 direct_fetch = true;
4763 fetch_dst = dst;
4764 } else {
4765 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4766 }
4767
4768 if (use_mubuf) {
4769 Instruction *mubuf = bld.mubuf(opcode,
4770 Definition(fetch_dst), list, fetch_index, soffset,
4771 fetch_offset, false, true).instr;
4772 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4773 } else {
4774 Instruction *mtbuf = bld.mtbuf(opcode,
4775 Definition(fetch_dst), list, fetch_index, soffset,
4776 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4777 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4778 }
4779
4780 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4781
4782 if (fetch_size == 1) {
4783 channels[channel_start] = fetch_dst;
4784 } else {
4785 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4786 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4787 }
4788
4789 channel_start += fetch_size;
4790 }
4791
4792 if (!direct_fetch) {
4793 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4794 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4795
4796 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4797 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4798 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4799
4800 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4801 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4802 unsigned num_temp = 0;
4803 for (unsigned i = 0; i < dst.size(); i++) {
4804 unsigned idx = i + component;
4805 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4806 Temp channel = channels[swizzle[idx]];
4807 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4808 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4809 vec->operands[i] = Operand(channel);
4810
4811 num_temp++;
4812 elems[i] = channel;
4813 } else if (is_float && idx == 3) {
4814 vec->operands[i] = Operand(0x3f800000u);
4815 } else if (!is_float && idx == 3) {
4816 vec->operands[i] = Operand(1u);
4817 } else {
4818 vec->operands[i] = Operand(0u);
4819 }
4820 }
4821 vec->definitions[0] = Definition(dst);
4822 ctx->block->instructions.emplace_back(std::move(vec));
4823 emit_split_vector(ctx, dst, dst.size());
4824
4825 if (num_temp == dst.size())
4826 ctx->allocated_vec.emplace(dst.id(), elems);
4827 }
4828 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4829 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4830 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4831 if (off_instr->type != nir_instr_type_load_const ||
4832 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4833 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4834 nir_print_instr(off_instr, stderr);
4835 fprintf(stderr, "\n");
4836 }
4837
4838 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4839 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4840 if (offset) {
4841 assert(offset->u32 == 0);
4842 } else {
4843 /* the lower 15bit of the prim_mask contain the offset into LDS
4844 * while the upper bits contain the number of prims */
4845 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4846 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4847 Builder bld(ctx->program, ctx->block);
4848 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4849 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4850 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4851 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4852 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4853 }
4854
4855 unsigned idx = nir_intrinsic_base(instr);
4856 unsigned component = nir_intrinsic_component(instr);
4857 unsigned vertex_id = 2; /* P0 */
4858
4859 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4860 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4861 switch (src0->u32) {
4862 case 0:
4863 vertex_id = 2; /* P0 */
4864 break;
4865 case 1:
4866 vertex_id = 0; /* P10 */
4867 break;
4868 case 2:
4869 vertex_id = 1; /* P20 */
4870 break;
4871 default:
4872 unreachable("invalid vertex index");
4873 }
4874 }
4875
4876 if (dst.size() == 1) {
4877 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4878 } else {
4879 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4880 for (unsigned i = 0; i < dst.size(); i++)
4881 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4882 vec->definitions[0] = Definition(dst);
4883 bld.insert(std::move(vec));
4884 }
4885
4886 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4887 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4888 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4889 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4890 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4891
4892 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4893 } else {
4894 unreachable("Shader stage not implemented");
4895 }
4896 }
4897
4898 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4899 {
4900 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4901
4902 Builder bld(ctx->program, ctx->block);
4903 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4904 Temp vertex_offset;
4905
4906 if (!nir_src_is_const(*vertex_src)) {
4907 /* better code could be created, but this case probably doesn't happen
4908 * much in practice */
4909 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4910 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4911 Temp elem;
4912
4913 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4914 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4915 if (i % 2u)
4916 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4917 } else {
4918 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4919 }
4920
4921 if (vertex_offset.id()) {
4922 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4923 Operand(i), indirect_vertex);
4924 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4925 } else {
4926 vertex_offset = elem;
4927 }
4928 }
4929
4930 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4931 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4932 } else {
4933 unsigned vertex = nir_src_as_uint(*vertex_src);
4934 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4935 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4936 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4937 Operand((vertex % 2u) * 16u), Operand(16u));
4938 else
4939 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4940 }
4941
4942 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4943 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4944 return offset_mul(ctx, offs, 4u);
4945 }
4946
4947 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4948 {
4949 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4950
4951 Builder bld(ctx->program, ctx->block);
4952 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4953 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4954
4955 if (ctx->stage == geometry_gs) {
4956 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4957 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4958 load_vmem_mubuf(ctx, dst, ring, offs.first, Temp(), offs.second, elem_size_bytes, instr->dest.ssa.num_components, 4u * ctx->program->wave_size, false, true);
4959 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4960 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4961 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4962 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4963 } else {
4964 unreachable("Unsupported GS stage.");
4965 }
4966 }
4967
4968 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4969 {
4970 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4971
4972 Builder bld(ctx->program, ctx->block);
4973 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4974
4975 if (load_input_from_temps(ctx, instr, dst))
4976 return;
4977
4978 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4979 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4980 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4981
4982 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4983 }
4984
4985 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4986 {
4987 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4988
4989 Builder bld(ctx->program, ctx->block);
4990
4991 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4992 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4993 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4994
4995 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4996 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4997
4998 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4999 }
5000
5001 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5002 {
5003 switch (ctx->shader->info.stage) {
5004 case MESA_SHADER_GEOMETRY:
5005 visit_load_gs_per_vertex_input(ctx, instr);
5006 break;
5007 case MESA_SHADER_TESS_CTRL:
5008 visit_load_tcs_per_vertex_input(ctx, instr);
5009 break;
5010 case MESA_SHADER_TESS_EVAL:
5011 visit_load_tes_per_vertex_input(ctx, instr);
5012 break;
5013 default:
5014 unreachable("Unimplemented shader stage");
5015 }
5016 }
5017
5018 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5019 {
5020 visit_load_tcs_output(ctx, instr, true);
5021 }
5022
5023 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5024 {
5025 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5026 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5027
5028 visit_store_tcs_output(ctx, instr, true);
5029 }
5030
5031 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5032 {
5033 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5034
5035 Builder bld(ctx->program, ctx->block);
5036 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5037
5038 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5039 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5040 Operand tes_w(0u);
5041
5042 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5043 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5044 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5045 tes_w = Operand(tmp);
5046 }
5047
5048 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5049 emit_split_vector(ctx, tess_coord, 3);
5050 }
5051
5052 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5053 {
5054 if (ctx->program->info->need_indirect_descriptor_sets) {
5055 Builder bld(ctx->program, ctx->block);
5056 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5057 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5058 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5059 }
5060
5061 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5062 }
5063
5064
5065 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5066 {
5067 Builder bld(ctx->program, ctx->block);
5068 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5069 if (!ctx->divergent_vals[instr->dest.ssa.index])
5070 index = bld.as_uniform(index);
5071 unsigned desc_set = nir_intrinsic_desc_set(instr);
5072 unsigned binding = nir_intrinsic_binding(instr);
5073
5074 Temp desc_ptr;
5075 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5076 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5077 unsigned offset = layout->binding[binding].offset;
5078 unsigned stride;
5079 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5080 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5081 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5082 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5083 offset = pipeline_layout->push_constant_size + 16 * idx;
5084 stride = 16;
5085 } else {
5086 desc_ptr = load_desc_ptr(ctx, desc_set);
5087 stride = layout->binding[binding].size;
5088 }
5089
5090 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5091 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5092 if (stride != 1) {
5093 if (nir_const_index) {
5094 const_index = const_index * stride;
5095 } else if (index.type() == RegType::vgpr) {
5096 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5097 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5098 } else {
5099 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5100 }
5101 }
5102 if (offset) {
5103 if (nir_const_index) {
5104 const_index = const_index + offset;
5105 } else if (index.type() == RegType::vgpr) {
5106 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5107 } else {
5108 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5109 }
5110 }
5111
5112 if (nir_const_index && const_index == 0) {
5113 index = desc_ptr;
5114 } else if (index.type() == RegType::vgpr) {
5115 index = bld.vadd32(bld.def(v1),
5116 nir_const_index ? Operand(const_index) : Operand(index),
5117 Operand(desc_ptr));
5118 } else {
5119 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5120 nir_const_index ? Operand(const_index) : Operand(index),
5121 Operand(desc_ptr));
5122 }
5123
5124 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5125 }
5126
5127 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5128 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5129 bool glc=false, bool readonly=true)
5130 {
5131 Builder bld(ctx->program, ctx->block);
5132
5133 bool use_smem = dst.type() != RegType::vgpr && ((ctx->options->chip_class >= GFX8 && component_size >= 4) || readonly);
5134 if (use_smem)
5135 offset = bld.as_uniform(offset);
5136
5137 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5138 info.glc = glc;
5139 info.barrier = readonly ? barrier_none : barrier_buffer;
5140 info.can_reorder = readonly;
5141 info.align_mul = align_mul;
5142 info.align_offset = align_offset;
5143 if (use_smem)
5144 emit_smem_load(ctx, bld, &info);
5145 else
5146 emit_mubuf_load(ctx, bld, &info);
5147 }
5148
5149 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5150 {
5151 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5152 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5153
5154 Builder bld(ctx->program, ctx->block);
5155
5156 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5157 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5158 unsigned binding = nir_intrinsic_binding(idx_instr);
5159 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5160
5161 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5162 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5163 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5164 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5165 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5166 if (ctx->options->chip_class >= GFX10) {
5167 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5168 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5169 S_008F0C_RESOURCE_LEVEL(1);
5170 } else {
5171 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5172 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5173 }
5174 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5175 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5176 Operand(0xFFFFFFFFu),
5177 Operand(desc_type));
5178 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5179 rsrc, upper_dwords);
5180 } else {
5181 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5182 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5183 }
5184 unsigned size = instr->dest.ssa.bit_size / 8;
5185 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5186 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5187 }
5188
5189 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5190 {
5191 Builder bld(ctx->program, ctx->block);
5192 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5193 unsigned offset = nir_intrinsic_base(instr);
5194 unsigned count = instr->dest.ssa.num_components;
5195 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5196
5197 if (index_cv && instr->dest.ssa.bit_size == 32) {
5198 unsigned start = (offset + index_cv->u32) / 4u;
5199 start -= ctx->args->ac.base_inline_push_consts;
5200 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5201 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5202 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5203 for (unsigned i = 0; i < count; ++i) {
5204 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5205 vec->operands[i] = Operand{elems[i]};
5206 }
5207 vec->definitions[0] = Definition(dst);
5208 ctx->block->instructions.emplace_back(std::move(vec));
5209 ctx->allocated_vec.emplace(dst.id(), elems);
5210 return;
5211 }
5212 }
5213
5214 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5215 if (offset != 0) // TODO check if index != 0 as well
5216 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5217 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5218 Temp vec = dst;
5219 bool trim = false;
5220 bool aligned = true;
5221
5222 if (instr->dest.ssa.bit_size == 8) {
5223 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5224 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5225 if (!aligned)
5226 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5227 } else if (instr->dest.ssa.bit_size == 16) {
5228 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5229 if (!aligned)
5230 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5231 }
5232
5233 aco_opcode op;
5234
5235 switch (vec.size()) {
5236 case 1:
5237 op = aco_opcode::s_load_dword;
5238 break;
5239 case 2:
5240 op = aco_opcode::s_load_dwordx2;
5241 break;
5242 case 3:
5243 vec = bld.tmp(s4);
5244 trim = true;
5245 case 4:
5246 op = aco_opcode::s_load_dwordx4;
5247 break;
5248 case 6:
5249 vec = bld.tmp(s8);
5250 trim = true;
5251 case 8:
5252 op = aco_opcode::s_load_dwordx8;
5253 break;
5254 default:
5255 unreachable("unimplemented or forbidden load_push_constant.");
5256 }
5257
5258 bld.smem(op, Definition(vec), ptr, index);
5259
5260 if (!aligned) {
5261 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5262 byte_align_scalar(ctx, vec, byte_offset, dst);
5263 return;
5264 }
5265
5266 if (trim) {
5267 emit_split_vector(ctx, vec, 4);
5268 RegClass rc = dst.size() == 3 ? s1 : s2;
5269 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5270 emit_extract_vector(ctx, vec, 0, rc),
5271 emit_extract_vector(ctx, vec, 1, rc),
5272 emit_extract_vector(ctx, vec, 2, rc));
5273
5274 }
5275 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5276 }
5277
5278 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5279 {
5280 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5281
5282 Builder bld(ctx->program, ctx->block);
5283
5284 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5285 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5286 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5287 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5288 if (ctx->options->chip_class >= GFX10) {
5289 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5290 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5291 S_008F0C_RESOURCE_LEVEL(1);
5292 } else {
5293 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5294 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5295 }
5296
5297 unsigned base = nir_intrinsic_base(instr);
5298 unsigned range = nir_intrinsic_range(instr);
5299
5300 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5301 if (base && offset.type() == RegType::sgpr)
5302 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5303 else if (base && offset.type() == RegType::vgpr)
5304 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5305
5306 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5307 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5308 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5309 Operand(desc_type));
5310 unsigned size = instr->dest.ssa.bit_size / 8;
5311 // TODO: get alignment information for subdword constants
5312 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5313 }
5314
5315 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5316 {
5317 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5318 ctx->cf_info.exec_potentially_empty_discard = true;
5319
5320 ctx->program->needs_exact = true;
5321
5322 // TODO: optimize uniform conditions
5323 Builder bld(ctx->program, ctx->block);
5324 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5325 assert(src.regClass() == bld.lm);
5326 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5327 bld.pseudo(aco_opcode::p_discard_if, src);
5328 ctx->block->kind |= block_kind_uses_discard_if;
5329 return;
5330 }
5331
5332 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5333 {
5334 Builder bld(ctx->program, ctx->block);
5335
5336 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5337 ctx->cf_info.exec_potentially_empty_discard = true;
5338
5339 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5340 ctx->cf_info.parent_loop.has_divergent_continue;
5341
5342 if (ctx->block->loop_nest_depth &&
5343 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5344 /* we handle discards the same way as jump instructions */
5345 append_logical_end(ctx->block);
5346
5347 /* in loops, discard behaves like break */
5348 Block *linear_target = ctx->cf_info.parent_loop.exit;
5349 ctx->block->kind |= block_kind_discard;
5350
5351 if (!divergent) {
5352 /* uniform discard - loop ends here */
5353 assert(nir_instr_is_last(&instr->instr));
5354 ctx->block->kind |= block_kind_uniform;
5355 ctx->cf_info.has_branch = true;
5356 bld.branch(aco_opcode::p_branch);
5357 add_linear_edge(ctx->block->index, linear_target);
5358 return;
5359 }
5360
5361 /* we add a break right behind the discard() instructions */
5362 ctx->block->kind |= block_kind_break;
5363 unsigned idx = ctx->block->index;
5364
5365 ctx->cf_info.parent_loop.has_divergent_branch = true;
5366 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5367
5368 /* remove critical edges from linear CFG */
5369 bld.branch(aco_opcode::p_branch);
5370 Block* break_block = ctx->program->create_and_insert_block();
5371 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5372 break_block->kind |= block_kind_uniform;
5373 add_linear_edge(idx, break_block);
5374 add_linear_edge(break_block->index, linear_target);
5375 bld.reset(break_block);
5376 bld.branch(aco_opcode::p_branch);
5377
5378 Block* continue_block = ctx->program->create_and_insert_block();
5379 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5380 add_linear_edge(idx, continue_block);
5381 append_logical_start(continue_block);
5382 ctx->block = continue_block;
5383
5384 return;
5385 }
5386
5387 /* it can currently happen that NIR doesn't remove the unreachable code */
5388 if (!nir_instr_is_last(&instr->instr)) {
5389 ctx->program->needs_exact = true;
5390 /* save exec somewhere temporarily so that it doesn't get
5391 * overwritten before the discard from outer exec masks */
5392 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5393 bld.pseudo(aco_opcode::p_discard_if, cond);
5394 ctx->block->kind |= block_kind_uses_discard_if;
5395 return;
5396 }
5397
5398 /* This condition is incorrect for uniformly branched discards in a loop
5399 * predicated by a divergent condition, but the above code catches that case
5400 * and the discard would end up turning into a discard_if.
5401 * For example:
5402 * if (divergent) {
5403 * while (...) {
5404 * if (uniform) {
5405 * discard;
5406 * }
5407 * }
5408 * }
5409 */
5410 if (!ctx->cf_info.parent_if.is_divergent) {
5411 /* program just ends here */
5412 ctx->block->kind |= block_kind_uniform;
5413 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5414 0 /* enabled mask */, 9 /* dest */,
5415 false /* compressed */, true/* done */, true /* valid mask */);
5416 bld.sopp(aco_opcode::s_endpgm);
5417 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5418 } else {
5419 ctx->block->kind |= block_kind_discard;
5420 /* branch and linear edge is added by visit_if() */
5421 }
5422 }
5423
5424 enum aco_descriptor_type {
5425 ACO_DESC_IMAGE,
5426 ACO_DESC_FMASK,
5427 ACO_DESC_SAMPLER,
5428 ACO_DESC_BUFFER,
5429 ACO_DESC_PLANE_0,
5430 ACO_DESC_PLANE_1,
5431 ACO_DESC_PLANE_2,
5432 };
5433
5434 static bool
5435 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5436 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5437 return false;
5438 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5439 return dim == ac_image_cube ||
5440 dim == ac_image_1darray ||
5441 dim == ac_image_2darray ||
5442 dim == ac_image_2darraymsaa;
5443 }
5444
5445 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5446 enum aco_descriptor_type desc_type,
5447 const nir_tex_instr *tex_instr, bool image, bool write)
5448 {
5449 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5450 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5451 if (it != ctx->tex_desc.end())
5452 return it->second;
5453 */
5454 Temp index = Temp();
5455 bool index_set = false;
5456 unsigned constant_index = 0;
5457 unsigned descriptor_set;
5458 unsigned base_index;
5459 Builder bld(ctx->program, ctx->block);
5460
5461 if (!deref_instr) {
5462 assert(tex_instr && !image);
5463 descriptor_set = 0;
5464 base_index = tex_instr->sampler_index;
5465 } else {
5466 while(deref_instr->deref_type != nir_deref_type_var) {
5467 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5468 if (!array_size)
5469 array_size = 1;
5470
5471 assert(deref_instr->deref_type == nir_deref_type_array);
5472 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5473 if (const_value) {
5474 constant_index += array_size * const_value->u32;
5475 } else {
5476 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5477 if (indirect.type() == RegType::vgpr)
5478 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5479
5480 if (array_size != 1)
5481 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5482
5483 if (!index_set) {
5484 index = indirect;
5485 index_set = true;
5486 } else {
5487 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5488 }
5489 }
5490
5491 deref_instr = nir_src_as_deref(deref_instr->parent);
5492 }
5493 descriptor_set = deref_instr->var->data.descriptor_set;
5494 base_index = deref_instr->var->data.binding;
5495 }
5496
5497 Temp list = load_desc_ptr(ctx, descriptor_set);
5498 list = convert_pointer_to_64_bit(ctx, list);
5499
5500 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5501 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5502 unsigned offset = binding->offset;
5503 unsigned stride = binding->size;
5504 aco_opcode opcode;
5505 RegClass type;
5506
5507 assert(base_index < layout->binding_count);
5508
5509 switch (desc_type) {
5510 case ACO_DESC_IMAGE:
5511 type = s8;
5512 opcode = aco_opcode::s_load_dwordx8;
5513 break;
5514 case ACO_DESC_FMASK:
5515 type = s8;
5516 opcode = aco_opcode::s_load_dwordx8;
5517 offset += 32;
5518 break;
5519 case ACO_DESC_SAMPLER:
5520 type = s4;
5521 opcode = aco_opcode::s_load_dwordx4;
5522 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5523 offset += radv_combined_image_descriptor_sampler_offset(binding);
5524 break;
5525 case ACO_DESC_BUFFER:
5526 type = s4;
5527 opcode = aco_opcode::s_load_dwordx4;
5528 break;
5529 case ACO_DESC_PLANE_0:
5530 case ACO_DESC_PLANE_1:
5531 type = s8;
5532 opcode = aco_opcode::s_load_dwordx8;
5533 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5534 break;
5535 case ACO_DESC_PLANE_2:
5536 type = s4;
5537 opcode = aco_opcode::s_load_dwordx4;
5538 offset += 64;
5539 break;
5540 default:
5541 unreachable("invalid desc_type\n");
5542 }
5543
5544 offset += constant_index * stride;
5545
5546 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5547 (!index_set || binding->immutable_samplers_equal)) {
5548 if (binding->immutable_samplers_equal)
5549 constant_index = 0;
5550
5551 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5552 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5553 Operand(samplers[constant_index * 4 + 0]),
5554 Operand(samplers[constant_index * 4 + 1]),
5555 Operand(samplers[constant_index * 4 + 2]),
5556 Operand(samplers[constant_index * 4 + 3]));
5557 }
5558
5559 Operand off;
5560 if (!index_set) {
5561 off = bld.copy(bld.def(s1), Operand(offset));
5562 } else {
5563 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5564 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5565 }
5566
5567 Temp res = bld.smem(opcode, bld.def(type), list, off);
5568
5569 if (desc_type == ACO_DESC_PLANE_2) {
5570 Temp components[8];
5571 for (unsigned i = 0; i < 8; i++)
5572 components[i] = bld.tmp(s1);
5573 bld.pseudo(aco_opcode::p_split_vector,
5574 Definition(components[0]),
5575 Definition(components[1]),
5576 Definition(components[2]),
5577 Definition(components[3]),
5578 res);
5579
5580 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5581 bld.pseudo(aco_opcode::p_split_vector,
5582 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5583 Definition(components[4]),
5584 Definition(components[5]),
5585 Definition(components[6]),
5586 Definition(components[7]),
5587 desc2);
5588
5589 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5590 components[0], components[1], components[2], components[3],
5591 components[4], components[5], components[6], components[7]);
5592 }
5593
5594 return res;
5595 }
5596
5597 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5598 {
5599 switch (dim) {
5600 case GLSL_SAMPLER_DIM_BUF:
5601 return 1;
5602 case GLSL_SAMPLER_DIM_1D:
5603 return array ? 2 : 1;
5604 case GLSL_SAMPLER_DIM_2D:
5605 return array ? 3 : 2;
5606 case GLSL_SAMPLER_DIM_MS:
5607 return array ? 4 : 3;
5608 case GLSL_SAMPLER_DIM_3D:
5609 case GLSL_SAMPLER_DIM_CUBE:
5610 return 3;
5611 case GLSL_SAMPLER_DIM_RECT:
5612 case GLSL_SAMPLER_DIM_SUBPASS:
5613 return 2;
5614 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5615 return 3;
5616 default:
5617 break;
5618 }
5619 return 0;
5620 }
5621
5622
5623 /* Adjust the sample index according to FMASK.
5624 *
5625 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5626 * which is the identity mapping. Each nibble says which physical sample
5627 * should be fetched to get that sample.
5628 *
5629 * For example, 0x11111100 means there are only 2 samples stored and
5630 * the second sample covers 3/4 of the pixel. When reading samples 0
5631 * and 1, return physical sample 0 (determined by the first two 0s
5632 * in FMASK), otherwise return physical sample 1.
5633 *
5634 * The sample index should be adjusted as follows:
5635 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5636 */
5637 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5638 {
5639 Builder bld(ctx->program, ctx->block);
5640 Temp fmask = bld.tmp(v1);
5641 unsigned dim = ctx->options->chip_class >= GFX10
5642 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5643 : 0;
5644
5645 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5646 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5647 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5648 load->operands[0] = Operand(fmask_desc_ptr);
5649 load->operands[1] = Operand(s4); /* no sampler */
5650 load->operands[2] = Operand(coord);
5651 load->definitions[0] = Definition(fmask);
5652 load->glc = false;
5653 load->dlc = false;
5654 load->dmask = 0x1;
5655 load->unrm = true;
5656 load->da = da;
5657 load->dim = dim;
5658 load->can_reorder = true; /* fmask images shouldn't be modified */
5659 ctx->block->instructions.emplace_back(std::move(load));
5660
5661 Operand sample_index4;
5662 if (sample_index.isConstant()) {
5663 if (sample_index.constantValue() < 16) {
5664 sample_index4 = Operand(sample_index.constantValue() << 2);
5665 } else {
5666 sample_index4 = Operand(0u);
5667 }
5668 } else if (sample_index.regClass() == s1) {
5669 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5670 } else {
5671 assert(sample_index.regClass() == v1);
5672 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5673 }
5674
5675 Temp final_sample;
5676 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5677 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5678 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5679 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5680 else
5681 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5682
5683 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5684 * resource descriptor is 0 (invalid),
5685 */
5686 Temp compare = bld.tmp(bld.lm);
5687 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5688 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5689
5690 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5691
5692 /* Replace the MSAA sample index. */
5693 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5694 }
5695
5696 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5697 {
5698
5699 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5700 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5701 bool is_array = glsl_sampler_type_is_array(type);
5702 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5703 assert(!add_frag_pos && "Input attachments should be lowered.");
5704 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5705 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5706 int count = image_type_to_components_count(dim, is_array);
5707 std::vector<Temp> coords(count);
5708 Builder bld(ctx->program, ctx->block);
5709
5710 if (is_ms) {
5711 count--;
5712 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5713 /* get sample index */
5714 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5715 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5716 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5717 std::vector<Temp> fmask_load_address;
5718 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5719 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5720
5721 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5722 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5723 } else {
5724 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5725 }
5726 }
5727
5728 if (gfx9_1d) {
5729 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5730 coords.resize(coords.size() + 1);
5731 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5732 if (is_array)
5733 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5734 } else {
5735 for (int i = 0; i < count; i++)
5736 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5737 }
5738
5739 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5740 instr->intrinsic == nir_intrinsic_image_deref_store) {
5741 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5742 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5743
5744 if (!level_zero)
5745 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5746 }
5747
5748 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5749 for (unsigned i = 0; i < coords.size(); i++)
5750 vec->operands[i] = Operand(coords[i]);
5751 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5752 vec->definitions[0] = Definition(res);
5753 ctx->block->instructions.emplace_back(std::move(vec));
5754 return res;
5755 }
5756
5757
5758 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5759 {
5760 Builder bld(ctx->program, ctx->block);
5761 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5762 const struct glsl_type *type = glsl_without_array(var->type);
5763 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5764 bool is_array = glsl_sampler_type_is_array(type);
5765 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5766
5767 if (dim == GLSL_SAMPLER_DIM_BUF) {
5768 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5769 unsigned num_channels = util_last_bit(mask);
5770 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5771 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5772
5773 aco_opcode opcode;
5774 switch (num_channels) {
5775 case 1:
5776 opcode = aco_opcode::buffer_load_format_x;
5777 break;
5778 case 2:
5779 opcode = aco_opcode::buffer_load_format_xy;
5780 break;
5781 case 3:
5782 opcode = aco_opcode::buffer_load_format_xyz;
5783 break;
5784 case 4:
5785 opcode = aco_opcode::buffer_load_format_xyzw;
5786 break;
5787 default:
5788 unreachable(">4 channel buffer image load");
5789 }
5790 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5791 load->operands[0] = Operand(rsrc);
5792 load->operands[1] = Operand(vindex);
5793 load->operands[2] = Operand((uint32_t) 0);
5794 Temp tmp;
5795 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5796 tmp = dst;
5797 else
5798 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5799 load->definitions[0] = Definition(tmp);
5800 load->idxen = true;
5801 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5802 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5803 load->barrier = barrier_image;
5804 ctx->block->instructions.emplace_back(std::move(load));
5805
5806 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5807 return;
5808 }
5809
5810 Temp coords = get_image_coords(ctx, instr, type);
5811 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5812
5813 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5814 unsigned num_components = util_bitcount(dmask);
5815 Temp tmp;
5816 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5817 tmp = dst;
5818 else
5819 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5820
5821 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5822 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5823
5824 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5825 load->operands[0] = Operand(resource);
5826 load->operands[1] = Operand(s4); /* no sampler */
5827 load->operands[2] = Operand(coords);
5828 load->definitions[0] = Definition(tmp);
5829 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5830 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5831 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5832 load->dmask = dmask;
5833 load->unrm = true;
5834 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5835 load->barrier = barrier_image;
5836 ctx->block->instructions.emplace_back(std::move(load));
5837
5838 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5839 return;
5840 }
5841
5842 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5843 {
5844 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5845 const struct glsl_type *type = glsl_without_array(var->type);
5846 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5847 bool is_array = glsl_sampler_type_is_array(type);
5848 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5849
5850 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5851
5852 if (dim == GLSL_SAMPLER_DIM_BUF) {
5853 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5854 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5855 aco_opcode opcode;
5856 switch (data.size()) {
5857 case 1:
5858 opcode = aco_opcode::buffer_store_format_x;
5859 break;
5860 case 2:
5861 opcode = aco_opcode::buffer_store_format_xy;
5862 break;
5863 case 3:
5864 opcode = aco_opcode::buffer_store_format_xyz;
5865 break;
5866 case 4:
5867 opcode = aco_opcode::buffer_store_format_xyzw;
5868 break;
5869 default:
5870 unreachable(">4 channel buffer image store");
5871 }
5872 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5873 store->operands[0] = Operand(rsrc);
5874 store->operands[1] = Operand(vindex);
5875 store->operands[2] = Operand((uint32_t) 0);
5876 store->operands[3] = Operand(data);
5877 store->idxen = true;
5878 store->glc = glc;
5879 store->dlc = false;
5880 store->disable_wqm = true;
5881 store->barrier = barrier_image;
5882 ctx->program->needs_exact = true;
5883 ctx->block->instructions.emplace_back(std::move(store));
5884 return;
5885 }
5886
5887 assert(data.type() == RegType::vgpr);
5888 Temp coords = get_image_coords(ctx, instr, type);
5889 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5890
5891 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5892 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5893
5894 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5895 store->operands[0] = Operand(resource);
5896 store->operands[1] = Operand(data);
5897 store->operands[2] = Operand(coords);
5898 store->glc = glc;
5899 store->dlc = false;
5900 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5901 store->dmask = (1 << data.size()) - 1;
5902 store->unrm = true;
5903 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5904 store->disable_wqm = true;
5905 store->barrier = barrier_image;
5906 ctx->program->needs_exact = true;
5907 ctx->block->instructions.emplace_back(std::move(store));
5908 return;
5909 }
5910
5911 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5912 {
5913 /* return the previous value if dest is ever used */
5914 bool return_previous = false;
5915 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5916 return_previous = true;
5917 break;
5918 }
5919 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5920 return_previous = true;
5921 break;
5922 }
5923
5924 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5925 const struct glsl_type *type = glsl_without_array(var->type);
5926 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5927 bool is_array = glsl_sampler_type_is_array(type);
5928 Builder bld(ctx->program, ctx->block);
5929
5930 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5931 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5932
5933 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5934 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5935
5936 aco_opcode buf_op, image_op;
5937 switch (instr->intrinsic) {
5938 case nir_intrinsic_image_deref_atomic_add:
5939 buf_op = aco_opcode::buffer_atomic_add;
5940 image_op = aco_opcode::image_atomic_add;
5941 break;
5942 case nir_intrinsic_image_deref_atomic_umin:
5943 buf_op = aco_opcode::buffer_atomic_umin;
5944 image_op = aco_opcode::image_atomic_umin;
5945 break;
5946 case nir_intrinsic_image_deref_atomic_imin:
5947 buf_op = aco_opcode::buffer_atomic_smin;
5948 image_op = aco_opcode::image_atomic_smin;
5949 break;
5950 case nir_intrinsic_image_deref_atomic_umax:
5951 buf_op = aco_opcode::buffer_atomic_umax;
5952 image_op = aco_opcode::image_atomic_umax;
5953 break;
5954 case nir_intrinsic_image_deref_atomic_imax:
5955 buf_op = aco_opcode::buffer_atomic_smax;
5956 image_op = aco_opcode::image_atomic_smax;
5957 break;
5958 case nir_intrinsic_image_deref_atomic_and:
5959 buf_op = aco_opcode::buffer_atomic_and;
5960 image_op = aco_opcode::image_atomic_and;
5961 break;
5962 case nir_intrinsic_image_deref_atomic_or:
5963 buf_op = aco_opcode::buffer_atomic_or;
5964 image_op = aco_opcode::image_atomic_or;
5965 break;
5966 case nir_intrinsic_image_deref_atomic_xor:
5967 buf_op = aco_opcode::buffer_atomic_xor;
5968 image_op = aco_opcode::image_atomic_xor;
5969 break;
5970 case nir_intrinsic_image_deref_atomic_exchange:
5971 buf_op = aco_opcode::buffer_atomic_swap;
5972 image_op = aco_opcode::image_atomic_swap;
5973 break;
5974 case nir_intrinsic_image_deref_atomic_comp_swap:
5975 buf_op = aco_opcode::buffer_atomic_cmpswap;
5976 image_op = aco_opcode::image_atomic_cmpswap;
5977 break;
5978 default:
5979 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5980 }
5981
5982 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5983
5984 if (dim == GLSL_SAMPLER_DIM_BUF) {
5985 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5986 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5987 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5988 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5989 mubuf->operands[0] = Operand(resource);
5990 mubuf->operands[1] = Operand(vindex);
5991 mubuf->operands[2] = Operand((uint32_t)0);
5992 mubuf->operands[3] = Operand(data);
5993 if (return_previous)
5994 mubuf->definitions[0] = Definition(dst);
5995 mubuf->offset = 0;
5996 mubuf->idxen = true;
5997 mubuf->glc = return_previous;
5998 mubuf->dlc = false; /* Not needed for atomics */
5999 mubuf->disable_wqm = true;
6000 mubuf->barrier = barrier_image;
6001 ctx->program->needs_exact = true;
6002 ctx->block->instructions.emplace_back(std::move(mubuf));
6003 return;
6004 }
6005
6006 Temp coords = get_image_coords(ctx, instr, type);
6007 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6008 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6009 mimg->operands[0] = Operand(resource);
6010 mimg->operands[1] = Operand(data);
6011 mimg->operands[2] = Operand(coords);
6012 if (return_previous)
6013 mimg->definitions[0] = Definition(dst);
6014 mimg->glc = return_previous;
6015 mimg->dlc = false; /* Not needed for atomics */
6016 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6017 mimg->dmask = (1 << data.size()) - 1;
6018 mimg->unrm = true;
6019 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6020 mimg->disable_wqm = true;
6021 mimg->barrier = barrier_image;
6022 ctx->program->needs_exact = true;
6023 ctx->block->instructions.emplace_back(std::move(mimg));
6024 return;
6025 }
6026
6027 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6028 {
6029 if (in_elements && ctx->options->chip_class == GFX8) {
6030 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6031 Builder bld(ctx->program, ctx->block);
6032
6033 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6034
6035 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6036 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6037
6038 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6039 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6040
6041 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6042 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6043
6044 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6045 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6046 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6047 if (dst.type() == RegType::vgpr)
6048 bld.copy(Definition(dst), shr_dst);
6049
6050 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6051 } else {
6052 emit_extract_vector(ctx, desc, 2, dst);
6053 }
6054 }
6055
6056 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6057 {
6058 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6059 const struct glsl_type *type = glsl_without_array(var->type);
6060 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6061 bool is_array = glsl_sampler_type_is_array(type);
6062 Builder bld(ctx->program, ctx->block);
6063
6064 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6065 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6066 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6067 }
6068
6069 /* LOD */
6070 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6071
6072 /* Resource */
6073 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6074
6075 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6076
6077 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6078 mimg->operands[0] = Operand(resource);
6079 mimg->operands[1] = Operand(s4); /* no sampler */
6080 mimg->operands[2] = Operand(lod);
6081 uint8_t& dmask = mimg->dmask;
6082 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6083 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6084 mimg->da = glsl_sampler_type_is_array(type);
6085 mimg->can_reorder = true;
6086 Definition& def = mimg->definitions[0];
6087 ctx->block->instructions.emplace_back(std::move(mimg));
6088
6089 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6090 glsl_sampler_type_is_array(type)) {
6091
6092 assert(instr->dest.ssa.num_components == 3);
6093 Temp tmp = {ctx->program->allocateId(), v3};
6094 def = Definition(tmp);
6095 emit_split_vector(ctx, tmp, 3);
6096
6097 /* divide 3rd value by 6 by multiplying with magic number */
6098 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6099 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6100
6101 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6102 emit_extract_vector(ctx, tmp, 0, v1),
6103 emit_extract_vector(ctx, tmp, 1, v1),
6104 by_6);
6105
6106 } else if (ctx->options->chip_class == GFX9 &&
6107 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6108 glsl_sampler_type_is_array(type)) {
6109 assert(instr->dest.ssa.num_components == 2);
6110 def = Definition(dst);
6111 dmask = 0x5;
6112 } else {
6113 def = Definition(dst);
6114 }
6115
6116 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6117 }
6118
6119 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6120 {
6121 Builder bld(ctx->program, ctx->block);
6122 unsigned num_components = instr->num_components;
6123
6124 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6125 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6126 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6127
6128 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6129 unsigned size = instr->dest.ssa.bit_size / 8;
6130 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6131 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false);
6132 }
6133
6134 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6135 {
6136 Builder bld(ctx->program, ctx->block);
6137 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6138 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6139 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6140 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6141
6142 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6143 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6144
6145 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
6146 ctx->options->chip_class >= GFX8 &&
6147 elem_size_bytes >= 4;
6148 if (smem)
6149 offset = bld.as_uniform(offset);
6150 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6151
6152 unsigned write_count = 0;
6153 Temp write_datas[32];
6154 unsigned offsets[32];
6155 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6156 data, writemask, 16, &write_count, write_datas, offsets);
6157
6158 for (unsigned i = 0; i < write_count; i++) {
6159 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6160 if (smem && ctx->stage == fragment_fs)
6161 op = aco_opcode::p_fs_buffer_store_smem;
6162
6163 if (smem) {
6164 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6165 store->operands[0] = Operand(rsrc);
6166 if (offsets[i]) {
6167 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6168 offset, Operand(offsets[i]));
6169 store->operands[1] = Operand(off);
6170 } else {
6171 store->operands[1] = Operand(offset);
6172 }
6173 if (op != aco_opcode::p_fs_buffer_store_smem)
6174 store->operands[1].setFixed(m0);
6175 store->operands[2] = Operand(write_datas[i]);
6176 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6177 store->dlc = false;
6178 store->disable_wqm = true;
6179 store->barrier = barrier_buffer;
6180 ctx->block->instructions.emplace_back(std::move(store));
6181 ctx->program->wb_smem_l1_on_end = true;
6182 if (op == aco_opcode::p_fs_buffer_store_smem) {
6183 ctx->block->kind |= block_kind_needs_lowering;
6184 ctx->program->needs_exact = true;
6185 }
6186 } else {
6187 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6188 store->operands[0] = Operand(rsrc);
6189 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6190 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6191 store->operands[3] = Operand(write_datas[i]);
6192 store->offset = offsets[i];
6193 store->offen = (offset.type() == RegType::vgpr);
6194 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6195 store->dlc = false;
6196 store->disable_wqm = true;
6197 store->barrier = barrier_buffer;
6198 ctx->program->needs_exact = true;
6199 ctx->block->instructions.emplace_back(std::move(store));
6200 }
6201 }
6202 }
6203
6204 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6205 {
6206 /* return the previous value if dest is ever used */
6207 bool return_previous = false;
6208 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6209 return_previous = true;
6210 break;
6211 }
6212 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6213 return_previous = true;
6214 break;
6215 }
6216
6217 Builder bld(ctx->program, ctx->block);
6218 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6219
6220 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6221 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6222 get_ssa_temp(ctx, instr->src[3].ssa), data);
6223
6224 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6225 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6226 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6227
6228 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6229
6230 aco_opcode op32, op64;
6231 switch (instr->intrinsic) {
6232 case nir_intrinsic_ssbo_atomic_add:
6233 op32 = aco_opcode::buffer_atomic_add;
6234 op64 = aco_opcode::buffer_atomic_add_x2;
6235 break;
6236 case nir_intrinsic_ssbo_atomic_imin:
6237 op32 = aco_opcode::buffer_atomic_smin;
6238 op64 = aco_opcode::buffer_atomic_smin_x2;
6239 break;
6240 case nir_intrinsic_ssbo_atomic_umin:
6241 op32 = aco_opcode::buffer_atomic_umin;
6242 op64 = aco_opcode::buffer_atomic_umin_x2;
6243 break;
6244 case nir_intrinsic_ssbo_atomic_imax:
6245 op32 = aco_opcode::buffer_atomic_smax;
6246 op64 = aco_opcode::buffer_atomic_smax_x2;
6247 break;
6248 case nir_intrinsic_ssbo_atomic_umax:
6249 op32 = aco_opcode::buffer_atomic_umax;
6250 op64 = aco_opcode::buffer_atomic_umax_x2;
6251 break;
6252 case nir_intrinsic_ssbo_atomic_and:
6253 op32 = aco_opcode::buffer_atomic_and;
6254 op64 = aco_opcode::buffer_atomic_and_x2;
6255 break;
6256 case nir_intrinsic_ssbo_atomic_or:
6257 op32 = aco_opcode::buffer_atomic_or;
6258 op64 = aco_opcode::buffer_atomic_or_x2;
6259 break;
6260 case nir_intrinsic_ssbo_atomic_xor:
6261 op32 = aco_opcode::buffer_atomic_xor;
6262 op64 = aco_opcode::buffer_atomic_xor_x2;
6263 break;
6264 case nir_intrinsic_ssbo_atomic_exchange:
6265 op32 = aco_opcode::buffer_atomic_swap;
6266 op64 = aco_opcode::buffer_atomic_swap_x2;
6267 break;
6268 case nir_intrinsic_ssbo_atomic_comp_swap:
6269 op32 = aco_opcode::buffer_atomic_cmpswap;
6270 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6271 break;
6272 default:
6273 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6274 }
6275 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6276 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6277 mubuf->operands[0] = Operand(rsrc);
6278 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6279 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6280 mubuf->operands[3] = Operand(data);
6281 if (return_previous)
6282 mubuf->definitions[0] = Definition(dst);
6283 mubuf->offset = 0;
6284 mubuf->offen = (offset.type() == RegType::vgpr);
6285 mubuf->glc = return_previous;
6286 mubuf->dlc = false; /* Not needed for atomics */
6287 mubuf->disable_wqm = true;
6288 mubuf->barrier = barrier_buffer;
6289 ctx->program->needs_exact = true;
6290 ctx->block->instructions.emplace_back(std::move(mubuf));
6291 }
6292
6293 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6294
6295 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6296 Builder bld(ctx->program, ctx->block);
6297 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6298 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6299 }
6300
6301 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6302 {
6303 Builder bld(ctx->program, ctx->block);
6304 unsigned num_components = instr->num_components;
6305 unsigned component_size = instr->dest.ssa.bit_size / 8;
6306
6307 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6308 get_ssa_temp(ctx, &instr->dest.ssa),
6309 num_components, component_size};
6310 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6311 info.align_mul = nir_intrinsic_align_mul(instr);
6312 info.align_offset = nir_intrinsic_align_offset(instr);
6313 info.barrier = barrier_buffer;
6314 info.can_reorder = false;
6315 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6316 * it's safe to use SMEM */
6317 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6318 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6319 emit_global_load(ctx, bld, &info);
6320 } else {
6321 info.offset = Operand(bld.as_uniform(info.offset));
6322 emit_smem_load(ctx, bld, &info);
6323 }
6324 }
6325
6326 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6327 {
6328 Builder bld(ctx->program, ctx->block);
6329 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6330 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6331
6332 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6333 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6334 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6335
6336 if (ctx->options->chip_class >= GFX7)
6337 addr = as_vgpr(ctx, addr);
6338
6339 unsigned write_count = 0;
6340 Temp write_datas[32];
6341 unsigned offsets[32];
6342 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6343 16, &write_count, write_datas, offsets);
6344
6345 for (unsigned i = 0; i < write_count; i++) {
6346 if (ctx->options->chip_class >= GFX7) {
6347 unsigned offset = offsets[i];
6348 Temp store_addr = addr;
6349 if (offset > 0 && ctx->options->chip_class < GFX9) {
6350 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6351 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6352 Temp carry = bld.tmp(bld.lm);
6353 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6354
6355 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6356 Operand(offset), addr0);
6357 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6358 Operand(0u), addr1,
6359 carry).def(1).setHint(vcc);
6360
6361 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6362
6363 offset = 0;
6364 }
6365
6366 bool global = ctx->options->chip_class >= GFX9;
6367 aco_opcode op;
6368 switch (write_datas[i].bytes()) {
6369 case 1:
6370 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6371 break;
6372 case 2:
6373 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6374 break;
6375 case 4:
6376 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6377 break;
6378 case 8:
6379 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6380 break;
6381 case 12:
6382 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6383 break;
6384 case 16:
6385 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6386 break;
6387 default:
6388 unreachable("store_global not implemented for this size.");
6389 }
6390
6391 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6392 flat->operands[0] = Operand(store_addr);
6393 flat->operands[1] = Operand(s1);
6394 flat->operands[2] = Operand(write_datas[i]);
6395 flat->glc = glc;
6396 flat->dlc = false;
6397 flat->offset = offset;
6398 flat->disable_wqm = true;
6399 flat->barrier = barrier_buffer;
6400 ctx->program->needs_exact = true;
6401 ctx->block->instructions.emplace_back(std::move(flat));
6402 } else {
6403 assert(ctx->options->chip_class == GFX6);
6404
6405 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6406
6407 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6408
6409 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6410 mubuf->operands[0] = Operand(rsrc);
6411 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6412 mubuf->operands[2] = Operand(0u);
6413 mubuf->operands[3] = Operand(write_datas[i]);
6414 mubuf->glc = glc;
6415 mubuf->dlc = false;
6416 mubuf->offset = offsets[i];
6417 mubuf->addr64 = addr.type() == RegType::vgpr;
6418 mubuf->disable_wqm = true;
6419 mubuf->barrier = barrier_buffer;
6420 ctx->program->needs_exact = true;
6421 ctx->block->instructions.emplace_back(std::move(mubuf));
6422 }
6423 }
6424 }
6425
6426 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6427 {
6428 /* return the previous value if dest is ever used */
6429 bool return_previous = false;
6430 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6431 return_previous = true;
6432 break;
6433 }
6434 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6435 return_previous = true;
6436 break;
6437 }
6438
6439 Builder bld(ctx->program, ctx->block);
6440 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6441 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6442
6443 if (ctx->options->chip_class >= GFX7)
6444 addr = as_vgpr(ctx, addr);
6445
6446 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6447 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6448 get_ssa_temp(ctx, instr->src[2].ssa), data);
6449
6450 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6451
6452 aco_opcode op32, op64;
6453
6454 if (ctx->options->chip_class >= GFX7) {
6455 bool global = ctx->options->chip_class >= GFX9;
6456 switch (instr->intrinsic) {
6457 case nir_intrinsic_global_atomic_add:
6458 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6459 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6460 break;
6461 case nir_intrinsic_global_atomic_imin:
6462 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6463 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6464 break;
6465 case nir_intrinsic_global_atomic_umin:
6466 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6467 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6468 break;
6469 case nir_intrinsic_global_atomic_imax:
6470 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6471 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6472 break;
6473 case nir_intrinsic_global_atomic_umax:
6474 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6475 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6476 break;
6477 case nir_intrinsic_global_atomic_and:
6478 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6479 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6480 break;
6481 case nir_intrinsic_global_atomic_or:
6482 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6483 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6484 break;
6485 case nir_intrinsic_global_atomic_xor:
6486 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6487 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6488 break;
6489 case nir_intrinsic_global_atomic_exchange:
6490 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6491 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6492 break;
6493 case nir_intrinsic_global_atomic_comp_swap:
6494 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6495 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6496 break;
6497 default:
6498 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6499 }
6500
6501 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6502 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6503 flat->operands[0] = Operand(addr);
6504 flat->operands[1] = Operand(s1);
6505 flat->operands[2] = Operand(data);
6506 if (return_previous)
6507 flat->definitions[0] = Definition(dst);
6508 flat->glc = return_previous;
6509 flat->dlc = false; /* Not needed for atomics */
6510 flat->offset = 0;
6511 flat->disable_wqm = true;
6512 flat->barrier = barrier_buffer;
6513 ctx->program->needs_exact = true;
6514 ctx->block->instructions.emplace_back(std::move(flat));
6515 } else {
6516 assert(ctx->options->chip_class == GFX6);
6517
6518 switch (instr->intrinsic) {
6519 case nir_intrinsic_global_atomic_add:
6520 op32 = aco_opcode::buffer_atomic_add;
6521 op64 = aco_opcode::buffer_atomic_add_x2;
6522 break;
6523 case nir_intrinsic_global_atomic_imin:
6524 op32 = aco_opcode::buffer_atomic_smin;
6525 op64 = aco_opcode::buffer_atomic_smin_x2;
6526 break;
6527 case nir_intrinsic_global_atomic_umin:
6528 op32 = aco_opcode::buffer_atomic_umin;
6529 op64 = aco_opcode::buffer_atomic_umin_x2;
6530 break;
6531 case nir_intrinsic_global_atomic_imax:
6532 op32 = aco_opcode::buffer_atomic_smax;
6533 op64 = aco_opcode::buffer_atomic_smax_x2;
6534 break;
6535 case nir_intrinsic_global_atomic_umax:
6536 op32 = aco_opcode::buffer_atomic_umax;
6537 op64 = aco_opcode::buffer_atomic_umax_x2;
6538 break;
6539 case nir_intrinsic_global_atomic_and:
6540 op32 = aco_opcode::buffer_atomic_and;
6541 op64 = aco_opcode::buffer_atomic_and_x2;
6542 break;
6543 case nir_intrinsic_global_atomic_or:
6544 op32 = aco_opcode::buffer_atomic_or;
6545 op64 = aco_opcode::buffer_atomic_or_x2;
6546 break;
6547 case nir_intrinsic_global_atomic_xor:
6548 op32 = aco_opcode::buffer_atomic_xor;
6549 op64 = aco_opcode::buffer_atomic_xor_x2;
6550 break;
6551 case nir_intrinsic_global_atomic_exchange:
6552 op32 = aco_opcode::buffer_atomic_swap;
6553 op64 = aco_opcode::buffer_atomic_swap_x2;
6554 break;
6555 case nir_intrinsic_global_atomic_comp_swap:
6556 op32 = aco_opcode::buffer_atomic_cmpswap;
6557 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6558 break;
6559 default:
6560 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6561 }
6562
6563 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6564
6565 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6566
6567 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6568 mubuf->operands[0] = Operand(rsrc);
6569 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6570 mubuf->operands[2] = Operand(0u);
6571 mubuf->operands[3] = Operand(data);
6572 if (return_previous)
6573 mubuf->definitions[0] = Definition(dst);
6574 mubuf->glc = return_previous;
6575 mubuf->dlc = false;
6576 mubuf->offset = 0;
6577 mubuf->addr64 = addr.type() == RegType::vgpr;
6578 mubuf->disable_wqm = true;
6579 mubuf->barrier = barrier_buffer;
6580 ctx->program->needs_exact = true;
6581 ctx->block->instructions.emplace_back(std::move(mubuf));
6582 }
6583 }
6584
6585 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6586 Builder bld(ctx->program, ctx->block);
6587 switch(instr->intrinsic) {
6588 case nir_intrinsic_group_memory_barrier:
6589 case nir_intrinsic_memory_barrier:
6590 bld.barrier(aco_opcode::p_memory_barrier_common);
6591 break;
6592 case nir_intrinsic_memory_barrier_buffer:
6593 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6594 break;
6595 case nir_intrinsic_memory_barrier_image:
6596 bld.barrier(aco_opcode::p_memory_barrier_image);
6597 break;
6598 case nir_intrinsic_memory_barrier_tcs_patch:
6599 case nir_intrinsic_memory_barrier_shared:
6600 bld.barrier(aco_opcode::p_memory_barrier_shared);
6601 break;
6602 default:
6603 unreachable("Unimplemented memory barrier intrinsic");
6604 break;
6605 }
6606 }
6607
6608 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6609 {
6610 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6611 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6612 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6613 Builder bld(ctx->program, ctx->block);
6614
6615 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6616 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6617 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6618 }
6619
6620 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6621 {
6622 unsigned writemask = nir_intrinsic_write_mask(instr);
6623 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6624 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6625 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6626
6627 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6628 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6629 }
6630
6631 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6632 {
6633 unsigned offset = nir_intrinsic_base(instr);
6634 Builder bld(ctx->program, ctx->block);
6635 Operand m = load_lds_size_m0(bld);
6636 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6637 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6638
6639 unsigned num_operands = 3;
6640 aco_opcode op32, op64, op32_rtn, op64_rtn;
6641 switch(instr->intrinsic) {
6642 case nir_intrinsic_shared_atomic_add:
6643 op32 = aco_opcode::ds_add_u32;
6644 op64 = aco_opcode::ds_add_u64;
6645 op32_rtn = aco_opcode::ds_add_rtn_u32;
6646 op64_rtn = aco_opcode::ds_add_rtn_u64;
6647 break;
6648 case nir_intrinsic_shared_atomic_imin:
6649 op32 = aco_opcode::ds_min_i32;
6650 op64 = aco_opcode::ds_min_i64;
6651 op32_rtn = aco_opcode::ds_min_rtn_i32;
6652 op64_rtn = aco_opcode::ds_min_rtn_i64;
6653 break;
6654 case nir_intrinsic_shared_atomic_umin:
6655 op32 = aco_opcode::ds_min_u32;
6656 op64 = aco_opcode::ds_min_u64;
6657 op32_rtn = aco_opcode::ds_min_rtn_u32;
6658 op64_rtn = aco_opcode::ds_min_rtn_u64;
6659 break;
6660 case nir_intrinsic_shared_atomic_imax:
6661 op32 = aco_opcode::ds_max_i32;
6662 op64 = aco_opcode::ds_max_i64;
6663 op32_rtn = aco_opcode::ds_max_rtn_i32;
6664 op64_rtn = aco_opcode::ds_max_rtn_i64;
6665 break;
6666 case nir_intrinsic_shared_atomic_umax:
6667 op32 = aco_opcode::ds_max_u32;
6668 op64 = aco_opcode::ds_max_u64;
6669 op32_rtn = aco_opcode::ds_max_rtn_u32;
6670 op64_rtn = aco_opcode::ds_max_rtn_u64;
6671 break;
6672 case nir_intrinsic_shared_atomic_and:
6673 op32 = aco_opcode::ds_and_b32;
6674 op64 = aco_opcode::ds_and_b64;
6675 op32_rtn = aco_opcode::ds_and_rtn_b32;
6676 op64_rtn = aco_opcode::ds_and_rtn_b64;
6677 break;
6678 case nir_intrinsic_shared_atomic_or:
6679 op32 = aco_opcode::ds_or_b32;
6680 op64 = aco_opcode::ds_or_b64;
6681 op32_rtn = aco_opcode::ds_or_rtn_b32;
6682 op64_rtn = aco_opcode::ds_or_rtn_b64;
6683 break;
6684 case nir_intrinsic_shared_atomic_xor:
6685 op32 = aco_opcode::ds_xor_b32;
6686 op64 = aco_opcode::ds_xor_b64;
6687 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6688 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6689 break;
6690 case nir_intrinsic_shared_atomic_exchange:
6691 op32 = aco_opcode::ds_write_b32;
6692 op64 = aco_opcode::ds_write_b64;
6693 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6694 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6695 break;
6696 case nir_intrinsic_shared_atomic_comp_swap:
6697 op32 = aco_opcode::ds_cmpst_b32;
6698 op64 = aco_opcode::ds_cmpst_b64;
6699 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6700 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6701 num_operands = 4;
6702 break;
6703 default:
6704 unreachable("Unhandled shared atomic intrinsic");
6705 }
6706
6707 /* return the previous value if dest is ever used */
6708 bool return_previous = false;
6709 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6710 return_previous = true;
6711 break;
6712 }
6713 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6714 return_previous = true;
6715 break;
6716 }
6717
6718 aco_opcode op;
6719 if (data.size() == 1) {
6720 assert(instr->dest.ssa.bit_size == 32);
6721 op = return_previous ? op32_rtn : op32;
6722 } else {
6723 assert(instr->dest.ssa.bit_size == 64);
6724 op = return_previous ? op64_rtn : op64;
6725 }
6726
6727 if (offset > 65535) {
6728 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6729 offset = 0;
6730 }
6731
6732 aco_ptr<DS_instruction> ds;
6733 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6734 ds->operands[0] = Operand(address);
6735 ds->operands[1] = Operand(data);
6736 if (num_operands == 4)
6737 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6738 ds->operands[num_operands - 1] = m;
6739 ds->offset0 = offset;
6740 if (return_previous)
6741 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6742 ctx->block->instructions.emplace_back(std::move(ds));
6743 }
6744
6745 Temp get_scratch_resource(isel_context *ctx)
6746 {
6747 Builder bld(ctx->program, ctx->block);
6748 Temp scratch_addr = ctx->program->private_segment_buffer;
6749 if (ctx->stage != compute_cs)
6750 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6751
6752 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6753 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6754
6755 if (ctx->program->chip_class >= GFX10) {
6756 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6757 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6758 S_008F0C_RESOURCE_LEVEL(1);
6759 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6760 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6761 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6762 }
6763
6764 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6765 if (ctx->program->chip_class <= GFX8)
6766 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6767
6768 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6769 }
6770
6771 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6772 Builder bld(ctx->program, ctx->block);
6773 Temp rsrc = get_scratch_resource(ctx);
6774 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6775 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6776
6777 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6778 instr->dest.ssa.bit_size / 8u, rsrc};
6779 info.align_mul = nir_intrinsic_align_mul(instr);
6780 info.align_offset = nir_intrinsic_align_offset(instr);
6781 info.swizzle_component_size = 16;
6782 info.can_reorder = false;
6783 info.soffset = ctx->program->scratch_offset;
6784 emit_mubuf_load(ctx, bld, &info);
6785 }
6786
6787 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6788 Builder bld(ctx->program, ctx->block);
6789 Temp rsrc = get_scratch_resource(ctx);
6790 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6791 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6792
6793 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6794 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6795
6796 unsigned write_count = 0;
6797 Temp write_datas[32];
6798 unsigned offsets[32];
6799 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6800 16, &write_count, write_datas, offsets);
6801
6802 for (unsigned i = 0; i < write_count; i++) {
6803 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6804 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true);
6805 }
6806 }
6807
6808 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6809 uint8_t log2_ps_iter_samples;
6810 if (ctx->program->info->ps.force_persample) {
6811 log2_ps_iter_samples =
6812 util_logbase2(ctx->options->key.fs.num_samples);
6813 } else {
6814 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6815 }
6816
6817 /* The bit pattern matches that used by fixed function fragment
6818 * processing. */
6819 static const unsigned ps_iter_masks[] = {
6820 0xffff, /* not used */
6821 0x5555,
6822 0x1111,
6823 0x0101,
6824 0x0001,
6825 };
6826 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6827
6828 Builder bld(ctx->program, ctx->block);
6829
6830 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6831 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6832 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6833 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6834 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6835 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6836 }
6837
6838 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6839 Builder bld(ctx->program, ctx->block);
6840
6841 unsigned stream = nir_intrinsic_stream_id(instr);
6842 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6843 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6844 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6845
6846 /* get GSVS ring */
6847 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6848
6849 unsigned num_components =
6850 ctx->program->info->gs.num_stream_output_components[stream];
6851 assert(num_components);
6852
6853 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6854 unsigned stream_offset = 0;
6855 for (unsigned i = 0; i < stream; i++) {
6856 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6857 stream_offset += prev_stride * ctx->program->wave_size;
6858 }
6859
6860 /* Limit on the stride field for <= GFX7. */
6861 assert(stride < (1 << 14));
6862
6863 Temp gsvs_dwords[4];
6864 for (unsigned i = 0; i < 4; i++)
6865 gsvs_dwords[i] = bld.tmp(s1);
6866 bld.pseudo(aco_opcode::p_split_vector,
6867 Definition(gsvs_dwords[0]),
6868 Definition(gsvs_dwords[1]),
6869 Definition(gsvs_dwords[2]),
6870 Definition(gsvs_dwords[3]),
6871 gsvs_ring);
6872
6873 if (stream_offset) {
6874 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6875
6876 Temp carry = bld.tmp(s1);
6877 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6878 gsvs_dwords[1] = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), gsvs_dwords[1], Operand(0u), bld.scc(carry));
6879 }
6880
6881 gsvs_dwords[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), gsvs_dwords[1], Operand(S_008F04_STRIDE(stride)));
6882 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6883
6884 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6885 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6886
6887 unsigned offset = 0;
6888 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6889 if (ctx->program->info->gs.output_streams[i] != stream)
6890 continue;
6891
6892 for (unsigned j = 0; j < 4; j++) {
6893 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6894 continue;
6895
6896 if (ctx->outputs.mask[i] & (1 << j)) {
6897 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6898 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6899 if (const_offset >= 4096u) {
6900 if (vaddr_offset.isUndefined())
6901 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6902 else
6903 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6904 const_offset %= 4096u;
6905 }
6906
6907 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6908 mtbuf->operands[0] = Operand(gsvs_ring);
6909 mtbuf->operands[1] = vaddr_offset;
6910 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6911 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6912 mtbuf->offen = !vaddr_offset.isUndefined();
6913 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6914 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6915 mtbuf->offset = const_offset;
6916 mtbuf->glc = true;
6917 mtbuf->slc = true;
6918 mtbuf->barrier = barrier_gs_data;
6919 mtbuf->can_reorder = true;
6920 bld.insert(std::move(mtbuf));
6921 }
6922
6923 offset += ctx->shader->info.gs.vertices_out;
6924 }
6925
6926 /* outputs for the next vertex are undefined and keeping them around can
6927 * create invalid IR with control flow */
6928 ctx->outputs.mask[i] = 0;
6929 }
6930
6931 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6932 }
6933
6934 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6935 {
6936 Builder bld(ctx->program, ctx->block);
6937
6938 if (cluster_size == 1) {
6939 return src;
6940 } if (op == nir_op_iand && cluster_size == 4) {
6941 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6942 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6943 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6944 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6945 } else if (op == nir_op_ior && cluster_size == 4) {
6946 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6947 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6948 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6949 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6950 //subgroupAnd(val) -> (exec & ~val) == 0
6951 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6952 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6953 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6954 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6955 //subgroupOr(val) -> (val & exec) != 0
6956 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6957 return bool_to_vector_condition(ctx, tmp);
6958 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6959 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6960 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6961 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6962 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6963 return bool_to_vector_condition(ctx, tmp);
6964 } else {
6965 //subgroupClustered{And,Or,Xor}(val, n) ->
6966 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6967 //cluster_offset = ~(n - 1) & lane_id
6968 //cluster_mask = ((1 << n) - 1)
6969 //subgroupClusteredAnd():
6970 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6971 //subgroupClusteredOr():
6972 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6973 //subgroupClusteredXor():
6974 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6975 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6976 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6977
6978 Temp tmp;
6979 if (op == nir_op_iand)
6980 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6981 else
6982 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6983
6984 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6985
6986 if (ctx->program->chip_class <= GFX7)
6987 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6988 else if (ctx->program->wave_size == 64)
6989 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6990 else
6991 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6992 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6993 if (cluster_mask != 0xffffffff)
6994 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6995
6996 Definition cmp_def = Definition();
6997 if (op == nir_op_iand) {
6998 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6999 } else if (op == nir_op_ior) {
7000 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7001 } else if (op == nir_op_ixor) {
7002 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7003 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7004 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7005 }
7006 cmp_def.setHint(vcc);
7007 return cmp_def.getTemp();
7008 }
7009 }
7010
7011 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7012 {
7013 Builder bld(ctx->program, ctx->block);
7014
7015 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7016 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7017 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7018 Temp tmp;
7019 if (op == nir_op_iand)
7020 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7021 else
7022 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7023
7024 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7025 Temp lo = lohi.def(0).getTemp();
7026 Temp hi = lohi.def(1).getTemp();
7027 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7028
7029 Definition cmp_def = Definition();
7030 if (op == nir_op_iand)
7031 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7032 else if (op == nir_op_ior)
7033 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7034 else if (op == nir_op_ixor)
7035 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7036 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7037 cmp_def.setHint(vcc);
7038 return cmp_def.getTemp();
7039 }
7040
7041 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7042 {
7043 Builder bld(ctx->program, ctx->block);
7044
7045 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7046 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7047 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7048 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7049 if (op == nir_op_iand)
7050 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7051 else if (op == nir_op_ior)
7052 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7053 else if (op == nir_op_ixor)
7054 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7055
7056 assert(false);
7057 return Temp();
7058 }
7059
7060 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7061 {
7062 Builder bld(ctx->program, ctx->block);
7063 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7064 if (src.regClass().type() == RegType::vgpr) {
7065 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7066 } else if (src.regClass() == s1) {
7067 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7068 } else if (src.regClass() == s2) {
7069 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7070 } else {
7071 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7072 nir_print_instr(&instr->instr, stderr);
7073 fprintf(stderr, "\n");
7074 }
7075 }
7076
7077 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7078 {
7079 Builder bld(ctx->program, ctx->block);
7080 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7081 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7082 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7083
7084 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7085 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7086 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7087 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7088
7089 /* Build DD X/Y */
7090 if (ctx->program->chip_class >= GFX8) {
7091 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7092 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7093 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7094 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7095 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7096 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7097 } else {
7098 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7099 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7100 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7101 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7102 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7103 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7104 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7105 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7106 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7107 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7108 }
7109
7110 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7111 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7112 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7113 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7114 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7115 Temp wqm1 = bld.tmp(v1);
7116 emit_wqm(ctx, tmp1, wqm1, true);
7117 Temp wqm2 = bld.tmp(v1);
7118 emit_wqm(ctx, tmp2, wqm2, true);
7119 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7120 return;
7121 }
7122
7123 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7124 {
7125 Builder bld(ctx->program, ctx->block);
7126 switch(instr->intrinsic) {
7127 case nir_intrinsic_load_barycentric_sample:
7128 case nir_intrinsic_load_barycentric_pixel:
7129 case nir_intrinsic_load_barycentric_centroid: {
7130 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7131 Temp bary = Temp(0, s2);
7132 switch (mode) {
7133 case INTERP_MODE_SMOOTH:
7134 case INTERP_MODE_NONE:
7135 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7136 bary = get_arg(ctx, ctx->args->ac.persp_center);
7137 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7138 bary = ctx->persp_centroid;
7139 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7140 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7141 break;
7142 case INTERP_MODE_NOPERSPECTIVE:
7143 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7144 bary = get_arg(ctx, ctx->args->ac.linear_center);
7145 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7146 bary = ctx->linear_centroid;
7147 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7148 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7149 break;
7150 default:
7151 break;
7152 }
7153 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7154 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7155 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7156 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7157 Operand(p1), Operand(p2));
7158 emit_split_vector(ctx, dst, 2);
7159 break;
7160 }
7161 case nir_intrinsic_load_barycentric_model: {
7162 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7163
7164 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7165 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7166 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7167 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7168 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7169 Operand(p1), Operand(p2), Operand(p3));
7170 emit_split_vector(ctx, dst, 3);
7171 break;
7172 }
7173 case nir_intrinsic_load_barycentric_at_sample: {
7174 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7175 switch (ctx->options->key.fs.num_samples) {
7176 case 2: sample_pos_offset += 1 << 3; break;
7177 case 4: sample_pos_offset += 3 << 3; break;
7178 case 8: sample_pos_offset += 7 << 3; break;
7179 default: break;
7180 }
7181 Temp sample_pos;
7182 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7183 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7184 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7185 if (addr.type() == RegType::sgpr) {
7186 Operand offset;
7187 if (const_addr) {
7188 sample_pos_offset += const_addr->u32 << 3;
7189 offset = Operand(sample_pos_offset);
7190 } else if (ctx->options->chip_class >= GFX9) {
7191 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7192 } else {
7193 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7194 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7195 }
7196
7197 Operand off = bld.copy(bld.def(s1), Operand(offset));
7198 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7199
7200 } else if (ctx->options->chip_class >= GFX9) {
7201 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7202 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7203 } else if (ctx->options->chip_class >= GFX7) {
7204 /* addr += private_segment_buffer + sample_pos_offset */
7205 Temp tmp0 = bld.tmp(s1);
7206 Temp tmp1 = bld.tmp(s1);
7207 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7208 Definition scc_tmp = bld.def(s1, scc);
7209 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7210 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7211 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7212 Temp pck0 = bld.tmp(v1);
7213 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7214 tmp1 = as_vgpr(ctx, tmp1);
7215 Temp pck1 = bld.vop2_e64(aco_opcode::v_addc_co_u32, bld.def(v1), bld.hint_vcc(bld.def(bld.lm)), tmp1, Operand(0u), carry);
7216 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7217
7218 /* sample_pos = flat_load_dwordx2 addr */
7219 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7220 } else {
7221 assert(ctx->options->chip_class == GFX6);
7222
7223 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7224 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7225 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7226
7227 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7228 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7229
7230 sample_pos = bld.tmp(v2);
7231
7232 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7233 load->definitions[0] = Definition(sample_pos);
7234 load->operands[0] = Operand(rsrc);
7235 load->operands[1] = Operand(addr);
7236 load->operands[2] = Operand(0u);
7237 load->offset = sample_pos_offset;
7238 load->offen = 0;
7239 load->addr64 = true;
7240 load->glc = false;
7241 load->dlc = false;
7242 load->disable_wqm = false;
7243 load->barrier = barrier_none;
7244 load->can_reorder = true;
7245 ctx->block->instructions.emplace_back(std::move(load));
7246 }
7247
7248 /* sample_pos -= 0.5 */
7249 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7250 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7251 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7252 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7253 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7254
7255 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7256 break;
7257 }
7258 case nir_intrinsic_load_barycentric_at_offset: {
7259 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7260 RegClass rc = RegClass(offset.type(), 1);
7261 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7262 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7263 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7264 break;
7265 }
7266 case nir_intrinsic_load_front_face: {
7267 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7268 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7269 break;
7270 }
7271 case nir_intrinsic_load_view_index: {
7272 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7273 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7274 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7275 break;
7276 }
7277
7278 /* fallthrough */
7279 }
7280 case nir_intrinsic_load_layer_id: {
7281 unsigned idx = nir_intrinsic_base(instr);
7282 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7283 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7284 break;
7285 }
7286 case nir_intrinsic_load_frag_coord: {
7287 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7288 break;
7289 }
7290 case nir_intrinsic_load_sample_pos: {
7291 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7292 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7293 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7294 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7295 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7296 break;
7297 }
7298 case nir_intrinsic_load_tess_coord:
7299 visit_load_tess_coord(ctx, instr);
7300 break;
7301 case nir_intrinsic_load_interpolated_input:
7302 visit_load_interpolated_input(ctx, instr);
7303 break;
7304 case nir_intrinsic_store_output:
7305 visit_store_output(ctx, instr);
7306 break;
7307 case nir_intrinsic_load_input:
7308 case nir_intrinsic_load_input_vertex:
7309 visit_load_input(ctx, instr);
7310 break;
7311 case nir_intrinsic_load_output:
7312 visit_load_output(ctx, instr);
7313 break;
7314 case nir_intrinsic_load_per_vertex_input:
7315 visit_load_per_vertex_input(ctx, instr);
7316 break;
7317 case nir_intrinsic_load_per_vertex_output:
7318 visit_load_per_vertex_output(ctx, instr);
7319 break;
7320 case nir_intrinsic_store_per_vertex_output:
7321 visit_store_per_vertex_output(ctx, instr);
7322 break;
7323 case nir_intrinsic_load_ubo:
7324 visit_load_ubo(ctx, instr);
7325 break;
7326 case nir_intrinsic_load_push_constant:
7327 visit_load_push_constant(ctx, instr);
7328 break;
7329 case nir_intrinsic_load_constant:
7330 visit_load_constant(ctx, instr);
7331 break;
7332 case nir_intrinsic_vulkan_resource_index:
7333 visit_load_resource(ctx, instr);
7334 break;
7335 case nir_intrinsic_discard:
7336 visit_discard(ctx, instr);
7337 break;
7338 case nir_intrinsic_discard_if:
7339 visit_discard_if(ctx, instr);
7340 break;
7341 case nir_intrinsic_load_shared:
7342 visit_load_shared(ctx, instr);
7343 break;
7344 case nir_intrinsic_store_shared:
7345 visit_store_shared(ctx, instr);
7346 break;
7347 case nir_intrinsic_shared_atomic_add:
7348 case nir_intrinsic_shared_atomic_imin:
7349 case nir_intrinsic_shared_atomic_umin:
7350 case nir_intrinsic_shared_atomic_imax:
7351 case nir_intrinsic_shared_atomic_umax:
7352 case nir_intrinsic_shared_atomic_and:
7353 case nir_intrinsic_shared_atomic_or:
7354 case nir_intrinsic_shared_atomic_xor:
7355 case nir_intrinsic_shared_atomic_exchange:
7356 case nir_intrinsic_shared_atomic_comp_swap:
7357 visit_shared_atomic(ctx, instr);
7358 break;
7359 case nir_intrinsic_image_deref_load:
7360 visit_image_load(ctx, instr);
7361 break;
7362 case nir_intrinsic_image_deref_store:
7363 visit_image_store(ctx, instr);
7364 break;
7365 case nir_intrinsic_image_deref_atomic_add:
7366 case nir_intrinsic_image_deref_atomic_umin:
7367 case nir_intrinsic_image_deref_atomic_imin:
7368 case nir_intrinsic_image_deref_atomic_umax:
7369 case nir_intrinsic_image_deref_atomic_imax:
7370 case nir_intrinsic_image_deref_atomic_and:
7371 case nir_intrinsic_image_deref_atomic_or:
7372 case nir_intrinsic_image_deref_atomic_xor:
7373 case nir_intrinsic_image_deref_atomic_exchange:
7374 case nir_intrinsic_image_deref_atomic_comp_swap:
7375 visit_image_atomic(ctx, instr);
7376 break;
7377 case nir_intrinsic_image_deref_size:
7378 visit_image_size(ctx, instr);
7379 break;
7380 case nir_intrinsic_load_ssbo:
7381 visit_load_ssbo(ctx, instr);
7382 break;
7383 case nir_intrinsic_store_ssbo:
7384 visit_store_ssbo(ctx, instr);
7385 break;
7386 case nir_intrinsic_load_global:
7387 visit_load_global(ctx, instr);
7388 break;
7389 case nir_intrinsic_store_global:
7390 visit_store_global(ctx, instr);
7391 break;
7392 case nir_intrinsic_global_atomic_add:
7393 case nir_intrinsic_global_atomic_imin:
7394 case nir_intrinsic_global_atomic_umin:
7395 case nir_intrinsic_global_atomic_imax:
7396 case nir_intrinsic_global_atomic_umax:
7397 case nir_intrinsic_global_atomic_and:
7398 case nir_intrinsic_global_atomic_or:
7399 case nir_intrinsic_global_atomic_xor:
7400 case nir_intrinsic_global_atomic_exchange:
7401 case nir_intrinsic_global_atomic_comp_swap:
7402 visit_global_atomic(ctx, instr);
7403 break;
7404 case nir_intrinsic_ssbo_atomic_add:
7405 case nir_intrinsic_ssbo_atomic_imin:
7406 case nir_intrinsic_ssbo_atomic_umin:
7407 case nir_intrinsic_ssbo_atomic_imax:
7408 case nir_intrinsic_ssbo_atomic_umax:
7409 case nir_intrinsic_ssbo_atomic_and:
7410 case nir_intrinsic_ssbo_atomic_or:
7411 case nir_intrinsic_ssbo_atomic_xor:
7412 case nir_intrinsic_ssbo_atomic_exchange:
7413 case nir_intrinsic_ssbo_atomic_comp_swap:
7414 visit_atomic_ssbo(ctx, instr);
7415 break;
7416 case nir_intrinsic_load_scratch:
7417 visit_load_scratch(ctx, instr);
7418 break;
7419 case nir_intrinsic_store_scratch:
7420 visit_store_scratch(ctx, instr);
7421 break;
7422 case nir_intrinsic_get_buffer_size:
7423 visit_get_buffer_size(ctx, instr);
7424 break;
7425 case nir_intrinsic_control_barrier: {
7426 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7427 /* GFX6 only (thanks to a hw bug workaround):
7428 * The real barrier instruction isn’t needed, because an entire patch
7429 * always fits into a single wave.
7430 */
7431 break;
7432 }
7433
7434 if (ctx->program->workgroup_size > ctx->program->wave_size)
7435 bld.sopp(aco_opcode::s_barrier);
7436
7437 break;
7438 }
7439 case nir_intrinsic_memory_barrier_tcs_patch:
7440 case nir_intrinsic_group_memory_barrier:
7441 case nir_intrinsic_memory_barrier:
7442 case nir_intrinsic_memory_barrier_buffer:
7443 case nir_intrinsic_memory_barrier_image:
7444 case nir_intrinsic_memory_barrier_shared:
7445 emit_memory_barrier(ctx, instr);
7446 break;
7447 case nir_intrinsic_load_num_work_groups: {
7448 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7449 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7450 emit_split_vector(ctx, dst, 3);
7451 break;
7452 }
7453 case nir_intrinsic_load_local_invocation_id: {
7454 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7455 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7456 emit_split_vector(ctx, dst, 3);
7457 break;
7458 }
7459 case nir_intrinsic_load_work_group_id: {
7460 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7461 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7462 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7463 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7464 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7465 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7466 emit_split_vector(ctx, dst, 3);
7467 break;
7468 }
7469 case nir_intrinsic_load_local_invocation_index: {
7470 Temp id = emit_mbcnt(ctx, bld.def(v1));
7471
7472 /* The tg_size bits [6:11] contain the subgroup id,
7473 * we need this multiplied by the wave size, and then OR the thread id to it.
7474 */
7475 if (ctx->program->wave_size == 64) {
7476 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7477 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7478 get_arg(ctx, ctx->args->ac.tg_size));
7479 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7480 } else {
7481 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7482 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7483 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7484 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7485 }
7486 break;
7487 }
7488 case nir_intrinsic_load_subgroup_id: {
7489 if (ctx->stage == compute_cs) {
7490 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7491 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7492 } else {
7493 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7494 }
7495 break;
7496 }
7497 case nir_intrinsic_load_subgroup_invocation: {
7498 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7499 break;
7500 }
7501 case nir_intrinsic_load_num_subgroups: {
7502 if (ctx->stage == compute_cs)
7503 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7504 get_arg(ctx, ctx->args->ac.tg_size));
7505 else
7506 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7507 break;
7508 }
7509 case nir_intrinsic_ballot: {
7510 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7511 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7512 Definition tmp = bld.def(dst.regClass());
7513 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7514 if (instr->src[0].ssa->bit_size == 1) {
7515 assert(src.regClass() == bld.lm);
7516 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7517 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7518 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7519 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7520 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7521 } else {
7522 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7523 nir_print_instr(&instr->instr, stderr);
7524 fprintf(stderr, "\n");
7525 }
7526 if (dst.size() != bld.lm.size()) {
7527 /* Wave32 with ballot size set to 64 */
7528 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7529 }
7530 emit_wqm(ctx, tmp.getTemp(), dst);
7531 break;
7532 }
7533 case nir_intrinsic_shuffle:
7534 case nir_intrinsic_read_invocation: {
7535 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7536 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7537 emit_uniform_subgroup(ctx, instr, src);
7538 } else {
7539 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7540 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7541 tid = bld.as_uniform(tid);
7542 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7543 if (src.regClass() == v1) {
7544 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7545 } else if (src.regClass() == v2) {
7546 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7547 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7548 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7549 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7550 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7551 emit_split_vector(ctx, dst, 2);
7552 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7553 assert(src.regClass() == bld.lm);
7554 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7555 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7556 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7557 assert(src.regClass() == bld.lm);
7558 Temp tmp;
7559 if (ctx->program->chip_class <= GFX7)
7560 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7561 else if (ctx->program->wave_size == 64)
7562 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7563 else
7564 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7565 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7566 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7567 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7568 } else {
7569 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7570 nir_print_instr(&instr->instr, stderr);
7571 fprintf(stderr, "\n");
7572 }
7573 }
7574 break;
7575 }
7576 case nir_intrinsic_load_sample_id: {
7577 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7578 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7579 break;
7580 }
7581 case nir_intrinsic_load_sample_mask_in: {
7582 visit_load_sample_mask_in(ctx, instr);
7583 break;
7584 }
7585 case nir_intrinsic_read_first_invocation: {
7586 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7587 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7588 if (src.regClass() == v1) {
7589 emit_wqm(ctx,
7590 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7591 dst);
7592 } else if (src.regClass() == v2) {
7593 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7594 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7595 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7596 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7597 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7598 emit_split_vector(ctx, dst, 2);
7599 } else if (instr->dest.ssa.bit_size == 1) {
7600 assert(src.regClass() == bld.lm);
7601 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7602 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7603 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7604 } else if (src.regClass() == s1) {
7605 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7606 } else if (src.regClass() == s2) {
7607 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7608 } else {
7609 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7610 nir_print_instr(&instr->instr, stderr);
7611 fprintf(stderr, "\n");
7612 }
7613 break;
7614 }
7615 case nir_intrinsic_vote_all: {
7616 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7617 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7618 assert(src.regClass() == bld.lm);
7619 assert(dst.regClass() == bld.lm);
7620
7621 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7622 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7623 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7624 break;
7625 }
7626 case nir_intrinsic_vote_any: {
7627 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7628 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7629 assert(src.regClass() == bld.lm);
7630 assert(dst.regClass() == bld.lm);
7631
7632 Temp tmp = bool_to_scalar_condition(ctx, src);
7633 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7634 break;
7635 }
7636 case nir_intrinsic_reduce:
7637 case nir_intrinsic_inclusive_scan:
7638 case nir_intrinsic_exclusive_scan: {
7639 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7640 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7641 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7642 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7643 nir_intrinsic_cluster_size(instr) : 0;
7644 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7645
7646 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7647 emit_uniform_subgroup(ctx, instr, src);
7648 } else if (instr->dest.ssa.bit_size == 1) {
7649 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7650 op = nir_op_iand;
7651 else if (op == nir_op_iadd)
7652 op = nir_op_ixor;
7653 else if (op == nir_op_umax || op == nir_op_imax)
7654 op = nir_op_ior;
7655 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7656
7657 switch (instr->intrinsic) {
7658 case nir_intrinsic_reduce:
7659 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7660 break;
7661 case nir_intrinsic_exclusive_scan:
7662 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7663 break;
7664 case nir_intrinsic_inclusive_scan:
7665 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7666 break;
7667 default:
7668 assert(false);
7669 }
7670 } else if (cluster_size == 1) {
7671 bld.copy(Definition(dst), src);
7672 } else {
7673 src = as_vgpr(ctx, src);
7674
7675 ReduceOp reduce_op;
7676 switch (op) {
7677 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7678 CASE(iadd)
7679 CASE(imul)
7680 CASE(fadd)
7681 CASE(fmul)
7682 CASE(imin)
7683 CASE(umin)
7684 CASE(fmin)
7685 CASE(imax)
7686 CASE(umax)
7687 CASE(fmax)
7688 CASE(iand)
7689 CASE(ior)
7690 CASE(ixor)
7691 default:
7692 unreachable("unknown reduction op");
7693 #undef CASE
7694 }
7695
7696 aco_opcode aco_op;
7697 switch (instr->intrinsic) {
7698 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7699 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7700 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7701 default:
7702 unreachable("unknown reduce intrinsic");
7703 }
7704
7705 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7706 reduce->operands[0] = Operand(src);
7707 // filled in by aco_reduce_assign.cpp, used internally as part of the
7708 // reduce sequence
7709 assert(dst.size() == 1 || dst.size() == 2);
7710 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7711 reduce->operands[2] = Operand(v1.as_linear());
7712
7713 Temp tmp_dst = bld.tmp(dst.regClass());
7714 reduce->definitions[0] = Definition(tmp_dst);
7715 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7716 reduce->definitions[2] = Definition();
7717 reduce->definitions[3] = Definition(scc, s1);
7718 reduce->definitions[4] = Definition();
7719 reduce->reduce_op = reduce_op;
7720 reduce->cluster_size = cluster_size;
7721 ctx->block->instructions.emplace_back(std::move(reduce));
7722
7723 emit_wqm(ctx, tmp_dst, dst);
7724 }
7725 break;
7726 }
7727 case nir_intrinsic_quad_broadcast: {
7728 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7729 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7730 emit_uniform_subgroup(ctx, instr, src);
7731 } else {
7732 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7733 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7734 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7735
7736 if (instr->dest.ssa.bit_size == 1) {
7737 assert(src.regClass() == bld.lm);
7738 assert(dst.regClass() == bld.lm);
7739 uint32_t half_mask = 0x11111111u << lane;
7740 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7741 Temp tmp = bld.tmp(bld.lm);
7742 bld.sop1(Builder::s_wqm, Definition(tmp),
7743 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7744 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7745 emit_wqm(ctx, tmp, dst);
7746 } else if (instr->dest.ssa.bit_size == 32) {
7747 if (ctx->program->chip_class >= GFX8)
7748 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7749 else
7750 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7751 } else if (instr->dest.ssa.bit_size == 64) {
7752 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7753 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7754 if (ctx->program->chip_class >= GFX8) {
7755 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7756 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7757 } else {
7758 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7759 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7760 }
7761 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7762 emit_split_vector(ctx, dst, 2);
7763 } else {
7764 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7765 nir_print_instr(&instr->instr, stderr);
7766 fprintf(stderr, "\n");
7767 }
7768 }
7769 break;
7770 }
7771 case nir_intrinsic_quad_swap_horizontal:
7772 case nir_intrinsic_quad_swap_vertical:
7773 case nir_intrinsic_quad_swap_diagonal:
7774 case nir_intrinsic_quad_swizzle_amd: {
7775 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7776 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7777 emit_uniform_subgroup(ctx, instr, src);
7778 break;
7779 }
7780 uint16_t dpp_ctrl = 0;
7781 switch (instr->intrinsic) {
7782 case nir_intrinsic_quad_swap_horizontal:
7783 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7784 break;
7785 case nir_intrinsic_quad_swap_vertical:
7786 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7787 break;
7788 case nir_intrinsic_quad_swap_diagonal:
7789 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7790 break;
7791 case nir_intrinsic_quad_swizzle_amd:
7792 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7793 break;
7794 default:
7795 break;
7796 }
7797 if (ctx->program->chip_class < GFX8)
7798 dpp_ctrl |= (1 << 15);
7799
7800 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7801 if (instr->dest.ssa.bit_size == 1) {
7802 assert(src.regClass() == bld.lm);
7803 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7804 if (ctx->program->chip_class >= GFX8)
7805 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7806 else
7807 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7808 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7809 emit_wqm(ctx, tmp, dst);
7810 } else if (instr->dest.ssa.bit_size == 32) {
7811 Temp tmp;
7812 if (ctx->program->chip_class >= GFX8)
7813 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7814 else
7815 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7816 emit_wqm(ctx, tmp, dst);
7817 } else if (instr->dest.ssa.bit_size == 64) {
7818 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7819 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7820 if (ctx->program->chip_class >= GFX8) {
7821 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7822 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7823 } else {
7824 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7825 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7826 }
7827 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7828 emit_split_vector(ctx, dst, 2);
7829 } else {
7830 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7831 nir_print_instr(&instr->instr, stderr);
7832 fprintf(stderr, "\n");
7833 }
7834 break;
7835 }
7836 case nir_intrinsic_masked_swizzle_amd: {
7837 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7838 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7839 emit_uniform_subgroup(ctx, instr, src);
7840 break;
7841 }
7842 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7843 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7844 if (dst.regClass() == v1) {
7845 emit_wqm(ctx,
7846 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7847 dst);
7848 } else if (dst.regClass() == v2) {
7849 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7850 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7851 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7852 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7853 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7854 emit_split_vector(ctx, dst, 2);
7855 } else {
7856 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7857 nir_print_instr(&instr->instr, stderr);
7858 fprintf(stderr, "\n");
7859 }
7860 break;
7861 }
7862 case nir_intrinsic_write_invocation_amd: {
7863 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7864 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7865 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7866 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7867 if (dst.regClass() == v1) {
7868 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7869 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7870 } else if (dst.regClass() == v2) {
7871 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7872 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7873 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7874 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7875 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7876 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7877 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7878 emit_split_vector(ctx, dst, 2);
7879 } else {
7880 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7881 nir_print_instr(&instr->instr, stderr);
7882 fprintf(stderr, "\n");
7883 }
7884 break;
7885 }
7886 case nir_intrinsic_mbcnt_amd: {
7887 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7888 RegClass rc = RegClass(src.type(), 1);
7889 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7890 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7891 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7892 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7893 emit_wqm(ctx, wqm_tmp, dst);
7894 break;
7895 }
7896 case nir_intrinsic_load_helper_invocation: {
7897 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7898 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7899 ctx->block->kind |= block_kind_needs_lowering;
7900 ctx->program->needs_exact = true;
7901 break;
7902 }
7903 case nir_intrinsic_is_helper_invocation: {
7904 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7905 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7906 ctx->block->kind |= block_kind_needs_lowering;
7907 ctx->program->needs_exact = true;
7908 break;
7909 }
7910 case nir_intrinsic_demote:
7911 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7912
7913 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7914 ctx->cf_info.exec_potentially_empty_discard = true;
7915 ctx->block->kind |= block_kind_uses_demote;
7916 ctx->program->needs_exact = true;
7917 break;
7918 case nir_intrinsic_demote_if: {
7919 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7920 assert(src.regClass() == bld.lm);
7921 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7922 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7923
7924 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7925 ctx->cf_info.exec_potentially_empty_discard = true;
7926 ctx->block->kind |= block_kind_uses_demote;
7927 ctx->program->needs_exact = true;
7928 break;
7929 }
7930 case nir_intrinsic_first_invocation: {
7931 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7932 get_ssa_temp(ctx, &instr->dest.ssa));
7933 break;
7934 }
7935 case nir_intrinsic_shader_clock:
7936 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7937 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7938 break;
7939 case nir_intrinsic_load_vertex_id_zero_base: {
7940 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7941 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7942 break;
7943 }
7944 case nir_intrinsic_load_first_vertex: {
7945 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7946 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7947 break;
7948 }
7949 case nir_intrinsic_load_base_instance: {
7950 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7951 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7952 break;
7953 }
7954 case nir_intrinsic_load_instance_id: {
7955 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7956 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7957 break;
7958 }
7959 case nir_intrinsic_load_draw_id: {
7960 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7961 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7962 break;
7963 }
7964 case nir_intrinsic_load_invocation_id: {
7965 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7966
7967 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7968 if (ctx->options->chip_class >= GFX10)
7969 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7970 else
7971 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7972 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7973 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7974 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7975 } else {
7976 unreachable("Unsupported stage for load_invocation_id");
7977 }
7978
7979 break;
7980 }
7981 case nir_intrinsic_load_primitive_id: {
7982 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7983
7984 switch (ctx->shader->info.stage) {
7985 case MESA_SHADER_GEOMETRY:
7986 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7987 break;
7988 case MESA_SHADER_TESS_CTRL:
7989 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7990 break;
7991 case MESA_SHADER_TESS_EVAL:
7992 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7993 break;
7994 default:
7995 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7996 }
7997
7998 break;
7999 }
8000 case nir_intrinsic_load_patch_vertices_in: {
8001 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8002 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8003
8004 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8005 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8006 break;
8007 }
8008 case nir_intrinsic_emit_vertex_with_counter: {
8009 visit_emit_vertex_with_counter(ctx, instr);
8010 break;
8011 }
8012 case nir_intrinsic_end_primitive_with_counter: {
8013 unsigned stream = nir_intrinsic_stream_id(instr);
8014 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8015 break;
8016 }
8017 case nir_intrinsic_set_vertex_count: {
8018 /* unused, the HW keeps track of this for us */
8019 break;
8020 }
8021 default:
8022 fprintf(stderr, "Unimplemented intrinsic instr: ");
8023 nir_print_instr(&instr->instr, stderr);
8024 fprintf(stderr, "\n");
8025 abort();
8026
8027 break;
8028 }
8029 }
8030
8031
8032 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8033 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8034 enum glsl_base_type *stype)
8035 {
8036 nir_deref_instr *texture_deref_instr = NULL;
8037 nir_deref_instr *sampler_deref_instr = NULL;
8038 int plane = -1;
8039
8040 for (unsigned i = 0; i < instr->num_srcs; i++) {
8041 switch (instr->src[i].src_type) {
8042 case nir_tex_src_texture_deref:
8043 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8044 break;
8045 case nir_tex_src_sampler_deref:
8046 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8047 break;
8048 case nir_tex_src_plane:
8049 plane = nir_src_as_int(instr->src[i].src);
8050 break;
8051 default:
8052 break;
8053 }
8054 }
8055
8056 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8057
8058 if (!sampler_deref_instr)
8059 sampler_deref_instr = texture_deref_instr;
8060
8061 if (plane >= 0) {
8062 assert(instr->op != nir_texop_txf_ms &&
8063 instr->op != nir_texop_samples_identical);
8064 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8065 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8066 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8067 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8068 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8069 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8070 } else {
8071 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8072 }
8073 if (samp_ptr) {
8074 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8075
8076 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8077 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8078 Builder bld(ctx->program, ctx->block);
8079
8080 /* to avoid unnecessary moves, we split and recombine sampler and image */
8081 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8082 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8083 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8084 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8085 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8086 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8087 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8088 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8089
8090 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8091 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8092 img[0], img[1], img[2], img[3],
8093 img[4], img[5], img[6], img[7]);
8094 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8095 samp[0], samp[1], samp[2], samp[3]);
8096 }
8097 }
8098 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8099 instr->op == nir_texop_samples_identical))
8100 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8101 }
8102
8103 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8104 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8105 {
8106 Builder bld(ctx->program, ctx->block);
8107
8108 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8109 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8110 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8111
8112 Operand neg_one(0xbf800000u);
8113 Operand one(0x3f800000u);
8114 Operand two(0x40000000u);
8115 Operand four(0x40800000u);
8116
8117 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8118 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8119 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8120
8121 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8122 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8123 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8124 Temp is_not_ma_x = bld.sop2(aco_opcode::s_or_b64, bld.hint_vcc(bld.def(bld.lm)), bld.def(s1, scc), is_ma_z, is_ma_y);
8125
8126 // select sc
8127 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8128 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8129 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8130 one, is_ma_y);
8131 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8132
8133 // select tc
8134 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8135 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8136 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8137
8138 // select ma
8139 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8140 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8141 deriv_z, is_ma_z);
8142 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8143 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8144 }
8145
8146 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8147 {
8148 Builder bld(ctx->program, ctx->block);
8149 Temp ma, tc, sc, id;
8150
8151 if (is_array) {
8152 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8153
8154 // see comment in ac_prepare_cube_coords()
8155 if (ctx->options->chip_class <= GFX8)
8156 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8157 }
8158
8159 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8160
8161 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8162 vop3a->operands[0] = Operand(ma);
8163 vop3a->abs[0] = true;
8164 Temp invma = bld.tmp(v1);
8165 vop3a->definitions[0] = Definition(invma);
8166 ctx->block->instructions.emplace_back(std::move(vop3a));
8167
8168 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8169 if (!is_deriv)
8170 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8171
8172 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8173 if (!is_deriv)
8174 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8175
8176 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8177
8178 if (is_deriv) {
8179 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8180 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8181
8182 for (unsigned i = 0; i < 2; i++) {
8183 // see comment in ac_prepare_cube_coords()
8184 Temp deriv_ma;
8185 Temp deriv_sc, deriv_tc;
8186 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8187 &deriv_ma, &deriv_sc, &deriv_tc);
8188
8189 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8190
8191 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8192 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8193 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8194 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8195 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8196 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8197 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8198 }
8199
8200 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8201 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8202 }
8203
8204 if (is_array)
8205 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8206 coords.resize(3);
8207 coords[0] = sc;
8208 coords[1] = tc;
8209 coords[2] = id;
8210 }
8211
8212 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8213 {
8214 if (vec->parent_instr->type != nir_instr_type_alu)
8215 return;
8216 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8217 if (vec_instr->op != nir_op_vec(vec->num_components))
8218 return;
8219
8220 for (unsigned i = 0; i < vec->num_components; i++) {
8221 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8222 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8223 }
8224 }
8225
8226 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8227 {
8228 Builder bld(ctx->program, ctx->block);
8229 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8230 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8231 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8232 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8233 std::vector<Temp> coords;
8234 std::vector<Temp> derivs;
8235 nir_const_value *sample_index_cv = NULL;
8236 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8237 enum glsl_base_type stype;
8238 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8239
8240 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8241 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8242 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8243 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8244
8245 for (unsigned i = 0; i < instr->num_srcs; i++) {
8246 switch (instr->src[i].src_type) {
8247 case nir_tex_src_coord: {
8248 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8249 for (unsigned i = 0; i < coord.size(); i++)
8250 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8251 break;
8252 }
8253 case nir_tex_src_bias:
8254 if (instr->op == nir_texop_txb) {
8255 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8256 has_bias = true;
8257 }
8258 break;
8259 case nir_tex_src_lod: {
8260 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8261
8262 if (val && val->f32 <= 0.0) {
8263 level_zero = true;
8264 } else {
8265 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8266 has_lod = true;
8267 }
8268 break;
8269 }
8270 case nir_tex_src_comparator:
8271 if (instr->is_shadow) {
8272 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8273 has_compare = true;
8274 }
8275 break;
8276 case nir_tex_src_offset:
8277 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8278 get_const_vec(instr->src[i].src.ssa, const_offset);
8279 has_offset = true;
8280 break;
8281 case nir_tex_src_ddx:
8282 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8283 has_ddx = true;
8284 break;
8285 case nir_tex_src_ddy:
8286 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8287 has_ddy = true;
8288 break;
8289 case nir_tex_src_ms_index:
8290 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8291 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8292 has_sample_index = true;
8293 break;
8294 case nir_tex_src_texture_offset:
8295 case nir_tex_src_sampler_offset:
8296 default:
8297 break;
8298 }
8299 }
8300
8301 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8302 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8303
8304 if (instr->op == nir_texop_texture_samples) {
8305 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8306
8307 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8308 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8309 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 */));
8310
8311 Operand default_sample = Operand(1u);
8312 if (ctx->options->robust_buffer_access) {
8313 /* Extract the second dword of the descriptor, if it's
8314 * all zero, then it's a null descriptor.
8315 */
8316 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8317 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8318 default_sample = Operand(is_non_null_descriptor);
8319 }
8320
8321 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8322 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8323 samples, default_sample, bld.scc(is_msaa));
8324 return;
8325 }
8326
8327 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8328 aco_ptr<Instruction> tmp_instr;
8329 Temp acc, pack = Temp();
8330
8331 uint32_t pack_const = 0;
8332 for (unsigned i = 0; i < offset.size(); i++) {
8333 if (!const_offset[i])
8334 continue;
8335 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8336 }
8337
8338 if (offset.type() == RegType::sgpr) {
8339 for (unsigned i = 0; i < offset.size(); i++) {
8340 if (const_offset[i])
8341 continue;
8342
8343 acc = emit_extract_vector(ctx, offset, i, s1);
8344 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8345
8346 if (i) {
8347 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8348 }
8349
8350 if (pack == Temp()) {
8351 pack = acc;
8352 } else {
8353 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8354 }
8355 }
8356
8357 if (pack_const && pack != Temp())
8358 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8359 } else {
8360 for (unsigned i = 0; i < offset.size(); i++) {
8361 if (const_offset[i])
8362 continue;
8363
8364 acc = emit_extract_vector(ctx, offset, i, v1);
8365 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8366
8367 if (i) {
8368 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8369 }
8370
8371 if (pack == Temp()) {
8372 pack = acc;
8373 } else {
8374 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8375 }
8376 }
8377
8378 if (pack_const && pack != Temp())
8379 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8380 }
8381 if (pack_const && pack == Temp())
8382 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8383 else if (pack == Temp())
8384 has_offset = false;
8385 else
8386 offset = pack;
8387 }
8388
8389 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8390 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8391
8392 /* pack derivatives */
8393 if (has_ddx || has_ddy) {
8394 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8395 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8396 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8397 derivs = {ddx, zero, ddy, zero};
8398 } else {
8399 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8400 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8401 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8402 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8403 }
8404 has_derivs = true;
8405 }
8406
8407 if (instr->coord_components > 1 &&
8408 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8409 instr->is_array &&
8410 instr->op != nir_texop_txf)
8411 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8412
8413 if (instr->coord_components > 2 &&
8414 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8415 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8416 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8417 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8418 instr->is_array &&
8419 instr->op != nir_texop_txf &&
8420 instr->op != nir_texop_txf_ms &&
8421 instr->op != nir_texop_fragment_fetch &&
8422 instr->op != nir_texop_fragment_mask_fetch)
8423 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8424
8425 if (ctx->options->chip_class == GFX9 &&
8426 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8427 instr->op != nir_texop_lod && instr->coord_components) {
8428 assert(coords.size() > 0 && coords.size() < 3);
8429
8430 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8431 Operand((uint32_t) 0) :
8432 Operand((uint32_t) 0x3f000000)));
8433 }
8434
8435 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8436
8437 if (instr->op == nir_texop_samples_identical)
8438 resource = fmask_ptr;
8439
8440 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8441 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8442 instr->op != nir_texop_txs &&
8443 instr->op != nir_texop_fragment_fetch &&
8444 instr->op != nir_texop_fragment_mask_fetch) {
8445 assert(has_sample_index);
8446 Operand op(sample_index);
8447 if (sample_index_cv)
8448 op = Operand(sample_index_cv->u32);
8449 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8450 }
8451
8452 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8453 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8454 Temp off = emit_extract_vector(ctx, offset, i, v1);
8455 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8456 }
8457 has_offset = false;
8458 }
8459
8460 /* Build tex instruction */
8461 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8462 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8463 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8464 : 0;
8465 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8466 Temp tmp_dst = dst;
8467
8468 /* gather4 selects the component by dmask and always returns vec4 */
8469 if (instr->op == nir_texop_tg4) {
8470 assert(instr->dest.ssa.num_components == 4);
8471 if (instr->is_shadow)
8472 dmask = 1;
8473 else
8474 dmask = 1 << instr->component;
8475 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8476 tmp_dst = bld.tmp(v4);
8477 } else if (instr->op == nir_texop_samples_identical) {
8478 tmp_dst = bld.tmp(v1);
8479 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8480 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8481 }
8482
8483 aco_ptr<MIMG_instruction> tex;
8484 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8485 if (!has_lod)
8486 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8487
8488 bool div_by_6 = instr->op == nir_texop_txs &&
8489 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8490 instr->is_array &&
8491 (dmask & (1 << 2));
8492 if (tmp_dst.id() == dst.id() && div_by_6)
8493 tmp_dst = bld.tmp(tmp_dst.regClass());
8494
8495 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8496 tex->operands[0] = Operand(resource);
8497 tex->operands[1] = Operand(s4); /* no sampler */
8498 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8499 if (ctx->options->chip_class == GFX9 &&
8500 instr->op == nir_texop_txs &&
8501 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8502 instr->is_array) {
8503 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8504 } else if (instr->op == nir_texop_query_levels) {
8505 tex->dmask = 1 << 3;
8506 } else {
8507 tex->dmask = dmask;
8508 }
8509 tex->da = da;
8510 tex->definitions[0] = Definition(tmp_dst);
8511 tex->dim = dim;
8512 tex->can_reorder = true;
8513 ctx->block->instructions.emplace_back(std::move(tex));
8514
8515 if (div_by_6) {
8516 /* divide 3rd value by 6 by multiplying with magic number */
8517 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8518 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8519 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8520 assert(instr->dest.ssa.num_components == 3);
8521 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8522 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8523 emit_extract_vector(ctx, tmp_dst, 0, v1),
8524 emit_extract_vector(ctx, tmp_dst, 1, v1),
8525 by_6);
8526
8527 }
8528
8529 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8530 return;
8531 }
8532
8533 Temp tg4_compare_cube_wa64 = Temp();
8534
8535 if (tg4_integer_workarounds) {
8536 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8537 tex->operands[0] = Operand(resource);
8538 tex->operands[1] = Operand(s4); /* no sampler */
8539 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8540 tex->dim = dim;
8541 tex->dmask = 0x3;
8542 tex->da = da;
8543 Temp size = bld.tmp(v2);
8544 tex->definitions[0] = Definition(size);
8545 tex->can_reorder = true;
8546 ctx->block->instructions.emplace_back(std::move(tex));
8547 emit_split_vector(ctx, size, size.size());
8548
8549 Temp half_texel[2];
8550 for (unsigned i = 0; i < 2; i++) {
8551 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8552 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8553 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8554 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8555 }
8556
8557 Temp new_coords[2] = {
8558 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8559 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8560 };
8561
8562 if (tg4_integer_cube_workaround) {
8563 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8564 Temp desc[resource.size()];
8565 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8566 Format::PSEUDO, 1, resource.size())};
8567 split->operands[0] = Operand(resource);
8568 for (unsigned i = 0; i < resource.size(); i++) {
8569 desc[i] = bld.tmp(s1);
8570 split->definitions[i] = Definition(desc[i]);
8571 }
8572 ctx->block->instructions.emplace_back(std::move(split));
8573
8574 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8575 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8576 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8577
8578 Temp nfmt;
8579 if (stype == GLSL_TYPE_UINT) {
8580 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8581 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8582 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8583 bld.scc(compare_cube_wa));
8584 } else {
8585 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8586 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8587 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8588 bld.scc(compare_cube_wa));
8589 }
8590 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8591 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8592
8593 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8594
8595 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8596 Operand((uint32_t)C_008F14_NUM_FORMAT));
8597 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8598
8599 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8600 Format::PSEUDO, resource.size(), 1)};
8601 for (unsigned i = 0; i < resource.size(); i++)
8602 vec->operands[i] = Operand(desc[i]);
8603 resource = bld.tmp(resource.regClass());
8604 vec->definitions[0] = Definition(resource);
8605 ctx->block->instructions.emplace_back(std::move(vec));
8606
8607 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8608 new_coords[0], coords[0], tg4_compare_cube_wa64);
8609 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8610 new_coords[1], coords[1], tg4_compare_cube_wa64);
8611 }
8612 coords[0] = new_coords[0];
8613 coords[1] = new_coords[1];
8614 }
8615
8616 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8617 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8618
8619 assert(coords.size() == 1);
8620 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8621 aco_opcode op;
8622 switch (last_bit) {
8623 case 1:
8624 op = aco_opcode::buffer_load_format_x; break;
8625 case 2:
8626 op = aco_opcode::buffer_load_format_xy; break;
8627 case 3:
8628 op = aco_opcode::buffer_load_format_xyz; break;
8629 case 4:
8630 op = aco_opcode::buffer_load_format_xyzw; break;
8631 default:
8632 unreachable("Tex instruction loads more than 4 components.");
8633 }
8634
8635 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8636 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8637 tmp_dst = dst;
8638 else
8639 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8640
8641 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8642 mubuf->operands[0] = Operand(resource);
8643 mubuf->operands[1] = Operand(coords[0]);
8644 mubuf->operands[2] = Operand((uint32_t) 0);
8645 mubuf->definitions[0] = Definition(tmp_dst);
8646 mubuf->idxen = true;
8647 mubuf->can_reorder = true;
8648 ctx->block->instructions.emplace_back(std::move(mubuf));
8649
8650 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8651 return;
8652 }
8653
8654 /* gather MIMG address components */
8655 std::vector<Temp> args;
8656 if (has_offset)
8657 args.emplace_back(offset);
8658 if (has_bias)
8659 args.emplace_back(bias);
8660 if (has_compare)
8661 args.emplace_back(compare);
8662 if (has_derivs)
8663 args.insert(args.end(), derivs.begin(), derivs.end());
8664
8665 args.insert(args.end(), coords.begin(), coords.end());
8666 if (has_sample_index)
8667 args.emplace_back(sample_index);
8668 if (has_lod)
8669 args.emplace_back(lod);
8670
8671 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8672 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8673 vec->definitions[0] = Definition(arg);
8674 for (unsigned i = 0; i < args.size(); i++)
8675 vec->operands[i] = Operand(args[i]);
8676 ctx->block->instructions.emplace_back(std::move(vec));
8677
8678
8679 if (instr->op == nir_texop_txf ||
8680 instr->op == nir_texop_txf_ms ||
8681 instr->op == nir_texop_samples_identical ||
8682 instr->op == nir_texop_fragment_fetch ||
8683 instr->op == nir_texop_fragment_mask_fetch) {
8684 aco_opcode op = level_zero || instr->sampler_dim == GLSL_SAMPLER_DIM_MS || instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS ? aco_opcode::image_load : aco_opcode::image_load_mip;
8685 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8686 tex->operands[0] = Operand(resource);
8687 tex->operands[1] = Operand(s4); /* no sampler */
8688 tex->operands[2] = Operand(arg);
8689 tex->dim = dim;
8690 tex->dmask = dmask;
8691 tex->unrm = true;
8692 tex->da = da;
8693 tex->definitions[0] = Definition(tmp_dst);
8694 tex->can_reorder = true;
8695 ctx->block->instructions.emplace_back(std::move(tex));
8696
8697 if (instr->op == nir_texop_samples_identical) {
8698 assert(dmask == 1 && dst.regClass() == v1);
8699 assert(dst.id() != tmp_dst.id());
8700
8701 Temp tmp = bld.tmp(bld.lm);
8702 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8703 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8704
8705 } else {
8706 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8707 }
8708 return;
8709 }
8710
8711 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8712 aco_opcode opcode = aco_opcode::image_sample;
8713 if (has_offset) { /* image_sample_*_o */
8714 if (has_compare) {
8715 opcode = aco_opcode::image_sample_c_o;
8716 if (has_derivs)
8717 opcode = aco_opcode::image_sample_c_d_o;
8718 if (has_bias)
8719 opcode = aco_opcode::image_sample_c_b_o;
8720 if (level_zero)
8721 opcode = aco_opcode::image_sample_c_lz_o;
8722 if (has_lod)
8723 opcode = aco_opcode::image_sample_c_l_o;
8724 } else {
8725 opcode = aco_opcode::image_sample_o;
8726 if (has_derivs)
8727 opcode = aco_opcode::image_sample_d_o;
8728 if (has_bias)
8729 opcode = aco_opcode::image_sample_b_o;
8730 if (level_zero)
8731 opcode = aco_opcode::image_sample_lz_o;
8732 if (has_lod)
8733 opcode = aco_opcode::image_sample_l_o;
8734 }
8735 } else { /* no offset */
8736 if (has_compare) {
8737 opcode = aco_opcode::image_sample_c;
8738 if (has_derivs)
8739 opcode = aco_opcode::image_sample_c_d;
8740 if (has_bias)
8741 opcode = aco_opcode::image_sample_c_b;
8742 if (level_zero)
8743 opcode = aco_opcode::image_sample_c_lz;
8744 if (has_lod)
8745 opcode = aco_opcode::image_sample_c_l;
8746 } else {
8747 opcode = aco_opcode::image_sample;
8748 if (has_derivs)
8749 opcode = aco_opcode::image_sample_d;
8750 if (has_bias)
8751 opcode = aco_opcode::image_sample_b;
8752 if (level_zero)
8753 opcode = aco_opcode::image_sample_lz;
8754 if (has_lod)
8755 opcode = aco_opcode::image_sample_l;
8756 }
8757 }
8758
8759 if (instr->op == nir_texop_tg4) {
8760 if (has_offset) {
8761 opcode = aco_opcode::image_gather4_lz_o;
8762 if (has_compare)
8763 opcode = aco_opcode::image_gather4_c_lz_o;
8764 } else {
8765 opcode = aco_opcode::image_gather4_lz;
8766 if (has_compare)
8767 opcode = aco_opcode::image_gather4_c_lz;
8768 }
8769 } else if (instr->op == nir_texop_lod) {
8770 opcode = aco_opcode::image_get_lod;
8771 }
8772
8773 /* we don't need the bias, sample index, compare value or offset to be
8774 * computed in WQM but if the p_create_vector copies the coordinates, then it
8775 * needs to be in WQM */
8776 if (ctx->stage == fragment_fs &&
8777 !has_derivs && !has_lod && !level_zero &&
8778 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8779 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8780 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8781
8782 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8783 tex->operands[0] = Operand(resource);
8784 tex->operands[1] = Operand(sampler);
8785 tex->operands[2] = Operand(arg);
8786 tex->dim = dim;
8787 tex->dmask = dmask;
8788 tex->da = da;
8789 tex->definitions[0] = Definition(tmp_dst);
8790 tex->can_reorder = true;
8791 ctx->block->instructions.emplace_back(std::move(tex));
8792
8793 if (tg4_integer_cube_workaround) {
8794 assert(tmp_dst.id() != dst.id());
8795 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8796
8797 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8798 Temp val[4];
8799 for (unsigned i = 0; i < dst.size(); i++) {
8800 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8801 Temp cvt_val;
8802 if (stype == GLSL_TYPE_UINT)
8803 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8804 else
8805 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8806 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8807 }
8808 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8809 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8810 val[0], val[1], val[2], val[3]);
8811 }
8812 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8813 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8814
8815 }
8816
8817
8818 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8819 {
8820 Temp tmp = get_ssa_temp(ctx, ssa);
8821 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8822 return Operand(tmp.regClass());
8823 else
8824 return Operand(tmp);
8825 }
8826
8827 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8828 {
8829 aco_ptr<Pseudo_instruction> phi;
8830 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8831 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8832
8833 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8834 logical |= ctx->block->kind & block_kind_merge;
8835 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8836
8837 /* we want a sorted list of sources, since the predecessor list is also sorted */
8838 std::map<unsigned, nir_ssa_def*> phi_src;
8839 nir_foreach_phi_src(src, instr)
8840 phi_src[src->pred->index] = src->src.ssa;
8841
8842 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8843 unsigned num_operands = 0;
8844 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8845 unsigned num_defined = 0;
8846 unsigned cur_pred_idx = 0;
8847 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8848 if (cur_pred_idx < preds.size()) {
8849 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8850 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8851 unsigned skipped = 0;
8852 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8853 skipped++;
8854 if (cur_pred_idx + skipped < preds.size()) {
8855 for (unsigned i = 0; i < skipped; i++)
8856 operands[num_operands++] = Operand(dst.regClass());
8857 cur_pred_idx += skipped;
8858 } else {
8859 continue;
8860 }
8861 }
8862 /* Handle missing predecessors at the end. This shouldn't happen with loop
8863 * headers and we can't ignore these sources for loop header phis. */
8864 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8865 continue;
8866 cur_pred_idx++;
8867 Operand op = get_phi_operand(ctx, src.second);
8868 operands[num_operands++] = op;
8869 num_defined += !op.isUndefined();
8870 }
8871 /* handle block_kind_continue_or_break at loop exit blocks */
8872 while (cur_pred_idx++ < preds.size())
8873 operands[num_operands++] = Operand(dst.regClass());
8874
8875 /* If the loop ends with a break, still add a linear continue edge in case
8876 * that break is divergent or continue_or_break is used. We'll either remove
8877 * this operand later in visit_loop() if it's not necessary or replace the
8878 * undef with something correct. */
8879 if (!logical && ctx->block->kind & block_kind_loop_header) {
8880 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8881 nir_block *last = nir_loop_last_block(loop);
8882 if (last->successors[0] != instr->instr.block)
8883 operands[num_operands++] = Operand(RegClass());
8884 }
8885
8886 if (num_defined == 0) {
8887 Builder bld(ctx->program, ctx->block);
8888 if (dst.regClass() == s1) {
8889 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8890 } else if (dst.regClass() == v1) {
8891 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8892 } else {
8893 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8894 for (unsigned i = 0; i < dst.size(); i++)
8895 vec->operands[i] = Operand(0u);
8896 vec->definitions[0] = Definition(dst);
8897 ctx->block->instructions.emplace_back(std::move(vec));
8898 }
8899 return;
8900 }
8901
8902 /* we can use a linear phi in some cases if one src is undef */
8903 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8904 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8905
8906 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8907 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8908 assert(invert->kind & block_kind_invert);
8909
8910 unsigned then_block = invert->linear_preds[0];
8911
8912 Block* insert_block = NULL;
8913 for (unsigned i = 0; i < num_operands; i++) {
8914 Operand op = operands[i];
8915 if (op.isUndefined())
8916 continue;
8917 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8918 phi->operands[0] = op;
8919 break;
8920 }
8921 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8922 phi->operands[1] = Operand(dst.regClass());
8923 phi->definitions[0] = Definition(dst);
8924 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8925 return;
8926 }
8927
8928 /* try to scalarize vector phis */
8929 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8930 // TODO: scalarize linear phis on divergent ifs
8931 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8932 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8933 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8934 Operand src = operands[i];
8935 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8936 can_scalarize = false;
8937 }
8938 if (can_scalarize) {
8939 unsigned num_components = instr->dest.ssa.num_components;
8940 assert(dst.size() % num_components == 0);
8941 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8942
8943 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8944 for (unsigned k = 0; k < num_components; k++) {
8945 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8946 for (unsigned i = 0; i < num_operands; i++) {
8947 Operand src = operands[i];
8948 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8949 }
8950 Temp phi_dst = {ctx->program->allocateId(), rc};
8951 phi->definitions[0] = Definition(phi_dst);
8952 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8953 new_vec[k] = phi_dst;
8954 vec->operands[k] = Operand(phi_dst);
8955 }
8956 vec->definitions[0] = Definition(dst);
8957 ctx->block->instructions.emplace_back(std::move(vec));
8958 ctx->allocated_vec.emplace(dst.id(), new_vec);
8959 return;
8960 }
8961 }
8962
8963 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8964 for (unsigned i = 0; i < num_operands; i++)
8965 phi->operands[i] = operands[i];
8966 phi->definitions[0] = Definition(dst);
8967 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8968 }
8969
8970
8971 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8972 {
8973 Temp dst = get_ssa_temp(ctx, &instr->def);
8974
8975 assert(dst.type() == RegType::sgpr);
8976
8977 if (dst.size() == 1) {
8978 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8979 } else {
8980 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8981 for (unsigned i = 0; i < dst.size(); i++)
8982 vec->operands[i] = Operand(0u);
8983 vec->definitions[0] = Definition(dst);
8984 ctx->block->instructions.emplace_back(std::move(vec));
8985 }
8986 }
8987
8988 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8989 {
8990 Builder bld(ctx->program, ctx->block);
8991 Block *logical_target;
8992 append_logical_end(ctx->block);
8993 unsigned idx = ctx->block->index;
8994
8995 switch (instr->type) {
8996 case nir_jump_break:
8997 logical_target = ctx->cf_info.parent_loop.exit;
8998 add_logical_edge(idx, logical_target);
8999 ctx->block->kind |= block_kind_break;
9000
9001 if (!ctx->cf_info.parent_if.is_divergent &&
9002 !ctx->cf_info.parent_loop.has_divergent_continue) {
9003 /* uniform break - directly jump out of the loop */
9004 ctx->block->kind |= block_kind_uniform;
9005 ctx->cf_info.has_branch = true;
9006 bld.branch(aco_opcode::p_branch);
9007 add_linear_edge(idx, logical_target);
9008 return;
9009 }
9010 ctx->cf_info.parent_loop.has_divergent_branch = true;
9011 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9012 break;
9013 case nir_jump_continue:
9014 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9015 add_logical_edge(idx, logical_target);
9016 ctx->block->kind |= block_kind_continue;
9017
9018 if (ctx->cf_info.parent_if.is_divergent) {
9019 /* for potential uniform breaks after this continue,
9020 we must ensure that they are handled correctly */
9021 ctx->cf_info.parent_loop.has_divergent_continue = true;
9022 ctx->cf_info.parent_loop.has_divergent_branch = true;
9023 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9024 } else {
9025 /* uniform continue - directly jump to the loop header */
9026 ctx->block->kind |= block_kind_uniform;
9027 ctx->cf_info.has_branch = true;
9028 bld.branch(aco_opcode::p_branch);
9029 add_linear_edge(idx, logical_target);
9030 return;
9031 }
9032 break;
9033 default:
9034 fprintf(stderr, "Unknown NIR jump instr: ");
9035 nir_print_instr(&instr->instr, stderr);
9036 fprintf(stderr, "\n");
9037 abort();
9038 }
9039
9040 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9041 ctx->cf_info.exec_potentially_empty_break = true;
9042 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9043 }
9044
9045 /* remove critical edges from linear CFG */
9046 bld.branch(aco_opcode::p_branch);
9047 Block* break_block = ctx->program->create_and_insert_block();
9048 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9049 break_block->kind |= block_kind_uniform;
9050 add_linear_edge(idx, break_block);
9051 /* the loop_header pointer might be invalidated by this point */
9052 if (instr->type == nir_jump_continue)
9053 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9054 add_linear_edge(break_block->index, logical_target);
9055 bld.reset(break_block);
9056 bld.branch(aco_opcode::p_branch);
9057
9058 Block* continue_block = ctx->program->create_and_insert_block();
9059 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9060 add_linear_edge(idx, continue_block);
9061 append_logical_start(continue_block);
9062 ctx->block = continue_block;
9063 return;
9064 }
9065
9066 void visit_block(isel_context *ctx, nir_block *block)
9067 {
9068 nir_foreach_instr(instr, block) {
9069 switch (instr->type) {
9070 case nir_instr_type_alu:
9071 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9072 break;
9073 case nir_instr_type_load_const:
9074 visit_load_const(ctx, nir_instr_as_load_const(instr));
9075 break;
9076 case nir_instr_type_intrinsic:
9077 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9078 break;
9079 case nir_instr_type_tex:
9080 visit_tex(ctx, nir_instr_as_tex(instr));
9081 break;
9082 case nir_instr_type_phi:
9083 visit_phi(ctx, nir_instr_as_phi(instr));
9084 break;
9085 case nir_instr_type_ssa_undef:
9086 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9087 break;
9088 case nir_instr_type_deref:
9089 break;
9090 case nir_instr_type_jump:
9091 visit_jump(ctx, nir_instr_as_jump(instr));
9092 break;
9093 default:
9094 fprintf(stderr, "Unknown NIR instr type: ");
9095 nir_print_instr(instr, stderr);
9096 fprintf(stderr, "\n");
9097 //abort();
9098 }
9099 }
9100
9101 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9102 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9103 }
9104
9105
9106
9107 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9108 aco_ptr<Instruction>& header_phi, Operand *vals)
9109 {
9110 vals[0] = Operand(header_phi->definitions[0].getTemp());
9111 RegClass rc = vals[0].regClass();
9112
9113 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9114
9115 unsigned next_pred = 1;
9116
9117 for (unsigned idx = first + 1; idx <= last; idx++) {
9118 Block& block = ctx->program->blocks[idx];
9119 if (block.loop_nest_depth != loop_nest_depth) {
9120 vals[idx - first] = vals[idx - 1 - first];
9121 continue;
9122 }
9123
9124 if (block.kind & block_kind_continue) {
9125 vals[idx - first] = header_phi->operands[next_pred];
9126 next_pred++;
9127 continue;
9128 }
9129
9130 bool all_same = true;
9131 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9132 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9133
9134 Operand val;
9135 if (all_same) {
9136 val = vals[block.linear_preds[0] - first];
9137 } else {
9138 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9139 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9140 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9141 phi->operands[i] = vals[block.linear_preds[i] - first];
9142 val = Operand(Temp(ctx->program->allocateId(), rc));
9143 phi->definitions[0] = Definition(val.getTemp());
9144 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9145 }
9146 vals[idx - first] = val;
9147 }
9148
9149 return vals[last - first];
9150 }
9151
9152 static void visit_loop(isel_context *ctx, nir_loop *loop)
9153 {
9154 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9155 append_logical_end(ctx->block);
9156 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9157 Builder bld(ctx->program, ctx->block);
9158 bld.branch(aco_opcode::p_branch);
9159 unsigned loop_preheader_idx = ctx->block->index;
9160
9161 Block loop_exit = Block();
9162 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9163 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9164
9165 Block* loop_header = ctx->program->create_and_insert_block();
9166 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9167 loop_header->kind |= block_kind_loop_header;
9168 add_edge(loop_preheader_idx, loop_header);
9169 ctx->block = loop_header;
9170
9171 /* emit loop body */
9172 unsigned loop_header_idx = loop_header->index;
9173 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9174 append_logical_start(ctx->block);
9175 bool unreachable = visit_cf_list(ctx, &loop->body);
9176
9177 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9178 if (!ctx->cf_info.has_branch) {
9179 append_logical_end(ctx->block);
9180 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9181 /* Discards can result in code running with an empty exec mask.
9182 * This would result in divergent breaks not ever being taken. As a
9183 * workaround, break the loop when the loop mask is empty instead of
9184 * always continuing. */
9185 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9186 unsigned block_idx = ctx->block->index;
9187
9188 /* create helper blocks to avoid critical edges */
9189 Block *break_block = ctx->program->create_and_insert_block();
9190 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9191 break_block->kind = block_kind_uniform;
9192 bld.reset(break_block);
9193 bld.branch(aco_opcode::p_branch);
9194 add_linear_edge(block_idx, break_block);
9195 add_linear_edge(break_block->index, &loop_exit);
9196
9197 Block *continue_block = ctx->program->create_and_insert_block();
9198 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9199 continue_block->kind = block_kind_uniform;
9200 bld.reset(continue_block);
9201 bld.branch(aco_opcode::p_branch);
9202 add_linear_edge(block_idx, continue_block);
9203 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9204
9205 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9206 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9207 ctx->block = &ctx->program->blocks[block_idx];
9208 } else {
9209 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9210 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9211 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9212 else
9213 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9214 }
9215
9216 bld.reset(ctx->block);
9217 bld.branch(aco_opcode::p_branch);
9218 }
9219
9220 /* Fixup phis in loop header from unreachable blocks.
9221 * has_branch/has_divergent_branch also indicates if the loop ends with a
9222 * break/continue instruction, but we don't emit those if unreachable=true */
9223 if (unreachable) {
9224 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9225 bool linear = ctx->cf_info.has_branch;
9226 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9227 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9228 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9229 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9230 /* the last operand should be the one that needs to be removed */
9231 instr->operands.pop_back();
9232 } else if (!is_phi(instr)) {
9233 break;
9234 }
9235 }
9236 }
9237
9238 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9239 * and the previous one shouldn't both happen at once because a break in the
9240 * merge block would get CSE'd */
9241 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9242 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9243 Operand vals[num_vals];
9244 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9245 if (instr->opcode == aco_opcode::p_linear_phi) {
9246 if (ctx->cf_info.has_branch)
9247 instr->operands.pop_back();
9248 else
9249 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9250 } else if (!is_phi(instr)) {
9251 break;
9252 }
9253 }
9254 }
9255
9256 ctx->cf_info.has_branch = false;
9257
9258 // TODO: if the loop has not a single exit, we must add one °°
9259 /* emit loop successor block */
9260 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9261 append_logical_start(ctx->block);
9262
9263 #if 0
9264 // TODO: check if it is beneficial to not branch on continues
9265 /* trim linear phis in loop header */
9266 for (auto&& instr : loop_entry->instructions) {
9267 if (instr->opcode == aco_opcode::p_linear_phi) {
9268 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9269 new_phi->definitions[0] = instr->definitions[0];
9270 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9271 new_phi->operands[i] = instr->operands[i];
9272 /* check that the remaining operands are all the same */
9273 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9274 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9275 instr.swap(new_phi);
9276 } else if (instr->opcode == aco_opcode::p_phi) {
9277 continue;
9278 } else {
9279 break;
9280 }
9281 }
9282 #endif
9283 }
9284
9285 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9286 {
9287 ic->cond = cond;
9288
9289 append_logical_end(ctx->block);
9290 ctx->block->kind |= block_kind_branch;
9291
9292 /* branch to linear then block */
9293 assert(cond.regClass() == ctx->program->lane_mask);
9294 aco_ptr<Pseudo_branch_instruction> branch;
9295 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9296 branch->operands[0] = Operand(cond);
9297 ctx->block->instructions.push_back(std::move(branch));
9298
9299 ic->BB_if_idx = ctx->block->index;
9300 ic->BB_invert = Block();
9301 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9302 /* Invert blocks are intentionally not marked as top level because they
9303 * are not part of the logical cfg. */
9304 ic->BB_invert.kind |= block_kind_invert;
9305 ic->BB_endif = Block();
9306 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9307 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9308
9309 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9310 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9311 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9312 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9313 ctx->cf_info.parent_if.is_divergent = true;
9314
9315 /* divergent branches use cbranch_execz */
9316 ctx->cf_info.exec_potentially_empty_discard = false;
9317 ctx->cf_info.exec_potentially_empty_break = false;
9318 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9319
9320 /** emit logical then block */
9321 Block* BB_then_logical = ctx->program->create_and_insert_block();
9322 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9323 add_edge(ic->BB_if_idx, BB_then_logical);
9324 ctx->block = BB_then_logical;
9325 append_logical_start(BB_then_logical);
9326 }
9327
9328 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9329 {
9330 Block *BB_then_logical = ctx->block;
9331 append_logical_end(BB_then_logical);
9332 /* branch from logical then block to invert block */
9333 aco_ptr<Pseudo_branch_instruction> branch;
9334 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9335 BB_then_logical->instructions.emplace_back(std::move(branch));
9336 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9337 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9338 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9339 BB_then_logical->kind |= block_kind_uniform;
9340 assert(!ctx->cf_info.has_branch);
9341 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9342 ctx->cf_info.parent_loop.has_divergent_branch = false;
9343
9344 /** emit linear then block */
9345 Block* BB_then_linear = ctx->program->create_and_insert_block();
9346 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9347 BB_then_linear->kind |= block_kind_uniform;
9348 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9349 /* branch from linear then block to invert block */
9350 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9351 BB_then_linear->instructions.emplace_back(std::move(branch));
9352 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9353
9354 /** emit invert merge block */
9355 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9356 ic->invert_idx = ctx->block->index;
9357
9358 /* branch to linear else block (skip else) */
9359 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9360 branch->operands[0] = Operand(ic->cond);
9361 ctx->block->instructions.push_back(std::move(branch));
9362
9363 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9364 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9365 ic->exec_potentially_empty_break_depth_old =
9366 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9367 /* divergent branches use cbranch_execz */
9368 ctx->cf_info.exec_potentially_empty_discard = false;
9369 ctx->cf_info.exec_potentially_empty_break = false;
9370 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9371
9372 /** emit logical else block */
9373 Block* BB_else_logical = ctx->program->create_and_insert_block();
9374 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9375 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9376 add_linear_edge(ic->invert_idx, BB_else_logical);
9377 ctx->block = BB_else_logical;
9378 append_logical_start(BB_else_logical);
9379 }
9380
9381 static void end_divergent_if(isel_context *ctx, if_context *ic)
9382 {
9383 Block *BB_else_logical = ctx->block;
9384 append_logical_end(BB_else_logical);
9385
9386 /* branch from logical else block to endif block */
9387 aco_ptr<Pseudo_branch_instruction> branch;
9388 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9389 BB_else_logical->instructions.emplace_back(std::move(branch));
9390 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9391 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9392 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9393 BB_else_logical->kind |= block_kind_uniform;
9394
9395 assert(!ctx->cf_info.has_branch);
9396 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9397
9398
9399 /** emit linear else block */
9400 Block* BB_else_linear = ctx->program->create_and_insert_block();
9401 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9402 BB_else_linear->kind |= block_kind_uniform;
9403 add_linear_edge(ic->invert_idx, BB_else_linear);
9404
9405 /* branch from linear else block to endif block */
9406 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9407 BB_else_linear->instructions.emplace_back(std::move(branch));
9408 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9409
9410
9411 /** emit endif merge block */
9412 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9413 append_logical_start(ctx->block);
9414
9415
9416 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9417 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9418 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9419 ctx->cf_info.exec_potentially_empty_break_depth =
9420 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9421 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9422 !ctx->cf_info.parent_if.is_divergent) {
9423 ctx->cf_info.exec_potentially_empty_break = false;
9424 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9425 }
9426 /* uniform control flow never has an empty exec-mask */
9427 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9428 ctx->cf_info.exec_potentially_empty_discard = false;
9429 ctx->cf_info.exec_potentially_empty_break = false;
9430 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9431 }
9432 }
9433
9434 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9435 {
9436 assert(cond.regClass() == s1);
9437
9438 append_logical_end(ctx->block);
9439 ctx->block->kind |= block_kind_uniform;
9440
9441 aco_ptr<Pseudo_branch_instruction> branch;
9442 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9443 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9444 branch->operands[0] = Operand(cond);
9445 branch->operands[0].setFixed(scc);
9446 ctx->block->instructions.emplace_back(std::move(branch));
9447
9448 ic->BB_if_idx = ctx->block->index;
9449 ic->BB_endif = Block();
9450 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9451 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9452
9453 ctx->cf_info.has_branch = false;
9454 ctx->cf_info.parent_loop.has_divergent_branch = false;
9455
9456 /** emit then block */
9457 Block* BB_then = ctx->program->create_and_insert_block();
9458 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9459 add_edge(ic->BB_if_idx, BB_then);
9460 append_logical_start(BB_then);
9461 ctx->block = BB_then;
9462 }
9463
9464 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9465 {
9466 Block *BB_then = ctx->block;
9467
9468 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9469 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9470
9471 if (!ic->uniform_has_then_branch) {
9472 append_logical_end(BB_then);
9473 /* branch from then block to endif block */
9474 aco_ptr<Pseudo_branch_instruction> branch;
9475 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9476 BB_then->instructions.emplace_back(std::move(branch));
9477 add_linear_edge(BB_then->index, &ic->BB_endif);
9478 if (!ic->then_branch_divergent)
9479 add_logical_edge(BB_then->index, &ic->BB_endif);
9480 BB_then->kind |= block_kind_uniform;
9481 }
9482
9483 ctx->cf_info.has_branch = false;
9484 ctx->cf_info.parent_loop.has_divergent_branch = false;
9485
9486 /** emit else block */
9487 Block* BB_else = ctx->program->create_and_insert_block();
9488 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9489 add_edge(ic->BB_if_idx, BB_else);
9490 append_logical_start(BB_else);
9491 ctx->block = BB_else;
9492 }
9493
9494 static void end_uniform_if(isel_context *ctx, if_context *ic)
9495 {
9496 Block *BB_else = ctx->block;
9497
9498 if (!ctx->cf_info.has_branch) {
9499 append_logical_end(BB_else);
9500 /* branch from then block to endif block */
9501 aco_ptr<Pseudo_branch_instruction> branch;
9502 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9503 BB_else->instructions.emplace_back(std::move(branch));
9504 add_linear_edge(BB_else->index, &ic->BB_endif);
9505 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9506 add_logical_edge(BB_else->index, &ic->BB_endif);
9507 BB_else->kind |= block_kind_uniform;
9508 }
9509
9510 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9511 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9512
9513 /** emit endif merge block */
9514 if (!ctx->cf_info.has_branch) {
9515 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9516 append_logical_start(ctx->block);
9517 }
9518 }
9519
9520 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9521 {
9522 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9523 Builder bld(ctx->program, ctx->block);
9524 aco_ptr<Pseudo_branch_instruction> branch;
9525 if_context ic;
9526
9527 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9528 /**
9529 * Uniform conditionals are represented in the following way*) :
9530 *
9531 * The linear and logical CFG:
9532 * BB_IF
9533 * / \
9534 * BB_THEN (logical) BB_ELSE (logical)
9535 * \ /
9536 * BB_ENDIF
9537 *
9538 * *) Exceptions may be due to break and continue statements within loops
9539 * If a break/continue happens within uniform control flow, it branches
9540 * to the loop exit/entry block. Otherwise, it branches to the next
9541 * merge block.
9542 **/
9543
9544 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9545 assert(cond.regClass() == ctx->program->lane_mask);
9546 cond = bool_to_scalar_condition(ctx, cond);
9547
9548 begin_uniform_if_then(ctx, &ic, cond);
9549 visit_cf_list(ctx, &if_stmt->then_list);
9550
9551 begin_uniform_if_else(ctx, &ic);
9552 visit_cf_list(ctx, &if_stmt->else_list);
9553
9554 end_uniform_if(ctx, &ic);
9555
9556 return !ctx->cf_info.has_branch;
9557 } else { /* non-uniform condition */
9558 /**
9559 * To maintain a logical and linear CFG without critical edges,
9560 * non-uniform conditionals are represented in the following way*) :
9561 *
9562 * The linear CFG:
9563 * BB_IF
9564 * / \
9565 * BB_THEN (logical) BB_THEN (linear)
9566 * \ /
9567 * BB_INVERT (linear)
9568 * / \
9569 * BB_ELSE (logical) BB_ELSE (linear)
9570 * \ /
9571 * BB_ENDIF
9572 *
9573 * The logical CFG:
9574 * BB_IF
9575 * / \
9576 * BB_THEN (logical) BB_ELSE (logical)
9577 * \ /
9578 * BB_ENDIF
9579 *
9580 * *) Exceptions may be due to break and continue statements within loops
9581 **/
9582
9583 begin_divergent_if_then(ctx, &ic, cond);
9584 visit_cf_list(ctx, &if_stmt->then_list);
9585
9586 begin_divergent_if_else(ctx, &ic);
9587 visit_cf_list(ctx, &if_stmt->else_list);
9588
9589 end_divergent_if(ctx, &ic);
9590
9591 return true;
9592 }
9593 }
9594
9595 static bool visit_cf_list(isel_context *ctx,
9596 struct exec_list *list)
9597 {
9598 foreach_list_typed(nir_cf_node, node, node, list) {
9599 switch (node->type) {
9600 case nir_cf_node_block:
9601 visit_block(ctx, nir_cf_node_as_block(node));
9602 break;
9603 case nir_cf_node_if:
9604 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9605 return true;
9606 break;
9607 case nir_cf_node_loop:
9608 visit_loop(ctx, nir_cf_node_as_loop(node));
9609 break;
9610 default:
9611 unreachable("unimplemented cf list type");
9612 }
9613 }
9614 return false;
9615 }
9616
9617 static void create_null_export(isel_context *ctx)
9618 {
9619 /* Some shader stages always need to have exports.
9620 * So when there is none, we need to add a null export.
9621 */
9622
9623 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9624 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9625 Builder bld(ctx->program, ctx->block);
9626 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9627 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9628 }
9629
9630 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9631 {
9632 assert(ctx->stage == vertex_vs ||
9633 ctx->stage == tess_eval_vs ||
9634 ctx->stage == gs_copy_vs ||
9635 ctx->stage == ngg_vertex_gs ||
9636 ctx->stage == ngg_tess_eval_gs);
9637
9638 int offset = (ctx->stage & sw_tes)
9639 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9640 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9641 uint64_t mask = ctx->outputs.mask[slot];
9642 if (!is_pos && !mask)
9643 return false;
9644 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9645 return false;
9646 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9647 exp->enabled_mask = mask;
9648 for (unsigned i = 0; i < 4; ++i) {
9649 if (mask & (1 << i))
9650 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9651 else
9652 exp->operands[i] = Operand(v1);
9653 }
9654 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9655 * Setting valid_mask=1 prevents it and has no other effect.
9656 */
9657 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9658 exp->done = false;
9659 exp->compressed = false;
9660 if (is_pos)
9661 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9662 else
9663 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9664 ctx->block->instructions.emplace_back(std::move(exp));
9665
9666 return true;
9667 }
9668
9669 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9670 {
9671 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9672 exp->enabled_mask = 0;
9673 for (unsigned i = 0; i < 4; ++i)
9674 exp->operands[i] = Operand(v1);
9675 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9676 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9677 exp->enabled_mask |= 0x1;
9678 }
9679 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9680 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9681 exp->enabled_mask |= 0x4;
9682 }
9683 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9684 if (ctx->options->chip_class < GFX9) {
9685 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9686 exp->enabled_mask |= 0x8;
9687 } else {
9688 Builder bld(ctx->program, ctx->block);
9689
9690 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9691 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9692 if (exp->operands[2].isTemp())
9693 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9694
9695 exp->operands[2] = Operand(out);
9696 exp->enabled_mask |= 0x4;
9697 }
9698 }
9699 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9700 exp->done = false;
9701 exp->compressed = false;
9702 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9703 ctx->block->instructions.emplace_back(std::move(exp));
9704 }
9705
9706 static void create_export_phis(isel_context *ctx)
9707 {
9708 /* Used when exports are needed, but the output temps are defined in a preceding block.
9709 * This function will set up phis in order to access the outputs in the next block.
9710 */
9711
9712 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9713 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9714 ctx->block->instructions.pop_back();
9715
9716 Builder bld(ctx->program, ctx->block);
9717
9718 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9719 uint64_t mask = ctx->outputs.mask[slot];
9720 for (unsigned i = 0; i < 4; ++i) {
9721 if (!(mask & (1 << i)))
9722 continue;
9723
9724 Temp old = ctx->outputs.temps[slot * 4 + i];
9725 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9726 ctx->outputs.temps[slot * 4 + i] = phi;
9727 }
9728 }
9729
9730 bld.insert(std::move(logical_start));
9731 }
9732
9733 static void create_vs_exports(isel_context *ctx)
9734 {
9735 assert(ctx->stage == vertex_vs ||
9736 ctx->stage == tess_eval_vs ||
9737 ctx->stage == gs_copy_vs ||
9738 ctx->stage == ngg_vertex_gs ||
9739 ctx->stage == ngg_tess_eval_gs);
9740
9741 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9742 ? &ctx->program->info->tes.outinfo
9743 : &ctx->program->info->vs.outinfo;
9744
9745 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9746 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9747 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9748 }
9749
9750 if (ctx->options->key.has_multiview_view_index) {
9751 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9752 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9753 }
9754
9755 /* the order these position exports are created is important */
9756 int next_pos = 0;
9757 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9758 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9759 export_vs_psiz_layer_viewport(ctx, &next_pos);
9760 exported_pos = true;
9761 }
9762 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9763 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9764 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9765 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9766
9767 if (ctx->export_clip_dists) {
9768 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9769 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9770 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9771 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9772 }
9773
9774 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9775 if (i < VARYING_SLOT_VAR0 &&
9776 i != VARYING_SLOT_LAYER &&
9777 i != VARYING_SLOT_PRIMITIVE_ID &&
9778 i != VARYING_SLOT_VIEWPORT)
9779 continue;
9780
9781 export_vs_varying(ctx, i, false, NULL);
9782 }
9783
9784 if (!exported_pos)
9785 create_null_export(ctx);
9786 }
9787
9788 static bool export_fs_mrt_z(isel_context *ctx)
9789 {
9790 Builder bld(ctx->program, ctx->block);
9791 unsigned enabled_channels = 0;
9792 bool compr = false;
9793 Operand values[4];
9794
9795 for (unsigned i = 0; i < 4; ++i) {
9796 values[i] = Operand(v1);
9797 }
9798
9799 /* Both stencil and sample mask only need 16-bits. */
9800 if (!ctx->program->info->ps.writes_z &&
9801 (ctx->program->info->ps.writes_stencil ||
9802 ctx->program->info->ps.writes_sample_mask)) {
9803 compr = true; /* COMPR flag */
9804
9805 if (ctx->program->info->ps.writes_stencil) {
9806 /* Stencil should be in X[23:16]. */
9807 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9808 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9809 enabled_channels |= 0x3;
9810 }
9811
9812 if (ctx->program->info->ps.writes_sample_mask) {
9813 /* SampleMask should be in Y[15:0]. */
9814 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9815 enabled_channels |= 0xc;
9816 }
9817 } else {
9818 if (ctx->program->info->ps.writes_z) {
9819 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9820 enabled_channels |= 0x1;
9821 }
9822
9823 if (ctx->program->info->ps.writes_stencil) {
9824 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9825 enabled_channels |= 0x2;
9826 }
9827
9828 if (ctx->program->info->ps.writes_sample_mask) {
9829 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9830 enabled_channels |= 0x4;
9831 }
9832 }
9833
9834 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9835 * writemask component.
9836 */
9837 if (ctx->options->chip_class == GFX6 &&
9838 ctx->options->family != CHIP_OLAND &&
9839 ctx->options->family != CHIP_HAINAN) {
9840 enabled_channels |= 0x1;
9841 }
9842
9843 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9844 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9845
9846 return true;
9847 }
9848
9849 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9850 {
9851 Builder bld(ctx->program, ctx->block);
9852 unsigned write_mask = ctx->outputs.mask[slot];
9853 Operand values[4];
9854
9855 for (unsigned i = 0; i < 4; ++i) {
9856 if (write_mask & (1 << i)) {
9857 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9858 } else {
9859 values[i] = Operand(v1);
9860 }
9861 }
9862
9863 unsigned target, col_format;
9864 unsigned enabled_channels = 0;
9865 aco_opcode compr_op = (aco_opcode)0;
9866
9867 slot -= FRAG_RESULT_DATA0;
9868 target = V_008DFC_SQ_EXP_MRT + slot;
9869 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9870
9871 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9872 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9873
9874 switch (col_format)
9875 {
9876 case V_028714_SPI_SHADER_ZERO:
9877 enabled_channels = 0; /* writemask */
9878 target = V_008DFC_SQ_EXP_NULL;
9879 break;
9880
9881 case V_028714_SPI_SHADER_32_R:
9882 enabled_channels = 1;
9883 break;
9884
9885 case V_028714_SPI_SHADER_32_GR:
9886 enabled_channels = 0x3;
9887 break;
9888
9889 case V_028714_SPI_SHADER_32_AR:
9890 if (ctx->options->chip_class >= GFX10) {
9891 /* Special case: on GFX10, the outputs are different for 32_AR */
9892 enabled_channels = 0x3;
9893 values[1] = values[3];
9894 values[3] = Operand(v1);
9895 } else {
9896 enabled_channels = 0x9;
9897 }
9898 break;
9899
9900 case V_028714_SPI_SHADER_FP16_ABGR:
9901 enabled_channels = 0x5;
9902 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9903 break;
9904
9905 case V_028714_SPI_SHADER_UNORM16_ABGR:
9906 enabled_channels = 0x5;
9907 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9908 break;
9909
9910 case V_028714_SPI_SHADER_SNORM16_ABGR:
9911 enabled_channels = 0x5;
9912 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9913 break;
9914
9915 case V_028714_SPI_SHADER_UINT16_ABGR: {
9916 enabled_channels = 0x5;
9917 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9918 if (is_int8 || is_int10) {
9919 /* clamp */
9920 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9921 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9922
9923 for (unsigned i = 0; i < 4; i++) {
9924 if ((write_mask >> i) & 1) {
9925 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9926 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9927 values[i]);
9928 }
9929 }
9930 }
9931 break;
9932 }
9933
9934 case V_028714_SPI_SHADER_SINT16_ABGR:
9935 enabled_channels = 0x5;
9936 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9937 if (is_int8 || is_int10) {
9938 /* clamp */
9939 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9940 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9941 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9942 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9943
9944 for (unsigned i = 0; i < 4; i++) {
9945 if ((write_mask >> i) & 1) {
9946 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9947 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9948 values[i]);
9949 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9950 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9951 values[i]);
9952 }
9953 }
9954 }
9955 break;
9956
9957 case V_028714_SPI_SHADER_32_ABGR:
9958 enabled_channels = 0xF;
9959 break;
9960
9961 default:
9962 break;
9963 }
9964
9965 if (target == V_008DFC_SQ_EXP_NULL)
9966 return false;
9967
9968 if ((bool) compr_op) {
9969 for (int i = 0; i < 2; i++) {
9970 /* check if at least one of the values to be compressed is enabled */
9971 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9972 if (enabled) {
9973 enabled_channels |= enabled << (i*2);
9974 values[i] = bld.vop3(compr_op, bld.def(v1),
9975 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9976 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9977 } else {
9978 values[i] = Operand(v1);
9979 }
9980 }
9981 values[2] = Operand(v1);
9982 values[3] = Operand(v1);
9983 } else {
9984 for (int i = 0; i < 4; i++)
9985 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
9986 }
9987
9988 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9989 enabled_channels, target, (bool) compr_op);
9990 return true;
9991 }
9992
9993 static void create_fs_exports(isel_context *ctx)
9994 {
9995 bool exported = false;
9996
9997 /* Export depth, stencil and sample mask. */
9998 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
9999 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10000 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10001 exported |= export_fs_mrt_z(ctx);
10002
10003 /* Export all color render targets. */
10004 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10005 if (ctx->outputs.mask[i])
10006 exported |= export_fs_mrt_color(ctx, i);
10007
10008 if (!exported)
10009 create_null_export(ctx);
10010 }
10011
10012 static void write_tcs_tess_factors(isel_context *ctx)
10013 {
10014 unsigned outer_comps;
10015 unsigned inner_comps;
10016
10017 switch (ctx->args->options->key.tcs.primitive_mode) {
10018 case GL_ISOLINES:
10019 outer_comps = 2;
10020 inner_comps = 0;
10021 break;
10022 case GL_TRIANGLES:
10023 outer_comps = 3;
10024 inner_comps = 1;
10025 break;
10026 case GL_QUADS:
10027 outer_comps = 4;
10028 inner_comps = 2;
10029 break;
10030 default:
10031 return;
10032 }
10033
10034 Builder bld(ctx->program, ctx->block);
10035
10036 bld.barrier(aco_opcode::p_memory_barrier_shared);
10037 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10038 bld.sopp(aco_opcode::s_barrier);
10039
10040 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10041 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10042
10043 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10044 if_context ic_invocation_id_is_zero;
10045 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10046 bld.reset(ctx->block);
10047
10048 Temp hs_ring_tess_factor = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_FACTOR * 16u));
10049
10050 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10051 unsigned stride = inner_comps + outer_comps;
10052 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10053 Temp tf_inner_vec;
10054 Temp tf_outer_vec;
10055 Temp out[6];
10056 assert(stride <= (sizeof(out) / sizeof(Temp)));
10057
10058 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10059 // LINES reversal
10060 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10061 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10062 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10063 } else {
10064 tf_outer_vec = load_lds(ctx, 4, bld.tmp(RegClass(RegType::vgpr, outer_comps)), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10065 tf_inner_vec = load_lds(ctx, 4, bld.tmp(RegClass(RegType::vgpr, inner_comps)), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_in_loc, lds_align);
10066
10067 for (unsigned i = 0; i < outer_comps; ++i)
10068 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10069 for (unsigned i = 0; i < inner_comps; ++i)
10070 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10071 }
10072
10073 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10074 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10075 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10076 unsigned tf_const_offset = 0;
10077
10078 if (ctx->program->chip_class <= GFX8) {
10079 Temp rel_patch_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), rel_patch_id);
10080 if_context ic_rel_patch_id_is_zero;
10081 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10082 bld.reset(ctx->block);
10083
10084 /* Store the dynamic HS control word. */
10085 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10086 bld.mubuf(aco_opcode::buffer_store_dword,
10087 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10088 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10089 /* disable_wqm */ false, /* glc */ true);
10090 tf_const_offset += 4;
10091
10092 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10093 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10094 bld.reset(ctx->block);
10095 }
10096
10097 assert(stride == 2 || stride == 4 || stride == 6);
10098 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10099 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10100
10101 /* Store to offchip for TES to read - only if TES reads them */
10102 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10103 Temp hs_ring_tess_offchip = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
10104 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10105
10106 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10107 store_vmem_mubuf(ctx, tf_outer_vec, hs_ring_tess_offchip, vmem_offs_outer.first, oc_lds, vmem_offs_outer.second, 4, (1 << outer_comps) - 1, true, false);
10108
10109 if (likely(inner_comps)) {
10110 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10111 store_vmem_mubuf(ctx, tf_inner_vec, hs_ring_tess_offchip, vmem_offs_inner.first, oc_lds, vmem_offs_inner.second, 4, (1 << inner_comps) - 1, true, false);
10112 }
10113 }
10114
10115 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10116 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10117 }
10118
10119 static void emit_stream_output(isel_context *ctx,
10120 Temp const *so_buffers,
10121 Temp const *so_write_offset,
10122 const struct radv_stream_output *output)
10123 {
10124 unsigned num_comps = util_bitcount(output->component_mask);
10125 unsigned writemask = (1 << num_comps) - 1;
10126 unsigned loc = output->location;
10127 unsigned buf = output->buffer;
10128
10129 assert(num_comps && num_comps <= 4);
10130 if (!num_comps || num_comps > 4)
10131 return;
10132
10133 unsigned start = ffs(output->component_mask) - 1;
10134
10135 Temp out[4];
10136 bool all_undef = true;
10137 assert(ctx->stage & hw_vs);
10138 for (unsigned i = 0; i < num_comps; i++) {
10139 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10140 all_undef = all_undef && !out[i].id();
10141 }
10142 if (all_undef)
10143 return;
10144
10145 while (writemask) {
10146 int start, count;
10147 u_bit_scan_consecutive_range(&writemask, &start, &count);
10148 if (count == 3 && ctx->options->chip_class == GFX6) {
10149 /* GFX6 doesn't support storing vec3, split it. */
10150 writemask |= 1u << (start + 2);
10151 count = 2;
10152 }
10153
10154 unsigned offset = output->offset + start * 4;
10155
10156 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10157 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10158 for (int i = 0; i < count; ++i)
10159 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10160 vec->definitions[0] = Definition(write_data);
10161 ctx->block->instructions.emplace_back(std::move(vec));
10162
10163 aco_opcode opcode;
10164 switch (count) {
10165 case 1:
10166 opcode = aco_opcode::buffer_store_dword;
10167 break;
10168 case 2:
10169 opcode = aco_opcode::buffer_store_dwordx2;
10170 break;
10171 case 3:
10172 opcode = aco_opcode::buffer_store_dwordx3;
10173 break;
10174 case 4:
10175 opcode = aco_opcode::buffer_store_dwordx4;
10176 break;
10177 default:
10178 unreachable("Unsupported dword count.");
10179 }
10180
10181 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10182 store->operands[0] = Operand(so_buffers[buf]);
10183 store->operands[1] = Operand(so_write_offset[buf]);
10184 store->operands[2] = Operand((uint32_t) 0);
10185 store->operands[3] = Operand(write_data);
10186 if (offset > 4095) {
10187 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10188 Builder bld(ctx->program, ctx->block);
10189 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10190 } else {
10191 store->offset = offset;
10192 }
10193 store->offen = true;
10194 store->glc = true;
10195 store->dlc = false;
10196 store->slc = true;
10197 store->can_reorder = true;
10198 ctx->block->instructions.emplace_back(std::move(store));
10199 }
10200 }
10201
10202 static void emit_streamout(isel_context *ctx, unsigned stream)
10203 {
10204 Builder bld(ctx->program, ctx->block);
10205
10206 Temp so_buffers[4];
10207 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10208 for (unsigned i = 0; i < 4; i++) {
10209 unsigned stride = ctx->program->info->so.strides[i];
10210 if (!stride)
10211 continue;
10212
10213 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10214 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10215 }
10216
10217 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10218 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10219
10220 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10221
10222 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10223
10224 if_context ic;
10225 begin_divergent_if_then(ctx, &ic, can_emit);
10226
10227 bld.reset(ctx->block);
10228
10229 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10230
10231 Temp so_write_offset[4];
10232
10233 for (unsigned i = 0; i < 4; i++) {
10234 unsigned stride = ctx->program->info->so.strides[i];
10235 if (!stride)
10236 continue;
10237
10238 if (stride == 1) {
10239 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10240 get_arg(ctx, ctx->args->streamout_write_idx),
10241 get_arg(ctx, ctx->args->streamout_offset[i]));
10242 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10243
10244 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10245 } else {
10246 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10247 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10248 get_arg(ctx, ctx->args->streamout_offset[i]));
10249 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10250 }
10251 }
10252
10253 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10254 struct radv_stream_output *output =
10255 &ctx->program->info->so.outputs[i];
10256 if (stream != output->stream)
10257 continue;
10258
10259 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10260 }
10261
10262 begin_divergent_if_else(ctx, &ic);
10263 end_divergent_if(ctx, &ic);
10264 }
10265
10266 } /* end namespace */
10267
10268 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10269 {
10270 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10271 Builder bld(ctx->program, ctx->block);
10272 constexpr unsigned hs_idx = 1u;
10273 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10274 get_arg(ctx, ctx->args->merged_wave_info),
10275 Operand((8u << 16) | (hs_idx * 8u)));
10276 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10277
10278 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10279
10280 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10281 get_arg(ctx, ctx->args->rel_auto_id),
10282 get_arg(ctx, ctx->args->ac.instance_id),
10283 ls_has_nonzero_hs_threads);
10284 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10285 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10286 get_arg(ctx, ctx->args->rel_auto_id),
10287 ls_has_nonzero_hs_threads);
10288 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10289 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10290 get_arg(ctx, ctx->args->ac.vertex_id),
10291 ls_has_nonzero_hs_threads);
10292
10293 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10294 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10295 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10296 }
10297
10298 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10299 {
10300 /* Split all arguments except for the first (ring_offsets) and the last
10301 * (exec) so that the dead channels don't stay live throughout the program.
10302 */
10303 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10304 if (startpgm->definitions[i].regClass().size() > 1) {
10305 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10306 startpgm->definitions[i].regClass().size());
10307 }
10308 }
10309 }
10310
10311 void handle_bc_optimize(isel_context *ctx)
10312 {
10313 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10314 Builder bld(ctx->program, ctx->block);
10315 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10316 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10317 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10318 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10319 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10320 if (uses_center && uses_centroid) {
10321 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10322 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10323
10324 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10325 Temp new_coord[2];
10326 for (unsigned i = 0; i < 2; i++) {
10327 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10328 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10329 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10330 persp_centroid, persp_center, sel);
10331 }
10332 ctx->persp_centroid = bld.tmp(v2);
10333 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10334 Operand(new_coord[0]), Operand(new_coord[1]));
10335 emit_split_vector(ctx, ctx->persp_centroid, 2);
10336 }
10337
10338 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10339 Temp new_coord[2];
10340 for (unsigned i = 0; i < 2; i++) {
10341 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10342 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10343 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10344 linear_centroid, linear_center, sel);
10345 }
10346 ctx->linear_centroid = bld.tmp(v2);
10347 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10348 Operand(new_coord[0]), Operand(new_coord[1]));
10349 emit_split_vector(ctx, ctx->linear_centroid, 2);
10350 }
10351 }
10352 }
10353
10354 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10355 {
10356 Program *program = ctx->program;
10357
10358 unsigned float_controls = shader->info.float_controls_execution_mode;
10359
10360 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10361 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10362 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10363 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10364 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10365
10366 program->next_fp_mode.must_flush_denorms32 =
10367 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10368 program->next_fp_mode.must_flush_denorms16_64 =
10369 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10370 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10371
10372 program->next_fp_mode.care_about_round32 =
10373 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10374
10375 program->next_fp_mode.care_about_round16_64 =
10376 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10377 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10378
10379 /* default to preserving fp16 and fp64 denorms, since it's free */
10380 if (program->next_fp_mode.must_flush_denorms16_64)
10381 program->next_fp_mode.denorm16_64 = 0;
10382 else
10383 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10384
10385 /* preserving fp32 denorms is expensive, so only do it if asked */
10386 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10387 program->next_fp_mode.denorm32 = fp_denorm_keep;
10388 else
10389 program->next_fp_mode.denorm32 = 0;
10390
10391 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10392 program->next_fp_mode.round32 = fp_round_tz;
10393 else
10394 program->next_fp_mode.round32 = fp_round_ne;
10395
10396 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10397 program->next_fp_mode.round16_64 = fp_round_tz;
10398 else
10399 program->next_fp_mode.round16_64 = fp_round_ne;
10400
10401 ctx->block->fp_mode = program->next_fp_mode;
10402 }
10403
10404 void cleanup_cfg(Program *program)
10405 {
10406 /* create linear_succs/logical_succs */
10407 for (Block& BB : program->blocks) {
10408 for (unsigned idx : BB.linear_preds)
10409 program->blocks[idx].linear_succs.emplace_back(BB.index);
10410 for (unsigned idx : BB.logical_preds)
10411 program->blocks[idx].logical_succs.emplace_back(BB.index);
10412 }
10413 }
10414
10415 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10416 {
10417 Builder bld(ctx->program, ctx->block);
10418
10419 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10420 Temp count = i == 0
10421 ? get_arg(ctx, ctx->args->merged_wave_info)
10422 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10423 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10424
10425 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10426 Temp cond;
10427
10428 if (ctx->program->wave_size == 64) {
10429 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10430 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10431 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10432 } else {
10433 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10434 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10435 }
10436
10437 return cond;
10438 }
10439
10440 bool ngg_early_prim_export(isel_context *ctx)
10441 {
10442 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10443 return true;
10444 }
10445
10446 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10447 {
10448 Builder bld(ctx->program, ctx->block);
10449
10450 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10451 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10452
10453 /* Get the id of the current wave within the threadgroup (workgroup) */
10454 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10455 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10456
10457 /* Execute the following code only on the first wave (wave id 0),
10458 * use the SCC def to tell if the wave id is zero or not.
10459 */
10460 Temp cond = wave_id_in_tg.def(1).getTemp();
10461 if_context ic;
10462 begin_uniform_if_then(ctx, &ic, cond);
10463 begin_uniform_if_else(ctx, &ic);
10464 bld.reset(ctx->block);
10465
10466 /* Number of vertices output by VS/TES */
10467 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10468 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10469 /* Number of primitives output by VS/TES */
10470 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10471 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10472
10473 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10474 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10475 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10476
10477 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10478 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10479
10480 end_uniform_if(ctx, &ic);
10481
10482 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10483 bld.reset(ctx->block);
10484 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10485 }
10486
10487 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10488 {
10489 Builder bld(ctx->program, ctx->block);
10490
10491 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10492 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10493 }
10494
10495 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10496 Temp tmp;
10497
10498 for (unsigned i = 0; i < num_vertices; ++i) {
10499 assert(vtxindex[i].id());
10500
10501 if (i)
10502 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10503 else
10504 tmp = vtxindex[i];
10505
10506 /* The initial edge flag is always false in tess eval shaders. */
10507 if (ctx->stage == ngg_vertex_gs) {
10508 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10509 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10510 }
10511 }
10512
10513 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10514
10515 return tmp;
10516 }
10517
10518 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10519 {
10520 Builder bld(ctx->program, ctx->block);
10521 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10522
10523 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10524 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10525 false /* compressed */, true/* done */, false /* valid mask */);
10526 }
10527
10528 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10529 {
10530 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10531 * These must always come before VS exports.
10532 *
10533 * It is recommended to do these as early as possible. They can be at the beginning when
10534 * there is no SW GS and the shader doesn't write edge flags.
10535 */
10536
10537 if_context ic;
10538 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10539 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10540
10541 Builder bld(ctx->program, ctx->block);
10542 constexpr unsigned max_vertices_per_primitive = 3;
10543 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10544
10545 if (ctx->stage == ngg_vertex_gs) {
10546 /* TODO: optimize for points & lines */
10547 } else if (ctx->stage == ngg_tess_eval_gs) {
10548 if (ctx->shader->info.tess.point_mode)
10549 num_vertices_per_primitive = 1;
10550 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10551 num_vertices_per_primitive = 2;
10552 } else {
10553 unreachable("Unsupported NGG shader stage");
10554 }
10555
10556 Temp vtxindex[max_vertices_per_primitive];
10557 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10558 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10559 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10560 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10561 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10562 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10563 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10564 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10565
10566 /* Export primitive data to the index buffer. */
10567 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10568
10569 /* Export primitive ID. */
10570 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10571 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10572 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10573 Temp provoking_vtx_index = vtxindex[0];
10574 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10575
10576 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10577 }
10578
10579 begin_divergent_if_else(ctx, &ic);
10580 end_divergent_if(ctx, &ic);
10581 }
10582
10583 void ngg_emit_nogs_output(isel_context *ctx)
10584 {
10585 /* Emits NGG GS output, for stages that don't have SW GS. */
10586
10587 if_context ic;
10588 Builder bld(ctx->program, ctx->block);
10589 bool late_prim_export = !ngg_early_prim_export(ctx);
10590
10591 /* NGG streamout is currently disabled by default. */
10592 assert(!ctx->args->shader_info->so.num_outputs);
10593
10594 if (late_prim_export) {
10595 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10596 create_export_phis(ctx);
10597 /* Do what we need to do in the GS threads. */
10598 ngg_emit_nogs_gsthreads(ctx);
10599
10600 /* What comes next should be executed on ES threads. */
10601 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10602 begin_divergent_if_then(ctx, &ic, is_es_thread);
10603 bld.reset(ctx->block);
10604 }
10605
10606 /* Export VS outputs */
10607 ctx->block->kind |= block_kind_export_end;
10608 create_vs_exports(ctx);
10609
10610 /* Export primitive ID */
10611 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10612 Temp prim_id;
10613
10614 if (ctx->stage == ngg_vertex_gs) {
10615 /* Wait for GS threads to store primitive ID in LDS. */
10616 bld.barrier(aco_opcode::p_memory_barrier_shared);
10617 bld.sopp(aco_opcode::s_barrier);
10618
10619 /* Calculate LDS address where the GS threads stored the primitive ID. */
10620 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10621 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10622 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10623 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10624 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10625 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10626
10627 /* Load primitive ID from LDS. */
10628 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10629 } else if (ctx->stage == ngg_tess_eval_gs) {
10630 /* TES: Just use the patch ID as the primitive ID. */
10631 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10632 } else {
10633 unreachable("unsupported NGG shader stage.");
10634 }
10635
10636 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10637 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10638
10639 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10640 }
10641
10642 if (late_prim_export) {
10643 begin_divergent_if_else(ctx, &ic);
10644 end_divergent_if(ctx, &ic);
10645 bld.reset(ctx->block);
10646 }
10647 }
10648
10649 void select_program(Program *program,
10650 unsigned shader_count,
10651 struct nir_shader *const *shaders,
10652 ac_shader_config* config,
10653 struct radv_shader_args *args)
10654 {
10655 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10656 if_context ic_merged_wave_info;
10657 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10658
10659 for (unsigned i = 0; i < shader_count; i++) {
10660 nir_shader *nir = shaders[i];
10661 init_context(&ctx, nir);
10662
10663 setup_fp_mode(&ctx, nir);
10664
10665 if (!i) {
10666 /* needs to be after init_context() for FS */
10667 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10668 append_logical_start(ctx.block);
10669
10670 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10671 fix_ls_vgpr_init_bug(&ctx, startpgm);
10672
10673 split_arguments(&ctx, startpgm);
10674 }
10675
10676 if (ngg_no_gs) {
10677 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10678
10679 if (ngg_early_prim_export(&ctx))
10680 ngg_emit_nogs_gsthreads(&ctx);
10681 }
10682
10683 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10684 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10685 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10686 ((nir->info.stage == MESA_SHADER_VERTEX &&
10687 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10688 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10689 ctx.stage == tess_eval_geometry_gs));
10690
10691 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10692 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10693 if (check_merged_wave_info) {
10694 Temp cond = merged_wave_info_to_mask(&ctx, i);
10695 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10696 }
10697
10698 if (i) {
10699 Builder bld(ctx.program, ctx.block);
10700
10701 bld.barrier(aco_opcode::p_memory_barrier_shared);
10702 bld.sopp(aco_opcode::s_barrier);
10703
10704 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10705 ctx.gs_wave_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1, m0), bld.def(s1, scc), get_arg(&ctx, args->merged_wave_info), Operand((8u << 16) | 16u));
10706 }
10707 } else if (ctx.stage == geometry_gs)
10708 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10709
10710 if (ctx.stage == fragment_fs)
10711 handle_bc_optimize(&ctx);
10712
10713 visit_cf_list(&ctx, &func->body);
10714
10715 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10716 emit_streamout(&ctx, 0);
10717
10718 if (ctx.stage & hw_vs) {
10719 create_vs_exports(&ctx);
10720 ctx.block->kind |= block_kind_export_end;
10721 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10722 ngg_emit_nogs_output(&ctx);
10723 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10724 Builder bld(ctx.program, ctx.block);
10725 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10726 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10727 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10728 write_tcs_tess_factors(&ctx);
10729 }
10730
10731 if (ctx.stage == fragment_fs) {
10732 create_fs_exports(&ctx);
10733 ctx.block->kind |= block_kind_export_end;
10734 }
10735
10736 if (endif_merged_wave_info) {
10737 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10738 end_divergent_if(&ctx, &ic_merged_wave_info);
10739 }
10740
10741 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10742 ngg_emit_nogs_output(&ctx);
10743
10744 ralloc_free(ctx.divergent_vals);
10745
10746 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10747 /* Outputs of the previous stage are inputs to the next stage */
10748 ctx.inputs = ctx.outputs;
10749 ctx.outputs = shader_io_state();
10750 }
10751 }
10752
10753 program->config->float_mode = program->blocks[0].fp_mode.val;
10754
10755 append_logical_end(ctx.block);
10756 ctx.block->kind |= block_kind_uniform;
10757 Builder bld(ctx.program, ctx.block);
10758 if (ctx.program->wb_smem_l1_on_end)
10759 bld.smem(aco_opcode::s_dcache_wb, false);
10760 bld.sopp(aco_opcode::s_endpgm);
10761
10762 cleanup_cfg(program);
10763 }
10764
10765 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10766 ac_shader_config* config,
10767 struct radv_shader_args *args)
10768 {
10769 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10770
10771 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10772 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10773 program->next_fp_mode.must_flush_denorms32 = false;
10774 program->next_fp_mode.must_flush_denorms16_64 = false;
10775 program->next_fp_mode.care_about_round32 = false;
10776 program->next_fp_mode.care_about_round16_64 = false;
10777 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10778 program->next_fp_mode.denorm32 = 0;
10779 program->next_fp_mode.round32 = fp_round_ne;
10780 program->next_fp_mode.round16_64 = fp_round_ne;
10781 ctx.block->fp_mode = program->next_fp_mode;
10782
10783 add_startpgm(&ctx);
10784 append_logical_start(ctx.block);
10785
10786 Builder bld(ctx.program, ctx.block);
10787
10788 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10789
10790 Operand stream_id(0u);
10791 if (args->shader_info->so.num_outputs)
10792 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10793 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10794
10795 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10796
10797 std::stack<Block> endif_blocks;
10798
10799 for (unsigned stream = 0; stream < 4; stream++) {
10800 if (stream_id.isConstant() && stream != stream_id.constantValue())
10801 continue;
10802
10803 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10804 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10805 continue;
10806
10807 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10808
10809 unsigned BB_if_idx = ctx.block->index;
10810 Block BB_endif = Block();
10811 if (!stream_id.isConstant()) {
10812 /* begin IF */
10813 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10814 append_logical_end(ctx.block);
10815 ctx.block->kind |= block_kind_uniform;
10816 bld.branch(aco_opcode::p_cbranch_z, cond);
10817
10818 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10819
10820 ctx.block = ctx.program->create_and_insert_block();
10821 add_edge(BB_if_idx, ctx.block);
10822 bld.reset(ctx.block);
10823 append_logical_start(ctx.block);
10824 }
10825
10826 unsigned offset = 0;
10827 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10828 if (args->shader_info->gs.output_streams[i] != stream)
10829 continue;
10830
10831 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10832 unsigned length = util_last_bit(output_usage_mask);
10833 for (unsigned j = 0; j < length; ++j) {
10834 if (!(output_usage_mask & (1 << j)))
10835 continue;
10836
10837 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10838 Temp voffset = vtx_offset;
10839 if (const_offset >= 4096u) {
10840 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10841 const_offset %= 4096u;
10842 }
10843
10844 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10845 mubuf->definitions[0] = bld.def(v1);
10846 mubuf->operands[0] = Operand(gsvs_ring);
10847 mubuf->operands[1] = Operand(voffset);
10848 mubuf->operands[2] = Operand(0u);
10849 mubuf->offen = true;
10850 mubuf->offset = const_offset;
10851 mubuf->glc = true;
10852 mubuf->slc = true;
10853 mubuf->dlc = args->options->chip_class >= GFX10;
10854 mubuf->barrier = barrier_none;
10855 mubuf->can_reorder = true;
10856
10857 ctx.outputs.mask[i] |= 1 << j;
10858 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10859
10860 bld.insert(std::move(mubuf));
10861
10862 offset++;
10863 }
10864 }
10865
10866 if (args->shader_info->so.num_outputs) {
10867 emit_streamout(&ctx, stream);
10868 bld.reset(ctx.block);
10869 }
10870
10871 if (stream == 0) {
10872 create_vs_exports(&ctx);
10873 ctx.block->kind |= block_kind_export_end;
10874 }
10875
10876 if (!stream_id.isConstant()) {
10877 append_logical_end(ctx.block);
10878
10879 /* branch from then block to endif block */
10880 bld.branch(aco_opcode::p_branch);
10881 add_edge(ctx.block->index, &BB_endif);
10882 ctx.block->kind |= block_kind_uniform;
10883
10884 /* emit else block */
10885 ctx.block = ctx.program->create_and_insert_block();
10886 add_edge(BB_if_idx, ctx.block);
10887 bld.reset(ctx.block);
10888 append_logical_start(ctx.block);
10889
10890 endif_blocks.push(std::move(BB_endif));
10891 }
10892 }
10893
10894 while (!endif_blocks.empty()) {
10895 Block BB_endif = std::move(endif_blocks.top());
10896 endif_blocks.pop();
10897
10898 Block *BB_else = ctx.block;
10899
10900 append_logical_end(BB_else);
10901 /* branch from else block to endif block */
10902 bld.branch(aco_opcode::p_branch);
10903 add_edge(BB_else->index, &BB_endif);
10904 BB_else->kind |= block_kind_uniform;
10905
10906 /** emit endif merge block */
10907 ctx.block = program->insert_block(std::move(BB_endif));
10908 bld.reset(ctx.block);
10909 append_logical_start(ctx.block);
10910 }
10911
10912 program->config->float_mode = program->blocks[0].fp_mode.val;
10913
10914 append_logical_end(ctx.block);
10915 ctx.block->kind |= block_kind_uniform;
10916 bld.sopp(aco_opcode::s_endpgm);
10917
10918 cleanup_cfg(program);
10919 }
10920 }