aco: refactor store_lds() to use new helpers
[mesa.git] / src / amd / compiler / aco_instruction_selection.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <algorithm>
27 #include <array>
28 #include <stack>
29 #include <map>
30
31 #include "ac_shader_util.h"
32 #include "aco_ir.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
37
38 namespace aco {
39 namespace {
40
41 class loop_info_RAII {
42 isel_context* ctx;
43 unsigned header_idx_old;
44 Block* exit_old;
45 bool divergent_cont_old;
46 bool divergent_branch_old;
47 bool divergent_if_old;
48
49 public:
50 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
51 : ctx(ctx),
52 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
53 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
54 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
55 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
56 {
57 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
58 ctx->cf_info.parent_loop.exit = loop_exit;
59 ctx->cf_info.parent_loop.has_divergent_continue = false;
60 ctx->cf_info.parent_loop.has_divergent_branch = false;
61 ctx->cf_info.parent_if.is_divergent = false;
62 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
63 }
64
65 ~loop_info_RAII()
66 {
67 ctx->cf_info.parent_loop.header_idx = header_idx_old;
68 ctx->cf_info.parent_loop.exit = exit_old;
69 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
70 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
71 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
72 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
73 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
74 ctx->cf_info.exec_potentially_empty_discard = false;
75 }
76 };
77
78 struct if_context {
79 Temp cond;
80
81 bool divergent_old;
82 bool exec_potentially_empty_discard_old;
83 bool exec_potentially_empty_break_old;
84 uint16_t exec_potentially_empty_break_depth_old;
85
86 unsigned BB_if_idx;
87 unsigned invert_idx;
88 bool uniform_has_then_branch;
89 bool then_branch_divergent;
90 Block BB_invert;
91 Block BB_endif;
92 };
93
94 static bool visit_cf_list(struct isel_context *ctx,
95 struct exec_list *list);
96
97 static void add_logical_edge(unsigned pred_idx, Block *succ)
98 {
99 succ->logical_preds.emplace_back(pred_idx);
100 }
101
102
103 static void add_linear_edge(unsigned pred_idx, Block *succ)
104 {
105 succ->linear_preds.emplace_back(pred_idx);
106 }
107
108 static void add_edge(unsigned pred_idx, Block *succ)
109 {
110 add_logical_edge(pred_idx, succ);
111 add_linear_edge(pred_idx, succ);
112 }
113
114 static void append_logical_start(Block *b)
115 {
116 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
117 }
118
119 static void append_logical_end(Block *b)
120 {
121 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
122 }
123
124 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
125 {
126 assert(ctx->allocated[def->index].id());
127 return ctx->allocated[def->index];
128 }
129
130 Temp emit_mbcnt(isel_context *ctx, Definition dst,
131 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
132 {
133 Builder bld(ctx->program, ctx->block);
134 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
135 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
136
137 if (ctx->program->wave_size == 32) {
138 return thread_id_lo;
139 } else {
140 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 }
143 }
144
145 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
146 {
147 Builder bld(ctx->program, ctx->block);
148
149 if (!dst.id())
150 dst = bld.tmp(src.regClass());
151
152 assert(src.size() == dst.size());
153
154 if (ctx->stage != fragment_fs) {
155 if (!dst.id())
156 return src;
157
158 bld.copy(Definition(dst), src);
159 return dst;
160 }
161
162 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
163 ctx->program->needs_wqm |= program_needs_wqm;
164 return dst;
165 }
166
167 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
168 {
169 if (index.regClass() == s1)
170 return bld.readlane(bld.def(s1), data, index);
171
172 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
173
174 /* Currently not implemented on GFX6-7 */
175 assert(ctx->options->chip_class >= GFX8);
176
177 if (ctx->options->chip_class <= GFX9 || ctx->program->wave_size == 32) {
178 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
179 }
180
181 /* GFX10, wave64 mode:
182 * The bpermute instruction is limited to half-wave operation, which means that it can't
183 * properly support subgroup shuffle like older generations (or wave32 mode), so we
184 * emulate it here.
185 */
186 if (!ctx->has_gfx10_wave64_bpermute) {
187 ctx->has_gfx10_wave64_bpermute = true;
188 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
189 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
190 }
191
192 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
193 Temp lane_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), lane_id);
194 Temp index_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), index);
195 Temp cmp = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm, vcc), lane_is_hi, index_is_hi);
196
197 return bld.reduction(aco_opcode::p_wave64_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc),
198 bld.vcc(cmp), Operand(v2.as_linear()), index_x4, data, gfx10_wave64_bpermute);
199 }
200
201 Temp as_vgpr(isel_context *ctx, Temp val)
202 {
203 if (val.type() == RegType::sgpr) {
204 Builder bld(ctx->program, ctx->block);
205 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
206 }
207 assert(val.type() == RegType::vgpr);
208 return val;
209 }
210
211 //assumes a != 0xffffffff
212 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
213 {
214 assert(b != 0);
215 Builder bld(ctx->program, ctx->block);
216
217 if (util_is_power_of_two_or_zero(b)) {
218 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
219 return;
220 }
221
222 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
223
224 assert(info.multiplier <= 0xffffffff);
225
226 bool pre_shift = info.pre_shift != 0;
227 bool increment = info.increment != 0;
228 bool multiply = true;
229 bool post_shift = info.post_shift != 0;
230
231 if (!pre_shift && !increment && !multiply && !post_shift) {
232 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
233 return;
234 }
235
236 Temp pre_shift_dst = a;
237 if (pre_shift) {
238 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
239 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
240 }
241
242 Temp increment_dst = pre_shift_dst;
243 if (increment) {
244 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
245 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
246 }
247
248 Temp multiply_dst = increment_dst;
249 if (multiply) {
250 multiply_dst = post_shift ? bld.tmp(v1) : dst;
251 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
252 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
253 }
254
255 if (post_shift) {
256 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
257 }
258 }
259
260 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
261 {
262 Builder bld(ctx->program, ctx->block);
263 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
264 }
265
266
267 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
268 {
269 /* no need to extract the whole vector */
270 if (src.regClass() == dst_rc) {
271 assert(idx == 0);
272 return src;
273 }
274
275 assert(src.bytes() > (idx * dst_rc.bytes()));
276 Builder bld(ctx->program, ctx->block);
277 auto it = ctx->allocated_vec.find(src.id());
278 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
279 if (it->second[idx].regClass() == dst_rc) {
280 return it->second[idx];
281 } else {
282 assert(!dst_rc.is_subdword());
283 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
284 return bld.copy(bld.def(dst_rc), it->second[idx]);
285 }
286 }
287
288 if (dst_rc.is_subdword())
289 src = as_vgpr(ctx, src);
290
291 if (src.bytes() == dst_rc.bytes()) {
292 assert(idx == 0);
293 return bld.copy(bld.def(dst_rc), src);
294 } else {
295 Temp dst = bld.tmp(dst_rc);
296 emit_extract_vector(ctx, src, idx, dst);
297 return dst;
298 }
299 }
300
301 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
302 {
303 if (num_components == 1)
304 return;
305 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
306 return;
307 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
308 split->operands[0] = Operand(vec_src);
309 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
310 RegClass rc;
311 if (num_components > vec_src.size()) {
312 if (vec_src.type() == RegType::sgpr)
313 return;
314
315 /* sub-dword split */
316 assert(vec_src.type() == RegType::vgpr);
317 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
318 } else {
319 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
320 }
321 for (unsigned i = 0; i < num_components; i++) {
322 elems[i] = {ctx->program->allocateId(), rc};
323 split->definitions[i] = Definition(elems[i]);
324 }
325 ctx->block->instructions.emplace_back(std::move(split));
326 ctx->allocated_vec.emplace(vec_src.id(), elems);
327 }
328
329 /* This vector expansion uses a mask to determine which elements in the new vector
330 * come from the original vector. The other elements are undefined. */
331 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
332 {
333 emit_split_vector(ctx, vec_src, util_bitcount(mask));
334
335 if (vec_src == dst)
336 return;
337
338 Builder bld(ctx->program, ctx->block);
339 if (num_components == 1) {
340 if (dst.type() == RegType::sgpr)
341 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
342 else
343 bld.copy(Definition(dst), vec_src);
344 return;
345 }
346
347 unsigned component_size = dst.size() / num_components;
348 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
349
350 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
351 vec->definitions[0] = Definition(dst);
352 unsigned k = 0;
353 for (unsigned i = 0; i < num_components; i++) {
354 if (mask & (1 << i)) {
355 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
356 if (dst.type() == RegType::sgpr)
357 src = bld.as_uniform(src);
358 vec->operands[i] = Operand(src);
359 } else {
360 vec->operands[i] = Operand(0u);
361 }
362 elems[i] = vec->operands[i].getTemp();
363 }
364 ctx->block->instructions.emplace_back(std::move(vec));
365 ctx->allocated_vec.emplace(dst.id(), elems);
366 }
367
368 /* adjust misaligned small bit size loads */
369 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
370 {
371 Builder bld(ctx->program, ctx->block);
372 Operand shift;
373 Temp select = Temp();
374 if (offset.isConstant()) {
375 assert(offset.constantValue() && offset.constantValue() < 4);
376 shift = Operand(offset.constantValue() * 8);
377 } else {
378 /* bit_offset = 8 * (offset & 0x3) */
379 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
380 select = bld.tmp(s1);
381 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
382 }
383
384 if (vec.size() == 1) {
385 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
386 } else if (vec.size() == 2) {
387 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
388 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
389 if (tmp == dst)
390 emit_split_vector(ctx, dst, 2);
391 else
392 emit_extract_vector(ctx, tmp, 0, dst);
393 } else if (vec.size() == 4) {
394 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
395 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
396 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
397 if (select != Temp())
398 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), select);
399 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
400 Temp mid = bld.tmp(s1);
401 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
402 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
403 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
404 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
405 emit_split_vector(ctx, dst, 2);
406 }
407 }
408
409 /* this function trims subdword vectors:
410 * if dst is vgpr - split the src and create a shrunk version according to the mask.
411 * if dst is sgpr - split the src, but move the original to sgpr. */
412 void trim_subdword_vector(isel_context *ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
413 {
414 assert(vec_src.type() == RegType::vgpr);
415 emit_split_vector(ctx, vec_src, num_components);
416
417 Builder bld(ctx->program, ctx->block);
418 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
419 unsigned component_size = vec_src.bytes() / num_components;
420 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
421
422 unsigned k = 0;
423 for (unsigned i = 0; i < num_components; i++) {
424 if (mask & (1 << i))
425 elems[k++] = emit_extract_vector(ctx, vec_src, i, rc);
426 }
427
428 if (dst.type() == RegType::vgpr) {
429 assert(dst.bytes() == k * component_size);
430 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, k, 1)};
431 for (unsigned i = 0; i < k; i++)
432 vec->operands[i] = Operand(elems[i]);
433 vec->definitions[0] = Definition(dst);
434 bld.insert(std::move(vec));
435 } else {
436 // TODO: alignbyte if mask doesn't start with 1?
437 assert(mask & 1);
438 assert(dst.size() == vec_src.size());
439 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
440 }
441 ctx->allocated_vec.emplace(dst.id(), elems);
442 }
443
444 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
445 {
446 Builder bld(ctx->program, ctx->block);
447 if (!dst.id())
448 dst = bld.tmp(bld.lm);
449
450 assert(val.regClass() == s1);
451 assert(dst.regClass() == bld.lm);
452
453 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
454 }
455
456 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
457 {
458 Builder bld(ctx->program, ctx->block);
459 if (!dst.id())
460 dst = bld.tmp(s1);
461
462 assert(val.regClass() == bld.lm);
463 assert(dst.regClass() == s1);
464
465 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
466 Temp tmp = bld.tmp(s1);
467 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
468 return emit_wqm(ctx, tmp, dst);
469 }
470
471 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
472 {
473 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
474 return get_ssa_temp(ctx, src.src.ssa);
475
476 if (src.src.ssa->num_components == size) {
477 bool identity_swizzle = true;
478 for (unsigned i = 0; identity_swizzle && i < size; i++) {
479 if (src.swizzle[i] != i)
480 identity_swizzle = false;
481 }
482 if (identity_swizzle)
483 return get_ssa_temp(ctx, src.src.ssa);
484 }
485
486 Temp vec = get_ssa_temp(ctx, src.src.ssa);
487 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
488 assert(elem_size > 0);
489 assert(vec.bytes() % elem_size == 0);
490
491 if (elem_size < 4 && vec.type() == RegType::sgpr) {
492 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
493 assert(size == 1);
494 unsigned swizzle = src.swizzle[0];
495 if (vec.size() > 1) {
496 assert(src.src.ssa->bit_size == 16);
497 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
498 swizzle = swizzle & 1;
499 }
500 if (swizzle == 0)
501 return vec;
502
503 Temp dst{ctx->program->allocateId(), s1};
504 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 1)};
505 bfe->operands[0] = Operand(vec);
506 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
507 bfe->definitions[0] = Definition(dst);
508 ctx->block->instructions.emplace_back(std::move(bfe));
509 return dst;
510 }
511
512 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
513 if (size == 1) {
514 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
515 } else {
516 assert(size <= 4);
517 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
518 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
519 for (unsigned i = 0; i < size; ++i) {
520 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
521 vec_instr->operands[i] = Operand{elems[i]};
522 }
523 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
524 vec_instr->definitions[0] = Definition(dst);
525 ctx->block->instructions.emplace_back(std::move(vec_instr));
526 ctx->allocated_vec.emplace(dst.id(), elems);
527 return dst;
528 }
529 }
530
531 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
532 {
533 if (ptr.size() == 2)
534 return ptr;
535 Builder bld(ctx->program, ctx->block);
536 if (ptr.type() == RegType::vgpr)
537 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
538 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
539 ptr, Operand((unsigned)ctx->options->address32_hi));
540 }
541
542 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
543 {
544 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
545 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
546 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
547 sop2->definitions[0] = Definition(dst);
548 if (writes_scc)
549 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
550 ctx->block->instructions.emplace_back(std::move(sop2));
551 }
552
553 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
554 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
555 {
556 Builder bld(ctx->program, ctx->block);
557 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
558 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
559 if (src1.type() == RegType::sgpr) {
560 if (commutative && src0.type() == RegType::vgpr) {
561 Temp t = src0;
562 src0 = src1;
563 src1 = t;
564 } else {
565 src1 = as_vgpr(ctx, src1);
566 }
567 }
568
569 if (flush_denorms && ctx->program->chip_class < GFX9) {
570 assert(dst.size() == 1);
571 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
572 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
573 } else {
574 bld.vop2(op, Definition(dst), src0, src1);
575 }
576 }
577
578 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
579 bool flush_denorms = false)
580 {
581 Temp src0 = get_alu_src(ctx, instr->src[0]);
582 Temp src1 = get_alu_src(ctx, instr->src[1]);
583 Temp src2 = get_alu_src(ctx, instr->src[2]);
584
585 /* ensure that the instruction has at most 1 sgpr operand
586 * The optimizer will inline constants for us */
587 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
588 src0 = as_vgpr(ctx, src0);
589 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
590 src1 = as_vgpr(ctx, src1);
591 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
592 src2 = as_vgpr(ctx, src2);
593
594 Builder bld(ctx->program, ctx->block);
595 if (flush_denorms && ctx->program->chip_class < GFX9) {
596 assert(dst.size() == 1);
597 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
598 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
599 } else {
600 bld.vop3(op, Definition(dst), src0, src1, src2);
601 }
602 }
603
604 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
605 {
606 Builder bld(ctx->program, ctx->block);
607 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
608 }
609
610 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
611 {
612 Temp src0 = get_alu_src(ctx, instr->src[0]);
613 Temp src1 = get_alu_src(ctx, instr->src[1]);
614 assert(src0.size() == src1.size());
615
616 aco_ptr<Instruction> vopc;
617 if (src1.type() == RegType::sgpr) {
618 if (src0.type() == RegType::vgpr) {
619 /* to swap the operands, we might also have to change the opcode */
620 switch (op) {
621 case aco_opcode::v_cmp_lt_f16:
622 op = aco_opcode::v_cmp_gt_f16;
623 break;
624 case aco_opcode::v_cmp_ge_f16:
625 op = aco_opcode::v_cmp_le_f16;
626 break;
627 case aco_opcode::v_cmp_lt_i16:
628 op = aco_opcode::v_cmp_gt_i16;
629 break;
630 case aco_opcode::v_cmp_ge_i16:
631 op = aco_opcode::v_cmp_le_i16;
632 break;
633 case aco_opcode::v_cmp_lt_u16:
634 op = aco_opcode::v_cmp_gt_u16;
635 break;
636 case aco_opcode::v_cmp_ge_u16:
637 op = aco_opcode::v_cmp_le_u16;
638 break;
639 case aco_opcode::v_cmp_lt_f32:
640 op = aco_opcode::v_cmp_gt_f32;
641 break;
642 case aco_opcode::v_cmp_ge_f32:
643 op = aco_opcode::v_cmp_le_f32;
644 break;
645 case aco_opcode::v_cmp_lt_i32:
646 op = aco_opcode::v_cmp_gt_i32;
647 break;
648 case aco_opcode::v_cmp_ge_i32:
649 op = aco_opcode::v_cmp_le_i32;
650 break;
651 case aco_opcode::v_cmp_lt_u32:
652 op = aco_opcode::v_cmp_gt_u32;
653 break;
654 case aco_opcode::v_cmp_ge_u32:
655 op = aco_opcode::v_cmp_le_u32;
656 break;
657 case aco_opcode::v_cmp_lt_f64:
658 op = aco_opcode::v_cmp_gt_f64;
659 break;
660 case aco_opcode::v_cmp_ge_f64:
661 op = aco_opcode::v_cmp_le_f64;
662 break;
663 case aco_opcode::v_cmp_lt_i64:
664 op = aco_opcode::v_cmp_gt_i64;
665 break;
666 case aco_opcode::v_cmp_ge_i64:
667 op = aco_opcode::v_cmp_le_i64;
668 break;
669 case aco_opcode::v_cmp_lt_u64:
670 op = aco_opcode::v_cmp_gt_u64;
671 break;
672 case aco_opcode::v_cmp_ge_u64:
673 op = aco_opcode::v_cmp_le_u64;
674 break;
675 default: /* eq and ne are commutative */
676 break;
677 }
678 Temp t = src0;
679 src0 = src1;
680 src1 = t;
681 } else {
682 src1 = as_vgpr(ctx, src1);
683 }
684 }
685
686 Builder bld(ctx->program, ctx->block);
687 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
688 }
689
690 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
691 {
692 Temp src0 = get_alu_src(ctx, instr->src[0]);
693 Temp src1 = get_alu_src(ctx, instr->src[1]);
694 Builder bld(ctx->program, ctx->block);
695
696 assert(dst.regClass() == bld.lm);
697 assert(src0.type() == RegType::sgpr);
698 assert(src1.type() == RegType::sgpr);
699 assert(src0.regClass() == src1.regClass());
700
701 /* Emit the SALU comparison instruction */
702 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
703 /* Turn the result into a per-lane bool */
704 bool_to_vector_condition(ctx, cmp, dst);
705 }
706
707 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
708 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)
709 {
710 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;
711 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;
712 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
713 bool use_valu = s_op == aco_opcode::num_opcodes ||
714 divergent_vals ||
715 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
716 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
717 aco_opcode op = use_valu ? v_op : s_op;
718 assert(op != aco_opcode::num_opcodes);
719 assert(dst.regClass() == ctx->program->lane_mask);
720
721 if (use_valu)
722 emit_vopc_instruction(ctx, instr, op, dst);
723 else
724 emit_sopc_instruction(ctx, instr, op, dst);
725 }
726
727 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
728 {
729 Builder bld(ctx->program, ctx->block);
730 Temp src0 = get_alu_src(ctx, instr->src[0]);
731 Temp src1 = get_alu_src(ctx, instr->src[1]);
732
733 assert(dst.regClass() == bld.lm);
734 assert(src0.regClass() == bld.lm);
735 assert(src1.regClass() == bld.lm);
736
737 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
738 }
739
740 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
741 {
742 Builder bld(ctx->program, ctx->block);
743 Temp cond = get_alu_src(ctx, instr->src[0]);
744 Temp then = get_alu_src(ctx, instr->src[1]);
745 Temp els = get_alu_src(ctx, instr->src[2]);
746
747 assert(cond.regClass() == bld.lm);
748
749 if (dst.type() == RegType::vgpr) {
750 aco_ptr<Instruction> bcsel;
751 if (dst.regClass() == v2b) {
752 then = as_vgpr(ctx, then);
753 els = as_vgpr(ctx, els);
754
755 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), els, then, cond);
756 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
757 } else if (dst.regClass() == v1) {
758 then = as_vgpr(ctx, then);
759 els = as_vgpr(ctx, els);
760
761 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
762 } else if (dst.regClass() == v2) {
763 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
764 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
765 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
766 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
767
768 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
769 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
770
771 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
772 } else {
773 fprintf(stderr, "Unimplemented NIR instr bit size: ");
774 nir_print_instr(&instr->instr, stderr);
775 fprintf(stderr, "\n");
776 }
777 return;
778 }
779
780 if (instr->dest.dest.ssa.bit_size == 1) {
781 assert(dst.regClass() == bld.lm);
782 assert(then.regClass() == bld.lm);
783 assert(els.regClass() == bld.lm);
784 }
785
786 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
787 if (dst.regClass() == s1 || dst.regClass() == s2) {
788 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
789 assert(dst.size() == then.size());
790 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
791 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
792 } else {
793 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
794 nir_print_instr(&instr->instr, stderr);
795 fprintf(stderr, "\n");
796 }
797 return;
798 }
799
800 /* divergent boolean bcsel
801 * this implements bcsel on bools: dst = s0 ? s1 : s2
802 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
803 assert(instr->dest.dest.ssa.bit_size == 1);
804
805 if (cond.id() != then.id())
806 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
807
808 if (cond.id() == els.id())
809 bld.sop1(Builder::s_mov, Definition(dst), then);
810 else
811 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
812 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
813 }
814
815 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
816 aco_opcode op, uint32_t undo)
817 {
818 /* multiply by 16777216 to handle denormals */
819 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
820 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
821 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
822 scaled = bld.vop1(op, bld.def(v1), scaled);
823 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
824
825 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
826
827 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
828 }
829
830 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
831 {
832 if (ctx->block->fp_mode.denorm32 == 0) {
833 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
834 return;
835 }
836
837 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
838 }
839
840 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
841 {
842 if (ctx->block->fp_mode.denorm32 == 0) {
843 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
844 return;
845 }
846
847 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
848 }
849
850 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
851 {
852 if (ctx->block->fp_mode.denorm32 == 0) {
853 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
854 return;
855 }
856
857 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
858 }
859
860 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
861 {
862 if (ctx->block->fp_mode.denorm32 == 0) {
863 bld.vop1(aco_opcode::v_log_f32, dst, val);
864 return;
865 }
866
867 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
868 }
869
870 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
871 {
872 if (ctx->options->chip_class >= GFX7)
873 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
874
875 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
876 /* TODO: create more efficient code! */
877 if (val.type() == RegType::sgpr)
878 val = as_vgpr(ctx, val);
879
880 /* Split the input value. */
881 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
882 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
883
884 /* Extract the exponent and compute the unbiased value. */
885 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
886
887 /* Extract the fractional part. */
888 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
889 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
890
891 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
892 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
893
894 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
895 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
896 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
897 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
898 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
899
900 /* Get the sign bit. */
901 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
902
903 /* Decide the operation to apply depending on the unbiased exponent. */
904 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
905 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
906 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
907 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
908 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
909 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
910
911 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
912 }
913
914 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
915 {
916 if (ctx->options->chip_class >= GFX7)
917 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
918
919 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
920 Temp src0 = as_vgpr(ctx, val);
921
922 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
923 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
924
925 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
926 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
927 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
928
929 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
930 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
931 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
932 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
933
934 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
935 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
936
937 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
938
939 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
940 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
941
942 return add->definitions[0].getTemp();
943 }
944
945 Temp convert_int(Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
946 if (!dst.id()) {
947 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
948 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
949 else
950 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
951 }
952
953 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
954 return bld.copy(Definition(dst), src);
955 else if (dst.bytes() < src.bytes())
956 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
957
958 Temp tmp = dst;
959 if (dst_bits == 64)
960 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
961
962 if (tmp == src) {
963 } else if (src.regClass() == s1) {
964 if (is_signed)
965 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
966 else
967 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
968 } else {
969 assert(src_bits != 8 || src.regClass() == v1b);
970 assert(src_bits != 16 || src.regClass() == v2b);
971 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
972 sdwa->operands[0] = Operand(src);
973 sdwa->definitions[0] = Definition(tmp);
974 if (is_signed)
975 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
976 else
977 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
978 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
979 bld.insert(std::move(sdwa));
980 }
981
982 if (dst_bits == 64) {
983 if (is_signed && dst.regClass() == s2) {
984 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
985 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
986 } else if (is_signed && dst.regClass() == v2) {
987 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
988 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
989 } else {
990 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
991 }
992 }
993
994 return dst;
995 }
996
997 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
998 {
999 if (!instr->dest.dest.is_ssa) {
1000 fprintf(stderr, "nir alu dst not in ssa: ");
1001 nir_print_instr(&instr->instr, stderr);
1002 fprintf(stderr, "\n");
1003 abort();
1004 }
1005 Builder bld(ctx->program, ctx->block);
1006 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1007 switch(instr->op) {
1008 case nir_op_vec2:
1009 case nir_op_vec3:
1010 case nir_op_vec4: {
1011 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1012 unsigned num = instr->dest.dest.ssa.num_components;
1013 for (unsigned i = 0; i < num; ++i)
1014 elems[i] = get_alu_src(ctx, instr->src[i]);
1015
1016 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1017 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1018 for (unsigned i = 0; i < num; ++i)
1019 vec->operands[i] = Operand{elems[i]};
1020 vec->definitions[0] = Definition(dst);
1021 ctx->block->instructions.emplace_back(std::move(vec));
1022 ctx->allocated_vec.emplace(dst.id(), elems);
1023 } else {
1024 // TODO: that is a bit suboptimal..
1025 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1026 for (unsigned i = 0; i < num - 1; ++i)
1027 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1028 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1029 for (unsigned i = 0; i < num; ++i) {
1030 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1031 if (bit % 32 == 0) {
1032 elems[bit / 32] = elems[i];
1033 } else {
1034 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1035 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1036 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1037 }
1038 }
1039 if (dst.size() == 1)
1040 bld.copy(Definition(dst), elems[0]);
1041 else
1042 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1043 }
1044 break;
1045 }
1046 case nir_op_mov: {
1047 Temp src = get_alu_src(ctx, instr->src[0]);
1048 aco_ptr<Instruction> mov;
1049 if (dst.type() == RegType::sgpr) {
1050 if (src.type() == RegType::vgpr)
1051 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1052 else if (src.regClass() == s1)
1053 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1054 else if (src.regClass() == s2)
1055 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1056 else
1057 unreachable("wrong src register class for nir_op_imov");
1058 } else if (dst.regClass() == v1) {
1059 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1060 } else if (dst.regClass() == v2) {
1061 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1062 } else {
1063 nir_print_instr(&instr->instr, stderr);
1064 unreachable("Should have been lowered to scalar.");
1065 }
1066 break;
1067 }
1068 case nir_op_inot: {
1069 Temp src = get_alu_src(ctx, instr->src[0]);
1070 if (instr->dest.dest.ssa.bit_size == 1) {
1071 assert(src.regClass() == bld.lm);
1072 assert(dst.regClass() == bld.lm);
1073 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1074 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1075 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1076 } else if (dst.regClass() == v1) {
1077 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1078 } else if (dst.type() == RegType::sgpr) {
1079 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1080 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1081 } else {
1082 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1083 nir_print_instr(&instr->instr, stderr);
1084 fprintf(stderr, "\n");
1085 }
1086 break;
1087 }
1088 case nir_op_ineg: {
1089 Temp src = get_alu_src(ctx, instr->src[0]);
1090 if (dst.regClass() == v1) {
1091 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1092 } else if (dst.regClass() == s1) {
1093 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1094 } else if (dst.size() == 2) {
1095 Temp src0 = bld.tmp(dst.type(), 1);
1096 Temp src1 = bld.tmp(dst.type(), 1);
1097 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1098
1099 if (dst.regClass() == s2) {
1100 Temp carry = bld.tmp(s1);
1101 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1102 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1103 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1104 } else {
1105 Temp lower = bld.tmp(v1);
1106 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1107 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1108 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1109 }
1110 } else {
1111 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1112 nir_print_instr(&instr->instr, stderr);
1113 fprintf(stderr, "\n");
1114 }
1115 break;
1116 }
1117 case nir_op_iabs: {
1118 if (dst.regClass() == s1) {
1119 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1120 } else if (dst.regClass() == v1) {
1121 Temp src = get_alu_src(ctx, instr->src[0]);
1122 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1123 } else {
1124 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1125 nir_print_instr(&instr->instr, stderr);
1126 fprintf(stderr, "\n");
1127 }
1128 break;
1129 }
1130 case nir_op_isign: {
1131 Temp src = get_alu_src(ctx, instr->src[0]);
1132 if (dst.regClass() == s1) {
1133 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1134 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1135 } else if (dst.regClass() == s2) {
1136 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1137 Temp neqz;
1138 if (ctx->program->chip_class >= GFX8)
1139 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1140 else
1141 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1142 /* SCC gets zero-extended to 64 bit */
1143 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1144 } else if (dst.regClass() == v1) {
1145 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1146 } else if (dst.regClass() == v2) {
1147 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1148 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1149 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1150 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1151 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1152 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1153 } else {
1154 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1155 nir_print_instr(&instr->instr, stderr);
1156 fprintf(stderr, "\n");
1157 }
1158 break;
1159 }
1160 case nir_op_imax: {
1161 if (dst.regClass() == v1) {
1162 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1163 } else if (dst.regClass() == s1) {
1164 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1165 } else {
1166 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1167 nir_print_instr(&instr->instr, stderr);
1168 fprintf(stderr, "\n");
1169 }
1170 break;
1171 }
1172 case nir_op_umax: {
1173 if (dst.regClass() == v1) {
1174 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1175 } else if (dst.regClass() == s1) {
1176 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1177 } else {
1178 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1179 nir_print_instr(&instr->instr, stderr);
1180 fprintf(stderr, "\n");
1181 }
1182 break;
1183 }
1184 case nir_op_imin: {
1185 if (dst.regClass() == v1) {
1186 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1187 } else if (dst.regClass() == s1) {
1188 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1189 } else {
1190 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1191 nir_print_instr(&instr->instr, stderr);
1192 fprintf(stderr, "\n");
1193 }
1194 break;
1195 }
1196 case nir_op_umin: {
1197 if (dst.regClass() == v1) {
1198 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1199 } else if (dst.regClass() == s1) {
1200 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1201 } else {
1202 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1203 nir_print_instr(&instr->instr, stderr);
1204 fprintf(stderr, "\n");
1205 }
1206 break;
1207 }
1208 case nir_op_ior: {
1209 if (instr->dest.dest.ssa.bit_size == 1) {
1210 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1211 } else if (dst.regClass() == v1) {
1212 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1213 } else if (dst.regClass() == s1) {
1214 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1215 } else if (dst.regClass() == s2) {
1216 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1217 } else {
1218 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1219 nir_print_instr(&instr->instr, stderr);
1220 fprintf(stderr, "\n");
1221 }
1222 break;
1223 }
1224 case nir_op_iand: {
1225 if (instr->dest.dest.ssa.bit_size == 1) {
1226 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1227 } else if (dst.regClass() == v1) {
1228 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1229 } else if (dst.regClass() == s1) {
1230 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1231 } else if (dst.regClass() == s2) {
1232 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1233 } else {
1234 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1235 nir_print_instr(&instr->instr, stderr);
1236 fprintf(stderr, "\n");
1237 }
1238 break;
1239 }
1240 case nir_op_ixor: {
1241 if (instr->dest.dest.ssa.bit_size == 1) {
1242 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1243 } else if (dst.regClass() == v1) {
1244 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1245 } else if (dst.regClass() == s1) {
1246 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1247 } else if (dst.regClass() == s2) {
1248 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1249 } else {
1250 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1251 nir_print_instr(&instr->instr, stderr);
1252 fprintf(stderr, "\n");
1253 }
1254 break;
1255 }
1256 case nir_op_ushr: {
1257 if (dst.regClass() == v1) {
1258 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1259 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1260 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1261 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1262 } else if (dst.regClass() == v2) {
1263 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1264 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1265 } else if (dst.regClass() == s2) {
1266 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1267 } else if (dst.regClass() == s1) {
1268 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1269 } else {
1270 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1271 nir_print_instr(&instr->instr, stderr);
1272 fprintf(stderr, "\n");
1273 }
1274 break;
1275 }
1276 case nir_op_ishl: {
1277 if (dst.regClass() == v1) {
1278 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1279 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1280 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1281 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1282 } else if (dst.regClass() == v2) {
1283 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1284 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1285 } else if (dst.regClass() == s1) {
1286 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1287 } else if (dst.regClass() == s2) {
1288 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1289 } else {
1290 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1291 nir_print_instr(&instr->instr, stderr);
1292 fprintf(stderr, "\n");
1293 }
1294 break;
1295 }
1296 case nir_op_ishr: {
1297 if (dst.regClass() == v1) {
1298 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1299 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1300 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1301 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1302 } else if (dst.regClass() == v2) {
1303 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1304 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1305 } else if (dst.regClass() == s1) {
1306 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1307 } else if (dst.regClass() == s2) {
1308 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1309 } else {
1310 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1311 nir_print_instr(&instr->instr, stderr);
1312 fprintf(stderr, "\n");
1313 }
1314 break;
1315 }
1316 case nir_op_find_lsb: {
1317 Temp src = get_alu_src(ctx, instr->src[0]);
1318 if (src.regClass() == s1) {
1319 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1320 } else if (src.regClass() == v1) {
1321 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1322 } else if (src.regClass() == s2) {
1323 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1324 } else {
1325 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1326 nir_print_instr(&instr->instr, stderr);
1327 fprintf(stderr, "\n");
1328 }
1329 break;
1330 }
1331 case nir_op_ufind_msb:
1332 case nir_op_ifind_msb: {
1333 Temp src = get_alu_src(ctx, instr->src[0]);
1334 if (src.regClass() == s1 || src.regClass() == s2) {
1335 aco_opcode op = src.regClass() == s2 ?
1336 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1337 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1338 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1339
1340 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1341 Operand(src.size() * 32u - 1u), msb_rev);
1342 Temp msb = sub.def(0).getTemp();
1343 Temp carry = sub.def(1).getTemp();
1344
1345 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1346 } else if (src.regClass() == v1) {
1347 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1348 Temp msb_rev = bld.tmp(v1);
1349 emit_vop1_instruction(ctx, instr, op, msb_rev);
1350 Temp msb = bld.tmp(v1);
1351 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1352 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1353 } else {
1354 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1355 nir_print_instr(&instr->instr, stderr);
1356 fprintf(stderr, "\n");
1357 }
1358 break;
1359 }
1360 case nir_op_bitfield_reverse: {
1361 if (dst.regClass() == s1) {
1362 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1363 } else if (dst.regClass() == v1) {
1364 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1365 } else {
1366 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1367 nir_print_instr(&instr->instr, stderr);
1368 fprintf(stderr, "\n");
1369 }
1370 break;
1371 }
1372 case nir_op_iadd: {
1373 if (dst.regClass() == s1) {
1374 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1375 break;
1376 }
1377
1378 Temp src0 = get_alu_src(ctx, instr->src[0]);
1379 Temp src1 = get_alu_src(ctx, instr->src[1]);
1380 if (dst.regClass() == v1) {
1381 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1382 break;
1383 }
1384
1385 assert(src0.size() == 2 && src1.size() == 2);
1386 Temp src00 = bld.tmp(src0.type(), 1);
1387 Temp src01 = bld.tmp(dst.type(), 1);
1388 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1389 Temp src10 = bld.tmp(src1.type(), 1);
1390 Temp src11 = bld.tmp(dst.type(), 1);
1391 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1392
1393 if (dst.regClass() == s2) {
1394 Temp carry = bld.tmp(s1);
1395 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1396 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1397 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1398 } else if (dst.regClass() == v2) {
1399 Temp dst0 = bld.tmp(v1);
1400 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1401 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1402 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1403 } else {
1404 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1405 nir_print_instr(&instr->instr, stderr);
1406 fprintf(stderr, "\n");
1407 }
1408 break;
1409 }
1410 case nir_op_uadd_sat: {
1411 Temp src0 = get_alu_src(ctx, instr->src[0]);
1412 Temp src1 = get_alu_src(ctx, instr->src[1]);
1413 if (dst.regClass() == s1) {
1414 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1415 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1416 src0, src1);
1417 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1418 } else if (dst.regClass() == v1) {
1419 if (ctx->options->chip_class >= GFX9) {
1420 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1421 add->operands[0] = Operand(src0);
1422 add->operands[1] = Operand(src1);
1423 add->definitions[0] = Definition(dst);
1424 add->clamp = 1;
1425 ctx->block->instructions.emplace_back(std::move(add));
1426 } else {
1427 if (src1.regClass() != v1)
1428 std::swap(src0, src1);
1429 assert(src1.regClass() == v1);
1430 Temp tmp = bld.tmp(v1);
1431 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1432 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1433 }
1434 } else {
1435 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1436 nir_print_instr(&instr->instr, stderr);
1437 fprintf(stderr, "\n");
1438 }
1439 break;
1440 }
1441 case nir_op_uadd_carry: {
1442 Temp src0 = get_alu_src(ctx, instr->src[0]);
1443 Temp src1 = get_alu_src(ctx, instr->src[1]);
1444 if (dst.regClass() == s1) {
1445 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1446 break;
1447 }
1448 if (dst.regClass() == v1) {
1449 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1450 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1451 break;
1452 }
1453
1454 Temp src00 = bld.tmp(src0.type(), 1);
1455 Temp src01 = bld.tmp(dst.type(), 1);
1456 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1457 Temp src10 = bld.tmp(src1.type(), 1);
1458 Temp src11 = bld.tmp(dst.type(), 1);
1459 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1460 if (dst.regClass() == s2) {
1461 Temp carry = bld.tmp(s1);
1462 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1463 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1464 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1465 } else if (dst.regClass() == v2) {
1466 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1467 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1468 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1469 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1470 } else {
1471 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1472 nir_print_instr(&instr->instr, stderr);
1473 fprintf(stderr, "\n");
1474 }
1475 break;
1476 }
1477 case nir_op_isub: {
1478 if (dst.regClass() == s1) {
1479 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1480 break;
1481 }
1482
1483 Temp src0 = get_alu_src(ctx, instr->src[0]);
1484 Temp src1 = get_alu_src(ctx, instr->src[1]);
1485 if (dst.regClass() == v1) {
1486 bld.vsub32(Definition(dst), src0, src1);
1487 break;
1488 }
1489
1490 Temp src00 = bld.tmp(src0.type(), 1);
1491 Temp src01 = bld.tmp(dst.type(), 1);
1492 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1493 Temp src10 = bld.tmp(src1.type(), 1);
1494 Temp src11 = bld.tmp(dst.type(), 1);
1495 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1496 if (dst.regClass() == s2) {
1497 Temp carry = bld.tmp(s1);
1498 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1499 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1500 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1501 } else if (dst.regClass() == v2) {
1502 Temp lower = bld.tmp(v1);
1503 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1504 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1505 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1506 } else {
1507 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1508 nir_print_instr(&instr->instr, stderr);
1509 fprintf(stderr, "\n");
1510 }
1511 break;
1512 }
1513 case nir_op_usub_borrow: {
1514 Temp src0 = get_alu_src(ctx, instr->src[0]);
1515 Temp src1 = get_alu_src(ctx, instr->src[1]);
1516 if (dst.regClass() == s1) {
1517 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1518 break;
1519 } else if (dst.regClass() == v1) {
1520 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1521 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1522 break;
1523 }
1524
1525 Temp src00 = bld.tmp(src0.type(), 1);
1526 Temp src01 = bld.tmp(dst.type(), 1);
1527 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1528 Temp src10 = bld.tmp(src1.type(), 1);
1529 Temp src11 = bld.tmp(dst.type(), 1);
1530 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1531 if (dst.regClass() == s2) {
1532 Temp borrow = bld.tmp(s1);
1533 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1534 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1535 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1536 } else if (dst.regClass() == v2) {
1537 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1538 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1539 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1540 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1541 } else {
1542 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1543 nir_print_instr(&instr->instr, stderr);
1544 fprintf(stderr, "\n");
1545 }
1546 break;
1547 }
1548 case nir_op_imul: {
1549 if (dst.regClass() == v1) {
1550 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1551 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1552 } else if (dst.regClass() == s1) {
1553 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1554 } else {
1555 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1556 nir_print_instr(&instr->instr, stderr);
1557 fprintf(stderr, "\n");
1558 }
1559 break;
1560 }
1561 case nir_op_umul_high: {
1562 if (dst.regClass() == v1) {
1563 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1564 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1565 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1566 } else if (dst.regClass() == s1) {
1567 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1568 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1569 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1570 } else {
1571 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1572 nir_print_instr(&instr->instr, stderr);
1573 fprintf(stderr, "\n");
1574 }
1575 break;
1576 }
1577 case nir_op_imul_high: {
1578 if (dst.regClass() == v1) {
1579 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1580 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1581 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1582 } else if (dst.regClass() == s1) {
1583 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1584 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1585 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1586 } else {
1587 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1588 nir_print_instr(&instr->instr, stderr);
1589 fprintf(stderr, "\n");
1590 }
1591 break;
1592 }
1593 case nir_op_fmul: {
1594 Temp src0 = get_alu_src(ctx, instr->src[0]);
1595 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1596 if (dst.regClass() == v2b) {
1597 Temp tmp = bld.tmp(v1);
1598 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, tmp, true);
1599 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1600 } else if (dst.regClass() == v1) {
1601 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1602 } else if (dst.regClass() == v2) {
1603 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1604 } else {
1605 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1606 nir_print_instr(&instr->instr, stderr);
1607 fprintf(stderr, "\n");
1608 }
1609 break;
1610 }
1611 case nir_op_fadd: {
1612 Temp src0 = get_alu_src(ctx, instr->src[0]);
1613 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1614 if (dst.regClass() == v2b) {
1615 Temp tmp = bld.tmp(v1);
1616 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, tmp, true);
1617 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1618 } else if (dst.regClass() == v1) {
1619 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1620 } else if (dst.regClass() == v2) {
1621 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1622 } else {
1623 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1624 nir_print_instr(&instr->instr, stderr);
1625 fprintf(stderr, "\n");
1626 }
1627 break;
1628 }
1629 case nir_op_fsub: {
1630 Temp src0 = get_alu_src(ctx, instr->src[0]);
1631 Temp src1 = get_alu_src(ctx, instr->src[1]);
1632 if (dst.regClass() == v2b) {
1633 Temp tmp = bld.tmp(v1);
1634 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1635 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, tmp, false);
1636 else
1637 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, tmp, true);
1638 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1639 } else if (dst.regClass() == v1) {
1640 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1641 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1642 else
1643 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1644 } else if (dst.regClass() == v2) {
1645 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1646 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1647 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1648 sub->neg[1] = true;
1649 } else {
1650 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1651 nir_print_instr(&instr->instr, stderr);
1652 fprintf(stderr, "\n");
1653 }
1654 break;
1655 }
1656 case nir_op_fmax: {
1657 Temp src0 = get_alu_src(ctx, instr->src[0]);
1658 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1659 if (dst.regClass() == v2b) {
1660 // TODO: check fp_mode.must_flush_denorms16_64
1661 Temp tmp = bld.tmp(v1);
1662 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, tmp, true);
1663 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1664 } else if (dst.regClass() == v1) {
1665 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1666 } else if (dst.regClass() == v2) {
1667 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1668 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1669 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1670 } else {
1671 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1672 }
1673 } else {
1674 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1675 nir_print_instr(&instr->instr, stderr);
1676 fprintf(stderr, "\n");
1677 }
1678 break;
1679 }
1680 case nir_op_fmin: {
1681 Temp src0 = get_alu_src(ctx, instr->src[0]);
1682 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1683 if (dst.regClass() == v2b) {
1684 // TODO: check fp_mode.must_flush_denorms16_64
1685 Temp tmp = bld.tmp(v1);
1686 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, tmp, true);
1687 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1688 } else if (dst.regClass() == v1) {
1689 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1690 } else if (dst.regClass() == v2) {
1691 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1692 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1693 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1694 } else {
1695 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1696 }
1697 } else {
1698 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1699 nir_print_instr(&instr->instr, stderr);
1700 fprintf(stderr, "\n");
1701 }
1702 break;
1703 }
1704 case nir_op_fmax3: {
1705 if (dst.regClass() == v2b) {
1706 Temp tmp = bld.tmp(v1);
1707 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, tmp, false);
1708 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1709 } else if (dst.regClass() == v1) {
1710 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1711 } else {
1712 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1713 nir_print_instr(&instr->instr, stderr);
1714 fprintf(stderr, "\n");
1715 }
1716 break;
1717 }
1718 case nir_op_fmin3: {
1719 if (dst.regClass() == v2b) {
1720 Temp tmp = bld.tmp(v1);
1721 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, tmp, false);
1722 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1723 } else if (dst.regClass() == v1) {
1724 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1725 } else {
1726 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1727 nir_print_instr(&instr->instr, stderr);
1728 fprintf(stderr, "\n");
1729 }
1730 break;
1731 }
1732 case nir_op_fmed3: {
1733 if (dst.regClass() == v2b) {
1734 Temp tmp = bld.tmp(v1);
1735 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, tmp, false);
1736 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1737 } else if (dst.regClass() == v1) {
1738 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1739 } else {
1740 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1741 nir_print_instr(&instr->instr, stderr);
1742 fprintf(stderr, "\n");
1743 }
1744 break;
1745 }
1746 case nir_op_umax3: {
1747 if (dst.size() == 1) {
1748 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1749 } else {
1750 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1751 nir_print_instr(&instr->instr, stderr);
1752 fprintf(stderr, "\n");
1753 }
1754 break;
1755 }
1756 case nir_op_umin3: {
1757 if (dst.size() == 1) {
1758 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1759 } else {
1760 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1761 nir_print_instr(&instr->instr, stderr);
1762 fprintf(stderr, "\n");
1763 }
1764 break;
1765 }
1766 case nir_op_umed3: {
1767 if (dst.size() == 1) {
1768 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1769 } else {
1770 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1771 nir_print_instr(&instr->instr, stderr);
1772 fprintf(stderr, "\n");
1773 }
1774 break;
1775 }
1776 case nir_op_imax3: {
1777 if (dst.size() == 1) {
1778 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1779 } else {
1780 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1781 nir_print_instr(&instr->instr, stderr);
1782 fprintf(stderr, "\n");
1783 }
1784 break;
1785 }
1786 case nir_op_imin3: {
1787 if (dst.size() == 1) {
1788 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1789 } else {
1790 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1791 nir_print_instr(&instr->instr, stderr);
1792 fprintf(stderr, "\n");
1793 }
1794 break;
1795 }
1796 case nir_op_imed3: {
1797 if (dst.size() == 1) {
1798 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1799 } else {
1800 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1801 nir_print_instr(&instr->instr, stderr);
1802 fprintf(stderr, "\n");
1803 }
1804 break;
1805 }
1806 case nir_op_cube_face_coord: {
1807 Temp in = get_alu_src(ctx, instr->src[0], 3);
1808 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1809 emit_extract_vector(ctx, in, 1, v1),
1810 emit_extract_vector(ctx, in, 2, v1) };
1811 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1812 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1813 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1814 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1815 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1816 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1817 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1818 break;
1819 }
1820 case nir_op_cube_face_index: {
1821 Temp in = get_alu_src(ctx, instr->src[0], 3);
1822 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1823 emit_extract_vector(ctx, in, 1, v1),
1824 emit_extract_vector(ctx, in, 2, v1) };
1825 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1826 break;
1827 }
1828 case nir_op_bcsel: {
1829 emit_bcsel(ctx, instr, dst);
1830 break;
1831 }
1832 case nir_op_frsq: {
1833 Temp src = get_alu_src(ctx, instr->src[0]);
1834 if (dst.regClass() == v2b) {
1835 Temp tmp = bld.vop1(aco_opcode::v_rsq_f16, bld.def(v1), src);
1836 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1837 } else if (dst.regClass() == v1) {
1838 emit_rsq(ctx, bld, Definition(dst), src);
1839 } else if (dst.regClass() == v2) {
1840 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1841 } else {
1842 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1843 nir_print_instr(&instr->instr, stderr);
1844 fprintf(stderr, "\n");
1845 }
1846 break;
1847 }
1848 case nir_op_fneg: {
1849 Temp src = get_alu_src(ctx, instr->src[0]);
1850 if (dst.regClass() == v2b) {
1851 Temp tmp = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x8000u), as_vgpr(ctx, src));
1852 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1853 } else if (dst.regClass() == v1) {
1854 if (ctx->block->fp_mode.must_flush_denorms32)
1855 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1856 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1857 } else if (dst.regClass() == v2) {
1858 if (ctx->block->fp_mode.must_flush_denorms16_64)
1859 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1860 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1861 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1862 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1863 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1864 } else {
1865 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1866 nir_print_instr(&instr->instr, stderr);
1867 fprintf(stderr, "\n");
1868 }
1869 break;
1870 }
1871 case nir_op_fabs: {
1872 Temp src = get_alu_src(ctx, instr->src[0]);
1873 if (dst.regClass() == v2b) {
1874 Temp tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFu), as_vgpr(ctx, src));
1875 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1876 } else if (dst.regClass() == v1) {
1877 if (ctx->block->fp_mode.must_flush_denorms32)
1878 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1879 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1880 } else if (dst.regClass() == v2) {
1881 if (ctx->block->fp_mode.must_flush_denorms16_64)
1882 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1883 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1884 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1885 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1886 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1887 } else {
1888 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1889 nir_print_instr(&instr->instr, stderr);
1890 fprintf(stderr, "\n");
1891 }
1892 break;
1893 }
1894 case nir_op_fsat: {
1895 Temp src = get_alu_src(ctx, instr->src[0]);
1896 if (dst.regClass() == v2b) {
1897 Temp tmp = bld.vop3(aco_opcode::v_med3_f16, bld.def(v1), Operand(0u), Operand(0x3f800000u), src);
1898 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1899 } else if (dst.regClass() == v1) {
1900 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1901 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1902 // TODO: confirm that this holds under any circumstances
1903 } else if (dst.regClass() == v2) {
1904 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1905 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1906 vop3->clamp = true;
1907 } else {
1908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1909 nir_print_instr(&instr->instr, stderr);
1910 fprintf(stderr, "\n");
1911 }
1912 break;
1913 }
1914 case nir_op_flog2: {
1915 Temp src = get_alu_src(ctx, instr->src[0]);
1916 if (dst.regClass() == v2b) {
1917 Temp tmp = bld.vop1(aco_opcode::v_log_f16, bld.def(v1), src);
1918 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1919 } else if (dst.regClass() == v1) {
1920 emit_log2(ctx, bld, Definition(dst), src);
1921 } else {
1922 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1923 nir_print_instr(&instr->instr, stderr);
1924 fprintf(stderr, "\n");
1925 }
1926 break;
1927 }
1928 case nir_op_frcp: {
1929 Temp src = get_alu_src(ctx, instr->src[0]);
1930 if (dst.regClass() == v2b) {
1931 Temp tmp = bld.vop1(aco_opcode::v_rcp_f16, bld.def(v1), src);
1932 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1933 } else if (dst.regClass() == v1) {
1934 emit_rcp(ctx, bld, Definition(dst), src);
1935 } else if (dst.regClass() == v2) {
1936 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1937 } else {
1938 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1939 nir_print_instr(&instr->instr, stderr);
1940 fprintf(stderr, "\n");
1941 }
1942 break;
1943 }
1944 case nir_op_fexp2: {
1945 if (dst.regClass() == v2b) {
1946 Temp src = get_alu_src(ctx, instr->src[0]);
1947 Temp tmp = bld.vop1(aco_opcode::v_exp_f16, bld.def(v1), src);
1948 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1949 } else if (dst.regClass() == v1) {
1950 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1951 } else {
1952 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1953 nir_print_instr(&instr->instr, stderr);
1954 fprintf(stderr, "\n");
1955 }
1956 break;
1957 }
1958 case nir_op_fsqrt: {
1959 Temp src = get_alu_src(ctx, instr->src[0]);
1960 if (dst.regClass() == v2b) {
1961 Temp tmp = bld.vop1(aco_opcode::v_sqrt_f16, bld.def(v1), src);
1962 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1963 } else if (dst.regClass() == v1) {
1964 emit_sqrt(ctx, bld, Definition(dst), src);
1965 } else if (dst.regClass() == v2) {
1966 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1967 } else {
1968 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1969 nir_print_instr(&instr->instr, stderr);
1970 fprintf(stderr, "\n");
1971 }
1972 break;
1973 }
1974 case nir_op_ffract: {
1975 if (dst.regClass() == v2b) {
1976 Temp src = get_alu_src(ctx, instr->src[0]);
1977 Temp tmp = bld.vop1(aco_opcode::v_fract_f16, bld.def(v1), src);
1978 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1979 } else if (dst.regClass() == v1) {
1980 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1981 } else if (dst.regClass() == v2) {
1982 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1983 } else {
1984 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1985 nir_print_instr(&instr->instr, stderr);
1986 fprintf(stderr, "\n");
1987 }
1988 break;
1989 }
1990 case nir_op_ffloor: {
1991 Temp src = get_alu_src(ctx, instr->src[0]);
1992 if (dst.regClass() == v2b) {
1993 Temp tmp = bld.vop1(aco_opcode::v_floor_f16, bld.def(v1), src);
1994 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1995 } else if (dst.regClass() == v1) {
1996 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1997 } else if (dst.regClass() == v2) {
1998 emit_floor_f64(ctx, bld, Definition(dst), src);
1999 } else {
2000 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2001 nir_print_instr(&instr->instr, stderr);
2002 fprintf(stderr, "\n");
2003 }
2004 break;
2005 }
2006 case nir_op_fceil: {
2007 Temp src0 = get_alu_src(ctx, instr->src[0]);
2008 if (dst.regClass() == v2b) {
2009 Temp tmp = bld.vop1(aco_opcode::v_ceil_f16, bld.def(v1), src0);
2010 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2011 } else if (dst.regClass() == v1) {
2012 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2013 } else if (dst.regClass() == v2) {
2014 if (ctx->options->chip_class >= GFX7) {
2015 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2016 } else {
2017 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2018 /* trunc = trunc(src0)
2019 * if (src0 > 0.0 && src0 != trunc)
2020 * trunc += 1.0
2021 */
2022 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2023 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2024 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2025 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2026 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);
2027 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2028 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2029 }
2030 } else {
2031 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2032 nir_print_instr(&instr->instr, stderr);
2033 fprintf(stderr, "\n");
2034 }
2035 break;
2036 }
2037 case nir_op_ftrunc: {
2038 Temp src = get_alu_src(ctx, instr->src[0]);
2039 if (dst.regClass() == v2b) {
2040 Temp tmp = bld.vop1(aco_opcode::v_trunc_f16, bld.def(v1), src);
2041 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2042 } else if (dst.regClass() == v1) {
2043 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2044 } else if (dst.regClass() == v2) {
2045 emit_trunc_f64(ctx, bld, Definition(dst), src);
2046 } else {
2047 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2048 nir_print_instr(&instr->instr, stderr);
2049 fprintf(stderr, "\n");
2050 }
2051 break;
2052 }
2053 case nir_op_fround_even: {
2054 Temp src0 = get_alu_src(ctx, instr->src[0]);
2055 if (dst.regClass() == v2b) {
2056 Temp tmp = bld.vop1(aco_opcode::v_rndne_f16, bld.def(v1), src0);
2057 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2058 } else if (dst.regClass() == v1) {
2059 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2060 } else if (dst.regClass() == v2) {
2061 if (ctx->options->chip_class >= GFX7) {
2062 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2063 } else {
2064 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2065 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2066 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2067
2068 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2069 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));
2070 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));
2071 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));
2072 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2073 tmp = sub->definitions[0].getTemp();
2074
2075 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2076 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2077 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2078 Temp cond = vop3->definitions[0].getTemp();
2079
2080 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2081 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2082 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2083 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2084
2085 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2086 }
2087 } else {
2088 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr->instr, stderr);
2090 fprintf(stderr, "\n");
2091 }
2092 break;
2093 }
2094 case nir_op_fsin:
2095 case nir_op_fcos: {
2096 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2097 aco_ptr<Instruction> norm;
2098 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2099 if (dst.regClass() == v2b) {
2100 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2101 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2102 tmp = bld.vop1(opcode, bld.def(v1), tmp);
2103 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2104 } else if (dst.regClass() == v1) {
2105 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2106
2107 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2108 if (ctx->options->chip_class < GFX9)
2109 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2110
2111 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2112 bld.vop1(opcode, Definition(dst), tmp);
2113 } else {
2114 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2115 nir_print_instr(&instr->instr, stderr);
2116 fprintf(stderr, "\n");
2117 }
2118 break;
2119 }
2120 case nir_op_ldexp: {
2121 Temp src0 = get_alu_src(ctx, instr->src[0]);
2122 Temp src1 = get_alu_src(ctx, instr->src[1]);
2123 if (dst.regClass() == v2b) {
2124 Temp tmp = bld.tmp(v1);
2125 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, tmp, false);
2126 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2127 } else if (dst.regClass() == v1) {
2128 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2129 } else if (dst.regClass() == v2) {
2130 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2131 } else {
2132 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2133 nir_print_instr(&instr->instr, stderr);
2134 fprintf(stderr, "\n");
2135 }
2136 break;
2137 }
2138 case nir_op_frexp_sig: {
2139 Temp src = get_alu_src(ctx, instr->src[0]);
2140 if (dst.regClass() == v2b) {
2141 Temp tmp = bld.vop1(aco_opcode::v_frexp_mant_f16, bld.def(v1), src);
2142 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2143 } else if (dst.regClass() == v1) {
2144 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2145 } else if (dst.regClass() == v2) {
2146 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2147 } else {
2148 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2149 nir_print_instr(&instr->instr, stderr);
2150 fprintf(stderr, "\n");
2151 }
2152 break;
2153 }
2154 case nir_op_frexp_exp: {
2155 Temp src = get_alu_src(ctx, instr->src[0]);
2156 if (instr->src[0].src.ssa->bit_size == 16) {
2157 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2158 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2159 convert_int(bld, tmp, 8, 32, true, dst);
2160 } else if (instr->src[0].src.ssa->bit_size == 32) {
2161 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2162 } else if (instr->src[0].src.ssa->bit_size == 64) {
2163 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2164 } else {
2165 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2166 nir_print_instr(&instr->instr, stderr);
2167 fprintf(stderr, "\n");
2168 }
2169 break;
2170 }
2171 case nir_op_fsign: {
2172 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2173 if (dst.regClass() == v2b) {
2174 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2175 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2176 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2177 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2178 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2179 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), minus_one, src, cond);
2180 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2181 } else if (dst.regClass() == v1) {
2182 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2183 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2184 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2185 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2186 } else if (dst.regClass() == v2) {
2187 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2188 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2189 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2190
2191 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2192 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2193 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2194
2195 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2196 } else {
2197 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2198 nir_print_instr(&instr->instr, stderr);
2199 fprintf(stderr, "\n");
2200 }
2201 break;
2202 }
2203 case nir_op_f2f16:
2204 case nir_op_f2f16_rtne: {
2205 Temp src = get_alu_src(ctx, instr->src[0]);
2206 if (instr->src[0].src.ssa->bit_size == 64)
2207 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2208 src = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2209 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2210 break;
2211 }
2212 case nir_op_f2f16_rtz: {
2213 Temp src = get_alu_src(ctx, instr->src[0]);
2214 if (instr->src[0].src.ssa->bit_size == 64)
2215 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2216 src = bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, bld.def(v1), src, Operand(0u));
2217 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2218 break;
2219 }
2220 case nir_op_f2f32: {
2221 if (instr->src[0].src.ssa->bit_size == 16) {
2222 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2223 } else if (instr->src[0].src.ssa->bit_size == 64) {
2224 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2225 } else {
2226 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2227 nir_print_instr(&instr->instr, stderr);
2228 fprintf(stderr, "\n");
2229 }
2230 break;
2231 }
2232 case nir_op_f2f64: {
2233 Temp src = get_alu_src(ctx, instr->src[0]);
2234 if (instr->src[0].src.ssa->bit_size == 16)
2235 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2236 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2237 break;
2238 }
2239 case nir_op_i2f16: {
2240 assert(dst.regClass() == v2b);
2241 Temp src = get_alu_src(ctx, instr->src[0]);
2242 if (instr->src[0].src.ssa->bit_size == 8)
2243 src = convert_int(bld, src, 8, 16, true);
2244 Temp tmp = bld.vop1(aco_opcode::v_cvt_f16_i16, bld.def(v1), src);
2245 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2246 break;
2247 }
2248 case nir_op_i2f32: {
2249 assert(dst.size() == 1);
2250 Temp src = get_alu_src(ctx, instr->src[0]);
2251 if (instr->src[0].src.ssa->bit_size <= 16)
2252 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2253 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2254 break;
2255 }
2256 case nir_op_i2f64: {
2257 if (instr->src[0].src.ssa->bit_size <= 32) {
2258 Temp src = get_alu_src(ctx, instr->src[0]);
2259 if (instr->src[0].src.ssa->bit_size <= 16)
2260 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2261 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2262 } else if (instr->src[0].src.ssa->bit_size == 64) {
2263 Temp src = get_alu_src(ctx, instr->src[0]);
2264 RegClass rc = RegClass(src.type(), 1);
2265 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2266 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2267 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2268 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2269 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2270 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2271
2272 } else {
2273 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2274 nir_print_instr(&instr->instr, stderr);
2275 fprintf(stderr, "\n");
2276 }
2277 break;
2278 }
2279 case nir_op_u2f16: {
2280 assert(dst.regClass() == v2b);
2281 Temp src = get_alu_src(ctx, instr->src[0]);
2282 if (instr->src[0].src.ssa->bit_size == 8)
2283 src = convert_int(bld, src, 8, 16, false);
2284 Temp tmp = bld.vop1(aco_opcode::v_cvt_f16_u16, bld.def(v1), src);
2285 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2286 break;
2287 }
2288 case nir_op_u2f32: {
2289 assert(dst.size() == 1);
2290 Temp src = get_alu_src(ctx, instr->src[0]);
2291 if (instr->src[0].src.ssa->bit_size == 8) {
2292 //TODO: we should use v_cvt_f32_ubyte1/v_cvt_f32_ubyte2/etc depending on the register assignment
2293 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2294 } else {
2295 if (instr->src[0].src.ssa->bit_size == 16)
2296 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2297 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2298 }
2299 break;
2300 }
2301 case nir_op_u2f64: {
2302 if (instr->src[0].src.ssa->bit_size <= 32) {
2303 Temp src = get_alu_src(ctx, instr->src[0]);
2304 if (instr->src[0].src.ssa->bit_size <= 16)
2305 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2306 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2307 } else if (instr->src[0].src.ssa->bit_size == 64) {
2308 Temp src = get_alu_src(ctx, instr->src[0]);
2309 RegClass rc = RegClass(src.type(), 1);
2310 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2311 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2312 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2313 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2314 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2315 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2316 } else {
2317 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2318 nir_print_instr(&instr->instr, stderr);
2319 fprintf(stderr, "\n");
2320 }
2321 break;
2322 }
2323 case nir_op_f2i8:
2324 case nir_op_f2i16: {
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_i16_f16, bld.def(v1), src);
2328 else if (instr->src[0].src.ssa->bit_size == 32)
2329 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2330 else
2331 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2332
2333 if (dst.type() == RegType::vgpr)
2334 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2335 else
2336 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2337 break;
2338 }
2339 case nir_op_f2u8:
2340 case nir_op_f2u16: {
2341 Temp src = get_alu_src(ctx, instr->src[0]);
2342 if (instr->src[0].src.ssa->bit_size == 16)
2343 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2344 else if (instr->src[0].src.ssa->bit_size == 32)
2345 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2346 else
2347 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2348
2349 if (dst.type() == RegType::vgpr)
2350 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2351 else
2352 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2353 break;
2354 }
2355 case nir_op_f2i32: {
2356 Temp src = get_alu_src(ctx, instr->src[0]);
2357 if (instr->src[0].src.ssa->bit_size == 16) {
2358 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2359 if (dst.type() == RegType::vgpr) {
2360 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2361 } else {
2362 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2363 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2364 }
2365 } else if (instr->src[0].src.ssa->bit_size == 32) {
2366 if (dst.type() == RegType::vgpr)
2367 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2368 else
2369 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2370 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2371
2372 } else if (instr->src[0].src.ssa->bit_size == 64) {
2373 if (dst.type() == RegType::vgpr)
2374 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2375 else
2376 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2377 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2378
2379 } else {
2380 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2381 nir_print_instr(&instr->instr, stderr);
2382 fprintf(stderr, "\n");
2383 }
2384 break;
2385 }
2386 case nir_op_f2u32: {
2387 Temp src = get_alu_src(ctx, instr->src[0]);
2388 if (instr->src[0].src.ssa->bit_size == 16) {
2389 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2390 if (dst.type() == RegType::vgpr) {
2391 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2392 } else {
2393 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2394 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2395 }
2396 } else if (instr->src[0].src.ssa->bit_size == 32) {
2397 if (dst.type() == RegType::vgpr)
2398 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2399 else
2400 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2401 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2402
2403 } else if (instr->src[0].src.ssa->bit_size == 64) {
2404 if (dst.type() == RegType::vgpr)
2405 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2406 else
2407 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2408 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2409
2410 } else {
2411 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2412 nir_print_instr(&instr->instr, stderr);
2413 fprintf(stderr, "\n");
2414 }
2415 break;
2416 }
2417 case nir_op_f2i64: {
2418 Temp src = get_alu_src(ctx, instr->src[0]);
2419 if (instr->src[0].src.ssa->bit_size == 16)
2420 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2421
2422 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2423 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2424 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2425 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2426 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2427 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2428 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2429 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2430 Temp new_exponent = bld.tmp(v1);
2431 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2432 if (ctx->program->chip_class >= GFX8)
2433 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2434 else
2435 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2436 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2437 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2438 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2439 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2440 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2441 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2442 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2443 Temp new_lower = bld.tmp(v1);
2444 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2445 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2446 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2447
2448 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2449 if (src.type() == RegType::vgpr)
2450 src = bld.as_uniform(src);
2451 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2452 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2453 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2454 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2455 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2456 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2457 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2458 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2459 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2460 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2461 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2462 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2463 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2464 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2465 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2466 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2467 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2468 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2469 Temp borrow = bld.tmp(s1);
2470 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2471 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2472 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2473
2474 } else if (instr->src[0].src.ssa->bit_size == 64) {
2475 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2476 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2477 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2478 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2479 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2480 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2481 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2482 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2483 if (dst.type() == RegType::sgpr) {
2484 lower = bld.as_uniform(lower);
2485 upper = bld.as_uniform(upper);
2486 }
2487 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2488
2489 } else {
2490 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2491 nir_print_instr(&instr->instr, stderr);
2492 fprintf(stderr, "\n");
2493 }
2494 break;
2495 }
2496 case nir_op_f2u64: {
2497 Temp src = get_alu_src(ctx, instr->src[0]);
2498 if (instr->src[0].src.ssa->bit_size == 16)
2499 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2500
2501 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2502 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2503 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2504 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2505 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2506 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2507 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2508 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2509 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2510 Temp new_exponent = bld.tmp(v1);
2511 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2512 if (ctx->program->chip_class >= GFX8)
2513 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2514 else
2515 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2516 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2517 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2518 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2519 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2520 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2521 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2522 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2523
2524 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2525 if (src.type() == RegType::vgpr)
2526 src = bld.as_uniform(src);
2527 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2528 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2529 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2530 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2531 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2532 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2533 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2534 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2535 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2536 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2537 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2538 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2539 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2540 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2541 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2542 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2543 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2544 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2545
2546 } else if (instr->src[0].src.ssa->bit_size == 64) {
2547 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2548 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2549 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2550 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2551 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2552 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2553 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2554 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2555 if (dst.type() == RegType::sgpr) {
2556 lower = bld.as_uniform(lower);
2557 upper = bld.as_uniform(upper);
2558 }
2559 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2560
2561 } else {
2562 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2563 nir_print_instr(&instr->instr, stderr);
2564 fprintf(stderr, "\n");
2565 }
2566 break;
2567 }
2568 case nir_op_b2f16: {
2569 Temp src = get_alu_src(ctx, instr->src[0]);
2570 assert(src.regClass() == bld.lm);
2571
2572 if (dst.regClass() == s1) {
2573 src = bool_to_scalar_condition(ctx, src);
2574 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2575 } else if (dst.regClass() == v2b) {
2576 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2577 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2578 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2579 } else {
2580 unreachable("Wrong destination register class for nir_op_b2f16.");
2581 }
2582 break;
2583 }
2584 case nir_op_b2f32: {
2585 Temp src = get_alu_src(ctx, instr->src[0]);
2586 assert(src.regClass() == bld.lm);
2587
2588 if (dst.regClass() == s1) {
2589 src = bool_to_scalar_condition(ctx, src);
2590 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2591 } else if (dst.regClass() == v1) {
2592 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2593 } else {
2594 unreachable("Wrong destination register class for nir_op_b2f32.");
2595 }
2596 break;
2597 }
2598 case nir_op_b2f64: {
2599 Temp src = get_alu_src(ctx, instr->src[0]);
2600 assert(src.regClass() == bld.lm);
2601
2602 if (dst.regClass() == s2) {
2603 src = bool_to_scalar_condition(ctx, src);
2604 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2605 } else if (dst.regClass() == v2) {
2606 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2607 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2608 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2609 } else {
2610 unreachable("Wrong destination register class for nir_op_b2f64.");
2611 }
2612 break;
2613 }
2614 case nir_op_i2i8:
2615 case nir_op_i2i16:
2616 case nir_op_i2i32:
2617 case nir_op_i2i64: {
2618 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2619 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2620 break;
2621 }
2622 case nir_op_u2u8:
2623 case nir_op_u2u16:
2624 case nir_op_u2u32:
2625 case nir_op_u2u64: {
2626 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2627 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2628 break;
2629 }
2630 case nir_op_b2b32:
2631 case nir_op_b2i32: {
2632 Temp src = get_alu_src(ctx, instr->src[0]);
2633 assert(src.regClass() == bld.lm);
2634
2635 if (dst.regClass() == s1) {
2636 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2637 bool_to_scalar_condition(ctx, src, dst);
2638 } else if (dst.regClass() == v1) {
2639 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2640 } else {
2641 unreachable("Invalid register class for b2i32");
2642 }
2643 break;
2644 }
2645 case nir_op_b2b1:
2646 case nir_op_i2b1: {
2647 Temp src = get_alu_src(ctx, instr->src[0]);
2648 assert(dst.regClass() == bld.lm);
2649
2650 if (src.type() == RegType::vgpr) {
2651 assert(src.regClass() == v1 || src.regClass() == v2);
2652 assert(dst.regClass() == bld.lm);
2653 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2654 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2655 } else {
2656 assert(src.regClass() == s1 || src.regClass() == s2);
2657 Temp tmp;
2658 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2659 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2660 } else {
2661 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2662 bld.scc(bld.def(s1)), Operand(0u), src);
2663 }
2664 bool_to_vector_condition(ctx, tmp, dst);
2665 }
2666 break;
2667 }
2668 case nir_op_pack_64_2x32_split: {
2669 Temp src0 = get_alu_src(ctx, instr->src[0]);
2670 Temp src1 = get_alu_src(ctx, instr->src[1]);
2671
2672 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2673 break;
2674 }
2675 case nir_op_unpack_64_2x32_split_x:
2676 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2677 break;
2678 case nir_op_unpack_64_2x32_split_y:
2679 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2680 break;
2681 case nir_op_unpack_32_2x16_split_x:
2682 if (dst.type() == RegType::vgpr) {
2683 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2684 } else {
2685 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2686 }
2687 break;
2688 case nir_op_unpack_32_2x16_split_y:
2689 if (dst.type() == RegType::vgpr) {
2690 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2691 } else {
2692 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)));
2693 }
2694 break;
2695 case nir_op_pack_32_2x16_split: {
2696 Temp src0 = get_alu_src(ctx, instr->src[0]);
2697 Temp src1 = get_alu_src(ctx, instr->src[1]);
2698 if (dst.regClass() == v1) {
2699 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2700 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2701 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2702 } else {
2703 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2704 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2705 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2706 }
2707 break;
2708 }
2709 case nir_op_pack_half_2x16: {
2710 Temp src = get_alu_src(ctx, instr->src[0], 2);
2711
2712 if (dst.regClass() == v1) {
2713 Temp src0 = bld.tmp(v1);
2714 Temp src1 = bld.tmp(v1);
2715 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2716 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2717 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2718 else
2719 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2720 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2721 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2722 } else {
2723 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2724 nir_print_instr(&instr->instr, stderr);
2725 fprintf(stderr, "\n");
2726 }
2727 break;
2728 }
2729 case nir_op_unpack_half_2x16_split_x: {
2730 if (dst.regClass() == v1) {
2731 Builder bld(ctx->program, ctx->block);
2732 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2733 } else {
2734 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2735 nir_print_instr(&instr->instr, stderr);
2736 fprintf(stderr, "\n");
2737 }
2738 break;
2739 }
2740 case nir_op_unpack_half_2x16_split_y: {
2741 if (dst.regClass() == v1) {
2742 Builder bld(ctx->program, ctx->block);
2743 /* TODO: use SDWA here */
2744 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2745 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2746 } else {
2747 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2748 nir_print_instr(&instr->instr, stderr);
2749 fprintf(stderr, "\n");
2750 }
2751 break;
2752 }
2753 case nir_op_fquantize2f16: {
2754 Temp src = get_alu_src(ctx, instr->src[0]);
2755 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2756 Temp f32, cmp_res;
2757
2758 if (ctx->program->chip_class >= GFX8) {
2759 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2760 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2761 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2762 } else {
2763 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2764 * so compare the result and flush to 0 if it's smaller.
2765 */
2766 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2767 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2768 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2769 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2770 cmp_res = vop3->definitions[0].getTemp();
2771 }
2772
2773 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2774 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2775 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2776 } else {
2777 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2778 }
2779 break;
2780 }
2781 case nir_op_bfm: {
2782 Temp bits = get_alu_src(ctx, instr->src[0]);
2783 Temp offset = get_alu_src(ctx, instr->src[1]);
2784
2785 if (dst.regClass() == s1) {
2786 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2787 } else if (dst.regClass() == v1) {
2788 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2789 } else {
2790 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2791 nir_print_instr(&instr->instr, stderr);
2792 fprintf(stderr, "\n");
2793 }
2794 break;
2795 }
2796 case nir_op_bitfield_select: {
2797 /* (mask & insert) | (~mask & base) */
2798 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2799 Temp insert = get_alu_src(ctx, instr->src[1]);
2800 Temp base = get_alu_src(ctx, instr->src[2]);
2801
2802 /* dst = (insert & bitmask) | (base & ~bitmask) */
2803 if (dst.regClass() == s1) {
2804 aco_ptr<Instruction> sop2;
2805 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2806 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2807 Operand lhs;
2808 if (const_insert && const_bitmask) {
2809 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2810 } else {
2811 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2812 lhs = Operand(insert);
2813 }
2814
2815 Operand rhs;
2816 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2817 if (const_base && const_bitmask) {
2818 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2819 } else {
2820 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2821 rhs = Operand(base);
2822 }
2823
2824 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2825
2826 } else if (dst.regClass() == v1) {
2827 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2828 base = as_vgpr(ctx, base);
2829 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2830 insert = as_vgpr(ctx, insert);
2831
2832 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2833
2834 } else {
2835 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2836 nir_print_instr(&instr->instr, stderr);
2837 fprintf(stderr, "\n");
2838 }
2839 break;
2840 }
2841 case nir_op_ubfe:
2842 case nir_op_ibfe: {
2843 Temp base = get_alu_src(ctx, instr->src[0]);
2844 Temp offset = get_alu_src(ctx, instr->src[1]);
2845 Temp bits = get_alu_src(ctx, instr->src[2]);
2846
2847 if (dst.type() == RegType::sgpr) {
2848 Operand extract;
2849 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2850 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2851 if (const_offset && const_bits) {
2852 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2853 extract = Operand(const_extract);
2854 } else {
2855 Operand width;
2856 if (const_bits) {
2857 width = Operand(const_bits->u32 << 16);
2858 } else {
2859 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2860 }
2861 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2862 }
2863
2864 aco_opcode opcode;
2865 if (dst.regClass() == s1) {
2866 if (instr->op == nir_op_ubfe)
2867 opcode = aco_opcode::s_bfe_u32;
2868 else
2869 opcode = aco_opcode::s_bfe_i32;
2870 } else if (dst.regClass() == s2) {
2871 if (instr->op == nir_op_ubfe)
2872 opcode = aco_opcode::s_bfe_u64;
2873 else
2874 opcode = aco_opcode::s_bfe_i64;
2875 } else {
2876 unreachable("Unsupported BFE bit size");
2877 }
2878
2879 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2880
2881 } else {
2882 aco_opcode opcode;
2883 if (dst.regClass() == v1) {
2884 if (instr->op == nir_op_ubfe)
2885 opcode = aco_opcode::v_bfe_u32;
2886 else
2887 opcode = aco_opcode::v_bfe_i32;
2888 } else {
2889 unreachable("Unsupported BFE bit size");
2890 }
2891
2892 emit_vop3a_instruction(ctx, instr, opcode, dst);
2893 }
2894 break;
2895 }
2896 case nir_op_bit_count: {
2897 Temp src = get_alu_src(ctx, instr->src[0]);
2898 if (src.regClass() == s1) {
2899 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2900 } else if (src.regClass() == v1) {
2901 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2902 } else if (src.regClass() == v2) {
2903 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2904 emit_extract_vector(ctx, src, 1, v1),
2905 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2906 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2907 } else if (src.regClass() == s2) {
2908 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2909 } else {
2910 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2911 nir_print_instr(&instr->instr, stderr);
2912 fprintf(stderr, "\n");
2913 }
2914 break;
2915 }
2916 case nir_op_flt: {
2917 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2918 break;
2919 }
2920 case nir_op_fge: {
2921 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2922 break;
2923 }
2924 case nir_op_feq: {
2925 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2926 break;
2927 }
2928 case nir_op_fne: {
2929 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2930 break;
2931 }
2932 case nir_op_ilt: {
2933 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);
2934 break;
2935 }
2936 case nir_op_ige: {
2937 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);
2938 break;
2939 }
2940 case nir_op_ieq: {
2941 if (instr->src[0].src.ssa->bit_size == 1)
2942 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2943 else
2944 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,
2945 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2946 break;
2947 }
2948 case nir_op_ine: {
2949 if (instr->src[0].src.ssa->bit_size == 1)
2950 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2951 else
2952 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,
2953 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2954 break;
2955 }
2956 case nir_op_ult: {
2957 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);
2958 break;
2959 }
2960 case nir_op_uge: {
2961 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);
2962 break;
2963 }
2964 case nir_op_fddx:
2965 case nir_op_fddy:
2966 case nir_op_fddx_fine:
2967 case nir_op_fddy_fine:
2968 case nir_op_fddx_coarse:
2969 case nir_op_fddy_coarse: {
2970 Temp src = get_alu_src(ctx, instr->src[0]);
2971 uint16_t dpp_ctrl1, dpp_ctrl2;
2972 if (instr->op == nir_op_fddx_fine) {
2973 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2974 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2975 } else if (instr->op == nir_op_fddy_fine) {
2976 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2977 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2978 } else {
2979 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2980 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2981 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2982 else
2983 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2984 }
2985
2986 Temp tmp;
2987 if (ctx->program->chip_class >= GFX8) {
2988 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2989 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2990 } else {
2991 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2992 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2993 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2994 }
2995 emit_wqm(ctx, tmp, dst, true);
2996 break;
2997 }
2998 default:
2999 fprintf(stderr, "Unknown NIR ALU instr: ");
3000 nir_print_instr(&instr->instr, stderr);
3001 fprintf(stderr, "\n");
3002 }
3003 }
3004
3005 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3006 {
3007 Temp dst = get_ssa_temp(ctx, &instr->def);
3008
3009 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3010 // which get truncated the lsb if double and msb if int
3011 // for now, we only use s_mov_b64 with 64bit inline constants
3012 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3013 assert(dst.type() == RegType::sgpr);
3014
3015 Builder bld(ctx->program, ctx->block);
3016
3017 if (instr->def.bit_size == 1) {
3018 assert(dst.regClass() == bld.lm);
3019 int val = instr->value[0].b ? -1 : 0;
3020 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3021 bld.sop1(Builder::s_mov, Definition(dst), op);
3022 } else if (instr->def.bit_size == 8) {
3023 /* ensure that the value is correctly represented in the low byte of the register */
3024 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3025 } else if (instr->def.bit_size == 16) {
3026 /* ensure that the value is correctly represented in the low half of the register */
3027 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3028 } else if (dst.size() == 1) {
3029 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3030 } else {
3031 assert(dst.size() != 1);
3032 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3033 if (instr->def.bit_size == 64)
3034 for (unsigned i = 0; i < dst.size(); i++)
3035 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3036 else {
3037 for (unsigned i = 0; i < dst.size(); i++)
3038 vec->operands[i] = Operand{instr->value[i].u32};
3039 }
3040 vec->definitions[0] = Definition(dst);
3041 ctx->block->instructions.emplace_back(std::move(vec));
3042 }
3043 }
3044
3045 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3046 {
3047 uint32_t new_mask = 0;
3048 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3049 if (mask & (1u << i))
3050 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3051 return new_mask;
3052 }
3053
3054 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst)
3055 {
3056 Builder bld(ctx->program, ctx->block);
3057 if (offset.isTemp()) {
3058 Temp tmp[3] = {vec, vec, vec};
3059
3060 if (vec.size() == 3) {
3061 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
3062 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
3063 } else if (vec.size() == 2) {
3064 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
3065 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
3066 }
3067 for (unsigned i = 0; i < dst.size(); i++)
3068 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
3069
3070 vec = tmp[0];
3071 if (dst.size() == 2)
3072 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
3073
3074 offset = Operand(0u);
3075 }
3076
3077 if (vec.bytes() == dst.bytes() && offset.constantValue() == 0)
3078 bld.copy(Definition(dst), vec);
3079 else
3080 trim_subdword_vector(ctx, vec, dst, vec.bytes(), ((1 << dst.bytes()) - 1) << offset.constantValue());
3081 }
3082
3083 struct LoadEmitInfo {
3084 Operand offset;
3085 Temp dst;
3086 unsigned num_components;
3087 unsigned component_size;
3088 Temp resource = Temp(0, s1);
3089 unsigned component_stride = 0;
3090 unsigned const_offset = 0;
3091 unsigned align_mul = 0;
3092 unsigned align_offset = 0;
3093
3094 bool glc = false;
3095 unsigned swizzle_component_size = 0;
3096 barrier_interaction barrier = barrier_none;
3097 bool can_reorder = true;
3098 Temp soffset = Temp(0, s1);
3099 };
3100
3101 using LoadCallback = Temp(*)(
3102 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3103 unsigned align, unsigned const_offset, Temp dst_hint);
3104
3105 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3106 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3107 {
3108 unsigned load_size = info->num_components * info->component_size;
3109 unsigned component_size = info->component_size;
3110
3111 unsigned num_vals = 0;
3112 Temp vals[info->dst.bytes()];
3113
3114 unsigned const_offset = info->const_offset;
3115
3116 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3117 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3118
3119 unsigned bytes_read = 0;
3120 while (bytes_read < load_size) {
3121 unsigned bytes_needed = load_size - bytes_read;
3122
3123 /* add buffer for unaligned loads */
3124 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3125
3126 if (byte_align) {
3127 if ((bytes_needed > 2 || !supports_8bit_16bit_loads) && byte_align_loads) {
3128 if (info->component_stride) {
3129 assert(supports_8bit_16bit_loads && "unimplemented");
3130 bytes_needed = 2;
3131 byte_align = 0;
3132 } else {
3133 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3134 bytes_needed = align(bytes_needed, 4);
3135 }
3136 } else {
3137 byte_align = 0;
3138 }
3139 }
3140
3141 if (info->swizzle_component_size)
3142 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3143 if (info->component_stride)
3144 bytes_needed = MIN2(bytes_needed, info->component_size);
3145
3146 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3147
3148 /* reduce constant offset */
3149 Operand offset = info->offset;
3150 unsigned reduced_const_offset = const_offset;
3151 bool remove_const_offset_completely = need_to_align_offset;
3152 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3153 unsigned to_add = const_offset;
3154 if (remove_const_offset_completely) {
3155 reduced_const_offset = 0;
3156 } else {
3157 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3158 reduced_const_offset %= max_const_offset_plus_one;
3159 }
3160 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3161 if (offset.isConstant()) {
3162 offset = Operand(offset.constantValue() + to_add);
3163 } else if (offset_tmp.regClass() == s1) {
3164 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3165 offset_tmp, Operand(to_add));
3166 } else if (offset_tmp.regClass() == v1) {
3167 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3168 } else {
3169 Temp lo = bld.tmp(offset_tmp.type(), 1);
3170 Temp hi = bld.tmp(offset_tmp.type(), 1);
3171 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3172
3173 if (offset_tmp.regClass() == s2) {
3174 Temp carry = bld.tmp(s1);
3175 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3176 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3177 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3178 } else {
3179 Temp new_lo = bld.tmp(v1);
3180 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3181 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3182 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3183 }
3184 }
3185 }
3186
3187 /* align offset down if needed */
3188 Operand aligned_offset = offset;
3189 if (need_to_align_offset) {
3190 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3191 if (offset.isConstant()) {
3192 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3193 } else if (offset_tmp.regClass() == s1) {
3194 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3195 } else if (offset_tmp.regClass() == s2) {
3196 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3197 } else if (offset_tmp.regClass() == v1) {
3198 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3199 } else if (offset_tmp.regClass() == v2) {
3200 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3201 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3202 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3203 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3204 }
3205 }
3206 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3207 bld.copy(bld.def(s1), aligned_offset);
3208
3209 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3210 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3211 reduced_const_offset, byte_align ? Temp() : info->dst);
3212
3213 /* shift result right if needed */
3214 if (byte_align) {
3215 Operand align((uint32_t)byte_align);
3216 if (byte_align == -1) {
3217 if (offset.isConstant())
3218 align = Operand(offset.constantValue() % 4u);
3219 else if (offset.size() == 2)
3220 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3221 else
3222 align = offset;
3223 }
3224
3225 if (align.isTemp() || align.constantValue()) {
3226 assert(val.bytes() >= load_size && "unimplemented");
3227 Temp new_val = bld.tmp(RegClass::get(val.type(), load_size));
3228 if (val.type() == RegType::sgpr)
3229 byte_align_scalar(ctx, val, align, new_val);
3230 else
3231 byte_align_vector(ctx, val, align, new_val);
3232 val = new_val;
3233 }
3234 }
3235
3236 /* add result to list and advance */
3237 if (info->component_stride) {
3238 assert(val.bytes() == info->component_size && "unimplemented");
3239 const_offset += info->component_stride;
3240 align_offset = (align_offset + info->component_stride) % align_mul;
3241 } else {
3242 const_offset += val.bytes();
3243 align_offset = (align_offset + val.bytes()) % align_mul;
3244 }
3245 bytes_read += val.bytes();
3246 vals[num_vals++] = val;
3247 }
3248
3249 /* the callback wrote directly to dst */
3250 if (vals[0] == info->dst) {
3251 assert(num_vals == 1);
3252 emit_split_vector(ctx, info->dst, info->num_components);
3253 return;
3254 }
3255
3256 /* create array of components */
3257 unsigned components_split = 0;
3258 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3259 bool has_vgprs = false;
3260 for (unsigned i = 0; i < num_vals;) {
3261 Temp tmp[num_vals];
3262 unsigned num_tmps = 0;
3263 unsigned tmp_size = 0;
3264 RegType reg_type = RegType::sgpr;
3265 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3266 if (vals[i].type() == RegType::vgpr)
3267 reg_type = RegType::vgpr;
3268 tmp_size += vals[i].bytes();
3269 tmp[num_tmps++] = vals[i++];
3270 }
3271 if (num_tmps > 1) {
3272 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3273 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3274 for (unsigned i = 0; i < num_vals; i++)
3275 vec->operands[i] = Operand(tmp[i]);
3276 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3277 vec->definitions[0] = Definition(tmp[0]);
3278 bld.insert(std::move(vec));
3279 }
3280
3281 if (tmp[0].bytes() % component_size) {
3282 /* trim tmp[0] */
3283 assert(i == num_vals);
3284 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3285 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3286 }
3287
3288 RegClass elem_rc = RegClass::get(reg_type, component_size);
3289
3290 unsigned start = components_split;
3291
3292 if (tmp_size == elem_rc.bytes()) {
3293 allocated_vec[components_split++] = tmp[0];
3294 } else {
3295 assert(tmp_size % elem_rc.bytes() == 0);
3296 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3297 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3298 for (unsigned i = 0; i < split->definitions.size(); i++) {
3299 Temp component = bld.tmp(elem_rc);
3300 allocated_vec[components_split++] = component;
3301 split->definitions[i] = Definition(component);
3302 }
3303 split->operands[0] = Operand(tmp[0]);
3304 bld.insert(std::move(split));
3305 }
3306
3307 /* try to p_as_uniform early so we can create more optimizable code and
3308 * also update allocated_vec */
3309 for (unsigned j = start; j < components_split; j++) {
3310 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3311 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3312 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3313 }
3314 }
3315
3316 /* concatenate components and p_as_uniform() result if needed */
3317 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3318 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3319
3320 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3321
3322 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3323 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3324 for (unsigned i = 0; i < info->num_components; i++)
3325 vec->operands[i] = Operand(allocated_vec[i]);
3326 if (padding_bytes)
3327 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3328 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3329 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3330 vec->definitions[0] = Definition(tmp);
3331 bld.insert(std::move(vec));
3332 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3333 } else {
3334 vec->definitions[0] = Definition(info->dst);
3335 bld.insert(std::move(vec));
3336 }
3337 }
3338
3339 Operand load_lds_size_m0(Builder& bld)
3340 {
3341 /* TODO: m0 does not need to be initialized on GFX9+ */
3342 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3343 }
3344
3345 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3346 Temp offset, unsigned bytes_needed,
3347 unsigned align, unsigned const_offset,
3348 Temp dst_hint)
3349 {
3350 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3351
3352 Operand m = load_lds_size_m0(bld);
3353
3354 bool large_ds_read = bld.program->chip_class >= GFX7;
3355 bool usable_read2 = bld.program->chip_class >= GFX7;
3356
3357 bool read2 = false;
3358 unsigned size = 0;
3359 aco_opcode op;
3360 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3361 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3362 size = 16;
3363 op = aco_opcode::ds_read_b128;
3364 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3365 size = 16;
3366 read2 = true;
3367 op = aco_opcode::ds_read2_b64;
3368 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3369 size = 12;
3370 op = aco_opcode::ds_read_b96;
3371 } else if (bytes_needed >= 8 && align % 8 == 0) {
3372 size = 8;
3373 op = aco_opcode::ds_read_b64;
3374 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3375 size = 8;
3376 read2 = true;
3377 op = aco_opcode::ds_read2_b32;
3378 } else if (bytes_needed >= 4 && align % 4 == 0) {
3379 size = 4;
3380 op = aco_opcode::ds_read_b32;
3381 } else if (bytes_needed >= 2 && align % 2 == 0) {
3382 size = 2;
3383 op = aco_opcode::ds_read_u16;
3384 } else {
3385 size = 1;
3386 op = aco_opcode::ds_read_u8;
3387 }
3388
3389 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3390 if (const_offset >= max_offset_plus_one) {
3391 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3392 const_offset %= max_offset_plus_one;
3393 }
3394
3395 if (read2)
3396 const_offset /= (size / 2u);
3397
3398 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3399 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3400 if (read2)
3401 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3402 else
3403 bld.ds(op, Definition(val), offset, m, const_offset);
3404
3405 if (size < 4)
3406 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3407
3408 return val;
3409 }
3410
3411 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3412
3413 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3414 Temp offset, unsigned bytes_needed,
3415 unsigned align, unsigned const_offset,
3416 Temp dst_hint)
3417 {
3418 unsigned size = 0;
3419 aco_opcode op;
3420 if (bytes_needed <= 4) {
3421 size = 1;
3422 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3423 } else if (bytes_needed <= 8) {
3424 size = 2;
3425 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3426 } else if (bytes_needed <= 16) {
3427 size = 4;
3428 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3429 } else if (bytes_needed <= 32) {
3430 size = 8;
3431 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3432 } else {
3433 size = 16;
3434 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3435 }
3436 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3437 if (info->resource.id()) {
3438 load->operands[0] = Operand(info->resource);
3439 load->operands[1] = Operand(offset);
3440 } else {
3441 load->operands[0] = Operand(offset);
3442 load->operands[1] = Operand(0u);
3443 }
3444 RegClass rc(RegType::sgpr, size);
3445 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3446 load->definitions[0] = Definition(val);
3447 load->glc = info->glc;
3448 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3449 load->barrier = info->barrier;
3450 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3451 bld.insert(std::move(load));
3452 return val;
3453 }
3454
3455 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3456
3457 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3458 Temp offset, unsigned bytes_needed,
3459 unsigned align_, unsigned const_offset,
3460 Temp dst_hint)
3461 {
3462 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3463 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3464
3465 if (info->soffset.id()) {
3466 if (soffset.isTemp())
3467 vaddr = bld.copy(bld.def(v1), soffset);
3468 soffset = Operand(info->soffset);
3469 }
3470
3471 unsigned bytes_size = 0;
3472 aco_opcode op;
3473 if (bytes_needed == 1) {
3474 bytes_size = 1;
3475 op = aco_opcode::buffer_load_ubyte;
3476 } else if (bytes_needed == 2) {
3477 bytes_size = 2;
3478 op = aco_opcode::buffer_load_ushort;
3479 } else if (bytes_needed <= 4) {
3480 bytes_size = 4;
3481 op = aco_opcode::buffer_load_dword;
3482 } else if (bytes_needed <= 8) {
3483 bytes_size = 8;
3484 op = aco_opcode::buffer_load_dwordx2;
3485 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3486 bytes_size = 12;
3487 op = aco_opcode::buffer_load_dwordx3;
3488 } else {
3489 bytes_size = 16;
3490 op = aco_opcode::buffer_load_dwordx4;
3491 }
3492 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3493 mubuf->operands[0] = Operand(info->resource);
3494 mubuf->operands[1] = vaddr;
3495 mubuf->operands[2] = soffset;
3496 mubuf->offen = (offset.type() == RegType::vgpr);
3497 mubuf->glc = info->glc;
3498 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3499 mubuf->barrier = info->barrier;
3500 mubuf->can_reorder = info->can_reorder;
3501 mubuf->offset = const_offset;
3502 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3503 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3504 mubuf->definitions[0] = Definition(val);
3505 bld.insert(std::move(mubuf));
3506
3507 if (bytes_size < 4)
3508 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3509
3510 return val;
3511 }
3512
3513 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3514
3515 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3516 {
3517 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3518 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3519
3520 if (addr.type() == RegType::vgpr)
3521 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3522 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3523 }
3524
3525 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3526 Temp offset, unsigned bytes_needed,
3527 unsigned align_, unsigned const_offset,
3528 Temp dst_hint)
3529 {
3530 unsigned bytes_size = 0;
3531 bool mubuf = bld.program->chip_class == GFX6;
3532 bool global = bld.program->chip_class >= GFX9;
3533 aco_opcode op;
3534 if (bytes_needed == 1) {
3535 bytes_size = 1;
3536 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3537 } else if (bytes_needed == 2) {
3538 bytes_size = 2;
3539 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3540 } else if (bytes_needed <= 4) {
3541 bytes_size = 4;
3542 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3543 } else if (bytes_needed <= 8) {
3544 bytes_size = 8;
3545 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3546 } else if (bytes_needed <= 12 && !mubuf) {
3547 bytes_size = 12;
3548 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3549 } else {
3550 bytes_size = 16;
3551 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3552 }
3553 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3554 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3555 if (mubuf) {
3556 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3557 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3558 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3559 mubuf->operands[2] = Operand(0u);
3560 mubuf->glc = info->glc;
3561 mubuf->dlc = false;
3562 mubuf->offset = 0;
3563 mubuf->addr64 = offset.type() == RegType::vgpr;
3564 mubuf->disable_wqm = false;
3565 mubuf->barrier = info->barrier;
3566 mubuf->definitions[0] = Definition(val);
3567 bld.insert(std::move(mubuf));
3568 } else {
3569 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3570
3571 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3572 flat->operands[0] = Operand(offset);
3573 flat->operands[1] = Operand(s1);
3574 flat->glc = info->glc;
3575 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3576 flat->barrier = info->barrier;
3577 flat->offset = 0u;
3578 flat->definitions[0] = Definition(val);
3579 bld.insert(std::move(flat));
3580 }
3581
3582 if (bytes_size < 4)
3583 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3584
3585 return val;
3586 }
3587
3588 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3589
3590 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3591 Temp address, unsigned base_offset, unsigned align)
3592 {
3593 assert(util_is_power_of_two_nonzero(align));
3594
3595 Builder bld(ctx->program, ctx->block);
3596
3597 unsigned num_components = dst.bytes() / elem_size_bytes;
3598 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3599 info.align_mul = align;
3600 info.align_offset = 0;
3601 info.barrier = barrier_shared;
3602 info.can_reorder = false;
3603 info.const_offset = base_offset;
3604 emit_lds_load(ctx, bld, &info);
3605
3606 return dst;
3607 }
3608
3609 Temp extract_subvector(isel_context *ctx, Temp data, unsigned start, unsigned size, RegType type)
3610 {
3611 if (start == 0 && size == data.size())
3612 return type == RegType::vgpr ? as_vgpr(ctx, data) : data;
3613
3614 unsigned size_hint = 1;
3615 auto it = ctx->allocated_vec.find(data.id());
3616 if (it != ctx->allocated_vec.end())
3617 size_hint = it->second[0].size();
3618 if (size % size_hint || start % size_hint)
3619 size_hint = 1;
3620
3621 start /= size_hint;
3622 size /= size_hint;
3623
3624 Temp elems[size];
3625 for (unsigned i = 0; i < size; i++)
3626 elems[i] = emit_extract_vector(ctx, data, start + i, RegClass(type, size_hint));
3627
3628 if (size == 1)
3629 return type == RegType::vgpr ? as_vgpr(ctx, elems[0]) : elems[0];
3630
3631 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
3632 for (unsigned i = 0; i < size; i++)
3633 vec->operands[i] = Operand(elems[i]);
3634 Temp res = {ctx->program->allocateId(), RegClass(type, size * size_hint)};
3635 vec->definitions[0] = Definition(res);
3636 ctx->block->instructions.emplace_back(std::move(vec));
3637 return res;
3638 }
3639
3640 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3641 {
3642 if (!count)
3643 return;
3644
3645 Builder bld(ctx->program, ctx->block);
3646
3647 ASSERTED bool is_subdword = false;
3648 for (unsigned i = 0; i < count; i++)
3649 is_subdword |= offsets[i] % 4;
3650 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3651 assert(!is_subdword || dst_type == RegType::vgpr);
3652
3653 /* count == 1 fast path */
3654 if (count == 1) {
3655 if (dst_type == RegType::sgpr)
3656 dst[0] = bld.as_uniform(src);
3657 else
3658 dst[0] = as_vgpr(ctx, src);
3659 return;
3660 }
3661
3662 for (unsigned i = 0; i < count - 1; i++)
3663 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3664 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3665
3666 if (is_subdword && src.type() == RegType::sgpr) {
3667 src = as_vgpr(ctx, src);
3668 } else {
3669 /* use allocated_vec if possible */
3670 auto it = ctx->allocated_vec.find(src.id());
3671 if (it != ctx->allocated_vec.end()) {
3672 unsigned total_size = 0;
3673 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3674 total_size += it->second[i].bytes();
3675 if (total_size != src.bytes())
3676 goto split;
3677
3678 unsigned elem_size = it->second[0].bytes();
3679
3680 for (unsigned i = 0; i < count; i++) {
3681 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3682 goto split;
3683 }
3684
3685 for (unsigned i = 0; i < count; i++) {
3686 unsigned start_idx = offsets[i] / elem_size;
3687 unsigned op_count = dst[i].bytes() / elem_size;
3688 if (op_count == 1) {
3689 if (dst_type == RegType::sgpr)
3690 dst[i] = bld.as_uniform(it->second[start_idx]);
3691 else
3692 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3693 continue;
3694 }
3695
3696 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3697 for (unsigned j = 0; j < op_count; j++) {
3698 Temp tmp = it->second[start_idx + j];
3699 if (dst_type == RegType::sgpr)
3700 tmp = bld.as_uniform(tmp);
3701 vec->operands[j] = Operand(tmp);
3702 }
3703 vec->definitions[0] = Definition(dst[i]);
3704 bld.insert(std::move(vec));
3705 }
3706 return;
3707 }
3708 }
3709
3710 if (dst_type == RegType::sgpr)
3711 src = bld.as_uniform(src);
3712
3713 split:
3714 /* just split it */
3715 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3716 split->operands[0] = Operand(src);
3717 for (unsigned i = 0; i < count; i++)
3718 split->definitions[i] = Definition(dst[i]);
3719 bld.insert(std::move(split));
3720 }
3721
3722 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3723 int *start, int *count)
3724 {
3725 unsigned start_elem = ffs(todo_mask) - 1;
3726 bool skip = !(mask & (1 << start_elem));
3727 if (skip)
3728 mask = ~mask & todo_mask;
3729
3730 mask &= todo_mask;
3731
3732 u_bit_scan_consecutive_range(&mask, start, count);
3733
3734 return !skip;
3735 }
3736
3737 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3738 {
3739 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3740 }
3741
3742 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3743 Temp address, unsigned base_offset, unsigned align)
3744 {
3745 assert(util_is_power_of_two_nonzero(align));
3746 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3747
3748 Builder bld(ctx->program, ctx->block);
3749 bool large_ds_write = ctx->options->chip_class >= GFX7;
3750 bool usable_write2 = ctx->options->chip_class >= GFX7;
3751
3752 unsigned write_count = 0;
3753 Temp write_datas[32];
3754 unsigned offsets[32];
3755 aco_opcode opcodes[32];
3756
3757 wrmask = widen_mask(wrmask, elem_size_bytes);
3758
3759 uint32_t todo = u_bit_consecutive(0, data.bytes());
3760 while (todo) {
3761 int offset, bytes;
3762 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3763 offsets[write_count] = offset;
3764 opcodes[write_count] = aco_opcode::num_opcodes;
3765 write_count++;
3766 advance_write_mask(&todo, offset, bytes);
3767 continue;
3768 }
3769
3770 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3771 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3772 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3773 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3774
3775 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3776 aco_opcode op = aco_opcode::num_opcodes;
3777 if (bytes >= 16 && aligned16 && large_ds_write) {
3778 op = aco_opcode::ds_write_b128;
3779 bytes = 16;
3780 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3781 op = aco_opcode::ds_write_b96;
3782 bytes = 12;
3783 } else if (bytes >= 8 && aligned8) {
3784 op = aco_opcode::ds_write_b64;
3785 bytes = 8;
3786 } else if (bytes >= 4 && aligned4) {
3787 op = aco_opcode::ds_write_b32;
3788 bytes = 4;
3789 } else if (bytes >= 2 && aligned2) {
3790 op = aco_opcode::ds_write_b16;
3791 bytes = 2;
3792 } else if (bytes >= 1) {
3793 op = aco_opcode::ds_write_b8;
3794 bytes = 1;
3795 } else {
3796 assert(false);
3797 }
3798
3799 offsets[write_count] = offset;
3800 opcodes[write_count] = op;
3801 write_count++;
3802 advance_write_mask(&todo, offset, bytes);
3803 }
3804
3805 Operand m = load_lds_size_m0(bld);
3806
3807 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3808
3809 for (unsigned i = 0; i < write_count; i++) {
3810 aco_opcode op = opcodes[i];
3811 if (op == aco_opcode::num_opcodes)
3812 continue;
3813
3814 Temp data = write_datas[i];
3815
3816 unsigned second = write_count;
3817 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3818 for (second = i + 1; second < write_count; second++) {
3819 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3820 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3821 opcodes[second] = aco_opcode::num_opcodes;
3822 break;
3823 }
3824 }
3825 }
3826
3827 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3828 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3829
3830 unsigned inline_offset = base_offset + offsets[i];
3831 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3832 Temp address_offset = address;
3833 if (inline_offset > max_offset) {
3834 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3835 inline_offset = offsets[i];
3836 }
3837 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3838
3839 if (write2) {
3840 Temp second_data = write_datas[second];
3841 inline_offset /= data.bytes();
3842 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3843 } else {
3844 bld.ds(op, address_offset, data, m, inline_offset);
3845 }
3846 }
3847 }
3848
3849 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3850 {
3851 unsigned align = 16;
3852 if (const_offset)
3853 align = std::min(align, 1u << (ffs(const_offset) - 1));
3854
3855 return align;
3856 }
3857
3858
3859 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3860 Temp data, unsigned writemask, int swizzle_element_size,
3861 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3862 {
3863 unsigned write_count_with_skips = 0;
3864 bool skips[16];
3865
3866 /* determine how to split the data */
3867 unsigned todo = u_bit_consecutive(0, data.bytes());
3868 while (todo) {
3869 int offset, bytes;
3870 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3871 offsets[write_count_with_skips] = offset;
3872 if (skips[write_count_with_skips]) {
3873 advance_write_mask(&todo, offset, bytes);
3874 write_count_with_skips++;
3875 continue;
3876 }
3877
3878 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3879 * larger than swizzle_element_size */
3880 bytes = MIN2(bytes, swizzle_element_size);
3881 if (bytes % 4)
3882 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3883
3884 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3885 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3886 bytes = 8;
3887
3888 /* dword or larger stores have to be dword-aligned */
3889 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3890 unsigned align_offset = instr ? nir_intrinsic_align_mul(instr) : 0;
3891 bool dword_aligned = (align_offset + offset) % 4 == 0 && align_mul % 4 == 0;
3892 if (bytes >= 4 && !dword_aligned)
3893 bytes = MIN2(bytes, 2);
3894
3895 advance_write_mask(&todo, offset, bytes);
3896 write_count_with_skips++;
3897 }
3898
3899 /* actually split data */
3900 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3901
3902 /* remove skips */
3903 for (unsigned i = 0; i < write_count_with_skips; i++) {
3904 if (skips[i])
3905 continue;
3906 write_datas[*write_count] = write_datas[i];
3907 offsets[*write_count] = offsets[i];
3908 (*write_count)++;
3909 }
3910 }
3911
3912 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3913 unsigned split_cnt = 0u, Temp dst = Temp())
3914 {
3915 Builder bld(ctx->program, ctx->block);
3916 unsigned dword_size = elem_size_bytes / 4;
3917
3918 if (!dst.id())
3919 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3920
3921 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3922 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3923 instr->definitions[0] = Definition(dst);
3924
3925 for (unsigned i = 0; i < cnt; ++i) {
3926 if (arr[i].id()) {
3927 assert(arr[i].size() == dword_size);
3928 allocated_vec[i] = arr[i];
3929 instr->operands[i] = Operand(arr[i]);
3930 } else {
3931 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3932 allocated_vec[i] = zero;
3933 instr->operands[i] = Operand(zero);
3934 }
3935 }
3936
3937 bld.insert(std::move(instr));
3938
3939 if (split_cnt)
3940 emit_split_vector(ctx, dst, split_cnt);
3941 else
3942 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3943
3944 return dst;
3945 }
3946
3947 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3948 {
3949 if (const_offset >= 4096) {
3950 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3951 const_offset %= 4096u;
3952
3953 if (!voffset.id())
3954 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3955 else if (unlikely(voffset.regClass() == s1))
3956 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3957 else if (likely(voffset.regClass() == v1))
3958 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3959 else
3960 unreachable("Unsupported register class of voffset");
3961 }
3962
3963 return const_offset;
3964 }
3965
3966 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3967 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3968 {
3969 assert(vdata.id());
3970 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3971 assert(vdata.size() >= 1 && vdata.size() <= 4);
3972
3973 Builder bld(ctx->program, ctx->block);
3974 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_store_dword + vdata.size() - 1);
3975 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3976
3977 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3978 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3979 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3980 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3981 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3982
3983 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3984 }
3985
3986 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3987 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3988 bool allow_combining = true, bool reorder = true, bool slc = false)
3989 {
3990 Builder bld(ctx->program, ctx->block);
3991 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3992 assert(write_mask);
3993
3994 if (elem_size_bytes == 8) {
3995 elem_size_bytes = 4;
3996 write_mask = widen_mask(write_mask, 2);
3997 }
3998
3999 while (write_mask) {
4000 int start = 0;
4001 int count = 0;
4002 u_bit_scan_consecutive_range(&write_mask, &start, &count);
4003 assert(count > 0);
4004 assert(start >= 0);
4005
4006 while (count > 0) {
4007 unsigned sub_count = allow_combining ? MIN2(count, 4) : 1;
4008 unsigned const_offset = (unsigned) start * elem_size_bytes + base_const_offset;
4009
4010 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
4011 if (unlikely(ctx->program->chip_class == GFX6 && sub_count == 3))
4012 sub_count = 2;
4013
4014 Temp elem = extract_subvector(ctx, src, start, sub_count, RegType::vgpr);
4015 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, elem, const_offset, reorder, slc);
4016
4017 count -= sub_count;
4018 start += sub_count;
4019 }
4020
4021 assert(count == 0);
4022 }
4023 }
4024
4025 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4026 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4027 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4028 {
4029 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
4030 assert((num_components * elem_size_bytes / 4) == dst.size());
4031 assert(!!stride != allow_combining);
4032
4033 Builder bld(ctx->program, ctx->block);
4034
4035 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4036 info.component_stride = allow_combining ? 0 : stride;
4037 info.glc = true;
4038 info.swizzle_component_size = allow_combining ? 0 : 4;
4039 info.align_mul = MIN2(elem_size_bytes, 4);
4040 info.align_offset = 0;
4041 info.soffset = soffset;
4042 info.const_offset = base_const_offset;
4043 emit_mubuf_load(ctx, bld, &info);
4044 }
4045
4046 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)
4047 {
4048 Builder bld(ctx->program, ctx->block);
4049 Temp offset = base_offset.first;
4050 unsigned const_offset = base_offset.second;
4051
4052 if (!nir_src_is_const(*off_src)) {
4053 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4054 Temp with_stride;
4055
4056 /* Calculate indirect offset with stride */
4057 if (likely(indirect_offset_arg.regClass() == v1))
4058 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4059 else if (indirect_offset_arg.regClass() == s1)
4060 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4061 else
4062 unreachable("Unsupported register class of indirect offset");
4063
4064 /* Add to the supplied base offset */
4065 if (offset.id() == 0)
4066 offset = with_stride;
4067 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4068 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4069 else if (offset.size() == 1 && with_stride.size() == 1)
4070 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4071 else
4072 unreachable("Unsupported register class of indirect offset");
4073 } else {
4074 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4075 const_offset += const_offset_arg * stride;
4076 }
4077
4078 return std::make_pair(offset, const_offset);
4079 }
4080
4081 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4082 {
4083 Builder bld(ctx->program, ctx->block);
4084 Temp offset;
4085
4086 if (off1.first.id() && off2.first.id()) {
4087 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4088 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4089 else if (off1.first.size() == 1 && off2.first.size() == 1)
4090 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4091 else
4092 unreachable("Unsupported register class of indirect offset");
4093 } else {
4094 offset = off1.first.id() ? off1.first : off2.first;
4095 }
4096
4097 return std::make_pair(offset, off1.second + off2.second);
4098 }
4099
4100 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4101 {
4102 Builder bld(ctx->program, ctx->block);
4103 unsigned const_offset = offs.second * multiplier;
4104
4105 if (!offs.first.id())
4106 return std::make_pair(offs.first, const_offset);
4107
4108 Temp offset = unlikely(offs.first.regClass() == s1)
4109 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4110 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4111
4112 return std::make_pair(offset, const_offset);
4113 }
4114
4115 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4116 {
4117 Builder bld(ctx->program, ctx->block);
4118
4119 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4120 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4121 /* component is in bytes */
4122 const_offset += nir_intrinsic_component(instr) * component_stride;
4123
4124 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4125 nir_src *off_src = nir_get_io_offset_src(instr);
4126 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4127 }
4128
4129 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4130 {
4131 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4132 }
4133
4134 Temp get_tess_rel_patch_id(isel_context *ctx)
4135 {
4136 Builder bld(ctx->program, ctx->block);
4137
4138 switch (ctx->shader->info.stage) {
4139 case MESA_SHADER_TESS_CTRL:
4140 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4141 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4142 case MESA_SHADER_TESS_EVAL:
4143 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4144 default:
4145 unreachable("Unsupported stage in get_tess_rel_patch_id");
4146 }
4147 }
4148
4149 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4150 {
4151 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4152 Builder bld(ctx->program, ctx->block);
4153
4154 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4155 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4156
4157 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4158
4159 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4160 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4161
4162 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4163 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4164 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4165
4166 return offset_mul(ctx, offs, 4u);
4167 }
4168
4169 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4170 {
4171 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4172 Builder bld(ctx->program, ctx->block);
4173
4174 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4175 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
4176 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
4177 uint32_t output_vertex_size = num_tcs_outputs * 16;
4178 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4179 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
4180
4181 std::pair<Temp, unsigned> offs = instr
4182 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4183 : std::make_pair(Temp(), 0u);
4184
4185 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4186 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4187
4188 if (per_vertex) {
4189 assert(instr);
4190
4191 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4192 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4193
4194 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4195 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4196 } else {
4197 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4198 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4199 }
4200
4201 return offs;
4202 }
4203
4204 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4205 {
4206 Builder bld(ctx->program, ctx->block);
4207
4208 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4209 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4210
4211 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4212
4213 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4214 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4215 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4216
4217 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4218 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4219
4220 return offs;
4221 }
4222
4223 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4224 {
4225 Builder bld(ctx->program, ctx->block);
4226
4227 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
4228 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
4229 : ctx->args->options->key.tes.tcs_num_outputs;
4230
4231 unsigned output_vertex_size = num_tcs_outputs * 16;
4232 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4233 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4234 unsigned attr_stride = ctx->tcs_num_patches;
4235
4236 std::pair<Temp, unsigned> offs = instr
4237 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4238 : std::make_pair(Temp(), 0u);
4239
4240 if (const_base_offset)
4241 offs.second += const_base_offset * attr_stride;
4242
4243 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4244 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4245 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4246
4247 return offs;
4248 }
4249
4250 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4251 {
4252 if (mask == 0)
4253 return false;
4254
4255 unsigned off = nir_intrinsic_base(instr) * 4u;
4256 nir_src *off_src = nir_get_io_offset_src(instr);
4257
4258 if (!nir_src_is_const(*off_src)) {
4259 *indirect = true;
4260 return false;
4261 }
4262
4263 *indirect = false;
4264 off += nir_src_as_uint(*off_src) * 16u;
4265
4266 while (mask) {
4267 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
4268 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
4269 return true;
4270 }
4271
4272 return false;
4273 }
4274
4275 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4276 {
4277 unsigned write_mask = nir_intrinsic_write_mask(instr);
4278 unsigned component = nir_intrinsic_component(instr);
4279 unsigned idx = nir_intrinsic_base(instr) + component;
4280
4281 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4282 if (off_instr->type != nir_instr_type_load_const)
4283 return false;
4284
4285 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4286 idx += nir_src_as_uint(instr->src[1]) * 4u;
4287
4288 if (instr->src[0].ssa->bit_size == 64)
4289 write_mask = widen_mask(write_mask, 2);
4290
4291 for (unsigned i = 0; i < 8; ++i) {
4292 if (write_mask & (1 << i)) {
4293 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4294 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
4295 }
4296 idx++;
4297 }
4298
4299 return true;
4300 }
4301
4302 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4303 {
4304 /* Only TCS per-vertex inputs are supported by this function.
4305 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4306 */
4307 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4308 return false;
4309
4310 nir_src *off_src = nir_get_io_offset_src(instr);
4311 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4312 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4313 bool can_use_temps = nir_src_is_const(*off_src) &&
4314 vertex_index_instr->type == nir_instr_type_intrinsic &&
4315 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4316
4317 if (!can_use_temps)
4318 return false;
4319
4320 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4321 Temp *src = &ctx->inputs.temps[idx];
4322 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4323
4324 return true;
4325 }
4326
4327 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4328 {
4329 Builder bld(ctx->program, ctx->block);
4330
4331 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4332 /* 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. */
4333 bool indirect_write;
4334 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4335 if (temp_only_input && !indirect_write)
4336 return;
4337 }
4338
4339 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4340 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4341 unsigned write_mask = nir_intrinsic_write_mask(instr);
4342 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4343
4344 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4345 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4346 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4347 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4348 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4349 } else {
4350 Temp lds_base;
4351
4352 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4353 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4354 unsigned itemsize = ctx->stage == vertex_geometry_gs
4355 ? ctx->program->info->vs.es_info.esgs_itemsize
4356 : ctx->program->info->tes.es_info.esgs_itemsize;
4357 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4358 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));
4359 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4360 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4361 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4362 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4363 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4364 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4365 */
4366 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
4367 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4368 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
4369 } else {
4370 unreachable("Invalid LS or ES stage");
4371 }
4372
4373 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4374 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4375 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4376 }
4377 }
4378
4379 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4380 {
4381 if (per_vertex)
4382 return false;
4383
4384 unsigned off = nir_intrinsic_base(instr) * 4u;
4385 return off == ctx->tcs_tess_lvl_out_loc ||
4386 off == ctx->tcs_tess_lvl_in_loc;
4387
4388 }
4389
4390 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4391 {
4392 uint64_t mask = per_vertex
4393 ? ctx->program->info->tcs.tes_inputs_read
4394 : ctx->program->info->tcs.tes_patch_inputs_read;
4395
4396 bool indirect_write = false;
4397 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4398 return indirect_write || output_read_by_tes;
4399 }
4400
4401 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4402 {
4403 uint64_t mask = per_vertex
4404 ? ctx->shader->info.outputs_read
4405 : ctx->shader->info.patch_outputs_read;
4406
4407 bool indirect_write = false;
4408 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4409 return indirect_write || output_read;
4410 }
4411
4412 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4413 {
4414 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4415 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4416
4417 Builder bld(ctx->program, ctx->block);
4418
4419 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4420 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4421 unsigned write_mask = nir_intrinsic_write_mask(instr);
4422
4423 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4424 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4425 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4426
4427 if (write_to_vmem) {
4428 std::pair<Temp, unsigned> vmem_offs = per_vertex
4429 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4430 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4431
4432 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));
4433 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4434 store_vmem_mubuf(ctx, store_val, hs_ring_tess_offchip, vmem_offs.first, oc_lds, vmem_offs.second, elem_size_bytes, write_mask, true, false);
4435 }
4436
4437 if (write_to_lds) {
4438 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4439 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4440 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4441 }
4442 }
4443
4444 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4445 {
4446 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4447 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4448
4449 Builder bld(ctx->program, ctx->block);
4450
4451 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4452 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4453 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4454 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4455
4456 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4457 }
4458
4459 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4460 {
4461 if (ctx->stage == vertex_vs ||
4462 ctx->stage == tess_eval_vs ||
4463 ctx->stage == fragment_fs ||
4464 ctx->stage == ngg_vertex_gs ||
4465 ctx->stage == ngg_tess_eval_gs ||
4466 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4467 bool stored_to_temps = store_output_to_temps(ctx, instr);
4468 if (!stored_to_temps) {
4469 fprintf(stderr, "Unimplemented output offset instruction:\n");
4470 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4471 fprintf(stderr, "\n");
4472 abort();
4473 }
4474 } else if (ctx->stage == vertex_es ||
4475 ctx->stage == vertex_ls ||
4476 ctx->stage == tess_eval_es ||
4477 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4478 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4479 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4480 visit_store_ls_or_es_output(ctx, instr);
4481 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4482 visit_store_tcs_output(ctx, instr, false);
4483 } else {
4484 unreachable("Shader stage not implemented");
4485 }
4486 }
4487
4488 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4489 {
4490 visit_load_tcs_output(ctx, instr, false);
4491 }
4492
4493 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4494 {
4495 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4496 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4497
4498 Builder bld(ctx->program, ctx->block);
4499 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
4500 if (ctx->program->has_16bank_lds)
4501 interp_p1.instr->operands[0].setLateKill(true);
4502 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
4503 }
4504
4505 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4506 {
4507 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4508 for (unsigned i = 0; i < num_components; i++)
4509 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4510 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4511 assert(num_components == 4);
4512 Builder bld(ctx->program, ctx->block);
4513 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4514 }
4515
4516 for (Operand& op : vec->operands)
4517 op = op.isUndefined() ? Operand(0u) : op;
4518
4519 vec->definitions[0] = Definition(dst);
4520 ctx->block->instructions.emplace_back(std::move(vec));
4521 emit_split_vector(ctx, dst, num_components);
4522 return;
4523 }
4524
4525 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4526 {
4527 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4528 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4529 unsigned idx = nir_intrinsic_base(instr);
4530 unsigned component = nir_intrinsic_component(instr);
4531 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4532
4533 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4534 if (offset) {
4535 assert(offset->u32 == 0);
4536 } else {
4537 /* the lower 15bit of the prim_mask contain the offset into LDS
4538 * while the upper bits contain the number of prims */
4539 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4540 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4541 Builder bld(ctx->program, ctx->block);
4542 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4543 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4544 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4545 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4546 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4547 }
4548
4549 if (instr->dest.ssa.num_components == 1) {
4550 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4551 } else {
4552 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4553 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4554 {
4555 Temp tmp = {ctx->program->allocateId(), v1};
4556 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4557 vec->operands[i] = Operand(tmp);
4558 }
4559 vec->definitions[0] = Definition(dst);
4560 ctx->block->instructions.emplace_back(std::move(vec));
4561 }
4562 }
4563
4564 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4565 unsigned offset, unsigned stride, unsigned channels)
4566 {
4567 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4568 if (vtx_info->chan_byte_size != 4 && channels == 3)
4569 return false;
4570 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4571 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4572 }
4573
4574 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4575 unsigned offset, unsigned stride, unsigned *channels)
4576 {
4577 if (!vtx_info->chan_byte_size) {
4578 *channels = vtx_info->num_channels;
4579 return vtx_info->chan_format;
4580 }
4581
4582 unsigned num_channels = *channels;
4583 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4584 unsigned new_channels = num_channels + 1;
4585 /* first, assume more loads is worse and try using a larger data format */
4586 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4587 new_channels++;
4588 /* don't make the attribute potentially out-of-bounds */
4589 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4590 new_channels = 5;
4591 }
4592
4593 if (new_channels == 5) {
4594 /* then try decreasing load size (at the cost of more loads) */
4595 new_channels = *channels;
4596 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4597 new_channels--;
4598 }
4599
4600 if (new_channels < *channels)
4601 *channels = new_channels;
4602 num_channels = new_channels;
4603 }
4604
4605 switch (vtx_info->chan_format) {
4606 case V_008F0C_BUF_DATA_FORMAT_8:
4607 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4608 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4609 case V_008F0C_BUF_DATA_FORMAT_16:
4610 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4611 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4612 case V_008F0C_BUF_DATA_FORMAT_32:
4613 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4614 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4615 }
4616 unreachable("shouldn't reach here");
4617 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4618 }
4619
4620 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4621 * so we may need to fix it up. */
4622 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4623 {
4624 Builder bld(ctx->program, ctx->block);
4625
4626 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4627 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4628
4629 /* For the integer-like cases, do a natural sign extension.
4630 *
4631 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4632 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4633 * exponent.
4634 */
4635 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4636 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4637
4638 /* Convert back to the right type. */
4639 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4640 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4641 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4642 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4643 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4644 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4645 }
4646
4647 return alpha;
4648 }
4649
4650 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4651 {
4652 Builder bld(ctx->program, ctx->block);
4653 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4654 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4655
4656 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4657 if (off_instr->type != nir_instr_type_load_const) {
4658 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4659 nir_print_instr(off_instr, stderr);
4660 fprintf(stderr, "\n");
4661 }
4662 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4663
4664 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4665
4666 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4667 unsigned component = nir_intrinsic_component(instr);
4668 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4669 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4670 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4671 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4672
4673 unsigned dfmt = attrib_format & 0xf;
4674 unsigned nfmt = (attrib_format >> 4) & 0x7;
4675 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4676
4677 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4678 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4679 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4680 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4681 if (post_shuffle)
4682 num_channels = MAX2(num_channels, 3);
4683
4684 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4685 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4686
4687 Temp index;
4688 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4689 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4690 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4691 if (divisor) {
4692 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4693 if (divisor != 1) {
4694 Temp divided = bld.tmp(v1);
4695 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4696 index = bld.vadd32(bld.def(v1), start_instance, divided);
4697 } else {
4698 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4699 }
4700 } else {
4701 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4702 }
4703 } else {
4704 index = bld.vadd32(bld.def(v1),
4705 get_arg(ctx, ctx->args->ac.base_vertex),
4706 get_arg(ctx, ctx->args->ac.vertex_id));
4707 }
4708
4709 Temp channels[num_channels];
4710 unsigned channel_start = 0;
4711 bool direct_fetch = false;
4712
4713 /* skip unused channels at the start */
4714 if (vtx_info->chan_byte_size && !post_shuffle) {
4715 channel_start = ffs(mask) - 1;
4716 for (unsigned i = 0; i < channel_start; i++)
4717 channels[i] = Temp(0, s1);
4718 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4719 num_channels = 3 - (ffs(mask) - 1);
4720 }
4721
4722 /* load channels */
4723 while (channel_start < num_channels) {
4724 unsigned fetch_size = num_channels - channel_start;
4725 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4726 bool expanded = false;
4727
4728 /* use MUBUF when possible to avoid possible alignment issues */
4729 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4730 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4731 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4732 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4733 vtx_info->chan_byte_size == 4;
4734 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4735 if (!use_mubuf) {
4736 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4737 } else {
4738 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4739 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4740 fetch_size = 4;
4741 expanded = true;
4742 }
4743 }
4744
4745 Temp fetch_index = index;
4746 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4747 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4748 fetch_offset = fetch_offset % attrib_stride;
4749 }
4750
4751 Operand soffset(0u);
4752 if (fetch_offset >= 4096) {
4753 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4754 fetch_offset %= 4096;
4755 }
4756
4757 aco_opcode opcode;
4758 switch (fetch_size) {
4759 case 1:
4760 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4761 break;
4762 case 2:
4763 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4764 break;
4765 case 3:
4766 assert(ctx->options->chip_class >= GFX7 ||
4767 (!use_mubuf && ctx->options->chip_class == GFX6));
4768 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4769 break;
4770 case 4:
4771 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4772 break;
4773 default:
4774 unreachable("Unimplemented load_input vector size");
4775 }
4776
4777 Temp fetch_dst;
4778 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4779 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4780 num_channels <= 3)) {
4781 direct_fetch = true;
4782 fetch_dst = dst;
4783 } else {
4784 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4785 }
4786
4787 if (use_mubuf) {
4788 Instruction *mubuf = bld.mubuf(opcode,
4789 Definition(fetch_dst), list, fetch_index, soffset,
4790 fetch_offset, false, true).instr;
4791 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4792 } else {
4793 Instruction *mtbuf = bld.mtbuf(opcode,
4794 Definition(fetch_dst), list, fetch_index, soffset,
4795 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4796 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4797 }
4798
4799 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4800
4801 if (fetch_size == 1) {
4802 channels[channel_start] = fetch_dst;
4803 } else {
4804 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4805 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4806 }
4807
4808 channel_start += fetch_size;
4809 }
4810
4811 if (!direct_fetch) {
4812 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4813 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4814
4815 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4816 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4817 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4818
4819 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4820 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4821 unsigned num_temp = 0;
4822 for (unsigned i = 0; i < dst.size(); i++) {
4823 unsigned idx = i + component;
4824 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4825 Temp channel = channels[swizzle[idx]];
4826 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4827 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4828 vec->operands[i] = Operand(channel);
4829
4830 num_temp++;
4831 elems[i] = channel;
4832 } else if (is_float && idx == 3) {
4833 vec->operands[i] = Operand(0x3f800000u);
4834 } else if (!is_float && idx == 3) {
4835 vec->operands[i] = Operand(1u);
4836 } else {
4837 vec->operands[i] = Operand(0u);
4838 }
4839 }
4840 vec->definitions[0] = Definition(dst);
4841 ctx->block->instructions.emplace_back(std::move(vec));
4842 emit_split_vector(ctx, dst, dst.size());
4843
4844 if (num_temp == dst.size())
4845 ctx->allocated_vec.emplace(dst.id(), elems);
4846 }
4847 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4848 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4849 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4850 if (off_instr->type != nir_instr_type_load_const ||
4851 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4852 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4853 nir_print_instr(off_instr, stderr);
4854 fprintf(stderr, "\n");
4855 }
4856
4857 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4858 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4859 if (offset) {
4860 assert(offset->u32 == 0);
4861 } else {
4862 /* the lower 15bit of the prim_mask contain the offset into LDS
4863 * while the upper bits contain the number of prims */
4864 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4865 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4866 Builder bld(ctx->program, ctx->block);
4867 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4868 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4869 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4870 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4871 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4872 }
4873
4874 unsigned idx = nir_intrinsic_base(instr);
4875 unsigned component = nir_intrinsic_component(instr);
4876 unsigned vertex_id = 2; /* P0 */
4877
4878 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4879 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4880 switch (src0->u32) {
4881 case 0:
4882 vertex_id = 2; /* P0 */
4883 break;
4884 case 1:
4885 vertex_id = 0; /* P10 */
4886 break;
4887 case 2:
4888 vertex_id = 1; /* P20 */
4889 break;
4890 default:
4891 unreachable("invalid vertex index");
4892 }
4893 }
4894
4895 if (dst.size() == 1) {
4896 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4897 } else {
4898 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4899 for (unsigned i = 0; i < dst.size(); i++)
4900 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4901 vec->definitions[0] = Definition(dst);
4902 bld.insert(std::move(vec));
4903 }
4904
4905 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4906 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4907 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4908 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4909 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4910
4911 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4912 } else {
4913 unreachable("Shader stage not implemented");
4914 }
4915 }
4916
4917 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4918 {
4919 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4920
4921 Builder bld(ctx->program, ctx->block);
4922 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4923 Temp vertex_offset;
4924
4925 if (!nir_src_is_const(*vertex_src)) {
4926 /* better code could be created, but this case probably doesn't happen
4927 * much in practice */
4928 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4929 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4930 Temp elem;
4931
4932 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4933 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4934 if (i % 2u)
4935 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4936 } else {
4937 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4938 }
4939
4940 if (vertex_offset.id()) {
4941 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4942 Operand(i), indirect_vertex);
4943 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4944 } else {
4945 vertex_offset = elem;
4946 }
4947 }
4948
4949 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4950 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4951 } else {
4952 unsigned vertex = nir_src_as_uint(*vertex_src);
4953 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4954 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4955 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4956 Operand((vertex % 2u) * 16u), Operand(16u));
4957 else
4958 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4959 }
4960
4961 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4962 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4963 return offset_mul(ctx, offs, 4u);
4964 }
4965
4966 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4967 {
4968 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4969
4970 Builder bld(ctx->program, ctx->block);
4971 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4972 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4973
4974 if (ctx->stage == geometry_gs) {
4975 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4976 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4977 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);
4978 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4979 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4980 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4981 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4982 } else {
4983 unreachable("Unsupported GS stage.");
4984 }
4985 }
4986
4987 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4988 {
4989 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4990
4991 Builder bld(ctx->program, ctx->block);
4992 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4993
4994 if (load_input_from_temps(ctx, instr, dst))
4995 return;
4996
4997 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4998 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4999 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5000
5001 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5002 }
5003
5004 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5005 {
5006 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5007
5008 Builder bld(ctx->program, ctx->block);
5009
5010 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5011 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
5012 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5013
5014 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5015 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5016
5017 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5018 }
5019
5020 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5021 {
5022 switch (ctx->shader->info.stage) {
5023 case MESA_SHADER_GEOMETRY:
5024 visit_load_gs_per_vertex_input(ctx, instr);
5025 break;
5026 case MESA_SHADER_TESS_CTRL:
5027 visit_load_tcs_per_vertex_input(ctx, instr);
5028 break;
5029 case MESA_SHADER_TESS_EVAL:
5030 visit_load_tes_per_vertex_input(ctx, instr);
5031 break;
5032 default:
5033 unreachable("Unimplemented shader stage");
5034 }
5035 }
5036
5037 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5038 {
5039 visit_load_tcs_output(ctx, instr, true);
5040 }
5041
5042 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5043 {
5044 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5045 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5046
5047 visit_store_tcs_output(ctx, instr, true);
5048 }
5049
5050 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5051 {
5052 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5053
5054 Builder bld(ctx->program, ctx->block);
5055 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5056
5057 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5058 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5059 Operand tes_w(0u);
5060
5061 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5062 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5063 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5064 tes_w = Operand(tmp);
5065 }
5066
5067 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5068 emit_split_vector(ctx, tess_coord, 3);
5069 }
5070
5071 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5072 {
5073 if (ctx->program->info->need_indirect_descriptor_sets) {
5074 Builder bld(ctx->program, ctx->block);
5075 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5076 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5077 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5078 }
5079
5080 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5081 }
5082
5083
5084 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5085 {
5086 Builder bld(ctx->program, ctx->block);
5087 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5088 if (!ctx->divergent_vals[instr->dest.ssa.index])
5089 index = bld.as_uniform(index);
5090 unsigned desc_set = nir_intrinsic_desc_set(instr);
5091 unsigned binding = nir_intrinsic_binding(instr);
5092
5093 Temp desc_ptr;
5094 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5095 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5096 unsigned offset = layout->binding[binding].offset;
5097 unsigned stride;
5098 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5099 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5100 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5101 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5102 offset = pipeline_layout->push_constant_size + 16 * idx;
5103 stride = 16;
5104 } else {
5105 desc_ptr = load_desc_ptr(ctx, desc_set);
5106 stride = layout->binding[binding].size;
5107 }
5108
5109 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5110 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5111 if (stride != 1) {
5112 if (nir_const_index) {
5113 const_index = const_index * stride;
5114 } else if (index.type() == RegType::vgpr) {
5115 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5116 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5117 } else {
5118 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5119 }
5120 }
5121 if (offset) {
5122 if (nir_const_index) {
5123 const_index = const_index + offset;
5124 } else if (index.type() == RegType::vgpr) {
5125 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5126 } else {
5127 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5128 }
5129 }
5130
5131 if (nir_const_index && const_index == 0) {
5132 index = desc_ptr;
5133 } else if (index.type() == RegType::vgpr) {
5134 index = bld.vadd32(bld.def(v1),
5135 nir_const_index ? Operand(const_index) : Operand(index),
5136 Operand(desc_ptr));
5137 } else {
5138 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5139 nir_const_index ? Operand(const_index) : Operand(index),
5140 Operand(desc_ptr));
5141 }
5142
5143 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5144 }
5145
5146 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5147 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5148 bool glc=false, bool readonly=true)
5149 {
5150 Builder bld(ctx->program, ctx->block);
5151
5152 bool use_smem = dst.type() != RegType::vgpr && ((ctx->options->chip_class >= GFX8 && component_size >= 4) || readonly);
5153 if (use_smem)
5154 offset = bld.as_uniform(offset);
5155
5156 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5157 info.glc = glc;
5158 info.barrier = readonly ? barrier_none : barrier_buffer;
5159 info.can_reorder = readonly;
5160 info.align_mul = align_mul;
5161 info.align_offset = align_offset;
5162 if (use_smem)
5163 emit_smem_load(ctx, bld, &info);
5164 else
5165 emit_mubuf_load(ctx, bld, &info);
5166 }
5167
5168 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5169 {
5170 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5171 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5172
5173 Builder bld(ctx->program, ctx->block);
5174
5175 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5176 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5177 unsigned binding = nir_intrinsic_binding(idx_instr);
5178 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5179
5180 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5181 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5182 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5183 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5184 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5185 if (ctx->options->chip_class >= GFX10) {
5186 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5187 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5188 S_008F0C_RESOURCE_LEVEL(1);
5189 } else {
5190 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5191 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5192 }
5193 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5194 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5195 Operand(0xFFFFFFFFu),
5196 Operand(desc_type));
5197 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5198 rsrc, upper_dwords);
5199 } else {
5200 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5201 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5202 }
5203 unsigned size = instr->dest.ssa.bit_size / 8;
5204 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5205 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5206 }
5207
5208 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5209 {
5210 Builder bld(ctx->program, ctx->block);
5211 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5212 unsigned offset = nir_intrinsic_base(instr);
5213 unsigned count = instr->dest.ssa.num_components;
5214 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5215
5216 if (index_cv && instr->dest.ssa.bit_size == 32) {
5217 unsigned start = (offset + index_cv->u32) / 4u;
5218 start -= ctx->args->ac.base_inline_push_consts;
5219 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5220 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5221 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5222 for (unsigned i = 0; i < count; ++i) {
5223 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5224 vec->operands[i] = Operand{elems[i]};
5225 }
5226 vec->definitions[0] = Definition(dst);
5227 ctx->block->instructions.emplace_back(std::move(vec));
5228 ctx->allocated_vec.emplace(dst.id(), elems);
5229 return;
5230 }
5231 }
5232
5233 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5234 if (offset != 0) // TODO check if index != 0 as well
5235 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5236 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5237 Temp vec = dst;
5238 bool trim = false;
5239 bool aligned = true;
5240
5241 if (instr->dest.ssa.bit_size == 8) {
5242 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5243 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5244 if (!aligned)
5245 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5246 } else if (instr->dest.ssa.bit_size == 16) {
5247 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5248 if (!aligned)
5249 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5250 }
5251
5252 aco_opcode op;
5253
5254 switch (vec.size()) {
5255 case 1:
5256 op = aco_opcode::s_load_dword;
5257 break;
5258 case 2:
5259 op = aco_opcode::s_load_dwordx2;
5260 break;
5261 case 3:
5262 vec = bld.tmp(s4);
5263 trim = true;
5264 case 4:
5265 op = aco_opcode::s_load_dwordx4;
5266 break;
5267 case 6:
5268 vec = bld.tmp(s8);
5269 trim = true;
5270 case 8:
5271 op = aco_opcode::s_load_dwordx8;
5272 break;
5273 default:
5274 unreachable("unimplemented or forbidden load_push_constant.");
5275 }
5276
5277 bld.smem(op, Definition(vec), ptr, index);
5278
5279 if (!aligned) {
5280 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5281 byte_align_scalar(ctx, vec, byte_offset, dst);
5282 return;
5283 }
5284
5285 if (trim) {
5286 emit_split_vector(ctx, vec, 4);
5287 RegClass rc = dst.size() == 3 ? s1 : s2;
5288 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5289 emit_extract_vector(ctx, vec, 0, rc),
5290 emit_extract_vector(ctx, vec, 1, rc),
5291 emit_extract_vector(ctx, vec, 2, rc));
5292
5293 }
5294 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5295 }
5296
5297 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5298 {
5299 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5300
5301 Builder bld(ctx->program, ctx->block);
5302
5303 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5304 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5305 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5306 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5307 if (ctx->options->chip_class >= GFX10) {
5308 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5309 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5310 S_008F0C_RESOURCE_LEVEL(1);
5311 } else {
5312 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5313 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5314 }
5315
5316 unsigned base = nir_intrinsic_base(instr);
5317 unsigned range = nir_intrinsic_range(instr);
5318
5319 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5320 if (base && offset.type() == RegType::sgpr)
5321 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5322 else if (base && offset.type() == RegType::vgpr)
5323 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5324
5325 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5326 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5327 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5328 Operand(desc_type));
5329 unsigned size = instr->dest.ssa.bit_size / 8;
5330 // TODO: get alignment information for subdword constants
5331 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5332 }
5333
5334 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5335 {
5336 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5337 ctx->cf_info.exec_potentially_empty_discard = true;
5338
5339 ctx->program->needs_exact = true;
5340
5341 // TODO: optimize uniform conditions
5342 Builder bld(ctx->program, ctx->block);
5343 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5344 assert(src.regClass() == bld.lm);
5345 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5346 bld.pseudo(aco_opcode::p_discard_if, src);
5347 ctx->block->kind |= block_kind_uses_discard_if;
5348 return;
5349 }
5350
5351 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5352 {
5353 Builder bld(ctx->program, ctx->block);
5354
5355 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5356 ctx->cf_info.exec_potentially_empty_discard = true;
5357
5358 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5359 ctx->cf_info.parent_loop.has_divergent_continue;
5360
5361 if (ctx->block->loop_nest_depth &&
5362 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5363 /* we handle discards the same way as jump instructions */
5364 append_logical_end(ctx->block);
5365
5366 /* in loops, discard behaves like break */
5367 Block *linear_target = ctx->cf_info.parent_loop.exit;
5368 ctx->block->kind |= block_kind_discard;
5369
5370 if (!divergent) {
5371 /* uniform discard - loop ends here */
5372 assert(nir_instr_is_last(&instr->instr));
5373 ctx->block->kind |= block_kind_uniform;
5374 ctx->cf_info.has_branch = true;
5375 bld.branch(aco_opcode::p_branch);
5376 add_linear_edge(ctx->block->index, linear_target);
5377 return;
5378 }
5379
5380 /* we add a break right behind the discard() instructions */
5381 ctx->block->kind |= block_kind_break;
5382 unsigned idx = ctx->block->index;
5383
5384 ctx->cf_info.parent_loop.has_divergent_branch = true;
5385 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5386
5387 /* remove critical edges from linear CFG */
5388 bld.branch(aco_opcode::p_branch);
5389 Block* break_block = ctx->program->create_and_insert_block();
5390 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5391 break_block->kind |= block_kind_uniform;
5392 add_linear_edge(idx, break_block);
5393 add_linear_edge(break_block->index, linear_target);
5394 bld.reset(break_block);
5395 bld.branch(aco_opcode::p_branch);
5396
5397 Block* continue_block = ctx->program->create_and_insert_block();
5398 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5399 add_linear_edge(idx, continue_block);
5400 append_logical_start(continue_block);
5401 ctx->block = continue_block;
5402
5403 return;
5404 }
5405
5406 /* it can currently happen that NIR doesn't remove the unreachable code */
5407 if (!nir_instr_is_last(&instr->instr)) {
5408 ctx->program->needs_exact = true;
5409 /* save exec somewhere temporarily so that it doesn't get
5410 * overwritten before the discard from outer exec masks */
5411 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5412 bld.pseudo(aco_opcode::p_discard_if, cond);
5413 ctx->block->kind |= block_kind_uses_discard_if;
5414 return;
5415 }
5416
5417 /* This condition is incorrect for uniformly branched discards in a loop
5418 * predicated by a divergent condition, but the above code catches that case
5419 * and the discard would end up turning into a discard_if.
5420 * For example:
5421 * if (divergent) {
5422 * while (...) {
5423 * if (uniform) {
5424 * discard;
5425 * }
5426 * }
5427 * }
5428 */
5429 if (!ctx->cf_info.parent_if.is_divergent) {
5430 /* program just ends here */
5431 ctx->block->kind |= block_kind_uniform;
5432 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5433 0 /* enabled mask */, 9 /* dest */,
5434 false /* compressed */, true/* done */, true /* valid mask */);
5435 bld.sopp(aco_opcode::s_endpgm);
5436 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5437 } else {
5438 ctx->block->kind |= block_kind_discard;
5439 /* branch and linear edge is added by visit_if() */
5440 }
5441 }
5442
5443 enum aco_descriptor_type {
5444 ACO_DESC_IMAGE,
5445 ACO_DESC_FMASK,
5446 ACO_DESC_SAMPLER,
5447 ACO_DESC_BUFFER,
5448 ACO_DESC_PLANE_0,
5449 ACO_DESC_PLANE_1,
5450 ACO_DESC_PLANE_2,
5451 };
5452
5453 static bool
5454 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5455 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5456 return false;
5457 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5458 return dim == ac_image_cube ||
5459 dim == ac_image_1darray ||
5460 dim == ac_image_2darray ||
5461 dim == ac_image_2darraymsaa;
5462 }
5463
5464 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5465 enum aco_descriptor_type desc_type,
5466 const nir_tex_instr *tex_instr, bool image, bool write)
5467 {
5468 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5469 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5470 if (it != ctx->tex_desc.end())
5471 return it->second;
5472 */
5473 Temp index = Temp();
5474 bool index_set = false;
5475 unsigned constant_index = 0;
5476 unsigned descriptor_set;
5477 unsigned base_index;
5478 Builder bld(ctx->program, ctx->block);
5479
5480 if (!deref_instr) {
5481 assert(tex_instr && !image);
5482 descriptor_set = 0;
5483 base_index = tex_instr->sampler_index;
5484 } else {
5485 while(deref_instr->deref_type != nir_deref_type_var) {
5486 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5487 if (!array_size)
5488 array_size = 1;
5489
5490 assert(deref_instr->deref_type == nir_deref_type_array);
5491 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5492 if (const_value) {
5493 constant_index += array_size * const_value->u32;
5494 } else {
5495 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5496 if (indirect.type() == RegType::vgpr)
5497 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5498
5499 if (array_size != 1)
5500 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5501
5502 if (!index_set) {
5503 index = indirect;
5504 index_set = true;
5505 } else {
5506 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5507 }
5508 }
5509
5510 deref_instr = nir_src_as_deref(deref_instr->parent);
5511 }
5512 descriptor_set = deref_instr->var->data.descriptor_set;
5513 base_index = deref_instr->var->data.binding;
5514 }
5515
5516 Temp list = load_desc_ptr(ctx, descriptor_set);
5517 list = convert_pointer_to_64_bit(ctx, list);
5518
5519 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5520 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5521 unsigned offset = binding->offset;
5522 unsigned stride = binding->size;
5523 aco_opcode opcode;
5524 RegClass type;
5525
5526 assert(base_index < layout->binding_count);
5527
5528 switch (desc_type) {
5529 case ACO_DESC_IMAGE:
5530 type = s8;
5531 opcode = aco_opcode::s_load_dwordx8;
5532 break;
5533 case ACO_DESC_FMASK:
5534 type = s8;
5535 opcode = aco_opcode::s_load_dwordx8;
5536 offset += 32;
5537 break;
5538 case ACO_DESC_SAMPLER:
5539 type = s4;
5540 opcode = aco_opcode::s_load_dwordx4;
5541 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5542 offset += radv_combined_image_descriptor_sampler_offset(binding);
5543 break;
5544 case ACO_DESC_BUFFER:
5545 type = s4;
5546 opcode = aco_opcode::s_load_dwordx4;
5547 break;
5548 case ACO_DESC_PLANE_0:
5549 case ACO_DESC_PLANE_1:
5550 type = s8;
5551 opcode = aco_opcode::s_load_dwordx8;
5552 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5553 break;
5554 case ACO_DESC_PLANE_2:
5555 type = s4;
5556 opcode = aco_opcode::s_load_dwordx4;
5557 offset += 64;
5558 break;
5559 default:
5560 unreachable("invalid desc_type\n");
5561 }
5562
5563 offset += constant_index * stride;
5564
5565 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5566 (!index_set || binding->immutable_samplers_equal)) {
5567 if (binding->immutable_samplers_equal)
5568 constant_index = 0;
5569
5570 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5571 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5572 Operand(samplers[constant_index * 4 + 0]),
5573 Operand(samplers[constant_index * 4 + 1]),
5574 Operand(samplers[constant_index * 4 + 2]),
5575 Operand(samplers[constant_index * 4 + 3]));
5576 }
5577
5578 Operand off;
5579 if (!index_set) {
5580 off = bld.copy(bld.def(s1), Operand(offset));
5581 } else {
5582 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5583 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5584 }
5585
5586 Temp res = bld.smem(opcode, bld.def(type), list, off);
5587
5588 if (desc_type == ACO_DESC_PLANE_2) {
5589 Temp components[8];
5590 for (unsigned i = 0; i < 8; i++)
5591 components[i] = bld.tmp(s1);
5592 bld.pseudo(aco_opcode::p_split_vector,
5593 Definition(components[0]),
5594 Definition(components[1]),
5595 Definition(components[2]),
5596 Definition(components[3]),
5597 res);
5598
5599 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5600 bld.pseudo(aco_opcode::p_split_vector,
5601 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5602 Definition(components[4]),
5603 Definition(components[5]),
5604 Definition(components[6]),
5605 Definition(components[7]),
5606 desc2);
5607
5608 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5609 components[0], components[1], components[2], components[3],
5610 components[4], components[5], components[6], components[7]);
5611 }
5612
5613 return res;
5614 }
5615
5616 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5617 {
5618 switch (dim) {
5619 case GLSL_SAMPLER_DIM_BUF:
5620 return 1;
5621 case GLSL_SAMPLER_DIM_1D:
5622 return array ? 2 : 1;
5623 case GLSL_SAMPLER_DIM_2D:
5624 return array ? 3 : 2;
5625 case GLSL_SAMPLER_DIM_MS:
5626 return array ? 4 : 3;
5627 case GLSL_SAMPLER_DIM_3D:
5628 case GLSL_SAMPLER_DIM_CUBE:
5629 return 3;
5630 case GLSL_SAMPLER_DIM_RECT:
5631 case GLSL_SAMPLER_DIM_SUBPASS:
5632 return 2;
5633 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5634 return 3;
5635 default:
5636 break;
5637 }
5638 return 0;
5639 }
5640
5641
5642 /* Adjust the sample index according to FMASK.
5643 *
5644 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5645 * which is the identity mapping. Each nibble says which physical sample
5646 * should be fetched to get that sample.
5647 *
5648 * For example, 0x11111100 means there are only 2 samples stored and
5649 * the second sample covers 3/4 of the pixel. When reading samples 0
5650 * and 1, return physical sample 0 (determined by the first two 0s
5651 * in FMASK), otherwise return physical sample 1.
5652 *
5653 * The sample index should be adjusted as follows:
5654 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5655 */
5656 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5657 {
5658 Builder bld(ctx->program, ctx->block);
5659 Temp fmask = bld.tmp(v1);
5660 unsigned dim = ctx->options->chip_class >= GFX10
5661 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5662 : 0;
5663
5664 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5665 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5666 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5667 load->operands[0] = Operand(fmask_desc_ptr);
5668 load->operands[1] = Operand(s4); /* no sampler */
5669 load->operands[2] = Operand(coord);
5670 load->definitions[0] = Definition(fmask);
5671 load->glc = false;
5672 load->dlc = false;
5673 load->dmask = 0x1;
5674 load->unrm = true;
5675 load->da = da;
5676 load->dim = dim;
5677 load->can_reorder = true; /* fmask images shouldn't be modified */
5678 ctx->block->instructions.emplace_back(std::move(load));
5679
5680 Operand sample_index4;
5681 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
5682 sample_index4 = Operand(sample_index.constantValue() << 2);
5683 } else if (sample_index.regClass() == s1) {
5684 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5685 } else {
5686 assert(sample_index.regClass() == v1);
5687 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5688 }
5689
5690 Temp final_sample;
5691 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5692 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5693 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5694 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5695 else
5696 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5697
5698 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5699 * resource descriptor is 0 (invalid),
5700 */
5701 Temp compare = bld.tmp(bld.lm);
5702 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5703 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5704
5705 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5706
5707 /* Replace the MSAA sample index. */
5708 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5709 }
5710
5711 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5712 {
5713
5714 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5715 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5716 bool is_array = glsl_sampler_type_is_array(type);
5717 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5718 assert(!add_frag_pos && "Input attachments should be lowered.");
5719 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5720 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5721 int count = image_type_to_components_count(dim, is_array);
5722 std::vector<Temp> coords(count);
5723 Builder bld(ctx->program, ctx->block);
5724
5725 if (is_ms) {
5726 count--;
5727 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5728 /* get sample index */
5729 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5730 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5731 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5732 std::vector<Temp> fmask_load_address;
5733 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5734 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5735
5736 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5737 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5738 } else {
5739 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5740 }
5741 }
5742
5743 if (gfx9_1d) {
5744 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5745 coords.resize(coords.size() + 1);
5746 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5747 if (is_array)
5748 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5749 } else {
5750 for (int i = 0; i < count; i++)
5751 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5752 }
5753
5754 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5755 instr->intrinsic == nir_intrinsic_image_deref_store) {
5756 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5757 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5758
5759 if (!level_zero)
5760 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5761 }
5762
5763 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5764 for (unsigned i = 0; i < coords.size(); i++)
5765 vec->operands[i] = Operand(coords[i]);
5766 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5767 vec->definitions[0] = Definition(res);
5768 ctx->block->instructions.emplace_back(std::move(vec));
5769 return res;
5770 }
5771
5772
5773 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5774 {
5775 Builder bld(ctx->program, ctx->block);
5776 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5777 const struct glsl_type *type = glsl_without_array(var->type);
5778 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5779 bool is_array = glsl_sampler_type_is_array(type);
5780 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5781
5782 if (dim == GLSL_SAMPLER_DIM_BUF) {
5783 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5784 unsigned num_channels = util_last_bit(mask);
5785 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5786 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5787
5788 aco_opcode opcode;
5789 switch (num_channels) {
5790 case 1:
5791 opcode = aco_opcode::buffer_load_format_x;
5792 break;
5793 case 2:
5794 opcode = aco_opcode::buffer_load_format_xy;
5795 break;
5796 case 3:
5797 opcode = aco_opcode::buffer_load_format_xyz;
5798 break;
5799 case 4:
5800 opcode = aco_opcode::buffer_load_format_xyzw;
5801 break;
5802 default:
5803 unreachable(">4 channel buffer image load");
5804 }
5805 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5806 load->operands[0] = Operand(rsrc);
5807 load->operands[1] = Operand(vindex);
5808 load->operands[2] = Operand((uint32_t) 0);
5809 Temp tmp;
5810 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5811 tmp = dst;
5812 else
5813 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5814 load->definitions[0] = Definition(tmp);
5815 load->idxen = true;
5816 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5817 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5818 load->barrier = barrier_image;
5819 ctx->block->instructions.emplace_back(std::move(load));
5820
5821 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5822 return;
5823 }
5824
5825 Temp coords = get_image_coords(ctx, instr, type);
5826 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5827
5828 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5829 unsigned num_components = util_bitcount(dmask);
5830 Temp tmp;
5831 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5832 tmp = dst;
5833 else
5834 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5835
5836 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5837 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5838
5839 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5840 load->operands[0] = Operand(resource);
5841 load->operands[1] = Operand(s4); /* no sampler */
5842 load->operands[2] = Operand(coords);
5843 load->definitions[0] = Definition(tmp);
5844 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5845 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5846 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5847 load->dmask = dmask;
5848 load->unrm = true;
5849 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5850 load->barrier = barrier_image;
5851 ctx->block->instructions.emplace_back(std::move(load));
5852
5853 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5854 return;
5855 }
5856
5857 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5858 {
5859 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5860 const struct glsl_type *type = glsl_without_array(var->type);
5861 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5862 bool is_array = glsl_sampler_type_is_array(type);
5863 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5864
5865 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5866
5867 if (dim == GLSL_SAMPLER_DIM_BUF) {
5868 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5869 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5870 aco_opcode opcode;
5871 switch (data.size()) {
5872 case 1:
5873 opcode = aco_opcode::buffer_store_format_x;
5874 break;
5875 case 2:
5876 opcode = aco_opcode::buffer_store_format_xy;
5877 break;
5878 case 3:
5879 opcode = aco_opcode::buffer_store_format_xyz;
5880 break;
5881 case 4:
5882 opcode = aco_opcode::buffer_store_format_xyzw;
5883 break;
5884 default:
5885 unreachable(">4 channel buffer image store");
5886 }
5887 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5888 store->operands[0] = Operand(rsrc);
5889 store->operands[1] = Operand(vindex);
5890 store->operands[2] = Operand((uint32_t) 0);
5891 store->operands[3] = Operand(data);
5892 store->idxen = true;
5893 store->glc = glc;
5894 store->dlc = false;
5895 store->disable_wqm = true;
5896 store->barrier = barrier_image;
5897 ctx->program->needs_exact = true;
5898 ctx->block->instructions.emplace_back(std::move(store));
5899 return;
5900 }
5901
5902 assert(data.type() == RegType::vgpr);
5903 Temp coords = get_image_coords(ctx, instr, type);
5904 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5905
5906 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5907 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5908
5909 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5910 store->operands[0] = Operand(resource);
5911 store->operands[1] = Operand(data);
5912 store->operands[2] = Operand(coords);
5913 store->glc = glc;
5914 store->dlc = false;
5915 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5916 store->dmask = (1 << data.size()) - 1;
5917 store->unrm = true;
5918 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5919 store->disable_wqm = true;
5920 store->barrier = barrier_image;
5921 ctx->program->needs_exact = true;
5922 ctx->block->instructions.emplace_back(std::move(store));
5923 return;
5924 }
5925
5926 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5927 {
5928 /* return the previous value if dest is ever used */
5929 bool return_previous = false;
5930 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5931 return_previous = true;
5932 break;
5933 }
5934 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5935 return_previous = true;
5936 break;
5937 }
5938
5939 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5940 const struct glsl_type *type = glsl_without_array(var->type);
5941 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5942 bool is_array = glsl_sampler_type_is_array(type);
5943 Builder bld(ctx->program, ctx->block);
5944
5945 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5946 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5947
5948 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5949 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5950
5951 aco_opcode buf_op, image_op;
5952 switch (instr->intrinsic) {
5953 case nir_intrinsic_image_deref_atomic_add:
5954 buf_op = aco_opcode::buffer_atomic_add;
5955 image_op = aco_opcode::image_atomic_add;
5956 break;
5957 case nir_intrinsic_image_deref_atomic_umin:
5958 buf_op = aco_opcode::buffer_atomic_umin;
5959 image_op = aco_opcode::image_atomic_umin;
5960 break;
5961 case nir_intrinsic_image_deref_atomic_imin:
5962 buf_op = aco_opcode::buffer_atomic_smin;
5963 image_op = aco_opcode::image_atomic_smin;
5964 break;
5965 case nir_intrinsic_image_deref_atomic_umax:
5966 buf_op = aco_opcode::buffer_atomic_umax;
5967 image_op = aco_opcode::image_atomic_umax;
5968 break;
5969 case nir_intrinsic_image_deref_atomic_imax:
5970 buf_op = aco_opcode::buffer_atomic_smax;
5971 image_op = aco_opcode::image_atomic_smax;
5972 break;
5973 case nir_intrinsic_image_deref_atomic_and:
5974 buf_op = aco_opcode::buffer_atomic_and;
5975 image_op = aco_opcode::image_atomic_and;
5976 break;
5977 case nir_intrinsic_image_deref_atomic_or:
5978 buf_op = aco_opcode::buffer_atomic_or;
5979 image_op = aco_opcode::image_atomic_or;
5980 break;
5981 case nir_intrinsic_image_deref_atomic_xor:
5982 buf_op = aco_opcode::buffer_atomic_xor;
5983 image_op = aco_opcode::image_atomic_xor;
5984 break;
5985 case nir_intrinsic_image_deref_atomic_exchange:
5986 buf_op = aco_opcode::buffer_atomic_swap;
5987 image_op = aco_opcode::image_atomic_swap;
5988 break;
5989 case nir_intrinsic_image_deref_atomic_comp_swap:
5990 buf_op = aco_opcode::buffer_atomic_cmpswap;
5991 image_op = aco_opcode::image_atomic_cmpswap;
5992 break;
5993 default:
5994 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5995 }
5996
5997 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5998
5999 if (dim == GLSL_SAMPLER_DIM_BUF) {
6000 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
6001 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
6002 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6003 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6004 mubuf->operands[0] = Operand(resource);
6005 mubuf->operands[1] = Operand(vindex);
6006 mubuf->operands[2] = Operand((uint32_t)0);
6007 mubuf->operands[3] = Operand(data);
6008 if (return_previous)
6009 mubuf->definitions[0] = Definition(dst);
6010 mubuf->offset = 0;
6011 mubuf->idxen = true;
6012 mubuf->glc = return_previous;
6013 mubuf->dlc = false; /* Not needed for atomics */
6014 mubuf->disable_wqm = true;
6015 mubuf->barrier = barrier_image;
6016 ctx->program->needs_exact = true;
6017 ctx->block->instructions.emplace_back(std::move(mubuf));
6018 return;
6019 }
6020
6021 Temp coords = get_image_coords(ctx, instr, type);
6022 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6023 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6024 mimg->operands[0] = Operand(resource);
6025 mimg->operands[1] = Operand(data);
6026 mimg->operands[2] = Operand(coords);
6027 if (return_previous)
6028 mimg->definitions[0] = Definition(dst);
6029 mimg->glc = return_previous;
6030 mimg->dlc = false; /* Not needed for atomics */
6031 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6032 mimg->dmask = (1 << data.size()) - 1;
6033 mimg->unrm = true;
6034 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6035 mimg->disable_wqm = true;
6036 mimg->barrier = barrier_image;
6037 ctx->program->needs_exact = true;
6038 ctx->block->instructions.emplace_back(std::move(mimg));
6039 return;
6040 }
6041
6042 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6043 {
6044 if (in_elements && ctx->options->chip_class == GFX8) {
6045 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6046 Builder bld(ctx->program, ctx->block);
6047
6048 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6049
6050 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6051 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6052
6053 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6054 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6055
6056 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6057 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6058
6059 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6060 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6061 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6062 if (dst.type() == RegType::vgpr)
6063 bld.copy(Definition(dst), shr_dst);
6064
6065 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6066 } else {
6067 emit_extract_vector(ctx, desc, 2, dst);
6068 }
6069 }
6070
6071 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6072 {
6073 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6074 const struct glsl_type *type = glsl_without_array(var->type);
6075 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6076 bool is_array = glsl_sampler_type_is_array(type);
6077 Builder bld(ctx->program, ctx->block);
6078
6079 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6080 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6081 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6082 }
6083
6084 /* LOD */
6085 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6086
6087 /* Resource */
6088 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6089
6090 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6091
6092 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6093 mimg->operands[0] = Operand(resource);
6094 mimg->operands[1] = Operand(s4); /* no sampler */
6095 mimg->operands[2] = Operand(lod);
6096 uint8_t& dmask = mimg->dmask;
6097 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6098 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6099 mimg->da = glsl_sampler_type_is_array(type);
6100 mimg->can_reorder = true;
6101 Definition& def = mimg->definitions[0];
6102 ctx->block->instructions.emplace_back(std::move(mimg));
6103
6104 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6105 glsl_sampler_type_is_array(type)) {
6106
6107 assert(instr->dest.ssa.num_components == 3);
6108 Temp tmp = {ctx->program->allocateId(), v3};
6109 def = Definition(tmp);
6110 emit_split_vector(ctx, tmp, 3);
6111
6112 /* divide 3rd value by 6 by multiplying with magic number */
6113 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6114 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6115
6116 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6117 emit_extract_vector(ctx, tmp, 0, v1),
6118 emit_extract_vector(ctx, tmp, 1, v1),
6119 by_6);
6120
6121 } else if (ctx->options->chip_class == GFX9 &&
6122 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6123 glsl_sampler_type_is_array(type)) {
6124 assert(instr->dest.ssa.num_components == 2);
6125 def = Definition(dst);
6126 dmask = 0x5;
6127 } else {
6128 def = Definition(dst);
6129 }
6130
6131 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6132 }
6133
6134 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6135 {
6136 Builder bld(ctx->program, ctx->block);
6137 unsigned num_components = instr->num_components;
6138
6139 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6140 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6141 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6142
6143 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6144 unsigned size = instr->dest.ssa.bit_size / 8;
6145 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6146 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false);
6147 }
6148
6149 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6150 {
6151 Builder bld(ctx->program, ctx->block);
6152 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6153 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6154 unsigned writemask = nir_intrinsic_write_mask(instr);
6155 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6156
6157 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6158 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6159
6160 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
6161 ctx->options->chip_class >= GFX8 &&
6162 elem_size_bytes >= 4;
6163 if (smem)
6164 offset = bld.as_uniform(offset);
6165 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6166
6167 while (writemask) {
6168 int start, count;
6169 u_bit_scan_consecutive_range(&writemask, &start, &count);
6170 if (count == 3 && (smem || ctx->options->chip_class == GFX6)) {
6171 /* GFX6 doesn't support storing vec3, split it. */
6172 writemask |= 1u << (start + 2);
6173 count = 2;
6174 }
6175 int num_bytes = count * elem_size_bytes;
6176
6177 /* dword or larger stores have to be dword-aligned */
6178 if (elem_size_bytes < 4 && num_bytes > 2) {
6179 // TODO: improve alignment check of sub-dword stores
6180 unsigned count_new = 2 / elem_size_bytes;
6181 writemask |= ((1 << (count - count_new)) - 1) << (start + count_new);
6182 count = count_new;
6183 num_bytes = 2;
6184 }
6185
6186 if (num_bytes > 16) {
6187 assert(elem_size_bytes == 8);
6188 writemask |= (((count - 2) << 1) - 1) << (start + 2);
6189 count = 2;
6190 num_bytes = 16;
6191 }
6192
6193 Temp write_data;
6194 if (elem_size_bytes < 4) {
6195 if (data.type() == RegType::sgpr) {
6196 data = as_vgpr(ctx, data);
6197 emit_split_vector(ctx, data, 4 * data.size() / elem_size_bytes);
6198 }
6199 RegClass rc = RegClass(RegType::vgpr, elem_size_bytes).as_subdword();
6200 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6201 for (int i = 0; i < count; i++)
6202 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, rc));
6203 write_data = bld.tmp(RegClass(RegType::vgpr, num_bytes).as_subdword());
6204 vec->definitions[0] = Definition(write_data);
6205 bld.insert(std::move(vec));
6206 } else if (count != instr->num_components) {
6207 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6208 for (int i = 0; i < count; i++) {
6209 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
6210 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
6211 }
6212 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
6213 vec->definitions[0] = Definition(write_data);
6214 ctx->block->instructions.emplace_back(std::move(vec));
6215 } else if (!smem && data.type() != RegType::vgpr) {
6216 assert(num_bytes % 4 == 0);
6217 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
6218 } else if (smem_nonfs && data.type() == RegType::vgpr) {
6219 assert(num_bytes % 4 == 0);
6220 write_data = bld.as_uniform(data);
6221 } else {
6222 write_data = data;
6223 }
6224
6225 aco_opcode vmem_op, smem_op = aco_opcode::last_opcode;
6226 switch (num_bytes) {
6227 case 1:
6228 vmem_op = aco_opcode::buffer_store_byte;
6229 break;
6230 case 2:
6231 vmem_op = aco_opcode::buffer_store_short;
6232 break;
6233 case 4:
6234 vmem_op = aco_opcode::buffer_store_dword;
6235 smem_op = aco_opcode::s_buffer_store_dword;
6236 break;
6237 case 8:
6238 vmem_op = aco_opcode::buffer_store_dwordx2;
6239 smem_op = aco_opcode::s_buffer_store_dwordx2;
6240 break;
6241 case 12:
6242 vmem_op = aco_opcode::buffer_store_dwordx3;
6243 assert(!smem && ctx->options->chip_class > GFX6);
6244 break;
6245 case 16:
6246 vmem_op = aco_opcode::buffer_store_dwordx4;
6247 smem_op = aco_opcode::s_buffer_store_dwordx4;
6248 break;
6249 default:
6250 unreachable("Store SSBO not implemented for this size.");
6251 }
6252 if (ctx->stage == fragment_fs)
6253 smem_op = aco_opcode::p_fs_buffer_store_smem;
6254
6255 if (smem) {
6256 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
6257 store->operands[0] = Operand(rsrc);
6258 if (start) {
6259 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6260 offset, Operand(start * elem_size_bytes));
6261 store->operands[1] = Operand(off);
6262 } else {
6263 store->operands[1] = Operand(offset);
6264 }
6265 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
6266 store->operands[1].setFixed(m0);
6267 store->operands[2] = Operand(write_data);
6268 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6269 store->dlc = false;
6270 store->disable_wqm = true;
6271 store->barrier = barrier_buffer;
6272 ctx->block->instructions.emplace_back(std::move(store));
6273 ctx->program->wb_smem_l1_on_end = true;
6274 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
6275 ctx->block->kind |= block_kind_needs_lowering;
6276 ctx->program->needs_exact = true;
6277 }
6278 } else {
6279 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
6280 store->operands[0] = Operand(rsrc);
6281 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6282 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6283 store->operands[3] = Operand(write_data);
6284 store->offset = start * elem_size_bytes;
6285 store->offen = (offset.type() == RegType::vgpr);
6286 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6287 store->dlc = false;
6288 store->disable_wqm = true;
6289 store->barrier = barrier_buffer;
6290 ctx->program->needs_exact = true;
6291 ctx->block->instructions.emplace_back(std::move(store));
6292 }
6293 }
6294 }
6295
6296 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6297 {
6298 /* return the previous value if dest is ever used */
6299 bool return_previous = false;
6300 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6301 return_previous = true;
6302 break;
6303 }
6304 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6305 return_previous = true;
6306 break;
6307 }
6308
6309 Builder bld(ctx->program, ctx->block);
6310 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6311
6312 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6313 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6314 get_ssa_temp(ctx, instr->src[3].ssa), data);
6315
6316 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6317 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6318 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6319
6320 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6321
6322 aco_opcode op32, op64;
6323 switch (instr->intrinsic) {
6324 case nir_intrinsic_ssbo_atomic_add:
6325 op32 = aco_opcode::buffer_atomic_add;
6326 op64 = aco_opcode::buffer_atomic_add_x2;
6327 break;
6328 case nir_intrinsic_ssbo_atomic_imin:
6329 op32 = aco_opcode::buffer_atomic_smin;
6330 op64 = aco_opcode::buffer_atomic_smin_x2;
6331 break;
6332 case nir_intrinsic_ssbo_atomic_umin:
6333 op32 = aco_opcode::buffer_atomic_umin;
6334 op64 = aco_opcode::buffer_atomic_umin_x2;
6335 break;
6336 case nir_intrinsic_ssbo_atomic_imax:
6337 op32 = aco_opcode::buffer_atomic_smax;
6338 op64 = aco_opcode::buffer_atomic_smax_x2;
6339 break;
6340 case nir_intrinsic_ssbo_atomic_umax:
6341 op32 = aco_opcode::buffer_atomic_umax;
6342 op64 = aco_opcode::buffer_atomic_umax_x2;
6343 break;
6344 case nir_intrinsic_ssbo_atomic_and:
6345 op32 = aco_opcode::buffer_atomic_and;
6346 op64 = aco_opcode::buffer_atomic_and_x2;
6347 break;
6348 case nir_intrinsic_ssbo_atomic_or:
6349 op32 = aco_opcode::buffer_atomic_or;
6350 op64 = aco_opcode::buffer_atomic_or_x2;
6351 break;
6352 case nir_intrinsic_ssbo_atomic_xor:
6353 op32 = aco_opcode::buffer_atomic_xor;
6354 op64 = aco_opcode::buffer_atomic_xor_x2;
6355 break;
6356 case nir_intrinsic_ssbo_atomic_exchange:
6357 op32 = aco_opcode::buffer_atomic_swap;
6358 op64 = aco_opcode::buffer_atomic_swap_x2;
6359 break;
6360 case nir_intrinsic_ssbo_atomic_comp_swap:
6361 op32 = aco_opcode::buffer_atomic_cmpswap;
6362 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6363 break;
6364 default:
6365 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6366 }
6367 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6368 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6369 mubuf->operands[0] = Operand(rsrc);
6370 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6371 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6372 mubuf->operands[3] = Operand(data);
6373 if (return_previous)
6374 mubuf->definitions[0] = Definition(dst);
6375 mubuf->offset = 0;
6376 mubuf->offen = (offset.type() == RegType::vgpr);
6377 mubuf->glc = return_previous;
6378 mubuf->dlc = false; /* Not needed for atomics */
6379 mubuf->disable_wqm = true;
6380 mubuf->barrier = barrier_buffer;
6381 ctx->program->needs_exact = true;
6382 ctx->block->instructions.emplace_back(std::move(mubuf));
6383 }
6384
6385 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6386
6387 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6388 Builder bld(ctx->program, ctx->block);
6389 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6390 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6391 }
6392
6393 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6394 {
6395 Builder bld(ctx->program, ctx->block);
6396 unsigned num_components = instr->num_components;
6397 unsigned component_size = instr->dest.ssa.bit_size / 8;
6398
6399 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6400 get_ssa_temp(ctx, &instr->dest.ssa),
6401 num_components, component_size};
6402 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6403 info.align_mul = nir_intrinsic_align_mul(instr);
6404 info.align_offset = nir_intrinsic_align_offset(instr);
6405 info.barrier = barrier_buffer;
6406 info.can_reorder = false;
6407 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6408 * it's safe to use SMEM */
6409 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6410 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6411 emit_global_load(ctx, bld, &info);
6412 } else {
6413 info.offset = Operand(bld.as_uniform(info.offset));
6414 emit_smem_load(ctx, bld, &info);
6415 }
6416 }
6417
6418 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6419 {
6420 Builder bld(ctx->program, ctx->block);
6421 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6422
6423 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6424 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6425
6426 if (ctx->options->chip_class >= GFX7)
6427 addr = as_vgpr(ctx, addr);
6428
6429 unsigned writemask = nir_intrinsic_write_mask(instr);
6430 while (writemask) {
6431 int start, count;
6432 u_bit_scan_consecutive_range(&writemask, &start, &count);
6433 if (count == 3 && ctx->options->chip_class == GFX6) {
6434 /* GFX6 doesn't support storing vec3, split it. */
6435 writemask |= 1u << (start + 2);
6436 count = 2;
6437 }
6438 unsigned num_bytes = count * elem_size_bytes;
6439
6440 Temp write_data = data;
6441 if (count != instr->num_components) {
6442 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6443 for (int i = 0; i < count; i++)
6444 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
6445 write_data = bld.tmp(RegType::vgpr, count);
6446 vec->definitions[0] = Definition(write_data);
6447 ctx->block->instructions.emplace_back(std::move(vec));
6448 }
6449
6450 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6451 unsigned offset = start * elem_size_bytes;
6452
6453 if (ctx->options->chip_class >= GFX7) {
6454 if (offset > 0 && ctx->options->chip_class < GFX9) {
6455 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6456 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6457 Temp carry = bld.tmp(bld.lm);
6458 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6459
6460 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6461 Operand(offset), addr0);
6462 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6463 Operand(0u), addr1,
6464 carry).def(1).setHint(vcc);
6465
6466 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6467
6468 offset = 0;
6469 }
6470
6471 bool global = ctx->options->chip_class >= GFX9;
6472 aco_opcode op;
6473 switch (num_bytes) {
6474 case 4:
6475 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6476 break;
6477 case 8:
6478 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6479 break;
6480 case 12:
6481 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6482 break;
6483 case 16:
6484 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6485 break;
6486 default:
6487 unreachable("store_global not implemented for this size.");
6488 }
6489
6490 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6491 flat->operands[0] = Operand(addr);
6492 flat->operands[1] = Operand(s1);
6493 flat->operands[2] = Operand(data);
6494 flat->glc = glc;
6495 flat->dlc = false;
6496 flat->offset = offset;
6497 flat->disable_wqm = true;
6498 flat->barrier = barrier_buffer;
6499 ctx->program->needs_exact = true;
6500 ctx->block->instructions.emplace_back(std::move(flat));
6501 } else {
6502 assert(ctx->options->chip_class == GFX6);
6503
6504 aco_opcode op;
6505 switch (num_bytes) {
6506 case 4:
6507 op = aco_opcode::buffer_store_dword;
6508 break;
6509 case 8:
6510 op = aco_opcode::buffer_store_dwordx2;
6511 break;
6512 case 16:
6513 op = aco_opcode::buffer_store_dwordx4;
6514 break;
6515 default:
6516 unreachable("store_global not implemented for this size.");
6517 }
6518
6519 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6520
6521 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6522 mubuf->operands[0] = Operand(rsrc);
6523 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6524 mubuf->operands[2] = Operand(0u);
6525 mubuf->operands[3] = Operand(write_data);
6526 mubuf->glc = glc;
6527 mubuf->dlc = false;
6528 mubuf->offset = offset;
6529 mubuf->addr64 = addr.type() == RegType::vgpr;
6530 mubuf->disable_wqm = true;
6531 mubuf->barrier = barrier_buffer;
6532 ctx->program->needs_exact = true;
6533 ctx->block->instructions.emplace_back(std::move(mubuf));
6534 }
6535 }
6536 }
6537
6538 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6539 {
6540 /* return the previous value if dest is ever used */
6541 bool return_previous = false;
6542 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6543 return_previous = true;
6544 break;
6545 }
6546 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6547 return_previous = true;
6548 break;
6549 }
6550
6551 Builder bld(ctx->program, ctx->block);
6552 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6553 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6554
6555 if (ctx->options->chip_class >= GFX7)
6556 addr = as_vgpr(ctx, addr);
6557
6558 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6559 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6560 get_ssa_temp(ctx, instr->src[2].ssa), data);
6561
6562 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6563
6564 aco_opcode op32, op64;
6565
6566 if (ctx->options->chip_class >= GFX7) {
6567 bool global = ctx->options->chip_class >= GFX9;
6568 switch (instr->intrinsic) {
6569 case nir_intrinsic_global_atomic_add:
6570 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6571 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6572 break;
6573 case nir_intrinsic_global_atomic_imin:
6574 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6575 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6576 break;
6577 case nir_intrinsic_global_atomic_umin:
6578 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6579 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6580 break;
6581 case nir_intrinsic_global_atomic_imax:
6582 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6583 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6584 break;
6585 case nir_intrinsic_global_atomic_umax:
6586 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6587 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6588 break;
6589 case nir_intrinsic_global_atomic_and:
6590 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6591 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6592 break;
6593 case nir_intrinsic_global_atomic_or:
6594 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6595 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6596 break;
6597 case nir_intrinsic_global_atomic_xor:
6598 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6599 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6600 break;
6601 case nir_intrinsic_global_atomic_exchange:
6602 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6603 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6604 break;
6605 case nir_intrinsic_global_atomic_comp_swap:
6606 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6607 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6608 break;
6609 default:
6610 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6611 }
6612
6613 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6614 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6615 flat->operands[0] = Operand(addr);
6616 flat->operands[1] = Operand(s1);
6617 flat->operands[2] = Operand(data);
6618 if (return_previous)
6619 flat->definitions[0] = Definition(dst);
6620 flat->glc = return_previous;
6621 flat->dlc = false; /* Not needed for atomics */
6622 flat->offset = 0;
6623 flat->disable_wqm = true;
6624 flat->barrier = barrier_buffer;
6625 ctx->program->needs_exact = true;
6626 ctx->block->instructions.emplace_back(std::move(flat));
6627 } else {
6628 assert(ctx->options->chip_class == GFX6);
6629
6630 switch (instr->intrinsic) {
6631 case nir_intrinsic_global_atomic_add:
6632 op32 = aco_opcode::buffer_atomic_add;
6633 op64 = aco_opcode::buffer_atomic_add_x2;
6634 break;
6635 case nir_intrinsic_global_atomic_imin:
6636 op32 = aco_opcode::buffer_atomic_smin;
6637 op64 = aco_opcode::buffer_atomic_smin_x2;
6638 break;
6639 case nir_intrinsic_global_atomic_umin:
6640 op32 = aco_opcode::buffer_atomic_umin;
6641 op64 = aco_opcode::buffer_atomic_umin_x2;
6642 break;
6643 case nir_intrinsic_global_atomic_imax:
6644 op32 = aco_opcode::buffer_atomic_smax;
6645 op64 = aco_opcode::buffer_atomic_smax_x2;
6646 break;
6647 case nir_intrinsic_global_atomic_umax:
6648 op32 = aco_opcode::buffer_atomic_umax;
6649 op64 = aco_opcode::buffer_atomic_umax_x2;
6650 break;
6651 case nir_intrinsic_global_atomic_and:
6652 op32 = aco_opcode::buffer_atomic_and;
6653 op64 = aco_opcode::buffer_atomic_and_x2;
6654 break;
6655 case nir_intrinsic_global_atomic_or:
6656 op32 = aco_opcode::buffer_atomic_or;
6657 op64 = aco_opcode::buffer_atomic_or_x2;
6658 break;
6659 case nir_intrinsic_global_atomic_xor:
6660 op32 = aco_opcode::buffer_atomic_xor;
6661 op64 = aco_opcode::buffer_atomic_xor_x2;
6662 break;
6663 case nir_intrinsic_global_atomic_exchange:
6664 op32 = aco_opcode::buffer_atomic_swap;
6665 op64 = aco_opcode::buffer_atomic_swap_x2;
6666 break;
6667 case nir_intrinsic_global_atomic_comp_swap:
6668 op32 = aco_opcode::buffer_atomic_cmpswap;
6669 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6670 break;
6671 default:
6672 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6673 }
6674
6675 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6676
6677 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6678
6679 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6680 mubuf->operands[0] = Operand(rsrc);
6681 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6682 mubuf->operands[2] = Operand(0u);
6683 mubuf->operands[3] = Operand(data);
6684 if (return_previous)
6685 mubuf->definitions[0] = Definition(dst);
6686 mubuf->glc = return_previous;
6687 mubuf->dlc = false;
6688 mubuf->offset = 0;
6689 mubuf->addr64 = addr.type() == RegType::vgpr;
6690 mubuf->disable_wqm = true;
6691 mubuf->barrier = barrier_buffer;
6692 ctx->program->needs_exact = true;
6693 ctx->block->instructions.emplace_back(std::move(mubuf));
6694 }
6695 }
6696
6697 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6698 Builder bld(ctx->program, ctx->block);
6699 switch(instr->intrinsic) {
6700 case nir_intrinsic_group_memory_barrier:
6701 case nir_intrinsic_memory_barrier:
6702 bld.barrier(aco_opcode::p_memory_barrier_common);
6703 break;
6704 case nir_intrinsic_memory_barrier_buffer:
6705 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6706 break;
6707 case nir_intrinsic_memory_barrier_image:
6708 bld.barrier(aco_opcode::p_memory_barrier_image);
6709 break;
6710 case nir_intrinsic_memory_barrier_tcs_patch:
6711 case nir_intrinsic_memory_barrier_shared:
6712 bld.barrier(aco_opcode::p_memory_barrier_shared);
6713 break;
6714 default:
6715 unreachable("Unimplemented memory barrier intrinsic");
6716 break;
6717 }
6718 }
6719
6720 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6721 {
6722 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6723 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6724 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
6725 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6726 Builder bld(ctx->program, ctx->block);
6727
6728 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6729 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6730 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6731 }
6732
6733 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6734 {
6735 unsigned writemask = nir_intrinsic_write_mask(instr);
6736 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6737 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6738 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6739 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
6740
6741 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6742 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6743 }
6744
6745 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6746 {
6747 unsigned offset = nir_intrinsic_base(instr);
6748 Builder bld(ctx->program, ctx->block);
6749 Operand m = load_lds_size_m0(bld);
6750 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6751 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6752
6753 unsigned num_operands = 3;
6754 aco_opcode op32, op64, op32_rtn, op64_rtn;
6755 switch(instr->intrinsic) {
6756 case nir_intrinsic_shared_atomic_add:
6757 op32 = aco_opcode::ds_add_u32;
6758 op64 = aco_opcode::ds_add_u64;
6759 op32_rtn = aco_opcode::ds_add_rtn_u32;
6760 op64_rtn = aco_opcode::ds_add_rtn_u64;
6761 break;
6762 case nir_intrinsic_shared_atomic_imin:
6763 op32 = aco_opcode::ds_min_i32;
6764 op64 = aco_opcode::ds_min_i64;
6765 op32_rtn = aco_opcode::ds_min_rtn_i32;
6766 op64_rtn = aco_opcode::ds_min_rtn_i64;
6767 break;
6768 case nir_intrinsic_shared_atomic_umin:
6769 op32 = aco_opcode::ds_min_u32;
6770 op64 = aco_opcode::ds_min_u64;
6771 op32_rtn = aco_opcode::ds_min_rtn_u32;
6772 op64_rtn = aco_opcode::ds_min_rtn_u64;
6773 break;
6774 case nir_intrinsic_shared_atomic_imax:
6775 op32 = aco_opcode::ds_max_i32;
6776 op64 = aco_opcode::ds_max_i64;
6777 op32_rtn = aco_opcode::ds_max_rtn_i32;
6778 op64_rtn = aco_opcode::ds_max_rtn_i64;
6779 break;
6780 case nir_intrinsic_shared_atomic_umax:
6781 op32 = aco_opcode::ds_max_u32;
6782 op64 = aco_opcode::ds_max_u64;
6783 op32_rtn = aco_opcode::ds_max_rtn_u32;
6784 op64_rtn = aco_opcode::ds_max_rtn_u64;
6785 break;
6786 case nir_intrinsic_shared_atomic_and:
6787 op32 = aco_opcode::ds_and_b32;
6788 op64 = aco_opcode::ds_and_b64;
6789 op32_rtn = aco_opcode::ds_and_rtn_b32;
6790 op64_rtn = aco_opcode::ds_and_rtn_b64;
6791 break;
6792 case nir_intrinsic_shared_atomic_or:
6793 op32 = aco_opcode::ds_or_b32;
6794 op64 = aco_opcode::ds_or_b64;
6795 op32_rtn = aco_opcode::ds_or_rtn_b32;
6796 op64_rtn = aco_opcode::ds_or_rtn_b64;
6797 break;
6798 case nir_intrinsic_shared_atomic_xor:
6799 op32 = aco_opcode::ds_xor_b32;
6800 op64 = aco_opcode::ds_xor_b64;
6801 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6802 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6803 break;
6804 case nir_intrinsic_shared_atomic_exchange:
6805 op32 = aco_opcode::ds_write_b32;
6806 op64 = aco_opcode::ds_write_b64;
6807 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6808 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6809 break;
6810 case nir_intrinsic_shared_atomic_comp_swap:
6811 op32 = aco_opcode::ds_cmpst_b32;
6812 op64 = aco_opcode::ds_cmpst_b64;
6813 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6814 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6815 num_operands = 4;
6816 break;
6817 default:
6818 unreachable("Unhandled shared atomic intrinsic");
6819 }
6820
6821 /* return the previous value if dest is ever used */
6822 bool return_previous = false;
6823 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6824 return_previous = true;
6825 break;
6826 }
6827 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6828 return_previous = true;
6829 break;
6830 }
6831
6832 aco_opcode op;
6833 if (data.size() == 1) {
6834 assert(instr->dest.ssa.bit_size == 32);
6835 op = return_previous ? op32_rtn : op32;
6836 } else {
6837 assert(instr->dest.ssa.bit_size == 64);
6838 op = return_previous ? op64_rtn : op64;
6839 }
6840
6841 if (offset > 65535) {
6842 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6843 offset = 0;
6844 }
6845
6846 aco_ptr<DS_instruction> ds;
6847 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6848 ds->operands[0] = Operand(address);
6849 ds->operands[1] = Operand(data);
6850 if (num_operands == 4)
6851 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6852 ds->operands[num_operands - 1] = m;
6853 ds->offset0 = offset;
6854 if (return_previous)
6855 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6856 ctx->block->instructions.emplace_back(std::move(ds));
6857 }
6858
6859 Temp get_scratch_resource(isel_context *ctx)
6860 {
6861 Builder bld(ctx->program, ctx->block);
6862 Temp scratch_addr = ctx->program->private_segment_buffer;
6863 if (ctx->stage != compute_cs)
6864 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6865
6866 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6867 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6868
6869 if (ctx->program->chip_class >= GFX10) {
6870 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6871 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6872 S_008F0C_RESOURCE_LEVEL(1);
6873 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6874 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6875 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6876 }
6877
6878 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6879 if (ctx->program->chip_class <= GFX8)
6880 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6881
6882 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6883 }
6884
6885 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6886 Builder bld(ctx->program, ctx->block);
6887 Temp rsrc = get_scratch_resource(ctx);
6888 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6889 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6890
6891 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6892 instr->dest.ssa.bit_size / 8u, rsrc};
6893 info.align_mul = nir_intrinsic_align_mul(instr);
6894 info.align_offset = nir_intrinsic_align_offset(instr);
6895 info.swizzle_component_size = 16;
6896 info.can_reorder = false;
6897 info.soffset = ctx->program->scratch_offset;
6898 emit_mubuf_load(ctx, bld, &info);
6899 }
6900
6901 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6902 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
6903 Builder bld(ctx->program, ctx->block);
6904 Temp rsrc = get_scratch_resource(ctx);
6905 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6906 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6907
6908 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6909 unsigned writemask = nir_intrinsic_write_mask(instr);
6910
6911 while (writemask) {
6912 int start, count;
6913 u_bit_scan_consecutive_range(&writemask, &start, &count);
6914 int num_bytes = count * elem_size_bytes;
6915
6916 if (num_bytes > 16) {
6917 assert(elem_size_bytes == 8);
6918 writemask |= (((count - 2) << 1) - 1) << (start + 2);
6919 count = 2;
6920 num_bytes = 16;
6921 }
6922
6923 // TODO: check alignment of sub-dword stores
6924 // TODO: split 3 bytes. there is no store instruction for that
6925
6926 Temp write_data;
6927 if (count != instr->num_components) {
6928 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6929 for (int i = 0; i < count; i++) {
6930 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
6931 vec->operands[i] = Operand(elem);
6932 }
6933 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
6934 vec->definitions[0] = Definition(write_data);
6935 ctx->block->instructions.emplace_back(std::move(vec));
6936 } else {
6937 write_data = data;
6938 }
6939
6940 aco_opcode op;
6941 switch (num_bytes) {
6942 case 4:
6943 op = aco_opcode::buffer_store_dword;
6944 break;
6945 case 8:
6946 op = aco_opcode::buffer_store_dwordx2;
6947 break;
6948 case 12:
6949 op = aco_opcode::buffer_store_dwordx3;
6950 break;
6951 case 16:
6952 op = aco_opcode::buffer_store_dwordx4;
6953 break;
6954 default:
6955 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
6956 }
6957
6958 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
6959 }
6960 }
6961
6962 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6963 uint8_t log2_ps_iter_samples;
6964 if (ctx->program->info->ps.force_persample) {
6965 log2_ps_iter_samples =
6966 util_logbase2(ctx->options->key.fs.num_samples);
6967 } else {
6968 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6969 }
6970
6971 /* The bit pattern matches that used by fixed function fragment
6972 * processing. */
6973 static const unsigned ps_iter_masks[] = {
6974 0xffff, /* not used */
6975 0x5555,
6976 0x1111,
6977 0x0101,
6978 0x0001,
6979 };
6980 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6981
6982 Builder bld(ctx->program, ctx->block);
6983
6984 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6985 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6986 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6987 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6988 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6989 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6990 }
6991
6992 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6993 Builder bld(ctx->program, ctx->block);
6994
6995 unsigned stream = nir_intrinsic_stream_id(instr);
6996 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6997 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6998 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6999
7000 /* get GSVS ring */
7001 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
7002
7003 unsigned num_components =
7004 ctx->program->info->gs.num_stream_output_components[stream];
7005 assert(num_components);
7006
7007 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
7008 unsigned stream_offset = 0;
7009 for (unsigned i = 0; i < stream; i++) {
7010 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
7011 stream_offset += prev_stride * ctx->program->wave_size;
7012 }
7013
7014 /* Limit on the stride field for <= GFX7. */
7015 assert(stride < (1 << 14));
7016
7017 Temp gsvs_dwords[4];
7018 for (unsigned i = 0; i < 4; i++)
7019 gsvs_dwords[i] = bld.tmp(s1);
7020 bld.pseudo(aco_opcode::p_split_vector,
7021 Definition(gsvs_dwords[0]),
7022 Definition(gsvs_dwords[1]),
7023 Definition(gsvs_dwords[2]),
7024 Definition(gsvs_dwords[3]),
7025 gsvs_ring);
7026
7027 if (stream_offset) {
7028 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
7029
7030 Temp carry = bld.tmp(s1);
7031 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
7032 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));
7033 }
7034
7035 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)));
7036 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
7037
7038 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
7039 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
7040
7041 unsigned offset = 0;
7042 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
7043 if (ctx->program->info->gs.output_streams[i] != stream)
7044 continue;
7045
7046 for (unsigned j = 0; j < 4; j++) {
7047 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
7048 continue;
7049
7050 if (ctx->outputs.mask[i] & (1 << j)) {
7051 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
7052 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
7053 if (const_offset >= 4096u) {
7054 if (vaddr_offset.isUndefined())
7055 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
7056 else
7057 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
7058 const_offset %= 4096u;
7059 }
7060
7061 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
7062 mtbuf->operands[0] = Operand(gsvs_ring);
7063 mtbuf->operands[1] = vaddr_offset;
7064 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
7065 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
7066 mtbuf->offen = !vaddr_offset.isUndefined();
7067 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
7068 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
7069 mtbuf->offset = const_offset;
7070 mtbuf->glc = true;
7071 mtbuf->slc = true;
7072 mtbuf->barrier = barrier_gs_data;
7073 mtbuf->can_reorder = true;
7074 bld.insert(std::move(mtbuf));
7075 }
7076
7077 offset += ctx->shader->info.gs.vertices_out;
7078 }
7079
7080 /* outputs for the next vertex are undefined and keeping them around can
7081 * create invalid IR with control flow */
7082 ctx->outputs.mask[i] = 0;
7083 }
7084
7085 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
7086 }
7087
7088 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
7089 {
7090 Builder bld(ctx->program, ctx->block);
7091
7092 if (cluster_size == 1) {
7093 return src;
7094 } if (op == nir_op_iand && cluster_size == 4) {
7095 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7096 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7097 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
7098 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
7099 } else if (op == nir_op_ior && cluster_size == 4) {
7100 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7101 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
7102 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
7103 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
7104 //subgroupAnd(val) -> (exec & ~val) == 0
7105 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7106 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7107 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
7108 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
7109 //subgroupOr(val) -> (val & exec) != 0
7110 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
7111 return bool_to_vector_condition(ctx, tmp);
7112 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
7113 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7114 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7115 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
7116 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
7117 return bool_to_vector_condition(ctx, tmp);
7118 } else {
7119 //subgroupClustered{And,Or,Xor}(val, n) ->
7120 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7121 //cluster_offset = ~(n - 1) & lane_id
7122 //cluster_mask = ((1 << n) - 1)
7123 //subgroupClusteredAnd():
7124 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7125 //subgroupClusteredOr():
7126 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7127 //subgroupClusteredXor():
7128 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7129 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7130 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7131
7132 Temp tmp;
7133 if (op == nir_op_iand)
7134 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7135 else
7136 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7137
7138 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7139
7140 if (ctx->program->chip_class <= GFX7)
7141 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7142 else if (ctx->program->wave_size == 64)
7143 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7144 else
7145 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7146 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7147 if (cluster_mask != 0xffffffff)
7148 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7149
7150 Definition cmp_def = Definition();
7151 if (op == nir_op_iand) {
7152 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7153 } else if (op == nir_op_ior) {
7154 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7155 } else if (op == nir_op_ixor) {
7156 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7157 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7158 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7159 }
7160 cmp_def.setHint(vcc);
7161 return cmp_def.getTemp();
7162 }
7163 }
7164
7165 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7166 {
7167 Builder bld(ctx->program, ctx->block);
7168
7169 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7170 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7171 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7172 Temp tmp;
7173 if (op == nir_op_iand)
7174 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7175 else
7176 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7177
7178 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7179 Temp lo = lohi.def(0).getTemp();
7180 Temp hi = lohi.def(1).getTemp();
7181 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7182
7183 Definition cmp_def = Definition();
7184 if (op == nir_op_iand)
7185 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7186 else if (op == nir_op_ior)
7187 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7188 else if (op == nir_op_ixor)
7189 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7190 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7191 cmp_def.setHint(vcc);
7192 return cmp_def.getTemp();
7193 }
7194
7195 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7196 {
7197 Builder bld(ctx->program, ctx->block);
7198
7199 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7200 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7201 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7202 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7203 if (op == nir_op_iand)
7204 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7205 else if (op == nir_op_ior)
7206 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7207 else if (op == nir_op_ixor)
7208 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7209
7210 assert(false);
7211 return Temp();
7212 }
7213
7214 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7215 {
7216 Builder bld(ctx->program, ctx->block);
7217 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7218 if (src.regClass().type() == RegType::vgpr) {
7219 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7220 } else if (src.regClass() == s1) {
7221 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7222 } else if (src.regClass() == s2) {
7223 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7224 } else {
7225 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7226 nir_print_instr(&instr->instr, stderr);
7227 fprintf(stderr, "\n");
7228 }
7229 }
7230
7231 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7232 {
7233 Builder bld(ctx->program, ctx->block);
7234 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7235 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7236 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7237
7238 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7239 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7240 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7241 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7242
7243 /* Build DD X/Y */
7244 if (ctx->program->chip_class >= GFX8) {
7245 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7246 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7247 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7248 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7249 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7250 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7251 } else {
7252 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7253 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7254 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7255 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7256 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7257 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7258 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7259 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7260 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7261 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7262 }
7263
7264 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7265 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7266 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7267 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7268 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7269 Temp wqm1 = bld.tmp(v1);
7270 emit_wqm(ctx, tmp1, wqm1, true);
7271 Temp wqm2 = bld.tmp(v1);
7272 emit_wqm(ctx, tmp2, wqm2, true);
7273 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7274 return;
7275 }
7276
7277 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7278 {
7279 Builder bld(ctx->program, ctx->block);
7280 switch(instr->intrinsic) {
7281 case nir_intrinsic_load_barycentric_sample:
7282 case nir_intrinsic_load_barycentric_pixel:
7283 case nir_intrinsic_load_barycentric_centroid: {
7284 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7285 Temp bary = Temp(0, s2);
7286 switch (mode) {
7287 case INTERP_MODE_SMOOTH:
7288 case INTERP_MODE_NONE:
7289 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7290 bary = get_arg(ctx, ctx->args->ac.persp_center);
7291 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7292 bary = ctx->persp_centroid;
7293 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7294 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7295 break;
7296 case INTERP_MODE_NOPERSPECTIVE:
7297 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7298 bary = get_arg(ctx, ctx->args->ac.linear_center);
7299 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7300 bary = ctx->linear_centroid;
7301 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7302 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7303 break;
7304 default:
7305 break;
7306 }
7307 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7308 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7309 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7310 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7311 Operand(p1), Operand(p2));
7312 emit_split_vector(ctx, dst, 2);
7313 break;
7314 }
7315 case nir_intrinsic_load_barycentric_model: {
7316 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7317
7318 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7319 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7320 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7321 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7322 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7323 Operand(p1), Operand(p2), Operand(p3));
7324 emit_split_vector(ctx, dst, 3);
7325 break;
7326 }
7327 case nir_intrinsic_load_barycentric_at_sample: {
7328 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7329 switch (ctx->options->key.fs.num_samples) {
7330 case 2: sample_pos_offset += 1 << 3; break;
7331 case 4: sample_pos_offset += 3 << 3; break;
7332 case 8: sample_pos_offset += 7 << 3; break;
7333 default: break;
7334 }
7335 Temp sample_pos;
7336 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7337 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7338 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7339 if (addr.type() == RegType::sgpr) {
7340 Operand offset;
7341 if (const_addr) {
7342 sample_pos_offset += const_addr->u32 << 3;
7343 offset = Operand(sample_pos_offset);
7344 } else if (ctx->options->chip_class >= GFX9) {
7345 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7346 } else {
7347 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7348 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7349 }
7350
7351 Operand off = bld.copy(bld.def(s1), Operand(offset));
7352 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7353
7354 } else if (ctx->options->chip_class >= GFX9) {
7355 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7356 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7357 } else if (ctx->options->chip_class >= GFX7) {
7358 /* addr += private_segment_buffer + sample_pos_offset */
7359 Temp tmp0 = bld.tmp(s1);
7360 Temp tmp1 = bld.tmp(s1);
7361 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7362 Definition scc_tmp = bld.def(s1, scc);
7363 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7364 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7365 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7366 Temp pck0 = bld.tmp(v1);
7367 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7368 tmp1 = as_vgpr(ctx, tmp1);
7369 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);
7370 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7371
7372 /* sample_pos = flat_load_dwordx2 addr */
7373 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7374 } else {
7375 assert(ctx->options->chip_class == GFX6);
7376
7377 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7378 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7379 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7380
7381 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7382 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7383
7384 sample_pos = bld.tmp(v2);
7385
7386 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7387 load->definitions[0] = Definition(sample_pos);
7388 load->operands[0] = Operand(rsrc);
7389 load->operands[1] = Operand(addr);
7390 load->operands[2] = Operand(0u);
7391 load->offset = sample_pos_offset;
7392 load->offen = 0;
7393 load->addr64 = true;
7394 load->glc = false;
7395 load->dlc = false;
7396 load->disable_wqm = false;
7397 load->barrier = barrier_none;
7398 load->can_reorder = true;
7399 ctx->block->instructions.emplace_back(std::move(load));
7400 }
7401
7402 /* sample_pos -= 0.5 */
7403 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7404 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7405 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7406 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7407 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7408
7409 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7410 break;
7411 }
7412 case nir_intrinsic_load_barycentric_at_offset: {
7413 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7414 RegClass rc = RegClass(offset.type(), 1);
7415 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7416 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7417 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7418 break;
7419 }
7420 case nir_intrinsic_load_front_face: {
7421 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7422 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7423 break;
7424 }
7425 case nir_intrinsic_load_view_index: {
7426 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7427 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7428 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7429 break;
7430 }
7431
7432 /* fallthrough */
7433 }
7434 case nir_intrinsic_load_layer_id: {
7435 unsigned idx = nir_intrinsic_base(instr);
7436 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7437 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7438 break;
7439 }
7440 case nir_intrinsic_load_frag_coord: {
7441 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7442 break;
7443 }
7444 case nir_intrinsic_load_sample_pos: {
7445 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7446 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7447 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7448 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7449 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7450 break;
7451 }
7452 case nir_intrinsic_load_tess_coord:
7453 visit_load_tess_coord(ctx, instr);
7454 break;
7455 case nir_intrinsic_load_interpolated_input:
7456 visit_load_interpolated_input(ctx, instr);
7457 break;
7458 case nir_intrinsic_store_output:
7459 visit_store_output(ctx, instr);
7460 break;
7461 case nir_intrinsic_load_input:
7462 case nir_intrinsic_load_input_vertex:
7463 visit_load_input(ctx, instr);
7464 break;
7465 case nir_intrinsic_load_output:
7466 visit_load_output(ctx, instr);
7467 break;
7468 case nir_intrinsic_load_per_vertex_input:
7469 visit_load_per_vertex_input(ctx, instr);
7470 break;
7471 case nir_intrinsic_load_per_vertex_output:
7472 visit_load_per_vertex_output(ctx, instr);
7473 break;
7474 case nir_intrinsic_store_per_vertex_output:
7475 visit_store_per_vertex_output(ctx, instr);
7476 break;
7477 case nir_intrinsic_load_ubo:
7478 visit_load_ubo(ctx, instr);
7479 break;
7480 case nir_intrinsic_load_push_constant:
7481 visit_load_push_constant(ctx, instr);
7482 break;
7483 case nir_intrinsic_load_constant:
7484 visit_load_constant(ctx, instr);
7485 break;
7486 case nir_intrinsic_vulkan_resource_index:
7487 visit_load_resource(ctx, instr);
7488 break;
7489 case nir_intrinsic_discard:
7490 visit_discard(ctx, instr);
7491 break;
7492 case nir_intrinsic_discard_if:
7493 visit_discard_if(ctx, instr);
7494 break;
7495 case nir_intrinsic_load_shared:
7496 visit_load_shared(ctx, instr);
7497 break;
7498 case nir_intrinsic_store_shared:
7499 visit_store_shared(ctx, instr);
7500 break;
7501 case nir_intrinsic_shared_atomic_add:
7502 case nir_intrinsic_shared_atomic_imin:
7503 case nir_intrinsic_shared_atomic_umin:
7504 case nir_intrinsic_shared_atomic_imax:
7505 case nir_intrinsic_shared_atomic_umax:
7506 case nir_intrinsic_shared_atomic_and:
7507 case nir_intrinsic_shared_atomic_or:
7508 case nir_intrinsic_shared_atomic_xor:
7509 case nir_intrinsic_shared_atomic_exchange:
7510 case nir_intrinsic_shared_atomic_comp_swap:
7511 visit_shared_atomic(ctx, instr);
7512 break;
7513 case nir_intrinsic_image_deref_load:
7514 visit_image_load(ctx, instr);
7515 break;
7516 case nir_intrinsic_image_deref_store:
7517 visit_image_store(ctx, instr);
7518 break;
7519 case nir_intrinsic_image_deref_atomic_add:
7520 case nir_intrinsic_image_deref_atomic_umin:
7521 case nir_intrinsic_image_deref_atomic_imin:
7522 case nir_intrinsic_image_deref_atomic_umax:
7523 case nir_intrinsic_image_deref_atomic_imax:
7524 case nir_intrinsic_image_deref_atomic_and:
7525 case nir_intrinsic_image_deref_atomic_or:
7526 case nir_intrinsic_image_deref_atomic_xor:
7527 case nir_intrinsic_image_deref_atomic_exchange:
7528 case nir_intrinsic_image_deref_atomic_comp_swap:
7529 visit_image_atomic(ctx, instr);
7530 break;
7531 case nir_intrinsic_image_deref_size:
7532 visit_image_size(ctx, instr);
7533 break;
7534 case nir_intrinsic_load_ssbo:
7535 visit_load_ssbo(ctx, instr);
7536 break;
7537 case nir_intrinsic_store_ssbo:
7538 visit_store_ssbo(ctx, instr);
7539 break;
7540 case nir_intrinsic_load_global:
7541 visit_load_global(ctx, instr);
7542 break;
7543 case nir_intrinsic_store_global:
7544 visit_store_global(ctx, instr);
7545 break;
7546 case nir_intrinsic_global_atomic_add:
7547 case nir_intrinsic_global_atomic_imin:
7548 case nir_intrinsic_global_atomic_umin:
7549 case nir_intrinsic_global_atomic_imax:
7550 case nir_intrinsic_global_atomic_umax:
7551 case nir_intrinsic_global_atomic_and:
7552 case nir_intrinsic_global_atomic_or:
7553 case nir_intrinsic_global_atomic_xor:
7554 case nir_intrinsic_global_atomic_exchange:
7555 case nir_intrinsic_global_atomic_comp_swap:
7556 visit_global_atomic(ctx, instr);
7557 break;
7558 case nir_intrinsic_ssbo_atomic_add:
7559 case nir_intrinsic_ssbo_atomic_imin:
7560 case nir_intrinsic_ssbo_atomic_umin:
7561 case nir_intrinsic_ssbo_atomic_imax:
7562 case nir_intrinsic_ssbo_atomic_umax:
7563 case nir_intrinsic_ssbo_atomic_and:
7564 case nir_intrinsic_ssbo_atomic_or:
7565 case nir_intrinsic_ssbo_atomic_xor:
7566 case nir_intrinsic_ssbo_atomic_exchange:
7567 case nir_intrinsic_ssbo_atomic_comp_swap:
7568 visit_atomic_ssbo(ctx, instr);
7569 break;
7570 case nir_intrinsic_load_scratch:
7571 visit_load_scratch(ctx, instr);
7572 break;
7573 case nir_intrinsic_store_scratch:
7574 visit_store_scratch(ctx, instr);
7575 break;
7576 case nir_intrinsic_get_buffer_size:
7577 visit_get_buffer_size(ctx, instr);
7578 break;
7579 case nir_intrinsic_control_barrier: {
7580 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7581 /* GFX6 only (thanks to a hw bug workaround):
7582 * The real barrier instruction isn’t needed, because an entire patch
7583 * always fits into a single wave.
7584 */
7585 break;
7586 }
7587
7588 if (ctx->program->workgroup_size > ctx->program->wave_size)
7589 bld.sopp(aco_opcode::s_barrier);
7590
7591 break;
7592 }
7593 case nir_intrinsic_memory_barrier_tcs_patch:
7594 case nir_intrinsic_group_memory_barrier:
7595 case nir_intrinsic_memory_barrier:
7596 case nir_intrinsic_memory_barrier_buffer:
7597 case nir_intrinsic_memory_barrier_image:
7598 case nir_intrinsic_memory_barrier_shared:
7599 emit_memory_barrier(ctx, instr);
7600 break;
7601 case nir_intrinsic_load_num_work_groups: {
7602 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7603 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7604 emit_split_vector(ctx, dst, 3);
7605 break;
7606 }
7607 case nir_intrinsic_load_local_invocation_id: {
7608 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7609 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7610 emit_split_vector(ctx, dst, 3);
7611 break;
7612 }
7613 case nir_intrinsic_load_work_group_id: {
7614 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7615 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7616 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7617 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7618 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7619 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7620 emit_split_vector(ctx, dst, 3);
7621 break;
7622 }
7623 case nir_intrinsic_load_local_invocation_index: {
7624 Temp id = emit_mbcnt(ctx, bld.def(v1));
7625
7626 /* The tg_size bits [6:11] contain the subgroup id,
7627 * we need this multiplied by the wave size, and then OR the thread id to it.
7628 */
7629 if (ctx->program->wave_size == 64) {
7630 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7631 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7632 get_arg(ctx, ctx->args->ac.tg_size));
7633 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7634 } else {
7635 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7636 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7637 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7638 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7639 }
7640 break;
7641 }
7642 case nir_intrinsic_load_subgroup_id: {
7643 if (ctx->stage == compute_cs) {
7644 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7645 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7646 } else {
7647 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7648 }
7649 break;
7650 }
7651 case nir_intrinsic_load_subgroup_invocation: {
7652 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7653 break;
7654 }
7655 case nir_intrinsic_load_num_subgroups: {
7656 if (ctx->stage == compute_cs)
7657 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7658 get_arg(ctx, ctx->args->ac.tg_size));
7659 else
7660 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7661 break;
7662 }
7663 case nir_intrinsic_ballot: {
7664 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7665 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7666 Definition tmp = bld.def(dst.regClass());
7667 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7668 if (instr->src[0].ssa->bit_size == 1) {
7669 assert(src.regClass() == bld.lm);
7670 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7671 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7672 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7673 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7674 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7675 } else {
7676 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7677 nir_print_instr(&instr->instr, stderr);
7678 fprintf(stderr, "\n");
7679 }
7680 if (dst.size() != bld.lm.size()) {
7681 /* Wave32 with ballot size set to 64 */
7682 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7683 }
7684 emit_wqm(ctx, tmp.getTemp(), dst);
7685 break;
7686 }
7687 case nir_intrinsic_shuffle:
7688 case nir_intrinsic_read_invocation: {
7689 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7690 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7691 emit_uniform_subgroup(ctx, instr, src);
7692 } else {
7693 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7694 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7695 tid = bld.as_uniform(tid);
7696 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7697 if (src.regClass() == v1) {
7698 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7699 } else if (src.regClass() == v2) {
7700 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7701 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7702 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7703 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7704 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7705 emit_split_vector(ctx, dst, 2);
7706 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7707 assert(src.regClass() == bld.lm);
7708 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7709 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7710 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7711 assert(src.regClass() == bld.lm);
7712 Temp tmp;
7713 if (ctx->program->chip_class <= GFX7)
7714 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7715 else if (ctx->program->wave_size == 64)
7716 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7717 else
7718 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7719 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7720 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7721 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7722 } else {
7723 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7724 nir_print_instr(&instr->instr, stderr);
7725 fprintf(stderr, "\n");
7726 }
7727 }
7728 break;
7729 }
7730 case nir_intrinsic_load_sample_id: {
7731 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7732 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7733 break;
7734 }
7735 case nir_intrinsic_load_sample_mask_in: {
7736 visit_load_sample_mask_in(ctx, instr);
7737 break;
7738 }
7739 case nir_intrinsic_read_first_invocation: {
7740 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7741 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7742 if (src.regClass() == v1) {
7743 emit_wqm(ctx,
7744 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7745 dst);
7746 } else if (src.regClass() == v2) {
7747 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7748 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7749 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7750 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7751 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7752 emit_split_vector(ctx, dst, 2);
7753 } else if (instr->dest.ssa.bit_size == 1) {
7754 assert(src.regClass() == bld.lm);
7755 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7756 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7757 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7758 } else if (src.regClass() == s1) {
7759 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7760 } else if (src.regClass() == s2) {
7761 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7762 } else {
7763 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7764 nir_print_instr(&instr->instr, stderr);
7765 fprintf(stderr, "\n");
7766 }
7767 break;
7768 }
7769 case nir_intrinsic_vote_all: {
7770 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7771 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7772 assert(src.regClass() == bld.lm);
7773 assert(dst.regClass() == bld.lm);
7774
7775 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7776 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7777 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7778 break;
7779 }
7780 case nir_intrinsic_vote_any: {
7781 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7782 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7783 assert(src.regClass() == bld.lm);
7784 assert(dst.regClass() == bld.lm);
7785
7786 Temp tmp = bool_to_scalar_condition(ctx, src);
7787 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7788 break;
7789 }
7790 case nir_intrinsic_reduce:
7791 case nir_intrinsic_inclusive_scan:
7792 case nir_intrinsic_exclusive_scan: {
7793 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7794 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7795 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7796 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7797 nir_intrinsic_cluster_size(instr) : 0;
7798 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7799
7800 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7801 emit_uniform_subgroup(ctx, instr, src);
7802 } else if (instr->dest.ssa.bit_size == 1) {
7803 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7804 op = nir_op_iand;
7805 else if (op == nir_op_iadd)
7806 op = nir_op_ixor;
7807 else if (op == nir_op_umax || op == nir_op_imax)
7808 op = nir_op_ior;
7809 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7810
7811 switch (instr->intrinsic) {
7812 case nir_intrinsic_reduce:
7813 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7814 break;
7815 case nir_intrinsic_exclusive_scan:
7816 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7817 break;
7818 case nir_intrinsic_inclusive_scan:
7819 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7820 break;
7821 default:
7822 assert(false);
7823 }
7824 } else if (cluster_size == 1) {
7825 bld.copy(Definition(dst), src);
7826 } else {
7827 src = as_vgpr(ctx, src);
7828
7829 ReduceOp reduce_op;
7830 switch (op) {
7831 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7832 CASE(iadd)
7833 CASE(imul)
7834 CASE(fadd)
7835 CASE(fmul)
7836 CASE(imin)
7837 CASE(umin)
7838 CASE(fmin)
7839 CASE(imax)
7840 CASE(umax)
7841 CASE(fmax)
7842 CASE(iand)
7843 CASE(ior)
7844 CASE(ixor)
7845 default:
7846 unreachable("unknown reduction op");
7847 #undef CASE
7848 }
7849
7850 aco_opcode aco_op;
7851 switch (instr->intrinsic) {
7852 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7853 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7854 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7855 default:
7856 unreachable("unknown reduce intrinsic");
7857 }
7858
7859 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7860 reduce->operands[0] = Operand(src);
7861 // filled in by aco_reduce_assign.cpp, used internally as part of the
7862 // reduce sequence
7863 assert(dst.size() == 1 || dst.size() == 2);
7864 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7865 reduce->operands[2] = Operand(v1.as_linear());
7866
7867 Temp tmp_dst = bld.tmp(dst.regClass());
7868 reduce->definitions[0] = Definition(tmp_dst);
7869 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7870 reduce->definitions[2] = Definition();
7871 reduce->definitions[3] = Definition(scc, s1);
7872 reduce->definitions[4] = Definition();
7873 reduce->reduce_op = reduce_op;
7874 reduce->cluster_size = cluster_size;
7875 ctx->block->instructions.emplace_back(std::move(reduce));
7876
7877 emit_wqm(ctx, tmp_dst, dst);
7878 }
7879 break;
7880 }
7881 case nir_intrinsic_quad_broadcast: {
7882 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7883 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7884 emit_uniform_subgroup(ctx, instr, src);
7885 } else {
7886 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7887 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7888 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7889
7890 if (instr->dest.ssa.bit_size == 1) {
7891 assert(src.regClass() == bld.lm);
7892 assert(dst.regClass() == bld.lm);
7893 uint32_t half_mask = 0x11111111u << lane;
7894 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7895 Temp tmp = bld.tmp(bld.lm);
7896 bld.sop1(Builder::s_wqm, Definition(tmp),
7897 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7898 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7899 emit_wqm(ctx, tmp, dst);
7900 } else if (instr->dest.ssa.bit_size == 32) {
7901 if (ctx->program->chip_class >= GFX8)
7902 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7903 else
7904 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7905 } else if (instr->dest.ssa.bit_size == 64) {
7906 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7907 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7908 if (ctx->program->chip_class >= GFX8) {
7909 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7910 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7911 } else {
7912 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7913 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7914 }
7915 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7916 emit_split_vector(ctx, dst, 2);
7917 } else {
7918 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7919 nir_print_instr(&instr->instr, stderr);
7920 fprintf(stderr, "\n");
7921 }
7922 }
7923 break;
7924 }
7925 case nir_intrinsic_quad_swap_horizontal:
7926 case nir_intrinsic_quad_swap_vertical:
7927 case nir_intrinsic_quad_swap_diagonal:
7928 case nir_intrinsic_quad_swizzle_amd: {
7929 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7930 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7931 emit_uniform_subgroup(ctx, instr, src);
7932 break;
7933 }
7934 uint16_t dpp_ctrl = 0;
7935 switch (instr->intrinsic) {
7936 case nir_intrinsic_quad_swap_horizontal:
7937 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7938 break;
7939 case nir_intrinsic_quad_swap_vertical:
7940 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7941 break;
7942 case nir_intrinsic_quad_swap_diagonal:
7943 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7944 break;
7945 case nir_intrinsic_quad_swizzle_amd:
7946 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7947 break;
7948 default:
7949 break;
7950 }
7951 if (ctx->program->chip_class < GFX8)
7952 dpp_ctrl |= (1 << 15);
7953
7954 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7955 if (instr->dest.ssa.bit_size == 1) {
7956 assert(src.regClass() == bld.lm);
7957 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7958 if (ctx->program->chip_class >= GFX8)
7959 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7960 else
7961 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7962 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7963 emit_wqm(ctx, tmp, dst);
7964 } else if (instr->dest.ssa.bit_size == 32) {
7965 Temp tmp;
7966 if (ctx->program->chip_class >= GFX8)
7967 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7968 else
7969 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7970 emit_wqm(ctx, tmp, dst);
7971 } else if (instr->dest.ssa.bit_size == 64) {
7972 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7973 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7974 if (ctx->program->chip_class >= GFX8) {
7975 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7976 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7977 } else {
7978 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7979 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7980 }
7981 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7982 emit_split_vector(ctx, dst, 2);
7983 } else {
7984 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7985 nir_print_instr(&instr->instr, stderr);
7986 fprintf(stderr, "\n");
7987 }
7988 break;
7989 }
7990 case nir_intrinsic_masked_swizzle_amd: {
7991 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7992 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7993 emit_uniform_subgroup(ctx, instr, src);
7994 break;
7995 }
7996 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7997 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7998 if (dst.regClass() == v1) {
7999 emit_wqm(ctx,
8000 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
8001 dst);
8002 } else if (dst.regClass() == v2) {
8003 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
8004 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
8005 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
8006 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
8007 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8008 emit_split_vector(ctx, dst, 2);
8009 } else {
8010 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8011 nir_print_instr(&instr->instr, stderr);
8012 fprintf(stderr, "\n");
8013 }
8014 break;
8015 }
8016 case nir_intrinsic_write_invocation_amd: {
8017 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
8018 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
8019 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
8020 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8021 if (dst.regClass() == v1) {
8022 /* src2 is ignored for writelane. RA assigns the same reg for dst */
8023 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
8024 } else if (dst.regClass() == v2) {
8025 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
8026 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
8027 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
8028 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
8029 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
8030 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
8031 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8032 emit_split_vector(ctx, dst, 2);
8033 } else {
8034 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8035 nir_print_instr(&instr->instr, stderr);
8036 fprintf(stderr, "\n");
8037 }
8038 break;
8039 }
8040 case nir_intrinsic_mbcnt_amd: {
8041 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8042 RegClass rc = RegClass(src.type(), 1);
8043 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
8044 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
8045 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8046 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
8047 emit_wqm(ctx, wqm_tmp, dst);
8048 break;
8049 }
8050 case nir_intrinsic_load_helper_invocation: {
8051 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8052 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
8053 ctx->block->kind |= block_kind_needs_lowering;
8054 ctx->program->needs_exact = true;
8055 break;
8056 }
8057 case nir_intrinsic_is_helper_invocation: {
8058 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8059 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
8060 ctx->block->kind |= block_kind_needs_lowering;
8061 ctx->program->needs_exact = true;
8062 break;
8063 }
8064 case nir_intrinsic_demote:
8065 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
8066
8067 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8068 ctx->cf_info.exec_potentially_empty_discard = true;
8069 ctx->block->kind |= block_kind_uses_demote;
8070 ctx->program->needs_exact = true;
8071 break;
8072 case nir_intrinsic_demote_if: {
8073 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8074 assert(src.regClass() == bld.lm);
8075 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
8076 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
8077
8078 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8079 ctx->cf_info.exec_potentially_empty_discard = true;
8080 ctx->block->kind |= block_kind_uses_demote;
8081 ctx->program->needs_exact = true;
8082 break;
8083 }
8084 case nir_intrinsic_first_invocation: {
8085 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
8086 get_ssa_temp(ctx, &instr->dest.ssa));
8087 break;
8088 }
8089 case nir_intrinsic_shader_clock:
8090 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
8091 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
8092 break;
8093 case nir_intrinsic_load_vertex_id_zero_base: {
8094 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8095 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
8096 break;
8097 }
8098 case nir_intrinsic_load_first_vertex: {
8099 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8100 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
8101 break;
8102 }
8103 case nir_intrinsic_load_base_instance: {
8104 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8105 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
8106 break;
8107 }
8108 case nir_intrinsic_load_instance_id: {
8109 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8110 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
8111 break;
8112 }
8113 case nir_intrinsic_load_draw_id: {
8114 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8115 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8116 break;
8117 }
8118 case nir_intrinsic_load_invocation_id: {
8119 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8120
8121 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8122 if (ctx->options->chip_class >= GFX10)
8123 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8124 else
8125 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8126 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8127 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8128 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8129 } else {
8130 unreachable("Unsupported stage for load_invocation_id");
8131 }
8132
8133 break;
8134 }
8135 case nir_intrinsic_load_primitive_id: {
8136 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8137
8138 switch (ctx->shader->info.stage) {
8139 case MESA_SHADER_GEOMETRY:
8140 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8141 break;
8142 case MESA_SHADER_TESS_CTRL:
8143 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8144 break;
8145 case MESA_SHADER_TESS_EVAL:
8146 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8147 break;
8148 default:
8149 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8150 }
8151
8152 break;
8153 }
8154 case nir_intrinsic_load_patch_vertices_in: {
8155 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8156 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8157
8158 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8159 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8160 break;
8161 }
8162 case nir_intrinsic_emit_vertex_with_counter: {
8163 visit_emit_vertex_with_counter(ctx, instr);
8164 break;
8165 }
8166 case nir_intrinsic_end_primitive_with_counter: {
8167 unsigned stream = nir_intrinsic_stream_id(instr);
8168 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8169 break;
8170 }
8171 case nir_intrinsic_set_vertex_count: {
8172 /* unused, the HW keeps track of this for us */
8173 break;
8174 }
8175 default:
8176 fprintf(stderr, "Unimplemented intrinsic instr: ");
8177 nir_print_instr(&instr->instr, stderr);
8178 fprintf(stderr, "\n");
8179 abort();
8180
8181 break;
8182 }
8183 }
8184
8185
8186 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8187 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8188 enum glsl_base_type *stype)
8189 {
8190 nir_deref_instr *texture_deref_instr = NULL;
8191 nir_deref_instr *sampler_deref_instr = NULL;
8192 int plane = -1;
8193
8194 for (unsigned i = 0; i < instr->num_srcs; i++) {
8195 switch (instr->src[i].src_type) {
8196 case nir_tex_src_texture_deref:
8197 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8198 break;
8199 case nir_tex_src_sampler_deref:
8200 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8201 break;
8202 case nir_tex_src_plane:
8203 plane = nir_src_as_int(instr->src[i].src);
8204 break;
8205 default:
8206 break;
8207 }
8208 }
8209
8210 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8211
8212 if (!sampler_deref_instr)
8213 sampler_deref_instr = texture_deref_instr;
8214
8215 if (plane >= 0) {
8216 assert(instr->op != nir_texop_txf_ms &&
8217 instr->op != nir_texop_samples_identical);
8218 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8219 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8220 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8221 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8222 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8223 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8224 } else {
8225 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8226 }
8227 if (samp_ptr) {
8228 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8229
8230 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8231 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8232 Builder bld(ctx->program, ctx->block);
8233
8234 /* to avoid unnecessary moves, we split and recombine sampler and image */
8235 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8236 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8237 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8238 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8239 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8240 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8241 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8242 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8243
8244 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8245 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8246 img[0], img[1], img[2], img[3],
8247 img[4], img[5], img[6], img[7]);
8248 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8249 samp[0], samp[1], samp[2], samp[3]);
8250 }
8251 }
8252 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8253 instr->op == nir_texop_samples_identical))
8254 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8255 }
8256
8257 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8258 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8259 {
8260 Builder bld(ctx->program, ctx->block);
8261
8262 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8263 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8264 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8265
8266 Operand neg_one(0xbf800000u);
8267 Operand one(0x3f800000u);
8268 Operand two(0x40000000u);
8269 Operand four(0x40800000u);
8270
8271 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8272 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8273 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8274
8275 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8276 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8277 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8278 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);
8279
8280 // select sc
8281 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8282 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8283 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8284 one, is_ma_y);
8285 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8286
8287 // select tc
8288 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8289 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8290 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8291
8292 // select ma
8293 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8294 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8295 deriv_z, is_ma_z);
8296 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8297 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8298 }
8299
8300 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8301 {
8302 Builder bld(ctx->program, ctx->block);
8303 Temp ma, tc, sc, id;
8304
8305 if (is_array) {
8306 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8307
8308 // see comment in ac_prepare_cube_coords()
8309 if (ctx->options->chip_class <= GFX8)
8310 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8311 }
8312
8313 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8314
8315 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8316 vop3a->operands[0] = Operand(ma);
8317 vop3a->abs[0] = true;
8318 Temp invma = bld.tmp(v1);
8319 vop3a->definitions[0] = Definition(invma);
8320 ctx->block->instructions.emplace_back(std::move(vop3a));
8321
8322 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8323 if (!is_deriv)
8324 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8325
8326 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8327 if (!is_deriv)
8328 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8329
8330 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8331
8332 if (is_deriv) {
8333 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8334 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8335
8336 for (unsigned i = 0; i < 2; i++) {
8337 // see comment in ac_prepare_cube_coords()
8338 Temp deriv_ma;
8339 Temp deriv_sc, deriv_tc;
8340 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8341 &deriv_ma, &deriv_sc, &deriv_tc);
8342
8343 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8344
8345 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8346 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8347 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8348 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8349 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8350 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8351 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8352 }
8353
8354 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8355 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8356 }
8357
8358 if (is_array)
8359 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8360 coords.resize(3);
8361 coords[0] = sc;
8362 coords[1] = tc;
8363 coords[2] = id;
8364 }
8365
8366 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8367 {
8368 if (vec->parent_instr->type != nir_instr_type_alu)
8369 return;
8370 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8371 if (vec_instr->op != nir_op_vec(vec->num_components))
8372 return;
8373
8374 for (unsigned i = 0; i < vec->num_components; i++) {
8375 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8376 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8377 }
8378 }
8379
8380 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8381 {
8382 Builder bld(ctx->program, ctx->block);
8383 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8384 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8385 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8386 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8387 std::vector<Temp> coords;
8388 std::vector<Temp> derivs;
8389 nir_const_value *sample_index_cv = NULL;
8390 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8391 enum glsl_base_type stype;
8392 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8393
8394 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8395 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8396 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8397 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8398
8399 for (unsigned i = 0; i < instr->num_srcs; i++) {
8400 switch (instr->src[i].src_type) {
8401 case nir_tex_src_coord: {
8402 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8403 for (unsigned i = 0; i < coord.size(); i++)
8404 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8405 break;
8406 }
8407 case nir_tex_src_bias:
8408 if (instr->op == nir_texop_txb) {
8409 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8410 has_bias = true;
8411 }
8412 break;
8413 case nir_tex_src_lod: {
8414 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8415
8416 if (val && val->f32 <= 0.0) {
8417 level_zero = true;
8418 } else {
8419 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8420 has_lod = true;
8421 }
8422 break;
8423 }
8424 case nir_tex_src_comparator:
8425 if (instr->is_shadow) {
8426 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8427 has_compare = true;
8428 }
8429 break;
8430 case nir_tex_src_offset:
8431 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8432 get_const_vec(instr->src[i].src.ssa, const_offset);
8433 has_offset = true;
8434 break;
8435 case nir_tex_src_ddx:
8436 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8437 has_ddx = true;
8438 break;
8439 case nir_tex_src_ddy:
8440 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8441 has_ddy = true;
8442 break;
8443 case nir_tex_src_ms_index:
8444 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8445 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8446 has_sample_index = true;
8447 break;
8448 case nir_tex_src_texture_offset:
8449 case nir_tex_src_sampler_offset:
8450 default:
8451 break;
8452 }
8453 }
8454
8455 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8456 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8457
8458 if (instr->op == nir_texop_texture_samples) {
8459 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8460
8461 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8462 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8463 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 */));
8464 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8465
8466 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8467 samples, Operand(1u), bld.scc(is_msaa));
8468 return;
8469 }
8470
8471 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8472 aco_ptr<Instruction> tmp_instr;
8473 Temp acc, pack = Temp();
8474
8475 uint32_t pack_const = 0;
8476 for (unsigned i = 0; i < offset.size(); i++) {
8477 if (!const_offset[i])
8478 continue;
8479 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8480 }
8481
8482 if (offset.type() == RegType::sgpr) {
8483 for (unsigned i = 0; i < offset.size(); i++) {
8484 if (const_offset[i])
8485 continue;
8486
8487 acc = emit_extract_vector(ctx, offset, i, s1);
8488 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8489
8490 if (i) {
8491 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8492 }
8493
8494 if (pack == Temp()) {
8495 pack = acc;
8496 } else {
8497 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8498 }
8499 }
8500
8501 if (pack_const && pack != Temp())
8502 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8503 } else {
8504 for (unsigned i = 0; i < offset.size(); i++) {
8505 if (const_offset[i])
8506 continue;
8507
8508 acc = emit_extract_vector(ctx, offset, i, v1);
8509 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8510
8511 if (i) {
8512 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8513 }
8514
8515 if (pack == Temp()) {
8516 pack = acc;
8517 } else {
8518 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8519 }
8520 }
8521
8522 if (pack_const && pack != Temp())
8523 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8524 }
8525 if (pack_const && pack == Temp())
8526 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8527 else if (pack == Temp())
8528 has_offset = false;
8529 else
8530 offset = pack;
8531 }
8532
8533 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8534 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8535
8536 /* pack derivatives */
8537 if (has_ddx || has_ddy) {
8538 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8539 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8540 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8541 derivs = {ddx, zero, ddy, zero};
8542 } else {
8543 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8544 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8545 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8546 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8547 }
8548 has_derivs = true;
8549 }
8550
8551 if (instr->coord_components > 1 &&
8552 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8553 instr->is_array &&
8554 instr->op != nir_texop_txf)
8555 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8556
8557 if (instr->coord_components > 2 &&
8558 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8559 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8560 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8561 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8562 instr->is_array &&
8563 instr->op != nir_texop_txf &&
8564 instr->op != nir_texop_txf_ms &&
8565 instr->op != nir_texop_fragment_fetch &&
8566 instr->op != nir_texop_fragment_mask_fetch)
8567 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8568
8569 if (ctx->options->chip_class == GFX9 &&
8570 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8571 instr->op != nir_texop_lod && instr->coord_components) {
8572 assert(coords.size() > 0 && coords.size() < 3);
8573
8574 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8575 Operand((uint32_t) 0) :
8576 Operand((uint32_t) 0x3f000000)));
8577 }
8578
8579 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8580
8581 if (instr->op == nir_texop_samples_identical)
8582 resource = fmask_ptr;
8583
8584 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8585 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8586 instr->op != nir_texop_txs &&
8587 instr->op != nir_texop_fragment_fetch &&
8588 instr->op != nir_texop_fragment_mask_fetch) {
8589 assert(has_sample_index);
8590 Operand op(sample_index);
8591 if (sample_index_cv)
8592 op = Operand(sample_index_cv->u32);
8593 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8594 }
8595
8596 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8597 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8598 Temp off = emit_extract_vector(ctx, offset, i, v1);
8599 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8600 }
8601 has_offset = false;
8602 }
8603
8604 /* Build tex instruction */
8605 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8606 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8607 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8608 : 0;
8609 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8610 Temp tmp_dst = dst;
8611
8612 /* gather4 selects the component by dmask and always returns vec4 */
8613 if (instr->op == nir_texop_tg4) {
8614 assert(instr->dest.ssa.num_components == 4);
8615 if (instr->is_shadow)
8616 dmask = 1;
8617 else
8618 dmask = 1 << instr->component;
8619 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8620 tmp_dst = bld.tmp(v4);
8621 } else if (instr->op == nir_texop_samples_identical) {
8622 tmp_dst = bld.tmp(v1);
8623 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8624 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8625 }
8626
8627 aco_ptr<MIMG_instruction> tex;
8628 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8629 if (!has_lod)
8630 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8631
8632 bool div_by_6 = instr->op == nir_texop_txs &&
8633 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8634 instr->is_array &&
8635 (dmask & (1 << 2));
8636 if (tmp_dst.id() == dst.id() && div_by_6)
8637 tmp_dst = bld.tmp(tmp_dst.regClass());
8638
8639 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8640 tex->operands[0] = Operand(resource);
8641 tex->operands[1] = Operand(s4); /* no sampler */
8642 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8643 if (ctx->options->chip_class == GFX9 &&
8644 instr->op == nir_texop_txs &&
8645 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8646 instr->is_array) {
8647 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8648 } else if (instr->op == nir_texop_query_levels) {
8649 tex->dmask = 1 << 3;
8650 } else {
8651 tex->dmask = dmask;
8652 }
8653 tex->da = da;
8654 tex->definitions[0] = Definition(tmp_dst);
8655 tex->dim = dim;
8656 tex->can_reorder = true;
8657 ctx->block->instructions.emplace_back(std::move(tex));
8658
8659 if (div_by_6) {
8660 /* divide 3rd value by 6 by multiplying with magic number */
8661 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8662 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8663 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8664 assert(instr->dest.ssa.num_components == 3);
8665 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8666 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8667 emit_extract_vector(ctx, tmp_dst, 0, v1),
8668 emit_extract_vector(ctx, tmp_dst, 1, v1),
8669 by_6);
8670
8671 }
8672
8673 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8674 return;
8675 }
8676
8677 Temp tg4_compare_cube_wa64 = Temp();
8678
8679 if (tg4_integer_workarounds) {
8680 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8681 tex->operands[0] = Operand(resource);
8682 tex->operands[1] = Operand(s4); /* no sampler */
8683 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8684 tex->dim = dim;
8685 tex->dmask = 0x3;
8686 tex->da = da;
8687 Temp size = bld.tmp(v2);
8688 tex->definitions[0] = Definition(size);
8689 tex->can_reorder = true;
8690 ctx->block->instructions.emplace_back(std::move(tex));
8691 emit_split_vector(ctx, size, size.size());
8692
8693 Temp half_texel[2];
8694 for (unsigned i = 0; i < 2; i++) {
8695 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8696 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8697 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8698 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8699 }
8700
8701 Temp new_coords[2] = {
8702 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8703 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8704 };
8705
8706 if (tg4_integer_cube_workaround) {
8707 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8708 Temp desc[resource.size()];
8709 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8710 Format::PSEUDO, 1, resource.size())};
8711 split->operands[0] = Operand(resource);
8712 for (unsigned i = 0; i < resource.size(); i++) {
8713 desc[i] = bld.tmp(s1);
8714 split->definitions[i] = Definition(desc[i]);
8715 }
8716 ctx->block->instructions.emplace_back(std::move(split));
8717
8718 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8719 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8720 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8721
8722 Temp nfmt;
8723 if (stype == GLSL_TYPE_UINT) {
8724 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8725 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8726 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8727 bld.scc(compare_cube_wa));
8728 } else {
8729 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8730 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8731 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8732 bld.scc(compare_cube_wa));
8733 }
8734 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8735 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8736
8737 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8738
8739 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8740 Operand((uint32_t)C_008F14_NUM_FORMAT));
8741 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8742
8743 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8744 Format::PSEUDO, resource.size(), 1)};
8745 for (unsigned i = 0; i < resource.size(); i++)
8746 vec->operands[i] = Operand(desc[i]);
8747 resource = bld.tmp(resource.regClass());
8748 vec->definitions[0] = Definition(resource);
8749 ctx->block->instructions.emplace_back(std::move(vec));
8750
8751 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8752 new_coords[0], coords[0], tg4_compare_cube_wa64);
8753 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8754 new_coords[1], coords[1], tg4_compare_cube_wa64);
8755 }
8756 coords[0] = new_coords[0];
8757 coords[1] = new_coords[1];
8758 }
8759
8760 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8761 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8762
8763 assert(coords.size() == 1);
8764 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8765 aco_opcode op;
8766 switch (last_bit) {
8767 case 1:
8768 op = aco_opcode::buffer_load_format_x; break;
8769 case 2:
8770 op = aco_opcode::buffer_load_format_xy; break;
8771 case 3:
8772 op = aco_opcode::buffer_load_format_xyz; break;
8773 case 4:
8774 op = aco_opcode::buffer_load_format_xyzw; break;
8775 default:
8776 unreachable("Tex instruction loads more than 4 components.");
8777 }
8778
8779 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8780 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8781 tmp_dst = dst;
8782 else
8783 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8784
8785 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8786 mubuf->operands[0] = Operand(resource);
8787 mubuf->operands[1] = Operand(coords[0]);
8788 mubuf->operands[2] = Operand((uint32_t) 0);
8789 mubuf->definitions[0] = Definition(tmp_dst);
8790 mubuf->idxen = true;
8791 mubuf->can_reorder = true;
8792 ctx->block->instructions.emplace_back(std::move(mubuf));
8793
8794 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8795 return;
8796 }
8797
8798 /* gather MIMG address components */
8799 std::vector<Temp> args;
8800 if (has_offset)
8801 args.emplace_back(offset);
8802 if (has_bias)
8803 args.emplace_back(bias);
8804 if (has_compare)
8805 args.emplace_back(compare);
8806 if (has_derivs)
8807 args.insert(args.end(), derivs.begin(), derivs.end());
8808
8809 args.insert(args.end(), coords.begin(), coords.end());
8810 if (has_sample_index)
8811 args.emplace_back(sample_index);
8812 if (has_lod)
8813 args.emplace_back(lod);
8814
8815 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8816 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8817 vec->definitions[0] = Definition(arg);
8818 for (unsigned i = 0; i < args.size(); i++)
8819 vec->operands[i] = Operand(args[i]);
8820 ctx->block->instructions.emplace_back(std::move(vec));
8821
8822
8823 if (instr->op == nir_texop_txf ||
8824 instr->op == nir_texop_txf_ms ||
8825 instr->op == nir_texop_samples_identical ||
8826 instr->op == nir_texop_fragment_fetch ||
8827 instr->op == nir_texop_fragment_mask_fetch) {
8828 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;
8829 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8830 tex->operands[0] = Operand(resource);
8831 tex->operands[1] = Operand(s4); /* no sampler */
8832 tex->operands[2] = Operand(arg);
8833 tex->dim = dim;
8834 tex->dmask = dmask;
8835 tex->unrm = true;
8836 tex->da = da;
8837 tex->definitions[0] = Definition(tmp_dst);
8838 tex->can_reorder = true;
8839 ctx->block->instructions.emplace_back(std::move(tex));
8840
8841 if (instr->op == nir_texop_samples_identical) {
8842 assert(dmask == 1 && dst.regClass() == v1);
8843 assert(dst.id() != tmp_dst.id());
8844
8845 Temp tmp = bld.tmp(bld.lm);
8846 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8847 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8848
8849 } else {
8850 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8851 }
8852 return;
8853 }
8854
8855 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8856 aco_opcode opcode = aco_opcode::image_sample;
8857 if (has_offset) { /* image_sample_*_o */
8858 if (has_compare) {
8859 opcode = aco_opcode::image_sample_c_o;
8860 if (has_derivs)
8861 opcode = aco_opcode::image_sample_c_d_o;
8862 if (has_bias)
8863 opcode = aco_opcode::image_sample_c_b_o;
8864 if (level_zero)
8865 opcode = aco_opcode::image_sample_c_lz_o;
8866 if (has_lod)
8867 opcode = aco_opcode::image_sample_c_l_o;
8868 } else {
8869 opcode = aco_opcode::image_sample_o;
8870 if (has_derivs)
8871 opcode = aco_opcode::image_sample_d_o;
8872 if (has_bias)
8873 opcode = aco_opcode::image_sample_b_o;
8874 if (level_zero)
8875 opcode = aco_opcode::image_sample_lz_o;
8876 if (has_lod)
8877 opcode = aco_opcode::image_sample_l_o;
8878 }
8879 } else { /* no offset */
8880 if (has_compare) {
8881 opcode = aco_opcode::image_sample_c;
8882 if (has_derivs)
8883 opcode = aco_opcode::image_sample_c_d;
8884 if (has_bias)
8885 opcode = aco_opcode::image_sample_c_b;
8886 if (level_zero)
8887 opcode = aco_opcode::image_sample_c_lz;
8888 if (has_lod)
8889 opcode = aco_opcode::image_sample_c_l;
8890 } else {
8891 opcode = aco_opcode::image_sample;
8892 if (has_derivs)
8893 opcode = aco_opcode::image_sample_d;
8894 if (has_bias)
8895 opcode = aco_opcode::image_sample_b;
8896 if (level_zero)
8897 opcode = aco_opcode::image_sample_lz;
8898 if (has_lod)
8899 opcode = aco_opcode::image_sample_l;
8900 }
8901 }
8902
8903 if (instr->op == nir_texop_tg4) {
8904 if (has_offset) {
8905 opcode = aco_opcode::image_gather4_lz_o;
8906 if (has_compare)
8907 opcode = aco_opcode::image_gather4_c_lz_o;
8908 } else {
8909 opcode = aco_opcode::image_gather4_lz;
8910 if (has_compare)
8911 opcode = aco_opcode::image_gather4_c_lz;
8912 }
8913 } else if (instr->op == nir_texop_lod) {
8914 opcode = aco_opcode::image_get_lod;
8915 }
8916
8917 /* we don't need the bias, sample index, compare value or offset to be
8918 * computed in WQM but if the p_create_vector copies the coordinates, then it
8919 * needs to be in WQM */
8920 if (ctx->stage == fragment_fs &&
8921 !has_derivs && !has_lod && !level_zero &&
8922 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8923 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8924 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8925
8926 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8927 tex->operands[0] = Operand(resource);
8928 tex->operands[1] = Operand(sampler);
8929 tex->operands[2] = Operand(arg);
8930 tex->dim = dim;
8931 tex->dmask = dmask;
8932 tex->da = da;
8933 tex->definitions[0] = Definition(tmp_dst);
8934 tex->can_reorder = true;
8935 ctx->block->instructions.emplace_back(std::move(tex));
8936
8937 if (tg4_integer_cube_workaround) {
8938 assert(tmp_dst.id() != dst.id());
8939 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8940
8941 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8942 Temp val[4];
8943 for (unsigned i = 0; i < dst.size(); i++) {
8944 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8945 Temp cvt_val;
8946 if (stype == GLSL_TYPE_UINT)
8947 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8948 else
8949 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8950 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8951 }
8952 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8953 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8954 val[0], val[1], val[2], val[3]);
8955 }
8956 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8957 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8958
8959 }
8960
8961
8962 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8963 {
8964 Temp tmp = get_ssa_temp(ctx, ssa);
8965 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8966 return Operand(tmp.regClass());
8967 else
8968 return Operand(tmp);
8969 }
8970
8971 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8972 {
8973 aco_ptr<Pseudo_instruction> phi;
8974 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8975 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8976
8977 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8978 logical |= ctx->block->kind & block_kind_merge;
8979 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8980
8981 /* we want a sorted list of sources, since the predecessor list is also sorted */
8982 std::map<unsigned, nir_ssa_def*> phi_src;
8983 nir_foreach_phi_src(src, instr)
8984 phi_src[src->pred->index] = src->src.ssa;
8985
8986 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8987 unsigned num_operands = 0;
8988 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8989 unsigned num_defined = 0;
8990 unsigned cur_pred_idx = 0;
8991 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8992 if (cur_pred_idx < preds.size()) {
8993 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8994 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8995 unsigned skipped = 0;
8996 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8997 skipped++;
8998 if (cur_pred_idx + skipped < preds.size()) {
8999 for (unsigned i = 0; i < skipped; i++)
9000 operands[num_operands++] = Operand(dst.regClass());
9001 cur_pred_idx += skipped;
9002 } else {
9003 continue;
9004 }
9005 }
9006 /* Handle missing predecessors at the end. This shouldn't happen with loop
9007 * headers and we can't ignore these sources for loop header phis. */
9008 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
9009 continue;
9010 cur_pred_idx++;
9011 Operand op = get_phi_operand(ctx, src.second);
9012 operands[num_operands++] = op;
9013 num_defined += !op.isUndefined();
9014 }
9015 /* handle block_kind_continue_or_break at loop exit blocks */
9016 while (cur_pred_idx++ < preds.size())
9017 operands[num_operands++] = Operand(dst.regClass());
9018
9019 /* If the loop ends with a break, still add a linear continue edge in case
9020 * that break is divergent or continue_or_break is used. We'll either remove
9021 * this operand later in visit_loop() if it's not necessary or replace the
9022 * undef with something correct. */
9023 if (!logical && ctx->block->kind & block_kind_loop_header) {
9024 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
9025 nir_block *last = nir_loop_last_block(loop);
9026 if (last->successors[0] != instr->instr.block)
9027 operands[num_operands++] = Operand(RegClass());
9028 }
9029
9030 if (num_defined == 0) {
9031 Builder bld(ctx->program, ctx->block);
9032 if (dst.regClass() == s1) {
9033 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
9034 } else if (dst.regClass() == v1) {
9035 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
9036 } else {
9037 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9038 for (unsigned i = 0; i < dst.size(); i++)
9039 vec->operands[i] = Operand(0u);
9040 vec->definitions[0] = Definition(dst);
9041 ctx->block->instructions.emplace_back(std::move(vec));
9042 }
9043 return;
9044 }
9045
9046 /* we can use a linear phi in some cases if one src is undef */
9047 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9048 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9049
9050 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9051 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9052 assert(invert->kind & block_kind_invert);
9053
9054 unsigned then_block = invert->linear_preds[0];
9055
9056 Block* insert_block = NULL;
9057 for (unsigned i = 0; i < num_operands; i++) {
9058 Operand op = operands[i];
9059 if (op.isUndefined())
9060 continue;
9061 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9062 phi->operands[0] = op;
9063 break;
9064 }
9065 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9066 phi->operands[1] = Operand(dst.regClass());
9067 phi->definitions[0] = Definition(dst);
9068 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9069 return;
9070 }
9071
9072 /* try to scalarize vector phis */
9073 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9074 // TODO: scalarize linear phis on divergent ifs
9075 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9076 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9077 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9078 Operand src = operands[i];
9079 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9080 can_scalarize = false;
9081 }
9082 if (can_scalarize) {
9083 unsigned num_components = instr->dest.ssa.num_components;
9084 assert(dst.size() % num_components == 0);
9085 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9086
9087 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9088 for (unsigned k = 0; k < num_components; k++) {
9089 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9090 for (unsigned i = 0; i < num_operands; i++) {
9091 Operand src = operands[i];
9092 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9093 }
9094 Temp phi_dst = {ctx->program->allocateId(), rc};
9095 phi->definitions[0] = Definition(phi_dst);
9096 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9097 new_vec[k] = phi_dst;
9098 vec->operands[k] = Operand(phi_dst);
9099 }
9100 vec->definitions[0] = Definition(dst);
9101 ctx->block->instructions.emplace_back(std::move(vec));
9102 ctx->allocated_vec.emplace(dst.id(), new_vec);
9103 return;
9104 }
9105 }
9106
9107 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9108 for (unsigned i = 0; i < num_operands; i++)
9109 phi->operands[i] = operands[i];
9110 phi->definitions[0] = Definition(dst);
9111 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9112 }
9113
9114
9115 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9116 {
9117 Temp dst = get_ssa_temp(ctx, &instr->def);
9118
9119 assert(dst.type() == RegType::sgpr);
9120
9121 if (dst.size() == 1) {
9122 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9123 } else {
9124 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9125 for (unsigned i = 0; i < dst.size(); i++)
9126 vec->operands[i] = Operand(0u);
9127 vec->definitions[0] = Definition(dst);
9128 ctx->block->instructions.emplace_back(std::move(vec));
9129 }
9130 }
9131
9132 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9133 {
9134 Builder bld(ctx->program, ctx->block);
9135 Block *logical_target;
9136 append_logical_end(ctx->block);
9137 unsigned idx = ctx->block->index;
9138
9139 switch (instr->type) {
9140 case nir_jump_break:
9141 logical_target = ctx->cf_info.parent_loop.exit;
9142 add_logical_edge(idx, logical_target);
9143 ctx->block->kind |= block_kind_break;
9144
9145 if (!ctx->cf_info.parent_if.is_divergent &&
9146 !ctx->cf_info.parent_loop.has_divergent_continue) {
9147 /* uniform break - directly jump out of the loop */
9148 ctx->block->kind |= block_kind_uniform;
9149 ctx->cf_info.has_branch = true;
9150 bld.branch(aco_opcode::p_branch);
9151 add_linear_edge(idx, logical_target);
9152 return;
9153 }
9154 ctx->cf_info.parent_loop.has_divergent_branch = true;
9155 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9156 break;
9157 case nir_jump_continue:
9158 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9159 add_logical_edge(idx, logical_target);
9160 ctx->block->kind |= block_kind_continue;
9161
9162 if (ctx->cf_info.parent_if.is_divergent) {
9163 /* for potential uniform breaks after this continue,
9164 we must ensure that they are handled correctly */
9165 ctx->cf_info.parent_loop.has_divergent_continue = true;
9166 ctx->cf_info.parent_loop.has_divergent_branch = true;
9167 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9168 } else {
9169 /* uniform continue - directly jump to the loop header */
9170 ctx->block->kind |= block_kind_uniform;
9171 ctx->cf_info.has_branch = true;
9172 bld.branch(aco_opcode::p_branch);
9173 add_linear_edge(idx, logical_target);
9174 return;
9175 }
9176 break;
9177 default:
9178 fprintf(stderr, "Unknown NIR jump instr: ");
9179 nir_print_instr(&instr->instr, stderr);
9180 fprintf(stderr, "\n");
9181 abort();
9182 }
9183
9184 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9185 ctx->cf_info.exec_potentially_empty_break = true;
9186 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9187 }
9188
9189 /* remove critical edges from linear CFG */
9190 bld.branch(aco_opcode::p_branch);
9191 Block* break_block = ctx->program->create_and_insert_block();
9192 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9193 break_block->kind |= block_kind_uniform;
9194 add_linear_edge(idx, break_block);
9195 /* the loop_header pointer might be invalidated by this point */
9196 if (instr->type == nir_jump_continue)
9197 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9198 add_linear_edge(break_block->index, logical_target);
9199 bld.reset(break_block);
9200 bld.branch(aco_opcode::p_branch);
9201
9202 Block* continue_block = ctx->program->create_and_insert_block();
9203 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9204 add_linear_edge(idx, continue_block);
9205 append_logical_start(continue_block);
9206 ctx->block = continue_block;
9207 return;
9208 }
9209
9210 void visit_block(isel_context *ctx, nir_block *block)
9211 {
9212 nir_foreach_instr(instr, block) {
9213 switch (instr->type) {
9214 case nir_instr_type_alu:
9215 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9216 break;
9217 case nir_instr_type_load_const:
9218 visit_load_const(ctx, nir_instr_as_load_const(instr));
9219 break;
9220 case nir_instr_type_intrinsic:
9221 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9222 break;
9223 case nir_instr_type_tex:
9224 visit_tex(ctx, nir_instr_as_tex(instr));
9225 break;
9226 case nir_instr_type_phi:
9227 visit_phi(ctx, nir_instr_as_phi(instr));
9228 break;
9229 case nir_instr_type_ssa_undef:
9230 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9231 break;
9232 case nir_instr_type_deref:
9233 break;
9234 case nir_instr_type_jump:
9235 visit_jump(ctx, nir_instr_as_jump(instr));
9236 break;
9237 default:
9238 fprintf(stderr, "Unknown NIR instr type: ");
9239 nir_print_instr(instr, stderr);
9240 fprintf(stderr, "\n");
9241 //abort();
9242 }
9243 }
9244
9245 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9246 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9247 }
9248
9249
9250
9251 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9252 aco_ptr<Instruction>& header_phi, Operand *vals)
9253 {
9254 vals[0] = Operand(header_phi->definitions[0].getTemp());
9255 RegClass rc = vals[0].regClass();
9256
9257 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9258
9259 unsigned next_pred = 1;
9260
9261 for (unsigned idx = first + 1; idx <= last; idx++) {
9262 Block& block = ctx->program->blocks[idx];
9263 if (block.loop_nest_depth != loop_nest_depth) {
9264 vals[idx - first] = vals[idx - 1 - first];
9265 continue;
9266 }
9267
9268 if (block.kind & block_kind_continue) {
9269 vals[idx - first] = header_phi->operands[next_pred];
9270 next_pred++;
9271 continue;
9272 }
9273
9274 bool all_same = true;
9275 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9276 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9277
9278 Operand val;
9279 if (all_same) {
9280 val = vals[block.linear_preds[0] - first];
9281 } else {
9282 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9283 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9284 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9285 phi->operands[i] = vals[block.linear_preds[i] - first];
9286 val = Operand(Temp(ctx->program->allocateId(), rc));
9287 phi->definitions[0] = Definition(val.getTemp());
9288 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9289 }
9290 vals[idx - first] = val;
9291 }
9292
9293 return vals[last - first];
9294 }
9295
9296 static void visit_loop(isel_context *ctx, nir_loop *loop)
9297 {
9298 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9299 append_logical_end(ctx->block);
9300 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9301 Builder bld(ctx->program, ctx->block);
9302 bld.branch(aco_opcode::p_branch);
9303 unsigned loop_preheader_idx = ctx->block->index;
9304
9305 Block loop_exit = Block();
9306 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9307 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9308
9309 Block* loop_header = ctx->program->create_and_insert_block();
9310 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9311 loop_header->kind |= block_kind_loop_header;
9312 add_edge(loop_preheader_idx, loop_header);
9313 ctx->block = loop_header;
9314
9315 /* emit loop body */
9316 unsigned loop_header_idx = loop_header->index;
9317 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9318 append_logical_start(ctx->block);
9319 bool unreachable = visit_cf_list(ctx, &loop->body);
9320
9321 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9322 if (!ctx->cf_info.has_branch) {
9323 append_logical_end(ctx->block);
9324 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9325 /* Discards can result in code running with an empty exec mask.
9326 * This would result in divergent breaks not ever being taken. As a
9327 * workaround, break the loop when the loop mask is empty instead of
9328 * always continuing. */
9329 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9330 unsigned block_idx = ctx->block->index;
9331
9332 /* create helper blocks to avoid critical edges */
9333 Block *break_block = ctx->program->create_and_insert_block();
9334 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9335 break_block->kind = block_kind_uniform;
9336 bld.reset(break_block);
9337 bld.branch(aco_opcode::p_branch);
9338 add_linear_edge(block_idx, break_block);
9339 add_linear_edge(break_block->index, &loop_exit);
9340
9341 Block *continue_block = ctx->program->create_and_insert_block();
9342 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9343 continue_block->kind = block_kind_uniform;
9344 bld.reset(continue_block);
9345 bld.branch(aco_opcode::p_branch);
9346 add_linear_edge(block_idx, continue_block);
9347 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9348
9349 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9350 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9351 ctx->block = &ctx->program->blocks[block_idx];
9352 } else {
9353 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9354 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9355 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9356 else
9357 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9358 }
9359
9360 bld.reset(ctx->block);
9361 bld.branch(aco_opcode::p_branch);
9362 }
9363
9364 /* Fixup phis in loop header from unreachable blocks.
9365 * has_branch/has_divergent_branch also indicates if the loop ends with a
9366 * break/continue instruction, but we don't emit those if unreachable=true */
9367 if (unreachable) {
9368 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9369 bool linear = ctx->cf_info.has_branch;
9370 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9371 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9372 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9373 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9374 /* the last operand should be the one that needs to be removed */
9375 instr->operands.pop_back();
9376 } else if (!is_phi(instr)) {
9377 break;
9378 }
9379 }
9380 }
9381
9382 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9383 * and the previous one shouldn't both happen at once because a break in the
9384 * merge block would get CSE'd */
9385 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9386 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9387 Operand vals[num_vals];
9388 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9389 if (instr->opcode == aco_opcode::p_linear_phi) {
9390 if (ctx->cf_info.has_branch)
9391 instr->operands.pop_back();
9392 else
9393 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9394 } else if (!is_phi(instr)) {
9395 break;
9396 }
9397 }
9398 }
9399
9400 ctx->cf_info.has_branch = false;
9401
9402 // TODO: if the loop has not a single exit, we must add one °°
9403 /* emit loop successor block */
9404 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9405 append_logical_start(ctx->block);
9406
9407 #if 0
9408 // TODO: check if it is beneficial to not branch on continues
9409 /* trim linear phis in loop header */
9410 for (auto&& instr : loop_entry->instructions) {
9411 if (instr->opcode == aco_opcode::p_linear_phi) {
9412 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9413 new_phi->definitions[0] = instr->definitions[0];
9414 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9415 new_phi->operands[i] = instr->operands[i];
9416 /* check that the remaining operands are all the same */
9417 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9418 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9419 instr.swap(new_phi);
9420 } else if (instr->opcode == aco_opcode::p_phi) {
9421 continue;
9422 } else {
9423 break;
9424 }
9425 }
9426 #endif
9427 }
9428
9429 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9430 {
9431 ic->cond = cond;
9432
9433 append_logical_end(ctx->block);
9434 ctx->block->kind |= block_kind_branch;
9435
9436 /* branch to linear then block */
9437 assert(cond.regClass() == ctx->program->lane_mask);
9438 aco_ptr<Pseudo_branch_instruction> branch;
9439 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9440 branch->operands[0] = Operand(cond);
9441 ctx->block->instructions.push_back(std::move(branch));
9442
9443 ic->BB_if_idx = ctx->block->index;
9444 ic->BB_invert = Block();
9445 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9446 /* Invert blocks are intentionally not marked as top level because they
9447 * are not part of the logical cfg. */
9448 ic->BB_invert.kind |= block_kind_invert;
9449 ic->BB_endif = Block();
9450 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9451 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9452
9453 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9454 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9455 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9456 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9457 ctx->cf_info.parent_if.is_divergent = true;
9458
9459 /* divergent branches use cbranch_execz */
9460 ctx->cf_info.exec_potentially_empty_discard = false;
9461 ctx->cf_info.exec_potentially_empty_break = false;
9462 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9463
9464 /** emit logical then block */
9465 Block* BB_then_logical = ctx->program->create_and_insert_block();
9466 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9467 add_edge(ic->BB_if_idx, BB_then_logical);
9468 ctx->block = BB_then_logical;
9469 append_logical_start(BB_then_logical);
9470 }
9471
9472 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9473 {
9474 Block *BB_then_logical = ctx->block;
9475 append_logical_end(BB_then_logical);
9476 /* branch from logical then block to invert block */
9477 aco_ptr<Pseudo_branch_instruction> branch;
9478 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9479 BB_then_logical->instructions.emplace_back(std::move(branch));
9480 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9481 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9482 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9483 BB_then_logical->kind |= block_kind_uniform;
9484 assert(!ctx->cf_info.has_branch);
9485 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9486 ctx->cf_info.parent_loop.has_divergent_branch = false;
9487
9488 /** emit linear then block */
9489 Block* BB_then_linear = ctx->program->create_and_insert_block();
9490 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9491 BB_then_linear->kind |= block_kind_uniform;
9492 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9493 /* branch from linear then block to invert block */
9494 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9495 BB_then_linear->instructions.emplace_back(std::move(branch));
9496 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9497
9498 /** emit invert merge block */
9499 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9500 ic->invert_idx = ctx->block->index;
9501
9502 /* branch to linear else block (skip else) */
9503 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9504 branch->operands[0] = Operand(ic->cond);
9505 ctx->block->instructions.push_back(std::move(branch));
9506
9507 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9508 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9509 ic->exec_potentially_empty_break_depth_old =
9510 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9511 /* divergent branches use cbranch_execz */
9512 ctx->cf_info.exec_potentially_empty_discard = false;
9513 ctx->cf_info.exec_potentially_empty_break = false;
9514 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9515
9516 /** emit logical else block */
9517 Block* BB_else_logical = ctx->program->create_and_insert_block();
9518 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9519 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9520 add_linear_edge(ic->invert_idx, BB_else_logical);
9521 ctx->block = BB_else_logical;
9522 append_logical_start(BB_else_logical);
9523 }
9524
9525 static void end_divergent_if(isel_context *ctx, if_context *ic)
9526 {
9527 Block *BB_else_logical = ctx->block;
9528 append_logical_end(BB_else_logical);
9529
9530 /* branch from logical else block to endif block */
9531 aco_ptr<Pseudo_branch_instruction> branch;
9532 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9533 BB_else_logical->instructions.emplace_back(std::move(branch));
9534 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9535 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9536 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9537 BB_else_logical->kind |= block_kind_uniform;
9538
9539 assert(!ctx->cf_info.has_branch);
9540 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9541
9542
9543 /** emit linear else block */
9544 Block* BB_else_linear = ctx->program->create_and_insert_block();
9545 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9546 BB_else_linear->kind |= block_kind_uniform;
9547 add_linear_edge(ic->invert_idx, BB_else_linear);
9548
9549 /* branch from linear else block to endif block */
9550 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9551 BB_else_linear->instructions.emplace_back(std::move(branch));
9552 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9553
9554
9555 /** emit endif merge block */
9556 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9557 append_logical_start(ctx->block);
9558
9559
9560 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9561 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9562 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9563 ctx->cf_info.exec_potentially_empty_break_depth =
9564 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9565 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9566 !ctx->cf_info.parent_if.is_divergent) {
9567 ctx->cf_info.exec_potentially_empty_break = false;
9568 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9569 }
9570 /* uniform control flow never has an empty exec-mask */
9571 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9572 ctx->cf_info.exec_potentially_empty_discard = false;
9573 ctx->cf_info.exec_potentially_empty_break = false;
9574 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9575 }
9576 }
9577
9578 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9579 {
9580 assert(cond.regClass() == s1);
9581
9582 append_logical_end(ctx->block);
9583 ctx->block->kind |= block_kind_uniform;
9584
9585 aco_ptr<Pseudo_branch_instruction> branch;
9586 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9587 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9588 branch->operands[0] = Operand(cond);
9589 branch->operands[0].setFixed(scc);
9590 ctx->block->instructions.emplace_back(std::move(branch));
9591
9592 ic->BB_if_idx = ctx->block->index;
9593 ic->BB_endif = Block();
9594 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9595 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9596
9597 ctx->cf_info.has_branch = false;
9598 ctx->cf_info.parent_loop.has_divergent_branch = false;
9599
9600 /** emit then block */
9601 Block* BB_then = ctx->program->create_and_insert_block();
9602 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9603 add_edge(ic->BB_if_idx, BB_then);
9604 append_logical_start(BB_then);
9605 ctx->block = BB_then;
9606 }
9607
9608 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9609 {
9610 Block *BB_then = ctx->block;
9611
9612 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9613 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9614
9615 if (!ic->uniform_has_then_branch) {
9616 append_logical_end(BB_then);
9617 /* branch from then block to endif block */
9618 aco_ptr<Pseudo_branch_instruction> branch;
9619 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9620 BB_then->instructions.emplace_back(std::move(branch));
9621 add_linear_edge(BB_then->index, &ic->BB_endif);
9622 if (!ic->then_branch_divergent)
9623 add_logical_edge(BB_then->index, &ic->BB_endif);
9624 BB_then->kind |= block_kind_uniform;
9625 }
9626
9627 ctx->cf_info.has_branch = false;
9628 ctx->cf_info.parent_loop.has_divergent_branch = false;
9629
9630 /** emit else block */
9631 Block* BB_else = ctx->program->create_and_insert_block();
9632 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9633 add_edge(ic->BB_if_idx, BB_else);
9634 append_logical_start(BB_else);
9635 ctx->block = BB_else;
9636 }
9637
9638 static void end_uniform_if(isel_context *ctx, if_context *ic)
9639 {
9640 Block *BB_else = ctx->block;
9641
9642 if (!ctx->cf_info.has_branch) {
9643 append_logical_end(BB_else);
9644 /* branch from then block to endif block */
9645 aco_ptr<Pseudo_branch_instruction> branch;
9646 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9647 BB_else->instructions.emplace_back(std::move(branch));
9648 add_linear_edge(BB_else->index, &ic->BB_endif);
9649 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9650 add_logical_edge(BB_else->index, &ic->BB_endif);
9651 BB_else->kind |= block_kind_uniform;
9652 }
9653
9654 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9655 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9656
9657 /** emit endif merge block */
9658 if (!ctx->cf_info.has_branch) {
9659 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9660 append_logical_start(ctx->block);
9661 }
9662 }
9663
9664 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9665 {
9666 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9667 Builder bld(ctx->program, ctx->block);
9668 aco_ptr<Pseudo_branch_instruction> branch;
9669 if_context ic;
9670
9671 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9672 /**
9673 * Uniform conditionals are represented in the following way*) :
9674 *
9675 * The linear and logical CFG:
9676 * BB_IF
9677 * / \
9678 * BB_THEN (logical) BB_ELSE (logical)
9679 * \ /
9680 * BB_ENDIF
9681 *
9682 * *) Exceptions may be due to break and continue statements within loops
9683 * If a break/continue happens within uniform control flow, it branches
9684 * to the loop exit/entry block. Otherwise, it branches to the next
9685 * merge block.
9686 **/
9687
9688 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9689 assert(cond.regClass() == ctx->program->lane_mask);
9690 cond = bool_to_scalar_condition(ctx, cond);
9691
9692 begin_uniform_if_then(ctx, &ic, cond);
9693 visit_cf_list(ctx, &if_stmt->then_list);
9694
9695 begin_uniform_if_else(ctx, &ic);
9696 visit_cf_list(ctx, &if_stmt->else_list);
9697
9698 end_uniform_if(ctx, &ic);
9699
9700 return !ctx->cf_info.has_branch;
9701 } else { /* non-uniform condition */
9702 /**
9703 * To maintain a logical and linear CFG without critical edges,
9704 * non-uniform conditionals are represented in the following way*) :
9705 *
9706 * The linear CFG:
9707 * BB_IF
9708 * / \
9709 * BB_THEN (logical) BB_THEN (linear)
9710 * \ /
9711 * BB_INVERT (linear)
9712 * / \
9713 * BB_ELSE (logical) BB_ELSE (linear)
9714 * \ /
9715 * BB_ENDIF
9716 *
9717 * The logical CFG:
9718 * BB_IF
9719 * / \
9720 * BB_THEN (logical) BB_ELSE (logical)
9721 * \ /
9722 * BB_ENDIF
9723 *
9724 * *) Exceptions may be due to break and continue statements within loops
9725 **/
9726
9727 begin_divergent_if_then(ctx, &ic, cond);
9728 visit_cf_list(ctx, &if_stmt->then_list);
9729
9730 begin_divergent_if_else(ctx, &ic);
9731 visit_cf_list(ctx, &if_stmt->else_list);
9732
9733 end_divergent_if(ctx, &ic);
9734
9735 return true;
9736 }
9737 }
9738
9739 static bool visit_cf_list(isel_context *ctx,
9740 struct exec_list *list)
9741 {
9742 foreach_list_typed(nir_cf_node, node, node, list) {
9743 switch (node->type) {
9744 case nir_cf_node_block:
9745 visit_block(ctx, nir_cf_node_as_block(node));
9746 break;
9747 case nir_cf_node_if:
9748 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9749 return true;
9750 break;
9751 case nir_cf_node_loop:
9752 visit_loop(ctx, nir_cf_node_as_loop(node));
9753 break;
9754 default:
9755 unreachable("unimplemented cf list type");
9756 }
9757 }
9758 return false;
9759 }
9760
9761 static void create_null_export(isel_context *ctx)
9762 {
9763 /* Some shader stages always need to have exports.
9764 * So when there is none, we need to add a null export.
9765 */
9766
9767 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9768 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9769 Builder bld(ctx->program, ctx->block);
9770 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9771 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9772 }
9773
9774 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9775 {
9776 assert(ctx->stage == vertex_vs ||
9777 ctx->stage == tess_eval_vs ||
9778 ctx->stage == gs_copy_vs ||
9779 ctx->stage == ngg_vertex_gs ||
9780 ctx->stage == ngg_tess_eval_gs);
9781
9782 int offset = (ctx->stage & sw_tes)
9783 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9784 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9785 uint64_t mask = ctx->outputs.mask[slot];
9786 if (!is_pos && !mask)
9787 return false;
9788 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9789 return false;
9790 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9791 exp->enabled_mask = mask;
9792 for (unsigned i = 0; i < 4; ++i) {
9793 if (mask & (1 << i))
9794 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9795 else
9796 exp->operands[i] = Operand(v1);
9797 }
9798 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9799 * Setting valid_mask=1 prevents it and has no other effect.
9800 */
9801 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9802 exp->done = false;
9803 exp->compressed = false;
9804 if (is_pos)
9805 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9806 else
9807 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9808 ctx->block->instructions.emplace_back(std::move(exp));
9809
9810 return true;
9811 }
9812
9813 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9814 {
9815 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9816 exp->enabled_mask = 0;
9817 for (unsigned i = 0; i < 4; ++i)
9818 exp->operands[i] = Operand(v1);
9819 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9820 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9821 exp->enabled_mask |= 0x1;
9822 }
9823 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9824 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9825 exp->enabled_mask |= 0x4;
9826 }
9827 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9828 if (ctx->options->chip_class < GFX9) {
9829 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9830 exp->enabled_mask |= 0x8;
9831 } else {
9832 Builder bld(ctx->program, ctx->block);
9833
9834 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9835 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9836 if (exp->operands[2].isTemp())
9837 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9838
9839 exp->operands[2] = Operand(out);
9840 exp->enabled_mask |= 0x4;
9841 }
9842 }
9843 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9844 exp->done = false;
9845 exp->compressed = false;
9846 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9847 ctx->block->instructions.emplace_back(std::move(exp));
9848 }
9849
9850 static void create_export_phis(isel_context *ctx)
9851 {
9852 /* Used when exports are needed, but the output temps are defined in a preceding block.
9853 * This function will set up phis in order to access the outputs in the next block.
9854 */
9855
9856 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9857 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9858 ctx->block->instructions.pop_back();
9859
9860 Builder bld(ctx->program, ctx->block);
9861
9862 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9863 uint64_t mask = ctx->outputs.mask[slot];
9864 for (unsigned i = 0; i < 4; ++i) {
9865 if (!(mask & (1 << i)))
9866 continue;
9867
9868 Temp old = ctx->outputs.temps[slot * 4 + i];
9869 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9870 ctx->outputs.temps[slot * 4 + i] = phi;
9871 }
9872 }
9873
9874 bld.insert(std::move(logical_start));
9875 }
9876
9877 static void create_vs_exports(isel_context *ctx)
9878 {
9879 assert(ctx->stage == vertex_vs ||
9880 ctx->stage == tess_eval_vs ||
9881 ctx->stage == gs_copy_vs ||
9882 ctx->stage == ngg_vertex_gs ||
9883 ctx->stage == ngg_tess_eval_gs);
9884
9885 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9886 ? &ctx->program->info->tes.outinfo
9887 : &ctx->program->info->vs.outinfo;
9888
9889 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9890 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9891 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9892 }
9893
9894 if (ctx->options->key.has_multiview_view_index) {
9895 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9896 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9897 }
9898
9899 /* the order these position exports are created is important */
9900 int next_pos = 0;
9901 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9902 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9903 export_vs_psiz_layer_viewport(ctx, &next_pos);
9904 exported_pos = true;
9905 }
9906 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9907 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9908 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9909 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9910
9911 if (ctx->export_clip_dists) {
9912 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9913 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9914 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9915 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9916 }
9917
9918 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9919 if (i < VARYING_SLOT_VAR0 &&
9920 i != VARYING_SLOT_LAYER &&
9921 i != VARYING_SLOT_PRIMITIVE_ID &&
9922 i != VARYING_SLOT_VIEWPORT)
9923 continue;
9924
9925 export_vs_varying(ctx, i, false, NULL);
9926 }
9927
9928 if (!exported_pos)
9929 create_null_export(ctx);
9930 }
9931
9932 static bool export_fs_mrt_z(isel_context *ctx)
9933 {
9934 Builder bld(ctx->program, ctx->block);
9935 unsigned enabled_channels = 0;
9936 bool compr = false;
9937 Operand values[4];
9938
9939 for (unsigned i = 0; i < 4; ++i) {
9940 values[i] = Operand(v1);
9941 }
9942
9943 /* Both stencil and sample mask only need 16-bits. */
9944 if (!ctx->program->info->ps.writes_z &&
9945 (ctx->program->info->ps.writes_stencil ||
9946 ctx->program->info->ps.writes_sample_mask)) {
9947 compr = true; /* COMPR flag */
9948
9949 if (ctx->program->info->ps.writes_stencil) {
9950 /* Stencil should be in X[23:16]. */
9951 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9952 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9953 enabled_channels |= 0x3;
9954 }
9955
9956 if (ctx->program->info->ps.writes_sample_mask) {
9957 /* SampleMask should be in Y[15:0]. */
9958 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9959 enabled_channels |= 0xc;
9960 }
9961 } else {
9962 if (ctx->program->info->ps.writes_z) {
9963 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9964 enabled_channels |= 0x1;
9965 }
9966
9967 if (ctx->program->info->ps.writes_stencil) {
9968 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9969 enabled_channels |= 0x2;
9970 }
9971
9972 if (ctx->program->info->ps.writes_sample_mask) {
9973 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9974 enabled_channels |= 0x4;
9975 }
9976 }
9977
9978 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9979 * writemask component.
9980 */
9981 if (ctx->options->chip_class == GFX6 &&
9982 ctx->options->family != CHIP_OLAND &&
9983 ctx->options->family != CHIP_HAINAN) {
9984 enabled_channels |= 0x1;
9985 }
9986
9987 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9988 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9989
9990 return true;
9991 }
9992
9993 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9994 {
9995 Builder bld(ctx->program, ctx->block);
9996 unsigned write_mask = ctx->outputs.mask[slot];
9997 Operand values[4];
9998
9999 for (unsigned i = 0; i < 4; ++i) {
10000 if (write_mask & (1 << i)) {
10001 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
10002 } else {
10003 values[i] = Operand(v1);
10004 }
10005 }
10006
10007 unsigned target, col_format;
10008 unsigned enabled_channels = 0;
10009 aco_opcode compr_op = (aco_opcode)0;
10010
10011 slot -= FRAG_RESULT_DATA0;
10012 target = V_008DFC_SQ_EXP_MRT + slot;
10013 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
10014
10015 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
10016 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
10017
10018 switch (col_format)
10019 {
10020 case V_028714_SPI_SHADER_ZERO:
10021 enabled_channels = 0; /* writemask */
10022 target = V_008DFC_SQ_EXP_NULL;
10023 break;
10024
10025 case V_028714_SPI_SHADER_32_R:
10026 enabled_channels = 1;
10027 break;
10028
10029 case V_028714_SPI_SHADER_32_GR:
10030 enabled_channels = 0x3;
10031 break;
10032
10033 case V_028714_SPI_SHADER_32_AR:
10034 if (ctx->options->chip_class >= GFX10) {
10035 /* Special case: on GFX10, the outputs are different for 32_AR */
10036 enabled_channels = 0x3;
10037 values[1] = values[3];
10038 values[3] = Operand(v1);
10039 } else {
10040 enabled_channels = 0x9;
10041 }
10042 break;
10043
10044 case V_028714_SPI_SHADER_FP16_ABGR:
10045 enabled_channels = 0x5;
10046 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
10047 break;
10048
10049 case V_028714_SPI_SHADER_UNORM16_ABGR:
10050 enabled_channels = 0x5;
10051 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10052 break;
10053
10054 case V_028714_SPI_SHADER_SNORM16_ABGR:
10055 enabled_channels = 0x5;
10056 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10057 break;
10058
10059 case V_028714_SPI_SHADER_UINT16_ABGR: {
10060 enabled_channels = 0x5;
10061 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10062 if (is_int8 || is_int10) {
10063 /* clamp */
10064 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10065 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10066
10067 for (unsigned i = 0; i < 4; i++) {
10068 if ((write_mask >> i) & 1) {
10069 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10070 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10071 values[i]);
10072 }
10073 }
10074 }
10075 break;
10076 }
10077
10078 case V_028714_SPI_SHADER_SINT16_ABGR:
10079 enabled_channels = 0x5;
10080 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10081 if (is_int8 || is_int10) {
10082 /* clamp */
10083 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10084 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10085 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10086 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10087
10088 for (unsigned i = 0; i < 4; i++) {
10089 if ((write_mask >> i) & 1) {
10090 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10091 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10092 values[i]);
10093 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10094 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10095 values[i]);
10096 }
10097 }
10098 }
10099 break;
10100
10101 case V_028714_SPI_SHADER_32_ABGR:
10102 enabled_channels = 0xF;
10103 break;
10104
10105 default:
10106 break;
10107 }
10108
10109 if (target == V_008DFC_SQ_EXP_NULL)
10110 return false;
10111
10112 if ((bool) compr_op) {
10113 for (int i = 0; i < 2; i++) {
10114 /* check if at least one of the values to be compressed is enabled */
10115 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10116 if (enabled) {
10117 enabled_channels |= enabled << (i*2);
10118 values[i] = bld.vop3(compr_op, bld.def(v1),
10119 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10120 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10121 } else {
10122 values[i] = Operand(v1);
10123 }
10124 }
10125 values[2] = Operand(v1);
10126 values[3] = Operand(v1);
10127 } else {
10128 for (int i = 0; i < 4; i++)
10129 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10130 }
10131
10132 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10133 enabled_channels, target, (bool) compr_op);
10134 return true;
10135 }
10136
10137 static void create_fs_exports(isel_context *ctx)
10138 {
10139 bool exported = false;
10140
10141 /* Export depth, stencil and sample mask. */
10142 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10143 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10144 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10145 exported |= export_fs_mrt_z(ctx);
10146
10147 /* Export all color render targets. */
10148 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10149 if (ctx->outputs.mask[i])
10150 exported |= export_fs_mrt_color(ctx, i);
10151
10152 if (!exported)
10153 create_null_export(ctx);
10154 }
10155
10156 static void write_tcs_tess_factors(isel_context *ctx)
10157 {
10158 unsigned outer_comps;
10159 unsigned inner_comps;
10160
10161 switch (ctx->args->options->key.tcs.primitive_mode) {
10162 case GL_ISOLINES:
10163 outer_comps = 2;
10164 inner_comps = 0;
10165 break;
10166 case GL_TRIANGLES:
10167 outer_comps = 3;
10168 inner_comps = 1;
10169 break;
10170 case GL_QUADS:
10171 outer_comps = 4;
10172 inner_comps = 2;
10173 break;
10174 default:
10175 return;
10176 }
10177
10178 Builder bld(ctx->program, ctx->block);
10179
10180 bld.barrier(aco_opcode::p_memory_barrier_shared);
10181 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10182 bld.sopp(aco_opcode::s_barrier);
10183
10184 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10185 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10186
10187 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10188 if_context ic_invocation_id_is_zero;
10189 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10190 bld.reset(ctx->block);
10191
10192 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));
10193
10194 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10195 unsigned stride = inner_comps + outer_comps;
10196 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10197 Temp tf_inner_vec;
10198 Temp tf_outer_vec;
10199 Temp out[6];
10200 assert(stride <= (sizeof(out) / sizeof(Temp)));
10201
10202 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10203 // LINES reversal
10204 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10205 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10206 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10207 } else {
10208 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);
10209 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);
10210
10211 for (unsigned i = 0; i < outer_comps; ++i)
10212 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10213 for (unsigned i = 0; i < inner_comps; ++i)
10214 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10215 }
10216
10217 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10218 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10219 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10220 unsigned tf_const_offset = 0;
10221
10222 if (ctx->program->chip_class <= GFX8) {
10223 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);
10224 if_context ic_rel_patch_id_is_zero;
10225 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10226 bld.reset(ctx->block);
10227
10228 /* Store the dynamic HS control word. */
10229 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10230 bld.mubuf(aco_opcode::buffer_store_dword,
10231 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10232 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10233 /* disable_wqm */ false, /* glc */ true);
10234 tf_const_offset += 4;
10235
10236 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10237 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10238 bld.reset(ctx->block);
10239 }
10240
10241 assert(stride == 2 || stride == 4 || stride == 6);
10242 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10243 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10244
10245 /* Store to offchip for TES to read - only if TES reads them */
10246 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10247 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));
10248 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10249
10250 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10251 store_vmem_mubuf(ctx, tf_outer_vec, hs_ring_tess_offchip, vmem_offs_outer.first, oc_lds, vmem_offs_outer.second, 4, (1 << outer_comps) - 1, true, false);
10252
10253 if (likely(inner_comps)) {
10254 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10255 store_vmem_mubuf(ctx, tf_inner_vec, hs_ring_tess_offchip, vmem_offs_inner.first, oc_lds, vmem_offs_inner.second, 4, (1 << inner_comps) - 1, true, false);
10256 }
10257 }
10258
10259 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10260 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10261 }
10262
10263 static void emit_stream_output(isel_context *ctx,
10264 Temp const *so_buffers,
10265 Temp const *so_write_offset,
10266 const struct radv_stream_output *output)
10267 {
10268 unsigned num_comps = util_bitcount(output->component_mask);
10269 unsigned writemask = (1 << num_comps) - 1;
10270 unsigned loc = output->location;
10271 unsigned buf = output->buffer;
10272
10273 assert(num_comps && num_comps <= 4);
10274 if (!num_comps || num_comps > 4)
10275 return;
10276
10277 unsigned start = ffs(output->component_mask) - 1;
10278
10279 Temp out[4];
10280 bool all_undef = true;
10281 assert(ctx->stage & hw_vs);
10282 for (unsigned i = 0; i < num_comps; i++) {
10283 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10284 all_undef = all_undef && !out[i].id();
10285 }
10286 if (all_undef)
10287 return;
10288
10289 while (writemask) {
10290 int start, count;
10291 u_bit_scan_consecutive_range(&writemask, &start, &count);
10292 if (count == 3 && ctx->options->chip_class == GFX6) {
10293 /* GFX6 doesn't support storing vec3, split it. */
10294 writemask |= 1u << (start + 2);
10295 count = 2;
10296 }
10297
10298 unsigned offset = output->offset + start * 4;
10299
10300 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10301 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10302 for (int i = 0; i < count; ++i)
10303 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10304 vec->definitions[0] = Definition(write_data);
10305 ctx->block->instructions.emplace_back(std::move(vec));
10306
10307 aco_opcode opcode;
10308 switch (count) {
10309 case 1:
10310 opcode = aco_opcode::buffer_store_dword;
10311 break;
10312 case 2:
10313 opcode = aco_opcode::buffer_store_dwordx2;
10314 break;
10315 case 3:
10316 opcode = aco_opcode::buffer_store_dwordx3;
10317 break;
10318 case 4:
10319 opcode = aco_opcode::buffer_store_dwordx4;
10320 break;
10321 default:
10322 unreachable("Unsupported dword count.");
10323 }
10324
10325 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10326 store->operands[0] = Operand(so_buffers[buf]);
10327 store->operands[1] = Operand(so_write_offset[buf]);
10328 store->operands[2] = Operand((uint32_t) 0);
10329 store->operands[3] = Operand(write_data);
10330 if (offset > 4095) {
10331 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10332 Builder bld(ctx->program, ctx->block);
10333 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10334 } else {
10335 store->offset = offset;
10336 }
10337 store->offen = true;
10338 store->glc = true;
10339 store->dlc = false;
10340 store->slc = true;
10341 store->can_reorder = true;
10342 ctx->block->instructions.emplace_back(std::move(store));
10343 }
10344 }
10345
10346 static void emit_streamout(isel_context *ctx, unsigned stream)
10347 {
10348 Builder bld(ctx->program, ctx->block);
10349
10350 Temp so_buffers[4];
10351 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10352 for (unsigned i = 0; i < 4; i++) {
10353 unsigned stride = ctx->program->info->so.strides[i];
10354 if (!stride)
10355 continue;
10356
10357 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10358 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10359 }
10360
10361 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10362 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10363
10364 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10365
10366 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10367
10368 if_context ic;
10369 begin_divergent_if_then(ctx, &ic, can_emit);
10370
10371 bld.reset(ctx->block);
10372
10373 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10374
10375 Temp so_write_offset[4];
10376
10377 for (unsigned i = 0; i < 4; i++) {
10378 unsigned stride = ctx->program->info->so.strides[i];
10379 if (!stride)
10380 continue;
10381
10382 if (stride == 1) {
10383 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10384 get_arg(ctx, ctx->args->streamout_write_idx),
10385 get_arg(ctx, ctx->args->streamout_offset[i]));
10386 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10387
10388 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10389 } else {
10390 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10391 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10392 get_arg(ctx, ctx->args->streamout_offset[i]));
10393 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10394 }
10395 }
10396
10397 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10398 struct radv_stream_output *output =
10399 &ctx->program->info->so.outputs[i];
10400 if (stream != output->stream)
10401 continue;
10402
10403 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10404 }
10405
10406 begin_divergent_if_else(ctx, &ic);
10407 end_divergent_if(ctx, &ic);
10408 }
10409
10410 } /* end namespace */
10411
10412 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10413 {
10414 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10415 Builder bld(ctx->program, ctx->block);
10416 constexpr unsigned hs_idx = 1u;
10417 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10418 get_arg(ctx, ctx->args->merged_wave_info),
10419 Operand((8u << 16) | (hs_idx * 8u)));
10420 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10421
10422 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10423
10424 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10425 get_arg(ctx, ctx->args->rel_auto_id),
10426 get_arg(ctx, ctx->args->ac.instance_id),
10427 ls_has_nonzero_hs_threads);
10428 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10429 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10430 get_arg(ctx, ctx->args->rel_auto_id),
10431 ls_has_nonzero_hs_threads);
10432 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10433 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10434 get_arg(ctx, ctx->args->ac.vertex_id),
10435 ls_has_nonzero_hs_threads);
10436
10437 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10438 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10439 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10440 }
10441
10442 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10443 {
10444 /* Split all arguments except for the first (ring_offsets) and the last
10445 * (exec) so that the dead channels don't stay live throughout the program.
10446 */
10447 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10448 if (startpgm->definitions[i].regClass().size() > 1) {
10449 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10450 startpgm->definitions[i].regClass().size());
10451 }
10452 }
10453 }
10454
10455 void handle_bc_optimize(isel_context *ctx)
10456 {
10457 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10458 Builder bld(ctx->program, ctx->block);
10459 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10460 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10461 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10462 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10463 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10464 if (uses_center && uses_centroid) {
10465 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10466 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10467
10468 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10469 Temp new_coord[2];
10470 for (unsigned i = 0; i < 2; i++) {
10471 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10472 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10473 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10474 persp_centroid, persp_center, sel);
10475 }
10476 ctx->persp_centroid = bld.tmp(v2);
10477 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10478 Operand(new_coord[0]), Operand(new_coord[1]));
10479 emit_split_vector(ctx, ctx->persp_centroid, 2);
10480 }
10481
10482 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10483 Temp new_coord[2];
10484 for (unsigned i = 0; i < 2; i++) {
10485 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10486 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10487 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10488 linear_centroid, linear_center, sel);
10489 }
10490 ctx->linear_centroid = bld.tmp(v2);
10491 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10492 Operand(new_coord[0]), Operand(new_coord[1]));
10493 emit_split_vector(ctx, ctx->linear_centroid, 2);
10494 }
10495 }
10496 }
10497
10498 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10499 {
10500 Program *program = ctx->program;
10501
10502 unsigned float_controls = shader->info.float_controls_execution_mode;
10503
10504 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10505 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10506 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10507 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10508 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10509
10510 program->next_fp_mode.must_flush_denorms32 =
10511 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10512 program->next_fp_mode.must_flush_denorms16_64 =
10513 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10514 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10515
10516 program->next_fp_mode.care_about_round32 =
10517 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10518
10519 program->next_fp_mode.care_about_round16_64 =
10520 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10521 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10522
10523 /* default to preserving fp16 and fp64 denorms, since it's free */
10524 if (program->next_fp_mode.must_flush_denorms16_64)
10525 program->next_fp_mode.denorm16_64 = 0;
10526 else
10527 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10528
10529 /* preserving fp32 denorms is expensive, so only do it if asked */
10530 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10531 program->next_fp_mode.denorm32 = fp_denorm_keep;
10532 else
10533 program->next_fp_mode.denorm32 = 0;
10534
10535 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10536 program->next_fp_mode.round32 = fp_round_tz;
10537 else
10538 program->next_fp_mode.round32 = fp_round_ne;
10539
10540 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10541 program->next_fp_mode.round16_64 = fp_round_tz;
10542 else
10543 program->next_fp_mode.round16_64 = fp_round_ne;
10544
10545 ctx->block->fp_mode = program->next_fp_mode;
10546 }
10547
10548 void cleanup_cfg(Program *program)
10549 {
10550 /* create linear_succs/logical_succs */
10551 for (Block& BB : program->blocks) {
10552 for (unsigned idx : BB.linear_preds)
10553 program->blocks[idx].linear_succs.emplace_back(BB.index);
10554 for (unsigned idx : BB.logical_preds)
10555 program->blocks[idx].logical_succs.emplace_back(BB.index);
10556 }
10557 }
10558
10559 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10560 {
10561 Builder bld(ctx->program, ctx->block);
10562
10563 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10564 Temp count = i == 0
10565 ? get_arg(ctx, ctx->args->merged_wave_info)
10566 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10567 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10568
10569 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10570 Temp cond;
10571
10572 if (ctx->program->wave_size == 64) {
10573 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10574 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10575 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10576 } else {
10577 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10578 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10579 }
10580
10581 return cond;
10582 }
10583
10584 bool ngg_early_prim_export(isel_context *ctx)
10585 {
10586 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10587 return true;
10588 }
10589
10590 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10591 {
10592 Builder bld(ctx->program, ctx->block);
10593
10594 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10595 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10596
10597 /* Get the id of the current wave within the threadgroup (workgroup) */
10598 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10599 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10600
10601 /* Execute the following code only on the first wave (wave id 0),
10602 * use the SCC def to tell if the wave id is zero or not.
10603 */
10604 Temp cond = wave_id_in_tg.def(1).getTemp();
10605 if_context ic;
10606 begin_uniform_if_then(ctx, &ic, cond);
10607 begin_uniform_if_else(ctx, &ic);
10608 bld.reset(ctx->block);
10609
10610 /* Number of vertices output by VS/TES */
10611 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10612 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10613 /* Number of primitives output by VS/TES */
10614 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10615 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10616
10617 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10618 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10619 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10620
10621 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10622 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10623
10624 end_uniform_if(ctx, &ic);
10625
10626 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10627 bld.reset(ctx->block);
10628 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10629 }
10630
10631 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10632 {
10633 Builder bld(ctx->program, ctx->block);
10634
10635 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10636 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10637 }
10638
10639 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10640 Temp tmp;
10641
10642 for (unsigned i = 0; i < num_vertices; ++i) {
10643 assert(vtxindex[i].id());
10644
10645 if (i)
10646 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10647 else
10648 tmp = vtxindex[i];
10649
10650 /* The initial edge flag is always false in tess eval shaders. */
10651 if (ctx->stage == ngg_vertex_gs) {
10652 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10653 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10654 }
10655 }
10656
10657 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10658
10659 return tmp;
10660 }
10661
10662 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10663 {
10664 Builder bld(ctx->program, ctx->block);
10665 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10666
10667 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10668 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10669 false /* compressed */, true/* done */, false /* valid mask */);
10670 }
10671
10672 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10673 {
10674 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10675 * These must always come before VS exports.
10676 *
10677 * It is recommended to do these as early as possible. They can be at the beginning when
10678 * there is no SW GS and the shader doesn't write edge flags.
10679 */
10680
10681 if_context ic;
10682 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10683 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10684
10685 Builder bld(ctx->program, ctx->block);
10686 constexpr unsigned max_vertices_per_primitive = 3;
10687 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10688
10689 if (ctx->stage == ngg_vertex_gs) {
10690 /* TODO: optimize for points & lines */
10691 } else if (ctx->stage == ngg_tess_eval_gs) {
10692 if (ctx->shader->info.tess.point_mode)
10693 num_vertices_per_primitive = 1;
10694 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10695 num_vertices_per_primitive = 2;
10696 } else {
10697 unreachable("Unsupported NGG shader stage");
10698 }
10699
10700 Temp vtxindex[max_vertices_per_primitive];
10701 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10702 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10703 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10704 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10705 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10706 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10707 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10708 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10709
10710 /* Export primitive data to the index buffer. */
10711 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10712
10713 /* Export primitive ID. */
10714 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10715 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10716 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10717 Temp provoking_vtx_index = vtxindex[0];
10718 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10719
10720 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10721 }
10722
10723 begin_divergent_if_else(ctx, &ic);
10724 end_divergent_if(ctx, &ic);
10725 }
10726
10727 void ngg_emit_nogs_output(isel_context *ctx)
10728 {
10729 /* Emits NGG GS output, for stages that don't have SW GS. */
10730
10731 if_context ic;
10732 Builder bld(ctx->program, ctx->block);
10733 bool late_prim_export = !ngg_early_prim_export(ctx);
10734
10735 /* NGG streamout is currently disabled by default. */
10736 assert(!ctx->args->shader_info->so.num_outputs);
10737
10738 if (late_prim_export) {
10739 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10740 create_export_phis(ctx);
10741 /* Do what we need to do in the GS threads. */
10742 ngg_emit_nogs_gsthreads(ctx);
10743
10744 /* What comes next should be executed on ES threads. */
10745 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10746 begin_divergent_if_then(ctx, &ic, is_es_thread);
10747 bld.reset(ctx->block);
10748 }
10749
10750 /* Export VS outputs */
10751 ctx->block->kind |= block_kind_export_end;
10752 create_vs_exports(ctx);
10753
10754 /* Export primitive ID */
10755 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10756 Temp prim_id;
10757
10758 if (ctx->stage == ngg_vertex_gs) {
10759 /* Wait for GS threads to store primitive ID in LDS. */
10760 bld.barrier(aco_opcode::p_memory_barrier_shared);
10761 bld.sopp(aco_opcode::s_barrier);
10762
10763 /* Calculate LDS address where the GS threads stored the primitive ID. */
10764 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10765 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10766 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10767 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10768 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10769 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10770
10771 /* Load primitive ID from LDS. */
10772 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10773 } else if (ctx->stage == ngg_tess_eval_gs) {
10774 /* TES: Just use the patch ID as the primitive ID. */
10775 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10776 } else {
10777 unreachable("unsupported NGG shader stage.");
10778 }
10779
10780 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10781 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10782
10783 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10784 }
10785
10786 if (late_prim_export) {
10787 begin_divergent_if_else(ctx, &ic);
10788 end_divergent_if(ctx, &ic);
10789 bld.reset(ctx->block);
10790 }
10791 }
10792
10793 void select_program(Program *program,
10794 unsigned shader_count,
10795 struct nir_shader *const *shaders,
10796 ac_shader_config* config,
10797 struct radv_shader_args *args)
10798 {
10799 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10800 if_context ic_merged_wave_info;
10801 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10802
10803 for (unsigned i = 0; i < shader_count; i++) {
10804 nir_shader *nir = shaders[i];
10805 init_context(&ctx, nir);
10806
10807 setup_fp_mode(&ctx, nir);
10808
10809 if (!i) {
10810 /* needs to be after init_context() for FS */
10811 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10812 append_logical_start(ctx.block);
10813
10814 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10815 fix_ls_vgpr_init_bug(&ctx, startpgm);
10816
10817 split_arguments(&ctx, startpgm);
10818 }
10819
10820 if (ngg_no_gs) {
10821 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10822
10823 if (ngg_early_prim_export(&ctx))
10824 ngg_emit_nogs_gsthreads(&ctx);
10825 }
10826
10827 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10828 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10829 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10830 ((nir->info.stage == MESA_SHADER_VERTEX &&
10831 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10832 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10833 ctx.stage == tess_eval_geometry_gs));
10834
10835 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10836 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10837 if (check_merged_wave_info) {
10838 Temp cond = merged_wave_info_to_mask(&ctx, i);
10839 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10840 }
10841
10842 if (i) {
10843 Builder bld(ctx.program, ctx.block);
10844
10845 bld.barrier(aco_opcode::p_memory_barrier_shared);
10846 bld.sopp(aco_opcode::s_barrier);
10847
10848 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10849 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));
10850 }
10851 } else if (ctx.stage == geometry_gs)
10852 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10853
10854 if (ctx.stage == fragment_fs)
10855 handle_bc_optimize(&ctx);
10856
10857 visit_cf_list(&ctx, &func->body);
10858
10859 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10860 emit_streamout(&ctx, 0);
10861
10862 if (ctx.stage & hw_vs) {
10863 create_vs_exports(&ctx);
10864 ctx.block->kind |= block_kind_export_end;
10865 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10866 ngg_emit_nogs_output(&ctx);
10867 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10868 Builder bld(ctx.program, ctx.block);
10869 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10870 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10871 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10872 write_tcs_tess_factors(&ctx);
10873 }
10874
10875 if (ctx.stage == fragment_fs) {
10876 create_fs_exports(&ctx);
10877 ctx.block->kind |= block_kind_export_end;
10878 }
10879
10880 if (endif_merged_wave_info) {
10881 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10882 end_divergent_if(&ctx, &ic_merged_wave_info);
10883 }
10884
10885 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10886 ngg_emit_nogs_output(&ctx);
10887
10888 ralloc_free(ctx.divergent_vals);
10889
10890 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10891 /* Outputs of the previous stage are inputs to the next stage */
10892 ctx.inputs = ctx.outputs;
10893 ctx.outputs = shader_io_state();
10894 }
10895 }
10896
10897 program->config->float_mode = program->blocks[0].fp_mode.val;
10898
10899 append_logical_end(ctx.block);
10900 ctx.block->kind |= block_kind_uniform;
10901 Builder bld(ctx.program, ctx.block);
10902 if (ctx.program->wb_smem_l1_on_end)
10903 bld.smem(aco_opcode::s_dcache_wb, false);
10904 bld.sopp(aco_opcode::s_endpgm);
10905
10906 cleanup_cfg(program);
10907 }
10908
10909 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10910 ac_shader_config* config,
10911 struct radv_shader_args *args)
10912 {
10913 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10914
10915 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10916 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10917 program->next_fp_mode.must_flush_denorms32 = false;
10918 program->next_fp_mode.must_flush_denorms16_64 = false;
10919 program->next_fp_mode.care_about_round32 = false;
10920 program->next_fp_mode.care_about_round16_64 = false;
10921 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10922 program->next_fp_mode.denorm32 = 0;
10923 program->next_fp_mode.round32 = fp_round_ne;
10924 program->next_fp_mode.round16_64 = fp_round_ne;
10925 ctx.block->fp_mode = program->next_fp_mode;
10926
10927 add_startpgm(&ctx);
10928 append_logical_start(ctx.block);
10929
10930 Builder bld(ctx.program, ctx.block);
10931
10932 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10933
10934 Operand stream_id(0u);
10935 if (args->shader_info->so.num_outputs)
10936 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10937 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10938
10939 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10940
10941 std::stack<Block> endif_blocks;
10942
10943 for (unsigned stream = 0; stream < 4; stream++) {
10944 if (stream_id.isConstant() && stream != stream_id.constantValue())
10945 continue;
10946
10947 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10948 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10949 continue;
10950
10951 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10952
10953 unsigned BB_if_idx = ctx.block->index;
10954 Block BB_endif = Block();
10955 if (!stream_id.isConstant()) {
10956 /* begin IF */
10957 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10958 append_logical_end(ctx.block);
10959 ctx.block->kind |= block_kind_uniform;
10960 bld.branch(aco_opcode::p_cbranch_z, cond);
10961
10962 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10963
10964 ctx.block = ctx.program->create_and_insert_block();
10965 add_edge(BB_if_idx, ctx.block);
10966 bld.reset(ctx.block);
10967 append_logical_start(ctx.block);
10968 }
10969
10970 unsigned offset = 0;
10971 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10972 if (args->shader_info->gs.output_streams[i] != stream)
10973 continue;
10974
10975 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10976 unsigned length = util_last_bit(output_usage_mask);
10977 for (unsigned j = 0; j < length; ++j) {
10978 if (!(output_usage_mask & (1 << j)))
10979 continue;
10980
10981 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10982 Temp voffset = vtx_offset;
10983 if (const_offset >= 4096u) {
10984 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10985 const_offset %= 4096u;
10986 }
10987
10988 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10989 mubuf->definitions[0] = bld.def(v1);
10990 mubuf->operands[0] = Operand(gsvs_ring);
10991 mubuf->operands[1] = Operand(voffset);
10992 mubuf->operands[2] = Operand(0u);
10993 mubuf->offen = true;
10994 mubuf->offset = const_offset;
10995 mubuf->glc = true;
10996 mubuf->slc = true;
10997 mubuf->dlc = args->options->chip_class >= GFX10;
10998 mubuf->barrier = barrier_none;
10999 mubuf->can_reorder = true;
11000
11001 ctx.outputs.mask[i] |= 1 << j;
11002 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
11003
11004 bld.insert(std::move(mubuf));
11005
11006 offset++;
11007 }
11008 }
11009
11010 if (args->shader_info->so.num_outputs) {
11011 emit_streamout(&ctx, stream);
11012 bld.reset(ctx.block);
11013 }
11014
11015 if (stream == 0) {
11016 create_vs_exports(&ctx);
11017 ctx.block->kind |= block_kind_export_end;
11018 }
11019
11020 if (!stream_id.isConstant()) {
11021 append_logical_end(ctx.block);
11022
11023 /* branch from then block to endif block */
11024 bld.branch(aco_opcode::p_branch);
11025 add_edge(ctx.block->index, &BB_endif);
11026 ctx.block->kind |= block_kind_uniform;
11027
11028 /* emit else block */
11029 ctx.block = ctx.program->create_and_insert_block();
11030 add_edge(BB_if_idx, ctx.block);
11031 bld.reset(ctx.block);
11032 append_logical_start(ctx.block);
11033
11034 endif_blocks.push(std::move(BB_endif));
11035 }
11036 }
11037
11038 while (!endif_blocks.empty()) {
11039 Block BB_endif = std::move(endif_blocks.top());
11040 endif_blocks.pop();
11041
11042 Block *BB_else = ctx.block;
11043
11044 append_logical_end(BB_else);
11045 /* branch from else block to endif block */
11046 bld.branch(aco_opcode::p_branch);
11047 add_edge(BB_else->index, &BB_endif);
11048 BB_else->kind |= block_kind_uniform;
11049
11050 /** emit endif merge block */
11051 ctx.block = program->insert_block(std::move(BB_endif));
11052 bld.reset(ctx.block);
11053 append_logical_start(ctx.block);
11054 }
11055
11056 program->config->float_mode = program->blocks[0].fp_mode.val;
11057
11058 append_logical_end(ctx.block);
11059 ctx.block->kind |= block_kind_uniform;
11060 bld.sopp(aco_opcode::s_endpgm);
11061
11062 cleanup_cfg(program);
11063 }
11064 }