aco: allow 8/16-bit shared loads
[mesa.git] / src / amd / compiler / aco_instruction_selection.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <algorithm>
27 #include <array>
28 #include <stack>
29 #include <map>
30
31 #include "ac_shader_util.h"
32 #include "aco_ir.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
37
38 namespace aco {
39 namespace {
40
41 class loop_info_RAII {
42 isel_context* ctx;
43 unsigned header_idx_old;
44 Block* exit_old;
45 bool divergent_cont_old;
46 bool divergent_branch_old;
47 bool divergent_if_old;
48
49 public:
50 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
51 : ctx(ctx),
52 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
53 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
54 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
55 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
56 {
57 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
58 ctx->cf_info.parent_loop.exit = loop_exit;
59 ctx->cf_info.parent_loop.has_divergent_continue = false;
60 ctx->cf_info.parent_loop.has_divergent_branch = false;
61 ctx->cf_info.parent_if.is_divergent = false;
62 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
63 }
64
65 ~loop_info_RAII()
66 {
67 ctx->cf_info.parent_loop.header_idx = header_idx_old;
68 ctx->cf_info.parent_loop.exit = exit_old;
69 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
70 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
71 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
72 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
73 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
74 ctx->cf_info.exec_potentially_empty_discard = false;
75 }
76 };
77
78 struct if_context {
79 Temp cond;
80
81 bool divergent_old;
82 bool exec_potentially_empty_discard_old;
83 bool exec_potentially_empty_break_old;
84 uint16_t exec_potentially_empty_break_depth_old;
85
86 unsigned BB_if_idx;
87 unsigned invert_idx;
88 bool uniform_has_then_branch;
89 bool then_branch_divergent;
90 Block BB_invert;
91 Block BB_endif;
92 };
93
94 static bool visit_cf_list(struct isel_context *ctx,
95 struct exec_list *list);
96
97 static void add_logical_edge(unsigned pred_idx, Block *succ)
98 {
99 succ->logical_preds.emplace_back(pred_idx);
100 }
101
102
103 static void add_linear_edge(unsigned pred_idx, Block *succ)
104 {
105 succ->linear_preds.emplace_back(pred_idx);
106 }
107
108 static void add_edge(unsigned pred_idx, Block *succ)
109 {
110 add_logical_edge(pred_idx, succ);
111 add_linear_edge(pred_idx, succ);
112 }
113
114 static void append_logical_start(Block *b)
115 {
116 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
117 }
118
119 static void append_logical_end(Block *b)
120 {
121 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
122 }
123
124 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
125 {
126 assert(ctx->allocated[def->index].id());
127 return ctx->allocated[def->index];
128 }
129
130 Temp emit_mbcnt(isel_context *ctx, Definition dst,
131 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
132 {
133 Builder bld(ctx->program, ctx->block);
134 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
135 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
136
137 if (ctx->program->wave_size == 32) {
138 return thread_id_lo;
139 } else {
140 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 }
143 }
144
145 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
146 {
147 Builder bld(ctx->program, ctx->block);
148
149 if (!dst.id())
150 dst = bld.tmp(src.regClass());
151
152 assert(src.size() == dst.size());
153
154 if (ctx->stage != fragment_fs) {
155 if (!dst.id())
156 return src;
157
158 bld.copy(Definition(dst), src);
159 return dst;
160 }
161
162 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
163 ctx->program->needs_wqm |= program_needs_wqm;
164 return dst;
165 }
166
167 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
168 {
169 if (index.regClass() == s1)
170 return bld.readlane(bld.def(s1), data, index);
171
172 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
173
174 /* Currently not implemented on GFX6-7 */
175 assert(ctx->options->chip_class >= GFX8);
176
177 if (ctx->options->chip_class <= GFX9 || ctx->program->wave_size == 32) {
178 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
179 }
180
181 /* GFX10, wave64 mode:
182 * The bpermute instruction is limited to half-wave operation, which means that it can't
183 * properly support subgroup shuffle like older generations (or wave32 mode), so we
184 * emulate it here.
185 */
186 if (!ctx->has_gfx10_wave64_bpermute) {
187 ctx->has_gfx10_wave64_bpermute = true;
188 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
189 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
190 }
191
192 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
193 Temp lane_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), lane_id);
194 Temp index_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), index);
195 Temp cmp = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm, vcc), lane_is_hi, index_is_hi);
196
197 return bld.reduction(aco_opcode::p_wave64_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc),
198 bld.vcc(cmp), Operand(v2.as_linear()), index_x4, data, gfx10_wave64_bpermute);
199 }
200
201 Temp as_vgpr(isel_context *ctx, Temp val)
202 {
203 if (val.type() == RegType::sgpr) {
204 Builder bld(ctx->program, ctx->block);
205 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
206 }
207 assert(val.type() == RegType::vgpr);
208 return val;
209 }
210
211 //assumes a != 0xffffffff
212 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
213 {
214 assert(b != 0);
215 Builder bld(ctx->program, ctx->block);
216
217 if (util_is_power_of_two_or_zero(b)) {
218 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
219 return;
220 }
221
222 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
223
224 assert(info.multiplier <= 0xffffffff);
225
226 bool pre_shift = info.pre_shift != 0;
227 bool increment = info.increment != 0;
228 bool multiply = true;
229 bool post_shift = info.post_shift != 0;
230
231 if (!pre_shift && !increment && !multiply && !post_shift) {
232 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
233 return;
234 }
235
236 Temp pre_shift_dst = a;
237 if (pre_shift) {
238 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
239 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
240 }
241
242 Temp increment_dst = pre_shift_dst;
243 if (increment) {
244 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
245 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
246 }
247
248 Temp multiply_dst = increment_dst;
249 if (multiply) {
250 multiply_dst = post_shift ? bld.tmp(v1) : dst;
251 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
252 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
253 }
254
255 if (post_shift) {
256 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
257 }
258 }
259
260 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
261 {
262 Builder bld(ctx->program, ctx->block);
263 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
264 }
265
266
267 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
268 {
269 /* no need to extract the whole vector */
270 if (src.regClass() == dst_rc) {
271 assert(idx == 0);
272 return src;
273 }
274
275 assert(src.bytes() > (idx * dst_rc.bytes()));
276 Builder bld(ctx->program, ctx->block);
277 auto it = ctx->allocated_vec.find(src.id());
278 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
279 if (it->second[idx].regClass() == dst_rc) {
280 return it->second[idx];
281 } else {
282 assert(!dst_rc.is_subdword());
283 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
284 return bld.copy(bld.def(dst_rc), it->second[idx]);
285 }
286 }
287
288 if (dst_rc.is_subdword())
289 src = as_vgpr(ctx, src);
290
291 if (src.bytes() == dst_rc.bytes()) {
292 assert(idx == 0);
293 return bld.copy(bld.def(dst_rc), src);
294 } else {
295 Temp dst = bld.tmp(dst_rc);
296 emit_extract_vector(ctx, src, idx, dst);
297 return dst;
298 }
299 }
300
301 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
302 {
303 if (num_components == 1)
304 return;
305 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
306 return;
307 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
308 split->operands[0] = Operand(vec_src);
309 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
310 RegClass rc;
311 if (num_components > vec_src.size()) {
312 if (vec_src.type() == RegType::sgpr)
313 return;
314
315 /* sub-dword split */
316 assert(vec_src.type() == RegType::vgpr);
317 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
318 } else {
319 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
320 }
321 for (unsigned i = 0; i < num_components; i++) {
322 elems[i] = {ctx->program->allocateId(), rc};
323 split->definitions[i] = Definition(elems[i]);
324 }
325 ctx->block->instructions.emplace_back(std::move(split));
326 ctx->allocated_vec.emplace(vec_src.id(), elems);
327 }
328
329 /* This vector expansion uses a mask to determine which elements in the new vector
330 * come from the original vector. The other elements are undefined. */
331 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
332 {
333 emit_split_vector(ctx, vec_src, util_bitcount(mask));
334
335 if (vec_src == dst)
336 return;
337
338 Builder bld(ctx->program, ctx->block);
339 if (num_components == 1) {
340 if (dst.type() == RegType::sgpr)
341 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
342 else
343 bld.copy(Definition(dst), vec_src);
344 return;
345 }
346
347 unsigned component_size = dst.size() / num_components;
348 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
349
350 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
351 vec->definitions[0] = Definition(dst);
352 unsigned k = 0;
353 for (unsigned i = 0; i < num_components; i++) {
354 if (mask & (1 << i)) {
355 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
356 if (dst.type() == RegType::sgpr)
357 src = bld.as_uniform(src);
358 vec->operands[i] = Operand(src);
359 } else {
360 vec->operands[i] = Operand(0u);
361 }
362 elems[i] = vec->operands[i].getTemp();
363 }
364 ctx->block->instructions.emplace_back(std::move(vec));
365 ctx->allocated_vec.emplace(dst.id(), elems);
366 }
367
368 /* adjust misaligned small bit size loads */
369 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
370 {
371 Builder bld(ctx->program, ctx->block);
372 Operand shift;
373 Temp select = Temp();
374 if (offset.isConstant()) {
375 assert(offset.constantValue() && offset.constantValue() < 4);
376 shift = Operand(offset.constantValue() * 8);
377 } else {
378 /* bit_offset = 8 * (offset & 0x3) */
379 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
380 select = bld.tmp(s1);
381 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
382 }
383
384 if (vec.size() == 1) {
385 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
386 } else if (vec.size() == 2) {
387 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
388 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
389 if (tmp == dst)
390 emit_split_vector(ctx, dst, 2);
391 else
392 emit_extract_vector(ctx, tmp, 0, dst);
393 } else if (vec.size() == 4) {
394 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
395 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
396 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
397 if (select != Temp())
398 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), select);
399 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
400 Temp mid = bld.tmp(s1);
401 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
402 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
403 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
404 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
405 emit_split_vector(ctx, dst, 2);
406 }
407 }
408
409 /* this function trims subdword vectors:
410 * if dst is vgpr - split the src and create a shrunk version according to the mask.
411 * if dst is sgpr - split the src, but move the original to sgpr. */
412 void trim_subdword_vector(isel_context *ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
413 {
414 assert(vec_src.type() == RegType::vgpr);
415 emit_split_vector(ctx, vec_src, num_components);
416
417 Builder bld(ctx->program, ctx->block);
418 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
419 unsigned component_size = vec_src.bytes() / num_components;
420 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
421
422 unsigned k = 0;
423 for (unsigned i = 0; i < num_components; i++) {
424 if (mask & (1 << i))
425 elems[k++] = emit_extract_vector(ctx, vec_src, i, rc);
426 }
427
428 if (dst.type() == RegType::vgpr) {
429 assert(dst.bytes() == k * component_size);
430 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, k, 1)};
431 for (unsigned i = 0; i < k; i++)
432 vec->operands[i] = Operand(elems[i]);
433 vec->definitions[0] = Definition(dst);
434 bld.insert(std::move(vec));
435 } else {
436 // TODO: alignbyte if mask doesn't start with 1?
437 assert(mask & 1);
438 assert(dst.size() == vec_src.size());
439 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
440 }
441 ctx->allocated_vec.emplace(dst.id(), elems);
442 }
443
444 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
445 {
446 Builder bld(ctx->program, ctx->block);
447 if (!dst.id())
448 dst = bld.tmp(bld.lm);
449
450 assert(val.regClass() == s1);
451 assert(dst.regClass() == bld.lm);
452
453 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
454 }
455
456 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
457 {
458 Builder bld(ctx->program, ctx->block);
459 if (!dst.id())
460 dst = bld.tmp(s1);
461
462 assert(val.regClass() == bld.lm);
463 assert(dst.regClass() == s1);
464
465 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
466 Temp tmp = bld.tmp(s1);
467 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
468 return emit_wqm(ctx, tmp, dst);
469 }
470
471 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
472 {
473 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
474 return get_ssa_temp(ctx, src.src.ssa);
475
476 if (src.src.ssa->num_components == size) {
477 bool identity_swizzle = true;
478 for (unsigned i = 0; identity_swizzle && i < size; i++) {
479 if (src.swizzle[i] != i)
480 identity_swizzle = false;
481 }
482 if (identity_swizzle)
483 return get_ssa_temp(ctx, src.src.ssa);
484 }
485
486 Temp vec = get_ssa_temp(ctx, src.src.ssa);
487 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
488 assert(elem_size > 0);
489 assert(vec.bytes() % elem_size == 0);
490
491 if (elem_size < 4 && vec.type() == RegType::sgpr) {
492 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
493 assert(size == 1);
494 unsigned swizzle = src.swizzle[0];
495 if (vec.size() > 1) {
496 assert(src.src.ssa->bit_size == 16);
497 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
498 swizzle = swizzle & 1;
499 }
500 if (swizzle == 0)
501 return vec;
502
503 Temp dst{ctx->program->allocateId(), s1};
504 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 1)};
505 bfe->operands[0] = Operand(vec);
506 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
507 bfe->definitions[0] = Definition(dst);
508 ctx->block->instructions.emplace_back(std::move(bfe));
509 return dst;
510 }
511
512 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
513 if (size == 1) {
514 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
515 } else {
516 assert(size <= 4);
517 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
518 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
519 for (unsigned i = 0; i < size; ++i) {
520 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
521 vec_instr->operands[i] = Operand{elems[i]};
522 }
523 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
524 vec_instr->definitions[0] = Definition(dst);
525 ctx->block->instructions.emplace_back(std::move(vec_instr));
526 ctx->allocated_vec.emplace(dst.id(), elems);
527 return dst;
528 }
529 }
530
531 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
532 {
533 if (ptr.size() == 2)
534 return ptr;
535 Builder bld(ctx->program, ctx->block);
536 if (ptr.type() == RegType::vgpr)
537 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
538 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
539 ptr, Operand((unsigned)ctx->options->address32_hi));
540 }
541
542 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
543 {
544 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
545 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
546 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
547 sop2->definitions[0] = Definition(dst);
548 if (writes_scc)
549 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
550 ctx->block->instructions.emplace_back(std::move(sop2));
551 }
552
553 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
554 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
555 {
556 Builder bld(ctx->program, ctx->block);
557 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
558 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
559 if (src1.type() == RegType::sgpr) {
560 if (commutative && src0.type() == RegType::vgpr) {
561 Temp t = src0;
562 src0 = src1;
563 src1 = t;
564 } else {
565 src1 = as_vgpr(ctx, src1);
566 }
567 }
568
569 if (flush_denorms && ctx->program->chip_class < GFX9) {
570 assert(dst.size() == 1);
571 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
572 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
573 } else {
574 bld.vop2(op, Definition(dst), src0, src1);
575 }
576 }
577
578 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
579 bool flush_denorms = false)
580 {
581 Temp src0 = get_alu_src(ctx, instr->src[0]);
582 Temp src1 = get_alu_src(ctx, instr->src[1]);
583 Temp src2 = get_alu_src(ctx, instr->src[2]);
584
585 /* ensure that the instruction has at most 1 sgpr operand
586 * The optimizer will inline constants for us */
587 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
588 src0 = as_vgpr(ctx, src0);
589 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
590 src1 = as_vgpr(ctx, src1);
591 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
592 src2 = as_vgpr(ctx, src2);
593
594 Builder bld(ctx->program, ctx->block);
595 if (flush_denorms && ctx->program->chip_class < GFX9) {
596 assert(dst.size() == 1);
597 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
598 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
599 } else {
600 bld.vop3(op, Definition(dst), src0, src1, src2);
601 }
602 }
603
604 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
605 {
606 Builder bld(ctx->program, ctx->block);
607 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
608 }
609
610 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
611 {
612 Temp src0 = get_alu_src(ctx, instr->src[0]);
613 Temp src1 = get_alu_src(ctx, instr->src[1]);
614 assert(src0.size() == src1.size());
615
616 aco_ptr<Instruction> vopc;
617 if (src1.type() == RegType::sgpr) {
618 if (src0.type() == RegType::vgpr) {
619 /* to swap the operands, we might also have to change the opcode */
620 switch (op) {
621 case aco_opcode::v_cmp_lt_f16:
622 op = aco_opcode::v_cmp_gt_f16;
623 break;
624 case aco_opcode::v_cmp_ge_f16:
625 op = aco_opcode::v_cmp_le_f16;
626 break;
627 case aco_opcode::v_cmp_lt_i16:
628 op = aco_opcode::v_cmp_gt_i16;
629 break;
630 case aco_opcode::v_cmp_ge_i16:
631 op = aco_opcode::v_cmp_le_i16;
632 break;
633 case aco_opcode::v_cmp_lt_u16:
634 op = aco_opcode::v_cmp_gt_u16;
635 break;
636 case aco_opcode::v_cmp_ge_u16:
637 op = aco_opcode::v_cmp_le_u16;
638 break;
639 case aco_opcode::v_cmp_lt_f32:
640 op = aco_opcode::v_cmp_gt_f32;
641 break;
642 case aco_opcode::v_cmp_ge_f32:
643 op = aco_opcode::v_cmp_le_f32;
644 break;
645 case aco_opcode::v_cmp_lt_i32:
646 op = aco_opcode::v_cmp_gt_i32;
647 break;
648 case aco_opcode::v_cmp_ge_i32:
649 op = aco_opcode::v_cmp_le_i32;
650 break;
651 case aco_opcode::v_cmp_lt_u32:
652 op = aco_opcode::v_cmp_gt_u32;
653 break;
654 case aco_opcode::v_cmp_ge_u32:
655 op = aco_opcode::v_cmp_le_u32;
656 break;
657 case aco_opcode::v_cmp_lt_f64:
658 op = aco_opcode::v_cmp_gt_f64;
659 break;
660 case aco_opcode::v_cmp_ge_f64:
661 op = aco_opcode::v_cmp_le_f64;
662 break;
663 case aco_opcode::v_cmp_lt_i64:
664 op = aco_opcode::v_cmp_gt_i64;
665 break;
666 case aco_opcode::v_cmp_ge_i64:
667 op = aco_opcode::v_cmp_le_i64;
668 break;
669 case aco_opcode::v_cmp_lt_u64:
670 op = aco_opcode::v_cmp_gt_u64;
671 break;
672 case aco_opcode::v_cmp_ge_u64:
673 op = aco_opcode::v_cmp_le_u64;
674 break;
675 default: /* eq and ne are commutative */
676 break;
677 }
678 Temp t = src0;
679 src0 = src1;
680 src1 = t;
681 } else {
682 src1 = as_vgpr(ctx, src1);
683 }
684 }
685
686 Builder bld(ctx->program, ctx->block);
687 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
688 }
689
690 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
691 {
692 Temp src0 = get_alu_src(ctx, instr->src[0]);
693 Temp src1 = get_alu_src(ctx, instr->src[1]);
694 Builder bld(ctx->program, ctx->block);
695
696 assert(dst.regClass() == bld.lm);
697 assert(src0.type() == RegType::sgpr);
698 assert(src1.type() == RegType::sgpr);
699 assert(src0.regClass() == src1.regClass());
700
701 /* Emit the SALU comparison instruction */
702 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
703 /* Turn the result into a per-lane bool */
704 bool_to_vector_condition(ctx, cmp, dst);
705 }
706
707 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
708 aco_opcode v16_op, aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
709 {
710 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : instr->src[0].src.ssa->bit_size == 32 ? s32_op : aco_opcode::num_opcodes;
711 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : instr->src[0].src.ssa->bit_size == 32 ? v32_op : v16_op;
712 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
713 bool use_valu = s_op == aco_opcode::num_opcodes ||
714 divergent_vals ||
715 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
716 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
717 aco_opcode op = use_valu ? v_op : s_op;
718 assert(op != aco_opcode::num_opcodes);
719 assert(dst.regClass() == ctx->program->lane_mask);
720
721 if (use_valu)
722 emit_vopc_instruction(ctx, instr, op, dst);
723 else
724 emit_sopc_instruction(ctx, instr, op, dst);
725 }
726
727 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
728 {
729 Builder bld(ctx->program, ctx->block);
730 Temp src0 = get_alu_src(ctx, instr->src[0]);
731 Temp src1 = get_alu_src(ctx, instr->src[1]);
732
733 assert(dst.regClass() == bld.lm);
734 assert(src0.regClass() == bld.lm);
735 assert(src1.regClass() == bld.lm);
736
737 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
738 }
739
740 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
741 {
742 Builder bld(ctx->program, ctx->block);
743 Temp cond = get_alu_src(ctx, instr->src[0]);
744 Temp then = get_alu_src(ctx, instr->src[1]);
745 Temp els = get_alu_src(ctx, instr->src[2]);
746
747 assert(cond.regClass() == bld.lm);
748
749 if (dst.type() == RegType::vgpr) {
750 aco_ptr<Instruction> bcsel;
751 if (dst.regClass() == v2b) {
752 then = as_vgpr(ctx, then);
753 els = as_vgpr(ctx, els);
754
755 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), els, then, cond);
756 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
757 } else if (dst.regClass() == v1) {
758 then = as_vgpr(ctx, then);
759 els = as_vgpr(ctx, els);
760
761 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
762 } else if (dst.regClass() == v2) {
763 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
764 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
765 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
766 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
767
768 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
769 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
770
771 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
772 } else {
773 fprintf(stderr, "Unimplemented NIR instr bit size: ");
774 nir_print_instr(&instr->instr, stderr);
775 fprintf(stderr, "\n");
776 }
777 return;
778 }
779
780 if (instr->dest.dest.ssa.bit_size == 1) {
781 assert(dst.regClass() == bld.lm);
782 assert(then.regClass() == bld.lm);
783 assert(els.regClass() == bld.lm);
784 }
785
786 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
787 if (dst.regClass() == s1 || dst.regClass() == s2) {
788 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
789 assert(dst.size() == then.size());
790 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
791 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
792 } else {
793 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
794 nir_print_instr(&instr->instr, stderr);
795 fprintf(stderr, "\n");
796 }
797 return;
798 }
799
800 /* divergent boolean bcsel
801 * this implements bcsel on bools: dst = s0 ? s1 : s2
802 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
803 assert(instr->dest.dest.ssa.bit_size == 1);
804
805 if (cond.id() != then.id())
806 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
807
808 if (cond.id() == els.id())
809 bld.sop1(Builder::s_mov, Definition(dst), then);
810 else
811 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
812 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
813 }
814
815 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
816 aco_opcode op, uint32_t undo)
817 {
818 /* multiply by 16777216 to handle denormals */
819 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
820 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
821 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
822 scaled = bld.vop1(op, bld.def(v1), scaled);
823 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
824
825 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
826
827 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
828 }
829
830 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
831 {
832 if (ctx->block->fp_mode.denorm32 == 0) {
833 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
834 return;
835 }
836
837 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
838 }
839
840 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
841 {
842 if (ctx->block->fp_mode.denorm32 == 0) {
843 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
844 return;
845 }
846
847 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
848 }
849
850 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
851 {
852 if (ctx->block->fp_mode.denorm32 == 0) {
853 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
854 return;
855 }
856
857 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
858 }
859
860 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
861 {
862 if (ctx->block->fp_mode.denorm32 == 0) {
863 bld.vop1(aco_opcode::v_log_f32, dst, val);
864 return;
865 }
866
867 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
868 }
869
870 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
871 {
872 if (ctx->options->chip_class >= GFX7)
873 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
874
875 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
876 /* TODO: create more efficient code! */
877 if (val.type() == RegType::sgpr)
878 val = as_vgpr(ctx, val);
879
880 /* Split the input value. */
881 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
882 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
883
884 /* Extract the exponent and compute the unbiased value. */
885 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
886
887 /* Extract the fractional part. */
888 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
889 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
890
891 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
892 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
893
894 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
895 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
896 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
897 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
898 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
899
900 /* Get the sign bit. */
901 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
902
903 /* Decide the operation to apply depending on the unbiased exponent. */
904 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
905 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
906 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
907 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
908 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
909 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
910
911 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
912 }
913
914 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
915 {
916 if (ctx->options->chip_class >= GFX7)
917 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
918
919 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
920 Temp src0 = as_vgpr(ctx, val);
921
922 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
923 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
924
925 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
926 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
927 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
928
929 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
930 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
931 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
932 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
933
934 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
935 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
936
937 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
938
939 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
940 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
941
942 return add->definitions[0].getTemp();
943 }
944
945 Temp convert_int(Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
946 if (!dst.id()) {
947 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
948 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
949 else
950 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
951 }
952
953 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
954 return bld.copy(Definition(dst), src);
955 else if (dst.bytes() < src.bytes())
956 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
957
958 Temp tmp = dst;
959 if (dst_bits == 64)
960 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
961
962 if (tmp == src) {
963 } else if (src.regClass() == s1) {
964 if (is_signed)
965 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
966 else
967 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
968 } else {
969 assert(src_bits != 8 || src.regClass() == v1b);
970 assert(src_bits != 16 || src.regClass() == v2b);
971 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
972 sdwa->operands[0] = Operand(src);
973 sdwa->definitions[0] = Definition(tmp);
974 if (is_signed)
975 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
976 else
977 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
978 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
979 bld.insert(std::move(sdwa));
980 }
981
982 if (dst_bits == 64) {
983 if (is_signed && dst.regClass() == s2) {
984 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
985 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
986 } else if (is_signed && dst.regClass() == v2) {
987 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
988 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
989 } else {
990 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
991 }
992 }
993
994 return dst;
995 }
996
997 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
998 {
999 if (!instr->dest.dest.is_ssa) {
1000 fprintf(stderr, "nir alu dst not in ssa: ");
1001 nir_print_instr(&instr->instr, stderr);
1002 fprintf(stderr, "\n");
1003 abort();
1004 }
1005 Builder bld(ctx->program, ctx->block);
1006 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1007 switch(instr->op) {
1008 case nir_op_vec2:
1009 case nir_op_vec3:
1010 case nir_op_vec4: {
1011 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1012 unsigned num = instr->dest.dest.ssa.num_components;
1013 for (unsigned i = 0; i < num; ++i)
1014 elems[i] = get_alu_src(ctx, instr->src[i]);
1015
1016 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1017 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1018 for (unsigned i = 0; i < num; ++i)
1019 vec->operands[i] = Operand{elems[i]};
1020 vec->definitions[0] = Definition(dst);
1021 ctx->block->instructions.emplace_back(std::move(vec));
1022 ctx->allocated_vec.emplace(dst.id(), elems);
1023 } else {
1024 // TODO: that is a bit suboptimal..
1025 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1026 for (unsigned i = 0; i < num - 1; ++i)
1027 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1028 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1029 for (unsigned i = 0; i < num; ++i) {
1030 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1031 if (bit % 32 == 0) {
1032 elems[bit / 32] = elems[i];
1033 } else {
1034 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1035 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1036 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1037 }
1038 }
1039 if (dst.size() == 1)
1040 bld.copy(Definition(dst), elems[0]);
1041 else
1042 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1043 }
1044 break;
1045 }
1046 case nir_op_mov: {
1047 Temp src = get_alu_src(ctx, instr->src[0]);
1048 aco_ptr<Instruction> mov;
1049 if (dst.type() == RegType::sgpr) {
1050 if (src.type() == RegType::vgpr)
1051 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1052 else if (src.regClass() == s1)
1053 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1054 else if (src.regClass() == s2)
1055 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1056 else
1057 unreachable("wrong src register class for nir_op_imov");
1058 } else if (dst.regClass() == v1) {
1059 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1060 } else if (dst.regClass() == v2) {
1061 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1062 } else {
1063 nir_print_instr(&instr->instr, stderr);
1064 unreachable("Should have been lowered to scalar.");
1065 }
1066 break;
1067 }
1068 case nir_op_inot: {
1069 Temp src = get_alu_src(ctx, instr->src[0]);
1070 if (instr->dest.dest.ssa.bit_size == 1) {
1071 assert(src.regClass() == bld.lm);
1072 assert(dst.regClass() == bld.lm);
1073 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1074 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1075 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1076 } else if (dst.regClass() == v1) {
1077 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1078 } else if (dst.type() == RegType::sgpr) {
1079 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1080 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1081 } else {
1082 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1083 nir_print_instr(&instr->instr, stderr);
1084 fprintf(stderr, "\n");
1085 }
1086 break;
1087 }
1088 case nir_op_ineg: {
1089 Temp src = get_alu_src(ctx, instr->src[0]);
1090 if (dst.regClass() == v1) {
1091 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1092 } else if (dst.regClass() == s1) {
1093 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1094 } else if (dst.size() == 2) {
1095 Temp src0 = bld.tmp(dst.type(), 1);
1096 Temp src1 = bld.tmp(dst.type(), 1);
1097 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1098
1099 if (dst.regClass() == s2) {
1100 Temp carry = bld.tmp(s1);
1101 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1102 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1103 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1104 } else {
1105 Temp lower = bld.tmp(v1);
1106 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1107 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1108 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1109 }
1110 } else {
1111 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1112 nir_print_instr(&instr->instr, stderr);
1113 fprintf(stderr, "\n");
1114 }
1115 break;
1116 }
1117 case nir_op_iabs: {
1118 if (dst.regClass() == s1) {
1119 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1120 } else if (dst.regClass() == v1) {
1121 Temp src = get_alu_src(ctx, instr->src[0]);
1122 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1123 } else {
1124 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1125 nir_print_instr(&instr->instr, stderr);
1126 fprintf(stderr, "\n");
1127 }
1128 break;
1129 }
1130 case nir_op_isign: {
1131 Temp src = get_alu_src(ctx, instr->src[0]);
1132 if (dst.regClass() == s1) {
1133 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1134 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1135 } else if (dst.regClass() == s2) {
1136 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1137 Temp neqz;
1138 if (ctx->program->chip_class >= GFX8)
1139 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1140 else
1141 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1142 /* SCC gets zero-extended to 64 bit */
1143 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1144 } else if (dst.regClass() == v1) {
1145 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1146 } else if (dst.regClass() == v2) {
1147 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1148 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1149 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1150 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1151 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1152 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1153 } else {
1154 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1155 nir_print_instr(&instr->instr, stderr);
1156 fprintf(stderr, "\n");
1157 }
1158 break;
1159 }
1160 case nir_op_imax: {
1161 if (dst.regClass() == v1) {
1162 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1163 } else if (dst.regClass() == s1) {
1164 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1165 } else {
1166 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1167 nir_print_instr(&instr->instr, stderr);
1168 fprintf(stderr, "\n");
1169 }
1170 break;
1171 }
1172 case nir_op_umax: {
1173 if (dst.regClass() == v1) {
1174 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1175 } else if (dst.regClass() == s1) {
1176 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1177 } else {
1178 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1179 nir_print_instr(&instr->instr, stderr);
1180 fprintf(stderr, "\n");
1181 }
1182 break;
1183 }
1184 case nir_op_imin: {
1185 if (dst.regClass() == v1) {
1186 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1187 } else if (dst.regClass() == s1) {
1188 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1189 } else {
1190 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1191 nir_print_instr(&instr->instr, stderr);
1192 fprintf(stderr, "\n");
1193 }
1194 break;
1195 }
1196 case nir_op_umin: {
1197 if (dst.regClass() == v1) {
1198 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1199 } else if (dst.regClass() == s1) {
1200 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1201 } else {
1202 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1203 nir_print_instr(&instr->instr, stderr);
1204 fprintf(stderr, "\n");
1205 }
1206 break;
1207 }
1208 case nir_op_ior: {
1209 if (instr->dest.dest.ssa.bit_size == 1) {
1210 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1211 } else if (dst.regClass() == v1) {
1212 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1213 } else if (dst.regClass() == s1) {
1214 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1215 } else if (dst.regClass() == s2) {
1216 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1217 } else {
1218 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1219 nir_print_instr(&instr->instr, stderr);
1220 fprintf(stderr, "\n");
1221 }
1222 break;
1223 }
1224 case nir_op_iand: {
1225 if (instr->dest.dest.ssa.bit_size == 1) {
1226 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1227 } else if (dst.regClass() == v1) {
1228 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1229 } else if (dst.regClass() == s1) {
1230 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1231 } else if (dst.regClass() == s2) {
1232 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1233 } else {
1234 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1235 nir_print_instr(&instr->instr, stderr);
1236 fprintf(stderr, "\n");
1237 }
1238 break;
1239 }
1240 case nir_op_ixor: {
1241 if (instr->dest.dest.ssa.bit_size == 1) {
1242 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1243 } else if (dst.regClass() == v1) {
1244 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1245 } else if (dst.regClass() == s1) {
1246 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1247 } else if (dst.regClass() == s2) {
1248 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1249 } else {
1250 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1251 nir_print_instr(&instr->instr, stderr);
1252 fprintf(stderr, "\n");
1253 }
1254 break;
1255 }
1256 case nir_op_ushr: {
1257 if (dst.regClass() == v1) {
1258 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1259 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1260 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1261 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1262 } else if (dst.regClass() == v2) {
1263 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1264 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1265 } else if (dst.regClass() == s2) {
1266 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1267 } else if (dst.regClass() == s1) {
1268 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1269 } else {
1270 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1271 nir_print_instr(&instr->instr, stderr);
1272 fprintf(stderr, "\n");
1273 }
1274 break;
1275 }
1276 case nir_op_ishl: {
1277 if (dst.regClass() == v1) {
1278 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1279 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1280 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1281 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1282 } else if (dst.regClass() == v2) {
1283 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1284 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1285 } else if (dst.regClass() == s1) {
1286 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1287 } else if (dst.regClass() == s2) {
1288 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1289 } else {
1290 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1291 nir_print_instr(&instr->instr, stderr);
1292 fprintf(stderr, "\n");
1293 }
1294 break;
1295 }
1296 case nir_op_ishr: {
1297 if (dst.regClass() == v1) {
1298 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1299 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1300 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1301 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1302 } else if (dst.regClass() == v2) {
1303 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1304 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1305 } else if (dst.regClass() == s1) {
1306 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1307 } else if (dst.regClass() == s2) {
1308 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1309 } else {
1310 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1311 nir_print_instr(&instr->instr, stderr);
1312 fprintf(stderr, "\n");
1313 }
1314 break;
1315 }
1316 case nir_op_find_lsb: {
1317 Temp src = get_alu_src(ctx, instr->src[0]);
1318 if (src.regClass() == s1) {
1319 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1320 } else if (src.regClass() == v1) {
1321 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1322 } else if (src.regClass() == s2) {
1323 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1324 } else {
1325 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1326 nir_print_instr(&instr->instr, stderr);
1327 fprintf(stderr, "\n");
1328 }
1329 break;
1330 }
1331 case nir_op_ufind_msb:
1332 case nir_op_ifind_msb: {
1333 Temp src = get_alu_src(ctx, instr->src[0]);
1334 if (src.regClass() == s1 || src.regClass() == s2) {
1335 aco_opcode op = src.regClass() == s2 ?
1336 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1337 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1338 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1339
1340 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1341 Operand(src.size() * 32u - 1u), msb_rev);
1342 Temp msb = sub.def(0).getTemp();
1343 Temp carry = sub.def(1).getTemp();
1344
1345 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1346 } else if (src.regClass() == v1) {
1347 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1348 Temp msb_rev = bld.tmp(v1);
1349 emit_vop1_instruction(ctx, instr, op, msb_rev);
1350 Temp msb = bld.tmp(v1);
1351 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1352 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1353 } else {
1354 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1355 nir_print_instr(&instr->instr, stderr);
1356 fprintf(stderr, "\n");
1357 }
1358 break;
1359 }
1360 case nir_op_bitfield_reverse: {
1361 if (dst.regClass() == s1) {
1362 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1363 } else if (dst.regClass() == v1) {
1364 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1365 } else {
1366 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1367 nir_print_instr(&instr->instr, stderr);
1368 fprintf(stderr, "\n");
1369 }
1370 break;
1371 }
1372 case nir_op_iadd: {
1373 if (dst.regClass() == s1) {
1374 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1375 break;
1376 }
1377
1378 Temp src0 = get_alu_src(ctx, instr->src[0]);
1379 Temp src1 = get_alu_src(ctx, instr->src[1]);
1380 if (dst.regClass() == v1) {
1381 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1382 break;
1383 }
1384
1385 assert(src0.size() == 2 && src1.size() == 2);
1386 Temp src00 = bld.tmp(src0.type(), 1);
1387 Temp src01 = bld.tmp(dst.type(), 1);
1388 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1389 Temp src10 = bld.tmp(src1.type(), 1);
1390 Temp src11 = bld.tmp(dst.type(), 1);
1391 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1392
1393 if (dst.regClass() == s2) {
1394 Temp carry = bld.tmp(s1);
1395 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1396 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1397 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1398 } else if (dst.regClass() == v2) {
1399 Temp dst0 = bld.tmp(v1);
1400 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1401 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1402 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1403 } else {
1404 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1405 nir_print_instr(&instr->instr, stderr);
1406 fprintf(stderr, "\n");
1407 }
1408 break;
1409 }
1410 case nir_op_uadd_sat: {
1411 Temp src0 = get_alu_src(ctx, instr->src[0]);
1412 Temp src1 = get_alu_src(ctx, instr->src[1]);
1413 if (dst.regClass() == s1) {
1414 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1415 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1416 src0, src1);
1417 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1418 } else if (dst.regClass() == v1) {
1419 if (ctx->options->chip_class >= GFX9) {
1420 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1421 add->operands[0] = Operand(src0);
1422 add->operands[1] = Operand(src1);
1423 add->definitions[0] = Definition(dst);
1424 add->clamp = 1;
1425 ctx->block->instructions.emplace_back(std::move(add));
1426 } else {
1427 if (src1.regClass() != v1)
1428 std::swap(src0, src1);
1429 assert(src1.regClass() == v1);
1430 Temp tmp = bld.tmp(v1);
1431 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1432 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1433 }
1434 } else {
1435 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1436 nir_print_instr(&instr->instr, stderr);
1437 fprintf(stderr, "\n");
1438 }
1439 break;
1440 }
1441 case nir_op_uadd_carry: {
1442 Temp src0 = get_alu_src(ctx, instr->src[0]);
1443 Temp src1 = get_alu_src(ctx, instr->src[1]);
1444 if (dst.regClass() == s1) {
1445 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1446 break;
1447 }
1448 if (dst.regClass() == v1) {
1449 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1450 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1451 break;
1452 }
1453
1454 Temp src00 = bld.tmp(src0.type(), 1);
1455 Temp src01 = bld.tmp(dst.type(), 1);
1456 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1457 Temp src10 = bld.tmp(src1.type(), 1);
1458 Temp src11 = bld.tmp(dst.type(), 1);
1459 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1460 if (dst.regClass() == s2) {
1461 Temp carry = bld.tmp(s1);
1462 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1463 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1464 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1465 } else if (dst.regClass() == v2) {
1466 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1467 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1468 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1469 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1470 } else {
1471 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1472 nir_print_instr(&instr->instr, stderr);
1473 fprintf(stderr, "\n");
1474 }
1475 break;
1476 }
1477 case nir_op_isub: {
1478 if (dst.regClass() == s1) {
1479 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1480 break;
1481 }
1482
1483 Temp src0 = get_alu_src(ctx, instr->src[0]);
1484 Temp src1 = get_alu_src(ctx, instr->src[1]);
1485 if (dst.regClass() == v1) {
1486 bld.vsub32(Definition(dst), src0, src1);
1487 break;
1488 }
1489
1490 Temp src00 = bld.tmp(src0.type(), 1);
1491 Temp src01 = bld.tmp(dst.type(), 1);
1492 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1493 Temp src10 = bld.tmp(src1.type(), 1);
1494 Temp src11 = bld.tmp(dst.type(), 1);
1495 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1496 if (dst.regClass() == s2) {
1497 Temp carry = bld.tmp(s1);
1498 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1499 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1500 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1501 } else if (dst.regClass() == v2) {
1502 Temp lower = bld.tmp(v1);
1503 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1504 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1505 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1506 } else {
1507 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1508 nir_print_instr(&instr->instr, stderr);
1509 fprintf(stderr, "\n");
1510 }
1511 break;
1512 }
1513 case nir_op_usub_borrow: {
1514 Temp src0 = get_alu_src(ctx, instr->src[0]);
1515 Temp src1 = get_alu_src(ctx, instr->src[1]);
1516 if (dst.regClass() == s1) {
1517 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1518 break;
1519 } else if (dst.regClass() == v1) {
1520 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1521 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1522 break;
1523 }
1524
1525 Temp src00 = bld.tmp(src0.type(), 1);
1526 Temp src01 = bld.tmp(dst.type(), 1);
1527 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1528 Temp src10 = bld.tmp(src1.type(), 1);
1529 Temp src11 = bld.tmp(dst.type(), 1);
1530 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1531 if (dst.regClass() == s2) {
1532 Temp borrow = bld.tmp(s1);
1533 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1534 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1535 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1536 } else if (dst.regClass() == v2) {
1537 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1538 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1539 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1540 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1541 } else {
1542 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1543 nir_print_instr(&instr->instr, stderr);
1544 fprintf(stderr, "\n");
1545 }
1546 break;
1547 }
1548 case nir_op_imul: {
1549 if (dst.regClass() == v1) {
1550 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1551 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1552 } else if (dst.regClass() == s1) {
1553 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1554 } else {
1555 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1556 nir_print_instr(&instr->instr, stderr);
1557 fprintf(stderr, "\n");
1558 }
1559 break;
1560 }
1561 case nir_op_umul_high: {
1562 if (dst.regClass() == v1) {
1563 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1564 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1565 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1566 } else if (dst.regClass() == s1) {
1567 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1568 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1569 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1570 } else {
1571 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1572 nir_print_instr(&instr->instr, stderr);
1573 fprintf(stderr, "\n");
1574 }
1575 break;
1576 }
1577 case nir_op_imul_high: {
1578 if (dst.regClass() == v1) {
1579 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1580 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1581 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1582 } else if (dst.regClass() == s1) {
1583 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1584 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1585 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1586 } else {
1587 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1588 nir_print_instr(&instr->instr, stderr);
1589 fprintf(stderr, "\n");
1590 }
1591 break;
1592 }
1593 case nir_op_fmul: {
1594 Temp src0 = get_alu_src(ctx, instr->src[0]);
1595 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1596 if (dst.regClass() == v2b) {
1597 Temp tmp = bld.tmp(v1);
1598 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, tmp, true);
1599 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1600 } else if (dst.regClass() == v1) {
1601 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1602 } else if (dst.regClass() == v2) {
1603 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1604 } else {
1605 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1606 nir_print_instr(&instr->instr, stderr);
1607 fprintf(stderr, "\n");
1608 }
1609 break;
1610 }
1611 case nir_op_fadd: {
1612 Temp src0 = get_alu_src(ctx, instr->src[0]);
1613 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1614 if (dst.regClass() == v2b) {
1615 Temp tmp = bld.tmp(v1);
1616 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, tmp, true);
1617 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1618 } else if (dst.regClass() == v1) {
1619 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1620 } else if (dst.regClass() == v2) {
1621 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1622 } else {
1623 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1624 nir_print_instr(&instr->instr, stderr);
1625 fprintf(stderr, "\n");
1626 }
1627 break;
1628 }
1629 case nir_op_fsub: {
1630 Temp src0 = get_alu_src(ctx, instr->src[0]);
1631 Temp src1 = get_alu_src(ctx, instr->src[1]);
1632 if (dst.regClass() == v2b) {
1633 Temp tmp = bld.tmp(v1);
1634 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1635 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, tmp, false);
1636 else
1637 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, tmp, true);
1638 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1639 } else if (dst.regClass() == v1) {
1640 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1641 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1642 else
1643 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1644 } else if (dst.regClass() == v2) {
1645 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1646 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1647 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1648 sub->neg[1] = true;
1649 } else {
1650 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1651 nir_print_instr(&instr->instr, stderr);
1652 fprintf(stderr, "\n");
1653 }
1654 break;
1655 }
1656 case nir_op_fmax: {
1657 Temp src0 = get_alu_src(ctx, instr->src[0]);
1658 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1659 if (dst.regClass() == v2b) {
1660 // TODO: check fp_mode.must_flush_denorms16_64
1661 Temp tmp = bld.tmp(v1);
1662 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, tmp, true);
1663 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1664 } else if (dst.regClass() == v1) {
1665 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1666 } else if (dst.regClass() == v2) {
1667 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1668 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1669 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1670 } else {
1671 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1672 }
1673 } else {
1674 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1675 nir_print_instr(&instr->instr, stderr);
1676 fprintf(stderr, "\n");
1677 }
1678 break;
1679 }
1680 case nir_op_fmin: {
1681 Temp src0 = get_alu_src(ctx, instr->src[0]);
1682 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1683 if (dst.regClass() == v2b) {
1684 // TODO: check fp_mode.must_flush_denorms16_64
1685 Temp tmp = bld.tmp(v1);
1686 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, tmp, true);
1687 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1688 } else if (dst.regClass() == v1) {
1689 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1690 } else if (dst.regClass() == v2) {
1691 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1692 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1693 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1694 } else {
1695 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1696 }
1697 } else {
1698 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1699 nir_print_instr(&instr->instr, stderr);
1700 fprintf(stderr, "\n");
1701 }
1702 break;
1703 }
1704 case nir_op_fmax3: {
1705 if (dst.regClass() == v2b) {
1706 Temp tmp = bld.tmp(v1);
1707 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, tmp, false);
1708 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1709 } else if (dst.regClass() == v1) {
1710 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1711 } else {
1712 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1713 nir_print_instr(&instr->instr, stderr);
1714 fprintf(stderr, "\n");
1715 }
1716 break;
1717 }
1718 case nir_op_fmin3: {
1719 if (dst.regClass() == v2b) {
1720 Temp tmp = bld.tmp(v1);
1721 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, tmp, false);
1722 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1723 } else if (dst.regClass() == v1) {
1724 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1725 } else {
1726 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1727 nir_print_instr(&instr->instr, stderr);
1728 fprintf(stderr, "\n");
1729 }
1730 break;
1731 }
1732 case nir_op_fmed3: {
1733 if (dst.regClass() == v2b) {
1734 Temp tmp = bld.tmp(v1);
1735 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, tmp, false);
1736 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1737 } else if (dst.regClass() == v1) {
1738 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1739 } else {
1740 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1741 nir_print_instr(&instr->instr, stderr);
1742 fprintf(stderr, "\n");
1743 }
1744 break;
1745 }
1746 case nir_op_umax3: {
1747 if (dst.size() == 1) {
1748 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1749 } else {
1750 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1751 nir_print_instr(&instr->instr, stderr);
1752 fprintf(stderr, "\n");
1753 }
1754 break;
1755 }
1756 case nir_op_umin3: {
1757 if (dst.size() == 1) {
1758 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1759 } else {
1760 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1761 nir_print_instr(&instr->instr, stderr);
1762 fprintf(stderr, "\n");
1763 }
1764 break;
1765 }
1766 case nir_op_umed3: {
1767 if (dst.size() == 1) {
1768 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1769 } else {
1770 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1771 nir_print_instr(&instr->instr, stderr);
1772 fprintf(stderr, "\n");
1773 }
1774 break;
1775 }
1776 case nir_op_imax3: {
1777 if (dst.size() == 1) {
1778 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1779 } else {
1780 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1781 nir_print_instr(&instr->instr, stderr);
1782 fprintf(stderr, "\n");
1783 }
1784 break;
1785 }
1786 case nir_op_imin3: {
1787 if (dst.size() == 1) {
1788 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1789 } else {
1790 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1791 nir_print_instr(&instr->instr, stderr);
1792 fprintf(stderr, "\n");
1793 }
1794 break;
1795 }
1796 case nir_op_imed3: {
1797 if (dst.size() == 1) {
1798 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1799 } else {
1800 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1801 nir_print_instr(&instr->instr, stderr);
1802 fprintf(stderr, "\n");
1803 }
1804 break;
1805 }
1806 case nir_op_cube_face_coord: {
1807 Temp in = get_alu_src(ctx, instr->src[0], 3);
1808 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1809 emit_extract_vector(ctx, in, 1, v1),
1810 emit_extract_vector(ctx, in, 2, v1) };
1811 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1812 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1813 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1814 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1815 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1816 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1817 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1818 break;
1819 }
1820 case nir_op_cube_face_index: {
1821 Temp in = get_alu_src(ctx, instr->src[0], 3);
1822 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1823 emit_extract_vector(ctx, in, 1, v1),
1824 emit_extract_vector(ctx, in, 2, v1) };
1825 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1826 break;
1827 }
1828 case nir_op_bcsel: {
1829 emit_bcsel(ctx, instr, dst);
1830 break;
1831 }
1832 case nir_op_frsq: {
1833 Temp src = get_alu_src(ctx, instr->src[0]);
1834 if (dst.regClass() == v2b) {
1835 Temp tmp = bld.vop1(aco_opcode::v_rsq_f16, bld.def(v1), src);
1836 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1837 } else if (dst.regClass() == v1) {
1838 emit_rsq(ctx, bld, Definition(dst), src);
1839 } else if (dst.regClass() == v2) {
1840 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1841 } else {
1842 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1843 nir_print_instr(&instr->instr, stderr);
1844 fprintf(stderr, "\n");
1845 }
1846 break;
1847 }
1848 case nir_op_fneg: {
1849 Temp src = get_alu_src(ctx, instr->src[0]);
1850 if (dst.regClass() == v2b) {
1851 Temp tmp = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x8000u), as_vgpr(ctx, src));
1852 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1853 } else if (dst.regClass() == v1) {
1854 if (ctx->block->fp_mode.must_flush_denorms32)
1855 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1856 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1857 } else if (dst.regClass() == v2) {
1858 if (ctx->block->fp_mode.must_flush_denorms16_64)
1859 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1860 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1861 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1862 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1863 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1864 } else {
1865 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1866 nir_print_instr(&instr->instr, stderr);
1867 fprintf(stderr, "\n");
1868 }
1869 break;
1870 }
1871 case nir_op_fabs: {
1872 Temp src = get_alu_src(ctx, instr->src[0]);
1873 if (dst.regClass() == v2b) {
1874 Temp tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFu), as_vgpr(ctx, src));
1875 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1876 } else if (dst.regClass() == v1) {
1877 if (ctx->block->fp_mode.must_flush_denorms32)
1878 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1879 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1880 } else if (dst.regClass() == v2) {
1881 if (ctx->block->fp_mode.must_flush_denorms16_64)
1882 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1883 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1884 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1885 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1886 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1887 } else {
1888 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1889 nir_print_instr(&instr->instr, stderr);
1890 fprintf(stderr, "\n");
1891 }
1892 break;
1893 }
1894 case nir_op_fsat: {
1895 Temp src = get_alu_src(ctx, instr->src[0]);
1896 if (dst.regClass() == v2b) {
1897 Temp tmp = bld.vop3(aco_opcode::v_med3_f16, bld.def(v1), Operand(0u), Operand(0x3f800000u), src);
1898 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1899 } else if (dst.regClass() == v1) {
1900 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1901 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1902 // TODO: confirm that this holds under any circumstances
1903 } else if (dst.regClass() == v2) {
1904 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1905 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1906 vop3->clamp = true;
1907 } else {
1908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1909 nir_print_instr(&instr->instr, stderr);
1910 fprintf(stderr, "\n");
1911 }
1912 break;
1913 }
1914 case nir_op_flog2: {
1915 Temp src = get_alu_src(ctx, instr->src[0]);
1916 if (dst.regClass() == v2b) {
1917 Temp tmp = bld.vop1(aco_opcode::v_log_f16, bld.def(v1), src);
1918 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1919 } else if (dst.regClass() == v1) {
1920 emit_log2(ctx, bld, Definition(dst), src);
1921 } else {
1922 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1923 nir_print_instr(&instr->instr, stderr);
1924 fprintf(stderr, "\n");
1925 }
1926 break;
1927 }
1928 case nir_op_frcp: {
1929 Temp src = get_alu_src(ctx, instr->src[0]);
1930 if (dst.regClass() == v2b) {
1931 Temp tmp = bld.vop1(aco_opcode::v_rcp_f16, bld.def(v1), src);
1932 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1933 } else if (dst.regClass() == v1) {
1934 emit_rcp(ctx, bld, Definition(dst), src);
1935 } else if (dst.regClass() == v2) {
1936 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1937 } else {
1938 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1939 nir_print_instr(&instr->instr, stderr);
1940 fprintf(stderr, "\n");
1941 }
1942 break;
1943 }
1944 case nir_op_fexp2: {
1945 if (dst.regClass() == v2b) {
1946 Temp src = get_alu_src(ctx, instr->src[0]);
1947 Temp tmp = bld.vop1(aco_opcode::v_exp_f16, bld.def(v1), src);
1948 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1949 } else if (dst.regClass() == v1) {
1950 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1951 } else {
1952 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1953 nir_print_instr(&instr->instr, stderr);
1954 fprintf(stderr, "\n");
1955 }
1956 break;
1957 }
1958 case nir_op_fsqrt: {
1959 Temp src = get_alu_src(ctx, instr->src[0]);
1960 if (dst.regClass() == v2b) {
1961 Temp tmp = bld.vop1(aco_opcode::v_sqrt_f16, bld.def(v1), src);
1962 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1963 } else if (dst.regClass() == v1) {
1964 emit_sqrt(ctx, bld, Definition(dst), src);
1965 } else if (dst.regClass() == v2) {
1966 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1967 } else {
1968 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1969 nir_print_instr(&instr->instr, stderr);
1970 fprintf(stderr, "\n");
1971 }
1972 break;
1973 }
1974 case nir_op_ffract: {
1975 if (dst.regClass() == v2b) {
1976 Temp src = get_alu_src(ctx, instr->src[0]);
1977 Temp tmp = bld.vop1(aco_opcode::v_fract_f16, bld.def(v1), src);
1978 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1979 } else if (dst.regClass() == v1) {
1980 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1981 } else if (dst.regClass() == v2) {
1982 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1983 } else {
1984 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1985 nir_print_instr(&instr->instr, stderr);
1986 fprintf(stderr, "\n");
1987 }
1988 break;
1989 }
1990 case nir_op_ffloor: {
1991 Temp src = get_alu_src(ctx, instr->src[0]);
1992 if (dst.regClass() == v2b) {
1993 Temp tmp = bld.vop1(aco_opcode::v_floor_f16, bld.def(v1), src);
1994 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1995 } else if (dst.regClass() == v1) {
1996 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1997 } else if (dst.regClass() == v2) {
1998 emit_floor_f64(ctx, bld, Definition(dst), src);
1999 } else {
2000 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2001 nir_print_instr(&instr->instr, stderr);
2002 fprintf(stderr, "\n");
2003 }
2004 break;
2005 }
2006 case nir_op_fceil: {
2007 Temp src0 = get_alu_src(ctx, instr->src[0]);
2008 if (dst.regClass() == v2b) {
2009 Temp tmp = bld.vop1(aco_opcode::v_ceil_f16, bld.def(v1), src0);
2010 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2011 } else if (dst.regClass() == v1) {
2012 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2013 } else if (dst.regClass() == v2) {
2014 if (ctx->options->chip_class >= GFX7) {
2015 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2016 } else {
2017 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2018 /* trunc = trunc(src0)
2019 * if (src0 > 0.0 && src0 != trunc)
2020 * trunc += 1.0
2021 */
2022 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2023 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2024 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2025 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2026 Temp add = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), bld.copy(bld.def(v1), Operand(0u)), bld.copy(bld.def(v1), Operand(0x3ff00000u)), cond);
2027 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2028 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2029 }
2030 } else {
2031 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2032 nir_print_instr(&instr->instr, stderr);
2033 fprintf(stderr, "\n");
2034 }
2035 break;
2036 }
2037 case nir_op_ftrunc: {
2038 Temp src = get_alu_src(ctx, instr->src[0]);
2039 if (dst.regClass() == v2b) {
2040 Temp tmp = bld.vop1(aco_opcode::v_trunc_f16, bld.def(v1), src);
2041 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2042 } else if (dst.regClass() == v1) {
2043 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2044 } else if (dst.regClass() == v2) {
2045 emit_trunc_f64(ctx, bld, Definition(dst), src);
2046 } else {
2047 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2048 nir_print_instr(&instr->instr, stderr);
2049 fprintf(stderr, "\n");
2050 }
2051 break;
2052 }
2053 case nir_op_fround_even: {
2054 Temp src0 = get_alu_src(ctx, instr->src[0]);
2055 if (dst.regClass() == v2b) {
2056 Temp tmp = bld.vop1(aco_opcode::v_rndne_f16, bld.def(v1), src0);
2057 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2058 } else if (dst.regClass() == v1) {
2059 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2060 } else if (dst.regClass() == v2) {
2061 if (ctx->options->chip_class >= GFX7) {
2062 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2063 } else {
2064 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2065 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2066 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2067
2068 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2069 Temp bfi = bld.vop3(aco_opcode::v_bfi_b32, bld.def(v1), bitmask, bld.copy(bld.def(v1), Operand(0x43300000u)), as_vgpr(ctx, src0_hi));
2070 Temp tmp = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), src0, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
2071 Instruction *sub = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), tmp, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
2072 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2073 tmp = sub->definitions[0].getTemp();
2074
2075 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2076 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2077 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2078 Temp cond = vop3->definitions[0].getTemp();
2079
2080 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2081 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2082 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2083 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2084
2085 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2086 }
2087 } else {
2088 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr->instr, stderr);
2090 fprintf(stderr, "\n");
2091 }
2092 break;
2093 }
2094 case nir_op_fsin:
2095 case nir_op_fcos: {
2096 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2097 aco_ptr<Instruction> norm;
2098 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2099 if (dst.regClass() == v2b) {
2100 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2101 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2102 tmp = bld.vop1(opcode, bld.def(v1), tmp);
2103 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2104 } else if (dst.regClass() == v1) {
2105 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2106
2107 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2108 if (ctx->options->chip_class < GFX9)
2109 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2110
2111 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2112 bld.vop1(opcode, Definition(dst), tmp);
2113 } else {
2114 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2115 nir_print_instr(&instr->instr, stderr);
2116 fprintf(stderr, "\n");
2117 }
2118 break;
2119 }
2120 case nir_op_ldexp: {
2121 Temp src0 = get_alu_src(ctx, instr->src[0]);
2122 Temp src1 = get_alu_src(ctx, instr->src[1]);
2123 if (dst.regClass() == v2b) {
2124 Temp tmp = bld.tmp(v1);
2125 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, tmp, false);
2126 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2127 } else if (dst.regClass() == v1) {
2128 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2129 } else if (dst.regClass() == v2) {
2130 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2131 } else {
2132 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2133 nir_print_instr(&instr->instr, stderr);
2134 fprintf(stderr, "\n");
2135 }
2136 break;
2137 }
2138 case nir_op_frexp_sig: {
2139 Temp src = get_alu_src(ctx, instr->src[0]);
2140 if (dst.regClass() == v2b) {
2141 Temp tmp = bld.vop1(aco_opcode::v_frexp_mant_f16, bld.def(v1), src);
2142 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2143 } else if (dst.regClass() == v1) {
2144 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2145 } else if (dst.regClass() == v2) {
2146 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2147 } else {
2148 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2149 nir_print_instr(&instr->instr, stderr);
2150 fprintf(stderr, "\n");
2151 }
2152 break;
2153 }
2154 case nir_op_frexp_exp: {
2155 Temp src = get_alu_src(ctx, instr->src[0]);
2156 if (instr->src[0].src.ssa->bit_size == 16) {
2157 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2158 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2159 convert_int(bld, tmp, 8, 32, true, dst);
2160 } else if (instr->src[0].src.ssa->bit_size == 32) {
2161 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2162 } else if (instr->src[0].src.ssa->bit_size == 64) {
2163 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2164 } else {
2165 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2166 nir_print_instr(&instr->instr, stderr);
2167 fprintf(stderr, "\n");
2168 }
2169 break;
2170 }
2171 case nir_op_fsign: {
2172 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2173 if (dst.regClass() == v2b) {
2174 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2175 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2176 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2177 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2178 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2179 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), minus_one, src, cond);
2180 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2181 } else if (dst.regClass() == v1) {
2182 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2183 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2184 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2185 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2186 } else if (dst.regClass() == v2) {
2187 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2188 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2189 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2190
2191 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2192 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2193 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2194
2195 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2196 } else {
2197 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2198 nir_print_instr(&instr->instr, stderr);
2199 fprintf(stderr, "\n");
2200 }
2201 break;
2202 }
2203 case nir_op_f2f16:
2204 case nir_op_f2f16_rtne: {
2205 Temp src = get_alu_src(ctx, instr->src[0]);
2206 if (instr->src[0].src.ssa->bit_size == 64)
2207 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2208 src = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2209 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2210 break;
2211 }
2212 case nir_op_f2f16_rtz: {
2213 Temp src = get_alu_src(ctx, instr->src[0]);
2214 if (instr->src[0].src.ssa->bit_size == 64)
2215 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2216 src = bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, bld.def(v1), src, Operand(0u));
2217 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2218 break;
2219 }
2220 case nir_op_f2f32: {
2221 if (instr->src[0].src.ssa->bit_size == 16) {
2222 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2223 } else if (instr->src[0].src.ssa->bit_size == 64) {
2224 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2225 } else {
2226 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2227 nir_print_instr(&instr->instr, stderr);
2228 fprintf(stderr, "\n");
2229 }
2230 break;
2231 }
2232 case nir_op_f2f64: {
2233 Temp src = get_alu_src(ctx, instr->src[0]);
2234 if (instr->src[0].src.ssa->bit_size == 16)
2235 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2236 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2237 break;
2238 }
2239 case nir_op_i2f16: {
2240 assert(dst.regClass() == v2b);
2241 Temp src = get_alu_src(ctx, instr->src[0]);
2242 if (instr->src[0].src.ssa->bit_size == 8)
2243 src = convert_int(bld, src, 8, 16, true);
2244 Temp tmp = bld.vop1(aco_opcode::v_cvt_f16_i16, bld.def(v1), src);
2245 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2246 break;
2247 }
2248 case nir_op_i2f32: {
2249 assert(dst.size() == 1);
2250 Temp src = get_alu_src(ctx, instr->src[0]);
2251 if (instr->src[0].src.ssa->bit_size <= 16)
2252 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2253 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2254 break;
2255 }
2256 case nir_op_i2f64: {
2257 if (instr->src[0].src.ssa->bit_size <= 32) {
2258 Temp src = get_alu_src(ctx, instr->src[0]);
2259 if (instr->src[0].src.ssa->bit_size <= 16)
2260 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2261 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2262 } else if (instr->src[0].src.ssa->bit_size == 64) {
2263 Temp src = get_alu_src(ctx, instr->src[0]);
2264 RegClass rc = RegClass(src.type(), 1);
2265 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2266 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2267 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2268 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2269 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2270 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2271
2272 } else {
2273 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2274 nir_print_instr(&instr->instr, stderr);
2275 fprintf(stderr, "\n");
2276 }
2277 break;
2278 }
2279 case nir_op_u2f16: {
2280 assert(dst.regClass() == v2b);
2281 Temp src = get_alu_src(ctx, instr->src[0]);
2282 if (instr->src[0].src.ssa->bit_size == 8)
2283 src = convert_int(bld, src, 8, 16, false);
2284 Temp tmp = bld.vop1(aco_opcode::v_cvt_f16_u16, bld.def(v1), src);
2285 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2286 break;
2287 }
2288 case nir_op_u2f32: {
2289 assert(dst.size() == 1);
2290 Temp src = get_alu_src(ctx, instr->src[0]);
2291 if (instr->src[0].src.ssa->bit_size == 8) {
2292 //TODO: we should use v_cvt_f32_ubyte1/v_cvt_f32_ubyte2/etc depending on the register assignment
2293 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2294 } else {
2295 if (instr->src[0].src.ssa->bit_size == 16)
2296 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2297 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2298 }
2299 break;
2300 }
2301 case nir_op_u2f64: {
2302 if (instr->src[0].src.ssa->bit_size <= 32) {
2303 Temp src = get_alu_src(ctx, instr->src[0]);
2304 if (instr->src[0].src.ssa->bit_size <= 16)
2305 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2306 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2307 } else if (instr->src[0].src.ssa->bit_size == 64) {
2308 Temp src = get_alu_src(ctx, instr->src[0]);
2309 RegClass rc = RegClass(src.type(), 1);
2310 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2311 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2312 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2313 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2314 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2315 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2316 } else {
2317 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2318 nir_print_instr(&instr->instr, stderr);
2319 fprintf(stderr, "\n");
2320 }
2321 break;
2322 }
2323 case nir_op_f2i8:
2324 case nir_op_f2i16: {
2325 Temp src = get_alu_src(ctx, instr->src[0]);
2326 if (instr->src[0].src.ssa->bit_size == 16)
2327 src = bld.vop1(aco_opcode::v_cvt_i16_f16, bld.def(v1), src);
2328 else if (instr->src[0].src.ssa->bit_size == 32)
2329 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2330 else
2331 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2332
2333 if (dst.type() == RegType::vgpr)
2334 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2335 else
2336 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2337 break;
2338 }
2339 case nir_op_f2u8:
2340 case nir_op_f2u16: {
2341 Temp src = get_alu_src(ctx, instr->src[0]);
2342 if (instr->src[0].src.ssa->bit_size == 16)
2343 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2344 else if (instr->src[0].src.ssa->bit_size == 32)
2345 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2346 else
2347 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2348
2349 if (dst.type() == RegType::vgpr)
2350 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2351 else
2352 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2353 break;
2354 }
2355 case nir_op_f2i32: {
2356 Temp src = get_alu_src(ctx, instr->src[0]);
2357 if (instr->src[0].src.ssa->bit_size == 16) {
2358 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2359 if (dst.type() == RegType::vgpr) {
2360 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2361 } else {
2362 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2363 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2364 }
2365 } else if (instr->src[0].src.ssa->bit_size == 32) {
2366 if (dst.type() == RegType::vgpr)
2367 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2368 else
2369 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2370 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2371
2372 } else if (instr->src[0].src.ssa->bit_size == 64) {
2373 if (dst.type() == RegType::vgpr)
2374 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2375 else
2376 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2377 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2378
2379 } else {
2380 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2381 nir_print_instr(&instr->instr, stderr);
2382 fprintf(stderr, "\n");
2383 }
2384 break;
2385 }
2386 case nir_op_f2u32: {
2387 Temp src = get_alu_src(ctx, instr->src[0]);
2388 if (instr->src[0].src.ssa->bit_size == 16) {
2389 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2390 if (dst.type() == RegType::vgpr) {
2391 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2392 } else {
2393 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2394 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2395 }
2396 } else if (instr->src[0].src.ssa->bit_size == 32) {
2397 if (dst.type() == RegType::vgpr)
2398 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2399 else
2400 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2401 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2402
2403 } else if (instr->src[0].src.ssa->bit_size == 64) {
2404 if (dst.type() == RegType::vgpr)
2405 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2406 else
2407 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2408 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2409
2410 } else {
2411 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2412 nir_print_instr(&instr->instr, stderr);
2413 fprintf(stderr, "\n");
2414 }
2415 break;
2416 }
2417 case nir_op_f2i64: {
2418 Temp src = get_alu_src(ctx, instr->src[0]);
2419 if (instr->src[0].src.ssa->bit_size == 16)
2420 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2421
2422 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2423 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2424 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2425 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2426 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2427 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2428 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2429 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2430 Temp new_exponent = bld.tmp(v1);
2431 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2432 if (ctx->program->chip_class >= GFX8)
2433 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2434 else
2435 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2436 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2437 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2438 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2439 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2440 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2441 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2442 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2443 Temp new_lower = bld.tmp(v1);
2444 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2445 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2446 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2447
2448 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2449 if (src.type() == RegType::vgpr)
2450 src = bld.as_uniform(src);
2451 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2452 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2453 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2454 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2455 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2456 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2457 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2458 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2459 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2460 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2461 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2462 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2463 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2464 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2465 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2466 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2467 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2468 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2469 Temp borrow = bld.tmp(s1);
2470 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2471 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2472 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2473
2474 } else if (instr->src[0].src.ssa->bit_size == 64) {
2475 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2476 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2477 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2478 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2479 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2480 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2481 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2482 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2483 if (dst.type() == RegType::sgpr) {
2484 lower = bld.as_uniform(lower);
2485 upper = bld.as_uniform(upper);
2486 }
2487 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2488
2489 } else {
2490 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2491 nir_print_instr(&instr->instr, stderr);
2492 fprintf(stderr, "\n");
2493 }
2494 break;
2495 }
2496 case nir_op_f2u64: {
2497 Temp src = get_alu_src(ctx, instr->src[0]);
2498 if (instr->src[0].src.ssa->bit_size == 16)
2499 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2500
2501 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2502 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2503 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2504 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2505 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2506 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2507 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2508 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2509 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2510 Temp new_exponent = bld.tmp(v1);
2511 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2512 if (ctx->program->chip_class >= GFX8)
2513 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2514 else
2515 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2516 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2517 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2518 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2519 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2520 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2521 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2522 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2523
2524 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2525 if (src.type() == RegType::vgpr)
2526 src = bld.as_uniform(src);
2527 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2528 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2529 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2530 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2531 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2532 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2533 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2534 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2535 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2536 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2537 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2538 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2539 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2540 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2541 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2542 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2543 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2544 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2545
2546 } else if (instr->src[0].src.ssa->bit_size == 64) {
2547 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2548 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2549 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2550 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2551 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2552 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2553 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2554 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2555 if (dst.type() == RegType::sgpr) {
2556 lower = bld.as_uniform(lower);
2557 upper = bld.as_uniform(upper);
2558 }
2559 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2560
2561 } else {
2562 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2563 nir_print_instr(&instr->instr, stderr);
2564 fprintf(stderr, "\n");
2565 }
2566 break;
2567 }
2568 case nir_op_b2f16: {
2569 Temp src = get_alu_src(ctx, instr->src[0]);
2570 assert(src.regClass() == bld.lm);
2571
2572 if (dst.regClass() == s1) {
2573 src = bool_to_scalar_condition(ctx, src);
2574 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2575 } else if (dst.regClass() == v2b) {
2576 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2577 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2578 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2579 } else {
2580 unreachable("Wrong destination register class for nir_op_b2f16.");
2581 }
2582 break;
2583 }
2584 case nir_op_b2f32: {
2585 Temp src = get_alu_src(ctx, instr->src[0]);
2586 assert(src.regClass() == bld.lm);
2587
2588 if (dst.regClass() == s1) {
2589 src = bool_to_scalar_condition(ctx, src);
2590 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2591 } else if (dst.regClass() == v1) {
2592 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2593 } else {
2594 unreachable("Wrong destination register class for nir_op_b2f32.");
2595 }
2596 break;
2597 }
2598 case nir_op_b2f64: {
2599 Temp src = get_alu_src(ctx, instr->src[0]);
2600 assert(src.regClass() == bld.lm);
2601
2602 if (dst.regClass() == s2) {
2603 src = bool_to_scalar_condition(ctx, src);
2604 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2605 } else if (dst.regClass() == v2) {
2606 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2607 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2608 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2609 } else {
2610 unreachable("Wrong destination register class for nir_op_b2f64.");
2611 }
2612 break;
2613 }
2614 case nir_op_i2i8:
2615 case nir_op_i2i16:
2616 case nir_op_i2i32:
2617 case nir_op_i2i64: {
2618 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2619 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2620 break;
2621 }
2622 case nir_op_u2u8:
2623 case nir_op_u2u16:
2624 case nir_op_u2u32:
2625 case nir_op_u2u64: {
2626 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2627 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2628 break;
2629 }
2630 case nir_op_b2b32:
2631 case nir_op_b2i32: {
2632 Temp src = get_alu_src(ctx, instr->src[0]);
2633 assert(src.regClass() == bld.lm);
2634
2635 if (dst.regClass() == s1) {
2636 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2637 bool_to_scalar_condition(ctx, src, dst);
2638 } else if (dst.regClass() == v1) {
2639 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2640 } else {
2641 unreachable("Invalid register class for b2i32");
2642 }
2643 break;
2644 }
2645 case nir_op_b2b1:
2646 case nir_op_i2b1: {
2647 Temp src = get_alu_src(ctx, instr->src[0]);
2648 assert(dst.regClass() == bld.lm);
2649
2650 if (src.type() == RegType::vgpr) {
2651 assert(src.regClass() == v1 || src.regClass() == v2);
2652 assert(dst.regClass() == bld.lm);
2653 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2654 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2655 } else {
2656 assert(src.regClass() == s1 || src.regClass() == s2);
2657 Temp tmp;
2658 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2659 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2660 } else {
2661 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2662 bld.scc(bld.def(s1)), Operand(0u), src);
2663 }
2664 bool_to_vector_condition(ctx, tmp, dst);
2665 }
2666 break;
2667 }
2668 case nir_op_pack_64_2x32_split: {
2669 Temp src0 = get_alu_src(ctx, instr->src[0]);
2670 Temp src1 = get_alu_src(ctx, instr->src[1]);
2671
2672 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2673 break;
2674 }
2675 case nir_op_unpack_64_2x32_split_x:
2676 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2677 break;
2678 case nir_op_unpack_64_2x32_split_y:
2679 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2680 break;
2681 case nir_op_unpack_32_2x16_split_x:
2682 if (dst.type() == RegType::vgpr) {
2683 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2684 } else {
2685 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2686 }
2687 break;
2688 case nir_op_unpack_32_2x16_split_y:
2689 if (dst.type() == RegType::vgpr) {
2690 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2691 } else {
2692 bld.sop2(aco_opcode::s_bfe_u32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]), Operand(uint32_t(16 << 16 | 16)));
2693 }
2694 break;
2695 case nir_op_pack_32_2x16_split: {
2696 Temp src0 = get_alu_src(ctx, instr->src[0]);
2697 Temp src1 = get_alu_src(ctx, instr->src[1]);
2698 if (dst.regClass() == v1) {
2699 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2700 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2701 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2702 } else {
2703 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2704 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2705 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2706 }
2707 break;
2708 }
2709 case nir_op_pack_half_2x16: {
2710 Temp src = get_alu_src(ctx, instr->src[0], 2);
2711
2712 if (dst.regClass() == v1) {
2713 Temp src0 = bld.tmp(v1);
2714 Temp src1 = bld.tmp(v1);
2715 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2716 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2717 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2718 else
2719 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2720 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2721 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2722 } else {
2723 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2724 nir_print_instr(&instr->instr, stderr);
2725 fprintf(stderr, "\n");
2726 }
2727 break;
2728 }
2729 case nir_op_unpack_half_2x16_split_x: {
2730 if (dst.regClass() == v1) {
2731 Builder bld(ctx->program, ctx->block);
2732 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2733 } else {
2734 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2735 nir_print_instr(&instr->instr, stderr);
2736 fprintf(stderr, "\n");
2737 }
2738 break;
2739 }
2740 case nir_op_unpack_half_2x16_split_y: {
2741 if (dst.regClass() == v1) {
2742 Builder bld(ctx->program, ctx->block);
2743 /* TODO: use SDWA here */
2744 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2745 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2746 } else {
2747 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2748 nir_print_instr(&instr->instr, stderr);
2749 fprintf(stderr, "\n");
2750 }
2751 break;
2752 }
2753 case nir_op_fquantize2f16: {
2754 Temp src = get_alu_src(ctx, instr->src[0]);
2755 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2756 Temp f32, cmp_res;
2757
2758 if (ctx->program->chip_class >= GFX8) {
2759 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2760 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2761 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2762 } else {
2763 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2764 * so compare the result and flush to 0 if it's smaller.
2765 */
2766 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2767 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2768 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2769 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2770 cmp_res = vop3->definitions[0].getTemp();
2771 }
2772
2773 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2774 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2775 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2776 } else {
2777 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2778 }
2779 break;
2780 }
2781 case nir_op_bfm: {
2782 Temp bits = get_alu_src(ctx, instr->src[0]);
2783 Temp offset = get_alu_src(ctx, instr->src[1]);
2784
2785 if (dst.regClass() == s1) {
2786 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2787 } else if (dst.regClass() == v1) {
2788 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2789 } else {
2790 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2791 nir_print_instr(&instr->instr, stderr);
2792 fprintf(stderr, "\n");
2793 }
2794 break;
2795 }
2796 case nir_op_bitfield_select: {
2797 /* (mask & insert) | (~mask & base) */
2798 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2799 Temp insert = get_alu_src(ctx, instr->src[1]);
2800 Temp base = get_alu_src(ctx, instr->src[2]);
2801
2802 /* dst = (insert & bitmask) | (base & ~bitmask) */
2803 if (dst.regClass() == s1) {
2804 aco_ptr<Instruction> sop2;
2805 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2806 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2807 Operand lhs;
2808 if (const_insert && const_bitmask) {
2809 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2810 } else {
2811 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2812 lhs = Operand(insert);
2813 }
2814
2815 Operand rhs;
2816 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2817 if (const_base && const_bitmask) {
2818 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2819 } else {
2820 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2821 rhs = Operand(base);
2822 }
2823
2824 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2825
2826 } else if (dst.regClass() == v1) {
2827 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2828 base = as_vgpr(ctx, base);
2829 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2830 insert = as_vgpr(ctx, insert);
2831
2832 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2833
2834 } else {
2835 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2836 nir_print_instr(&instr->instr, stderr);
2837 fprintf(stderr, "\n");
2838 }
2839 break;
2840 }
2841 case nir_op_ubfe:
2842 case nir_op_ibfe: {
2843 Temp base = get_alu_src(ctx, instr->src[0]);
2844 Temp offset = get_alu_src(ctx, instr->src[1]);
2845 Temp bits = get_alu_src(ctx, instr->src[2]);
2846
2847 if (dst.type() == RegType::sgpr) {
2848 Operand extract;
2849 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2850 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2851 if (const_offset && const_bits) {
2852 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2853 extract = Operand(const_extract);
2854 } else {
2855 Operand width;
2856 if (const_bits) {
2857 width = Operand(const_bits->u32 << 16);
2858 } else {
2859 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2860 }
2861 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2862 }
2863
2864 aco_opcode opcode;
2865 if (dst.regClass() == s1) {
2866 if (instr->op == nir_op_ubfe)
2867 opcode = aco_opcode::s_bfe_u32;
2868 else
2869 opcode = aco_opcode::s_bfe_i32;
2870 } else if (dst.regClass() == s2) {
2871 if (instr->op == nir_op_ubfe)
2872 opcode = aco_opcode::s_bfe_u64;
2873 else
2874 opcode = aco_opcode::s_bfe_i64;
2875 } else {
2876 unreachable("Unsupported BFE bit size");
2877 }
2878
2879 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2880
2881 } else {
2882 aco_opcode opcode;
2883 if (dst.regClass() == v1) {
2884 if (instr->op == nir_op_ubfe)
2885 opcode = aco_opcode::v_bfe_u32;
2886 else
2887 opcode = aco_opcode::v_bfe_i32;
2888 } else {
2889 unreachable("Unsupported BFE bit size");
2890 }
2891
2892 emit_vop3a_instruction(ctx, instr, opcode, dst);
2893 }
2894 break;
2895 }
2896 case nir_op_bit_count: {
2897 Temp src = get_alu_src(ctx, instr->src[0]);
2898 if (src.regClass() == s1) {
2899 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2900 } else if (src.regClass() == v1) {
2901 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2902 } else if (src.regClass() == v2) {
2903 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2904 emit_extract_vector(ctx, src, 1, v1),
2905 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2906 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2907 } else if (src.regClass() == s2) {
2908 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2909 } else {
2910 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2911 nir_print_instr(&instr->instr, stderr);
2912 fprintf(stderr, "\n");
2913 }
2914 break;
2915 }
2916 case nir_op_flt: {
2917 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2918 break;
2919 }
2920 case nir_op_fge: {
2921 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2922 break;
2923 }
2924 case nir_op_feq: {
2925 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2926 break;
2927 }
2928 case nir_op_fne: {
2929 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2930 break;
2931 }
2932 case nir_op_ilt: {
2933 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_i16, aco_opcode::v_cmp_lt_i32, aco_opcode::v_cmp_lt_i64, aco_opcode::s_cmp_lt_i32);
2934 break;
2935 }
2936 case nir_op_ige: {
2937 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_i16, aco_opcode::v_cmp_ge_i32, aco_opcode::v_cmp_ge_i64, aco_opcode::s_cmp_ge_i32);
2938 break;
2939 }
2940 case nir_op_ieq: {
2941 if (instr->src[0].src.ssa->bit_size == 1)
2942 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2943 else
2944 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_i16, aco_opcode::v_cmp_eq_i32, aco_opcode::v_cmp_eq_i64, aco_opcode::s_cmp_eq_i32,
2945 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2946 break;
2947 }
2948 case nir_op_ine: {
2949 if (instr->src[0].src.ssa->bit_size == 1)
2950 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2951 else
2952 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lg_i16, aco_opcode::v_cmp_lg_i32, aco_opcode::v_cmp_lg_i64, aco_opcode::s_cmp_lg_i32,
2953 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2954 break;
2955 }
2956 case nir_op_ult: {
2957 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_u16, aco_opcode::v_cmp_lt_u32, aco_opcode::v_cmp_lt_u64, aco_opcode::s_cmp_lt_u32);
2958 break;
2959 }
2960 case nir_op_uge: {
2961 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_u16, aco_opcode::v_cmp_ge_u32, aco_opcode::v_cmp_ge_u64, aco_opcode::s_cmp_ge_u32);
2962 break;
2963 }
2964 case nir_op_fddx:
2965 case nir_op_fddy:
2966 case nir_op_fddx_fine:
2967 case nir_op_fddy_fine:
2968 case nir_op_fddx_coarse:
2969 case nir_op_fddy_coarse: {
2970 Temp src = get_alu_src(ctx, instr->src[0]);
2971 uint16_t dpp_ctrl1, dpp_ctrl2;
2972 if (instr->op == nir_op_fddx_fine) {
2973 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2974 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2975 } else if (instr->op == nir_op_fddy_fine) {
2976 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2977 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2978 } else {
2979 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2980 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2981 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2982 else
2983 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2984 }
2985
2986 Temp tmp;
2987 if (ctx->program->chip_class >= GFX8) {
2988 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2989 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2990 } else {
2991 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2992 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2993 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2994 }
2995 emit_wqm(ctx, tmp, dst, true);
2996 break;
2997 }
2998 default:
2999 fprintf(stderr, "Unknown NIR ALU instr: ");
3000 nir_print_instr(&instr->instr, stderr);
3001 fprintf(stderr, "\n");
3002 }
3003 }
3004
3005 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3006 {
3007 Temp dst = get_ssa_temp(ctx, &instr->def);
3008
3009 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3010 // which get truncated the lsb if double and msb if int
3011 // for now, we only use s_mov_b64 with 64bit inline constants
3012 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3013 assert(dst.type() == RegType::sgpr);
3014
3015 Builder bld(ctx->program, ctx->block);
3016
3017 if (instr->def.bit_size == 1) {
3018 assert(dst.regClass() == bld.lm);
3019 int val = instr->value[0].b ? -1 : 0;
3020 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3021 bld.sop1(Builder::s_mov, Definition(dst), op);
3022 } else if (instr->def.bit_size == 8) {
3023 /* ensure that the value is correctly represented in the low byte of the register */
3024 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3025 } else if (instr->def.bit_size == 16) {
3026 /* ensure that the value is correctly represented in the low half of the register */
3027 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3028 } else if (dst.size() == 1) {
3029 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3030 } else {
3031 assert(dst.size() != 1);
3032 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3033 if (instr->def.bit_size == 64)
3034 for (unsigned i = 0; i < dst.size(); i++)
3035 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3036 else {
3037 for (unsigned i = 0; i < dst.size(); i++)
3038 vec->operands[i] = Operand{instr->value[i].u32};
3039 }
3040 vec->definitions[0] = Definition(dst);
3041 ctx->block->instructions.emplace_back(std::move(vec));
3042 }
3043 }
3044
3045 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3046 {
3047 uint32_t new_mask = 0;
3048 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3049 if (mask & (1u << i))
3050 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3051 return new_mask;
3052 }
3053
3054 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst)
3055 {
3056 Builder bld(ctx->program, ctx->block);
3057 if (offset.isTemp()) {
3058 Temp tmp[3] = {vec, vec, vec};
3059
3060 if (vec.size() == 3) {
3061 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
3062 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
3063 } else if (vec.size() == 2) {
3064 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
3065 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
3066 }
3067 for (unsigned i = 0; i < dst.size(); i++)
3068 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
3069
3070 vec = tmp[0];
3071 if (dst.size() == 2)
3072 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
3073
3074 offset = Operand(0u);
3075 }
3076
3077 if (vec.bytes() == dst.bytes() && offset.constantValue() == 0)
3078 bld.copy(Definition(dst), vec);
3079 else
3080 trim_subdword_vector(ctx, vec, dst, vec.bytes(), ((1 << dst.bytes()) - 1) << offset.constantValue());
3081 }
3082
3083 struct LoadEmitInfo {
3084 Operand offset;
3085 Temp dst;
3086 unsigned num_components;
3087 unsigned component_size;
3088 Temp resource = Temp(0, s1);
3089 unsigned component_stride = 0;
3090 unsigned const_offset = 0;
3091 unsigned align_mul = 0;
3092 unsigned align_offset = 0;
3093
3094 bool glc = false;
3095 unsigned swizzle_component_size = 0;
3096 barrier_interaction barrier = barrier_none;
3097 bool can_reorder = true;
3098 Temp soffset = Temp(0, s1);
3099 };
3100
3101 using LoadCallback = Temp(*)(
3102 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3103 unsigned align, unsigned const_offset, Temp dst_hint);
3104
3105 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3106 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3107 {
3108 unsigned load_size = info->num_components * info->component_size;
3109 unsigned component_size = info->component_size;
3110
3111 unsigned num_vals = 0;
3112 Temp vals[info->dst.bytes()];
3113
3114 unsigned const_offset = info->const_offset;
3115
3116 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3117 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3118
3119 unsigned bytes_read = 0;
3120 while (bytes_read < load_size) {
3121 unsigned bytes_needed = load_size - bytes_read;
3122
3123 /* add buffer for unaligned loads */
3124 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3125
3126 if (byte_align) {
3127 if ((bytes_needed > 2 || !supports_8bit_16bit_loads) && byte_align_loads) {
3128 if (info->component_stride) {
3129 assert(supports_8bit_16bit_loads && "unimplemented");
3130 bytes_needed = 2;
3131 byte_align = 0;
3132 } else {
3133 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3134 bytes_needed = align(bytes_needed, 4);
3135 }
3136 } else {
3137 byte_align = 0;
3138 }
3139 }
3140
3141 if (info->swizzle_component_size)
3142 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3143 if (info->component_stride)
3144 bytes_needed = MIN2(bytes_needed, info->component_size);
3145
3146 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3147
3148 /* reduce constant offset */
3149 Operand offset = info->offset;
3150 unsigned reduced_const_offset = const_offset;
3151 bool remove_const_offset_completely = need_to_align_offset;
3152 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3153 unsigned to_add = const_offset;
3154 if (remove_const_offset_completely) {
3155 reduced_const_offset = 0;
3156 } else {
3157 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3158 reduced_const_offset %= max_const_offset_plus_one;
3159 }
3160 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3161 if (offset.isConstant()) {
3162 offset = Operand(offset.constantValue() + to_add);
3163 } else if (offset_tmp.regClass() == s1) {
3164 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3165 offset_tmp, Operand(to_add));
3166 } else if (offset_tmp.regClass() == v1) {
3167 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3168 } else {
3169 Temp lo = bld.tmp(offset_tmp.type(), 1);
3170 Temp hi = bld.tmp(offset_tmp.type(), 1);
3171 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3172
3173 if (offset_tmp.regClass() == s2) {
3174 Temp carry = bld.tmp(s1);
3175 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3176 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3177 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3178 } else {
3179 Temp new_lo = bld.tmp(v1);
3180 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3181 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3182 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3183 }
3184 }
3185 }
3186
3187 /* align offset down if needed */
3188 Operand aligned_offset = offset;
3189 if (need_to_align_offset) {
3190 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3191 if (offset.isConstant()) {
3192 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3193 } else if (offset_tmp.regClass() == s1) {
3194 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3195 } else if (offset_tmp.regClass() == s2) {
3196 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3197 } else if (offset_tmp.regClass() == v1) {
3198 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3199 } else if (offset_tmp.regClass() == v2) {
3200 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3201 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3202 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3203 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3204 }
3205 }
3206 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3207 bld.copy(bld.def(s1), aligned_offset);
3208
3209 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3210 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3211 reduced_const_offset, byte_align ? Temp() : info->dst);
3212
3213 /* shift result right if needed */
3214 if (byte_align) {
3215 Operand align((uint32_t)byte_align);
3216 if (byte_align == -1) {
3217 if (offset.isConstant())
3218 align = Operand(offset.constantValue() % 4u);
3219 else if (offset.size() == 2)
3220 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3221 else
3222 align = offset;
3223 }
3224
3225 if (align.isTemp() || align.constantValue()) {
3226 assert(val.bytes() >= load_size && "unimplemented");
3227 Temp new_val = bld.tmp(RegClass::get(val.type(), load_size));
3228 if (val.type() == RegType::sgpr)
3229 byte_align_scalar(ctx, val, align, new_val);
3230 else
3231 byte_align_vector(ctx, val, align, new_val);
3232 val = new_val;
3233 }
3234 }
3235
3236 /* add result to list and advance */
3237 if (info->component_stride) {
3238 assert(val.bytes() == info->component_size && "unimplemented");
3239 const_offset += info->component_stride;
3240 align_offset = (align_offset + info->component_stride) % align_mul;
3241 } else {
3242 const_offset += val.bytes();
3243 align_offset = (align_offset + val.bytes()) % align_mul;
3244 }
3245 bytes_read += val.bytes();
3246 vals[num_vals++] = val;
3247 }
3248
3249 /* the callback wrote directly to dst */
3250 if (vals[0] == info->dst) {
3251 assert(num_vals == 1);
3252 emit_split_vector(ctx, info->dst, info->num_components);
3253 return;
3254 }
3255
3256 /* create array of components */
3257 unsigned components_split = 0;
3258 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3259 bool has_vgprs = false;
3260 for (unsigned i = 0; i < num_vals;) {
3261 Temp tmp[num_vals];
3262 unsigned num_tmps = 0;
3263 unsigned tmp_size = 0;
3264 RegType reg_type = RegType::sgpr;
3265 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3266 if (vals[i].type() == RegType::vgpr)
3267 reg_type = RegType::vgpr;
3268 tmp_size += vals[i].bytes();
3269 tmp[num_tmps++] = vals[i++];
3270 }
3271 if (num_tmps > 1) {
3272 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3273 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3274 for (unsigned i = 0; i < num_vals; i++)
3275 vec->operands[i] = Operand(tmp[i]);
3276 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3277 vec->definitions[0] = Definition(tmp[0]);
3278 bld.insert(std::move(vec));
3279 }
3280
3281 if (tmp[0].bytes() % component_size) {
3282 /* trim tmp[0] */
3283 assert(i == num_vals);
3284 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3285 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3286 }
3287
3288 RegClass elem_rc = RegClass::get(reg_type, component_size);
3289
3290 unsigned start = components_split;
3291
3292 if (tmp_size == elem_rc.bytes()) {
3293 allocated_vec[components_split++] = tmp[0];
3294 } else {
3295 assert(tmp_size % elem_rc.bytes() == 0);
3296 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3297 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3298 for (unsigned i = 0; i < split->definitions.size(); i++) {
3299 Temp component = bld.tmp(elem_rc);
3300 allocated_vec[components_split++] = component;
3301 split->definitions[i] = Definition(component);
3302 }
3303 split->operands[0] = Operand(tmp[0]);
3304 bld.insert(std::move(split));
3305 }
3306
3307 /* try to p_as_uniform early so we can create more optimizable code and
3308 * also update allocated_vec */
3309 for (unsigned j = start; j < components_split; j++) {
3310 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3311 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3312 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3313 }
3314 }
3315
3316 /* concatenate components and p_as_uniform() result if needed */
3317 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3318 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3319
3320 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3321
3322 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3323 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3324 for (unsigned i = 0; i < info->num_components; i++)
3325 vec->operands[i] = Operand(allocated_vec[i]);
3326 if (padding_bytes)
3327 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3328 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3329 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3330 vec->definitions[0] = Definition(tmp);
3331 bld.insert(std::move(vec));
3332 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3333 } else {
3334 vec->definitions[0] = Definition(info->dst);
3335 bld.insert(std::move(vec));
3336 }
3337 }
3338
3339 Operand load_lds_size_m0(Builder& bld)
3340 {
3341 /* TODO: m0 does not need to be initialized on GFX9+ */
3342 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3343 }
3344
3345 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3346 Temp offset, unsigned bytes_needed,
3347 unsigned align, unsigned const_offset,
3348 Temp dst_hint)
3349 {
3350 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3351
3352 Operand m = load_lds_size_m0(bld);
3353
3354 bool large_ds_read = bld.program->chip_class >= GFX7;
3355 bool usable_read2 = bld.program->chip_class >= GFX7;
3356
3357 bool read2 = false;
3358 unsigned size = 0;
3359 aco_opcode op;
3360 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3361 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3362 size = 16;
3363 op = aco_opcode::ds_read_b128;
3364 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3365 size = 16;
3366 read2 = true;
3367 op = aco_opcode::ds_read2_b64;
3368 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3369 size = 12;
3370 op = aco_opcode::ds_read_b96;
3371 } else if (bytes_needed >= 8 && align % 8 == 0) {
3372 size = 8;
3373 op = aco_opcode::ds_read_b64;
3374 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3375 size = 8;
3376 read2 = true;
3377 op = aco_opcode::ds_read2_b32;
3378 } else if (bytes_needed >= 4 && align % 4 == 0) {
3379 size = 4;
3380 op = aco_opcode::ds_read_b32;
3381 } else if (bytes_needed >= 2 && align % 2 == 0) {
3382 size = 2;
3383 op = aco_opcode::ds_read_u16;
3384 } else {
3385 size = 1;
3386 op = aco_opcode::ds_read_u8;
3387 }
3388
3389 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3390 if (const_offset >= max_offset_plus_one) {
3391 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3392 const_offset %= max_offset_plus_one;
3393 }
3394
3395 if (read2)
3396 const_offset /= (size / 2u);
3397
3398 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3399 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3400 if (read2)
3401 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3402 else
3403 bld.ds(op, Definition(val), offset, m, const_offset);
3404
3405 if (size < 4)
3406 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3407
3408 return val;
3409 }
3410
3411 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3412
3413 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3414 Temp offset, unsigned bytes_needed,
3415 unsigned align, unsigned const_offset,
3416 Temp dst_hint)
3417 {
3418 unsigned size = 0;
3419 aco_opcode op;
3420 if (bytes_needed <= 4) {
3421 size = 1;
3422 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3423 } else if (bytes_needed <= 8) {
3424 size = 2;
3425 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3426 } else if (bytes_needed <= 16) {
3427 size = 4;
3428 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3429 } else if (bytes_needed <= 32) {
3430 size = 8;
3431 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3432 } else {
3433 size = 16;
3434 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3435 }
3436 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3437 if (info->resource.id()) {
3438 load->operands[0] = Operand(info->resource);
3439 load->operands[1] = Operand(offset);
3440 } else {
3441 load->operands[0] = Operand(offset);
3442 load->operands[1] = Operand(0u);
3443 }
3444 RegClass rc(RegType::sgpr, size);
3445 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3446 load->definitions[0] = Definition(val);
3447 load->glc = info->glc;
3448 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3449 load->barrier = info->barrier;
3450 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3451 bld.insert(std::move(load));
3452 return val;
3453 }
3454
3455 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3456
3457 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3458 Temp offset, unsigned bytes_needed,
3459 unsigned align_, unsigned const_offset,
3460 Temp dst_hint)
3461 {
3462 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3463 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3464
3465 if (info->soffset.id()) {
3466 if (soffset.isTemp())
3467 vaddr = bld.copy(bld.def(v1), soffset);
3468 soffset = Operand(info->soffset);
3469 }
3470
3471 unsigned bytes_size = 0;
3472 aco_opcode op;
3473 if (bytes_needed == 1) {
3474 bytes_size = 1;
3475 op = aco_opcode::buffer_load_ubyte;
3476 } else if (bytes_needed == 2) {
3477 bytes_size = 2;
3478 op = aco_opcode::buffer_load_ushort;
3479 } else if (bytes_needed <= 4) {
3480 bytes_size = 4;
3481 op = aco_opcode::buffer_load_dword;
3482 } else if (bytes_needed <= 8) {
3483 bytes_size = 8;
3484 op = aco_opcode::buffer_load_dwordx2;
3485 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3486 bytes_size = 12;
3487 op = aco_opcode::buffer_load_dwordx3;
3488 } else {
3489 bytes_size = 16;
3490 op = aco_opcode::buffer_load_dwordx4;
3491 }
3492 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3493 mubuf->operands[0] = Operand(info->resource);
3494 mubuf->operands[1] = vaddr;
3495 mubuf->operands[2] = soffset;
3496 mubuf->offen = (offset.type() == RegType::vgpr);
3497 mubuf->glc = info->glc;
3498 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3499 mubuf->barrier = info->barrier;
3500 mubuf->can_reorder = info->can_reorder;
3501 mubuf->offset = const_offset;
3502 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3503 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3504 mubuf->definitions[0] = Definition(val);
3505 bld.insert(std::move(mubuf));
3506
3507 if (bytes_size < 4)
3508 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3509
3510 return val;
3511 }
3512
3513 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3514
3515 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3516 {
3517 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3518 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3519
3520 if (addr.type() == RegType::vgpr)
3521 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3522 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3523 }
3524
3525 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3526 Temp offset, unsigned bytes_needed,
3527 unsigned align_, unsigned const_offset,
3528 Temp dst_hint)
3529 {
3530 unsigned bytes_size = 0;
3531 bool mubuf = bld.program->chip_class == GFX6;
3532 bool global = bld.program->chip_class >= GFX9;
3533 aco_opcode op;
3534 if (bytes_needed == 1) {
3535 bytes_size = 1;
3536 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3537 } else if (bytes_needed == 2) {
3538 bytes_size = 2;
3539 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3540 } else if (bytes_needed <= 4) {
3541 bytes_size = 4;
3542 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3543 } else if (bytes_needed <= 8) {
3544 bytes_size = 8;
3545 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3546 } else if (bytes_needed <= 12 && !mubuf) {
3547 bytes_size = 12;
3548 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3549 } else {
3550 bytes_size = 16;
3551 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3552 }
3553 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3554 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3555 if (mubuf) {
3556 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3557 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3558 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3559 mubuf->operands[2] = Operand(0u);
3560 mubuf->glc = info->glc;
3561 mubuf->dlc = false;
3562 mubuf->offset = 0;
3563 mubuf->addr64 = offset.type() == RegType::vgpr;
3564 mubuf->disable_wqm = false;
3565 mubuf->barrier = info->barrier;
3566 mubuf->definitions[0] = Definition(val);
3567 bld.insert(std::move(mubuf));
3568 } else {
3569 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3570
3571 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3572 flat->operands[0] = Operand(offset);
3573 flat->operands[1] = Operand(s1);
3574 flat->glc = info->glc;
3575 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3576 flat->barrier = info->barrier;
3577 flat->offset = 0u;
3578 flat->definitions[0] = Definition(val);
3579 bld.insert(std::move(flat));
3580 }
3581
3582 if (bytes_size < 4)
3583 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3584
3585 return val;
3586 }
3587
3588 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3589
3590 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3591 Temp address, unsigned base_offset, unsigned align)
3592 {
3593 assert(util_is_power_of_two_nonzero(align));
3594
3595 Builder bld(ctx->program, ctx->block);
3596
3597 unsigned num_components = dst.bytes() / elem_size_bytes;
3598 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3599 info.align_mul = align;
3600 info.align_offset = 0;
3601 info.barrier = barrier_shared;
3602 info.can_reorder = false;
3603 info.const_offset = base_offset;
3604 emit_lds_load(ctx, bld, &info);
3605
3606 return dst;
3607 }
3608
3609 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3610 {
3611 if (!count)
3612 return;
3613
3614 Builder bld(ctx->program, ctx->block);
3615
3616 ASSERTED bool is_subdword = false;
3617 for (unsigned i = 0; i < count; i++)
3618 is_subdword |= offsets[i] % 4;
3619 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3620 assert(!is_subdword || dst_type == RegType::vgpr);
3621
3622 /* count == 1 fast path */
3623 if (count == 1) {
3624 if (dst_type == RegType::sgpr)
3625 dst[0] = bld.as_uniform(src);
3626 else
3627 dst[0] = as_vgpr(ctx, src);
3628 return;
3629 }
3630
3631 for (unsigned i = 0; i < count - 1; i++)
3632 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3633 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3634
3635 if (is_subdword && src.type() == RegType::sgpr) {
3636 src = as_vgpr(ctx, src);
3637 } else {
3638 /* use allocated_vec if possible */
3639 auto it = ctx->allocated_vec.find(src.id());
3640 if (it != ctx->allocated_vec.end()) {
3641 unsigned total_size = 0;
3642 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3643 total_size += it->second[i].bytes();
3644 if (total_size != src.bytes())
3645 goto split;
3646
3647 unsigned elem_size = it->second[0].bytes();
3648
3649 for (unsigned i = 0; i < count; i++) {
3650 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3651 goto split;
3652 }
3653
3654 for (unsigned i = 0; i < count; i++) {
3655 unsigned start_idx = offsets[i] / elem_size;
3656 unsigned op_count = dst[i].bytes() / elem_size;
3657 if (op_count == 1) {
3658 if (dst_type == RegType::sgpr)
3659 dst[i] = bld.as_uniform(it->second[start_idx]);
3660 else
3661 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3662 continue;
3663 }
3664
3665 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3666 for (unsigned j = 0; j < op_count; j++) {
3667 Temp tmp = it->second[start_idx + j];
3668 if (dst_type == RegType::sgpr)
3669 tmp = bld.as_uniform(tmp);
3670 vec->operands[j] = Operand(tmp);
3671 }
3672 vec->definitions[0] = Definition(dst[i]);
3673 bld.insert(std::move(vec));
3674 }
3675 return;
3676 }
3677 }
3678
3679 if (dst_type == RegType::sgpr)
3680 src = bld.as_uniform(src);
3681
3682 split:
3683 /* just split it */
3684 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3685 split->operands[0] = Operand(src);
3686 for (unsigned i = 0; i < count; i++)
3687 split->definitions[i] = Definition(dst[i]);
3688 bld.insert(std::move(split));
3689 }
3690
3691 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3692 int *start, int *count)
3693 {
3694 unsigned start_elem = ffs(todo_mask) - 1;
3695 bool skip = !(mask & (1 << start_elem));
3696 if (skip)
3697 mask = ~mask & todo_mask;
3698
3699 mask &= todo_mask;
3700
3701 u_bit_scan_consecutive_range(&mask, start, count);
3702
3703 return !skip;
3704 }
3705
3706 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3707 {
3708 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3709 }
3710
3711 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3712 Temp address, unsigned base_offset, unsigned align)
3713 {
3714 assert(util_is_power_of_two_nonzero(align));
3715 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3716
3717 Builder bld(ctx->program, ctx->block);
3718 bool large_ds_write = ctx->options->chip_class >= GFX7;
3719 bool usable_write2 = ctx->options->chip_class >= GFX7;
3720
3721 unsigned write_count = 0;
3722 Temp write_datas[32];
3723 unsigned offsets[32];
3724 aco_opcode opcodes[32];
3725
3726 wrmask = widen_mask(wrmask, elem_size_bytes);
3727
3728 uint32_t todo = u_bit_consecutive(0, data.bytes());
3729 while (todo) {
3730 int offset, bytes;
3731 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3732 offsets[write_count] = offset;
3733 opcodes[write_count] = aco_opcode::num_opcodes;
3734 write_count++;
3735 advance_write_mask(&todo, offset, bytes);
3736 continue;
3737 }
3738
3739 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3740 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3741 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3742 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3743
3744 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3745 aco_opcode op = aco_opcode::num_opcodes;
3746 if (bytes >= 16 && aligned16 && large_ds_write) {
3747 op = aco_opcode::ds_write_b128;
3748 bytes = 16;
3749 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3750 op = aco_opcode::ds_write_b96;
3751 bytes = 12;
3752 } else if (bytes >= 8 && aligned8) {
3753 op = aco_opcode::ds_write_b64;
3754 bytes = 8;
3755 } else if (bytes >= 4 && aligned4) {
3756 op = aco_opcode::ds_write_b32;
3757 bytes = 4;
3758 } else if (bytes >= 2 && aligned2) {
3759 op = aco_opcode::ds_write_b16;
3760 bytes = 2;
3761 } else if (bytes >= 1) {
3762 op = aco_opcode::ds_write_b8;
3763 bytes = 1;
3764 } else {
3765 assert(false);
3766 }
3767
3768 offsets[write_count] = offset;
3769 opcodes[write_count] = op;
3770 write_count++;
3771 advance_write_mask(&todo, offset, bytes);
3772 }
3773
3774 Operand m = load_lds_size_m0(bld);
3775
3776 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3777
3778 for (unsigned i = 0; i < write_count; i++) {
3779 aco_opcode op = opcodes[i];
3780 if (op == aco_opcode::num_opcodes)
3781 continue;
3782
3783 Temp data = write_datas[i];
3784
3785 unsigned second = write_count;
3786 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3787 for (second = i + 1; second < write_count; second++) {
3788 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3789 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3790 opcodes[second] = aco_opcode::num_opcodes;
3791 break;
3792 }
3793 }
3794 }
3795
3796 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3797 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3798
3799 unsigned inline_offset = base_offset + offsets[i];
3800 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3801 Temp address_offset = address;
3802 if (inline_offset > max_offset) {
3803 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3804 inline_offset = offsets[i];
3805 }
3806 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3807
3808 if (write2) {
3809 Temp second_data = write_datas[second];
3810 inline_offset /= data.bytes();
3811 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3812 } else {
3813 bld.ds(op, address_offset, data, m, inline_offset);
3814 }
3815 }
3816 }
3817
3818 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3819 {
3820 unsigned align = 16;
3821 if (const_offset)
3822 align = std::min(align, 1u << (ffs(const_offset) - 1));
3823
3824 return align;
3825 }
3826
3827
3828 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3829 {
3830 switch (bytes) {
3831 case 1:
3832 assert(!smem);
3833 return aco_opcode::buffer_store_byte;
3834 case 2:
3835 assert(!smem);
3836 return aco_opcode::buffer_store_short;
3837 case 4:
3838 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3839 case 8:
3840 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3841 case 12:
3842 assert(!smem);
3843 return aco_opcode::buffer_store_dwordx3;
3844 case 16:
3845 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3846 }
3847 unreachable("Unexpected store size");
3848 return aco_opcode::num_opcodes;
3849 }
3850
3851 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3852 Temp data, unsigned writemask, int swizzle_element_size,
3853 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3854 {
3855 unsigned write_count_with_skips = 0;
3856 bool skips[16];
3857
3858 /* determine how to split the data */
3859 unsigned todo = u_bit_consecutive(0, data.bytes());
3860 while (todo) {
3861 int offset, bytes;
3862 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3863 offsets[write_count_with_skips] = offset;
3864 if (skips[write_count_with_skips]) {
3865 advance_write_mask(&todo, offset, bytes);
3866 write_count_with_skips++;
3867 continue;
3868 }
3869
3870 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3871 * larger than swizzle_element_size */
3872 bytes = MIN2(bytes, swizzle_element_size);
3873 if (bytes % 4)
3874 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3875
3876 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3877 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3878 bytes = 8;
3879
3880 /* dword or larger stores have to be dword-aligned */
3881 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3882 unsigned align_offset = instr ? nir_intrinsic_align_mul(instr) : 0;
3883 bool dword_aligned = (align_offset + offset) % 4 == 0 && align_mul % 4 == 0;
3884 if (bytes >= 4 && !dword_aligned)
3885 bytes = MIN2(bytes, 2);
3886
3887 advance_write_mask(&todo, offset, bytes);
3888 write_count_with_skips++;
3889 }
3890
3891 /* actually split data */
3892 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3893
3894 /* remove skips */
3895 for (unsigned i = 0; i < write_count_with_skips; i++) {
3896 if (skips[i])
3897 continue;
3898 write_datas[*write_count] = write_datas[i];
3899 offsets[*write_count] = offsets[i];
3900 (*write_count)++;
3901 }
3902 }
3903
3904 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3905 unsigned split_cnt = 0u, Temp dst = Temp())
3906 {
3907 Builder bld(ctx->program, ctx->block);
3908 unsigned dword_size = elem_size_bytes / 4;
3909
3910 if (!dst.id())
3911 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3912
3913 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3914 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3915 instr->definitions[0] = Definition(dst);
3916
3917 for (unsigned i = 0; i < cnt; ++i) {
3918 if (arr[i].id()) {
3919 assert(arr[i].size() == dword_size);
3920 allocated_vec[i] = arr[i];
3921 instr->operands[i] = Operand(arr[i]);
3922 } else {
3923 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3924 allocated_vec[i] = zero;
3925 instr->operands[i] = Operand(zero);
3926 }
3927 }
3928
3929 bld.insert(std::move(instr));
3930
3931 if (split_cnt)
3932 emit_split_vector(ctx, dst, split_cnt);
3933 else
3934 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3935
3936 return dst;
3937 }
3938
3939 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3940 {
3941 if (const_offset >= 4096) {
3942 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3943 const_offset %= 4096u;
3944
3945 if (!voffset.id())
3946 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3947 else if (unlikely(voffset.regClass() == s1))
3948 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3949 else if (likely(voffset.regClass() == v1))
3950 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3951 else
3952 unreachable("Unsupported register class of voffset");
3953 }
3954
3955 return const_offset;
3956 }
3957
3958 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3959 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3960 {
3961 assert(vdata.id());
3962 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3963 assert(vdata.size() >= 1 && vdata.size() <= 4);
3964
3965 Builder bld(ctx->program, ctx->block);
3966 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
3967 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3968
3969 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3970 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3971 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3972 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3973 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3974
3975 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3976 }
3977
3978 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3979 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3980 bool allow_combining = true, bool reorder = true, bool slc = false)
3981 {
3982 Builder bld(ctx->program, ctx->block);
3983 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3984 assert(write_mask);
3985 write_mask = widen_mask(write_mask, elem_size_bytes);
3986
3987 unsigned write_count = 0;
3988 Temp write_datas[32];
3989 unsigned offsets[32];
3990 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
3991 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
3992
3993 for (unsigned i = 0; i < write_count; i++) {
3994 unsigned const_offset = offsets[i] + base_const_offset;
3995 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc);
3996 }
3997 }
3998
3999 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4000 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4001 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4002 {
4003 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
4004 assert((num_components * elem_size_bytes / 4) == dst.size());
4005 assert(!!stride != allow_combining);
4006
4007 Builder bld(ctx->program, ctx->block);
4008
4009 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4010 info.component_stride = allow_combining ? 0 : stride;
4011 info.glc = true;
4012 info.swizzle_component_size = allow_combining ? 0 : 4;
4013 info.align_mul = MIN2(elem_size_bytes, 4);
4014 info.align_offset = 0;
4015 info.soffset = soffset;
4016 info.const_offset = base_const_offset;
4017 emit_mubuf_load(ctx, bld, &info);
4018 }
4019
4020 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)
4021 {
4022 Builder bld(ctx->program, ctx->block);
4023 Temp offset = base_offset.first;
4024 unsigned const_offset = base_offset.second;
4025
4026 if (!nir_src_is_const(*off_src)) {
4027 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4028 Temp with_stride;
4029
4030 /* Calculate indirect offset with stride */
4031 if (likely(indirect_offset_arg.regClass() == v1))
4032 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4033 else if (indirect_offset_arg.regClass() == s1)
4034 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4035 else
4036 unreachable("Unsupported register class of indirect offset");
4037
4038 /* Add to the supplied base offset */
4039 if (offset.id() == 0)
4040 offset = with_stride;
4041 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4042 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4043 else if (offset.size() == 1 && with_stride.size() == 1)
4044 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4045 else
4046 unreachable("Unsupported register class of indirect offset");
4047 } else {
4048 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4049 const_offset += const_offset_arg * stride;
4050 }
4051
4052 return std::make_pair(offset, const_offset);
4053 }
4054
4055 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4056 {
4057 Builder bld(ctx->program, ctx->block);
4058 Temp offset;
4059
4060 if (off1.first.id() && off2.first.id()) {
4061 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4062 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4063 else if (off1.first.size() == 1 && off2.first.size() == 1)
4064 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4065 else
4066 unreachable("Unsupported register class of indirect offset");
4067 } else {
4068 offset = off1.first.id() ? off1.first : off2.first;
4069 }
4070
4071 return std::make_pair(offset, off1.second + off2.second);
4072 }
4073
4074 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4075 {
4076 Builder bld(ctx->program, ctx->block);
4077 unsigned const_offset = offs.second * multiplier;
4078
4079 if (!offs.first.id())
4080 return std::make_pair(offs.first, const_offset);
4081
4082 Temp offset = unlikely(offs.first.regClass() == s1)
4083 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4084 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4085
4086 return std::make_pair(offset, const_offset);
4087 }
4088
4089 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4090 {
4091 Builder bld(ctx->program, ctx->block);
4092
4093 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4094 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4095 /* component is in bytes */
4096 const_offset += nir_intrinsic_component(instr) * component_stride;
4097
4098 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4099 nir_src *off_src = nir_get_io_offset_src(instr);
4100 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4101 }
4102
4103 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4104 {
4105 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4106 }
4107
4108 Temp get_tess_rel_patch_id(isel_context *ctx)
4109 {
4110 Builder bld(ctx->program, ctx->block);
4111
4112 switch (ctx->shader->info.stage) {
4113 case MESA_SHADER_TESS_CTRL:
4114 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4115 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4116 case MESA_SHADER_TESS_EVAL:
4117 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4118 default:
4119 unreachable("Unsupported stage in get_tess_rel_patch_id");
4120 }
4121 }
4122
4123 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4124 {
4125 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4126 Builder bld(ctx->program, ctx->block);
4127
4128 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4129 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4130
4131 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4132
4133 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4134 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4135
4136 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4137 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4138 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4139
4140 return offset_mul(ctx, offs, 4u);
4141 }
4142
4143 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4144 {
4145 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4146 Builder bld(ctx->program, ctx->block);
4147
4148 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4149 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
4150 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
4151 uint32_t output_vertex_size = num_tcs_outputs * 16;
4152 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4153 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
4154
4155 std::pair<Temp, unsigned> offs = instr
4156 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4157 : std::make_pair(Temp(), 0u);
4158
4159 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4160 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4161
4162 if (per_vertex) {
4163 assert(instr);
4164
4165 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4166 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4167
4168 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4169 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4170 } else {
4171 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4172 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4173 }
4174
4175 return offs;
4176 }
4177
4178 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4179 {
4180 Builder bld(ctx->program, ctx->block);
4181
4182 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4183 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4184
4185 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4186
4187 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4188 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4189 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4190
4191 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4192 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4193
4194 return offs;
4195 }
4196
4197 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4198 {
4199 Builder bld(ctx->program, ctx->block);
4200
4201 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
4202 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
4203 : ctx->args->options->key.tes.tcs_num_outputs;
4204
4205 unsigned output_vertex_size = num_tcs_outputs * 16;
4206 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4207 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4208 unsigned attr_stride = ctx->tcs_num_patches;
4209
4210 std::pair<Temp, unsigned> offs = instr
4211 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4212 : std::make_pair(Temp(), 0u);
4213
4214 if (const_base_offset)
4215 offs.second += const_base_offset * attr_stride;
4216
4217 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4218 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4219 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4220
4221 return offs;
4222 }
4223
4224 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4225 {
4226 if (mask == 0)
4227 return false;
4228
4229 unsigned off = nir_intrinsic_base(instr) * 4u;
4230 nir_src *off_src = nir_get_io_offset_src(instr);
4231
4232 if (!nir_src_is_const(*off_src)) {
4233 *indirect = true;
4234 return false;
4235 }
4236
4237 *indirect = false;
4238 off += nir_src_as_uint(*off_src) * 16u;
4239
4240 while (mask) {
4241 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
4242 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
4243 return true;
4244 }
4245
4246 return false;
4247 }
4248
4249 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4250 {
4251 unsigned write_mask = nir_intrinsic_write_mask(instr);
4252 unsigned component = nir_intrinsic_component(instr);
4253 unsigned idx = nir_intrinsic_base(instr) + component;
4254
4255 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4256 if (off_instr->type != nir_instr_type_load_const)
4257 return false;
4258
4259 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4260 idx += nir_src_as_uint(instr->src[1]) * 4u;
4261
4262 if (instr->src[0].ssa->bit_size == 64)
4263 write_mask = widen_mask(write_mask, 2);
4264
4265 for (unsigned i = 0; i < 8; ++i) {
4266 if (write_mask & (1 << i)) {
4267 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4268 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
4269 }
4270 idx++;
4271 }
4272
4273 return true;
4274 }
4275
4276 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4277 {
4278 /* Only TCS per-vertex inputs are supported by this function.
4279 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4280 */
4281 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4282 return false;
4283
4284 nir_src *off_src = nir_get_io_offset_src(instr);
4285 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4286 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4287 bool can_use_temps = nir_src_is_const(*off_src) &&
4288 vertex_index_instr->type == nir_instr_type_intrinsic &&
4289 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4290
4291 if (!can_use_temps)
4292 return false;
4293
4294 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4295 Temp *src = &ctx->inputs.temps[idx];
4296 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4297
4298 return true;
4299 }
4300
4301 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4302 {
4303 Builder bld(ctx->program, ctx->block);
4304
4305 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4306 /* 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. */
4307 bool indirect_write;
4308 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4309 if (temp_only_input && !indirect_write)
4310 return;
4311 }
4312
4313 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4314 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4315 unsigned write_mask = nir_intrinsic_write_mask(instr);
4316 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4317
4318 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4319 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4320 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4321 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4322 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4323 } else {
4324 Temp lds_base;
4325
4326 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4327 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4328 unsigned itemsize = ctx->stage == vertex_geometry_gs
4329 ? ctx->program->info->vs.es_info.esgs_itemsize
4330 : ctx->program->info->tes.es_info.esgs_itemsize;
4331 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4332 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));
4333 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4334 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4335 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4336 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4337 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4338 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4339 */
4340 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
4341 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4342 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
4343 } else {
4344 unreachable("Invalid LS or ES stage");
4345 }
4346
4347 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4348 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4349 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4350 }
4351 }
4352
4353 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4354 {
4355 if (per_vertex)
4356 return false;
4357
4358 unsigned off = nir_intrinsic_base(instr) * 4u;
4359 return off == ctx->tcs_tess_lvl_out_loc ||
4360 off == ctx->tcs_tess_lvl_in_loc;
4361
4362 }
4363
4364 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4365 {
4366 uint64_t mask = per_vertex
4367 ? ctx->program->info->tcs.tes_inputs_read
4368 : ctx->program->info->tcs.tes_patch_inputs_read;
4369
4370 bool indirect_write = false;
4371 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4372 return indirect_write || output_read_by_tes;
4373 }
4374
4375 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4376 {
4377 uint64_t mask = per_vertex
4378 ? ctx->shader->info.outputs_read
4379 : ctx->shader->info.patch_outputs_read;
4380
4381 bool indirect_write = false;
4382 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4383 return indirect_write || output_read;
4384 }
4385
4386 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4387 {
4388 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4389 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4390
4391 Builder bld(ctx->program, ctx->block);
4392
4393 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4394 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4395 unsigned write_mask = nir_intrinsic_write_mask(instr);
4396
4397 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4398 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4399 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4400
4401 if (write_to_vmem) {
4402 std::pair<Temp, unsigned> vmem_offs = per_vertex
4403 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4404 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4405
4406 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));
4407 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4408 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);
4409 }
4410
4411 if (write_to_lds) {
4412 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4413 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4414 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4415 }
4416 }
4417
4418 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4419 {
4420 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4421 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4422
4423 Builder bld(ctx->program, ctx->block);
4424
4425 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4426 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4427 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4428 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4429
4430 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4431 }
4432
4433 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4434 {
4435 if (ctx->stage == vertex_vs ||
4436 ctx->stage == tess_eval_vs ||
4437 ctx->stage == fragment_fs ||
4438 ctx->stage == ngg_vertex_gs ||
4439 ctx->stage == ngg_tess_eval_gs ||
4440 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4441 bool stored_to_temps = store_output_to_temps(ctx, instr);
4442 if (!stored_to_temps) {
4443 fprintf(stderr, "Unimplemented output offset instruction:\n");
4444 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4445 fprintf(stderr, "\n");
4446 abort();
4447 }
4448 } else if (ctx->stage == vertex_es ||
4449 ctx->stage == vertex_ls ||
4450 ctx->stage == tess_eval_es ||
4451 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4452 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4453 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4454 visit_store_ls_or_es_output(ctx, instr);
4455 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4456 visit_store_tcs_output(ctx, instr, false);
4457 } else {
4458 unreachable("Shader stage not implemented");
4459 }
4460 }
4461
4462 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4463 {
4464 visit_load_tcs_output(ctx, instr, false);
4465 }
4466
4467 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4468 {
4469 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4470 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4471
4472 Builder bld(ctx->program, ctx->block);
4473 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
4474 if (ctx->program->has_16bank_lds)
4475 interp_p1.instr->operands[0].setLateKill(true);
4476 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
4477 }
4478
4479 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4480 {
4481 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4482 for (unsigned i = 0; i < num_components; i++)
4483 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4484 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4485 assert(num_components == 4);
4486 Builder bld(ctx->program, ctx->block);
4487 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4488 }
4489
4490 for (Operand& op : vec->operands)
4491 op = op.isUndefined() ? Operand(0u) : op;
4492
4493 vec->definitions[0] = Definition(dst);
4494 ctx->block->instructions.emplace_back(std::move(vec));
4495 emit_split_vector(ctx, dst, num_components);
4496 return;
4497 }
4498
4499 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4500 {
4501 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4502 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4503 unsigned idx = nir_intrinsic_base(instr);
4504 unsigned component = nir_intrinsic_component(instr);
4505 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4506
4507 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4508 if (offset) {
4509 assert(offset->u32 == 0);
4510 } else {
4511 /* the lower 15bit of the prim_mask contain the offset into LDS
4512 * while the upper bits contain the number of prims */
4513 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4514 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4515 Builder bld(ctx->program, ctx->block);
4516 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4517 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4518 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4519 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4520 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4521 }
4522
4523 if (instr->dest.ssa.num_components == 1) {
4524 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4525 } else {
4526 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4527 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4528 {
4529 Temp tmp = {ctx->program->allocateId(), v1};
4530 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4531 vec->operands[i] = Operand(tmp);
4532 }
4533 vec->definitions[0] = Definition(dst);
4534 ctx->block->instructions.emplace_back(std::move(vec));
4535 }
4536 }
4537
4538 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4539 unsigned offset, unsigned stride, unsigned channels)
4540 {
4541 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4542 if (vtx_info->chan_byte_size != 4 && channels == 3)
4543 return false;
4544 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4545 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4546 }
4547
4548 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4549 unsigned offset, unsigned stride, unsigned *channels)
4550 {
4551 if (!vtx_info->chan_byte_size) {
4552 *channels = vtx_info->num_channels;
4553 return vtx_info->chan_format;
4554 }
4555
4556 unsigned num_channels = *channels;
4557 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4558 unsigned new_channels = num_channels + 1;
4559 /* first, assume more loads is worse and try using a larger data format */
4560 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4561 new_channels++;
4562 /* don't make the attribute potentially out-of-bounds */
4563 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4564 new_channels = 5;
4565 }
4566
4567 if (new_channels == 5) {
4568 /* then try decreasing load size (at the cost of more loads) */
4569 new_channels = *channels;
4570 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4571 new_channels--;
4572 }
4573
4574 if (new_channels < *channels)
4575 *channels = new_channels;
4576 num_channels = new_channels;
4577 }
4578
4579 switch (vtx_info->chan_format) {
4580 case V_008F0C_BUF_DATA_FORMAT_8:
4581 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4582 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4583 case V_008F0C_BUF_DATA_FORMAT_16:
4584 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4585 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4586 case V_008F0C_BUF_DATA_FORMAT_32:
4587 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4588 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4589 }
4590 unreachable("shouldn't reach here");
4591 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4592 }
4593
4594 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4595 * so we may need to fix it up. */
4596 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4597 {
4598 Builder bld(ctx->program, ctx->block);
4599
4600 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4601 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4602
4603 /* For the integer-like cases, do a natural sign extension.
4604 *
4605 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4606 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4607 * exponent.
4608 */
4609 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4610 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4611
4612 /* Convert back to the right type. */
4613 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4614 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4615 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4616 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4617 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4618 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4619 }
4620
4621 return alpha;
4622 }
4623
4624 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4625 {
4626 Builder bld(ctx->program, ctx->block);
4627 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4628 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4629
4630 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4631 if (off_instr->type != nir_instr_type_load_const) {
4632 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4633 nir_print_instr(off_instr, stderr);
4634 fprintf(stderr, "\n");
4635 }
4636 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4637
4638 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4639
4640 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4641 unsigned component = nir_intrinsic_component(instr);
4642 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4643 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4644 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4645 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4646
4647 unsigned dfmt = attrib_format & 0xf;
4648 unsigned nfmt = (attrib_format >> 4) & 0x7;
4649 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4650
4651 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4652 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4653 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4654 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4655 if (post_shuffle)
4656 num_channels = MAX2(num_channels, 3);
4657
4658 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4659 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4660
4661 Temp index;
4662 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4663 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4664 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4665 if (divisor) {
4666 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4667 if (divisor != 1) {
4668 Temp divided = bld.tmp(v1);
4669 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4670 index = bld.vadd32(bld.def(v1), start_instance, divided);
4671 } else {
4672 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4673 }
4674 } else {
4675 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4676 }
4677 } else {
4678 index = bld.vadd32(bld.def(v1),
4679 get_arg(ctx, ctx->args->ac.base_vertex),
4680 get_arg(ctx, ctx->args->ac.vertex_id));
4681 }
4682
4683 Temp channels[num_channels];
4684 unsigned channel_start = 0;
4685 bool direct_fetch = false;
4686
4687 /* skip unused channels at the start */
4688 if (vtx_info->chan_byte_size && !post_shuffle) {
4689 channel_start = ffs(mask) - 1;
4690 for (unsigned i = 0; i < channel_start; i++)
4691 channels[i] = Temp(0, s1);
4692 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4693 num_channels = 3 - (ffs(mask) - 1);
4694 }
4695
4696 /* load channels */
4697 while (channel_start < num_channels) {
4698 unsigned fetch_size = num_channels - channel_start;
4699 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4700 bool expanded = false;
4701
4702 /* use MUBUF when possible to avoid possible alignment issues */
4703 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4704 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4705 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4706 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4707 vtx_info->chan_byte_size == 4;
4708 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4709 if (!use_mubuf) {
4710 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4711 } else {
4712 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4713 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4714 fetch_size = 4;
4715 expanded = true;
4716 }
4717 }
4718
4719 Temp fetch_index = index;
4720 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4721 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4722 fetch_offset = fetch_offset % attrib_stride;
4723 }
4724
4725 Operand soffset(0u);
4726 if (fetch_offset >= 4096) {
4727 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4728 fetch_offset %= 4096;
4729 }
4730
4731 aco_opcode opcode;
4732 switch (fetch_size) {
4733 case 1:
4734 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4735 break;
4736 case 2:
4737 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4738 break;
4739 case 3:
4740 assert(ctx->options->chip_class >= GFX7 ||
4741 (!use_mubuf && ctx->options->chip_class == GFX6));
4742 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4743 break;
4744 case 4:
4745 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4746 break;
4747 default:
4748 unreachable("Unimplemented load_input vector size");
4749 }
4750
4751 Temp fetch_dst;
4752 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4753 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4754 num_channels <= 3)) {
4755 direct_fetch = true;
4756 fetch_dst = dst;
4757 } else {
4758 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4759 }
4760
4761 if (use_mubuf) {
4762 Instruction *mubuf = bld.mubuf(opcode,
4763 Definition(fetch_dst), list, fetch_index, soffset,
4764 fetch_offset, false, true).instr;
4765 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4766 } else {
4767 Instruction *mtbuf = bld.mtbuf(opcode,
4768 Definition(fetch_dst), list, fetch_index, soffset,
4769 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4770 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4771 }
4772
4773 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4774
4775 if (fetch_size == 1) {
4776 channels[channel_start] = fetch_dst;
4777 } else {
4778 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4779 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4780 }
4781
4782 channel_start += fetch_size;
4783 }
4784
4785 if (!direct_fetch) {
4786 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4787 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4788
4789 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4790 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4791 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4792
4793 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4794 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4795 unsigned num_temp = 0;
4796 for (unsigned i = 0; i < dst.size(); i++) {
4797 unsigned idx = i + component;
4798 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4799 Temp channel = channels[swizzle[idx]];
4800 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4801 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4802 vec->operands[i] = Operand(channel);
4803
4804 num_temp++;
4805 elems[i] = channel;
4806 } else if (is_float && idx == 3) {
4807 vec->operands[i] = Operand(0x3f800000u);
4808 } else if (!is_float && idx == 3) {
4809 vec->operands[i] = Operand(1u);
4810 } else {
4811 vec->operands[i] = Operand(0u);
4812 }
4813 }
4814 vec->definitions[0] = Definition(dst);
4815 ctx->block->instructions.emplace_back(std::move(vec));
4816 emit_split_vector(ctx, dst, dst.size());
4817
4818 if (num_temp == dst.size())
4819 ctx->allocated_vec.emplace(dst.id(), elems);
4820 }
4821 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4822 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4823 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4824 if (off_instr->type != nir_instr_type_load_const ||
4825 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4826 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4827 nir_print_instr(off_instr, stderr);
4828 fprintf(stderr, "\n");
4829 }
4830
4831 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4832 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4833 if (offset) {
4834 assert(offset->u32 == 0);
4835 } else {
4836 /* the lower 15bit of the prim_mask contain the offset into LDS
4837 * while the upper bits contain the number of prims */
4838 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4839 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4840 Builder bld(ctx->program, ctx->block);
4841 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4842 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4843 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4844 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4845 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4846 }
4847
4848 unsigned idx = nir_intrinsic_base(instr);
4849 unsigned component = nir_intrinsic_component(instr);
4850 unsigned vertex_id = 2; /* P0 */
4851
4852 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4853 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4854 switch (src0->u32) {
4855 case 0:
4856 vertex_id = 2; /* P0 */
4857 break;
4858 case 1:
4859 vertex_id = 0; /* P10 */
4860 break;
4861 case 2:
4862 vertex_id = 1; /* P20 */
4863 break;
4864 default:
4865 unreachable("invalid vertex index");
4866 }
4867 }
4868
4869 if (dst.size() == 1) {
4870 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4871 } else {
4872 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4873 for (unsigned i = 0; i < dst.size(); i++)
4874 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4875 vec->definitions[0] = Definition(dst);
4876 bld.insert(std::move(vec));
4877 }
4878
4879 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4880 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4881 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4882 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4883 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4884
4885 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4886 } else {
4887 unreachable("Shader stage not implemented");
4888 }
4889 }
4890
4891 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4892 {
4893 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4894
4895 Builder bld(ctx->program, ctx->block);
4896 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4897 Temp vertex_offset;
4898
4899 if (!nir_src_is_const(*vertex_src)) {
4900 /* better code could be created, but this case probably doesn't happen
4901 * much in practice */
4902 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4903 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4904 Temp elem;
4905
4906 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4907 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4908 if (i % 2u)
4909 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4910 } else {
4911 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4912 }
4913
4914 if (vertex_offset.id()) {
4915 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4916 Operand(i), indirect_vertex);
4917 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4918 } else {
4919 vertex_offset = elem;
4920 }
4921 }
4922
4923 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4924 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4925 } else {
4926 unsigned vertex = nir_src_as_uint(*vertex_src);
4927 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4928 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4929 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4930 Operand((vertex % 2u) * 16u), Operand(16u));
4931 else
4932 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4933 }
4934
4935 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4936 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4937 return offset_mul(ctx, offs, 4u);
4938 }
4939
4940 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4941 {
4942 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4943
4944 Builder bld(ctx->program, ctx->block);
4945 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4946 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4947
4948 if (ctx->stage == geometry_gs) {
4949 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4950 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4951 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);
4952 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4953 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4954 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4955 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4956 } else {
4957 unreachable("Unsupported GS stage.");
4958 }
4959 }
4960
4961 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4962 {
4963 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4964
4965 Builder bld(ctx->program, ctx->block);
4966 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4967
4968 if (load_input_from_temps(ctx, instr, dst))
4969 return;
4970
4971 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4972 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4973 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4974
4975 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4976 }
4977
4978 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4979 {
4980 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4981
4982 Builder bld(ctx->program, ctx->block);
4983
4984 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4985 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4986 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4987
4988 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4989 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4990
4991 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4992 }
4993
4994 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4995 {
4996 switch (ctx->shader->info.stage) {
4997 case MESA_SHADER_GEOMETRY:
4998 visit_load_gs_per_vertex_input(ctx, instr);
4999 break;
5000 case MESA_SHADER_TESS_CTRL:
5001 visit_load_tcs_per_vertex_input(ctx, instr);
5002 break;
5003 case MESA_SHADER_TESS_EVAL:
5004 visit_load_tes_per_vertex_input(ctx, instr);
5005 break;
5006 default:
5007 unreachable("Unimplemented shader stage");
5008 }
5009 }
5010
5011 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5012 {
5013 visit_load_tcs_output(ctx, instr, true);
5014 }
5015
5016 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5017 {
5018 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5019 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5020
5021 visit_store_tcs_output(ctx, instr, true);
5022 }
5023
5024 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5025 {
5026 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5027
5028 Builder bld(ctx->program, ctx->block);
5029 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5030
5031 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5032 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5033 Operand tes_w(0u);
5034
5035 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5036 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5037 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5038 tes_w = Operand(tmp);
5039 }
5040
5041 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5042 emit_split_vector(ctx, tess_coord, 3);
5043 }
5044
5045 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5046 {
5047 if (ctx->program->info->need_indirect_descriptor_sets) {
5048 Builder bld(ctx->program, ctx->block);
5049 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5050 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5051 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5052 }
5053
5054 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5055 }
5056
5057
5058 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5059 {
5060 Builder bld(ctx->program, ctx->block);
5061 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5062 if (!ctx->divergent_vals[instr->dest.ssa.index])
5063 index = bld.as_uniform(index);
5064 unsigned desc_set = nir_intrinsic_desc_set(instr);
5065 unsigned binding = nir_intrinsic_binding(instr);
5066
5067 Temp desc_ptr;
5068 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5069 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5070 unsigned offset = layout->binding[binding].offset;
5071 unsigned stride;
5072 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5073 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5074 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5075 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5076 offset = pipeline_layout->push_constant_size + 16 * idx;
5077 stride = 16;
5078 } else {
5079 desc_ptr = load_desc_ptr(ctx, desc_set);
5080 stride = layout->binding[binding].size;
5081 }
5082
5083 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5084 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5085 if (stride != 1) {
5086 if (nir_const_index) {
5087 const_index = const_index * stride;
5088 } else if (index.type() == RegType::vgpr) {
5089 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5090 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5091 } else {
5092 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5093 }
5094 }
5095 if (offset) {
5096 if (nir_const_index) {
5097 const_index = const_index + offset;
5098 } else if (index.type() == RegType::vgpr) {
5099 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5100 } else {
5101 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5102 }
5103 }
5104
5105 if (nir_const_index && const_index == 0) {
5106 index = desc_ptr;
5107 } else if (index.type() == RegType::vgpr) {
5108 index = bld.vadd32(bld.def(v1),
5109 nir_const_index ? Operand(const_index) : Operand(index),
5110 Operand(desc_ptr));
5111 } else {
5112 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5113 nir_const_index ? Operand(const_index) : Operand(index),
5114 Operand(desc_ptr));
5115 }
5116
5117 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5118 }
5119
5120 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5121 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5122 bool glc=false, bool readonly=true)
5123 {
5124 Builder bld(ctx->program, ctx->block);
5125
5126 bool use_smem = dst.type() != RegType::vgpr && ((ctx->options->chip_class >= GFX8 && component_size >= 4) || readonly);
5127 if (use_smem)
5128 offset = bld.as_uniform(offset);
5129
5130 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5131 info.glc = glc;
5132 info.barrier = readonly ? barrier_none : barrier_buffer;
5133 info.can_reorder = readonly;
5134 info.align_mul = align_mul;
5135 info.align_offset = align_offset;
5136 if (use_smem)
5137 emit_smem_load(ctx, bld, &info);
5138 else
5139 emit_mubuf_load(ctx, bld, &info);
5140 }
5141
5142 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5143 {
5144 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5145 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5146
5147 Builder bld(ctx->program, ctx->block);
5148
5149 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5150 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5151 unsigned binding = nir_intrinsic_binding(idx_instr);
5152 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5153
5154 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5155 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5156 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5157 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5158 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5159 if (ctx->options->chip_class >= GFX10) {
5160 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5161 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5162 S_008F0C_RESOURCE_LEVEL(1);
5163 } else {
5164 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5165 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5166 }
5167 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5168 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5169 Operand(0xFFFFFFFFu),
5170 Operand(desc_type));
5171 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5172 rsrc, upper_dwords);
5173 } else {
5174 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5175 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5176 }
5177 unsigned size = instr->dest.ssa.bit_size / 8;
5178 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5179 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5180 }
5181
5182 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5183 {
5184 Builder bld(ctx->program, ctx->block);
5185 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5186 unsigned offset = nir_intrinsic_base(instr);
5187 unsigned count = instr->dest.ssa.num_components;
5188 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5189
5190 if (index_cv && instr->dest.ssa.bit_size == 32) {
5191 unsigned start = (offset + index_cv->u32) / 4u;
5192 start -= ctx->args->ac.base_inline_push_consts;
5193 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5194 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5195 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5196 for (unsigned i = 0; i < count; ++i) {
5197 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5198 vec->operands[i] = Operand{elems[i]};
5199 }
5200 vec->definitions[0] = Definition(dst);
5201 ctx->block->instructions.emplace_back(std::move(vec));
5202 ctx->allocated_vec.emplace(dst.id(), elems);
5203 return;
5204 }
5205 }
5206
5207 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5208 if (offset != 0) // TODO check if index != 0 as well
5209 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5210 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5211 Temp vec = dst;
5212 bool trim = false;
5213 bool aligned = true;
5214
5215 if (instr->dest.ssa.bit_size == 8) {
5216 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5217 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5218 if (!aligned)
5219 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5220 } else if (instr->dest.ssa.bit_size == 16) {
5221 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5222 if (!aligned)
5223 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5224 }
5225
5226 aco_opcode op;
5227
5228 switch (vec.size()) {
5229 case 1:
5230 op = aco_opcode::s_load_dword;
5231 break;
5232 case 2:
5233 op = aco_opcode::s_load_dwordx2;
5234 break;
5235 case 3:
5236 vec = bld.tmp(s4);
5237 trim = true;
5238 case 4:
5239 op = aco_opcode::s_load_dwordx4;
5240 break;
5241 case 6:
5242 vec = bld.tmp(s8);
5243 trim = true;
5244 case 8:
5245 op = aco_opcode::s_load_dwordx8;
5246 break;
5247 default:
5248 unreachable("unimplemented or forbidden load_push_constant.");
5249 }
5250
5251 bld.smem(op, Definition(vec), ptr, index);
5252
5253 if (!aligned) {
5254 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5255 byte_align_scalar(ctx, vec, byte_offset, dst);
5256 return;
5257 }
5258
5259 if (trim) {
5260 emit_split_vector(ctx, vec, 4);
5261 RegClass rc = dst.size() == 3 ? s1 : s2;
5262 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5263 emit_extract_vector(ctx, vec, 0, rc),
5264 emit_extract_vector(ctx, vec, 1, rc),
5265 emit_extract_vector(ctx, vec, 2, rc));
5266
5267 }
5268 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5269 }
5270
5271 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5272 {
5273 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5274
5275 Builder bld(ctx->program, ctx->block);
5276
5277 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5278 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5279 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5280 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5281 if (ctx->options->chip_class >= GFX10) {
5282 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5283 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5284 S_008F0C_RESOURCE_LEVEL(1);
5285 } else {
5286 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5287 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5288 }
5289
5290 unsigned base = nir_intrinsic_base(instr);
5291 unsigned range = nir_intrinsic_range(instr);
5292
5293 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5294 if (base && offset.type() == RegType::sgpr)
5295 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5296 else if (base && offset.type() == RegType::vgpr)
5297 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5298
5299 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5300 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5301 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5302 Operand(desc_type));
5303 unsigned size = instr->dest.ssa.bit_size / 8;
5304 // TODO: get alignment information for subdword constants
5305 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5306 }
5307
5308 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5309 {
5310 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5311 ctx->cf_info.exec_potentially_empty_discard = true;
5312
5313 ctx->program->needs_exact = true;
5314
5315 // TODO: optimize uniform conditions
5316 Builder bld(ctx->program, ctx->block);
5317 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5318 assert(src.regClass() == bld.lm);
5319 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5320 bld.pseudo(aco_opcode::p_discard_if, src);
5321 ctx->block->kind |= block_kind_uses_discard_if;
5322 return;
5323 }
5324
5325 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5326 {
5327 Builder bld(ctx->program, ctx->block);
5328
5329 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5330 ctx->cf_info.exec_potentially_empty_discard = true;
5331
5332 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5333 ctx->cf_info.parent_loop.has_divergent_continue;
5334
5335 if (ctx->block->loop_nest_depth &&
5336 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5337 /* we handle discards the same way as jump instructions */
5338 append_logical_end(ctx->block);
5339
5340 /* in loops, discard behaves like break */
5341 Block *linear_target = ctx->cf_info.parent_loop.exit;
5342 ctx->block->kind |= block_kind_discard;
5343
5344 if (!divergent) {
5345 /* uniform discard - loop ends here */
5346 assert(nir_instr_is_last(&instr->instr));
5347 ctx->block->kind |= block_kind_uniform;
5348 ctx->cf_info.has_branch = true;
5349 bld.branch(aco_opcode::p_branch);
5350 add_linear_edge(ctx->block->index, linear_target);
5351 return;
5352 }
5353
5354 /* we add a break right behind the discard() instructions */
5355 ctx->block->kind |= block_kind_break;
5356 unsigned idx = ctx->block->index;
5357
5358 ctx->cf_info.parent_loop.has_divergent_branch = true;
5359 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5360
5361 /* remove critical edges from linear CFG */
5362 bld.branch(aco_opcode::p_branch);
5363 Block* break_block = ctx->program->create_and_insert_block();
5364 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5365 break_block->kind |= block_kind_uniform;
5366 add_linear_edge(idx, break_block);
5367 add_linear_edge(break_block->index, linear_target);
5368 bld.reset(break_block);
5369 bld.branch(aco_opcode::p_branch);
5370
5371 Block* continue_block = ctx->program->create_and_insert_block();
5372 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5373 add_linear_edge(idx, continue_block);
5374 append_logical_start(continue_block);
5375 ctx->block = continue_block;
5376
5377 return;
5378 }
5379
5380 /* it can currently happen that NIR doesn't remove the unreachable code */
5381 if (!nir_instr_is_last(&instr->instr)) {
5382 ctx->program->needs_exact = true;
5383 /* save exec somewhere temporarily so that it doesn't get
5384 * overwritten before the discard from outer exec masks */
5385 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5386 bld.pseudo(aco_opcode::p_discard_if, cond);
5387 ctx->block->kind |= block_kind_uses_discard_if;
5388 return;
5389 }
5390
5391 /* This condition is incorrect for uniformly branched discards in a loop
5392 * predicated by a divergent condition, but the above code catches that case
5393 * and the discard would end up turning into a discard_if.
5394 * For example:
5395 * if (divergent) {
5396 * while (...) {
5397 * if (uniform) {
5398 * discard;
5399 * }
5400 * }
5401 * }
5402 */
5403 if (!ctx->cf_info.parent_if.is_divergent) {
5404 /* program just ends here */
5405 ctx->block->kind |= block_kind_uniform;
5406 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5407 0 /* enabled mask */, 9 /* dest */,
5408 false /* compressed */, true/* done */, true /* valid mask */);
5409 bld.sopp(aco_opcode::s_endpgm);
5410 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5411 } else {
5412 ctx->block->kind |= block_kind_discard;
5413 /* branch and linear edge is added by visit_if() */
5414 }
5415 }
5416
5417 enum aco_descriptor_type {
5418 ACO_DESC_IMAGE,
5419 ACO_DESC_FMASK,
5420 ACO_DESC_SAMPLER,
5421 ACO_DESC_BUFFER,
5422 ACO_DESC_PLANE_0,
5423 ACO_DESC_PLANE_1,
5424 ACO_DESC_PLANE_2,
5425 };
5426
5427 static bool
5428 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5429 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5430 return false;
5431 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5432 return dim == ac_image_cube ||
5433 dim == ac_image_1darray ||
5434 dim == ac_image_2darray ||
5435 dim == ac_image_2darraymsaa;
5436 }
5437
5438 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5439 enum aco_descriptor_type desc_type,
5440 const nir_tex_instr *tex_instr, bool image, bool write)
5441 {
5442 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5443 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5444 if (it != ctx->tex_desc.end())
5445 return it->second;
5446 */
5447 Temp index = Temp();
5448 bool index_set = false;
5449 unsigned constant_index = 0;
5450 unsigned descriptor_set;
5451 unsigned base_index;
5452 Builder bld(ctx->program, ctx->block);
5453
5454 if (!deref_instr) {
5455 assert(tex_instr && !image);
5456 descriptor_set = 0;
5457 base_index = tex_instr->sampler_index;
5458 } else {
5459 while(deref_instr->deref_type != nir_deref_type_var) {
5460 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5461 if (!array_size)
5462 array_size = 1;
5463
5464 assert(deref_instr->deref_type == nir_deref_type_array);
5465 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5466 if (const_value) {
5467 constant_index += array_size * const_value->u32;
5468 } else {
5469 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5470 if (indirect.type() == RegType::vgpr)
5471 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5472
5473 if (array_size != 1)
5474 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5475
5476 if (!index_set) {
5477 index = indirect;
5478 index_set = true;
5479 } else {
5480 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5481 }
5482 }
5483
5484 deref_instr = nir_src_as_deref(deref_instr->parent);
5485 }
5486 descriptor_set = deref_instr->var->data.descriptor_set;
5487 base_index = deref_instr->var->data.binding;
5488 }
5489
5490 Temp list = load_desc_ptr(ctx, descriptor_set);
5491 list = convert_pointer_to_64_bit(ctx, list);
5492
5493 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5494 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5495 unsigned offset = binding->offset;
5496 unsigned stride = binding->size;
5497 aco_opcode opcode;
5498 RegClass type;
5499
5500 assert(base_index < layout->binding_count);
5501
5502 switch (desc_type) {
5503 case ACO_DESC_IMAGE:
5504 type = s8;
5505 opcode = aco_opcode::s_load_dwordx8;
5506 break;
5507 case ACO_DESC_FMASK:
5508 type = s8;
5509 opcode = aco_opcode::s_load_dwordx8;
5510 offset += 32;
5511 break;
5512 case ACO_DESC_SAMPLER:
5513 type = s4;
5514 opcode = aco_opcode::s_load_dwordx4;
5515 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5516 offset += radv_combined_image_descriptor_sampler_offset(binding);
5517 break;
5518 case ACO_DESC_BUFFER:
5519 type = s4;
5520 opcode = aco_opcode::s_load_dwordx4;
5521 break;
5522 case ACO_DESC_PLANE_0:
5523 case ACO_DESC_PLANE_1:
5524 type = s8;
5525 opcode = aco_opcode::s_load_dwordx8;
5526 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5527 break;
5528 case ACO_DESC_PLANE_2:
5529 type = s4;
5530 opcode = aco_opcode::s_load_dwordx4;
5531 offset += 64;
5532 break;
5533 default:
5534 unreachable("invalid desc_type\n");
5535 }
5536
5537 offset += constant_index * stride;
5538
5539 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5540 (!index_set || binding->immutable_samplers_equal)) {
5541 if (binding->immutable_samplers_equal)
5542 constant_index = 0;
5543
5544 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5545 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5546 Operand(samplers[constant_index * 4 + 0]),
5547 Operand(samplers[constant_index * 4 + 1]),
5548 Operand(samplers[constant_index * 4 + 2]),
5549 Operand(samplers[constant_index * 4 + 3]));
5550 }
5551
5552 Operand off;
5553 if (!index_set) {
5554 off = bld.copy(bld.def(s1), Operand(offset));
5555 } else {
5556 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5557 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5558 }
5559
5560 Temp res = bld.smem(opcode, bld.def(type), list, off);
5561
5562 if (desc_type == ACO_DESC_PLANE_2) {
5563 Temp components[8];
5564 for (unsigned i = 0; i < 8; i++)
5565 components[i] = bld.tmp(s1);
5566 bld.pseudo(aco_opcode::p_split_vector,
5567 Definition(components[0]),
5568 Definition(components[1]),
5569 Definition(components[2]),
5570 Definition(components[3]),
5571 res);
5572
5573 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5574 bld.pseudo(aco_opcode::p_split_vector,
5575 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5576 Definition(components[4]),
5577 Definition(components[5]),
5578 Definition(components[6]),
5579 Definition(components[7]),
5580 desc2);
5581
5582 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5583 components[0], components[1], components[2], components[3],
5584 components[4], components[5], components[6], components[7]);
5585 }
5586
5587 return res;
5588 }
5589
5590 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5591 {
5592 switch (dim) {
5593 case GLSL_SAMPLER_DIM_BUF:
5594 return 1;
5595 case GLSL_SAMPLER_DIM_1D:
5596 return array ? 2 : 1;
5597 case GLSL_SAMPLER_DIM_2D:
5598 return array ? 3 : 2;
5599 case GLSL_SAMPLER_DIM_MS:
5600 return array ? 4 : 3;
5601 case GLSL_SAMPLER_DIM_3D:
5602 case GLSL_SAMPLER_DIM_CUBE:
5603 return 3;
5604 case GLSL_SAMPLER_DIM_RECT:
5605 case GLSL_SAMPLER_DIM_SUBPASS:
5606 return 2;
5607 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5608 return 3;
5609 default:
5610 break;
5611 }
5612 return 0;
5613 }
5614
5615
5616 /* Adjust the sample index according to FMASK.
5617 *
5618 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5619 * which is the identity mapping. Each nibble says which physical sample
5620 * should be fetched to get that sample.
5621 *
5622 * For example, 0x11111100 means there are only 2 samples stored and
5623 * the second sample covers 3/4 of the pixel. When reading samples 0
5624 * and 1, return physical sample 0 (determined by the first two 0s
5625 * in FMASK), otherwise return physical sample 1.
5626 *
5627 * The sample index should be adjusted as follows:
5628 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5629 */
5630 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5631 {
5632 Builder bld(ctx->program, ctx->block);
5633 Temp fmask = bld.tmp(v1);
5634 unsigned dim = ctx->options->chip_class >= GFX10
5635 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5636 : 0;
5637
5638 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5639 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5640 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5641 load->operands[0] = Operand(fmask_desc_ptr);
5642 load->operands[1] = Operand(s4); /* no sampler */
5643 load->operands[2] = Operand(coord);
5644 load->definitions[0] = Definition(fmask);
5645 load->glc = false;
5646 load->dlc = false;
5647 load->dmask = 0x1;
5648 load->unrm = true;
5649 load->da = da;
5650 load->dim = dim;
5651 load->can_reorder = true; /* fmask images shouldn't be modified */
5652 ctx->block->instructions.emplace_back(std::move(load));
5653
5654 Operand sample_index4;
5655 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
5656 sample_index4 = Operand(sample_index.constantValue() << 2);
5657 } else if (sample_index.regClass() == s1) {
5658 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5659 } else {
5660 assert(sample_index.regClass() == v1);
5661 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5662 }
5663
5664 Temp final_sample;
5665 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5666 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5667 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5668 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5669 else
5670 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5671
5672 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5673 * resource descriptor is 0 (invalid),
5674 */
5675 Temp compare = bld.tmp(bld.lm);
5676 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5677 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5678
5679 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5680
5681 /* Replace the MSAA sample index. */
5682 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5683 }
5684
5685 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5686 {
5687
5688 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5689 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5690 bool is_array = glsl_sampler_type_is_array(type);
5691 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5692 assert(!add_frag_pos && "Input attachments should be lowered.");
5693 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5694 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5695 int count = image_type_to_components_count(dim, is_array);
5696 std::vector<Temp> coords(count);
5697 Builder bld(ctx->program, ctx->block);
5698
5699 if (is_ms) {
5700 count--;
5701 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5702 /* get sample index */
5703 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5704 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5705 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5706 std::vector<Temp> fmask_load_address;
5707 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5708 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5709
5710 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5711 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5712 } else {
5713 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5714 }
5715 }
5716
5717 if (gfx9_1d) {
5718 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5719 coords.resize(coords.size() + 1);
5720 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5721 if (is_array)
5722 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5723 } else {
5724 for (int i = 0; i < count; i++)
5725 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5726 }
5727
5728 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5729 instr->intrinsic == nir_intrinsic_image_deref_store) {
5730 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5731 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5732
5733 if (!level_zero)
5734 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5735 }
5736
5737 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5738 for (unsigned i = 0; i < coords.size(); i++)
5739 vec->operands[i] = Operand(coords[i]);
5740 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5741 vec->definitions[0] = Definition(res);
5742 ctx->block->instructions.emplace_back(std::move(vec));
5743 return res;
5744 }
5745
5746
5747 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5748 {
5749 Builder bld(ctx->program, ctx->block);
5750 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5751 const struct glsl_type *type = glsl_without_array(var->type);
5752 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5753 bool is_array = glsl_sampler_type_is_array(type);
5754 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5755
5756 if (dim == GLSL_SAMPLER_DIM_BUF) {
5757 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5758 unsigned num_channels = util_last_bit(mask);
5759 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5760 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5761
5762 aco_opcode opcode;
5763 switch (num_channels) {
5764 case 1:
5765 opcode = aco_opcode::buffer_load_format_x;
5766 break;
5767 case 2:
5768 opcode = aco_opcode::buffer_load_format_xy;
5769 break;
5770 case 3:
5771 opcode = aco_opcode::buffer_load_format_xyz;
5772 break;
5773 case 4:
5774 opcode = aco_opcode::buffer_load_format_xyzw;
5775 break;
5776 default:
5777 unreachable(">4 channel buffer image load");
5778 }
5779 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5780 load->operands[0] = Operand(rsrc);
5781 load->operands[1] = Operand(vindex);
5782 load->operands[2] = Operand((uint32_t) 0);
5783 Temp tmp;
5784 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5785 tmp = dst;
5786 else
5787 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5788 load->definitions[0] = Definition(tmp);
5789 load->idxen = true;
5790 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5791 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5792 load->barrier = barrier_image;
5793 ctx->block->instructions.emplace_back(std::move(load));
5794
5795 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5796 return;
5797 }
5798
5799 Temp coords = get_image_coords(ctx, instr, type);
5800 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5801
5802 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5803 unsigned num_components = util_bitcount(dmask);
5804 Temp tmp;
5805 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5806 tmp = dst;
5807 else
5808 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5809
5810 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5811 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5812
5813 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5814 load->operands[0] = Operand(resource);
5815 load->operands[1] = Operand(s4); /* no sampler */
5816 load->operands[2] = Operand(coords);
5817 load->definitions[0] = Definition(tmp);
5818 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5819 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5820 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5821 load->dmask = dmask;
5822 load->unrm = true;
5823 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5824 load->barrier = barrier_image;
5825 ctx->block->instructions.emplace_back(std::move(load));
5826
5827 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5828 return;
5829 }
5830
5831 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5832 {
5833 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5834 const struct glsl_type *type = glsl_without_array(var->type);
5835 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5836 bool is_array = glsl_sampler_type_is_array(type);
5837 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5838
5839 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5840
5841 if (dim == GLSL_SAMPLER_DIM_BUF) {
5842 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5843 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5844 aco_opcode opcode;
5845 switch (data.size()) {
5846 case 1:
5847 opcode = aco_opcode::buffer_store_format_x;
5848 break;
5849 case 2:
5850 opcode = aco_opcode::buffer_store_format_xy;
5851 break;
5852 case 3:
5853 opcode = aco_opcode::buffer_store_format_xyz;
5854 break;
5855 case 4:
5856 opcode = aco_opcode::buffer_store_format_xyzw;
5857 break;
5858 default:
5859 unreachable(">4 channel buffer image store");
5860 }
5861 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5862 store->operands[0] = Operand(rsrc);
5863 store->operands[1] = Operand(vindex);
5864 store->operands[2] = Operand((uint32_t) 0);
5865 store->operands[3] = Operand(data);
5866 store->idxen = true;
5867 store->glc = glc;
5868 store->dlc = false;
5869 store->disable_wqm = true;
5870 store->barrier = barrier_image;
5871 ctx->program->needs_exact = true;
5872 ctx->block->instructions.emplace_back(std::move(store));
5873 return;
5874 }
5875
5876 assert(data.type() == RegType::vgpr);
5877 Temp coords = get_image_coords(ctx, instr, type);
5878 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5879
5880 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5881 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5882
5883 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5884 store->operands[0] = Operand(resource);
5885 store->operands[1] = Operand(data);
5886 store->operands[2] = Operand(coords);
5887 store->glc = glc;
5888 store->dlc = false;
5889 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5890 store->dmask = (1 << data.size()) - 1;
5891 store->unrm = true;
5892 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5893 store->disable_wqm = true;
5894 store->barrier = barrier_image;
5895 ctx->program->needs_exact = true;
5896 ctx->block->instructions.emplace_back(std::move(store));
5897 return;
5898 }
5899
5900 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5901 {
5902 /* return the previous value if dest is ever used */
5903 bool return_previous = false;
5904 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5905 return_previous = true;
5906 break;
5907 }
5908 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5909 return_previous = true;
5910 break;
5911 }
5912
5913 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5914 const struct glsl_type *type = glsl_without_array(var->type);
5915 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5916 bool is_array = glsl_sampler_type_is_array(type);
5917 Builder bld(ctx->program, ctx->block);
5918
5919 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5920 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5921
5922 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5923 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5924
5925 aco_opcode buf_op, image_op;
5926 switch (instr->intrinsic) {
5927 case nir_intrinsic_image_deref_atomic_add:
5928 buf_op = aco_opcode::buffer_atomic_add;
5929 image_op = aco_opcode::image_atomic_add;
5930 break;
5931 case nir_intrinsic_image_deref_atomic_umin:
5932 buf_op = aco_opcode::buffer_atomic_umin;
5933 image_op = aco_opcode::image_atomic_umin;
5934 break;
5935 case nir_intrinsic_image_deref_atomic_imin:
5936 buf_op = aco_opcode::buffer_atomic_smin;
5937 image_op = aco_opcode::image_atomic_smin;
5938 break;
5939 case nir_intrinsic_image_deref_atomic_umax:
5940 buf_op = aco_opcode::buffer_atomic_umax;
5941 image_op = aco_opcode::image_atomic_umax;
5942 break;
5943 case nir_intrinsic_image_deref_atomic_imax:
5944 buf_op = aco_opcode::buffer_atomic_smax;
5945 image_op = aco_opcode::image_atomic_smax;
5946 break;
5947 case nir_intrinsic_image_deref_atomic_and:
5948 buf_op = aco_opcode::buffer_atomic_and;
5949 image_op = aco_opcode::image_atomic_and;
5950 break;
5951 case nir_intrinsic_image_deref_atomic_or:
5952 buf_op = aco_opcode::buffer_atomic_or;
5953 image_op = aco_opcode::image_atomic_or;
5954 break;
5955 case nir_intrinsic_image_deref_atomic_xor:
5956 buf_op = aco_opcode::buffer_atomic_xor;
5957 image_op = aco_opcode::image_atomic_xor;
5958 break;
5959 case nir_intrinsic_image_deref_atomic_exchange:
5960 buf_op = aco_opcode::buffer_atomic_swap;
5961 image_op = aco_opcode::image_atomic_swap;
5962 break;
5963 case nir_intrinsic_image_deref_atomic_comp_swap:
5964 buf_op = aco_opcode::buffer_atomic_cmpswap;
5965 image_op = aco_opcode::image_atomic_cmpswap;
5966 break;
5967 default:
5968 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5969 }
5970
5971 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5972
5973 if (dim == GLSL_SAMPLER_DIM_BUF) {
5974 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5975 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5976 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5977 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5978 mubuf->operands[0] = Operand(resource);
5979 mubuf->operands[1] = Operand(vindex);
5980 mubuf->operands[2] = Operand((uint32_t)0);
5981 mubuf->operands[3] = Operand(data);
5982 if (return_previous)
5983 mubuf->definitions[0] = Definition(dst);
5984 mubuf->offset = 0;
5985 mubuf->idxen = true;
5986 mubuf->glc = return_previous;
5987 mubuf->dlc = false; /* Not needed for atomics */
5988 mubuf->disable_wqm = true;
5989 mubuf->barrier = barrier_image;
5990 ctx->program->needs_exact = true;
5991 ctx->block->instructions.emplace_back(std::move(mubuf));
5992 return;
5993 }
5994
5995 Temp coords = get_image_coords(ctx, instr, type);
5996 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5997 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5998 mimg->operands[0] = Operand(resource);
5999 mimg->operands[1] = Operand(data);
6000 mimg->operands[2] = Operand(coords);
6001 if (return_previous)
6002 mimg->definitions[0] = Definition(dst);
6003 mimg->glc = return_previous;
6004 mimg->dlc = false; /* Not needed for atomics */
6005 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6006 mimg->dmask = (1 << data.size()) - 1;
6007 mimg->unrm = true;
6008 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6009 mimg->disable_wqm = true;
6010 mimg->barrier = barrier_image;
6011 ctx->program->needs_exact = true;
6012 ctx->block->instructions.emplace_back(std::move(mimg));
6013 return;
6014 }
6015
6016 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6017 {
6018 if (in_elements && ctx->options->chip_class == GFX8) {
6019 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6020 Builder bld(ctx->program, ctx->block);
6021
6022 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6023
6024 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6025 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6026
6027 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6028 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6029
6030 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6031 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6032
6033 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6034 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6035 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6036 if (dst.type() == RegType::vgpr)
6037 bld.copy(Definition(dst), shr_dst);
6038
6039 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6040 } else {
6041 emit_extract_vector(ctx, desc, 2, dst);
6042 }
6043 }
6044
6045 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6046 {
6047 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6048 const struct glsl_type *type = glsl_without_array(var->type);
6049 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6050 bool is_array = glsl_sampler_type_is_array(type);
6051 Builder bld(ctx->program, ctx->block);
6052
6053 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6054 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6055 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6056 }
6057
6058 /* LOD */
6059 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6060
6061 /* Resource */
6062 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6063
6064 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6065
6066 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6067 mimg->operands[0] = Operand(resource);
6068 mimg->operands[1] = Operand(s4); /* no sampler */
6069 mimg->operands[2] = Operand(lod);
6070 uint8_t& dmask = mimg->dmask;
6071 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6072 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6073 mimg->da = glsl_sampler_type_is_array(type);
6074 mimg->can_reorder = true;
6075 Definition& def = mimg->definitions[0];
6076 ctx->block->instructions.emplace_back(std::move(mimg));
6077
6078 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6079 glsl_sampler_type_is_array(type)) {
6080
6081 assert(instr->dest.ssa.num_components == 3);
6082 Temp tmp = {ctx->program->allocateId(), v3};
6083 def = Definition(tmp);
6084 emit_split_vector(ctx, tmp, 3);
6085
6086 /* divide 3rd value by 6 by multiplying with magic number */
6087 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6088 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6089
6090 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6091 emit_extract_vector(ctx, tmp, 0, v1),
6092 emit_extract_vector(ctx, tmp, 1, v1),
6093 by_6);
6094
6095 } else if (ctx->options->chip_class == GFX9 &&
6096 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6097 glsl_sampler_type_is_array(type)) {
6098 assert(instr->dest.ssa.num_components == 2);
6099 def = Definition(dst);
6100 dmask = 0x5;
6101 } else {
6102 def = Definition(dst);
6103 }
6104
6105 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6106 }
6107
6108 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6109 {
6110 Builder bld(ctx->program, ctx->block);
6111 unsigned num_components = instr->num_components;
6112
6113 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6114 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6115 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6116
6117 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6118 unsigned size = instr->dest.ssa.bit_size / 8;
6119 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6120 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false);
6121 }
6122
6123 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6124 {
6125 Builder bld(ctx->program, ctx->block);
6126 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6127 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6128 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6129 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6130
6131 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6132 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6133
6134 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
6135 ctx->options->chip_class >= GFX8 &&
6136 elem_size_bytes >= 4;
6137 if (smem)
6138 offset = bld.as_uniform(offset);
6139 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6140
6141 unsigned write_count = 0;
6142 Temp write_datas[32];
6143 unsigned offsets[32];
6144 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6145 data, writemask, 16, &write_count, write_datas, offsets);
6146
6147 for (unsigned i = 0; i < write_count; i++) {
6148 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6149 if (smem && ctx->stage == fragment_fs)
6150 op = aco_opcode::p_fs_buffer_store_smem;
6151
6152 if (smem) {
6153 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6154 store->operands[0] = Operand(rsrc);
6155 if (offsets[i]) {
6156 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6157 offset, Operand(offsets[i]));
6158 store->operands[1] = Operand(off);
6159 } else {
6160 store->operands[1] = Operand(offset);
6161 }
6162 if (op != aco_opcode::p_fs_buffer_store_smem)
6163 store->operands[1].setFixed(m0);
6164 store->operands[2] = Operand(write_datas[i]);
6165 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6166 store->dlc = false;
6167 store->disable_wqm = true;
6168 store->barrier = barrier_buffer;
6169 ctx->block->instructions.emplace_back(std::move(store));
6170 ctx->program->wb_smem_l1_on_end = true;
6171 if (op == aco_opcode::p_fs_buffer_store_smem) {
6172 ctx->block->kind |= block_kind_needs_lowering;
6173 ctx->program->needs_exact = true;
6174 }
6175 } else {
6176 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6177 store->operands[0] = Operand(rsrc);
6178 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6179 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6180 store->operands[3] = Operand(write_datas[i]);
6181 store->offset = offsets[i];
6182 store->offen = (offset.type() == RegType::vgpr);
6183 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6184 store->dlc = false;
6185 store->disable_wqm = true;
6186 store->barrier = barrier_buffer;
6187 ctx->program->needs_exact = true;
6188 ctx->block->instructions.emplace_back(std::move(store));
6189 }
6190 }
6191 }
6192
6193 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6194 {
6195 /* return the previous value if dest is ever used */
6196 bool return_previous = false;
6197 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6198 return_previous = true;
6199 break;
6200 }
6201 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6202 return_previous = true;
6203 break;
6204 }
6205
6206 Builder bld(ctx->program, ctx->block);
6207 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6208
6209 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6210 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6211 get_ssa_temp(ctx, instr->src[3].ssa), data);
6212
6213 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6214 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6215 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6216
6217 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6218
6219 aco_opcode op32, op64;
6220 switch (instr->intrinsic) {
6221 case nir_intrinsic_ssbo_atomic_add:
6222 op32 = aco_opcode::buffer_atomic_add;
6223 op64 = aco_opcode::buffer_atomic_add_x2;
6224 break;
6225 case nir_intrinsic_ssbo_atomic_imin:
6226 op32 = aco_opcode::buffer_atomic_smin;
6227 op64 = aco_opcode::buffer_atomic_smin_x2;
6228 break;
6229 case nir_intrinsic_ssbo_atomic_umin:
6230 op32 = aco_opcode::buffer_atomic_umin;
6231 op64 = aco_opcode::buffer_atomic_umin_x2;
6232 break;
6233 case nir_intrinsic_ssbo_atomic_imax:
6234 op32 = aco_opcode::buffer_atomic_smax;
6235 op64 = aco_opcode::buffer_atomic_smax_x2;
6236 break;
6237 case nir_intrinsic_ssbo_atomic_umax:
6238 op32 = aco_opcode::buffer_atomic_umax;
6239 op64 = aco_opcode::buffer_atomic_umax_x2;
6240 break;
6241 case nir_intrinsic_ssbo_atomic_and:
6242 op32 = aco_opcode::buffer_atomic_and;
6243 op64 = aco_opcode::buffer_atomic_and_x2;
6244 break;
6245 case nir_intrinsic_ssbo_atomic_or:
6246 op32 = aco_opcode::buffer_atomic_or;
6247 op64 = aco_opcode::buffer_atomic_or_x2;
6248 break;
6249 case nir_intrinsic_ssbo_atomic_xor:
6250 op32 = aco_opcode::buffer_atomic_xor;
6251 op64 = aco_opcode::buffer_atomic_xor_x2;
6252 break;
6253 case nir_intrinsic_ssbo_atomic_exchange:
6254 op32 = aco_opcode::buffer_atomic_swap;
6255 op64 = aco_opcode::buffer_atomic_swap_x2;
6256 break;
6257 case nir_intrinsic_ssbo_atomic_comp_swap:
6258 op32 = aco_opcode::buffer_atomic_cmpswap;
6259 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6260 break;
6261 default:
6262 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6263 }
6264 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6265 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6266 mubuf->operands[0] = Operand(rsrc);
6267 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6268 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6269 mubuf->operands[3] = Operand(data);
6270 if (return_previous)
6271 mubuf->definitions[0] = Definition(dst);
6272 mubuf->offset = 0;
6273 mubuf->offen = (offset.type() == RegType::vgpr);
6274 mubuf->glc = return_previous;
6275 mubuf->dlc = false; /* Not needed for atomics */
6276 mubuf->disable_wqm = true;
6277 mubuf->barrier = barrier_buffer;
6278 ctx->program->needs_exact = true;
6279 ctx->block->instructions.emplace_back(std::move(mubuf));
6280 }
6281
6282 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6283
6284 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6285 Builder bld(ctx->program, ctx->block);
6286 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6287 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6288 }
6289
6290 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6291 {
6292 Builder bld(ctx->program, ctx->block);
6293 unsigned num_components = instr->num_components;
6294 unsigned component_size = instr->dest.ssa.bit_size / 8;
6295
6296 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6297 get_ssa_temp(ctx, &instr->dest.ssa),
6298 num_components, component_size};
6299 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6300 info.align_mul = nir_intrinsic_align_mul(instr);
6301 info.align_offset = nir_intrinsic_align_offset(instr);
6302 info.barrier = barrier_buffer;
6303 info.can_reorder = false;
6304 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6305 * it's safe to use SMEM */
6306 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6307 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6308 emit_global_load(ctx, bld, &info);
6309 } else {
6310 info.offset = Operand(bld.as_uniform(info.offset));
6311 emit_smem_load(ctx, bld, &info);
6312 }
6313 }
6314
6315 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6316 {
6317 Builder bld(ctx->program, ctx->block);
6318 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6319 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6320
6321 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6322 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6323 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6324
6325 if (ctx->options->chip_class >= GFX7)
6326 addr = as_vgpr(ctx, addr);
6327
6328 unsigned write_count = 0;
6329 Temp write_datas[32];
6330 unsigned offsets[32];
6331 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6332 16, &write_count, write_datas, offsets);
6333
6334 for (unsigned i = 0; i < write_count; i++) {
6335 if (ctx->options->chip_class >= GFX7) {
6336 unsigned offset = offsets[i];
6337 Temp store_addr = addr;
6338 if (offset > 0 && ctx->options->chip_class < GFX9) {
6339 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6340 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6341 Temp carry = bld.tmp(bld.lm);
6342 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6343
6344 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6345 Operand(offset), addr0);
6346 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6347 Operand(0u), addr1,
6348 carry).def(1).setHint(vcc);
6349
6350 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6351
6352 offset = 0;
6353 }
6354
6355 bool global = ctx->options->chip_class >= GFX9;
6356 aco_opcode op;
6357 switch (write_datas[i].bytes()) {
6358 case 1:
6359 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6360 break;
6361 case 2:
6362 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6363 break;
6364 case 4:
6365 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6366 break;
6367 case 8:
6368 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6369 break;
6370 case 12:
6371 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6372 break;
6373 case 16:
6374 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6375 break;
6376 default:
6377 unreachable("store_global not implemented for this size.");
6378 }
6379
6380 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6381 flat->operands[0] = Operand(store_addr);
6382 flat->operands[1] = Operand(s1);
6383 flat->operands[2] = Operand(write_datas[i]);
6384 flat->glc = glc;
6385 flat->dlc = false;
6386 flat->offset = offset;
6387 flat->disable_wqm = true;
6388 flat->barrier = barrier_buffer;
6389 ctx->program->needs_exact = true;
6390 ctx->block->instructions.emplace_back(std::move(flat));
6391 } else {
6392 assert(ctx->options->chip_class == GFX6);
6393
6394 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6395
6396 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6397
6398 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6399 mubuf->operands[0] = Operand(rsrc);
6400 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6401 mubuf->operands[2] = Operand(0u);
6402 mubuf->operands[3] = Operand(write_datas[i]);
6403 mubuf->glc = glc;
6404 mubuf->dlc = false;
6405 mubuf->offset = offsets[i];
6406 mubuf->addr64 = addr.type() == RegType::vgpr;
6407 mubuf->disable_wqm = true;
6408 mubuf->barrier = barrier_buffer;
6409 ctx->program->needs_exact = true;
6410 ctx->block->instructions.emplace_back(std::move(mubuf));
6411 }
6412 }
6413 }
6414
6415 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6416 {
6417 /* return the previous value if dest is ever used */
6418 bool return_previous = false;
6419 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6420 return_previous = true;
6421 break;
6422 }
6423 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6424 return_previous = true;
6425 break;
6426 }
6427
6428 Builder bld(ctx->program, ctx->block);
6429 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6430 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6431
6432 if (ctx->options->chip_class >= GFX7)
6433 addr = as_vgpr(ctx, addr);
6434
6435 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6436 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6437 get_ssa_temp(ctx, instr->src[2].ssa), data);
6438
6439 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6440
6441 aco_opcode op32, op64;
6442
6443 if (ctx->options->chip_class >= GFX7) {
6444 bool global = ctx->options->chip_class >= GFX9;
6445 switch (instr->intrinsic) {
6446 case nir_intrinsic_global_atomic_add:
6447 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6448 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6449 break;
6450 case nir_intrinsic_global_atomic_imin:
6451 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6452 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6453 break;
6454 case nir_intrinsic_global_atomic_umin:
6455 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6456 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6457 break;
6458 case nir_intrinsic_global_atomic_imax:
6459 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6460 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6461 break;
6462 case nir_intrinsic_global_atomic_umax:
6463 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6464 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6465 break;
6466 case nir_intrinsic_global_atomic_and:
6467 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6468 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6469 break;
6470 case nir_intrinsic_global_atomic_or:
6471 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6472 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6473 break;
6474 case nir_intrinsic_global_atomic_xor:
6475 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6476 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6477 break;
6478 case nir_intrinsic_global_atomic_exchange:
6479 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6480 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6481 break;
6482 case nir_intrinsic_global_atomic_comp_swap:
6483 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6484 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6485 break;
6486 default:
6487 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6488 }
6489
6490 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6491 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6492 flat->operands[0] = Operand(addr);
6493 flat->operands[1] = Operand(s1);
6494 flat->operands[2] = Operand(data);
6495 if (return_previous)
6496 flat->definitions[0] = Definition(dst);
6497 flat->glc = return_previous;
6498 flat->dlc = false; /* Not needed for atomics */
6499 flat->offset = 0;
6500 flat->disable_wqm = true;
6501 flat->barrier = barrier_buffer;
6502 ctx->program->needs_exact = true;
6503 ctx->block->instructions.emplace_back(std::move(flat));
6504 } else {
6505 assert(ctx->options->chip_class == GFX6);
6506
6507 switch (instr->intrinsic) {
6508 case nir_intrinsic_global_atomic_add:
6509 op32 = aco_opcode::buffer_atomic_add;
6510 op64 = aco_opcode::buffer_atomic_add_x2;
6511 break;
6512 case nir_intrinsic_global_atomic_imin:
6513 op32 = aco_opcode::buffer_atomic_smin;
6514 op64 = aco_opcode::buffer_atomic_smin_x2;
6515 break;
6516 case nir_intrinsic_global_atomic_umin:
6517 op32 = aco_opcode::buffer_atomic_umin;
6518 op64 = aco_opcode::buffer_atomic_umin_x2;
6519 break;
6520 case nir_intrinsic_global_atomic_imax:
6521 op32 = aco_opcode::buffer_atomic_smax;
6522 op64 = aco_opcode::buffer_atomic_smax_x2;
6523 break;
6524 case nir_intrinsic_global_atomic_umax:
6525 op32 = aco_opcode::buffer_atomic_umax;
6526 op64 = aco_opcode::buffer_atomic_umax_x2;
6527 break;
6528 case nir_intrinsic_global_atomic_and:
6529 op32 = aco_opcode::buffer_atomic_and;
6530 op64 = aco_opcode::buffer_atomic_and_x2;
6531 break;
6532 case nir_intrinsic_global_atomic_or:
6533 op32 = aco_opcode::buffer_atomic_or;
6534 op64 = aco_opcode::buffer_atomic_or_x2;
6535 break;
6536 case nir_intrinsic_global_atomic_xor:
6537 op32 = aco_opcode::buffer_atomic_xor;
6538 op64 = aco_opcode::buffer_atomic_xor_x2;
6539 break;
6540 case nir_intrinsic_global_atomic_exchange:
6541 op32 = aco_opcode::buffer_atomic_swap;
6542 op64 = aco_opcode::buffer_atomic_swap_x2;
6543 break;
6544 case nir_intrinsic_global_atomic_comp_swap:
6545 op32 = aco_opcode::buffer_atomic_cmpswap;
6546 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6547 break;
6548 default:
6549 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6550 }
6551
6552 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6553
6554 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6555
6556 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6557 mubuf->operands[0] = Operand(rsrc);
6558 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6559 mubuf->operands[2] = Operand(0u);
6560 mubuf->operands[3] = Operand(data);
6561 if (return_previous)
6562 mubuf->definitions[0] = Definition(dst);
6563 mubuf->glc = return_previous;
6564 mubuf->dlc = false;
6565 mubuf->offset = 0;
6566 mubuf->addr64 = addr.type() == RegType::vgpr;
6567 mubuf->disable_wqm = true;
6568 mubuf->barrier = barrier_buffer;
6569 ctx->program->needs_exact = true;
6570 ctx->block->instructions.emplace_back(std::move(mubuf));
6571 }
6572 }
6573
6574 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6575 Builder bld(ctx->program, ctx->block);
6576 switch(instr->intrinsic) {
6577 case nir_intrinsic_group_memory_barrier:
6578 case nir_intrinsic_memory_barrier:
6579 bld.barrier(aco_opcode::p_memory_barrier_common);
6580 break;
6581 case nir_intrinsic_memory_barrier_buffer:
6582 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6583 break;
6584 case nir_intrinsic_memory_barrier_image:
6585 bld.barrier(aco_opcode::p_memory_barrier_image);
6586 break;
6587 case nir_intrinsic_memory_barrier_tcs_patch:
6588 case nir_intrinsic_memory_barrier_shared:
6589 bld.barrier(aco_opcode::p_memory_barrier_shared);
6590 break;
6591 default:
6592 unreachable("Unimplemented memory barrier intrinsic");
6593 break;
6594 }
6595 }
6596
6597 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6598 {
6599 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6600 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6601 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6602 Builder bld(ctx->program, ctx->block);
6603
6604 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6605 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6606 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6607 }
6608
6609 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6610 {
6611 unsigned writemask = nir_intrinsic_write_mask(instr);
6612 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6613 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6614 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6615
6616 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6617 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6618 }
6619
6620 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6621 {
6622 unsigned offset = nir_intrinsic_base(instr);
6623 Builder bld(ctx->program, ctx->block);
6624 Operand m = load_lds_size_m0(bld);
6625 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6626 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6627
6628 unsigned num_operands = 3;
6629 aco_opcode op32, op64, op32_rtn, op64_rtn;
6630 switch(instr->intrinsic) {
6631 case nir_intrinsic_shared_atomic_add:
6632 op32 = aco_opcode::ds_add_u32;
6633 op64 = aco_opcode::ds_add_u64;
6634 op32_rtn = aco_opcode::ds_add_rtn_u32;
6635 op64_rtn = aco_opcode::ds_add_rtn_u64;
6636 break;
6637 case nir_intrinsic_shared_atomic_imin:
6638 op32 = aco_opcode::ds_min_i32;
6639 op64 = aco_opcode::ds_min_i64;
6640 op32_rtn = aco_opcode::ds_min_rtn_i32;
6641 op64_rtn = aco_opcode::ds_min_rtn_i64;
6642 break;
6643 case nir_intrinsic_shared_atomic_umin:
6644 op32 = aco_opcode::ds_min_u32;
6645 op64 = aco_opcode::ds_min_u64;
6646 op32_rtn = aco_opcode::ds_min_rtn_u32;
6647 op64_rtn = aco_opcode::ds_min_rtn_u64;
6648 break;
6649 case nir_intrinsic_shared_atomic_imax:
6650 op32 = aco_opcode::ds_max_i32;
6651 op64 = aco_opcode::ds_max_i64;
6652 op32_rtn = aco_opcode::ds_max_rtn_i32;
6653 op64_rtn = aco_opcode::ds_max_rtn_i64;
6654 break;
6655 case nir_intrinsic_shared_atomic_umax:
6656 op32 = aco_opcode::ds_max_u32;
6657 op64 = aco_opcode::ds_max_u64;
6658 op32_rtn = aco_opcode::ds_max_rtn_u32;
6659 op64_rtn = aco_opcode::ds_max_rtn_u64;
6660 break;
6661 case nir_intrinsic_shared_atomic_and:
6662 op32 = aco_opcode::ds_and_b32;
6663 op64 = aco_opcode::ds_and_b64;
6664 op32_rtn = aco_opcode::ds_and_rtn_b32;
6665 op64_rtn = aco_opcode::ds_and_rtn_b64;
6666 break;
6667 case nir_intrinsic_shared_atomic_or:
6668 op32 = aco_opcode::ds_or_b32;
6669 op64 = aco_opcode::ds_or_b64;
6670 op32_rtn = aco_opcode::ds_or_rtn_b32;
6671 op64_rtn = aco_opcode::ds_or_rtn_b64;
6672 break;
6673 case nir_intrinsic_shared_atomic_xor:
6674 op32 = aco_opcode::ds_xor_b32;
6675 op64 = aco_opcode::ds_xor_b64;
6676 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6677 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6678 break;
6679 case nir_intrinsic_shared_atomic_exchange:
6680 op32 = aco_opcode::ds_write_b32;
6681 op64 = aco_opcode::ds_write_b64;
6682 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6683 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6684 break;
6685 case nir_intrinsic_shared_atomic_comp_swap:
6686 op32 = aco_opcode::ds_cmpst_b32;
6687 op64 = aco_opcode::ds_cmpst_b64;
6688 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6689 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6690 num_operands = 4;
6691 break;
6692 default:
6693 unreachable("Unhandled shared atomic intrinsic");
6694 }
6695
6696 /* return the previous value if dest is ever used */
6697 bool return_previous = false;
6698 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6699 return_previous = true;
6700 break;
6701 }
6702 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6703 return_previous = true;
6704 break;
6705 }
6706
6707 aco_opcode op;
6708 if (data.size() == 1) {
6709 assert(instr->dest.ssa.bit_size == 32);
6710 op = return_previous ? op32_rtn : op32;
6711 } else {
6712 assert(instr->dest.ssa.bit_size == 64);
6713 op = return_previous ? op64_rtn : op64;
6714 }
6715
6716 if (offset > 65535) {
6717 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6718 offset = 0;
6719 }
6720
6721 aco_ptr<DS_instruction> ds;
6722 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6723 ds->operands[0] = Operand(address);
6724 ds->operands[1] = Operand(data);
6725 if (num_operands == 4)
6726 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6727 ds->operands[num_operands - 1] = m;
6728 ds->offset0 = offset;
6729 if (return_previous)
6730 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6731 ctx->block->instructions.emplace_back(std::move(ds));
6732 }
6733
6734 Temp get_scratch_resource(isel_context *ctx)
6735 {
6736 Builder bld(ctx->program, ctx->block);
6737 Temp scratch_addr = ctx->program->private_segment_buffer;
6738 if (ctx->stage != compute_cs)
6739 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6740
6741 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6742 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6743
6744 if (ctx->program->chip_class >= GFX10) {
6745 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6746 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6747 S_008F0C_RESOURCE_LEVEL(1);
6748 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6749 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6750 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6751 }
6752
6753 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6754 if (ctx->program->chip_class <= GFX8)
6755 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6756
6757 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6758 }
6759
6760 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6761 Builder bld(ctx->program, ctx->block);
6762 Temp rsrc = get_scratch_resource(ctx);
6763 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6764 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6765
6766 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6767 instr->dest.ssa.bit_size / 8u, rsrc};
6768 info.align_mul = nir_intrinsic_align_mul(instr);
6769 info.align_offset = nir_intrinsic_align_offset(instr);
6770 info.swizzle_component_size = 16;
6771 info.can_reorder = false;
6772 info.soffset = ctx->program->scratch_offset;
6773 emit_mubuf_load(ctx, bld, &info);
6774 }
6775
6776 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6777 Builder bld(ctx->program, ctx->block);
6778 Temp rsrc = get_scratch_resource(ctx);
6779 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6780 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6781
6782 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6783 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6784
6785 unsigned write_count = 0;
6786 Temp write_datas[32];
6787 unsigned offsets[32];
6788 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6789 16, &write_count, write_datas, offsets);
6790
6791 for (unsigned i = 0; i < write_count; i++) {
6792 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6793 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true);
6794 }
6795 }
6796
6797 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6798 uint8_t log2_ps_iter_samples;
6799 if (ctx->program->info->ps.force_persample) {
6800 log2_ps_iter_samples =
6801 util_logbase2(ctx->options->key.fs.num_samples);
6802 } else {
6803 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6804 }
6805
6806 /* The bit pattern matches that used by fixed function fragment
6807 * processing. */
6808 static const unsigned ps_iter_masks[] = {
6809 0xffff, /* not used */
6810 0x5555,
6811 0x1111,
6812 0x0101,
6813 0x0001,
6814 };
6815 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6816
6817 Builder bld(ctx->program, ctx->block);
6818
6819 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6820 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6821 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6822 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6823 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6824 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6825 }
6826
6827 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6828 Builder bld(ctx->program, ctx->block);
6829
6830 unsigned stream = nir_intrinsic_stream_id(instr);
6831 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6832 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6833 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6834
6835 /* get GSVS ring */
6836 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6837
6838 unsigned num_components =
6839 ctx->program->info->gs.num_stream_output_components[stream];
6840 assert(num_components);
6841
6842 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6843 unsigned stream_offset = 0;
6844 for (unsigned i = 0; i < stream; i++) {
6845 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6846 stream_offset += prev_stride * ctx->program->wave_size;
6847 }
6848
6849 /* Limit on the stride field for <= GFX7. */
6850 assert(stride < (1 << 14));
6851
6852 Temp gsvs_dwords[4];
6853 for (unsigned i = 0; i < 4; i++)
6854 gsvs_dwords[i] = bld.tmp(s1);
6855 bld.pseudo(aco_opcode::p_split_vector,
6856 Definition(gsvs_dwords[0]),
6857 Definition(gsvs_dwords[1]),
6858 Definition(gsvs_dwords[2]),
6859 Definition(gsvs_dwords[3]),
6860 gsvs_ring);
6861
6862 if (stream_offset) {
6863 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6864
6865 Temp carry = bld.tmp(s1);
6866 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6867 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));
6868 }
6869
6870 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)));
6871 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6872
6873 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6874 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6875
6876 unsigned offset = 0;
6877 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6878 if (ctx->program->info->gs.output_streams[i] != stream)
6879 continue;
6880
6881 for (unsigned j = 0; j < 4; j++) {
6882 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6883 continue;
6884
6885 if (ctx->outputs.mask[i] & (1 << j)) {
6886 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6887 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6888 if (const_offset >= 4096u) {
6889 if (vaddr_offset.isUndefined())
6890 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6891 else
6892 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6893 const_offset %= 4096u;
6894 }
6895
6896 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6897 mtbuf->operands[0] = Operand(gsvs_ring);
6898 mtbuf->operands[1] = vaddr_offset;
6899 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6900 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6901 mtbuf->offen = !vaddr_offset.isUndefined();
6902 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6903 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6904 mtbuf->offset = const_offset;
6905 mtbuf->glc = true;
6906 mtbuf->slc = true;
6907 mtbuf->barrier = barrier_gs_data;
6908 mtbuf->can_reorder = true;
6909 bld.insert(std::move(mtbuf));
6910 }
6911
6912 offset += ctx->shader->info.gs.vertices_out;
6913 }
6914
6915 /* outputs for the next vertex are undefined and keeping them around can
6916 * create invalid IR with control flow */
6917 ctx->outputs.mask[i] = 0;
6918 }
6919
6920 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6921 }
6922
6923 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6924 {
6925 Builder bld(ctx->program, ctx->block);
6926
6927 if (cluster_size == 1) {
6928 return src;
6929 } if (op == nir_op_iand && cluster_size == 4) {
6930 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6931 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6932 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6933 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6934 } else if (op == nir_op_ior && cluster_size == 4) {
6935 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6936 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6937 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6938 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6939 //subgroupAnd(val) -> (exec & ~val) == 0
6940 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6941 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6942 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6943 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6944 //subgroupOr(val) -> (val & exec) != 0
6945 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6946 return bool_to_vector_condition(ctx, tmp);
6947 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6948 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6949 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6950 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6951 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6952 return bool_to_vector_condition(ctx, tmp);
6953 } else {
6954 //subgroupClustered{And,Or,Xor}(val, n) ->
6955 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6956 //cluster_offset = ~(n - 1) & lane_id
6957 //cluster_mask = ((1 << n) - 1)
6958 //subgroupClusteredAnd():
6959 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6960 //subgroupClusteredOr():
6961 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6962 //subgroupClusteredXor():
6963 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6964 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6965 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6966
6967 Temp tmp;
6968 if (op == nir_op_iand)
6969 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6970 else
6971 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6972
6973 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6974
6975 if (ctx->program->chip_class <= GFX7)
6976 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6977 else if (ctx->program->wave_size == 64)
6978 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6979 else
6980 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6981 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6982 if (cluster_mask != 0xffffffff)
6983 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6984
6985 Definition cmp_def = Definition();
6986 if (op == nir_op_iand) {
6987 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6988 } else if (op == nir_op_ior) {
6989 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6990 } else if (op == nir_op_ixor) {
6991 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
6992 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
6993 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6994 }
6995 cmp_def.setHint(vcc);
6996 return cmp_def.getTemp();
6997 }
6998 }
6999
7000 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7001 {
7002 Builder bld(ctx->program, ctx->block);
7003
7004 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7005 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7006 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7007 Temp tmp;
7008 if (op == nir_op_iand)
7009 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7010 else
7011 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7012
7013 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7014 Temp lo = lohi.def(0).getTemp();
7015 Temp hi = lohi.def(1).getTemp();
7016 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7017
7018 Definition cmp_def = Definition();
7019 if (op == nir_op_iand)
7020 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7021 else if (op == nir_op_ior)
7022 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7023 else if (op == nir_op_ixor)
7024 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7025 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7026 cmp_def.setHint(vcc);
7027 return cmp_def.getTemp();
7028 }
7029
7030 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7031 {
7032 Builder bld(ctx->program, ctx->block);
7033
7034 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7035 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7036 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7037 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7038 if (op == nir_op_iand)
7039 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7040 else if (op == nir_op_ior)
7041 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7042 else if (op == nir_op_ixor)
7043 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7044
7045 assert(false);
7046 return Temp();
7047 }
7048
7049 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7050 {
7051 Builder bld(ctx->program, ctx->block);
7052 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7053 if (src.regClass().type() == RegType::vgpr) {
7054 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7055 } else if (src.regClass() == s1) {
7056 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7057 } else if (src.regClass() == s2) {
7058 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7059 } else {
7060 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7061 nir_print_instr(&instr->instr, stderr);
7062 fprintf(stderr, "\n");
7063 }
7064 }
7065
7066 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7067 {
7068 Builder bld(ctx->program, ctx->block);
7069 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7070 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7071 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7072
7073 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7074 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7075 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7076 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7077
7078 /* Build DD X/Y */
7079 if (ctx->program->chip_class >= GFX8) {
7080 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7081 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7082 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7083 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7084 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7085 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7086 } else {
7087 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7088 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7089 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7090 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7091 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7092 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7093 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7094 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7095 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7096 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7097 }
7098
7099 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7100 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7101 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7102 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7103 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7104 Temp wqm1 = bld.tmp(v1);
7105 emit_wqm(ctx, tmp1, wqm1, true);
7106 Temp wqm2 = bld.tmp(v1);
7107 emit_wqm(ctx, tmp2, wqm2, true);
7108 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7109 return;
7110 }
7111
7112 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7113 {
7114 Builder bld(ctx->program, ctx->block);
7115 switch(instr->intrinsic) {
7116 case nir_intrinsic_load_barycentric_sample:
7117 case nir_intrinsic_load_barycentric_pixel:
7118 case nir_intrinsic_load_barycentric_centroid: {
7119 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7120 Temp bary = Temp(0, s2);
7121 switch (mode) {
7122 case INTERP_MODE_SMOOTH:
7123 case INTERP_MODE_NONE:
7124 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7125 bary = get_arg(ctx, ctx->args->ac.persp_center);
7126 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7127 bary = ctx->persp_centroid;
7128 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7129 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7130 break;
7131 case INTERP_MODE_NOPERSPECTIVE:
7132 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7133 bary = get_arg(ctx, ctx->args->ac.linear_center);
7134 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7135 bary = ctx->linear_centroid;
7136 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7137 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7138 break;
7139 default:
7140 break;
7141 }
7142 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7143 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7144 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7145 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7146 Operand(p1), Operand(p2));
7147 emit_split_vector(ctx, dst, 2);
7148 break;
7149 }
7150 case nir_intrinsic_load_barycentric_model: {
7151 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7152
7153 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7154 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7155 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7156 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7157 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7158 Operand(p1), Operand(p2), Operand(p3));
7159 emit_split_vector(ctx, dst, 3);
7160 break;
7161 }
7162 case nir_intrinsic_load_barycentric_at_sample: {
7163 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7164 switch (ctx->options->key.fs.num_samples) {
7165 case 2: sample_pos_offset += 1 << 3; break;
7166 case 4: sample_pos_offset += 3 << 3; break;
7167 case 8: sample_pos_offset += 7 << 3; break;
7168 default: break;
7169 }
7170 Temp sample_pos;
7171 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7172 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7173 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7174 if (addr.type() == RegType::sgpr) {
7175 Operand offset;
7176 if (const_addr) {
7177 sample_pos_offset += const_addr->u32 << 3;
7178 offset = Operand(sample_pos_offset);
7179 } else if (ctx->options->chip_class >= GFX9) {
7180 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7181 } else {
7182 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7183 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7184 }
7185
7186 Operand off = bld.copy(bld.def(s1), Operand(offset));
7187 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7188
7189 } else if (ctx->options->chip_class >= GFX9) {
7190 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7191 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7192 } else if (ctx->options->chip_class >= GFX7) {
7193 /* addr += private_segment_buffer + sample_pos_offset */
7194 Temp tmp0 = bld.tmp(s1);
7195 Temp tmp1 = bld.tmp(s1);
7196 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7197 Definition scc_tmp = bld.def(s1, scc);
7198 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7199 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7200 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7201 Temp pck0 = bld.tmp(v1);
7202 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7203 tmp1 = as_vgpr(ctx, tmp1);
7204 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);
7205 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7206
7207 /* sample_pos = flat_load_dwordx2 addr */
7208 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7209 } else {
7210 assert(ctx->options->chip_class == GFX6);
7211
7212 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7213 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7214 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7215
7216 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7217 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7218
7219 sample_pos = bld.tmp(v2);
7220
7221 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7222 load->definitions[0] = Definition(sample_pos);
7223 load->operands[0] = Operand(rsrc);
7224 load->operands[1] = Operand(addr);
7225 load->operands[2] = Operand(0u);
7226 load->offset = sample_pos_offset;
7227 load->offen = 0;
7228 load->addr64 = true;
7229 load->glc = false;
7230 load->dlc = false;
7231 load->disable_wqm = false;
7232 load->barrier = barrier_none;
7233 load->can_reorder = true;
7234 ctx->block->instructions.emplace_back(std::move(load));
7235 }
7236
7237 /* sample_pos -= 0.5 */
7238 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7239 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7240 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7241 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7242 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7243
7244 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7245 break;
7246 }
7247 case nir_intrinsic_load_barycentric_at_offset: {
7248 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7249 RegClass rc = RegClass(offset.type(), 1);
7250 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7251 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7252 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7253 break;
7254 }
7255 case nir_intrinsic_load_front_face: {
7256 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7257 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7258 break;
7259 }
7260 case nir_intrinsic_load_view_index: {
7261 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7262 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7263 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7264 break;
7265 }
7266
7267 /* fallthrough */
7268 }
7269 case nir_intrinsic_load_layer_id: {
7270 unsigned idx = nir_intrinsic_base(instr);
7271 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7272 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7273 break;
7274 }
7275 case nir_intrinsic_load_frag_coord: {
7276 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7277 break;
7278 }
7279 case nir_intrinsic_load_sample_pos: {
7280 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7281 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7282 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7283 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7284 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7285 break;
7286 }
7287 case nir_intrinsic_load_tess_coord:
7288 visit_load_tess_coord(ctx, instr);
7289 break;
7290 case nir_intrinsic_load_interpolated_input:
7291 visit_load_interpolated_input(ctx, instr);
7292 break;
7293 case nir_intrinsic_store_output:
7294 visit_store_output(ctx, instr);
7295 break;
7296 case nir_intrinsic_load_input:
7297 case nir_intrinsic_load_input_vertex:
7298 visit_load_input(ctx, instr);
7299 break;
7300 case nir_intrinsic_load_output:
7301 visit_load_output(ctx, instr);
7302 break;
7303 case nir_intrinsic_load_per_vertex_input:
7304 visit_load_per_vertex_input(ctx, instr);
7305 break;
7306 case nir_intrinsic_load_per_vertex_output:
7307 visit_load_per_vertex_output(ctx, instr);
7308 break;
7309 case nir_intrinsic_store_per_vertex_output:
7310 visit_store_per_vertex_output(ctx, instr);
7311 break;
7312 case nir_intrinsic_load_ubo:
7313 visit_load_ubo(ctx, instr);
7314 break;
7315 case nir_intrinsic_load_push_constant:
7316 visit_load_push_constant(ctx, instr);
7317 break;
7318 case nir_intrinsic_load_constant:
7319 visit_load_constant(ctx, instr);
7320 break;
7321 case nir_intrinsic_vulkan_resource_index:
7322 visit_load_resource(ctx, instr);
7323 break;
7324 case nir_intrinsic_discard:
7325 visit_discard(ctx, instr);
7326 break;
7327 case nir_intrinsic_discard_if:
7328 visit_discard_if(ctx, instr);
7329 break;
7330 case nir_intrinsic_load_shared:
7331 visit_load_shared(ctx, instr);
7332 break;
7333 case nir_intrinsic_store_shared:
7334 visit_store_shared(ctx, instr);
7335 break;
7336 case nir_intrinsic_shared_atomic_add:
7337 case nir_intrinsic_shared_atomic_imin:
7338 case nir_intrinsic_shared_atomic_umin:
7339 case nir_intrinsic_shared_atomic_imax:
7340 case nir_intrinsic_shared_atomic_umax:
7341 case nir_intrinsic_shared_atomic_and:
7342 case nir_intrinsic_shared_atomic_or:
7343 case nir_intrinsic_shared_atomic_xor:
7344 case nir_intrinsic_shared_atomic_exchange:
7345 case nir_intrinsic_shared_atomic_comp_swap:
7346 visit_shared_atomic(ctx, instr);
7347 break;
7348 case nir_intrinsic_image_deref_load:
7349 visit_image_load(ctx, instr);
7350 break;
7351 case nir_intrinsic_image_deref_store:
7352 visit_image_store(ctx, instr);
7353 break;
7354 case nir_intrinsic_image_deref_atomic_add:
7355 case nir_intrinsic_image_deref_atomic_umin:
7356 case nir_intrinsic_image_deref_atomic_imin:
7357 case nir_intrinsic_image_deref_atomic_umax:
7358 case nir_intrinsic_image_deref_atomic_imax:
7359 case nir_intrinsic_image_deref_atomic_and:
7360 case nir_intrinsic_image_deref_atomic_or:
7361 case nir_intrinsic_image_deref_atomic_xor:
7362 case nir_intrinsic_image_deref_atomic_exchange:
7363 case nir_intrinsic_image_deref_atomic_comp_swap:
7364 visit_image_atomic(ctx, instr);
7365 break;
7366 case nir_intrinsic_image_deref_size:
7367 visit_image_size(ctx, instr);
7368 break;
7369 case nir_intrinsic_load_ssbo:
7370 visit_load_ssbo(ctx, instr);
7371 break;
7372 case nir_intrinsic_store_ssbo:
7373 visit_store_ssbo(ctx, instr);
7374 break;
7375 case nir_intrinsic_load_global:
7376 visit_load_global(ctx, instr);
7377 break;
7378 case nir_intrinsic_store_global:
7379 visit_store_global(ctx, instr);
7380 break;
7381 case nir_intrinsic_global_atomic_add:
7382 case nir_intrinsic_global_atomic_imin:
7383 case nir_intrinsic_global_atomic_umin:
7384 case nir_intrinsic_global_atomic_imax:
7385 case nir_intrinsic_global_atomic_umax:
7386 case nir_intrinsic_global_atomic_and:
7387 case nir_intrinsic_global_atomic_or:
7388 case nir_intrinsic_global_atomic_xor:
7389 case nir_intrinsic_global_atomic_exchange:
7390 case nir_intrinsic_global_atomic_comp_swap:
7391 visit_global_atomic(ctx, instr);
7392 break;
7393 case nir_intrinsic_ssbo_atomic_add:
7394 case nir_intrinsic_ssbo_atomic_imin:
7395 case nir_intrinsic_ssbo_atomic_umin:
7396 case nir_intrinsic_ssbo_atomic_imax:
7397 case nir_intrinsic_ssbo_atomic_umax:
7398 case nir_intrinsic_ssbo_atomic_and:
7399 case nir_intrinsic_ssbo_atomic_or:
7400 case nir_intrinsic_ssbo_atomic_xor:
7401 case nir_intrinsic_ssbo_atomic_exchange:
7402 case nir_intrinsic_ssbo_atomic_comp_swap:
7403 visit_atomic_ssbo(ctx, instr);
7404 break;
7405 case nir_intrinsic_load_scratch:
7406 visit_load_scratch(ctx, instr);
7407 break;
7408 case nir_intrinsic_store_scratch:
7409 visit_store_scratch(ctx, instr);
7410 break;
7411 case nir_intrinsic_get_buffer_size:
7412 visit_get_buffer_size(ctx, instr);
7413 break;
7414 case nir_intrinsic_control_barrier: {
7415 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7416 /* GFX6 only (thanks to a hw bug workaround):
7417 * The real barrier instruction isn’t needed, because an entire patch
7418 * always fits into a single wave.
7419 */
7420 break;
7421 }
7422
7423 if (ctx->program->workgroup_size > ctx->program->wave_size)
7424 bld.sopp(aco_opcode::s_barrier);
7425
7426 break;
7427 }
7428 case nir_intrinsic_memory_barrier_tcs_patch:
7429 case nir_intrinsic_group_memory_barrier:
7430 case nir_intrinsic_memory_barrier:
7431 case nir_intrinsic_memory_barrier_buffer:
7432 case nir_intrinsic_memory_barrier_image:
7433 case nir_intrinsic_memory_barrier_shared:
7434 emit_memory_barrier(ctx, instr);
7435 break;
7436 case nir_intrinsic_load_num_work_groups: {
7437 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7438 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7439 emit_split_vector(ctx, dst, 3);
7440 break;
7441 }
7442 case nir_intrinsic_load_local_invocation_id: {
7443 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7444 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7445 emit_split_vector(ctx, dst, 3);
7446 break;
7447 }
7448 case nir_intrinsic_load_work_group_id: {
7449 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7450 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7451 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7452 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7453 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7454 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7455 emit_split_vector(ctx, dst, 3);
7456 break;
7457 }
7458 case nir_intrinsic_load_local_invocation_index: {
7459 Temp id = emit_mbcnt(ctx, bld.def(v1));
7460
7461 /* The tg_size bits [6:11] contain the subgroup id,
7462 * we need this multiplied by the wave size, and then OR the thread id to it.
7463 */
7464 if (ctx->program->wave_size == 64) {
7465 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7466 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7467 get_arg(ctx, ctx->args->ac.tg_size));
7468 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7469 } else {
7470 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7471 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7472 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7473 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7474 }
7475 break;
7476 }
7477 case nir_intrinsic_load_subgroup_id: {
7478 if (ctx->stage == compute_cs) {
7479 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7480 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7481 } else {
7482 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7483 }
7484 break;
7485 }
7486 case nir_intrinsic_load_subgroup_invocation: {
7487 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7488 break;
7489 }
7490 case nir_intrinsic_load_num_subgroups: {
7491 if (ctx->stage == compute_cs)
7492 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7493 get_arg(ctx, ctx->args->ac.tg_size));
7494 else
7495 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7496 break;
7497 }
7498 case nir_intrinsic_ballot: {
7499 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7500 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7501 Definition tmp = bld.def(dst.regClass());
7502 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7503 if (instr->src[0].ssa->bit_size == 1) {
7504 assert(src.regClass() == bld.lm);
7505 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7506 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7507 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7508 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7509 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7510 } else {
7511 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7512 nir_print_instr(&instr->instr, stderr);
7513 fprintf(stderr, "\n");
7514 }
7515 if (dst.size() != bld.lm.size()) {
7516 /* Wave32 with ballot size set to 64 */
7517 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7518 }
7519 emit_wqm(ctx, tmp.getTemp(), dst);
7520 break;
7521 }
7522 case nir_intrinsic_shuffle:
7523 case nir_intrinsic_read_invocation: {
7524 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7525 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7526 emit_uniform_subgroup(ctx, instr, src);
7527 } else {
7528 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7529 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7530 tid = bld.as_uniform(tid);
7531 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7532 if (src.regClass() == v1) {
7533 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7534 } else if (src.regClass() == v2) {
7535 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7536 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7537 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7538 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7539 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7540 emit_split_vector(ctx, dst, 2);
7541 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7542 assert(src.regClass() == bld.lm);
7543 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7544 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7545 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7546 assert(src.regClass() == bld.lm);
7547 Temp tmp;
7548 if (ctx->program->chip_class <= GFX7)
7549 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7550 else if (ctx->program->wave_size == 64)
7551 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7552 else
7553 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7554 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7555 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7556 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7557 } else {
7558 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7559 nir_print_instr(&instr->instr, stderr);
7560 fprintf(stderr, "\n");
7561 }
7562 }
7563 break;
7564 }
7565 case nir_intrinsic_load_sample_id: {
7566 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7567 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7568 break;
7569 }
7570 case nir_intrinsic_load_sample_mask_in: {
7571 visit_load_sample_mask_in(ctx, instr);
7572 break;
7573 }
7574 case nir_intrinsic_read_first_invocation: {
7575 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7576 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7577 if (src.regClass() == v1) {
7578 emit_wqm(ctx,
7579 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7580 dst);
7581 } else if (src.regClass() == v2) {
7582 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7583 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7584 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7585 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7586 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7587 emit_split_vector(ctx, dst, 2);
7588 } else if (instr->dest.ssa.bit_size == 1) {
7589 assert(src.regClass() == bld.lm);
7590 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7591 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7592 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7593 } else if (src.regClass() == s1) {
7594 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7595 } else if (src.regClass() == s2) {
7596 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7597 } else {
7598 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7599 nir_print_instr(&instr->instr, stderr);
7600 fprintf(stderr, "\n");
7601 }
7602 break;
7603 }
7604 case nir_intrinsic_vote_all: {
7605 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7606 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7607 assert(src.regClass() == bld.lm);
7608 assert(dst.regClass() == bld.lm);
7609
7610 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7611 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7612 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7613 break;
7614 }
7615 case nir_intrinsic_vote_any: {
7616 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7617 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7618 assert(src.regClass() == bld.lm);
7619 assert(dst.regClass() == bld.lm);
7620
7621 Temp tmp = bool_to_scalar_condition(ctx, src);
7622 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7623 break;
7624 }
7625 case nir_intrinsic_reduce:
7626 case nir_intrinsic_inclusive_scan:
7627 case nir_intrinsic_exclusive_scan: {
7628 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7629 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7630 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7631 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7632 nir_intrinsic_cluster_size(instr) : 0;
7633 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7634
7635 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7636 emit_uniform_subgroup(ctx, instr, src);
7637 } else if (instr->dest.ssa.bit_size == 1) {
7638 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7639 op = nir_op_iand;
7640 else if (op == nir_op_iadd)
7641 op = nir_op_ixor;
7642 else if (op == nir_op_umax || op == nir_op_imax)
7643 op = nir_op_ior;
7644 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7645
7646 switch (instr->intrinsic) {
7647 case nir_intrinsic_reduce:
7648 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7649 break;
7650 case nir_intrinsic_exclusive_scan:
7651 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7652 break;
7653 case nir_intrinsic_inclusive_scan:
7654 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7655 break;
7656 default:
7657 assert(false);
7658 }
7659 } else if (cluster_size == 1) {
7660 bld.copy(Definition(dst), src);
7661 } else {
7662 src = as_vgpr(ctx, src);
7663
7664 ReduceOp reduce_op;
7665 switch (op) {
7666 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7667 CASE(iadd)
7668 CASE(imul)
7669 CASE(fadd)
7670 CASE(fmul)
7671 CASE(imin)
7672 CASE(umin)
7673 CASE(fmin)
7674 CASE(imax)
7675 CASE(umax)
7676 CASE(fmax)
7677 CASE(iand)
7678 CASE(ior)
7679 CASE(ixor)
7680 default:
7681 unreachable("unknown reduction op");
7682 #undef CASE
7683 }
7684
7685 aco_opcode aco_op;
7686 switch (instr->intrinsic) {
7687 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7688 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7689 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7690 default:
7691 unreachable("unknown reduce intrinsic");
7692 }
7693
7694 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7695 reduce->operands[0] = Operand(src);
7696 // filled in by aco_reduce_assign.cpp, used internally as part of the
7697 // reduce sequence
7698 assert(dst.size() == 1 || dst.size() == 2);
7699 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7700 reduce->operands[2] = Operand(v1.as_linear());
7701
7702 Temp tmp_dst = bld.tmp(dst.regClass());
7703 reduce->definitions[0] = Definition(tmp_dst);
7704 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7705 reduce->definitions[2] = Definition();
7706 reduce->definitions[3] = Definition(scc, s1);
7707 reduce->definitions[4] = Definition();
7708 reduce->reduce_op = reduce_op;
7709 reduce->cluster_size = cluster_size;
7710 ctx->block->instructions.emplace_back(std::move(reduce));
7711
7712 emit_wqm(ctx, tmp_dst, dst);
7713 }
7714 break;
7715 }
7716 case nir_intrinsic_quad_broadcast: {
7717 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7718 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7719 emit_uniform_subgroup(ctx, instr, src);
7720 } else {
7721 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7722 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7723 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7724
7725 if (instr->dest.ssa.bit_size == 1) {
7726 assert(src.regClass() == bld.lm);
7727 assert(dst.regClass() == bld.lm);
7728 uint32_t half_mask = 0x11111111u << lane;
7729 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7730 Temp tmp = bld.tmp(bld.lm);
7731 bld.sop1(Builder::s_wqm, Definition(tmp),
7732 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7733 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7734 emit_wqm(ctx, tmp, dst);
7735 } else if (instr->dest.ssa.bit_size == 32) {
7736 if (ctx->program->chip_class >= GFX8)
7737 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7738 else
7739 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7740 } else if (instr->dest.ssa.bit_size == 64) {
7741 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7742 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7743 if (ctx->program->chip_class >= GFX8) {
7744 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7745 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7746 } else {
7747 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7748 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7749 }
7750 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7751 emit_split_vector(ctx, dst, 2);
7752 } else {
7753 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7754 nir_print_instr(&instr->instr, stderr);
7755 fprintf(stderr, "\n");
7756 }
7757 }
7758 break;
7759 }
7760 case nir_intrinsic_quad_swap_horizontal:
7761 case nir_intrinsic_quad_swap_vertical:
7762 case nir_intrinsic_quad_swap_diagonal:
7763 case nir_intrinsic_quad_swizzle_amd: {
7764 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7765 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7766 emit_uniform_subgroup(ctx, instr, src);
7767 break;
7768 }
7769 uint16_t dpp_ctrl = 0;
7770 switch (instr->intrinsic) {
7771 case nir_intrinsic_quad_swap_horizontal:
7772 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7773 break;
7774 case nir_intrinsic_quad_swap_vertical:
7775 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7776 break;
7777 case nir_intrinsic_quad_swap_diagonal:
7778 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7779 break;
7780 case nir_intrinsic_quad_swizzle_amd:
7781 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7782 break;
7783 default:
7784 break;
7785 }
7786 if (ctx->program->chip_class < GFX8)
7787 dpp_ctrl |= (1 << 15);
7788
7789 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7790 if (instr->dest.ssa.bit_size == 1) {
7791 assert(src.regClass() == bld.lm);
7792 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7793 if (ctx->program->chip_class >= GFX8)
7794 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7795 else
7796 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7797 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7798 emit_wqm(ctx, tmp, dst);
7799 } else if (instr->dest.ssa.bit_size == 32) {
7800 Temp tmp;
7801 if (ctx->program->chip_class >= GFX8)
7802 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7803 else
7804 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7805 emit_wqm(ctx, tmp, dst);
7806 } else if (instr->dest.ssa.bit_size == 64) {
7807 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7808 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7809 if (ctx->program->chip_class >= GFX8) {
7810 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7811 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7812 } else {
7813 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7814 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7815 }
7816 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7817 emit_split_vector(ctx, dst, 2);
7818 } else {
7819 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7820 nir_print_instr(&instr->instr, stderr);
7821 fprintf(stderr, "\n");
7822 }
7823 break;
7824 }
7825 case nir_intrinsic_masked_swizzle_amd: {
7826 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7827 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7828 emit_uniform_subgroup(ctx, instr, src);
7829 break;
7830 }
7831 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7832 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7833 if (dst.regClass() == v1) {
7834 emit_wqm(ctx,
7835 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7836 dst);
7837 } else if (dst.regClass() == v2) {
7838 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7839 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7840 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7841 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7842 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7843 emit_split_vector(ctx, dst, 2);
7844 } else {
7845 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7846 nir_print_instr(&instr->instr, stderr);
7847 fprintf(stderr, "\n");
7848 }
7849 break;
7850 }
7851 case nir_intrinsic_write_invocation_amd: {
7852 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7853 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7854 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7855 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7856 if (dst.regClass() == v1) {
7857 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7858 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7859 } else if (dst.regClass() == v2) {
7860 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7861 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7862 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7863 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7864 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7865 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7866 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7867 emit_split_vector(ctx, dst, 2);
7868 } else {
7869 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7870 nir_print_instr(&instr->instr, stderr);
7871 fprintf(stderr, "\n");
7872 }
7873 break;
7874 }
7875 case nir_intrinsic_mbcnt_amd: {
7876 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7877 RegClass rc = RegClass(src.type(), 1);
7878 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7879 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7880 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7881 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7882 emit_wqm(ctx, wqm_tmp, dst);
7883 break;
7884 }
7885 case nir_intrinsic_load_helper_invocation: {
7886 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7887 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7888 ctx->block->kind |= block_kind_needs_lowering;
7889 ctx->program->needs_exact = true;
7890 break;
7891 }
7892 case nir_intrinsic_is_helper_invocation: {
7893 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7894 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7895 ctx->block->kind |= block_kind_needs_lowering;
7896 ctx->program->needs_exact = true;
7897 break;
7898 }
7899 case nir_intrinsic_demote:
7900 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7901
7902 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7903 ctx->cf_info.exec_potentially_empty_discard = true;
7904 ctx->block->kind |= block_kind_uses_demote;
7905 ctx->program->needs_exact = true;
7906 break;
7907 case nir_intrinsic_demote_if: {
7908 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7909 assert(src.regClass() == bld.lm);
7910 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7911 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7912
7913 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7914 ctx->cf_info.exec_potentially_empty_discard = true;
7915 ctx->block->kind |= block_kind_uses_demote;
7916 ctx->program->needs_exact = true;
7917 break;
7918 }
7919 case nir_intrinsic_first_invocation: {
7920 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7921 get_ssa_temp(ctx, &instr->dest.ssa));
7922 break;
7923 }
7924 case nir_intrinsic_shader_clock:
7925 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7926 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7927 break;
7928 case nir_intrinsic_load_vertex_id_zero_base: {
7929 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7930 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7931 break;
7932 }
7933 case nir_intrinsic_load_first_vertex: {
7934 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7935 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7936 break;
7937 }
7938 case nir_intrinsic_load_base_instance: {
7939 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7940 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7941 break;
7942 }
7943 case nir_intrinsic_load_instance_id: {
7944 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7945 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7946 break;
7947 }
7948 case nir_intrinsic_load_draw_id: {
7949 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7950 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7951 break;
7952 }
7953 case nir_intrinsic_load_invocation_id: {
7954 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7955
7956 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7957 if (ctx->options->chip_class >= GFX10)
7958 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7959 else
7960 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7961 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7962 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7963 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7964 } else {
7965 unreachable("Unsupported stage for load_invocation_id");
7966 }
7967
7968 break;
7969 }
7970 case nir_intrinsic_load_primitive_id: {
7971 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7972
7973 switch (ctx->shader->info.stage) {
7974 case MESA_SHADER_GEOMETRY:
7975 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7976 break;
7977 case MESA_SHADER_TESS_CTRL:
7978 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7979 break;
7980 case MESA_SHADER_TESS_EVAL:
7981 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7982 break;
7983 default:
7984 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7985 }
7986
7987 break;
7988 }
7989 case nir_intrinsic_load_patch_vertices_in: {
7990 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
7991 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
7992
7993 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7994 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
7995 break;
7996 }
7997 case nir_intrinsic_emit_vertex_with_counter: {
7998 visit_emit_vertex_with_counter(ctx, instr);
7999 break;
8000 }
8001 case nir_intrinsic_end_primitive_with_counter: {
8002 unsigned stream = nir_intrinsic_stream_id(instr);
8003 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8004 break;
8005 }
8006 case nir_intrinsic_set_vertex_count: {
8007 /* unused, the HW keeps track of this for us */
8008 break;
8009 }
8010 default:
8011 fprintf(stderr, "Unimplemented intrinsic instr: ");
8012 nir_print_instr(&instr->instr, stderr);
8013 fprintf(stderr, "\n");
8014 abort();
8015
8016 break;
8017 }
8018 }
8019
8020
8021 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8022 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8023 enum glsl_base_type *stype)
8024 {
8025 nir_deref_instr *texture_deref_instr = NULL;
8026 nir_deref_instr *sampler_deref_instr = NULL;
8027 int plane = -1;
8028
8029 for (unsigned i = 0; i < instr->num_srcs; i++) {
8030 switch (instr->src[i].src_type) {
8031 case nir_tex_src_texture_deref:
8032 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8033 break;
8034 case nir_tex_src_sampler_deref:
8035 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8036 break;
8037 case nir_tex_src_plane:
8038 plane = nir_src_as_int(instr->src[i].src);
8039 break;
8040 default:
8041 break;
8042 }
8043 }
8044
8045 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8046
8047 if (!sampler_deref_instr)
8048 sampler_deref_instr = texture_deref_instr;
8049
8050 if (plane >= 0) {
8051 assert(instr->op != nir_texop_txf_ms &&
8052 instr->op != nir_texop_samples_identical);
8053 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8054 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8055 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8056 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8057 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8058 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8059 } else {
8060 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8061 }
8062 if (samp_ptr) {
8063 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8064
8065 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8066 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8067 Builder bld(ctx->program, ctx->block);
8068
8069 /* to avoid unnecessary moves, we split and recombine sampler and image */
8070 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8071 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8072 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8073 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8074 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8075 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8076 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8077 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8078
8079 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8080 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8081 img[0], img[1], img[2], img[3],
8082 img[4], img[5], img[6], img[7]);
8083 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8084 samp[0], samp[1], samp[2], samp[3]);
8085 }
8086 }
8087 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8088 instr->op == nir_texop_samples_identical))
8089 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8090 }
8091
8092 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8093 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8094 {
8095 Builder bld(ctx->program, ctx->block);
8096
8097 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8098 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8099 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8100
8101 Operand neg_one(0xbf800000u);
8102 Operand one(0x3f800000u);
8103 Operand two(0x40000000u);
8104 Operand four(0x40800000u);
8105
8106 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8107 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8108 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8109
8110 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8111 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8112 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8113 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);
8114
8115 // select sc
8116 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8117 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8118 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8119 one, is_ma_y);
8120 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8121
8122 // select tc
8123 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8124 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8125 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8126
8127 // select ma
8128 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8129 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8130 deriv_z, is_ma_z);
8131 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8132 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8133 }
8134
8135 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8136 {
8137 Builder bld(ctx->program, ctx->block);
8138 Temp ma, tc, sc, id;
8139
8140 if (is_array) {
8141 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8142
8143 // see comment in ac_prepare_cube_coords()
8144 if (ctx->options->chip_class <= GFX8)
8145 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8146 }
8147
8148 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8149
8150 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8151 vop3a->operands[0] = Operand(ma);
8152 vop3a->abs[0] = true;
8153 Temp invma = bld.tmp(v1);
8154 vop3a->definitions[0] = Definition(invma);
8155 ctx->block->instructions.emplace_back(std::move(vop3a));
8156
8157 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8158 if (!is_deriv)
8159 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8160
8161 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8162 if (!is_deriv)
8163 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8164
8165 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8166
8167 if (is_deriv) {
8168 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8169 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8170
8171 for (unsigned i = 0; i < 2; i++) {
8172 // see comment in ac_prepare_cube_coords()
8173 Temp deriv_ma;
8174 Temp deriv_sc, deriv_tc;
8175 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8176 &deriv_ma, &deriv_sc, &deriv_tc);
8177
8178 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8179
8180 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8181 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8182 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8183 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8184 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8185 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8186 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8187 }
8188
8189 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8190 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8191 }
8192
8193 if (is_array)
8194 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8195 coords.resize(3);
8196 coords[0] = sc;
8197 coords[1] = tc;
8198 coords[2] = id;
8199 }
8200
8201 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8202 {
8203 if (vec->parent_instr->type != nir_instr_type_alu)
8204 return;
8205 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8206 if (vec_instr->op != nir_op_vec(vec->num_components))
8207 return;
8208
8209 for (unsigned i = 0; i < vec->num_components; i++) {
8210 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8211 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8212 }
8213 }
8214
8215 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8216 {
8217 Builder bld(ctx->program, ctx->block);
8218 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8219 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8220 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8221 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8222 std::vector<Temp> coords;
8223 std::vector<Temp> derivs;
8224 nir_const_value *sample_index_cv = NULL;
8225 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8226 enum glsl_base_type stype;
8227 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8228
8229 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8230 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8231 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8232 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8233
8234 for (unsigned i = 0; i < instr->num_srcs; i++) {
8235 switch (instr->src[i].src_type) {
8236 case nir_tex_src_coord: {
8237 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8238 for (unsigned i = 0; i < coord.size(); i++)
8239 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8240 break;
8241 }
8242 case nir_tex_src_bias:
8243 if (instr->op == nir_texop_txb) {
8244 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8245 has_bias = true;
8246 }
8247 break;
8248 case nir_tex_src_lod: {
8249 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8250
8251 if (val && val->f32 <= 0.0) {
8252 level_zero = true;
8253 } else {
8254 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8255 has_lod = true;
8256 }
8257 break;
8258 }
8259 case nir_tex_src_comparator:
8260 if (instr->is_shadow) {
8261 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8262 has_compare = true;
8263 }
8264 break;
8265 case nir_tex_src_offset:
8266 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8267 get_const_vec(instr->src[i].src.ssa, const_offset);
8268 has_offset = true;
8269 break;
8270 case nir_tex_src_ddx:
8271 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8272 has_ddx = true;
8273 break;
8274 case nir_tex_src_ddy:
8275 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8276 has_ddy = true;
8277 break;
8278 case nir_tex_src_ms_index:
8279 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8280 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8281 has_sample_index = true;
8282 break;
8283 case nir_tex_src_texture_offset:
8284 case nir_tex_src_sampler_offset:
8285 default:
8286 break;
8287 }
8288 }
8289
8290 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8291 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8292
8293 if (instr->op == nir_texop_texture_samples) {
8294 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8295
8296 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8297 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8298 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 */));
8299 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8300
8301 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8302 samples, Operand(1u), bld.scc(is_msaa));
8303 return;
8304 }
8305
8306 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8307 aco_ptr<Instruction> tmp_instr;
8308 Temp acc, pack = Temp();
8309
8310 uint32_t pack_const = 0;
8311 for (unsigned i = 0; i < offset.size(); i++) {
8312 if (!const_offset[i])
8313 continue;
8314 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8315 }
8316
8317 if (offset.type() == RegType::sgpr) {
8318 for (unsigned i = 0; i < offset.size(); i++) {
8319 if (const_offset[i])
8320 continue;
8321
8322 acc = emit_extract_vector(ctx, offset, i, s1);
8323 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8324
8325 if (i) {
8326 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8327 }
8328
8329 if (pack == Temp()) {
8330 pack = acc;
8331 } else {
8332 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8333 }
8334 }
8335
8336 if (pack_const && pack != Temp())
8337 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8338 } else {
8339 for (unsigned i = 0; i < offset.size(); i++) {
8340 if (const_offset[i])
8341 continue;
8342
8343 acc = emit_extract_vector(ctx, offset, i, v1);
8344 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8345
8346 if (i) {
8347 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8348 }
8349
8350 if (pack == Temp()) {
8351 pack = acc;
8352 } else {
8353 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8354 }
8355 }
8356
8357 if (pack_const && pack != Temp())
8358 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8359 }
8360 if (pack_const && pack == Temp())
8361 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8362 else if (pack == Temp())
8363 has_offset = false;
8364 else
8365 offset = pack;
8366 }
8367
8368 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8369 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8370
8371 /* pack derivatives */
8372 if (has_ddx || has_ddy) {
8373 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8374 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8375 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8376 derivs = {ddx, zero, ddy, zero};
8377 } else {
8378 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8379 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8380 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8381 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8382 }
8383 has_derivs = true;
8384 }
8385
8386 if (instr->coord_components > 1 &&
8387 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8388 instr->is_array &&
8389 instr->op != nir_texop_txf)
8390 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8391
8392 if (instr->coord_components > 2 &&
8393 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8394 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8395 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8396 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8397 instr->is_array &&
8398 instr->op != nir_texop_txf &&
8399 instr->op != nir_texop_txf_ms &&
8400 instr->op != nir_texop_fragment_fetch &&
8401 instr->op != nir_texop_fragment_mask_fetch)
8402 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8403
8404 if (ctx->options->chip_class == GFX9 &&
8405 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8406 instr->op != nir_texop_lod && instr->coord_components) {
8407 assert(coords.size() > 0 && coords.size() < 3);
8408
8409 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8410 Operand((uint32_t) 0) :
8411 Operand((uint32_t) 0x3f000000)));
8412 }
8413
8414 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8415
8416 if (instr->op == nir_texop_samples_identical)
8417 resource = fmask_ptr;
8418
8419 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8420 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8421 instr->op != nir_texop_txs &&
8422 instr->op != nir_texop_fragment_fetch &&
8423 instr->op != nir_texop_fragment_mask_fetch) {
8424 assert(has_sample_index);
8425 Operand op(sample_index);
8426 if (sample_index_cv)
8427 op = Operand(sample_index_cv->u32);
8428 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8429 }
8430
8431 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8432 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8433 Temp off = emit_extract_vector(ctx, offset, i, v1);
8434 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8435 }
8436 has_offset = false;
8437 }
8438
8439 /* Build tex instruction */
8440 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8441 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8442 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8443 : 0;
8444 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8445 Temp tmp_dst = dst;
8446
8447 /* gather4 selects the component by dmask and always returns vec4 */
8448 if (instr->op == nir_texop_tg4) {
8449 assert(instr->dest.ssa.num_components == 4);
8450 if (instr->is_shadow)
8451 dmask = 1;
8452 else
8453 dmask = 1 << instr->component;
8454 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8455 tmp_dst = bld.tmp(v4);
8456 } else if (instr->op == nir_texop_samples_identical) {
8457 tmp_dst = bld.tmp(v1);
8458 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8459 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8460 }
8461
8462 aco_ptr<MIMG_instruction> tex;
8463 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8464 if (!has_lod)
8465 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8466
8467 bool div_by_6 = instr->op == nir_texop_txs &&
8468 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8469 instr->is_array &&
8470 (dmask & (1 << 2));
8471 if (tmp_dst.id() == dst.id() && div_by_6)
8472 tmp_dst = bld.tmp(tmp_dst.regClass());
8473
8474 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8475 tex->operands[0] = Operand(resource);
8476 tex->operands[1] = Operand(s4); /* no sampler */
8477 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8478 if (ctx->options->chip_class == GFX9 &&
8479 instr->op == nir_texop_txs &&
8480 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8481 instr->is_array) {
8482 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8483 } else if (instr->op == nir_texop_query_levels) {
8484 tex->dmask = 1 << 3;
8485 } else {
8486 tex->dmask = dmask;
8487 }
8488 tex->da = da;
8489 tex->definitions[0] = Definition(tmp_dst);
8490 tex->dim = dim;
8491 tex->can_reorder = true;
8492 ctx->block->instructions.emplace_back(std::move(tex));
8493
8494 if (div_by_6) {
8495 /* divide 3rd value by 6 by multiplying with magic number */
8496 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8497 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8498 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8499 assert(instr->dest.ssa.num_components == 3);
8500 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8501 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8502 emit_extract_vector(ctx, tmp_dst, 0, v1),
8503 emit_extract_vector(ctx, tmp_dst, 1, v1),
8504 by_6);
8505
8506 }
8507
8508 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8509 return;
8510 }
8511
8512 Temp tg4_compare_cube_wa64 = Temp();
8513
8514 if (tg4_integer_workarounds) {
8515 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8516 tex->operands[0] = Operand(resource);
8517 tex->operands[1] = Operand(s4); /* no sampler */
8518 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8519 tex->dim = dim;
8520 tex->dmask = 0x3;
8521 tex->da = da;
8522 Temp size = bld.tmp(v2);
8523 tex->definitions[0] = Definition(size);
8524 tex->can_reorder = true;
8525 ctx->block->instructions.emplace_back(std::move(tex));
8526 emit_split_vector(ctx, size, size.size());
8527
8528 Temp half_texel[2];
8529 for (unsigned i = 0; i < 2; i++) {
8530 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8531 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8532 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8533 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8534 }
8535
8536 Temp new_coords[2] = {
8537 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8538 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8539 };
8540
8541 if (tg4_integer_cube_workaround) {
8542 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8543 Temp desc[resource.size()];
8544 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8545 Format::PSEUDO, 1, resource.size())};
8546 split->operands[0] = Operand(resource);
8547 for (unsigned i = 0; i < resource.size(); i++) {
8548 desc[i] = bld.tmp(s1);
8549 split->definitions[i] = Definition(desc[i]);
8550 }
8551 ctx->block->instructions.emplace_back(std::move(split));
8552
8553 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8554 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8555 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8556
8557 Temp nfmt;
8558 if (stype == GLSL_TYPE_UINT) {
8559 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8560 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8561 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8562 bld.scc(compare_cube_wa));
8563 } else {
8564 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8565 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8566 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8567 bld.scc(compare_cube_wa));
8568 }
8569 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8570 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8571
8572 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8573
8574 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8575 Operand((uint32_t)C_008F14_NUM_FORMAT));
8576 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8577
8578 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8579 Format::PSEUDO, resource.size(), 1)};
8580 for (unsigned i = 0; i < resource.size(); i++)
8581 vec->operands[i] = Operand(desc[i]);
8582 resource = bld.tmp(resource.regClass());
8583 vec->definitions[0] = Definition(resource);
8584 ctx->block->instructions.emplace_back(std::move(vec));
8585
8586 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8587 new_coords[0], coords[0], tg4_compare_cube_wa64);
8588 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8589 new_coords[1], coords[1], tg4_compare_cube_wa64);
8590 }
8591 coords[0] = new_coords[0];
8592 coords[1] = new_coords[1];
8593 }
8594
8595 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8596 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8597
8598 assert(coords.size() == 1);
8599 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8600 aco_opcode op;
8601 switch (last_bit) {
8602 case 1:
8603 op = aco_opcode::buffer_load_format_x; break;
8604 case 2:
8605 op = aco_opcode::buffer_load_format_xy; break;
8606 case 3:
8607 op = aco_opcode::buffer_load_format_xyz; break;
8608 case 4:
8609 op = aco_opcode::buffer_load_format_xyzw; break;
8610 default:
8611 unreachable("Tex instruction loads more than 4 components.");
8612 }
8613
8614 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8615 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8616 tmp_dst = dst;
8617 else
8618 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8619
8620 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8621 mubuf->operands[0] = Operand(resource);
8622 mubuf->operands[1] = Operand(coords[0]);
8623 mubuf->operands[2] = Operand((uint32_t) 0);
8624 mubuf->definitions[0] = Definition(tmp_dst);
8625 mubuf->idxen = true;
8626 mubuf->can_reorder = true;
8627 ctx->block->instructions.emplace_back(std::move(mubuf));
8628
8629 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8630 return;
8631 }
8632
8633 /* gather MIMG address components */
8634 std::vector<Temp> args;
8635 if (has_offset)
8636 args.emplace_back(offset);
8637 if (has_bias)
8638 args.emplace_back(bias);
8639 if (has_compare)
8640 args.emplace_back(compare);
8641 if (has_derivs)
8642 args.insert(args.end(), derivs.begin(), derivs.end());
8643
8644 args.insert(args.end(), coords.begin(), coords.end());
8645 if (has_sample_index)
8646 args.emplace_back(sample_index);
8647 if (has_lod)
8648 args.emplace_back(lod);
8649
8650 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8651 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8652 vec->definitions[0] = Definition(arg);
8653 for (unsigned i = 0; i < args.size(); i++)
8654 vec->operands[i] = Operand(args[i]);
8655 ctx->block->instructions.emplace_back(std::move(vec));
8656
8657
8658 if (instr->op == nir_texop_txf ||
8659 instr->op == nir_texop_txf_ms ||
8660 instr->op == nir_texop_samples_identical ||
8661 instr->op == nir_texop_fragment_fetch ||
8662 instr->op == nir_texop_fragment_mask_fetch) {
8663 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;
8664 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8665 tex->operands[0] = Operand(resource);
8666 tex->operands[1] = Operand(s4); /* no sampler */
8667 tex->operands[2] = Operand(arg);
8668 tex->dim = dim;
8669 tex->dmask = dmask;
8670 tex->unrm = true;
8671 tex->da = da;
8672 tex->definitions[0] = Definition(tmp_dst);
8673 tex->can_reorder = true;
8674 ctx->block->instructions.emplace_back(std::move(tex));
8675
8676 if (instr->op == nir_texop_samples_identical) {
8677 assert(dmask == 1 && dst.regClass() == v1);
8678 assert(dst.id() != tmp_dst.id());
8679
8680 Temp tmp = bld.tmp(bld.lm);
8681 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8682 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8683
8684 } else {
8685 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8686 }
8687 return;
8688 }
8689
8690 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8691 aco_opcode opcode = aco_opcode::image_sample;
8692 if (has_offset) { /* image_sample_*_o */
8693 if (has_compare) {
8694 opcode = aco_opcode::image_sample_c_o;
8695 if (has_derivs)
8696 opcode = aco_opcode::image_sample_c_d_o;
8697 if (has_bias)
8698 opcode = aco_opcode::image_sample_c_b_o;
8699 if (level_zero)
8700 opcode = aco_opcode::image_sample_c_lz_o;
8701 if (has_lod)
8702 opcode = aco_opcode::image_sample_c_l_o;
8703 } else {
8704 opcode = aco_opcode::image_sample_o;
8705 if (has_derivs)
8706 opcode = aco_opcode::image_sample_d_o;
8707 if (has_bias)
8708 opcode = aco_opcode::image_sample_b_o;
8709 if (level_zero)
8710 opcode = aco_opcode::image_sample_lz_o;
8711 if (has_lod)
8712 opcode = aco_opcode::image_sample_l_o;
8713 }
8714 } else { /* no offset */
8715 if (has_compare) {
8716 opcode = aco_opcode::image_sample_c;
8717 if (has_derivs)
8718 opcode = aco_opcode::image_sample_c_d;
8719 if (has_bias)
8720 opcode = aco_opcode::image_sample_c_b;
8721 if (level_zero)
8722 opcode = aco_opcode::image_sample_c_lz;
8723 if (has_lod)
8724 opcode = aco_opcode::image_sample_c_l;
8725 } else {
8726 opcode = aco_opcode::image_sample;
8727 if (has_derivs)
8728 opcode = aco_opcode::image_sample_d;
8729 if (has_bias)
8730 opcode = aco_opcode::image_sample_b;
8731 if (level_zero)
8732 opcode = aco_opcode::image_sample_lz;
8733 if (has_lod)
8734 opcode = aco_opcode::image_sample_l;
8735 }
8736 }
8737
8738 if (instr->op == nir_texop_tg4) {
8739 if (has_offset) {
8740 opcode = aco_opcode::image_gather4_lz_o;
8741 if (has_compare)
8742 opcode = aco_opcode::image_gather4_c_lz_o;
8743 } else {
8744 opcode = aco_opcode::image_gather4_lz;
8745 if (has_compare)
8746 opcode = aco_opcode::image_gather4_c_lz;
8747 }
8748 } else if (instr->op == nir_texop_lod) {
8749 opcode = aco_opcode::image_get_lod;
8750 }
8751
8752 /* we don't need the bias, sample index, compare value or offset to be
8753 * computed in WQM but if the p_create_vector copies the coordinates, then it
8754 * needs to be in WQM */
8755 if (ctx->stage == fragment_fs &&
8756 !has_derivs && !has_lod && !level_zero &&
8757 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8758 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8759 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8760
8761 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8762 tex->operands[0] = Operand(resource);
8763 tex->operands[1] = Operand(sampler);
8764 tex->operands[2] = Operand(arg);
8765 tex->dim = dim;
8766 tex->dmask = dmask;
8767 tex->da = da;
8768 tex->definitions[0] = Definition(tmp_dst);
8769 tex->can_reorder = true;
8770 ctx->block->instructions.emplace_back(std::move(tex));
8771
8772 if (tg4_integer_cube_workaround) {
8773 assert(tmp_dst.id() != dst.id());
8774 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8775
8776 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8777 Temp val[4];
8778 for (unsigned i = 0; i < dst.size(); i++) {
8779 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8780 Temp cvt_val;
8781 if (stype == GLSL_TYPE_UINT)
8782 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8783 else
8784 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8785 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8786 }
8787 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8788 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8789 val[0], val[1], val[2], val[3]);
8790 }
8791 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8792 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8793
8794 }
8795
8796
8797 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8798 {
8799 Temp tmp = get_ssa_temp(ctx, ssa);
8800 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8801 return Operand(tmp.regClass());
8802 else
8803 return Operand(tmp);
8804 }
8805
8806 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8807 {
8808 aco_ptr<Pseudo_instruction> phi;
8809 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8810 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8811
8812 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8813 logical |= ctx->block->kind & block_kind_merge;
8814 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8815
8816 /* we want a sorted list of sources, since the predecessor list is also sorted */
8817 std::map<unsigned, nir_ssa_def*> phi_src;
8818 nir_foreach_phi_src(src, instr)
8819 phi_src[src->pred->index] = src->src.ssa;
8820
8821 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8822 unsigned num_operands = 0;
8823 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8824 unsigned num_defined = 0;
8825 unsigned cur_pred_idx = 0;
8826 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8827 if (cur_pred_idx < preds.size()) {
8828 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8829 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8830 unsigned skipped = 0;
8831 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8832 skipped++;
8833 if (cur_pred_idx + skipped < preds.size()) {
8834 for (unsigned i = 0; i < skipped; i++)
8835 operands[num_operands++] = Operand(dst.regClass());
8836 cur_pred_idx += skipped;
8837 } else {
8838 continue;
8839 }
8840 }
8841 /* Handle missing predecessors at the end. This shouldn't happen with loop
8842 * headers and we can't ignore these sources for loop header phis. */
8843 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8844 continue;
8845 cur_pred_idx++;
8846 Operand op = get_phi_operand(ctx, src.second);
8847 operands[num_operands++] = op;
8848 num_defined += !op.isUndefined();
8849 }
8850 /* handle block_kind_continue_or_break at loop exit blocks */
8851 while (cur_pred_idx++ < preds.size())
8852 operands[num_operands++] = Operand(dst.regClass());
8853
8854 /* If the loop ends with a break, still add a linear continue edge in case
8855 * that break is divergent or continue_or_break is used. We'll either remove
8856 * this operand later in visit_loop() if it's not necessary or replace the
8857 * undef with something correct. */
8858 if (!logical && ctx->block->kind & block_kind_loop_header) {
8859 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8860 nir_block *last = nir_loop_last_block(loop);
8861 if (last->successors[0] != instr->instr.block)
8862 operands[num_operands++] = Operand(RegClass());
8863 }
8864
8865 if (num_defined == 0) {
8866 Builder bld(ctx->program, ctx->block);
8867 if (dst.regClass() == s1) {
8868 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8869 } else if (dst.regClass() == v1) {
8870 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8871 } else {
8872 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8873 for (unsigned i = 0; i < dst.size(); i++)
8874 vec->operands[i] = Operand(0u);
8875 vec->definitions[0] = Definition(dst);
8876 ctx->block->instructions.emplace_back(std::move(vec));
8877 }
8878 return;
8879 }
8880
8881 /* we can use a linear phi in some cases if one src is undef */
8882 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8883 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8884
8885 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8886 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8887 assert(invert->kind & block_kind_invert);
8888
8889 unsigned then_block = invert->linear_preds[0];
8890
8891 Block* insert_block = NULL;
8892 for (unsigned i = 0; i < num_operands; i++) {
8893 Operand op = operands[i];
8894 if (op.isUndefined())
8895 continue;
8896 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8897 phi->operands[0] = op;
8898 break;
8899 }
8900 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8901 phi->operands[1] = Operand(dst.regClass());
8902 phi->definitions[0] = Definition(dst);
8903 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8904 return;
8905 }
8906
8907 /* try to scalarize vector phis */
8908 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8909 // TODO: scalarize linear phis on divergent ifs
8910 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8911 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8912 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8913 Operand src = operands[i];
8914 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8915 can_scalarize = false;
8916 }
8917 if (can_scalarize) {
8918 unsigned num_components = instr->dest.ssa.num_components;
8919 assert(dst.size() % num_components == 0);
8920 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8921
8922 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8923 for (unsigned k = 0; k < num_components; k++) {
8924 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8925 for (unsigned i = 0; i < num_operands; i++) {
8926 Operand src = operands[i];
8927 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8928 }
8929 Temp phi_dst = {ctx->program->allocateId(), rc};
8930 phi->definitions[0] = Definition(phi_dst);
8931 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8932 new_vec[k] = phi_dst;
8933 vec->operands[k] = Operand(phi_dst);
8934 }
8935 vec->definitions[0] = Definition(dst);
8936 ctx->block->instructions.emplace_back(std::move(vec));
8937 ctx->allocated_vec.emplace(dst.id(), new_vec);
8938 return;
8939 }
8940 }
8941
8942 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8943 for (unsigned i = 0; i < num_operands; i++)
8944 phi->operands[i] = operands[i];
8945 phi->definitions[0] = Definition(dst);
8946 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8947 }
8948
8949
8950 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8951 {
8952 Temp dst = get_ssa_temp(ctx, &instr->def);
8953
8954 assert(dst.type() == RegType::sgpr);
8955
8956 if (dst.size() == 1) {
8957 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8958 } else {
8959 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8960 for (unsigned i = 0; i < dst.size(); i++)
8961 vec->operands[i] = Operand(0u);
8962 vec->definitions[0] = Definition(dst);
8963 ctx->block->instructions.emplace_back(std::move(vec));
8964 }
8965 }
8966
8967 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8968 {
8969 Builder bld(ctx->program, ctx->block);
8970 Block *logical_target;
8971 append_logical_end(ctx->block);
8972 unsigned idx = ctx->block->index;
8973
8974 switch (instr->type) {
8975 case nir_jump_break:
8976 logical_target = ctx->cf_info.parent_loop.exit;
8977 add_logical_edge(idx, logical_target);
8978 ctx->block->kind |= block_kind_break;
8979
8980 if (!ctx->cf_info.parent_if.is_divergent &&
8981 !ctx->cf_info.parent_loop.has_divergent_continue) {
8982 /* uniform break - directly jump out of the loop */
8983 ctx->block->kind |= block_kind_uniform;
8984 ctx->cf_info.has_branch = true;
8985 bld.branch(aco_opcode::p_branch);
8986 add_linear_edge(idx, logical_target);
8987 return;
8988 }
8989 ctx->cf_info.parent_loop.has_divergent_branch = true;
8990 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8991 break;
8992 case nir_jump_continue:
8993 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8994 add_logical_edge(idx, logical_target);
8995 ctx->block->kind |= block_kind_continue;
8996
8997 if (ctx->cf_info.parent_if.is_divergent) {
8998 /* for potential uniform breaks after this continue,
8999 we must ensure that they are handled correctly */
9000 ctx->cf_info.parent_loop.has_divergent_continue = true;
9001 ctx->cf_info.parent_loop.has_divergent_branch = true;
9002 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9003 } else {
9004 /* uniform continue - directly jump to the loop header */
9005 ctx->block->kind |= block_kind_uniform;
9006 ctx->cf_info.has_branch = true;
9007 bld.branch(aco_opcode::p_branch);
9008 add_linear_edge(idx, logical_target);
9009 return;
9010 }
9011 break;
9012 default:
9013 fprintf(stderr, "Unknown NIR jump instr: ");
9014 nir_print_instr(&instr->instr, stderr);
9015 fprintf(stderr, "\n");
9016 abort();
9017 }
9018
9019 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9020 ctx->cf_info.exec_potentially_empty_break = true;
9021 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9022 }
9023
9024 /* remove critical edges from linear CFG */
9025 bld.branch(aco_opcode::p_branch);
9026 Block* break_block = ctx->program->create_and_insert_block();
9027 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9028 break_block->kind |= block_kind_uniform;
9029 add_linear_edge(idx, break_block);
9030 /* the loop_header pointer might be invalidated by this point */
9031 if (instr->type == nir_jump_continue)
9032 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9033 add_linear_edge(break_block->index, logical_target);
9034 bld.reset(break_block);
9035 bld.branch(aco_opcode::p_branch);
9036
9037 Block* continue_block = ctx->program->create_and_insert_block();
9038 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9039 add_linear_edge(idx, continue_block);
9040 append_logical_start(continue_block);
9041 ctx->block = continue_block;
9042 return;
9043 }
9044
9045 void visit_block(isel_context *ctx, nir_block *block)
9046 {
9047 nir_foreach_instr(instr, block) {
9048 switch (instr->type) {
9049 case nir_instr_type_alu:
9050 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9051 break;
9052 case nir_instr_type_load_const:
9053 visit_load_const(ctx, nir_instr_as_load_const(instr));
9054 break;
9055 case nir_instr_type_intrinsic:
9056 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9057 break;
9058 case nir_instr_type_tex:
9059 visit_tex(ctx, nir_instr_as_tex(instr));
9060 break;
9061 case nir_instr_type_phi:
9062 visit_phi(ctx, nir_instr_as_phi(instr));
9063 break;
9064 case nir_instr_type_ssa_undef:
9065 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9066 break;
9067 case nir_instr_type_deref:
9068 break;
9069 case nir_instr_type_jump:
9070 visit_jump(ctx, nir_instr_as_jump(instr));
9071 break;
9072 default:
9073 fprintf(stderr, "Unknown NIR instr type: ");
9074 nir_print_instr(instr, stderr);
9075 fprintf(stderr, "\n");
9076 //abort();
9077 }
9078 }
9079
9080 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9081 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9082 }
9083
9084
9085
9086 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9087 aco_ptr<Instruction>& header_phi, Operand *vals)
9088 {
9089 vals[0] = Operand(header_phi->definitions[0].getTemp());
9090 RegClass rc = vals[0].regClass();
9091
9092 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9093
9094 unsigned next_pred = 1;
9095
9096 for (unsigned idx = first + 1; idx <= last; idx++) {
9097 Block& block = ctx->program->blocks[idx];
9098 if (block.loop_nest_depth != loop_nest_depth) {
9099 vals[idx - first] = vals[idx - 1 - first];
9100 continue;
9101 }
9102
9103 if (block.kind & block_kind_continue) {
9104 vals[idx - first] = header_phi->operands[next_pred];
9105 next_pred++;
9106 continue;
9107 }
9108
9109 bool all_same = true;
9110 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9111 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9112
9113 Operand val;
9114 if (all_same) {
9115 val = vals[block.linear_preds[0] - first];
9116 } else {
9117 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9118 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9119 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9120 phi->operands[i] = vals[block.linear_preds[i] - first];
9121 val = Operand(Temp(ctx->program->allocateId(), rc));
9122 phi->definitions[0] = Definition(val.getTemp());
9123 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9124 }
9125 vals[idx - first] = val;
9126 }
9127
9128 return vals[last - first];
9129 }
9130
9131 static void visit_loop(isel_context *ctx, nir_loop *loop)
9132 {
9133 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9134 append_logical_end(ctx->block);
9135 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9136 Builder bld(ctx->program, ctx->block);
9137 bld.branch(aco_opcode::p_branch);
9138 unsigned loop_preheader_idx = ctx->block->index;
9139
9140 Block loop_exit = Block();
9141 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9142 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9143
9144 Block* loop_header = ctx->program->create_and_insert_block();
9145 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9146 loop_header->kind |= block_kind_loop_header;
9147 add_edge(loop_preheader_idx, loop_header);
9148 ctx->block = loop_header;
9149
9150 /* emit loop body */
9151 unsigned loop_header_idx = loop_header->index;
9152 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9153 append_logical_start(ctx->block);
9154 bool unreachable = visit_cf_list(ctx, &loop->body);
9155
9156 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9157 if (!ctx->cf_info.has_branch) {
9158 append_logical_end(ctx->block);
9159 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9160 /* Discards can result in code running with an empty exec mask.
9161 * This would result in divergent breaks not ever being taken. As a
9162 * workaround, break the loop when the loop mask is empty instead of
9163 * always continuing. */
9164 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9165 unsigned block_idx = ctx->block->index;
9166
9167 /* create helper blocks to avoid critical edges */
9168 Block *break_block = ctx->program->create_and_insert_block();
9169 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9170 break_block->kind = block_kind_uniform;
9171 bld.reset(break_block);
9172 bld.branch(aco_opcode::p_branch);
9173 add_linear_edge(block_idx, break_block);
9174 add_linear_edge(break_block->index, &loop_exit);
9175
9176 Block *continue_block = ctx->program->create_and_insert_block();
9177 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9178 continue_block->kind = block_kind_uniform;
9179 bld.reset(continue_block);
9180 bld.branch(aco_opcode::p_branch);
9181 add_linear_edge(block_idx, continue_block);
9182 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9183
9184 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9185 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9186 ctx->block = &ctx->program->blocks[block_idx];
9187 } else {
9188 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9189 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9190 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9191 else
9192 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9193 }
9194
9195 bld.reset(ctx->block);
9196 bld.branch(aco_opcode::p_branch);
9197 }
9198
9199 /* Fixup phis in loop header from unreachable blocks.
9200 * has_branch/has_divergent_branch also indicates if the loop ends with a
9201 * break/continue instruction, but we don't emit those if unreachable=true */
9202 if (unreachable) {
9203 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9204 bool linear = ctx->cf_info.has_branch;
9205 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9206 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9207 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9208 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9209 /* the last operand should be the one that needs to be removed */
9210 instr->operands.pop_back();
9211 } else if (!is_phi(instr)) {
9212 break;
9213 }
9214 }
9215 }
9216
9217 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9218 * and the previous one shouldn't both happen at once because a break in the
9219 * merge block would get CSE'd */
9220 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9221 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9222 Operand vals[num_vals];
9223 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9224 if (instr->opcode == aco_opcode::p_linear_phi) {
9225 if (ctx->cf_info.has_branch)
9226 instr->operands.pop_back();
9227 else
9228 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9229 } else if (!is_phi(instr)) {
9230 break;
9231 }
9232 }
9233 }
9234
9235 ctx->cf_info.has_branch = false;
9236
9237 // TODO: if the loop has not a single exit, we must add one °°
9238 /* emit loop successor block */
9239 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9240 append_logical_start(ctx->block);
9241
9242 #if 0
9243 // TODO: check if it is beneficial to not branch on continues
9244 /* trim linear phis in loop header */
9245 for (auto&& instr : loop_entry->instructions) {
9246 if (instr->opcode == aco_opcode::p_linear_phi) {
9247 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9248 new_phi->definitions[0] = instr->definitions[0];
9249 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9250 new_phi->operands[i] = instr->operands[i];
9251 /* check that the remaining operands are all the same */
9252 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9253 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9254 instr.swap(new_phi);
9255 } else if (instr->opcode == aco_opcode::p_phi) {
9256 continue;
9257 } else {
9258 break;
9259 }
9260 }
9261 #endif
9262 }
9263
9264 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9265 {
9266 ic->cond = cond;
9267
9268 append_logical_end(ctx->block);
9269 ctx->block->kind |= block_kind_branch;
9270
9271 /* branch to linear then block */
9272 assert(cond.regClass() == ctx->program->lane_mask);
9273 aco_ptr<Pseudo_branch_instruction> branch;
9274 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9275 branch->operands[0] = Operand(cond);
9276 ctx->block->instructions.push_back(std::move(branch));
9277
9278 ic->BB_if_idx = ctx->block->index;
9279 ic->BB_invert = Block();
9280 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9281 /* Invert blocks are intentionally not marked as top level because they
9282 * are not part of the logical cfg. */
9283 ic->BB_invert.kind |= block_kind_invert;
9284 ic->BB_endif = Block();
9285 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9286 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9287
9288 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9289 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9290 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9291 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9292 ctx->cf_info.parent_if.is_divergent = true;
9293
9294 /* divergent branches use cbranch_execz */
9295 ctx->cf_info.exec_potentially_empty_discard = false;
9296 ctx->cf_info.exec_potentially_empty_break = false;
9297 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9298
9299 /** emit logical then block */
9300 Block* BB_then_logical = ctx->program->create_and_insert_block();
9301 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9302 add_edge(ic->BB_if_idx, BB_then_logical);
9303 ctx->block = BB_then_logical;
9304 append_logical_start(BB_then_logical);
9305 }
9306
9307 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9308 {
9309 Block *BB_then_logical = ctx->block;
9310 append_logical_end(BB_then_logical);
9311 /* branch from logical then block to invert block */
9312 aco_ptr<Pseudo_branch_instruction> branch;
9313 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9314 BB_then_logical->instructions.emplace_back(std::move(branch));
9315 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9316 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9317 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9318 BB_then_logical->kind |= block_kind_uniform;
9319 assert(!ctx->cf_info.has_branch);
9320 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9321 ctx->cf_info.parent_loop.has_divergent_branch = false;
9322
9323 /** emit linear then block */
9324 Block* BB_then_linear = ctx->program->create_and_insert_block();
9325 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9326 BB_then_linear->kind |= block_kind_uniform;
9327 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9328 /* branch from linear then block to invert block */
9329 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9330 BB_then_linear->instructions.emplace_back(std::move(branch));
9331 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9332
9333 /** emit invert merge block */
9334 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9335 ic->invert_idx = ctx->block->index;
9336
9337 /* branch to linear else block (skip else) */
9338 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9339 branch->operands[0] = Operand(ic->cond);
9340 ctx->block->instructions.push_back(std::move(branch));
9341
9342 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9343 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9344 ic->exec_potentially_empty_break_depth_old =
9345 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9346 /* divergent branches use cbranch_execz */
9347 ctx->cf_info.exec_potentially_empty_discard = false;
9348 ctx->cf_info.exec_potentially_empty_break = false;
9349 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9350
9351 /** emit logical else block */
9352 Block* BB_else_logical = ctx->program->create_and_insert_block();
9353 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9354 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9355 add_linear_edge(ic->invert_idx, BB_else_logical);
9356 ctx->block = BB_else_logical;
9357 append_logical_start(BB_else_logical);
9358 }
9359
9360 static void end_divergent_if(isel_context *ctx, if_context *ic)
9361 {
9362 Block *BB_else_logical = ctx->block;
9363 append_logical_end(BB_else_logical);
9364
9365 /* branch from logical else block to endif block */
9366 aco_ptr<Pseudo_branch_instruction> branch;
9367 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9368 BB_else_logical->instructions.emplace_back(std::move(branch));
9369 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9370 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9371 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9372 BB_else_logical->kind |= block_kind_uniform;
9373
9374 assert(!ctx->cf_info.has_branch);
9375 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9376
9377
9378 /** emit linear else block */
9379 Block* BB_else_linear = ctx->program->create_and_insert_block();
9380 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9381 BB_else_linear->kind |= block_kind_uniform;
9382 add_linear_edge(ic->invert_idx, BB_else_linear);
9383
9384 /* branch from linear else block to endif block */
9385 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9386 BB_else_linear->instructions.emplace_back(std::move(branch));
9387 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9388
9389
9390 /** emit endif merge block */
9391 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9392 append_logical_start(ctx->block);
9393
9394
9395 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9396 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9397 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9398 ctx->cf_info.exec_potentially_empty_break_depth =
9399 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9400 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9401 !ctx->cf_info.parent_if.is_divergent) {
9402 ctx->cf_info.exec_potentially_empty_break = false;
9403 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9404 }
9405 /* uniform control flow never has an empty exec-mask */
9406 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9407 ctx->cf_info.exec_potentially_empty_discard = false;
9408 ctx->cf_info.exec_potentially_empty_break = false;
9409 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9410 }
9411 }
9412
9413 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9414 {
9415 assert(cond.regClass() == s1);
9416
9417 append_logical_end(ctx->block);
9418 ctx->block->kind |= block_kind_uniform;
9419
9420 aco_ptr<Pseudo_branch_instruction> branch;
9421 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9422 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9423 branch->operands[0] = Operand(cond);
9424 branch->operands[0].setFixed(scc);
9425 ctx->block->instructions.emplace_back(std::move(branch));
9426
9427 ic->BB_if_idx = ctx->block->index;
9428 ic->BB_endif = Block();
9429 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9430 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9431
9432 ctx->cf_info.has_branch = false;
9433 ctx->cf_info.parent_loop.has_divergent_branch = false;
9434
9435 /** emit then block */
9436 Block* BB_then = ctx->program->create_and_insert_block();
9437 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9438 add_edge(ic->BB_if_idx, BB_then);
9439 append_logical_start(BB_then);
9440 ctx->block = BB_then;
9441 }
9442
9443 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9444 {
9445 Block *BB_then = ctx->block;
9446
9447 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9448 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9449
9450 if (!ic->uniform_has_then_branch) {
9451 append_logical_end(BB_then);
9452 /* branch from then block to endif block */
9453 aco_ptr<Pseudo_branch_instruction> branch;
9454 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9455 BB_then->instructions.emplace_back(std::move(branch));
9456 add_linear_edge(BB_then->index, &ic->BB_endif);
9457 if (!ic->then_branch_divergent)
9458 add_logical_edge(BB_then->index, &ic->BB_endif);
9459 BB_then->kind |= block_kind_uniform;
9460 }
9461
9462 ctx->cf_info.has_branch = false;
9463 ctx->cf_info.parent_loop.has_divergent_branch = false;
9464
9465 /** emit else block */
9466 Block* BB_else = ctx->program->create_and_insert_block();
9467 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9468 add_edge(ic->BB_if_idx, BB_else);
9469 append_logical_start(BB_else);
9470 ctx->block = BB_else;
9471 }
9472
9473 static void end_uniform_if(isel_context *ctx, if_context *ic)
9474 {
9475 Block *BB_else = ctx->block;
9476
9477 if (!ctx->cf_info.has_branch) {
9478 append_logical_end(BB_else);
9479 /* branch from then block to endif block */
9480 aco_ptr<Pseudo_branch_instruction> branch;
9481 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9482 BB_else->instructions.emplace_back(std::move(branch));
9483 add_linear_edge(BB_else->index, &ic->BB_endif);
9484 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9485 add_logical_edge(BB_else->index, &ic->BB_endif);
9486 BB_else->kind |= block_kind_uniform;
9487 }
9488
9489 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9490 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9491
9492 /** emit endif merge block */
9493 if (!ctx->cf_info.has_branch) {
9494 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9495 append_logical_start(ctx->block);
9496 }
9497 }
9498
9499 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9500 {
9501 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9502 Builder bld(ctx->program, ctx->block);
9503 aco_ptr<Pseudo_branch_instruction> branch;
9504 if_context ic;
9505
9506 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9507 /**
9508 * Uniform conditionals are represented in the following way*) :
9509 *
9510 * The linear and logical CFG:
9511 * BB_IF
9512 * / \
9513 * BB_THEN (logical) BB_ELSE (logical)
9514 * \ /
9515 * BB_ENDIF
9516 *
9517 * *) Exceptions may be due to break and continue statements within loops
9518 * If a break/continue happens within uniform control flow, it branches
9519 * to the loop exit/entry block. Otherwise, it branches to the next
9520 * merge block.
9521 **/
9522
9523 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9524 assert(cond.regClass() == ctx->program->lane_mask);
9525 cond = bool_to_scalar_condition(ctx, cond);
9526
9527 begin_uniform_if_then(ctx, &ic, cond);
9528 visit_cf_list(ctx, &if_stmt->then_list);
9529
9530 begin_uniform_if_else(ctx, &ic);
9531 visit_cf_list(ctx, &if_stmt->else_list);
9532
9533 end_uniform_if(ctx, &ic);
9534
9535 return !ctx->cf_info.has_branch;
9536 } else { /* non-uniform condition */
9537 /**
9538 * To maintain a logical and linear CFG without critical edges,
9539 * non-uniform conditionals are represented in the following way*) :
9540 *
9541 * The linear CFG:
9542 * BB_IF
9543 * / \
9544 * BB_THEN (logical) BB_THEN (linear)
9545 * \ /
9546 * BB_INVERT (linear)
9547 * / \
9548 * BB_ELSE (logical) BB_ELSE (linear)
9549 * \ /
9550 * BB_ENDIF
9551 *
9552 * The logical CFG:
9553 * BB_IF
9554 * / \
9555 * BB_THEN (logical) BB_ELSE (logical)
9556 * \ /
9557 * BB_ENDIF
9558 *
9559 * *) Exceptions may be due to break and continue statements within loops
9560 **/
9561
9562 begin_divergent_if_then(ctx, &ic, cond);
9563 visit_cf_list(ctx, &if_stmt->then_list);
9564
9565 begin_divergent_if_else(ctx, &ic);
9566 visit_cf_list(ctx, &if_stmt->else_list);
9567
9568 end_divergent_if(ctx, &ic);
9569
9570 return true;
9571 }
9572 }
9573
9574 static bool visit_cf_list(isel_context *ctx,
9575 struct exec_list *list)
9576 {
9577 foreach_list_typed(nir_cf_node, node, node, list) {
9578 switch (node->type) {
9579 case nir_cf_node_block:
9580 visit_block(ctx, nir_cf_node_as_block(node));
9581 break;
9582 case nir_cf_node_if:
9583 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9584 return true;
9585 break;
9586 case nir_cf_node_loop:
9587 visit_loop(ctx, nir_cf_node_as_loop(node));
9588 break;
9589 default:
9590 unreachable("unimplemented cf list type");
9591 }
9592 }
9593 return false;
9594 }
9595
9596 static void create_null_export(isel_context *ctx)
9597 {
9598 /* Some shader stages always need to have exports.
9599 * So when there is none, we need to add a null export.
9600 */
9601
9602 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9603 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9604 Builder bld(ctx->program, ctx->block);
9605 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9606 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9607 }
9608
9609 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9610 {
9611 assert(ctx->stage == vertex_vs ||
9612 ctx->stage == tess_eval_vs ||
9613 ctx->stage == gs_copy_vs ||
9614 ctx->stage == ngg_vertex_gs ||
9615 ctx->stage == ngg_tess_eval_gs);
9616
9617 int offset = (ctx->stage & sw_tes)
9618 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9619 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9620 uint64_t mask = ctx->outputs.mask[slot];
9621 if (!is_pos && !mask)
9622 return false;
9623 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9624 return false;
9625 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9626 exp->enabled_mask = mask;
9627 for (unsigned i = 0; i < 4; ++i) {
9628 if (mask & (1 << i))
9629 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9630 else
9631 exp->operands[i] = Operand(v1);
9632 }
9633 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9634 * Setting valid_mask=1 prevents it and has no other effect.
9635 */
9636 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9637 exp->done = false;
9638 exp->compressed = false;
9639 if (is_pos)
9640 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9641 else
9642 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9643 ctx->block->instructions.emplace_back(std::move(exp));
9644
9645 return true;
9646 }
9647
9648 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9649 {
9650 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9651 exp->enabled_mask = 0;
9652 for (unsigned i = 0; i < 4; ++i)
9653 exp->operands[i] = Operand(v1);
9654 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9655 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9656 exp->enabled_mask |= 0x1;
9657 }
9658 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9659 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9660 exp->enabled_mask |= 0x4;
9661 }
9662 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9663 if (ctx->options->chip_class < GFX9) {
9664 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9665 exp->enabled_mask |= 0x8;
9666 } else {
9667 Builder bld(ctx->program, ctx->block);
9668
9669 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9670 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9671 if (exp->operands[2].isTemp())
9672 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9673
9674 exp->operands[2] = Operand(out);
9675 exp->enabled_mask |= 0x4;
9676 }
9677 }
9678 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9679 exp->done = false;
9680 exp->compressed = false;
9681 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9682 ctx->block->instructions.emplace_back(std::move(exp));
9683 }
9684
9685 static void create_export_phis(isel_context *ctx)
9686 {
9687 /* Used when exports are needed, but the output temps are defined in a preceding block.
9688 * This function will set up phis in order to access the outputs in the next block.
9689 */
9690
9691 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9692 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9693 ctx->block->instructions.pop_back();
9694
9695 Builder bld(ctx->program, ctx->block);
9696
9697 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9698 uint64_t mask = ctx->outputs.mask[slot];
9699 for (unsigned i = 0; i < 4; ++i) {
9700 if (!(mask & (1 << i)))
9701 continue;
9702
9703 Temp old = ctx->outputs.temps[slot * 4 + i];
9704 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9705 ctx->outputs.temps[slot * 4 + i] = phi;
9706 }
9707 }
9708
9709 bld.insert(std::move(logical_start));
9710 }
9711
9712 static void create_vs_exports(isel_context *ctx)
9713 {
9714 assert(ctx->stage == vertex_vs ||
9715 ctx->stage == tess_eval_vs ||
9716 ctx->stage == gs_copy_vs ||
9717 ctx->stage == ngg_vertex_gs ||
9718 ctx->stage == ngg_tess_eval_gs);
9719
9720 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9721 ? &ctx->program->info->tes.outinfo
9722 : &ctx->program->info->vs.outinfo;
9723
9724 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9725 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9726 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9727 }
9728
9729 if (ctx->options->key.has_multiview_view_index) {
9730 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9731 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9732 }
9733
9734 /* the order these position exports are created is important */
9735 int next_pos = 0;
9736 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9737 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9738 export_vs_psiz_layer_viewport(ctx, &next_pos);
9739 exported_pos = true;
9740 }
9741 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9742 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9743 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9744 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9745
9746 if (ctx->export_clip_dists) {
9747 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9748 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9749 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9750 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9751 }
9752
9753 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9754 if (i < VARYING_SLOT_VAR0 &&
9755 i != VARYING_SLOT_LAYER &&
9756 i != VARYING_SLOT_PRIMITIVE_ID &&
9757 i != VARYING_SLOT_VIEWPORT)
9758 continue;
9759
9760 export_vs_varying(ctx, i, false, NULL);
9761 }
9762
9763 if (!exported_pos)
9764 create_null_export(ctx);
9765 }
9766
9767 static bool export_fs_mrt_z(isel_context *ctx)
9768 {
9769 Builder bld(ctx->program, ctx->block);
9770 unsigned enabled_channels = 0;
9771 bool compr = false;
9772 Operand values[4];
9773
9774 for (unsigned i = 0; i < 4; ++i) {
9775 values[i] = Operand(v1);
9776 }
9777
9778 /* Both stencil and sample mask only need 16-bits. */
9779 if (!ctx->program->info->ps.writes_z &&
9780 (ctx->program->info->ps.writes_stencil ||
9781 ctx->program->info->ps.writes_sample_mask)) {
9782 compr = true; /* COMPR flag */
9783
9784 if (ctx->program->info->ps.writes_stencil) {
9785 /* Stencil should be in X[23:16]. */
9786 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9787 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9788 enabled_channels |= 0x3;
9789 }
9790
9791 if (ctx->program->info->ps.writes_sample_mask) {
9792 /* SampleMask should be in Y[15:0]. */
9793 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9794 enabled_channels |= 0xc;
9795 }
9796 } else {
9797 if (ctx->program->info->ps.writes_z) {
9798 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9799 enabled_channels |= 0x1;
9800 }
9801
9802 if (ctx->program->info->ps.writes_stencil) {
9803 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9804 enabled_channels |= 0x2;
9805 }
9806
9807 if (ctx->program->info->ps.writes_sample_mask) {
9808 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9809 enabled_channels |= 0x4;
9810 }
9811 }
9812
9813 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9814 * writemask component.
9815 */
9816 if (ctx->options->chip_class == GFX6 &&
9817 ctx->options->family != CHIP_OLAND &&
9818 ctx->options->family != CHIP_HAINAN) {
9819 enabled_channels |= 0x1;
9820 }
9821
9822 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9823 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9824
9825 return true;
9826 }
9827
9828 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9829 {
9830 Builder bld(ctx->program, ctx->block);
9831 unsigned write_mask = ctx->outputs.mask[slot];
9832 Operand values[4];
9833
9834 for (unsigned i = 0; i < 4; ++i) {
9835 if (write_mask & (1 << i)) {
9836 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9837 } else {
9838 values[i] = Operand(v1);
9839 }
9840 }
9841
9842 unsigned target, col_format;
9843 unsigned enabled_channels = 0;
9844 aco_opcode compr_op = (aco_opcode)0;
9845
9846 slot -= FRAG_RESULT_DATA0;
9847 target = V_008DFC_SQ_EXP_MRT + slot;
9848 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9849
9850 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9851 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9852
9853 switch (col_format)
9854 {
9855 case V_028714_SPI_SHADER_ZERO:
9856 enabled_channels = 0; /* writemask */
9857 target = V_008DFC_SQ_EXP_NULL;
9858 break;
9859
9860 case V_028714_SPI_SHADER_32_R:
9861 enabled_channels = 1;
9862 break;
9863
9864 case V_028714_SPI_SHADER_32_GR:
9865 enabled_channels = 0x3;
9866 break;
9867
9868 case V_028714_SPI_SHADER_32_AR:
9869 if (ctx->options->chip_class >= GFX10) {
9870 /* Special case: on GFX10, the outputs are different for 32_AR */
9871 enabled_channels = 0x3;
9872 values[1] = values[3];
9873 values[3] = Operand(v1);
9874 } else {
9875 enabled_channels = 0x9;
9876 }
9877 break;
9878
9879 case V_028714_SPI_SHADER_FP16_ABGR:
9880 enabled_channels = 0x5;
9881 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9882 break;
9883
9884 case V_028714_SPI_SHADER_UNORM16_ABGR:
9885 enabled_channels = 0x5;
9886 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9887 break;
9888
9889 case V_028714_SPI_SHADER_SNORM16_ABGR:
9890 enabled_channels = 0x5;
9891 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9892 break;
9893
9894 case V_028714_SPI_SHADER_UINT16_ABGR: {
9895 enabled_channels = 0x5;
9896 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9897 if (is_int8 || is_int10) {
9898 /* clamp */
9899 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9900 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9901
9902 for (unsigned i = 0; i < 4; i++) {
9903 if ((write_mask >> i) & 1) {
9904 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9905 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9906 values[i]);
9907 }
9908 }
9909 }
9910 break;
9911 }
9912
9913 case V_028714_SPI_SHADER_SINT16_ABGR:
9914 enabled_channels = 0x5;
9915 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9916 if (is_int8 || is_int10) {
9917 /* clamp */
9918 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9919 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9920 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9921 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9922
9923 for (unsigned i = 0; i < 4; i++) {
9924 if ((write_mask >> i) & 1) {
9925 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9926 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9927 values[i]);
9928 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9929 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9930 values[i]);
9931 }
9932 }
9933 }
9934 break;
9935
9936 case V_028714_SPI_SHADER_32_ABGR:
9937 enabled_channels = 0xF;
9938 break;
9939
9940 default:
9941 break;
9942 }
9943
9944 if (target == V_008DFC_SQ_EXP_NULL)
9945 return false;
9946
9947 if ((bool) compr_op) {
9948 for (int i = 0; i < 2; i++) {
9949 /* check if at least one of the values to be compressed is enabled */
9950 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9951 if (enabled) {
9952 enabled_channels |= enabled << (i*2);
9953 values[i] = bld.vop3(compr_op, bld.def(v1),
9954 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9955 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9956 } else {
9957 values[i] = Operand(v1);
9958 }
9959 }
9960 values[2] = Operand(v1);
9961 values[3] = Operand(v1);
9962 } else {
9963 for (int i = 0; i < 4; i++)
9964 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
9965 }
9966
9967 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9968 enabled_channels, target, (bool) compr_op);
9969 return true;
9970 }
9971
9972 static void create_fs_exports(isel_context *ctx)
9973 {
9974 bool exported = false;
9975
9976 /* Export depth, stencil and sample mask. */
9977 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
9978 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
9979 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
9980 exported |= export_fs_mrt_z(ctx);
9981
9982 /* Export all color render targets. */
9983 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
9984 if (ctx->outputs.mask[i])
9985 exported |= export_fs_mrt_color(ctx, i);
9986
9987 if (!exported)
9988 create_null_export(ctx);
9989 }
9990
9991 static void write_tcs_tess_factors(isel_context *ctx)
9992 {
9993 unsigned outer_comps;
9994 unsigned inner_comps;
9995
9996 switch (ctx->args->options->key.tcs.primitive_mode) {
9997 case GL_ISOLINES:
9998 outer_comps = 2;
9999 inner_comps = 0;
10000 break;
10001 case GL_TRIANGLES:
10002 outer_comps = 3;
10003 inner_comps = 1;
10004 break;
10005 case GL_QUADS:
10006 outer_comps = 4;
10007 inner_comps = 2;
10008 break;
10009 default:
10010 return;
10011 }
10012
10013 Builder bld(ctx->program, ctx->block);
10014
10015 bld.barrier(aco_opcode::p_memory_barrier_shared);
10016 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10017 bld.sopp(aco_opcode::s_barrier);
10018
10019 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10020 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10021
10022 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10023 if_context ic_invocation_id_is_zero;
10024 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10025 bld.reset(ctx->block);
10026
10027 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));
10028
10029 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10030 unsigned stride = inner_comps + outer_comps;
10031 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10032 Temp tf_inner_vec;
10033 Temp tf_outer_vec;
10034 Temp out[6];
10035 assert(stride <= (sizeof(out) / sizeof(Temp)));
10036
10037 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10038 // LINES reversal
10039 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10040 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10041 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10042 } else {
10043 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);
10044 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);
10045
10046 for (unsigned i = 0; i < outer_comps; ++i)
10047 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10048 for (unsigned i = 0; i < inner_comps; ++i)
10049 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10050 }
10051
10052 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10053 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10054 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10055 unsigned tf_const_offset = 0;
10056
10057 if (ctx->program->chip_class <= GFX8) {
10058 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);
10059 if_context ic_rel_patch_id_is_zero;
10060 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10061 bld.reset(ctx->block);
10062
10063 /* Store the dynamic HS control word. */
10064 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10065 bld.mubuf(aco_opcode::buffer_store_dword,
10066 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10067 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10068 /* disable_wqm */ false, /* glc */ true);
10069 tf_const_offset += 4;
10070
10071 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10072 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10073 bld.reset(ctx->block);
10074 }
10075
10076 assert(stride == 2 || stride == 4 || stride == 6);
10077 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10078 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10079
10080 /* Store to offchip for TES to read - only if TES reads them */
10081 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10082 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));
10083 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10084
10085 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10086 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);
10087
10088 if (likely(inner_comps)) {
10089 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10090 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);
10091 }
10092 }
10093
10094 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10095 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10096 }
10097
10098 static void emit_stream_output(isel_context *ctx,
10099 Temp const *so_buffers,
10100 Temp const *so_write_offset,
10101 const struct radv_stream_output *output)
10102 {
10103 unsigned num_comps = util_bitcount(output->component_mask);
10104 unsigned writemask = (1 << num_comps) - 1;
10105 unsigned loc = output->location;
10106 unsigned buf = output->buffer;
10107
10108 assert(num_comps && num_comps <= 4);
10109 if (!num_comps || num_comps > 4)
10110 return;
10111
10112 unsigned start = ffs(output->component_mask) - 1;
10113
10114 Temp out[4];
10115 bool all_undef = true;
10116 assert(ctx->stage & hw_vs);
10117 for (unsigned i = 0; i < num_comps; i++) {
10118 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10119 all_undef = all_undef && !out[i].id();
10120 }
10121 if (all_undef)
10122 return;
10123
10124 while (writemask) {
10125 int start, count;
10126 u_bit_scan_consecutive_range(&writemask, &start, &count);
10127 if (count == 3 && ctx->options->chip_class == GFX6) {
10128 /* GFX6 doesn't support storing vec3, split it. */
10129 writemask |= 1u << (start + 2);
10130 count = 2;
10131 }
10132
10133 unsigned offset = output->offset + start * 4;
10134
10135 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10136 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10137 for (int i = 0; i < count; ++i)
10138 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10139 vec->definitions[0] = Definition(write_data);
10140 ctx->block->instructions.emplace_back(std::move(vec));
10141
10142 aco_opcode opcode;
10143 switch (count) {
10144 case 1:
10145 opcode = aco_opcode::buffer_store_dword;
10146 break;
10147 case 2:
10148 opcode = aco_opcode::buffer_store_dwordx2;
10149 break;
10150 case 3:
10151 opcode = aco_opcode::buffer_store_dwordx3;
10152 break;
10153 case 4:
10154 opcode = aco_opcode::buffer_store_dwordx4;
10155 break;
10156 default:
10157 unreachable("Unsupported dword count.");
10158 }
10159
10160 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10161 store->operands[0] = Operand(so_buffers[buf]);
10162 store->operands[1] = Operand(so_write_offset[buf]);
10163 store->operands[2] = Operand((uint32_t) 0);
10164 store->operands[3] = Operand(write_data);
10165 if (offset > 4095) {
10166 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10167 Builder bld(ctx->program, ctx->block);
10168 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10169 } else {
10170 store->offset = offset;
10171 }
10172 store->offen = true;
10173 store->glc = true;
10174 store->dlc = false;
10175 store->slc = true;
10176 store->can_reorder = true;
10177 ctx->block->instructions.emplace_back(std::move(store));
10178 }
10179 }
10180
10181 static void emit_streamout(isel_context *ctx, unsigned stream)
10182 {
10183 Builder bld(ctx->program, ctx->block);
10184
10185 Temp so_buffers[4];
10186 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10187 for (unsigned i = 0; i < 4; i++) {
10188 unsigned stride = ctx->program->info->so.strides[i];
10189 if (!stride)
10190 continue;
10191
10192 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10193 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10194 }
10195
10196 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10197 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10198
10199 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10200
10201 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10202
10203 if_context ic;
10204 begin_divergent_if_then(ctx, &ic, can_emit);
10205
10206 bld.reset(ctx->block);
10207
10208 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10209
10210 Temp so_write_offset[4];
10211
10212 for (unsigned i = 0; i < 4; i++) {
10213 unsigned stride = ctx->program->info->so.strides[i];
10214 if (!stride)
10215 continue;
10216
10217 if (stride == 1) {
10218 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10219 get_arg(ctx, ctx->args->streamout_write_idx),
10220 get_arg(ctx, ctx->args->streamout_offset[i]));
10221 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10222
10223 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10224 } else {
10225 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10226 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10227 get_arg(ctx, ctx->args->streamout_offset[i]));
10228 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10229 }
10230 }
10231
10232 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10233 struct radv_stream_output *output =
10234 &ctx->program->info->so.outputs[i];
10235 if (stream != output->stream)
10236 continue;
10237
10238 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10239 }
10240
10241 begin_divergent_if_else(ctx, &ic);
10242 end_divergent_if(ctx, &ic);
10243 }
10244
10245 } /* end namespace */
10246
10247 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10248 {
10249 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10250 Builder bld(ctx->program, ctx->block);
10251 constexpr unsigned hs_idx = 1u;
10252 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10253 get_arg(ctx, ctx->args->merged_wave_info),
10254 Operand((8u << 16) | (hs_idx * 8u)));
10255 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10256
10257 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10258
10259 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10260 get_arg(ctx, ctx->args->rel_auto_id),
10261 get_arg(ctx, ctx->args->ac.instance_id),
10262 ls_has_nonzero_hs_threads);
10263 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10264 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10265 get_arg(ctx, ctx->args->rel_auto_id),
10266 ls_has_nonzero_hs_threads);
10267 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10268 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10269 get_arg(ctx, ctx->args->ac.vertex_id),
10270 ls_has_nonzero_hs_threads);
10271
10272 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10273 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10274 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10275 }
10276
10277 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10278 {
10279 /* Split all arguments except for the first (ring_offsets) and the last
10280 * (exec) so that the dead channels don't stay live throughout the program.
10281 */
10282 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10283 if (startpgm->definitions[i].regClass().size() > 1) {
10284 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10285 startpgm->definitions[i].regClass().size());
10286 }
10287 }
10288 }
10289
10290 void handle_bc_optimize(isel_context *ctx)
10291 {
10292 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10293 Builder bld(ctx->program, ctx->block);
10294 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10295 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10296 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10297 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10298 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10299 if (uses_center && uses_centroid) {
10300 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10301 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10302
10303 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10304 Temp new_coord[2];
10305 for (unsigned i = 0; i < 2; i++) {
10306 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10307 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10308 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10309 persp_centroid, persp_center, sel);
10310 }
10311 ctx->persp_centroid = bld.tmp(v2);
10312 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10313 Operand(new_coord[0]), Operand(new_coord[1]));
10314 emit_split_vector(ctx, ctx->persp_centroid, 2);
10315 }
10316
10317 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10318 Temp new_coord[2];
10319 for (unsigned i = 0; i < 2; i++) {
10320 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10321 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10322 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10323 linear_centroid, linear_center, sel);
10324 }
10325 ctx->linear_centroid = bld.tmp(v2);
10326 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10327 Operand(new_coord[0]), Operand(new_coord[1]));
10328 emit_split_vector(ctx, ctx->linear_centroid, 2);
10329 }
10330 }
10331 }
10332
10333 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10334 {
10335 Program *program = ctx->program;
10336
10337 unsigned float_controls = shader->info.float_controls_execution_mode;
10338
10339 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10340 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10341 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10342 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10343 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10344
10345 program->next_fp_mode.must_flush_denorms32 =
10346 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10347 program->next_fp_mode.must_flush_denorms16_64 =
10348 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10349 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10350
10351 program->next_fp_mode.care_about_round32 =
10352 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10353
10354 program->next_fp_mode.care_about_round16_64 =
10355 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10356 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10357
10358 /* default to preserving fp16 and fp64 denorms, since it's free */
10359 if (program->next_fp_mode.must_flush_denorms16_64)
10360 program->next_fp_mode.denorm16_64 = 0;
10361 else
10362 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10363
10364 /* preserving fp32 denorms is expensive, so only do it if asked */
10365 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10366 program->next_fp_mode.denorm32 = fp_denorm_keep;
10367 else
10368 program->next_fp_mode.denorm32 = 0;
10369
10370 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10371 program->next_fp_mode.round32 = fp_round_tz;
10372 else
10373 program->next_fp_mode.round32 = fp_round_ne;
10374
10375 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10376 program->next_fp_mode.round16_64 = fp_round_tz;
10377 else
10378 program->next_fp_mode.round16_64 = fp_round_ne;
10379
10380 ctx->block->fp_mode = program->next_fp_mode;
10381 }
10382
10383 void cleanup_cfg(Program *program)
10384 {
10385 /* create linear_succs/logical_succs */
10386 for (Block& BB : program->blocks) {
10387 for (unsigned idx : BB.linear_preds)
10388 program->blocks[idx].linear_succs.emplace_back(BB.index);
10389 for (unsigned idx : BB.logical_preds)
10390 program->blocks[idx].logical_succs.emplace_back(BB.index);
10391 }
10392 }
10393
10394 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10395 {
10396 Builder bld(ctx->program, ctx->block);
10397
10398 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10399 Temp count = i == 0
10400 ? get_arg(ctx, ctx->args->merged_wave_info)
10401 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10402 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10403
10404 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10405 Temp cond;
10406
10407 if (ctx->program->wave_size == 64) {
10408 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10409 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10410 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10411 } else {
10412 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10413 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10414 }
10415
10416 return cond;
10417 }
10418
10419 bool ngg_early_prim_export(isel_context *ctx)
10420 {
10421 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10422 return true;
10423 }
10424
10425 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10426 {
10427 Builder bld(ctx->program, ctx->block);
10428
10429 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10430 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10431
10432 /* Get the id of the current wave within the threadgroup (workgroup) */
10433 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10434 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10435
10436 /* Execute the following code only on the first wave (wave id 0),
10437 * use the SCC def to tell if the wave id is zero or not.
10438 */
10439 Temp cond = wave_id_in_tg.def(1).getTemp();
10440 if_context ic;
10441 begin_uniform_if_then(ctx, &ic, cond);
10442 begin_uniform_if_else(ctx, &ic);
10443 bld.reset(ctx->block);
10444
10445 /* Number of vertices output by VS/TES */
10446 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10447 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10448 /* Number of primitives output by VS/TES */
10449 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10450 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10451
10452 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10453 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10454 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10455
10456 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10457 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10458
10459 end_uniform_if(ctx, &ic);
10460
10461 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10462 bld.reset(ctx->block);
10463 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10464 }
10465
10466 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10467 {
10468 Builder bld(ctx->program, ctx->block);
10469
10470 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10471 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10472 }
10473
10474 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10475 Temp tmp;
10476
10477 for (unsigned i = 0; i < num_vertices; ++i) {
10478 assert(vtxindex[i].id());
10479
10480 if (i)
10481 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10482 else
10483 tmp = vtxindex[i];
10484
10485 /* The initial edge flag is always false in tess eval shaders. */
10486 if (ctx->stage == ngg_vertex_gs) {
10487 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10488 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10489 }
10490 }
10491
10492 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10493
10494 return tmp;
10495 }
10496
10497 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10498 {
10499 Builder bld(ctx->program, ctx->block);
10500 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10501
10502 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10503 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10504 false /* compressed */, true/* done */, false /* valid mask */);
10505 }
10506
10507 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10508 {
10509 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10510 * These must always come before VS exports.
10511 *
10512 * It is recommended to do these as early as possible. They can be at the beginning when
10513 * there is no SW GS and the shader doesn't write edge flags.
10514 */
10515
10516 if_context ic;
10517 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10518 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10519
10520 Builder bld(ctx->program, ctx->block);
10521 constexpr unsigned max_vertices_per_primitive = 3;
10522 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10523
10524 if (ctx->stage == ngg_vertex_gs) {
10525 /* TODO: optimize for points & lines */
10526 } else if (ctx->stage == ngg_tess_eval_gs) {
10527 if (ctx->shader->info.tess.point_mode)
10528 num_vertices_per_primitive = 1;
10529 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10530 num_vertices_per_primitive = 2;
10531 } else {
10532 unreachable("Unsupported NGG shader stage");
10533 }
10534
10535 Temp vtxindex[max_vertices_per_primitive];
10536 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10537 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10538 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10539 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10540 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10541 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10542 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10543 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10544
10545 /* Export primitive data to the index buffer. */
10546 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10547
10548 /* Export primitive ID. */
10549 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10550 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10551 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10552 Temp provoking_vtx_index = vtxindex[0];
10553 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10554
10555 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10556 }
10557
10558 begin_divergent_if_else(ctx, &ic);
10559 end_divergent_if(ctx, &ic);
10560 }
10561
10562 void ngg_emit_nogs_output(isel_context *ctx)
10563 {
10564 /* Emits NGG GS output, for stages that don't have SW GS. */
10565
10566 if_context ic;
10567 Builder bld(ctx->program, ctx->block);
10568 bool late_prim_export = !ngg_early_prim_export(ctx);
10569
10570 /* NGG streamout is currently disabled by default. */
10571 assert(!ctx->args->shader_info->so.num_outputs);
10572
10573 if (late_prim_export) {
10574 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10575 create_export_phis(ctx);
10576 /* Do what we need to do in the GS threads. */
10577 ngg_emit_nogs_gsthreads(ctx);
10578
10579 /* What comes next should be executed on ES threads. */
10580 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10581 begin_divergent_if_then(ctx, &ic, is_es_thread);
10582 bld.reset(ctx->block);
10583 }
10584
10585 /* Export VS outputs */
10586 ctx->block->kind |= block_kind_export_end;
10587 create_vs_exports(ctx);
10588
10589 /* Export primitive ID */
10590 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10591 Temp prim_id;
10592
10593 if (ctx->stage == ngg_vertex_gs) {
10594 /* Wait for GS threads to store primitive ID in LDS. */
10595 bld.barrier(aco_opcode::p_memory_barrier_shared);
10596 bld.sopp(aco_opcode::s_barrier);
10597
10598 /* Calculate LDS address where the GS threads stored the primitive ID. */
10599 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10600 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10601 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10602 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10603 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10604 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10605
10606 /* Load primitive ID from LDS. */
10607 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10608 } else if (ctx->stage == ngg_tess_eval_gs) {
10609 /* TES: Just use the patch ID as the primitive ID. */
10610 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10611 } else {
10612 unreachable("unsupported NGG shader stage.");
10613 }
10614
10615 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10616 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10617
10618 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10619 }
10620
10621 if (late_prim_export) {
10622 begin_divergent_if_else(ctx, &ic);
10623 end_divergent_if(ctx, &ic);
10624 bld.reset(ctx->block);
10625 }
10626 }
10627
10628 void select_program(Program *program,
10629 unsigned shader_count,
10630 struct nir_shader *const *shaders,
10631 ac_shader_config* config,
10632 struct radv_shader_args *args)
10633 {
10634 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10635 if_context ic_merged_wave_info;
10636 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10637
10638 for (unsigned i = 0; i < shader_count; i++) {
10639 nir_shader *nir = shaders[i];
10640 init_context(&ctx, nir);
10641
10642 setup_fp_mode(&ctx, nir);
10643
10644 if (!i) {
10645 /* needs to be after init_context() for FS */
10646 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10647 append_logical_start(ctx.block);
10648
10649 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10650 fix_ls_vgpr_init_bug(&ctx, startpgm);
10651
10652 split_arguments(&ctx, startpgm);
10653 }
10654
10655 if (ngg_no_gs) {
10656 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10657
10658 if (ngg_early_prim_export(&ctx))
10659 ngg_emit_nogs_gsthreads(&ctx);
10660 }
10661
10662 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10663 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10664 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10665 ((nir->info.stage == MESA_SHADER_VERTEX &&
10666 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10667 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10668 ctx.stage == tess_eval_geometry_gs));
10669
10670 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10671 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10672 if (check_merged_wave_info) {
10673 Temp cond = merged_wave_info_to_mask(&ctx, i);
10674 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10675 }
10676
10677 if (i) {
10678 Builder bld(ctx.program, ctx.block);
10679
10680 bld.barrier(aco_opcode::p_memory_barrier_shared);
10681 bld.sopp(aco_opcode::s_barrier);
10682
10683 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10684 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));
10685 }
10686 } else if (ctx.stage == geometry_gs)
10687 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10688
10689 if (ctx.stage == fragment_fs)
10690 handle_bc_optimize(&ctx);
10691
10692 visit_cf_list(&ctx, &func->body);
10693
10694 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10695 emit_streamout(&ctx, 0);
10696
10697 if (ctx.stage & hw_vs) {
10698 create_vs_exports(&ctx);
10699 ctx.block->kind |= block_kind_export_end;
10700 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10701 ngg_emit_nogs_output(&ctx);
10702 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10703 Builder bld(ctx.program, ctx.block);
10704 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10705 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10706 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10707 write_tcs_tess_factors(&ctx);
10708 }
10709
10710 if (ctx.stage == fragment_fs) {
10711 create_fs_exports(&ctx);
10712 ctx.block->kind |= block_kind_export_end;
10713 }
10714
10715 if (endif_merged_wave_info) {
10716 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10717 end_divergent_if(&ctx, &ic_merged_wave_info);
10718 }
10719
10720 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10721 ngg_emit_nogs_output(&ctx);
10722
10723 ralloc_free(ctx.divergent_vals);
10724
10725 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10726 /* Outputs of the previous stage are inputs to the next stage */
10727 ctx.inputs = ctx.outputs;
10728 ctx.outputs = shader_io_state();
10729 }
10730 }
10731
10732 program->config->float_mode = program->blocks[0].fp_mode.val;
10733
10734 append_logical_end(ctx.block);
10735 ctx.block->kind |= block_kind_uniform;
10736 Builder bld(ctx.program, ctx.block);
10737 if (ctx.program->wb_smem_l1_on_end)
10738 bld.smem(aco_opcode::s_dcache_wb, false);
10739 bld.sopp(aco_opcode::s_endpgm);
10740
10741 cleanup_cfg(program);
10742 }
10743
10744 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10745 ac_shader_config* config,
10746 struct radv_shader_args *args)
10747 {
10748 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10749
10750 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10751 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10752 program->next_fp_mode.must_flush_denorms32 = false;
10753 program->next_fp_mode.must_flush_denorms16_64 = false;
10754 program->next_fp_mode.care_about_round32 = false;
10755 program->next_fp_mode.care_about_round16_64 = false;
10756 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10757 program->next_fp_mode.denorm32 = 0;
10758 program->next_fp_mode.round32 = fp_round_ne;
10759 program->next_fp_mode.round16_64 = fp_round_ne;
10760 ctx.block->fp_mode = program->next_fp_mode;
10761
10762 add_startpgm(&ctx);
10763 append_logical_start(ctx.block);
10764
10765 Builder bld(ctx.program, ctx.block);
10766
10767 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10768
10769 Operand stream_id(0u);
10770 if (args->shader_info->so.num_outputs)
10771 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10772 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10773
10774 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10775
10776 std::stack<Block> endif_blocks;
10777
10778 for (unsigned stream = 0; stream < 4; stream++) {
10779 if (stream_id.isConstant() && stream != stream_id.constantValue())
10780 continue;
10781
10782 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10783 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10784 continue;
10785
10786 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10787
10788 unsigned BB_if_idx = ctx.block->index;
10789 Block BB_endif = Block();
10790 if (!stream_id.isConstant()) {
10791 /* begin IF */
10792 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10793 append_logical_end(ctx.block);
10794 ctx.block->kind |= block_kind_uniform;
10795 bld.branch(aco_opcode::p_cbranch_z, cond);
10796
10797 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10798
10799 ctx.block = ctx.program->create_and_insert_block();
10800 add_edge(BB_if_idx, ctx.block);
10801 bld.reset(ctx.block);
10802 append_logical_start(ctx.block);
10803 }
10804
10805 unsigned offset = 0;
10806 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10807 if (args->shader_info->gs.output_streams[i] != stream)
10808 continue;
10809
10810 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10811 unsigned length = util_last_bit(output_usage_mask);
10812 for (unsigned j = 0; j < length; ++j) {
10813 if (!(output_usage_mask & (1 << j)))
10814 continue;
10815
10816 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10817 Temp voffset = vtx_offset;
10818 if (const_offset >= 4096u) {
10819 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10820 const_offset %= 4096u;
10821 }
10822
10823 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10824 mubuf->definitions[0] = bld.def(v1);
10825 mubuf->operands[0] = Operand(gsvs_ring);
10826 mubuf->operands[1] = Operand(voffset);
10827 mubuf->operands[2] = Operand(0u);
10828 mubuf->offen = true;
10829 mubuf->offset = const_offset;
10830 mubuf->glc = true;
10831 mubuf->slc = true;
10832 mubuf->dlc = args->options->chip_class >= GFX10;
10833 mubuf->barrier = barrier_none;
10834 mubuf->can_reorder = true;
10835
10836 ctx.outputs.mask[i] |= 1 << j;
10837 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10838
10839 bld.insert(std::move(mubuf));
10840
10841 offset++;
10842 }
10843 }
10844
10845 if (args->shader_info->so.num_outputs) {
10846 emit_streamout(&ctx, stream);
10847 bld.reset(ctx.block);
10848 }
10849
10850 if (stream == 0) {
10851 create_vs_exports(&ctx);
10852 ctx.block->kind |= block_kind_export_end;
10853 }
10854
10855 if (!stream_id.isConstant()) {
10856 append_logical_end(ctx.block);
10857
10858 /* branch from then block to endif block */
10859 bld.branch(aco_opcode::p_branch);
10860 add_edge(ctx.block->index, &BB_endif);
10861 ctx.block->kind |= block_kind_uniform;
10862
10863 /* emit else block */
10864 ctx.block = ctx.program->create_and_insert_block();
10865 add_edge(BB_if_idx, ctx.block);
10866 bld.reset(ctx.block);
10867 append_logical_start(ctx.block);
10868
10869 endif_blocks.push(std::move(BB_endif));
10870 }
10871 }
10872
10873 while (!endif_blocks.empty()) {
10874 Block BB_endif = std::move(endif_blocks.top());
10875 endif_blocks.pop();
10876
10877 Block *BB_else = ctx.block;
10878
10879 append_logical_end(BB_else);
10880 /* branch from else block to endif block */
10881 bld.branch(aco_opcode::p_branch);
10882 add_edge(BB_else->index, &BB_endif);
10883 BB_else->kind |= block_kind_uniform;
10884
10885 /** emit endif merge block */
10886 ctx.block = program->insert_block(std::move(BB_endif));
10887 bld.reset(ctx.block);
10888 append_logical_start(ctx.block);
10889 }
10890
10891 program->config->float_mode = program->blocks[0].fp_mode.val;
10892
10893 append_logical_end(ctx.block);
10894 ctx.block->kind |= block_kind_uniform;
10895 bld.sopp(aco_opcode::s_endpgm);
10896
10897 cleanup_cfg(program);
10898 }
10899 }