aco: remove 64-bit SGPR ubfe/ibfe
[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 #define isel_err(...) _isel_err(ctx, __FILE__, __LINE__, __VA_ARGS__)
42
43 static void _isel_err(isel_context *ctx, const char *file, unsigned line,
44 const nir_instr *instr, const char *msg)
45 {
46 char *out;
47 size_t outsize;
48 FILE *memf = open_memstream(&out, &outsize);
49
50 fprintf(memf, "%s: ", msg);
51 nir_print_instr(instr, memf);
52 fclose(memf);
53
54 _aco_err(ctx->program, file, line, out);
55 free(out);
56 }
57
58 class loop_info_RAII {
59 isel_context* ctx;
60 unsigned header_idx_old;
61 Block* exit_old;
62 bool divergent_cont_old;
63 bool divergent_branch_old;
64 bool divergent_if_old;
65
66 public:
67 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
68 : ctx(ctx),
69 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
70 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
71 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
72 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
73 {
74 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
75 ctx->cf_info.parent_loop.exit = loop_exit;
76 ctx->cf_info.parent_loop.has_divergent_continue = false;
77 ctx->cf_info.parent_loop.has_divergent_branch = false;
78 ctx->cf_info.parent_if.is_divergent = false;
79 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
80 }
81
82 ~loop_info_RAII()
83 {
84 ctx->cf_info.parent_loop.header_idx = header_idx_old;
85 ctx->cf_info.parent_loop.exit = exit_old;
86 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
87 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
88 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
89 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
90 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
91 ctx->cf_info.exec_potentially_empty_discard = false;
92 }
93 };
94
95 struct if_context {
96 Temp cond;
97
98 bool divergent_old;
99 bool exec_potentially_empty_discard_old;
100 bool exec_potentially_empty_break_old;
101 uint16_t exec_potentially_empty_break_depth_old;
102
103 unsigned BB_if_idx;
104 unsigned invert_idx;
105 bool uniform_has_then_branch;
106 bool then_branch_divergent;
107 Block BB_invert;
108 Block BB_endif;
109 };
110
111 static bool visit_cf_list(struct isel_context *ctx,
112 struct exec_list *list);
113
114 static void add_logical_edge(unsigned pred_idx, Block *succ)
115 {
116 succ->logical_preds.emplace_back(pred_idx);
117 }
118
119
120 static void add_linear_edge(unsigned pred_idx, Block *succ)
121 {
122 succ->linear_preds.emplace_back(pred_idx);
123 }
124
125 static void add_edge(unsigned pred_idx, Block *succ)
126 {
127 add_logical_edge(pred_idx, succ);
128 add_linear_edge(pred_idx, succ);
129 }
130
131 static void append_logical_start(Block *b)
132 {
133 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
134 }
135
136 static void append_logical_end(Block *b)
137 {
138 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
139 }
140
141 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
142 {
143 assert(ctx->allocated[def->index].id());
144 return ctx->allocated[def->index];
145 }
146
147 Temp emit_mbcnt(isel_context *ctx, Definition dst,
148 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
149 {
150 Builder bld(ctx->program, ctx->block);
151 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
152 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
153
154 if (ctx->program->wave_size == 32) {
155 return thread_id_lo;
156 } else if (ctx->program->chip_class <= GFX7) {
157 Temp thread_id_hi = bld.vop2(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
158 return thread_id_hi;
159 } else {
160 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32_e64, dst, mask_hi, thread_id_lo);
161 return thread_id_hi;
162 }
163 }
164
165 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
166 {
167 Builder bld(ctx->program, ctx->block);
168
169 if (!dst.id())
170 dst = bld.tmp(src.regClass());
171
172 assert(src.size() == dst.size());
173
174 if (ctx->stage != fragment_fs) {
175 if (!dst.id())
176 return src;
177
178 bld.copy(Definition(dst), src);
179 return dst;
180 }
181
182 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
183 ctx->program->needs_wqm |= program_needs_wqm;
184 return dst;
185 }
186
187 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
188 {
189 if (index.regClass() == s1)
190 return bld.readlane(bld.def(s1), data, index);
191
192 if (ctx->options->chip_class <= GFX7) {
193 /* GFX6-7: there is no bpermute instruction */
194 Operand index_op(index);
195 Operand input_data(data);
196 index_op.setLateKill(true);
197 input_data.setLateKill(true);
198
199 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(bld.lm), bld.def(bld.lm, vcc), index_op, input_data);
200 } else if (ctx->options->chip_class >= GFX10 && ctx->program->wave_size == 64) {
201 /* GFX10 wave64 mode: emulate full-wave bpermute */
202 if (!ctx->has_gfx10_wave64_bpermute) {
203 ctx->has_gfx10_wave64_bpermute = true;
204 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
205 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
206 }
207
208 Temp index_is_lo = bld.vopc(aco_opcode::v_cmp_ge_u32, bld.def(bld.lm), Operand(31u), index);
209 Builder::Result index_is_lo_split = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), index_is_lo);
210 Temp index_is_lo_n1 = bld.sop1(aco_opcode::s_not_b32, bld.def(s1), bld.def(s1, scc), index_is_lo_split.def(1).getTemp());
211 Operand same_half = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), index_is_lo_split.def(0).getTemp(), index_is_lo_n1);
212 Operand index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
213 Operand input_data(data);
214
215 index_x4.setLateKill(true);
216 input_data.setLateKill(true);
217 same_half.setLateKill(true);
218
219 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc), index_x4, input_data, same_half);
220 } else {
221 /* GFX8-9 or GFX10 wave32: bpermute works normally */
222 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
223 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
224 }
225 }
226
227 static Temp emit_masked_swizzle(isel_context *ctx, Builder &bld, Temp src, unsigned mask)
228 {
229 if (ctx->options->chip_class >= GFX8) {
230 unsigned and_mask = mask & 0x1f;
231 unsigned or_mask = (mask >> 5) & 0x1f;
232 unsigned xor_mask = (mask >> 10) & 0x1f;
233
234 uint16_t dpp_ctrl = 0xffff;
235
236 // TODO: we could use DPP8 for some swizzles
237 if (and_mask == 0x1f && or_mask < 4 && xor_mask < 4) {
238 unsigned res[4] = {0, 1, 2, 3};
239 for (unsigned i = 0; i < 4; i++)
240 res[i] = ((res[i] | or_mask) ^ xor_mask) & 0x3;
241 dpp_ctrl = dpp_quad_perm(res[0], res[1], res[2], res[3]);
242 } else if (and_mask == 0x1f && !or_mask && xor_mask == 8) {
243 dpp_ctrl = dpp_row_rr(8);
244 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0xf) {
245 dpp_ctrl = dpp_row_mirror;
246 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0x7) {
247 dpp_ctrl = dpp_row_half_mirror;
248 }
249
250 if (dpp_ctrl != 0xffff)
251 return bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
252 }
253
254 return bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false);
255 }
256
257 Temp as_vgpr(isel_context *ctx, Temp val)
258 {
259 if (val.type() == RegType::sgpr) {
260 Builder bld(ctx->program, ctx->block);
261 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
262 }
263 assert(val.type() == RegType::vgpr);
264 return val;
265 }
266
267 //assumes a != 0xffffffff
268 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
269 {
270 assert(b != 0);
271 Builder bld(ctx->program, ctx->block);
272
273 if (util_is_power_of_two_or_zero(b)) {
274 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
275 return;
276 }
277
278 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
279
280 assert(info.multiplier <= 0xffffffff);
281
282 bool pre_shift = info.pre_shift != 0;
283 bool increment = info.increment != 0;
284 bool multiply = true;
285 bool post_shift = info.post_shift != 0;
286
287 if (!pre_shift && !increment && !multiply && !post_shift) {
288 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
289 return;
290 }
291
292 Temp pre_shift_dst = a;
293 if (pre_shift) {
294 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
295 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
296 }
297
298 Temp increment_dst = pre_shift_dst;
299 if (increment) {
300 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
301 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
302 }
303
304 Temp multiply_dst = increment_dst;
305 if (multiply) {
306 multiply_dst = post_shift ? bld.tmp(v1) : dst;
307 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
308 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
309 }
310
311 if (post_shift) {
312 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
313 }
314 }
315
316 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
317 {
318 Builder bld(ctx->program, ctx->block);
319 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
320 }
321
322
323 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
324 {
325 /* no need to extract the whole vector */
326 if (src.regClass() == dst_rc) {
327 assert(idx == 0);
328 return src;
329 }
330
331 assert(src.bytes() > (idx * dst_rc.bytes()));
332 Builder bld(ctx->program, ctx->block);
333 auto it = ctx->allocated_vec.find(src.id());
334 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
335 if (it->second[idx].regClass() == dst_rc) {
336 return it->second[idx];
337 } else {
338 assert(!dst_rc.is_subdword());
339 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
340 return bld.copy(bld.def(dst_rc), it->second[idx]);
341 }
342 }
343
344 if (dst_rc.is_subdword())
345 src = as_vgpr(ctx, src);
346
347 if (src.bytes() == dst_rc.bytes()) {
348 assert(idx == 0);
349 return bld.copy(bld.def(dst_rc), src);
350 } else {
351 Temp dst = bld.tmp(dst_rc);
352 emit_extract_vector(ctx, src, idx, dst);
353 return dst;
354 }
355 }
356
357 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
358 {
359 if (num_components == 1)
360 return;
361 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
362 return;
363 RegClass rc;
364 if (num_components > vec_src.size()) {
365 if (vec_src.type() == RegType::sgpr) {
366 /* should still help get_alu_src() */
367 emit_split_vector(ctx, vec_src, vec_src.size());
368 return;
369 }
370 /* sub-dword split */
371 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
372 } else {
373 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
374 }
375 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
376 split->operands[0] = Operand(vec_src);
377 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
378 for (unsigned i = 0; i < num_components; i++) {
379 elems[i] = {ctx->program->allocateId(), rc};
380 split->definitions[i] = Definition(elems[i]);
381 }
382 ctx->block->instructions.emplace_back(std::move(split));
383 ctx->allocated_vec.emplace(vec_src.id(), elems);
384 }
385
386 /* This vector expansion uses a mask to determine which elements in the new vector
387 * come from the original vector. The other elements are undefined. */
388 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
389 {
390 emit_split_vector(ctx, vec_src, util_bitcount(mask));
391
392 if (vec_src == dst)
393 return;
394
395 Builder bld(ctx->program, ctx->block);
396 if (num_components == 1) {
397 if (dst.type() == RegType::sgpr)
398 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
399 else
400 bld.copy(Definition(dst), vec_src);
401 return;
402 }
403
404 unsigned component_size = dst.size() / num_components;
405 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
406
407 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
408 vec->definitions[0] = Definition(dst);
409 unsigned k = 0;
410 for (unsigned i = 0; i < num_components; i++) {
411 if (mask & (1 << i)) {
412 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
413 if (dst.type() == RegType::sgpr)
414 src = bld.as_uniform(src);
415 vec->operands[i] = Operand(src);
416 } else {
417 vec->operands[i] = Operand(0u);
418 }
419 elems[i] = vec->operands[i].getTemp();
420 }
421 ctx->block->instructions.emplace_back(std::move(vec));
422 ctx->allocated_vec.emplace(dst.id(), elems);
423 }
424
425 /* adjust misaligned small bit size loads */
426 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
427 {
428 Builder bld(ctx->program, ctx->block);
429 Operand shift;
430 Temp select = Temp();
431 if (offset.isConstant()) {
432 assert(offset.constantValue() && offset.constantValue() < 4);
433 shift = Operand(offset.constantValue() * 8);
434 } else {
435 /* bit_offset = 8 * (offset & 0x3) */
436 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
437 select = bld.tmp(s1);
438 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
439 }
440
441 if (vec.size() == 1) {
442 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
443 } else if (vec.size() == 2) {
444 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
445 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
446 if (tmp == dst)
447 emit_split_vector(ctx, dst, 2);
448 else
449 emit_extract_vector(ctx, tmp, 0, dst);
450 } else if (vec.size() == 4) {
451 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
452 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
453 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
454 if (select != Temp())
455 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), bld.scc(select));
456 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
457 Temp mid = bld.tmp(s1);
458 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
459 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
460 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
461 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
462 emit_split_vector(ctx, dst, 2);
463 }
464 }
465
466 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst, unsigned component_size)
467 {
468 Builder bld(ctx->program, ctx->block);
469 if (offset.isTemp()) {
470 Temp tmp[4] = {vec, vec, vec, vec};
471
472 if (vec.size() == 4) {
473 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1), tmp[3] = bld.tmp(v1);
474 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), Definition(tmp[3]), vec);
475 } else if (vec.size() == 3) {
476 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
477 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
478 } else if (vec.size() == 2) {
479 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
480 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
481 }
482 for (unsigned i = 0; i < dst.size(); i++)
483 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
484
485 vec = tmp[0];
486 if (dst.size() == 2)
487 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
488
489 offset = Operand(0u);
490 }
491
492 unsigned num_components = vec.bytes() / component_size;
493 if (vec.regClass() == dst.regClass()) {
494 assert(offset.constantValue() == 0);
495 bld.copy(Definition(dst), vec);
496 emit_split_vector(ctx, dst, num_components);
497 return;
498 }
499
500 emit_split_vector(ctx, vec, num_components);
501 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
502 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
503
504 assert(offset.constantValue() % component_size == 0);
505 unsigned skip = offset.constantValue() / component_size;
506 for (unsigned i = skip; i < num_components; i++)
507 elems[i - skip] = emit_extract_vector(ctx, vec, i, rc);
508
509 /* if dst is vgpr - split the src and create a shrunk version according to the mask. */
510 if (dst.type() == RegType::vgpr) {
511 num_components = dst.bytes() / component_size;
512 aco_ptr<Pseudo_instruction> create_vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
513 for (unsigned i = 0; i < num_components; i++)
514 create_vec->operands[i] = Operand(elems[i]);
515 create_vec->definitions[0] = Definition(dst);
516 bld.insert(std::move(create_vec));
517
518 /* if dst is sgpr - split the src, but move the original to sgpr. */
519 } else if (skip) {
520 vec = bld.pseudo(aco_opcode::p_as_uniform, bld.def(RegClass(RegType::sgpr, vec.size())), vec);
521 byte_align_scalar(ctx, vec, offset, dst);
522 } else {
523 assert(dst.size() == vec.size());
524 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
525 }
526
527 ctx->allocated_vec.emplace(dst.id(), elems);
528 }
529
530 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
531 {
532 Builder bld(ctx->program, ctx->block);
533 if (!dst.id())
534 dst = bld.tmp(bld.lm);
535
536 assert(val.regClass() == s1);
537 assert(dst.regClass() == bld.lm);
538
539 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
540 }
541
542 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
543 {
544 Builder bld(ctx->program, ctx->block);
545 if (!dst.id())
546 dst = bld.tmp(s1);
547
548 assert(val.regClass() == bld.lm);
549 assert(dst.regClass() == s1);
550
551 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
552 Temp tmp = bld.tmp(s1);
553 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
554 return emit_wqm(ctx, tmp, dst);
555 }
556
557 Temp convert_int(isel_context *ctx, Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp())
558 {
559 if (!dst.id()) {
560 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
561 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
562 else
563 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
564 }
565
566 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
567 return bld.copy(Definition(dst), src);
568 else if (dst.bytes() < src.bytes())
569 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
570
571 Temp tmp = dst;
572 if (dst_bits == 64)
573 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
574
575 if (tmp == src) {
576 } else if (src.regClass() == s1) {
577 if (is_signed)
578 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
579 else
580 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
581 } else if (ctx->options->chip_class >= GFX8) {
582 assert(src_bits != 8 || src.regClass() == v1b);
583 assert(src_bits != 16 || src.regClass() == v2b);
584 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
585 sdwa->operands[0] = Operand(src);
586 sdwa->definitions[0] = Definition(tmp);
587 if (is_signed)
588 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
589 else
590 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
591 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
592 bld.insert(std::move(sdwa));
593 } else {
594 assert(ctx->options->chip_class == GFX6 || ctx->options->chip_class == GFX7);
595 aco_opcode opcode = is_signed ? aco_opcode::v_bfe_i32 : aco_opcode::v_bfe_u32;
596 bld.vop3(opcode, Definition(tmp), src, Operand(0u), Operand(src_bits == 8 ? 8u : 16u));
597 }
598
599 if (dst_bits == 64) {
600 if (is_signed && dst.regClass() == s2) {
601 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
602 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
603 } else if (is_signed && dst.regClass() == v2) {
604 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
605 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
606 } else {
607 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
608 }
609 }
610
611 return dst;
612 }
613
614 enum sgpr_extract_mode {
615 sgpr_extract_sext,
616 sgpr_extract_zext,
617 sgpr_extract_undef,
618 };
619
620 Temp extract_8_16_bit_sgpr_element(isel_context *ctx, Temp dst, nir_alu_src *src, sgpr_extract_mode mode)
621 {
622 Temp vec = get_ssa_temp(ctx, src->src.ssa);
623 unsigned src_size = src->src.ssa->bit_size;
624 unsigned swizzle = src->swizzle[0];
625
626 if (vec.size() > 1) {
627 assert(src_size == 16);
628 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
629 swizzle = swizzle & 1;
630 }
631
632 Builder bld(ctx->program, ctx->block);
633 unsigned offset = src_size * swizzle;
634 Temp tmp = dst.regClass() == s2 ? bld.tmp(s1) : dst;
635
636 if (mode == sgpr_extract_undef && swizzle == 0) {
637 bld.copy(Definition(tmp), vec);
638 } else if (mode == sgpr_extract_undef || (offset == 24 && mode == sgpr_extract_zext)) {
639 bld.sop2(aco_opcode::s_lshr_b32, Definition(tmp), bld.def(s1, scc), vec, Operand(offset));
640 } else if (src_size == 8 && swizzle == 0 && mode == sgpr_extract_sext) {
641 bld.sop1(aco_opcode::s_sext_i32_i8, Definition(tmp), vec);
642 } else if (src_size == 16 && swizzle == 0 && mode == sgpr_extract_sext) {
643 bld.sop1(aco_opcode::s_sext_i32_i16, Definition(tmp), vec);
644 } else {
645 aco_opcode op = mode == sgpr_extract_zext ? aco_opcode::s_bfe_u32 : aco_opcode::s_bfe_i32;
646 bld.sop2(op, Definition(tmp), bld.def(s1, scc), vec, Operand((src_size << 16) | offset));
647 }
648
649 if (dst.regClass() == s2)
650 convert_int(ctx, bld, tmp, 32, 64, mode == sgpr_extract_sext, dst);
651
652 return dst;
653 }
654
655 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
656 {
657 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
658 return get_ssa_temp(ctx, src.src.ssa);
659
660 if (src.src.ssa->num_components == size) {
661 bool identity_swizzle = true;
662 for (unsigned i = 0; identity_swizzle && i < size; i++) {
663 if (src.swizzle[i] != i)
664 identity_swizzle = false;
665 }
666 if (identity_swizzle)
667 return get_ssa_temp(ctx, src.src.ssa);
668 }
669
670 Temp vec = get_ssa_temp(ctx, src.src.ssa);
671 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
672 assert(elem_size > 0);
673 assert(vec.bytes() % elem_size == 0);
674
675 if (elem_size < 4 && vec.type() == RegType::sgpr) {
676 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
677 assert(size == 1);
678 return extract_8_16_bit_sgpr_element(
679 ctx, Temp(ctx->program->allocateId(), s1), &src, sgpr_extract_undef);
680 }
681
682 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
683 if (size == 1) {
684 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
685 } else {
686 assert(size <= 4);
687 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
688 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
689 for (unsigned i = 0; i < size; ++i) {
690 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
691 vec_instr->operands[i] = Operand{elems[i]};
692 }
693 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
694 vec_instr->definitions[0] = Definition(dst);
695 ctx->block->instructions.emplace_back(std::move(vec_instr));
696 ctx->allocated_vec.emplace(dst.id(), elems);
697 return dst;
698 }
699 }
700
701 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
702 {
703 if (ptr.size() == 2)
704 return ptr;
705 Builder bld(ctx->program, ctx->block);
706 if (ptr.type() == RegType::vgpr)
707 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
708 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
709 ptr, Operand((unsigned)ctx->options->address32_hi));
710 }
711
712 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
713 {
714 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
715 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
716 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
717 sop2->definitions[0] = Definition(dst);
718 if (instr->no_unsigned_wrap)
719 sop2->definitions[0].setNUW(true);
720 if (writes_scc)
721 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
722 ctx->block->instructions.emplace_back(std::move(sop2));
723 }
724
725 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
726 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
727 {
728 Builder bld(ctx->program, ctx->block);
729 bld.is_precise = instr->exact;
730
731 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
732 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
733 if (src1.type() == RegType::sgpr) {
734 if (commutative && src0.type() == RegType::vgpr) {
735 Temp t = src0;
736 src0 = src1;
737 src1 = t;
738 } else {
739 src1 = as_vgpr(ctx, src1);
740 }
741 }
742
743 if (flush_denorms && ctx->program->chip_class < GFX9) {
744 assert(dst.size() == 1);
745 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
746 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
747 } else {
748 bld.vop2(op, Definition(dst), src0, src1);
749 }
750 }
751
752 void emit_vop2_instruction_logic64(isel_context *ctx, nir_alu_instr *instr,
753 aco_opcode op, Temp dst)
754 {
755 Builder bld(ctx->program, ctx->block);
756 bld.is_precise = instr->exact;
757
758 Temp src0 = get_alu_src(ctx, instr->src[0]);
759 Temp src1 = get_alu_src(ctx, instr->src[1]);
760
761 if (src1.type() == RegType::sgpr) {
762 assert(src0.type() == RegType::vgpr);
763 std::swap(src0, src1);
764 }
765
766 Temp src00 = bld.tmp(src0.type(), 1);
767 Temp src01 = bld.tmp(src0.type(), 1);
768 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
769 Temp src10 = bld.tmp(v1);
770 Temp src11 = bld.tmp(v1);
771 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
772 Temp lo = bld.vop2(op, bld.def(v1), src00, src10);
773 Temp hi = bld.vop2(op, bld.def(v1), src01, src11);
774 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
775 }
776
777 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
778 bool flush_denorms = false)
779 {
780 Temp src0 = get_alu_src(ctx, instr->src[0]);
781 Temp src1 = get_alu_src(ctx, instr->src[1]);
782 Temp src2 = get_alu_src(ctx, instr->src[2]);
783
784 /* ensure that the instruction has at most 1 sgpr operand
785 * The optimizer will inline constants for us */
786 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
787 src0 = as_vgpr(ctx, src0);
788 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
789 src1 = as_vgpr(ctx, src1);
790 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
791 src2 = as_vgpr(ctx, src2);
792
793 Builder bld(ctx->program, ctx->block);
794 bld.is_precise = instr->exact;
795 if (flush_denorms && ctx->program->chip_class < GFX9) {
796 assert(dst.size() == 1);
797 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
798 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
799 } else {
800 bld.vop3(op, Definition(dst), src0, src1, src2);
801 }
802 }
803
804 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
805 {
806 Builder bld(ctx->program, ctx->block);
807 bld.is_precise = instr->exact;
808 if (dst.type() == RegType::sgpr)
809 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
810 bld.vop1(op, bld.def(RegType::vgpr, dst.size()), get_alu_src(ctx, instr->src[0])));
811 else
812 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
813 }
814
815 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
816 {
817 Temp src0 = get_alu_src(ctx, instr->src[0]);
818 Temp src1 = get_alu_src(ctx, instr->src[1]);
819 assert(src0.size() == src1.size());
820
821 aco_ptr<Instruction> vopc;
822 if (src1.type() == RegType::sgpr) {
823 if (src0.type() == RegType::vgpr) {
824 /* to swap the operands, we might also have to change the opcode */
825 switch (op) {
826 case aco_opcode::v_cmp_lt_f16:
827 op = aco_opcode::v_cmp_gt_f16;
828 break;
829 case aco_opcode::v_cmp_ge_f16:
830 op = aco_opcode::v_cmp_le_f16;
831 break;
832 case aco_opcode::v_cmp_lt_i16:
833 op = aco_opcode::v_cmp_gt_i16;
834 break;
835 case aco_opcode::v_cmp_ge_i16:
836 op = aco_opcode::v_cmp_le_i16;
837 break;
838 case aco_opcode::v_cmp_lt_u16:
839 op = aco_opcode::v_cmp_gt_u16;
840 break;
841 case aco_opcode::v_cmp_ge_u16:
842 op = aco_opcode::v_cmp_le_u16;
843 break;
844 case aco_opcode::v_cmp_lt_f32:
845 op = aco_opcode::v_cmp_gt_f32;
846 break;
847 case aco_opcode::v_cmp_ge_f32:
848 op = aco_opcode::v_cmp_le_f32;
849 break;
850 case aco_opcode::v_cmp_lt_i32:
851 op = aco_opcode::v_cmp_gt_i32;
852 break;
853 case aco_opcode::v_cmp_ge_i32:
854 op = aco_opcode::v_cmp_le_i32;
855 break;
856 case aco_opcode::v_cmp_lt_u32:
857 op = aco_opcode::v_cmp_gt_u32;
858 break;
859 case aco_opcode::v_cmp_ge_u32:
860 op = aco_opcode::v_cmp_le_u32;
861 break;
862 case aco_opcode::v_cmp_lt_f64:
863 op = aco_opcode::v_cmp_gt_f64;
864 break;
865 case aco_opcode::v_cmp_ge_f64:
866 op = aco_opcode::v_cmp_le_f64;
867 break;
868 case aco_opcode::v_cmp_lt_i64:
869 op = aco_opcode::v_cmp_gt_i64;
870 break;
871 case aco_opcode::v_cmp_ge_i64:
872 op = aco_opcode::v_cmp_le_i64;
873 break;
874 case aco_opcode::v_cmp_lt_u64:
875 op = aco_opcode::v_cmp_gt_u64;
876 break;
877 case aco_opcode::v_cmp_ge_u64:
878 op = aco_opcode::v_cmp_le_u64;
879 break;
880 default: /* eq and ne are commutative */
881 break;
882 }
883 Temp t = src0;
884 src0 = src1;
885 src1 = t;
886 } else {
887 src1 = as_vgpr(ctx, src1);
888 }
889 }
890
891 Builder bld(ctx->program, ctx->block);
892 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
893 }
894
895 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
896 {
897 Temp src0 = get_alu_src(ctx, instr->src[0]);
898 Temp src1 = get_alu_src(ctx, instr->src[1]);
899 Builder bld(ctx->program, ctx->block);
900
901 assert(dst.regClass() == bld.lm);
902 assert(src0.type() == RegType::sgpr);
903 assert(src1.type() == RegType::sgpr);
904 assert(src0.regClass() == src1.regClass());
905
906 /* Emit the SALU comparison instruction */
907 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
908 /* Turn the result into a per-lane bool */
909 bool_to_vector_condition(ctx, cmp, dst);
910 }
911
912 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
913 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)
914 {
915 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;
916 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;
917 bool use_valu = s_op == aco_opcode::num_opcodes ||
918 nir_dest_is_divergent(instr->dest.dest) ||
919 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
920 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
921 aco_opcode op = use_valu ? v_op : s_op;
922 assert(op != aco_opcode::num_opcodes);
923 assert(dst.regClass() == ctx->program->lane_mask);
924
925 if (use_valu)
926 emit_vopc_instruction(ctx, instr, op, dst);
927 else
928 emit_sopc_instruction(ctx, instr, op, dst);
929 }
930
931 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
932 {
933 Builder bld(ctx->program, ctx->block);
934 Temp src0 = get_alu_src(ctx, instr->src[0]);
935 Temp src1 = get_alu_src(ctx, instr->src[1]);
936
937 assert(dst.regClass() == bld.lm);
938 assert(src0.regClass() == bld.lm);
939 assert(src1.regClass() == bld.lm);
940
941 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
942 }
943
944 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
945 {
946 Builder bld(ctx->program, ctx->block);
947 Temp cond = get_alu_src(ctx, instr->src[0]);
948 Temp then = get_alu_src(ctx, instr->src[1]);
949 Temp els = get_alu_src(ctx, instr->src[2]);
950
951 assert(cond.regClass() == bld.lm);
952
953 if (dst.type() == RegType::vgpr) {
954 aco_ptr<Instruction> bcsel;
955 if (dst.size() == 1) {
956 then = as_vgpr(ctx, then);
957 els = as_vgpr(ctx, els);
958
959 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
960 } else if (dst.size() == 2) {
961 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
962 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
963 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
964 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
965
966 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
967 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
968
969 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
970 } else {
971 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
972 }
973 return;
974 }
975
976 if (instr->dest.dest.ssa.bit_size == 1) {
977 assert(dst.regClass() == bld.lm);
978 assert(then.regClass() == bld.lm);
979 assert(els.regClass() == bld.lm);
980 }
981
982 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
983 if (dst.regClass() == s1 || dst.regClass() == s2) {
984 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
985 assert(dst.size() == then.size());
986 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
987 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
988 } else {
989 isel_err(&instr->instr, "Unimplemented uniform bcsel bit size");
990 }
991 return;
992 }
993
994 /* divergent boolean bcsel
995 * this implements bcsel on bools: dst = s0 ? s1 : s2
996 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
997 assert(instr->dest.dest.ssa.bit_size == 1);
998
999 if (cond.id() != then.id())
1000 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
1001
1002 if (cond.id() == els.id())
1003 bld.sop1(Builder::s_mov, Definition(dst), then);
1004 else
1005 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
1006 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
1007 }
1008
1009 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
1010 aco_opcode op, uint32_t undo)
1011 {
1012 /* multiply by 16777216 to handle denormals */
1013 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
1014 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
1015 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
1016 scaled = bld.vop1(op, bld.def(v1), scaled);
1017 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
1018
1019 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
1020
1021 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
1022 }
1023
1024 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1025 {
1026 if (ctx->block->fp_mode.denorm32 == 0) {
1027 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
1028 return;
1029 }
1030
1031 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
1032 }
1033
1034 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1035 {
1036 if (ctx->block->fp_mode.denorm32 == 0) {
1037 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
1038 return;
1039 }
1040
1041 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
1042 }
1043
1044 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1045 {
1046 if (ctx->block->fp_mode.denorm32 == 0) {
1047 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
1048 return;
1049 }
1050
1051 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
1052 }
1053
1054 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1055 {
1056 if (ctx->block->fp_mode.denorm32 == 0) {
1057 bld.vop1(aco_opcode::v_log_f32, dst, val);
1058 return;
1059 }
1060
1061 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
1062 }
1063
1064 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1065 {
1066 if (ctx->options->chip_class >= GFX7)
1067 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
1068
1069 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
1070 /* TODO: create more efficient code! */
1071 if (val.type() == RegType::sgpr)
1072 val = as_vgpr(ctx, val);
1073
1074 /* Split the input value. */
1075 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
1076 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
1077
1078 /* Extract the exponent and compute the unbiased value. */
1079 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
1080 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
1081
1082 /* Extract the fractional part. */
1083 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
1084 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
1085
1086 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
1087 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
1088
1089 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
1090 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
1091 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
1092 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
1093 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
1094
1095 /* Get the sign bit. */
1096 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
1097
1098 /* Decide the operation to apply depending on the unbiased exponent. */
1099 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
1100 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
1101 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
1102 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
1103 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
1104 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
1105
1106 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
1107 }
1108
1109 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1110 {
1111 if (ctx->options->chip_class >= GFX7)
1112 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
1113
1114 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1115 * lowered at NIR level for precision reasons). */
1116 Temp src0 = as_vgpr(ctx, val);
1117
1118 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
1119 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
1120
1121 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
1122 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
1123 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
1124
1125 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
1126 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
1127 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
1128 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
1129
1130 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
1131 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
1132
1133 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
1134
1135 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
1136 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
1137
1138 return add->definitions[0].getTemp();
1139 }
1140
1141 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1142 {
1143 if (!instr->dest.dest.is_ssa) {
1144 isel_err(&instr->instr, "nir alu dst not in ssa");
1145 abort();
1146 }
1147 Builder bld(ctx->program, ctx->block);
1148 bld.is_precise = instr->exact;
1149 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1150 switch(instr->op) {
1151 case nir_op_vec2:
1152 case nir_op_vec3:
1153 case nir_op_vec4: {
1154 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1155 unsigned num = instr->dest.dest.ssa.num_components;
1156 for (unsigned i = 0; i < num; ++i)
1157 elems[i] = get_alu_src(ctx, instr->src[i]);
1158
1159 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1160 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1161 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1162 for (unsigned i = 0; i < num; ++i) {
1163 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1164 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1165 else
1166 vec->operands[i] = Operand{elems[i]};
1167 }
1168 vec->definitions[0] = Definition(dst);
1169 ctx->block->instructions.emplace_back(std::move(vec));
1170 ctx->allocated_vec.emplace(dst.id(), elems);
1171 } else {
1172 // TODO: that is a bit suboptimal..
1173 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1174 for (unsigned i = 0; i < num - 1; ++i)
1175 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1176 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1177 for (unsigned i = 0; i < num; ++i) {
1178 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1179 if (bit % 32 == 0) {
1180 elems[bit / 32] = elems[i];
1181 } else {
1182 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1183 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1184 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1185 }
1186 }
1187 if (dst.size() == 1)
1188 bld.copy(Definition(dst), elems[0]);
1189 else
1190 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1191 }
1192 break;
1193 }
1194 case nir_op_mov: {
1195 Temp src = get_alu_src(ctx, instr->src[0]);
1196 aco_ptr<Instruction> mov;
1197 if (dst.type() == RegType::sgpr) {
1198 if (src.type() == RegType::vgpr)
1199 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1200 else if (src.regClass() == s1)
1201 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1202 else if (src.regClass() == s2)
1203 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1204 else
1205 unreachable("wrong src register class for nir_op_imov");
1206 } else {
1207 if (dst.regClass() == v1)
1208 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1209 else if (dst.regClass() == v1b ||
1210 dst.regClass() == v2b ||
1211 dst.regClass() == v2)
1212 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1213 else
1214 unreachable("wrong src register class for nir_op_imov");
1215 }
1216 break;
1217 }
1218 case nir_op_inot: {
1219 Temp src = get_alu_src(ctx, instr->src[0]);
1220 if (instr->dest.dest.ssa.bit_size == 1) {
1221 assert(src.regClass() == bld.lm);
1222 assert(dst.regClass() == bld.lm);
1223 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1224 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1225 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1226 } else if (dst.regClass() == v1) {
1227 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1228 } else if (dst.regClass() == v2) {
1229 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
1230 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
1231 lo = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), lo);
1232 hi = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), hi);
1233 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
1234 } else if (dst.type() == RegType::sgpr) {
1235 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1236 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1237 } else {
1238 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1239 }
1240 break;
1241 }
1242 case nir_op_ineg: {
1243 Temp src = get_alu_src(ctx, instr->src[0]);
1244 if (dst.regClass() == v1) {
1245 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1246 } else if (dst.regClass() == s1) {
1247 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1248 } else if (dst.size() == 2) {
1249 Temp src0 = bld.tmp(dst.type(), 1);
1250 Temp src1 = bld.tmp(dst.type(), 1);
1251 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1252
1253 if (dst.regClass() == s2) {
1254 Temp carry = bld.tmp(s1);
1255 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1256 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1257 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1258 } else {
1259 Temp lower = bld.tmp(v1);
1260 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1261 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1262 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1263 }
1264 } else {
1265 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1266 }
1267 break;
1268 }
1269 case nir_op_iabs: {
1270 if (dst.regClass() == s1) {
1271 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1272 } else if (dst.regClass() == v1) {
1273 Temp src = get_alu_src(ctx, instr->src[0]);
1274 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1275 } else {
1276 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1277 }
1278 break;
1279 }
1280 case nir_op_isign: {
1281 Temp src = get_alu_src(ctx, instr->src[0]);
1282 if (dst.regClass() == s1) {
1283 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1284 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1285 } else if (dst.regClass() == s2) {
1286 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1287 Temp neqz;
1288 if (ctx->program->chip_class >= GFX8)
1289 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1290 else
1291 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1292 /* SCC gets zero-extended to 64 bit */
1293 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1294 } else if (dst.regClass() == v1) {
1295 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1296 } else if (dst.regClass() == v2) {
1297 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1298 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1299 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1300 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1301 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1302 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1303 } else {
1304 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1305 }
1306 break;
1307 }
1308 case nir_op_imax: {
1309 if (dst.regClass() == v1) {
1310 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1311 } else if (dst.regClass() == s1) {
1312 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1313 } else {
1314 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1315 }
1316 break;
1317 }
1318 case nir_op_umax: {
1319 if (dst.regClass() == v1) {
1320 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1321 } else if (dst.regClass() == s1) {
1322 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1323 } else {
1324 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1325 }
1326 break;
1327 }
1328 case nir_op_imin: {
1329 if (dst.regClass() == v1) {
1330 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1331 } else if (dst.regClass() == s1) {
1332 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1333 } else {
1334 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1335 }
1336 break;
1337 }
1338 case nir_op_umin: {
1339 if (dst.regClass() == v1) {
1340 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1341 } else if (dst.regClass() == s1) {
1342 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1343 } else {
1344 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1345 }
1346 break;
1347 }
1348 case nir_op_ior: {
1349 if (instr->dest.dest.ssa.bit_size == 1) {
1350 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1351 } else if (dst.regClass() == v1) {
1352 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1353 } else if (dst.regClass() == v2) {
1354 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_or_b32, dst);
1355 } else if (dst.regClass() == s1) {
1356 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1357 } else if (dst.regClass() == s2) {
1358 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1359 } else {
1360 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1361 }
1362 break;
1363 }
1364 case nir_op_iand: {
1365 if (instr->dest.dest.ssa.bit_size == 1) {
1366 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1367 } else if (dst.regClass() == v1) {
1368 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1369 } else if (dst.regClass() == v2) {
1370 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_and_b32, dst);
1371 } else if (dst.regClass() == s1) {
1372 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1373 } else if (dst.regClass() == s2) {
1374 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1375 } else {
1376 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1377 }
1378 break;
1379 }
1380 case nir_op_ixor: {
1381 if (instr->dest.dest.ssa.bit_size == 1) {
1382 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1383 } else if (dst.regClass() == v1) {
1384 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1385 } else if (dst.regClass() == v2) {
1386 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_xor_b32, dst);
1387 } else if (dst.regClass() == s1) {
1388 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1389 } else if (dst.regClass() == s2) {
1390 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1391 } else {
1392 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1393 }
1394 break;
1395 }
1396 case nir_op_ushr: {
1397 if (dst.regClass() == v1) {
1398 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1399 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1400 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1401 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1402 } else if (dst.regClass() == v2) {
1403 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1404 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1405 } else if (dst.regClass() == s2) {
1406 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1407 } else if (dst.regClass() == s1) {
1408 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1409 } else {
1410 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1411 }
1412 break;
1413 }
1414 case nir_op_ishl: {
1415 if (dst.regClass() == v1) {
1416 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1417 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1418 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1419 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1420 } else if (dst.regClass() == v2) {
1421 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1422 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1423 } else if (dst.regClass() == s1) {
1424 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1425 } else if (dst.regClass() == s2) {
1426 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1427 } else {
1428 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1429 }
1430 break;
1431 }
1432 case nir_op_ishr: {
1433 if (dst.regClass() == v1) {
1434 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1435 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1436 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1437 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1438 } else if (dst.regClass() == v2) {
1439 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1440 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1441 } else if (dst.regClass() == s1) {
1442 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1443 } else if (dst.regClass() == s2) {
1444 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1445 } else {
1446 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1447 }
1448 break;
1449 }
1450 case nir_op_find_lsb: {
1451 Temp src = get_alu_src(ctx, instr->src[0]);
1452 if (src.regClass() == s1) {
1453 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1454 } else if (src.regClass() == v1) {
1455 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1456 } else if (src.regClass() == s2) {
1457 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1458 } else {
1459 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1460 }
1461 break;
1462 }
1463 case nir_op_ufind_msb:
1464 case nir_op_ifind_msb: {
1465 Temp src = get_alu_src(ctx, instr->src[0]);
1466 if (src.regClass() == s1 || src.regClass() == s2) {
1467 aco_opcode op = src.regClass() == s2 ?
1468 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1469 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1470 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1471
1472 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1473 Operand(src.size() * 32u - 1u), msb_rev);
1474 Temp msb = sub.def(0).getTemp();
1475 Temp carry = sub.def(1).getTemp();
1476
1477 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1478 } else if (src.regClass() == v1) {
1479 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1480 Temp msb_rev = bld.tmp(v1);
1481 emit_vop1_instruction(ctx, instr, op, msb_rev);
1482 Temp msb = bld.tmp(v1);
1483 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1484 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1485 } else {
1486 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1487 }
1488 break;
1489 }
1490 case nir_op_bitfield_reverse: {
1491 if (dst.regClass() == s1) {
1492 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1493 } else if (dst.regClass() == v1) {
1494 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1495 } else {
1496 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1497 }
1498 break;
1499 }
1500 case nir_op_iadd: {
1501 if (dst.regClass() == s1) {
1502 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1503 break;
1504 }
1505
1506 Temp src0 = get_alu_src(ctx, instr->src[0]);
1507 Temp src1 = get_alu_src(ctx, instr->src[1]);
1508 if (dst.regClass() == v1) {
1509 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1510 break;
1511 }
1512
1513 assert(src0.size() == 2 && src1.size() == 2);
1514 Temp src00 = bld.tmp(src0.type(), 1);
1515 Temp src01 = bld.tmp(dst.type(), 1);
1516 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1517 Temp src10 = bld.tmp(src1.type(), 1);
1518 Temp src11 = bld.tmp(dst.type(), 1);
1519 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1520
1521 if (dst.regClass() == s2) {
1522 Temp carry = bld.tmp(s1);
1523 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1524 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1525 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1526 } else if (dst.regClass() == v2) {
1527 Temp dst0 = bld.tmp(v1);
1528 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1529 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1530 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1531 } else {
1532 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1533 }
1534 break;
1535 }
1536 case nir_op_uadd_sat: {
1537 Temp src0 = get_alu_src(ctx, instr->src[0]);
1538 Temp src1 = get_alu_src(ctx, instr->src[1]);
1539 if (dst.regClass() == s1) {
1540 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1541 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1542 src0, src1);
1543 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1544 } else if (dst.regClass() == v1) {
1545 if (ctx->options->chip_class >= GFX9) {
1546 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1547 add->operands[0] = Operand(src0);
1548 add->operands[1] = Operand(src1);
1549 add->definitions[0] = Definition(dst);
1550 add->clamp = 1;
1551 ctx->block->instructions.emplace_back(std::move(add));
1552 } else {
1553 if (src1.regClass() != v1)
1554 std::swap(src0, src1);
1555 assert(src1.regClass() == v1);
1556 Temp tmp = bld.tmp(v1);
1557 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1558 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1559 }
1560 } else {
1561 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1562 }
1563 break;
1564 }
1565 case nir_op_uadd_carry: {
1566 Temp src0 = get_alu_src(ctx, instr->src[0]);
1567 Temp src1 = get_alu_src(ctx, instr->src[1]);
1568 if (dst.regClass() == s1) {
1569 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1570 break;
1571 }
1572 if (dst.regClass() == v1) {
1573 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1574 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1575 break;
1576 }
1577
1578 Temp src00 = bld.tmp(src0.type(), 1);
1579 Temp src01 = bld.tmp(dst.type(), 1);
1580 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1581 Temp src10 = bld.tmp(src1.type(), 1);
1582 Temp src11 = bld.tmp(dst.type(), 1);
1583 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1584 if (dst.regClass() == s2) {
1585 Temp carry = bld.tmp(s1);
1586 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1587 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1588 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1589 } else if (dst.regClass() == v2) {
1590 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1591 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1592 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1593 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1594 } else {
1595 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1596 }
1597 break;
1598 }
1599 case nir_op_isub: {
1600 if (dst.regClass() == s1) {
1601 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1602 break;
1603 }
1604
1605 Temp src0 = get_alu_src(ctx, instr->src[0]);
1606 Temp src1 = get_alu_src(ctx, instr->src[1]);
1607 if (dst.regClass() == v1) {
1608 bld.vsub32(Definition(dst), src0, src1);
1609 break;
1610 }
1611
1612 Temp src00 = bld.tmp(src0.type(), 1);
1613 Temp src01 = bld.tmp(dst.type(), 1);
1614 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1615 Temp src10 = bld.tmp(src1.type(), 1);
1616 Temp src11 = bld.tmp(dst.type(), 1);
1617 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1618 if (dst.regClass() == s2) {
1619 Temp carry = bld.tmp(s1);
1620 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1621 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1622 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1623 } else if (dst.regClass() == v2) {
1624 Temp lower = bld.tmp(v1);
1625 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1626 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1627 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1628 } else {
1629 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1630 }
1631 break;
1632 }
1633 case nir_op_usub_borrow: {
1634 Temp src0 = get_alu_src(ctx, instr->src[0]);
1635 Temp src1 = get_alu_src(ctx, instr->src[1]);
1636 if (dst.regClass() == s1) {
1637 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1638 break;
1639 } else if (dst.regClass() == v1) {
1640 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1641 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1642 break;
1643 }
1644
1645 Temp src00 = bld.tmp(src0.type(), 1);
1646 Temp src01 = bld.tmp(dst.type(), 1);
1647 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1648 Temp src10 = bld.tmp(src1.type(), 1);
1649 Temp src11 = bld.tmp(dst.type(), 1);
1650 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1651 if (dst.regClass() == s2) {
1652 Temp borrow = bld.tmp(s1);
1653 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1654 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1655 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1656 } else if (dst.regClass() == v2) {
1657 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1658 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1659 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1660 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1661 } else {
1662 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1663 }
1664 break;
1665 }
1666 case nir_op_imul: {
1667 if (dst.regClass() == v1) {
1668 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1669 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1670 } else if (dst.regClass() == s1) {
1671 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1672 } else {
1673 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1674 }
1675 break;
1676 }
1677 case nir_op_umul_high: {
1678 if (dst.regClass() == v1) {
1679 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1680 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1681 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1682 } else if (dst.regClass() == s1) {
1683 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1684 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1685 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1686 } else {
1687 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1688 }
1689 break;
1690 }
1691 case nir_op_imul_high: {
1692 if (dst.regClass() == v1) {
1693 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1694 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1695 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1696 } else if (dst.regClass() == s1) {
1697 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1698 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1699 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1700 } else {
1701 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1702 }
1703 break;
1704 }
1705 case nir_op_fmul: {
1706 Temp src0 = get_alu_src(ctx, instr->src[0]);
1707 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1708 if (dst.regClass() == v2b) {
1709 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1710 } else if (dst.regClass() == v1) {
1711 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1712 } else if (dst.regClass() == v2) {
1713 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1714 } else {
1715 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1716 }
1717 break;
1718 }
1719 case nir_op_fadd: {
1720 Temp src0 = get_alu_src(ctx, instr->src[0]);
1721 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1722 if (dst.regClass() == v2b) {
1723 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1724 } else if (dst.regClass() == v1) {
1725 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1726 } else if (dst.regClass() == v2) {
1727 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1728 } else {
1729 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1730 }
1731 break;
1732 }
1733 case nir_op_fsub: {
1734 Temp src0 = get_alu_src(ctx, instr->src[0]);
1735 Temp src1 = get_alu_src(ctx, instr->src[1]);
1736 if (dst.regClass() == v2b) {
1737 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1738 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1739 else
1740 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1741 } else if (dst.regClass() == v1) {
1742 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1743 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1744 else
1745 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1746 } else if (dst.regClass() == v2) {
1747 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1748 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1749 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1750 sub->neg[1] = true;
1751 } else {
1752 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1753 }
1754 break;
1755 }
1756 case nir_op_fmax: {
1757 Temp src0 = get_alu_src(ctx, instr->src[0]);
1758 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1759 if (dst.regClass() == v2b) {
1760 // TODO: check fp_mode.must_flush_denorms16_64
1761 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1762 } else if (dst.regClass() == v1) {
1763 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1764 } else if (dst.regClass() == v2) {
1765 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1766 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1767 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1768 } else {
1769 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1770 }
1771 } else {
1772 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1773 }
1774 break;
1775 }
1776 case nir_op_fmin: {
1777 Temp src0 = get_alu_src(ctx, instr->src[0]);
1778 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1779 if (dst.regClass() == v2b) {
1780 // TODO: check fp_mode.must_flush_denorms16_64
1781 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1782 } else if (dst.regClass() == v1) {
1783 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1784 } else if (dst.regClass() == v2) {
1785 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1786 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1787 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1788 } else {
1789 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1790 }
1791 } else {
1792 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1793 }
1794 break;
1795 }
1796 case nir_op_cube_face_coord: {
1797 Temp in = get_alu_src(ctx, instr->src[0], 3);
1798 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1799 emit_extract_vector(ctx, in, 1, v1),
1800 emit_extract_vector(ctx, in, 2, v1) };
1801 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1802 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1803 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1804 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1805 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1),
1806 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, ma), Operand(0x3f000000u/*0.5*/));
1807 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1),
1808 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, ma), Operand(0x3f000000u/*0.5*/));
1809 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1810 break;
1811 }
1812 case nir_op_cube_face_index: {
1813 Temp in = get_alu_src(ctx, instr->src[0], 3);
1814 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1815 emit_extract_vector(ctx, in, 1, v1),
1816 emit_extract_vector(ctx, in, 2, v1) };
1817 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1818 break;
1819 }
1820 case nir_op_bcsel: {
1821 emit_bcsel(ctx, instr, dst);
1822 break;
1823 }
1824 case nir_op_frsq: {
1825 Temp src = get_alu_src(ctx, instr->src[0]);
1826 if (dst.regClass() == v2b) {
1827 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1828 } else if (dst.regClass() == v1) {
1829 emit_rsq(ctx, bld, Definition(dst), src);
1830 } else if (dst.regClass() == v2) {
1831 /* Lowered at NIR level for precision reasons. */
1832 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1833 } else {
1834 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1835 }
1836 break;
1837 }
1838 case nir_op_fneg: {
1839 Temp src = get_alu_src(ctx, instr->src[0]);
1840 if (dst.regClass() == v2b) {
1841 if (ctx->block->fp_mode.must_flush_denorms16_64)
1842 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1843 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1844 } else if (dst.regClass() == v1) {
1845 if (ctx->block->fp_mode.must_flush_denorms32)
1846 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1847 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1848 } else if (dst.regClass() == v2) {
1849 if (ctx->block->fp_mode.must_flush_denorms16_64)
1850 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1851 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1852 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1853 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1854 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1855 } else {
1856 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1857 }
1858 break;
1859 }
1860 case nir_op_fabs: {
1861 Temp src = get_alu_src(ctx, instr->src[0]);
1862 if (dst.regClass() == v2b) {
1863 if (ctx->block->fp_mode.must_flush_denorms16_64)
1864 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1865 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1866 } else if (dst.regClass() == v1) {
1867 if (ctx->block->fp_mode.must_flush_denorms32)
1868 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1869 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1870 } else if (dst.regClass() == v2) {
1871 if (ctx->block->fp_mode.must_flush_denorms16_64)
1872 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1873 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1874 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1875 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1876 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1877 } else {
1878 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1879 }
1880 break;
1881 }
1882 case nir_op_fsat: {
1883 Temp src = get_alu_src(ctx, instr->src[0]);
1884 if (dst.regClass() == v2b) {
1885 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src);
1886 } else if (dst.regClass() == v1) {
1887 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1888 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1889 // TODO: confirm that this holds under any circumstances
1890 } else if (dst.regClass() == v2) {
1891 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1892 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1893 vop3->clamp = true;
1894 } else {
1895 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1896 }
1897 break;
1898 }
1899 case nir_op_flog2: {
1900 Temp src = get_alu_src(ctx, instr->src[0]);
1901 if (dst.regClass() == v2b) {
1902 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
1903 } else if (dst.regClass() == v1) {
1904 emit_log2(ctx, bld, Definition(dst), src);
1905 } else {
1906 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1907 }
1908 break;
1909 }
1910 case nir_op_frcp: {
1911 Temp src = get_alu_src(ctx, instr->src[0]);
1912 if (dst.regClass() == v2b) {
1913 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
1914 } else if (dst.regClass() == v1) {
1915 emit_rcp(ctx, bld, Definition(dst), src);
1916 } else if (dst.regClass() == v2) {
1917 /* Lowered at NIR level for precision reasons. */
1918 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1919 } else {
1920 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1921 }
1922 break;
1923 }
1924 case nir_op_fexp2: {
1925 if (dst.regClass() == v2b) {
1926 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
1927 } else if (dst.regClass() == v1) {
1928 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1929 } else {
1930 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1931 }
1932 break;
1933 }
1934 case nir_op_fsqrt: {
1935 Temp src = get_alu_src(ctx, instr->src[0]);
1936 if (dst.regClass() == v2b) {
1937 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
1938 } else if (dst.regClass() == v1) {
1939 emit_sqrt(ctx, bld, Definition(dst), src);
1940 } else if (dst.regClass() == v2) {
1941 /* Lowered at NIR level for precision reasons. */
1942 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1943 } else {
1944 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1945 }
1946 break;
1947 }
1948 case nir_op_ffract: {
1949 if (dst.regClass() == v2b) {
1950 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
1951 } else if (dst.regClass() == v1) {
1952 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1953 } else if (dst.regClass() == v2) {
1954 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1955 } else {
1956 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1957 }
1958 break;
1959 }
1960 case nir_op_ffloor: {
1961 Temp src = get_alu_src(ctx, instr->src[0]);
1962 if (dst.regClass() == v2b) {
1963 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
1964 } else if (dst.regClass() == v1) {
1965 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1966 } else if (dst.regClass() == v2) {
1967 emit_floor_f64(ctx, bld, Definition(dst), src);
1968 } else {
1969 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1970 }
1971 break;
1972 }
1973 case nir_op_fceil: {
1974 Temp src0 = get_alu_src(ctx, instr->src[0]);
1975 if (dst.regClass() == v2b) {
1976 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
1977 } else if (dst.regClass() == v1) {
1978 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1979 } else if (dst.regClass() == v2) {
1980 if (ctx->options->chip_class >= GFX7) {
1981 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1982 } else {
1983 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1984 /* trunc = trunc(src0)
1985 * if (src0 > 0.0 && src0 != trunc)
1986 * trunc += 1.0
1987 */
1988 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
1989 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
1990 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
1991 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
1992 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);
1993 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
1994 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
1995 }
1996 } else {
1997 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
1998 }
1999 break;
2000 }
2001 case nir_op_ftrunc: {
2002 Temp src = get_alu_src(ctx, instr->src[0]);
2003 if (dst.regClass() == v2b) {
2004 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2005 } else if (dst.regClass() == v1) {
2006 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2007 } else if (dst.regClass() == v2) {
2008 emit_trunc_f64(ctx, bld, Definition(dst), src);
2009 } else {
2010 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2011 }
2012 break;
2013 }
2014 case nir_op_fround_even: {
2015 Temp src0 = get_alu_src(ctx, instr->src[0]);
2016 if (dst.regClass() == v2b) {
2017 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2018 } else if (dst.regClass() == v1) {
2019 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2020 } else if (dst.regClass() == v2) {
2021 if (ctx->options->chip_class >= GFX7) {
2022 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2023 } else {
2024 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2025 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2026 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2027
2028 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2029 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));
2030 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));
2031 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));
2032 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2033 tmp = sub->definitions[0].getTemp();
2034
2035 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2036 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2037 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2038 Temp cond = vop3->definitions[0].getTemp();
2039
2040 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2041 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2042 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2043 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2044
2045 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2046 }
2047 } else {
2048 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2049 }
2050 break;
2051 }
2052 case nir_op_fsin:
2053 case nir_op_fcos: {
2054 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2055 aco_ptr<Instruction> norm;
2056 if (dst.regClass() == v2b) {
2057 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3118u));
2058 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2059 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2060 bld.vop1(opcode, Definition(dst), tmp);
2061 } else if (dst.regClass() == v1) {
2062 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2063 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2064
2065 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2066 if (ctx->options->chip_class < GFX9)
2067 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2068
2069 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2070 bld.vop1(opcode, Definition(dst), tmp);
2071 } else {
2072 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2073 }
2074 break;
2075 }
2076 case nir_op_ldexp: {
2077 Temp src0 = get_alu_src(ctx, instr->src[0]);
2078 Temp src1 = get_alu_src(ctx, instr->src[1]);
2079 if (dst.regClass() == v2b) {
2080 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2081 } else if (dst.regClass() == v1) {
2082 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2083 } else if (dst.regClass() == v2) {
2084 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2085 } else {
2086 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2087 }
2088 break;
2089 }
2090 case nir_op_frexp_sig: {
2091 Temp src = get_alu_src(ctx, instr->src[0]);
2092 if (dst.regClass() == v2b) {
2093 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2094 } else if (dst.regClass() == v1) {
2095 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2096 } else if (dst.regClass() == v2) {
2097 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2098 } else {
2099 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2100 }
2101 break;
2102 }
2103 case nir_op_frexp_exp: {
2104 Temp src = get_alu_src(ctx, instr->src[0]);
2105 if (instr->src[0].src.ssa->bit_size == 16) {
2106 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2107 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2108 convert_int(ctx, bld, tmp, 8, 32, true, dst);
2109 } else if (instr->src[0].src.ssa->bit_size == 32) {
2110 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2111 } else if (instr->src[0].src.ssa->bit_size == 64) {
2112 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2113 } else {
2114 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2115 }
2116 break;
2117 }
2118 case nir_op_fsign: {
2119 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2120 if (dst.regClass() == v2b) {
2121 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2122 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2123 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2124 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2125 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2126 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2127 } else if (dst.regClass() == v1) {
2128 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2129 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2130 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2131 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2132 } else if (dst.regClass() == v2) {
2133 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2134 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2135 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2136
2137 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2138 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2139 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2140
2141 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2142 } else {
2143 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2144 }
2145 break;
2146 }
2147 case nir_op_f2f16:
2148 case nir_op_f2f16_rtne: {
2149 Temp src = get_alu_src(ctx, instr->src[0]);
2150 if (instr->src[0].src.ssa->bit_size == 64)
2151 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2152 if (instr->op == nir_op_f2f16_rtne && ctx->block->fp_mode.round16_64 != fp_round_ne)
2153 /* We emit s_round_mode/s_setreg_imm32 in lower_to_hw_instr to
2154 * keep value numbering and the scheduler simpler.
2155 */
2156 bld.vop1(aco_opcode::p_cvt_f16_f32_rtne, Definition(dst), src);
2157 else
2158 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2159 break;
2160 }
2161 case nir_op_f2f16_rtz: {
2162 Temp src = get_alu_src(ctx, instr->src[0]);
2163 if (instr->src[0].src.ssa->bit_size == 64)
2164 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2165 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2166 break;
2167 }
2168 case nir_op_f2f32: {
2169 if (instr->src[0].src.ssa->bit_size == 16) {
2170 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2171 } else if (instr->src[0].src.ssa->bit_size == 64) {
2172 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2173 } else {
2174 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2175 }
2176 break;
2177 }
2178 case nir_op_f2f64: {
2179 Temp src = get_alu_src(ctx, instr->src[0]);
2180 if (instr->src[0].src.ssa->bit_size == 16)
2181 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2182 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2183 break;
2184 }
2185 case nir_op_i2f16: {
2186 assert(dst.regClass() == v2b);
2187 Temp src = get_alu_src(ctx, instr->src[0]);
2188 if (instr->src[0].src.ssa->bit_size == 8)
2189 src = convert_int(ctx, bld, src, 8, 16, true);
2190 else if (instr->src[0].src.ssa->bit_size == 64)
2191 src = convert_int(ctx, bld, src, 64, 32, false);
2192 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2193 break;
2194 }
2195 case nir_op_i2f32: {
2196 assert(dst.size() == 1);
2197 Temp src = get_alu_src(ctx, instr->src[0]);
2198 if (instr->src[0].src.ssa->bit_size <= 16)
2199 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2200 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2201 break;
2202 }
2203 case nir_op_i2f64: {
2204 if (instr->src[0].src.ssa->bit_size <= 32) {
2205 Temp src = get_alu_src(ctx, instr->src[0]);
2206 if (instr->src[0].src.ssa->bit_size <= 16)
2207 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2208 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2209 } else if (instr->src[0].src.ssa->bit_size == 64) {
2210 Temp src = get_alu_src(ctx, instr->src[0]);
2211 RegClass rc = RegClass(src.type(), 1);
2212 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2213 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2214 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2215 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2216 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2217 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2218
2219 } else {
2220 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2221 }
2222 break;
2223 }
2224 case nir_op_u2f16: {
2225 assert(dst.regClass() == v2b);
2226 Temp src = get_alu_src(ctx, instr->src[0]);
2227 if (instr->src[0].src.ssa->bit_size == 8)
2228 src = convert_int(ctx, bld, src, 8, 16, false);
2229 else if (instr->src[0].src.ssa->bit_size == 64)
2230 src = convert_int(ctx, bld, src, 64, 32, false);
2231 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2232 break;
2233 }
2234 case nir_op_u2f32: {
2235 assert(dst.size() == 1);
2236 Temp src = get_alu_src(ctx, instr->src[0]);
2237 if (instr->src[0].src.ssa->bit_size == 8) {
2238 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2239 } else {
2240 if (instr->src[0].src.ssa->bit_size == 16)
2241 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2242 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2243 }
2244 break;
2245 }
2246 case nir_op_u2f64: {
2247 if (instr->src[0].src.ssa->bit_size <= 32) {
2248 Temp src = get_alu_src(ctx, instr->src[0]);
2249 if (instr->src[0].src.ssa->bit_size <= 16)
2250 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2251 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2252 } else if (instr->src[0].src.ssa->bit_size == 64) {
2253 Temp src = get_alu_src(ctx, instr->src[0]);
2254 RegClass rc = RegClass(src.type(), 1);
2255 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2256 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2257 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2258 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2259 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2260 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2261 } else {
2262 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2263 }
2264 break;
2265 }
2266 case nir_op_f2i8:
2267 case nir_op_f2i16: {
2268 if (instr->src[0].src.ssa->bit_size == 16)
2269 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i16_f16, dst);
2270 else if (instr->src[0].src.ssa->bit_size == 32)
2271 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2272 else
2273 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2274 break;
2275 }
2276 case nir_op_f2u8:
2277 case nir_op_f2u16: {
2278 if (instr->src[0].src.ssa->bit_size == 16)
2279 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u16_f16, dst);
2280 else if (instr->src[0].src.ssa->bit_size == 32)
2281 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2282 else
2283 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2284 break;
2285 }
2286 case nir_op_f2i32: {
2287 Temp src = get_alu_src(ctx, instr->src[0]);
2288 if (instr->src[0].src.ssa->bit_size == 16) {
2289 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2290 if (dst.type() == RegType::vgpr) {
2291 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2292 } else {
2293 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2294 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2295 }
2296 } else if (instr->src[0].src.ssa->bit_size == 32) {
2297 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2298 } else if (instr->src[0].src.ssa->bit_size == 64) {
2299 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2300 } else {
2301 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2302 }
2303 break;
2304 }
2305 case nir_op_f2u32: {
2306 Temp src = get_alu_src(ctx, instr->src[0]);
2307 if (instr->src[0].src.ssa->bit_size == 16) {
2308 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2309 if (dst.type() == RegType::vgpr) {
2310 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2311 } else {
2312 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2313 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2314 }
2315 } else if (instr->src[0].src.ssa->bit_size == 32) {
2316 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2317 } else if (instr->src[0].src.ssa->bit_size == 64) {
2318 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2319 } else {
2320 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2321 }
2322 break;
2323 }
2324 case nir_op_f2i64: {
2325 Temp src = get_alu_src(ctx, instr->src[0]);
2326 if (instr->src[0].src.ssa->bit_size == 16)
2327 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2328
2329 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2330 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2331 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2332 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2333 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2334 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2335 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2336 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2337 Temp new_exponent = bld.tmp(v1);
2338 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2339 if (ctx->program->chip_class >= GFX8)
2340 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2341 else
2342 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2343 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2344 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2345 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2346 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2347 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2348 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2349 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2350 Temp new_lower = bld.tmp(v1);
2351 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2352 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2353 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2354
2355 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2356 if (src.type() == RegType::vgpr)
2357 src = bld.as_uniform(src);
2358 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2359 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2360 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2361 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2362 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2363 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2364 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2365 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2366 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2367 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2368 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2369 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2370 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2371 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2372 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2373 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2374 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2375 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2376 Temp borrow = bld.tmp(s1);
2377 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2378 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2379 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2380
2381 } else if (instr->src[0].src.ssa->bit_size == 64) {
2382 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2383 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2384 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2385 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2386 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2387 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2388 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2389 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2390 if (dst.type() == RegType::sgpr) {
2391 lower = bld.as_uniform(lower);
2392 upper = bld.as_uniform(upper);
2393 }
2394 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2395
2396 } else {
2397 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2398 }
2399 break;
2400 }
2401 case nir_op_f2u64: {
2402 Temp src = get_alu_src(ctx, instr->src[0]);
2403 if (instr->src[0].src.ssa->bit_size == 16)
2404 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2405
2406 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2407 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2408 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2409 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2410 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2411 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2412 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2413 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2414 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2415 Temp new_exponent = bld.tmp(v1);
2416 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2417 if (ctx->program->chip_class >= GFX8)
2418 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2419 else
2420 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2421 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2422 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2423 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2424 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2425 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2426 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2427 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2428
2429 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2430 if (src.type() == RegType::vgpr)
2431 src = bld.as_uniform(src);
2432 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2433 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2434 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2435 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2436 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2437 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2438 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2439 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2440 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2441 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2442 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2443 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2444 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2445 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2446 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2447 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2448 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2449 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2450
2451 } else if (instr->src[0].src.ssa->bit_size == 64) {
2452 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2453 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2454 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2455 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2456 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2457 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2458 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2459 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2460 if (dst.type() == RegType::sgpr) {
2461 lower = bld.as_uniform(lower);
2462 upper = bld.as_uniform(upper);
2463 }
2464 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2465
2466 } else {
2467 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2468 }
2469 break;
2470 }
2471 case nir_op_b2f16: {
2472 Temp src = get_alu_src(ctx, instr->src[0]);
2473 assert(src.regClass() == bld.lm);
2474
2475 if (dst.regClass() == s1) {
2476 src = bool_to_scalar_condition(ctx, src);
2477 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2478 } else if (dst.regClass() == v2b) {
2479 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2480 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2481 } else {
2482 unreachable("Wrong destination register class for nir_op_b2f16.");
2483 }
2484 break;
2485 }
2486 case nir_op_b2f32: {
2487 Temp src = get_alu_src(ctx, instr->src[0]);
2488 assert(src.regClass() == bld.lm);
2489
2490 if (dst.regClass() == s1) {
2491 src = bool_to_scalar_condition(ctx, src);
2492 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2493 } else if (dst.regClass() == v1) {
2494 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2495 } else {
2496 unreachable("Wrong destination register class for nir_op_b2f32.");
2497 }
2498 break;
2499 }
2500 case nir_op_b2f64: {
2501 Temp src = get_alu_src(ctx, instr->src[0]);
2502 assert(src.regClass() == bld.lm);
2503
2504 if (dst.regClass() == s2) {
2505 src = bool_to_scalar_condition(ctx, src);
2506 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2507 } else if (dst.regClass() == v2) {
2508 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2509 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2510 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2511 } else {
2512 unreachable("Wrong destination register class for nir_op_b2f64.");
2513 }
2514 break;
2515 }
2516 case nir_op_i2i8:
2517 case nir_op_i2i16:
2518 case nir_op_i2i32:
2519 case nir_op_i2i64: {
2520 if (dst.type() == RegType::sgpr && instr->src[0].src.ssa->bit_size < 32) {
2521 /* no need to do the extract in get_alu_src() */
2522 sgpr_extract_mode mode = instr->dest.dest.ssa.bit_size > instr->src[0].src.ssa->bit_size ?
2523 sgpr_extract_sext : sgpr_extract_undef;
2524 extract_8_16_bit_sgpr_element(ctx, dst, &instr->src[0], mode);
2525 } else {
2526 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2527 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2528 }
2529 break;
2530 }
2531 case nir_op_u2u8:
2532 case nir_op_u2u16:
2533 case nir_op_u2u32:
2534 case nir_op_u2u64: {
2535 if (dst.type() == RegType::sgpr && instr->src[0].src.ssa->bit_size < 32) {
2536 /* no need to do the extract in get_alu_src() */
2537 sgpr_extract_mode mode = instr->dest.dest.ssa.bit_size > instr->src[0].src.ssa->bit_size ?
2538 sgpr_extract_zext : sgpr_extract_undef;
2539 extract_8_16_bit_sgpr_element(ctx, dst, &instr->src[0], mode);
2540 } else {
2541 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2542 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2543 }
2544 break;
2545 }
2546 case nir_op_b2b32:
2547 case nir_op_b2i8:
2548 case nir_op_b2i16:
2549 case nir_op_b2i32:
2550 case nir_op_b2i64: {
2551 Temp src = get_alu_src(ctx, instr->src[0]);
2552 assert(src.regClass() == bld.lm);
2553
2554 Temp tmp = dst.bytes() == 8 ? bld.tmp(RegClass::get(dst.type(), 4)) : dst;
2555 if (tmp.regClass() == s1) {
2556 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2557 bool_to_scalar_condition(ctx, src, tmp);
2558 } else if (tmp.type() == RegType::vgpr) {
2559 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(tmp), Operand(0u), Operand(1u), src);
2560 } else {
2561 unreachable("Invalid register class for b2i32");
2562 }
2563
2564 if (tmp != dst)
2565 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
2566 break;
2567 }
2568 case nir_op_b2b1:
2569 case nir_op_i2b1: {
2570 Temp src = get_alu_src(ctx, instr->src[0]);
2571 assert(dst.regClass() == bld.lm);
2572
2573 if (src.type() == RegType::vgpr) {
2574 assert(src.regClass() == v1 || src.regClass() == v2);
2575 assert(dst.regClass() == bld.lm);
2576 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2577 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2578 } else {
2579 assert(src.regClass() == s1 || src.regClass() == s2);
2580 Temp tmp;
2581 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2582 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2583 } else {
2584 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2585 bld.scc(bld.def(s1)), Operand(0u), src);
2586 }
2587 bool_to_vector_condition(ctx, tmp, dst);
2588 }
2589 break;
2590 }
2591 case nir_op_pack_64_2x32_split: {
2592 Temp src0 = get_alu_src(ctx, instr->src[0]);
2593 Temp src1 = get_alu_src(ctx, instr->src[1]);
2594
2595 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2596 break;
2597 }
2598 case nir_op_unpack_64_2x32_split_x:
2599 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2600 break;
2601 case nir_op_unpack_64_2x32_split_y:
2602 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2603 break;
2604 case nir_op_unpack_32_2x16_split_x:
2605 if (dst.type() == RegType::vgpr) {
2606 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2607 } else {
2608 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2609 }
2610 break;
2611 case nir_op_unpack_32_2x16_split_y:
2612 if (dst.type() == RegType::vgpr) {
2613 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2614 } else {
2615 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)));
2616 }
2617 break;
2618 case nir_op_pack_32_2x16_split: {
2619 Temp src0 = get_alu_src(ctx, instr->src[0]);
2620 Temp src1 = get_alu_src(ctx, instr->src[1]);
2621 if (dst.regClass() == v1) {
2622 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2623 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2624 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2625 } else {
2626 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2627 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2628 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2629 }
2630 break;
2631 }
2632 case nir_op_pack_half_2x16: {
2633 Temp src = get_alu_src(ctx, instr->src[0], 2);
2634
2635 if (dst.regClass() == v1) {
2636 Temp src0 = bld.tmp(v1);
2637 Temp src1 = bld.tmp(v1);
2638 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2639 if (0 && (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)) {
2640 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2641 } else {
2642 src0 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src0);
2643 src1 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src1);
2644 if (ctx->program->chip_class >= GFX10) {
2645 /* the high bits of v_cvt_f16_f32 isn't zero'd on GFX10 */
2646 bld.vop3(aco_opcode::v_pack_b32_f16, Definition(dst), src0, src1);
2647 } else {
2648 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst), src0, src1);
2649 }
2650 }
2651 } else {
2652 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2653 }
2654 break;
2655 }
2656 case nir_op_unpack_half_2x16_split_x: {
2657 if (dst.regClass() == v1) {
2658 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2659 } else {
2660 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2661 }
2662 break;
2663 }
2664 case nir_op_unpack_half_2x16_split_y: {
2665 if (dst.regClass() == v1) {
2666 /* TODO: use SDWA here */
2667 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2668 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2669 } else {
2670 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2671 }
2672 break;
2673 }
2674 case nir_op_fquantize2f16: {
2675 Temp src = get_alu_src(ctx, instr->src[0]);
2676 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2677 Temp f32, cmp_res;
2678
2679 if (ctx->program->chip_class >= GFX8) {
2680 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2681 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2682 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2683 } else {
2684 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2685 * so compare the result and flush to 0 if it's smaller.
2686 */
2687 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2688 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2689 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2690 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2691 cmp_res = vop3->definitions[0].getTemp();
2692 }
2693
2694 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2695 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2696 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2697 } else {
2698 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2699 }
2700 break;
2701 }
2702 case nir_op_bfm: {
2703 Temp bits = get_alu_src(ctx, instr->src[0]);
2704 Temp offset = get_alu_src(ctx, instr->src[1]);
2705
2706 if (dst.regClass() == s1) {
2707 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2708 } else if (dst.regClass() == v1) {
2709 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2710 } else {
2711 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2712 }
2713 break;
2714 }
2715 case nir_op_bitfield_select: {
2716 /* (mask & insert) | (~mask & base) */
2717 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2718 Temp insert = get_alu_src(ctx, instr->src[1]);
2719 Temp base = get_alu_src(ctx, instr->src[2]);
2720
2721 /* dst = (insert & bitmask) | (base & ~bitmask) */
2722 if (dst.regClass() == s1) {
2723 aco_ptr<Instruction> sop2;
2724 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2725 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2726 Operand lhs;
2727 if (const_insert && const_bitmask) {
2728 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2729 } else {
2730 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2731 lhs = Operand(insert);
2732 }
2733
2734 Operand rhs;
2735 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2736 if (const_base && const_bitmask) {
2737 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2738 } else {
2739 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2740 rhs = Operand(base);
2741 }
2742
2743 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2744
2745 } else if (dst.regClass() == v1) {
2746 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2747 base = as_vgpr(ctx, base);
2748 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2749 insert = as_vgpr(ctx, insert);
2750
2751 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2752
2753 } else {
2754 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2755 }
2756 break;
2757 }
2758 case nir_op_ubfe:
2759 case nir_op_ibfe: {
2760 Temp base = get_alu_src(ctx, instr->src[0]);
2761 Temp offset = get_alu_src(ctx, instr->src[1]);
2762 Temp bits = get_alu_src(ctx, instr->src[2]);
2763
2764 if (dst.bytes() != 4)
2765 unreachable("Unsupported BFE bit size");
2766
2767 if (dst.type() == RegType::sgpr) {
2768 Operand extract;
2769 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2770 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2771 if (const_offset && const_bits) {
2772 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2773 extract = Operand(const_extract);
2774 } else {
2775 Operand width;
2776 if (const_bits) {
2777 width = Operand(const_bits->u32 << 16);
2778 } else {
2779 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2780 }
2781 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2782 }
2783
2784 aco_opcode opcode = instr->op == nir_op_ubfe ? aco_opcode::s_bfe_u32 : aco_opcode::s_bfe_i32;
2785 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2786
2787 } else {
2788 aco_opcode opcode = instr->op == nir_op_ubfe ? aco_opcode::v_bfe_u32 : aco_opcode::v_bfe_i32;
2789 emit_vop3a_instruction(ctx, instr, opcode, dst);
2790 }
2791 break;
2792 }
2793 case nir_op_bit_count: {
2794 Temp src = get_alu_src(ctx, instr->src[0]);
2795 if (src.regClass() == s1) {
2796 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2797 } else if (src.regClass() == v1) {
2798 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2799 } else if (src.regClass() == v2) {
2800 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2801 emit_extract_vector(ctx, src, 1, v1),
2802 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2803 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2804 } else if (src.regClass() == s2) {
2805 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2806 } else {
2807 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
2808 }
2809 break;
2810 }
2811 case nir_op_flt: {
2812 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2813 break;
2814 }
2815 case nir_op_fge: {
2816 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2817 break;
2818 }
2819 case nir_op_feq: {
2820 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2821 break;
2822 }
2823 case nir_op_fneu: {
2824 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2825 break;
2826 }
2827 case nir_op_ilt: {
2828 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);
2829 break;
2830 }
2831 case nir_op_ige: {
2832 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);
2833 break;
2834 }
2835 case nir_op_ieq: {
2836 if (instr->src[0].src.ssa->bit_size == 1)
2837 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2838 else
2839 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,
2840 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2841 break;
2842 }
2843 case nir_op_ine: {
2844 if (instr->src[0].src.ssa->bit_size == 1)
2845 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2846 else
2847 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,
2848 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2849 break;
2850 }
2851 case nir_op_ult: {
2852 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);
2853 break;
2854 }
2855 case nir_op_uge: {
2856 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);
2857 break;
2858 }
2859 case nir_op_fddx:
2860 case nir_op_fddy:
2861 case nir_op_fddx_fine:
2862 case nir_op_fddy_fine:
2863 case nir_op_fddx_coarse:
2864 case nir_op_fddy_coarse: {
2865 Temp src = get_alu_src(ctx, instr->src[0]);
2866 uint16_t dpp_ctrl1, dpp_ctrl2;
2867 if (instr->op == nir_op_fddx_fine) {
2868 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2869 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2870 } else if (instr->op == nir_op_fddy_fine) {
2871 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2872 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2873 } else {
2874 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2875 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2876 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2877 else
2878 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2879 }
2880
2881 Temp tmp;
2882 if (ctx->program->chip_class >= GFX8) {
2883 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2884 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2885 } else {
2886 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2887 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2888 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2889 }
2890 emit_wqm(ctx, tmp, dst, true);
2891 break;
2892 }
2893 default:
2894 isel_err(&instr->instr, "Unknown NIR ALU instr");
2895 }
2896 }
2897
2898 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
2899 {
2900 Temp dst = get_ssa_temp(ctx, &instr->def);
2901
2902 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2903 // which get truncated the lsb if double and msb if int
2904 // for now, we only use s_mov_b64 with 64bit inline constants
2905 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
2906 assert(dst.type() == RegType::sgpr);
2907
2908 Builder bld(ctx->program, ctx->block);
2909
2910 if (instr->def.bit_size == 1) {
2911 assert(dst.regClass() == bld.lm);
2912 int val = instr->value[0].b ? -1 : 0;
2913 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
2914 bld.sop1(Builder::s_mov, Definition(dst), op);
2915 } else if (instr->def.bit_size == 8) {
2916 /* ensure that the value is correctly represented in the low byte of the register */
2917 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
2918 } else if (instr->def.bit_size == 16) {
2919 /* ensure that the value is correctly represented in the low half of the register */
2920 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
2921 } else if (dst.size() == 1) {
2922 bld.copy(Definition(dst), Operand(instr->value[0].u32));
2923 } else {
2924 assert(dst.size() != 1);
2925 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
2926 if (instr->def.bit_size == 64)
2927 for (unsigned i = 0; i < dst.size(); i++)
2928 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
2929 else {
2930 for (unsigned i = 0; i < dst.size(); i++)
2931 vec->operands[i] = Operand{instr->value[i].u32};
2932 }
2933 vec->definitions[0] = Definition(dst);
2934 ctx->block->instructions.emplace_back(std::move(vec));
2935 }
2936 }
2937
2938 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
2939 {
2940 uint32_t new_mask = 0;
2941 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
2942 if (mask & (1u << i))
2943 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
2944 return new_mask;
2945 }
2946
2947 struct LoadEmitInfo {
2948 Operand offset;
2949 Temp dst;
2950 unsigned num_components;
2951 unsigned component_size;
2952 Temp resource = Temp(0, s1);
2953 unsigned component_stride = 0;
2954 unsigned const_offset = 0;
2955 unsigned align_mul = 0;
2956 unsigned align_offset = 0;
2957
2958 bool glc = false;
2959 unsigned swizzle_component_size = 0;
2960 memory_sync_info sync;
2961 Temp soffset = Temp(0, s1);
2962 };
2963
2964 using LoadCallback = Temp(*)(
2965 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
2966 unsigned align, unsigned const_offset, Temp dst_hint);
2967
2968 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
2969 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
2970 {
2971 unsigned load_size = info->num_components * info->component_size;
2972 unsigned component_size = info->component_size;
2973
2974 unsigned num_vals = 0;
2975 Temp vals[info->dst.bytes()];
2976
2977 unsigned const_offset = info->const_offset;
2978
2979 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
2980 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
2981
2982 unsigned bytes_read = 0;
2983 while (bytes_read < load_size) {
2984 unsigned bytes_needed = load_size - bytes_read;
2985
2986 /* add buffer for unaligned loads */
2987 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
2988
2989 if (byte_align) {
2990 if ((bytes_needed > 2 ||
2991 (bytes_needed == 2 && (align_mul % 2 || align_offset % 2)) ||
2992 !supports_8bit_16bit_loads) && byte_align_loads) {
2993 if (info->component_stride) {
2994 assert(supports_8bit_16bit_loads && "unimplemented");
2995 bytes_needed = 2;
2996 byte_align = 0;
2997 } else {
2998 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
2999 bytes_needed = align(bytes_needed, 4);
3000 }
3001 } else {
3002 byte_align = 0;
3003 }
3004 }
3005
3006 if (info->swizzle_component_size)
3007 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3008 if (info->component_stride)
3009 bytes_needed = MIN2(bytes_needed, info->component_size);
3010
3011 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3012
3013 /* reduce constant offset */
3014 Operand offset = info->offset;
3015 unsigned reduced_const_offset = const_offset;
3016 bool remove_const_offset_completely = need_to_align_offset;
3017 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3018 unsigned to_add = const_offset;
3019 if (remove_const_offset_completely) {
3020 reduced_const_offset = 0;
3021 } else {
3022 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3023 reduced_const_offset %= max_const_offset_plus_one;
3024 }
3025 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3026 if (offset.isConstant()) {
3027 offset = Operand(offset.constantValue() + to_add);
3028 } else if (offset_tmp.regClass() == s1) {
3029 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3030 offset_tmp, Operand(to_add));
3031 } else if (offset_tmp.regClass() == v1) {
3032 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3033 } else {
3034 Temp lo = bld.tmp(offset_tmp.type(), 1);
3035 Temp hi = bld.tmp(offset_tmp.type(), 1);
3036 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3037
3038 if (offset_tmp.regClass() == s2) {
3039 Temp carry = bld.tmp(s1);
3040 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3041 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3042 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3043 } else {
3044 Temp new_lo = bld.tmp(v1);
3045 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3046 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3047 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3048 }
3049 }
3050 }
3051
3052 /* align offset down if needed */
3053 Operand aligned_offset = offset;
3054 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3055 if (need_to_align_offset) {
3056 align = 4;
3057 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3058 if (offset.isConstant()) {
3059 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3060 } else if (offset_tmp.regClass() == s1) {
3061 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3062 } else if (offset_tmp.regClass() == s2) {
3063 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3064 } else if (offset_tmp.regClass() == v1) {
3065 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3066 } else if (offset_tmp.regClass() == v2) {
3067 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3068 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3069 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3070 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3071 }
3072 }
3073 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3074 bld.copy(bld.def(s1), aligned_offset);
3075
3076 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3077 reduced_const_offset, byte_align ? Temp() : info->dst);
3078
3079 /* the callback wrote directly to dst */
3080 if (val == info->dst) {
3081 assert(num_vals == 0);
3082 emit_split_vector(ctx, info->dst, info->num_components);
3083 return;
3084 }
3085
3086 /* shift result right if needed */
3087 if (info->component_size < 4 && byte_align_loads) {
3088 Operand align((uint32_t)byte_align);
3089 if (byte_align == -1) {
3090 if (offset.isConstant())
3091 align = Operand(offset.constantValue() % 4u);
3092 else if (offset.size() == 2)
3093 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3094 else
3095 align = offset;
3096 }
3097
3098 assert(val.bytes() >= load_size && "unimplemented");
3099 if (val.type() == RegType::sgpr)
3100 byte_align_scalar(ctx, val, align, info->dst);
3101 else
3102 byte_align_vector(ctx, val, align, info->dst, component_size);
3103 return;
3104 }
3105
3106 /* add result to list and advance */
3107 if (info->component_stride) {
3108 assert(val.bytes() == info->component_size && "unimplemented");
3109 const_offset += info->component_stride;
3110 align_offset = (align_offset + info->component_stride) % align_mul;
3111 } else {
3112 const_offset += val.bytes();
3113 align_offset = (align_offset + val.bytes()) % align_mul;
3114 }
3115 bytes_read += val.bytes();
3116 vals[num_vals++] = val;
3117 }
3118
3119 /* create array of components */
3120 unsigned components_split = 0;
3121 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3122 bool has_vgprs = false;
3123 for (unsigned i = 0; i < num_vals;) {
3124 Temp tmp[num_vals];
3125 unsigned num_tmps = 0;
3126 unsigned tmp_size = 0;
3127 RegType reg_type = RegType::sgpr;
3128 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3129 if (vals[i].type() == RegType::vgpr)
3130 reg_type = RegType::vgpr;
3131 tmp_size += vals[i].bytes();
3132 tmp[num_tmps++] = vals[i++];
3133 }
3134 if (num_tmps > 1) {
3135 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3136 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3137 for (unsigned i = 0; i < num_tmps; i++)
3138 vec->operands[i] = Operand(tmp[i]);
3139 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3140 vec->definitions[0] = Definition(tmp[0]);
3141 bld.insert(std::move(vec));
3142 }
3143
3144 if (tmp[0].bytes() % component_size) {
3145 /* trim tmp[0] */
3146 assert(i == num_vals);
3147 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3148 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3149 }
3150
3151 RegClass elem_rc = RegClass::get(reg_type, component_size);
3152
3153 unsigned start = components_split;
3154
3155 if (tmp_size == elem_rc.bytes()) {
3156 allocated_vec[components_split++] = tmp[0];
3157 } else {
3158 assert(tmp_size % elem_rc.bytes() == 0);
3159 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3160 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3161 for (unsigned i = 0; i < split->definitions.size(); i++) {
3162 Temp component = bld.tmp(elem_rc);
3163 allocated_vec[components_split++] = component;
3164 split->definitions[i] = Definition(component);
3165 }
3166 split->operands[0] = Operand(tmp[0]);
3167 bld.insert(std::move(split));
3168 }
3169
3170 /* try to p_as_uniform early so we can create more optimizable code and
3171 * also update allocated_vec */
3172 for (unsigned j = start; j < components_split; j++) {
3173 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3174 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3175 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3176 }
3177 }
3178
3179 /* concatenate components and p_as_uniform() result if needed */
3180 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3181 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3182
3183 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3184
3185 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3186 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3187 for (unsigned i = 0; i < info->num_components; i++)
3188 vec->operands[i] = Operand(allocated_vec[i]);
3189 if (padding_bytes)
3190 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3191 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3192 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3193 vec->definitions[0] = Definition(tmp);
3194 bld.insert(std::move(vec));
3195 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3196 } else {
3197 vec->definitions[0] = Definition(info->dst);
3198 bld.insert(std::move(vec));
3199 }
3200 }
3201
3202 Operand load_lds_size_m0(Builder& bld)
3203 {
3204 /* TODO: m0 does not need to be initialized on GFX9+ */
3205 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3206 }
3207
3208 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3209 Temp offset, unsigned bytes_needed,
3210 unsigned align, unsigned const_offset,
3211 Temp dst_hint)
3212 {
3213 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3214
3215 Operand m = load_lds_size_m0(bld);
3216
3217 bool large_ds_read = bld.program->chip_class >= GFX7;
3218 bool usable_read2 = bld.program->chip_class >= GFX7;
3219
3220 bool read2 = false;
3221 unsigned size = 0;
3222 aco_opcode op;
3223 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3224 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3225 size = 16;
3226 op = aco_opcode::ds_read_b128;
3227 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3228 size = 16;
3229 read2 = true;
3230 op = aco_opcode::ds_read2_b64;
3231 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3232 size = 12;
3233 op = aco_opcode::ds_read_b96;
3234 } else if (bytes_needed >= 8 && align % 8 == 0) {
3235 size = 8;
3236 op = aco_opcode::ds_read_b64;
3237 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3238 size = 8;
3239 read2 = true;
3240 op = aco_opcode::ds_read2_b32;
3241 } else if (bytes_needed >= 4 && align % 4 == 0) {
3242 size = 4;
3243 op = aco_opcode::ds_read_b32;
3244 } else if (bytes_needed >= 2 && align % 2 == 0) {
3245 size = 2;
3246 op = aco_opcode::ds_read_u16;
3247 } else {
3248 size = 1;
3249 op = aco_opcode::ds_read_u8;
3250 }
3251
3252 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3253 if (const_offset >= max_offset_plus_one) {
3254 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3255 const_offset %= max_offset_plus_one;
3256 }
3257
3258 if (read2)
3259 const_offset /= (size / 2u);
3260
3261 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3262 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3263 Instruction *instr;
3264 if (read2)
3265 instr = bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3266 else
3267 instr = bld.ds(op, Definition(val), offset, m, const_offset);
3268 static_cast<DS_instruction *>(instr)->sync = info->sync;
3269
3270 if (size < 4)
3271 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3272
3273 return val;
3274 }
3275
3276 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3277
3278 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3279 Temp offset, unsigned bytes_needed,
3280 unsigned align, unsigned const_offset,
3281 Temp dst_hint)
3282 {
3283 unsigned size = 0;
3284 aco_opcode op;
3285 if (bytes_needed <= 4) {
3286 size = 1;
3287 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3288 } else if (bytes_needed <= 8) {
3289 size = 2;
3290 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3291 } else if (bytes_needed <= 16) {
3292 size = 4;
3293 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3294 } else if (bytes_needed <= 32) {
3295 size = 8;
3296 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3297 } else {
3298 size = 16;
3299 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3300 }
3301 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3302 if (info->resource.id()) {
3303 load->operands[0] = Operand(info->resource);
3304 load->operands[1] = Operand(offset);
3305 } else {
3306 load->operands[0] = Operand(offset);
3307 load->operands[1] = Operand(0u);
3308 }
3309 RegClass rc(RegType::sgpr, size);
3310 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3311 load->definitions[0] = Definition(val);
3312 load->glc = info->glc;
3313 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3314 load->sync = info->sync;
3315 bld.insert(std::move(load));
3316 return val;
3317 }
3318
3319 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3320
3321 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3322 Temp offset, unsigned bytes_needed,
3323 unsigned align_, unsigned const_offset,
3324 Temp dst_hint)
3325 {
3326 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3327 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3328
3329 if (info->soffset.id()) {
3330 if (soffset.isTemp())
3331 vaddr = bld.copy(bld.def(v1), soffset);
3332 soffset = Operand(info->soffset);
3333 }
3334
3335 unsigned bytes_size = 0;
3336 aco_opcode op;
3337 if (bytes_needed == 1 || align_ % 2) {
3338 bytes_size = 1;
3339 op = aco_opcode::buffer_load_ubyte;
3340 } else if (bytes_needed == 2 || align_ % 4) {
3341 bytes_size = 2;
3342 op = aco_opcode::buffer_load_ushort;
3343 } else if (bytes_needed <= 4) {
3344 bytes_size = 4;
3345 op = aco_opcode::buffer_load_dword;
3346 } else if (bytes_needed <= 8) {
3347 bytes_size = 8;
3348 op = aco_opcode::buffer_load_dwordx2;
3349 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3350 bytes_size = 12;
3351 op = aco_opcode::buffer_load_dwordx3;
3352 } else {
3353 bytes_size = 16;
3354 op = aco_opcode::buffer_load_dwordx4;
3355 }
3356 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3357 mubuf->operands[0] = Operand(info->resource);
3358 mubuf->operands[1] = vaddr;
3359 mubuf->operands[2] = soffset;
3360 mubuf->offen = (offset.type() == RegType::vgpr);
3361 mubuf->glc = info->glc;
3362 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3363 mubuf->sync = info->sync;
3364 mubuf->offset = const_offset;
3365 mubuf->swizzled = info->swizzle_component_size != 0;
3366 RegClass rc = RegClass::get(RegType::vgpr, bytes_size);
3367 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3368 mubuf->definitions[0] = Definition(val);
3369 bld.insert(std::move(mubuf));
3370
3371 return val;
3372 }
3373
3374 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3375 static auto emit_scratch_load = emit_load<mubuf_load_callback, false, true, 4096>;
3376
3377 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3378 {
3379 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3380 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3381
3382 if (addr.type() == RegType::vgpr)
3383 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3384 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3385 }
3386
3387 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3388 Temp offset, unsigned bytes_needed,
3389 unsigned align_, unsigned const_offset,
3390 Temp dst_hint)
3391 {
3392 unsigned bytes_size = 0;
3393 bool mubuf = bld.program->chip_class == GFX6;
3394 bool global = bld.program->chip_class >= GFX9;
3395 aco_opcode op;
3396 if (bytes_needed == 1) {
3397 bytes_size = 1;
3398 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3399 } else if (bytes_needed == 2) {
3400 bytes_size = 2;
3401 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3402 } else if (bytes_needed <= 4) {
3403 bytes_size = 4;
3404 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3405 } else if (bytes_needed <= 8) {
3406 bytes_size = 8;
3407 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3408 } else if (bytes_needed <= 12 && !mubuf) {
3409 bytes_size = 12;
3410 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3411 } else {
3412 bytes_size = 16;
3413 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3414 }
3415 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3416 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3417 if (mubuf) {
3418 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3419 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3420 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3421 mubuf->operands[2] = Operand(0u);
3422 mubuf->glc = info->glc;
3423 mubuf->dlc = false;
3424 mubuf->offset = 0;
3425 mubuf->addr64 = offset.type() == RegType::vgpr;
3426 mubuf->disable_wqm = false;
3427 mubuf->sync = info->sync;
3428 mubuf->definitions[0] = Definition(val);
3429 bld.insert(std::move(mubuf));
3430 } else {
3431 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3432
3433 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3434 flat->operands[0] = Operand(offset);
3435 flat->operands[1] = Operand(s1);
3436 flat->glc = info->glc;
3437 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3438 flat->sync = info->sync;
3439 flat->offset = 0u;
3440 flat->definitions[0] = Definition(val);
3441 bld.insert(std::move(flat));
3442 }
3443
3444 return val;
3445 }
3446
3447 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3448
3449 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3450 Temp address, unsigned base_offset, unsigned align)
3451 {
3452 assert(util_is_power_of_two_nonzero(align));
3453
3454 Builder bld(ctx->program, ctx->block);
3455
3456 unsigned num_components = dst.bytes() / elem_size_bytes;
3457 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3458 info.align_mul = align;
3459 info.align_offset = 0;
3460 info.sync = memory_sync_info(storage_shared);
3461 info.const_offset = base_offset;
3462 emit_lds_load(ctx, bld, &info);
3463
3464 return dst;
3465 }
3466
3467 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3468 {
3469 if (!count)
3470 return;
3471
3472 Builder bld(ctx->program, ctx->block);
3473
3474 ASSERTED bool is_subdword = false;
3475 for (unsigned i = 0; i < count; i++)
3476 is_subdword |= offsets[i] % 4;
3477 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3478 assert(!is_subdword || dst_type == RegType::vgpr);
3479
3480 /* count == 1 fast path */
3481 if (count == 1) {
3482 if (dst_type == RegType::sgpr)
3483 dst[0] = bld.as_uniform(src);
3484 else
3485 dst[0] = as_vgpr(ctx, src);
3486 return;
3487 }
3488
3489 for (unsigned i = 0; i < count - 1; i++)
3490 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3491 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3492
3493 if (is_subdword && src.type() == RegType::sgpr) {
3494 src = as_vgpr(ctx, src);
3495 } else {
3496 /* use allocated_vec if possible */
3497 auto it = ctx->allocated_vec.find(src.id());
3498 if (it != ctx->allocated_vec.end()) {
3499 if (!it->second[0].id())
3500 goto split;
3501 unsigned elem_size = it->second[0].bytes();
3502 assert(src.bytes() % elem_size == 0);
3503
3504 for (unsigned i = 0; i < src.bytes() / elem_size; i++) {
3505 if (!it->second[i].id())
3506 goto split;
3507 }
3508
3509 for (unsigned i = 0; i < count; i++) {
3510 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3511 goto split;
3512 }
3513
3514 for (unsigned i = 0; i < count; i++) {
3515 unsigned start_idx = offsets[i] / elem_size;
3516 unsigned op_count = dst[i].bytes() / elem_size;
3517 if (op_count == 1) {
3518 if (dst_type == RegType::sgpr)
3519 dst[i] = bld.as_uniform(it->second[start_idx]);
3520 else
3521 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3522 continue;
3523 }
3524
3525 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3526 for (unsigned j = 0; j < op_count; j++) {
3527 Temp tmp = it->second[start_idx + j];
3528 if (dst_type == RegType::sgpr)
3529 tmp = bld.as_uniform(tmp);
3530 vec->operands[j] = Operand(tmp);
3531 }
3532 vec->definitions[0] = Definition(dst[i]);
3533 bld.insert(std::move(vec));
3534 }
3535 return;
3536 }
3537 }
3538
3539 split:
3540
3541 if (dst_type == RegType::sgpr)
3542 src = bld.as_uniform(src);
3543
3544 /* just split it */
3545 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3546 split->operands[0] = Operand(src);
3547 for (unsigned i = 0; i < count; i++)
3548 split->definitions[i] = Definition(dst[i]);
3549 bld.insert(std::move(split));
3550 }
3551
3552 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3553 int *start, int *count)
3554 {
3555 unsigned start_elem = ffs(todo_mask) - 1;
3556 bool skip = !(mask & (1 << start_elem));
3557 if (skip)
3558 mask = ~mask & todo_mask;
3559
3560 mask &= todo_mask;
3561
3562 u_bit_scan_consecutive_range(&mask, start, count);
3563
3564 return !skip;
3565 }
3566
3567 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3568 {
3569 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3570 }
3571
3572 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3573 Temp address, unsigned base_offset, unsigned align)
3574 {
3575 assert(util_is_power_of_two_nonzero(align));
3576 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3577
3578 Builder bld(ctx->program, ctx->block);
3579 bool large_ds_write = ctx->options->chip_class >= GFX7;
3580 bool usable_write2 = ctx->options->chip_class >= GFX7;
3581
3582 unsigned write_count = 0;
3583 Temp write_datas[32];
3584 unsigned offsets[32];
3585 aco_opcode opcodes[32];
3586
3587 wrmask = widen_mask(wrmask, elem_size_bytes);
3588
3589 uint32_t todo = u_bit_consecutive(0, data.bytes());
3590 while (todo) {
3591 int offset, bytes;
3592 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3593 offsets[write_count] = offset;
3594 opcodes[write_count] = aco_opcode::num_opcodes;
3595 write_count++;
3596 advance_write_mask(&todo, offset, bytes);
3597 continue;
3598 }
3599
3600 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3601 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3602 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3603 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3604
3605 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3606 aco_opcode op = aco_opcode::num_opcodes;
3607 if (bytes >= 16 && aligned16 && large_ds_write) {
3608 op = aco_opcode::ds_write_b128;
3609 bytes = 16;
3610 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3611 op = aco_opcode::ds_write_b96;
3612 bytes = 12;
3613 } else if (bytes >= 8 && aligned8) {
3614 op = aco_opcode::ds_write_b64;
3615 bytes = 8;
3616 } else if (bytes >= 4 && aligned4) {
3617 op = aco_opcode::ds_write_b32;
3618 bytes = 4;
3619 } else if (bytes >= 2 && aligned2) {
3620 op = aco_opcode::ds_write_b16;
3621 bytes = 2;
3622 } else if (bytes >= 1) {
3623 op = aco_opcode::ds_write_b8;
3624 bytes = 1;
3625 } else {
3626 assert(false);
3627 }
3628
3629 offsets[write_count] = offset;
3630 opcodes[write_count] = op;
3631 write_count++;
3632 advance_write_mask(&todo, offset, bytes);
3633 }
3634
3635 Operand m = load_lds_size_m0(bld);
3636
3637 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3638
3639 for (unsigned i = 0; i < write_count; i++) {
3640 aco_opcode op = opcodes[i];
3641 if (op == aco_opcode::num_opcodes)
3642 continue;
3643
3644 Temp data = write_datas[i];
3645
3646 unsigned second = write_count;
3647 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3648 for (second = i + 1; second < write_count; second++) {
3649 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3650 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3651 opcodes[second] = aco_opcode::num_opcodes;
3652 break;
3653 }
3654 }
3655 }
3656
3657 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3658 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3659
3660 unsigned inline_offset = base_offset + offsets[i];
3661 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3662 Temp address_offset = address;
3663 if (inline_offset > max_offset) {
3664 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3665 inline_offset = offsets[i];
3666 }
3667 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3668
3669 Instruction *instr;
3670 if (write2) {
3671 Temp second_data = write_datas[second];
3672 inline_offset /= data.bytes();
3673 instr = bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3674 } else {
3675 instr = bld.ds(op, address_offset, data, m, inline_offset);
3676 }
3677 static_cast<DS_instruction *>(instr)->sync =
3678 memory_sync_info(storage_shared);
3679 }
3680 }
3681
3682 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3683 {
3684 unsigned align = 16;
3685 if (const_offset)
3686 align = std::min(align, 1u << (ffs(const_offset) - 1));
3687
3688 return align;
3689 }
3690
3691
3692 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3693 {
3694 switch (bytes) {
3695 case 1:
3696 assert(!smem);
3697 return aco_opcode::buffer_store_byte;
3698 case 2:
3699 assert(!smem);
3700 return aco_opcode::buffer_store_short;
3701 case 4:
3702 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3703 case 8:
3704 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3705 case 12:
3706 assert(!smem);
3707 return aco_opcode::buffer_store_dwordx3;
3708 case 16:
3709 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3710 }
3711 unreachable("Unexpected store size");
3712 return aco_opcode::num_opcodes;
3713 }
3714
3715 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3716 Temp data, unsigned writemask, int swizzle_element_size,
3717 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3718 {
3719 unsigned write_count_with_skips = 0;
3720 bool skips[16];
3721
3722 /* determine how to split the data */
3723 unsigned todo = u_bit_consecutive(0, data.bytes());
3724 while (todo) {
3725 int offset, bytes;
3726 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3727 offsets[write_count_with_skips] = offset;
3728 if (skips[write_count_with_skips]) {
3729 advance_write_mask(&todo, offset, bytes);
3730 write_count_with_skips++;
3731 continue;
3732 }
3733
3734 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3735 * larger than swizzle_element_size */
3736 bytes = MIN2(bytes, swizzle_element_size);
3737 if (bytes % 4)
3738 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3739
3740 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3741 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3742 bytes = 8;
3743
3744 /* dword or larger stores have to be dword-aligned */
3745 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3746 unsigned align_offset = (instr ? nir_intrinsic_align_offset(instr) : 0) + offset;
3747 bool dword_aligned = align_offset % 4 == 0 && align_mul % 4 == 0;
3748 if (!dword_aligned)
3749 bytes = MIN2(bytes, (align_offset % 2 == 0 && align_mul % 2 == 0) ? 2 : 1);
3750
3751 advance_write_mask(&todo, offset, bytes);
3752 write_count_with_skips++;
3753 }
3754
3755 /* actually split data */
3756 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3757
3758 /* remove skips */
3759 for (unsigned i = 0; i < write_count_with_skips; i++) {
3760 if (skips[i])
3761 continue;
3762 write_datas[*write_count] = write_datas[i];
3763 offsets[*write_count] = offsets[i];
3764 (*write_count)++;
3765 }
3766 }
3767
3768 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3769 unsigned split_cnt = 0u, Temp dst = Temp())
3770 {
3771 Builder bld(ctx->program, ctx->block);
3772 unsigned dword_size = elem_size_bytes / 4;
3773
3774 if (!dst.id())
3775 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3776
3777 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3778 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3779 instr->definitions[0] = Definition(dst);
3780
3781 for (unsigned i = 0; i < cnt; ++i) {
3782 if (arr[i].id()) {
3783 assert(arr[i].size() == dword_size);
3784 allocated_vec[i] = arr[i];
3785 instr->operands[i] = Operand(arr[i]);
3786 } else {
3787 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3788 allocated_vec[i] = zero;
3789 instr->operands[i] = Operand(zero);
3790 }
3791 }
3792
3793 bld.insert(std::move(instr));
3794
3795 if (split_cnt)
3796 emit_split_vector(ctx, dst, split_cnt);
3797 else
3798 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3799
3800 return dst;
3801 }
3802
3803 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3804 {
3805 if (const_offset >= 4096) {
3806 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3807 const_offset %= 4096u;
3808
3809 if (!voffset.id())
3810 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3811 else if (unlikely(voffset.regClass() == s1))
3812 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3813 else if (likely(voffset.regClass() == v1))
3814 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3815 else
3816 unreachable("Unsupported register class of voffset");
3817 }
3818
3819 return const_offset;
3820 }
3821
3822 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3823 unsigned const_offset = 0u, memory_sync_info sync=memory_sync_info(),
3824 bool slc = false, bool swizzled = false)
3825 {
3826 assert(vdata.id());
3827 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3828 assert(vdata.size() >= 1 && vdata.size() <= 4);
3829
3830 Builder bld(ctx->program, ctx->block);
3831 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
3832 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3833
3834 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3835 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3836 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3837 /* offen */ !voffset_op.isUndefined(), /* swizzled */ swizzled,
3838 /* idxen*/ false, /* addr64 */ false, /* disable_wqm */ false, /* glc */ true,
3839 /* dlc*/ false, /* slc */ slc);
3840
3841 static_cast<MUBUF_instruction *>(r.instr)->sync = sync;
3842 }
3843
3844 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3845 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3846 bool allow_combining = true, memory_sync_info sync=memory_sync_info(), bool slc = false)
3847 {
3848 Builder bld(ctx->program, ctx->block);
3849 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
3850 assert(write_mask);
3851 write_mask = widen_mask(write_mask, elem_size_bytes);
3852
3853 unsigned write_count = 0;
3854 Temp write_datas[32];
3855 unsigned offsets[32];
3856 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
3857 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
3858
3859 for (unsigned i = 0; i < write_count; i++) {
3860 unsigned const_offset = offsets[i] + base_const_offset;
3861 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, sync, slc, !allow_combining);
3862 }
3863 }
3864
3865 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3866 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3867 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3868 {
3869 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
3870 assert((num_components * elem_size_bytes) == dst.bytes());
3871 assert(!!stride != allow_combining);
3872
3873 Builder bld(ctx->program, ctx->block);
3874
3875 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
3876 info.component_stride = allow_combining ? 0 : stride;
3877 info.glc = true;
3878 info.swizzle_component_size = allow_combining ? 0 : 4;
3879 info.align_mul = MIN2(elem_size_bytes, 4);
3880 info.align_offset = 0;
3881 info.soffset = soffset;
3882 info.const_offset = base_const_offset;
3883 emit_mubuf_load(ctx, bld, &info);
3884 }
3885
3886 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)
3887 {
3888 Builder bld(ctx->program, ctx->block);
3889 Temp offset = base_offset.first;
3890 unsigned const_offset = base_offset.second;
3891
3892 if (!nir_src_is_const(*off_src)) {
3893 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3894 Temp with_stride;
3895
3896 /* Calculate indirect offset with stride */
3897 if (likely(indirect_offset_arg.regClass() == v1))
3898 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
3899 else if (indirect_offset_arg.regClass() == s1)
3900 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3901 else
3902 unreachable("Unsupported register class of indirect offset");
3903
3904 /* Add to the supplied base offset */
3905 if (offset.id() == 0)
3906 offset = with_stride;
3907 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
3908 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
3909 else if (offset.size() == 1 && with_stride.size() == 1)
3910 offset = bld.vadd32(bld.def(v1), with_stride, offset);
3911 else
3912 unreachable("Unsupported register class of indirect offset");
3913 } else {
3914 unsigned const_offset_arg = nir_src_as_uint(*off_src);
3915 const_offset += const_offset_arg * stride;
3916 }
3917
3918 return std::make_pair(offset, const_offset);
3919 }
3920
3921 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
3922 {
3923 Builder bld(ctx->program, ctx->block);
3924 Temp offset;
3925
3926 if (off1.first.id() && off2.first.id()) {
3927 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
3928 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
3929 else if (off1.first.size() == 1 && off2.first.size() == 1)
3930 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
3931 else
3932 unreachable("Unsupported register class of indirect offset");
3933 } else {
3934 offset = off1.first.id() ? off1.first : off2.first;
3935 }
3936
3937 return std::make_pair(offset, off1.second + off2.second);
3938 }
3939
3940 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
3941 {
3942 Builder bld(ctx->program, ctx->block);
3943 unsigned const_offset = offs.second * multiplier;
3944
3945 if (!offs.first.id())
3946 return std::make_pair(offs.first, const_offset);
3947
3948 Temp offset = unlikely(offs.first.regClass() == s1)
3949 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
3950 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
3951
3952 return std::make_pair(offset, const_offset);
3953 }
3954
3955 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
3956 {
3957 Builder bld(ctx->program, ctx->block);
3958
3959 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3960 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
3961 /* component is in bytes */
3962 const_offset += nir_intrinsic_component(instr) * component_stride;
3963
3964 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3965 nir_src *off_src = nir_get_io_offset_src(instr);
3966 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
3967 }
3968
3969 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
3970 {
3971 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
3972 }
3973
3974 Temp get_tess_rel_patch_id(isel_context *ctx)
3975 {
3976 Builder bld(ctx->program, ctx->block);
3977
3978 switch (ctx->shader->info.stage) {
3979 case MESA_SHADER_TESS_CTRL:
3980 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
3981 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
3982 case MESA_SHADER_TESS_EVAL:
3983 return get_arg(ctx, ctx->args->tes_rel_patch_id);
3984 default:
3985 unreachable("Unsupported stage in get_tess_rel_patch_id");
3986 }
3987 }
3988
3989 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3990 {
3991 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3992 Builder bld(ctx->program, ctx->block);
3993
3994 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
3995 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
3996
3997 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
3998
3999 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4000 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4001
4002 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4003 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4004 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4005
4006 return offset_mul(ctx, offs, 4u);
4007 }
4008
4009 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4010 {
4011 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4012 Builder bld(ctx->program, ctx->block);
4013
4014 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4015 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4016 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4017 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4018
4019 std::pair<Temp, unsigned> offs = instr
4020 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4021 : std::make_pair(Temp(), 0u);
4022
4023 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4024 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4025
4026 if (per_vertex) {
4027 assert(instr);
4028
4029 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4030 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4031
4032 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4033 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4034 } else {
4035 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4036 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4037 }
4038
4039 return offs;
4040 }
4041
4042 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4043 {
4044 Builder bld(ctx->program, ctx->block);
4045
4046 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4047 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4048
4049 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4050
4051 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4052 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4053 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4054
4055 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4056 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4057
4058 return offs;
4059 }
4060
4061 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4062 {
4063 Builder bld(ctx->program, ctx->block);
4064
4065 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4066 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4067 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4068 unsigned attr_stride = ctx->tcs_num_patches;
4069
4070 std::pair<Temp, unsigned> offs = instr
4071 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4072 : std::make_pair(Temp(), 0u);
4073
4074 if (const_base_offset)
4075 offs.second += const_base_offset * attr_stride;
4076
4077 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4078 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4079 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4080
4081 return offs;
4082 }
4083
4084 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4085 {
4086 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4087
4088 if (mask == 0)
4089 return false;
4090
4091 unsigned drv_loc = nir_intrinsic_base(instr);
4092 nir_src *off_src = nir_get_io_offset_src(instr);
4093
4094 if (!nir_src_is_const(*off_src)) {
4095 *indirect = true;
4096 return false;
4097 }
4098
4099 *indirect = false;
4100 uint64_t slot = per_vertex
4101 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4102 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4103 return (((uint64_t) 1) << slot) & mask;
4104 }
4105
4106 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4107 {
4108 unsigned write_mask = nir_intrinsic_write_mask(instr);
4109 unsigned component = nir_intrinsic_component(instr);
4110 unsigned idx = nir_intrinsic_base(instr) + component;
4111
4112 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4113 if (off_instr->type != nir_instr_type_load_const)
4114 return false;
4115
4116 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4117 idx += nir_src_as_uint(instr->src[1]) * 4u;
4118
4119 if (instr->src[0].ssa->bit_size == 64)
4120 write_mask = widen_mask(write_mask, 2);
4121
4122 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4123
4124 for (unsigned i = 0; i < 8; ++i) {
4125 if (write_mask & (1 << i)) {
4126 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4127 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4128 }
4129 idx++;
4130 }
4131
4132 return true;
4133 }
4134
4135 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4136 {
4137 /* Only TCS per-vertex inputs are supported by this function.
4138 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4139 */
4140 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4141 return false;
4142
4143 nir_src *off_src = nir_get_io_offset_src(instr);
4144 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4145 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4146 bool can_use_temps = nir_src_is_const(*off_src) &&
4147 vertex_index_instr->type == nir_instr_type_intrinsic &&
4148 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4149
4150 if (!can_use_temps)
4151 return false;
4152
4153 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4154 Temp *src = &ctx->inputs.temps[idx];
4155 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4156
4157 return true;
4158 }
4159
4160 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4161 {
4162 Builder bld(ctx->program, ctx->block);
4163
4164 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4165 /* 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. */
4166 bool indirect_write;
4167 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4168 if (temp_only_input && !indirect_write)
4169 return;
4170 }
4171
4172 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4173 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4174 unsigned write_mask = nir_intrinsic_write_mask(instr);
4175 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4176
4177 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4178 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4179 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4180 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4181 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, memory_sync_info(), true);
4182 } else {
4183 Temp lds_base;
4184
4185 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4186 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4187 unsigned itemsize = ctx->stage == vertex_geometry_gs
4188 ? ctx->program->info->vs.es_info.esgs_itemsize
4189 : ctx->program->info->tes.es_info.esgs_itemsize;
4190 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4191 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));
4192 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4193 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4194 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4195 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4196 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4197 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4198 */
4199 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4200 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4201 } else {
4202 unreachable("Invalid LS or ES stage");
4203 }
4204
4205 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4206 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4207 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4208 }
4209 }
4210
4211 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4212 {
4213 if (per_vertex)
4214 return false;
4215
4216 unsigned off = nir_intrinsic_base(instr) * 4u;
4217 return off == ctx->tcs_tess_lvl_out_loc ||
4218 off == ctx->tcs_tess_lvl_in_loc;
4219
4220 }
4221
4222 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4223 {
4224 uint64_t mask = per_vertex
4225 ? ctx->program->info->tcs.tes_inputs_read
4226 : ctx->program->info->tcs.tes_patch_inputs_read;
4227
4228 bool indirect_write = false;
4229 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4230 return indirect_write || output_read_by_tes;
4231 }
4232
4233 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4234 {
4235 uint64_t mask = per_vertex
4236 ? ctx->shader->info.outputs_read
4237 : ctx->shader->info.patch_outputs_read;
4238
4239 bool indirect_write = false;
4240 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4241 return indirect_write || output_read;
4242 }
4243
4244 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4245 {
4246 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4247 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4248
4249 Builder bld(ctx->program, ctx->block);
4250
4251 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4252 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4253 unsigned write_mask = nir_intrinsic_write_mask(instr);
4254
4255 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4256 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4257 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4258
4259 if (write_to_vmem) {
4260 std::pair<Temp, unsigned> vmem_offs = per_vertex
4261 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4262 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4263
4264 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));
4265 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4266 store_vmem_mubuf(ctx, store_val, hs_ring_tess_offchip, vmem_offs.first, oc_lds, vmem_offs.second, elem_size_bytes, write_mask, true, memory_sync_info(storage_vmem_output));
4267 }
4268
4269 if (write_to_lds) {
4270 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4271 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4272 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4273 }
4274 }
4275
4276 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4277 {
4278 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4279 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4280
4281 Builder bld(ctx->program, ctx->block);
4282
4283 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4284 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4285 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4286 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4287
4288 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4289 }
4290
4291 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4292 {
4293 if (ctx->stage == vertex_vs ||
4294 ctx->stage == tess_eval_vs ||
4295 ctx->stage == fragment_fs ||
4296 ctx->stage == ngg_vertex_gs ||
4297 ctx->stage == ngg_tess_eval_gs ||
4298 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4299 bool stored_to_temps = store_output_to_temps(ctx, instr);
4300 if (!stored_to_temps) {
4301 isel_err(instr->src[1].ssa->parent_instr, "Unimplemented output offset instruction");
4302 abort();
4303 }
4304 } else if (ctx->stage == vertex_es ||
4305 ctx->stage == vertex_ls ||
4306 ctx->stage == tess_eval_es ||
4307 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4308 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4309 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4310 visit_store_ls_or_es_output(ctx, instr);
4311 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4312 visit_store_tcs_output(ctx, instr, false);
4313 } else {
4314 unreachable("Shader stage not implemented");
4315 }
4316 }
4317
4318 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4319 {
4320 visit_load_tcs_output(ctx, instr, false);
4321 }
4322
4323 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4324 {
4325 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4326 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4327
4328 Builder bld(ctx->program, ctx->block);
4329
4330 if (dst.regClass() == v2b) {
4331 if (ctx->program->has_16bank_lds) {
4332 assert(ctx->options->chip_class <= GFX8);
4333 Builder::Result interp_p1 =
4334 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4335 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4336 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4337 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4338 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4339 bld.m0(prim_mask), interp_p1, idx, component);
4340 } else {
4341 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4342
4343 if (ctx->options->chip_class == GFX8)
4344 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4345
4346 Builder::Result interp_p1 =
4347 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4348 coord1, bld.m0(prim_mask), idx, component);
4349 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4350 interp_p1, idx, component);
4351 }
4352 } else {
4353 Builder::Result interp_p1 =
4354 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4355 bld.m0(prim_mask), idx, component);
4356
4357 if (ctx->program->has_16bank_lds)
4358 interp_p1.instr->operands[0].setLateKill(true);
4359
4360 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4361 bld.m0(prim_mask), interp_p1, idx, component);
4362 }
4363 }
4364
4365 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4366 {
4367 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4368 for (unsigned i = 0; i < num_components; i++)
4369 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4370 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4371 assert(num_components == 4);
4372 Builder bld(ctx->program, ctx->block);
4373 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4374 }
4375
4376 for (Operand& op : vec->operands)
4377 op = op.isUndefined() ? Operand(0u) : op;
4378
4379 vec->definitions[0] = Definition(dst);
4380 ctx->block->instructions.emplace_back(std::move(vec));
4381 emit_split_vector(ctx, dst, num_components);
4382 return;
4383 }
4384
4385 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4386 {
4387 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4388 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4389 unsigned idx = nir_intrinsic_base(instr);
4390 unsigned component = nir_intrinsic_component(instr);
4391 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4392
4393 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4394 if (offset) {
4395 assert(offset->u32 == 0);
4396 } else {
4397 /* the lower 15bit of the prim_mask contain the offset into LDS
4398 * while the upper bits contain the number of prims */
4399 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4400 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4401 Builder bld(ctx->program, ctx->block);
4402 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4403 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4404 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4405 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4406 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4407 }
4408
4409 if (instr->dest.ssa.num_components == 1) {
4410 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4411 } else {
4412 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4413 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4414 {
4415 Temp tmp = {ctx->program->allocateId(), v1};
4416 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4417 vec->operands[i] = Operand(tmp);
4418 }
4419 vec->definitions[0] = Definition(dst);
4420 ctx->block->instructions.emplace_back(std::move(vec));
4421 }
4422 }
4423
4424 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4425 unsigned offset, unsigned stride, unsigned channels)
4426 {
4427 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4428 if (vtx_info->chan_byte_size != 4 && channels == 3)
4429 return false;
4430 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4431 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4432 }
4433
4434 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4435 unsigned offset, unsigned stride, unsigned *channels)
4436 {
4437 if (!vtx_info->chan_byte_size) {
4438 *channels = vtx_info->num_channels;
4439 return vtx_info->chan_format;
4440 }
4441
4442 unsigned num_channels = *channels;
4443 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4444 unsigned new_channels = num_channels + 1;
4445 /* first, assume more loads is worse and try using a larger data format */
4446 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4447 new_channels++;
4448 /* don't make the attribute potentially out-of-bounds */
4449 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4450 new_channels = 5;
4451 }
4452
4453 if (new_channels == 5) {
4454 /* then try decreasing load size (at the cost of more loads) */
4455 new_channels = *channels;
4456 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4457 new_channels--;
4458 }
4459
4460 if (new_channels < *channels)
4461 *channels = new_channels;
4462 num_channels = new_channels;
4463 }
4464
4465 switch (vtx_info->chan_format) {
4466 case V_008F0C_BUF_DATA_FORMAT_8:
4467 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4468 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4469 case V_008F0C_BUF_DATA_FORMAT_16:
4470 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4471 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4472 case V_008F0C_BUF_DATA_FORMAT_32:
4473 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4474 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4475 }
4476 unreachable("shouldn't reach here");
4477 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4478 }
4479
4480 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4481 * so we may need to fix it up. */
4482 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4483 {
4484 Builder bld(ctx->program, ctx->block);
4485
4486 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4487 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4488
4489 /* For the integer-like cases, do a natural sign extension.
4490 *
4491 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4492 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4493 * exponent.
4494 */
4495 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4496 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4497
4498 /* Convert back to the right type. */
4499 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4500 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4501 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4502 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4503 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4504 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4505 }
4506
4507 return alpha;
4508 }
4509
4510 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4511 {
4512 Builder bld(ctx->program, ctx->block);
4513 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4514 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4515
4516 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4517 if (off_instr->type != nir_instr_type_load_const) {
4518 isel_err(off_instr, "Unimplemented nir_intrinsic_load_input offset");
4519 }
4520 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4521
4522 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4523
4524 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4525 unsigned component = nir_intrinsic_component(instr);
4526 unsigned bitsize = instr->dest.ssa.bit_size;
4527 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4528 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4529 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4530 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4531
4532 unsigned dfmt = attrib_format & 0xf;
4533 unsigned nfmt = (attrib_format >> 4) & 0x7;
4534 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4535
4536 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4537 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4538 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4539 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4540 if (post_shuffle)
4541 num_channels = MAX2(num_channels, 3);
4542
4543 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4544 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4545
4546 Temp index;
4547 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4548 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4549 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4550 if (divisor) {
4551 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4552 if (divisor != 1) {
4553 Temp divided = bld.tmp(v1);
4554 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4555 index = bld.vadd32(bld.def(v1), start_instance, divided);
4556 } else {
4557 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4558 }
4559 } else {
4560 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4561 }
4562 } else {
4563 index = bld.vadd32(bld.def(v1),
4564 get_arg(ctx, ctx->args->ac.base_vertex),
4565 get_arg(ctx, ctx->args->ac.vertex_id));
4566 }
4567
4568 Temp channels[num_channels];
4569 unsigned channel_start = 0;
4570 bool direct_fetch = false;
4571
4572 /* skip unused channels at the start */
4573 if (vtx_info->chan_byte_size && !post_shuffle) {
4574 channel_start = ffs(mask) - 1;
4575 for (unsigned i = 0; i < channel_start; i++)
4576 channels[i] = Temp(0, s1);
4577 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4578 num_channels = 3 - (ffs(mask) - 1);
4579 }
4580
4581 /* load channels */
4582 while (channel_start < num_channels) {
4583 unsigned fetch_component = num_channels - channel_start;
4584 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4585 bool expanded = false;
4586
4587 /* use MUBUF when possible to avoid possible alignment issues */
4588 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4589 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4590 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4591 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4592 vtx_info->chan_byte_size == 4;
4593 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4594 if (!use_mubuf) {
4595 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4596 } else {
4597 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4598 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4599 fetch_component = 4;
4600 expanded = true;
4601 }
4602 }
4603
4604 unsigned fetch_bytes = fetch_component * bitsize / 8;
4605
4606 Temp fetch_index = index;
4607 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4608 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4609 fetch_offset = fetch_offset % attrib_stride;
4610 }
4611
4612 Operand soffset(0u);
4613 if (fetch_offset >= 4096) {
4614 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4615 fetch_offset %= 4096;
4616 }
4617
4618 aco_opcode opcode;
4619 switch (fetch_bytes) {
4620 case 2:
4621 assert(!use_mubuf && bitsize == 16);
4622 opcode = aco_opcode::tbuffer_load_format_d16_x;
4623 break;
4624 case 4:
4625 if (bitsize == 16) {
4626 assert(!use_mubuf);
4627 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4628 } else {
4629 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4630 }
4631 break;
4632 case 6:
4633 assert(!use_mubuf && bitsize == 16);
4634 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4635 break;
4636 case 8:
4637 if (bitsize == 16) {
4638 assert(!use_mubuf);
4639 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4640 } else {
4641 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4642 }
4643 break;
4644 case 12:
4645 assert(ctx->options->chip_class >= GFX7 ||
4646 (!use_mubuf && ctx->options->chip_class == GFX6));
4647 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4648 break;
4649 case 16:
4650 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4651 break;
4652 default:
4653 unreachable("Unimplemented load_input vector size");
4654 }
4655
4656 Temp fetch_dst;
4657 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4658 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4659 num_channels <= 3)) {
4660 direct_fetch = true;
4661 fetch_dst = dst;
4662 } else {
4663 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4664 }
4665
4666 if (use_mubuf) {
4667 bld.mubuf(opcode,
4668 Definition(fetch_dst), list, fetch_index, soffset,
4669 fetch_offset, false, false, true).instr;
4670 } else {
4671 bld.mtbuf(opcode,
4672 Definition(fetch_dst), list, fetch_index, soffset,
4673 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4674 }
4675
4676 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4677
4678 if (fetch_component == 1) {
4679 channels[channel_start] = fetch_dst;
4680 } else {
4681 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4682 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4683 bitsize == 16 ? v2b : v1);
4684 }
4685
4686 channel_start += fetch_component;
4687 }
4688
4689 if (!direct_fetch) {
4690 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4691 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4692
4693 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4694 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4695 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4696
4697 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4698 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4699 unsigned num_temp = 0;
4700 for (unsigned i = 0; i < dst.size(); i++) {
4701 unsigned idx = i + component;
4702 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4703 Temp channel = channels[swizzle[idx]];
4704 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4705 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4706 vec->operands[i] = Operand(channel);
4707
4708 num_temp++;
4709 elems[i] = channel;
4710 } else if (is_float && idx == 3) {
4711 vec->operands[i] = Operand(0x3f800000u);
4712 } else if (!is_float && idx == 3) {
4713 vec->operands[i] = Operand(1u);
4714 } else {
4715 vec->operands[i] = Operand(0u);
4716 }
4717 }
4718 vec->definitions[0] = Definition(dst);
4719 ctx->block->instructions.emplace_back(std::move(vec));
4720 emit_split_vector(ctx, dst, dst.size());
4721
4722 if (num_temp == dst.size())
4723 ctx->allocated_vec.emplace(dst.id(), elems);
4724 }
4725 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4726 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4727 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4728 if (off_instr->type != nir_instr_type_load_const ||
4729 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4730 isel_err(off_instr, "Unimplemented nir_intrinsic_load_input offset");
4731 }
4732
4733 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4734 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4735 if (offset) {
4736 assert(offset->u32 == 0);
4737 } else {
4738 /* the lower 15bit of the prim_mask contain the offset into LDS
4739 * while the upper bits contain the number of prims */
4740 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4741 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4742 Builder bld(ctx->program, ctx->block);
4743 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4744 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4745 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4746 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4747 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4748 }
4749
4750 unsigned idx = nir_intrinsic_base(instr);
4751 unsigned component = nir_intrinsic_component(instr);
4752 unsigned vertex_id = 2; /* P0 */
4753
4754 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4755 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4756 switch (src0->u32) {
4757 case 0:
4758 vertex_id = 2; /* P0 */
4759 break;
4760 case 1:
4761 vertex_id = 0; /* P10 */
4762 break;
4763 case 2:
4764 vertex_id = 1; /* P20 */
4765 break;
4766 default:
4767 unreachable("invalid vertex index");
4768 }
4769 }
4770
4771 if (dst.size() == 1) {
4772 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4773 } else {
4774 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4775 for (unsigned i = 0; i < dst.size(); i++)
4776 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4777 vec->definitions[0] = Definition(dst);
4778 bld.insert(std::move(vec));
4779 }
4780
4781 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4782 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4783 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4784 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4785 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4786
4787 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4788 } else {
4789 unreachable("Shader stage not implemented");
4790 }
4791 }
4792
4793 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4794 {
4795 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4796
4797 Builder bld(ctx->program, ctx->block);
4798 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4799 Temp vertex_offset;
4800
4801 if (!nir_src_is_const(*vertex_src)) {
4802 /* better code could be created, but this case probably doesn't happen
4803 * much in practice */
4804 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4805 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4806 Temp elem;
4807
4808 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4809 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4810 if (i % 2u)
4811 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4812 } else {
4813 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4814 }
4815
4816 if (vertex_offset.id()) {
4817 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4818 Operand(i), indirect_vertex);
4819 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4820 } else {
4821 vertex_offset = elem;
4822 }
4823 }
4824
4825 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4826 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4827 } else {
4828 unsigned vertex = nir_src_as_uint(*vertex_src);
4829 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4830 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4831 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4832 Operand((vertex % 2u) * 16u), Operand(16u));
4833 else
4834 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4835 }
4836
4837 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4838 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4839 return offset_mul(ctx, offs, 4u);
4840 }
4841
4842 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4843 {
4844 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4845
4846 Builder bld(ctx->program, ctx->block);
4847 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4848 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4849
4850 if (ctx->stage == geometry_gs) {
4851 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4852 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4853 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);
4854 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4855 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4856 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4857 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4858 } else {
4859 unreachable("Unsupported GS stage.");
4860 }
4861 }
4862
4863 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4864 {
4865 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4866
4867 Builder bld(ctx->program, ctx->block);
4868 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4869
4870 if (load_input_from_temps(ctx, instr, dst))
4871 return;
4872
4873 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4874 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4875 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4876
4877 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4878 }
4879
4880 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4881 {
4882 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4883
4884 Builder bld(ctx->program, ctx->block);
4885
4886 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4887 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4888 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4889
4890 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4891 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4892
4893 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4894 }
4895
4896 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4897 {
4898 switch (ctx->shader->info.stage) {
4899 case MESA_SHADER_GEOMETRY:
4900 visit_load_gs_per_vertex_input(ctx, instr);
4901 break;
4902 case MESA_SHADER_TESS_CTRL:
4903 visit_load_tcs_per_vertex_input(ctx, instr);
4904 break;
4905 case MESA_SHADER_TESS_EVAL:
4906 visit_load_tes_per_vertex_input(ctx, instr);
4907 break;
4908 default:
4909 unreachable("Unimplemented shader stage");
4910 }
4911 }
4912
4913 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4914 {
4915 visit_load_tcs_output(ctx, instr, true);
4916 }
4917
4918 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4919 {
4920 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4921 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4922
4923 visit_store_tcs_output(ctx, instr, true);
4924 }
4925
4926 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
4927 {
4928 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4929
4930 Builder bld(ctx->program, ctx->block);
4931 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4932
4933 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
4934 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
4935 Operand tes_w(0u);
4936
4937 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
4938 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
4939 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
4940 tes_w = Operand(tmp);
4941 }
4942
4943 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
4944 emit_split_vector(ctx, tess_coord, 3);
4945 }
4946
4947 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
4948 {
4949 if (ctx->program->info->need_indirect_descriptor_sets) {
4950 Builder bld(ctx->program, ctx->block);
4951 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
4952 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
4953 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
4954 }
4955
4956 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
4957 }
4958
4959
4960 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
4961 {
4962 Builder bld(ctx->program, ctx->block);
4963 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
4964 if (!nir_dest_is_divergent(instr->dest))
4965 index = bld.as_uniform(index);
4966 unsigned desc_set = nir_intrinsic_desc_set(instr);
4967 unsigned binding = nir_intrinsic_binding(instr);
4968
4969 Temp desc_ptr;
4970 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
4971 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
4972 unsigned offset = layout->binding[binding].offset;
4973 unsigned stride;
4974 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
4975 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
4976 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
4977 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
4978 offset = pipeline_layout->push_constant_size + 16 * idx;
4979 stride = 16;
4980 } else {
4981 desc_ptr = load_desc_ptr(ctx, desc_set);
4982 stride = layout->binding[binding].size;
4983 }
4984
4985 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
4986 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
4987 if (stride != 1) {
4988 if (nir_const_index) {
4989 const_index = const_index * stride;
4990 } else if (index.type() == RegType::vgpr) {
4991 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
4992 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
4993 } else {
4994 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
4995 }
4996 }
4997 if (offset) {
4998 if (nir_const_index) {
4999 const_index = const_index + offset;
5000 } else if (index.type() == RegType::vgpr) {
5001 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5002 } else {
5003 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5004 }
5005 }
5006
5007 if (nir_const_index && const_index == 0) {
5008 index = desc_ptr;
5009 } else if (index.type() == RegType::vgpr) {
5010 index = bld.vadd32(bld.def(v1),
5011 nir_const_index ? Operand(const_index) : Operand(index),
5012 Operand(desc_ptr));
5013 } else {
5014 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5015 nir_const_index ? Operand(const_index) : Operand(index),
5016 Operand(desc_ptr));
5017 }
5018
5019 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5020 }
5021
5022 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5023 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5024 bool glc=false, bool allow_smem=true, memory_sync_info sync=memory_sync_info())
5025 {
5026 Builder bld(ctx->program, ctx->block);
5027
5028 bool use_smem = dst.type() != RegType::vgpr && (!glc || ctx->options->chip_class >= GFX8) && allow_smem;
5029 if (use_smem)
5030 offset = bld.as_uniform(offset);
5031
5032 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5033 info.glc = glc;
5034 info.sync = sync;
5035 info.align_mul = align_mul;
5036 info.align_offset = align_offset;
5037 if (use_smem)
5038 emit_smem_load(ctx, bld, &info);
5039 else
5040 emit_mubuf_load(ctx, bld, &info);
5041 }
5042
5043 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5044 {
5045 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5046 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5047
5048 Builder bld(ctx->program, ctx->block);
5049
5050 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5051 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5052 unsigned binding = nir_intrinsic_binding(idx_instr);
5053 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5054
5055 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5056 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5057 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5058 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5059 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5060 if (ctx->options->chip_class >= GFX10) {
5061 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5062 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5063 S_008F0C_RESOURCE_LEVEL(1);
5064 } else {
5065 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5066 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5067 }
5068 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5069 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5070 Operand(0xFFFFFFFFu),
5071 Operand(desc_type));
5072 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5073 rsrc, upper_dwords);
5074 } else {
5075 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5076 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5077 }
5078 unsigned size = instr->dest.ssa.bit_size / 8;
5079 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5080 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5081 }
5082
5083 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5084 {
5085 Builder bld(ctx->program, ctx->block);
5086 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5087 unsigned offset = nir_intrinsic_base(instr);
5088 unsigned count = instr->dest.ssa.num_components;
5089 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5090
5091 if (index_cv && instr->dest.ssa.bit_size == 32) {
5092 unsigned start = (offset + index_cv->u32) / 4u;
5093 start -= ctx->args->ac.base_inline_push_consts;
5094 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5095 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5096 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5097 for (unsigned i = 0; i < count; ++i) {
5098 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5099 vec->operands[i] = Operand{elems[i]};
5100 }
5101 vec->definitions[0] = Definition(dst);
5102 ctx->block->instructions.emplace_back(std::move(vec));
5103 ctx->allocated_vec.emplace(dst.id(), elems);
5104 return;
5105 }
5106 }
5107
5108 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5109 if (offset != 0) // TODO check if index != 0 as well
5110 index = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5111 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5112 Temp vec = dst;
5113 bool trim = false;
5114 bool aligned = true;
5115
5116 if (instr->dest.ssa.bit_size == 8) {
5117 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5118 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5119 if (!aligned)
5120 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5121 } else if (instr->dest.ssa.bit_size == 16) {
5122 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5123 if (!aligned)
5124 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5125 }
5126
5127 aco_opcode op;
5128
5129 switch (vec.size()) {
5130 case 1:
5131 op = aco_opcode::s_load_dword;
5132 break;
5133 case 2:
5134 op = aco_opcode::s_load_dwordx2;
5135 break;
5136 case 3:
5137 vec = bld.tmp(s4);
5138 trim = true;
5139 case 4:
5140 op = aco_opcode::s_load_dwordx4;
5141 break;
5142 case 6:
5143 vec = bld.tmp(s8);
5144 trim = true;
5145 case 8:
5146 op = aco_opcode::s_load_dwordx8;
5147 break;
5148 default:
5149 unreachable("unimplemented or forbidden load_push_constant.");
5150 }
5151
5152 static_cast<SMEM_instruction*>(bld.smem(op, Definition(vec), ptr, index).instr)->prevent_overflow = true;
5153
5154 if (!aligned) {
5155 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5156 byte_align_scalar(ctx, vec, byte_offset, dst);
5157 return;
5158 }
5159
5160 if (trim) {
5161 emit_split_vector(ctx, vec, 4);
5162 RegClass rc = dst.size() == 3 ? s1 : s2;
5163 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5164 emit_extract_vector(ctx, vec, 0, rc),
5165 emit_extract_vector(ctx, vec, 1, rc),
5166 emit_extract_vector(ctx, vec, 2, rc));
5167
5168 }
5169 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5170 }
5171
5172 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5173 {
5174 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5175
5176 Builder bld(ctx->program, ctx->block);
5177
5178 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5179 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5180 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5181 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5182 if (ctx->options->chip_class >= GFX10) {
5183 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5184 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5185 S_008F0C_RESOURCE_LEVEL(1);
5186 } else {
5187 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5188 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5189 }
5190
5191 unsigned base = nir_intrinsic_base(instr);
5192 unsigned range = nir_intrinsic_range(instr);
5193
5194 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5195 if (base && offset.type() == RegType::sgpr)
5196 offset = bld.nuw().sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5197 else if (base && offset.type() == RegType::vgpr)
5198 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5199
5200 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5201 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5202 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5203 Operand(desc_type));
5204 unsigned size = instr->dest.ssa.bit_size / 8;
5205 // TODO: get alignment information for subdword constants
5206 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5207 }
5208
5209 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5210 {
5211 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5212 ctx->cf_info.exec_potentially_empty_discard = true;
5213
5214 ctx->program->needs_exact = true;
5215
5216 // TODO: optimize uniform conditions
5217 Builder bld(ctx->program, ctx->block);
5218 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5219 assert(src.regClass() == bld.lm);
5220 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5221 bld.pseudo(aco_opcode::p_discard_if, src);
5222 ctx->block->kind |= block_kind_uses_discard_if;
5223 return;
5224 }
5225
5226 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5227 {
5228 Builder bld(ctx->program, ctx->block);
5229
5230 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5231 ctx->cf_info.exec_potentially_empty_discard = true;
5232
5233 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5234 ctx->cf_info.parent_loop.has_divergent_continue;
5235
5236 if (ctx->block->loop_nest_depth &&
5237 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5238 /* we handle discards the same way as jump instructions */
5239 append_logical_end(ctx->block);
5240
5241 /* in loops, discard behaves like break */
5242 Block *linear_target = ctx->cf_info.parent_loop.exit;
5243 ctx->block->kind |= block_kind_discard;
5244
5245 if (!divergent) {
5246 /* uniform discard - loop ends here */
5247 assert(nir_instr_is_last(&instr->instr));
5248 ctx->block->kind |= block_kind_uniform;
5249 ctx->cf_info.has_branch = true;
5250 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
5251 add_linear_edge(ctx->block->index, linear_target);
5252 return;
5253 }
5254
5255 /* we add a break right behind the discard() instructions */
5256 ctx->block->kind |= block_kind_break;
5257 unsigned idx = ctx->block->index;
5258
5259 ctx->cf_info.parent_loop.has_divergent_branch = true;
5260 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5261
5262 /* remove critical edges from linear CFG */
5263 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
5264 Block* break_block = ctx->program->create_and_insert_block();
5265 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5266 break_block->kind |= block_kind_uniform;
5267 add_linear_edge(idx, break_block);
5268 add_linear_edge(break_block->index, linear_target);
5269 bld.reset(break_block);
5270 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
5271
5272 Block* continue_block = ctx->program->create_and_insert_block();
5273 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5274 add_linear_edge(idx, continue_block);
5275 append_logical_start(continue_block);
5276 ctx->block = continue_block;
5277
5278 return;
5279 }
5280
5281 /* it can currently happen that NIR doesn't remove the unreachable code */
5282 if (!nir_instr_is_last(&instr->instr)) {
5283 ctx->program->needs_exact = true;
5284 /* save exec somewhere temporarily so that it doesn't get
5285 * overwritten before the discard from outer exec masks */
5286 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5287 bld.pseudo(aco_opcode::p_discard_if, cond);
5288 ctx->block->kind |= block_kind_uses_discard_if;
5289 return;
5290 }
5291
5292 /* This condition is incorrect for uniformly branched discards in a loop
5293 * predicated by a divergent condition, but the above code catches that case
5294 * and the discard would end up turning into a discard_if.
5295 * For example:
5296 * if (divergent) {
5297 * while (...) {
5298 * if (uniform) {
5299 * discard;
5300 * }
5301 * }
5302 * }
5303 */
5304 if (!ctx->cf_info.parent_if.is_divergent) {
5305 /* program just ends here */
5306 ctx->block->kind |= block_kind_uniform;
5307 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5308 0 /* enabled mask */, 9 /* dest */,
5309 false /* compressed */, true/* done */, true /* valid mask */);
5310 bld.sopp(aco_opcode::s_endpgm);
5311 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5312 } else {
5313 ctx->block->kind |= block_kind_discard;
5314 /* branch and linear edge is added by visit_if() */
5315 }
5316 }
5317
5318 enum aco_descriptor_type {
5319 ACO_DESC_IMAGE,
5320 ACO_DESC_FMASK,
5321 ACO_DESC_SAMPLER,
5322 ACO_DESC_BUFFER,
5323 ACO_DESC_PLANE_0,
5324 ACO_DESC_PLANE_1,
5325 ACO_DESC_PLANE_2,
5326 };
5327
5328 static bool
5329 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5330 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5331 return false;
5332 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5333 return dim == ac_image_cube ||
5334 dim == ac_image_1darray ||
5335 dim == ac_image_2darray ||
5336 dim == ac_image_2darraymsaa;
5337 }
5338
5339 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5340 enum aco_descriptor_type desc_type,
5341 const nir_tex_instr *tex_instr, bool image, bool write)
5342 {
5343 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5344 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5345 if (it != ctx->tex_desc.end())
5346 return it->second;
5347 */
5348 Temp index = Temp();
5349 bool index_set = false;
5350 unsigned constant_index = 0;
5351 unsigned descriptor_set;
5352 unsigned base_index;
5353 Builder bld(ctx->program, ctx->block);
5354
5355 if (!deref_instr) {
5356 assert(tex_instr && !image);
5357 descriptor_set = 0;
5358 base_index = tex_instr->sampler_index;
5359 } else {
5360 while(deref_instr->deref_type != nir_deref_type_var) {
5361 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5362 if (!array_size)
5363 array_size = 1;
5364
5365 assert(deref_instr->deref_type == nir_deref_type_array);
5366 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5367 if (const_value) {
5368 constant_index += array_size * const_value->u32;
5369 } else {
5370 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5371 if (indirect.type() == RegType::vgpr)
5372 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5373
5374 if (array_size != 1)
5375 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5376
5377 if (!index_set) {
5378 index = indirect;
5379 index_set = true;
5380 } else {
5381 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5382 }
5383 }
5384
5385 deref_instr = nir_src_as_deref(deref_instr->parent);
5386 }
5387 descriptor_set = deref_instr->var->data.descriptor_set;
5388 base_index = deref_instr->var->data.binding;
5389 }
5390
5391 Temp list = load_desc_ptr(ctx, descriptor_set);
5392 list = convert_pointer_to_64_bit(ctx, list);
5393
5394 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5395 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5396 unsigned offset = binding->offset;
5397 unsigned stride = binding->size;
5398 aco_opcode opcode;
5399 RegClass type;
5400
5401 assert(base_index < layout->binding_count);
5402
5403 switch (desc_type) {
5404 case ACO_DESC_IMAGE:
5405 type = s8;
5406 opcode = aco_opcode::s_load_dwordx8;
5407 break;
5408 case ACO_DESC_FMASK:
5409 type = s8;
5410 opcode = aco_opcode::s_load_dwordx8;
5411 offset += 32;
5412 break;
5413 case ACO_DESC_SAMPLER:
5414 type = s4;
5415 opcode = aco_opcode::s_load_dwordx4;
5416 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5417 offset += radv_combined_image_descriptor_sampler_offset(binding);
5418 break;
5419 case ACO_DESC_BUFFER:
5420 type = s4;
5421 opcode = aco_opcode::s_load_dwordx4;
5422 break;
5423 case ACO_DESC_PLANE_0:
5424 case ACO_DESC_PLANE_1:
5425 type = s8;
5426 opcode = aco_opcode::s_load_dwordx8;
5427 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5428 break;
5429 case ACO_DESC_PLANE_2:
5430 type = s4;
5431 opcode = aco_opcode::s_load_dwordx4;
5432 offset += 64;
5433 break;
5434 default:
5435 unreachable("invalid desc_type\n");
5436 }
5437
5438 offset += constant_index * stride;
5439
5440 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5441 (!index_set || binding->immutable_samplers_equal)) {
5442 if (binding->immutable_samplers_equal)
5443 constant_index = 0;
5444
5445 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5446 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5447 Operand(samplers[constant_index * 4 + 0]),
5448 Operand(samplers[constant_index * 4 + 1]),
5449 Operand(samplers[constant_index * 4 + 2]),
5450 Operand(samplers[constant_index * 4 + 3]));
5451 }
5452
5453 Operand off;
5454 if (!index_set) {
5455 off = bld.copy(bld.def(s1), Operand(offset));
5456 } else {
5457 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5458 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5459 }
5460
5461 Temp res = bld.smem(opcode, bld.def(type), list, off);
5462
5463 if (desc_type == ACO_DESC_PLANE_2) {
5464 Temp components[8];
5465 for (unsigned i = 0; i < 8; i++)
5466 components[i] = bld.tmp(s1);
5467 bld.pseudo(aco_opcode::p_split_vector,
5468 Definition(components[0]),
5469 Definition(components[1]),
5470 Definition(components[2]),
5471 Definition(components[3]),
5472 res);
5473
5474 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5475 bld.pseudo(aco_opcode::p_split_vector,
5476 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5477 Definition(components[4]),
5478 Definition(components[5]),
5479 Definition(components[6]),
5480 Definition(components[7]),
5481 desc2);
5482
5483 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5484 components[0], components[1], components[2], components[3],
5485 components[4], components[5], components[6], components[7]);
5486 }
5487
5488 return res;
5489 }
5490
5491 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5492 {
5493 switch (dim) {
5494 case GLSL_SAMPLER_DIM_BUF:
5495 return 1;
5496 case GLSL_SAMPLER_DIM_1D:
5497 return array ? 2 : 1;
5498 case GLSL_SAMPLER_DIM_2D:
5499 return array ? 3 : 2;
5500 case GLSL_SAMPLER_DIM_MS:
5501 return array ? 4 : 3;
5502 case GLSL_SAMPLER_DIM_3D:
5503 case GLSL_SAMPLER_DIM_CUBE:
5504 return 3;
5505 case GLSL_SAMPLER_DIM_RECT:
5506 case GLSL_SAMPLER_DIM_SUBPASS:
5507 return 2;
5508 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5509 return 3;
5510 default:
5511 break;
5512 }
5513 return 0;
5514 }
5515
5516
5517 /* Adjust the sample index according to FMASK.
5518 *
5519 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5520 * which is the identity mapping. Each nibble says which physical sample
5521 * should be fetched to get that sample.
5522 *
5523 * For example, 0x11111100 means there are only 2 samples stored and
5524 * the second sample covers 3/4 of the pixel. When reading samples 0
5525 * and 1, return physical sample 0 (determined by the first two 0s
5526 * in FMASK), otherwise return physical sample 1.
5527 *
5528 * The sample index should be adjusted as follows:
5529 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5530 */
5531 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5532 {
5533 Builder bld(ctx->program, ctx->block);
5534 Temp fmask = bld.tmp(v1);
5535 unsigned dim = ctx->options->chip_class >= GFX10
5536 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5537 : 0;
5538
5539 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5540 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5541 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5542 load->operands[0] = Operand(fmask_desc_ptr);
5543 load->operands[1] = Operand(s4); /* no sampler */
5544 load->operands[2] = Operand(coord);
5545 load->definitions[0] = Definition(fmask);
5546 load->glc = false;
5547 load->dlc = false;
5548 load->dmask = 0x1;
5549 load->unrm = true;
5550 load->da = da;
5551 load->dim = dim;
5552 ctx->block->instructions.emplace_back(std::move(load));
5553
5554 Operand sample_index4;
5555 if (sample_index.isConstant()) {
5556 if (sample_index.constantValue() < 16) {
5557 sample_index4 = Operand(sample_index.constantValue() << 2);
5558 } else {
5559 sample_index4 = Operand(0u);
5560 }
5561 } else if (sample_index.regClass() == s1) {
5562 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5563 } else {
5564 assert(sample_index.regClass() == v1);
5565 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5566 }
5567
5568 Temp final_sample;
5569 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5570 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5571 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5572 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5573 else
5574 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5575
5576 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5577 * resource descriptor is 0 (invalid),
5578 */
5579 Temp compare = bld.tmp(bld.lm);
5580 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5581 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5582
5583 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5584
5585 /* Replace the MSAA sample index. */
5586 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5587 }
5588
5589 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5590 {
5591
5592 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5593 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5594 bool is_array = glsl_sampler_type_is_array(type);
5595 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5596 assert(!add_frag_pos && "Input attachments should be lowered.");
5597 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5598 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5599 int count = image_type_to_components_count(dim, is_array);
5600 std::vector<Temp> coords(count);
5601 Builder bld(ctx->program, ctx->block);
5602
5603 if (is_ms) {
5604 count--;
5605 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5606 /* get sample index */
5607 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5608 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5609 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5610 std::vector<Temp> fmask_load_address;
5611 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5612 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5613
5614 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5615 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5616 } else {
5617 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5618 }
5619 }
5620
5621 if (gfx9_1d) {
5622 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5623 coords.resize(coords.size() + 1);
5624 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5625 if (is_array)
5626 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5627 } else {
5628 for (int i = 0; i < count; i++)
5629 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5630 }
5631
5632 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5633 instr->intrinsic == nir_intrinsic_image_deref_store) {
5634 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5635 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5636
5637 if (!level_zero)
5638 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5639 }
5640
5641 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5642 for (unsigned i = 0; i < coords.size(); i++)
5643 vec->operands[i] = Operand(coords[i]);
5644 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5645 vec->definitions[0] = Definition(res);
5646 ctx->block->instructions.emplace_back(std::move(vec));
5647 return res;
5648 }
5649
5650
5651 memory_sync_info get_memory_sync_info(nir_intrinsic_instr *instr, storage_class storage, unsigned semantics)
5652 {
5653 /* atomicrmw might not have NIR_INTRINSIC_ACCESS and there's nothing interesting there anyway */
5654 if (semantics & semantic_atomicrmw)
5655 return memory_sync_info(storage, semantics);
5656
5657 unsigned access = nir_intrinsic_access(instr);
5658
5659 if (access & ACCESS_VOLATILE)
5660 semantics |= semantic_volatile;
5661 if (access & ACCESS_CAN_REORDER)
5662 semantics |= semantic_can_reorder | semantic_private;
5663
5664 return memory_sync_info(storage, semantics);
5665 }
5666
5667 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5668 {
5669 Builder bld(ctx->program, ctx->block);
5670 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5671 const struct glsl_type *type = glsl_without_array(var->type);
5672 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5673 bool is_array = glsl_sampler_type_is_array(type);
5674 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5675
5676 memory_sync_info sync = get_memory_sync_info(instr, storage_image, 0);
5677 unsigned access = var->data.access | nir_intrinsic_access(instr);
5678
5679 if (dim == GLSL_SAMPLER_DIM_BUF) {
5680 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5681 unsigned num_channels = util_last_bit(mask);
5682 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5683 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5684
5685 aco_opcode opcode;
5686 switch (num_channels) {
5687 case 1:
5688 opcode = aco_opcode::buffer_load_format_x;
5689 break;
5690 case 2:
5691 opcode = aco_opcode::buffer_load_format_xy;
5692 break;
5693 case 3:
5694 opcode = aco_opcode::buffer_load_format_xyz;
5695 break;
5696 case 4:
5697 opcode = aco_opcode::buffer_load_format_xyzw;
5698 break;
5699 default:
5700 unreachable(">4 channel buffer image load");
5701 }
5702 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5703 load->operands[0] = Operand(rsrc);
5704 load->operands[1] = Operand(vindex);
5705 load->operands[2] = Operand((uint32_t) 0);
5706 Temp tmp;
5707 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5708 tmp = dst;
5709 else
5710 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5711 load->definitions[0] = Definition(tmp);
5712 load->idxen = true;
5713 load->glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5714 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5715 load->sync = sync;
5716 ctx->block->instructions.emplace_back(std::move(load));
5717
5718 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5719 return;
5720 }
5721
5722 Temp coords = get_image_coords(ctx, instr, type);
5723 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5724
5725 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5726 unsigned num_components = util_bitcount(dmask);
5727 Temp tmp;
5728 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5729 tmp = dst;
5730 else
5731 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5732
5733 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5734 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5735
5736 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5737 load->operands[0] = Operand(resource);
5738 load->operands[1] = Operand(s4); /* no sampler */
5739 load->operands[2] = Operand(coords);
5740 load->definitions[0] = Definition(tmp);
5741 load->glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5742 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5743 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5744 load->dmask = dmask;
5745 load->unrm = true;
5746 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5747 load->sync = sync;
5748 ctx->block->instructions.emplace_back(std::move(load));
5749
5750 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5751 return;
5752 }
5753
5754 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5755 {
5756 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5757 const struct glsl_type *type = glsl_without_array(var->type);
5758 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5759 bool is_array = glsl_sampler_type_is_array(type);
5760 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5761
5762 memory_sync_info sync = get_memory_sync_info(instr, storage_image, 0);
5763 unsigned access = var->data.access | nir_intrinsic_access(instr);
5764 bool glc = ctx->options->chip_class == GFX6 || access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5765
5766 if (dim == GLSL_SAMPLER_DIM_BUF) {
5767 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5768 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5769 aco_opcode opcode;
5770 switch (data.size()) {
5771 case 1:
5772 opcode = aco_opcode::buffer_store_format_x;
5773 break;
5774 case 2:
5775 opcode = aco_opcode::buffer_store_format_xy;
5776 break;
5777 case 3:
5778 opcode = aco_opcode::buffer_store_format_xyz;
5779 break;
5780 case 4:
5781 opcode = aco_opcode::buffer_store_format_xyzw;
5782 break;
5783 default:
5784 unreachable(">4 channel buffer image store");
5785 }
5786 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5787 store->operands[0] = Operand(rsrc);
5788 store->operands[1] = Operand(vindex);
5789 store->operands[2] = Operand((uint32_t) 0);
5790 store->operands[3] = Operand(data);
5791 store->idxen = true;
5792 store->glc = glc;
5793 store->dlc = false;
5794 store->disable_wqm = true;
5795 store->sync = sync;
5796 ctx->program->needs_exact = true;
5797 ctx->block->instructions.emplace_back(std::move(store));
5798 return;
5799 }
5800
5801 assert(data.type() == RegType::vgpr);
5802 Temp coords = get_image_coords(ctx, instr, type);
5803 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5804
5805 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5806 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5807
5808 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5809 store->operands[0] = Operand(resource);
5810 store->operands[1] = Operand(data);
5811 store->operands[2] = Operand(coords);
5812 store->glc = glc;
5813 store->dlc = false;
5814 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5815 store->dmask = (1 << data.size()) - 1;
5816 store->unrm = true;
5817 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5818 store->disable_wqm = true;
5819 store->sync = sync;
5820 ctx->program->needs_exact = true;
5821 ctx->block->instructions.emplace_back(std::move(store));
5822 return;
5823 }
5824
5825 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5826 {
5827 /* return the previous value if dest is ever used */
5828 bool return_previous = false;
5829 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5830 return_previous = true;
5831 break;
5832 }
5833 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5834 return_previous = true;
5835 break;
5836 }
5837
5838 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5839 const struct glsl_type *type = glsl_without_array(var->type);
5840 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5841 bool is_array = glsl_sampler_type_is_array(type);
5842 Builder bld(ctx->program, ctx->block);
5843
5844 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5845 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5846
5847 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5848 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5849
5850 aco_opcode buf_op, image_op;
5851 switch (instr->intrinsic) {
5852 case nir_intrinsic_image_deref_atomic_add:
5853 buf_op = aco_opcode::buffer_atomic_add;
5854 image_op = aco_opcode::image_atomic_add;
5855 break;
5856 case nir_intrinsic_image_deref_atomic_umin:
5857 buf_op = aco_opcode::buffer_atomic_umin;
5858 image_op = aco_opcode::image_atomic_umin;
5859 break;
5860 case nir_intrinsic_image_deref_atomic_imin:
5861 buf_op = aco_opcode::buffer_atomic_smin;
5862 image_op = aco_opcode::image_atomic_smin;
5863 break;
5864 case nir_intrinsic_image_deref_atomic_umax:
5865 buf_op = aco_opcode::buffer_atomic_umax;
5866 image_op = aco_opcode::image_atomic_umax;
5867 break;
5868 case nir_intrinsic_image_deref_atomic_imax:
5869 buf_op = aco_opcode::buffer_atomic_smax;
5870 image_op = aco_opcode::image_atomic_smax;
5871 break;
5872 case nir_intrinsic_image_deref_atomic_and:
5873 buf_op = aco_opcode::buffer_atomic_and;
5874 image_op = aco_opcode::image_atomic_and;
5875 break;
5876 case nir_intrinsic_image_deref_atomic_or:
5877 buf_op = aco_opcode::buffer_atomic_or;
5878 image_op = aco_opcode::image_atomic_or;
5879 break;
5880 case nir_intrinsic_image_deref_atomic_xor:
5881 buf_op = aco_opcode::buffer_atomic_xor;
5882 image_op = aco_opcode::image_atomic_xor;
5883 break;
5884 case nir_intrinsic_image_deref_atomic_exchange:
5885 buf_op = aco_opcode::buffer_atomic_swap;
5886 image_op = aco_opcode::image_atomic_swap;
5887 break;
5888 case nir_intrinsic_image_deref_atomic_comp_swap:
5889 buf_op = aco_opcode::buffer_atomic_cmpswap;
5890 image_op = aco_opcode::image_atomic_cmpswap;
5891 break;
5892 default:
5893 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5894 }
5895
5896 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5897 memory_sync_info sync = get_memory_sync_info(instr, storage_image, semantic_atomicrmw);
5898
5899 if (dim == GLSL_SAMPLER_DIM_BUF) {
5900 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5901 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5902 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5903 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5904 mubuf->operands[0] = Operand(resource);
5905 mubuf->operands[1] = Operand(vindex);
5906 mubuf->operands[2] = Operand((uint32_t)0);
5907 mubuf->operands[3] = Operand(data);
5908 if (return_previous)
5909 mubuf->definitions[0] = Definition(dst);
5910 mubuf->offset = 0;
5911 mubuf->idxen = true;
5912 mubuf->glc = return_previous;
5913 mubuf->dlc = false; /* Not needed for atomics */
5914 mubuf->disable_wqm = true;
5915 mubuf->sync = sync;
5916 ctx->program->needs_exact = true;
5917 ctx->block->instructions.emplace_back(std::move(mubuf));
5918 return;
5919 }
5920
5921 Temp coords = get_image_coords(ctx, instr, type);
5922 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5923 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5924 mimg->operands[0] = Operand(resource);
5925 mimg->operands[1] = Operand(data);
5926 mimg->operands[2] = Operand(coords);
5927 if (return_previous)
5928 mimg->definitions[0] = Definition(dst);
5929 mimg->glc = return_previous;
5930 mimg->dlc = false; /* Not needed for atomics */
5931 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5932 mimg->dmask = (1 << data.size()) - 1;
5933 mimg->unrm = true;
5934 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5935 mimg->disable_wqm = true;
5936 mimg->sync = sync;
5937 ctx->program->needs_exact = true;
5938 ctx->block->instructions.emplace_back(std::move(mimg));
5939 return;
5940 }
5941
5942 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
5943 {
5944 if (in_elements && ctx->options->chip_class == GFX8) {
5945 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5946 Builder bld(ctx->program, ctx->block);
5947
5948 Temp size = emit_extract_vector(ctx, desc, 2, s1);
5949
5950 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
5951 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
5952
5953 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
5954 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
5955
5956 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
5957 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
5958
5959 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
5960 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
5961 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
5962 if (dst.type() == RegType::vgpr)
5963 bld.copy(Definition(dst), shr_dst);
5964
5965 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5966 } else {
5967 emit_extract_vector(ctx, desc, 2, dst);
5968 }
5969 }
5970
5971 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
5972 {
5973 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5974 const struct glsl_type *type = glsl_without_array(var->type);
5975 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5976 bool is_array = glsl_sampler_type_is_array(type);
5977 Builder bld(ctx->program, ctx->block);
5978
5979 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
5980 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
5981 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
5982 }
5983
5984 /* LOD */
5985 assert(nir_src_as_uint(instr->src[1]) == 0);
5986 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
5987
5988 /* Resource */
5989 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
5990
5991 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5992
5993 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
5994 mimg->operands[0] = Operand(resource);
5995 mimg->operands[1] = Operand(s4); /* no sampler */
5996 mimg->operands[2] = Operand(lod);
5997 uint8_t& dmask = mimg->dmask;
5998 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5999 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6000 mimg->da = glsl_sampler_type_is_array(type);
6001 Definition& def = mimg->definitions[0];
6002 ctx->block->instructions.emplace_back(std::move(mimg));
6003
6004 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6005 glsl_sampler_type_is_array(type)) {
6006
6007 assert(instr->dest.ssa.num_components == 3);
6008 Temp tmp = {ctx->program->allocateId(), v3};
6009 def = Definition(tmp);
6010 emit_split_vector(ctx, tmp, 3);
6011
6012 /* divide 3rd value by 6 by multiplying with magic number */
6013 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6014 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6015
6016 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6017 emit_extract_vector(ctx, tmp, 0, v1),
6018 emit_extract_vector(ctx, tmp, 1, v1),
6019 by_6);
6020
6021 } else if (ctx->options->chip_class == GFX9 &&
6022 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6023 glsl_sampler_type_is_array(type)) {
6024 assert(instr->dest.ssa.num_components == 2);
6025 def = Definition(dst);
6026 dmask = 0x5;
6027 } else {
6028 def = Definition(dst);
6029 }
6030
6031 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6032 }
6033
6034 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6035 {
6036 Builder bld(ctx->program, ctx->block);
6037 unsigned num_components = instr->num_components;
6038
6039 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6040 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6041 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6042
6043 unsigned access = nir_intrinsic_access(instr);
6044 bool glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
6045 unsigned size = instr->dest.ssa.bit_size / 8;
6046
6047 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[0].ssa, access);
6048 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6049 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6050 */
6051 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_store : has_vmem_store));
6052 allow_smem |= ((access & ACCESS_RESTRICT) && (access & ACCESS_NON_WRITEABLE)) || (access & ACCESS_CAN_REORDER);
6053
6054 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6055 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, allow_smem,
6056 get_memory_sync_info(instr, storage_buffer, 0));
6057 }
6058
6059 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6060 {
6061 Builder bld(ctx->program, ctx->block);
6062 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6063 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6064 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6065 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6066
6067 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6068 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6069
6070 memory_sync_info sync = get_memory_sync_info(instr, storage_buffer, 0);
6071 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6072 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[1].ssa, nir_intrinsic_access(instr));
6073 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6074 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6075 */
6076 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_loadstore : has_vmem_loadstore));
6077
6078 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6079 ctx->options->chip_class >= GFX8 &&
6080 ctx->options->chip_class < GFX10_3 &&
6081 (elem_size_bytes >= 4 || can_subdword_ssbo_store_use_smem(instr)) &&
6082 allow_smem;
6083 if (smem)
6084 offset = bld.as_uniform(offset);
6085 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6086
6087 unsigned write_count = 0;
6088 Temp write_datas[32];
6089 unsigned offsets[32];
6090 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6091 data, writemask, 16, &write_count, write_datas, offsets);
6092
6093 for (unsigned i = 0; i < write_count; i++) {
6094 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6095 if (smem && ctx->stage == fragment_fs)
6096 op = aco_opcode::p_fs_buffer_store_smem;
6097
6098 if (smem) {
6099 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6100 store->operands[0] = Operand(rsrc);
6101 if (offsets[i]) {
6102 Temp off = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6103 offset, Operand(offsets[i]));
6104 store->operands[1] = Operand(off);
6105 } else {
6106 store->operands[1] = Operand(offset);
6107 }
6108 if (op != aco_opcode::p_fs_buffer_store_smem)
6109 store->operands[1].setFixed(m0);
6110 store->operands[2] = Operand(write_datas[i]);
6111 store->glc = glc;
6112 store->dlc = false;
6113 store->disable_wqm = true;
6114 store->sync = sync;
6115 ctx->block->instructions.emplace_back(std::move(store));
6116 ctx->program->wb_smem_l1_on_end = true;
6117 if (op == aco_opcode::p_fs_buffer_store_smem) {
6118 ctx->block->kind |= block_kind_needs_lowering;
6119 ctx->program->needs_exact = true;
6120 }
6121 } else {
6122 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6123 store->operands[0] = Operand(rsrc);
6124 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6125 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6126 store->operands[3] = Operand(write_datas[i]);
6127 store->offset = offsets[i];
6128 store->offen = (offset.type() == RegType::vgpr);
6129 store->glc = glc;
6130 store->dlc = false;
6131 store->disable_wqm = true;
6132 store->sync = sync;
6133 ctx->program->needs_exact = true;
6134 ctx->block->instructions.emplace_back(std::move(store));
6135 }
6136 }
6137 }
6138
6139 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6140 {
6141 /* return the previous value if dest is ever used */
6142 bool return_previous = false;
6143 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6144 return_previous = true;
6145 break;
6146 }
6147 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6148 return_previous = true;
6149 break;
6150 }
6151
6152 Builder bld(ctx->program, ctx->block);
6153 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6154
6155 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6156 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6157 get_ssa_temp(ctx, instr->src[3].ssa), data);
6158
6159 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6160 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6161 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6162
6163 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6164
6165 aco_opcode op32, op64;
6166 switch (instr->intrinsic) {
6167 case nir_intrinsic_ssbo_atomic_add:
6168 op32 = aco_opcode::buffer_atomic_add;
6169 op64 = aco_opcode::buffer_atomic_add_x2;
6170 break;
6171 case nir_intrinsic_ssbo_atomic_imin:
6172 op32 = aco_opcode::buffer_atomic_smin;
6173 op64 = aco_opcode::buffer_atomic_smin_x2;
6174 break;
6175 case nir_intrinsic_ssbo_atomic_umin:
6176 op32 = aco_opcode::buffer_atomic_umin;
6177 op64 = aco_opcode::buffer_atomic_umin_x2;
6178 break;
6179 case nir_intrinsic_ssbo_atomic_imax:
6180 op32 = aco_opcode::buffer_atomic_smax;
6181 op64 = aco_opcode::buffer_atomic_smax_x2;
6182 break;
6183 case nir_intrinsic_ssbo_atomic_umax:
6184 op32 = aco_opcode::buffer_atomic_umax;
6185 op64 = aco_opcode::buffer_atomic_umax_x2;
6186 break;
6187 case nir_intrinsic_ssbo_atomic_and:
6188 op32 = aco_opcode::buffer_atomic_and;
6189 op64 = aco_opcode::buffer_atomic_and_x2;
6190 break;
6191 case nir_intrinsic_ssbo_atomic_or:
6192 op32 = aco_opcode::buffer_atomic_or;
6193 op64 = aco_opcode::buffer_atomic_or_x2;
6194 break;
6195 case nir_intrinsic_ssbo_atomic_xor:
6196 op32 = aco_opcode::buffer_atomic_xor;
6197 op64 = aco_opcode::buffer_atomic_xor_x2;
6198 break;
6199 case nir_intrinsic_ssbo_atomic_exchange:
6200 op32 = aco_opcode::buffer_atomic_swap;
6201 op64 = aco_opcode::buffer_atomic_swap_x2;
6202 break;
6203 case nir_intrinsic_ssbo_atomic_comp_swap:
6204 op32 = aco_opcode::buffer_atomic_cmpswap;
6205 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6206 break;
6207 default:
6208 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6209 }
6210 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6211 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6212 mubuf->operands[0] = Operand(rsrc);
6213 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6214 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6215 mubuf->operands[3] = Operand(data);
6216 if (return_previous)
6217 mubuf->definitions[0] = Definition(dst);
6218 mubuf->offset = 0;
6219 mubuf->offen = (offset.type() == RegType::vgpr);
6220 mubuf->glc = return_previous;
6221 mubuf->dlc = false; /* Not needed for atomics */
6222 mubuf->disable_wqm = true;
6223 mubuf->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6224 ctx->program->needs_exact = true;
6225 ctx->block->instructions.emplace_back(std::move(mubuf));
6226 }
6227
6228 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6229
6230 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6231 Builder bld(ctx->program, ctx->block);
6232 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6233 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6234 }
6235
6236 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6237 {
6238 Builder bld(ctx->program, ctx->block);
6239 unsigned num_components = instr->num_components;
6240 unsigned component_size = instr->dest.ssa.bit_size / 8;
6241
6242 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6243 get_ssa_temp(ctx, &instr->dest.ssa),
6244 num_components, component_size};
6245 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6246 info.align_mul = nir_intrinsic_align_mul(instr);
6247 info.align_offset = nir_intrinsic_align_offset(instr);
6248 info.sync = get_memory_sync_info(instr, storage_buffer, 0);
6249 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6250 * it's safe to use SMEM */
6251 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6252 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6253 emit_global_load(ctx, bld, &info);
6254 } else {
6255 info.offset = Operand(bld.as_uniform(info.offset));
6256 emit_smem_load(ctx, bld, &info);
6257 }
6258 }
6259
6260 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6261 {
6262 Builder bld(ctx->program, ctx->block);
6263 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6264 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6265
6266 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6267 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6268 memory_sync_info sync = get_memory_sync_info(instr, storage_buffer, 0);
6269 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6270
6271 if (ctx->options->chip_class >= GFX7)
6272 addr = as_vgpr(ctx, addr);
6273
6274 unsigned write_count = 0;
6275 Temp write_datas[32];
6276 unsigned offsets[32];
6277 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6278 16, &write_count, write_datas, offsets);
6279
6280 for (unsigned i = 0; i < write_count; i++) {
6281 if (ctx->options->chip_class >= GFX7) {
6282 unsigned offset = offsets[i];
6283 Temp store_addr = addr;
6284 if (offset > 0 && ctx->options->chip_class < GFX9) {
6285 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6286 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6287 Temp carry = bld.tmp(bld.lm);
6288 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6289
6290 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6291 Operand(offset), addr0);
6292 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6293 Operand(0u), addr1,
6294 carry).def(1).setHint(vcc);
6295
6296 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6297
6298 offset = 0;
6299 }
6300
6301 bool global = ctx->options->chip_class >= GFX9;
6302 aco_opcode op;
6303 switch (write_datas[i].bytes()) {
6304 case 1:
6305 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6306 break;
6307 case 2:
6308 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6309 break;
6310 case 4:
6311 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6312 break;
6313 case 8:
6314 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6315 break;
6316 case 12:
6317 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6318 break;
6319 case 16:
6320 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6321 break;
6322 default:
6323 unreachable("store_global not implemented for this size.");
6324 }
6325
6326 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6327 flat->operands[0] = Operand(store_addr);
6328 flat->operands[1] = Operand(s1);
6329 flat->operands[2] = Operand(write_datas[i]);
6330 flat->glc = glc;
6331 flat->dlc = false;
6332 flat->offset = offset;
6333 flat->disable_wqm = true;
6334 flat->sync = sync;
6335 ctx->program->needs_exact = true;
6336 ctx->block->instructions.emplace_back(std::move(flat));
6337 } else {
6338 assert(ctx->options->chip_class == GFX6);
6339
6340 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6341
6342 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6343
6344 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6345 mubuf->operands[0] = Operand(rsrc);
6346 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6347 mubuf->operands[2] = Operand(0u);
6348 mubuf->operands[3] = Operand(write_datas[i]);
6349 mubuf->glc = glc;
6350 mubuf->dlc = false;
6351 mubuf->offset = offsets[i];
6352 mubuf->addr64 = addr.type() == RegType::vgpr;
6353 mubuf->disable_wqm = true;
6354 mubuf->sync = sync;
6355 ctx->program->needs_exact = true;
6356 ctx->block->instructions.emplace_back(std::move(mubuf));
6357 }
6358 }
6359 }
6360
6361 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6362 {
6363 /* return the previous value if dest is ever used */
6364 bool return_previous = false;
6365 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6366 return_previous = true;
6367 break;
6368 }
6369 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6370 return_previous = true;
6371 break;
6372 }
6373
6374 Builder bld(ctx->program, ctx->block);
6375 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6376 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6377
6378 if (ctx->options->chip_class >= GFX7)
6379 addr = as_vgpr(ctx, addr);
6380
6381 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6382 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6383 get_ssa_temp(ctx, instr->src[2].ssa), data);
6384
6385 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6386
6387 aco_opcode op32, op64;
6388
6389 if (ctx->options->chip_class >= GFX7) {
6390 bool global = ctx->options->chip_class >= GFX9;
6391 switch (instr->intrinsic) {
6392 case nir_intrinsic_global_atomic_add:
6393 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6394 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6395 break;
6396 case nir_intrinsic_global_atomic_imin:
6397 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6398 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6399 break;
6400 case nir_intrinsic_global_atomic_umin:
6401 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6402 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6403 break;
6404 case nir_intrinsic_global_atomic_imax:
6405 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6406 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6407 break;
6408 case nir_intrinsic_global_atomic_umax:
6409 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6410 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6411 break;
6412 case nir_intrinsic_global_atomic_and:
6413 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6414 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6415 break;
6416 case nir_intrinsic_global_atomic_or:
6417 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6418 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6419 break;
6420 case nir_intrinsic_global_atomic_xor:
6421 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6422 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6423 break;
6424 case nir_intrinsic_global_atomic_exchange:
6425 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6426 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6427 break;
6428 case nir_intrinsic_global_atomic_comp_swap:
6429 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6430 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6431 break;
6432 default:
6433 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6434 }
6435
6436 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6437 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6438 flat->operands[0] = Operand(addr);
6439 flat->operands[1] = Operand(s1);
6440 flat->operands[2] = Operand(data);
6441 if (return_previous)
6442 flat->definitions[0] = Definition(dst);
6443 flat->glc = return_previous;
6444 flat->dlc = false; /* Not needed for atomics */
6445 flat->offset = 0;
6446 flat->disable_wqm = true;
6447 flat->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6448 ctx->program->needs_exact = true;
6449 ctx->block->instructions.emplace_back(std::move(flat));
6450 } else {
6451 assert(ctx->options->chip_class == GFX6);
6452
6453 switch (instr->intrinsic) {
6454 case nir_intrinsic_global_atomic_add:
6455 op32 = aco_opcode::buffer_atomic_add;
6456 op64 = aco_opcode::buffer_atomic_add_x2;
6457 break;
6458 case nir_intrinsic_global_atomic_imin:
6459 op32 = aco_opcode::buffer_atomic_smin;
6460 op64 = aco_opcode::buffer_atomic_smin_x2;
6461 break;
6462 case nir_intrinsic_global_atomic_umin:
6463 op32 = aco_opcode::buffer_atomic_umin;
6464 op64 = aco_opcode::buffer_atomic_umin_x2;
6465 break;
6466 case nir_intrinsic_global_atomic_imax:
6467 op32 = aco_opcode::buffer_atomic_smax;
6468 op64 = aco_opcode::buffer_atomic_smax_x2;
6469 break;
6470 case nir_intrinsic_global_atomic_umax:
6471 op32 = aco_opcode::buffer_atomic_umax;
6472 op64 = aco_opcode::buffer_atomic_umax_x2;
6473 break;
6474 case nir_intrinsic_global_atomic_and:
6475 op32 = aco_opcode::buffer_atomic_and;
6476 op64 = aco_opcode::buffer_atomic_and_x2;
6477 break;
6478 case nir_intrinsic_global_atomic_or:
6479 op32 = aco_opcode::buffer_atomic_or;
6480 op64 = aco_opcode::buffer_atomic_or_x2;
6481 break;
6482 case nir_intrinsic_global_atomic_xor:
6483 op32 = aco_opcode::buffer_atomic_xor;
6484 op64 = aco_opcode::buffer_atomic_xor_x2;
6485 break;
6486 case nir_intrinsic_global_atomic_exchange:
6487 op32 = aco_opcode::buffer_atomic_swap;
6488 op64 = aco_opcode::buffer_atomic_swap_x2;
6489 break;
6490 case nir_intrinsic_global_atomic_comp_swap:
6491 op32 = aco_opcode::buffer_atomic_cmpswap;
6492 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6493 break;
6494 default:
6495 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6496 }
6497
6498 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6499
6500 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6501
6502 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6503 mubuf->operands[0] = Operand(rsrc);
6504 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6505 mubuf->operands[2] = Operand(0u);
6506 mubuf->operands[3] = Operand(data);
6507 if (return_previous)
6508 mubuf->definitions[0] = Definition(dst);
6509 mubuf->glc = return_previous;
6510 mubuf->dlc = false;
6511 mubuf->offset = 0;
6512 mubuf->addr64 = addr.type() == RegType::vgpr;
6513 mubuf->disable_wqm = true;
6514 mubuf->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6515 ctx->program->needs_exact = true;
6516 ctx->block->instructions.emplace_back(std::move(mubuf));
6517 }
6518 }
6519
6520 sync_scope translate_nir_scope(nir_scope scope)
6521 {
6522 switch (scope) {
6523 case NIR_SCOPE_NONE:
6524 case NIR_SCOPE_INVOCATION:
6525 return scope_invocation;
6526 case NIR_SCOPE_SUBGROUP:
6527 return scope_subgroup;
6528 case NIR_SCOPE_WORKGROUP:
6529 return scope_workgroup;
6530 case NIR_SCOPE_QUEUE_FAMILY:
6531 return scope_queuefamily;
6532 case NIR_SCOPE_DEVICE:
6533 return scope_device;
6534 }
6535 unreachable("invalid scope");
6536 }
6537
6538 void emit_scoped_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6539 Builder bld(ctx->program, ctx->block);
6540
6541 unsigned semantics = 0;
6542 unsigned storage = 0;
6543 sync_scope mem_scope = translate_nir_scope(nir_intrinsic_memory_scope(instr));
6544 sync_scope exec_scope = translate_nir_scope(nir_intrinsic_execution_scope(instr));
6545
6546 unsigned nir_storage = nir_intrinsic_memory_modes(instr);
6547 if (nir_storage & (nir_var_mem_ssbo | nir_var_mem_global))
6548 storage |= storage_buffer | storage_image; //TODO: split this when NIR gets nir_var_mem_image
6549 if (ctx->shader->info.stage == MESA_SHADER_COMPUTE && (nir_storage & nir_var_mem_shared))
6550 storage |= storage_shared;
6551 if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL && (nir_storage & nir_var_shader_out))
6552 storage |= storage_shared;
6553
6554 unsigned nir_semantics = nir_intrinsic_memory_semantics(instr);
6555 if (nir_semantics & NIR_MEMORY_ACQUIRE)
6556 semantics |= semantic_acquire | semantic_release;
6557 if (nir_semantics & NIR_MEMORY_RELEASE)
6558 semantics |= semantic_acquire | semantic_release;
6559
6560 assert(!(nir_semantics & (NIR_MEMORY_MAKE_AVAILABLE | NIR_MEMORY_MAKE_VISIBLE)));
6561
6562 bld.barrier(aco_opcode::p_barrier,
6563 memory_sync_info((storage_class)storage, (memory_semantics)semantics, mem_scope),
6564 exec_scope);
6565 }
6566
6567 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6568 {
6569 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6570 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6571 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6572 Builder bld(ctx->program, ctx->block);
6573
6574 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6575 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6576 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6577 }
6578
6579 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6580 {
6581 unsigned writemask = nir_intrinsic_write_mask(instr);
6582 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6583 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6584 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6585
6586 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6587 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6588 }
6589
6590 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6591 {
6592 unsigned offset = nir_intrinsic_base(instr);
6593 Builder bld(ctx->program, ctx->block);
6594 Operand m = load_lds_size_m0(bld);
6595 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6596 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6597
6598 unsigned num_operands = 3;
6599 aco_opcode op32, op64, op32_rtn, op64_rtn;
6600 switch(instr->intrinsic) {
6601 case nir_intrinsic_shared_atomic_add:
6602 op32 = aco_opcode::ds_add_u32;
6603 op64 = aco_opcode::ds_add_u64;
6604 op32_rtn = aco_opcode::ds_add_rtn_u32;
6605 op64_rtn = aco_opcode::ds_add_rtn_u64;
6606 break;
6607 case nir_intrinsic_shared_atomic_imin:
6608 op32 = aco_opcode::ds_min_i32;
6609 op64 = aco_opcode::ds_min_i64;
6610 op32_rtn = aco_opcode::ds_min_rtn_i32;
6611 op64_rtn = aco_opcode::ds_min_rtn_i64;
6612 break;
6613 case nir_intrinsic_shared_atomic_umin:
6614 op32 = aco_opcode::ds_min_u32;
6615 op64 = aco_opcode::ds_min_u64;
6616 op32_rtn = aco_opcode::ds_min_rtn_u32;
6617 op64_rtn = aco_opcode::ds_min_rtn_u64;
6618 break;
6619 case nir_intrinsic_shared_atomic_imax:
6620 op32 = aco_opcode::ds_max_i32;
6621 op64 = aco_opcode::ds_max_i64;
6622 op32_rtn = aco_opcode::ds_max_rtn_i32;
6623 op64_rtn = aco_opcode::ds_max_rtn_i64;
6624 break;
6625 case nir_intrinsic_shared_atomic_umax:
6626 op32 = aco_opcode::ds_max_u32;
6627 op64 = aco_opcode::ds_max_u64;
6628 op32_rtn = aco_opcode::ds_max_rtn_u32;
6629 op64_rtn = aco_opcode::ds_max_rtn_u64;
6630 break;
6631 case nir_intrinsic_shared_atomic_and:
6632 op32 = aco_opcode::ds_and_b32;
6633 op64 = aco_opcode::ds_and_b64;
6634 op32_rtn = aco_opcode::ds_and_rtn_b32;
6635 op64_rtn = aco_opcode::ds_and_rtn_b64;
6636 break;
6637 case nir_intrinsic_shared_atomic_or:
6638 op32 = aco_opcode::ds_or_b32;
6639 op64 = aco_opcode::ds_or_b64;
6640 op32_rtn = aco_opcode::ds_or_rtn_b32;
6641 op64_rtn = aco_opcode::ds_or_rtn_b64;
6642 break;
6643 case nir_intrinsic_shared_atomic_xor:
6644 op32 = aco_opcode::ds_xor_b32;
6645 op64 = aco_opcode::ds_xor_b64;
6646 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6647 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6648 break;
6649 case nir_intrinsic_shared_atomic_exchange:
6650 op32 = aco_opcode::ds_write_b32;
6651 op64 = aco_opcode::ds_write_b64;
6652 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6653 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6654 break;
6655 case nir_intrinsic_shared_atomic_comp_swap:
6656 op32 = aco_opcode::ds_cmpst_b32;
6657 op64 = aco_opcode::ds_cmpst_b64;
6658 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6659 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6660 num_operands = 4;
6661 break;
6662 case nir_intrinsic_shared_atomic_fadd:
6663 op32 = aco_opcode::ds_add_f32;
6664 op32_rtn = aco_opcode::ds_add_rtn_f32;
6665 op64 = aco_opcode::num_opcodes;
6666 op64_rtn = aco_opcode::num_opcodes;
6667 break;
6668 default:
6669 unreachable("Unhandled shared atomic intrinsic");
6670 }
6671
6672 /* return the previous value if dest is ever used */
6673 bool return_previous = false;
6674 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6675 return_previous = true;
6676 break;
6677 }
6678 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6679 return_previous = true;
6680 break;
6681 }
6682
6683 aco_opcode op;
6684 if (data.size() == 1) {
6685 assert(instr->dest.ssa.bit_size == 32);
6686 op = return_previous ? op32_rtn : op32;
6687 } else {
6688 assert(instr->dest.ssa.bit_size == 64);
6689 op = return_previous ? op64_rtn : op64;
6690 }
6691
6692 if (offset > 65535) {
6693 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6694 offset = 0;
6695 }
6696
6697 aco_ptr<DS_instruction> ds;
6698 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6699 ds->operands[0] = Operand(address);
6700 ds->operands[1] = Operand(data);
6701 if (num_operands == 4)
6702 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6703 ds->operands[num_operands - 1] = m;
6704 ds->offset0 = offset;
6705 if (return_previous)
6706 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6707 ds->sync = memory_sync_info(storage_shared, semantic_atomicrmw);
6708 ctx->block->instructions.emplace_back(std::move(ds));
6709 }
6710
6711 Temp get_scratch_resource(isel_context *ctx)
6712 {
6713 Builder bld(ctx->program, ctx->block);
6714 Temp scratch_addr = ctx->program->private_segment_buffer;
6715 if (ctx->stage != compute_cs)
6716 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6717
6718 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6719 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);
6720
6721 if (ctx->program->chip_class >= GFX10) {
6722 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6723 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6724 S_008F0C_RESOURCE_LEVEL(1);
6725 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6726 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6727 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6728 }
6729
6730 /* older generations need element size = 4 bytes. element size removed in GFX9 */
6731 if (ctx->program->chip_class <= GFX8)
6732 rsrc_conf |= S_008F0C_ELEMENT_SIZE(1);
6733
6734 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6735 }
6736
6737 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6738 Builder bld(ctx->program, ctx->block);
6739 Temp rsrc = get_scratch_resource(ctx);
6740 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6741 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6742
6743 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6744 instr->dest.ssa.bit_size / 8u, rsrc};
6745 info.align_mul = nir_intrinsic_align_mul(instr);
6746 info.align_offset = nir_intrinsic_align_offset(instr);
6747 info.swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 0;
6748 info.sync = memory_sync_info(storage_scratch, semantic_private);
6749 info.soffset = ctx->program->scratch_offset;
6750 emit_scratch_load(ctx, bld, &info);
6751 }
6752
6753 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6754 Builder bld(ctx->program, ctx->block);
6755 Temp rsrc = get_scratch_resource(ctx);
6756 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6757 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6758
6759 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6760 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6761
6762 unsigned write_count = 0;
6763 Temp write_datas[32];
6764 unsigned offsets[32];
6765 unsigned swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 16;
6766 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6767 swizzle_component_size, &write_count, write_datas, offsets);
6768
6769 for (unsigned i = 0; i < write_count; i++) {
6770 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6771 Instruction *instr = bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true, true);
6772 static_cast<MUBUF_instruction *>(instr)->sync = memory_sync_info(storage_scratch, semantic_private);
6773 }
6774 }
6775
6776 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6777 uint8_t log2_ps_iter_samples;
6778 if (ctx->program->info->ps.force_persample) {
6779 log2_ps_iter_samples =
6780 util_logbase2(ctx->options->key.fs.num_samples);
6781 } else {
6782 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6783 }
6784
6785 /* The bit pattern matches that used by fixed function fragment
6786 * processing. */
6787 static const unsigned ps_iter_masks[] = {
6788 0xffff, /* not used */
6789 0x5555,
6790 0x1111,
6791 0x0101,
6792 0x0001,
6793 };
6794 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6795
6796 Builder bld(ctx->program, ctx->block);
6797
6798 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6799 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6800 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6801 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6802 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6803 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6804 }
6805
6806 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6807 Builder bld(ctx->program, ctx->block);
6808
6809 unsigned stream = nir_intrinsic_stream_id(instr);
6810 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6811 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6812 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6813
6814 /* get GSVS ring */
6815 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6816
6817 unsigned num_components =
6818 ctx->program->info->gs.num_stream_output_components[stream];
6819 assert(num_components);
6820
6821 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6822 unsigned stream_offset = 0;
6823 for (unsigned i = 0; i < stream; i++) {
6824 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6825 stream_offset += prev_stride * ctx->program->wave_size;
6826 }
6827
6828 /* Limit on the stride field for <= GFX7. */
6829 assert(stride < (1 << 14));
6830
6831 Temp gsvs_dwords[4];
6832 for (unsigned i = 0; i < 4; i++)
6833 gsvs_dwords[i] = bld.tmp(s1);
6834 bld.pseudo(aco_opcode::p_split_vector,
6835 Definition(gsvs_dwords[0]),
6836 Definition(gsvs_dwords[1]),
6837 Definition(gsvs_dwords[2]),
6838 Definition(gsvs_dwords[3]),
6839 gsvs_ring);
6840
6841 if (stream_offset) {
6842 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6843
6844 Temp carry = bld.tmp(s1);
6845 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6846 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));
6847 }
6848
6849 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)));
6850 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6851
6852 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6853 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6854
6855 unsigned offset = 0;
6856 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6857 if (ctx->program->info->gs.output_streams[i] != stream)
6858 continue;
6859
6860 for (unsigned j = 0; j < 4; j++) {
6861 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6862 continue;
6863
6864 if (ctx->outputs.mask[i] & (1 << j)) {
6865 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6866 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6867 if (const_offset >= 4096u) {
6868 if (vaddr_offset.isUndefined())
6869 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6870 else
6871 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6872 const_offset %= 4096u;
6873 }
6874
6875 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6876 mtbuf->operands[0] = Operand(gsvs_ring);
6877 mtbuf->operands[1] = vaddr_offset;
6878 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6879 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6880 mtbuf->offen = !vaddr_offset.isUndefined();
6881 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6882 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6883 mtbuf->offset = const_offset;
6884 mtbuf->glc = true;
6885 mtbuf->slc = true;
6886 mtbuf->sync = memory_sync_info(storage_vmem_output, semantic_can_reorder);
6887 bld.insert(std::move(mtbuf));
6888 }
6889
6890 offset += ctx->shader->info.gs.vertices_out;
6891 }
6892
6893 /* outputs for the next vertex are undefined and keeping them around can
6894 * create invalid IR with control flow */
6895 ctx->outputs.mask[i] = 0;
6896 }
6897
6898 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6899 }
6900
6901 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6902 {
6903 Builder bld(ctx->program, ctx->block);
6904
6905 if (cluster_size == 1) {
6906 return src;
6907 } if (op == nir_op_iand && cluster_size == 4) {
6908 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6909 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6910 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6911 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6912 } else if (op == nir_op_ior && cluster_size == 4) {
6913 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6914 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6915 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6916 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6917 //subgroupAnd(val) -> (exec & ~val) == 0
6918 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6919 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6920 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6921 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6922 //subgroupOr(val) -> (val & exec) != 0
6923 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6924 return bool_to_vector_condition(ctx, tmp);
6925 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6926 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6927 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6928 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6929 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6930 return bool_to_vector_condition(ctx, tmp);
6931 } else {
6932 //subgroupClustered{And,Or,Xor}(val, n) ->
6933 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6934 //cluster_offset = ~(n - 1) & lane_id
6935 //cluster_mask = ((1 << n) - 1)
6936 //subgroupClusteredAnd():
6937 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6938 //subgroupClusteredOr():
6939 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6940 //subgroupClusteredXor():
6941 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6942 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6943 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6944
6945 Temp tmp;
6946 if (op == nir_op_iand)
6947 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6948 else
6949 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6950
6951 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6952
6953 if (ctx->program->chip_class <= GFX7)
6954 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6955 else if (ctx->program->wave_size == 64)
6956 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6957 else
6958 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6959 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6960 if (cluster_mask != 0xffffffff)
6961 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6962
6963 Definition cmp_def = Definition();
6964 if (op == nir_op_iand) {
6965 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6966 } else if (op == nir_op_ior) {
6967 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6968 } else if (op == nir_op_ixor) {
6969 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
6970 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
6971 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6972 }
6973 cmp_def.setHint(vcc);
6974 return cmp_def.getTemp();
6975 }
6976 }
6977
6978 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
6979 {
6980 Builder bld(ctx->program, ctx->block);
6981
6982 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
6983 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
6984 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
6985 Temp tmp;
6986 if (op == nir_op_iand)
6987 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6988 else
6989 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
6990
6991 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
6992 Temp lo = lohi.def(0).getTemp();
6993 Temp hi = lohi.def(1).getTemp();
6994 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
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(0u), mbcnt).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), mbcnt).def(0);
7001 else if (op == nir_op_ixor)
7002 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7003 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7004 cmp_def.setHint(vcc);
7005 return cmp_def.getTemp();
7006 }
7007
7008 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7009 {
7010 Builder bld(ctx->program, ctx->block);
7011
7012 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7013 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7014 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7015 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7016 if (op == nir_op_iand)
7017 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7018 else if (op == nir_op_ior)
7019 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7020 else if (op == nir_op_ixor)
7021 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7022
7023 assert(false);
7024 return Temp();
7025 }
7026
7027 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7028 {
7029 Builder bld(ctx->program, ctx->block);
7030 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7031 if (src.regClass().type() == RegType::vgpr) {
7032 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7033 } else if (src.regClass() == s1) {
7034 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7035 } else if (src.regClass() == s2) {
7036 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7037 } else {
7038 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7039 }
7040 }
7041
7042 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7043 {
7044 Builder bld(ctx->program, ctx->block);
7045 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7046 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7047 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7048
7049 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7050 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7051 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7052 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7053
7054 /* Build DD X/Y */
7055 if (ctx->program->chip_class >= GFX8) {
7056 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7057 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7058 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7059 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7060 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7061 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7062 } else {
7063 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7064 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7065 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7066 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7067 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7068 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7069 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7070 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7071 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7072 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7073 }
7074
7075 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7076 aco_opcode mad = ctx->program->chip_class >= GFX10_3 ? aco_opcode::v_fma_f32 : aco_opcode::v_mad_f32;
7077 Temp tmp1 = bld.vop3(mad, bld.def(v1), ddx_1, pos1, p1);
7078 Temp tmp2 = bld.vop3(mad, bld.def(v1), ddx_2, pos1, p2);
7079 tmp1 = bld.vop3(mad, bld.def(v1), ddy_1, pos2, tmp1);
7080 tmp2 = bld.vop3(mad, bld.def(v1), ddy_2, pos2, tmp2);
7081 Temp wqm1 = bld.tmp(v1);
7082 emit_wqm(ctx, tmp1, wqm1, true);
7083 Temp wqm2 = bld.tmp(v1);
7084 emit_wqm(ctx, tmp2, wqm2, true);
7085 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7086 return;
7087 }
7088
7089 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7090 {
7091 Builder bld(ctx->program, ctx->block);
7092 switch(instr->intrinsic) {
7093 case nir_intrinsic_load_barycentric_sample:
7094 case nir_intrinsic_load_barycentric_pixel:
7095 case nir_intrinsic_load_barycentric_centroid: {
7096 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7097 Temp bary = Temp(0, s2);
7098 switch (mode) {
7099 case INTERP_MODE_SMOOTH:
7100 case INTERP_MODE_NONE:
7101 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7102 bary = get_arg(ctx, ctx->args->ac.persp_center);
7103 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7104 bary = ctx->persp_centroid;
7105 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7106 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7107 break;
7108 case INTERP_MODE_NOPERSPECTIVE:
7109 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7110 bary = get_arg(ctx, ctx->args->ac.linear_center);
7111 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7112 bary = ctx->linear_centroid;
7113 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7114 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7115 break;
7116 default:
7117 break;
7118 }
7119 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7120 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7121 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7122 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7123 Operand(p1), Operand(p2));
7124 emit_split_vector(ctx, dst, 2);
7125 break;
7126 }
7127 case nir_intrinsic_load_barycentric_model: {
7128 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7129
7130 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7131 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7132 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7133 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7134 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7135 Operand(p1), Operand(p2), Operand(p3));
7136 emit_split_vector(ctx, dst, 3);
7137 break;
7138 }
7139 case nir_intrinsic_load_barycentric_at_sample: {
7140 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7141 switch (ctx->options->key.fs.num_samples) {
7142 case 2: sample_pos_offset += 1 << 3; break;
7143 case 4: sample_pos_offset += 3 << 3; break;
7144 case 8: sample_pos_offset += 7 << 3; break;
7145 default: break;
7146 }
7147 Temp sample_pos;
7148 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7149 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7150 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7151 //TODO: bounds checking?
7152 if (addr.type() == RegType::sgpr) {
7153 Operand offset;
7154 if (const_addr) {
7155 sample_pos_offset += const_addr->u32 << 3;
7156 offset = Operand(sample_pos_offset);
7157 } else if (ctx->options->chip_class >= GFX9) {
7158 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7159 } else {
7160 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7161 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7162 }
7163
7164 Operand off = bld.copy(bld.def(s1), Operand(offset));
7165 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7166
7167 } else if (ctx->options->chip_class >= GFX9) {
7168 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7169 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7170 } else if (ctx->options->chip_class >= GFX7) {
7171 /* addr += private_segment_buffer + sample_pos_offset */
7172 Temp tmp0 = bld.tmp(s1);
7173 Temp tmp1 = bld.tmp(s1);
7174 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7175 Definition scc_tmp = bld.def(s1, scc);
7176 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7177 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7178 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7179 Temp pck0 = bld.tmp(v1);
7180 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7181 tmp1 = as_vgpr(ctx, tmp1);
7182 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);
7183 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7184
7185 /* sample_pos = flat_load_dwordx2 addr */
7186 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7187 } else {
7188 assert(ctx->options->chip_class == GFX6);
7189
7190 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7191 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7192 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7193
7194 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7195 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7196
7197 sample_pos = bld.tmp(v2);
7198
7199 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7200 load->definitions[0] = Definition(sample_pos);
7201 load->operands[0] = Operand(rsrc);
7202 load->operands[1] = Operand(addr);
7203 load->operands[2] = Operand(0u);
7204 load->offset = sample_pos_offset;
7205 load->offen = 0;
7206 load->addr64 = true;
7207 load->glc = false;
7208 load->dlc = false;
7209 load->disable_wqm = false;
7210 ctx->block->instructions.emplace_back(std::move(load));
7211 }
7212
7213 /* sample_pos -= 0.5 */
7214 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7215 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7216 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7217 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7218 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7219
7220 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7221 break;
7222 }
7223 case nir_intrinsic_load_barycentric_at_offset: {
7224 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7225 RegClass rc = RegClass(offset.type(), 1);
7226 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7227 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7228 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7229 break;
7230 }
7231 case nir_intrinsic_load_front_face: {
7232 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7233 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7234 break;
7235 }
7236 case nir_intrinsic_load_view_index: {
7237 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7238 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7239 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7240 break;
7241 }
7242
7243 /* fallthrough */
7244 }
7245 case nir_intrinsic_load_layer_id: {
7246 unsigned idx = nir_intrinsic_base(instr);
7247 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7248 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7249 break;
7250 }
7251 case nir_intrinsic_load_frag_coord: {
7252 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7253 break;
7254 }
7255 case nir_intrinsic_load_sample_pos: {
7256 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7257 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7258 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7259 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7260 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7261 break;
7262 }
7263 case nir_intrinsic_load_tess_coord:
7264 visit_load_tess_coord(ctx, instr);
7265 break;
7266 case nir_intrinsic_load_interpolated_input:
7267 visit_load_interpolated_input(ctx, instr);
7268 break;
7269 case nir_intrinsic_store_output:
7270 visit_store_output(ctx, instr);
7271 break;
7272 case nir_intrinsic_load_input:
7273 case nir_intrinsic_load_input_vertex:
7274 visit_load_input(ctx, instr);
7275 break;
7276 case nir_intrinsic_load_output:
7277 visit_load_output(ctx, instr);
7278 break;
7279 case nir_intrinsic_load_per_vertex_input:
7280 visit_load_per_vertex_input(ctx, instr);
7281 break;
7282 case nir_intrinsic_load_per_vertex_output:
7283 visit_load_per_vertex_output(ctx, instr);
7284 break;
7285 case nir_intrinsic_store_per_vertex_output:
7286 visit_store_per_vertex_output(ctx, instr);
7287 break;
7288 case nir_intrinsic_load_ubo:
7289 visit_load_ubo(ctx, instr);
7290 break;
7291 case nir_intrinsic_load_push_constant:
7292 visit_load_push_constant(ctx, instr);
7293 break;
7294 case nir_intrinsic_load_constant:
7295 visit_load_constant(ctx, instr);
7296 break;
7297 case nir_intrinsic_vulkan_resource_index:
7298 visit_load_resource(ctx, instr);
7299 break;
7300 case nir_intrinsic_discard:
7301 visit_discard(ctx, instr);
7302 break;
7303 case nir_intrinsic_discard_if:
7304 visit_discard_if(ctx, instr);
7305 break;
7306 case nir_intrinsic_load_shared:
7307 visit_load_shared(ctx, instr);
7308 break;
7309 case nir_intrinsic_store_shared:
7310 visit_store_shared(ctx, instr);
7311 break;
7312 case nir_intrinsic_shared_atomic_add:
7313 case nir_intrinsic_shared_atomic_imin:
7314 case nir_intrinsic_shared_atomic_umin:
7315 case nir_intrinsic_shared_atomic_imax:
7316 case nir_intrinsic_shared_atomic_umax:
7317 case nir_intrinsic_shared_atomic_and:
7318 case nir_intrinsic_shared_atomic_or:
7319 case nir_intrinsic_shared_atomic_xor:
7320 case nir_intrinsic_shared_atomic_exchange:
7321 case nir_intrinsic_shared_atomic_comp_swap:
7322 case nir_intrinsic_shared_atomic_fadd:
7323 visit_shared_atomic(ctx, instr);
7324 break;
7325 case nir_intrinsic_image_deref_load:
7326 visit_image_load(ctx, instr);
7327 break;
7328 case nir_intrinsic_image_deref_store:
7329 visit_image_store(ctx, instr);
7330 break;
7331 case nir_intrinsic_image_deref_atomic_add:
7332 case nir_intrinsic_image_deref_atomic_umin:
7333 case nir_intrinsic_image_deref_atomic_imin:
7334 case nir_intrinsic_image_deref_atomic_umax:
7335 case nir_intrinsic_image_deref_atomic_imax:
7336 case nir_intrinsic_image_deref_atomic_and:
7337 case nir_intrinsic_image_deref_atomic_or:
7338 case nir_intrinsic_image_deref_atomic_xor:
7339 case nir_intrinsic_image_deref_atomic_exchange:
7340 case nir_intrinsic_image_deref_atomic_comp_swap:
7341 visit_image_atomic(ctx, instr);
7342 break;
7343 case nir_intrinsic_image_deref_size:
7344 visit_image_size(ctx, instr);
7345 break;
7346 case nir_intrinsic_load_ssbo:
7347 visit_load_ssbo(ctx, instr);
7348 break;
7349 case nir_intrinsic_store_ssbo:
7350 visit_store_ssbo(ctx, instr);
7351 break;
7352 case nir_intrinsic_load_global:
7353 visit_load_global(ctx, instr);
7354 break;
7355 case nir_intrinsic_store_global:
7356 visit_store_global(ctx, instr);
7357 break;
7358 case nir_intrinsic_global_atomic_add:
7359 case nir_intrinsic_global_atomic_imin:
7360 case nir_intrinsic_global_atomic_umin:
7361 case nir_intrinsic_global_atomic_imax:
7362 case nir_intrinsic_global_atomic_umax:
7363 case nir_intrinsic_global_atomic_and:
7364 case nir_intrinsic_global_atomic_or:
7365 case nir_intrinsic_global_atomic_xor:
7366 case nir_intrinsic_global_atomic_exchange:
7367 case nir_intrinsic_global_atomic_comp_swap:
7368 visit_global_atomic(ctx, instr);
7369 break;
7370 case nir_intrinsic_ssbo_atomic_add:
7371 case nir_intrinsic_ssbo_atomic_imin:
7372 case nir_intrinsic_ssbo_atomic_umin:
7373 case nir_intrinsic_ssbo_atomic_imax:
7374 case nir_intrinsic_ssbo_atomic_umax:
7375 case nir_intrinsic_ssbo_atomic_and:
7376 case nir_intrinsic_ssbo_atomic_or:
7377 case nir_intrinsic_ssbo_atomic_xor:
7378 case nir_intrinsic_ssbo_atomic_exchange:
7379 case nir_intrinsic_ssbo_atomic_comp_swap:
7380 visit_atomic_ssbo(ctx, instr);
7381 break;
7382 case nir_intrinsic_load_scratch:
7383 visit_load_scratch(ctx, instr);
7384 break;
7385 case nir_intrinsic_store_scratch:
7386 visit_store_scratch(ctx, instr);
7387 break;
7388 case nir_intrinsic_get_buffer_size:
7389 visit_get_buffer_size(ctx, instr);
7390 break;
7391 case nir_intrinsic_scoped_barrier:
7392 emit_scoped_barrier(ctx, instr);
7393 break;
7394 case nir_intrinsic_load_num_work_groups: {
7395 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7396 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7397 emit_split_vector(ctx, dst, 3);
7398 break;
7399 }
7400 case nir_intrinsic_load_local_invocation_id: {
7401 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7402 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7403 emit_split_vector(ctx, dst, 3);
7404 break;
7405 }
7406 case nir_intrinsic_load_work_group_id: {
7407 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7408 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7409 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7410 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7411 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7412 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7413 emit_split_vector(ctx, dst, 3);
7414 break;
7415 }
7416 case nir_intrinsic_load_local_invocation_index: {
7417 Temp id = emit_mbcnt(ctx, bld.def(v1));
7418
7419 /* The tg_size bits [6:11] contain the subgroup id,
7420 * we need this multiplied by the wave size, and then OR the thread id to it.
7421 */
7422 if (ctx->program->wave_size == 64) {
7423 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7424 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7425 get_arg(ctx, ctx->args->ac.tg_size));
7426 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7427 } else {
7428 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7429 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7430 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7431 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7432 }
7433 break;
7434 }
7435 case nir_intrinsic_load_subgroup_id: {
7436 if (ctx->stage == compute_cs) {
7437 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7438 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7439 } else {
7440 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7441 }
7442 break;
7443 }
7444 case nir_intrinsic_load_subgroup_invocation: {
7445 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7446 break;
7447 }
7448 case nir_intrinsic_load_num_subgroups: {
7449 if (ctx->stage == compute_cs)
7450 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7451 get_arg(ctx, ctx->args->ac.tg_size));
7452 else
7453 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7454 break;
7455 }
7456 case nir_intrinsic_ballot: {
7457 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7458 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7459 Definition tmp = bld.def(dst.regClass());
7460 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7461 if (instr->src[0].ssa->bit_size == 1) {
7462 assert(src.regClass() == bld.lm);
7463 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7464 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7465 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7466 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7467 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7468 } else {
7469 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7470 }
7471 if (dst.size() != bld.lm.size()) {
7472 /* Wave32 with ballot size set to 64 */
7473 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7474 }
7475 emit_wqm(ctx, tmp.getTemp(), dst);
7476 break;
7477 }
7478 case nir_intrinsic_shuffle:
7479 case nir_intrinsic_read_invocation: {
7480 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7481 if (!nir_src_is_divergent(instr->src[0])) {
7482 emit_uniform_subgroup(ctx, instr, src);
7483 } else {
7484 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7485 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7486 tid = bld.as_uniform(tid);
7487 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7488 if (src.regClass() == v1b || src.regClass() == v2b) {
7489 Temp tmp = bld.tmp(v1);
7490 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7491 if (dst.type() == RegType::vgpr)
7492 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7493 else
7494 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7495 } else if (src.regClass() == v1) {
7496 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7497 } else if (src.regClass() == v2) {
7498 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7499 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7500 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7501 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7502 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7503 emit_split_vector(ctx, dst, 2);
7504 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7505 assert(src.regClass() == bld.lm);
7506 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7507 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7508 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7509 assert(src.regClass() == bld.lm);
7510 Temp tmp;
7511 if (ctx->program->chip_class <= GFX7)
7512 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7513 else if (ctx->program->wave_size == 64)
7514 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7515 else
7516 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7517 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7518 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7519 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7520 } else {
7521 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7522 }
7523 }
7524 break;
7525 }
7526 case nir_intrinsic_load_sample_id: {
7527 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7528 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7529 break;
7530 }
7531 case nir_intrinsic_load_sample_mask_in: {
7532 visit_load_sample_mask_in(ctx, instr);
7533 break;
7534 }
7535 case nir_intrinsic_read_first_invocation: {
7536 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7537 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7538 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7539 emit_wqm(ctx,
7540 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7541 dst);
7542 } else if (src.regClass() == v2) {
7543 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7544 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7545 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7546 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7547 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7548 emit_split_vector(ctx, dst, 2);
7549 } else if (instr->dest.ssa.bit_size == 1) {
7550 assert(src.regClass() == bld.lm);
7551 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7552 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7553 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7554 } else if (src.regClass() == s1) {
7555 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7556 } else if (src.regClass() == s2) {
7557 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7558 } else {
7559 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7560 }
7561 break;
7562 }
7563 case nir_intrinsic_vote_all: {
7564 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7565 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7566 assert(src.regClass() == bld.lm);
7567 assert(dst.regClass() == bld.lm);
7568
7569 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7570 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7571 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7572 break;
7573 }
7574 case nir_intrinsic_vote_any: {
7575 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7576 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7577 assert(src.regClass() == bld.lm);
7578 assert(dst.regClass() == bld.lm);
7579
7580 Temp tmp = bool_to_scalar_condition(ctx, src);
7581 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7582 break;
7583 }
7584 case nir_intrinsic_reduce:
7585 case nir_intrinsic_inclusive_scan:
7586 case nir_intrinsic_exclusive_scan: {
7587 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7588 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7589 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7590 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7591 nir_intrinsic_cluster_size(instr) : 0;
7592 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7593
7594 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7595 emit_uniform_subgroup(ctx, instr, src);
7596 } else if (instr->dest.ssa.bit_size == 1) {
7597 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7598 op = nir_op_iand;
7599 else if (op == nir_op_iadd)
7600 op = nir_op_ixor;
7601 else if (op == nir_op_umax || op == nir_op_imax)
7602 op = nir_op_ior;
7603 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7604
7605 switch (instr->intrinsic) {
7606 case nir_intrinsic_reduce:
7607 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7608 break;
7609 case nir_intrinsic_exclusive_scan:
7610 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7611 break;
7612 case nir_intrinsic_inclusive_scan:
7613 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7614 break;
7615 default:
7616 assert(false);
7617 }
7618 } else if (cluster_size == 1) {
7619 bld.copy(Definition(dst), src);
7620 } else {
7621 unsigned bit_size = instr->src[0].ssa->bit_size;
7622
7623 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7624
7625 ReduceOp reduce_op;
7626 switch (op) {
7627 #define CASEI(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : (bit_size == 8) ? name##8 : name##64; break;
7628 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7629 CASEI(iadd)
7630 CASEI(imul)
7631 CASEI(imin)
7632 CASEI(umin)
7633 CASEI(imax)
7634 CASEI(umax)
7635 CASEI(iand)
7636 CASEI(ior)
7637 CASEI(ixor)
7638 CASEF(fadd)
7639 CASEF(fmul)
7640 CASEF(fmin)
7641 CASEF(fmax)
7642 default:
7643 unreachable("unknown reduction op");
7644 #undef CASEI
7645 #undef CASEF
7646 }
7647
7648 aco_opcode aco_op;
7649 switch (instr->intrinsic) {
7650 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7651 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7652 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7653 default:
7654 unreachable("unknown reduce intrinsic");
7655 }
7656
7657 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7658 reduce->operands[0] = Operand(src);
7659 // filled in by aco_reduce_assign.cpp, used internally as part of the
7660 // reduce sequence
7661 assert(dst.size() == 1 || dst.size() == 2);
7662 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7663 reduce->operands[2] = Operand(v1.as_linear());
7664
7665 Temp tmp_dst = bld.tmp(dst.regClass());
7666 reduce->definitions[0] = Definition(tmp_dst);
7667 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7668 reduce->definitions[2] = Definition();
7669 reduce->definitions[3] = Definition(scc, s1);
7670 reduce->definitions[4] = Definition();
7671 reduce->reduce_op = reduce_op;
7672 reduce->cluster_size = cluster_size;
7673 ctx->block->instructions.emplace_back(std::move(reduce));
7674
7675 emit_wqm(ctx, tmp_dst, dst);
7676 }
7677 break;
7678 }
7679 case nir_intrinsic_quad_broadcast: {
7680 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7681 if (!nir_dest_is_divergent(instr->dest)) {
7682 emit_uniform_subgroup(ctx, instr, src);
7683 } else {
7684 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7685 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7686 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7687
7688 if (instr->dest.ssa.bit_size == 1) {
7689 assert(src.regClass() == bld.lm);
7690 assert(dst.regClass() == bld.lm);
7691 uint32_t half_mask = 0x11111111u << lane;
7692 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7693 Temp tmp = bld.tmp(bld.lm);
7694 bld.sop1(Builder::s_wqm, Definition(tmp),
7695 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7696 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7697 emit_wqm(ctx, tmp, dst);
7698 } else if (instr->dest.ssa.bit_size == 8) {
7699 Temp tmp = bld.tmp(v1);
7700 if (ctx->program->chip_class >= GFX8)
7701 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7702 else
7703 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7704 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7705 } else if (instr->dest.ssa.bit_size == 16) {
7706 Temp tmp = bld.tmp(v1);
7707 if (ctx->program->chip_class >= GFX8)
7708 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7709 else
7710 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7711 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7712 } else if (instr->dest.ssa.bit_size == 32) {
7713 if (ctx->program->chip_class >= GFX8)
7714 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7715 else
7716 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7717 } else if (instr->dest.ssa.bit_size == 64) {
7718 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7719 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7720 if (ctx->program->chip_class >= GFX8) {
7721 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7722 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7723 } else {
7724 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7725 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7726 }
7727 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7728 emit_split_vector(ctx, dst, 2);
7729 } else {
7730 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7731 }
7732 }
7733 break;
7734 }
7735 case nir_intrinsic_quad_swap_horizontal:
7736 case nir_intrinsic_quad_swap_vertical:
7737 case nir_intrinsic_quad_swap_diagonal:
7738 case nir_intrinsic_quad_swizzle_amd: {
7739 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7740 if (!nir_dest_is_divergent(instr->dest)) {
7741 emit_uniform_subgroup(ctx, instr, src);
7742 break;
7743 }
7744 uint16_t dpp_ctrl = 0;
7745 switch (instr->intrinsic) {
7746 case nir_intrinsic_quad_swap_horizontal:
7747 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7748 break;
7749 case nir_intrinsic_quad_swap_vertical:
7750 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7751 break;
7752 case nir_intrinsic_quad_swap_diagonal:
7753 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7754 break;
7755 case nir_intrinsic_quad_swizzle_amd:
7756 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7757 break;
7758 default:
7759 break;
7760 }
7761 if (ctx->program->chip_class < GFX8)
7762 dpp_ctrl |= (1 << 15);
7763
7764 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7765 if (instr->dest.ssa.bit_size == 1) {
7766 assert(src.regClass() == bld.lm);
7767 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7768 if (ctx->program->chip_class >= GFX8)
7769 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7770 else
7771 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7772 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7773 emit_wqm(ctx, tmp, dst);
7774 } else if (instr->dest.ssa.bit_size == 8) {
7775 Temp tmp = bld.tmp(v1);
7776 if (ctx->program->chip_class >= GFX8)
7777 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7778 else
7779 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7780 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7781 } else if (instr->dest.ssa.bit_size == 16) {
7782 Temp tmp = bld.tmp(v1);
7783 if (ctx->program->chip_class >= GFX8)
7784 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7785 else
7786 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7787 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7788 } else if (instr->dest.ssa.bit_size == 32) {
7789 Temp tmp;
7790 if (ctx->program->chip_class >= GFX8)
7791 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7792 else
7793 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7794 emit_wqm(ctx, tmp, dst);
7795 } else if (instr->dest.ssa.bit_size == 64) {
7796 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7797 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7798 if (ctx->program->chip_class >= GFX8) {
7799 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7800 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7801 } else {
7802 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7803 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7804 }
7805 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7806 emit_split_vector(ctx, dst, 2);
7807 } else {
7808 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7809 }
7810 break;
7811 }
7812 case nir_intrinsic_masked_swizzle_amd: {
7813 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7814 if (!nir_dest_is_divergent(instr->dest)) {
7815 emit_uniform_subgroup(ctx, instr, src);
7816 break;
7817 }
7818 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7819 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7820 if (instr->dest.ssa.bit_size == 1) {
7821 assert(src.regClass() == bld.lm);
7822 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7823 src = emit_masked_swizzle(ctx, bld, src, mask);
7824 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7825 emit_wqm(ctx, tmp, dst);
7826 } else if (dst.regClass() == v1b) {
7827 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7828 emit_extract_vector(ctx, tmp, 0, dst);
7829 } else if (dst.regClass() == v2b) {
7830 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7831 emit_extract_vector(ctx, tmp, 0, dst);
7832 } else if (dst.regClass() == v1) {
7833 emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask), dst);
7834 } else if (dst.regClass() == v2) {
7835 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7836 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7837 lo = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, lo, mask));
7838 hi = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, hi, mask));
7839 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7840 emit_split_vector(ctx, dst, 2);
7841 } else {
7842 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7843 }
7844 break;
7845 }
7846 case nir_intrinsic_write_invocation_amd: {
7847 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7848 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7849 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7850 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7851 if (dst.regClass() == v1) {
7852 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7853 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7854 } else if (dst.regClass() == v2) {
7855 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7856 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7857 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7858 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7859 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7860 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7861 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7862 emit_split_vector(ctx, dst, 2);
7863 } else {
7864 isel_err(&instr->instr, "Unimplemented NIR instr bit size");
7865 }
7866 break;
7867 }
7868 case nir_intrinsic_mbcnt_amd: {
7869 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7870 RegClass rc = RegClass(src.type(), 1);
7871 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7872 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7873 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7874 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7875 emit_wqm(ctx, wqm_tmp, dst);
7876 break;
7877 }
7878 case nir_intrinsic_load_helper_invocation: {
7879 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7880 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7881 ctx->block->kind |= block_kind_needs_lowering;
7882 ctx->program->needs_exact = true;
7883 break;
7884 }
7885 case nir_intrinsic_is_helper_invocation: {
7886 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7887 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7888 ctx->block->kind |= block_kind_needs_lowering;
7889 ctx->program->needs_exact = true;
7890 break;
7891 }
7892 case nir_intrinsic_demote:
7893 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7894
7895 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7896 ctx->cf_info.exec_potentially_empty_discard = true;
7897 ctx->block->kind |= block_kind_uses_demote;
7898 ctx->program->needs_exact = true;
7899 break;
7900 case nir_intrinsic_demote_if: {
7901 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7902 assert(src.regClass() == bld.lm);
7903 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7904 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7905
7906 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7907 ctx->cf_info.exec_potentially_empty_discard = true;
7908 ctx->block->kind |= block_kind_uses_demote;
7909 ctx->program->needs_exact = true;
7910 break;
7911 }
7912 case nir_intrinsic_first_invocation: {
7913 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7914 get_ssa_temp(ctx, &instr->dest.ssa));
7915 break;
7916 }
7917 case nir_intrinsic_shader_clock: {
7918 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7919 if (nir_intrinsic_memory_scope(instr) == NIR_SCOPE_SUBGROUP && ctx->options->chip_class >= GFX10_3) {
7920 /* "((size - 1) << 11) | register" (SHADER_CYCLES is encoded as register 29) */
7921 Temp clock = bld.sopk(aco_opcode::s_getreg_b32, bld.def(s1), ((20 - 1) << 11) | 29);
7922 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), clock, Operand(0u));
7923 } else {
7924 aco_opcode opcode =
7925 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
7926 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
7927 bld.smem(opcode, Definition(dst), memory_sync_info(0, semantic_volatile));
7928 }
7929 emit_split_vector(ctx, dst, 2);
7930 break;
7931 }
7932 case nir_intrinsic_load_vertex_id_zero_base: {
7933 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7934 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7935 break;
7936 }
7937 case nir_intrinsic_load_first_vertex: {
7938 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7939 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7940 break;
7941 }
7942 case nir_intrinsic_load_base_instance: {
7943 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7944 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7945 break;
7946 }
7947 case nir_intrinsic_load_instance_id: {
7948 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7949 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7950 break;
7951 }
7952 case nir_intrinsic_load_draw_id: {
7953 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7954 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7955 break;
7956 }
7957 case nir_intrinsic_load_invocation_id: {
7958 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7959
7960 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7961 if (ctx->options->chip_class >= GFX10)
7962 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7963 else
7964 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7965 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7966 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7967 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7968 } else {
7969 unreachable("Unsupported stage for load_invocation_id");
7970 }
7971
7972 break;
7973 }
7974 case nir_intrinsic_load_primitive_id: {
7975 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7976
7977 switch (ctx->shader->info.stage) {
7978 case MESA_SHADER_GEOMETRY:
7979 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7980 break;
7981 case MESA_SHADER_TESS_CTRL:
7982 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7983 break;
7984 case MESA_SHADER_TESS_EVAL:
7985 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7986 break;
7987 default:
7988 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7989 }
7990
7991 break;
7992 }
7993 case nir_intrinsic_load_patch_vertices_in: {
7994 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
7995 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
7996
7997 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7998 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
7999 break;
8000 }
8001 case nir_intrinsic_emit_vertex_with_counter: {
8002 visit_emit_vertex_with_counter(ctx, instr);
8003 break;
8004 }
8005 case nir_intrinsic_end_primitive_with_counter: {
8006 unsigned stream = nir_intrinsic_stream_id(instr);
8007 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8008 break;
8009 }
8010 case nir_intrinsic_set_vertex_count: {
8011 /* unused, the HW keeps track of this for us */
8012 break;
8013 }
8014 default:
8015 isel_err(&instr->instr, "Unimplemented intrinsic instr");
8016 abort();
8017
8018 break;
8019 }
8020 }
8021
8022
8023 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8024 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8025 enum glsl_base_type *stype)
8026 {
8027 nir_deref_instr *texture_deref_instr = NULL;
8028 nir_deref_instr *sampler_deref_instr = NULL;
8029 int plane = -1;
8030
8031 for (unsigned i = 0; i < instr->num_srcs; i++) {
8032 switch (instr->src[i].src_type) {
8033 case nir_tex_src_texture_deref:
8034 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8035 break;
8036 case nir_tex_src_sampler_deref:
8037 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8038 break;
8039 case nir_tex_src_plane:
8040 plane = nir_src_as_int(instr->src[i].src);
8041 break;
8042 default:
8043 break;
8044 }
8045 }
8046
8047 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8048
8049 if (!sampler_deref_instr)
8050 sampler_deref_instr = texture_deref_instr;
8051
8052 if (plane >= 0) {
8053 assert(instr->op != nir_texop_txf_ms &&
8054 instr->op != nir_texop_samples_identical);
8055 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8056 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8057 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8058 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8059 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8060 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8061 } else {
8062 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8063 }
8064 if (samp_ptr) {
8065 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8066
8067 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8068 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8069 Builder bld(ctx->program, ctx->block);
8070
8071 /* to avoid unnecessary moves, we split and recombine sampler and image */
8072 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8073 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8074 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8075 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8076 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8077 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8078 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8079 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8080
8081 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8082 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8083 img[0], img[1], img[2], img[3],
8084 img[4], img[5], img[6], img[7]);
8085 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8086 samp[0], samp[1], samp[2], samp[3]);
8087 }
8088 }
8089 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8090 instr->op == nir_texop_samples_identical))
8091 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8092 }
8093
8094 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8095 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8096 {
8097 Builder bld(ctx->program, ctx->block);
8098
8099 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8100 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8101 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8102
8103 Operand neg_one(0xbf800000u);
8104 Operand one(0x3f800000u);
8105 Operand two(0x40000000u);
8106 Operand four(0x40800000u);
8107
8108 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8109 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8110 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8111
8112 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8113 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8114 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8115 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);
8116
8117 // select sc
8118 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8119 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8120 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8121 one, is_ma_y);
8122 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8123
8124 // select tc
8125 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8126 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8127 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8128
8129 // select ma
8130 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8131 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8132 deriv_z, is_ma_z);
8133 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8134 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8135 }
8136
8137 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8138 {
8139 Builder bld(ctx->program, ctx->block);
8140 Temp ma, tc, sc, id;
8141 aco_opcode madak = ctx->program->chip_class >= GFX10_3 ? aco_opcode::v_fmaak_f32 : aco_opcode::v_madak_f32;
8142 aco_opcode madmk = ctx->program->chip_class >= GFX10_3 ? aco_opcode::v_fmamk_f32 : aco_opcode::v_madmk_f32;
8143
8144 if (is_array) {
8145 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8146
8147 // see comment in ac_prepare_cube_coords()
8148 if (ctx->options->chip_class <= GFX8)
8149 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8150 }
8151
8152 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8153
8154 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8155 vop3a->operands[0] = Operand(ma);
8156 vop3a->abs[0] = true;
8157 Temp invma = bld.tmp(v1);
8158 vop3a->definitions[0] = Definition(invma);
8159 ctx->block->instructions.emplace_back(std::move(vop3a));
8160
8161 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8162 if (!is_deriv)
8163 sc = bld.vop2(madak, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8164
8165 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8166 if (!is_deriv)
8167 tc = bld.vop2(madak, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8168
8169 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8170
8171 if (is_deriv) {
8172 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8173 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8174
8175 for (unsigned i = 0; i < 2; i++) {
8176 // see comment in ac_prepare_cube_coords()
8177 Temp deriv_ma;
8178 Temp deriv_sc, deriv_tc;
8179 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8180 &deriv_ma, &deriv_sc, &deriv_tc);
8181
8182 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8183
8184 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8185 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8186 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8187 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8188 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8189 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8190 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8191 }
8192
8193 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8194 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8195 }
8196
8197 if (is_array)
8198 id = bld.vop2(madmk, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8199 coords.resize(3);
8200 coords[0] = sc;
8201 coords[1] = tc;
8202 coords[2] = id;
8203 }
8204
8205 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8206 {
8207 if (vec->parent_instr->type != nir_instr_type_alu)
8208 return;
8209 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8210 if (vec_instr->op != nir_op_vec(vec->num_components))
8211 return;
8212
8213 for (unsigned i = 0; i < vec->num_components; i++) {
8214 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8215 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8216 }
8217 }
8218
8219 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8220 {
8221 Builder bld(ctx->program, ctx->block);
8222 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8223 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8224 has_clamped_lod = false;
8225 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8226 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8227 clamped_lod = Temp();
8228 std::vector<Temp> coords;
8229 std::vector<Temp> derivs;
8230 nir_const_value *sample_index_cv = NULL;
8231 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8232 enum glsl_base_type stype;
8233 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8234
8235 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8236 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8237 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8238 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8239
8240 for (unsigned i = 0; i < instr->num_srcs; i++) {
8241 switch (instr->src[i].src_type) {
8242 case nir_tex_src_coord: {
8243 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8244 for (unsigned i = 0; i < coord.size(); i++)
8245 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8246 break;
8247 }
8248 case nir_tex_src_bias:
8249 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8250 has_bias = true;
8251 break;
8252 case nir_tex_src_lod: {
8253 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8254
8255 if (val && val->f32 <= 0.0) {
8256 level_zero = true;
8257 } else {
8258 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8259 has_lod = true;
8260 }
8261 break;
8262 }
8263 case nir_tex_src_min_lod:
8264 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8265 has_clamped_lod = true;
8266 break;
8267 case nir_tex_src_comparator:
8268 if (instr->is_shadow) {
8269 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8270 has_compare = true;
8271 }
8272 break;
8273 case nir_tex_src_offset:
8274 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8275 get_const_vec(instr->src[i].src.ssa, const_offset);
8276 has_offset = true;
8277 break;
8278 case nir_tex_src_ddx:
8279 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8280 has_ddx = true;
8281 break;
8282 case nir_tex_src_ddy:
8283 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8284 has_ddy = true;
8285 break;
8286 case nir_tex_src_ms_index:
8287 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8288 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8289 has_sample_index = true;
8290 break;
8291 case nir_tex_src_texture_offset:
8292 case nir_tex_src_sampler_offset:
8293 default:
8294 break;
8295 }
8296 }
8297
8298 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8299 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8300
8301 if (instr->op == nir_texop_texture_samples) {
8302 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8303
8304 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8305 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8306 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 */));
8307
8308 Operand default_sample = Operand(1u);
8309 if (ctx->options->robust_buffer_access) {
8310 /* Extract the second dword of the descriptor, if it's
8311 * all zero, then it's a null descriptor.
8312 */
8313 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8314 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8315 default_sample = Operand(is_non_null_descriptor);
8316 }
8317
8318 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8319 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8320 samples, default_sample, bld.scc(is_msaa));
8321 return;
8322 }
8323
8324 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8325 aco_ptr<Instruction> tmp_instr;
8326 Temp acc, pack = Temp();
8327
8328 uint32_t pack_const = 0;
8329 for (unsigned i = 0; i < offset.size(); i++) {
8330 if (!const_offset[i])
8331 continue;
8332 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8333 }
8334
8335 if (offset.type() == RegType::sgpr) {
8336 for (unsigned i = 0; i < offset.size(); i++) {
8337 if (const_offset[i])
8338 continue;
8339
8340 acc = emit_extract_vector(ctx, offset, i, s1);
8341 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8342
8343 if (i) {
8344 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8345 }
8346
8347 if (pack == Temp()) {
8348 pack = acc;
8349 } else {
8350 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8351 }
8352 }
8353
8354 if (pack_const && pack != Temp())
8355 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8356 } else {
8357 for (unsigned i = 0; i < offset.size(); i++) {
8358 if (const_offset[i])
8359 continue;
8360
8361 acc = emit_extract_vector(ctx, offset, i, v1);
8362 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8363
8364 if (i) {
8365 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8366 }
8367
8368 if (pack == Temp()) {
8369 pack = acc;
8370 } else {
8371 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8372 }
8373 }
8374
8375 if (pack_const && pack != Temp())
8376 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8377 }
8378 if (pack_const && pack == Temp())
8379 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8380 else if (pack == Temp())
8381 has_offset = false;
8382 else
8383 offset = pack;
8384 }
8385
8386 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8387 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8388
8389 /* pack derivatives */
8390 if (has_ddx || has_ddy) {
8391 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8392 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8393 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8394 derivs = {ddx, zero, ddy, zero};
8395 } else {
8396 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8397 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8398 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8399 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8400 }
8401 has_derivs = true;
8402 }
8403
8404 if (instr->coord_components > 1 &&
8405 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8406 instr->is_array &&
8407 instr->op != nir_texop_txf)
8408 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8409
8410 if (instr->coord_components > 2 &&
8411 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8412 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8413 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8414 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8415 instr->is_array &&
8416 instr->op != nir_texop_txf &&
8417 instr->op != nir_texop_txf_ms &&
8418 instr->op != nir_texop_fragment_fetch &&
8419 instr->op != nir_texop_fragment_mask_fetch)
8420 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8421
8422 if (ctx->options->chip_class == GFX9 &&
8423 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8424 instr->op != nir_texop_lod && instr->coord_components) {
8425 assert(coords.size() > 0 && coords.size() < 3);
8426
8427 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8428 Operand((uint32_t) 0) :
8429 Operand((uint32_t) 0x3f000000)));
8430 }
8431
8432 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8433
8434 if (instr->op == nir_texop_samples_identical)
8435 resource = fmask_ptr;
8436
8437 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8438 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8439 instr->op != nir_texop_txs &&
8440 instr->op != nir_texop_fragment_fetch &&
8441 instr->op != nir_texop_fragment_mask_fetch) {
8442 assert(has_sample_index);
8443 Operand op(sample_index);
8444 if (sample_index_cv)
8445 op = Operand(sample_index_cv->u32);
8446 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8447 }
8448
8449 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8450 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8451 Temp off = emit_extract_vector(ctx, offset, i, v1);
8452 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8453 }
8454 has_offset = false;
8455 }
8456
8457 /* Build tex instruction */
8458 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8459 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8460 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8461 : 0;
8462 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8463 Temp tmp_dst = dst;
8464
8465 /* gather4 selects the component by dmask and always returns vec4 */
8466 if (instr->op == nir_texop_tg4) {
8467 assert(instr->dest.ssa.num_components == 4);
8468 if (instr->is_shadow)
8469 dmask = 1;
8470 else
8471 dmask = 1 << instr->component;
8472 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8473 tmp_dst = bld.tmp(v4);
8474 } else if (instr->op == nir_texop_samples_identical) {
8475 tmp_dst = bld.tmp(v1);
8476 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8477 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8478 }
8479
8480 aco_ptr<MIMG_instruction> tex;
8481 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8482 if (!has_lod)
8483 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8484
8485 bool div_by_6 = instr->op == nir_texop_txs &&
8486 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8487 instr->is_array &&
8488 (dmask & (1 << 2));
8489 if (tmp_dst.id() == dst.id() && div_by_6)
8490 tmp_dst = bld.tmp(tmp_dst.regClass());
8491
8492 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8493 tex->operands[0] = Operand(resource);
8494 tex->operands[1] = Operand(s4); /* no sampler */
8495 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8496 if (ctx->options->chip_class == GFX9 &&
8497 instr->op == nir_texop_txs &&
8498 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8499 instr->is_array) {
8500 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8501 } else if (instr->op == nir_texop_query_levels) {
8502 tex->dmask = 1 << 3;
8503 } else {
8504 tex->dmask = dmask;
8505 }
8506 tex->da = da;
8507 tex->definitions[0] = Definition(tmp_dst);
8508 tex->dim = dim;
8509 ctx->block->instructions.emplace_back(std::move(tex));
8510
8511 if (div_by_6) {
8512 /* divide 3rd value by 6 by multiplying with magic number */
8513 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8514 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8515 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8516 assert(instr->dest.ssa.num_components == 3);
8517 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8518 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8519 emit_extract_vector(ctx, tmp_dst, 0, v1),
8520 emit_extract_vector(ctx, tmp_dst, 1, v1),
8521 by_6);
8522
8523 }
8524
8525 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8526 return;
8527 }
8528
8529 Temp tg4_compare_cube_wa64 = Temp();
8530
8531 if (tg4_integer_workarounds) {
8532 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8533 tex->operands[0] = Operand(resource);
8534 tex->operands[1] = Operand(s4); /* no sampler */
8535 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8536 tex->dim = dim;
8537 tex->dmask = 0x3;
8538 tex->da = da;
8539 Temp size = bld.tmp(v2);
8540 tex->definitions[0] = Definition(size);
8541 ctx->block->instructions.emplace_back(std::move(tex));
8542 emit_split_vector(ctx, size, size.size());
8543
8544 Temp half_texel[2];
8545 for (unsigned i = 0; i < 2; i++) {
8546 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8547 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8548 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8549 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8550 }
8551
8552 Temp new_coords[2] = {
8553 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8554 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8555 };
8556
8557 if (tg4_integer_cube_workaround) {
8558 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8559 Temp desc[resource.size()];
8560 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8561 Format::PSEUDO, 1, resource.size())};
8562 split->operands[0] = Operand(resource);
8563 for (unsigned i = 0; i < resource.size(); i++) {
8564 desc[i] = bld.tmp(s1);
8565 split->definitions[i] = Definition(desc[i]);
8566 }
8567 ctx->block->instructions.emplace_back(std::move(split));
8568
8569 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8570 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8571 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8572
8573 Temp nfmt;
8574 if (stype == GLSL_TYPE_UINT) {
8575 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8576 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8577 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8578 bld.scc(compare_cube_wa));
8579 } else {
8580 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8581 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8582 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8583 bld.scc(compare_cube_wa));
8584 }
8585 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8586 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8587
8588 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8589
8590 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8591 Operand((uint32_t)C_008F14_NUM_FORMAT));
8592 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8593
8594 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8595 Format::PSEUDO, resource.size(), 1)};
8596 for (unsigned i = 0; i < resource.size(); i++)
8597 vec->operands[i] = Operand(desc[i]);
8598 resource = bld.tmp(resource.regClass());
8599 vec->definitions[0] = Definition(resource);
8600 ctx->block->instructions.emplace_back(std::move(vec));
8601
8602 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8603 new_coords[0], coords[0], tg4_compare_cube_wa64);
8604 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8605 new_coords[1], coords[1], tg4_compare_cube_wa64);
8606 }
8607 coords[0] = new_coords[0];
8608 coords[1] = new_coords[1];
8609 }
8610
8611 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8612 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8613
8614 assert(coords.size() == 1);
8615 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8616 aco_opcode op;
8617 switch (last_bit) {
8618 case 1:
8619 op = aco_opcode::buffer_load_format_x; break;
8620 case 2:
8621 op = aco_opcode::buffer_load_format_xy; break;
8622 case 3:
8623 op = aco_opcode::buffer_load_format_xyz; break;
8624 case 4:
8625 op = aco_opcode::buffer_load_format_xyzw; break;
8626 default:
8627 unreachable("Tex instruction loads more than 4 components.");
8628 }
8629
8630 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8631 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8632 tmp_dst = dst;
8633 else
8634 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8635
8636 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8637 mubuf->operands[0] = Operand(resource);
8638 mubuf->operands[1] = Operand(coords[0]);
8639 mubuf->operands[2] = Operand((uint32_t) 0);
8640 mubuf->definitions[0] = Definition(tmp_dst);
8641 mubuf->idxen = true;
8642 ctx->block->instructions.emplace_back(std::move(mubuf));
8643
8644 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8645 return;
8646 }
8647
8648 /* gather MIMG address components */
8649 std::vector<Temp> args;
8650 if (has_offset)
8651 args.emplace_back(offset);
8652 if (has_bias)
8653 args.emplace_back(bias);
8654 if (has_compare)
8655 args.emplace_back(compare);
8656 if (has_derivs)
8657 args.insert(args.end(), derivs.begin(), derivs.end());
8658
8659 args.insert(args.end(), coords.begin(), coords.end());
8660 if (has_sample_index)
8661 args.emplace_back(sample_index);
8662 if (has_lod)
8663 args.emplace_back(lod);
8664 if (has_clamped_lod)
8665 args.emplace_back(clamped_lod);
8666
8667 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8668 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8669 vec->definitions[0] = Definition(arg);
8670 for (unsigned i = 0; i < args.size(); i++)
8671 vec->operands[i] = Operand(args[i]);
8672 ctx->block->instructions.emplace_back(std::move(vec));
8673
8674
8675 if (instr->op == nir_texop_txf ||
8676 instr->op == nir_texop_txf_ms ||
8677 instr->op == nir_texop_samples_identical ||
8678 instr->op == nir_texop_fragment_fetch ||
8679 instr->op == nir_texop_fragment_mask_fetch) {
8680 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;
8681 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8682 tex->operands[0] = Operand(resource);
8683 tex->operands[1] = Operand(s4); /* no sampler */
8684 tex->operands[2] = Operand(arg);
8685 tex->dim = dim;
8686 tex->dmask = dmask;
8687 tex->unrm = true;
8688 tex->da = da;
8689 tex->definitions[0] = Definition(tmp_dst);
8690 ctx->block->instructions.emplace_back(std::move(tex));
8691
8692 if (instr->op == nir_texop_samples_identical) {
8693 assert(dmask == 1 && dst.regClass() == v1);
8694 assert(dst.id() != tmp_dst.id());
8695
8696 Temp tmp = bld.tmp(bld.lm);
8697 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8698 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8699
8700 } else {
8701 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8702 }
8703 return;
8704 }
8705
8706 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8707 aco_opcode opcode = aco_opcode::image_sample;
8708 if (has_offset) { /* image_sample_*_o */
8709 if (has_clamped_lod) {
8710 if (has_compare) {
8711 opcode = aco_opcode::image_sample_c_cl_o;
8712 if (has_derivs)
8713 opcode = aco_opcode::image_sample_c_d_cl_o;
8714 if (has_bias)
8715 opcode = aco_opcode::image_sample_c_b_cl_o;
8716 } else {
8717 opcode = aco_opcode::image_sample_cl_o;
8718 if (has_derivs)
8719 opcode = aco_opcode::image_sample_d_cl_o;
8720 if (has_bias)
8721 opcode = aco_opcode::image_sample_b_cl_o;
8722 }
8723 } else if (has_compare) {
8724 opcode = aco_opcode::image_sample_c_o;
8725 if (has_derivs)
8726 opcode = aco_opcode::image_sample_c_d_o;
8727 if (has_bias)
8728 opcode = aco_opcode::image_sample_c_b_o;
8729 if (level_zero)
8730 opcode = aco_opcode::image_sample_c_lz_o;
8731 if (has_lod)
8732 opcode = aco_opcode::image_sample_c_l_o;
8733 } else {
8734 opcode = aco_opcode::image_sample_o;
8735 if (has_derivs)
8736 opcode = aco_opcode::image_sample_d_o;
8737 if (has_bias)
8738 opcode = aco_opcode::image_sample_b_o;
8739 if (level_zero)
8740 opcode = aco_opcode::image_sample_lz_o;
8741 if (has_lod)
8742 opcode = aco_opcode::image_sample_l_o;
8743 }
8744 } else if (has_clamped_lod) { /* image_sample_*_cl */
8745 if (has_compare) {
8746 opcode = aco_opcode::image_sample_c_cl;
8747 if (has_derivs)
8748 opcode = aco_opcode::image_sample_c_d_cl;
8749 if (has_bias)
8750 opcode = aco_opcode::image_sample_c_b_cl;
8751 } else {
8752 opcode = aco_opcode::image_sample_cl;
8753 if (has_derivs)
8754 opcode = aco_opcode::image_sample_d_cl;
8755 if (has_bias)
8756 opcode = aco_opcode::image_sample_b_cl;
8757 }
8758 } else { /* no offset */
8759 if (has_compare) {
8760 opcode = aco_opcode::image_sample_c;
8761 if (has_derivs)
8762 opcode = aco_opcode::image_sample_c_d;
8763 if (has_bias)
8764 opcode = aco_opcode::image_sample_c_b;
8765 if (level_zero)
8766 opcode = aco_opcode::image_sample_c_lz;
8767 if (has_lod)
8768 opcode = aco_opcode::image_sample_c_l;
8769 } else {
8770 opcode = aco_opcode::image_sample;
8771 if (has_derivs)
8772 opcode = aco_opcode::image_sample_d;
8773 if (has_bias)
8774 opcode = aco_opcode::image_sample_b;
8775 if (level_zero)
8776 opcode = aco_opcode::image_sample_lz;
8777 if (has_lod)
8778 opcode = aco_opcode::image_sample_l;
8779 }
8780 }
8781
8782 if (instr->op == nir_texop_tg4) {
8783 if (has_offset) { /* image_gather4_*_o */
8784 if (has_compare) {
8785 opcode = aco_opcode::image_gather4_c_lz_o;
8786 if (has_lod)
8787 opcode = aco_opcode::image_gather4_c_l_o;
8788 if (has_bias)
8789 opcode = aco_opcode::image_gather4_c_b_o;
8790 } else {
8791 opcode = aco_opcode::image_gather4_lz_o;
8792 if (has_lod)
8793 opcode = aco_opcode::image_gather4_l_o;
8794 if (has_bias)
8795 opcode = aco_opcode::image_gather4_b_o;
8796 }
8797 } else {
8798 if (has_compare) {
8799 opcode = aco_opcode::image_gather4_c_lz;
8800 if (has_lod)
8801 opcode = aco_opcode::image_gather4_c_l;
8802 if (has_bias)
8803 opcode = aco_opcode::image_gather4_c_b;
8804 } else {
8805 opcode = aco_opcode::image_gather4_lz;
8806 if (has_lod)
8807 opcode = aco_opcode::image_gather4_l;
8808 if (has_bias)
8809 opcode = aco_opcode::image_gather4_b;
8810 }
8811 }
8812 } else if (instr->op == nir_texop_lod) {
8813 opcode = aco_opcode::image_get_lod;
8814 }
8815
8816 /* we don't need the bias, sample index, compare value or offset to be
8817 * computed in WQM but if the p_create_vector copies the coordinates, then it
8818 * needs to be in WQM */
8819 if (ctx->stage == fragment_fs &&
8820 !has_derivs && !has_lod && !level_zero &&
8821 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8822 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8823 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8824
8825 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8826 tex->operands[0] = Operand(resource);
8827 tex->operands[1] = Operand(sampler);
8828 tex->operands[2] = Operand(arg);
8829 tex->dim = dim;
8830 tex->dmask = dmask;
8831 tex->da = da;
8832 tex->definitions[0] = Definition(tmp_dst);
8833 ctx->block->instructions.emplace_back(std::move(tex));
8834
8835 if (tg4_integer_cube_workaround) {
8836 assert(tmp_dst.id() != dst.id());
8837 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8838
8839 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8840 Temp val[4];
8841 for (unsigned i = 0; i < dst.size(); i++) {
8842 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8843 Temp cvt_val;
8844 if (stype == GLSL_TYPE_UINT)
8845 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8846 else
8847 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8848 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8849 }
8850 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8851 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8852 val[0], val[1], val[2], val[3]);
8853 }
8854 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8855 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8856
8857 }
8858
8859
8860 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa, RegClass rc, bool logical)
8861 {
8862 Temp tmp = get_ssa_temp(ctx, ssa);
8863 if (ssa->parent_instr->type == nir_instr_type_ssa_undef) {
8864 return Operand(rc);
8865 } else if (logical && ssa->bit_size == 1 && ssa->parent_instr->type == nir_instr_type_load_const) {
8866 if (ctx->program->wave_size == 64)
8867 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT64_MAX : 0u);
8868 else
8869 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT32_MAX : 0u);
8870 } else {
8871 return Operand(tmp);
8872 }
8873 }
8874
8875 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8876 {
8877 aco_ptr<Pseudo_instruction> phi;
8878 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8879 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8880
8881 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
8882 logical |= ctx->block->kind & block_kind_merge;
8883 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8884
8885 /* we want a sorted list of sources, since the predecessor list is also sorted */
8886 std::map<unsigned, nir_ssa_def*> phi_src;
8887 nir_foreach_phi_src(src, instr)
8888 phi_src[src->pred->index] = src->src.ssa;
8889
8890 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8891 unsigned num_operands = 0;
8892 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8893 unsigned num_defined = 0;
8894 unsigned cur_pred_idx = 0;
8895 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8896 if (cur_pred_idx < preds.size()) {
8897 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8898 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8899 unsigned skipped = 0;
8900 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8901 skipped++;
8902 if (cur_pred_idx + skipped < preds.size()) {
8903 for (unsigned i = 0; i < skipped; i++)
8904 operands[num_operands++] = Operand(dst.regClass());
8905 cur_pred_idx += skipped;
8906 } else {
8907 continue;
8908 }
8909 }
8910 /* Handle missing predecessors at the end. This shouldn't happen with loop
8911 * headers and we can't ignore these sources for loop header phis. */
8912 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8913 continue;
8914 cur_pred_idx++;
8915 Operand op = get_phi_operand(ctx, src.second, dst.regClass(), logical);
8916 operands[num_operands++] = op;
8917 num_defined += !op.isUndefined();
8918 }
8919 /* handle block_kind_continue_or_break at loop exit blocks */
8920 while (cur_pred_idx++ < preds.size())
8921 operands[num_operands++] = Operand(dst.regClass());
8922
8923 /* If the loop ends with a break, still add a linear continue edge in case
8924 * that break is divergent or continue_or_break is used. We'll either remove
8925 * this operand later in visit_loop() if it's not necessary or replace the
8926 * undef with something correct. */
8927 if (!logical && ctx->block->kind & block_kind_loop_header) {
8928 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8929 nir_block *last = nir_loop_last_block(loop);
8930 if (last->successors[0] != instr->instr.block)
8931 operands[num_operands++] = Operand(RegClass());
8932 }
8933
8934 if (num_defined == 0) {
8935 Builder bld(ctx->program, ctx->block);
8936 if (dst.regClass() == s1) {
8937 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8938 } else if (dst.regClass() == v1) {
8939 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8940 } else {
8941 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8942 for (unsigned i = 0; i < dst.size(); i++)
8943 vec->operands[i] = Operand(0u);
8944 vec->definitions[0] = Definition(dst);
8945 ctx->block->instructions.emplace_back(std::move(vec));
8946 }
8947 return;
8948 }
8949
8950 /* we can use a linear phi in some cases if one src is undef */
8951 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8952 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8953
8954 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8955 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8956 assert(invert->kind & block_kind_invert);
8957
8958 unsigned then_block = invert->linear_preds[0];
8959
8960 Block* insert_block = NULL;
8961 for (unsigned i = 0; i < num_operands; i++) {
8962 Operand op = operands[i];
8963 if (op.isUndefined())
8964 continue;
8965 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8966 phi->operands[0] = op;
8967 break;
8968 }
8969 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8970 phi->operands[1] = Operand(dst.regClass());
8971 phi->definitions[0] = Definition(dst);
8972 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8973 return;
8974 }
8975
8976 /* try to scalarize vector phis */
8977 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8978 // TODO: scalarize linear phis on divergent ifs
8979 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8980 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8981 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8982 Operand src = operands[i];
8983 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8984 can_scalarize = false;
8985 }
8986 if (can_scalarize) {
8987 unsigned num_components = instr->dest.ssa.num_components;
8988 assert(dst.size() % num_components == 0);
8989 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8990
8991 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8992 for (unsigned k = 0; k < num_components; k++) {
8993 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8994 for (unsigned i = 0; i < num_operands; i++) {
8995 Operand src = operands[i];
8996 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8997 }
8998 Temp phi_dst = {ctx->program->allocateId(), rc};
8999 phi->definitions[0] = Definition(phi_dst);
9000 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9001 new_vec[k] = phi_dst;
9002 vec->operands[k] = Operand(phi_dst);
9003 }
9004 vec->definitions[0] = Definition(dst);
9005 ctx->block->instructions.emplace_back(std::move(vec));
9006 ctx->allocated_vec.emplace(dst.id(), new_vec);
9007 return;
9008 }
9009 }
9010
9011 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9012 for (unsigned i = 0; i < num_operands; i++)
9013 phi->operands[i] = operands[i];
9014 phi->definitions[0] = Definition(dst);
9015 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9016 }
9017
9018
9019 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9020 {
9021 Temp dst = get_ssa_temp(ctx, &instr->def);
9022
9023 assert(dst.type() == RegType::sgpr);
9024
9025 if (dst.size() == 1) {
9026 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9027 } else {
9028 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9029 for (unsigned i = 0; i < dst.size(); i++)
9030 vec->operands[i] = Operand(0u);
9031 vec->definitions[0] = Definition(dst);
9032 ctx->block->instructions.emplace_back(std::move(vec));
9033 }
9034 }
9035
9036 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9037 {
9038 Builder bld(ctx->program, ctx->block);
9039 Block *logical_target;
9040 append_logical_end(ctx->block);
9041 unsigned idx = ctx->block->index;
9042
9043 switch (instr->type) {
9044 case nir_jump_break:
9045 logical_target = ctx->cf_info.parent_loop.exit;
9046 add_logical_edge(idx, logical_target);
9047 ctx->block->kind |= block_kind_break;
9048
9049 if (!ctx->cf_info.parent_if.is_divergent &&
9050 !ctx->cf_info.parent_loop.has_divergent_continue) {
9051 /* uniform break - directly jump out of the loop */
9052 ctx->block->kind |= block_kind_uniform;
9053 ctx->cf_info.has_branch = true;
9054 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9055 add_linear_edge(idx, logical_target);
9056 return;
9057 }
9058 ctx->cf_info.parent_loop.has_divergent_branch = true;
9059 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9060 break;
9061 case nir_jump_continue:
9062 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9063 add_logical_edge(idx, logical_target);
9064 ctx->block->kind |= block_kind_continue;
9065
9066 if (ctx->cf_info.parent_if.is_divergent) {
9067 /* for potential uniform breaks after this continue,
9068 we must ensure that they are handled correctly */
9069 ctx->cf_info.parent_loop.has_divergent_continue = true;
9070 ctx->cf_info.parent_loop.has_divergent_branch = true;
9071 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9072 } else {
9073 /* uniform continue - directly jump to the loop header */
9074 ctx->block->kind |= block_kind_uniform;
9075 ctx->cf_info.has_branch = true;
9076 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9077 add_linear_edge(idx, logical_target);
9078 return;
9079 }
9080 break;
9081 default:
9082 isel_err(&instr->instr, "Unknown NIR jump instr");
9083 abort();
9084 }
9085
9086 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9087 ctx->cf_info.exec_potentially_empty_break = true;
9088 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9089 }
9090
9091 /* remove critical edges from linear CFG */
9092 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9093 Block* break_block = ctx->program->create_and_insert_block();
9094 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9095 break_block->kind |= block_kind_uniform;
9096 add_linear_edge(idx, break_block);
9097 /* the loop_header pointer might be invalidated by this point */
9098 if (instr->type == nir_jump_continue)
9099 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9100 add_linear_edge(break_block->index, logical_target);
9101 bld.reset(break_block);
9102 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9103
9104 Block* continue_block = ctx->program->create_and_insert_block();
9105 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9106 add_linear_edge(idx, continue_block);
9107 append_logical_start(continue_block);
9108 ctx->block = continue_block;
9109 return;
9110 }
9111
9112 void visit_block(isel_context *ctx, nir_block *block)
9113 {
9114 nir_foreach_instr(instr, block) {
9115 switch (instr->type) {
9116 case nir_instr_type_alu:
9117 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9118 break;
9119 case nir_instr_type_load_const:
9120 visit_load_const(ctx, nir_instr_as_load_const(instr));
9121 break;
9122 case nir_instr_type_intrinsic:
9123 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9124 break;
9125 case nir_instr_type_tex:
9126 visit_tex(ctx, nir_instr_as_tex(instr));
9127 break;
9128 case nir_instr_type_phi:
9129 visit_phi(ctx, nir_instr_as_phi(instr));
9130 break;
9131 case nir_instr_type_ssa_undef:
9132 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9133 break;
9134 case nir_instr_type_deref:
9135 break;
9136 case nir_instr_type_jump:
9137 visit_jump(ctx, nir_instr_as_jump(instr));
9138 break;
9139 default:
9140 isel_err(instr, "Unknown NIR instr type");
9141 //abort();
9142 }
9143 }
9144
9145 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9146 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9147 }
9148
9149
9150
9151 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9152 aco_ptr<Instruction>& header_phi, Operand *vals)
9153 {
9154 vals[0] = Operand(header_phi->definitions[0].getTemp());
9155 RegClass rc = vals[0].regClass();
9156
9157 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9158
9159 unsigned next_pred = 1;
9160
9161 for (unsigned idx = first + 1; idx <= last; idx++) {
9162 Block& block = ctx->program->blocks[idx];
9163 if (block.loop_nest_depth != loop_nest_depth) {
9164 vals[idx - first] = vals[idx - 1 - first];
9165 continue;
9166 }
9167
9168 if (block.kind & block_kind_continue) {
9169 vals[idx - first] = header_phi->operands[next_pred];
9170 next_pred++;
9171 continue;
9172 }
9173
9174 bool all_same = true;
9175 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9176 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9177
9178 Operand val;
9179 if (all_same) {
9180 val = vals[block.linear_preds[0] - first];
9181 } else {
9182 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9183 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9184 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9185 phi->operands[i] = vals[block.linear_preds[i] - first];
9186 val = Operand(Temp(ctx->program->allocateId(), rc));
9187 phi->definitions[0] = Definition(val.getTemp());
9188 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9189 }
9190 vals[idx - first] = val;
9191 }
9192
9193 return vals[last - first];
9194 }
9195
9196 static void visit_loop(isel_context *ctx, nir_loop *loop)
9197 {
9198 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9199 append_logical_end(ctx->block);
9200 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9201 Builder bld(ctx->program, ctx->block);
9202 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9203 unsigned loop_preheader_idx = ctx->block->index;
9204
9205 Block loop_exit = Block();
9206 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9207 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9208
9209 Block* loop_header = ctx->program->create_and_insert_block();
9210 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9211 loop_header->kind |= block_kind_loop_header;
9212 add_edge(loop_preheader_idx, loop_header);
9213 ctx->block = loop_header;
9214
9215 /* emit loop body */
9216 unsigned loop_header_idx = loop_header->index;
9217 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9218 append_logical_start(ctx->block);
9219 bool unreachable = visit_cf_list(ctx, &loop->body);
9220
9221 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9222 if (!ctx->cf_info.has_branch) {
9223 append_logical_end(ctx->block);
9224 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9225 /* Discards can result in code running with an empty exec mask.
9226 * This would result in divergent breaks not ever being taken. As a
9227 * workaround, break the loop when the loop mask is empty instead of
9228 * always continuing. */
9229 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9230 unsigned block_idx = ctx->block->index;
9231
9232 /* create helper blocks to avoid critical edges */
9233 Block *break_block = ctx->program->create_and_insert_block();
9234 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9235 break_block->kind = block_kind_uniform;
9236 bld.reset(break_block);
9237 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9238 add_linear_edge(block_idx, break_block);
9239 add_linear_edge(break_block->index, &loop_exit);
9240
9241 Block *continue_block = ctx->program->create_and_insert_block();
9242 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9243 continue_block->kind = block_kind_uniform;
9244 bld.reset(continue_block);
9245 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9246 add_linear_edge(block_idx, continue_block);
9247 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9248
9249 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9250 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9251 ctx->block = &ctx->program->blocks[block_idx];
9252 } else {
9253 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9254 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9255 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9256 else
9257 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9258 }
9259
9260 bld.reset(ctx->block);
9261 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
9262 }
9263
9264 /* Fixup phis in loop header from unreachable blocks.
9265 * has_branch/has_divergent_branch also indicates if the loop ends with a
9266 * break/continue instruction, but we don't emit those if unreachable=true */
9267 if (unreachable) {
9268 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9269 bool linear = ctx->cf_info.has_branch;
9270 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9271 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9272 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9273 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9274 /* the last operand should be the one that needs to be removed */
9275 instr->operands.pop_back();
9276 } else if (!is_phi(instr)) {
9277 break;
9278 }
9279 }
9280 }
9281
9282 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9283 * and the previous one shouldn't both happen at once because a break in the
9284 * merge block would get CSE'd */
9285 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9286 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9287 Operand vals[num_vals];
9288 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9289 if (instr->opcode == aco_opcode::p_linear_phi) {
9290 if (ctx->cf_info.has_branch)
9291 instr->operands.pop_back();
9292 else
9293 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9294 } else if (!is_phi(instr)) {
9295 break;
9296 }
9297 }
9298 }
9299
9300 ctx->cf_info.has_branch = false;
9301
9302 // TODO: if the loop has not a single exit, we must add one °°
9303 /* emit loop successor block */
9304 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9305 append_logical_start(ctx->block);
9306
9307 #if 0
9308 // TODO: check if it is beneficial to not branch on continues
9309 /* trim linear phis in loop header */
9310 for (auto&& instr : loop_entry->instructions) {
9311 if (instr->opcode == aco_opcode::p_linear_phi) {
9312 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9313 new_phi->definitions[0] = instr->definitions[0];
9314 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9315 new_phi->operands[i] = instr->operands[i];
9316 /* check that the remaining operands are all the same */
9317 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9318 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9319 instr.swap(new_phi);
9320 } else if (instr->opcode == aco_opcode::p_phi) {
9321 continue;
9322 } else {
9323 break;
9324 }
9325 }
9326 #endif
9327 }
9328
9329 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9330 {
9331 ic->cond = cond;
9332
9333 append_logical_end(ctx->block);
9334 ctx->block->kind |= block_kind_branch;
9335
9336 /* branch to linear then block */
9337 assert(cond.regClass() == ctx->program->lane_mask);
9338 aco_ptr<Pseudo_branch_instruction> branch;
9339 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 1));
9340 branch->definitions[0] = {ctx->program->allocateId(), s2};
9341 branch->definitions[0].setHint(vcc);
9342 branch->operands[0] = Operand(cond);
9343 ctx->block->instructions.push_back(std::move(branch));
9344
9345 ic->BB_if_idx = ctx->block->index;
9346 ic->BB_invert = Block();
9347 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9348 /* Invert blocks are intentionally not marked as top level because they
9349 * are not part of the logical cfg. */
9350 ic->BB_invert.kind |= block_kind_invert;
9351 ic->BB_endif = Block();
9352 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9353 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9354
9355 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9356 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9357 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9358 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9359 ctx->cf_info.parent_if.is_divergent = true;
9360
9361 /* divergent branches use cbranch_execz */
9362 ctx->cf_info.exec_potentially_empty_discard = false;
9363 ctx->cf_info.exec_potentially_empty_break = false;
9364 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9365
9366 /** emit logical then block */
9367 Block* BB_then_logical = ctx->program->create_and_insert_block();
9368 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9369 add_edge(ic->BB_if_idx, BB_then_logical);
9370 ctx->block = BB_then_logical;
9371 append_logical_start(BB_then_logical);
9372 }
9373
9374 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9375 {
9376 Block *BB_then_logical = ctx->block;
9377 append_logical_end(BB_then_logical);
9378 /* branch from logical then block to invert block */
9379 aco_ptr<Pseudo_branch_instruction> branch;
9380 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 1));
9381 branch->definitions[0] = {ctx->program->allocateId(), s2};
9382 branch->definitions[0].setHint(vcc);
9383 BB_then_logical->instructions.emplace_back(std::move(branch));
9384 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9385 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9386 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9387 BB_then_logical->kind |= block_kind_uniform;
9388 assert(!ctx->cf_info.has_branch);
9389 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9390 ctx->cf_info.parent_loop.has_divergent_branch = false;
9391
9392 /** emit linear then block */
9393 Block* BB_then_linear = ctx->program->create_and_insert_block();
9394 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9395 BB_then_linear->kind |= block_kind_uniform;
9396 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9397 /* branch from linear then block to invert block */
9398 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 1));
9399 branch->definitions[0] = {ctx->program->allocateId(), s2};
9400 branch->definitions[0].setHint(vcc);
9401 BB_then_linear->instructions.emplace_back(std::move(branch));
9402 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9403
9404 /** emit invert merge block */
9405 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9406 ic->invert_idx = ctx->block->index;
9407
9408 /* branch to linear else block (skip else) */
9409 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 1));
9410 branch->definitions[0] = {ctx->program->allocateId(), s2};
9411 branch->definitions[0].setHint(vcc);
9412 branch->operands[0] = Operand(ic->cond);
9413 ctx->block->instructions.push_back(std::move(branch));
9414
9415 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9416 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9417 ic->exec_potentially_empty_break_depth_old =
9418 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9419 /* divergent branches use cbranch_execz */
9420 ctx->cf_info.exec_potentially_empty_discard = false;
9421 ctx->cf_info.exec_potentially_empty_break = false;
9422 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9423
9424 /** emit logical else block */
9425 Block* BB_else_logical = ctx->program->create_and_insert_block();
9426 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9427 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9428 add_linear_edge(ic->invert_idx, BB_else_logical);
9429 ctx->block = BB_else_logical;
9430 append_logical_start(BB_else_logical);
9431 }
9432
9433 static void end_divergent_if(isel_context *ctx, if_context *ic)
9434 {
9435 Block *BB_else_logical = ctx->block;
9436 append_logical_end(BB_else_logical);
9437
9438 /* branch from logical else block to endif block */
9439 aco_ptr<Pseudo_branch_instruction> branch;
9440 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 1));
9441 branch->definitions[0] = {ctx->program->allocateId(), s2};
9442 branch->definitions[0].setHint(vcc);
9443 BB_else_logical->instructions.emplace_back(std::move(branch));
9444 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9445 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9446 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9447 BB_else_logical->kind |= block_kind_uniform;
9448
9449 assert(!ctx->cf_info.has_branch);
9450 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9451
9452
9453 /** emit linear else block */
9454 Block* BB_else_linear = ctx->program->create_and_insert_block();
9455 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9456 BB_else_linear->kind |= block_kind_uniform;
9457 add_linear_edge(ic->invert_idx, BB_else_linear);
9458
9459 /* branch from linear else block to endif block */
9460 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 1));
9461 branch->definitions[0] = {ctx->program->allocateId(), s2};
9462 branch->definitions[0].setHint(vcc);
9463 BB_else_linear->instructions.emplace_back(std::move(branch));
9464 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9465
9466
9467 /** emit endif merge block */
9468 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9469 append_logical_start(ctx->block);
9470
9471
9472 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9473 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9474 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9475 ctx->cf_info.exec_potentially_empty_break_depth =
9476 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9477 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9478 !ctx->cf_info.parent_if.is_divergent) {
9479 ctx->cf_info.exec_potentially_empty_break = false;
9480 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9481 }
9482 /* uniform control flow never has an empty exec-mask */
9483 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9484 ctx->cf_info.exec_potentially_empty_discard = false;
9485 ctx->cf_info.exec_potentially_empty_break = false;
9486 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9487 }
9488 }
9489
9490 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9491 {
9492 assert(cond.regClass() == s1);
9493
9494 append_logical_end(ctx->block);
9495 ctx->block->kind |= block_kind_uniform;
9496
9497 aco_ptr<Pseudo_branch_instruction> branch;
9498 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9499 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 1));
9500 branch->definitions[0] = {ctx->program->allocateId(), s2};
9501 branch->definitions[0].setHint(vcc);
9502 branch->operands[0] = Operand(cond);
9503 branch->operands[0].setFixed(scc);
9504 ctx->block->instructions.emplace_back(std::move(branch));
9505
9506 ic->BB_if_idx = ctx->block->index;
9507 ic->BB_endif = Block();
9508 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9509 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9510
9511 ctx->cf_info.has_branch = false;
9512 ctx->cf_info.parent_loop.has_divergent_branch = false;
9513
9514 /** emit then block */
9515 Block* BB_then = ctx->program->create_and_insert_block();
9516 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9517 add_edge(ic->BB_if_idx, BB_then);
9518 append_logical_start(BB_then);
9519 ctx->block = BB_then;
9520 }
9521
9522 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9523 {
9524 Block *BB_then = ctx->block;
9525
9526 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9527 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9528
9529 if (!ic->uniform_has_then_branch) {
9530 append_logical_end(BB_then);
9531 /* branch from then block to endif block */
9532 aco_ptr<Pseudo_branch_instruction> branch;
9533 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 1));
9534 branch->definitions[0] = {ctx->program->allocateId(), s2};
9535 branch->definitions[0].setHint(vcc);
9536 BB_then->instructions.emplace_back(std::move(branch));
9537 add_linear_edge(BB_then->index, &ic->BB_endif);
9538 if (!ic->then_branch_divergent)
9539 add_logical_edge(BB_then->index, &ic->BB_endif);
9540 BB_then->kind |= block_kind_uniform;
9541 }
9542
9543 ctx->cf_info.has_branch = false;
9544 ctx->cf_info.parent_loop.has_divergent_branch = false;
9545
9546 /** emit else block */
9547 Block* BB_else = ctx->program->create_and_insert_block();
9548 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9549 add_edge(ic->BB_if_idx, BB_else);
9550 append_logical_start(BB_else);
9551 ctx->block = BB_else;
9552 }
9553
9554 static void end_uniform_if(isel_context *ctx, if_context *ic)
9555 {
9556 Block *BB_else = ctx->block;
9557
9558 if (!ctx->cf_info.has_branch) {
9559 append_logical_end(BB_else);
9560 /* branch from then block to endif block */
9561 aco_ptr<Pseudo_branch_instruction> branch;
9562 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 1));
9563 branch->definitions[0] = {ctx->program->allocateId(), s2};
9564 branch->definitions[0].setHint(vcc);
9565 BB_else->instructions.emplace_back(std::move(branch));
9566 add_linear_edge(BB_else->index, &ic->BB_endif);
9567 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9568 add_logical_edge(BB_else->index, &ic->BB_endif);
9569 BB_else->kind |= block_kind_uniform;
9570 }
9571
9572 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9573 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9574
9575 /** emit endif merge block */
9576 if (!ctx->cf_info.has_branch) {
9577 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9578 append_logical_start(ctx->block);
9579 }
9580 }
9581
9582 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9583 {
9584 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9585 Builder bld(ctx->program, ctx->block);
9586 aco_ptr<Pseudo_branch_instruction> branch;
9587 if_context ic;
9588
9589 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9590 /**
9591 * Uniform conditionals are represented in the following way*) :
9592 *
9593 * The linear and logical CFG:
9594 * BB_IF
9595 * / \
9596 * BB_THEN (logical) BB_ELSE (logical)
9597 * \ /
9598 * BB_ENDIF
9599 *
9600 * *) Exceptions may be due to break and continue statements within loops
9601 * If a break/continue happens within uniform control flow, it branches
9602 * to the loop exit/entry block. Otherwise, it branches to the next
9603 * merge block.
9604 **/
9605
9606 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9607 assert(cond.regClass() == ctx->program->lane_mask);
9608 cond = bool_to_scalar_condition(ctx, cond);
9609
9610 begin_uniform_if_then(ctx, &ic, cond);
9611 visit_cf_list(ctx, &if_stmt->then_list);
9612
9613 begin_uniform_if_else(ctx, &ic);
9614 visit_cf_list(ctx, &if_stmt->else_list);
9615
9616 end_uniform_if(ctx, &ic);
9617 } else { /* non-uniform condition */
9618 /**
9619 * To maintain a logical and linear CFG without critical edges,
9620 * non-uniform conditionals are represented in the following way*) :
9621 *
9622 * The linear CFG:
9623 * BB_IF
9624 * / \
9625 * BB_THEN (logical) BB_THEN (linear)
9626 * \ /
9627 * BB_INVERT (linear)
9628 * / \
9629 * BB_ELSE (logical) BB_ELSE (linear)
9630 * \ /
9631 * BB_ENDIF
9632 *
9633 * The logical CFG:
9634 * BB_IF
9635 * / \
9636 * BB_THEN (logical) BB_ELSE (logical)
9637 * \ /
9638 * BB_ENDIF
9639 *
9640 * *) Exceptions may be due to break and continue statements within loops
9641 **/
9642
9643 begin_divergent_if_then(ctx, &ic, cond);
9644 visit_cf_list(ctx, &if_stmt->then_list);
9645
9646 begin_divergent_if_else(ctx, &ic);
9647 visit_cf_list(ctx, &if_stmt->else_list);
9648
9649 end_divergent_if(ctx, &ic);
9650 }
9651
9652 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9653 }
9654
9655 static bool visit_cf_list(isel_context *ctx,
9656 struct exec_list *list)
9657 {
9658 foreach_list_typed(nir_cf_node, node, node, list) {
9659 switch (node->type) {
9660 case nir_cf_node_block:
9661 visit_block(ctx, nir_cf_node_as_block(node));
9662 break;
9663 case nir_cf_node_if:
9664 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9665 return true;
9666 break;
9667 case nir_cf_node_loop:
9668 visit_loop(ctx, nir_cf_node_as_loop(node));
9669 break;
9670 default:
9671 unreachable("unimplemented cf list type");
9672 }
9673 }
9674 return false;
9675 }
9676
9677 static void create_null_export(isel_context *ctx)
9678 {
9679 /* Some shader stages always need to have exports.
9680 * So when there is none, we need to add a null export.
9681 */
9682
9683 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9684 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9685 Builder bld(ctx->program, ctx->block);
9686 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9687 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9688 }
9689
9690 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9691 {
9692 assert(ctx->stage == vertex_vs ||
9693 ctx->stage == tess_eval_vs ||
9694 ctx->stage == gs_copy_vs ||
9695 ctx->stage == ngg_vertex_gs ||
9696 ctx->stage == ngg_tess_eval_gs);
9697
9698 int offset = (ctx->stage & sw_tes)
9699 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9700 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9701 uint64_t mask = ctx->outputs.mask[slot];
9702 if (!is_pos && !mask)
9703 return false;
9704 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9705 return false;
9706 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9707 exp->enabled_mask = mask;
9708 for (unsigned i = 0; i < 4; ++i) {
9709 if (mask & (1 << i))
9710 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9711 else
9712 exp->operands[i] = Operand(v1);
9713 }
9714 /* GFX10 (Navi1x) skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9715 * Setting valid_mask=1 prevents it and has no other effect.
9716 */
9717 exp->valid_mask = ctx->options->chip_class == GFX10 && is_pos && *next_pos == 0;
9718 exp->done = false;
9719 exp->compressed = false;
9720 if (is_pos)
9721 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9722 else
9723 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9724 ctx->block->instructions.emplace_back(std::move(exp));
9725
9726 return true;
9727 }
9728
9729 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9730 {
9731 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9732 exp->enabled_mask = 0;
9733 for (unsigned i = 0; i < 4; ++i)
9734 exp->operands[i] = Operand(v1);
9735 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9736 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9737 exp->enabled_mask |= 0x1;
9738 }
9739 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9740 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9741 exp->enabled_mask |= 0x4;
9742 }
9743 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9744 if (ctx->options->chip_class < GFX9) {
9745 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9746 exp->enabled_mask |= 0x8;
9747 } else {
9748 Builder bld(ctx->program, ctx->block);
9749
9750 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9751 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9752 if (exp->operands[2].isTemp())
9753 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9754
9755 exp->operands[2] = Operand(out);
9756 exp->enabled_mask |= 0x4;
9757 }
9758 }
9759 exp->valid_mask = ctx->options->chip_class == GFX10 && *next_pos == 0;
9760 exp->done = false;
9761 exp->compressed = false;
9762 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9763 ctx->block->instructions.emplace_back(std::move(exp));
9764 }
9765
9766 static void create_export_phis(isel_context *ctx)
9767 {
9768 /* Used when exports are needed, but the output temps are defined in a preceding block.
9769 * This function will set up phis in order to access the outputs in the next block.
9770 */
9771
9772 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9773 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9774 ctx->block->instructions.pop_back();
9775
9776 Builder bld(ctx->program, ctx->block);
9777
9778 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9779 uint64_t mask = ctx->outputs.mask[slot];
9780 for (unsigned i = 0; i < 4; ++i) {
9781 if (!(mask & (1 << i)))
9782 continue;
9783
9784 Temp old = ctx->outputs.temps[slot * 4 + i];
9785 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9786 ctx->outputs.temps[slot * 4 + i] = phi;
9787 }
9788 }
9789
9790 bld.insert(std::move(logical_start));
9791 }
9792
9793 static void create_vs_exports(isel_context *ctx)
9794 {
9795 assert(ctx->stage == vertex_vs ||
9796 ctx->stage == tess_eval_vs ||
9797 ctx->stage == gs_copy_vs ||
9798 ctx->stage == ngg_vertex_gs ||
9799 ctx->stage == ngg_tess_eval_gs);
9800
9801 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9802 ? &ctx->program->info->tes.outinfo
9803 : &ctx->program->info->vs.outinfo;
9804
9805 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9806 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9807 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9808 }
9809
9810 if (ctx->options->key.has_multiview_view_index) {
9811 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9812 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9813 }
9814
9815 /* the order these position exports are created is important */
9816 int next_pos = 0;
9817 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9818 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9819 export_vs_psiz_layer_viewport(ctx, &next_pos);
9820 exported_pos = true;
9821 }
9822 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9823 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9824 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9825 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9826
9827 if (ctx->export_clip_dists) {
9828 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9829 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9830 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9831 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9832 }
9833
9834 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9835 if (i < VARYING_SLOT_VAR0 &&
9836 i != VARYING_SLOT_LAYER &&
9837 i != VARYING_SLOT_PRIMITIVE_ID &&
9838 i != VARYING_SLOT_VIEWPORT)
9839 continue;
9840
9841 export_vs_varying(ctx, i, false, NULL);
9842 }
9843
9844 if (!exported_pos)
9845 create_null_export(ctx);
9846 }
9847
9848 static bool export_fs_mrt_z(isel_context *ctx)
9849 {
9850 Builder bld(ctx->program, ctx->block);
9851 unsigned enabled_channels = 0;
9852 bool compr = false;
9853 Operand values[4];
9854
9855 for (unsigned i = 0; i < 4; ++i) {
9856 values[i] = Operand(v1);
9857 }
9858
9859 /* Both stencil and sample mask only need 16-bits. */
9860 if (!ctx->program->info->ps.writes_z &&
9861 (ctx->program->info->ps.writes_stencil ||
9862 ctx->program->info->ps.writes_sample_mask)) {
9863 compr = true; /* COMPR flag */
9864
9865 if (ctx->program->info->ps.writes_stencil) {
9866 /* Stencil should be in X[23:16]. */
9867 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9868 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9869 enabled_channels |= 0x3;
9870 }
9871
9872 if (ctx->program->info->ps.writes_sample_mask) {
9873 /* SampleMask should be in Y[15:0]. */
9874 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9875 enabled_channels |= 0xc;
9876 }
9877 } else {
9878 if (ctx->program->info->ps.writes_z) {
9879 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9880 enabled_channels |= 0x1;
9881 }
9882
9883 if (ctx->program->info->ps.writes_stencil) {
9884 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9885 enabled_channels |= 0x2;
9886 }
9887
9888 if (ctx->program->info->ps.writes_sample_mask) {
9889 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9890 enabled_channels |= 0x4;
9891 }
9892 }
9893
9894 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9895 * writemask component.
9896 */
9897 if (ctx->options->chip_class == GFX6 &&
9898 ctx->options->family != CHIP_OLAND &&
9899 ctx->options->family != CHIP_HAINAN) {
9900 enabled_channels |= 0x1;
9901 }
9902
9903 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9904 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9905
9906 return true;
9907 }
9908
9909 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9910 {
9911 Builder bld(ctx->program, ctx->block);
9912 unsigned write_mask = ctx->outputs.mask[slot];
9913 Operand values[4];
9914
9915 for (unsigned i = 0; i < 4; ++i) {
9916 if (write_mask & (1 << i)) {
9917 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9918 } else {
9919 values[i] = Operand(v1);
9920 }
9921 }
9922
9923 unsigned target, col_format;
9924 unsigned enabled_channels = 0;
9925 aco_opcode compr_op = (aco_opcode)0;
9926
9927 slot -= FRAG_RESULT_DATA0;
9928 target = V_008DFC_SQ_EXP_MRT + slot;
9929 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9930
9931 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9932 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9933 bool is_16bit = values[0].regClass() == v2b;
9934
9935 switch (col_format)
9936 {
9937 case V_028714_SPI_SHADER_ZERO:
9938 enabled_channels = 0; /* writemask */
9939 target = V_008DFC_SQ_EXP_NULL;
9940 break;
9941
9942 case V_028714_SPI_SHADER_32_R:
9943 enabled_channels = 1;
9944 break;
9945
9946 case V_028714_SPI_SHADER_32_GR:
9947 enabled_channels = 0x3;
9948 break;
9949
9950 case V_028714_SPI_SHADER_32_AR:
9951 if (ctx->options->chip_class >= GFX10) {
9952 /* Special case: on GFX10, the outputs are different for 32_AR */
9953 enabled_channels = 0x3;
9954 values[1] = values[3];
9955 values[3] = Operand(v1);
9956 } else {
9957 enabled_channels = 0x9;
9958 }
9959 break;
9960
9961 case V_028714_SPI_SHADER_FP16_ABGR:
9962 enabled_channels = 0x5;
9963 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9964 if (is_16bit) {
9965 if (ctx->options->chip_class >= GFX9) {
9966 /* Pack the FP16 values together instead of converting them to
9967 * FP32 and back to FP16.
9968 * TODO: use p_create_vector and let the compiler optimizes.
9969 */
9970 compr_op = aco_opcode::v_pack_b32_f16;
9971 } else {
9972 for (unsigned i = 0; i < 4; i++) {
9973 if ((write_mask >> i) & 1)
9974 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
9975 }
9976 }
9977 }
9978 break;
9979
9980 case V_028714_SPI_SHADER_UNORM16_ABGR:
9981 enabled_channels = 0x5;
9982 if (is_16bit && ctx->options->chip_class >= GFX9) {
9983 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
9984 } else {
9985 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9986 }
9987 break;
9988
9989 case V_028714_SPI_SHADER_SNORM16_ABGR:
9990 enabled_channels = 0x5;
9991 if (is_16bit && ctx->options->chip_class >= GFX9) {
9992 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
9993 } else {
9994 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9995 }
9996 break;
9997
9998 case V_028714_SPI_SHADER_UINT16_ABGR: {
9999 enabled_channels = 0x5;
10000 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10001 if (is_int8 || is_int10) {
10002 /* clamp */
10003 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10004 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10005
10006 for (unsigned i = 0; i < 4; i++) {
10007 if ((write_mask >> i) & 1) {
10008 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10009 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10010 values[i]);
10011 }
10012 }
10013 } else if (is_16bit) {
10014 for (unsigned i = 0; i < 4; i++) {
10015 if ((write_mask >> i) & 1) {
10016 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, false);
10017 values[i] = Operand(tmp);
10018 }
10019 }
10020 }
10021 break;
10022 }
10023
10024 case V_028714_SPI_SHADER_SINT16_ABGR:
10025 enabled_channels = 0x5;
10026 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10027 if (is_int8 || is_int10) {
10028 /* clamp */
10029 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10030 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10031 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10032 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10033
10034 for (unsigned i = 0; i < 4; i++) {
10035 if ((write_mask >> i) & 1) {
10036 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10037 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10038 values[i]);
10039 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10040 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10041 values[i]);
10042 }
10043 }
10044 } else if (is_16bit) {
10045 for (unsigned i = 0; i < 4; i++) {
10046 if ((write_mask >> i) & 1) {
10047 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, true);
10048 values[i] = Operand(tmp);
10049 }
10050 }
10051 }
10052 break;
10053
10054 case V_028714_SPI_SHADER_32_ABGR:
10055 enabled_channels = 0xF;
10056 break;
10057
10058 default:
10059 break;
10060 }
10061
10062 if (target == V_008DFC_SQ_EXP_NULL)
10063 return false;
10064
10065 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10066 if (ctx->options->enable_mrt_output_nan_fixup &&
10067 !is_16bit &&
10068 (col_format == V_028714_SPI_SHADER_32_R ||
10069 col_format == V_028714_SPI_SHADER_32_GR ||
10070 col_format == V_028714_SPI_SHADER_32_AR ||
10071 col_format == V_028714_SPI_SHADER_32_ABGR ||
10072 col_format == V_028714_SPI_SHADER_FP16_ABGR)) {
10073 for (int i = 0; i < 4; i++) {
10074 if (!(write_mask & (1 << i)))
10075 continue;
10076
10077 Temp isnan = bld.vopc(aco_opcode::v_cmp_class_f32,
10078 bld.hint_vcc(bld.def(bld.lm)), values[i],
10079 bld.copy(bld.def(v1), Operand(3u)));
10080 values[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), values[i],
10081 bld.copy(bld.def(v1), Operand(0u)), isnan);
10082 }
10083 }
10084
10085 if ((bool) compr_op) {
10086 for (int i = 0; i < 2; i++) {
10087 /* check if at least one of the values to be compressed is enabled */
10088 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10089 if (enabled) {
10090 enabled_channels |= enabled << (i*2);
10091 values[i] = bld.vop3(compr_op, bld.def(v1),
10092 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10093 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10094 } else {
10095 values[i] = Operand(v1);
10096 }
10097 }
10098 values[2] = Operand(v1);
10099 values[3] = Operand(v1);
10100 } else {
10101 for (int i = 0; i < 4; i++)
10102 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10103 }
10104
10105 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10106 enabled_channels, target, (bool) compr_op);
10107 return true;
10108 }
10109
10110 static void create_fs_exports(isel_context *ctx)
10111 {
10112 bool exported = false;
10113
10114 /* Export depth, stencil and sample mask. */
10115 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10116 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10117 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10118 exported |= export_fs_mrt_z(ctx);
10119
10120 /* Export all color render targets. */
10121 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10122 if (ctx->outputs.mask[i])
10123 exported |= export_fs_mrt_color(ctx, i);
10124
10125 if (!exported)
10126 create_null_export(ctx);
10127 }
10128
10129 static void create_workgroup_barrier(Builder& bld)
10130 {
10131 bld.barrier(aco_opcode::p_barrier,
10132 memory_sync_info(storage_shared, semantic_acqrel, scope_workgroup),
10133 scope_workgroup);
10134 }
10135
10136 static void write_tcs_tess_factors(isel_context *ctx)
10137 {
10138 unsigned outer_comps;
10139 unsigned inner_comps;
10140
10141 switch (ctx->args->options->key.tcs.primitive_mode) {
10142 case GL_ISOLINES:
10143 outer_comps = 2;
10144 inner_comps = 0;
10145 break;
10146 case GL_TRIANGLES:
10147 outer_comps = 3;
10148 inner_comps = 1;
10149 break;
10150 case GL_QUADS:
10151 outer_comps = 4;
10152 inner_comps = 2;
10153 break;
10154 default:
10155 return;
10156 }
10157
10158 Builder bld(ctx->program, ctx->block);
10159
10160 create_workgroup_barrier(bld);
10161
10162 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10163 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10164
10165 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10166 if_context ic_invocation_id_is_zero;
10167 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10168 bld.reset(ctx->block);
10169
10170 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));
10171
10172 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10173 unsigned stride = inner_comps + outer_comps;
10174 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10175 Temp tf_inner_vec;
10176 Temp tf_outer_vec;
10177 Temp out[6];
10178 assert(stride <= (sizeof(out) / sizeof(Temp)));
10179
10180 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10181 // LINES reversal
10182 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10183 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10184 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10185 } else {
10186 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);
10187 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);
10188
10189 for (unsigned i = 0; i < outer_comps; ++i)
10190 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10191 for (unsigned i = 0; i < inner_comps; ++i)
10192 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10193 }
10194
10195 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10196 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10197 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10198 unsigned tf_const_offset = 0;
10199
10200 if (ctx->program->chip_class <= GFX8) {
10201 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);
10202 if_context ic_rel_patch_id_is_zero;
10203 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10204 bld.reset(ctx->block);
10205
10206 /* Store the dynamic HS control word. */
10207 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10208 bld.mubuf(aco_opcode::buffer_store_dword,
10209 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10210 /* immediate OFFSET */ 0, /* OFFEN */ false, /* swizzled */ false, /* idxen*/ false,
10211 /* addr64 */ false, /* disable_wqm */ false, /* glc */ true);
10212 tf_const_offset += 4;
10213
10214 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10215 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10216 bld.reset(ctx->block);
10217 }
10218
10219 assert(stride == 2 || stride == 4 || stride == 6);
10220 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10221 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, memory_sync_info());
10222
10223 /* Store to offchip for TES to read - only if TES reads them */
10224 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10225 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));
10226 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10227
10228 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10229 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, memory_sync_info(storage_vmem_output));
10230
10231 if (likely(inner_comps)) {
10232 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10233 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, memory_sync_info(storage_vmem_output));
10234 }
10235 }
10236
10237 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10238 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10239 }
10240
10241 static void emit_stream_output(isel_context *ctx,
10242 Temp const *so_buffers,
10243 Temp const *so_write_offset,
10244 const struct radv_stream_output *output)
10245 {
10246 unsigned num_comps = util_bitcount(output->component_mask);
10247 unsigned writemask = (1 << num_comps) - 1;
10248 unsigned loc = output->location;
10249 unsigned buf = output->buffer;
10250
10251 assert(num_comps && num_comps <= 4);
10252 if (!num_comps || num_comps > 4)
10253 return;
10254
10255 unsigned start = ffs(output->component_mask) - 1;
10256
10257 Temp out[4];
10258 bool all_undef = true;
10259 assert(ctx->stage & hw_vs);
10260 for (unsigned i = 0; i < num_comps; i++) {
10261 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10262 all_undef = all_undef && !out[i].id();
10263 }
10264 if (all_undef)
10265 return;
10266
10267 while (writemask) {
10268 int start, count;
10269 u_bit_scan_consecutive_range(&writemask, &start, &count);
10270 if (count == 3 && ctx->options->chip_class == GFX6) {
10271 /* GFX6 doesn't support storing vec3, split it. */
10272 writemask |= 1u << (start + 2);
10273 count = 2;
10274 }
10275
10276 unsigned offset = output->offset + start * 4;
10277
10278 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10279 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10280 for (int i = 0; i < count; ++i)
10281 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10282 vec->definitions[0] = Definition(write_data);
10283 ctx->block->instructions.emplace_back(std::move(vec));
10284
10285 aco_opcode opcode;
10286 switch (count) {
10287 case 1:
10288 opcode = aco_opcode::buffer_store_dword;
10289 break;
10290 case 2:
10291 opcode = aco_opcode::buffer_store_dwordx2;
10292 break;
10293 case 3:
10294 opcode = aco_opcode::buffer_store_dwordx3;
10295 break;
10296 case 4:
10297 opcode = aco_opcode::buffer_store_dwordx4;
10298 break;
10299 default:
10300 unreachable("Unsupported dword count.");
10301 }
10302
10303 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10304 store->operands[0] = Operand(so_buffers[buf]);
10305 store->operands[1] = Operand(so_write_offset[buf]);
10306 store->operands[2] = Operand((uint32_t) 0);
10307 store->operands[3] = Operand(write_data);
10308 if (offset > 4095) {
10309 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10310 Builder bld(ctx->program, ctx->block);
10311 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10312 } else {
10313 store->offset = offset;
10314 }
10315 store->offen = true;
10316 store->glc = true;
10317 store->dlc = false;
10318 store->slc = true;
10319 ctx->block->instructions.emplace_back(std::move(store));
10320 }
10321 }
10322
10323 static void emit_streamout(isel_context *ctx, unsigned stream)
10324 {
10325 Builder bld(ctx->program, ctx->block);
10326
10327 Temp so_buffers[4];
10328 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10329 for (unsigned i = 0; i < 4; i++) {
10330 unsigned stride = ctx->program->info->so.strides[i];
10331 if (!stride)
10332 continue;
10333
10334 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10335 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10336 }
10337
10338 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10339 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10340
10341 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10342
10343 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10344
10345 if_context ic;
10346 begin_divergent_if_then(ctx, &ic, can_emit);
10347
10348 bld.reset(ctx->block);
10349
10350 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10351
10352 Temp so_write_offset[4];
10353
10354 for (unsigned i = 0; i < 4; i++) {
10355 unsigned stride = ctx->program->info->so.strides[i];
10356 if (!stride)
10357 continue;
10358
10359 if (stride == 1) {
10360 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10361 get_arg(ctx, ctx->args->streamout_write_idx),
10362 get_arg(ctx, ctx->args->streamout_offset[i]));
10363 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10364
10365 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10366 } else {
10367 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10368 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10369 get_arg(ctx, ctx->args->streamout_offset[i]));
10370 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10371 }
10372 }
10373
10374 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10375 struct radv_stream_output *output =
10376 &ctx->program->info->so.outputs[i];
10377 if (stream != output->stream)
10378 continue;
10379
10380 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10381 }
10382
10383 begin_divergent_if_else(ctx, &ic);
10384 end_divergent_if(ctx, &ic);
10385 }
10386
10387 } /* end namespace */
10388
10389 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10390 {
10391 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10392 Builder bld(ctx->program, ctx->block);
10393 constexpr unsigned hs_idx = 1u;
10394 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10395 get_arg(ctx, ctx->args->merged_wave_info),
10396 Operand((8u << 16) | (hs_idx * 8u)));
10397 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10398
10399 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10400
10401 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10402 get_arg(ctx, ctx->args->rel_auto_id),
10403 get_arg(ctx, ctx->args->ac.instance_id),
10404 ls_has_nonzero_hs_threads);
10405 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10406 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10407 get_arg(ctx, ctx->args->rel_auto_id),
10408 ls_has_nonzero_hs_threads);
10409 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10410 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10411 get_arg(ctx, ctx->args->ac.vertex_id),
10412 ls_has_nonzero_hs_threads);
10413
10414 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10415 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10416 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10417 }
10418
10419 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10420 {
10421 /* Split all arguments except for the first (ring_offsets) and the last
10422 * (exec) so that the dead channels don't stay live throughout the program.
10423 */
10424 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10425 if (startpgm->definitions[i].regClass().size() > 1) {
10426 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10427 startpgm->definitions[i].regClass().size());
10428 }
10429 }
10430 }
10431
10432 void handle_bc_optimize(isel_context *ctx)
10433 {
10434 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10435 Builder bld(ctx->program, ctx->block);
10436 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10437 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10438 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10439 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10440 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10441 if (uses_center && uses_centroid) {
10442 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10443 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10444
10445 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10446 Temp new_coord[2];
10447 for (unsigned i = 0; i < 2; i++) {
10448 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10449 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10450 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10451 persp_centroid, persp_center, sel);
10452 }
10453 ctx->persp_centroid = bld.tmp(v2);
10454 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10455 Operand(new_coord[0]), Operand(new_coord[1]));
10456 emit_split_vector(ctx, ctx->persp_centroid, 2);
10457 }
10458
10459 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10460 Temp new_coord[2];
10461 for (unsigned i = 0; i < 2; i++) {
10462 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10463 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10464 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10465 linear_centroid, linear_center, sel);
10466 }
10467 ctx->linear_centroid = bld.tmp(v2);
10468 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10469 Operand(new_coord[0]), Operand(new_coord[1]));
10470 emit_split_vector(ctx, ctx->linear_centroid, 2);
10471 }
10472 }
10473 }
10474
10475 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10476 {
10477 Program *program = ctx->program;
10478
10479 unsigned float_controls = shader->info.float_controls_execution_mode;
10480
10481 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10482 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10483 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10484 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10485 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10486
10487 program->next_fp_mode.must_flush_denorms32 =
10488 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10489 program->next_fp_mode.must_flush_denorms16_64 =
10490 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10491 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10492
10493 program->next_fp_mode.care_about_round32 =
10494 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10495
10496 program->next_fp_mode.care_about_round16_64 =
10497 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10498 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10499
10500 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10501 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10502 if (program->next_fp_mode.must_flush_denorms16_64)
10503 program->next_fp_mode.denorm16_64 = 0;
10504 else
10505 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10506
10507 /* preserving fp32 denorms is expensive, so only do it if asked */
10508 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10509 program->next_fp_mode.denorm32 = fp_denorm_keep;
10510 else
10511 program->next_fp_mode.denorm32 = 0;
10512
10513 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10514 program->next_fp_mode.round32 = fp_round_tz;
10515 else
10516 program->next_fp_mode.round32 = fp_round_ne;
10517
10518 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10519 program->next_fp_mode.round16_64 = fp_round_tz;
10520 else
10521 program->next_fp_mode.round16_64 = fp_round_ne;
10522
10523 ctx->block->fp_mode = program->next_fp_mode;
10524 }
10525
10526 void cleanup_cfg(Program *program)
10527 {
10528 /* create linear_succs/logical_succs */
10529 for (Block& BB : program->blocks) {
10530 for (unsigned idx : BB.linear_preds)
10531 program->blocks[idx].linear_succs.emplace_back(BB.index);
10532 for (unsigned idx : BB.logical_preds)
10533 program->blocks[idx].logical_succs.emplace_back(BB.index);
10534 }
10535 }
10536
10537 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10538 {
10539 Builder bld(ctx->program, ctx->block);
10540
10541 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10542 Temp count = i == 0
10543 ? get_arg(ctx, ctx->args->merged_wave_info)
10544 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10545 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10546
10547 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10548 Temp cond;
10549
10550 if (ctx->program->wave_size == 64) {
10551 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10552 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10553 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10554 } else {
10555 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10556 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10557 }
10558
10559 return cond;
10560 }
10561
10562 bool ngg_early_prim_export(isel_context *ctx)
10563 {
10564 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10565 return true;
10566 }
10567
10568 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10569 {
10570 Builder bld(ctx->program, ctx->block);
10571
10572 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10573 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10574
10575 /* Get the id of the current wave within the threadgroup (workgroup) */
10576 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10577 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10578
10579 /* Execute the following code only on the first wave (wave id 0),
10580 * use the SCC def to tell if the wave id is zero or not.
10581 */
10582 Temp cond = wave_id_in_tg.def(1).getTemp();
10583 if_context ic;
10584 begin_uniform_if_then(ctx, &ic, cond);
10585 begin_uniform_if_else(ctx, &ic);
10586 bld.reset(ctx->block);
10587
10588 /* Number of vertices output by VS/TES */
10589 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10590 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10591 /* Number of primitives output by VS/TES */
10592 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10593 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10594
10595 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10596 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10597 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10598
10599 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10600 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10601
10602 end_uniform_if(ctx, &ic);
10603
10604 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10605 bld.reset(ctx->block);
10606 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10607 }
10608
10609 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10610 {
10611 Builder bld(ctx->program, ctx->block);
10612
10613 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10614 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10615 }
10616
10617 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10618 Temp tmp;
10619
10620 for (unsigned i = 0; i < num_vertices; ++i) {
10621 assert(vtxindex[i].id());
10622
10623 if (i)
10624 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10625 else
10626 tmp = vtxindex[i];
10627
10628 /* The initial edge flag is always false in tess eval shaders. */
10629 if (ctx->stage == ngg_vertex_gs) {
10630 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10631 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10632 }
10633 }
10634
10635 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10636
10637 return tmp;
10638 }
10639
10640 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10641 {
10642 Builder bld(ctx->program, ctx->block);
10643 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10644
10645 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10646 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10647 false /* compressed */, true/* done */, false /* valid mask */);
10648 }
10649
10650 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10651 {
10652 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10653 * These must always come before VS exports.
10654 *
10655 * It is recommended to do these as early as possible. They can be at the beginning when
10656 * there is no SW GS and the shader doesn't write edge flags.
10657 */
10658
10659 if_context ic;
10660 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10661 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10662
10663 Builder bld(ctx->program, ctx->block);
10664 constexpr unsigned max_vertices_per_primitive = 3;
10665 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10666
10667 if (ctx->stage == ngg_vertex_gs) {
10668 /* TODO: optimize for points & lines */
10669 } else if (ctx->stage == ngg_tess_eval_gs) {
10670 if (ctx->shader->info.tess.point_mode)
10671 num_vertices_per_primitive = 1;
10672 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10673 num_vertices_per_primitive = 2;
10674 } else {
10675 unreachable("Unsupported NGG shader stage");
10676 }
10677
10678 Temp vtxindex[max_vertices_per_primitive];
10679 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10680 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10681 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10682 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10683 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10684 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10685 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10686 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10687
10688 /* Export primitive data to the index buffer. */
10689 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10690
10691 /* Export primitive ID. */
10692 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10693 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10694 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10695 Temp provoking_vtx_index = vtxindex[0];
10696 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10697
10698 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10699 }
10700
10701 begin_divergent_if_else(ctx, &ic);
10702 end_divergent_if(ctx, &ic);
10703 }
10704
10705 void ngg_emit_nogs_output(isel_context *ctx)
10706 {
10707 /* Emits NGG GS output, for stages that don't have SW GS. */
10708
10709 if_context ic;
10710 Builder bld(ctx->program, ctx->block);
10711 bool late_prim_export = !ngg_early_prim_export(ctx);
10712
10713 /* NGG streamout is currently disabled by default. */
10714 assert(!ctx->args->shader_info->so.num_outputs);
10715
10716 if (late_prim_export) {
10717 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10718 create_export_phis(ctx);
10719 /* Do what we need to do in the GS threads. */
10720 ngg_emit_nogs_gsthreads(ctx);
10721
10722 /* What comes next should be executed on ES threads. */
10723 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10724 begin_divergent_if_then(ctx, &ic, is_es_thread);
10725 bld.reset(ctx->block);
10726 }
10727
10728 /* Export VS outputs */
10729 ctx->block->kind |= block_kind_export_end;
10730 create_vs_exports(ctx);
10731
10732 /* Export primitive ID */
10733 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10734 Temp prim_id;
10735
10736 if (ctx->stage == ngg_vertex_gs) {
10737 /* Wait for GS threads to store primitive ID in LDS. */
10738 create_workgroup_barrier(bld);
10739
10740 /* Calculate LDS address where the GS threads stored the primitive ID. */
10741 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10742 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10743 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10744 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10745 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10746 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10747
10748 /* Load primitive ID from LDS. */
10749 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10750 } else if (ctx->stage == ngg_tess_eval_gs) {
10751 /* TES: Just use the patch ID as the primitive ID. */
10752 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10753 } else {
10754 unreachable("unsupported NGG shader stage.");
10755 }
10756
10757 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10758 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10759
10760 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10761 }
10762
10763 if (late_prim_export) {
10764 begin_divergent_if_else(ctx, &ic);
10765 end_divergent_if(ctx, &ic);
10766 bld.reset(ctx->block);
10767 }
10768 }
10769
10770 void select_program(Program *program,
10771 unsigned shader_count,
10772 struct nir_shader *const *shaders,
10773 ac_shader_config* config,
10774 struct radv_shader_args *args)
10775 {
10776 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10777 if_context ic_merged_wave_info;
10778 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10779
10780 for (unsigned i = 0; i < shader_count; i++) {
10781 nir_shader *nir = shaders[i];
10782 init_context(&ctx, nir);
10783
10784 setup_fp_mode(&ctx, nir);
10785
10786 if (!i) {
10787 /* needs to be after init_context() for FS */
10788 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10789 append_logical_start(ctx.block);
10790
10791 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10792 fix_ls_vgpr_init_bug(&ctx, startpgm);
10793
10794 split_arguments(&ctx, startpgm);
10795 }
10796
10797 if (ngg_no_gs) {
10798 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10799
10800 if (ngg_early_prim_export(&ctx))
10801 ngg_emit_nogs_gsthreads(&ctx);
10802 }
10803
10804 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10805 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10806 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10807 ((nir->info.stage == MESA_SHADER_VERTEX &&
10808 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10809 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10810 ctx.stage == tess_eval_geometry_gs));
10811
10812 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10813 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10814 if (check_merged_wave_info) {
10815 Temp cond = merged_wave_info_to_mask(&ctx, i);
10816 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10817 }
10818
10819 if (i) {
10820 Builder bld(ctx.program, ctx.block);
10821
10822 create_workgroup_barrier(bld);
10823
10824 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10825 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));
10826 }
10827 } else if (ctx.stage == geometry_gs)
10828 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10829
10830 if (ctx.stage == fragment_fs)
10831 handle_bc_optimize(&ctx);
10832
10833 visit_cf_list(&ctx, &func->body);
10834
10835 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10836 emit_streamout(&ctx, 0);
10837
10838 if (ctx.stage & hw_vs) {
10839 create_vs_exports(&ctx);
10840 ctx.block->kind |= block_kind_export_end;
10841 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10842 ngg_emit_nogs_output(&ctx);
10843 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10844 Builder bld(ctx.program, ctx.block);
10845 bld.barrier(aco_opcode::p_barrier,
10846 memory_sync_info(storage_vmem_output, semantic_release, scope_device));
10847 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10848 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10849 write_tcs_tess_factors(&ctx);
10850 }
10851
10852 if (ctx.stage == fragment_fs) {
10853 create_fs_exports(&ctx);
10854 ctx.block->kind |= block_kind_export_end;
10855 }
10856
10857 if (endif_merged_wave_info) {
10858 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10859 end_divergent_if(&ctx, &ic_merged_wave_info);
10860 }
10861
10862 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10863 ngg_emit_nogs_output(&ctx);
10864
10865 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10866 /* Outputs of the previous stage are inputs to the next stage */
10867 ctx.inputs = ctx.outputs;
10868 ctx.outputs = shader_io_state();
10869 }
10870 }
10871
10872 program->config->float_mode = program->blocks[0].fp_mode.val;
10873
10874 append_logical_end(ctx.block);
10875 ctx.block->kind |= block_kind_uniform;
10876 Builder bld(ctx.program, ctx.block);
10877 if (ctx.program->wb_smem_l1_on_end)
10878 bld.smem(aco_opcode::s_dcache_wb, memory_sync_info(storage_buffer, semantic_volatile));
10879 bld.sopp(aco_opcode::s_endpgm);
10880
10881 cleanup_cfg(program);
10882 }
10883
10884 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10885 ac_shader_config* config,
10886 struct radv_shader_args *args)
10887 {
10888 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10889
10890 ctx.block->fp_mode = program->next_fp_mode;
10891
10892 add_startpgm(&ctx);
10893 append_logical_start(ctx.block);
10894
10895 Builder bld(ctx.program, ctx.block);
10896
10897 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10898
10899 Operand stream_id(0u);
10900 if (args->shader_info->so.num_outputs)
10901 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10902 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10903
10904 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10905
10906 std::stack<Block> endif_blocks;
10907
10908 for (unsigned stream = 0; stream < 4; stream++) {
10909 if (stream_id.isConstant() && stream != stream_id.constantValue())
10910 continue;
10911
10912 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10913 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10914 continue;
10915
10916 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10917
10918 unsigned BB_if_idx = ctx.block->index;
10919 Block BB_endif = Block();
10920 if (!stream_id.isConstant()) {
10921 /* begin IF */
10922 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10923 append_logical_end(ctx.block);
10924 ctx.block->kind |= block_kind_uniform;
10925 bld.branch(aco_opcode::p_cbranch_z, bld.hint_vcc(bld.def(s2)), cond);
10926
10927 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10928
10929 ctx.block = ctx.program->create_and_insert_block();
10930 add_edge(BB_if_idx, ctx.block);
10931 bld.reset(ctx.block);
10932 append_logical_start(ctx.block);
10933 }
10934
10935 unsigned offset = 0;
10936 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10937 if (args->shader_info->gs.output_streams[i] != stream)
10938 continue;
10939
10940 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10941 unsigned length = util_last_bit(output_usage_mask);
10942 for (unsigned j = 0; j < length; ++j) {
10943 if (!(output_usage_mask & (1 << j)))
10944 continue;
10945
10946 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10947 Temp voffset = vtx_offset;
10948 if (const_offset >= 4096u) {
10949 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10950 const_offset %= 4096u;
10951 }
10952
10953 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10954 mubuf->definitions[0] = bld.def(v1);
10955 mubuf->operands[0] = Operand(gsvs_ring);
10956 mubuf->operands[1] = Operand(voffset);
10957 mubuf->operands[2] = Operand(0u);
10958 mubuf->offen = true;
10959 mubuf->offset = const_offset;
10960 mubuf->glc = true;
10961 mubuf->slc = true;
10962 mubuf->dlc = args->options->chip_class >= GFX10;
10963
10964 ctx.outputs.mask[i] |= 1 << j;
10965 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10966
10967 bld.insert(std::move(mubuf));
10968
10969 offset++;
10970 }
10971 }
10972
10973 if (args->shader_info->so.num_outputs) {
10974 emit_streamout(&ctx, stream);
10975 bld.reset(ctx.block);
10976 }
10977
10978 if (stream == 0) {
10979 create_vs_exports(&ctx);
10980 ctx.block->kind |= block_kind_export_end;
10981 }
10982
10983 if (!stream_id.isConstant()) {
10984 append_logical_end(ctx.block);
10985
10986 /* branch from then block to endif block */
10987 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
10988 add_edge(ctx.block->index, &BB_endif);
10989 ctx.block->kind |= block_kind_uniform;
10990
10991 /* emit else block */
10992 ctx.block = ctx.program->create_and_insert_block();
10993 add_edge(BB_if_idx, ctx.block);
10994 bld.reset(ctx.block);
10995 append_logical_start(ctx.block);
10996
10997 endif_blocks.push(std::move(BB_endif));
10998 }
10999 }
11000
11001 while (!endif_blocks.empty()) {
11002 Block BB_endif = std::move(endif_blocks.top());
11003 endif_blocks.pop();
11004
11005 Block *BB_else = ctx.block;
11006
11007 append_logical_end(BB_else);
11008 /* branch from else block to endif block */
11009 bld.branch(aco_opcode::p_branch, bld.hint_vcc(bld.def(s2)));
11010 add_edge(BB_else->index, &BB_endif);
11011 BB_else->kind |= block_kind_uniform;
11012
11013 /** emit endif merge block */
11014 ctx.block = program->insert_block(std::move(BB_endif));
11015 bld.reset(ctx.block);
11016 append_logical_start(ctx.block);
11017 }
11018
11019 program->config->float_mode = program->blocks[0].fp_mode.val;
11020
11021 append_logical_end(ctx.block);
11022 ctx.block->kind |= block_kind_uniform;
11023 bld.sopp(aco_opcode::s_endpgm);
11024
11025 cleanup_cfg(program);
11026 }
11027
11028 void select_trap_handler_shader(Program *program, struct nir_shader *shader,
11029 ac_shader_config* config,
11030 struct radv_shader_args *args)
11031 {
11032 assert(args->options->chip_class == GFX8);
11033
11034 init_program(program, compute_cs, args->shader_info,
11035 args->options->chip_class, args->options->family, config);
11036
11037 isel_context ctx = {};
11038 ctx.program = program;
11039 ctx.args = args;
11040 ctx.options = args->options;
11041 ctx.stage = program->stage;
11042
11043 ctx.block = ctx.program->create_and_insert_block();
11044 ctx.block->loop_nest_depth = 0;
11045 ctx.block->kind = block_kind_top_level;
11046
11047 program->workgroup_size = 1; /* XXX */
11048
11049 add_startpgm(&ctx);
11050 append_logical_start(ctx.block);
11051
11052 Builder bld(ctx.program, ctx.block);
11053
11054 /* Load the buffer descriptor from TMA. */
11055 bld.smem(aco_opcode::s_load_dwordx4, Definition(PhysReg{ttmp4}, s4),
11056 Operand(PhysReg{tma}, s2), Operand(0u));
11057
11058 /* Store TTMP0-TTMP1. */
11059 bld.smem(aco_opcode::s_buffer_store_dwordx2, Operand(PhysReg{ttmp4}, s4),
11060 Operand(0u), Operand(PhysReg{ttmp0}, s2), memory_sync_info(), true);
11061
11062 uint32_t hw_regs_idx[] = {
11063 2, /* HW_REG_STATUS */
11064 3, /* HW_REG_TRAP_STS */
11065 4, /* HW_REG_HW_ID */
11066 7, /* HW_REG_IB_STS */
11067 };
11068
11069 /* Store some hardware registers. */
11070 for (unsigned i = 0; i < ARRAY_SIZE(hw_regs_idx); i++) {
11071 /* "((size - 1) << 11) | register" */
11072 bld.sopk(aco_opcode::s_getreg_b32, Definition(PhysReg{ttmp8}, s1),
11073 ((20 - 1) << 11) | hw_regs_idx[i]);
11074
11075 bld.smem(aco_opcode::s_buffer_store_dword, Operand(PhysReg{ttmp4}, s4),
11076 Operand(8u + i * 4), Operand(PhysReg{ttmp8}, s1), memory_sync_info(), true);
11077 }
11078
11079 program->config->float_mode = program->blocks[0].fp_mode.val;
11080
11081 append_logical_end(ctx.block);
11082 ctx.block->kind |= block_kind_uniform;
11083 bld.sopp(aco_opcode::s_endpgm);
11084
11085 cleanup_cfg(program);
11086 }
11087 }