aco: implement 16-bit comparisons
[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 if (src0.type() == RegType::vgpr &&
565 op != aco_opcode::v_madmk_f32 &&
566 op != aco_opcode::v_madak_f32 &&
567 op != aco_opcode::v_madmk_f16 &&
568 op != aco_opcode::v_madak_f16) {
569 /* If the instruction is not commutative, we emit a VOP3A instruction */
570 bld.vop2_e64(op, Definition(dst), src0, src1);
571 return;
572 } else {
573 src1 = bld.copy(bld.def(RegType::vgpr, src1.size()), src1); //TODO: as_vgpr
574 }
575 }
576
577 if (flush_denorms && ctx->program->chip_class < GFX9) {
578 assert(dst.size() == 1);
579 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
580 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
581 } else {
582 bld.vop2(op, Definition(dst), src0, src1);
583 }
584 }
585
586 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
587 bool flush_denorms = false)
588 {
589 Temp src0 = get_alu_src(ctx, instr->src[0]);
590 Temp src1 = get_alu_src(ctx, instr->src[1]);
591 Temp src2 = get_alu_src(ctx, instr->src[2]);
592
593 /* ensure that the instruction has at most 1 sgpr operand
594 * The optimizer will inline constants for us */
595 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
596 src0 = as_vgpr(ctx, src0);
597 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
598 src1 = as_vgpr(ctx, src1);
599 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
600 src2 = as_vgpr(ctx, src2);
601
602 Builder bld(ctx->program, ctx->block);
603 if (flush_denorms && ctx->program->chip_class < GFX9) {
604 assert(dst.size() == 1);
605 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
606 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
607 } else {
608 bld.vop3(op, Definition(dst), src0, src1, src2);
609 }
610 }
611
612 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
613 {
614 Builder bld(ctx->program, ctx->block);
615 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
616 }
617
618 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
619 {
620 Temp src0 = get_alu_src(ctx, instr->src[0]);
621 Temp src1 = get_alu_src(ctx, instr->src[1]);
622 assert(src0.size() == src1.size());
623
624 aco_ptr<Instruction> vopc;
625 if (src1.type() == RegType::sgpr) {
626 if (src0.type() == RegType::vgpr) {
627 /* to swap the operands, we might also have to change the opcode */
628 switch (op) {
629 case aco_opcode::v_cmp_lt_f16:
630 op = aco_opcode::v_cmp_gt_f16;
631 break;
632 case aco_opcode::v_cmp_ge_f16:
633 op = aco_opcode::v_cmp_le_f16;
634 break;
635 case aco_opcode::v_cmp_lt_i16:
636 op = aco_opcode::v_cmp_gt_i16;
637 break;
638 case aco_opcode::v_cmp_ge_i16:
639 op = aco_opcode::v_cmp_le_i16;
640 break;
641 case aco_opcode::v_cmp_lt_u16:
642 op = aco_opcode::v_cmp_gt_u16;
643 break;
644 case aco_opcode::v_cmp_ge_u16:
645 op = aco_opcode::v_cmp_le_u16;
646 break;
647 case aco_opcode::v_cmp_lt_f32:
648 op = aco_opcode::v_cmp_gt_f32;
649 break;
650 case aco_opcode::v_cmp_ge_f32:
651 op = aco_opcode::v_cmp_le_f32;
652 break;
653 case aco_opcode::v_cmp_lt_i32:
654 op = aco_opcode::v_cmp_gt_i32;
655 break;
656 case aco_opcode::v_cmp_ge_i32:
657 op = aco_opcode::v_cmp_le_i32;
658 break;
659 case aco_opcode::v_cmp_lt_u32:
660 op = aco_opcode::v_cmp_gt_u32;
661 break;
662 case aco_opcode::v_cmp_ge_u32:
663 op = aco_opcode::v_cmp_le_u32;
664 break;
665 case aco_opcode::v_cmp_lt_f64:
666 op = aco_opcode::v_cmp_gt_f64;
667 break;
668 case aco_opcode::v_cmp_ge_f64:
669 op = aco_opcode::v_cmp_le_f64;
670 break;
671 case aco_opcode::v_cmp_lt_i64:
672 op = aco_opcode::v_cmp_gt_i64;
673 break;
674 case aco_opcode::v_cmp_ge_i64:
675 op = aco_opcode::v_cmp_le_i64;
676 break;
677 case aco_opcode::v_cmp_lt_u64:
678 op = aco_opcode::v_cmp_gt_u64;
679 break;
680 case aco_opcode::v_cmp_ge_u64:
681 op = aco_opcode::v_cmp_le_u64;
682 break;
683 default: /* eq and ne are commutative */
684 break;
685 }
686 Temp t = src0;
687 src0 = src1;
688 src1 = t;
689 } else {
690 src1 = as_vgpr(ctx, src1);
691 }
692 }
693
694 Builder bld(ctx->program, ctx->block);
695 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
696 }
697
698 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
699 {
700 Temp src0 = get_alu_src(ctx, instr->src[0]);
701 Temp src1 = get_alu_src(ctx, instr->src[1]);
702 Builder bld(ctx->program, ctx->block);
703
704 assert(dst.regClass() == bld.lm);
705 assert(src0.type() == RegType::sgpr);
706 assert(src1.type() == RegType::sgpr);
707 assert(src0.regClass() == src1.regClass());
708
709 /* Emit the SALU comparison instruction */
710 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
711 /* Turn the result into a per-lane bool */
712 bool_to_vector_condition(ctx, cmp, dst);
713 }
714
715 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
716 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)
717 {
718 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;
719 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;
720 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
721 bool use_valu = s_op == aco_opcode::num_opcodes ||
722 divergent_vals ||
723 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
724 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
725 aco_opcode op = use_valu ? v_op : s_op;
726 assert(op != aco_opcode::num_opcodes);
727 assert(dst.regClass() == ctx->program->lane_mask);
728
729 if (use_valu)
730 emit_vopc_instruction(ctx, instr, op, dst);
731 else
732 emit_sopc_instruction(ctx, instr, op, dst);
733 }
734
735 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
736 {
737 Builder bld(ctx->program, ctx->block);
738 Temp src0 = get_alu_src(ctx, instr->src[0]);
739 Temp src1 = get_alu_src(ctx, instr->src[1]);
740
741 assert(dst.regClass() == bld.lm);
742 assert(src0.regClass() == bld.lm);
743 assert(src1.regClass() == bld.lm);
744
745 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
746 }
747
748 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
749 {
750 Builder bld(ctx->program, ctx->block);
751 Temp cond = get_alu_src(ctx, instr->src[0]);
752 Temp then = get_alu_src(ctx, instr->src[1]);
753 Temp els = get_alu_src(ctx, instr->src[2]);
754
755 assert(cond.regClass() == bld.lm);
756
757 if (dst.type() == RegType::vgpr) {
758 aco_ptr<Instruction> bcsel;
759 if (dst.regClass() == v2b) {
760 then = as_vgpr(ctx, then);
761 els = as_vgpr(ctx, els);
762
763 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), els, then, cond);
764 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
765 } else if (dst.regClass() == v1) {
766 then = as_vgpr(ctx, then);
767 els = as_vgpr(ctx, els);
768
769 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
770 } else if (dst.regClass() == v2) {
771 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
772 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
773 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
774 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
775
776 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
777 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
778
779 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
780 } else {
781 fprintf(stderr, "Unimplemented NIR instr bit size: ");
782 nir_print_instr(&instr->instr, stderr);
783 fprintf(stderr, "\n");
784 }
785 return;
786 }
787
788 if (instr->dest.dest.ssa.bit_size == 1) {
789 assert(dst.regClass() == bld.lm);
790 assert(then.regClass() == bld.lm);
791 assert(els.regClass() == bld.lm);
792 }
793
794 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
795 if (dst.regClass() == s1 || dst.regClass() == s2) {
796 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
797 assert(dst.size() == then.size());
798 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
799 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
800 } else {
801 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
802 nir_print_instr(&instr->instr, stderr);
803 fprintf(stderr, "\n");
804 }
805 return;
806 }
807
808 /* divergent boolean bcsel
809 * this implements bcsel on bools: dst = s0 ? s1 : s2
810 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
811 assert(instr->dest.dest.ssa.bit_size == 1);
812
813 if (cond.id() != then.id())
814 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
815
816 if (cond.id() == els.id())
817 bld.sop1(Builder::s_mov, Definition(dst), then);
818 else
819 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
820 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
821 }
822
823 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
824 aco_opcode op, uint32_t undo)
825 {
826 /* multiply by 16777216 to handle denormals */
827 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
828 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
829 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
830 scaled = bld.vop1(op, bld.def(v1), scaled);
831 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
832
833 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
834
835 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
836 }
837
838 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
839 {
840 if (ctx->block->fp_mode.denorm32 == 0) {
841 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
842 return;
843 }
844
845 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
846 }
847
848 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
849 {
850 if (ctx->block->fp_mode.denorm32 == 0) {
851 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
852 return;
853 }
854
855 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
856 }
857
858 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
859 {
860 if (ctx->block->fp_mode.denorm32 == 0) {
861 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
862 return;
863 }
864
865 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
866 }
867
868 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
869 {
870 if (ctx->block->fp_mode.denorm32 == 0) {
871 bld.vop1(aco_opcode::v_log_f32, dst, val);
872 return;
873 }
874
875 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
876 }
877
878 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
879 {
880 if (ctx->options->chip_class >= GFX7)
881 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
882
883 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
884 /* TODO: create more efficient code! */
885 if (val.type() == RegType::sgpr)
886 val = as_vgpr(ctx, val);
887
888 /* Split the input value. */
889 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
890 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
891
892 /* Extract the exponent and compute the unbiased value. */
893 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
894
895 /* Extract the fractional part. */
896 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
897 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
898
899 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
900 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
901
902 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
903 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
904 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
905 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
906 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
907
908 /* Get the sign bit. */
909 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
910
911 /* Decide the operation to apply depending on the unbiased exponent. */
912 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
913 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
914 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
915 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
916 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
917 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
918
919 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
920 }
921
922 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
923 {
924 if (ctx->options->chip_class >= GFX7)
925 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
926
927 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
928 Temp src0 = as_vgpr(ctx, val);
929
930 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
931 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
932
933 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
934 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
935 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
936
937 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
938 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
939 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
940 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
941
942 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
943 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
944
945 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
946
947 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
948 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
949
950 return add->definitions[0].getTemp();
951 }
952
953 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
954 {
955 if (!instr->dest.dest.is_ssa) {
956 fprintf(stderr, "nir alu dst not in ssa: ");
957 nir_print_instr(&instr->instr, stderr);
958 fprintf(stderr, "\n");
959 abort();
960 }
961 Builder bld(ctx->program, ctx->block);
962 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
963 switch(instr->op) {
964 case nir_op_vec2:
965 case nir_op_vec3:
966 case nir_op_vec4: {
967 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
968 unsigned num = instr->dest.dest.ssa.num_components;
969 for (unsigned i = 0; i < num; ++i)
970 elems[i] = get_alu_src(ctx, instr->src[i]);
971
972 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
973 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
974 for (unsigned i = 0; i < num; ++i)
975 vec->operands[i] = Operand{elems[i]};
976 vec->definitions[0] = Definition(dst);
977 ctx->block->instructions.emplace_back(std::move(vec));
978 ctx->allocated_vec.emplace(dst.id(), elems);
979 } else {
980 // TODO: that is a bit suboptimal..
981 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
982 for (unsigned i = 0; i < num - 1; ++i)
983 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
984 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
985 for (unsigned i = 0; i < num; ++i) {
986 unsigned bit = i * instr->dest.dest.ssa.bit_size;
987 if (bit % 32 == 0) {
988 elems[bit / 32] = elems[i];
989 } else {
990 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
991 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
992 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
993 }
994 }
995 if (dst.size() == 1)
996 bld.copy(Definition(dst), elems[0]);
997 else
998 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
999 }
1000 break;
1001 }
1002 case nir_op_mov: {
1003 Temp src = get_alu_src(ctx, instr->src[0]);
1004 aco_ptr<Instruction> mov;
1005 if (dst.type() == RegType::sgpr) {
1006 if (src.type() == RegType::vgpr)
1007 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1008 else if (src.regClass() == s1)
1009 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1010 else if (src.regClass() == s2)
1011 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1012 else
1013 unreachable("wrong src register class for nir_op_imov");
1014 } else if (dst.regClass() == v1) {
1015 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1016 } else if (dst.regClass() == v2) {
1017 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1018 } else {
1019 nir_print_instr(&instr->instr, stderr);
1020 unreachable("Should have been lowered to scalar.");
1021 }
1022 break;
1023 }
1024 case nir_op_inot: {
1025 Temp src = get_alu_src(ctx, instr->src[0]);
1026 if (instr->dest.dest.ssa.bit_size == 1) {
1027 assert(src.regClass() == bld.lm);
1028 assert(dst.regClass() == bld.lm);
1029 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1030 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1031 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1032 } else if (dst.regClass() == v1) {
1033 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1034 } else if (dst.type() == RegType::sgpr) {
1035 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1036 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1037 } else {
1038 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1039 nir_print_instr(&instr->instr, stderr);
1040 fprintf(stderr, "\n");
1041 }
1042 break;
1043 }
1044 case nir_op_ineg: {
1045 Temp src = get_alu_src(ctx, instr->src[0]);
1046 if (dst.regClass() == v1) {
1047 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1048 } else if (dst.regClass() == s1) {
1049 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1050 } else if (dst.size() == 2) {
1051 Temp src0 = bld.tmp(dst.type(), 1);
1052 Temp src1 = bld.tmp(dst.type(), 1);
1053 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1054
1055 if (dst.regClass() == s2) {
1056 Temp carry = bld.tmp(s1);
1057 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1058 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1059 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1060 } else {
1061 Temp lower = bld.tmp(v1);
1062 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1063 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1064 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1065 }
1066 } else {
1067 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1068 nir_print_instr(&instr->instr, stderr);
1069 fprintf(stderr, "\n");
1070 }
1071 break;
1072 }
1073 case nir_op_iabs: {
1074 if (dst.regClass() == s1) {
1075 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1076 } else if (dst.regClass() == v1) {
1077 Temp src = get_alu_src(ctx, instr->src[0]);
1078 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1079 } else {
1080 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1081 nir_print_instr(&instr->instr, stderr);
1082 fprintf(stderr, "\n");
1083 }
1084 break;
1085 }
1086 case nir_op_isign: {
1087 Temp src = get_alu_src(ctx, instr->src[0]);
1088 if (dst.regClass() == s1) {
1089 Temp tmp = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
1090 Temp gtz = bld.sopc(aco_opcode::s_cmp_gt_i32, bld.def(s1, scc), src, Operand(0u));
1091 bld.sop2(aco_opcode::s_add_i32, Definition(dst), bld.def(s1, scc), gtz, tmp);
1092 } else if (dst.regClass() == s2) {
1093 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1094 Temp neqz;
1095 if (ctx->program->chip_class >= GFX8)
1096 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1097 else
1098 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1099 /* SCC gets zero-extended to 64 bit */
1100 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1101 } else if (dst.regClass() == v1) {
1102 Temp tmp = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
1103 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1104 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(1u), tmp, gtz);
1105 } else if (dst.regClass() == v2) {
1106 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1107 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1108 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1109 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1110 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1111 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1112 } else {
1113 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1114 nir_print_instr(&instr->instr, stderr);
1115 fprintf(stderr, "\n");
1116 }
1117 break;
1118 }
1119 case nir_op_imax: {
1120 if (dst.regClass() == v1) {
1121 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1122 } else if (dst.regClass() == s1) {
1123 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1124 } else {
1125 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1126 nir_print_instr(&instr->instr, stderr);
1127 fprintf(stderr, "\n");
1128 }
1129 break;
1130 }
1131 case nir_op_umax: {
1132 if (dst.regClass() == v1) {
1133 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1134 } else if (dst.regClass() == s1) {
1135 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1136 } else {
1137 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1138 nir_print_instr(&instr->instr, stderr);
1139 fprintf(stderr, "\n");
1140 }
1141 break;
1142 }
1143 case nir_op_imin: {
1144 if (dst.regClass() == v1) {
1145 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1146 } else if (dst.regClass() == s1) {
1147 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1148 } else {
1149 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1150 nir_print_instr(&instr->instr, stderr);
1151 fprintf(stderr, "\n");
1152 }
1153 break;
1154 }
1155 case nir_op_umin: {
1156 if (dst.regClass() == v1) {
1157 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1158 } else if (dst.regClass() == s1) {
1159 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1160 } else {
1161 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1162 nir_print_instr(&instr->instr, stderr);
1163 fprintf(stderr, "\n");
1164 }
1165 break;
1166 }
1167 case nir_op_ior: {
1168 if (instr->dest.dest.ssa.bit_size == 1) {
1169 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1170 } else if (dst.regClass() == v1) {
1171 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1172 } else if (dst.regClass() == s1) {
1173 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1174 } else if (dst.regClass() == s2) {
1175 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1176 } else {
1177 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1178 nir_print_instr(&instr->instr, stderr);
1179 fprintf(stderr, "\n");
1180 }
1181 break;
1182 }
1183 case nir_op_iand: {
1184 if (instr->dest.dest.ssa.bit_size == 1) {
1185 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1186 } else if (dst.regClass() == v1) {
1187 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1188 } else if (dst.regClass() == s1) {
1189 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1190 } else if (dst.regClass() == s2) {
1191 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1192 } else {
1193 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1194 nir_print_instr(&instr->instr, stderr);
1195 fprintf(stderr, "\n");
1196 }
1197 break;
1198 }
1199 case nir_op_ixor: {
1200 if (instr->dest.dest.ssa.bit_size == 1) {
1201 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1202 } else if (dst.regClass() == v1) {
1203 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1204 } else if (dst.regClass() == s1) {
1205 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1206 } else if (dst.regClass() == s2) {
1207 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1208 } else {
1209 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1210 nir_print_instr(&instr->instr, stderr);
1211 fprintf(stderr, "\n");
1212 }
1213 break;
1214 }
1215 case nir_op_ushr: {
1216 if (dst.regClass() == v1) {
1217 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1218 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1219 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1220 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1221 } else if (dst.regClass() == v2) {
1222 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1223 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1224 } else if (dst.regClass() == s2) {
1225 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1226 } else if (dst.regClass() == s1) {
1227 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1228 } else {
1229 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1230 nir_print_instr(&instr->instr, stderr);
1231 fprintf(stderr, "\n");
1232 }
1233 break;
1234 }
1235 case nir_op_ishl: {
1236 if (dst.regClass() == v1) {
1237 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1238 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1239 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1240 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1241 } else if (dst.regClass() == v2) {
1242 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1243 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1244 } else if (dst.regClass() == s1) {
1245 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1246 } else if (dst.regClass() == s2) {
1247 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1248 } else {
1249 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1250 nir_print_instr(&instr->instr, stderr);
1251 fprintf(stderr, "\n");
1252 }
1253 break;
1254 }
1255 case nir_op_ishr: {
1256 if (dst.regClass() == v1) {
1257 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1258 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1259 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1260 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1261 } else if (dst.regClass() == v2) {
1262 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1263 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1264 } else if (dst.regClass() == s1) {
1265 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1266 } else if (dst.regClass() == s2) {
1267 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1268 } else {
1269 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1270 nir_print_instr(&instr->instr, stderr);
1271 fprintf(stderr, "\n");
1272 }
1273 break;
1274 }
1275 case nir_op_find_lsb: {
1276 Temp src = get_alu_src(ctx, instr->src[0]);
1277 if (src.regClass() == s1) {
1278 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1279 } else if (src.regClass() == v1) {
1280 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1281 } else if (src.regClass() == s2) {
1282 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1283 } else {
1284 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1285 nir_print_instr(&instr->instr, stderr);
1286 fprintf(stderr, "\n");
1287 }
1288 break;
1289 }
1290 case nir_op_ufind_msb:
1291 case nir_op_ifind_msb: {
1292 Temp src = get_alu_src(ctx, instr->src[0]);
1293 if (src.regClass() == s1 || src.regClass() == s2) {
1294 aco_opcode op = src.regClass() == s2 ?
1295 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1296 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1297 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1298
1299 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1300 Operand(src.size() * 32u - 1u), msb_rev);
1301 Temp msb = sub.def(0).getTemp();
1302 Temp carry = sub.def(1).getTemp();
1303
1304 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1305 } else if (src.regClass() == v1) {
1306 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1307 Temp msb_rev = bld.tmp(v1);
1308 emit_vop1_instruction(ctx, instr, op, msb_rev);
1309 Temp msb = bld.tmp(v1);
1310 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1311 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1312 } else {
1313 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1314 nir_print_instr(&instr->instr, stderr);
1315 fprintf(stderr, "\n");
1316 }
1317 break;
1318 }
1319 case nir_op_bitfield_reverse: {
1320 if (dst.regClass() == s1) {
1321 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1322 } else if (dst.regClass() == v1) {
1323 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
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_iadd: {
1332 if (dst.regClass() == s1) {
1333 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1334 break;
1335 }
1336
1337 Temp src0 = get_alu_src(ctx, instr->src[0]);
1338 Temp src1 = get_alu_src(ctx, instr->src[1]);
1339 if (dst.regClass() == v1) {
1340 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1341 break;
1342 }
1343
1344 assert(src0.size() == 2 && src1.size() == 2);
1345 Temp src00 = bld.tmp(src0.type(), 1);
1346 Temp src01 = bld.tmp(dst.type(), 1);
1347 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1348 Temp src10 = bld.tmp(src1.type(), 1);
1349 Temp src11 = bld.tmp(dst.type(), 1);
1350 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1351
1352 if (dst.regClass() == s2) {
1353 Temp carry = bld.tmp(s1);
1354 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1355 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1356 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1357 } else if (dst.regClass() == v2) {
1358 Temp dst0 = bld.tmp(v1);
1359 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1360 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1361 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1362 } else {
1363 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1364 nir_print_instr(&instr->instr, stderr);
1365 fprintf(stderr, "\n");
1366 }
1367 break;
1368 }
1369 case nir_op_uadd_sat: {
1370 Temp src0 = get_alu_src(ctx, instr->src[0]);
1371 Temp src1 = get_alu_src(ctx, instr->src[1]);
1372 if (dst.regClass() == s1) {
1373 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1374 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1375 src0, src1);
1376 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1377 } else if (dst.regClass() == v1) {
1378 if (ctx->options->chip_class >= GFX9) {
1379 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1380 add->operands[0] = Operand(src0);
1381 add->operands[1] = Operand(src1);
1382 add->definitions[0] = Definition(dst);
1383 add->clamp = 1;
1384 ctx->block->instructions.emplace_back(std::move(add));
1385 } else {
1386 if (src1.regClass() != v1)
1387 std::swap(src0, src1);
1388 assert(src1.regClass() == v1);
1389 Temp tmp = bld.tmp(v1);
1390 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1391 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1392 }
1393 } else {
1394 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1395 nir_print_instr(&instr->instr, stderr);
1396 fprintf(stderr, "\n");
1397 }
1398 break;
1399 }
1400 case nir_op_uadd_carry: {
1401 Temp src0 = get_alu_src(ctx, instr->src[0]);
1402 Temp src1 = get_alu_src(ctx, instr->src[1]);
1403 if (dst.regClass() == s1) {
1404 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1405 break;
1406 }
1407 if (dst.regClass() == v1) {
1408 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1409 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1410 break;
1411 }
1412
1413 Temp src00 = bld.tmp(src0.type(), 1);
1414 Temp src01 = bld.tmp(dst.type(), 1);
1415 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1416 Temp src10 = bld.tmp(src1.type(), 1);
1417 Temp src11 = bld.tmp(dst.type(), 1);
1418 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1419 if (dst.regClass() == s2) {
1420 Temp carry = bld.tmp(s1);
1421 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1422 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1423 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1424 } else if (dst.regClass() == v2) {
1425 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1426 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1427 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1428 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1429 } else {
1430 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1431 nir_print_instr(&instr->instr, stderr);
1432 fprintf(stderr, "\n");
1433 }
1434 break;
1435 }
1436 case nir_op_isub: {
1437 if (dst.regClass() == s1) {
1438 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1439 break;
1440 }
1441
1442 Temp src0 = get_alu_src(ctx, instr->src[0]);
1443 Temp src1 = get_alu_src(ctx, instr->src[1]);
1444 if (dst.regClass() == v1) {
1445 bld.vsub32(Definition(dst), src0, src1);
1446 break;
1447 }
1448
1449 Temp src00 = bld.tmp(src0.type(), 1);
1450 Temp src01 = bld.tmp(dst.type(), 1);
1451 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1452 Temp src10 = bld.tmp(src1.type(), 1);
1453 Temp src11 = bld.tmp(dst.type(), 1);
1454 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1455 if (dst.regClass() == s2) {
1456 Temp carry = bld.tmp(s1);
1457 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1458 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1459 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1460 } else if (dst.regClass() == v2) {
1461 Temp lower = bld.tmp(v1);
1462 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1463 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1464 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1465 } else {
1466 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1467 nir_print_instr(&instr->instr, stderr);
1468 fprintf(stderr, "\n");
1469 }
1470 break;
1471 }
1472 case nir_op_usub_borrow: {
1473 Temp src0 = get_alu_src(ctx, instr->src[0]);
1474 Temp src1 = get_alu_src(ctx, instr->src[1]);
1475 if (dst.regClass() == s1) {
1476 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1477 break;
1478 } else if (dst.regClass() == v1) {
1479 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1480 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1481 break;
1482 }
1483
1484 Temp src00 = bld.tmp(src0.type(), 1);
1485 Temp src01 = bld.tmp(dst.type(), 1);
1486 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1487 Temp src10 = bld.tmp(src1.type(), 1);
1488 Temp src11 = bld.tmp(dst.type(), 1);
1489 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1490 if (dst.regClass() == s2) {
1491 Temp borrow = bld.tmp(s1);
1492 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1493 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1494 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1495 } else if (dst.regClass() == v2) {
1496 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1497 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1498 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1499 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1500 } else {
1501 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1502 nir_print_instr(&instr->instr, stderr);
1503 fprintf(stderr, "\n");
1504 }
1505 break;
1506 }
1507 case nir_op_imul: {
1508 if (dst.regClass() == v1) {
1509 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1510 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1511 } else if (dst.regClass() == s1) {
1512 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1513 } else {
1514 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1515 nir_print_instr(&instr->instr, stderr);
1516 fprintf(stderr, "\n");
1517 }
1518 break;
1519 }
1520 case nir_op_umul_high: {
1521 if (dst.regClass() == v1) {
1522 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1523 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1524 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1525 } else if (dst.regClass() == s1) {
1526 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1527 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1528 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1529 } else {
1530 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1531 nir_print_instr(&instr->instr, stderr);
1532 fprintf(stderr, "\n");
1533 }
1534 break;
1535 }
1536 case nir_op_imul_high: {
1537 if (dst.regClass() == v1) {
1538 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1539 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1540 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1541 } else if (dst.regClass() == s1) {
1542 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1543 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1544 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1545 } else {
1546 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1547 nir_print_instr(&instr->instr, stderr);
1548 fprintf(stderr, "\n");
1549 }
1550 break;
1551 }
1552 case nir_op_fmul: {
1553 Temp src0 = get_alu_src(ctx, instr->src[0]);
1554 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1555 if (dst.regClass() == v2b) {
1556 Temp tmp = bld.tmp(v1);
1557 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, tmp, true);
1558 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1559 } else if (dst.regClass() == v1) {
1560 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1561 } else if (dst.regClass() == v2) {
1562 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1563 } else {
1564 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1565 nir_print_instr(&instr->instr, stderr);
1566 fprintf(stderr, "\n");
1567 }
1568 break;
1569 }
1570 case nir_op_fadd: {
1571 Temp src0 = get_alu_src(ctx, instr->src[0]);
1572 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1573 if (dst.regClass() == v2b) {
1574 Temp tmp = bld.tmp(v1);
1575 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, tmp, true);
1576 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1577 } else if (dst.regClass() == v1) {
1578 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1579 } else if (dst.regClass() == v2) {
1580 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1581 } else {
1582 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1583 nir_print_instr(&instr->instr, stderr);
1584 fprintf(stderr, "\n");
1585 }
1586 break;
1587 }
1588 case nir_op_fsub: {
1589 Temp src0 = get_alu_src(ctx, instr->src[0]);
1590 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1591 if (dst.regClass() == v2b) {
1592 Temp tmp = bld.tmp(v1);
1593 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1594 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, tmp, false);
1595 else
1596 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, tmp, true);
1597 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1598 } else if (dst.regClass() == v1) {
1599 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1600 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1601 else
1602 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1603 } else if (dst.regClass() == v2) {
1604 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1605 src0, src1);
1606 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1607 sub->neg[1] = true;
1608 } else {
1609 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1610 nir_print_instr(&instr->instr, stderr);
1611 fprintf(stderr, "\n");
1612 }
1613 break;
1614 }
1615 case nir_op_fmax: {
1616 Temp src0 = get_alu_src(ctx, instr->src[0]);
1617 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1618 if (dst.regClass() == v2b) {
1619 // TODO: check fp_mode.must_flush_denorms16_64
1620 Temp tmp = bld.tmp(v1);
1621 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, tmp, true);
1622 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1623 } else if (dst.regClass() == v1) {
1624 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1625 } else if (dst.regClass() == v2) {
1626 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1627 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1628 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1629 } else {
1630 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1631 }
1632 } else {
1633 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1634 nir_print_instr(&instr->instr, stderr);
1635 fprintf(stderr, "\n");
1636 }
1637 break;
1638 }
1639 case nir_op_fmin: {
1640 Temp src0 = get_alu_src(ctx, instr->src[0]);
1641 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1642 if (dst.regClass() == v2b) {
1643 // TODO: check fp_mode.must_flush_denorms16_64
1644 Temp tmp = bld.tmp(v1);
1645 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, tmp, true);
1646 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1647 } else if (dst.regClass() == v1) {
1648 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1649 } else if (dst.regClass() == v2) {
1650 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1651 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1652 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1653 } else {
1654 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1655 }
1656 } else {
1657 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1658 nir_print_instr(&instr->instr, stderr);
1659 fprintf(stderr, "\n");
1660 }
1661 break;
1662 }
1663 case nir_op_fmax3: {
1664 if (dst.regClass() == v2b) {
1665 Temp tmp = bld.tmp(v1);
1666 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, tmp, false);
1667 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1668 } else if (dst.regClass() == v1) {
1669 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1670 } else {
1671 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1672 nir_print_instr(&instr->instr, stderr);
1673 fprintf(stderr, "\n");
1674 }
1675 break;
1676 }
1677 case nir_op_fmin3: {
1678 if (dst.regClass() == v2b) {
1679 Temp tmp = bld.tmp(v1);
1680 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, tmp, false);
1681 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1682 } else if (dst.regClass() == v1) {
1683 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1684 } else {
1685 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1686 nir_print_instr(&instr->instr, stderr);
1687 fprintf(stderr, "\n");
1688 }
1689 break;
1690 }
1691 case nir_op_fmed3: {
1692 if (dst.regClass() == v2b) {
1693 Temp tmp = bld.tmp(v1);
1694 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, tmp, false);
1695 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1696 } else if (dst.regClass() == v1) {
1697 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1698 } else {
1699 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1700 nir_print_instr(&instr->instr, stderr);
1701 fprintf(stderr, "\n");
1702 }
1703 break;
1704 }
1705 case nir_op_umax3: {
1706 if (dst.size() == 1) {
1707 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1708 } else {
1709 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1710 nir_print_instr(&instr->instr, stderr);
1711 fprintf(stderr, "\n");
1712 }
1713 break;
1714 }
1715 case nir_op_umin3: {
1716 if (dst.size() == 1) {
1717 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1718 } else {
1719 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1720 nir_print_instr(&instr->instr, stderr);
1721 fprintf(stderr, "\n");
1722 }
1723 break;
1724 }
1725 case nir_op_umed3: {
1726 if (dst.size() == 1) {
1727 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1728 } else {
1729 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1730 nir_print_instr(&instr->instr, stderr);
1731 fprintf(stderr, "\n");
1732 }
1733 break;
1734 }
1735 case nir_op_imax3: {
1736 if (dst.size() == 1) {
1737 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1738 } else {
1739 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1740 nir_print_instr(&instr->instr, stderr);
1741 fprintf(stderr, "\n");
1742 }
1743 break;
1744 }
1745 case nir_op_imin3: {
1746 if (dst.size() == 1) {
1747 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1748 } else {
1749 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1750 nir_print_instr(&instr->instr, stderr);
1751 fprintf(stderr, "\n");
1752 }
1753 break;
1754 }
1755 case nir_op_imed3: {
1756 if (dst.size() == 1) {
1757 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1758 } else {
1759 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1760 nir_print_instr(&instr->instr, stderr);
1761 fprintf(stderr, "\n");
1762 }
1763 break;
1764 }
1765 case nir_op_cube_face_coord: {
1766 Temp in = get_alu_src(ctx, instr->src[0], 3);
1767 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1768 emit_extract_vector(ctx, in, 1, v1),
1769 emit_extract_vector(ctx, in, 2, v1) };
1770 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1771 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1772 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1773 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1774 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1775 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1776 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1777 break;
1778 }
1779 case nir_op_cube_face_index: {
1780 Temp in = get_alu_src(ctx, instr->src[0], 3);
1781 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1782 emit_extract_vector(ctx, in, 1, v1),
1783 emit_extract_vector(ctx, in, 2, v1) };
1784 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1785 break;
1786 }
1787 case nir_op_bcsel: {
1788 emit_bcsel(ctx, instr, dst);
1789 break;
1790 }
1791 case nir_op_frsq: {
1792 Temp src = get_alu_src(ctx, instr->src[0]);
1793 if (dst.regClass() == v2b) {
1794 Temp tmp = bld.vop1(aco_opcode::v_rsq_f16, bld.def(v1), src);
1795 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1796 } else if (dst.regClass() == v1) {
1797 emit_rsq(ctx, bld, Definition(dst), src);
1798 } else if (dst.regClass() == v2) {
1799 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1800 } else {
1801 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1802 nir_print_instr(&instr->instr, stderr);
1803 fprintf(stderr, "\n");
1804 }
1805 break;
1806 }
1807 case nir_op_fneg: {
1808 Temp src = get_alu_src(ctx, instr->src[0]);
1809 if (dst.regClass() == v2b) {
1810 Temp tmp = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x8000u), as_vgpr(ctx, src));
1811 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1812 } else if (dst.regClass() == v1) {
1813 if (ctx->block->fp_mode.must_flush_denorms32)
1814 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1815 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1816 } else if (dst.regClass() == v2) {
1817 if (ctx->block->fp_mode.must_flush_denorms16_64)
1818 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1819 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1820 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1821 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1822 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1823 } else {
1824 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1825 nir_print_instr(&instr->instr, stderr);
1826 fprintf(stderr, "\n");
1827 }
1828 break;
1829 }
1830 case nir_op_fabs: {
1831 Temp src = get_alu_src(ctx, instr->src[0]);
1832 if (dst.regClass() == v2b) {
1833 Temp tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFu), as_vgpr(ctx, src));
1834 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1835 } else if (dst.regClass() == v1) {
1836 if (ctx->block->fp_mode.must_flush_denorms32)
1837 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1838 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1839 } else if (dst.regClass() == v2) {
1840 if (ctx->block->fp_mode.must_flush_denorms16_64)
1841 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1842 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1843 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1844 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1845 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1846 } else {
1847 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1848 nir_print_instr(&instr->instr, stderr);
1849 fprintf(stderr, "\n");
1850 }
1851 break;
1852 }
1853 case nir_op_fsat: {
1854 Temp src = get_alu_src(ctx, instr->src[0]);
1855 if (dst.regClass() == v2b) {
1856 Temp tmp = bld.vop3(aco_opcode::v_med3_f16, bld.def(v1), Operand(0u), Operand(0x3f800000u), src);
1857 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1858 } else if (dst.regClass() == v1) {
1859 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1860 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1861 // TODO: confirm that this holds under any circumstances
1862 } else if (dst.regClass() == v2) {
1863 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1864 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1865 vop3->clamp = true;
1866 } else {
1867 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1868 nir_print_instr(&instr->instr, stderr);
1869 fprintf(stderr, "\n");
1870 }
1871 break;
1872 }
1873 case nir_op_flog2: {
1874 Temp src = get_alu_src(ctx, instr->src[0]);
1875 if (dst.regClass() == v2b) {
1876 Temp tmp = bld.vop1(aco_opcode::v_log_f16, bld.def(v1), src);
1877 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1878 } else if (dst.regClass() == v1) {
1879 emit_log2(ctx, bld, Definition(dst), src);
1880 } else {
1881 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1882 nir_print_instr(&instr->instr, stderr);
1883 fprintf(stderr, "\n");
1884 }
1885 break;
1886 }
1887 case nir_op_frcp: {
1888 Temp src = get_alu_src(ctx, instr->src[0]);
1889 if (dst.regClass() == v2b) {
1890 Temp tmp = bld.vop1(aco_opcode::v_rcp_f16, bld.def(v1), src);
1891 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1892 } else if (dst.regClass() == v1) {
1893 emit_rcp(ctx, bld, Definition(dst), src);
1894 } else if (dst.regClass() == v2) {
1895 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1896 } else {
1897 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1898 nir_print_instr(&instr->instr, stderr);
1899 fprintf(stderr, "\n");
1900 }
1901 break;
1902 }
1903 case nir_op_fexp2: {
1904 if (dst.regClass() == v2b) {
1905 Temp src = get_alu_src(ctx, instr->src[0]);
1906 Temp tmp = bld.vop1(aco_opcode::v_exp_f16, bld.def(v1), src);
1907 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1908 } else if (dst.regClass() == v1) {
1909 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1910 } else {
1911 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1912 nir_print_instr(&instr->instr, stderr);
1913 fprintf(stderr, "\n");
1914 }
1915 break;
1916 }
1917 case nir_op_fsqrt: {
1918 Temp src = get_alu_src(ctx, instr->src[0]);
1919 if (dst.regClass() == v2b) {
1920 Temp tmp = bld.vop1(aco_opcode::v_sqrt_f16, bld.def(v1), src);
1921 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1922 } else if (dst.regClass() == v1) {
1923 emit_sqrt(ctx, bld, Definition(dst), src);
1924 } else if (dst.regClass() == v2) {
1925 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1926 } else {
1927 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1928 nir_print_instr(&instr->instr, stderr);
1929 fprintf(stderr, "\n");
1930 }
1931 break;
1932 }
1933 case nir_op_ffract: {
1934 if (dst.regClass() == v2b) {
1935 Temp src = get_alu_src(ctx, instr->src[0]);
1936 Temp tmp = bld.vop1(aco_opcode::v_fract_f16, bld.def(v1), src);
1937 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1938 } else if (dst.regClass() == v1) {
1939 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1940 } else if (dst.regClass() == v2) {
1941 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1942 } else {
1943 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1944 nir_print_instr(&instr->instr, stderr);
1945 fprintf(stderr, "\n");
1946 }
1947 break;
1948 }
1949 case nir_op_ffloor: {
1950 Temp src = get_alu_src(ctx, instr->src[0]);
1951 if (dst.regClass() == v2b) {
1952 Temp tmp = bld.vop1(aco_opcode::v_floor_f16, bld.def(v1), src);
1953 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1954 } else if (dst.regClass() == v1) {
1955 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1956 } else if (dst.regClass() == v2) {
1957 emit_floor_f64(ctx, bld, Definition(dst), src);
1958 } else {
1959 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1960 nir_print_instr(&instr->instr, stderr);
1961 fprintf(stderr, "\n");
1962 }
1963 break;
1964 }
1965 case nir_op_fceil: {
1966 Temp src0 = get_alu_src(ctx, instr->src[0]);
1967 if (dst.regClass() == v2b) {
1968 Temp tmp = bld.vop1(aco_opcode::v_ceil_f16, bld.def(v1), src0);
1969 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1970 } else if (dst.regClass() == v1) {
1971 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1972 } else if (dst.regClass() == v2) {
1973 if (ctx->options->chip_class >= GFX7) {
1974 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1975 } else {
1976 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1977 /* trunc = trunc(src0)
1978 * if (src0 > 0.0 && src0 != trunc)
1979 * trunc += 1.0
1980 */
1981 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
1982 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
1983 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
1984 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
1985 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);
1986 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
1987 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
1988 }
1989 } else {
1990 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1991 nir_print_instr(&instr->instr, stderr);
1992 fprintf(stderr, "\n");
1993 }
1994 break;
1995 }
1996 case nir_op_ftrunc: {
1997 Temp src = get_alu_src(ctx, instr->src[0]);
1998 if (dst.regClass() == v2b) {
1999 Temp tmp = bld.vop1(aco_opcode::v_trunc_f16, bld.def(v1), src);
2000 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2001 } else if (dst.regClass() == v1) {
2002 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2003 } else if (dst.regClass() == v2) {
2004 emit_trunc_f64(ctx, bld, Definition(dst), src);
2005 } else {
2006 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2007 nir_print_instr(&instr->instr, stderr);
2008 fprintf(stderr, "\n");
2009 }
2010 break;
2011 }
2012 case nir_op_fround_even: {
2013 Temp src0 = get_alu_src(ctx, instr->src[0]);
2014 if (dst.regClass() == v2b) {
2015 Temp tmp = bld.vop1(aco_opcode::v_rndne_f16, bld.def(v1), src0);
2016 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2017 } else if (dst.regClass() == v1) {
2018 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2019 } else if (dst.regClass() == v2) {
2020 if (ctx->options->chip_class >= GFX7) {
2021 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2022 } else {
2023 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2024 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2025 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2026
2027 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2028 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));
2029 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));
2030 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));
2031 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2032 tmp = sub->definitions[0].getTemp();
2033
2034 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2035 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2036 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2037 Temp cond = vop3->definitions[0].getTemp();
2038
2039 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2040 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2041 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2042 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2043
2044 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2045 }
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_fsin:
2054 case nir_op_fcos: {
2055 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2056 aco_ptr<Instruction> norm;
2057 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2058 if (dst.regClass() == v2b) {
2059 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2060 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2061 tmp = bld.vop1(opcode, bld.def(v1), tmp);
2062 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2063 } else if (dst.regClass() == v1) {
2064 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2065
2066 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2067 if (ctx->options->chip_class < GFX9)
2068 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2069
2070 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2071 bld.vop1(opcode, Definition(dst), tmp);
2072 } else {
2073 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2074 nir_print_instr(&instr->instr, stderr);
2075 fprintf(stderr, "\n");
2076 }
2077 break;
2078 }
2079 case nir_op_ldexp: {
2080 Temp src0 = get_alu_src(ctx, instr->src[0]);
2081 Temp src1 = get_alu_src(ctx, instr->src[1]);
2082 if (dst.regClass() == v2b) {
2083 Temp tmp = bld.tmp(v1);
2084 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, tmp, false);
2085 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2086 } else if (dst.regClass() == v1) {
2087 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2088 } else if (dst.regClass() == v2) {
2089 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2090 } else {
2091 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2092 nir_print_instr(&instr->instr, stderr);
2093 fprintf(stderr, "\n");
2094 }
2095 break;
2096 }
2097 case nir_op_frexp_sig: {
2098 Temp src = get_alu_src(ctx, instr->src[0]);
2099 if (dst.regClass() == v2b) {
2100 Temp tmp = bld.vop1(aco_opcode::v_frexp_mant_f16, bld.def(v1), src);
2101 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2102 } else if (dst.regClass() == v1) {
2103 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2104 } else if (dst.regClass() == v2) {
2105 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2106 } else {
2107 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2108 nir_print_instr(&instr->instr, stderr);
2109 fprintf(stderr, "\n");
2110 }
2111 break;
2112 }
2113 case nir_op_frexp_exp: {
2114 Temp src = get_alu_src(ctx, instr->src[0]);
2115 if (instr->src[0].src.ssa->bit_size == 16) {
2116 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2117 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), tmp, Operand(0u));
2118 } else if (instr->src[0].src.ssa->bit_size == 32) {
2119 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2120 } else if (instr->src[0].src.ssa->bit_size == 64) {
2121 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2122 } else {
2123 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2124 nir_print_instr(&instr->instr, stderr);
2125 fprintf(stderr, "\n");
2126 }
2127 break;
2128 }
2129 case nir_op_fsign: {
2130 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2131 if (dst.regClass() == v2b) {
2132 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2133 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2134 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2135 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2136 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2137 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), minus_one, src, cond);
2138 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2139 } else if (dst.regClass() == v1) {
2140 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2141 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2142 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2143 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2144 } else if (dst.regClass() == v2) {
2145 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2146 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2147 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2148
2149 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2150 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2151 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2152
2153 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2154 } else {
2155 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2156 nir_print_instr(&instr->instr, stderr);
2157 fprintf(stderr, "\n");
2158 }
2159 break;
2160 }
2161 case nir_op_f2f16:
2162 case nir_op_f2f16_rtne: {
2163 Temp src = get_alu_src(ctx, instr->src[0]);
2164 if (instr->src[0].src.ssa->bit_size == 64)
2165 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2166 src = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2167 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2168 break;
2169 }
2170 case nir_op_f2f16_rtz: {
2171 Temp src = get_alu_src(ctx, instr->src[0]);
2172 if (instr->src[0].src.ssa->bit_size == 64)
2173 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2174 src = bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, bld.def(v1), src, Operand(0u));
2175 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2176 break;
2177 }
2178 case nir_op_f2f32: {
2179 if (instr->src[0].src.ssa->bit_size == 16) {
2180 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2181 } else if (instr->src[0].src.ssa->bit_size == 64) {
2182 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2183 } else {
2184 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2185 nir_print_instr(&instr->instr, stderr);
2186 fprintf(stderr, "\n");
2187 }
2188 break;
2189 }
2190 case nir_op_f2f64: {
2191 Temp src = get_alu_src(ctx, instr->src[0]);
2192 if (instr->src[0].src.ssa->bit_size == 16)
2193 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2194 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2195 break;
2196 }
2197 case nir_op_i2f32: {
2198 assert(dst.size() == 1);
2199 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_i32, dst);
2200 break;
2201 }
2202 case nir_op_i2f64: {
2203 if (instr->src[0].src.ssa->bit_size == 32) {
2204 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_i32, dst);
2205 } else if (instr->src[0].src.ssa->bit_size == 64) {
2206 Temp src = get_alu_src(ctx, instr->src[0]);
2207 RegClass rc = RegClass(src.type(), 1);
2208 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2209 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2210 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2211 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2212 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2213 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2214
2215 } else {
2216 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2217 nir_print_instr(&instr->instr, stderr);
2218 fprintf(stderr, "\n");
2219 }
2220 break;
2221 }
2222 case nir_op_u2f32: {
2223 assert(dst.size() == 1);
2224 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_u32, dst);
2225 break;
2226 }
2227 case nir_op_u2f64: {
2228 if (instr->src[0].src.ssa->bit_size == 32) {
2229 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_u32, dst);
2230 } else if (instr->src[0].src.ssa->bit_size == 64) {
2231 Temp src = get_alu_src(ctx, instr->src[0]);
2232 RegClass rc = RegClass(src.type(), 1);
2233 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2234 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2235 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2236 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2237 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2238 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2239 } else {
2240 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2241 nir_print_instr(&instr->instr, stderr);
2242 fprintf(stderr, "\n");
2243 }
2244 break;
2245 }
2246 case nir_op_f2i16: {
2247 Temp src = get_alu_src(ctx, instr->src[0]);
2248 if (instr->src[0].src.ssa->bit_size == 16)
2249 src = bld.vop1(aco_opcode::v_cvt_i16_f16, bld.def(v1), src);
2250 else if (instr->src[0].src.ssa->bit_size == 32)
2251 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2252 else
2253 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2254
2255 if (dst.type() == RegType::vgpr)
2256 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2257 else
2258 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2259 break;
2260 }
2261 case nir_op_f2u16: {
2262 Temp src = get_alu_src(ctx, instr->src[0]);
2263 if (instr->src[0].src.ssa->bit_size == 16)
2264 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2265 else if (instr->src[0].src.ssa->bit_size == 32)
2266 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2267 else
2268 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2269
2270 if (dst.type() == RegType::vgpr)
2271 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2272 else
2273 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2274 break;
2275 }
2276 case nir_op_f2i32: {
2277 Temp src = get_alu_src(ctx, instr->src[0]);
2278 if (instr->src[0].src.ssa->bit_size == 16) {
2279 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2280 if (dst.type() == RegType::vgpr) {
2281 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2282 } else {
2283 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2284 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2285 }
2286 } else if (instr->src[0].src.ssa->bit_size == 32) {
2287 if (dst.type() == RegType::vgpr)
2288 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2289 else
2290 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2291 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2292
2293 } else if (instr->src[0].src.ssa->bit_size == 64) {
2294 if (dst.type() == RegType::vgpr)
2295 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2296 else
2297 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2298 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2299
2300 } else {
2301 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2302 nir_print_instr(&instr->instr, stderr);
2303 fprintf(stderr, "\n");
2304 }
2305 break;
2306 }
2307 case nir_op_f2u32: {
2308 Temp src = get_alu_src(ctx, instr->src[0]);
2309 if (instr->src[0].src.ssa->bit_size == 16) {
2310 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2311 if (dst.type() == RegType::vgpr) {
2312 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2313 } else {
2314 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2315 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2316 }
2317 } else if (instr->src[0].src.ssa->bit_size == 32) {
2318 if (dst.type() == RegType::vgpr)
2319 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2320 else
2321 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2322 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2323
2324 } else if (instr->src[0].src.ssa->bit_size == 64) {
2325 if (dst.type() == RegType::vgpr)
2326 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2327 else
2328 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2329 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2330
2331 } else {
2332 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2333 nir_print_instr(&instr->instr, stderr);
2334 fprintf(stderr, "\n");
2335 }
2336 break;
2337 }
2338 case nir_op_f2i64: {
2339 Temp src = get_alu_src(ctx, instr->src[0]);
2340 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
2341 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2342 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2343 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2344 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2345 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2346 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2347 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2348 Temp new_exponent = bld.tmp(v1);
2349 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2350 if (ctx->program->chip_class >= GFX8)
2351 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2352 else
2353 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2354 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2355 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2356 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2357 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2358 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2359 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2360 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2361 Temp new_lower = bld.tmp(v1);
2362 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2363 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2364 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2365
2366 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
2367 if (src.type() == RegType::vgpr)
2368 src = bld.as_uniform(src);
2369 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2370 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2371 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2372 exponent = bld.sop2(aco_opcode::s_min_u32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2373 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2374 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2375 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2376 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2377 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2378 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2379 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2380 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2381 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2382 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2383 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2384 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2385 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2386 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2387 Temp borrow = bld.tmp(s1);
2388 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2389 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2390 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2391
2392 } else if (instr->src[0].src.ssa->bit_size == 64) {
2393 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2394 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2395 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2396 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2397 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2398 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2399 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2400 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2401 if (dst.type() == RegType::sgpr) {
2402 lower = bld.as_uniform(lower);
2403 upper = bld.as_uniform(upper);
2404 }
2405 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2406
2407 } else {
2408 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2409 nir_print_instr(&instr->instr, stderr);
2410 fprintf(stderr, "\n");
2411 }
2412 break;
2413 }
2414 case nir_op_f2u64: {
2415 Temp src = get_alu_src(ctx, instr->src[0]);
2416 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
2417 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2418 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2419 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2420 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2421 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2422 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2423 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2424 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2425 Temp new_exponent = bld.tmp(v1);
2426 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2427 if (ctx->program->chip_class >= GFX8)
2428 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2429 else
2430 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2431 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2432 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2433 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2434 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2435 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2436 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2437 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2438
2439 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
2440 if (src.type() == RegType::vgpr)
2441 src = bld.as_uniform(src);
2442 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2443 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2444 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2445 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2446 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2447 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2448 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2449 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2450 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2451 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2452 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2453 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2454 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2455 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2456 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2457 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2458 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2459 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2460
2461 } else if (instr->src[0].src.ssa->bit_size == 64) {
2462 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2463 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2464 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2465 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2466 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2467 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2468 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2469 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2470 if (dst.type() == RegType::sgpr) {
2471 lower = bld.as_uniform(lower);
2472 upper = bld.as_uniform(upper);
2473 }
2474 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2475
2476 } else {
2477 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2478 nir_print_instr(&instr->instr, stderr);
2479 fprintf(stderr, "\n");
2480 }
2481 break;
2482 }
2483 case nir_op_b2f32: {
2484 Temp src = get_alu_src(ctx, instr->src[0]);
2485 assert(src.regClass() == bld.lm);
2486
2487 if (dst.regClass() == s1) {
2488 src = bool_to_scalar_condition(ctx, src);
2489 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2490 } else if (dst.regClass() == v1) {
2491 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2492 } else {
2493 unreachable("Wrong destination register class for nir_op_b2f32.");
2494 }
2495 break;
2496 }
2497 case nir_op_b2f64: {
2498 Temp src = get_alu_src(ctx, instr->src[0]);
2499 assert(src.regClass() == bld.lm);
2500
2501 if (dst.regClass() == s2) {
2502 src = bool_to_scalar_condition(ctx, src);
2503 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2504 } else if (dst.regClass() == v2) {
2505 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2506 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2507 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2508 } else {
2509 unreachable("Wrong destination register class for nir_op_b2f64.");
2510 }
2511 break;
2512 }
2513 case nir_op_i2i8:
2514 case nir_op_u2u8: {
2515 Temp src = get_alu_src(ctx, instr->src[0]);
2516 /* we can actually just say dst = src */
2517 if (src.regClass() == s1)
2518 bld.copy(Definition(dst), src);
2519 else
2520 emit_extract_vector(ctx, src, 0, dst);
2521 break;
2522 }
2523 case nir_op_i2i16: {
2524 Temp src = get_alu_src(ctx, instr->src[0]);
2525 if (instr->src[0].src.ssa->bit_size == 8) {
2526 if (dst.regClass() == s1) {
2527 bld.sop1(aco_opcode::s_sext_i32_i8, Definition(dst), Operand(src));
2528 } else {
2529 assert(src.regClass() == v1b);
2530 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2531 sdwa->operands[0] = Operand(src);
2532 sdwa->definitions[0] = Definition(dst);
2533 sdwa->sel[0] = sdwa_sbyte;
2534 sdwa->dst_sel = sdwa_sword;
2535 ctx->block->instructions.emplace_back(std::move(sdwa));
2536 }
2537 } else {
2538 Temp src = get_alu_src(ctx, instr->src[0]);
2539 /* we can actually just say dst = src */
2540 if (src.regClass() == s1)
2541 bld.copy(Definition(dst), src);
2542 else
2543 emit_extract_vector(ctx, src, 0, dst);
2544 }
2545 break;
2546 }
2547 case nir_op_u2u16: {
2548 Temp src = get_alu_src(ctx, instr->src[0]);
2549 if (instr->src[0].src.ssa->bit_size == 8) {
2550 if (dst.regClass() == s1)
2551 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFu), src);
2552 else {
2553 assert(src.regClass() == v1b);
2554 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2555 sdwa->operands[0] = Operand(src);
2556 sdwa->definitions[0] = Definition(dst);
2557 sdwa->sel[0] = sdwa_ubyte;
2558 sdwa->dst_sel = sdwa_uword;
2559 ctx->block->instructions.emplace_back(std::move(sdwa));
2560 }
2561 } else {
2562 Temp src = get_alu_src(ctx, instr->src[0]);
2563 /* we can actually just say dst = src */
2564 if (src.regClass() == s1)
2565 bld.copy(Definition(dst), src);
2566 else
2567 emit_extract_vector(ctx, src, 0, dst);
2568 }
2569 break;
2570 }
2571 case nir_op_i2i32: {
2572 Temp src = get_alu_src(ctx, instr->src[0]);
2573 if (instr->src[0].src.ssa->bit_size == 8) {
2574 if (dst.regClass() == s1) {
2575 bld.sop1(aco_opcode::s_sext_i32_i8, Definition(dst), Operand(src));
2576 } else {
2577 assert(src.regClass() == v1b);
2578 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2579 sdwa->operands[0] = Operand(src);
2580 sdwa->definitions[0] = Definition(dst);
2581 sdwa->sel[0] = sdwa_sbyte;
2582 sdwa->dst_sel = sdwa_sdword;
2583 ctx->block->instructions.emplace_back(std::move(sdwa));
2584 }
2585 } else if (instr->src[0].src.ssa->bit_size == 16) {
2586 if (dst.regClass() == s1) {
2587 bld.sop1(aco_opcode::s_sext_i32_i16, Definition(dst), Operand(src));
2588 } else {
2589 assert(src.regClass() == v2b);
2590 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2591 sdwa->operands[0] = Operand(src);
2592 sdwa->definitions[0] = Definition(dst);
2593 sdwa->sel[0] = sdwa_sword;
2594 sdwa->dst_sel = sdwa_udword;
2595 ctx->block->instructions.emplace_back(std::move(sdwa));
2596 }
2597 } else if (instr->src[0].src.ssa->bit_size == 64) {
2598 /* we can actually just say dst = src, as it would map the lower register */
2599 emit_extract_vector(ctx, src, 0, dst);
2600 } else {
2601 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2602 nir_print_instr(&instr->instr, stderr);
2603 fprintf(stderr, "\n");
2604 }
2605 break;
2606 }
2607 case nir_op_u2u32: {
2608 Temp src = get_alu_src(ctx, instr->src[0]);
2609 if (instr->src[0].src.ssa->bit_size == 8) {
2610 if (dst.regClass() == s1)
2611 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFu), src);
2612 else {
2613 assert(src.regClass() == v1b);
2614 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2615 sdwa->operands[0] = Operand(src);
2616 sdwa->definitions[0] = Definition(dst);
2617 sdwa->sel[0] = sdwa_ubyte;
2618 sdwa->dst_sel = sdwa_udword;
2619 ctx->block->instructions.emplace_back(std::move(sdwa));
2620 }
2621 } else if (instr->src[0].src.ssa->bit_size == 16) {
2622 if (dst.regClass() == s1) {
2623 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFFFu), src);
2624 } else {
2625 assert(src.regClass() == v2b);
2626 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2627 sdwa->operands[0] = Operand(src);
2628 sdwa->definitions[0] = Definition(dst);
2629 sdwa->sel[0] = sdwa_uword;
2630 sdwa->dst_sel = sdwa_udword;
2631 ctx->block->instructions.emplace_back(std::move(sdwa));
2632 }
2633 } else if (instr->src[0].src.ssa->bit_size == 64) {
2634 /* we can actually just say dst = src, as it would map the lower register */
2635 emit_extract_vector(ctx, src, 0, dst);
2636 } else {
2637 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2638 nir_print_instr(&instr->instr, stderr);
2639 fprintf(stderr, "\n");
2640 }
2641 break;
2642 }
2643 case nir_op_i2i64: {
2644 Temp src = get_alu_src(ctx, instr->src[0]);
2645 if (src.regClass() == s1) {
2646 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2647 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2648 } else if (src.regClass() == v1) {
2649 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2650 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2651 } else {
2652 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2653 nir_print_instr(&instr->instr, stderr);
2654 fprintf(stderr, "\n");
2655 }
2656 break;
2657 }
2658 case nir_op_u2u64: {
2659 Temp src = get_alu_src(ctx, instr->src[0]);
2660 if (instr->src[0].src.ssa->bit_size == 32) {
2661 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, Operand(0u));
2662 } else {
2663 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2664 nir_print_instr(&instr->instr, stderr);
2665 fprintf(stderr, "\n");
2666 }
2667 break;
2668 }
2669 case nir_op_b2b32:
2670 case nir_op_b2i32: {
2671 Temp src = get_alu_src(ctx, instr->src[0]);
2672 assert(src.regClass() == bld.lm);
2673
2674 if (dst.regClass() == s1) {
2675 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2676 bool_to_scalar_condition(ctx, src, dst);
2677 } else if (dst.regClass() == v1) {
2678 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2679 } else {
2680 unreachable("Invalid register class for b2i32");
2681 }
2682 break;
2683 }
2684 case nir_op_b2b1:
2685 case nir_op_i2b1: {
2686 Temp src = get_alu_src(ctx, instr->src[0]);
2687 assert(dst.regClass() == bld.lm);
2688
2689 if (src.type() == RegType::vgpr) {
2690 assert(src.regClass() == v1 || src.regClass() == v2);
2691 assert(dst.regClass() == bld.lm);
2692 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2693 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2694 } else {
2695 assert(src.regClass() == s1 || src.regClass() == s2);
2696 Temp tmp;
2697 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2698 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2699 } else {
2700 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2701 bld.scc(bld.def(s1)), Operand(0u), src);
2702 }
2703 bool_to_vector_condition(ctx, tmp, dst);
2704 }
2705 break;
2706 }
2707 case nir_op_pack_64_2x32_split: {
2708 Temp src0 = get_alu_src(ctx, instr->src[0]);
2709 Temp src1 = get_alu_src(ctx, instr->src[1]);
2710
2711 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2712 break;
2713 }
2714 case nir_op_unpack_64_2x32_split_x:
2715 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2716 break;
2717 case nir_op_unpack_64_2x32_split_y:
2718 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2719 break;
2720 case nir_op_unpack_32_2x16_split_x:
2721 if (dst.type() == RegType::vgpr) {
2722 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2723 } else {
2724 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2725 }
2726 break;
2727 case nir_op_unpack_32_2x16_split_y:
2728 if (dst.type() == RegType::vgpr) {
2729 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2730 } else {
2731 bld.sop2(aco_opcode::s_bfe_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), Operand(uint32_t(16 << 16 | 16)));
2732 }
2733 break;
2734 case nir_op_pack_32_2x16_split: {
2735 Temp src0 = get_alu_src(ctx, instr->src[0]);
2736 Temp src1 = get_alu_src(ctx, instr->src[1]);
2737 if (dst.regClass() == v1) {
2738 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2739 } else {
2740 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2741 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2742 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2743 }
2744 break;
2745 }
2746 case nir_op_pack_half_2x16: {
2747 Temp src = get_alu_src(ctx, instr->src[0], 2);
2748
2749 if (dst.regClass() == v1) {
2750 Temp src0 = bld.tmp(v1);
2751 Temp src1 = bld.tmp(v1);
2752 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2753 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2754 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2755 else
2756 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2757 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2758 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2759 } else {
2760 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2761 nir_print_instr(&instr->instr, stderr);
2762 fprintf(stderr, "\n");
2763 }
2764 break;
2765 }
2766 case nir_op_unpack_half_2x16_split_x: {
2767 if (dst.regClass() == v1) {
2768 Builder bld(ctx->program, ctx->block);
2769 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2770 } else {
2771 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2772 nir_print_instr(&instr->instr, stderr);
2773 fprintf(stderr, "\n");
2774 }
2775 break;
2776 }
2777 case nir_op_unpack_half_2x16_split_y: {
2778 if (dst.regClass() == v1) {
2779 Builder bld(ctx->program, ctx->block);
2780 /* TODO: use SDWA here */
2781 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2782 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2783 } else {
2784 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2785 nir_print_instr(&instr->instr, stderr);
2786 fprintf(stderr, "\n");
2787 }
2788 break;
2789 }
2790 case nir_op_fquantize2f16: {
2791 Temp src = get_alu_src(ctx, instr->src[0]);
2792 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2793 Temp f32, cmp_res;
2794
2795 if (ctx->program->chip_class >= GFX8) {
2796 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2797 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2798 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2799 } else {
2800 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2801 * so compare the result and flush to 0 if it's smaller.
2802 */
2803 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2804 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2805 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2806 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2807 cmp_res = vop3->definitions[0].getTemp();
2808 }
2809
2810 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2811 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2812 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2813 } else {
2814 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2815 }
2816 break;
2817 }
2818 case nir_op_bfm: {
2819 Temp bits = get_alu_src(ctx, instr->src[0]);
2820 Temp offset = get_alu_src(ctx, instr->src[1]);
2821
2822 if (dst.regClass() == s1) {
2823 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2824 } else if (dst.regClass() == v1) {
2825 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2826 } else {
2827 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2828 nir_print_instr(&instr->instr, stderr);
2829 fprintf(stderr, "\n");
2830 }
2831 break;
2832 }
2833 case nir_op_bitfield_select: {
2834 /* (mask & insert) | (~mask & base) */
2835 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2836 Temp insert = get_alu_src(ctx, instr->src[1]);
2837 Temp base = get_alu_src(ctx, instr->src[2]);
2838
2839 /* dst = (insert & bitmask) | (base & ~bitmask) */
2840 if (dst.regClass() == s1) {
2841 aco_ptr<Instruction> sop2;
2842 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2843 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2844 Operand lhs;
2845 if (const_insert && const_bitmask) {
2846 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2847 } else {
2848 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2849 lhs = Operand(insert);
2850 }
2851
2852 Operand rhs;
2853 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2854 if (const_base && const_bitmask) {
2855 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2856 } else {
2857 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2858 rhs = Operand(base);
2859 }
2860
2861 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2862
2863 } else if (dst.regClass() == v1) {
2864 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2865 base = as_vgpr(ctx, base);
2866 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2867 insert = as_vgpr(ctx, insert);
2868
2869 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2870
2871 } else {
2872 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2873 nir_print_instr(&instr->instr, stderr);
2874 fprintf(stderr, "\n");
2875 }
2876 break;
2877 }
2878 case nir_op_ubfe:
2879 case nir_op_ibfe: {
2880 Temp base = get_alu_src(ctx, instr->src[0]);
2881 Temp offset = get_alu_src(ctx, instr->src[1]);
2882 Temp bits = get_alu_src(ctx, instr->src[2]);
2883
2884 if (dst.type() == RegType::sgpr) {
2885 Operand extract;
2886 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2887 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2888 if (const_offset && const_bits) {
2889 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2890 extract = Operand(const_extract);
2891 } else {
2892 Operand width;
2893 if (const_bits) {
2894 width = Operand(const_bits->u32 << 16);
2895 } else {
2896 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2897 }
2898 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2899 }
2900
2901 aco_opcode opcode;
2902 if (dst.regClass() == s1) {
2903 if (instr->op == nir_op_ubfe)
2904 opcode = aco_opcode::s_bfe_u32;
2905 else
2906 opcode = aco_opcode::s_bfe_i32;
2907 } else if (dst.regClass() == s2) {
2908 if (instr->op == nir_op_ubfe)
2909 opcode = aco_opcode::s_bfe_u64;
2910 else
2911 opcode = aco_opcode::s_bfe_i64;
2912 } else {
2913 unreachable("Unsupported BFE bit size");
2914 }
2915
2916 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2917
2918 } else {
2919 aco_opcode opcode;
2920 if (dst.regClass() == v1) {
2921 if (instr->op == nir_op_ubfe)
2922 opcode = aco_opcode::v_bfe_u32;
2923 else
2924 opcode = aco_opcode::v_bfe_i32;
2925 } else {
2926 unreachable("Unsupported BFE bit size");
2927 }
2928
2929 emit_vop3a_instruction(ctx, instr, opcode, dst);
2930 }
2931 break;
2932 }
2933 case nir_op_bit_count: {
2934 Temp src = get_alu_src(ctx, instr->src[0]);
2935 if (src.regClass() == s1) {
2936 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2937 } else if (src.regClass() == v1) {
2938 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2939 } else if (src.regClass() == v2) {
2940 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2941 emit_extract_vector(ctx, src, 1, v1),
2942 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2943 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2944 } else if (src.regClass() == s2) {
2945 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2946 } else {
2947 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2948 nir_print_instr(&instr->instr, stderr);
2949 fprintf(stderr, "\n");
2950 }
2951 break;
2952 }
2953 case nir_op_flt: {
2954 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2955 break;
2956 }
2957 case nir_op_fge: {
2958 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2959 break;
2960 }
2961 case nir_op_feq: {
2962 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2963 break;
2964 }
2965 case nir_op_fne: {
2966 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2967 break;
2968 }
2969 case nir_op_ilt: {
2970 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);
2971 break;
2972 }
2973 case nir_op_ige: {
2974 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);
2975 break;
2976 }
2977 case nir_op_ieq: {
2978 if (instr->src[0].src.ssa->bit_size == 1)
2979 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2980 else
2981 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,
2982 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2983 break;
2984 }
2985 case nir_op_ine: {
2986 if (instr->src[0].src.ssa->bit_size == 1)
2987 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2988 else
2989 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,
2990 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2991 break;
2992 }
2993 case nir_op_ult: {
2994 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);
2995 break;
2996 }
2997 case nir_op_uge: {
2998 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);
2999 break;
3000 }
3001 case nir_op_fddx:
3002 case nir_op_fddy:
3003 case nir_op_fddx_fine:
3004 case nir_op_fddy_fine:
3005 case nir_op_fddx_coarse:
3006 case nir_op_fddy_coarse: {
3007 Temp src = get_alu_src(ctx, instr->src[0]);
3008 uint16_t dpp_ctrl1, dpp_ctrl2;
3009 if (instr->op == nir_op_fddx_fine) {
3010 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
3011 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
3012 } else if (instr->op == nir_op_fddy_fine) {
3013 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
3014 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
3015 } else {
3016 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
3017 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
3018 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
3019 else
3020 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
3021 }
3022
3023 Temp tmp;
3024 if (ctx->program->chip_class >= GFX8) {
3025 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
3026 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
3027 } else {
3028 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
3029 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3030 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3031 }
3032 emit_wqm(ctx, tmp, dst, true);
3033 break;
3034 }
3035 default:
3036 fprintf(stderr, "Unknown NIR ALU instr: ");
3037 nir_print_instr(&instr->instr, stderr);
3038 fprintf(stderr, "\n");
3039 }
3040 }
3041
3042 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3043 {
3044 Temp dst = get_ssa_temp(ctx, &instr->def);
3045
3046 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3047 // which get truncated the lsb if double and msb if int
3048 // for now, we only use s_mov_b64 with 64bit inline constants
3049 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3050 assert(dst.type() == RegType::sgpr);
3051
3052 Builder bld(ctx->program, ctx->block);
3053
3054 if (instr->def.bit_size == 1) {
3055 assert(dst.regClass() == bld.lm);
3056 int val = instr->value[0].b ? -1 : 0;
3057 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3058 bld.sop1(Builder::s_mov, Definition(dst), op);
3059 } else if (dst.size() == 1) {
3060 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3061 } else {
3062 assert(dst.size() != 1);
3063 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3064 if (instr->def.bit_size == 64)
3065 for (unsigned i = 0; i < dst.size(); i++)
3066 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3067 else {
3068 for (unsigned i = 0; i < dst.size(); i++)
3069 vec->operands[i] = Operand{instr->value[i].u32};
3070 }
3071 vec->definitions[0] = Definition(dst);
3072 ctx->block->instructions.emplace_back(std::move(vec));
3073 }
3074 }
3075
3076 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3077 {
3078 uint32_t new_mask = 0;
3079 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3080 if (mask & (1u << i))
3081 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3082 return new_mask;
3083 }
3084
3085 Operand load_lds_size_m0(isel_context *ctx)
3086 {
3087 /* TODO: m0 does not need to be initialized on GFX9+ */
3088 Builder bld(ctx->program, ctx->block);
3089 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3090 }
3091
3092 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3093 Temp address, unsigned base_offset, unsigned align)
3094 {
3095 assert(util_is_power_of_two_nonzero(align) && align >= 4);
3096
3097 Builder bld(ctx->program, ctx->block);
3098
3099 Operand m = load_lds_size_m0(ctx);
3100
3101 unsigned num_components = dst.size() * 4u / elem_size_bytes;
3102 unsigned bytes_read = 0;
3103 unsigned result_size = 0;
3104 unsigned total_bytes = num_components * elem_size_bytes;
3105 std::array<Temp, NIR_MAX_VEC_COMPONENTS> result;
3106 bool large_ds_read = ctx->options->chip_class >= GFX7;
3107 bool usable_read2 = ctx->options->chip_class >= GFX7;
3108
3109 while (bytes_read < total_bytes) {
3110 unsigned todo = total_bytes - bytes_read;
3111 bool aligned8 = bytes_read % 8 == 0 && align % 8 == 0;
3112 bool aligned16 = bytes_read % 16 == 0 && align % 16 == 0;
3113
3114 aco_opcode op = aco_opcode::last_opcode;
3115 bool read2 = false;
3116 if (todo >= 16 && aligned16 && large_ds_read) {
3117 op = aco_opcode::ds_read_b128;
3118 todo = 16;
3119 } else if (todo >= 16 && aligned8 && usable_read2) {
3120 op = aco_opcode::ds_read2_b64;
3121 read2 = true;
3122 todo = 16;
3123 } else if (todo >= 12 && aligned16 && large_ds_read) {
3124 op = aco_opcode::ds_read_b96;
3125 todo = 12;
3126 } else if (todo >= 8 && aligned8) {
3127 op = aco_opcode::ds_read_b64;
3128 todo = 8;
3129 } else if (todo >= 8 && usable_read2) {
3130 op = aco_opcode::ds_read2_b32;
3131 read2 = true;
3132 todo = 8;
3133 } else if (todo >= 4) {
3134 op = aco_opcode::ds_read_b32;
3135 todo = 4;
3136 } else {
3137 assert(false);
3138 }
3139 assert(todo % elem_size_bytes == 0);
3140 unsigned num_elements = todo / elem_size_bytes;
3141 unsigned offset = base_offset + bytes_read;
3142 unsigned max_offset = read2 ? 1019 : 65535;
3143
3144 Temp address_offset = address;
3145 if (offset > max_offset) {
3146 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3147 offset = bytes_read;
3148 }
3149 assert(offset <= max_offset); /* bytes_read shouldn't be large enough for this to happen */
3150
3151 Temp res;
3152 if (num_components == 1 && dst.type() == RegType::vgpr)
3153 res = dst;
3154 else
3155 res = bld.tmp(RegClass(RegType::vgpr, todo / 4));
3156
3157 if (read2)
3158 res = bld.ds(op, Definition(res), address_offset, m, offset / (todo / 2), (offset / (todo / 2)) + 1);
3159 else
3160 res = bld.ds(op, Definition(res), address_offset, m, offset);
3161
3162 if (num_components == 1) {
3163 assert(todo == total_bytes);
3164 if (dst.type() == RegType::sgpr)
3165 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
3166 return dst;
3167 }
3168
3169 if (dst.type() == RegType::sgpr) {
3170 Temp new_res = bld.tmp(RegType::sgpr, res.size());
3171 expand_vector(ctx, res, new_res, res.size(), (1 << res.size()) - 1);
3172 res = new_res;
3173 }
3174
3175 if (num_elements == 1) {
3176 result[result_size++] = res;
3177 } else {
3178 assert(res != dst && res.size() % num_elements == 0);
3179 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_elements)};
3180 split->operands[0] = Operand(res);
3181 for (unsigned i = 0; i < num_elements; i++)
3182 split->definitions[i] = Definition(result[result_size++] = bld.tmp(res.type(), elem_size_bytes / 4));
3183 ctx->block->instructions.emplace_back(std::move(split));
3184 }
3185
3186 bytes_read += todo;
3187 }
3188
3189 assert(result_size == num_components && result_size > 1);
3190 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, result_size, 1)};
3191 for (unsigned i = 0; i < result_size; i++)
3192 vec->operands[i] = Operand(result[i]);
3193 vec->definitions[0] = Definition(dst);
3194 ctx->block->instructions.emplace_back(std::move(vec));
3195 ctx->allocated_vec.emplace(dst.id(), result);
3196
3197 return dst;
3198 }
3199
3200 Temp extract_subvector(isel_context *ctx, Temp data, unsigned start, unsigned size, RegType type)
3201 {
3202 if (start == 0 && size == data.size())
3203 return type == RegType::vgpr ? as_vgpr(ctx, data) : data;
3204
3205 unsigned size_hint = 1;
3206 auto it = ctx->allocated_vec.find(data.id());
3207 if (it != ctx->allocated_vec.end())
3208 size_hint = it->second[0].size();
3209 if (size % size_hint || start % size_hint)
3210 size_hint = 1;
3211
3212 start /= size_hint;
3213 size /= size_hint;
3214
3215 Temp elems[size];
3216 for (unsigned i = 0; i < size; i++)
3217 elems[i] = emit_extract_vector(ctx, data, start + i, RegClass(type, size_hint));
3218
3219 if (size == 1)
3220 return type == RegType::vgpr ? as_vgpr(ctx, elems[0]) : elems[0];
3221
3222 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
3223 for (unsigned i = 0; i < size; i++)
3224 vec->operands[i] = Operand(elems[i]);
3225 Temp res = {ctx->program->allocateId(), RegClass(type, size * size_hint)};
3226 vec->definitions[0] = Definition(res);
3227 ctx->block->instructions.emplace_back(std::move(vec));
3228 return res;
3229 }
3230
3231 void ds_write_helper(isel_context *ctx, Operand m, Temp address, Temp data, unsigned data_start, unsigned total_size, unsigned offset0, unsigned offset1, unsigned align)
3232 {
3233 Builder bld(ctx->program, ctx->block);
3234 unsigned bytes_written = 0;
3235 bool large_ds_write = ctx->options->chip_class >= GFX7;
3236 bool usable_write2 = ctx->options->chip_class >= GFX7;
3237
3238 while (bytes_written < total_size * 4) {
3239 unsigned todo = total_size * 4 - bytes_written;
3240 bool aligned8 = bytes_written % 8 == 0 && align % 8 == 0;
3241 bool aligned16 = bytes_written % 16 == 0 && align % 16 == 0;
3242
3243 aco_opcode op = aco_opcode::last_opcode;
3244 bool write2 = false;
3245 unsigned size = 0;
3246 if (todo >= 16 && aligned16 && large_ds_write) {
3247 op = aco_opcode::ds_write_b128;
3248 size = 4;
3249 } else if (todo >= 16 && aligned8 && usable_write2) {
3250 op = aco_opcode::ds_write2_b64;
3251 write2 = true;
3252 size = 4;
3253 } else if (todo >= 12 && aligned16 && large_ds_write) {
3254 op = aco_opcode::ds_write_b96;
3255 size = 3;
3256 } else if (todo >= 8 && aligned8) {
3257 op = aco_opcode::ds_write_b64;
3258 size = 2;
3259 } else if (todo >= 8 && usable_write2) {
3260 op = aco_opcode::ds_write2_b32;
3261 write2 = true;
3262 size = 2;
3263 } else if (todo >= 4) {
3264 op = aco_opcode::ds_write_b32;
3265 size = 1;
3266 } else {
3267 assert(false);
3268 }
3269
3270 unsigned offset = offset0 + offset1 + bytes_written;
3271 unsigned max_offset = write2 ? 1020 : 65535;
3272 Temp address_offset = address;
3273 if (offset > max_offset) {
3274 address_offset = bld.vadd32(bld.def(v1), Operand(offset0), address_offset);
3275 offset = offset1 + bytes_written;
3276 }
3277 assert(offset <= max_offset); /* offset1 shouldn't be large enough for this to happen */
3278
3279 if (write2) {
3280 Temp val0 = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size / 2, RegType::vgpr);
3281 Temp val1 = extract_subvector(ctx, data, data_start + (bytes_written >> 2) + 1, size / 2, RegType::vgpr);
3282 bld.ds(op, address_offset, val0, val1, m, offset / size / 2, (offset / size / 2) + 1);
3283 } else {
3284 Temp val = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size, RegType::vgpr);
3285 bld.ds(op, address_offset, val, m, offset);
3286 }
3287
3288 bytes_written += size * 4;
3289 }
3290 }
3291
3292 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3293 Temp address, unsigned base_offset, unsigned align)
3294 {
3295 assert(util_is_power_of_two_nonzero(align) && align >= 4);
3296 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3297
3298 Operand m = load_lds_size_m0(ctx);
3299
3300 /* we need at most two stores, assuming that the writemask is at most 4 bits wide */
3301 assert(wrmask <= 0x0f);
3302 int start[2], count[2];
3303 u_bit_scan_consecutive_range(&wrmask, &start[0], &count[0]);
3304 u_bit_scan_consecutive_range(&wrmask, &start[1], &count[1]);
3305 assert(wrmask == 0);
3306
3307 /* one combined store is sufficient */
3308 if (count[0] == count[1] && (align % elem_size_bytes) == 0 && (base_offset % elem_size_bytes) == 0) {
3309 Builder bld(ctx->program, ctx->block);
3310
3311 Temp address_offset = address;
3312 if ((base_offset / elem_size_bytes) + start[1] > 255) {
3313 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3314 base_offset = 0;
3315 }
3316
3317 assert(count[0] == 1);
3318 RegClass xtract_rc(RegType::vgpr, elem_size_bytes / 4);
3319
3320 Temp val0 = emit_extract_vector(ctx, data, start[0], xtract_rc);
3321 Temp val1 = emit_extract_vector(ctx, data, start[1], xtract_rc);
3322 aco_opcode op = elem_size_bytes == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3323 base_offset = base_offset / elem_size_bytes;
3324 bld.ds(op, address_offset, val0, val1, m,
3325 base_offset + start[0], base_offset + start[1]);
3326 return;
3327 }
3328
3329 for (unsigned i = 0; i < 2; i++) {
3330 if (count[i] == 0)
3331 continue;
3332
3333 unsigned elem_size_words = elem_size_bytes / 4;
3334 ds_write_helper(ctx, m, address, data, start[i] * elem_size_words, count[i] * elem_size_words,
3335 base_offset, start[i] * elem_size_bytes, align);
3336 }
3337 return;
3338 }
3339
3340 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3341 {
3342 unsigned align = 16;
3343 if (const_offset)
3344 align = std::min(align, 1u << (ffs(const_offset) - 1));
3345
3346 return align;
3347 }
3348
3349
3350 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3351 unsigned split_cnt = 0u, Temp dst = Temp())
3352 {
3353 Builder bld(ctx->program, ctx->block);
3354 unsigned dword_size = elem_size_bytes / 4;
3355
3356 if (!dst.id())
3357 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3358
3359 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3360 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3361 instr->definitions[0] = Definition(dst);
3362
3363 for (unsigned i = 0; i < cnt; ++i) {
3364 if (arr[i].id()) {
3365 assert(arr[i].size() == dword_size);
3366 allocated_vec[i] = arr[i];
3367 instr->operands[i] = Operand(arr[i]);
3368 } else {
3369 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3370 allocated_vec[i] = zero;
3371 instr->operands[i] = Operand(zero);
3372 }
3373 }
3374
3375 bld.insert(std::move(instr));
3376
3377 if (split_cnt)
3378 emit_split_vector(ctx, dst, split_cnt);
3379 else
3380 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3381
3382 return dst;
3383 }
3384
3385 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3386 {
3387 if (const_offset >= 4096) {
3388 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3389 const_offset %= 4096u;
3390
3391 if (!voffset.id())
3392 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3393 else if (unlikely(voffset.regClass() == s1))
3394 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3395 else if (likely(voffset.regClass() == v1))
3396 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3397 else
3398 unreachable("Unsupported register class of voffset");
3399 }
3400
3401 return const_offset;
3402 }
3403
3404 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3405 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3406 {
3407 assert(vdata.id());
3408 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3409 assert(vdata.size() >= 1 && vdata.size() <= 4);
3410
3411 Builder bld(ctx->program, ctx->block);
3412 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_store_dword + vdata.size() - 1);
3413 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3414
3415 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3416 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3417 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3418 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3419 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3420
3421 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3422 }
3423
3424 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3425 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3426 bool allow_combining = true, bool reorder = true, bool slc = false)
3427 {
3428 Builder bld(ctx->program, ctx->block);
3429 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3430 assert(write_mask);
3431
3432 if (elem_size_bytes == 8) {
3433 elem_size_bytes = 4;
3434 write_mask = widen_mask(write_mask, 2);
3435 }
3436
3437 while (write_mask) {
3438 int start = 0;
3439 int count = 0;
3440 u_bit_scan_consecutive_range(&write_mask, &start, &count);
3441 assert(count > 0);
3442 assert(start >= 0);
3443
3444 while (count > 0) {
3445 unsigned sub_count = allow_combining ? MIN2(count, 4) : 1;
3446 unsigned const_offset = (unsigned) start * elem_size_bytes + base_const_offset;
3447
3448 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
3449 if (unlikely(ctx->program->chip_class == GFX6 && sub_count == 3))
3450 sub_count = 2;
3451
3452 Temp elem = extract_subvector(ctx, src, start, sub_count, RegType::vgpr);
3453 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, elem, const_offset, reorder, slc);
3454
3455 count -= sub_count;
3456 start += sub_count;
3457 }
3458
3459 assert(count == 0);
3460 }
3461 }
3462
3463 Temp emit_single_mubuf_load(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset,
3464 unsigned const_offset, unsigned size_dwords, bool allow_reorder = true)
3465 {
3466 assert(size_dwords != 3 || ctx->program->chip_class != GFX6);
3467 assert(size_dwords >= 1 && size_dwords <= 4);
3468
3469 Builder bld(ctx->program, ctx->block);
3470 Temp vdata = bld.tmp(RegClass(RegType::vgpr, size_dwords));
3471 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_load_dword + size_dwords - 1);
3472 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3473
3474 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3475 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3476 Builder::Result r = bld.mubuf(op, Definition(vdata), Operand(descriptor), voffset_op, soffset_op, const_offset,
3477 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3478 /* disable_wqm */ false, /* glc */ true,
3479 /* dlc*/ ctx->program->chip_class >= GFX10, /* slc */ false);
3480
3481 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3482
3483 return vdata;
3484 }
3485
3486 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3487 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3488 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3489 {
3490 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3491 assert((num_components * elem_size_bytes / 4) == dst.size());
3492 assert(!!stride != allow_combining);
3493
3494 Builder bld(ctx->program, ctx->block);
3495 unsigned split_cnt = num_components;
3496
3497 if (elem_size_bytes == 8) {
3498 elem_size_bytes = 4;
3499 num_components *= 2;
3500 }
3501
3502 if (!stride)
3503 stride = elem_size_bytes;
3504
3505 unsigned load_size = 1;
3506 if (allow_combining) {
3507 if ((num_components % 4) == 0)
3508 load_size = 4;
3509 else if ((num_components % 3) == 0 && ctx->program->chip_class != GFX6)
3510 load_size = 3;
3511 else if ((num_components % 2) == 0)
3512 load_size = 2;
3513 }
3514
3515 unsigned num_loads = num_components / load_size;
3516 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
3517
3518 for (unsigned i = 0; i < num_loads; ++i) {
3519 unsigned const_offset = i * stride * load_size + base_const_offset;
3520 elems[i] = emit_single_mubuf_load(ctx, descriptor, voffset, soffset, const_offset, load_size, allow_reorder);
3521 }
3522
3523 create_vec_from_array(ctx, elems.data(), num_loads, RegType::vgpr, load_size * 4u, split_cnt, dst);
3524 }
3525
3526 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)
3527 {
3528 Builder bld(ctx->program, ctx->block);
3529 Temp offset = base_offset.first;
3530 unsigned const_offset = base_offset.second;
3531
3532 if (!nir_src_is_const(*off_src)) {
3533 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3534 Temp with_stride;
3535
3536 /* Calculate indirect offset with stride */
3537 if (likely(indirect_offset_arg.regClass() == v1))
3538 with_stride = bld.v_mul_imm(bld.def(v1), indirect_offset_arg, stride);
3539 else if (indirect_offset_arg.regClass() == s1)
3540 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3541 else
3542 unreachable("Unsupported register class of indirect offset");
3543
3544 /* Add to the supplied base offset */
3545 if (offset.id() == 0)
3546 offset = with_stride;
3547 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
3548 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
3549 else if (offset.size() == 1 && with_stride.size() == 1)
3550 offset = bld.vadd32(bld.def(v1), with_stride, offset);
3551 else
3552 unreachable("Unsupported register class of indirect offset");
3553 } else {
3554 unsigned const_offset_arg = nir_src_as_uint(*off_src);
3555 const_offset += const_offset_arg * stride;
3556 }
3557
3558 return std::make_pair(offset, const_offset);
3559 }
3560
3561 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
3562 {
3563 Builder bld(ctx->program, ctx->block);
3564 Temp offset;
3565
3566 if (off1.first.id() && off2.first.id()) {
3567 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
3568 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
3569 else if (off1.first.size() == 1 && off2.first.size() == 1)
3570 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
3571 else
3572 unreachable("Unsupported register class of indirect offset");
3573 } else {
3574 offset = off1.first.id() ? off1.first : off2.first;
3575 }
3576
3577 return std::make_pair(offset, off1.second + off2.second);
3578 }
3579
3580 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
3581 {
3582 Builder bld(ctx->program, ctx->block);
3583 unsigned const_offset = offs.second * multiplier;
3584
3585 if (!offs.first.id())
3586 return std::make_pair(offs.first, const_offset);
3587
3588 Temp offset = unlikely(offs.first.regClass() == s1)
3589 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
3590 : bld.v_mul_imm(bld.def(v1), offs.first, multiplier);
3591
3592 return std::make_pair(offset, const_offset);
3593 }
3594
3595 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
3596 {
3597 Builder bld(ctx->program, ctx->block);
3598
3599 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3600 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
3601 /* component is in bytes */
3602 const_offset += nir_intrinsic_component(instr) * component_stride;
3603
3604 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3605 nir_src *off_src = nir_get_io_offset_src(instr);
3606 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
3607 }
3608
3609 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
3610 {
3611 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
3612 }
3613
3614 Temp get_tess_rel_patch_id(isel_context *ctx)
3615 {
3616 Builder bld(ctx->program, ctx->block);
3617
3618 switch (ctx->shader->info.stage) {
3619 case MESA_SHADER_TESS_CTRL:
3620 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
3621 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
3622 case MESA_SHADER_TESS_EVAL:
3623 return get_arg(ctx, ctx->args->tes_rel_patch_id);
3624 default:
3625 unreachable("Unsupported stage in get_tess_rel_patch_id");
3626 }
3627 }
3628
3629 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3630 {
3631 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3632 Builder bld(ctx->program, ctx->block);
3633
3634 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
3635 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
3636
3637 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
3638
3639 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3640 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
3641
3642 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3643 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
3644 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
3645
3646 return offset_mul(ctx, offs, 4u);
3647 }
3648
3649 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
3650 {
3651 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3652 Builder bld(ctx->program, ctx->block);
3653
3654 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
3655 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
3656 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
3657 uint32_t output_vertex_size = num_tcs_outputs * 16;
3658 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3659 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
3660
3661 std::pair<Temp, unsigned> offs = instr
3662 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
3663 : std::make_pair(Temp(), 0u);
3664
3665 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3666 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
3667
3668 if (per_vertex) {
3669 assert(instr);
3670
3671 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3672 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
3673
3674 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
3675 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
3676 } else {
3677 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
3678 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
3679 }
3680
3681 return offs;
3682 }
3683
3684 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3685 {
3686 Builder bld(ctx->program, ctx->block);
3687
3688 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
3689 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
3690
3691 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
3692
3693 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3694 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
3695 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
3696
3697 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3698 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
3699
3700 return offs;
3701 }
3702
3703 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
3704 {
3705 Builder bld(ctx->program, ctx->block);
3706
3707 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
3708 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
3709 : ctx->args->options->key.tes.tcs_num_outputs;
3710
3711 unsigned output_vertex_size = num_tcs_outputs * 16;
3712 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3713 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
3714 unsigned attr_stride = ctx->tcs_num_patches;
3715
3716 std::pair<Temp, unsigned> offs = instr
3717 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
3718 : std::make_pair(Temp(), 0u);
3719
3720 if (const_base_offset)
3721 offs.second += const_base_offset * attr_stride;
3722
3723 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3724 Temp patch_off = bld.v_mul_imm(bld.def(v1), rel_patch_id, 16u);
3725 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
3726
3727 return offs;
3728 }
3729
3730 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
3731 {
3732 unsigned off = nir_intrinsic_base(instr) * 4u;
3733 nir_src *off_src = nir_get_io_offset_src(instr);
3734
3735 if (!nir_src_is_const(*off_src)) {
3736 *indirect = true;
3737 return false;
3738 }
3739
3740 *indirect = false;
3741 off += nir_src_as_uint(*off_src) * 16u;
3742
3743 while (mask) {
3744 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
3745 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
3746 return true;
3747 }
3748
3749 return false;
3750 }
3751
3752 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
3753 {
3754 unsigned write_mask = nir_intrinsic_write_mask(instr);
3755 unsigned component = nir_intrinsic_component(instr);
3756 unsigned idx = nir_intrinsic_base(instr) + component;
3757
3758 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
3759 if (off_instr->type != nir_instr_type_load_const)
3760 return false;
3761
3762 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3763 idx += nir_src_as_uint(instr->src[1]) * 4u;
3764
3765 if (instr->src[0].ssa->bit_size == 64)
3766 write_mask = widen_mask(write_mask, 2);
3767
3768 for (unsigned i = 0; i < 8; ++i) {
3769 if (write_mask & (1 << i)) {
3770 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
3771 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
3772 }
3773 idx++;
3774 }
3775
3776 return true;
3777 }
3778
3779 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
3780 {
3781 /* Only TCS per-vertex inputs are supported by this function.
3782 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
3783 */
3784 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
3785 return false;
3786
3787 nir_src *off_src = nir_get_io_offset_src(instr);
3788 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3789 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
3790 bool can_use_temps = nir_src_is_const(*off_src) &&
3791 vertex_index_instr->type == nir_instr_type_intrinsic &&
3792 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
3793
3794 if (!can_use_temps)
3795 return false;
3796
3797 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
3798 Temp *src = &ctx->inputs.temps[idx];
3799 Temp vec = create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u);
3800 assert(vec.size() == dst.size());
3801
3802 Builder bld(ctx->program, ctx->block);
3803 bld.copy(Definition(dst), vec);
3804 return true;
3805 }
3806
3807 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
3808 {
3809 Builder bld(ctx->program, ctx->block);
3810
3811 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
3812 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3813 unsigned write_mask = nir_intrinsic_write_mask(instr);
3814 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
3815
3816 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
3817 /* 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. */
3818 bool indirect_write;
3819 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
3820 if (temp_only_input && !indirect_write)
3821 return;
3822 }
3823
3824 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
3825 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
3826 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
3827 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
3828 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
3829 } else {
3830 Temp lds_base;
3831
3832 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
3833 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
3834 unsigned itemsize = ctx->stage == vertex_geometry_gs
3835 ? ctx->program->info->vs.es_info.esgs_itemsize
3836 : ctx->program->info->tes.es_info.esgs_itemsize;
3837 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
3838 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));
3839 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
3840 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
3841 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
3842 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
3843 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
3844 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
3845 */
3846 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
3847 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
3848 lds_base = bld.v_mul_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
3849 } else {
3850 unreachable("Invalid LS or ES stage");
3851 }
3852
3853 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
3854 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3855 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
3856 }
3857 }
3858
3859 bool should_write_tcs_patch_output_to_vmem(isel_context *ctx, nir_intrinsic_instr *instr)
3860 {
3861 unsigned off = nir_intrinsic_base(instr) * 4u;
3862 return off != ctx->tcs_tess_lvl_out_loc &&
3863 off != ctx->tcs_tess_lvl_in_loc;
3864 }
3865
3866 bool should_write_tcs_output_to_lds(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3867 {
3868 /* When none of the appropriate outputs are read, we are OK to never write to LDS */
3869 if (per_vertex ? ctx->shader->info.outputs_read == 0U : ctx->shader->info.patch_outputs_read == 0u)
3870 return false;
3871
3872 uint64_t mask = per_vertex
3873 ? ctx->shader->info.outputs_read
3874 : ctx->shader->info.patch_outputs_read;
3875 bool indirect_write;
3876 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
3877 return indirect_write || output_read;
3878 }
3879
3880 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3881 {
3882 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3883 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3884
3885 Builder bld(ctx->program, ctx->block);
3886
3887 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
3888 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3889 unsigned write_mask = nir_intrinsic_write_mask(instr);
3890
3891 /* Only write to VMEM if the output is per-vertex or it's per-patch non tess factor */
3892 bool write_to_vmem = per_vertex || should_write_tcs_patch_output_to_vmem(ctx, instr);
3893 /* Only write to LDS if the output is read by the shader, or it's per-patch tess factor */
3894 bool write_to_lds = !write_to_vmem || should_write_tcs_output_to_lds(ctx, instr, per_vertex);
3895
3896 if (write_to_vmem) {
3897 std::pair<Temp, unsigned> vmem_offs = per_vertex
3898 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
3899 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
3900
3901 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));
3902 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
3903 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);
3904 }
3905
3906 if (write_to_lds) {
3907 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3908 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3909 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
3910 }
3911 }
3912
3913 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3914 {
3915 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3916 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3917
3918 Builder bld(ctx->program, ctx->block);
3919
3920 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3921 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3922 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3923 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3924
3925 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
3926 }
3927
3928 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
3929 {
3930 if (ctx->stage == vertex_vs ||
3931 ctx->stage == tess_eval_vs ||
3932 ctx->stage == fragment_fs ||
3933 ctx->stage == ngg_vertex_gs ||
3934 ctx->stage == ngg_tess_eval_gs ||
3935 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
3936 bool stored_to_temps = store_output_to_temps(ctx, instr);
3937 if (!stored_to_temps) {
3938 fprintf(stderr, "Unimplemented output offset instruction:\n");
3939 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
3940 fprintf(stderr, "\n");
3941 abort();
3942 }
3943 } else if (ctx->stage == vertex_es ||
3944 ctx->stage == vertex_ls ||
3945 ctx->stage == tess_eval_es ||
3946 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3947 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3948 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
3949 visit_store_ls_or_es_output(ctx, instr);
3950 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
3951 visit_store_tcs_output(ctx, instr, false);
3952 } else {
3953 unreachable("Shader stage not implemented");
3954 }
3955 }
3956
3957 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
3958 {
3959 visit_load_tcs_output(ctx, instr, false);
3960 }
3961
3962 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
3963 {
3964 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
3965 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
3966
3967 Builder bld(ctx->program, ctx->block);
3968 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
3969 if (ctx->program->has_16bank_lds)
3970 interp_p1.instr->operands[0].setLateKill(true);
3971 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
3972 }
3973
3974 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
3975 {
3976 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
3977 for (unsigned i = 0; i < num_components; i++)
3978 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
3979 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
3980 assert(num_components == 4);
3981 Builder bld(ctx->program, ctx->block);
3982 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
3983 }
3984
3985 for (Operand& op : vec->operands)
3986 op = op.isUndefined() ? Operand(0u) : op;
3987
3988 vec->definitions[0] = Definition(dst);
3989 ctx->block->instructions.emplace_back(std::move(vec));
3990 emit_split_vector(ctx, dst, num_components);
3991 return;
3992 }
3993
3994 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
3995 {
3996 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3997 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
3998 unsigned idx = nir_intrinsic_base(instr);
3999 unsigned component = nir_intrinsic_component(instr);
4000 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4001
4002 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4003 if (offset) {
4004 assert(offset->u32 == 0);
4005 } else {
4006 /* the lower 15bit of the prim_mask contain the offset into LDS
4007 * while the upper bits contain the number of prims */
4008 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4009 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4010 Builder bld(ctx->program, ctx->block);
4011 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4012 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4013 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4014 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4015 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4016 }
4017
4018 if (instr->dest.ssa.num_components == 1) {
4019 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4020 } else {
4021 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4022 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4023 {
4024 Temp tmp = {ctx->program->allocateId(), v1};
4025 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4026 vec->operands[i] = Operand(tmp);
4027 }
4028 vec->definitions[0] = Definition(dst);
4029 ctx->block->instructions.emplace_back(std::move(vec));
4030 }
4031 }
4032
4033 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4034 unsigned offset, unsigned stride, unsigned channels)
4035 {
4036 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4037 if (vtx_info->chan_byte_size != 4 && channels == 3)
4038 return false;
4039 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4040 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4041 }
4042
4043 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4044 unsigned offset, unsigned stride, unsigned *channels)
4045 {
4046 if (!vtx_info->chan_byte_size) {
4047 *channels = vtx_info->num_channels;
4048 return vtx_info->chan_format;
4049 }
4050
4051 unsigned num_channels = *channels;
4052 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4053 unsigned new_channels = num_channels + 1;
4054 /* first, assume more loads is worse and try using a larger data format */
4055 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4056 new_channels++;
4057 /* don't make the attribute potentially out-of-bounds */
4058 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4059 new_channels = 5;
4060 }
4061
4062 if (new_channels == 5) {
4063 /* then try decreasing load size (at the cost of more loads) */
4064 new_channels = *channels;
4065 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4066 new_channels--;
4067 }
4068
4069 if (new_channels < *channels)
4070 *channels = new_channels;
4071 num_channels = new_channels;
4072 }
4073
4074 switch (vtx_info->chan_format) {
4075 case V_008F0C_BUF_DATA_FORMAT_8:
4076 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4077 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4078 case V_008F0C_BUF_DATA_FORMAT_16:
4079 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4080 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4081 case V_008F0C_BUF_DATA_FORMAT_32:
4082 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4083 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4084 }
4085 unreachable("shouldn't reach here");
4086 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4087 }
4088
4089 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4090 * so we may need to fix it up. */
4091 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4092 {
4093 Builder bld(ctx->program, ctx->block);
4094
4095 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4096 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4097
4098 /* For the integer-like cases, do a natural sign extension.
4099 *
4100 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4101 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4102 * exponent.
4103 */
4104 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4105 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4106
4107 /* Convert back to the right type. */
4108 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4109 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4110 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4111 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4112 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4113 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4114 }
4115
4116 return alpha;
4117 }
4118
4119 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4120 {
4121 Builder bld(ctx->program, ctx->block);
4122 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4123 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4124
4125 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4126 if (off_instr->type != nir_instr_type_load_const) {
4127 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4128 nir_print_instr(off_instr, stderr);
4129 fprintf(stderr, "\n");
4130 }
4131 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4132
4133 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4134
4135 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4136 unsigned component = nir_intrinsic_component(instr);
4137 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4138 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4139 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4140 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4141
4142 unsigned dfmt = attrib_format & 0xf;
4143 unsigned nfmt = (attrib_format >> 4) & 0x7;
4144 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4145
4146 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4147 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4148 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4149 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4150 if (post_shuffle)
4151 num_channels = MAX2(num_channels, 3);
4152
4153 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4154 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4155
4156 Temp index;
4157 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4158 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4159 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4160 if (divisor) {
4161 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4162 if (divisor != 1) {
4163 Temp divided = bld.tmp(v1);
4164 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4165 index = bld.vadd32(bld.def(v1), start_instance, divided);
4166 } else {
4167 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4168 }
4169 } else {
4170 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4171 }
4172 } else {
4173 index = bld.vadd32(bld.def(v1),
4174 get_arg(ctx, ctx->args->ac.base_vertex),
4175 get_arg(ctx, ctx->args->ac.vertex_id));
4176 }
4177
4178 Temp channels[num_channels];
4179 unsigned channel_start = 0;
4180 bool direct_fetch = false;
4181
4182 /* skip unused channels at the start */
4183 if (vtx_info->chan_byte_size && !post_shuffle) {
4184 channel_start = ffs(mask) - 1;
4185 for (unsigned i = 0; i < channel_start; i++)
4186 channels[i] = Temp(0, s1);
4187 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4188 num_channels = 3 - (ffs(mask) - 1);
4189 }
4190
4191 /* load channels */
4192 while (channel_start < num_channels) {
4193 unsigned fetch_size = num_channels - channel_start;
4194 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4195 bool expanded = false;
4196
4197 /* use MUBUF when possible to avoid possible alignment issues */
4198 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4199 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4200 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4201 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4202 vtx_info->chan_byte_size == 4;
4203 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4204 if (!use_mubuf) {
4205 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4206 } else {
4207 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4208 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4209 fetch_size = 4;
4210 expanded = true;
4211 }
4212 }
4213
4214 Temp fetch_index = index;
4215 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4216 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4217 fetch_offset = fetch_offset % attrib_stride;
4218 }
4219
4220 Operand soffset(0u);
4221 if (fetch_offset >= 4096) {
4222 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4223 fetch_offset %= 4096;
4224 }
4225
4226 aco_opcode opcode;
4227 switch (fetch_size) {
4228 case 1:
4229 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4230 break;
4231 case 2:
4232 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4233 break;
4234 case 3:
4235 assert(ctx->options->chip_class >= GFX7 ||
4236 (!use_mubuf && ctx->options->chip_class == GFX6));
4237 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4238 break;
4239 case 4:
4240 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4241 break;
4242 default:
4243 unreachable("Unimplemented load_input vector size");
4244 }
4245
4246 Temp fetch_dst;
4247 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4248 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4249 num_channels <= 3)) {
4250 direct_fetch = true;
4251 fetch_dst = dst;
4252 } else {
4253 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4254 }
4255
4256 if (use_mubuf) {
4257 Instruction *mubuf = bld.mubuf(opcode,
4258 Definition(fetch_dst), list, fetch_index, soffset,
4259 fetch_offset, false, true).instr;
4260 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4261 } else {
4262 Instruction *mtbuf = bld.mtbuf(opcode,
4263 Definition(fetch_dst), list, fetch_index, soffset,
4264 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4265 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4266 }
4267
4268 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4269
4270 if (fetch_size == 1) {
4271 channels[channel_start] = fetch_dst;
4272 } else {
4273 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4274 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4275 }
4276
4277 channel_start += fetch_size;
4278 }
4279
4280 if (!direct_fetch) {
4281 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4282 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4283
4284 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4285 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4286 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4287
4288 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4289 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4290 unsigned num_temp = 0;
4291 for (unsigned i = 0; i < dst.size(); i++) {
4292 unsigned idx = i + component;
4293 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4294 Temp channel = channels[swizzle[idx]];
4295 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4296 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4297 vec->operands[i] = Operand(channel);
4298
4299 num_temp++;
4300 elems[i] = channel;
4301 } else if (is_float && idx == 3) {
4302 vec->operands[i] = Operand(0x3f800000u);
4303 } else if (!is_float && idx == 3) {
4304 vec->operands[i] = Operand(1u);
4305 } else {
4306 vec->operands[i] = Operand(0u);
4307 }
4308 }
4309 vec->definitions[0] = Definition(dst);
4310 ctx->block->instructions.emplace_back(std::move(vec));
4311 emit_split_vector(ctx, dst, dst.size());
4312
4313 if (num_temp == dst.size())
4314 ctx->allocated_vec.emplace(dst.id(), elems);
4315 }
4316 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4317 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4318 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4319 if (off_instr->type != nir_instr_type_load_const ||
4320 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4321 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4322 nir_print_instr(off_instr, stderr);
4323 fprintf(stderr, "\n");
4324 }
4325
4326 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4327 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4328 if (offset) {
4329 assert(offset->u32 == 0);
4330 } else {
4331 /* the lower 15bit of the prim_mask contain the offset into LDS
4332 * while the upper bits contain the number of prims */
4333 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4334 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4335 Builder bld(ctx->program, ctx->block);
4336 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4337 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4338 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4339 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4340 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4341 }
4342
4343 unsigned idx = nir_intrinsic_base(instr);
4344 unsigned component = nir_intrinsic_component(instr);
4345 unsigned vertex_id = 2; /* P0 */
4346
4347 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4348 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4349 switch (src0->u32) {
4350 case 0:
4351 vertex_id = 2; /* P0 */
4352 break;
4353 case 1:
4354 vertex_id = 0; /* P10 */
4355 break;
4356 case 2:
4357 vertex_id = 1; /* P20 */
4358 break;
4359 default:
4360 unreachable("invalid vertex index");
4361 }
4362 }
4363
4364 if (dst.size() == 1) {
4365 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4366 } else {
4367 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4368 for (unsigned i = 0; i < dst.size(); i++)
4369 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4370 vec->definitions[0] = Definition(dst);
4371 bld.insert(std::move(vec));
4372 }
4373
4374 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4375 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4376 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4377 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4378 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4379
4380 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4381 } else {
4382 unreachable("Shader stage not implemented");
4383 }
4384 }
4385
4386 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4387 {
4388 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4389
4390 Builder bld(ctx->program, ctx->block);
4391 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4392 Temp vertex_offset;
4393
4394 if (!nir_src_is_const(*vertex_src)) {
4395 /* better code could be created, but this case probably doesn't happen
4396 * much in practice */
4397 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4398 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4399 Temp elem;
4400
4401 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4402 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4403 if (i % 2u)
4404 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4405 } else {
4406 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4407 }
4408
4409 if (vertex_offset.id()) {
4410 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4411 Operand(i), indirect_vertex);
4412 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4413 } else {
4414 vertex_offset = elem;
4415 }
4416 }
4417
4418 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4419 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4420 } else {
4421 unsigned vertex = nir_src_as_uint(*vertex_src);
4422 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4423 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4424 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4425 Operand((vertex % 2u) * 16u), Operand(16u));
4426 else
4427 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4428 }
4429
4430 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4431 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4432 return offset_mul(ctx, offs, 4u);
4433 }
4434
4435 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4436 {
4437 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4438
4439 Builder bld(ctx->program, ctx->block);
4440 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4441 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4442
4443 if (ctx->stage == geometry_gs) {
4444 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4445 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4446 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);
4447 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4448 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4449 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4450 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4451 } else {
4452 unreachable("Unsupported GS stage.");
4453 }
4454 }
4455
4456 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4457 {
4458 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4459
4460 Builder bld(ctx->program, ctx->block);
4461 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4462
4463 if (load_input_from_temps(ctx, instr, dst))
4464 return;
4465
4466 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4467 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4468 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4469
4470 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4471 }
4472
4473 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4474 {
4475 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4476
4477 Builder bld(ctx->program, ctx->block);
4478
4479 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4480 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4481 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4482
4483 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4484 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4485
4486 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4487 }
4488
4489 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4490 {
4491 switch (ctx->shader->info.stage) {
4492 case MESA_SHADER_GEOMETRY:
4493 visit_load_gs_per_vertex_input(ctx, instr);
4494 break;
4495 case MESA_SHADER_TESS_CTRL:
4496 visit_load_tcs_per_vertex_input(ctx, instr);
4497 break;
4498 case MESA_SHADER_TESS_EVAL:
4499 visit_load_tes_per_vertex_input(ctx, instr);
4500 break;
4501 default:
4502 unreachable("Unimplemented shader stage");
4503 }
4504 }
4505
4506 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4507 {
4508 visit_load_tcs_output(ctx, instr, true);
4509 }
4510
4511 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4512 {
4513 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4514 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4515
4516 visit_store_tcs_output(ctx, instr, true);
4517 }
4518
4519 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
4520 {
4521 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4522
4523 Builder bld(ctx->program, ctx->block);
4524 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4525
4526 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
4527 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
4528 Operand tes_w(0u);
4529
4530 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
4531 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
4532 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
4533 tes_w = Operand(tmp);
4534 }
4535
4536 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
4537 emit_split_vector(ctx, tess_coord, 3);
4538 }
4539
4540 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
4541 {
4542 if (ctx->program->info->need_indirect_descriptor_sets) {
4543 Builder bld(ctx->program, ctx->block);
4544 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
4545 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
4546 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
4547 }
4548
4549 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
4550 }
4551
4552
4553 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
4554 {
4555 Builder bld(ctx->program, ctx->block);
4556 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
4557 if (!ctx->divergent_vals[instr->dest.ssa.index])
4558 index = bld.as_uniform(index);
4559 unsigned desc_set = nir_intrinsic_desc_set(instr);
4560 unsigned binding = nir_intrinsic_binding(instr);
4561
4562 Temp desc_ptr;
4563 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
4564 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
4565 unsigned offset = layout->binding[binding].offset;
4566 unsigned stride;
4567 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
4568 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
4569 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
4570 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
4571 offset = pipeline_layout->push_constant_size + 16 * idx;
4572 stride = 16;
4573 } else {
4574 desc_ptr = load_desc_ptr(ctx, desc_set);
4575 stride = layout->binding[binding].size;
4576 }
4577
4578 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
4579 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
4580 if (stride != 1) {
4581 if (nir_const_index) {
4582 const_index = const_index * stride;
4583 } else if (index.type() == RegType::vgpr) {
4584 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
4585 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
4586 } else {
4587 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
4588 }
4589 }
4590 if (offset) {
4591 if (nir_const_index) {
4592 const_index = const_index + offset;
4593 } else if (index.type() == RegType::vgpr) {
4594 index = bld.vadd32(bld.def(v1), Operand(offset), index);
4595 } else {
4596 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
4597 }
4598 }
4599
4600 if (nir_const_index && const_index == 0) {
4601 index = desc_ptr;
4602 } else if (index.type() == RegType::vgpr) {
4603 index = bld.vadd32(bld.def(v1),
4604 nir_const_index ? Operand(const_index) : Operand(index),
4605 Operand(desc_ptr));
4606 } else {
4607 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
4608 nir_const_index ? Operand(const_index) : Operand(index),
4609 Operand(desc_ptr));
4610 }
4611
4612 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
4613 }
4614
4615 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
4616 Temp dst, Temp rsrc, Temp offset, int byte_align,
4617 bool glc=false, bool readonly=true)
4618 {
4619 Builder bld(ctx->program, ctx->block);
4620 bool dlc = glc && ctx->options->chip_class >= GFX10;
4621 unsigned num_bytes = num_components * component_size;
4622
4623 aco_opcode op;
4624 if (dst.type() == RegType::vgpr || ((ctx->options->chip_class < GFX8 || component_size < 4) && !readonly)) {
4625 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4626 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4627 unsigned const_offset = 0;
4628
4629 /* for small bit sizes add buffer for unaligned loads */
4630 if (byte_align) {
4631 if (num_bytes > 2)
4632 num_bytes += byte_align == -1 ? 4 - component_size : byte_align;
4633 else
4634 byte_align = 0;
4635 }
4636
4637 Temp lower = Temp();
4638 if (num_bytes > 16) {
4639 assert(num_components == 3 || num_components == 4);
4640 op = aco_opcode::buffer_load_dwordx4;
4641 lower = bld.tmp(v4);
4642 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4643 mubuf->definitions[0] = Definition(lower);
4644 mubuf->operands[0] = Operand(rsrc);
4645 mubuf->operands[1] = vaddr;
4646 mubuf->operands[2] = soffset;
4647 mubuf->offen = (offset.type() == RegType::vgpr);
4648 mubuf->glc = glc;
4649 mubuf->dlc = dlc;
4650 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4651 mubuf->can_reorder = readonly;
4652 bld.insert(std::move(mubuf));
4653 emit_split_vector(ctx, lower, 2);
4654 num_bytes -= 16;
4655 const_offset = 16;
4656 } else if (num_bytes == 12 && ctx->options->chip_class == GFX6) {
4657 /* GFX6 doesn't support loading vec3, expand to vec4. */
4658 num_bytes = 16;
4659 }
4660
4661 switch (num_bytes) {
4662 case 1:
4663 op = aco_opcode::buffer_load_ubyte;
4664 break;
4665 case 2:
4666 op = aco_opcode::buffer_load_ushort;
4667 break;
4668 case 3:
4669 case 4:
4670 op = aco_opcode::buffer_load_dword;
4671 break;
4672 case 5:
4673 case 6:
4674 case 7:
4675 case 8:
4676 op = aco_opcode::buffer_load_dwordx2;
4677 break;
4678 case 10:
4679 case 12:
4680 assert(ctx->options->chip_class > GFX6);
4681 op = aco_opcode::buffer_load_dwordx3;
4682 break;
4683 case 16:
4684 op = aco_opcode::buffer_load_dwordx4;
4685 break;
4686 default:
4687 unreachable("Load SSBO not implemented for this size.");
4688 }
4689 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4690 mubuf->operands[0] = Operand(rsrc);
4691 mubuf->operands[1] = vaddr;
4692 mubuf->operands[2] = soffset;
4693 mubuf->offen = (offset.type() == RegType::vgpr);
4694 mubuf->glc = glc;
4695 mubuf->dlc = dlc;
4696 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4697 mubuf->can_reorder = readonly;
4698 mubuf->offset = const_offset;
4699 aco_ptr<Instruction> instr = std::move(mubuf);
4700
4701 if (component_size < 4) {
4702 Temp vec = num_bytes <= 4 ? bld.tmp(v1) : num_bytes <= 8 ? bld.tmp(v2) : bld.tmp(v3);
4703 instr->definitions[0] = Definition(vec);
4704 bld.insert(std::move(instr));
4705
4706 if (byte_align == -1 || (byte_align && dst.type() == RegType::sgpr)) {
4707 Operand align = byte_align == -1 ? Operand(offset) : Operand((uint32_t)byte_align);
4708 Temp tmp[3] = {vec, vec, vec};
4709
4710 if (vec.size() == 3) {
4711 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
4712 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
4713 } else if (vec.size() == 2) {
4714 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
4715 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
4716 }
4717 for (unsigned i = 0; i < dst.size(); i++)
4718 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], align);
4719
4720 vec = tmp[0];
4721 if (dst.size() == 2)
4722 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
4723
4724 byte_align = 0;
4725 }
4726
4727 if (dst.type() == RegType::vgpr && num_components == 1) {
4728 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), vec, Operand(byte_align / component_size));
4729 } else {
4730 trim_subdword_vector(ctx, vec, dst, 4 * vec.size() / component_size, ((1 << num_components) - 1) << byte_align / component_size);
4731 }
4732
4733 return;
4734
4735 } else if (dst.size() > 4) {
4736 assert(lower != Temp());
4737 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
4738 instr->definitions[0] = Definition(upper);
4739 bld.insert(std::move(instr));
4740 if (dst.size() == 8)
4741 emit_split_vector(ctx, upper, 2);
4742 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
4743 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
4744 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
4745 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
4746 if (dst.size() == 8)
4747 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
4748 } else if (dst.size() == 3 && ctx->options->chip_class == GFX6) {
4749 Temp vec = bld.tmp(v4);
4750 instr->definitions[0] = Definition(vec);
4751 bld.insert(std::move(instr));
4752 emit_split_vector(ctx, vec, 4);
4753
4754 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
4755 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
4756 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
4757 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
4758 }
4759
4760 if (dst.type() == RegType::sgpr) {
4761 Temp vec = bld.tmp(RegType::vgpr, dst.size());
4762 instr->definitions[0] = Definition(vec);
4763 bld.insert(std::move(instr));
4764 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
4765 } else {
4766 instr->definitions[0] = Definition(dst);
4767 bld.insert(std::move(instr));
4768 emit_split_vector(ctx, dst, num_components);
4769 }
4770 } else {
4771 /* for small bit sizes add buffer for unaligned loads */
4772 if (byte_align)
4773 num_bytes += byte_align == -1 ? 4 - component_size : byte_align;
4774
4775 switch (num_bytes) {
4776 case 1:
4777 case 2:
4778 case 3:
4779 case 4:
4780 op = aco_opcode::s_buffer_load_dword;
4781 break;
4782 case 5:
4783 case 6:
4784 case 7:
4785 case 8:
4786 op = aco_opcode::s_buffer_load_dwordx2;
4787 break;
4788 case 10:
4789 case 12:
4790 case 16:
4791 op = aco_opcode::s_buffer_load_dwordx4;
4792 break;
4793 case 24:
4794 case 32:
4795 op = aco_opcode::s_buffer_load_dwordx8;
4796 break;
4797 default:
4798 unreachable("Load SSBO not implemented for this size.");
4799 }
4800 offset = bld.as_uniform(offset);
4801 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
4802 load->operands[0] = Operand(rsrc);
4803 load->operands[1] = Operand(offset);
4804 assert(load->operands[1].getTemp().type() == RegType::sgpr);
4805 load->definitions[0] = Definition(dst);
4806 load->glc = glc;
4807 load->dlc = dlc;
4808 load->barrier = readonly ? barrier_none : barrier_buffer;
4809 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
4810 assert(ctx->options->chip_class >= GFX8 || !glc);
4811
4812 /* adjust misaligned small bit size loads */
4813 if (byte_align) {
4814 Temp vec = num_bytes <= 4 ? bld.tmp(s1) : num_bytes <= 8 ? bld.tmp(s2) : bld.tmp(s4);
4815 load->definitions[0] = Definition(vec);
4816 bld.insert(std::move(load));
4817 Operand byte_offset = byte_align > 0 ? Operand(uint32_t(byte_align)) : Operand(offset);
4818 byte_align_scalar(ctx, vec, byte_offset, dst);
4819
4820 /* trim vector */
4821 } else if (dst.size() == 3) {
4822 Temp vec = bld.tmp(s4);
4823 load->definitions[0] = Definition(vec);
4824 bld.insert(std::move(load));
4825 emit_split_vector(ctx, vec, 4);
4826
4827 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4828 emit_extract_vector(ctx, vec, 0, s1),
4829 emit_extract_vector(ctx, vec, 1, s1),
4830 emit_extract_vector(ctx, vec, 2, s1));
4831 } else if (dst.size() == 6) {
4832 Temp vec = bld.tmp(s8);
4833 load->definitions[0] = Definition(vec);
4834 bld.insert(std::move(load));
4835 emit_split_vector(ctx, vec, 4);
4836
4837 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4838 emit_extract_vector(ctx, vec, 0, s2),
4839 emit_extract_vector(ctx, vec, 1, s2),
4840 emit_extract_vector(ctx, vec, 2, s2));
4841 } else {
4842 bld.insert(std::move(load));
4843 }
4844 emit_split_vector(ctx, dst, num_components);
4845 }
4846 }
4847
4848 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
4849 {
4850 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4851 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
4852
4853 Builder bld(ctx->program, ctx->block);
4854
4855 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
4856 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
4857 unsigned binding = nir_intrinsic_binding(idx_instr);
4858 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
4859
4860 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
4861 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4862 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4863 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4864 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4865 if (ctx->options->chip_class >= GFX10) {
4866 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4867 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4868 S_008F0C_RESOURCE_LEVEL(1);
4869 } else {
4870 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4871 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4872 }
4873 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
4874 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
4875 Operand(0xFFFFFFFFu),
4876 Operand(desc_type));
4877 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4878 rsrc, upper_dwords);
4879 } else {
4880 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
4881 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4882 }
4883 unsigned size = instr->dest.ssa.bit_size / 8;
4884 int byte_align = 0;
4885 if (size < 4) {
4886 unsigned align_mul = nir_intrinsic_align_mul(instr);
4887 unsigned align_offset = nir_intrinsic_align_offset(instr);
4888 byte_align = align_mul % 4 == 0 ? align_offset : -1;
4889 }
4890 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), byte_align);
4891 }
4892
4893 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4894 {
4895 Builder bld(ctx->program, ctx->block);
4896 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4897 unsigned offset = nir_intrinsic_base(instr);
4898 unsigned count = instr->dest.ssa.num_components;
4899 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
4900
4901 if (index_cv && instr->dest.ssa.bit_size == 32) {
4902 unsigned start = (offset + index_cv->u32) / 4u;
4903 start -= ctx->args->ac.base_inline_push_consts;
4904 if (start + count <= ctx->args->ac.num_inline_push_consts) {
4905 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4906 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4907 for (unsigned i = 0; i < count; ++i) {
4908 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
4909 vec->operands[i] = Operand{elems[i]};
4910 }
4911 vec->definitions[0] = Definition(dst);
4912 ctx->block->instructions.emplace_back(std::move(vec));
4913 ctx->allocated_vec.emplace(dst.id(), elems);
4914 return;
4915 }
4916 }
4917
4918 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
4919 if (offset != 0) // TODO check if index != 0 as well
4920 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
4921 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
4922 Temp vec = dst;
4923 bool trim = false;
4924 bool aligned = true;
4925
4926 if (instr->dest.ssa.bit_size == 8) {
4927 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
4928 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
4929 if (!aligned)
4930 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
4931 } else if (instr->dest.ssa.bit_size == 16) {
4932 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
4933 if (!aligned)
4934 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
4935 }
4936
4937 aco_opcode op;
4938
4939 switch (vec.size()) {
4940 case 1:
4941 op = aco_opcode::s_load_dword;
4942 break;
4943 case 2:
4944 op = aco_opcode::s_load_dwordx2;
4945 break;
4946 case 3:
4947 vec = bld.tmp(s4);
4948 trim = true;
4949 case 4:
4950 op = aco_opcode::s_load_dwordx4;
4951 break;
4952 case 6:
4953 vec = bld.tmp(s8);
4954 trim = true;
4955 case 8:
4956 op = aco_opcode::s_load_dwordx8;
4957 break;
4958 default:
4959 unreachable("unimplemented or forbidden load_push_constant.");
4960 }
4961
4962 bld.smem(op, Definition(vec), ptr, index);
4963
4964 if (!aligned) {
4965 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
4966 byte_align_scalar(ctx, vec, byte_offset, dst);
4967 return;
4968 }
4969
4970 if (trim) {
4971 emit_split_vector(ctx, vec, 4);
4972 RegClass rc = dst.size() == 3 ? s1 : s2;
4973 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4974 emit_extract_vector(ctx, vec, 0, rc),
4975 emit_extract_vector(ctx, vec, 1, rc),
4976 emit_extract_vector(ctx, vec, 2, rc));
4977
4978 }
4979 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4980 }
4981
4982 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4983 {
4984 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4985
4986 Builder bld(ctx->program, ctx->block);
4987
4988 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4989 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4990 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4991 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4992 if (ctx->options->chip_class >= GFX10) {
4993 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4994 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4995 S_008F0C_RESOURCE_LEVEL(1);
4996 } else {
4997 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4998 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4999 }
5000
5001 unsigned base = nir_intrinsic_base(instr);
5002 unsigned range = nir_intrinsic_range(instr);
5003
5004 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5005 if (base && offset.type() == RegType::sgpr)
5006 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5007 else if (base && offset.type() == RegType::vgpr)
5008 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5009
5010 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5011 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5012 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5013 Operand(desc_type));
5014 unsigned size = instr->dest.ssa.bit_size / 8;
5015 // TODO: get alignment information for subdword constants
5016 unsigned byte_align = size < 4 ? -1 : 0;
5017 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, byte_align);
5018 }
5019
5020 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5021 {
5022 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5023 ctx->cf_info.exec_potentially_empty_discard = true;
5024
5025 ctx->program->needs_exact = true;
5026
5027 // TODO: optimize uniform conditions
5028 Builder bld(ctx->program, ctx->block);
5029 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5030 assert(src.regClass() == bld.lm);
5031 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5032 bld.pseudo(aco_opcode::p_discard_if, src);
5033 ctx->block->kind |= block_kind_uses_discard_if;
5034 return;
5035 }
5036
5037 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5038 {
5039 Builder bld(ctx->program, ctx->block);
5040
5041 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5042 ctx->cf_info.exec_potentially_empty_discard = true;
5043
5044 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5045 ctx->cf_info.parent_loop.has_divergent_continue;
5046
5047 if (ctx->block->loop_nest_depth &&
5048 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5049 /* we handle discards the same way as jump instructions */
5050 append_logical_end(ctx->block);
5051
5052 /* in loops, discard behaves like break */
5053 Block *linear_target = ctx->cf_info.parent_loop.exit;
5054 ctx->block->kind |= block_kind_discard;
5055
5056 if (!divergent) {
5057 /* uniform discard - loop ends here */
5058 assert(nir_instr_is_last(&instr->instr));
5059 ctx->block->kind |= block_kind_uniform;
5060 ctx->cf_info.has_branch = true;
5061 bld.branch(aco_opcode::p_branch);
5062 add_linear_edge(ctx->block->index, linear_target);
5063 return;
5064 }
5065
5066 /* we add a break right behind the discard() instructions */
5067 ctx->block->kind |= block_kind_break;
5068 unsigned idx = ctx->block->index;
5069
5070 ctx->cf_info.parent_loop.has_divergent_branch = true;
5071 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5072
5073 /* remove critical edges from linear CFG */
5074 bld.branch(aco_opcode::p_branch);
5075 Block* break_block = ctx->program->create_and_insert_block();
5076 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5077 break_block->kind |= block_kind_uniform;
5078 add_linear_edge(idx, break_block);
5079 add_linear_edge(break_block->index, linear_target);
5080 bld.reset(break_block);
5081 bld.branch(aco_opcode::p_branch);
5082
5083 Block* continue_block = ctx->program->create_and_insert_block();
5084 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5085 add_linear_edge(idx, continue_block);
5086 append_logical_start(continue_block);
5087 ctx->block = continue_block;
5088
5089 return;
5090 }
5091
5092 /* it can currently happen that NIR doesn't remove the unreachable code */
5093 if (!nir_instr_is_last(&instr->instr)) {
5094 ctx->program->needs_exact = true;
5095 /* save exec somewhere temporarily so that it doesn't get
5096 * overwritten before the discard from outer exec masks */
5097 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5098 bld.pseudo(aco_opcode::p_discard_if, cond);
5099 ctx->block->kind |= block_kind_uses_discard_if;
5100 return;
5101 }
5102
5103 /* This condition is incorrect for uniformly branched discards in a loop
5104 * predicated by a divergent condition, but the above code catches that case
5105 * and the discard would end up turning into a discard_if.
5106 * For example:
5107 * if (divergent) {
5108 * while (...) {
5109 * if (uniform) {
5110 * discard;
5111 * }
5112 * }
5113 * }
5114 */
5115 if (!ctx->cf_info.parent_if.is_divergent) {
5116 /* program just ends here */
5117 ctx->block->kind |= block_kind_uniform;
5118 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5119 0 /* enabled mask */, 9 /* dest */,
5120 false /* compressed */, true/* done */, true /* valid mask */);
5121 bld.sopp(aco_opcode::s_endpgm);
5122 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5123 } else {
5124 ctx->block->kind |= block_kind_discard;
5125 /* branch and linear edge is added by visit_if() */
5126 }
5127 }
5128
5129 enum aco_descriptor_type {
5130 ACO_DESC_IMAGE,
5131 ACO_DESC_FMASK,
5132 ACO_DESC_SAMPLER,
5133 ACO_DESC_BUFFER,
5134 ACO_DESC_PLANE_0,
5135 ACO_DESC_PLANE_1,
5136 ACO_DESC_PLANE_2,
5137 };
5138
5139 static bool
5140 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5141 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5142 return false;
5143 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5144 return dim == ac_image_cube ||
5145 dim == ac_image_1darray ||
5146 dim == ac_image_2darray ||
5147 dim == ac_image_2darraymsaa;
5148 }
5149
5150 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5151 enum aco_descriptor_type desc_type,
5152 const nir_tex_instr *tex_instr, bool image, bool write)
5153 {
5154 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5155 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5156 if (it != ctx->tex_desc.end())
5157 return it->second;
5158 */
5159 Temp index = Temp();
5160 bool index_set = false;
5161 unsigned constant_index = 0;
5162 unsigned descriptor_set;
5163 unsigned base_index;
5164 Builder bld(ctx->program, ctx->block);
5165
5166 if (!deref_instr) {
5167 assert(tex_instr && !image);
5168 descriptor_set = 0;
5169 base_index = tex_instr->sampler_index;
5170 } else {
5171 while(deref_instr->deref_type != nir_deref_type_var) {
5172 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5173 if (!array_size)
5174 array_size = 1;
5175
5176 assert(deref_instr->deref_type == nir_deref_type_array);
5177 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5178 if (const_value) {
5179 constant_index += array_size * const_value->u32;
5180 } else {
5181 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5182 if (indirect.type() == RegType::vgpr)
5183 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5184
5185 if (array_size != 1)
5186 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5187
5188 if (!index_set) {
5189 index = indirect;
5190 index_set = true;
5191 } else {
5192 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5193 }
5194 }
5195
5196 deref_instr = nir_src_as_deref(deref_instr->parent);
5197 }
5198 descriptor_set = deref_instr->var->data.descriptor_set;
5199 base_index = deref_instr->var->data.binding;
5200 }
5201
5202 Temp list = load_desc_ptr(ctx, descriptor_set);
5203 list = convert_pointer_to_64_bit(ctx, list);
5204
5205 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5206 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5207 unsigned offset = binding->offset;
5208 unsigned stride = binding->size;
5209 aco_opcode opcode;
5210 RegClass type;
5211
5212 assert(base_index < layout->binding_count);
5213
5214 switch (desc_type) {
5215 case ACO_DESC_IMAGE:
5216 type = s8;
5217 opcode = aco_opcode::s_load_dwordx8;
5218 break;
5219 case ACO_DESC_FMASK:
5220 type = s8;
5221 opcode = aco_opcode::s_load_dwordx8;
5222 offset += 32;
5223 break;
5224 case ACO_DESC_SAMPLER:
5225 type = s4;
5226 opcode = aco_opcode::s_load_dwordx4;
5227 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5228 offset += radv_combined_image_descriptor_sampler_offset(binding);
5229 break;
5230 case ACO_DESC_BUFFER:
5231 type = s4;
5232 opcode = aco_opcode::s_load_dwordx4;
5233 break;
5234 case ACO_DESC_PLANE_0:
5235 case ACO_DESC_PLANE_1:
5236 type = s8;
5237 opcode = aco_opcode::s_load_dwordx8;
5238 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5239 break;
5240 case ACO_DESC_PLANE_2:
5241 type = s4;
5242 opcode = aco_opcode::s_load_dwordx4;
5243 offset += 64;
5244 break;
5245 default:
5246 unreachable("invalid desc_type\n");
5247 }
5248
5249 offset += constant_index * stride;
5250
5251 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5252 (!index_set || binding->immutable_samplers_equal)) {
5253 if (binding->immutable_samplers_equal)
5254 constant_index = 0;
5255
5256 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5257 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5258 Operand(samplers[constant_index * 4 + 0]),
5259 Operand(samplers[constant_index * 4 + 1]),
5260 Operand(samplers[constant_index * 4 + 2]),
5261 Operand(samplers[constant_index * 4 + 3]));
5262 }
5263
5264 Operand off;
5265 if (!index_set) {
5266 off = bld.copy(bld.def(s1), Operand(offset));
5267 } else {
5268 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5269 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5270 }
5271
5272 Temp res = bld.smem(opcode, bld.def(type), list, off);
5273
5274 if (desc_type == ACO_DESC_PLANE_2) {
5275 Temp components[8];
5276 for (unsigned i = 0; i < 8; i++)
5277 components[i] = bld.tmp(s1);
5278 bld.pseudo(aco_opcode::p_split_vector,
5279 Definition(components[0]),
5280 Definition(components[1]),
5281 Definition(components[2]),
5282 Definition(components[3]),
5283 res);
5284
5285 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5286 bld.pseudo(aco_opcode::p_split_vector,
5287 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5288 Definition(components[4]),
5289 Definition(components[5]),
5290 Definition(components[6]),
5291 Definition(components[7]),
5292 desc2);
5293
5294 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5295 components[0], components[1], components[2], components[3],
5296 components[4], components[5], components[6], components[7]);
5297 }
5298
5299 return res;
5300 }
5301
5302 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5303 {
5304 switch (dim) {
5305 case GLSL_SAMPLER_DIM_BUF:
5306 return 1;
5307 case GLSL_SAMPLER_DIM_1D:
5308 return array ? 2 : 1;
5309 case GLSL_SAMPLER_DIM_2D:
5310 return array ? 3 : 2;
5311 case GLSL_SAMPLER_DIM_MS:
5312 return array ? 4 : 3;
5313 case GLSL_SAMPLER_DIM_3D:
5314 case GLSL_SAMPLER_DIM_CUBE:
5315 return 3;
5316 case GLSL_SAMPLER_DIM_RECT:
5317 case GLSL_SAMPLER_DIM_SUBPASS:
5318 return 2;
5319 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5320 return 3;
5321 default:
5322 break;
5323 }
5324 return 0;
5325 }
5326
5327
5328 /* Adjust the sample index according to FMASK.
5329 *
5330 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5331 * which is the identity mapping. Each nibble says which physical sample
5332 * should be fetched to get that sample.
5333 *
5334 * For example, 0x11111100 means there are only 2 samples stored and
5335 * the second sample covers 3/4 of the pixel. When reading samples 0
5336 * and 1, return physical sample 0 (determined by the first two 0s
5337 * in FMASK), otherwise return physical sample 1.
5338 *
5339 * The sample index should be adjusted as follows:
5340 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5341 */
5342 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5343 {
5344 Builder bld(ctx->program, ctx->block);
5345 Temp fmask = bld.tmp(v1);
5346 unsigned dim = ctx->options->chip_class >= GFX10
5347 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5348 : 0;
5349
5350 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5351 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5352 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5353 load->operands[0] = Operand(fmask_desc_ptr);
5354 load->operands[1] = Operand(s4); /* no sampler */
5355 load->operands[2] = Operand(coord);
5356 load->definitions[0] = Definition(fmask);
5357 load->glc = false;
5358 load->dlc = false;
5359 load->dmask = 0x1;
5360 load->unrm = true;
5361 load->da = da;
5362 load->dim = dim;
5363 load->can_reorder = true; /* fmask images shouldn't be modified */
5364 ctx->block->instructions.emplace_back(std::move(load));
5365
5366 Operand sample_index4;
5367 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
5368 sample_index4 = Operand(sample_index.constantValue() << 2);
5369 } else if (sample_index.regClass() == s1) {
5370 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5371 } else {
5372 assert(sample_index.regClass() == v1);
5373 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5374 }
5375
5376 Temp final_sample;
5377 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5378 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5379 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5380 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5381 else
5382 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5383
5384 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5385 * resource descriptor is 0 (invalid),
5386 */
5387 Temp compare = bld.tmp(bld.lm);
5388 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5389 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5390
5391 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5392
5393 /* Replace the MSAA sample index. */
5394 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5395 }
5396
5397 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5398 {
5399
5400 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5401 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5402 bool is_array = glsl_sampler_type_is_array(type);
5403 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5404 assert(!add_frag_pos && "Input attachments should be lowered.");
5405 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5406 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5407 int count = image_type_to_components_count(dim, is_array);
5408 std::vector<Temp> coords(count);
5409 Builder bld(ctx->program, ctx->block);
5410
5411 if (is_ms) {
5412 count--;
5413 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5414 /* get sample index */
5415 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5416 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5417 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5418 std::vector<Temp> fmask_load_address;
5419 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5420 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5421
5422 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5423 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5424 } else {
5425 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5426 }
5427 }
5428
5429 if (gfx9_1d) {
5430 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5431 coords.resize(coords.size() + 1);
5432 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5433 if (is_array)
5434 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5435 } else {
5436 for (int i = 0; i < count; i++)
5437 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5438 }
5439
5440 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5441 instr->intrinsic == nir_intrinsic_image_deref_store) {
5442 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5443 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5444
5445 if (!level_zero)
5446 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5447 }
5448
5449 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5450 for (unsigned i = 0; i < coords.size(); i++)
5451 vec->operands[i] = Operand(coords[i]);
5452 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5453 vec->definitions[0] = Definition(res);
5454 ctx->block->instructions.emplace_back(std::move(vec));
5455 return res;
5456 }
5457
5458
5459 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5460 {
5461 Builder bld(ctx->program, ctx->block);
5462 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5463 const struct glsl_type *type = glsl_without_array(var->type);
5464 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5465 bool is_array = glsl_sampler_type_is_array(type);
5466 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5467
5468 if (dim == GLSL_SAMPLER_DIM_BUF) {
5469 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5470 unsigned num_channels = util_last_bit(mask);
5471 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5472 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5473
5474 aco_opcode opcode;
5475 switch (num_channels) {
5476 case 1:
5477 opcode = aco_opcode::buffer_load_format_x;
5478 break;
5479 case 2:
5480 opcode = aco_opcode::buffer_load_format_xy;
5481 break;
5482 case 3:
5483 opcode = aco_opcode::buffer_load_format_xyz;
5484 break;
5485 case 4:
5486 opcode = aco_opcode::buffer_load_format_xyzw;
5487 break;
5488 default:
5489 unreachable(">4 channel buffer image load");
5490 }
5491 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5492 load->operands[0] = Operand(rsrc);
5493 load->operands[1] = Operand(vindex);
5494 load->operands[2] = Operand((uint32_t) 0);
5495 Temp tmp;
5496 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5497 tmp = dst;
5498 else
5499 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5500 load->definitions[0] = Definition(tmp);
5501 load->idxen = true;
5502 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5503 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5504 load->barrier = barrier_image;
5505 ctx->block->instructions.emplace_back(std::move(load));
5506
5507 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5508 return;
5509 }
5510
5511 Temp coords = get_image_coords(ctx, instr, type);
5512 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5513
5514 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5515 unsigned num_components = util_bitcount(dmask);
5516 Temp tmp;
5517 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5518 tmp = dst;
5519 else
5520 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5521
5522 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5523 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5524
5525 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5526 load->operands[0] = Operand(resource);
5527 load->operands[1] = Operand(s4); /* no sampler */
5528 load->operands[2] = Operand(coords);
5529 load->definitions[0] = Definition(tmp);
5530 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5531 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5532 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5533 load->dmask = dmask;
5534 load->unrm = true;
5535 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5536 load->barrier = barrier_image;
5537 ctx->block->instructions.emplace_back(std::move(load));
5538
5539 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5540 return;
5541 }
5542
5543 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5544 {
5545 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5546 const struct glsl_type *type = glsl_without_array(var->type);
5547 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5548 bool is_array = glsl_sampler_type_is_array(type);
5549 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5550
5551 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5552
5553 if (dim == GLSL_SAMPLER_DIM_BUF) {
5554 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5555 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5556 aco_opcode opcode;
5557 switch (data.size()) {
5558 case 1:
5559 opcode = aco_opcode::buffer_store_format_x;
5560 break;
5561 case 2:
5562 opcode = aco_opcode::buffer_store_format_xy;
5563 break;
5564 case 3:
5565 opcode = aco_opcode::buffer_store_format_xyz;
5566 break;
5567 case 4:
5568 opcode = aco_opcode::buffer_store_format_xyzw;
5569 break;
5570 default:
5571 unreachable(">4 channel buffer image store");
5572 }
5573 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5574 store->operands[0] = Operand(rsrc);
5575 store->operands[1] = Operand(vindex);
5576 store->operands[2] = Operand((uint32_t) 0);
5577 store->operands[3] = Operand(data);
5578 store->idxen = true;
5579 store->glc = glc;
5580 store->dlc = false;
5581 store->disable_wqm = true;
5582 store->barrier = barrier_image;
5583 ctx->program->needs_exact = true;
5584 ctx->block->instructions.emplace_back(std::move(store));
5585 return;
5586 }
5587
5588 assert(data.type() == RegType::vgpr);
5589 Temp coords = get_image_coords(ctx, instr, type);
5590 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5591
5592 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5593 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5594
5595 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5596 store->operands[0] = Operand(resource);
5597 store->operands[1] = Operand(data);
5598 store->operands[2] = Operand(coords);
5599 store->glc = glc;
5600 store->dlc = false;
5601 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5602 store->dmask = (1 << data.size()) - 1;
5603 store->unrm = true;
5604 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5605 store->disable_wqm = true;
5606 store->barrier = barrier_image;
5607 ctx->program->needs_exact = true;
5608 ctx->block->instructions.emplace_back(std::move(store));
5609 return;
5610 }
5611
5612 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5613 {
5614 /* return the previous value if dest is ever used */
5615 bool return_previous = false;
5616 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5617 return_previous = true;
5618 break;
5619 }
5620 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5621 return_previous = true;
5622 break;
5623 }
5624
5625 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5626 const struct glsl_type *type = glsl_without_array(var->type);
5627 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5628 bool is_array = glsl_sampler_type_is_array(type);
5629 Builder bld(ctx->program, ctx->block);
5630
5631 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5632 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5633
5634 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5635 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5636
5637 aco_opcode buf_op, image_op;
5638 switch (instr->intrinsic) {
5639 case nir_intrinsic_image_deref_atomic_add:
5640 buf_op = aco_opcode::buffer_atomic_add;
5641 image_op = aco_opcode::image_atomic_add;
5642 break;
5643 case nir_intrinsic_image_deref_atomic_umin:
5644 buf_op = aco_opcode::buffer_atomic_umin;
5645 image_op = aco_opcode::image_atomic_umin;
5646 break;
5647 case nir_intrinsic_image_deref_atomic_imin:
5648 buf_op = aco_opcode::buffer_atomic_smin;
5649 image_op = aco_opcode::image_atomic_smin;
5650 break;
5651 case nir_intrinsic_image_deref_atomic_umax:
5652 buf_op = aco_opcode::buffer_atomic_umax;
5653 image_op = aco_opcode::image_atomic_umax;
5654 break;
5655 case nir_intrinsic_image_deref_atomic_imax:
5656 buf_op = aco_opcode::buffer_atomic_smax;
5657 image_op = aco_opcode::image_atomic_smax;
5658 break;
5659 case nir_intrinsic_image_deref_atomic_and:
5660 buf_op = aco_opcode::buffer_atomic_and;
5661 image_op = aco_opcode::image_atomic_and;
5662 break;
5663 case nir_intrinsic_image_deref_atomic_or:
5664 buf_op = aco_opcode::buffer_atomic_or;
5665 image_op = aco_opcode::image_atomic_or;
5666 break;
5667 case nir_intrinsic_image_deref_atomic_xor:
5668 buf_op = aco_opcode::buffer_atomic_xor;
5669 image_op = aco_opcode::image_atomic_xor;
5670 break;
5671 case nir_intrinsic_image_deref_atomic_exchange:
5672 buf_op = aco_opcode::buffer_atomic_swap;
5673 image_op = aco_opcode::image_atomic_swap;
5674 break;
5675 case nir_intrinsic_image_deref_atomic_comp_swap:
5676 buf_op = aco_opcode::buffer_atomic_cmpswap;
5677 image_op = aco_opcode::image_atomic_cmpswap;
5678 break;
5679 default:
5680 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5681 }
5682
5683 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5684
5685 if (dim == GLSL_SAMPLER_DIM_BUF) {
5686 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5687 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5688 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5689 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5690 mubuf->operands[0] = Operand(resource);
5691 mubuf->operands[1] = Operand(vindex);
5692 mubuf->operands[2] = Operand((uint32_t)0);
5693 mubuf->operands[3] = Operand(data);
5694 if (return_previous)
5695 mubuf->definitions[0] = Definition(dst);
5696 mubuf->offset = 0;
5697 mubuf->idxen = true;
5698 mubuf->glc = return_previous;
5699 mubuf->dlc = false; /* Not needed for atomics */
5700 mubuf->disable_wqm = true;
5701 mubuf->barrier = barrier_image;
5702 ctx->program->needs_exact = true;
5703 ctx->block->instructions.emplace_back(std::move(mubuf));
5704 return;
5705 }
5706
5707 Temp coords = get_image_coords(ctx, instr, type);
5708 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5709 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5710 mimg->operands[0] = Operand(resource);
5711 mimg->operands[1] = Operand(data);
5712 mimg->operands[2] = Operand(coords);
5713 if (return_previous)
5714 mimg->definitions[0] = Definition(dst);
5715 mimg->glc = return_previous;
5716 mimg->dlc = false; /* Not needed for atomics */
5717 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5718 mimg->dmask = (1 << data.size()) - 1;
5719 mimg->unrm = true;
5720 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5721 mimg->disable_wqm = true;
5722 mimg->barrier = barrier_image;
5723 ctx->program->needs_exact = true;
5724 ctx->block->instructions.emplace_back(std::move(mimg));
5725 return;
5726 }
5727
5728 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
5729 {
5730 if (in_elements && ctx->options->chip_class == GFX8) {
5731 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5732 Builder bld(ctx->program, ctx->block);
5733
5734 Temp size = emit_extract_vector(ctx, desc, 2, s1);
5735
5736 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
5737 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
5738
5739 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
5740 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
5741
5742 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
5743 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
5744
5745 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
5746 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
5747 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
5748 if (dst.type() == RegType::vgpr)
5749 bld.copy(Definition(dst), shr_dst);
5750
5751 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5752 } else {
5753 emit_extract_vector(ctx, desc, 2, dst);
5754 }
5755 }
5756
5757 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
5758 {
5759 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5760 const struct glsl_type *type = glsl_without_array(var->type);
5761 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5762 bool is_array = glsl_sampler_type_is_array(type);
5763 Builder bld(ctx->program, ctx->block);
5764
5765 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
5766 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
5767 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
5768 }
5769
5770 /* LOD */
5771 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
5772
5773 /* Resource */
5774 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
5775
5776 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5777
5778 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
5779 mimg->operands[0] = Operand(resource);
5780 mimg->operands[1] = Operand(s4); /* no sampler */
5781 mimg->operands[2] = Operand(lod);
5782 uint8_t& dmask = mimg->dmask;
5783 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5784 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
5785 mimg->da = glsl_sampler_type_is_array(type);
5786 mimg->can_reorder = true;
5787 Definition& def = mimg->definitions[0];
5788 ctx->block->instructions.emplace_back(std::move(mimg));
5789
5790 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
5791 glsl_sampler_type_is_array(type)) {
5792
5793 assert(instr->dest.ssa.num_components == 3);
5794 Temp tmp = {ctx->program->allocateId(), v3};
5795 def = Definition(tmp);
5796 emit_split_vector(ctx, tmp, 3);
5797
5798 /* divide 3rd value by 6 by multiplying with magic number */
5799 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
5800 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
5801
5802 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5803 emit_extract_vector(ctx, tmp, 0, v1),
5804 emit_extract_vector(ctx, tmp, 1, v1),
5805 by_6);
5806
5807 } else if (ctx->options->chip_class == GFX9 &&
5808 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
5809 glsl_sampler_type_is_array(type)) {
5810 assert(instr->dest.ssa.num_components == 2);
5811 def = Definition(dst);
5812 dmask = 0x5;
5813 } else {
5814 def = Definition(dst);
5815 }
5816
5817 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5818 }
5819
5820 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5821 {
5822 Builder bld(ctx->program, ctx->block);
5823 unsigned num_components = instr->num_components;
5824
5825 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5826 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5827 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5828
5829 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
5830 unsigned size = instr->dest.ssa.bit_size / 8;
5831 int byte_align = 0;
5832 if (size < 4) {
5833 unsigned align_mul = nir_intrinsic_align_mul(instr);
5834 unsigned align_offset = nir_intrinsic_align_offset(instr);
5835 byte_align = align_mul % 4 == 0 ? align_offset : -1;
5836 }
5837 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), byte_align, glc, false);
5838 }
5839
5840 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5841 {
5842 Builder bld(ctx->program, ctx->block);
5843 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
5844 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5845 unsigned writemask = nir_intrinsic_write_mask(instr);
5846 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
5847
5848 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5849 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5850
5851 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
5852 ctx->options->chip_class >= GFX8 &&
5853 elem_size_bytes >= 4;
5854 if (smem)
5855 offset = bld.as_uniform(offset);
5856 bool smem_nonfs = smem && ctx->stage != fragment_fs;
5857
5858 while (writemask) {
5859 int start, count;
5860 u_bit_scan_consecutive_range(&writemask, &start, &count);
5861 if (count == 3 && (smem || ctx->options->chip_class == GFX6)) {
5862 /* GFX6 doesn't support storing vec3, split it. */
5863 writemask |= 1u << (start + 2);
5864 count = 2;
5865 }
5866 int num_bytes = count * elem_size_bytes;
5867
5868 /* dword or larger stores have to be dword-aligned */
5869 if (elem_size_bytes < 4 && num_bytes > 2) {
5870 // TODO: improve alignment check of sub-dword stores
5871 unsigned count_new = 2 / elem_size_bytes;
5872 writemask |= ((1 << (count - count_new)) - 1) << (start + count_new);
5873 count = count_new;
5874 num_bytes = 2;
5875 }
5876
5877 if (num_bytes > 16) {
5878 assert(elem_size_bytes == 8);
5879 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5880 count = 2;
5881 num_bytes = 16;
5882 }
5883
5884 Temp write_data;
5885 if (elem_size_bytes < 4) {
5886 if (data.type() == RegType::sgpr) {
5887 data = as_vgpr(ctx, data);
5888 emit_split_vector(ctx, data, 4 * data.size() / elem_size_bytes);
5889 }
5890 RegClass rc = RegClass(RegType::vgpr, elem_size_bytes).as_subdword();
5891 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5892 for (int i = 0; i < count; i++)
5893 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, rc));
5894 write_data = bld.tmp(RegClass(RegType::vgpr, num_bytes).as_subdword());
5895 vec->definitions[0] = Definition(write_data);
5896 bld.insert(std::move(vec));
5897 } else if (count != instr->num_components) {
5898 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5899 for (int i = 0; i < count; i++) {
5900 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
5901 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
5902 }
5903 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
5904 vec->definitions[0] = Definition(write_data);
5905 ctx->block->instructions.emplace_back(std::move(vec));
5906 } else if (!smem && data.type() != RegType::vgpr) {
5907 assert(num_bytes % 4 == 0);
5908 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
5909 } else if (smem_nonfs && data.type() == RegType::vgpr) {
5910 assert(num_bytes % 4 == 0);
5911 write_data = bld.as_uniform(data);
5912 } else {
5913 write_data = data;
5914 }
5915
5916 aco_opcode vmem_op, smem_op = aco_opcode::last_opcode;
5917 switch (num_bytes) {
5918 case 1:
5919 vmem_op = aco_opcode::buffer_store_byte;
5920 break;
5921 case 2:
5922 vmem_op = aco_opcode::buffer_store_short;
5923 break;
5924 case 4:
5925 vmem_op = aco_opcode::buffer_store_dword;
5926 smem_op = aco_opcode::s_buffer_store_dword;
5927 break;
5928 case 8:
5929 vmem_op = aco_opcode::buffer_store_dwordx2;
5930 smem_op = aco_opcode::s_buffer_store_dwordx2;
5931 break;
5932 case 12:
5933 vmem_op = aco_opcode::buffer_store_dwordx3;
5934 assert(!smem && ctx->options->chip_class > GFX6);
5935 break;
5936 case 16:
5937 vmem_op = aco_opcode::buffer_store_dwordx4;
5938 smem_op = aco_opcode::s_buffer_store_dwordx4;
5939 break;
5940 default:
5941 unreachable("Store SSBO not implemented for this size.");
5942 }
5943 if (ctx->stage == fragment_fs)
5944 smem_op = aco_opcode::p_fs_buffer_store_smem;
5945
5946 if (smem) {
5947 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
5948 store->operands[0] = Operand(rsrc);
5949 if (start) {
5950 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5951 offset, Operand(start * elem_size_bytes));
5952 store->operands[1] = Operand(off);
5953 } else {
5954 store->operands[1] = Operand(offset);
5955 }
5956 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
5957 store->operands[1].setFixed(m0);
5958 store->operands[2] = Operand(write_data);
5959 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5960 store->dlc = false;
5961 store->disable_wqm = true;
5962 store->barrier = barrier_buffer;
5963 ctx->block->instructions.emplace_back(std::move(store));
5964 ctx->program->wb_smem_l1_on_end = true;
5965 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
5966 ctx->block->kind |= block_kind_needs_lowering;
5967 ctx->program->needs_exact = true;
5968 }
5969 } else {
5970 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
5971 store->operands[0] = Operand(rsrc);
5972 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5973 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5974 store->operands[3] = Operand(write_data);
5975 store->offset = start * elem_size_bytes;
5976 store->offen = (offset.type() == RegType::vgpr);
5977 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5978 store->dlc = false;
5979 store->disable_wqm = true;
5980 store->barrier = barrier_buffer;
5981 ctx->program->needs_exact = true;
5982 ctx->block->instructions.emplace_back(std::move(store));
5983 }
5984 }
5985 }
5986
5987 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5988 {
5989 /* return the previous value if dest is ever used */
5990 bool return_previous = false;
5991 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5992 return_previous = true;
5993 break;
5994 }
5995 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5996 return_previous = true;
5997 break;
5998 }
5999
6000 Builder bld(ctx->program, ctx->block);
6001 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6002
6003 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6004 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6005 get_ssa_temp(ctx, instr->src[3].ssa), data);
6006
6007 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6008 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6009 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6010
6011 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6012
6013 aco_opcode op32, op64;
6014 switch (instr->intrinsic) {
6015 case nir_intrinsic_ssbo_atomic_add:
6016 op32 = aco_opcode::buffer_atomic_add;
6017 op64 = aco_opcode::buffer_atomic_add_x2;
6018 break;
6019 case nir_intrinsic_ssbo_atomic_imin:
6020 op32 = aco_opcode::buffer_atomic_smin;
6021 op64 = aco_opcode::buffer_atomic_smin_x2;
6022 break;
6023 case nir_intrinsic_ssbo_atomic_umin:
6024 op32 = aco_opcode::buffer_atomic_umin;
6025 op64 = aco_opcode::buffer_atomic_umin_x2;
6026 break;
6027 case nir_intrinsic_ssbo_atomic_imax:
6028 op32 = aco_opcode::buffer_atomic_smax;
6029 op64 = aco_opcode::buffer_atomic_smax_x2;
6030 break;
6031 case nir_intrinsic_ssbo_atomic_umax:
6032 op32 = aco_opcode::buffer_atomic_umax;
6033 op64 = aco_opcode::buffer_atomic_umax_x2;
6034 break;
6035 case nir_intrinsic_ssbo_atomic_and:
6036 op32 = aco_opcode::buffer_atomic_and;
6037 op64 = aco_opcode::buffer_atomic_and_x2;
6038 break;
6039 case nir_intrinsic_ssbo_atomic_or:
6040 op32 = aco_opcode::buffer_atomic_or;
6041 op64 = aco_opcode::buffer_atomic_or_x2;
6042 break;
6043 case nir_intrinsic_ssbo_atomic_xor:
6044 op32 = aco_opcode::buffer_atomic_xor;
6045 op64 = aco_opcode::buffer_atomic_xor_x2;
6046 break;
6047 case nir_intrinsic_ssbo_atomic_exchange:
6048 op32 = aco_opcode::buffer_atomic_swap;
6049 op64 = aco_opcode::buffer_atomic_swap_x2;
6050 break;
6051 case nir_intrinsic_ssbo_atomic_comp_swap:
6052 op32 = aco_opcode::buffer_atomic_cmpswap;
6053 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6054 break;
6055 default:
6056 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6057 }
6058 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6059 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6060 mubuf->operands[0] = Operand(rsrc);
6061 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6062 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6063 mubuf->operands[3] = Operand(data);
6064 if (return_previous)
6065 mubuf->definitions[0] = Definition(dst);
6066 mubuf->offset = 0;
6067 mubuf->offen = (offset.type() == RegType::vgpr);
6068 mubuf->glc = return_previous;
6069 mubuf->dlc = false; /* Not needed for atomics */
6070 mubuf->disable_wqm = true;
6071 mubuf->barrier = barrier_buffer;
6072 ctx->program->needs_exact = true;
6073 ctx->block->instructions.emplace_back(std::move(mubuf));
6074 }
6075
6076 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6077
6078 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6079 Builder bld(ctx->program, ctx->block);
6080 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6081 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6082 }
6083
6084 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
6085 {
6086 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6087 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6088
6089 if (addr.type() == RegType::vgpr)
6090 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
6091 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
6092 }
6093
6094 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6095 {
6096 Builder bld(ctx->program, ctx->block);
6097 unsigned num_components = instr->num_components;
6098 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
6099
6100 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6101 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6102
6103 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6104 bool dlc = glc && ctx->options->chip_class >= GFX10;
6105 aco_opcode op;
6106 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8)) {
6107 bool global = ctx->options->chip_class >= GFX9;
6108
6109 if (ctx->options->chip_class >= GFX7) {
6110 aco_opcode op;
6111 switch (num_bytes) {
6112 case 4:
6113 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
6114 break;
6115 case 8:
6116 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
6117 break;
6118 case 12:
6119 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
6120 break;
6121 case 16:
6122 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
6123 break;
6124 default:
6125 unreachable("load_global not implemented for this size.");
6126 }
6127
6128 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
6129 flat->operands[0] = Operand(addr);
6130 flat->operands[1] = Operand(s1);
6131 flat->glc = glc;
6132 flat->dlc = dlc;
6133 flat->barrier = barrier_buffer;
6134
6135 if (dst.type() == RegType::sgpr) {
6136 Temp vec = bld.tmp(RegType::vgpr, dst.size());
6137 flat->definitions[0] = Definition(vec);
6138 ctx->block->instructions.emplace_back(std::move(flat));
6139 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
6140 } else {
6141 flat->definitions[0] = Definition(dst);
6142 ctx->block->instructions.emplace_back(std::move(flat));
6143 }
6144 emit_split_vector(ctx, dst, num_components);
6145 } else {
6146 assert(ctx->options->chip_class == GFX6);
6147
6148 /* GFX6 doesn't support loading vec3, expand to vec4. */
6149 num_bytes = num_bytes == 12 ? 16 : num_bytes;
6150
6151 aco_opcode op;
6152 switch (num_bytes) {
6153 case 4:
6154 op = aco_opcode::buffer_load_dword;
6155 break;
6156 case 8:
6157 op = aco_opcode::buffer_load_dwordx2;
6158 break;
6159 case 16:
6160 op = aco_opcode::buffer_load_dwordx4;
6161 break;
6162 default:
6163 unreachable("load_global not implemented for this size.");
6164 }
6165
6166 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6167
6168 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
6169 mubuf->operands[0] = Operand(rsrc);
6170 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6171 mubuf->operands[2] = Operand(0u);
6172 mubuf->glc = glc;
6173 mubuf->dlc = false;
6174 mubuf->offset = 0;
6175 mubuf->addr64 = addr.type() == RegType::vgpr;
6176 mubuf->disable_wqm = false;
6177 mubuf->barrier = barrier_buffer;
6178 aco_ptr<Instruction> instr = std::move(mubuf);
6179
6180 /* expand vector */
6181 if (dst.size() == 3) {
6182 Temp vec = bld.tmp(v4);
6183 instr->definitions[0] = Definition(vec);
6184 bld.insert(std::move(instr));
6185 emit_split_vector(ctx, vec, 4);
6186
6187 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
6188 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
6189 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
6190 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
6191 }
6192
6193 if (dst.type() == RegType::sgpr) {
6194 Temp vec = bld.tmp(RegType::vgpr, dst.size());
6195 instr->definitions[0] = Definition(vec);
6196 bld.insert(std::move(instr));
6197 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
6198 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
6199 } else {
6200 instr->definitions[0] = Definition(dst);
6201 bld.insert(std::move(instr));
6202 emit_split_vector(ctx, dst, num_components);
6203 }
6204 }
6205 } else {
6206 switch (num_bytes) {
6207 case 4:
6208 op = aco_opcode::s_load_dword;
6209 break;
6210 case 8:
6211 op = aco_opcode::s_load_dwordx2;
6212 break;
6213 case 12:
6214 case 16:
6215 op = aco_opcode::s_load_dwordx4;
6216 break;
6217 default:
6218 unreachable("load_global not implemented for this size.");
6219 }
6220 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
6221 load->operands[0] = Operand(addr);
6222 load->operands[1] = Operand(0u);
6223 load->definitions[0] = Definition(dst);
6224 load->glc = glc;
6225 load->dlc = dlc;
6226 load->barrier = barrier_buffer;
6227 assert(ctx->options->chip_class >= GFX8 || !glc);
6228
6229 if (dst.size() == 3) {
6230 /* trim vector */
6231 Temp vec = bld.tmp(s4);
6232 load->definitions[0] = Definition(vec);
6233 ctx->block->instructions.emplace_back(std::move(load));
6234 emit_split_vector(ctx, vec, 4);
6235
6236 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6237 emit_extract_vector(ctx, vec, 0, s1),
6238 emit_extract_vector(ctx, vec, 1, s1),
6239 emit_extract_vector(ctx, vec, 2, s1));
6240 } else {
6241 ctx->block->instructions.emplace_back(std::move(load));
6242 }
6243 }
6244 }
6245
6246 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6247 {
6248 Builder bld(ctx->program, ctx->block);
6249 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6250
6251 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6252 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6253
6254 if (ctx->options->chip_class >= GFX7)
6255 addr = as_vgpr(ctx, addr);
6256
6257 unsigned writemask = nir_intrinsic_write_mask(instr);
6258 while (writemask) {
6259 int start, count;
6260 u_bit_scan_consecutive_range(&writemask, &start, &count);
6261 if (count == 3 && ctx->options->chip_class == GFX6) {
6262 /* GFX6 doesn't support storing vec3, split it. */
6263 writemask |= 1u << (start + 2);
6264 count = 2;
6265 }
6266 unsigned num_bytes = count * elem_size_bytes;
6267
6268 Temp write_data = data;
6269 if (count != instr->num_components) {
6270 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6271 for (int i = 0; i < count; i++)
6272 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
6273 write_data = bld.tmp(RegType::vgpr, count);
6274 vec->definitions[0] = Definition(write_data);
6275 ctx->block->instructions.emplace_back(std::move(vec));
6276 }
6277
6278 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6279 unsigned offset = start * elem_size_bytes;
6280
6281 if (ctx->options->chip_class >= GFX7) {
6282 if (offset > 0 && ctx->options->chip_class < GFX9) {
6283 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6284 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6285 Temp carry = bld.tmp(bld.lm);
6286 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6287
6288 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6289 Operand(offset), addr0);
6290 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6291 Operand(0u), addr1,
6292 carry).def(1).setHint(vcc);
6293
6294 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6295
6296 offset = 0;
6297 }
6298
6299 bool global = ctx->options->chip_class >= GFX9;
6300 aco_opcode op;
6301 switch (num_bytes) {
6302 case 4:
6303 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6304 break;
6305 case 8:
6306 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6307 break;
6308 case 12:
6309 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6310 break;
6311 case 16:
6312 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6313 break;
6314 default:
6315 unreachable("store_global not implemented for this size.");
6316 }
6317
6318 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6319 flat->operands[0] = Operand(addr);
6320 flat->operands[1] = Operand(s1);
6321 flat->operands[2] = Operand(data);
6322 flat->glc = glc;
6323 flat->dlc = false;
6324 flat->offset = offset;
6325 flat->disable_wqm = true;
6326 flat->barrier = barrier_buffer;
6327 ctx->program->needs_exact = true;
6328 ctx->block->instructions.emplace_back(std::move(flat));
6329 } else {
6330 assert(ctx->options->chip_class == GFX6);
6331
6332 aco_opcode op;
6333 switch (num_bytes) {
6334 case 4:
6335 op = aco_opcode::buffer_store_dword;
6336 break;
6337 case 8:
6338 op = aco_opcode::buffer_store_dwordx2;
6339 break;
6340 case 16:
6341 op = aco_opcode::buffer_store_dwordx4;
6342 break;
6343 default:
6344 unreachable("store_global not implemented for this size.");
6345 }
6346
6347 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6348
6349 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6350 mubuf->operands[0] = Operand(rsrc);
6351 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6352 mubuf->operands[2] = Operand(0u);
6353 mubuf->operands[3] = Operand(write_data);
6354 mubuf->glc = glc;
6355 mubuf->dlc = false;
6356 mubuf->offset = offset;
6357 mubuf->addr64 = addr.type() == RegType::vgpr;
6358 mubuf->disable_wqm = true;
6359 mubuf->barrier = barrier_buffer;
6360 ctx->program->needs_exact = true;
6361 ctx->block->instructions.emplace_back(std::move(mubuf));
6362 }
6363 }
6364 }
6365
6366 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6367 {
6368 /* return the previous value if dest is ever used */
6369 bool return_previous = false;
6370 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6371 return_previous = true;
6372 break;
6373 }
6374 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6375 return_previous = true;
6376 break;
6377 }
6378
6379 Builder bld(ctx->program, ctx->block);
6380 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6381 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6382
6383 if (ctx->options->chip_class >= GFX7)
6384 addr = as_vgpr(ctx, addr);
6385
6386 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6387 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6388 get_ssa_temp(ctx, instr->src[2].ssa), data);
6389
6390 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6391
6392 aco_opcode op32, op64;
6393
6394 if (ctx->options->chip_class >= GFX7) {
6395 bool global = ctx->options->chip_class >= GFX9;
6396 switch (instr->intrinsic) {
6397 case nir_intrinsic_global_atomic_add:
6398 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6399 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6400 break;
6401 case nir_intrinsic_global_atomic_imin:
6402 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6403 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6404 break;
6405 case nir_intrinsic_global_atomic_umin:
6406 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6407 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6408 break;
6409 case nir_intrinsic_global_atomic_imax:
6410 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6411 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6412 break;
6413 case nir_intrinsic_global_atomic_umax:
6414 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6415 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6416 break;
6417 case nir_intrinsic_global_atomic_and:
6418 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6419 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6420 break;
6421 case nir_intrinsic_global_atomic_or:
6422 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6423 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6424 break;
6425 case nir_intrinsic_global_atomic_xor:
6426 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6427 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6428 break;
6429 case nir_intrinsic_global_atomic_exchange:
6430 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6431 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6432 break;
6433 case nir_intrinsic_global_atomic_comp_swap:
6434 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6435 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6436 break;
6437 default:
6438 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6439 }
6440
6441 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6442 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6443 flat->operands[0] = Operand(addr);
6444 flat->operands[1] = Operand(s1);
6445 flat->operands[2] = Operand(data);
6446 if (return_previous)
6447 flat->definitions[0] = Definition(dst);
6448 flat->glc = return_previous;
6449 flat->dlc = false; /* Not needed for atomics */
6450 flat->offset = 0;
6451 flat->disable_wqm = true;
6452 flat->barrier = barrier_buffer;
6453 ctx->program->needs_exact = true;
6454 ctx->block->instructions.emplace_back(std::move(flat));
6455 } else {
6456 assert(ctx->options->chip_class == GFX6);
6457
6458 switch (instr->intrinsic) {
6459 case nir_intrinsic_global_atomic_add:
6460 op32 = aco_opcode::buffer_atomic_add;
6461 op64 = aco_opcode::buffer_atomic_add_x2;
6462 break;
6463 case nir_intrinsic_global_atomic_imin:
6464 op32 = aco_opcode::buffer_atomic_smin;
6465 op64 = aco_opcode::buffer_atomic_smin_x2;
6466 break;
6467 case nir_intrinsic_global_atomic_umin:
6468 op32 = aco_opcode::buffer_atomic_umin;
6469 op64 = aco_opcode::buffer_atomic_umin_x2;
6470 break;
6471 case nir_intrinsic_global_atomic_imax:
6472 op32 = aco_opcode::buffer_atomic_smax;
6473 op64 = aco_opcode::buffer_atomic_smax_x2;
6474 break;
6475 case nir_intrinsic_global_atomic_umax:
6476 op32 = aco_opcode::buffer_atomic_umax;
6477 op64 = aco_opcode::buffer_atomic_umax_x2;
6478 break;
6479 case nir_intrinsic_global_atomic_and:
6480 op32 = aco_opcode::buffer_atomic_and;
6481 op64 = aco_opcode::buffer_atomic_and_x2;
6482 break;
6483 case nir_intrinsic_global_atomic_or:
6484 op32 = aco_opcode::buffer_atomic_or;
6485 op64 = aco_opcode::buffer_atomic_or_x2;
6486 break;
6487 case nir_intrinsic_global_atomic_xor:
6488 op32 = aco_opcode::buffer_atomic_xor;
6489 op64 = aco_opcode::buffer_atomic_xor_x2;
6490 break;
6491 case nir_intrinsic_global_atomic_exchange:
6492 op32 = aco_opcode::buffer_atomic_swap;
6493 op64 = aco_opcode::buffer_atomic_swap_x2;
6494 break;
6495 case nir_intrinsic_global_atomic_comp_swap:
6496 op32 = aco_opcode::buffer_atomic_cmpswap;
6497 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6498 break;
6499 default:
6500 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6501 }
6502
6503 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6504
6505 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6506
6507 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6508 mubuf->operands[0] = Operand(rsrc);
6509 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6510 mubuf->operands[2] = Operand(0u);
6511 mubuf->operands[3] = Operand(data);
6512 if (return_previous)
6513 mubuf->definitions[0] = Definition(dst);
6514 mubuf->glc = return_previous;
6515 mubuf->dlc = false;
6516 mubuf->offset = 0;
6517 mubuf->addr64 = addr.type() == RegType::vgpr;
6518 mubuf->disable_wqm = true;
6519 mubuf->barrier = barrier_buffer;
6520 ctx->program->needs_exact = true;
6521 ctx->block->instructions.emplace_back(std::move(mubuf));
6522 }
6523 }
6524
6525 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6526 Builder bld(ctx->program, ctx->block);
6527 switch(instr->intrinsic) {
6528 case nir_intrinsic_group_memory_barrier:
6529 case nir_intrinsic_memory_barrier:
6530 bld.barrier(aco_opcode::p_memory_barrier_common);
6531 break;
6532 case nir_intrinsic_memory_barrier_buffer:
6533 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6534 break;
6535 case nir_intrinsic_memory_barrier_image:
6536 bld.barrier(aco_opcode::p_memory_barrier_image);
6537 break;
6538 case nir_intrinsic_memory_barrier_tcs_patch:
6539 case nir_intrinsic_memory_barrier_shared:
6540 bld.barrier(aco_opcode::p_memory_barrier_shared);
6541 break;
6542 default:
6543 unreachable("Unimplemented memory barrier intrinsic");
6544 break;
6545 }
6546 }
6547
6548 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6549 {
6550 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6551 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6552 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
6553 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6554 Builder bld(ctx->program, ctx->block);
6555
6556 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6557 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6558 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6559 }
6560
6561 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6562 {
6563 unsigned writemask = nir_intrinsic_write_mask(instr);
6564 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6565 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6566 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6567 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
6568
6569 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6570 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6571 }
6572
6573 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6574 {
6575 unsigned offset = nir_intrinsic_base(instr);
6576 Operand m = load_lds_size_m0(ctx);
6577 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6578 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6579
6580 unsigned num_operands = 3;
6581 aco_opcode op32, op64, op32_rtn, op64_rtn;
6582 switch(instr->intrinsic) {
6583 case nir_intrinsic_shared_atomic_add:
6584 op32 = aco_opcode::ds_add_u32;
6585 op64 = aco_opcode::ds_add_u64;
6586 op32_rtn = aco_opcode::ds_add_rtn_u32;
6587 op64_rtn = aco_opcode::ds_add_rtn_u64;
6588 break;
6589 case nir_intrinsic_shared_atomic_imin:
6590 op32 = aco_opcode::ds_min_i32;
6591 op64 = aco_opcode::ds_min_i64;
6592 op32_rtn = aco_opcode::ds_min_rtn_i32;
6593 op64_rtn = aco_opcode::ds_min_rtn_i64;
6594 break;
6595 case nir_intrinsic_shared_atomic_umin:
6596 op32 = aco_opcode::ds_min_u32;
6597 op64 = aco_opcode::ds_min_u64;
6598 op32_rtn = aco_opcode::ds_min_rtn_u32;
6599 op64_rtn = aco_opcode::ds_min_rtn_u64;
6600 break;
6601 case nir_intrinsic_shared_atomic_imax:
6602 op32 = aco_opcode::ds_max_i32;
6603 op64 = aco_opcode::ds_max_i64;
6604 op32_rtn = aco_opcode::ds_max_rtn_i32;
6605 op64_rtn = aco_opcode::ds_max_rtn_i64;
6606 break;
6607 case nir_intrinsic_shared_atomic_umax:
6608 op32 = aco_opcode::ds_max_u32;
6609 op64 = aco_opcode::ds_max_u64;
6610 op32_rtn = aco_opcode::ds_max_rtn_u32;
6611 op64_rtn = aco_opcode::ds_max_rtn_u64;
6612 break;
6613 case nir_intrinsic_shared_atomic_and:
6614 op32 = aco_opcode::ds_and_b32;
6615 op64 = aco_opcode::ds_and_b64;
6616 op32_rtn = aco_opcode::ds_and_rtn_b32;
6617 op64_rtn = aco_opcode::ds_and_rtn_b64;
6618 break;
6619 case nir_intrinsic_shared_atomic_or:
6620 op32 = aco_opcode::ds_or_b32;
6621 op64 = aco_opcode::ds_or_b64;
6622 op32_rtn = aco_opcode::ds_or_rtn_b32;
6623 op64_rtn = aco_opcode::ds_or_rtn_b64;
6624 break;
6625 case nir_intrinsic_shared_atomic_xor:
6626 op32 = aco_opcode::ds_xor_b32;
6627 op64 = aco_opcode::ds_xor_b64;
6628 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6629 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6630 break;
6631 case nir_intrinsic_shared_atomic_exchange:
6632 op32 = aco_opcode::ds_write_b32;
6633 op64 = aco_opcode::ds_write_b64;
6634 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6635 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6636 break;
6637 case nir_intrinsic_shared_atomic_comp_swap:
6638 op32 = aco_opcode::ds_cmpst_b32;
6639 op64 = aco_opcode::ds_cmpst_b64;
6640 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6641 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6642 num_operands = 4;
6643 break;
6644 default:
6645 unreachable("Unhandled shared atomic intrinsic");
6646 }
6647
6648 /* return the previous value if dest is ever used */
6649 bool return_previous = false;
6650 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6651 return_previous = true;
6652 break;
6653 }
6654 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6655 return_previous = true;
6656 break;
6657 }
6658
6659 aco_opcode op;
6660 if (data.size() == 1) {
6661 assert(instr->dest.ssa.bit_size == 32);
6662 op = return_previous ? op32_rtn : op32;
6663 } else {
6664 assert(instr->dest.ssa.bit_size == 64);
6665 op = return_previous ? op64_rtn : op64;
6666 }
6667
6668 if (offset > 65535) {
6669 Builder bld(ctx->program, ctx->block);
6670 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6671 offset = 0;
6672 }
6673
6674 aco_ptr<DS_instruction> ds;
6675 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6676 ds->operands[0] = Operand(address);
6677 ds->operands[1] = Operand(data);
6678 if (num_operands == 4)
6679 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6680 ds->operands[num_operands - 1] = m;
6681 ds->offset0 = offset;
6682 if (return_previous)
6683 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6684 ctx->block->instructions.emplace_back(std::move(ds));
6685 }
6686
6687 Temp get_scratch_resource(isel_context *ctx)
6688 {
6689 Builder bld(ctx->program, ctx->block);
6690 Temp scratch_addr = ctx->program->private_segment_buffer;
6691 if (ctx->stage != compute_cs)
6692 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6693
6694 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6695 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6696
6697 if (ctx->program->chip_class >= GFX10) {
6698 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6699 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6700 S_008F0C_RESOURCE_LEVEL(1);
6701 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6702 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6703 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6704 }
6705
6706 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6707 if (ctx->program->chip_class <= GFX8)
6708 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6709
6710 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6711 }
6712
6713 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6714 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
6715 Builder bld(ctx->program, ctx->block);
6716 Temp rsrc = get_scratch_resource(ctx);
6717 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6718 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6719
6720 aco_opcode op;
6721 switch (dst.size()) {
6722 case 1:
6723 op = aco_opcode::buffer_load_dword;
6724 break;
6725 case 2:
6726 op = aco_opcode::buffer_load_dwordx2;
6727 break;
6728 case 3:
6729 op = aco_opcode::buffer_load_dwordx3;
6730 break;
6731 case 4:
6732 op = aco_opcode::buffer_load_dwordx4;
6733 break;
6734 case 6:
6735 case 8: {
6736 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
6737 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
6738 bld.def(v4), rsrc, offset,
6739 ctx->program->scratch_offset, 0, true);
6740 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
6741 aco_opcode::buffer_load_dwordx4,
6742 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
6743 rsrc, offset, ctx->program->scratch_offset, 16, true);
6744 emit_split_vector(ctx, lower, 2);
6745 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
6746 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
6747 if (dst.size() == 8) {
6748 emit_split_vector(ctx, upper, 2);
6749 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
6750 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
6751 } else {
6752 elems[2] = upper;
6753 }
6754
6755 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
6756 Format::PSEUDO, dst.size() / 2, 1)};
6757 for (unsigned i = 0; i < dst.size() / 2; i++)
6758 vec->operands[i] = Operand(elems[i]);
6759 vec->definitions[0] = Definition(dst);
6760 bld.insert(std::move(vec));
6761 ctx->allocated_vec.emplace(dst.id(), elems);
6762 return;
6763 }
6764 default:
6765 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
6766 }
6767
6768 bld.mubuf(op, Definition(dst), rsrc, offset, ctx->program->scratch_offset, 0, true);
6769 emit_split_vector(ctx, dst, instr->num_components);
6770 }
6771
6772 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6773 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
6774 Builder bld(ctx->program, ctx->block);
6775 Temp rsrc = get_scratch_resource(ctx);
6776 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6777 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6778
6779 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6780 unsigned writemask = nir_intrinsic_write_mask(instr);
6781
6782 while (writemask) {
6783 int start, count;
6784 u_bit_scan_consecutive_range(&writemask, &start, &count);
6785 int num_bytes = count * elem_size_bytes;
6786
6787 if (num_bytes > 16) {
6788 assert(elem_size_bytes == 8);
6789 writemask |= (((count - 2) << 1) - 1) << (start + 2);
6790 count = 2;
6791 num_bytes = 16;
6792 }
6793
6794 // TODO: check alignment of sub-dword stores
6795 // TODO: split 3 bytes. there is no store instruction for that
6796
6797 Temp write_data;
6798 if (count != instr->num_components) {
6799 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6800 for (int i = 0; i < count; i++) {
6801 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
6802 vec->operands[i] = Operand(elem);
6803 }
6804 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
6805 vec->definitions[0] = Definition(write_data);
6806 ctx->block->instructions.emplace_back(std::move(vec));
6807 } else {
6808 write_data = data;
6809 }
6810
6811 aco_opcode op;
6812 switch (num_bytes) {
6813 case 4:
6814 op = aco_opcode::buffer_store_dword;
6815 break;
6816 case 8:
6817 op = aco_opcode::buffer_store_dwordx2;
6818 break;
6819 case 12:
6820 op = aco_opcode::buffer_store_dwordx3;
6821 break;
6822 case 16:
6823 op = aco_opcode::buffer_store_dwordx4;
6824 break;
6825 default:
6826 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
6827 }
6828
6829 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
6830 }
6831 }
6832
6833 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6834 uint8_t log2_ps_iter_samples;
6835 if (ctx->program->info->ps.force_persample) {
6836 log2_ps_iter_samples =
6837 util_logbase2(ctx->options->key.fs.num_samples);
6838 } else {
6839 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6840 }
6841
6842 /* The bit pattern matches that used by fixed function fragment
6843 * processing. */
6844 static const unsigned ps_iter_masks[] = {
6845 0xffff, /* not used */
6846 0x5555,
6847 0x1111,
6848 0x0101,
6849 0x0001,
6850 };
6851 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6852
6853 Builder bld(ctx->program, ctx->block);
6854
6855 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6856 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6857 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6858 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6859 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6860 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6861 }
6862
6863 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6864 Builder bld(ctx->program, ctx->block);
6865
6866 unsigned stream = nir_intrinsic_stream_id(instr);
6867 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6868 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6869 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6870
6871 /* get GSVS ring */
6872 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6873
6874 unsigned num_components =
6875 ctx->program->info->gs.num_stream_output_components[stream];
6876 assert(num_components);
6877
6878 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6879 unsigned stream_offset = 0;
6880 for (unsigned i = 0; i < stream; i++) {
6881 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6882 stream_offset += prev_stride * ctx->program->wave_size;
6883 }
6884
6885 /* Limit on the stride field for <= GFX7. */
6886 assert(stride < (1 << 14));
6887
6888 Temp gsvs_dwords[4];
6889 for (unsigned i = 0; i < 4; i++)
6890 gsvs_dwords[i] = bld.tmp(s1);
6891 bld.pseudo(aco_opcode::p_split_vector,
6892 Definition(gsvs_dwords[0]),
6893 Definition(gsvs_dwords[1]),
6894 Definition(gsvs_dwords[2]),
6895 Definition(gsvs_dwords[3]),
6896 gsvs_ring);
6897
6898 if (stream_offset) {
6899 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6900
6901 Temp carry = bld.tmp(s1);
6902 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6903 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));
6904 }
6905
6906 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)));
6907 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6908
6909 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6910 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6911
6912 unsigned offset = 0;
6913 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6914 if (ctx->program->info->gs.output_streams[i] != stream)
6915 continue;
6916
6917 for (unsigned j = 0; j < 4; j++) {
6918 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6919 continue;
6920
6921 if (ctx->outputs.mask[i] & (1 << j)) {
6922 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6923 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6924 if (const_offset >= 4096u) {
6925 if (vaddr_offset.isUndefined())
6926 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6927 else
6928 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6929 const_offset %= 4096u;
6930 }
6931
6932 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6933 mtbuf->operands[0] = Operand(gsvs_ring);
6934 mtbuf->operands[1] = vaddr_offset;
6935 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6936 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6937 mtbuf->offen = !vaddr_offset.isUndefined();
6938 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6939 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6940 mtbuf->offset = const_offset;
6941 mtbuf->glc = true;
6942 mtbuf->slc = true;
6943 mtbuf->barrier = barrier_gs_data;
6944 mtbuf->can_reorder = true;
6945 bld.insert(std::move(mtbuf));
6946 }
6947
6948 offset += ctx->shader->info.gs.vertices_out;
6949 }
6950
6951 /* outputs for the next vertex are undefined and keeping them around can
6952 * create invalid IR with control flow */
6953 ctx->outputs.mask[i] = 0;
6954 }
6955
6956 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6957 }
6958
6959 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6960 {
6961 Builder bld(ctx->program, ctx->block);
6962
6963 if (cluster_size == 1) {
6964 return src;
6965 } if (op == nir_op_iand && cluster_size == 4) {
6966 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6967 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6968 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6969 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6970 } else if (op == nir_op_ior && cluster_size == 4) {
6971 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6972 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6973 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6974 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6975 //subgroupAnd(val) -> (exec & ~val) == 0
6976 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6977 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6978 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6979 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6980 //subgroupOr(val) -> (val & exec) != 0
6981 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6982 return bool_to_vector_condition(ctx, tmp);
6983 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6984 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6985 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6986 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6987 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6988 return bool_to_vector_condition(ctx, tmp);
6989 } else {
6990 //subgroupClustered{And,Or,Xor}(val, n) ->
6991 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6992 //cluster_offset = ~(n - 1) & lane_id
6993 //cluster_mask = ((1 << n) - 1)
6994 //subgroupClusteredAnd():
6995 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6996 //subgroupClusteredOr():
6997 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6998 //subgroupClusteredXor():
6999 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7000 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7001 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7002
7003 Temp tmp;
7004 if (op == nir_op_iand)
7005 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7006 else
7007 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7008
7009 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7010
7011 if (ctx->program->chip_class <= GFX7)
7012 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7013 else if (ctx->program->wave_size == 64)
7014 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7015 else
7016 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7017 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7018 if (cluster_mask != 0xffffffff)
7019 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7020
7021 Definition cmp_def = Definition();
7022 if (op == nir_op_iand) {
7023 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7024 } else if (op == nir_op_ior) {
7025 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7026 } else if (op == nir_op_ixor) {
7027 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7028 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7029 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7030 }
7031 cmp_def.setHint(vcc);
7032 return cmp_def.getTemp();
7033 }
7034 }
7035
7036 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7037 {
7038 Builder bld(ctx->program, ctx->block);
7039
7040 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7041 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7042 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7043 Temp tmp;
7044 if (op == nir_op_iand)
7045 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7046 else
7047 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7048
7049 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7050 Temp lo = lohi.def(0).getTemp();
7051 Temp hi = lohi.def(1).getTemp();
7052 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7053
7054 Definition cmp_def = Definition();
7055 if (op == nir_op_iand)
7056 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7057 else if (op == nir_op_ior)
7058 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7059 else if (op == nir_op_ixor)
7060 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7061 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7062 cmp_def.setHint(vcc);
7063 return cmp_def.getTemp();
7064 }
7065
7066 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7067 {
7068 Builder bld(ctx->program, ctx->block);
7069
7070 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7071 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7072 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7073 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7074 if (op == nir_op_iand)
7075 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7076 else if (op == nir_op_ior)
7077 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7078 else if (op == nir_op_ixor)
7079 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7080
7081 assert(false);
7082 return Temp();
7083 }
7084
7085 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7086 {
7087 Builder bld(ctx->program, ctx->block);
7088 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7089 if (src.regClass().type() == RegType::vgpr) {
7090 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7091 } else if (src.regClass() == s1) {
7092 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7093 } else if (src.regClass() == s2) {
7094 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7095 } else {
7096 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7097 nir_print_instr(&instr->instr, stderr);
7098 fprintf(stderr, "\n");
7099 }
7100 }
7101
7102 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7103 {
7104 Builder bld(ctx->program, ctx->block);
7105 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7106 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7107 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7108
7109 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7110 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7111 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7112 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7113
7114 /* Build DD X/Y */
7115 if (ctx->program->chip_class >= GFX8) {
7116 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7117 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7118 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7119 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7120 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7121 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7122 } else {
7123 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7124 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7125 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7126 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7127 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7128 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7129 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7130 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7131 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7132 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7133 }
7134
7135 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7136 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7137 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7138 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7139 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7140 Temp wqm1 = bld.tmp(v1);
7141 emit_wqm(ctx, tmp1, wqm1, true);
7142 Temp wqm2 = bld.tmp(v1);
7143 emit_wqm(ctx, tmp2, wqm2, true);
7144 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7145 return;
7146 }
7147
7148 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7149 {
7150 Builder bld(ctx->program, ctx->block);
7151 switch(instr->intrinsic) {
7152 case nir_intrinsic_load_barycentric_sample:
7153 case nir_intrinsic_load_barycentric_pixel:
7154 case nir_intrinsic_load_barycentric_centroid: {
7155 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7156 Temp bary = Temp(0, s2);
7157 switch (mode) {
7158 case INTERP_MODE_SMOOTH:
7159 case INTERP_MODE_NONE:
7160 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7161 bary = get_arg(ctx, ctx->args->ac.persp_center);
7162 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7163 bary = ctx->persp_centroid;
7164 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7165 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7166 break;
7167 case INTERP_MODE_NOPERSPECTIVE:
7168 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7169 bary = get_arg(ctx, ctx->args->ac.linear_center);
7170 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7171 bary = ctx->linear_centroid;
7172 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7173 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7174 break;
7175 default:
7176 break;
7177 }
7178 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7179 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7180 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7181 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7182 Operand(p1), Operand(p2));
7183 emit_split_vector(ctx, dst, 2);
7184 break;
7185 }
7186 case nir_intrinsic_load_barycentric_model: {
7187 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7188
7189 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7190 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7191 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7192 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7193 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7194 Operand(p1), Operand(p2), Operand(p3));
7195 emit_split_vector(ctx, dst, 3);
7196 break;
7197 }
7198 case nir_intrinsic_load_barycentric_at_sample: {
7199 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7200 switch (ctx->options->key.fs.num_samples) {
7201 case 2: sample_pos_offset += 1 << 3; break;
7202 case 4: sample_pos_offset += 3 << 3; break;
7203 case 8: sample_pos_offset += 7 << 3; break;
7204 default: break;
7205 }
7206 Temp sample_pos;
7207 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7208 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7209 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7210 if (addr.type() == RegType::sgpr) {
7211 Operand offset;
7212 if (const_addr) {
7213 sample_pos_offset += const_addr->u32 << 3;
7214 offset = Operand(sample_pos_offset);
7215 } else if (ctx->options->chip_class >= GFX9) {
7216 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7217 } else {
7218 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7219 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7220 }
7221
7222 Operand off = bld.copy(bld.def(s1), Operand(offset));
7223 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7224
7225 } else if (ctx->options->chip_class >= GFX9) {
7226 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7227 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7228 } else if (ctx->options->chip_class >= GFX7) {
7229 /* addr += private_segment_buffer + sample_pos_offset */
7230 Temp tmp0 = bld.tmp(s1);
7231 Temp tmp1 = bld.tmp(s1);
7232 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7233 Definition scc_tmp = bld.def(s1, scc);
7234 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7235 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7236 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7237 Temp pck0 = bld.tmp(v1);
7238 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7239 tmp1 = as_vgpr(ctx, tmp1);
7240 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);
7241 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7242
7243 /* sample_pos = flat_load_dwordx2 addr */
7244 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7245 } else {
7246 assert(ctx->options->chip_class == GFX6);
7247
7248 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7249 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7250 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7251
7252 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7253 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7254
7255 sample_pos = bld.tmp(v2);
7256
7257 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7258 load->definitions[0] = Definition(sample_pos);
7259 load->operands[0] = Operand(rsrc);
7260 load->operands[1] = Operand(addr);
7261 load->operands[2] = Operand(0u);
7262 load->offset = sample_pos_offset;
7263 load->offen = 0;
7264 load->addr64 = true;
7265 load->glc = false;
7266 load->dlc = false;
7267 load->disable_wqm = false;
7268 load->barrier = barrier_none;
7269 load->can_reorder = true;
7270 ctx->block->instructions.emplace_back(std::move(load));
7271 }
7272
7273 /* sample_pos -= 0.5 */
7274 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7275 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7276 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7277 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7278 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7279
7280 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7281 break;
7282 }
7283 case nir_intrinsic_load_barycentric_at_offset: {
7284 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7285 RegClass rc = RegClass(offset.type(), 1);
7286 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7287 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7288 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7289 break;
7290 }
7291 case nir_intrinsic_load_front_face: {
7292 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7293 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7294 break;
7295 }
7296 case nir_intrinsic_load_view_index: {
7297 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7298 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7299 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7300 break;
7301 }
7302
7303 /* fallthrough */
7304 }
7305 case nir_intrinsic_load_layer_id: {
7306 unsigned idx = nir_intrinsic_base(instr);
7307 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7308 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7309 break;
7310 }
7311 case nir_intrinsic_load_frag_coord: {
7312 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7313 break;
7314 }
7315 case nir_intrinsic_load_sample_pos: {
7316 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7317 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7318 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7319 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7320 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7321 break;
7322 }
7323 case nir_intrinsic_load_tess_coord:
7324 visit_load_tess_coord(ctx, instr);
7325 break;
7326 case nir_intrinsic_load_interpolated_input:
7327 visit_load_interpolated_input(ctx, instr);
7328 break;
7329 case nir_intrinsic_store_output:
7330 visit_store_output(ctx, instr);
7331 break;
7332 case nir_intrinsic_load_input:
7333 case nir_intrinsic_load_input_vertex:
7334 visit_load_input(ctx, instr);
7335 break;
7336 case nir_intrinsic_load_output:
7337 visit_load_output(ctx, instr);
7338 break;
7339 case nir_intrinsic_load_per_vertex_input:
7340 visit_load_per_vertex_input(ctx, instr);
7341 break;
7342 case nir_intrinsic_load_per_vertex_output:
7343 visit_load_per_vertex_output(ctx, instr);
7344 break;
7345 case nir_intrinsic_store_per_vertex_output:
7346 visit_store_per_vertex_output(ctx, instr);
7347 break;
7348 case nir_intrinsic_load_ubo:
7349 visit_load_ubo(ctx, instr);
7350 break;
7351 case nir_intrinsic_load_push_constant:
7352 visit_load_push_constant(ctx, instr);
7353 break;
7354 case nir_intrinsic_load_constant:
7355 visit_load_constant(ctx, instr);
7356 break;
7357 case nir_intrinsic_vulkan_resource_index:
7358 visit_load_resource(ctx, instr);
7359 break;
7360 case nir_intrinsic_discard:
7361 visit_discard(ctx, instr);
7362 break;
7363 case nir_intrinsic_discard_if:
7364 visit_discard_if(ctx, instr);
7365 break;
7366 case nir_intrinsic_load_shared:
7367 visit_load_shared(ctx, instr);
7368 break;
7369 case nir_intrinsic_store_shared:
7370 visit_store_shared(ctx, instr);
7371 break;
7372 case nir_intrinsic_shared_atomic_add:
7373 case nir_intrinsic_shared_atomic_imin:
7374 case nir_intrinsic_shared_atomic_umin:
7375 case nir_intrinsic_shared_atomic_imax:
7376 case nir_intrinsic_shared_atomic_umax:
7377 case nir_intrinsic_shared_atomic_and:
7378 case nir_intrinsic_shared_atomic_or:
7379 case nir_intrinsic_shared_atomic_xor:
7380 case nir_intrinsic_shared_atomic_exchange:
7381 case nir_intrinsic_shared_atomic_comp_swap:
7382 visit_shared_atomic(ctx, instr);
7383 break;
7384 case nir_intrinsic_image_deref_load:
7385 visit_image_load(ctx, instr);
7386 break;
7387 case nir_intrinsic_image_deref_store:
7388 visit_image_store(ctx, instr);
7389 break;
7390 case nir_intrinsic_image_deref_atomic_add:
7391 case nir_intrinsic_image_deref_atomic_umin:
7392 case nir_intrinsic_image_deref_atomic_imin:
7393 case nir_intrinsic_image_deref_atomic_umax:
7394 case nir_intrinsic_image_deref_atomic_imax:
7395 case nir_intrinsic_image_deref_atomic_and:
7396 case nir_intrinsic_image_deref_atomic_or:
7397 case nir_intrinsic_image_deref_atomic_xor:
7398 case nir_intrinsic_image_deref_atomic_exchange:
7399 case nir_intrinsic_image_deref_atomic_comp_swap:
7400 visit_image_atomic(ctx, instr);
7401 break;
7402 case nir_intrinsic_image_deref_size:
7403 visit_image_size(ctx, instr);
7404 break;
7405 case nir_intrinsic_load_ssbo:
7406 visit_load_ssbo(ctx, instr);
7407 break;
7408 case nir_intrinsic_store_ssbo:
7409 visit_store_ssbo(ctx, instr);
7410 break;
7411 case nir_intrinsic_load_global:
7412 visit_load_global(ctx, instr);
7413 break;
7414 case nir_intrinsic_store_global:
7415 visit_store_global(ctx, instr);
7416 break;
7417 case nir_intrinsic_global_atomic_add:
7418 case nir_intrinsic_global_atomic_imin:
7419 case nir_intrinsic_global_atomic_umin:
7420 case nir_intrinsic_global_atomic_imax:
7421 case nir_intrinsic_global_atomic_umax:
7422 case nir_intrinsic_global_atomic_and:
7423 case nir_intrinsic_global_atomic_or:
7424 case nir_intrinsic_global_atomic_xor:
7425 case nir_intrinsic_global_atomic_exchange:
7426 case nir_intrinsic_global_atomic_comp_swap:
7427 visit_global_atomic(ctx, instr);
7428 break;
7429 case nir_intrinsic_ssbo_atomic_add:
7430 case nir_intrinsic_ssbo_atomic_imin:
7431 case nir_intrinsic_ssbo_atomic_umin:
7432 case nir_intrinsic_ssbo_atomic_imax:
7433 case nir_intrinsic_ssbo_atomic_umax:
7434 case nir_intrinsic_ssbo_atomic_and:
7435 case nir_intrinsic_ssbo_atomic_or:
7436 case nir_intrinsic_ssbo_atomic_xor:
7437 case nir_intrinsic_ssbo_atomic_exchange:
7438 case nir_intrinsic_ssbo_atomic_comp_swap:
7439 visit_atomic_ssbo(ctx, instr);
7440 break;
7441 case nir_intrinsic_load_scratch:
7442 visit_load_scratch(ctx, instr);
7443 break;
7444 case nir_intrinsic_store_scratch:
7445 visit_store_scratch(ctx, instr);
7446 break;
7447 case nir_intrinsic_get_buffer_size:
7448 visit_get_buffer_size(ctx, instr);
7449 break;
7450 case nir_intrinsic_control_barrier: {
7451 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7452 /* GFX6 only (thanks to a hw bug workaround):
7453 * The real barrier instruction isn’t needed, because an entire patch
7454 * always fits into a single wave.
7455 */
7456 break;
7457 }
7458
7459 if (ctx->program->workgroup_size > ctx->program->wave_size)
7460 bld.sopp(aco_opcode::s_barrier);
7461
7462 break;
7463 }
7464 case nir_intrinsic_memory_barrier_tcs_patch:
7465 case nir_intrinsic_group_memory_barrier:
7466 case nir_intrinsic_memory_barrier:
7467 case nir_intrinsic_memory_barrier_buffer:
7468 case nir_intrinsic_memory_barrier_image:
7469 case nir_intrinsic_memory_barrier_shared:
7470 emit_memory_barrier(ctx, instr);
7471 break;
7472 case nir_intrinsic_load_num_work_groups: {
7473 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7474 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7475 emit_split_vector(ctx, dst, 3);
7476 break;
7477 }
7478 case nir_intrinsic_load_local_invocation_id: {
7479 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7480 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7481 emit_split_vector(ctx, dst, 3);
7482 break;
7483 }
7484 case nir_intrinsic_load_work_group_id: {
7485 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7486 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7487 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7488 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7489 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7490 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7491 emit_split_vector(ctx, dst, 3);
7492 break;
7493 }
7494 case nir_intrinsic_load_local_invocation_index: {
7495 Temp id = emit_mbcnt(ctx, bld.def(v1));
7496
7497 /* The tg_size bits [6:11] contain the subgroup id,
7498 * we need this multiplied by the wave size, and then OR the thread id to it.
7499 */
7500 if (ctx->program->wave_size == 64) {
7501 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7502 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7503 get_arg(ctx, ctx->args->ac.tg_size));
7504 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7505 } else {
7506 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7507 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7508 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7509 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7510 }
7511 break;
7512 }
7513 case nir_intrinsic_load_subgroup_id: {
7514 if (ctx->stage == compute_cs) {
7515 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7516 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7517 } else {
7518 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7519 }
7520 break;
7521 }
7522 case nir_intrinsic_load_subgroup_invocation: {
7523 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7524 break;
7525 }
7526 case nir_intrinsic_load_num_subgroups: {
7527 if (ctx->stage == compute_cs)
7528 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7529 get_arg(ctx, ctx->args->ac.tg_size));
7530 else
7531 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7532 break;
7533 }
7534 case nir_intrinsic_ballot: {
7535 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7536 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7537 Definition tmp = bld.def(dst.regClass());
7538 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7539 if (instr->src[0].ssa->bit_size == 1) {
7540 assert(src.regClass() == bld.lm);
7541 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7542 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7543 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7544 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7545 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7546 } else {
7547 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7548 nir_print_instr(&instr->instr, stderr);
7549 fprintf(stderr, "\n");
7550 }
7551 if (dst.size() != bld.lm.size()) {
7552 /* Wave32 with ballot size set to 64 */
7553 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7554 }
7555 emit_wqm(ctx, tmp.getTemp(), dst);
7556 break;
7557 }
7558 case nir_intrinsic_shuffle:
7559 case nir_intrinsic_read_invocation: {
7560 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7561 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7562 emit_uniform_subgroup(ctx, instr, src);
7563 } else {
7564 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7565 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7566 tid = bld.as_uniform(tid);
7567 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7568 if (src.regClass() == v1) {
7569 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7570 } else if (src.regClass() == v2) {
7571 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7572 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7573 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7574 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7575 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7576 emit_split_vector(ctx, dst, 2);
7577 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7578 assert(src.regClass() == bld.lm);
7579 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7580 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7581 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7582 assert(src.regClass() == bld.lm);
7583 Temp tmp;
7584 if (ctx->program->chip_class <= GFX7)
7585 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7586 else if (ctx->program->wave_size == 64)
7587 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7588 else
7589 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7590 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7591 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7592 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7593 } else {
7594 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7595 nir_print_instr(&instr->instr, stderr);
7596 fprintf(stderr, "\n");
7597 }
7598 }
7599 break;
7600 }
7601 case nir_intrinsic_load_sample_id: {
7602 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7603 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7604 break;
7605 }
7606 case nir_intrinsic_load_sample_mask_in: {
7607 visit_load_sample_mask_in(ctx, instr);
7608 break;
7609 }
7610 case nir_intrinsic_read_first_invocation: {
7611 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7612 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7613 if (src.regClass() == v1) {
7614 emit_wqm(ctx,
7615 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7616 dst);
7617 } else if (src.regClass() == v2) {
7618 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7619 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7620 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7621 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7622 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7623 emit_split_vector(ctx, dst, 2);
7624 } else if (instr->dest.ssa.bit_size == 1) {
7625 assert(src.regClass() == bld.lm);
7626 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7627 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7628 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7629 } else if (src.regClass() == s1) {
7630 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7631 } else if (src.regClass() == s2) {
7632 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7633 } else {
7634 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7635 nir_print_instr(&instr->instr, stderr);
7636 fprintf(stderr, "\n");
7637 }
7638 break;
7639 }
7640 case nir_intrinsic_vote_all: {
7641 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7642 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7643 assert(src.regClass() == bld.lm);
7644 assert(dst.regClass() == bld.lm);
7645
7646 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7647 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7648 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7649 break;
7650 }
7651 case nir_intrinsic_vote_any: {
7652 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7653 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7654 assert(src.regClass() == bld.lm);
7655 assert(dst.regClass() == bld.lm);
7656
7657 Temp tmp = bool_to_scalar_condition(ctx, src);
7658 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7659 break;
7660 }
7661 case nir_intrinsic_reduce:
7662 case nir_intrinsic_inclusive_scan:
7663 case nir_intrinsic_exclusive_scan: {
7664 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7665 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7666 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7667 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7668 nir_intrinsic_cluster_size(instr) : 0;
7669 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7670
7671 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7672 emit_uniform_subgroup(ctx, instr, src);
7673 } else if (instr->dest.ssa.bit_size == 1) {
7674 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7675 op = nir_op_iand;
7676 else if (op == nir_op_iadd)
7677 op = nir_op_ixor;
7678 else if (op == nir_op_umax || op == nir_op_imax)
7679 op = nir_op_ior;
7680 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7681
7682 switch (instr->intrinsic) {
7683 case nir_intrinsic_reduce:
7684 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7685 break;
7686 case nir_intrinsic_exclusive_scan:
7687 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7688 break;
7689 case nir_intrinsic_inclusive_scan:
7690 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7691 break;
7692 default:
7693 assert(false);
7694 }
7695 } else if (cluster_size == 1) {
7696 bld.copy(Definition(dst), src);
7697 } else {
7698 src = as_vgpr(ctx, src);
7699
7700 ReduceOp reduce_op;
7701 switch (op) {
7702 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7703 CASE(iadd)
7704 CASE(imul)
7705 CASE(fadd)
7706 CASE(fmul)
7707 CASE(imin)
7708 CASE(umin)
7709 CASE(fmin)
7710 CASE(imax)
7711 CASE(umax)
7712 CASE(fmax)
7713 CASE(iand)
7714 CASE(ior)
7715 CASE(ixor)
7716 default:
7717 unreachable("unknown reduction op");
7718 #undef CASE
7719 }
7720
7721 aco_opcode aco_op;
7722 switch (instr->intrinsic) {
7723 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7724 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7725 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7726 default:
7727 unreachable("unknown reduce intrinsic");
7728 }
7729
7730 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7731 reduce->operands[0] = Operand(src);
7732 // filled in by aco_reduce_assign.cpp, used internally as part of the
7733 // reduce sequence
7734 assert(dst.size() == 1 || dst.size() == 2);
7735 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7736 reduce->operands[2] = Operand(v1.as_linear());
7737
7738 Temp tmp_dst = bld.tmp(dst.regClass());
7739 reduce->definitions[0] = Definition(tmp_dst);
7740 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7741 reduce->definitions[2] = Definition();
7742 reduce->definitions[3] = Definition(scc, s1);
7743 reduce->definitions[4] = Definition();
7744 reduce->reduce_op = reduce_op;
7745 reduce->cluster_size = cluster_size;
7746 ctx->block->instructions.emplace_back(std::move(reduce));
7747
7748 emit_wqm(ctx, tmp_dst, dst);
7749 }
7750 break;
7751 }
7752 case nir_intrinsic_quad_broadcast: {
7753 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7754 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7755 emit_uniform_subgroup(ctx, instr, src);
7756 } else {
7757 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7758 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7759 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7760
7761 if (instr->dest.ssa.bit_size == 1) {
7762 assert(src.regClass() == bld.lm);
7763 assert(dst.regClass() == bld.lm);
7764 uint32_t half_mask = 0x11111111u << lane;
7765 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7766 Temp tmp = bld.tmp(bld.lm);
7767 bld.sop1(Builder::s_wqm, Definition(tmp),
7768 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7769 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7770 emit_wqm(ctx, tmp, dst);
7771 } else if (instr->dest.ssa.bit_size == 32) {
7772 if (ctx->program->chip_class >= GFX8)
7773 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7774 else
7775 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7776 } else if (instr->dest.ssa.bit_size == 64) {
7777 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7778 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7779 if (ctx->program->chip_class >= GFX8) {
7780 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7781 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7782 } else {
7783 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7784 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7785 }
7786 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7787 emit_split_vector(ctx, dst, 2);
7788 } else {
7789 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7790 nir_print_instr(&instr->instr, stderr);
7791 fprintf(stderr, "\n");
7792 }
7793 }
7794 break;
7795 }
7796 case nir_intrinsic_quad_swap_horizontal:
7797 case nir_intrinsic_quad_swap_vertical:
7798 case nir_intrinsic_quad_swap_diagonal:
7799 case nir_intrinsic_quad_swizzle_amd: {
7800 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7801 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7802 emit_uniform_subgroup(ctx, instr, src);
7803 break;
7804 }
7805 uint16_t dpp_ctrl = 0;
7806 switch (instr->intrinsic) {
7807 case nir_intrinsic_quad_swap_horizontal:
7808 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7809 break;
7810 case nir_intrinsic_quad_swap_vertical:
7811 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7812 break;
7813 case nir_intrinsic_quad_swap_diagonal:
7814 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7815 break;
7816 case nir_intrinsic_quad_swizzle_amd:
7817 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7818 break;
7819 default:
7820 break;
7821 }
7822 if (ctx->program->chip_class < GFX8)
7823 dpp_ctrl |= (1 << 15);
7824
7825 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7826 if (instr->dest.ssa.bit_size == 1) {
7827 assert(src.regClass() == bld.lm);
7828 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7829 if (ctx->program->chip_class >= GFX8)
7830 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7831 else
7832 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7833 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7834 emit_wqm(ctx, tmp, dst);
7835 } else if (instr->dest.ssa.bit_size == 32) {
7836 Temp tmp;
7837 if (ctx->program->chip_class >= GFX8)
7838 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7839 else
7840 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7841 emit_wqm(ctx, tmp, dst);
7842 } else if (instr->dest.ssa.bit_size == 64) {
7843 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7844 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7845 if (ctx->program->chip_class >= GFX8) {
7846 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7847 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7848 } else {
7849 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7850 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7851 }
7852 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7853 emit_split_vector(ctx, dst, 2);
7854 } else {
7855 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7856 nir_print_instr(&instr->instr, stderr);
7857 fprintf(stderr, "\n");
7858 }
7859 break;
7860 }
7861 case nir_intrinsic_masked_swizzle_amd: {
7862 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7863 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7864 emit_uniform_subgroup(ctx, instr, src);
7865 break;
7866 }
7867 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7868 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7869 if (dst.regClass() == v1) {
7870 emit_wqm(ctx,
7871 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7872 dst);
7873 } else if (dst.regClass() == v2) {
7874 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7875 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7876 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7877 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7878 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7879 emit_split_vector(ctx, dst, 2);
7880 } else {
7881 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7882 nir_print_instr(&instr->instr, stderr);
7883 fprintf(stderr, "\n");
7884 }
7885 break;
7886 }
7887 case nir_intrinsic_write_invocation_amd: {
7888 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7889 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7890 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7891 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7892 if (dst.regClass() == v1) {
7893 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7894 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7895 } else if (dst.regClass() == v2) {
7896 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7897 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7898 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7899 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7900 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7901 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7902 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7903 emit_split_vector(ctx, dst, 2);
7904 } else {
7905 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7906 nir_print_instr(&instr->instr, stderr);
7907 fprintf(stderr, "\n");
7908 }
7909 break;
7910 }
7911 case nir_intrinsic_mbcnt_amd: {
7912 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7913 RegClass rc = RegClass(src.type(), 1);
7914 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7915 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7916 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7917 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7918 emit_wqm(ctx, wqm_tmp, dst);
7919 break;
7920 }
7921 case nir_intrinsic_load_helper_invocation: {
7922 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7923 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7924 ctx->block->kind |= block_kind_needs_lowering;
7925 ctx->program->needs_exact = true;
7926 break;
7927 }
7928 case nir_intrinsic_is_helper_invocation: {
7929 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7930 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7931 ctx->block->kind |= block_kind_needs_lowering;
7932 ctx->program->needs_exact = true;
7933 break;
7934 }
7935 case nir_intrinsic_demote:
7936 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7937
7938 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7939 ctx->cf_info.exec_potentially_empty_discard = true;
7940 ctx->block->kind |= block_kind_uses_demote;
7941 ctx->program->needs_exact = true;
7942 break;
7943 case nir_intrinsic_demote_if: {
7944 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7945 assert(src.regClass() == bld.lm);
7946 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7947 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7948
7949 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7950 ctx->cf_info.exec_potentially_empty_discard = true;
7951 ctx->block->kind |= block_kind_uses_demote;
7952 ctx->program->needs_exact = true;
7953 break;
7954 }
7955 case nir_intrinsic_first_invocation: {
7956 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7957 get_ssa_temp(ctx, &instr->dest.ssa));
7958 break;
7959 }
7960 case nir_intrinsic_shader_clock:
7961 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7962 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7963 break;
7964 case nir_intrinsic_load_vertex_id_zero_base: {
7965 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7966 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7967 break;
7968 }
7969 case nir_intrinsic_load_first_vertex: {
7970 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7971 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7972 break;
7973 }
7974 case nir_intrinsic_load_base_instance: {
7975 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7976 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7977 break;
7978 }
7979 case nir_intrinsic_load_instance_id: {
7980 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7981 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7982 break;
7983 }
7984 case nir_intrinsic_load_draw_id: {
7985 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7986 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7987 break;
7988 }
7989 case nir_intrinsic_load_invocation_id: {
7990 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7991
7992 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7993 if (ctx->options->chip_class >= GFX10)
7994 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7995 else
7996 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7997 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7998 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7999 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8000 } else {
8001 unreachable("Unsupported stage for load_invocation_id");
8002 }
8003
8004 break;
8005 }
8006 case nir_intrinsic_load_primitive_id: {
8007 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8008
8009 switch (ctx->shader->info.stage) {
8010 case MESA_SHADER_GEOMETRY:
8011 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8012 break;
8013 case MESA_SHADER_TESS_CTRL:
8014 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8015 break;
8016 case MESA_SHADER_TESS_EVAL:
8017 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8018 break;
8019 default:
8020 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8021 }
8022
8023 break;
8024 }
8025 case nir_intrinsic_load_patch_vertices_in: {
8026 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8027 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8028
8029 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8030 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8031 break;
8032 }
8033 case nir_intrinsic_emit_vertex_with_counter: {
8034 visit_emit_vertex_with_counter(ctx, instr);
8035 break;
8036 }
8037 case nir_intrinsic_end_primitive_with_counter: {
8038 unsigned stream = nir_intrinsic_stream_id(instr);
8039 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8040 break;
8041 }
8042 case nir_intrinsic_set_vertex_count: {
8043 /* unused, the HW keeps track of this for us */
8044 break;
8045 }
8046 default:
8047 fprintf(stderr, "Unimplemented intrinsic instr: ");
8048 nir_print_instr(&instr->instr, stderr);
8049 fprintf(stderr, "\n");
8050 abort();
8051
8052 break;
8053 }
8054 }
8055
8056
8057 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8058 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8059 enum glsl_base_type *stype)
8060 {
8061 nir_deref_instr *texture_deref_instr = NULL;
8062 nir_deref_instr *sampler_deref_instr = NULL;
8063 int plane = -1;
8064
8065 for (unsigned i = 0; i < instr->num_srcs; i++) {
8066 switch (instr->src[i].src_type) {
8067 case nir_tex_src_texture_deref:
8068 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8069 break;
8070 case nir_tex_src_sampler_deref:
8071 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8072 break;
8073 case nir_tex_src_plane:
8074 plane = nir_src_as_int(instr->src[i].src);
8075 break;
8076 default:
8077 break;
8078 }
8079 }
8080
8081 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8082
8083 if (!sampler_deref_instr)
8084 sampler_deref_instr = texture_deref_instr;
8085
8086 if (plane >= 0) {
8087 assert(instr->op != nir_texop_txf_ms &&
8088 instr->op != nir_texop_samples_identical);
8089 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8090 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8091 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8092 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8093 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8094 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8095 } else {
8096 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8097 }
8098 if (samp_ptr) {
8099 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8100
8101 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8102 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8103 Builder bld(ctx->program, ctx->block);
8104
8105 /* to avoid unnecessary moves, we split and recombine sampler and image */
8106 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8107 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8108 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8109 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8110 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8111 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8112 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8113 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8114
8115 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8116 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8117 img[0], img[1], img[2], img[3],
8118 img[4], img[5], img[6], img[7]);
8119 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8120 samp[0], samp[1], samp[2], samp[3]);
8121 }
8122 }
8123 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8124 instr->op == nir_texop_samples_identical))
8125 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8126 }
8127
8128 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8129 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8130 {
8131 Builder bld(ctx->program, ctx->block);
8132
8133 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8134 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8135 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8136
8137 Operand neg_one(0xbf800000u);
8138 Operand one(0x3f800000u);
8139 Operand two(0x40000000u);
8140 Operand four(0x40800000u);
8141
8142 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8143 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8144 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8145
8146 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8147 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8148 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8149 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);
8150
8151 // select sc
8152 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8153 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8154 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8155 one, is_ma_y);
8156 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8157
8158 // select tc
8159 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8160 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8161 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8162
8163 // select ma
8164 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8165 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8166 deriv_z, is_ma_z);
8167 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8168 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8169 }
8170
8171 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8172 {
8173 Builder bld(ctx->program, ctx->block);
8174 Temp ma, tc, sc, id;
8175
8176 if (is_array) {
8177 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8178
8179 // see comment in ac_prepare_cube_coords()
8180 if (ctx->options->chip_class <= GFX8)
8181 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8182 }
8183
8184 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8185
8186 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8187 vop3a->operands[0] = Operand(ma);
8188 vop3a->abs[0] = true;
8189 Temp invma = bld.tmp(v1);
8190 vop3a->definitions[0] = Definition(invma);
8191 ctx->block->instructions.emplace_back(std::move(vop3a));
8192
8193 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8194 if (!is_deriv)
8195 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8196
8197 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8198 if (!is_deriv)
8199 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8200
8201 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8202
8203 if (is_deriv) {
8204 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8205 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8206
8207 for (unsigned i = 0; i < 2; i++) {
8208 // see comment in ac_prepare_cube_coords()
8209 Temp deriv_ma;
8210 Temp deriv_sc, deriv_tc;
8211 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8212 &deriv_ma, &deriv_sc, &deriv_tc);
8213
8214 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8215
8216 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8217 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8218 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8219 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8220 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8221 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8222 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8223 }
8224
8225 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8226 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8227 }
8228
8229 if (is_array)
8230 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8231 coords.resize(3);
8232 coords[0] = sc;
8233 coords[1] = tc;
8234 coords[2] = id;
8235 }
8236
8237 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8238 {
8239 if (vec->parent_instr->type != nir_instr_type_alu)
8240 return;
8241 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8242 if (vec_instr->op != nir_op_vec(vec->num_components))
8243 return;
8244
8245 for (unsigned i = 0; i < vec->num_components; i++) {
8246 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8247 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8248 }
8249 }
8250
8251 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8252 {
8253 Builder bld(ctx->program, ctx->block);
8254 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8255 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8256 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8257 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8258 std::vector<Temp> coords;
8259 std::vector<Temp> derivs;
8260 nir_const_value *sample_index_cv = NULL;
8261 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8262 enum glsl_base_type stype;
8263 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8264
8265 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8266 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8267 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8268 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8269
8270 for (unsigned i = 0; i < instr->num_srcs; i++) {
8271 switch (instr->src[i].src_type) {
8272 case nir_tex_src_coord: {
8273 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8274 for (unsigned i = 0; i < coord.size(); i++)
8275 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8276 break;
8277 }
8278 case nir_tex_src_bias:
8279 if (instr->op == nir_texop_txb) {
8280 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8281 has_bias = true;
8282 }
8283 break;
8284 case nir_tex_src_lod: {
8285 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8286
8287 if (val && val->f32 <= 0.0) {
8288 level_zero = true;
8289 } else {
8290 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8291 has_lod = true;
8292 }
8293 break;
8294 }
8295 case nir_tex_src_comparator:
8296 if (instr->is_shadow) {
8297 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8298 has_compare = true;
8299 }
8300 break;
8301 case nir_tex_src_offset:
8302 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8303 get_const_vec(instr->src[i].src.ssa, const_offset);
8304 has_offset = true;
8305 break;
8306 case nir_tex_src_ddx:
8307 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8308 has_ddx = true;
8309 break;
8310 case nir_tex_src_ddy:
8311 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8312 has_ddy = true;
8313 break;
8314 case nir_tex_src_ms_index:
8315 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8316 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8317 has_sample_index = true;
8318 break;
8319 case nir_tex_src_texture_offset:
8320 case nir_tex_src_sampler_offset:
8321 default:
8322 break;
8323 }
8324 }
8325
8326 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8327 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8328
8329 if (instr->op == nir_texop_texture_samples) {
8330 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8331
8332 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8333 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8334 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 */));
8335 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8336
8337 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8338 samples, Operand(1u), bld.scc(is_msaa));
8339 return;
8340 }
8341
8342 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8343 aco_ptr<Instruction> tmp_instr;
8344 Temp acc, pack = Temp();
8345
8346 uint32_t pack_const = 0;
8347 for (unsigned i = 0; i < offset.size(); i++) {
8348 if (!const_offset[i])
8349 continue;
8350 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8351 }
8352
8353 if (offset.type() == RegType::sgpr) {
8354 for (unsigned i = 0; i < offset.size(); i++) {
8355 if (const_offset[i])
8356 continue;
8357
8358 acc = emit_extract_vector(ctx, offset, i, s1);
8359 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8360
8361 if (i) {
8362 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8363 }
8364
8365 if (pack == Temp()) {
8366 pack = acc;
8367 } else {
8368 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8369 }
8370 }
8371
8372 if (pack_const && pack != Temp())
8373 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8374 } else {
8375 for (unsigned i = 0; i < offset.size(); i++) {
8376 if (const_offset[i])
8377 continue;
8378
8379 acc = emit_extract_vector(ctx, offset, i, v1);
8380 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8381
8382 if (i) {
8383 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8384 }
8385
8386 if (pack == Temp()) {
8387 pack = acc;
8388 } else {
8389 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8390 }
8391 }
8392
8393 if (pack_const && pack != Temp())
8394 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8395 }
8396 if (pack_const && pack == Temp())
8397 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8398 else if (pack == Temp())
8399 has_offset = false;
8400 else
8401 offset = pack;
8402 }
8403
8404 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8405 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8406
8407 /* pack derivatives */
8408 if (has_ddx || has_ddy) {
8409 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8410 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8411 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8412 derivs = {ddy, zero, ddy, zero};
8413 } else {
8414 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8415 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8416 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8417 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8418 }
8419 has_derivs = true;
8420 }
8421
8422 if (instr->coord_components > 1 &&
8423 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8424 instr->is_array &&
8425 instr->op != nir_texop_txf)
8426 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8427
8428 if (instr->coord_components > 2 &&
8429 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8430 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8431 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8432 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8433 instr->is_array &&
8434 instr->op != nir_texop_txf &&
8435 instr->op != nir_texop_txf_ms &&
8436 instr->op != nir_texop_fragment_fetch &&
8437 instr->op != nir_texop_fragment_mask_fetch)
8438 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8439
8440 if (ctx->options->chip_class == GFX9 &&
8441 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8442 instr->op != nir_texop_lod && instr->coord_components) {
8443 assert(coords.size() > 0 && coords.size() < 3);
8444
8445 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8446 Operand((uint32_t) 0) :
8447 Operand((uint32_t) 0x3f000000)));
8448 }
8449
8450 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8451
8452 if (instr->op == nir_texop_samples_identical)
8453 resource = fmask_ptr;
8454
8455 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8456 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8457 instr->op != nir_texop_txs &&
8458 instr->op != nir_texop_fragment_fetch &&
8459 instr->op != nir_texop_fragment_mask_fetch) {
8460 assert(has_sample_index);
8461 Operand op(sample_index);
8462 if (sample_index_cv)
8463 op = Operand(sample_index_cv->u32);
8464 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8465 }
8466
8467 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8468 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8469 Temp off = emit_extract_vector(ctx, offset, i, v1);
8470 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8471 }
8472 has_offset = false;
8473 }
8474
8475 /* Build tex instruction */
8476 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8477 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8478 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8479 : 0;
8480 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8481 Temp tmp_dst = dst;
8482
8483 /* gather4 selects the component by dmask and always returns vec4 */
8484 if (instr->op == nir_texop_tg4) {
8485 assert(instr->dest.ssa.num_components == 4);
8486 if (instr->is_shadow)
8487 dmask = 1;
8488 else
8489 dmask = 1 << instr->component;
8490 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8491 tmp_dst = bld.tmp(v4);
8492 } else if (instr->op == nir_texop_samples_identical) {
8493 tmp_dst = bld.tmp(v1);
8494 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8495 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8496 }
8497
8498 aco_ptr<MIMG_instruction> tex;
8499 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8500 if (!has_lod)
8501 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8502
8503 bool div_by_6 = instr->op == nir_texop_txs &&
8504 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8505 instr->is_array &&
8506 (dmask & (1 << 2));
8507 if (tmp_dst.id() == dst.id() && div_by_6)
8508 tmp_dst = bld.tmp(tmp_dst.regClass());
8509
8510 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8511 tex->operands[0] = Operand(resource);
8512 tex->operands[1] = Operand(s4); /* no sampler */
8513 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8514 if (ctx->options->chip_class == GFX9 &&
8515 instr->op == nir_texop_txs &&
8516 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8517 instr->is_array) {
8518 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8519 } else if (instr->op == nir_texop_query_levels) {
8520 tex->dmask = 1 << 3;
8521 } else {
8522 tex->dmask = dmask;
8523 }
8524 tex->da = da;
8525 tex->definitions[0] = Definition(tmp_dst);
8526 tex->dim = dim;
8527 tex->can_reorder = true;
8528 ctx->block->instructions.emplace_back(std::move(tex));
8529
8530 if (div_by_6) {
8531 /* divide 3rd value by 6 by multiplying with magic number */
8532 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8533 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8534 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8535 assert(instr->dest.ssa.num_components == 3);
8536 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8537 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8538 emit_extract_vector(ctx, tmp_dst, 0, v1),
8539 emit_extract_vector(ctx, tmp_dst, 1, v1),
8540 by_6);
8541
8542 }
8543
8544 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8545 return;
8546 }
8547
8548 Temp tg4_compare_cube_wa64 = Temp();
8549
8550 if (tg4_integer_workarounds) {
8551 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8552 tex->operands[0] = Operand(resource);
8553 tex->operands[1] = Operand(s4); /* no sampler */
8554 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8555 tex->dim = dim;
8556 tex->dmask = 0x3;
8557 tex->da = da;
8558 Temp size = bld.tmp(v2);
8559 tex->definitions[0] = Definition(size);
8560 tex->can_reorder = true;
8561 ctx->block->instructions.emplace_back(std::move(tex));
8562 emit_split_vector(ctx, size, size.size());
8563
8564 Temp half_texel[2];
8565 for (unsigned i = 0; i < 2; i++) {
8566 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8567 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8568 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8569 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8570 }
8571
8572 Temp new_coords[2] = {
8573 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8574 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8575 };
8576
8577 if (tg4_integer_cube_workaround) {
8578 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8579 Temp desc[resource.size()];
8580 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8581 Format::PSEUDO, 1, resource.size())};
8582 split->operands[0] = Operand(resource);
8583 for (unsigned i = 0; i < resource.size(); i++) {
8584 desc[i] = bld.tmp(s1);
8585 split->definitions[i] = Definition(desc[i]);
8586 }
8587 ctx->block->instructions.emplace_back(std::move(split));
8588
8589 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8590 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8591 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8592
8593 Temp nfmt;
8594 if (stype == GLSL_TYPE_UINT) {
8595 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8596 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8597 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8598 bld.scc(compare_cube_wa));
8599 } else {
8600 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8601 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8602 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8603 bld.scc(compare_cube_wa));
8604 }
8605 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8606 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8607
8608 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8609
8610 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8611 Operand((uint32_t)C_008F14_NUM_FORMAT));
8612 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8613
8614 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8615 Format::PSEUDO, resource.size(), 1)};
8616 for (unsigned i = 0; i < resource.size(); i++)
8617 vec->operands[i] = Operand(desc[i]);
8618 resource = bld.tmp(resource.regClass());
8619 vec->definitions[0] = Definition(resource);
8620 ctx->block->instructions.emplace_back(std::move(vec));
8621
8622 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8623 new_coords[0], coords[0], tg4_compare_cube_wa64);
8624 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8625 new_coords[1], coords[1], tg4_compare_cube_wa64);
8626 }
8627 coords[0] = new_coords[0];
8628 coords[1] = new_coords[1];
8629 }
8630
8631 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8632 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8633
8634 assert(coords.size() == 1);
8635 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8636 aco_opcode op;
8637 switch (last_bit) {
8638 case 1:
8639 op = aco_opcode::buffer_load_format_x; break;
8640 case 2:
8641 op = aco_opcode::buffer_load_format_xy; break;
8642 case 3:
8643 op = aco_opcode::buffer_load_format_xyz; break;
8644 case 4:
8645 op = aco_opcode::buffer_load_format_xyzw; break;
8646 default:
8647 unreachable("Tex instruction loads more than 4 components.");
8648 }
8649
8650 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8651 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8652 tmp_dst = dst;
8653 else
8654 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8655
8656 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8657 mubuf->operands[0] = Operand(resource);
8658 mubuf->operands[1] = Operand(coords[0]);
8659 mubuf->operands[2] = Operand((uint32_t) 0);
8660 mubuf->definitions[0] = Definition(tmp_dst);
8661 mubuf->idxen = true;
8662 mubuf->can_reorder = true;
8663 ctx->block->instructions.emplace_back(std::move(mubuf));
8664
8665 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8666 return;
8667 }
8668
8669 /* gather MIMG address components */
8670 std::vector<Temp> args;
8671 if (has_offset)
8672 args.emplace_back(offset);
8673 if (has_bias)
8674 args.emplace_back(bias);
8675 if (has_compare)
8676 args.emplace_back(compare);
8677 if (has_derivs)
8678 args.insert(args.end(), derivs.begin(), derivs.end());
8679
8680 args.insert(args.end(), coords.begin(), coords.end());
8681 if (has_sample_index)
8682 args.emplace_back(sample_index);
8683 if (has_lod)
8684 args.emplace_back(lod);
8685
8686 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8687 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8688 vec->definitions[0] = Definition(arg);
8689 for (unsigned i = 0; i < args.size(); i++)
8690 vec->operands[i] = Operand(args[i]);
8691 ctx->block->instructions.emplace_back(std::move(vec));
8692
8693
8694 if (instr->op == nir_texop_txf ||
8695 instr->op == nir_texop_txf_ms ||
8696 instr->op == nir_texop_samples_identical ||
8697 instr->op == nir_texop_fragment_fetch ||
8698 instr->op == nir_texop_fragment_mask_fetch) {
8699 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;
8700 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8701 tex->operands[0] = Operand(resource);
8702 tex->operands[1] = Operand(s4); /* no sampler */
8703 tex->operands[2] = Operand(arg);
8704 tex->dim = dim;
8705 tex->dmask = dmask;
8706 tex->unrm = true;
8707 tex->da = da;
8708 tex->definitions[0] = Definition(tmp_dst);
8709 tex->can_reorder = true;
8710 ctx->block->instructions.emplace_back(std::move(tex));
8711
8712 if (instr->op == nir_texop_samples_identical) {
8713 assert(dmask == 1 && dst.regClass() == v1);
8714 assert(dst.id() != tmp_dst.id());
8715
8716 Temp tmp = bld.tmp(bld.lm);
8717 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8718 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8719
8720 } else {
8721 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8722 }
8723 return;
8724 }
8725
8726 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8727 aco_opcode opcode = aco_opcode::image_sample;
8728 if (has_offset) { /* image_sample_*_o */
8729 if (has_compare) {
8730 opcode = aco_opcode::image_sample_c_o;
8731 if (has_derivs)
8732 opcode = aco_opcode::image_sample_c_d_o;
8733 if (has_bias)
8734 opcode = aco_opcode::image_sample_c_b_o;
8735 if (level_zero)
8736 opcode = aco_opcode::image_sample_c_lz_o;
8737 if (has_lod)
8738 opcode = aco_opcode::image_sample_c_l_o;
8739 } else {
8740 opcode = aco_opcode::image_sample_o;
8741 if (has_derivs)
8742 opcode = aco_opcode::image_sample_d_o;
8743 if (has_bias)
8744 opcode = aco_opcode::image_sample_b_o;
8745 if (level_zero)
8746 opcode = aco_opcode::image_sample_lz_o;
8747 if (has_lod)
8748 opcode = aco_opcode::image_sample_l_o;
8749 }
8750 } else { /* no offset */
8751 if (has_compare) {
8752 opcode = aco_opcode::image_sample_c;
8753 if (has_derivs)
8754 opcode = aco_opcode::image_sample_c_d;
8755 if (has_bias)
8756 opcode = aco_opcode::image_sample_c_b;
8757 if (level_zero)
8758 opcode = aco_opcode::image_sample_c_lz;
8759 if (has_lod)
8760 opcode = aco_opcode::image_sample_c_l;
8761 } else {
8762 opcode = aco_opcode::image_sample;
8763 if (has_derivs)
8764 opcode = aco_opcode::image_sample_d;
8765 if (has_bias)
8766 opcode = aco_opcode::image_sample_b;
8767 if (level_zero)
8768 opcode = aco_opcode::image_sample_lz;
8769 if (has_lod)
8770 opcode = aco_opcode::image_sample_l;
8771 }
8772 }
8773
8774 if (instr->op == nir_texop_tg4) {
8775 if (has_offset) {
8776 opcode = aco_opcode::image_gather4_lz_o;
8777 if (has_compare)
8778 opcode = aco_opcode::image_gather4_c_lz_o;
8779 } else {
8780 opcode = aco_opcode::image_gather4_lz;
8781 if (has_compare)
8782 opcode = aco_opcode::image_gather4_c_lz;
8783 }
8784 } else if (instr->op == nir_texop_lod) {
8785 opcode = aco_opcode::image_get_lod;
8786 }
8787
8788 /* we don't need the bias, sample index, compare value or offset to be
8789 * computed in WQM but if the p_create_vector copies the coordinates, then it
8790 * needs to be in WQM */
8791 if (ctx->stage == fragment_fs &&
8792 !has_derivs && !has_lod && !level_zero &&
8793 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8794 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8795 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8796
8797 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8798 tex->operands[0] = Operand(resource);
8799 tex->operands[1] = Operand(sampler);
8800 tex->operands[2] = Operand(arg);
8801 tex->dim = dim;
8802 tex->dmask = dmask;
8803 tex->da = da;
8804 tex->definitions[0] = Definition(tmp_dst);
8805 tex->can_reorder = true;
8806 ctx->block->instructions.emplace_back(std::move(tex));
8807
8808 if (tg4_integer_cube_workaround) {
8809 assert(tmp_dst.id() != dst.id());
8810 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8811
8812 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8813 Temp val[4];
8814 for (unsigned i = 0; i < dst.size(); i++) {
8815 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8816 Temp cvt_val;
8817 if (stype == GLSL_TYPE_UINT)
8818 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8819 else
8820 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8821 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8822 }
8823 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8824 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8825 val[0], val[1], val[2], val[3]);
8826 }
8827 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8828 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8829
8830 }
8831
8832
8833 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8834 {
8835 Temp tmp = get_ssa_temp(ctx, ssa);
8836 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8837 return Operand(tmp.regClass());
8838 else
8839 return Operand(tmp);
8840 }
8841
8842 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8843 {
8844 aco_ptr<Pseudo_instruction> phi;
8845 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8846 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8847
8848 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8849 logical |= ctx->block->kind & block_kind_merge;
8850 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8851
8852 /* we want a sorted list of sources, since the predecessor list is also sorted */
8853 std::map<unsigned, nir_ssa_def*> phi_src;
8854 nir_foreach_phi_src(src, instr)
8855 phi_src[src->pred->index] = src->src.ssa;
8856
8857 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8858 unsigned num_operands = 0;
8859 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8860 unsigned num_defined = 0;
8861 unsigned cur_pred_idx = 0;
8862 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8863 if (cur_pred_idx < preds.size()) {
8864 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8865 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8866 unsigned skipped = 0;
8867 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8868 skipped++;
8869 if (cur_pred_idx + skipped < preds.size()) {
8870 for (unsigned i = 0; i < skipped; i++)
8871 operands[num_operands++] = Operand(dst.regClass());
8872 cur_pred_idx += skipped;
8873 } else {
8874 continue;
8875 }
8876 }
8877 /* Handle missing predecessors at the end. This shouldn't happen with loop
8878 * headers and we can't ignore these sources for loop header phis. */
8879 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8880 continue;
8881 cur_pred_idx++;
8882 Operand op = get_phi_operand(ctx, src.second);
8883 operands[num_operands++] = op;
8884 num_defined += !op.isUndefined();
8885 }
8886 /* handle block_kind_continue_or_break at loop exit blocks */
8887 while (cur_pred_idx++ < preds.size())
8888 operands[num_operands++] = Operand(dst.regClass());
8889
8890 /* If the loop ends with a break, still add a linear continue edge in case
8891 * that break is divergent or continue_or_break is used. We'll either remove
8892 * this operand later in visit_loop() if it's not necessary or replace the
8893 * undef with something correct. */
8894 if (!logical && ctx->block->kind & block_kind_loop_header) {
8895 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8896 nir_block *last = nir_loop_last_block(loop);
8897 if (last->successors[0] != instr->instr.block)
8898 operands[num_operands++] = Operand(RegClass());
8899 }
8900
8901 if (num_defined == 0) {
8902 Builder bld(ctx->program, ctx->block);
8903 if (dst.regClass() == s1) {
8904 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8905 } else if (dst.regClass() == v1) {
8906 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8907 } else {
8908 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8909 for (unsigned i = 0; i < dst.size(); i++)
8910 vec->operands[i] = Operand(0u);
8911 vec->definitions[0] = Definition(dst);
8912 ctx->block->instructions.emplace_back(std::move(vec));
8913 }
8914 return;
8915 }
8916
8917 /* we can use a linear phi in some cases if one src is undef */
8918 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8919 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8920
8921 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8922 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8923 assert(invert->kind & block_kind_invert);
8924
8925 unsigned then_block = invert->linear_preds[0];
8926
8927 Block* insert_block = NULL;
8928 for (unsigned i = 0; i < num_operands; i++) {
8929 Operand op = operands[i];
8930 if (op.isUndefined())
8931 continue;
8932 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8933 phi->operands[0] = op;
8934 break;
8935 }
8936 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8937 phi->operands[1] = Operand(dst.regClass());
8938 phi->definitions[0] = Definition(dst);
8939 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8940 return;
8941 }
8942
8943 /* try to scalarize vector phis */
8944 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8945 // TODO: scalarize linear phis on divergent ifs
8946 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8947 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8948 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8949 Operand src = operands[i];
8950 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8951 can_scalarize = false;
8952 }
8953 if (can_scalarize) {
8954 unsigned num_components = instr->dest.ssa.num_components;
8955 assert(dst.size() % num_components == 0);
8956 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8957
8958 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8959 for (unsigned k = 0; k < num_components; k++) {
8960 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8961 for (unsigned i = 0; i < num_operands; i++) {
8962 Operand src = operands[i];
8963 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8964 }
8965 Temp phi_dst = {ctx->program->allocateId(), rc};
8966 phi->definitions[0] = Definition(phi_dst);
8967 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8968 new_vec[k] = phi_dst;
8969 vec->operands[k] = Operand(phi_dst);
8970 }
8971 vec->definitions[0] = Definition(dst);
8972 ctx->block->instructions.emplace_back(std::move(vec));
8973 ctx->allocated_vec.emplace(dst.id(), new_vec);
8974 return;
8975 }
8976 }
8977
8978 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8979 for (unsigned i = 0; i < num_operands; i++)
8980 phi->operands[i] = operands[i];
8981 phi->definitions[0] = Definition(dst);
8982 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8983 }
8984
8985
8986 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8987 {
8988 Temp dst = get_ssa_temp(ctx, &instr->def);
8989
8990 assert(dst.type() == RegType::sgpr);
8991
8992 if (dst.size() == 1) {
8993 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8994 } else {
8995 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8996 for (unsigned i = 0; i < dst.size(); i++)
8997 vec->operands[i] = Operand(0u);
8998 vec->definitions[0] = Definition(dst);
8999 ctx->block->instructions.emplace_back(std::move(vec));
9000 }
9001 }
9002
9003 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9004 {
9005 Builder bld(ctx->program, ctx->block);
9006 Block *logical_target;
9007 append_logical_end(ctx->block);
9008 unsigned idx = ctx->block->index;
9009
9010 switch (instr->type) {
9011 case nir_jump_break:
9012 logical_target = ctx->cf_info.parent_loop.exit;
9013 add_logical_edge(idx, logical_target);
9014 ctx->block->kind |= block_kind_break;
9015
9016 if (!ctx->cf_info.parent_if.is_divergent &&
9017 !ctx->cf_info.parent_loop.has_divergent_continue) {
9018 /* uniform break - directly jump out of the loop */
9019 ctx->block->kind |= block_kind_uniform;
9020 ctx->cf_info.has_branch = true;
9021 bld.branch(aco_opcode::p_branch);
9022 add_linear_edge(idx, logical_target);
9023 return;
9024 }
9025 ctx->cf_info.parent_loop.has_divergent_branch = true;
9026 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9027 break;
9028 case nir_jump_continue:
9029 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9030 add_logical_edge(idx, logical_target);
9031 ctx->block->kind |= block_kind_continue;
9032
9033 if (ctx->cf_info.parent_if.is_divergent) {
9034 /* for potential uniform breaks after this continue,
9035 we must ensure that they are handled correctly */
9036 ctx->cf_info.parent_loop.has_divergent_continue = true;
9037 ctx->cf_info.parent_loop.has_divergent_branch = true;
9038 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9039 } else {
9040 /* uniform continue - directly jump to the loop header */
9041 ctx->block->kind |= block_kind_uniform;
9042 ctx->cf_info.has_branch = true;
9043 bld.branch(aco_opcode::p_branch);
9044 add_linear_edge(idx, logical_target);
9045 return;
9046 }
9047 break;
9048 default:
9049 fprintf(stderr, "Unknown NIR jump instr: ");
9050 nir_print_instr(&instr->instr, stderr);
9051 fprintf(stderr, "\n");
9052 abort();
9053 }
9054
9055 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9056 ctx->cf_info.exec_potentially_empty_break = true;
9057 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9058 }
9059
9060 /* remove critical edges from linear CFG */
9061 bld.branch(aco_opcode::p_branch);
9062 Block* break_block = ctx->program->create_and_insert_block();
9063 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9064 break_block->kind |= block_kind_uniform;
9065 add_linear_edge(idx, break_block);
9066 /* the loop_header pointer might be invalidated by this point */
9067 if (instr->type == nir_jump_continue)
9068 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9069 add_linear_edge(break_block->index, logical_target);
9070 bld.reset(break_block);
9071 bld.branch(aco_opcode::p_branch);
9072
9073 Block* continue_block = ctx->program->create_and_insert_block();
9074 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9075 add_linear_edge(idx, continue_block);
9076 append_logical_start(continue_block);
9077 ctx->block = continue_block;
9078 return;
9079 }
9080
9081 void visit_block(isel_context *ctx, nir_block *block)
9082 {
9083 nir_foreach_instr(instr, block) {
9084 switch (instr->type) {
9085 case nir_instr_type_alu:
9086 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9087 break;
9088 case nir_instr_type_load_const:
9089 visit_load_const(ctx, nir_instr_as_load_const(instr));
9090 break;
9091 case nir_instr_type_intrinsic:
9092 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9093 break;
9094 case nir_instr_type_tex:
9095 visit_tex(ctx, nir_instr_as_tex(instr));
9096 break;
9097 case nir_instr_type_phi:
9098 visit_phi(ctx, nir_instr_as_phi(instr));
9099 break;
9100 case nir_instr_type_ssa_undef:
9101 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9102 break;
9103 case nir_instr_type_deref:
9104 break;
9105 case nir_instr_type_jump:
9106 visit_jump(ctx, nir_instr_as_jump(instr));
9107 break;
9108 default:
9109 fprintf(stderr, "Unknown NIR instr type: ");
9110 nir_print_instr(instr, stderr);
9111 fprintf(stderr, "\n");
9112 //abort();
9113 }
9114 }
9115
9116 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9117 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9118 }
9119
9120
9121
9122 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9123 aco_ptr<Instruction>& header_phi, Operand *vals)
9124 {
9125 vals[0] = Operand(header_phi->definitions[0].getTemp());
9126 RegClass rc = vals[0].regClass();
9127
9128 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9129
9130 unsigned next_pred = 1;
9131
9132 for (unsigned idx = first + 1; idx <= last; idx++) {
9133 Block& block = ctx->program->blocks[idx];
9134 if (block.loop_nest_depth != loop_nest_depth) {
9135 vals[idx - first] = vals[idx - 1 - first];
9136 continue;
9137 }
9138
9139 if (block.kind & block_kind_continue) {
9140 vals[idx - first] = header_phi->operands[next_pred];
9141 next_pred++;
9142 continue;
9143 }
9144
9145 bool all_same = true;
9146 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9147 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9148
9149 Operand val;
9150 if (all_same) {
9151 val = vals[block.linear_preds[0] - first];
9152 } else {
9153 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9154 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9155 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9156 phi->operands[i] = vals[block.linear_preds[i] - first];
9157 val = Operand(Temp(ctx->program->allocateId(), rc));
9158 phi->definitions[0] = Definition(val.getTemp());
9159 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9160 }
9161 vals[idx - first] = val;
9162 }
9163
9164 return vals[last - first];
9165 }
9166
9167 static void visit_loop(isel_context *ctx, nir_loop *loop)
9168 {
9169 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9170 append_logical_end(ctx->block);
9171 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9172 Builder bld(ctx->program, ctx->block);
9173 bld.branch(aco_opcode::p_branch);
9174 unsigned loop_preheader_idx = ctx->block->index;
9175
9176 Block loop_exit = Block();
9177 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9178 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9179
9180 Block* loop_header = ctx->program->create_and_insert_block();
9181 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9182 loop_header->kind |= block_kind_loop_header;
9183 add_edge(loop_preheader_idx, loop_header);
9184 ctx->block = loop_header;
9185
9186 /* emit loop body */
9187 unsigned loop_header_idx = loop_header->index;
9188 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9189 append_logical_start(ctx->block);
9190 bool unreachable = visit_cf_list(ctx, &loop->body);
9191
9192 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9193 if (!ctx->cf_info.has_branch) {
9194 append_logical_end(ctx->block);
9195 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9196 /* Discards can result in code running with an empty exec mask.
9197 * This would result in divergent breaks not ever being taken. As a
9198 * workaround, break the loop when the loop mask is empty instead of
9199 * always continuing. */
9200 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9201 unsigned block_idx = ctx->block->index;
9202
9203 /* create helper blocks to avoid critical edges */
9204 Block *break_block = ctx->program->create_and_insert_block();
9205 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9206 break_block->kind = block_kind_uniform;
9207 bld.reset(break_block);
9208 bld.branch(aco_opcode::p_branch);
9209 add_linear_edge(block_idx, break_block);
9210 add_linear_edge(break_block->index, &loop_exit);
9211
9212 Block *continue_block = ctx->program->create_and_insert_block();
9213 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9214 continue_block->kind = block_kind_uniform;
9215 bld.reset(continue_block);
9216 bld.branch(aco_opcode::p_branch);
9217 add_linear_edge(block_idx, continue_block);
9218 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9219
9220 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9221 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9222 ctx->block = &ctx->program->blocks[block_idx];
9223 } else {
9224 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9225 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9226 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9227 else
9228 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9229 }
9230
9231 bld.reset(ctx->block);
9232 bld.branch(aco_opcode::p_branch);
9233 }
9234
9235 /* Fixup phis in loop header from unreachable blocks.
9236 * has_branch/has_divergent_branch also indicates if the loop ends with a
9237 * break/continue instruction, but we don't emit those if unreachable=true */
9238 if (unreachable) {
9239 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9240 bool linear = ctx->cf_info.has_branch;
9241 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9242 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9243 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9244 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9245 /* the last operand should be the one that needs to be removed */
9246 instr->operands.pop_back();
9247 } else if (!is_phi(instr)) {
9248 break;
9249 }
9250 }
9251 }
9252
9253 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9254 * and the previous one shouldn't both happen at once because a break in the
9255 * merge block would get CSE'd */
9256 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9257 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9258 Operand vals[num_vals];
9259 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9260 if (instr->opcode == aco_opcode::p_linear_phi) {
9261 if (ctx->cf_info.has_branch)
9262 instr->operands.pop_back();
9263 else
9264 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9265 } else if (!is_phi(instr)) {
9266 break;
9267 }
9268 }
9269 }
9270
9271 ctx->cf_info.has_branch = false;
9272
9273 // TODO: if the loop has not a single exit, we must add one °°
9274 /* emit loop successor block */
9275 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9276 append_logical_start(ctx->block);
9277
9278 #if 0
9279 // TODO: check if it is beneficial to not branch on continues
9280 /* trim linear phis in loop header */
9281 for (auto&& instr : loop_entry->instructions) {
9282 if (instr->opcode == aco_opcode::p_linear_phi) {
9283 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9284 new_phi->definitions[0] = instr->definitions[0];
9285 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9286 new_phi->operands[i] = instr->operands[i];
9287 /* check that the remaining operands are all the same */
9288 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9289 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9290 instr.swap(new_phi);
9291 } else if (instr->opcode == aco_opcode::p_phi) {
9292 continue;
9293 } else {
9294 break;
9295 }
9296 }
9297 #endif
9298 }
9299
9300 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9301 {
9302 ic->cond = cond;
9303
9304 append_logical_end(ctx->block);
9305 ctx->block->kind |= block_kind_branch;
9306
9307 /* branch to linear then block */
9308 assert(cond.regClass() == ctx->program->lane_mask);
9309 aco_ptr<Pseudo_branch_instruction> branch;
9310 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9311 branch->operands[0] = Operand(cond);
9312 ctx->block->instructions.push_back(std::move(branch));
9313
9314 ic->BB_if_idx = ctx->block->index;
9315 ic->BB_invert = Block();
9316 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9317 /* Invert blocks are intentionally not marked as top level because they
9318 * are not part of the logical cfg. */
9319 ic->BB_invert.kind |= block_kind_invert;
9320 ic->BB_endif = Block();
9321 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9322 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9323
9324 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9325 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9326 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9327 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9328 ctx->cf_info.parent_if.is_divergent = true;
9329
9330 /* divergent branches use cbranch_execz */
9331 ctx->cf_info.exec_potentially_empty_discard = false;
9332 ctx->cf_info.exec_potentially_empty_break = false;
9333 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9334
9335 /** emit logical then block */
9336 Block* BB_then_logical = ctx->program->create_and_insert_block();
9337 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9338 add_edge(ic->BB_if_idx, BB_then_logical);
9339 ctx->block = BB_then_logical;
9340 append_logical_start(BB_then_logical);
9341 }
9342
9343 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9344 {
9345 Block *BB_then_logical = ctx->block;
9346 append_logical_end(BB_then_logical);
9347 /* branch from logical then block to invert block */
9348 aco_ptr<Pseudo_branch_instruction> branch;
9349 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9350 BB_then_logical->instructions.emplace_back(std::move(branch));
9351 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9352 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9353 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9354 BB_then_logical->kind |= block_kind_uniform;
9355 assert(!ctx->cf_info.has_branch);
9356 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9357 ctx->cf_info.parent_loop.has_divergent_branch = false;
9358
9359 /** emit linear then block */
9360 Block* BB_then_linear = ctx->program->create_and_insert_block();
9361 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9362 BB_then_linear->kind |= block_kind_uniform;
9363 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9364 /* branch from linear then block to invert block */
9365 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9366 BB_then_linear->instructions.emplace_back(std::move(branch));
9367 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9368
9369 /** emit invert merge block */
9370 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9371 ic->invert_idx = ctx->block->index;
9372
9373 /* branch to linear else block (skip else) */
9374 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9375 branch->operands[0] = Operand(ic->cond);
9376 ctx->block->instructions.push_back(std::move(branch));
9377
9378 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9379 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9380 ic->exec_potentially_empty_break_depth_old =
9381 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9382 /* divergent branches use cbranch_execz */
9383 ctx->cf_info.exec_potentially_empty_discard = false;
9384 ctx->cf_info.exec_potentially_empty_break = false;
9385 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9386
9387 /** emit logical else block */
9388 Block* BB_else_logical = ctx->program->create_and_insert_block();
9389 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9390 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9391 add_linear_edge(ic->invert_idx, BB_else_logical);
9392 ctx->block = BB_else_logical;
9393 append_logical_start(BB_else_logical);
9394 }
9395
9396 static void end_divergent_if(isel_context *ctx, if_context *ic)
9397 {
9398 Block *BB_else_logical = ctx->block;
9399 append_logical_end(BB_else_logical);
9400
9401 /* branch from logical else block to endif block */
9402 aco_ptr<Pseudo_branch_instruction> branch;
9403 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9404 BB_else_logical->instructions.emplace_back(std::move(branch));
9405 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9406 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9407 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9408 BB_else_logical->kind |= block_kind_uniform;
9409
9410 assert(!ctx->cf_info.has_branch);
9411 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9412
9413
9414 /** emit linear else block */
9415 Block* BB_else_linear = ctx->program->create_and_insert_block();
9416 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9417 BB_else_linear->kind |= block_kind_uniform;
9418 add_linear_edge(ic->invert_idx, BB_else_linear);
9419
9420 /* branch from linear else block to endif block */
9421 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9422 BB_else_linear->instructions.emplace_back(std::move(branch));
9423 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9424
9425
9426 /** emit endif merge block */
9427 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9428 append_logical_start(ctx->block);
9429
9430
9431 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9432 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9433 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9434 ctx->cf_info.exec_potentially_empty_break_depth =
9435 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9436 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9437 !ctx->cf_info.parent_if.is_divergent) {
9438 ctx->cf_info.exec_potentially_empty_break = false;
9439 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9440 }
9441 /* uniform control flow never has an empty exec-mask */
9442 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9443 ctx->cf_info.exec_potentially_empty_discard = false;
9444 ctx->cf_info.exec_potentially_empty_break = false;
9445 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9446 }
9447 }
9448
9449 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9450 {
9451 assert(cond.regClass() == s1);
9452
9453 append_logical_end(ctx->block);
9454 ctx->block->kind |= block_kind_uniform;
9455
9456 aco_ptr<Pseudo_branch_instruction> branch;
9457 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9458 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9459 branch->operands[0] = Operand(cond);
9460 branch->operands[0].setFixed(scc);
9461 ctx->block->instructions.emplace_back(std::move(branch));
9462
9463 ic->BB_if_idx = ctx->block->index;
9464 ic->BB_endif = Block();
9465 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9466 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9467
9468 ctx->cf_info.has_branch = false;
9469 ctx->cf_info.parent_loop.has_divergent_branch = false;
9470
9471 /** emit then block */
9472 Block* BB_then = ctx->program->create_and_insert_block();
9473 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9474 add_edge(ic->BB_if_idx, BB_then);
9475 append_logical_start(BB_then);
9476 ctx->block = BB_then;
9477 }
9478
9479 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9480 {
9481 Block *BB_then = ctx->block;
9482
9483 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9484 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9485
9486 if (!ic->uniform_has_then_branch) {
9487 append_logical_end(BB_then);
9488 /* branch from then block to endif block */
9489 aco_ptr<Pseudo_branch_instruction> branch;
9490 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9491 BB_then->instructions.emplace_back(std::move(branch));
9492 add_linear_edge(BB_then->index, &ic->BB_endif);
9493 if (!ic->then_branch_divergent)
9494 add_logical_edge(BB_then->index, &ic->BB_endif);
9495 BB_then->kind |= block_kind_uniform;
9496 }
9497
9498 ctx->cf_info.has_branch = false;
9499 ctx->cf_info.parent_loop.has_divergent_branch = false;
9500
9501 /** emit else block */
9502 Block* BB_else = ctx->program->create_and_insert_block();
9503 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9504 add_edge(ic->BB_if_idx, BB_else);
9505 append_logical_start(BB_else);
9506 ctx->block = BB_else;
9507 }
9508
9509 static void end_uniform_if(isel_context *ctx, if_context *ic)
9510 {
9511 Block *BB_else = ctx->block;
9512
9513 if (!ctx->cf_info.has_branch) {
9514 append_logical_end(BB_else);
9515 /* branch from then block to endif block */
9516 aco_ptr<Pseudo_branch_instruction> branch;
9517 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9518 BB_else->instructions.emplace_back(std::move(branch));
9519 add_linear_edge(BB_else->index, &ic->BB_endif);
9520 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9521 add_logical_edge(BB_else->index, &ic->BB_endif);
9522 BB_else->kind |= block_kind_uniform;
9523 }
9524
9525 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9526 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9527
9528 /** emit endif merge block */
9529 if (!ctx->cf_info.has_branch) {
9530 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9531 append_logical_start(ctx->block);
9532 }
9533 }
9534
9535 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9536 {
9537 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9538 Builder bld(ctx->program, ctx->block);
9539 aco_ptr<Pseudo_branch_instruction> branch;
9540 if_context ic;
9541
9542 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9543 /**
9544 * Uniform conditionals are represented in the following way*) :
9545 *
9546 * The linear and logical CFG:
9547 * BB_IF
9548 * / \
9549 * BB_THEN (logical) BB_ELSE (logical)
9550 * \ /
9551 * BB_ENDIF
9552 *
9553 * *) Exceptions may be due to break and continue statements within loops
9554 * If a break/continue happens within uniform control flow, it branches
9555 * to the loop exit/entry block. Otherwise, it branches to the next
9556 * merge block.
9557 **/
9558
9559 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9560 assert(cond.regClass() == ctx->program->lane_mask);
9561 cond = bool_to_scalar_condition(ctx, cond);
9562
9563 begin_uniform_if_then(ctx, &ic, cond);
9564 visit_cf_list(ctx, &if_stmt->then_list);
9565
9566 begin_uniform_if_else(ctx, &ic);
9567 visit_cf_list(ctx, &if_stmt->else_list);
9568
9569 end_uniform_if(ctx, &ic);
9570
9571 return !ctx->cf_info.has_branch;
9572 } else { /* non-uniform condition */
9573 /**
9574 * To maintain a logical and linear CFG without critical edges,
9575 * non-uniform conditionals are represented in the following way*) :
9576 *
9577 * The linear CFG:
9578 * BB_IF
9579 * / \
9580 * BB_THEN (logical) BB_THEN (linear)
9581 * \ /
9582 * BB_INVERT (linear)
9583 * / \
9584 * BB_ELSE (logical) BB_ELSE (linear)
9585 * \ /
9586 * BB_ENDIF
9587 *
9588 * The logical CFG:
9589 * BB_IF
9590 * / \
9591 * BB_THEN (logical) BB_ELSE (logical)
9592 * \ /
9593 * BB_ENDIF
9594 *
9595 * *) Exceptions may be due to break and continue statements within loops
9596 **/
9597
9598 begin_divergent_if_then(ctx, &ic, cond);
9599 visit_cf_list(ctx, &if_stmt->then_list);
9600
9601 begin_divergent_if_else(ctx, &ic);
9602 visit_cf_list(ctx, &if_stmt->else_list);
9603
9604 end_divergent_if(ctx, &ic);
9605
9606 return true;
9607 }
9608 }
9609
9610 static bool visit_cf_list(isel_context *ctx,
9611 struct exec_list *list)
9612 {
9613 foreach_list_typed(nir_cf_node, node, node, list) {
9614 switch (node->type) {
9615 case nir_cf_node_block:
9616 visit_block(ctx, nir_cf_node_as_block(node));
9617 break;
9618 case nir_cf_node_if:
9619 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9620 return true;
9621 break;
9622 case nir_cf_node_loop:
9623 visit_loop(ctx, nir_cf_node_as_loop(node));
9624 break;
9625 default:
9626 unreachable("unimplemented cf list type");
9627 }
9628 }
9629 return false;
9630 }
9631
9632 static void create_null_export(isel_context *ctx)
9633 {
9634 /* Some shader stages always need to have exports.
9635 * So when there is none, we need to add a null export.
9636 */
9637
9638 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9639 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9640 Builder bld(ctx->program, ctx->block);
9641 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9642 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9643 }
9644
9645 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9646 {
9647 assert(ctx->stage == vertex_vs ||
9648 ctx->stage == tess_eval_vs ||
9649 ctx->stage == gs_copy_vs ||
9650 ctx->stage == ngg_vertex_gs ||
9651 ctx->stage == ngg_tess_eval_gs);
9652
9653 int offset = (ctx->stage & sw_tes)
9654 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9655 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9656 uint64_t mask = ctx->outputs.mask[slot];
9657 if (!is_pos && !mask)
9658 return false;
9659 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9660 return false;
9661 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9662 exp->enabled_mask = mask;
9663 for (unsigned i = 0; i < 4; ++i) {
9664 if (mask & (1 << i))
9665 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9666 else
9667 exp->operands[i] = Operand(v1);
9668 }
9669 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9670 * Setting valid_mask=1 prevents it and has no other effect.
9671 */
9672 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9673 exp->done = false;
9674 exp->compressed = false;
9675 if (is_pos)
9676 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9677 else
9678 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9679 ctx->block->instructions.emplace_back(std::move(exp));
9680
9681 return true;
9682 }
9683
9684 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9685 {
9686 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9687 exp->enabled_mask = 0;
9688 for (unsigned i = 0; i < 4; ++i)
9689 exp->operands[i] = Operand(v1);
9690 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9691 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9692 exp->enabled_mask |= 0x1;
9693 }
9694 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9695 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9696 exp->enabled_mask |= 0x4;
9697 }
9698 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9699 if (ctx->options->chip_class < GFX9) {
9700 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9701 exp->enabled_mask |= 0x8;
9702 } else {
9703 Builder bld(ctx->program, ctx->block);
9704
9705 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9706 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9707 if (exp->operands[2].isTemp())
9708 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9709
9710 exp->operands[2] = Operand(out);
9711 exp->enabled_mask |= 0x4;
9712 }
9713 }
9714 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9715 exp->done = false;
9716 exp->compressed = false;
9717 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9718 ctx->block->instructions.emplace_back(std::move(exp));
9719 }
9720
9721 static void create_export_phis(isel_context *ctx)
9722 {
9723 /* Used when exports are needed, but the output temps are defined in a preceding block.
9724 * This function will set up phis in order to access the outputs in the next block.
9725 */
9726
9727 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9728 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9729 ctx->block->instructions.pop_back();
9730
9731 Builder bld(ctx->program, ctx->block);
9732
9733 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9734 uint64_t mask = ctx->outputs.mask[slot];
9735 for (unsigned i = 0; i < 4; ++i) {
9736 if (!(mask & (1 << i)))
9737 continue;
9738
9739 Temp old = ctx->outputs.temps[slot * 4 + i];
9740 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9741 ctx->outputs.temps[slot * 4 + i] = phi;
9742 }
9743 }
9744
9745 bld.insert(std::move(logical_start));
9746 }
9747
9748 static void create_vs_exports(isel_context *ctx)
9749 {
9750 assert(ctx->stage == vertex_vs ||
9751 ctx->stage == tess_eval_vs ||
9752 ctx->stage == gs_copy_vs ||
9753 ctx->stage == ngg_vertex_gs ||
9754 ctx->stage == ngg_tess_eval_gs);
9755
9756 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9757 ? &ctx->program->info->tes.outinfo
9758 : &ctx->program->info->vs.outinfo;
9759
9760 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9761 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9762 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9763 }
9764
9765 if (ctx->options->key.has_multiview_view_index) {
9766 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9767 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9768 }
9769
9770 /* the order these position exports are created is important */
9771 int next_pos = 0;
9772 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9773 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9774 export_vs_psiz_layer_viewport(ctx, &next_pos);
9775 exported_pos = true;
9776 }
9777 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9778 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9779 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9780 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9781
9782 if (ctx->export_clip_dists) {
9783 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9784 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9785 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9786 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9787 }
9788
9789 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9790 if (i < VARYING_SLOT_VAR0 &&
9791 i != VARYING_SLOT_LAYER &&
9792 i != VARYING_SLOT_PRIMITIVE_ID)
9793 continue;
9794
9795 export_vs_varying(ctx, i, false, NULL);
9796 }
9797
9798 if (!exported_pos)
9799 create_null_export(ctx);
9800 }
9801
9802 static bool export_fs_mrt_z(isel_context *ctx)
9803 {
9804 Builder bld(ctx->program, ctx->block);
9805 unsigned enabled_channels = 0;
9806 bool compr = false;
9807 Operand values[4];
9808
9809 for (unsigned i = 0; i < 4; ++i) {
9810 values[i] = Operand(v1);
9811 }
9812
9813 /* Both stencil and sample mask only need 16-bits. */
9814 if (!ctx->program->info->ps.writes_z &&
9815 (ctx->program->info->ps.writes_stencil ||
9816 ctx->program->info->ps.writes_sample_mask)) {
9817 compr = true; /* COMPR flag */
9818
9819 if (ctx->program->info->ps.writes_stencil) {
9820 /* Stencil should be in X[23:16]. */
9821 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9822 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9823 enabled_channels |= 0x3;
9824 }
9825
9826 if (ctx->program->info->ps.writes_sample_mask) {
9827 /* SampleMask should be in Y[15:0]. */
9828 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9829 enabled_channels |= 0xc;
9830 }
9831 } else {
9832 if (ctx->program->info->ps.writes_z) {
9833 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9834 enabled_channels |= 0x1;
9835 }
9836
9837 if (ctx->program->info->ps.writes_stencil) {
9838 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9839 enabled_channels |= 0x2;
9840 }
9841
9842 if (ctx->program->info->ps.writes_sample_mask) {
9843 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9844 enabled_channels |= 0x4;
9845 }
9846 }
9847
9848 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9849 * writemask component.
9850 */
9851 if (ctx->options->chip_class == GFX6 &&
9852 ctx->options->family != CHIP_OLAND &&
9853 ctx->options->family != CHIP_HAINAN) {
9854 enabled_channels |= 0x1;
9855 }
9856
9857 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9858 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9859
9860 return true;
9861 }
9862
9863 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9864 {
9865 Builder bld(ctx->program, ctx->block);
9866 unsigned write_mask = ctx->outputs.mask[slot];
9867 Operand values[4];
9868
9869 for (unsigned i = 0; i < 4; ++i) {
9870 if (write_mask & (1 << i)) {
9871 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9872 } else {
9873 values[i] = Operand(v1);
9874 }
9875 }
9876
9877 unsigned target, col_format;
9878 unsigned enabled_channels = 0;
9879 aco_opcode compr_op = (aco_opcode)0;
9880
9881 slot -= FRAG_RESULT_DATA0;
9882 target = V_008DFC_SQ_EXP_MRT + slot;
9883 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9884
9885 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9886 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9887
9888 switch (col_format)
9889 {
9890 case V_028714_SPI_SHADER_ZERO:
9891 enabled_channels = 0; /* writemask */
9892 target = V_008DFC_SQ_EXP_NULL;
9893 break;
9894
9895 case V_028714_SPI_SHADER_32_R:
9896 enabled_channels = 1;
9897 break;
9898
9899 case V_028714_SPI_SHADER_32_GR:
9900 enabled_channels = 0x3;
9901 break;
9902
9903 case V_028714_SPI_SHADER_32_AR:
9904 if (ctx->options->chip_class >= GFX10) {
9905 /* Special case: on GFX10, the outputs are different for 32_AR */
9906 enabled_channels = 0x3;
9907 values[1] = values[3];
9908 values[3] = Operand(v1);
9909 } else {
9910 enabled_channels = 0x9;
9911 }
9912 break;
9913
9914 case V_028714_SPI_SHADER_FP16_ABGR:
9915 enabled_channels = 0x5;
9916 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9917 break;
9918
9919 case V_028714_SPI_SHADER_UNORM16_ABGR:
9920 enabled_channels = 0x5;
9921 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9922 break;
9923
9924 case V_028714_SPI_SHADER_SNORM16_ABGR:
9925 enabled_channels = 0x5;
9926 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9927 break;
9928
9929 case V_028714_SPI_SHADER_UINT16_ABGR: {
9930 enabled_channels = 0x5;
9931 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9932 if (is_int8 || is_int10) {
9933 /* clamp */
9934 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9935 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9936
9937 for (unsigned i = 0; i < 4; i++) {
9938 if ((write_mask >> i) & 1) {
9939 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9940 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9941 values[i]);
9942 }
9943 }
9944 }
9945 break;
9946 }
9947
9948 case V_028714_SPI_SHADER_SINT16_ABGR:
9949 enabled_channels = 0x5;
9950 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9951 if (is_int8 || is_int10) {
9952 /* clamp */
9953 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9954 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9955 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9956 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9957
9958 for (unsigned i = 0; i < 4; i++) {
9959 if ((write_mask >> i) & 1) {
9960 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9961 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9962 values[i]);
9963 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9964 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9965 values[i]);
9966 }
9967 }
9968 }
9969 break;
9970
9971 case V_028714_SPI_SHADER_32_ABGR:
9972 enabled_channels = 0xF;
9973 break;
9974
9975 default:
9976 break;
9977 }
9978
9979 if (target == V_008DFC_SQ_EXP_NULL)
9980 return false;
9981
9982 if ((bool) compr_op) {
9983 for (int i = 0; i < 2; i++) {
9984 /* check if at least one of the values to be compressed is enabled */
9985 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9986 if (enabled) {
9987 enabled_channels |= enabled << (i*2);
9988 values[i] = bld.vop3(compr_op, bld.def(v1),
9989 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9990 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9991 } else {
9992 values[i] = Operand(v1);
9993 }
9994 }
9995 values[2] = Operand(v1);
9996 values[3] = Operand(v1);
9997 } else {
9998 for (int i = 0; i < 4; i++)
9999 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10000 }
10001
10002 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10003 enabled_channels, target, (bool) compr_op);
10004 return true;
10005 }
10006
10007 static void create_fs_exports(isel_context *ctx)
10008 {
10009 bool exported = false;
10010
10011 /* Export depth, stencil and sample mask. */
10012 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10013 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10014 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10015 exported |= export_fs_mrt_z(ctx);
10016
10017 /* Export all color render targets. */
10018 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10019 if (ctx->outputs.mask[i])
10020 exported |= export_fs_mrt_color(ctx, i);
10021
10022 if (!exported)
10023 create_null_export(ctx);
10024 }
10025
10026 static void write_tcs_tess_factors(isel_context *ctx)
10027 {
10028 unsigned outer_comps;
10029 unsigned inner_comps;
10030
10031 switch (ctx->args->options->key.tcs.primitive_mode) {
10032 case GL_ISOLINES:
10033 outer_comps = 2;
10034 inner_comps = 0;
10035 break;
10036 case GL_TRIANGLES:
10037 outer_comps = 3;
10038 inner_comps = 1;
10039 break;
10040 case GL_QUADS:
10041 outer_comps = 4;
10042 inner_comps = 2;
10043 break;
10044 default:
10045 return;
10046 }
10047
10048 Builder bld(ctx->program, ctx->block);
10049
10050 bld.barrier(aco_opcode::p_memory_barrier_shared);
10051 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10052 bld.sopp(aco_opcode::s_barrier);
10053
10054 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10055 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10056
10057 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10058 if_context ic_invocation_id_is_zero;
10059 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10060 bld.reset(ctx->block);
10061
10062 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));
10063
10064 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10065 unsigned stride = inner_comps + outer_comps;
10066 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10067 Temp tf_inner_vec;
10068 Temp tf_outer_vec;
10069 Temp out[6];
10070 assert(stride <= (sizeof(out) / sizeof(Temp)));
10071
10072 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10073 // LINES reversal
10074 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10075 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10076 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10077 } else {
10078 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);
10079 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);
10080
10081 for (unsigned i = 0; i < outer_comps; ++i)
10082 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10083 for (unsigned i = 0; i < inner_comps; ++i)
10084 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10085 }
10086
10087 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10088 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10089 Temp byte_offset = bld.v_mul_imm(bld.def(v1), rel_patch_id, stride * 4u);
10090 unsigned tf_const_offset = 0;
10091
10092 if (ctx->program->chip_class <= GFX8) {
10093 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);
10094 if_context ic_rel_patch_id_is_zero;
10095 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10096 bld.reset(ctx->block);
10097
10098 /* Store the dynamic HS control word. */
10099 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10100 bld.mubuf(aco_opcode::buffer_store_dword,
10101 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10102 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10103 /* disable_wqm */ false, /* glc */ true);
10104 tf_const_offset += 4;
10105
10106 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10107 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10108 bld.reset(ctx->block);
10109 }
10110
10111 assert(stride == 2 || stride == 4 || stride == 6);
10112 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10113 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10114
10115 /* Store to offchip for TES to read - only if TES reads them */
10116 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10117 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));
10118 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10119
10120 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10121 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);
10122
10123 if (likely(inner_comps)) {
10124 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10125 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);
10126 }
10127 }
10128
10129 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10130 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10131 }
10132
10133 static void emit_stream_output(isel_context *ctx,
10134 Temp const *so_buffers,
10135 Temp const *so_write_offset,
10136 const struct radv_stream_output *output)
10137 {
10138 unsigned num_comps = util_bitcount(output->component_mask);
10139 unsigned writemask = (1 << num_comps) - 1;
10140 unsigned loc = output->location;
10141 unsigned buf = output->buffer;
10142
10143 assert(num_comps && num_comps <= 4);
10144 if (!num_comps || num_comps > 4)
10145 return;
10146
10147 unsigned start = ffs(output->component_mask) - 1;
10148
10149 Temp out[4];
10150 bool all_undef = true;
10151 assert(ctx->stage == vertex_vs || ctx->stage == gs_copy_vs);
10152 for (unsigned i = 0; i < num_comps; i++) {
10153 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10154 all_undef = all_undef && !out[i].id();
10155 }
10156 if (all_undef)
10157 return;
10158
10159 while (writemask) {
10160 int start, count;
10161 u_bit_scan_consecutive_range(&writemask, &start, &count);
10162 if (count == 3 && ctx->options->chip_class == GFX6) {
10163 /* GFX6 doesn't support storing vec3, split it. */
10164 writemask |= 1u << (start + 2);
10165 count = 2;
10166 }
10167
10168 unsigned offset = output->offset + start * 4;
10169
10170 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10171 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10172 for (int i = 0; i < count; ++i)
10173 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10174 vec->definitions[0] = Definition(write_data);
10175 ctx->block->instructions.emplace_back(std::move(vec));
10176
10177 aco_opcode opcode;
10178 switch (count) {
10179 case 1:
10180 opcode = aco_opcode::buffer_store_dword;
10181 break;
10182 case 2:
10183 opcode = aco_opcode::buffer_store_dwordx2;
10184 break;
10185 case 3:
10186 opcode = aco_opcode::buffer_store_dwordx3;
10187 break;
10188 case 4:
10189 opcode = aco_opcode::buffer_store_dwordx4;
10190 break;
10191 default:
10192 unreachable("Unsupported dword count.");
10193 }
10194
10195 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10196 store->operands[0] = Operand(so_buffers[buf]);
10197 store->operands[1] = Operand(so_write_offset[buf]);
10198 store->operands[2] = Operand((uint32_t) 0);
10199 store->operands[3] = Operand(write_data);
10200 if (offset > 4095) {
10201 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10202 Builder bld(ctx->program, ctx->block);
10203 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10204 } else {
10205 store->offset = offset;
10206 }
10207 store->offen = true;
10208 store->glc = true;
10209 store->dlc = false;
10210 store->slc = true;
10211 store->can_reorder = true;
10212 ctx->block->instructions.emplace_back(std::move(store));
10213 }
10214 }
10215
10216 static void emit_streamout(isel_context *ctx, unsigned stream)
10217 {
10218 Builder bld(ctx->program, ctx->block);
10219
10220 Temp so_buffers[4];
10221 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10222 for (unsigned i = 0; i < 4; i++) {
10223 unsigned stride = ctx->program->info->so.strides[i];
10224 if (!stride)
10225 continue;
10226
10227 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10228 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10229 }
10230
10231 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10232 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10233
10234 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10235
10236 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10237
10238 if_context ic;
10239 begin_divergent_if_then(ctx, &ic, can_emit);
10240
10241 bld.reset(ctx->block);
10242
10243 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10244
10245 Temp so_write_offset[4];
10246
10247 for (unsigned i = 0; i < 4; i++) {
10248 unsigned stride = ctx->program->info->so.strides[i];
10249 if (!stride)
10250 continue;
10251
10252 if (stride == 1) {
10253 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10254 get_arg(ctx, ctx->args->streamout_write_idx),
10255 get_arg(ctx, ctx->args->streamout_offset[i]));
10256 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10257
10258 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10259 } else {
10260 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10261 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10262 get_arg(ctx, ctx->args->streamout_offset[i]));
10263 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10264 }
10265 }
10266
10267 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10268 struct radv_stream_output *output =
10269 &ctx->program->info->so.outputs[i];
10270 if (stream != output->stream)
10271 continue;
10272
10273 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10274 }
10275
10276 begin_divergent_if_else(ctx, &ic);
10277 end_divergent_if(ctx, &ic);
10278 }
10279
10280 } /* end namespace */
10281
10282 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10283 {
10284 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10285 Builder bld(ctx->program, ctx->block);
10286 constexpr unsigned hs_idx = 1u;
10287 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10288 get_arg(ctx, ctx->args->merged_wave_info),
10289 Operand((8u << 16) | (hs_idx * 8u)));
10290 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10291
10292 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10293
10294 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10295 get_arg(ctx, ctx->args->rel_auto_id),
10296 get_arg(ctx, ctx->args->ac.instance_id),
10297 ls_has_nonzero_hs_threads);
10298 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10299 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10300 get_arg(ctx, ctx->args->rel_auto_id),
10301 ls_has_nonzero_hs_threads);
10302 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10303 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10304 get_arg(ctx, ctx->args->ac.vertex_id),
10305 ls_has_nonzero_hs_threads);
10306
10307 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10308 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10309 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10310 }
10311
10312 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10313 {
10314 /* Split all arguments except for the first (ring_offsets) and the last
10315 * (exec) so that the dead channels don't stay live throughout the program.
10316 */
10317 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10318 if (startpgm->definitions[i].regClass().size() > 1) {
10319 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10320 startpgm->definitions[i].regClass().size());
10321 }
10322 }
10323 }
10324
10325 void handle_bc_optimize(isel_context *ctx)
10326 {
10327 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10328 Builder bld(ctx->program, ctx->block);
10329 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10330 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10331 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10332 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10333 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10334 if (uses_center && uses_centroid) {
10335 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10336 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10337
10338 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10339 Temp new_coord[2];
10340 for (unsigned i = 0; i < 2; i++) {
10341 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10342 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10343 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10344 persp_centroid, persp_center, sel);
10345 }
10346 ctx->persp_centroid = bld.tmp(v2);
10347 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10348 Operand(new_coord[0]), Operand(new_coord[1]));
10349 emit_split_vector(ctx, ctx->persp_centroid, 2);
10350 }
10351
10352 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10353 Temp new_coord[2];
10354 for (unsigned i = 0; i < 2; i++) {
10355 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10356 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10357 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10358 linear_centroid, linear_center, sel);
10359 }
10360 ctx->linear_centroid = bld.tmp(v2);
10361 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10362 Operand(new_coord[0]), Operand(new_coord[1]));
10363 emit_split_vector(ctx, ctx->linear_centroid, 2);
10364 }
10365 }
10366 }
10367
10368 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10369 {
10370 Program *program = ctx->program;
10371
10372 unsigned float_controls = shader->info.float_controls_execution_mode;
10373
10374 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10375 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10376 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10377 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10378 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10379
10380 program->next_fp_mode.must_flush_denorms32 =
10381 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10382 program->next_fp_mode.must_flush_denorms16_64 =
10383 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10384 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10385
10386 program->next_fp_mode.care_about_round32 =
10387 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10388
10389 program->next_fp_mode.care_about_round16_64 =
10390 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10391 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10392
10393 /* default to preserving fp16 and fp64 denorms, since it's free */
10394 if (program->next_fp_mode.must_flush_denorms16_64)
10395 program->next_fp_mode.denorm16_64 = 0;
10396 else
10397 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10398
10399 /* preserving fp32 denorms is expensive, so only do it if asked */
10400 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10401 program->next_fp_mode.denorm32 = fp_denorm_keep;
10402 else
10403 program->next_fp_mode.denorm32 = 0;
10404
10405 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10406 program->next_fp_mode.round32 = fp_round_tz;
10407 else
10408 program->next_fp_mode.round32 = fp_round_ne;
10409
10410 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10411 program->next_fp_mode.round16_64 = fp_round_tz;
10412 else
10413 program->next_fp_mode.round16_64 = fp_round_ne;
10414
10415 ctx->block->fp_mode = program->next_fp_mode;
10416 }
10417
10418 void cleanup_cfg(Program *program)
10419 {
10420 /* create linear_succs/logical_succs */
10421 for (Block& BB : program->blocks) {
10422 for (unsigned idx : BB.linear_preds)
10423 program->blocks[idx].linear_succs.emplace_back(BB.index);
10424 for (unsigned idx : BB.logical_preds)
10425 program->blocks[idx].logical_succs.emplace_back(BB.index);
10426 }
10427 }
10428
10429 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10430 {
10431 Builder bld(ctx->program, ctx->block);
10432
10433 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10434 Temp count = i == 0
10435 ? get_arg(ctx, ctx->args->merged_wave_info)
10436 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10437 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10438
10439 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10440 Temp cond;
10441
10442 if (ctx->program->wave_size == 64) {
10443 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10444 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10445 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10446 } else {
10447 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10448 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10449 }
10450
10451 return cond;
10452 }
10453
10454 bool ngg_early_prim_export(isel_context *ctx)
10455 {
10456 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10457 return true;
10458 }
10459
10460 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10461 {
10462 Builder bld(ctx->program, ctx->block);
10463
10464 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10465 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10466
10467 /* Get the id of the current wave within the threadgroup (workgroup) */
10468 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10469 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10470
10471 /* Execute the following code only on the first wave (wave id 0),
10472 * use the SCC def to tell if the wave id is zero or not.
10473 */
10474 Temp cond = wave_id_in_tg.def(1).getTemp();
10475 if_context ic;
10476 begin_uniform_if_then(ctx, &ic, cond);
10477 begin_uniform_if_else(ctx, &ic);
10478 bld.reset(ctx->block);
10479
10480 /* Number of vertices output by VS/TES */
10481 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10482 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10483 /* Number of primitives output by VS/TES */
10484 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10485 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10486
10487 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10488 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10489 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10490
10491 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10492 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10493
10494 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10495 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10496
10497 end_uniform_if(ctx, &ic);
10498 }
10499
10500 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10501 {
10502 Builder bld(ctx->program, ctx->block);
10503
10504 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10505 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10506 }
10507
10508 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10509 Temp tmp;
10510
10511 for (unsigned i = 0; i < num_vertices; ++i) {
10512 assert(vtxindex[i].id());
10513
10514 if (i)
10515 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10516 else
10517 tmp = vtxindex[i];
10518
10519 /* The initial edge flag is always false in tess eval shaders. */
10520 if (ctx->stage == ngg_vertex_gs) {
10521 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10522 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10523 }
10524 }
10525
10526 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10527
10528 return tmp;
10529 }
10530
10531 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10532 {
10533 Builder bld(ctx->program, ctx->block);
10534 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10535
10536 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10537 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10538 false /* compressed */, true/* done */, false /* valid mask */);
10539 }
10540
10541 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10542 {
10543 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10544 * These must always come before VS exports.
10545 *
10546 * It is recommended to do these as early as possible. They can be at the beginning when
10547 * there is no SW GS and the shader doesn't write edge flags.
10548 */
10549
10550 if_context ic;
10551 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10552 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10553
10554 Builder bld(ctx->program, ctx->block);
10555 constexpr unsigned max_vertices_per_primitive = 3;
10556 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10557
10558 if (ctx->stage == ngg_vertex_gs) {
10559 /* TODO: optimize for points & lines */
10560 } else if (ctx->stage == ngg_tess_eval_gs) {
10561 if (ctx->shader->info.tess.point_mode)
10562 num_vertices_per_primitive = 1;
10563 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10564 num_vertices_per_primitive = 2;
10565 } else {
10566 unreachable("Unsupported NGG shader stage");
10567 }
10568
10569 Temp vtxindex[max_vertices_per_primitive];
10570 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10571 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10572 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10573 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10574 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10575 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10576 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10577 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10578
10579 /* Export primitive data to the index buffer. */
10580 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10581
10582 /* Export primitive ID. */
10583 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10584 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10585 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10586 Temp provoking_vtx_index = vtxindex[0];
10587 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10588
10589 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10590 }
10591
10592 begin_divergent_if_else(ctx, &ic);
10593 end_divergent_if(ctx, &ic);
10594 }
10595
10596 void ngg_emit_nogs_output(isel_context *ctx)
10597 {
10598 /* Emits NGG GS output, for stages that don't have SW GS. */
10599
10600 if_context ic;
10601 Builder bld(ctx->program, ctx->block);
10602 bool late_prim_export = !ngg_early_prim_export(ctx);
10603
10604 /* NGG streamout is currently disabled by default. */
10605 assert(!ctx->args->shader_info->so.num_outputs);
10606
10607 if (late_prim_export) {
10608 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10609 create_export_phis(ctx);
10610 /* Do what we need to do in the GS threads. */
10611 ngg_emit_nogs_gsthreads(ctx);
10612
10613 /* What comes next should be executed on ES threads. */
10614 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10615 begin_divergent_if_then(ctx, &ic, is_es_thread);
10616 bld.reset(ctx->block);
10617 }
10618
10619 /* Export VS outputs */
10620 ctx->block->kind |= block_kind_export_end;
10621 create_vs_exports(ctx);
10622
10623 /* Export primitive ID */
10624 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10625 Temp prim_id;
10626
10627 if (ctx->stage == ngg_vertex_gs) {
10628 /* Wait for GS threads to store primitive ID in LDS. */
10629 bld.barrier(aco_opcode::p_memory_barrier_shared);
10630 bld.sopp(aco_opcode::s_barrier);
10631
10632 /* Calculate LDS address where the GS threads stored the primitive ID. */
10633 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10634 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10635 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10636 Temp wave_id_mul = bld.v_mul_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10637 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10638 Temp addr = bld.v_mul_imm(bld.def(v1), thread_id_in_tg, 4u);
10639
10640 /* Load primitive ID from LDS. */
10641 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10642 } else if (ctx->stage == ngg_tess_eval_gs) {
10643 /* TES: Just use the patch ID as the primitive ID. */
10644 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10645 } else {
10646 unreachable("unsupported NGG shader stage.");
10647 }
10648
10649 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10650 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10651
10652 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10653 }
10654
10655 if (late_prim_export) {
10656 begin_divergent_if_else(ctx, &ic);
10657 end_divergent_if(ctx, &ic);
10658 bld.reset(ctx->block);
10659 }
10660 }
10661
10662 void select_program(Program *program,
10663 unsigned shader_count,
10664 struct nir_shader *const *shaders,
10665 ac_shader_config* config,
10666 struct radv_shader_args *args)
10667 {
10668 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10669 if_context ic_merged_wave_info;
10670 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10671
10672 for (unsigned i = 0; i < shader_count; i++) {
10673 nir_shader *nir = shaders[i];
10674 init_context(&ctx, nir);
10675
10676 setup_fp_mode(&ctx, nir);
10677
10678 if (!i) {
10679 /* needs to be after init_context() for FS */
10680 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10681 append_logical_start(ctx.block);
10682
10683 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10684 fix_ls_vgpr_init_bug(&ctx, startpgm);
10685
10686 split_arguments(&ctx, startpgm);
10687 }
10688
10689 if (ngg_no_gs) {
10690 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10691
10692 if (ngg_early_prim_export(&ctx))
10693 ngg_emit_nogs_gsthreads(&ctx);
10694 }
10695
10696 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10697 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10698 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10699 ((nir->info.stage == MESA_SHADER_VERTEX &&
10700 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10701 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10702 ctx.stage == tess_eval_geometry_gs));
10703
10704 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10705 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10706 if (check_merged_wave_info) {
10707 Temp cond = merged_wave_info_to_mask(&ctx, i);
10708 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10709 }
10710
10711 if (i) {
10712 Builder bld(ctx.program, ctx.block);
10713
10714 bld.barrier(aco_opcode::p_memory_barrier_shared);
10715 bld.sopp(aco_opcode::s_barrier);
10716
10717 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10718 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));
10719 }
10720 } else if (ctx.stage == geometry_gs)
10721 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10722
10723 if (ctx.stage == fragment_fs)
10724 handle_bc_optimize(&ctx);
10725
10726 visit_cf_list(&ctx, &func->body);
10727
10728 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10729 emit_streamout(&ctx, 0);
10730
10731 if (ctx.stage & hw_vs) {
10732 create_vs_exports(&ctx);
10733 ctx.block->kind |= block_kind_export_end;
10734 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10735 ngg_emit_nogs_output(&ctx);
10736 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10737 Builder bld(ctx.program, ctx.block);
10738 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10739 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10740 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10741 write_tcs_tess_factors(&ctx);
10742 }
10743
10744 if (ctx.stage == fragment_fs) {
10745 create_fs_exports(&ctx);
10746 ctx.block->kind |= block_kind_export_end;
10747 }
10748
10749 if (endif_merged_wave_info) {
10750 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10751 end_divergent_if(&ctx, &ic_merged_wave_info);
10752 }
10753
10754 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10755 ngg_emit_nogs_output(&ctx);
10756
10757 ralloc_free(ctx.divergent_vals);
10758
10759 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10760 /* Outputs of the previous stage are inputs to the next stage */
10761 ctx.inputs = ctx.outputs;
10762 ctx.outputs = shader_io_state();
10763 }
10764 }
10765
10766 program->config->float_mode = program->blocks[0].fp_mode.val;
10767
10768 append_logical_end(ctx.block);
10769 ctx.block->kind |= block_kind_uniform;
10770 Builder bld(ctx.program, ctx.block);
10771 if (ctx.program->wb_smem_l1_on_end)
10772 bld.smem(aco_opcode::s_dcache_wb, false);
10773 bld.sopp(aco_opcode::s_endpgm);
10774
10775 cleanup_cfg(program);
10776 }
10777
10778 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10779 ac_shader_config* config,
10780 struct radv_shader_args *args)
10781 {
10782 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10783
10784 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10785 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10786 program->next_fp_mode.must_flush_denorms32 = false;
10787 program->next_fp_mode.must_flush_denorms16_64 = false;
10788 program->next_fp_mode.care_about_round32 = false;
10789 program->next_fp_mode.care_about_round16_64 = false;
10790 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10791 program->next_fp_mode.denorm32 = 0;
10792 program->next_fp_mode.round32 = fp_round_ne;
10793 program->next_fp_mode.round16_64 = fp_round_ne;
10794 ctx.block->fp_mode = program->next_fp_mode;
10795
10796 add_startpgm(&ctx);
10797 append_logical_start(ctx.block);
10798
10799 Builder bld(ctx.program, ctx.block);
10800
10801 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10802
10803 Operand stream_id(0u);
10804 if (args->shader_info->so.num_outputs)
10805 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10806 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10807
10808 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10809
10810 std::stack<Block> endif_blocks;
10811
10812 for (unsigned stream = 0; stream < 4; stream++) {
10813 if (stream_id.isConstant() && stream != stream_id.constantValue())
10814 continue;
10815
10816 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10817 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10818 continue;
10819
10820 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10821
10822 unsigned BB_if_idx = ctx.block->index;
10823 Block BB_endif = Block();
10824 if (!stream_id.isConstant()) {
10825 /* begin IF */
10826 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10827 append_logical_end(ctx.block);
10828 ctx.block->kind |= block_kind_uniform;
10829 bld.branch(aco_opcode::p_cbranch_z, cond);
10830
10831 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10832
10833 ctx.block = ctx.program->create_and_insert_block();
10834 add_edge(BB_if_idx, ctx.block);
10835 bld.reset(ctx.block);
10836 append_logical_start(ctx.block);
10837 }
10838
10839 unsigned offset = 0;
10840 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10841 if (args->shader_info->gs.output_streams[i] != stream)
10842 continue;
10843
10844 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10845 unsigned length = util_last_bit(output_usage_mask);
10846 for (unsigned j = 0; j < length; ++j) {
10847 if (!(output_usage_mask & (1 << j)))
10848 continue;
10849
10850 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10851 Temp voffset = vtx_offset;
10852 if (const_offset >= 4096u) {
10853 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10854 const_offset %= 4096u;
10855 }
10856
10857 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10858 mubuf->definitions[0] = bld.def(v1);
10859 mubuf->operands[0] = Operand(gsvs_ring);
10860 mubuf->operands[1] = Operand(voffset);
10861 mubuf->operands[2] = Operand(0u);
10862 mubuf->offen = true;
10863 mubuf->offset = const_offset;
10864 mubuf->glc = true;
10865 mubuf->slc = true;
10866 mubuf->dlc = args->options->chip_class >= GFX10;
10867 mubuf->barrier = barrier_none;
10868 mubuf->can_reorder = true;
10869
10870 ctx.outputs.mask[i] |= 1 << j;
10871 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10872
10873 bld.insert(std::move(mubuf));
10874
10875 offset++;
10876 }
10877 }
10878
10879 if (args->shader_info->so.num_outputs) {
10880 emit_streamout(&ctx, stream);
10881 bld.reset(ctx.block);
10882 }
10883
10884 if (stream == 0) {
10885 create_vs_exports(&ctx);
10886 ctx.block->kind |= block_kind_export_end;
10887 }
10888
10889 if (!stream_id.isConstant()) {
10890 append_logical_end(ctx.block);
10891
10892 /* branch from then block to endif block */
10893 bld.branch(aco_opcode::p_branch);
10894 add_edge(ctx.block->index, &BB_endif);
10895 ctx.block->kind |= block_kind_uniform;
10896
10897 /* emit else block */
10898 ctx.block = ctx.program->create_and_insert_block();
10899 add_edge(BB_if_idx, ctx.block);
10900 bld.reset(ctx.block);
10901 append_logical_start(ctx.block);
10902
10903 endif_blocks.push(std::move(BB_endif));
10904 }
10905 }
10906
10907 while (!endif_blocks.empty()) {
10908 Block BB_endif = std::move(endif_blocks.top());
10909 endif_blocks.pop();
10910
10911 Block *BB_else = ctx.block;
10912
10913 append_logical_end(BB_else);
10914 /* branch from else block to endif block */
10915 bld.branch(aco_opcode::p_branch);
10916 add_edge(BB_else->index, &BB_endif);
10917 BB_else->kind |= block_kind_uniform;
10918
10919 /** emit endif merge block */
10920 ctx.block = program->insert_block(std::move(BB_endif));
10921 bld.reset(ctx.block);
10922 append_logical_start(ctx.block);
10923 }
10924
10925 program->config->float_mode = program->blocks[0].fp_mode.val;
10926
10927 append_logical_end(ctx.block);
10928 ctx.block->kind |= block_kind_uniform;
10929 bld.sopp(aco_opcode::s_endpgm);
10930
10931 cleanup_cfg(program);
10932 }
10933 }