aco: be more careful about using SMEM for load_global
[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 Operand load_lds_size_m0(isel_context *ctx)
3055 {
3056 /* TODO: m0 does not need to be initialized on GFX9+ */
3057 Builder bld(ctx->program, ctx->block);
3058 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3059 }
3060
3061 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3062 Temp address, unsigned base_offset, unsigned align)
3063 {
3064 assert(util_is_power_of_two_nonzero(align) && align >= 4);
3065
3066 Builder bld(ctx->program, ctx->block);
3067
3068 Operand m = load_lds_size_m0(ctx);
3069
3070 unsigned num_components = dst.size() * 4u / elem_size_bytes;
3071 unsigned bytes_read = 0;
3072 unsigned result_size = 0;
3073 unsigned total_bytes = num_components * elem_size_bytes;
3074 std::array<Temp, NIR_MAX_VEC_COMPONENTS> result;
3075 bool large_ds_read = ctx->options->chip_class >= GFX7;
3076 bool usable_read2 = ctx->options->chip_class >= GFX7;
3077
3078 while (bytes_read < total_bytes) {
3079 unsigned todo = total_bytes - bytes_read;
3080 bool aligned8 = bytes_read % 8 == 0 && align % 8 == 0;
3081 bool aligned16 = bytes_read % 16 == 0 && align % 16 == 0;
3082
3083 aco_opcode op = aco_opcode::last_opcode;
3084 bool read2 = false;
3085 if (todo >= 16 && aligned16 && large_ds_read) {
3086 op = aco_opcode::ds_read_b128;
3087 todo = 16;
3088 } else if (todo >= 16 && aligned8 && usable_read2) {
3089 op = aco_opcode::ds_read2_b64;
3090 read2 = true;
3091 todo = 16;
3092 } else if (todo >= 12 && aligned16 && large_ds_read) {
3093 op = aco_opcode::ds_read_b96;
3094 todo = 12;
3095 } else if (todo >= 8 && aligned8) {
3096 op = aco_opcode::ds_read_b64;
3097 todo = 8;
3098 } else if (todo >= 8 && usable_read2) {
3099 op = aco_opcode::ds_read2_b32;
3100 read2 = true;
3101 todo = 8;
3102 } else if (todo >= 4) {
3103 op = aco_opcode::ds_read_b32;
3104 todo = 4;
3105 } else {
3106 assert(false);
3107 }
3108 assert(todo % elem_size_bytes == 0);
3109 unsigned num_elements = todo / elem_size_bytes;
3110 unsigned offset = base_offset + bytes_read;
3111 unsigned max_offset = read2 ? 1019 : 65535;
3112
3113 Temp address_offset = address;
3114 if (offset > max_offset) {
3115 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3116 offset = bytes_read;
3117 }
3118 assert(offset <= max_offset); /* bytes_read shouldn't be large enough for this to happen */
3119
3120 Temp res;
3121 if (num_components == 1 && dst.type() == RegType::vgpr)
3122 res = dst;
3123 else
3124 res = bld.tmp(RegClass(RegType::vgpr, todo / 4));
3125
3126 if (read2)
3127 res = bld.ds(op, Definition(res), address_offset, m, offset / (todo / 2), (offset / (todo / 2)) + 1);
3128 else
3129 res = bld.ds(op, Definition(res), address_offset, m, offset);
3130
3131 if (num_components == 1) {
3132 assert(todo == total_bytes);
3133 if (dst.type() == RegType::sgpr)
3134 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
3135 return dst;
3136 }
3137
3138 if (dst.type() == RegType::sgpr) {
3139 Temp new_res = bld.tmp(RegType::sgpr, res.size());
3140 expand_vector(ctx, res, new_res, res.size(), (1 << res.size()) - 1);
3141 res = new_res;
3142 }
3143
3144 if (num_elements == 1) {
3145 result[result_size++] = res;
3146 } else {
3147 assert(res != dst && res.size() % num_elements == 0);
3148 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_elements)};
3149 split->operands[0] = Operand(res);
3150 for (unsigned i = 0; i < num_elements; i++)
3151 split->definitions[i] = Definition(result[result_size++] = bld.tmp(res.type(), elem_size_bytes / 4));
3152 ctx->block->instructions.emplace_back(std::move(split));
3153 }
3154
3155 bytes_read += todo;
3156 }
3157
3158 assert(result_size == num_components && result_size > 1);
3159 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, result_size, 1)};
3160 for (unsigned i = 0; i < result_size; i++)
3161 vec->operands[i] = Operand(result[i]);
3162 vec->definitions[0] = Definition(dst);
3163 ctx->block->instructions.emplace_back(std::move(vec));
3164 ctx->allocated_vec.emplace(dst.id(), result);
3165
3166 return dst;
3167 }
3168
3169 Temp extract_subvector(isel_context *ctx, Temp data, unsigned start, unsigned size, RegType type)
3170 {
3171 if (start == 0 && size == data.size())
3172 return type == RegType::vgpr ? as_vgpr(ctx, data) : data;
3173
3174 unsigned size_hint = 1;
3175 auto it = ctx->allocated_vec.find(data.id());
3176 if (it != ctx->allocated_vec.end())
3177 size_hint = it->second[0].size();
3178 if (size % size_hint || start % size_hint)
3179 size_hint = 1;
3180
3181 start /= size_hint;
3182 size /= size_hint;
3183
3184 Temp elems[size];
3185 for (unsigned i = 0; i < size; i++)
3186 elems[i] = emit_extract_vector(ctx, data, start + i, RegClass(type, size_hint));
3187
3188 if (size == 1)
3189 return type == RegType::vgpr ? as_vgpr(ctx, elems[0]) : elems[0];
3190
3191 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
3192 for (unsigned i = 0; i < size; i++)
3193 vec->operands[i] = Operand(elems[i]);
3194 Temp res = {ctx->program->allocateId(), RegClass(type, size * size_hint)};
3195 vec->definitions[0] = Definition(res);
3196 ctx->block->instructions.emplace_back(std::move(vec));
3197 return res;
3198 }
3199
3200 void ds_write_helper(isel_context *ctx, Operand m, Temp address, Temp data, unsigned data_start, unsigned total_size, unsigned offset0, unsigned offset1, unsigned align)
3201 {
3202 Builder bld(ctx->program, ctx->block);
3203 unsigned bytes_written = 0;
3204 bool large_ds_write = ctx->options->chip_class >= GFX7;
3205 bool usable_write2 = ctx->options->chip_class >= GFX7;
3206
3207 while (bytes_written < total_size * 4) {
3208 unsigned todo = total_size * 4 - bytes_written;
3209 bool aligned8 = bytes_written % 8 == 0 && align % 8 == 0;
3210 bool aligned16 = bytes_written % 16 == 0 && align % 16 == 0;
3211
3212 aco_opcode op = aco_opcode::last_opcode;
3213 bool write2 = false;
3214 unsigned size = 0;
3215 if (todo >= 16 && aligned16 && large_ds_write) {
3216 op = aco_opcode::ds_write_b128;
3217 size = 4;
3218 } else if (todo >= 16 && aligned8 && usable_write2) {
3219 op = aco_opcode::ds_write2_b64;
3220 write2 = true;
3221 size = 4;
3222 } else if (todo >= 12 && aligned16 && large_ds_write) {
3223 op = aco_opcode::ds_write_b96;
3224 size = 3;
3225 } else if (todo >= 8 && aligned8) {
3226 op = aco_opcode::ds_write_b64;
3227 size = 2;
3228 } else if (todo >= 8 && usable_write2) {
3229 op = aco_opcode::ds_write2_b32;
3230 write2 = true;
3231 size = 2;
3232 } else if (todo >= 4) {
3233 op = aco_opcode::ds_write_b32;
3234 size = 1;
3235 } else {
3236 assert(false);
3237 }
3238
3239 unsigned offset = offset0 + offset1 + bytes_written;
3240 unsigned max_offset = write2 ? 1020 : 65535;
3241 Temp address_offset = address;
3242 if (offset > max_offset) {
3243 address_offset = bld.vadd32(bld.def(v1), Operand(offset0), address_offset);
3244 offset = offset1 + bytes_written;
3245 }
3246 assert(offset <= max_offset); /* offset1 shouldn't be large enough for this to happen */
3247
3248 if (write2) {
3249 Temp val0 = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size / 2, RegType::vgpr);
3250 Temp val1 = extract_subvector(ctx, data, data_start + (bytes_written >> 2) + 1, size / 2, RegType::vgpr);
3251 bld.ds(op, address_offset, val0, val1, m, offset / size / 2, (offset / size / 2) + 1);
3252 } else {
3253 Temp val = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size, RegType::vgpr);
3254 bld.ds(op, address_offset, val, m, offset);
3255 }
3256
3257 bytes_written += size * 4;
3258 }
3259 }
3260
3261 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3262 Temp address, unsigned base_offset, unsigned align)
3263 {
3264 assert(util_is_power_of_two_nonzero(align) && align >= 4);
3265 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3266
3267 Operand m = load_lds_size_m0(ctx);
3268
3269 /* we need at most two stores, assuming that the writemask is at most 4 bits wide */
3270 assert(wrmask <= 0x0f);
3271 int start[2], count[2];
3272 u_bit_scan_consecutive_range(&wrmask, &start[0], &count[0]);
3273 u_bit_scan_consecutive_range(&wrmask, &start[1], &count[1]);
3274 assert(wrmask == 0);
3275
3276 /* one combined store is sufficient */
3277 if (count[0] == count[1] && (align % elem_size_bytes) == 0 && (base_offset % elem_size_bytes) == 0) {
3278 Builder bld(ctx->program, ctx->block);
3279
3280 Temp address_offset = address;
3281 if ((base_offset / elem_size_bytes) + start[1] > 255) {
3282 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3283 base_offset = 0;
3284 }
3285
3286 assert(count[0] == 1);
3287 RegClass xtract_rc(RegType::vgpr, elem_size_bytes / 4);
3288
3289 Temp val0 = emit_extract_vector(ctx, data, start[0], xtract_rc);
3290 Temp val1 = emit_extract_vector(ctx, data, start[1], xtract_rc);
3291 aco_opcode op = elem_size_bytes == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3292 base_offset = base_offset / elem_size_bytes;
3293 bld.ds(op, address_offset, val0, val1, m,
3294 base_offset + start[0], base_offset + start[1]);
3295 return;
3296 }
3297
3298 for (unsigned i = 0; i < 2; i++) {
3299 if (count[i] == 0)
3300 continue;
3301
3302 unsigned elem_size_words = elem_size_bytes / 4;
3303 ds_write_helper(ctx, m, address, data, start[i] * elem_size_words, count[i] * elem_size_words,
3304 base_offset, start[i] * elem_size_bytes, align);
3305 }
3306 return;
3307 }
3308
3309 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3310 {
3311 unsigned align = 16;
3312 if (const_offset)
3313 align = std::min(align, 1u << (ffs(const_offset) - 1));
3314
3315 return align;
3316 }
3317
3318
3319 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3320 unsigned split_cnt = 0u, Temp dst = Temp())
3321 {
3322 Builder bld(ctx->program, ctx->block);
3323 unsigned dword_size = elem_size_bytes / 4;
3324
3325 if (!dst.id())
3326 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3327
3328 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3329 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3330 instr->definitions[0] = Definition(dst);
3331
3332 for (unsigned i = 0; i < cnt; ++i) {
3333 if (arr[i].id()) {
3334 assert(arr[i].size() == dword_size);
3335 allocated_vec[i] = arr[i];
3336 instr->operands[i] = Operand(arr[i]);
3337 } else {
3338 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3339 allocated_vec[i] = zero;
3340 instr->operands[i] = Operand(zero);
3341 }
3342 }
3343
3344 bld.insert(std::move(instr));
3345
3346 if (split_cnt)
3347 emit_split_vector(ctx, dst, split_cnt);
3348 else
3349 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3350
3351 return dst;
3352 }
3353
3354 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3355 {
3356 if (const_offset >= 4096) {
3357 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3358 const_offset %= 4096u;
3359
3360 if (!voffset.id())
3361 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3362 else if (unlikely(voffset.regClass() == s1))
3363 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3364 else if (likely(voffset.regClass() == v1))
3365 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3366 else
3367 unreachable("Unsupported register class of voffset");
3368 }
3369
3370 return const_offset;
3371 }
3372
3373 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3374 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3375 {
3376 assert(vdata.id());
3377 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3378 assert(vdata.size() >= 1 && vdata.size() <= 4);
3379
3380 Builder bld(ctx->program, ctx->block);
3381 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_store_dword + vdata.size() - 1);
3382 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3383
3384 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3385 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3386 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3387 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3388 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3389
3390 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3391 }
3392
3393 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3394 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3395 bool allow_combining = true, bool reorder = true, bool slc = false)
3396 {
3397 Builder bld(ctx->program, ctx->block);
3398 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3399 assert(write_mask);
3400
3401 if (elem_size_bytes == 8) {
3402 elem_size_bytes = 4;
3403 write_mask = widen_mask(write_mask, 2);
3404 }
3405
3406 while (write_mask) {
3407 int start = 0;
3408 int count = 0;
3409 u_bit_scan_consecutive_range(&write_mask, &start, &count);
3410 assert(count > 0);
3411 assert(start >= 0);
3412
3413 while (count > 0) {
3414 unsigned sub_count = allow_combining ? MIN2(count, 4) : 1;
3415 unsigned const_offset = (unsigned) start * elem_size_bytes + base_const_offset;
3416
3417 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
3418 if (unlikely(ctx->program->chip_class == GFX6 && sub_count == 3))
3419 sub_count = 2;
3420
3421 Temp elem = extract_subvector(ctx, src, start, sub_count, RegType::vgpr);
3422 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, elem, const_offset, reorder, slc);
3423
3424 count -= sub_count;
3425 start += sub_count;
3426 }
3427
3428 assert(count == 0);
3429 }
3430 }
3431
3432 Temp emit_single_mubuf_load(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset,
3433 unsigned const_offset, unsigned size_dwords, bool allow_reorder = true)
3434 {
3435 assert(size_dwords != 3 || ctx->program->chip_class != GFX6);
3436 assert(size_dwords >= 1 && size_dwords <= 4);
3437
3438 Builder bld(ctx->program, ctx->block);
3439 Temp vdata = bld.tmp(RegClass(RegType::vgpr, size_dwords));
3440 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_load_dword + size_dwords - 1);
3441 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3442
3443 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3444 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3445 Builder::Result r = bld.mubuf(op, Definition(vdata), Operand(descriptor), voffset_op, soffset_op, const_offset,
3446 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3447 /* disable_wqm */ false, /* glc */ true,
3448 /* dlc*/ ctx->program->chip_class >= GFX10, /* slc */ false);
3449
3450 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3451
3452 return vdata;
3453 }
3454
3455 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3456 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3457 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3458 {
3459 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3460 assert((num_components * elem_size_bytes / 4) == dst.size());
3461 assert(!!stride != allow_combining);
3462
3463 Builder bld(ctx->program, ctx->block);
3464 unsigned split_cnt = num_components;
3465
3466 if (elem_size_bytes == 8) {
3467 elem_size_bytes = 4;
3468 num_components *= 2;
3469 }
3470
3471 if (!stride)
3472 stride = elem_size_bytes;
3473
3474 unsigned load_size = 1;
3475 if (allow_combining) {
3476 if ((num_components % 4) == 0)
3477 load_size = 4;
3478 else if ((num_components % 3) == 0 && ctx->program->chip_class != GFX6)
3479 load_size = 3;
3480 else if ((num_components % 2) == 0)
3481 load_size = 2;
3482 }
3483
3484 unsigned num_loads = num_components / load_size;
3485 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
3486
3487 for (unsigned i = 0; i < num_loads; ++i) {
3488 unsigned const_offset = i * stride * load_size + base_const_offset;
3489 elems[i] = emit_single_mubuf_load(ctx, descriptor, voffset, soffset, const_offset, load_size, allow_reorder);
3490 }
3491
3492 create_vec_from_array(ctx, elems.data(), num_loads, RegType::vgpr, load_size * 4u, split_cnt, dst);
3493 }
3494
3495 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)
3496 {
3497 Builder bld(ctx->program, ctx->block);
3498 Temp offset = base_offset.first;
3499 unsigned const_offset = base_offset.second;
3500
3501 if (!nir_src_is_const(*off_src)) {
3502 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3503 Temp with_stride;
3504
3505 /* Calculate indirect offset with stride */
3506 if (likely(indirect_offset_arg.regClass() == v1))
3507 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
3508 else if (indirect_offset_arg.regClass() == s1)
3509 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3510 else
3511 unreachable("Unsupported register class of indirect offset");
3512
3513 /* Add to the supplied base offset */
3514 if (offset.id() == 0)
3515 offset = with_stride;
3516 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
3517 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
3518 else if (offset.size() == 1 && with_stride.size() == 1)
3519 offset = bld.vadd32(bld.def(v1), with_stride, offset);
3520 else
3521 unreachable("Unsupported register class of indirect offset");
3522 } else {
3523 unsigned const_offset_arg = nir_src_as_uint(*off_src);
3524 const_offset += const_offset_arg * stride;
3525 }
3526
3527 return std::make_pair(offset, const_offset);
3528 }
3529
3530 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
3531 {
3532 Builder bld(ctx->program, ctx->block);
3533 Temp offset;
3534
3535 if (off1.first.id() && off2.first.id()) {
3536 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
3537 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
3538 else if (off1.first.size() == 1 && off2.first.size() == 1)
3539 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
3540 else
3541 unreachable("Unsupported register class of indirect offset");
3542 } else {
3543 offset = off1.first.id() ? off1.first : off2.first;
3544 }
3545
3546 return std::make_pair(offset, off1.second + off2.second);
3547 }
3548
3549 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
3550 {
3551 Builder bld(ctx->program, ctx->block);
3552 unsigned const_offset = offs.second * multiplier;
3553
3554 if (!offs.first.id())
3555 return std::make_pair(offs.first, const_offset);
3556
3557 Temp offset = unlikely(offs.first.regClass() == s1)
3558 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
3559 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
3560
3561 return std::make_pair(offset, const_offset);
3562 }
3563
3564 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
3565 {
3566 Builder bld(ctx->program, ctx->block);
3567
3568 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3569 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
3570 /* component is in bytes */
3571 const_offset += nir_intrinsic_component(instr) * component_stride;
3572
3573 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3574 nir_src *off_src = nir_get_io_offset_src(instr);
3575 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
3576 }
3577
3578 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
3579 {
3580 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
3581 }
3582
3583 Temp get_tess_rel_patch_id(isel_context *ctx)
3584 {
3585 Builder bld(ctx->program, ctx->block);
3586
3587 switch (ctx->shader->info.stage) {
3588 case MESA_SHADER_TESS_CTRL:
3589 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
3590 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
3591 case MESA_SHADER_TESS_EVAL:
3592 return get_arg(ctx, ctx->args->tes_rel_patch_id);
3593 default:
3594 unreachable("Unsupported stage in get_tess_rel_patch_id");
3595 }
3596 }
3597
3598 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3599 {
3600 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3601 Builder bld(ctx->program, ctx->block);
3602
3603 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
3604 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
3605
3606 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
3607
3608 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3609 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
3610
3611 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3612 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
3613 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
3614
3615 return offset_mul(ctx, offs, 4u);
3616 }
3617
3618 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
3619 {
3620 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3621 Builder bld(ctx->program, ctx->block);
3622
3623 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
3624 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
3625 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
3626 uint32_t output_vertex_size = num_tcs_outputs * 16;
3627 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3628 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
3629
3630 std::pair<Temp, unsigned> offs = instr
3631 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
3632 : std::make_pair(Temp(), 0u);
3633
3634 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3635 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
3636
3637 if (per_vertex) {
3638 assert(instr);
3639
3640 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3641 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
3642
3643 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
3644 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
3645 } else {
3646 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
3647 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
3648 }
3649
3650 return offs;
3651 }
3652
3653 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3654 {
3655 Builder bld(ctx->program, ctx->block);
3656
3657 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
3658 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
3659
3660 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
3661
3662 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3663 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
3664 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
3665
3666 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3667 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
3668
3669 return offs;
3670 }
3671
3672 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
3673 {
3674 Builder bld(ctx->program, ctx->block);
3675
3676 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
3677 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
3678 : ctx->args->options->key.tes.tcs_num_outputs;
3679
3680 unsigned output_vertex_size = num_tcs_outputs * 16;
3681 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3682 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
3683 unsigned attr_stride = ctx->tcs_num_patches;
3684
3685 std::pair<Temp, unsigned> offs = instr
3686 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
3687 : std::make_pair(Temp(), 0u);
3688
3689 if (const_base_offset)
3690 offs.second += const_base_offset * attr_stride;
3691
3692 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3693 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
3694 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
3695
3696 return offs;
3697 }
3698
3699 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
3700 {
3701 if (mask == 0)
3702 return false;
3703
3704 unsigned off = nir_intrinsic_base(instr) * 4u;
3705 nir_src *off_src = nir_get_io_offset_src(instr);
3706
3707 if (!nir_src_is_const(*off_src)) {
3708 *indirect = true;
3709 return false;
3710 }
3711
3712 *indirect = false;
3713 off += nir_src_as_uint(*off_src) * 16u;
3714
3715 while (mask) {
3716 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
3717 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
3718 return true;
3719 }
3720
3721 return false;
3722 }
3723
3724 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
3725 {
3726 unsigned write_mask = nir_intrinsic_write_mask(instr);
3727 unsigned component = nir_intrinsic_component(instr);
3728 unsigned idx = nir_intrinsic_base(instr) + component;
3729
3730 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
3731 if (off_instr->type != nir_instr_type_load_const)
3732 return false;
3733
3734 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3735 idx += nir_src_as_uint(instr->src[1]) * 4u;
3736
3737 if (instr->src[0].ssa->bit_size == 64)
3738 write_mask = widen_mask(write_mask, 2);
3739
3740 for (unsigned i = 0; i < 8; ++i) {
3741 if (write_mask & (1 << i)) {
3742 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
3743 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
3744 }
3745 idx++;
3746 }
3747
3748 return true;
3749 }
3750
3751 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
3752 {
3753 /* Only TCS per-vertex inputs are supported by this function.
3754 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
3755 */
3756 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
3757 return false;
3758
3759 nir_src *off_src = nir_get_io_offset_src(instr);
3760 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3761 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
3762 bool can_use_temps = nir_src_is_const(*off_src) &&
3763 vertex_index_instr->type == nir_instr_type_intrinsic &&
3764 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
3765
3766 if (!can_use_temps)
3767 return false;
3768
3769 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
3770 Temp *src = &ctx->inputs.temps[idx];
3771 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
3772
3773 return true;
3774 }
3775
3776 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
3777 {
3778 Builder bld(ctx->program, ctx->block);
3779
3780 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
3781 /* 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. */
3782 bool indirect_write;
3783 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
3784 if (temp_only_input && !indirect_write)
3785 return;
3786 }
3787
3788 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
3789 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3790 unsigned write_mask = nir_intrinsic_write_mask(instr);
3791 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
3792
3793 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
3794 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
3795 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
3796 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
3797 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
3798 } else {
3799 Temp lds_base;
3800
3801 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
3802 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
3803 unsigned itemsize = ctx->stage == vertex_geometry_gs
3804 ? ctx->program->info->vs.es_info.esgs_itemsize
3805 : ctx->program->info->tes.es_info.esgs_itemsize;
3806 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
3807 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));
3808 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
3809 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
3810 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
3811 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
3812 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
3813 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
3814 */
3815 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
3816 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
3817 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
3818 } else {
3819 unreachable("Invalid LS or ES stage");
3820 }
3821
3822 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
3823 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3824 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
3825 }
3826 }
3827
3828 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3829 {
3830 if (per_vertex)
3831 return false;
3832
3833 unsigned off = nir_intrinsic_base(instr) * 4u;
3834 return off == ctx->tcs_tess_lvl_out_loc ||
3835 off == ctx->tcs_tess_lvl_in_loc;
3836
3837 }
3838
3839 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3840 {
3841 uint64_t mask = per_vertex
3842 ? ctx->program->info->tcs.tes_inputs_read
3843 : ctx->program->info->tcs.tes_patch_inputs_read;
3844
3845 bool indirect_write = false;
3846 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
3847 return indirect_write || output_read_by_tes;
3848 }
3849
3850 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3851 {
3852 uint64_t mask = per_vertex
3853 ? ctx->shader->info.outputs_read
3854 : ctx->shader->info.patch_outputs_read;
3855
3856 bool indirect_write = false;
3857 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
3858 return indirect_write || output_read;
3859 }
3860
3861 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3862 {
3863 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3864 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3865
3866 Builder bld(ctx->program, ctx->block);
3867
3868 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
3869 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3870 unsigned write_mask = nir_intrinsic_write_mask(instr);
3871
3872 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
3873 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
3874 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
3875
3876 if (write_to_vmem) {
3877 std::pair<Temp, unsigned> vmem_offs = per_vertex
3878 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
3879 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
3880
3881 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));
3882 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
3883 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);
3884 }
3885
3886 if (write_to_lds) {
3887 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3888 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3889 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
3890 }
3891 }
3892
3893 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3894 {
3895 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3896 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3897
3898 Builder bld(ctx->program, ctx->block);
3899
3900 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3901 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3902 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3903 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3904
3905 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
3906 }
3907
3908 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
3909 {
3910 if (ctx->stage == vertex_vs ||
3911 ctx->stage == tess_eval_vs ||
3912 ctx->stage == fragment_fs ||
3913 ctx->stage == ngg_vertex_gs ||
3914 ctx->stage == ngg_tess_eval_gs ||
3915 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
3916 bool stored_to_temps = store_output_to_temps(ctx, instr);
3917 if (!stored_to_temps) {
3918 fprintf(stderr, "Unimplemented output offset instruction:\n");
3919 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
3920 fprintf(stderr, "\n");
3921 abort();
3922 }
3923 } else if (ctx->stage == vertex_es ||
3924 ctx->stage == vertex_ls ||
3925 ctx->stage == tess_eval_es ||
3926 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3927 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3928 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
3929 visit_store_ls_or_es_output(ctx, instr);
3930 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
3931 visit_store_tcs_output(ctx, instr, false);
3932 } else {
3933 unreachable("Shader stage not implemented");
3934 }
3935 }
3936
3937 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
3938 {
3939 visit_load_tcs_output(ctx, instr, false);
3940 }
3941
3942 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
3943 {
3944 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
3945 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
3946
3947 Builder bld(ctx->program, ctx->block);
3948 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
3949 if (ctx->program->has_16bank_lds)
3950 interp_p1.instr->operands[0].setLateKill(true);
3951 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
3952 }
3953
3954 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
3955 {
3956 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
3957 for (unsigned i = 0; i < num_components; i++)
3958 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
3959 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
3960 assert(num_components == 4);
3961 Builder bld(ctx->program, ctx->block);
3962 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
3963 }
3964
3965 for (Operand& op : vec->operands)
3966 op = op.isUndefined() ? Operand(0u) : op;
3967
3968 vec->definitions[0] = Definition(dst);
3969 ctx->block->instructions.emplace_back(std::move(vec));
3970 emit_split_vector(ctx, dst, num_components);
3971 return;
3972 }
3973
3974 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
3975 {
3976 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3977 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
3978 unsigned idx = nir_intrinsic_base(instr);
3979 unsigned component = nir_intrinsic_component(instr);
3980 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3981
3982 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
3983 if (offset) {
3984 assert(offset->u32 == 0);
3985 } else {
3986 /* the lower 15bit of the prim_mask contain the offset into LDS
3987 * while the upper bits contain the number of prims */
3988 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
3989 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3990 Builder bld(ctx->program, ctx->block);
3991 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3992 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3993 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3994 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3995 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3996 }
3997
3998 if (instr->dest.ssa.num_components == 1) {
3999 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4000 } else {
4001 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4002 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4003 {
4004 Temp tmp = {ctx->program->allocateId(), v1};
4005 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4006 vec->operands[i] = Operand(tmp);
4007 }
4008 vec->definitions[0] = Definition(dst);
4009 ctx->block->instructions.emplace_back(std::move(vec));
4010 }
4011 }
4012
4013 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4014 unsigned offset, unsigned stride, unsigned channels)
4015 {
4016 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4017 if (vtx_info->chan_byte_size != 4 && channels == 3)
4018 return false;
4019 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4020 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4021 }
4022
4023 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4024 unsigned offset, unsigned stride, unsigned *channels)
4025 {
4026 if (!vtx_info->chan_byte_size) {
4027 *channels = vtx_info->num_channels;
4028 return vtx_info->chan_format;
4029 }
4030
4031 unsigned num_channels = *channels;
4032 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4033 unsigned new_channels = num_channels + 1;
4034 /* first, assume more loads is worse and try using a larger data format */
4035 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4036 new_channels++;
4037 /* don't make the attribute potentially out-of-bounds */
4038 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4039 new_channels = 5;
4040 }
4041
4042 if (new_channels == 5) {
4043 /* then try decreasing load size (at the cost of more loads) */
4044 new_channels = *channels;
4045 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4046 new_channels--;
4047 }
4048
4049 if (new_channels < *channels)
4050 *channels = new_channels;
4051 num_channels = new_channels;
4052 }
4053
4054 switch (vtx_info->chan_format) {
4055 case V_008F0C_BUF_DATA_FORMAT_8:
4056 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4057 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4058 case V_008F0C_BUF_DATA_FORMAT_16:
4059 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4060 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4061 case V_008F0C_BUF_DATA_FORMAT_32:
4062 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4063 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4064 }
4065 unreachable("shouldn't reach here");
4066 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4067 }
4068
4069 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4070 * so we may need to fix it up. */
4071 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4072 {
4073 Builder bld(ctx->program, ctx->block);
4074
4075 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4076 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4077
4078 /* For the integer-like cases, do a natural sign extension.
4079 *
4080 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4081 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4082 * exponent.
4083 */
4084 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4085 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4086
4087 /* Convert back to the right type. */
4088 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4089 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4090 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4091 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4092 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4093 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4094 }
4095
4096 return alpha;
4097 }
4098
4099 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4100 {
4101 Builder bld(ctx->program, ctx->block);
4102 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4103 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4104
4105 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4106 if (off_instr->type != nir_instr_type_load_const) {
4107 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4108 nir_print_instr(off_instr, stderr);
4109 fprintf(stderr, "\n");
4110 }
4111 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4112
4113 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4114
4115 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4116 unsigned component = nir_intrinsic_component(instr);
4117 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4118 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4119 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4120 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4121
4122 unsigned dfmt = attrib_format & 0xf;
4123 unsigned nfmt = (attrib_format >> 4) & 0x7;
4124 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4125
4126 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4127 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4128 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4129 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4130 if (post_shuffle)
4131 num_channels = MAX2(num_channels, 3);
4132
4133 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4134 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4135
4136 Temp index;
4137 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4138 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4139 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4140 if (divisor) {
4141 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4142 if (divisor != 1) {
4143 Temp divided = bld.tmp(v1);
4144 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4145 index = bld.vadd32(bld.def(v1), start_instance, divided);
4146 } else {
4147 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4148 }
4149 } else {
4150 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4151 }
4152 } else {
4153 index = bld.vadd32(bld.def(v1),
4154 get_arg(ctx, ctx->args->ac.base_vertex),
4155 get_arg(ctx, ctx->args->ac.vertex_id));
4156 }
4157
4158 Temp channels[num_channels];
4159 unsigned channel_start = 0;
4160 bool direct_fetch = false;
4161
4162 /* skip unused channels at the start */
4163 if (vtx_info->chan_byte_size && !post_shuffle) {
4164 channel_start = ffs(mask) - 1;
4165 for (unsigned i = 0; i < channel_start; i++)
4166 channels[i] = Temp(0, s1);
4167 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4168 num_channels = 3 - (ffs(mask) - 1);
4169 }
4170
4171 /* load channels */
4172 while (channel_start < num_channels) {
4173 unsigned fetch_size = num_channels - channel_start;
4174 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4175 bool expanded = false;
4176
4177 /* use MUBUF when possible to avoid possible alignment issues */
4178 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4179 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4180 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4181 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4182 vtx_info->chan_byte_size == 4;
4183 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4184 if (!use_mubuf) {
4185 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4186 } else {
4187 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4188 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4189 fetch_size = 4;
4190 expanded = true;
4191 }
4192 }
4193
4194 Temp fetch_index = index;
4195 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4196 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4197 fetch_offset = fetch_offset % attrib_stride;
4198 }
4199
4200 Operand soffset(0u);
4201 if (fetch_offset >= 4096) {
4202 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4203 fetch_offset %= 4096;
4204 }
4205
4206 aco_opcode opcode;
4207 switch (fetch_size) {
4208 case 1:
4209 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4210 break;
4211 case 2:
4212 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4213 break;
4214 case 3:
4215 assert(ctx->options->chip_class >= GFX7 ||
4216 (!use_mubuf && ctx->options->chip_class == GFX6));
4217 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4218 break;
4219 case 4:
4220 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4221 break;
4222 default:
4223 unreachable("Unimplemented load_input vector size");
4224 }
4225
4226 Temp fetch_dst;
4227 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4228 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4229 num_channels <= 3)) {
4230 direct_fetch = true;
4231 fetch_dst = dst;
4232 } else {
4233 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4234 }
4235
4236 if (use_mubuf) {
4237 Instruction *mubuf = bld.mubuf(opcode,
4238 Definition(fetch_dst), list, fetch_index, soffset,
4239 fetch_offset, false, true).instr;
4240 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4241 } else {
4242 Instruction *mtbuf = bld.mtbuf(opcode,
4243 Definition(fetch_dst), list, fetch_index, soffset,
4244 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4245 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4246 }
4247
4248 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4249
4250 if (fetch_size == 1) {
4251 channels[channel_start] = fetch_dst;
4252 } else {
4253 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4254 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4255 }
4256
4257 channel_start += fetch_size;
4258 }
4259
4260 if (!direct_fetch) {
4261 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4262 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4263
4264 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4265 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4266 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4267
4268 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4269 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4270 unsigned num_temp = 0;
4271 for (unsigned i = 0; i < dst.size(); i++) {
4272 unsigned idx = i + component;
4273 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4274 Temp channel = channels[swizzle[idx]];
4275 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4276 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4277 vec->operands[i] = Operand(channel);
4278
4279 num_temp++;
4280 elems[i] = channel;
4281 } else if (is_float && idx == 3) {
4282 vec->operands[i] = Operand(0x3f800000u);
4283 } else if (!is_float && idx == 3) {
4284 vec->operands[i] = Operand(1u);
4285 } else {
4286 vec->operands[i] = Operand(0u);
4287 }
4288 }
4289 vec->definitions[0] = Definition(dst);
4290 ctx->block->instructions.emplace_back(std::move(vec));
4291 emit_split_vector(ctx, dst, dst.size());
4292
4293 if (num_temp == dst.size())
4294 ctx->allocated_vec.emplace(dst.id(), elems);
4295 }
4296 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4297 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4298 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4299 if (off_instr->type != nir_instr_type_load_const ||
4300 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4301 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4302 nir_print_instr(off_instr, stderr);
4303 fprintf(stderr, "\n");
4304 }
4305
4306 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4307 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4308 if (offset) {
4309 assert(offset->u32 == 0);
4310 } else {
4311 /* the lower 15bit of the prim_mask contain the offset into LDS
4312 * while the upper bits contain the number of prims */
4313 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4314 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4315 Builder bld(ctx->program, ctx->block);
4316 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4317 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4318 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4319 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4320 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4321 }
4322
4323 unsigned idx = nir_intrinsic_base(instr);
4324 unsigned component = nir_intrinsic_component(instr);
4325 unsigned vertex_id = 2; /* P0 */
4326
4327 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4328 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4329 switch (src0->u32) {
4330 case 0:
4331 vertex_id = 2; /* P0 */
4332 break;
4333 case 1:
4334 vertex_id = 0; /* P10 */
4335 break;
4336 case 2:
4337 vertex_id = 1; /* P20 */
4338 break;
4339 default:
4340 unreachable("invalid vertex index");
4341 }
4342 }
4343
4344 if (dst.size() == 1) {
4345 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4346 } else {
4347 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4348 for (unsigned i = 0; i < dst.size(); i++)
4349 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4350 vec->definitions[0] = Definition(dst);
4351 bld.insert(std::move(vec));
4352 }
4353
4354 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4355 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4356 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4357 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4358 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4359
4360 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4361 } else {
4362 unreachable("Shader stage not implemented");
4363 }
4364 }
4365
4366 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4367 {
4368 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4369
4370 Builder bld(ctx->program, ctx->block);
4371 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4372 Temp vertex_offset;
4373
4374 if (!nir_src_is_const(*vertex_src)) {
4375 /* better code could be created, but this case probably doesn't happen
4376 * much in practice */
4377 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4378 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4379 Temp elem;
4380
4381 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4382 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4383 if (i % 2u)
4384 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4385 } else {
4386 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4387 }
4388
4389 if (vertex_offset.id()) {
4390 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4391 Operand(i), indirect_vertex);
4392 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4393 } else {
4394 vertex_offset = elem;
4395 }
4396 }
4397
4398 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4399 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4400 } else {
4401 unsigned vertex = nir_src_as_uint(*vertex_src);
4402 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4403 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4404 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4405 Operand((vertex % 2u) * 16u), Operand(16u));
4406 else
4407 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4408 }
4409
4410 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4411 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4412 return offset_mul(ctx, offs, 4u);
4413 }
4414
4415 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4416 {
4417 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4418
4419 Builder bld(ctx->program, ctx->block);
4420 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4421 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4422
4423 if (ctx->stage == geometry_gs) {
4424 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4425 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4426 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);
4427 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4428 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4429 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4430 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4431 } else {
4432 unreachable("Unsupported GS stage.");
4433 }
4434 }
4435
4436 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4437 {
4438 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4439
4440 Builder bld(ctx->program, ctx->block);
4441 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4442
4443 if (load_input_from_temps(ctx, instr, dst))
4444 return;
4445
4446 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4447 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4448 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4449
4450 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4451 }
4452
4453 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4454 {
4455 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4456
4457 Builder bld(ctx->program, ctx->block);
4458
4459 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4460 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4461 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4462
4463 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4464 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4465
4466 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4467 }
4468
4469 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4470 {
4471 switch (ctx->shader->info.stage) {
4472 case MESA_SHADER_GEOMETRY:
4473 visit_load_gs_per_vertex_input(ctx, instr);
4474 break;
4475 case MESA_SHADER_TESS_CTRL:
4476 visit_load_tcs_per_vertex_input(ctx, instr);
4477 break;
4478 case MESA_SHADER_TESS_EVAL:
4479 visit_load_tes_per_vertex_input(ctx, instr);
4480 break;
4481 default:
4482 unreachable("Unimplemented shader stage");
4483 }
4484 }
4485
4486 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4487 {
4488 visit_load_tcs_output(ctx, instr, true);
4489 }
4490
4491 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4492 {
4493 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4494 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4495
4496 visit_store_tcs_output(ctx, instr, true);
4497 }
4498
4499 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
4500 {
4501 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4502
4503 Builder bld(ctx->program, ctx->block);
4504 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4505
4506 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
4507 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
4508 Operand tes_w(0u);
4509
4510 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
4511 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
4512 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
4513 tes_w = Operand(tmp);
4514 }
4515
4516 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
4517 emit_split_vector(ctx, tess_coord, 3);
4518 }
4519
4520 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
4521 {
4522 if (ctx->program->info->need_indirect_descriptor_sets) {
4523 Builder bld(ctx->program, ctx->block);
4524 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
4525 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
4526 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
4527 }
4528
4529 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
4530 }
4531
4532
4533 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
4534 {
4535 Builder bld(ctx->program, ctx->block);
4536 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
4537 if (!ctx->divergent_vals[instr->dest.ssa.index])
4538 index = bld.as_uniform(index);
4539 unsigned desc_set = nir_intrinsic_desc_set(instr);
4540 unsigned binding = nir_intrinsic_binding(instr);
4541
4542 Temp desc_ptr;
4543 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
4544 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
4545 unsigned offset = layout->binding[binding].offset;
4546 unsigned stride;
4547 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
4548 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
4549 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
4550 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
4551 offset = pipeline_layout->push_constant_size + 16 * idx;
4552 stride = 16;
4553 } else {
4554 desc_ptr = load_desc_ptr(ctx, desc_set);
4555 stride = layout->binding[binding].size;
4556 }
4557
4558 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
4559 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
4560 if (stride != 1) {
4561 if (nir_const_index) {
4562 const_index = const_index * stride;
4563 } else if (index.type() == RegType::vgpr) {
4564 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
4565 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
4566 } else {
4567 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
4568 }
4569 }
4570 if (offset) {
4571 if (nir_const_index) {
4572 const_index = const_index + offset;
4573 } else if (index.type() == RegType::vgpr) {
4574 index = bld.vadd32(bld.def(v1), Operand(offset), index);
4575 } else {
4576 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
4577 }
4578 }
4579
4580 if (nir_const_index && const_index == 0) {
4581 index = desc_ptr;
4582 } else if (index.type() == RegType::vgpr) {
4583 index = bld.vadd32(bld.def(v1),
4584 nir_const_index ? Operand(const_index) : Operand(index),
4585 Operand(desc_ptr));
4586 } else {
4587 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
4588 nir_const_index ? Operand(const_index) : Operand(index),
4589 Operand(desc_ptr));
4590 }
4591
4592 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
4593 }
4594
4595 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
4596 Temp dst, Temp rsrc, Temp offset, int byte_align,
4597 bool glc=false, bool readonly=true)
4598 {
4599 Builder bld(ctx->program, ctx->block);
4600 bool dlc = glc && ctx->options->chip_class >= GFX10;
4601 unsigned num_bytes = num_components * component_size;
4602
4603 aco_opcode op;
4604 if (dst.type() == RegType::vgpr || ((ctx->options->chip_class < GFX8 || component_size < 4) && !readonly)) {
4605 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4606 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4607 unsigned const_offset = 0;
4608
4609 /* for small bit sizes add buffer for unaligned loads */
4610 if (byte_align) {
4611 if (num_bytes > 2)
4612 num_bytes += byte_align == -1 ? 4 - component_size : byte_align;
4613 else
4614 byte_align = 0;
4615 }
4616
4617 Temp lower = Temp();
4618 if (num_bytes > 16) {
4619 assert(num_components == 3 || num_components == 4);
4620 op = aco_opcode::buffer_load_dwordx4;
4621 lower = bld.tmp(v4);
4622 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4623 mubuf->definitions[0] = Definition(lower);
4624 mubuf->operands[0] = Operand(rsrc);
4625 mubuf->operands[1] = vaddr;
4626 mubuf->operands[2] = soffset;
4627 mubuf->offen = (offset.type() == RegType::vgpr);
4628 mubuf->glc = glc;
4629 mubuf->dlc = dlc;
4630 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4631 mubuf->can_reorder = readonly;
4632 bld.insert(std::move(mubuf));
4633 emit_split_vector(ctx, lower, 2);
4634 num_bytes -= 16;
4635 const_offset = 16;
4636 } else if (num_bytes == 12 && ctx->options->chip_class == GFX6) {
4637 /* GFX6 doesn't support loading vec3, expand to vec4. */
4638 num_bytes = 16;
4639 }
4640
4641 switch (num_bytes) {
4642 case 1:
4643 op = aco_opcode::buffer_load_ubyte;
4644 break;
4645 case 2:
4646 op = aco_opcode::buffer_load_ushort;
4647 break;
4648 case 3:
4649 case 4:
4650 op = aco_opcode::buffer_load_dword;
4651 break;
4652 case 5:
4653 case 6:
4654 case 7:
4655 case 8:
4656 op = aco_opcode::buffer_load_dwordx2;
4657 break;
4658 case 10:
4659 case 12:
4660 assert(ctx->options->chip_class > GFX6);
4661 op = aco_opcode::buffer_load_dwordx3;
4662 break;
4663 case 16:
4664 op = aco_opcode::buffer_load_dwordx4;
4665 break;
4666 default:
4667 unreachable("Load SSBO not implemented for this size.");
4668 }
4669 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4670 mubuf->operands[0] = Operand(rsrc);
4671 mubuf->operands[1] = vaddr;
4672 mubuf->operands[2] = soffset;
4673 mubuf->offen = (offset.type() == RegType::vgpr);
4674 mubuf->glc = glc;
4675 mubuf->dlc = dlc;
4676 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4677 mubuf->can_reorder = readonly;
4678 mubuf->offset = const_offset;
4679 aco_ptr<Instruction> instr = std::move(mubuf);
4680
4681 if (component_size < 4) {
4682 Temp vec = num_bytes <= 4 ? bld.tmp(v1) : num_bytes <= 8 ? bld.tmp(v2) : bld.tmp(v3);
4683 instr->definitions[0] = Definition(vec);
4684 bld.insert(std::move(instr));
4685
4686 if (byte_align == -1 || (byte_align && dst.type() == RegType::sgpr)) {
4687 Operand align = byte_align == -1 ? Operand(offset) : Operand((uint32_t)byte_align);
4688 Temp tmp[3] = {vec, vec, vec};
4689
4690 if (vec.size() == 3) {
4691 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
4692 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
4693 } else if (vec.size() == 2) {
4694 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
4695 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
4696 }
4697 for (unsigned i = 0; i < dst.size(); i++)
4698 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], align);
4699
4700 vec = tmp[0];
4701 if (dst.size() == 2)
4702 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
4703
4704 byte_align = 0;
4705 }
4706
4707 if (dst.type() == RegType::vgpr && num_components == 1) {
4708 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), vec, Operand(byte_align / component_size));
4709 } else {
4710 trim_subdword_vector(ctx, vec, dst, 4 * vec.size() / component_size, ((1 << num_components) - 1) << byte_align / component_size);
4711 }
4712
4713 return;
4714
4715 } else if (dst.size() > 4) {
4716 assert(lower != Temp());
4717 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
4718 instr->definitions[0] = Definition(upper);
4719 bld.insert(std::move(instr));
4720 if (dst.size() == 8)
4721 emit_split_vector(ctx, upper, 2);
4722 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
4723 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
4724 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
4725 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
4726 if (dst.size() == 8)
4727 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
4728 } else if (dst.size() == 3 && ctx->options->chip_class == GFX6) {
4729 Temp vec = bld.tmp(v4);
4730 instr->definitions[0] = Definition(vec);
4731 bld.insert(std::move(instr));
4732 emit_split_vector(ctx, vec, 4);
4733
4734 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
4735 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
4736 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
4737 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
4738 }
4739
4740 if (dst.type() == RegType::sgpr) {
4741 Temp vec = bld.tmp(RegType::vgpr, dst.size());
4742 instr->definitions[0] = Definition(vec);
4743 bld.insert(std::move(instr));
4744 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
4745 } else {
4746 instr->definitions[0] = Definition(dst);
4747 bld.insert(std::move(instr));
4748 emit_split_vector(ctx, dst, num_components);
4749 }
4750 } else {
4751 /* for small bit sizes add buffer for unaligned loads */
4752 if (byte_align)
4753 num_bytes += byte_align == -1 ? 4 - component_size : byte_align;
4754
4755 switch (num_bytes) {
4756 case 1:
4757 case 2:
4758 case 3:
4759 case 4:
4760 op = aco_opcode::s_buffer_load_dword;
4761 break;
4762 case 5:
4763 case 6:
4764 case 7:
4765 case 8:
4766 op = aco_opcode::s_buffer_load_dwordx2;
4767 break;
4768 case 10:
4769 case 12:
4770 case 16:
4771 op = aco_opcode::s_buffer_load_dwordx4;
4772 break;
4773 case 24:
4774 case 32:
4775 op = aco_opcode::s_buffer_load_dwordx8;
4776 break;
4777 default:
4778 unreachable("Load SSBO not implemented for this size.");
4779 }
4780 offset = bld.as_uniform(offset);
4781 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
4782 load->operands[0] = Operand(rsrc);
4783 load->operands[1] = Operand(offset);
4784 assert(load->operands[1].getTemp().type() == RegType::sgpr);
4785 load->definitions[0] = Definition(dst);
4786 load->glc = glc;
4787 load->dlc = dlc;
4788 load->barrier = readonly ? barrier_none : barrier_buffer;
4789 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
4790 assert(ctx->options->chip_class >= GFX8 || !glc);
4791
4792 /* adjust misaligned small bit size loads */
4793 if (byte_align) {
4794 Temp vec = num_bytes <= 4 ? bld.tmp(s1) : num_bytes <= 8 ? bld.tmp(s2) : bld.tmp(s4);
4795 load->definitions[0] = Definition(vec);
4796 bld.insert(std::move(load));
4797 Operand byte_offset = byte_align > 0 ? Operand(uint32_t(byte_align)) : Operand(offset);
4798 byte_align_scalar(ctx, vec, byte_offset, dst);
4799
4800 /* trim vector */
4801 } else if (dst.size() == 3) {
4802 Temp vec = bld.tmp(s4);
4803 load->definitions[0] = Definition(vec);
4804 bld.insert(std::move(load));
4805 emit_split_vector(ctx, vec, 4);
4806
4807 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4808 emit_extract_vector(ctx, vec, 0, s1),
4809 emit_extract_vector(ctx, vec, 1, s1),
4810 emit_extract_vector(ctx, vec, 2, s1));
4811 } else if (dst.size() == 6) {
4812 Temp vec = bld.tmp(s8);
4813 load->definitions[0] = Definition(vec);
4814 bld.insert(std::move(load));
4815 emit_split_vector(ctx, vec, 4);
4816
4817 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4818 emit_extract_vector(ctx, vec, 0, s2),
4819 emit_extract_vector(ctx, vec, 1, s2),
4820 emit_extract_vector(ctx, vec, 2, s2));
4821 } else {
4822 bld.insert(std::move(load));
4823 }
4824 emit_split_vector(ctx, dst, num_components);
4825 }
4826 }
4827
4828 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
4829 {
4830 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4831 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
4832
4833 Builder bld(ctx->program, ctx->block);
4834
4835 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
4836 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
4837 unsigned binding = nir_intrinsic_binding(idx_instr);
4838 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
4839
4840 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
4841 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4842 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4843 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4844 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4845 if (ctx->options->chip_class >= GFX10) {
4846 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4847 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4848 S_008F0C_RESOURCE_LEVEL(1);
4849 } else {
4850 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4851 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4852 }
4853 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
4854 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
4855 Operand(0xFFFFFFFFu),
4856 Operand(desc_type));
4857 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4858 rsrc, upper_dwords);
4859 } else {
4860 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
4861 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4862 }
4863 unsigned size = instr->dest.ssa.bit_size / 8;
4864 int byte_align = 0;
4865 if (size < 4) {
4866 unsigned align_mul = nir_intrinsic_align_mul(instr);
4867 unsigned align_offset = nir_intrinsic_align_offset(instr);
4868 byte_align = align_mul % 4 == 0 ? align_offset : -1;
4869 }
4870 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), byte_align);
4871 }
4872
4873 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4874 {
4875 Builder bld(ctx->program, ctx->block);
4876 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4877 unsigned offset = nir_intrinsic_base(instr);
4878 unsigned count = instr->dest.ssa.num_components;
4879 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
4880
4881 if (index_cv && instr->dest.ssa.bit_size == 32) {
4882 unsigned start = (offset + index_cv->u32) / 4u;
4883 start -= ctx->args->ac.base_inline_push_consts;
4884 if (start + count <= ctx->args->ac.num_inline_push_consts) {
4885 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4886 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4887 for (unsigned i = 0; i < count; ++i) {
4888 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
4889 vec->operands[i] = Operand{elems[i]};
4890 }
4891 vec->definitions[0] = Definition(dst);
4892 ctx->block->instructions.emplace_back(std::move(vec));
4893 ctx->allocated_vec.emplace(dst.id(), elems);
4894 return;
4895 }
4896 }
4897
4898 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
4899 if (offset != 0) // TODO check if index != 0 as well
4900 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
4901 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
4902 Temp vec = dst;
4903 bool trim = false;
4904 bool aligned = true;
4905
4906 if (instr->dest.ssa.bit_size == 8) {
4907 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
4908 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
4909 if (!aligned)
4910 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
4911 } else if (instr->dest.ssa.bit_size == 16) {
4912 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
4913 if (!aligned)
4914 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
4915 }
4916
4917 aco_opcode op;
4918
4919 switch (vec.size()) {
4920 case 1:
4921 op = aco_opcode::s_load_dword;
4922 break;
4923 case 2:
4924 op = aco_opcode::s_load_dwordx2;
4925 break;
4926 case 3:
4927 vec = bld.tmp(s4);
4928 trim = true;
4929 case 4:
4930 op = aco_opcode::s_load_dwordx4;
4931 break;
4932 case 6:
4933 vec = bld.tmp(s8);
4934 trim = true;
4935 case 8:
4936 op = aco_opcode::s_load_dwordx8;
4937 break;
4938 default:
4939 unreachable("unimplemented or forbidden load_push_constant.");
4940 }
4941
4942 bld.smem(op, Definition(vec), ptr, index);
4943
4944 if (!aligned) {
4945 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
4946 byte_align_scalar(ctx, vec, byte_offset, dst);
4947 return;
4948 }
4949
4950 if (trim) {
4951 emit_split_vector(ctx, vec, 4);
4952 RegClass rc = dst.size() == 3 ? s1 : s2;
4953 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4954 emit_extract_vector(ctx, vec, 0, rc),
4955 emit_extract_vector(ctx, vec, 1, rc),
4956 emit_extract_vector(ctx, vec, 2, rc));
4957
4958 }
4959 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4960 }
4961
4962 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4963 {
4964 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4965
4966 Builder bld(ctx->program, ctx->block);
4967
4968 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4969 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4970 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4971 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4972 if (ctx->options->chip_class >= GFX10) {
4973 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4974 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4975 S_008F0C_RESOURCE_LEVEL(1);
4976 } else {
4977 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4978 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4979 }
4980
4981 unsigned base = nir_intrinsic_base(instr);
4982 unsigned range = nir_intrinsic_range(instr);
4983
4984 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
4985 if (base && offset.type() == RegType::sgpr)
4986 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
4987 else if (base && offset.type() == RegType::vgpr)
4988 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
4989
4990 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4991 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
4992 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
4993 Operand(desc_type));
4994 unsigned size = instr->dest.ssa.bit_size / 8;
4995 // TODO: get alignment information for subdword constants
4996 unsigned byte_align = size < 4 ? -1 : 0;
4997 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, byte_align);
4998 }
4999
5000 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5001 {
5002 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5003 ctx->cf_info.exec_potentially_empty_discard = true;
5004
5005 ctx->program->needs_exact = true;
5006
5007 // TODO: optimize uniform conditions
5008 Builder bld(ctx->program, ctx->block);
5009 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5010 assert(src.regClass() == bld.lm);
5011 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5012 bld.pseudo(aco_opcode::p_discard_if, src);
5013 ctx->block->kind |= block_kind_uses_discard_if;
5014 return;
5015 }
5016
5017 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5018 {
5019 Builder bld(ctx->program, ctx->block);
5020
5021 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5022 ctx->cf_info.exec_potentially_empty_discard = true;
5023
5024 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5025 ctx->cf_info.parent_loop.has_divergent_continue;
5026
5027 if (ctx->block->loop_nest_depth &&
5028 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5029 /* we handle discards the same way as jump instructions */
5030 append_logical_end(ctx->block);
5031
5032 /* in loops, discard behaves like break */
5033 Block *linear_target = ctx->cf_info.parent_loop.exit;
5034 ctx->block->kind |= block_kind_discard;
5035
5036 if (!divergent) {
5037 /* uniform discard - loop ends here */
5038 assert(nir_instr_is_last(&instr->instr));
5039 ctx->block->kind |= block_kind_uniform;
5040 ctx->cf_info.has_branch = true;
5041 bld.branch(aco_opcode::p_branch);
5042 add_linear_edge(ctx->block->index, linear_target);
5043 return;
5044 }
5045
5046 /* we add a break right behind the discard() instructions */
5047 ctx->block->kind |= block_kind_break;
5048 unsigned idx = ctx->block->index;
5049
5050 ctx->cf_info.parent_loop.has_divergent_branch = true;
5051 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5052
5053 /* remove critical edges from linear CFG */
5054 bld.branch(aco_opcode::p_branch);
5055 Block* break_block = ctx->program->create_and_insert_block();
5056 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5057 break_block->kind |= block_kind_uniform;
5058 add_linear_edge(idx, break_block);
5059 add_linear_edge(break_block->index, linear_target);
5060 bld.reset(break_block);
5061 bld.branch(aco_opcode::p_branch);
5062
5063 Block* continue_block = ctx->program->create_and_insert_block();
5064 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5065 add_linear_edge(idx, continue_block);
5066 append_logical_start(continue_block);
5067 ctx->block = continue_block;
5068
5069 return;
5070 }
5071
5072 /* it can currently happen that NIR doesn't remove the unreachable code */
5073 if (!nir_instr_is_last(&instr->instr)) {
5074 ctx->program->needs_exact = true;
5075 /* save exec somewhere temporarily so that it doesn't get
5076 * overwritten before the discard from outer exec masks */
5077 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5078 bld.pseudo(aco_opcode::p_discard_if, cond);
5079 ctx->block->kind |= block_kind_uses_discard_if;
5080 return;
5081 }
5082
5083 /* This condition is incorrect for uniformly branched discards in a loop
5084 * predicated by a divergent condition, but the above code catches that case
5085 * and the discard would end up turning into a discard_if.
5086 * For example:
5087 * if (divergent) {
5088 * while (...) {
5089 * if (uniform) {
5090 * discard;
5091 * }
5092 * }
5093 * }
5094 */
5095 if (!ctx->cf_info.parent_if.is_divergent) {
5096 /* program just ends here */
5097 ctx->block->kind |= block_kind_uniform;
5098 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5099 0 /* enabled mask */, 9 /* dest */,
5100 false /* compressed */, true/* done */, true /* valid mask */);
5101 bld.sopp(aco_opcode::s_endpgm);
5102 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5103 } else {
5104 ctx->block->kind |= block_kind_discard;
5105 /* branch and linear edge is added by visit_if() */
5106 }
5107 }
5108
5109 enum aco_descriptor_type {
5110 ACO_DESC_IMAGE,
5111 ACO_DESC_FMASK,
5112 ACO_DESC_SAMPLER,
5113 ACO_DESC_BUFFER,
5114 ACO_DESC_PLANE_0,
5115 ACO_DESC_PLANE_1,
5116 ACO_DESC_PLANE_2,
5117 };
5118
5119 static bool
5120 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5121 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5122 return false;
5123 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5124 return dim == ac_image_cube ||
5125 dim == ac_image_1darray ||
5126 dim == ac_image_2darray ||
5127 dim == ac_image_2darraymsaa;
5128 }
5129
5130 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5131 enum aco_descriptor_type desc_type,
5132 const nir_tex_instr *tex_instr, bool image, bool write)
5133 {
5134 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5135 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5136 if (it != ctx->tex_desc.end())
5137 return it->second;
5138 */
5139 Temp index = Temp();
5140 bool index_set = false;
5141 unsigned constant_index = 0;
5142 unsigned descriptor_set;
5143 unsigned base_index;
5144 Builder bld(ctx->program, ctx->block);
5145
5146 if (!deref_instr) {
5147 assert(tex_instr && !image);
5148 descriptor_set = 0;
5149 base_index = tex_instr->sampler_index;
5150 } else {
5151 while(deref_instr->deref_type != nir_deref_type_var) {
5152 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5153 if (!array_size)
5154 array_size = 1;
5155
5156 assert(deref_instr->deref_type == nir_deref_type_array);
5157 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5158 if (const_value) {
5159 constant_index += array_size * const_value->u32;
5160 } else {
5161 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5162 if (indirect.type() == RegType::vgpr)
5163 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5164
5165 if (array_size != 1)
5166 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5167
5168 if (!index_set) {
5169 index = indirect;
5170 index_set = true;
5171 } else {
5172 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5173 }
5174 }
5175
5176 deref_instr = nir_src_as_deref(deref_instr->parent);
5177 }
5178 descriptor_set = deref_instr->var->data.descriptor_set;
5179 base_index = deref_instr->var->data.binding;
5180 }
5181
5182 Temp list = load_desc_ptr(ctx, descriptor_set);
5183 list = convert_pointer_to_64_bit(ctx, list);
5184
5185 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5186 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5187 unsigned offset = binding->offset;
5188 unsigned stride = binding->size;
5189 aco_opcode opcode;
5190 RegClass type;
5191
5192 assert(base_index < layout->binding_count);
5193
5194 switch (desc_type) {
5195 case ACO_DESC_IMAGE:
5196 type = s8;
5197 opcode = aco_opcode::s_load_dwordx8;
5198 break;
5199 case ACO_DESC_FMASK:
5200 type = s8;
5201 opcode = aco_opcode::s_load_dwordx8;
5202 offset += 32;
5203 break;
5204 case ACO_DESC_SAMPLER:
5205 type = s4;
5206 opcode = aco_opcode::s_load_dwordx4;
5207 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5208 offset += radv_combined_image_descriptor_sampler_offset(binding);
5209 break;
5210 case ACO_DESC_BUFFER:
5211 type = s4;
5212 opcode = aco_opcode::s_load_dwordx4;
5213 break;
5214 case ACO_DESC_PLANE_0:
5215 case ACO_DESC_PLANE_1:
5216 type = s8;
5217 opcode = aco_opcode::s_load_dwordx8;
5218 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5219 break;
5220 case ACO_DESC_PLANE_2:
5221 type = s4;
5222 opcode = aco_opcode::s_load_dwordx4;
5223 offset += 64;
5224 break;
5225 default:
5226 unreachable("invalid desc_type\n");
5227 }
5228
5229 offset += constant_index * stride;
5230
5231 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5232 (!index_set || binding->immutable_samplers_equal)) {
5233 if (binding->immutable_samplers_equal)
5234 constant_index = 0;
5235
5236 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5237 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5238 Operand(samplers[constant_index * 4 + 0]),
5239 Operand(samplers[constant_index * 4 + 1]),
5240 Operand(samplers[constant_index * 4 + 2]),
5241 Operand(samplers[constant_index * 4 + 3]));
5242 }
5243
5244 Operand off;
5245 if (!index_set) {
5246 off = bld.copy(bld.def(s1), Operand(offset));
5247 } else {
5248 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5249 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5250 }
5251
5252 Temp res = bld.smem(opcode, bld.def(type), list, off);
5253
5254 if (desc_type == ACO_DESC_PLANE_2) {
5255 Temp components[8];
5256 for (unsigned i = 0; i < 8; i++)
5257 components[i] = bld.tmp(s1);
5258 bld.pseudo(aco_opcode::p_split_vector,
5259 Definition(components[0]),
5260 Definition(components[1]),
5261 Definition(components[2]),
5262 Definition(components[3]),
5263 res);
5264
5265 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5266 bld.pseudo(aco_opcode::p_split_vector,
5267 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5268 Definition(components[4]),
5269 Definition(components[5]),
5270 Definition(components[6]),
5271 Definition(components[7]),
5272 desc2);
5273
5274 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5275 components[0], components[1], components[2], components[3],
5276 components[4], components[5], components[6], components[7]);
5277 }
5278
5279 return res;
5280 }
5281
5282 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5283 {
5284 switch (dim) {
5285 case GLSL_SAMPLER_DIM_BUF:
5286 return 1;
5287 case GLSL_SAMPLER_DIM_1D:
5288 return array ? 2 : 1;
5289 case GLSL_SAMPLER_DIM_2D:
5290 return array ? 3 : 2;
5291 case GLSL_SAMPLER_DIM_MS:
5292 return array ? 4 : 3;
5293 case GLSL_SAMPLER_DIM_3D:
5294 case GLSL_SAMPLER_DIM_CUBE:
5295 return 3;
5296 case GLSL_SAMPLER_DIM_RECT:
5297 case GLSL_SAMPLER_DIM_SUBPASS:
5298 return 2;
5299 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5300 return 3;
5301 default:
5302 break;
5303 }
5304 return 0;
5305 }
5306
5307
5308 /* Adjust the sample index according to FMASK.
5309 *
5310 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5311 * which is the identity mapping. Each nibble says which physical sample
5312 * should be fetched to get that sample.
5313 *
5314 * For example, 0x11111100 means there are only 2 samples stored and
5315 * the second sample covers 3/4 of the pixel. When reading samples 0
5316 * and 1, return physical sample 0 (determined by the first two 0s
5317 * in FMASK), otherwise return physical sample 1.
5318 *
5319 * The sample index should be adjusted as follows:
5320 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5321 */
5322 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5323 {
5324 Builder bld(ctx->program, ctx->block);
5325 Temp fmask = bld.tmp(v1);
5326 unsigned dim = ctx->options->chip_class >= GFX10
5327 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5328 : 0;
5329
5330 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5331 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5332 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5333 load->operands[0] = Operand(fmask_desc_ptr);
5334 load->operands[1] = Operand(s4); /* no sampler */
5335 load->operands[2] = Operand(coord);
5336 load->definitions[0] = Definition(fmask);
5337 load->glc = false;
5338 load->dlc = false;
5339 load->dmask = 0x1;
5340 load->unrm = true;
5341 load->da = da;
5342 load->dim = dim;
5343 load->can_reorder = true; /* fmask images shouldn't be modified */
5344 ctx->block->instructions.emplace_back(std::move(load));
5345
5346 Operand sample_index4;
5347 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
5348 sample_index4 = Operand(sample_index.constantValue() << 2);
5349 } else if (sample_index.regClass() == s1) {
5350 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5351 } else {
5352 assert(sample_index.regClass() == v1);
5353 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5354 }
5355
5356 Temp final_sample;
5357 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5358 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5359 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5360 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5361 else
5362 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5363
5364 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5365 * resource descriptor is 0 (invalid),
5366 */
5367 Temp compare = bld.tmp(bld.lm);
5368 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5369 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5370
5371 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5372
5373 /* Replace the MSAA sample index. */
5374 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5375 }
5376
5377 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5378 {
5379
5380 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5381 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5382 bool is_array = glsl_sampler_type_is_array(type);
5383 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5384 assert(!add_frag_pos && "Input attachments should be lowered.");
5385 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5386 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5387 int count = image_type_to_components_count(dim, is_array);
5388 std::vector<Temp> coords(count);
5389 Builder bld(ctx->program, ctx->block);
5390
5391 if (is_ms) {
5392 count--;
5393 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5394 /* get sample index */
5395 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5396 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5397 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5398 std::vector<Temp> fmask_load_address;
5399 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5400 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5401
5402 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5403 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5404 } else {
5405 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5406 }
5407 }
5408
5409 if (gfx9_1d) {
5410 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5411 coords.resize(coords.size() + 1);
5412 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5413 if (is_array)
5414 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5415 } else {
5416 for (int i = 0; i < count; i++)
5417 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5418 }
5419
5420 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5421 instr->intrinsic == nir_intrinsic_image_deref_store) {
5422 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5423 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5424
5425 if (!level_zero)
5426 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5427 }
5428
5429 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5430 for (unsigned i = 0; i < coords.size(); i++)
5431 vec->operands[i] = Operand(coords[i]);
5432 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5433 vec->definitions[0] = Definition(res);
5434 ctx->block->instructions.emplace_back(std::move(vec));
5435 return res;
5436 }
5437
5438
5439 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5440 {
5441 Builder bld(ctx->program, ctx->block);
5442 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5443 const struct glsl_type *type = glsl_without_array(var->type);
5444 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5445 bool is_array = glsl_sampler_type_is_array(type);
5446 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5447
5448 if (dim == GLSL_SAMPLER_DIM_BUF) {
5449 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5450 unsigned num_channels = util_last_bit(mask);
5451 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5452 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5453
5454 aco_opcode opcode;
5455 switch (num_channels) {
5456 case 1:
5457 opcode = aco_opcode::buffer_load_format_x;
5458 break;
5459 case 2:
5460 opcode = aco_opcode::buffer_load_format_xy;
5461 break;
5462 case 3:
5463 opcode = aco_opcode::buffer_load_format_xyz;
5464 break;
5465 case 4:
5466 opcode = aco_opcode::buffer_load_format_xyzw;
5467 break;
5468 default:
5469 unreachable(">4 channel buffer image load");
5470 }
5471 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5472 load->operands[0] = Operand(rsrc);
5473 load->operands[1] = Operand(vindex);
5474 load->operands[2] = Operand((uint32_t) 0);
5475 Temp tmp;
5476 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5477 tmp = dst;
5478 else
5479 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5480 load->definitions[0] = Definition(tmp);
5481 load->idxen = true;
5482 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5483 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5484 load->barrier = barrier_image;
5485 ctx->block->instructions.emplace_back(std::move(load));
5486
5487 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5488 return;
5489 }
5490
5491 Temp coords = get_image_coords(ctx, instr, type);
5492 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5493
5494 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5495 unsigned num_components = util_bitcount(dmask);
5496 Temp tmp;
5497 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5498 tmp = dst;
5499 else
5500 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5501
5502 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5503 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5504
5505 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5506 load->operands[0] = Operand(resource);
5507 load->operands[1] = Operand(s4); /* no sampler */
5508 load->operands[2] = Operand(coords);
5509 load->definitions[0] = Definition(tmp);
5510 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5511 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5512 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5513 load->dmask = dmask;
5514 load->unrm = true;
5515 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5516 load->barrier = barrier_image;
5517 ctx->block->instructions.emplace_back(std::move(load));
5518
5519 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5520 return;
5521 }
5522
5523 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5524 {
5525 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5526 const struct glsl_type *type = glsl_without_array(var->type);
5527 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5528 bool is_array = glsl_sampler_type_is_array(type);
5529 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5530
5531 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5532
5533 if (dim == GLSL_SAMPLER_DIM_BUF) {
5534 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5535 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5536 aco_opcode opcode;
5537 switch (data.size()) {
5538 case 1:
5539 opcode = aco_opcode::buffer_store_format_x;
5540 break;
5541 case 2:
5542 opcode = aco_opcode::buffer_store_format_xy;
5543 break;
5544 case 3:
5545 opcode = aco_opcode::buffer_store_format_xyz;
5546 break;
5547 case 4:
5548 opcode = aco_opcode::buffer_store_format_xyzw;
5549 break;
5550 default:
5551 unreachable(">4 channel buffer image store");
5552 }
5553 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5554 store->operands[0] = Operand(rsrc);
5555 store->operands[1] = Operand(vindex);
5556 store->operands[2] = Operand((uint32_t) 0);
5557 store->operands[3] = Operand(data);
5558 store->idxen = true;
5559 store->glc = glc;
5560 store->dlc = false;
5561 store->disable_wqm = true;
5562 store->barrier = barrier_image;
5563 ctx->program->needs_exact = true;
5564 ctx->block->instructions.emplace_back(std::move(store));
5565 return;
5566 }
5567
5568 assert(data.type() == RegType::vgpr);
5569 Temp coords = get_image_coords(ctx, instr, type);
5570 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5571
5572 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5573 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5574
5575 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5576 store->operands[0] = Operand(resource);
5577 store->operands[1] = Operand(data);
5578 store->operands[2] = Operand(coords);
5579 store->glc = glc;
5580 store->dlc = false;
5581 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5582 store->dmask = (1 << data.size()) - 1;
5583 store->unrm = true;
5584 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5585 store->disable_wqm = true;
5586 store->barrier = barrier_image;
5587 ctx->program->needs_exact = true;
5588 ctx->block->instructions.emplace_back(std::move(store));
5589 return;
5590 }
5591
5592 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5593 {
5594 /* return the previous value if dest is ever used */
5595 bool return_previous = false;
5596 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5597 return_previous = true;
5598 break;
5599 }
5600 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5601 return_previous = true;
5602 break;
5603 }
5604
5605 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5606 const struct glsl_type *type = glsl_without_array(var->type);
5607 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5608 bool is_array = glsl_sampler_type_is_array(type);
5609 Builder bld(ctx->program, ctx->block);
5610
5611 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5612 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5613
5614 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5615 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5616
5617 aco_opcode buf_op, image_op;
5618 switch (instr->intrinsic) {
5619 case nir_intrinsic_image_deref_atomic_add:
5620 buf_op = aco_opcode::buffer_atomic_add;
5621 image_op = aco_opcode::image_atomic_add;
5622 break;
5623 case nir_intrinsic_image_deref_atomic_umin:
5624 buf_op = aco_opcode::buffer_atomic_umin;
5625 image_op = aco_opcode::image_atomic_umin;
5626 break;
5627 case nir_intrinsic_image_deref_atomic_imin:
5628 buf_op = aco_opcode::buffer_atomic_smin;
5629 image_op = aco_opcode::image_atomic_smin;
5630 break;
5631 case nir_intrinsic_image_deref_atomic_umax:
5632 buf_op = aco_opcode::buffer_atomic_umax;
5633 image_op = aco_opcode::image_atomic_umax;
5634 break;
5635 case nir_intrinsic_image_deref_atomic_imax:
5636 buf_op = aco_opcode::buffer_atomic_smax;
5637 image_op = aco_opcode::image_atomic_smax;
5638 break;
5639 case nir_intrinsic_image_deref_atomic_and:
5640 buf_op = aco_opcode::buffer_atomic_and;
5641 image_op = aco_opcode::image_atomic_and;
5642 break;
5643 case nir_intrinsic_image_deref_atomic_or:
5644 buf_op = aco_opcode::buffer_atomic_or;
5645 image_op = aco_opcode::image_atomic_or;
5646 break;
5647 case nir_intrinsic_image_deref_atomic_xor:
5648 buf_op = aco_opcode::buffer_atomic_xor;
5649 image_op = aco_opcode::image_atomic_xor;
5650 break;
5651 case nir_intrinsic_image_deref_atomic_exchange:
5652 buf_op = aco_opcode::buffer_atomic_swap;
5653 image_op = aco_opcode::image_atomic_swap;
5654 break;
5655 case nir_intrinsic_image_deref_atomic_comp_swap:
5656 buf_op = aco_opcode::buffer_atomic_cmpswap;
5657 image_op = aco_opcode::image_atomic_cmpswap;
5658 break;
5659 default:
5660 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5661 }
5662
5663 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5664
5665 if (dim == GLSL_SAMPLER_DIM_BUF) {
5666 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5667 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5668 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5669 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5670 mubuf->operands[0] = Operand(resource);
5671 mubuf->operands[1] = Operand(vindex);
5672 mubuf->operands[2] = Operand((uint32_t)0);
5673 mubuf->operands[3] = Operand(data);
5674 if (return_previous)
5675 mubuf->definitions[0] = Definition(dst);
5676 mubuf->offset = 0;
5677 mubuf->idxen = true;
5678 mubuf->glc = return_previous;
5679 mubuf->dlc = false; /* Not needed for atomics */
5680 mubuf->disable_wqm = true;
5681 mubuf->barrier = barrier_image;
5682 ctx->program->needs_exact = true;
5683 ctx->block->instructions.emplace_back(std::move(mubuf));
5684 return;
5685 }
5686
5687 Temp coords = get_image_coords(ctx, instr, type);
5688 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5689 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5690 mimg->operands[0] = Operand(resource);
5691 mimg->operands[1] = Operand(data);
5692 mimg->operands[2] = Operand(coords);
5693 if (return_previous)
5694 mimg->definitions[0] = Definition(dst);
5695 mimg->glc = return_previous;
5696 mimg->dlc = false; /* Not needed for atomics */
5697 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5698 mimg->dmask = (1 << data.size()) - 1;
5699 mimg->unrm = true;
5700 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5701 mimg->disable_wqm = true;
5702 mimg->barrier = barrier_image;
5703 ctx->program->needs_exact = true;
5704 ctx->block->instructions.emplace_back(std::move(mimg));
5705 return;
5706 }
5707
5708 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
5709 {
5710 if (in_elements && ctx->options->chip_class == GFX8) {
5711 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5712 Builder bld(ctx->program, ctx->block);
5713
5714 Temp size = emit_extract_vector(ctx, desc, 2, s1);
5715
5716 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
5717 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
5718
5719 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
5720 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
5721
5722 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
5723 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
5724
5725 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
5726 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
5727 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
5728 if (dst.type() == RegType::vgpr)
5729 bld.copy(Definition(dst), shr_dst);
5730
5731 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5732 } else {
5733 emit_extract_vector(ctx, desc, 2, dst);
5734 }
5735 }
5736
5737 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
5738 {
5739 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5740 const struct glsl_type *type = glsl_without_array(var->type);
5741 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5742 bool is_array = glsl_sampler_type_is_array(type);
5743 Builder bld(ctx->program, ctx->block);
5744
5745 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
5746 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
5747 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
5748 }
5749
5750 /* LOD */
5751 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
5752
5753 /* Resource */
5754 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
5755
5756 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5757
5758 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
5759 mimg->operands[0] = Operand(resource);
5760 mimg->operands[1] = Operand(s4); /* no sampler */
5761 mimg->operands[2] = Operand(lod);
5762 uint8_t& dmask = mimg->dmask;
5763 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5764 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
5765 mimg->da = glsl_sampler_type_is_array(type);
5766 mimg->can_reorder = true;
5767 Definition& def = mimg->definitions[0];
5768 ctx->block->instructions.emplace_back(std::move(mimg));
5769
5770 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
5771 glsl_sampler_type_is_array(type)) {
5772
5773 assert(instr->dest.ssa.num_components == 3);
5774 Temp tmp = {ctx->program->allocateId(), v3};
5775 def = Definition(tmp);
5776 emit_split_vector(ctx, tmp, 3);
5777
5778 /* divide 3rd value by 6 by multiplying with magic number */
5779 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
5780 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
5781
5782 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5783 emit_extract_vector(ctx, tmp, 0, v1),
5784 emit_extract_vector(ctx, tmp, 1, v1),
5785 by_6);
5786
5787 } else if (ctx->options->chip_class == GFX9 &&
5788 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
5789 glsl_sampler_type_is_array(type)) {
5790 assert(instr->dest.ssa.num_components == 2);
5791 def = Definition(dst);
5792 dmask = 0x5;
5793 } else {
5794 def = Definition(dst);
5795 }
5796
5797 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5798 }
5799
5800 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5801 {
5802 Builder bld(ctx->program, ctx->block);
5803 unsigned num_components = instr->num_components;
5804
5805 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5806 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5807 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5808
5809 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
5810 unsigned size = instr->dest.ssa.bit_size / 8;
5811 int byte_align = 0;
5812 if (size < 4) {
5813 unsigned align_mul = nir_intrinsic_align_mul(instr);
5814 unsigned align_offset = nir_intrinsic_align_offset(instr);
5815 byte_align = align_mul % 4 == 0 ? align_offset : -1;
5816 }
5817 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), byte_align, glc, false);
5818 }
5819
5820 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5821 {
5822 Builder bld(ctx->program, ctx->block);
5823 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
5824 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5825 unsigned writemask = nir_intrinsic_write_mask(instr);
5826 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
5827
5828 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5829 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5830
5831 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
5832 ctx->options->chip_class >= GFX8 &&
5833 elem_size_bytes >= 4;
5834 if (smem)
5835 offset = bld.as_uniform(offset);
5836 bool smem_nonfs = smem && ctx->stage != fragment_fs;
5837
5838 while (writemask) {
5839 int start, count;
5840 u_bit_scan_consecutive_range(&writemask, &start, &count);
5841 if (count == 3 && (smem || ctx->options->chip_class == GFX6)) {
5842 /* GFX6 doesn't support storing vec3, split it. */
5843 writemask |= 1u << (start + 2);
5844 count = 2;
5845 }
5846 int num_bytes = count * elem_size_bytes;
5847
5848 /* dword or larger stores have to be dword-aligned */
5849 if (elem_size_bytes < 4 && num_bytes > 2) {
5850 // TODO: improve alignment check of sub-dword stores
5851 unsigned count_new = 2 / elem_size_bytes;
5852 writemask |= ((1 << (count - count_new)) - 1) << (start + count_new);
5853 count = count_new;
5854 num_bytes = 2;
5855 }
5856
5857 if (num_bytes > 16) {
5858 assert(elem_size_bytes == 8);
5859 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5860 count = 2;
5861 num_bytes = 16;
5862 }
5863
5864 Temp write_data;
5865 if (elem_size_bytes < 4) {
5866 if (data.type() == RegType::sgpr) {
5867 data = as_vgpr(ctx, data);
5868 emit_split_vector(ctx, data, 4 * data.size() / elem_size_bytes);
5869 }
5870 RegClass rc = RegClass(RegType::vgpr, elem_size_bytes).as_subdword();
5871 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5872 for (int i = 0; i < count; i++)
5873 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, rc));
5874 write_data = bld.tmp(RegClass(RegType::vgpr, num_bytes).as_subdword());
5875 vec->definitions[0] = Definition(write_data);
5876 bld.insert(std::move(vec));
5877 } else if (count != instr->num_components) {
5878 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5879 for (int i = 0; i < count; i++) {
5880 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
5881 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
5882 }
5883 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
5884 vec->definitions[0] = Definition(write_data);
5885 ctx->block->instructions.emplace_back(std::move(vec));
5886 } else if (!smem && data.type() != RegType::vgpr) {
5887 assert(num_bytes % 4 == 0);
5888 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
5889 } else if (smem_nonfs && data.type() == RegType::vgpr) {
5890 assert(num_bytes % 4 == 0);
5891 write_data = bld.as_uniform(data);
5892 } else {
5893 write_data = data;
5894 }
5895
5896 aco_opcode vmem_op, smem_op = aco_opcode::last_opcode;
5897 switch (num_bytes) {
5898 case 1:
5899 vmem_op = aco_opcode::buffer_store_byte;
5900 break;
5901 case 2:
5902 vmem_op = aco_opcode::buffer_store_short;
5903 break;
5904 case 4:
5905 vmem_op = aco_opcode::buffer_store_dword;
5906 smem_op = aco_opcode::s_buffer_store_dword;
5907 break;
5908 case 8:
5909 vmem_op = aco_opcode::buffer_store_dwordx2;
5910 smem_op = aco_opcode::s_buffer_store_dwordx2;
5911 break;
5912 case 12:
5913 vmem_op = aco_opcode::buffer_store_dwordx3;
5914 assert(!smem && ctx->options->chip_class > GFX6);
5915 break;
5916 case 16:
5917 vmem_op = aco_opcode::buffer_store_dwordx4;
5918 smem_op = aco_opcode::s_buffer_store_dwordx4;
5919 break;
5920 default:
5921 unreachable("Store SSBO not implemented for this size.");
5922 }
5923 if (ctx->stage == fragment_fs)
5924 smem_op = aco_opcode::p_fs_buffer_store_smem;
5925
5926 if (smem) {
5927 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
5928 store->operands[0] = Operand(rsrc);
5929 if (start) {
5930 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5931 offset, Operand(start * elem_size_bytes));
5932 store->operands[1] = Operand(off);
5933 } else {
5934 store->operands[1] = Operand(offset);
5935 }
5936 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
5937 store->operands[1].setFixed(m0);
5938 store->operands[2] = Operand(write_data);
5939 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5940 store->dlc = false;
5941 store->disable_wqm = true;
5942 store->barrier = barrier_buffer;
5943 ctx->block->instructions.emplace_back(std::move(store));
5944 ctx->program->wb_smem_l1_on_end = true;
5945 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
5946 ctx->block->kind |= block_kind_needs_lowering;
5947 ctx->program->needs_exact = true;
5948 }
5949 } else {
5950 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
5951 store->operands[0] = Operand(rsrc);
5952 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5953 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5954 store->operands[3] = Operand(write_data);
5955 store->offset = start * elem_size_bytes;
5956 store->offen = (offset.type() == RegType::vgpr);
5957 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5958 store->dlc = false;
5959 store->disable_wqm = true;
5960 store->barrier = barrier_buffer;
5961 ctx->program->needs_exact = true;
5962 ctx->block->instructions.emplace_back(std::move(store));
5963 }
5964 }
5965 }
5966
5967 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5968 {
5969 /* return the previous value if dest is ever used */
5970 bool return_previous = false;
5971 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5972 return_previous = true;
5973 break;
5974 }
5975 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5976 return_previous = true;
5977 break;
5978 }
5979
5980 Builder bld(ctx->program, ctx->block);
5981 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
5982
5983 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
5984 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
5985 get_ssa_temp(ctx, instr->src[3].ssa), data);
5986
5987 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
5988 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5989 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5990
5991 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5992
5993 aco_opcode op32, op64;
5994 switch (instr->intrinsic) {
5995 case nir_intrinsic_ssbo_atomic_add:
5996 op32 = aco_opcode::buffer_atomic_add;
5997 op64 = aco_opcode::buffer_atomic_add_x2;
5998 break;
5999 case nir_intrinsic_ssbo_atomic_imin:
6000 op32 = aco_opcode::buffer_atomic_smin;
6001 op64 = aco_opcode::buffer_atomic_smin_x2;
6002 break;
6003 case nir_intrinsic_ssbo_atomic_umin:
6004 op32 = aco_opcode::buffer_atomic_umin;
6005 op64 = aco_opcode::buffer_atomic_umin_x2;
6006 break;
6007 case nir_intrinsic_ssbo_atomic_imax:
6008 op32 = aco_opcode::buffer_atomic_smax;
6009 op64 = aco_opcode::buffer_atomic_smax_x2;
6010 break;
6011 case nir_intrinsic_ssbo_atomic_umax:
6012 op32 = aco_opcode::buffer_atomic_umax;
6013 op64 = aco_opcode::buffer_atomic_umax_x2;
6014 break;
6015 case nir_intrinsic_ssbo_atomic_and:
6016 op32 = aco_opcode::buffer_atomic_and;
6017 op64 = aco_opcode::buffer_atomic_and_x2;
6018 break;
6019 case nir_intrinsic_ssbo_atomic_or:
6020 op32 = aco_opcode::buffer_atomic_or;
6021 op64 = aco_opcode::buffer_atomic_or_x2;
6022 break;
6023 case nir_intrinsic_ssbo_atomic_xor:
6024 op32 = aco_opcode::buffer_atomic_xor;
6025 op64 = aco_opcode::buffer_atomic_xor_x2;
6026 break;
6027 case nir_intrinsic_ssbo_atomic_exchange:
6028 op32 = aco_opcode::buffer_atomic_swap;
6029 op64 = aco_opcode::buffer_atomic_swap_x2;
6030 break;
6031 case nir_intrinsic_ssbo_atomic_comp_swap:
6032 op32 = aco_opcode::buffer_atomic_cmpswap;
6033 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6034 break;
6035 default:
6036 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6037 }
6038 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6039 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6040 mubuf->operands[0] = Operand(rsrc);
6041 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6042 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6043 mubuf->operands[3] = Operand(data);
6044 if (return_previous)
6045 mubuf->definitions[0] = Definition(dst);
6046 mubuf->offset = 0;
6047 mubuf->offen = (offset.type() == RegType::vgpr);
6048 mubuf->glc = return_previous;
6049 mubuf->dlc = false; /* Not needed for atomics */
6050 mubuf->disable_wqm = true;
6051 mubuf->barrier = barrier_buffer;
6052 ctx->program->needs_exact = true;
6053 ctx->block->instructions.emplace_back(std::move(mubuf));
6054 }
6055
6056 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6057
6058 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6059 Builder bld(ctx->program, ctx->block);
6060 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6061 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6062 }
6063
6064 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
6065 {
6066 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6067 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6068
6069 if (addr.type() == RegType::vgpr)
6070 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
6071 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
6072 }
6073
6074 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6075 {
6076 Builder bld(ctx->program, ctx->block);
6077 unsigned num_components = instr->num_components;
6078 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
6079
6080 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6081 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6082
6083 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6084 bool dlc = glc && ctx->options->chip_class >= GFX10;
6085 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6086 * it's safe to use SMEM */
6087 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6088 aco_opcode op;
6089 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6090 bool global = ctx->options->chip_class >= GFX9;
6091
6092 if (ctx->options->chip_class >= GFX7) {
6093 switch (num_bytes) {
6094 case 4:
6095 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
6096 break;
6097 case 8:
6098 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
6099 break;
6100 case 12:
6101 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
6102 break;
6103 case 16:
6104 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
6105 break;
6106 default:
6107 unreachable("load_global not implemented for this size.");
6108 }
6109
6110 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
6111 flat->operands[0] = Operand(addr);
6112 flat->operands[1] = Operand(s1);
6113 flat->glc = glc;
6114 flat->dlc = dlc;
6115 flat->barrier = barrier_buffer;
6116
6117 if (dst.type() == RegType::sgpr) {
6118 Temp vec = bld.tmp(RegType::vgpr, dst.size());
6119 flat->definitions[0] = Definition(vec);
6120 ctx->block->instructions.emplace_back(std::move(flat));
6121 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
6122 } else {
6123 flat->definitions[0] = Definition(dst);
6124 ctx->block->instructions.emplace_back(std::move(flat));
6125 }
6126 emit_split_vector(ctx, dst, num_components);
6127 } else {
6128 assert(ctx->options->chip_class == GFX6);
6129
6130 /* GFX6 doesn't support loading vec3, expand to vec4. */
6131 num_bytes = num_bytes == 12 ? 16 : num_bytes;
6132
6133 switch (num_bytes) {
6134 case 4:
6135 op = aco_opcode::buffer_load_dword;
6136 break;
6137 case 8:
6138 op = aco_opcode::buffer_load_dwordx2;
6139 break;
6140 case 16:
6141 op = aco_opcode::buffer_load_dwordx4;
6142 break;
6143 default:
6144 unreachable("load_global not implemented for this size.");
6145 }
6146
6147 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6148
6149 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
6150 mubuf->operands[0] = Operand(rsrc);
6151 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6152 mubuf->operands[2] = Operand(0u);
6153 mubuf->glc = glc;
6154 mubuf->dlc = false;
6155 mubuf->offset = 0;
6156 mubuf->addr64 = addr.type() == RegType::vgpr;
6157 mubuf->disable_wqm = false;
6158 mubuf->barrier = barrier_buffer;
6159 aco_ptr<Instruction> instr = std::move(mubuf);
6160
6161 /* expand vector */
6162 if (dst.size() == 3) {
6163 Temp vec = bld.tmp(v4);
6164 instr->definitions[0] = Definition(vec);
6165 bld.insert(std::move(instr));
6166 emit_split_vector(ctx, vec, 4);
6167
6168 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
6169 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
6170 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
6171 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
6172 }
6173
6174 if (dst.type() == RegType::sgpr) {
6175 Temp vec = bld.tmp(RegType::vgpr, dst.size());
6176 instr->definitions[0] = Definition(vec);
6177 bld.insert(std::move(instr));
6178 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
6179 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
6180 } else {
6181 instr->definitions[0] = Definition(dst);
6182 bld.insert(std::move(instr));
6183 emit_split_vector(ctx, dst, num_components);
6184 }
6185 }
6186 } else {
6187 switch (num_bytes) {
6188 case 4:
6189 op = aco_opcode::s_load_dword;
6190 break;
6191 case 8:
6192 op = aco_opcode::s_load_dwordx2;
6193 break;
6194 case 12:
6195 case 16:
6196 op = aco_opcode::s_load_dwordx4;
6197 break;
6198 default:
6199 unreachable("load_global not implemented for this size.");
6200 }
6201 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
6202 load->operands[0] = Operand(addr);
6203 load->operands[1] = Operand(0u);
6204 load->definitions[0] = Definition(dst);
6205 load->glc = glc;
6206 load->dlc = dlc;
6207 load->barrier = barrier_buffer;
6208 assert(ctx->options->chip_class >= GFX8 || !glc);
6209
6210 if (dst.size() == 3) {
6211 /* trim vector */
6212 Temp vec = bld.tmp(s4);
6213 load->definitions[0] = Definition(vec);
6214 ctx->block->instructions.emplace_back(std::move(load));
6215 emit_split_vector(ctx, vec, 4);
6216
6217 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6218 emit_extract_vector(ctx, vec, 0, s1),
6219 emit_extract_vector(ctx, vec, 1, s1),
6220 emit_extract_vector(ctx, vec, 2, s1));
6221 } else {
6222 ctx->block->instructions.emplace_back(std::move(load));
6223 }
6224 }
6225 }
6226
6227 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6228 {
6229 Builder bld(ctx->program, ctx->block);
6230 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6231
6232 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6233 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6234
6235 if (ctx->options->chip_class >= GFX7)
6236 addr = as_vgpr(ctx, addr);
6237
6238 unsigned writemask = nir_intrinsic_write_mask(instr);
6239 while (writemask) {
6240 int start, count;
6241 u_bit_scan_consecutive_range(&writemask, &start, &count);
6242 if (count == 3 && ctx->options->chip_class == GFX6) {
6243 /* GFX6 doesn't support storing vec3, split it. */
6244 writemask |= 1u << (start + 2);
6245 count = 2;
6246 }
6247 unsigned num_bytes = count * elem_size_bytes;
6248
6249 Temp write_data = data;
6250 if (count != instr->num_components) {
6251 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6252 for (int i = 0; i < count; i++)
6253 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
6254 write_data = bld.tmp(RegType::vgpr, count);
6255 vec->definitions[0] = Definition(write_data);
6256 ctx->block->instructions.emplace_back(std::move(vec));
6257 }
6258
6259 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6260 unsigned offset = start * elem_size_bytes;
6261
6262 if (ctx->options->chip_class >= GFX7) {
6263 if (offset > 0 && ctx->options->chip_class < GFX9) {
6264 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6265 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6266 Temp carry = bld.tmp(bld.lm);
6267 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6268
6269 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6270 Operand(offset), addr0);
6271 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6272 Operand(0u), addr1,
6273 carry).def(1).setHint(vcc);
6274
6275 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6276
6277 offset = 0;
6278 }
6279
6280 bool global = ctx->options->chip_class >= GFX9;
6281 aco_opcode op;
6282 switch (num_bytes) {
6283 case 4:
6284 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6285 break;
6286 case 8:
6287 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6288 break;
6289 case 12:
6290 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6291 break;
6292 case 16:
6293 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6294 break;
6295 default:
6296 unreachable("store_global not implemented for this size.");
6297 }
6298
6299 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6300 flat->operands[0] = Operand(addr);
6301 flat->operands[1] = Operand(s1);
6302 flat->operands[2] = Operand(data);
6303 flat->glc = glc;
6304 flat->dlc = false;
6305 flat->offset = offset;
6306 flat->disable_wqm = true;
6307 flat->barrier = barrier_buffer;
6308 ctx->program->needs_exact = true;
6309 ctx->block->instructions.emplace_back(std::move(flat));
6310 } else {
6311 assert(ctx->options->chip_class == GFX6);
6312
6313 aco_opcode op;
6314 switch (num_bytes) {
6315 case 4:
6316 op = aco_opcode::buffer_store_dword;
6317 break;
6318 case 8:
6319 op = aco_opcode::buffer_store_dwordx2;
6320 break;
6321 case 16:
6322 op = aco_opcode::buffer_store_dwordx4;
6323 break;
6324 default:
6325 unreachable("store_global not implemented for this size.");
6326 }
6327
6328 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6329
6330 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6331 mubuf->operands[0] = Operand(rsrc);
6332 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6333 mubuf->operands[2] = Operand(0u);
6334 mubuf->operands[3] = Operand(write_data);
6335 mubuf->glc = glc;
6336 mubuf->dlc = false;
6337 mubuf->offset = offset;
6338 mubuf->addr64 = addr.type() == RegType::vgpr;
6339 mubuf->disable_wqm = true;
6340 mubuf->barrier = barrier_buffer;
6341 ctx->program->needs_exact = true;
6342 ctx->block->instructions.emplace_back(std::move(mubuf));
6343 }
6344 }
6345 }
6346
6347 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6348 {
6349 /* return the previous value if dest is ever used */
6350 bool return_previous = false;
6351 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6352 return_previous = true;
6353 break;
6354 }
6355 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6356 return_previous = true;
6357 break;
6358 }
6359
6360 Builder bld(ctx->program, ctx->block);
6361 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6362 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6363
6364 if (ctx->options->chip_class >= GFX7)
6365 addr = as_vgpr(ctx, addr);
6366
6367 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6368 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6369 get_ssa_temp(ctx, instr->src[2].ssa), data);
6370
6371 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6372
6373 aco_opcode op32, op64;
6374
6375 if (ctx->options->chip_class >= GFX7) {
6376 bool global = ctx->options->chip_class >= GFX9;
6377 switch (instr->intrinsic) {
6378 case nir_intrinsic_global_atomic_add:
6379 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6380 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6381 break;
6382 case nir_intrinsic_global_atomic_imin:
6383 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6384 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6385 break;
6386 case nir_intrinsic_global_atomic_umin:
6387 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6388 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6389 break;
6390 case nir_intrinsic_global_atomic_imax:
6391 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6392 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6393 break;
6394 case nir_intrinsic_global_atomic_umax:
6395 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6396 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6397 break;
6398 case nir_intrinsic_global_atomic_and:
6399 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6400 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6401 break;
6402 case nir_intrinsic_global_atomic_or:
6403 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6404 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6405 break;
6406 case nir_intrinsic_global_atomic_xor:
6407 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6408 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6409 break;
6410 case nir_intrinsic_global_atomic_exchange:
6411 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6412 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6413 break;
6414 case nir_intrinsic_global_atomic_comp_swap:
6415 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6416 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6417 break;
6418 default:
6419 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6420 }
6421
6422 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6423 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6424 flat->operands[0] = Operand(addr);
6425 flat->operands[1] = Operand(s1);
6426 flat->operands[2] = Operand(data);
6427 if (return_previous)
6428 flat->definitions[0] = Definition(dst);
6429 flat->glc = return_previous;
6430 flat->dlc = false; /* Not needed for atomics */
6431 flat->offset = 0;
6432 flat->disable_wqm = true;
6433 flat->barrier = barrier_buffer;
6434 ctx->program->needs_exact = true;
6435 ctx->block->instructions.emplace_back(std::move(flat));
6436 } else {
6437 assert(ctx->options->chip_class == GFX6);
6438
6439 switch (instr->intrinsic) {
6440 case nir_intrinsic_global_atomic_add:
6441 op32 = aco_opcode::buffer_atomic_add;
6442 op64 = aco_opcode::buffer_atomic_add_x2;
6443 break;
6444 case nir_intrinsic_global_atomic_imin:
6445 op32 = aco_opcode::buffer_atomic_smin;
6446 op64 = aco_opcode::buffer_atomic_smin_x2;
6447 break;
6448 case nir_intrinsic_global_atomic_umin:
6449 op32 = aco_opcode::buffer_atomic_umin;
6450 op64 = aco_opcode::buffer_atomic_umin_x2;
6451 break;
6452 case nir_intrinsic_global_atomic_imax:
6453 op32 = aco_opcode::buffer_atomic_smax;
6454 op64 = aco_opcode::buffer_atomic_smax_x2;
6455 break;
6456 case nir_intrinsic_global_atomic_umax:
6457 op32 = aco_opcode::buffer_atomic_umax;
6458 op64 = aco_opcode::buffer_atomic_umax_x2;
6459 break;
6460 case nir_intrinsic_global_atomic_and:
6461 op32 = aco_opcode::buffer_atomic_and;
6462 op64 = aco_opcode::buffer_atomic_and_x2;
6463 break;
6464 case nir_intrinsic_global_atomic_or:
6465 op32 = aco_opcode::buffer_atomic_or;
6466 op64 = aco_opcode::buffer_atomic_or_x2;
6467 break;
6468 case nir_intrinsic_global_atomic_xor:
6469 op32 = aco_opcode::buffer_atomic_xor;
6470 op64 = aco_opcode::buffer_atomic_xor_x2;
6471 break;
6472 case nir_intrinsic_global_atomic_exchange:
6473 op32 = aco_opcode::buffer_atomic_swap;
6474 op64 = aco_opcode::buffer_atomic_swap_x2;
6475 break;
6476 case nir_intrinsic_global_atomic_comp_swap:
6477 op32 = aco_opcode::buffer_atomic_cmpswap;
6478 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6479 break;
6480 default:
6481 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6482 }
6483
6484 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6485
6486 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6487
6488 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6489 mubuf->operands[0] = Operand(rsrc);
6490 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6491 mubuf->operands[2] = Operand(0u);
6492 mubuf->operands[3] = Operand(data);
6493 if (return_previous)
6494 mubuf->definitions[0] = Definition(dst);
6495 mubuf->glc = return_previous;
6496 mubuf->dlc = false;
6497 mubuf->offset = 0;
6498 mubuf->addr64 = addr.type() == RegType::vgpr;
6499 mubuf->disable_wqm = true;
6500 mubuf->barrier = barrier_buffer;
6501 ctx->program->needs_exact = true;
6502 ctx->block->instructions.emplace_back(std::move(mubuf));
6503 }
6504 }
6505
6506 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6507 Builder bld(ctx->program, ctx->block);
6508 switch(instr->intrinsic) {
6509 case nir_intrinsic_group_memory_barrier:
6510 case nir_intrinsic_memory_barrier:
6511 bld.barrier(aco_opcode::p_memory_barrier_common);
6512 break;
6513 case nir_intrinsic_memory_barrier_buffer:
6514 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6515 break;
6516 case nir_intrinsic_memory_barrier_image:
6517 bld.barrier(aco_opcode::p_memory_barrier_image);
6518 break;
6519 case nir_intrinsic_memory_barrier_tcs_patch:
6520 case nir_intrinsic_memory_barrier_shared:
6521 bld.barrier(aco_opcode::p_memory_barrier_shared);
6522 break;
6523 default:
6524 unreachable("Unimplemented memory barrier intrinsic");
6525 break;
6526 }
6527 }
6528
6529 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6530 {
6531 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6532 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6533 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
6534 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6535 Builder bld(ctx->program, ctx->block);
6536
6537 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6538 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6539 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6540 }
6541
6542 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6543 {
6544 unsigned writemask = nir_intrinsic_write_mask(instr);
6545 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6546 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6547 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6548 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
6549
6550 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6551 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6552 }
6553
6554 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6555 {
6556 unsigned offset = nir_intrinsic_base(instr);
6557 Operand m = load_lds_size_m0(ctx);
6558 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6559 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6560
6561 unsigned num_operands = 3;
6562 aco_opcode op32, op64, op32_rtn, op64_rtn;
6563 switch(instr->intrinsic) {
6564 case nir_intrinsic_shared_atomic_add:
6565 op32 = aco_opcode::ds_add_u32;
6566 op64 = aco_opcode::ds_add_u64;
6567 op32_rtn = aco_opcode::ds_add_rtn_u32;
6568 op64_rtn = aco_opcode::ds_add_rtn_u64;
6569 break;
6570 case nir_intrinsic_shared_atomic_imin:
6571 op32 = aco_opcode::ds_min_i32;
6572 op64 = aco_opcode::ds_min_i64;
6573 op32_rtn = aco_opcode::ds_min_rtn_i32;
6574 op64_rtn = aco_opcode::ds_min_rtn_i64;
6575 break;
6576 case nir_intrinsic_shared_atomic_umin:
6577 op32 = aco_opcode::ds_min_u32;
6578 op64 = aco_opcode::ds_min_u64;
6579 op32_rtn = aco_opcode::ds_min_rtn_u32;
6580 op64_rtn = aco_opcode::ds_min_rtn_u64;
6581 break;
6582 case nir_intrinsic_shared_atomic_imax:
6583 op32 = aco_opcode::ds_max_i32;
6584 op64 = aco_opcode::ds_max_i64;
6585 op32_rtn = aco_opcode::ds_max_rtn_i32;
6586 op64_rtn = aco_opcode::ds_max_rtn_i64;
6587 break;
6588 case nir_intrinsic_shared_atomic_umax:
6589 op32 = aco_opcode::ds_max_u32;
6590 op64 = aco_opcode::ds_max_u64;
6591 op32_rtn = aco_opcode::ds_max_rtn_u32;
6592 op64_rtn = aco_opcode::ds_max_rtn_u64;
6593 break;
6594 case nir_intrinsic_shared_atomic_and:
6595 op32 = aco_opcode::ds_and_b32;
6596 op64 = aco_opcode::ds_and_b64;
6597 op32_rtn = aco_opcode::ds_and_rtn_b32;
6598 op64_rtn = aco_opcode::ds_and_rtn_b64;
6599 break;
6600 case nir_intrinsic_shared_atomic_or:
6601 op32 = aco_opcode::ds_or_b32;
6602 op64 = aco_opcode::ds_or_b64;
6603 op32_rtn = aco_opcode::ds_or_rtn_b32;
6604 op64_rtn = aco_opcode::ds_or_rtn_b64;
6605 break;
6606 case nir_intrinsic_shared_atomic_xor:
6607 op32 = aco_opcode::ds_xor_b32;
6608 op64 = aco_opcode::ds_xor_b64;
6609 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6610 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6611 break;
6612 case nir_intrinsic_shared_atomic_exchange:
6613 op32 = aco_opcode::ds_write_b32;
6614 op64 = aco_opcode::ds_write_b64;
6615 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6616 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6617 break;
6618 case nir_intrinsic_shared_atomic_comp_swap:
6619 op32 = aco_opcode::ds_cmpst_b32;
6620 op64 = aco_opcode::ds_cmpst_b64;
6621 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6622 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6623 num_operands = 4;
6624 break;
6625 default:
6626 unreachable("Unhandled shared atomic intrinsic");
6627 }
6628
6629 /* return the previous value if dest is ever used */
6630 bool return_previous = false;
6631 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6632 return_previous = true;
6633 break;
6634 }
6635 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6636 return_previous = true;
6637 break;
6638 }
6639
6640 aco_opcode op;
6641 if (data.size() == 1) {
6642 assert(instr->dest.ssa.bit_size == 32);
6643 op = return_previous ? op32_rtn : op32;
6644 } else {
6645 assert(instr->dest.ssa.bit_size == 64);
6646 op = return_previous ? op64_rtn : op64;
6647 }
6648
6649 if (offset > 65535) {
6650 Builder bld(ctx->program, ctx->block);
6651 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6652 offset = 0;
6653 }
6654
6655 aco_ptr<DS_instruction> ds;
6656 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6657 ds->operands[0] = Operand(address);
6658 ds->operands[1] = Operand(data);
6659 if (num_operands == 4)
6660 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6661 ds->operands[num_operands - 1] = m;
6662 ds->offset0 = offset;
6663 if (return_previous)
6664 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6665 ctx->block->instructions.emplace_back(std::move(ds));
6666 }
6667
6668 Temp get_scratch_resource(isel_context *ctx)
6669 {
6670 Builder bld(ctx->program, ctx->block);
6671 Temp scratch_addr = ctx->program->private_segment_buffer;
6672 if (ctx->stage != compute_cs)
6673 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6674
6675 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6676 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6677
6678 if (ctx->program->chip_class >= GFX10) {
6679 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6680 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6681 S_008F0C_RESOURCE_LEVEL(1);
6682 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6683 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6684 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6685 }
6686
6687 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6688 if (ctx->program->chip_class <= GFX8)
6689 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6690
6691 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6692 }
6693
6694 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6695 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
6696 Builder bld(ctx->program, ctx->block);
6697 Temp rsrc = get_scratch_resource(ctx);
6698 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6699 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6700
6701 aco_opcode op;
6702 switch (dst.size()) {
6703 case 1:
6704 op = aco_opcode::buffer_load_dword;
6705 break;
6706 case 2:
6707 op = aco_opcode::buffer_load_dwordx2;
6708 break;
6709 case 3:
6710 op = aco_opcode::buffer_load_dwordx3;
6711 break;
6712 case 4:
6713 op = aco_opcode::buffer_load_dwordx4;
6714 break;
6715 case 6:
6716 case 8: {
6717 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
6718 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
6719 bld.def(v4), rsrc, offset,
6720 ctx->program->scratch_offset, 0, true);
6721 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
6722 aco_opcode::buffer_load_dwordx4,
6723 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
6724 rsrc, offset, ctx->program->scratch_offset, 16, true);
6725 emit_split_vector(ctx, lower, 2);
6726 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
6727 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
6728 if (dst.size() == 8) {
6729 emit_split_vector(ctx, upper, 2);
6730 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
6731 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
6732 } else {
6733 elems[2] = upper;
6734 }
6735
6736 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
6737 Format::PSEUDO, dst.size() / 2, 1)};
6738 for (unsigned i = 0; i < dst.size() / 2; i++)
6739 vec->operands[i] = Operand(elems[i]);
6740 vec->definitions[0] = Definition(dst);
6741 bld.insert(std::move(vec));
6742 ctx->allocated_vec.emplace(dst.id(), elems);
6743 return;
6744 }
6745 default:
6746 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
6747 }
6748
6749 bld.mubuf(op, Definition(dst), rsrc, offset, ctx->program->scratch_offset, 0, true);
6750 emit_split_vector(ctx, dst, instr->num_components);
6751 }
6752
6753 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6754 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
6755 Builder bld(ctx->program, ctx->block);
6756 Temp rsrc = get_scratch_resource(ctx);
6757 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6758 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6759
6760 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6761 unsigned writemask = nir_intrinsic_write_mask(instr);
6762
6763 while (writemask) {
6764 int start, count;
6765 u_bit_scan_consecutive_range(&writemask, &start, &count);
6766 int num_bytes = count * elem_size_bytes;
6767
6768 if (num_bytes > 16) {
6769 assert(elem_size_bytes == 8);
6770 writemask |= (((count - 2) << 1) - 1) << (start + 2);
6771 count = 2;
6772 num_bytes = 16;
6773 }
6774
6775 // TODO: check alignment of sub-dword stores
6776 // TODO: split 3 bytes. there is no store instruction for that
6777
6778 Temp write_data;
6779 if (count != instr->num_components) {
6780 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6781 for (int i = 0; i < count; i++) {
6782 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
6783 vec->operands[i] = Operand(elem);
6784 }
6785 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
6786 vec->definitions[0] = Definition(write_data);
6787 ctx->block->instructions.emplace_back(std::move(vec));
6788 } else {
6789 write_data = data;
6790 }
6791
6792 aco_opcode op;
6793 switch (num_bytes) {
6794 case 4:
6795 op = aco_opcode::buffer_store_dword;
6796 break;
6797 case 8:
6798 op = aco_opcode::buffer_store_dwordx2;
6799 break;
6800 case 12:
6801 op = aco_opcode::buffer_store_dwordx3;
6802 break;
6803 case 16:
6804 op = aco_opcode::buffer_store_dwordx4;
6805 break;
6806 default:
6807 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
6808 }
6809
6810 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
6811 }
6812 }
6813
6814 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6815 uint8_t log2_ps_iter_samples;
6816 if (ctx->program->info->ps.force_persample) {
6817 log2_ps_iter_samples =
6818 util_logbase2(ctx->options->key.fs.num_samples);
6819 } else {
6820 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6821 }
6822
6823 /* The bit pattern matches that used by fixed function fragment
6824 * processing. */
6825 static const unsigned ps_iter_masks[] = {
6826 0xffff, /* not used */
6827 0x5555,
6828 0x1111,
6829 0x0101,
6830 0x0001,
6831 };
6832 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6833
6834 Builder bld(ctx->program, ctx->block);
6835
6836 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6837 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6838 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6839 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6840 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6841 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6842 }
6843
6844 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6845 Builder bld(ctx->program, ctx->block);
6846
6847 unsigned stream = nir_intrinsic_stream_id(instr);
6848 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6849 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6850 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6851
6852 /* get GSVS ring */
6853 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6854
6855 unsigned num_components =
6856 ctx->program->info->gs.num_stream_output_components[stream];
6857 assert(num_components);
6858
6859 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6860 unsigned stream_offset = 0;
6861 for (unsigned i = 0; i < stream; i++) {
6862 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6863 stream_offset += prev_stride * ctx->program->wave_size;
6864 }
6865
6866 /* Limit on the stride field for <= GFX7. */
6867 assert(stride < (1 << 14));
6868
6869 Temp gsvs_dwords[4];
6870 for (unsigned i = 0; i < 4; i++)
6871 gsvs_dwords[i] = bld.tmp(s1);
6872 bld.pseudo(aco_opcode::p_split_vector,
6873 Definition(gsvs_dwords[0]),
6874 Definition(gsvs_dwords[1]),
6875 Definition(gsvs_dwords[2]),
6876 Definition(gsvs_dwords[3]),
6877 gsvs_ring);
6878
6879 if (stream_offset) {
6880 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6881
6882 Temp carry = bld.tmp(s1);
6883 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6884 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));
6885 }
6886
6887 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)));
6888 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6889
6890 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6891 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6892
6893 unsigned offset = 0;
6894 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6895 if (ctx->program->info->gs.output_streams[i] != stream)
6896 continue;
6897
6898 for (unsigned j = 0; j < 4; j++) {
6899 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6900 continue;
6901
6902 if (ctx->outputs.mask[i] & (1 << j)) {
6903 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6904 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6905 if (const_offset >= 4096u) {
6906 if (vaddr_offset.isUndefined())
6907 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6908 else
6909 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6910 const_offset %= 4096u;
6911 }
6912
6913 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6914 mtbuf->operands[0] = Operand(gsvs_ring);
6915 mtbuf->operands[1] = vaddr_offset;
6916 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6917 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6918 mtbuf->offen = !vaddr_offset.isUndefined();
6919 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6920 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6921 mtbuf->offset = const_offset;
6922 mtbuf->glc = true;
6923 mtbuf->slc = true;
6924 mtbuf->barrier = barrier_gs_data;
6925 mtbuf->can_reorder = true;
6926 bld.insert(std::move(mtbuf));
6927 }
6928
6929 offset += ctx->shader->info.gs.vertices_out;
6930 }
6931
6932 /* outputs for the next vertex are undefined and keeping them around can
6933 * create invalid IR with control flow */
6934 ctx->outputs.mask[i] = 0;
6935 }
6936
6937 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6938 }
6939
6940 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6941 {
6942 Builder bld(ctx->program, ctx->block);
6943
6944 if (cluster_size == 1) {
6945 return src;
6946 } if (op == nir_op_iand && cluster_size == 4) {
6947 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6948 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6949 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6950 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6951 } else if (op == nir_op_ior && cluster_size == 4) {
6952 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6953 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6954 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6955 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6956 //subgroupAnd(val) -> (exec & ~val) == 0
6957 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6958 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6959 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6960 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6961 //subgroupOr(val) -> (val & exec) != 0
6962 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6963 return bool_to_vector_condition(ctx, tmp);
6964 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6965 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6966 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6967 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6968 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6969 return bool_to_vector_condition(ctx, tmp);
6970 } else {
6971 //subgroupClustered{And,Or,Xor}(val, n) ->
6972 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6973 //cluster_offset = ~(n - 1) & lane_id
6974 //cluster_mask = ((1 << n) - 1)
6975 //subgroupClusteredAnd():
6976 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6977 //subgroupClusteredOr():
6978 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6979 //subgroupClusteredXor():
6980 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6981 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6982 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6983
6984 Temp tmp;
6985 if (op == nir_op_iand)
6986 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6987 else
6988 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6989
6990 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6991
6992 if (ctx->program->chip_class <= GFX7)
6993 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6994 else if (ctx->program->wave_size == 64)
6995 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6996 else
6997 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6998 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6999 if (cluster_mask != 0xffffffff)
7000 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7001
7002 Definition cmp_def = Definition();
7003 if (op == nir_op_iand) {
7004 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7005 } else if (op == nir_op_ior) {
7006 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7007 } else if (op == nir_op_ixor) {
7008 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7009 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7010 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7011 }
7012 cmp_def.setHint(vcc);
7013 return cmp_def.getTemp();
7014 }
7015 }
7016
7017 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7018 {
7019 Builder bld(ctx->program, ctx->block);
7020
7021 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7022 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7023 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7024 Temp tmp;
7025 if (op == nir_op_iand)
7026 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7027 else
7028 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7029
7030 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7031 Temp lo = lohi.def(0).getTemp();
7032 Temp hi = lohi.def(1).getTemp();
7033 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7034
7035 Definition cmp_def = Definition();
7036 if (op == nir_op_iand)
7037 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7038 else if (op == nir_op_ior)
7039 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7040 else if (op == nir_op_ixor)
7041 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7042 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7043 cmp_def.setHint(vcc);
7044 return cmp_def.getTemp();
7045 }
7046
7047 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7048 {
7049 Builder bld(ctx->program, ctx->block);
7050
7051 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7052 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7053 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7054 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7055 if (op == nir_op_iand)
7056 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7057 else if (op == nir_op_ior)
7058 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7059 else if (op == nir_op_ixor)
7060 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7061
7062 assert(false);
7063 return Temp();
7064 }
7065
7066 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7067 {
7068 Builder bld(ctx->program, ctx->block);
7069 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7070 if (src.regClass().type() == RegType::vgpr) {
7071 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7072 } else if (src.regClass() == s1) {
7073 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7074 } else if (src.regClass() == s2) {
7075 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7076 } else {
7077 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7078 nir_print_instr(&instr->instr, stderr);
7079 fprintf(stderr, "\n");
7080 }
7081 }
7082
7083 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7084 {
7085 Builder bld(ctx->program, ctx->block);
7086 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7087 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7088 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7089
7090 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7091 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7092 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7093 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7094
7095 /* Build DD X/Y */
7096 if (ctx->program->chip_class >= GFX8) {
7097 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7098 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7099 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7100 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7101 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7102 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7103 } else {
7104 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7105 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7106 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7107 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7108 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7109 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7110 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7111 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7112 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7113 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7114 }
7115
7116 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7117 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7118 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7119 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7120 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7121 Temp wqm1 = bld.tmp(v1);
7122 emit_wqm(ctx, tmp1, wqm1, true);
7123 Temp wqm2 = bld.tmp(v1);
7124 emit_wqm(ctx, tmp2, wqm2, true);
7125 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7126 return;
7127 }
7128
7129 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7130 {
7131 Builder bld(ctx->program, ctx->block);
7132 switch(instr->intrinsic) {
7133 case nir_intrinsic_load_barycentric_sample:
7134 case nir_intrinsic_load_barycentric_pixel:
7135 case nir_intrinsic_load_barycentric_centroid: {
7136 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7137 Temp bary = Temp(0, s2);
7138 switch (mode) {
7139 case INTERP_MODE_SMOOTH:
7140 case INTERP_MODE_NONE:
7141 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7142 bary = get_arg(ctx, ctx->args->ac.persp_center);
7143 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7144 bary = ctx->persp_centroid;
7145 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7146 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7147 break;
7148 case INTERP_MODE_NOPERSPECTIVE:
7149 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7150 bary = get_arg(ctx, ctx->args->ac.linear_center);
7151 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7152 bary = ctx->linear_centroid;
7153 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7154 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7155 break;
7156 default:
7157 break;
7158 }
7159 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7160 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7161 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7162 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7163 Operand(p1), Operand(p2));
7164 emit_split_vector(ctx, dst, 2);
7165 break;
7166 }
7167 case nir_intrinsic_load_barycentric_model: {
7168 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7169
7170 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7171 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7172 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7173 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7174 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7175 Operand(p1), Operand(p2), Operand(p3));
7176 emit_split_vector(ctx, dst, 3);
7177 break;
7178 }
7179 case nir_intrinsic_load_barycentric_at_sample: {
7180 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7181 switch (ctx->options->key.fs.num_samples) {
7182 case 2: sample_pos_offset += 1 << 3; break;
7183 case 4: sample_pos_offset += 3 << 3; break;
7184 case 8: sample_pos_offset += 7 << 3; break;
7185 default: break;
7186 }
7187 Temp sample_pos;
7188 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7189 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7190 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7191 if (addr.type() == RegType::sgpr) {
7192 Operand offset;
7193 if (const_addr) {
7194 sample_pos_offset += const_addr->u32 << 3;
7195 offset = Operand(sample_pos_offset);
7196 } else if (ctx->options->chip_class >= GFX9) {
7197 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7198 } else {
7199 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7200 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7201 }
7202
7203 Operand off = bld.copy(bld.def(s1), Operand(offset));
7204 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7205
7206 } else if (ctx->options->chip_class >= GFX9) {
7207 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7208 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7209 } else if (ctx->options->chip_class >= GFX7) {
7210 /* addr += private_segment_buffer + sample_pos_offset */
7211 Temp tmp0 = bld.tmp(s1);
7212 Temp tmp1 = bld.tmp(s1);
7213 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7214 Definition scc_tmp = bld.def(s1, scc);
7215 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7216 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7217 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7218 Temp pck0 = bld.tmp(v1);
7219 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7220 tmp1 = as_vgpr(ctx, tmp1);
7221 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);
7222 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7223
7224 /* sample_pos = flat_load_dwordx2 addr */
7225 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7226 } else {
7227 assert(ctx->options->chip_class == GFX6);
7228
7229 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7230 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7231 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7232
7233 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7234 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7235
7236 sample_pos = bld.tmp(v2);
7237
7238 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7239 load->definitions[0] = Definition(sample_pos);
7240 load->operands[0] = Operand(rsrc);
7241 load->operands[1] = Operand(addr);
7242 load->operands[2] = Operand(0u);
7243 load->offset = sample_pos_offset;
7244 load->offen = 0;
7245 load->addr64 = true;
7246 load->glc = false;
7247 load->dlc = false;
7248 load->disable_wqm = false;
7249 load->barrier = barrier_none;
7250 load->can_reorder = true;
7251 ctx->block->instructions.emplace_back(std::move(load));
7252 }
7253
7254 /* sample_pos -= 0.5 */
7255 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7256 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7257 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7258 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7259 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7260
7261 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7262 break;
7263 }
7264 case nir_intrinsic_load_barycentric_at_offset: {
7265 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7266 RegClass rc = RegClass(offset.type(), 1);
7267 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7268 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7269 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7270 break;
7271 }
7272 case nir_intrinsic_load_front_face: {
7273 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7274 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7275 break;
7276 }
7277 case nir_intrinsic_load_view_index: {
7278 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7279 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7280 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7281 break;
7282 }
7283
7284 /* fallthrough */
7285 }
7286 case nir_intrinsic_load_layer_id: {
7287 unsigned idx = nir_intrinsic_base(instr);
7288 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7289 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7290 break;
7291 }
7292 case nir_intrinsic_load_frag_coord: {
7293 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7294 break;
7295 }
7296 case nir_intrinsic_load_sample_pos: {
7297 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7298 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7299 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7300 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7301 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7302 break;
7303 }
7304 case nir_intrinsic_load_tess_coord:
7305 visit_load_tess_coord(ctx, instr);
7306 break;
7307 case nir_intrinsic_load_interpolated_input:
7308 visit_load_interpolated_input(ctx, instr);
7309 break;
7310 case nir_intrinsic_store_output:
7311 visit_store_output(ctx, instr);
7312 break;
7313 case nir_intrinsic_load_input:
7314 case nir_intrinsic_load_input_vertex:
7315 visit_load_input(ctx, instr);
7316 break;
7317 case nir_intrinsic_load_output:
7318 visit_load_output(ctx, instr);
7319 break;
7320 case nir_intrinsic_load_per_vertex_input:
7321 visit_load_per_vertex_input(ctx, instr);
7322 break;
7323 case nir_intrinsic_load_per_vertex_output:
7324 visit_load_per_vertex_output(ctx, instr);
7325 break;
7326 case nir_intrinsic_store_per_vertex_output:
7327 visit_store_per_vertex_output(ctx, instr);
7328 break;
7329 case nir_intrinsic_load_ubo:
7330 visit_load_ubo(ctx, instr);
7331 break;
7332 case nir_intrinsic_load_push_constant:
7333 visit_load_push_constant(ctx, instr);
7334 break;
7335 case nir_intrinsic_load_constant:
7336 visit_load_constant(ctx, instr);
7337 break;
7338 case nir_intrinsic_vulkan_resource_index:
7339 visit_load_resource(ctx, instr);
7340 break;
7341 case nir_intrinsic_discard:
7342 visit_discard(ctx, instr);
7343 break;
7344 case nir_intrinsic_discard_if:
7345 visit_discard_if(ctx, instr);
7346 break;
7347 case nir_intrinsic_load_shared:
7348 visit_load_shared(ctx, instr);
7349 break;
7350 case nir_intrinsic_store_shared:
7351 visit_store_shared(ctx, instr);
7352 break;
7353 case nir_intrinsic_shared_atomic_add:
7354 case nir_intrinsic_shared_atomic_imin:
7355 case nir_intrinsic_shared_atomic_umin:
7356 case nir_intrinsic_shared_atomic_imax:
7357 case nir_intrinsic_shared_atomic_umax:
7358 case nir_intrinsic_shared_atomic_and:
7359 case nir_intrinsic_shared_atomic_or:
7360 case nir_intrinsic_shared_atomic_xor:
7361 case nir_intrinsic_shared_atomic_exchange:
7362 case nir_intrinsic_shared_atomic_comp_swap:
7363 visit_shared_atomic(ctx, instr);
7364 break;
7365 case nir_intrinsic_image_deref_load:
7366 visit_image_load(ctx, instr);
7367 break;
7368 case nir_intrinsic_image_deref_store:
7369 visit_image_store(ctx, instr);
7370 break;
7371 case nir_intrinsic_image_deref_atomic_add:
7372 case nir_intrinsic_image_deref_atomic_umin:
7373 case nir_intrinsic_image_deref_atomic_imin:
7374 case nir_intrinsic_image_deref_atomic_umax:
7375 case nir_intrinsic_image_deref_atomic_imax:
7376 case nir_intrinsic_image_deref_atomic_and:
7377 case nir_intrinsic_image_deref_atomic_or:
7378 case nir_intrinsic_image_deref_atomic_xor:
7379 case nir_intrinsic_image_deref_atomic_exchange:
7380 case nir_intrinsic_image_deref_atomic_comp_swap:
7381 visit_image_atomic(ctx, instr);
7382 break;
7383 case nir_intrinsic_image_deref_size:
7384 visit_image_size(ctx, instr);
7385 break;
7386 case nir_intrinsic_load_ssbo:
7387 visit_load_ssbo(ctx, instr);
7388 break;
7389 case nir_intrinsic_store_ssbo:
7390 visit_store_ssbo(ctx, instr);
7391 break;
7392 case nir_intrinsic_load_global:
7393 visit_load_global(ctx, instr);
7394 break;
7395 case nir_intrinsic_store_global:
7396 visit_store_global(ctx, instr);
7397 break;
7398 case nir_intrinsic_global_atomic_add:
7399 case nir_intrinsic_global_atomic_imin:
7400 case nir_intrinsic_global_atomic_umin:
7401 case nir_intrinsic_global_atomic_imax:
7402 case nir_intrinsic_global_atomic_umax:
7403 case nir_intrinsic_global_atomic_and:
7404 case nir_intrinsic_global_atomic_or:
7405 case nir_intrinsic_global_atomic_xor:
7406 case nir_intrinsic_global_atomic_exchange:
7407 case nir_intrinsic_global_atomic_comp_swap:
7408 visit_global_atomic(ctx, instr);
7409 break;
7410 case nir_intrinsic_ssbo_atomic_add:
7411 case nir_intrinsic_ssbo_atomic_imin:
7412 case nir_intrinsic_ssbo_atomic_umin:
7413 case nir_intrinsic_ssbo_atomic_imax:
7414 case nir_intrinsic_ssbo_atomic_umax:
7415 case nir_intrinsic_ssbo_atomic_and:
7416 case nir_intrinsic_ssbo_atomic_or:
7417 case nir_intrinsic_ssbo_atomic_xor:
7418 case nir_intrinsic_ssbo_atomic_exchange:
7419 case nir_intrinsic_ssbo_atomic_comp_swap:
7420 visit_atomic_ssbo(ctx, instr);
7421 break;
7422 case nir_intrinsic_load_scratch:
7423 visit_load_scratch(ctx, instr);
7424 break;
7425 case nir_intrinsic_store_scratch:
7426 visit_store_scratch(ctx, instr);
7427 break;
7428 case nir_intrinsic_get_buffer_size:
7429 visit_get_buffer_size(ctx, instr);
7430 break;
7431 case nir_intrinsic_control_barrier: {
7432 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7433 /* GFX6 only (thanks to a hw bug workaround):
7434 * The real barrier instruction isn’t needed, because an entire patch
7435 * always fits into a single wave.
7436 */
7437 break;
7438 }
7439
7440 if (ctx->program->workgroup_size > ctx->program->wave_size)
7441 bld.sopp(aco_opcode::s_barrier);
7442
7443 break;
7444 }
7445 case nir_intrinsic_memory_barrier_tcs_patch:
7446 case nir_intrinsic_group_memory_barrier:
7447 case nir_intrinsic_memory_barrier:
7448 case nir_intrinsic_memory_barrier_buffer:
7449 case nir_intrinsic_memory_barrier_image:
7450 case nir_intrinsic_memory_barrier_shared:
7451 emit_memory_barrier(ctx, instr);
7452 break;
7453 case nir_intrinsic_load_num_work_groups: {
7454 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7455 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7456 emit_split_vector(ctx, dst, 3);
7457 break;
7458 }
7459 case nir_intrinsic_load_local_invocation_id: {
7460 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7461 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7462 emit_split_vector(ctx, dst, 3);
7463 break;
7464 }
7465 case nir_intrinsic_load_work_group_id: {
7466 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7467 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7468 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7469 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7470 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7471 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7472 emit_split_vector(ctx, dst, 3);
7473 break;
7474 }
7475 case nir_intrinsic_load_local_invocation_index: {
7476 Temp id = emit_mbcnt(ctx, bld.def(v1));
7477
7478 /* The tg_size bits [6:11] contain the subgroup id,
7479 * we need this multiplied by the wave size, and then OR the thread id to it.
7480 */
7481 if (ctx->program->wave_size == 64) {
7482 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7483 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7484 get_arg(ctx, ctx->args->ac.tg_size));
7485 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7486 } else {
7487 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7488 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7489 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7490 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7491 }
7492 break;
7493 }
7494 case nir_intrinsic_load_subgroup_id: {
7495 if (ctx->stage == compute_cs) {
7496 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7497 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7498 } else {
7499 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7500 }
7501 break;
7502 }
7503 case nir_intrinsic_load_subgroup_invocation: {
7504 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7505 break;
7506 }
7507 case nir_intrinsic_load_num_subgroups: {
7508 if (ctx->stage == compute_cs)
7509 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7510 get_arg(ctx, ctx->args->ac.tg_size));
7511 else
7512 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7513 break;
7514 }
7515 case nir_intrinsic_ballot: {
7516 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7517 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7518 Definition tmp = bld.def(dst.regClass());
7519 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7520 if (instr->src[0].ssa->bit_size == 1) {
7521 assert(src.regClass() == bld.lm);
7522 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7523 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7524 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7525 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7526 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7527 } else {
7528 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7529 nir_print_instr(&instr->instr, stderr);
7530 fprintf(stderr, "\n");
7531 }
7532 if (dst.size() != bld.lm.size()) {
7533 /* Wave32 with ballot size set to 64 */
7534 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7535 }
7536 emit_wqm(ctx, tmp.getTemp(), dst);
7537 break;
7538 }
7539 case nir_intrinsic_shuffle:
7540 case nir_intrinsic_read_invocation: {
7541 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7542 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7543 emit_uniform_subgroup(ctx, instr, src);
7544 } else {
7545 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7546 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7547 tid = bld.as_uniform(tid);
7548 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7549 if (src.regClass() == v1) {
7550 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7551 } else if (src.regClass() == v2) {
7552 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7553 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7554 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7555 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7556 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7557 emit_split_vector(ctx, dst, 2);
7558 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7559 assert(src.regClass() == bld.lm);
7560 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7561 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7562 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7563 assert(src.regClass() == bld.lm);
7564 Temp tmp;
7565 if (ctx->program->chip_class <= GFX7)
7566 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7567 else if (ctx->program->wave_size == 64)
7568 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7569 else
7570 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7571 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7572 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7573 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7574 } else {
7575 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7576 nir_print_instr(&instr->instr, stderr);
7577 fprintf(stderr, "\n");
7578 }
7579 }
7580 break;
7581 }
7582 case nir_intrinsic_load_sample_id: {
7583 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7584 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7585 break;
7586 }
7587 case nir_intrinsic_load_sample_mask_in: {
7588 visit_load_sample_mask_in(ctx, instr);
7589 break;
7590 }
7591 case nir_intrinsic_read_first_invocation: {
7592 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7593 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7594 if (src.regClass() == v1) {
7595 emit_wqm(ctx,
7596 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7597 dst);
7598 } else if (src.regClass() == v2) {
7599 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7600 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7601 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7602 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7603 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7604 emit_split_vector(ctx, dst, 2);
7605 } else if (instr->dest.ssa.bit_size == 1) {
7606 assert(src.regClass() == bld.lm);
7607 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7608 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7609 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7610 } else if (src.regClass() == s1) {
7611 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7612 } else if (src.regClass() == s2) {
7613 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7614 } else {
7615 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7616 nir_print_instr(&instr->instr, stderr);
7617 fprintf(stderr, "\n");
7618 }
7619 break;
7620 }
7621 case nir_intrinsic_vote_all: {
7622 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7623 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7624 assert(src.regClass() == bld.lm);
7625 assert(dst.regClass() == bld.lm);
7626
7627 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7628 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7629 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7630 break;
7631 }
7632 case nir_intrinsic_vote_any: {
7633 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7634 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7635 assert(src.regClass() == bld.lm);
7636 assert(dst.regClass() == bld.lm);
7637
7638 Temp tmp = bool_to_scalar_condition(ctx, src);
7639 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7640 break;
7641 }
7642 case nir_intrinsic_reduce:
7643 case nir_intrinsic_inclusive_scan:
7644 case nir_intrinsic_exclusive_scan: {
7645 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7646 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7647 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7648 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7649 nir_intrinsic_cluster_size(instr) : 0;
7650 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7651
7652 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7653 emit_uniform_subgroup(ctx, instr, src);
7654 } else if (instr->dest.ssa.bit_size == 1) {
7655 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7656 op = nir_op_iand;
7657 else if (op == nir_op_iadd)
7658 op = nir_op_ixor;
7659 else if (op == nir_op_umax || op == nir_op_imax)
7660 op = nir_op_ior;
7661 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7662
7663 switch (instr->intrinsic) {
7664 case nir_intrinsic_reduce:
7665 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7666 break;
7667 case nir_intrinsic_exclusive_scan:
7668 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7669 break;
7670 case nir_intrinsic_inclusive_scan:
7671 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7672 break;
7673 default:
7674 assert(false);
7675 }
7676 } else if (cluster_size == 1) {
7677 bld.copy(Definition(dst), src);
7678 } else {
7679 src = as_vgpr(ctx, src);
7680
7681 ReduceOp reduce_op;
7682 switch (op) {
7683 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7684 CASE(iadd)
7685 CASE(imul)
7686 CASE(fadd)
7687 CASE(fmul)
7688 CASE(imin)
7689 CASE(umin)
7690 CASE(fmin)
7691 CASE(imax)
7692 CASE(umax)
7693 CASE(fmax)
7694 CASE(iand)
7695 CASE(ior)
7696 CASE(ixor)
7697 default:
7698 unreachable("unknown reduction op");
7699 #undef CASE
7700 }
7701
7702 aco_opcode aco_op;
7703 switch (instr->intrinsic) {
7704 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7705 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7706 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7707 default:
7708 unreachable("unknown reduce intrinsic");
7709 }
7710
7711 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7712 reduce->operands[0] = Operand(src);
7713 // filled in by aco_reduce_assign.cpp, used internally as part of the
7714 // reduce sequence
7715 assert(dst.size() == 1 || dst.size() == 2);
7716 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7717 reduce->operands[2] = Operand(v1.as_linear());
7718
7719 Temp tmp_dst = bld.tmp(dst.regClass());
7720 reduce->definitions[0] = Definition(tmp_dst);
7721 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7722 reduce->definitions[2] = Definition();
7723 reduce->definitions[3] = Definition(scc, s1);
7724 reduce->definitions[4] = Definition();
7725 reduce->reduce_op = reduce_op;
7726 reduce->cluster_size = cluster_size;
7727 ctx->block->instructions.emplace_back(std::move(reduce));
7728
7729 emit_wqm(ctx, tmp_dst, dst);
7730 }
7731 break;
7732 }
7733 case nir_intrinsic_quad_broadcast: {
7734 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7735 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7736 emit_uniform_subgroup(ctx, instr, src);
7737 } else {
7738 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7739 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7740 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7741
7742 if (instr->dest.ssa.bit_size == 1) {
7743 assert(src.regClass() == bld.lm);
7744 assert(dst.regClass() == bld.lm);
7745 uint32_t half_mask = 0x11111111u << lane;
7746 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7747 Temp tmp = bld.tmp(bld.lm);
7748 bld.sop1(Builder::s_wqm, Definition(tmp),
7749 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7750 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7751 emit_wqm(ctx, tmp, dst);
7752 } else if (instr->dest.ssa.bit_size == 32) {
7753 if (ctx->program->chip_class >= GFX8)
7754 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7755 else
7756 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7757 } else if (instr->dest.ssa.bit_size == 64) {
7758 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7759 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7760 if (ctx->program->chip_class >= GFX8) {
7761 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7762 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7763 } else {
7764 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7765 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7766 }
7767 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7768 emit_split_vector(ctx, dst, 2);
7769 } else {
7770 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7771 nir_print_instr(&instr->instr, stderr);
7772 fprintf(stderr, "\n");
7773 }
7774 }
7775 break;
7776 }
7777 case nir_intrinsic_quad_swap_horizontal:
7778 case nir_intrinsic_quad_swap_vertical:
7779 case nir_intrinsic_quad_swap_diagonal:
7780 case nir_intrinsic_quad_swizzle_amd: {
7781 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7782 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7783 emit_uniform_subgroup(ctx, instr, src);
7784 break;
7785 }
7786 uint16_t dpp_ctrl = 0;
7787 switch (instr->intrinsic) {
7788 case nir_intrinsic_quad_swap_horizontal:
7789 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7790 break;
7791 case nir_intrinsic_quad_swap_vertical:
7792 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7793 break;
7794 case nir_intrinsic_quad_swap_diagonal:
7795 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7796 break;
7797 case nir_intrinsic_quad_swizzle_amd:
7798 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7799 break;
7800 default:
7801 break;
7802 }
7803 if (ctx->program->chip_class < GFX8)
7804 dpp_ctrl |= (1 << 15);
7805
7806 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7807 if (instr->dest.ssa.bit_size == 1) {
7808 assert(src.regClass() == bld.lm);
7809 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7810 if (ctx->program->chip_class >= GFX8)
7811 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7812 else
7813 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7814 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7815 emit_wqm(ctx, tmp, dst);
7816 } else if (instr->dest.ssa.bit_size == 32) {
7817 Temp tmp;
7818 if (ctx->program->chip_class >= GFX8)
7819 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7820 else
7821 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7822 emit_wqm(ctx, tmp, dst);
7823 } else if (instr->dest.ssa.bit_size == 64) {
7824 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7825 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7826 if (ctx->program->chip_class >= GFX8) {
7827 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7828 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7829 } else {
7830 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7831 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7832 }
7833 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7834 emit_split_vector(ctx, dst, 2);
7835 } else {
7836 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7837 nir_print_instr(&instr->instr, stderr);
7838 fprintf(stderr, "\n");
7839 }
7840 break;
7841 }
7842 case nir_intrinsic_masked_swizzle_amd: {
7843 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7844 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7845 emit_uniform_subgroup(ctx, instr, src);
7846 break;
7847 }
7848 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7849 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7850 if (dst.regClass() == v1) {
7851 emit_wqm(ctx,
7852 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7853 dst);
7854 } else if (dst.regClass() == v2) {
7855 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7856 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7857 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7858 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7859 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7860 emit_split_vector(ctx, dst, 2);
7861 } else {
7862 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7863 nir_print_instr(&instr->instr, stderr);
7864 fprintf(stderr, "\n");
7865 }
7866 break;
7867 }
7868 case nir_intrinsic_write_invocation_amd: {
7869 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7870 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7871 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7872 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7873 if (dst.regClass() == v1) {
7874 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7875 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7876 } else if (dst.regClass() == v2) {
7877 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7878 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7879 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7880 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7881 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7882 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7883 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7884 emit_split_vector(ctx, dst, 2);
7885 } else {
7886 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7887 nir_print_instr(&instr->instr, stderr);
7888 fprintf(stderr, "\n");
7889 }
7890 break;
7891 }
7892 case nir_intrinsic_mbcnt_amd: {
7893 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7894 RegClass rc = RegClass(src.type(), 1);
7895 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7896 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7897 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7898 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7899 emit_wqm(ctx, wqm_tmp, dst);
7900 break;
7901 }
7902 case nir_intrinsic_load_helper_invocation: {
7903 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7904 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7905 ctx->block->kind |= block_kind_needs_lowering;
7906 ctx->program->needs_exact = true;
7907 break;
7908 }
7909 case nir_intrinsic_is_helper_invocation: {
7910 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7911 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7912 ctx->block->kind |= block_kind_needs_lowering;
7913 ctx->program->needs_exact = true;
7914 break;
7915 }
7916 case nir_intrinsic_demote:
7917 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7918
7919 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7920 ctx->cf_info.exec_potentially_empty_discard = true;
7921 ctx->block->kind |= block_kind_uses_demote;
7922 ctx->program->needs_exact = true;
7923 break;
7924 case nir_intrinsic_demote_if: {
7925 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7926 assert(src.regClass() == bld.lm);
7927 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7928 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7929
7930 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7931 ctx->cf_info.exec_potentially_empty_discard = true;
7932 ctx->block->kind |= block_kind_uses_demote;
7933 ctx->program->needs_exact = true;
7934 break;
7935 }
7936 case nir_intrinsic_first_invocation: {
7937 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7938 get_ssa_temp(ctx, &instr->dest.ssa));
7939 break;
7940 }
7941 case nir_intrinsic_shader_clock:
7942 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7943 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7944 break;
7945 case nir_intrinsic_load_vertex_id_zero_base: {
7946 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7947 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7948 break;
7949 }
7950 case nir_intrinsic_load_first_vertex: {
7951 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7952 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7953 break;
7954 }
7955 case nir_intrinsic_load_base_instance: {
7956 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7957 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7958 break;
7959 }
7960 case nir_intrinsic_load_instance_id: {
7961 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7962 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7963 break;
7964 }
7965 case nir_intrinsic_load_draw_id: {
7966 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7967 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7968 break;
7969 }
7970 case nir_intrinsic_load_invocation_id: {
7971 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7972
7973 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7974 if (ctx->options->chip_class >= GFX10)
7975 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7976 else
7977 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7978 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7979 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7980 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7981 } else {
7982 unreachable("Unsupported stage for load_invocation_id");
7983 }
7984
7985 break;
7986 }
7987 case nir_intrinsic_load_primitive_id: {
7988 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7989
7990 switch (ctx->shader->info.stage) {
7991 case MESA_SHADER_GEOMETRY:
7992 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7993 break;
7994 case MESA_SHADER_TESS_CTRL:
7995 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7996 break;
7997 case MESA_SHADER_TESS_EVAL:
7998 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7999 break;
8000 default:
8001 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8002 }
8003
8004 break;
8005 }
8006 case nir_intrinsic_load_patch_vertices_in: {
8007 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8008 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8009
8010 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8011 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8012 break;
8013 }
8014 case nir_intrinsic_emit_vertex_with_counter: {
8015 visit_emit_vertex_with_counter(ctx, instr);
8016 break;
8017 }
8018 case nir_intrinsic_end_primitive_with_counter: {
8019 unsigned stream = nir_intrinsic_stream_id(instr);
8020 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8021 break;
8022 }
8023 case nir_intrinsic_set_vertex_count: {
8024 /* unused, the HW keeps track of this for us */
8025 break;
8026 }
8027 default:
8028 fprintf(stderr, "Unimplemented intrinsic instr: ");
8029 nir_print_instr(&instr->instr, stderr);
8030 fprintf(stderr, "\n");
8031 abort();
8032
8033 break;
8034 }
8035 }
8036
8037
8038 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8039 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8040 enum glsl_base_type *stype)
8041 {
8042 nir_deref_instr *texture_deref_instr = NULL;
8043 nir_deref_instr *sampler_deref_instr = NULL;
8044 int plane = -1;
8045
8046 for (unsigned i = 0; i < instr->num_srcs; i++) {
8047 switch (instr->src[i].src_type) {
8048 case nir_tex_src_texture_deref:
8049 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8050 break;
8051 case nir_tex_src_sampler_deref:
8052 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8053 break;
8054 case nir_tex_src_plane:
8055 plane = nir_src_as_int(instr->src[i].src);
8056 break;
8057 default:
8058 break;
8059 }
8060 }
8061
8062 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8063
8064 if (!sampler_deref_instr)
8065 sampler_deref_instr = texture_deref_instr;
8066
8067 if (plane >= 0) {
8068 assert(instr->op != nir_texop_txf_ms &&
8069 instr->op != nir_texop_samples_identical);
8070 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8071 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8072 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8073 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8074 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8075 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8076 } else {
8077 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8078 }
8079 if (samp_ptr) {
8080 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8081
8082 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8083 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8084 Builder bld(ctx->program, ctx->block);
8085
8086 /* to avoid unnecessary moves, we split and recombine sampler and image */
8087 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8088 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8089 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8090 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8091 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8092 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8093 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8094 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8095
8096 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8097 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8098 img[0], img[1], img[2], img[3],
8099 img[4], img[5], img[6], img[7]);
8100 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8101 samp[0], samp[1], samp[2], samp[3]);
8102 }
8103 }
8104 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8105 instr->op == nir_texop_samples_identical))
8106 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8107 }
8108
8109 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8110 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8111 {
8112 Builder bld(ctx->program, ctx->block);
8113
8114 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8115 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8116 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8117
8118 Operand neg_one(0xbf800000u);
8119 Operand one(0x3f800000u);
8120 Operand two(0x40000000u);
8121 Operand four(0x40800000u);
8122
8123 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8124 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8125 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8126
8127 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8128 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8129 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8130 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);
8131
8132 // select sc
8133 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8134 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8135 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8136 one, is_ma_y);
8137 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8138
8139 // select tc
8140 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8141 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8142 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8143
8144 // select ma
8145 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8146 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8147 deriv_z, is_ma_z);
8148 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8149 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8150 }
8151
8152 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8153 {
8154 Builder bld(ctx->program, ctx->block);
8155 Temp ma, tc, sc, id;
8156
8157 if (is_array) {
8158 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8159
8160 // see comment in ac_prepare_cube_coords()
8161 if (ctx->options->chip_class <= GFX8)
8162 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8163 }
8164
8165 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8166
8167 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8168 vop3a->operands[0] = Operand(ma);
8169 vop3a->abs[0] = true;
8170 Temp invma = bld.tmp(v1);
8171 vop3a->definitions[0] = Definition(invma);
8172 ctx->block->instructions.emplace_back(std::move(vop3a));
8173
8174 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8175 if (!is_deriv)
8176 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8177
8178 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8179 if (!is_deriv)
8180 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8181
8182 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8183
8184 if (is_deriv) {
8185 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8186 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8187
8188 for (unsigned i = 0; i < 2; i++) {
8189 // see comment in ac_prepare_cube_coords()
8190 Temp deriv_ma;
8191 Temp deriv_sc, deriv_tc;
8192 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8193 &deriv_ma, &deriv_sc, &deriv_tc);
8194
8195 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8196
8197 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8198 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8199 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8200 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8201 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8202 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8203 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8204 }
8205
8206 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8207 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8208 }
8209
8210 if (is_array)
8211 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8212 coords.resize(3);
8213 coords[0] = sc;
8214 coords[1] = tc;
8215 coords[2] = id;
8216 }
8217
8218 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8219 {
8220 if (vec->parent_instr->type != nir_instr_type_alu)
8221 return;
8222 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8223 if (vec_instr->op != nir_op_vec(vec->num_components))
8224 return;
8225
8226 for (unsigned i = 0; i < vec->num_components; i++) {
8227 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8228 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8229 }
8230 }
8231
8232 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8233 {
8234 Builder bld(ctx->program, ctx->block);
8235 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8236 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8237 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8238 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8239 std::vector<Temp> coords;
8240 std::vector<Temp> derivs;
8241 nir_const_value *sample_index_cv = NULL;
8242 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8243 enum glsl_base_type stype;
8244 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8245
8246 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8247 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8248 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8249 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8250
8251 for (unsigned i = 0; i < instr->num_srcs; i++) {
8252 switch (instr->src[i].src_type) {
8253 case nir_tex_src_coord: {
8254 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8255 for (unsigned i = 0; i < coord.size(); i++)
8256 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8257 break;
8258 }
8259 case nir_tex_src_bias:
8260 if (instr->op == nir_texop_txb) {
8261 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8262 has_bias = true;
8263 }
8264 break;
8265 case nir_tex_src_lod: {
8266 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8267
8268 if (val && val->f32 <= 0.0) {
8269 level_zero = true;
8270 } else {
8271 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8272 has_lod = true;
8273 }
8274 break;
8275 }
8276 case nir_tex_src_comparator:
8277 if (instr->is_shadow) {
8278 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8279 has_compare = true;
8280 }
8281 break;
8282 case nir_tex_src_offset:
8283 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8284 get_const_vec(instr->src[i].src.ssa, const_offset);
8285 has_offset = true;
8286 break;
8287 case nir_tex_src_ddx:
8288 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8289 has_ddx = true;
8290 break;
8291 case nir_tex_src_ddy:
8292 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8293 has_ddy = true;
8294 break;
8295 case nir_tex_src_ms_index:
8296 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8297 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8298 has_sample_index = true;
8299 break;
8300 case nir_tex_src_texture_offset:
8301 case nir_tex_src_sampler_offset:
8302 default:
8303 break;
8304 }
8305 }
8306
8307 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8308 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8309
8310 if (instr->op == nir_texop_texture_samples) {
8311 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8312
8313 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8314 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8315 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 */));
8316 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8317
8318 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8319 samples, Operand(1u), bld.scc(is_msaa));
8320 return;
8321 }
8322
8323 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8324 aco_ptr<Instruction> tmp_instr;
8325 Temp acc, pack = Temp();
8326
8327 uint32_t pack_const = 0;
8328 for (unsigned i = 0; i < offset.size(); i++) {
8329 if (!const_offset[i])
8330 continue;
8331 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8332 }
8333
8334 if (offset.type() == RegType::sgpr) {
8335 for (unsigned i = 0; i < offset.size(); i++) {
8336 if (const_offset[i])
8337 continue;
8338
8339 acc = emit_extract_vector(ctx, offset, i, s1);
8340 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8341
8342 if (i) {
8343 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8344 }
8345
8346 if (pack == Temp()) {
8347 pack = acc;
8348 } else {
8349 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8350 }
8351 }
8352
8353 if (pack_const && pack != Temp())
8354 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8355 } else {
8356 for (unsigned i = 0; i < offset.size(); i++) {
8357 if (const_offset[i])
8358 continue;
8359
8360 acc = emit_extract_vector(ctx, offset, i, v1);
8361 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8362
8363 if (i) {
8364 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8365 }
8366
8367 if (pack == Temp()) {
8368 pack = acc;
8369 } else {
8370 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8371 }
8372 }
8373
8374 if (pack_const && pack != Temp())
8375 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8376 }
8377 if (pack_const && pack == Temp())
8378 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8379 else if (pack == Temp())
8380 has_offset = false;
8381 else
8382 offset = pack;
8383 }
8384
8385 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8386 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8387
8388 /* pack derivatives */
8389 if (has_ddx || has_ddy) {
8390 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8391 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8392 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8393 derivs = {ddx, zero, ddy, zero};
8394 } else {
8395 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8396 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8397 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8398 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8399 }
8400 has_derivs = true;
8401 }
8402
8403 if (instr->coord_components > 1 &&
8404 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8405 instr->is_array &&
8406 instr->op != nir_texop_txf)
8407 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8408
8409 if (instr->coord_components > 2 &&
8410 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8411 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8412 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8413 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8414 instr->is_array &&
8415 instr->op != nir_texop_txf &&
8416 instr->op != nir_texop_txf_ms &&
8417 instr->op != nir_texop_fragment_fetch &&
8418 instr->op != nir_texop_fragment_mask_fetch)
8419 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8420
8421 if (ctx->options->chip_class == GFX9 &&
8422 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8423 instr->op != nir_texop_lod && instr->coord_components) {
8424 assert(coords.size() > 0 && coords.size() < 3);
8425
8426 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8427 Operand((uint32_t) 0) :
8428 Operand((uint32_t) 0x3f000000)));
8429 }
8430
8431 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8432
8433 if (instr->op == nir_texop_samples_identical)
8434 resource = fmask_ptr;
8435
8436 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8437 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8438 instr->op != nir_texop_txs &&
8439 instr->op != nir_texop_fragment_fetch &&
8440 instr->op != nir_texop_fragment_mask_fetch) {
8441 assert(has_sample_index);
8442 Operand op(sample_index);
8443 if (sample_index_cv)
8444 op = Operand(sample_index_cv->u32);
8445 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8446 }
8447
8448 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8449 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8450 Temp off = emit_extract_vector(ctx, offset, i, v1);
8451 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8452 }
8453 has_offset = false;
8454 }
8455
8456 /* Build tex instruction */
8457 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8458 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8459 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8460 : 0;
8461 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8462 Temp tmp_dst = dst;
8463
8464 /* gather4 selects the component by dmask and always returns vec4 */
8465 if (instr->op == nir_texop_tg4) {
8466 assert(instr->dest.ssa.num_components == 4);
8467 if (instr->is_shadow)
8468 dmask = 1;
8469 else
8470 dmask = 1 << instr->component;
8471 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8472 tmp_dst = bld.tmp(v4);
8473 } else if (instr->op == nir_texop_samples_identical) {
8474 tmp_dst = bld.tmp(v1);
8475 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8476 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8477 }
8478
8479 aco_ptr<MIMG_instruction> tex;
8480 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8481 if (!has_lod)
8482 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8483
8484 bool div_by_6 = instr->op == nir_texop_txs &&
8485 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8486 instr->is_array &&
8487 (dmask & (1 << 2));
8488 if (tmp_dst.id() == dst.id() && div_by_6)
8489 tmp_dst = bld.tmp(tmp_dst.regClass());
8490
8491 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8492 tex->operands[0] = Operand(resource);
8493 tex->operands[1] = Operand(s4); /* no sampler */
8494 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8495 if (ctx->options->chip_class == GFX9 &&
8496 instr->op == nir_texop_txs &&
8497 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8498 instr->is_array) {
8499 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8500 } else if (instr->op == nir_texop_query_levels) {
8501 tex->dmask = 1 << 3;
8502 } else {
8503 tex->dmask = dmask;
8504 }
8505 tex->da = da;
8506 tex->definitions[0] = Definition(tmp_dst);
8507 tex->dim = dim;
8508 tex->can_reorder = true;
8509 ctx->block->instructions.emplace_back(std::move(tex));
8510
8511 if (div_by_6) {
8512 /* divide 3rd value by 6 by multiplying with magic number */
8513 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8514 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8515 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8516 assert(instr->dest.ssa.num_components == 3);
8517 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8518 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8519 emit_extract_vector(ctx, tmp_dst, 0, v1),
8520 emit_extract_vector(ctx, tmp_dst, 1, v1),
8521 by_6);
8522
8523 }
8524
8525 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8526 return;
8527 }
8528
8529 Temp tg4_compare_cube_wa64 = Temp();
8530
8531 if (tg4_integer_workarounds) {
8532 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8533 tex->operands[0] = Operand(resource);
8534 tex->operands[1] = Operand(s4); /* no sampler */
8535 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8536 tex->dim = dim;
8537 tex->dmask = 0x3;
8538 tex->da = da;
8539 Temp size = bld.tmp(v2);
8540 tex->definitions[0] = Definition(size);
8541 tex->can_reorder = true;
8542 ctx->block->instructions.emplace_back(std::move(tex));
8543 emit_split_vector(ctx, size, size.size());
8544
8545 Temp half_texel[2];
8546 for (unsigned i = 0; i < 2; i++) {
8547 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8548 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8549 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8550 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8551 }
8552
8553 Temp new_coords[2] = {
8554 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8555 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8556 };
8557
8558 if (tg4_integer_cube_workaround) {
8559 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8560 Temp desc[resource.size()];
8561 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8562 Format::PSEUDO, 1, resource.size())};
8563 split->operands[0] = Operand(resource);
8564 for (unsigned i = 0; i < resource.size(); i++) {
8565 desc[i] = bld.tmp(s1);
8566 split->definitions[i] = Definition(desc[i]);
8567 }
8568 ctx->block->instructions.emplace_back(std::move(split));
8569
8570 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8571 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8572 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8573
8574 Temp nfmt;
8575 if (stype == GLSL_TYPE_UINT) {
8576 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8577 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8578 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8579 bld.scc(compare_cube_wa));
8580 } else {
8581 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8582 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8583 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8584 bld.scc(compare_cube_wa));
8585 }
8586 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8587 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8588
8589 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8590
8591 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8592 Operand((uint32_t)C_008F14_NUM_FORMAT));
8593 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8594
8595 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8596 Format::PSEUDO, resource.size(), 1)};
8597 for (unsigned i = 0; i < resource.size(); i++)
8598 vec->operands[i] = Operand(desc[i]);
8599 resource = bld.tmp(resource.regClass());
8600 vec->definitions[0] = Definition(resource);
8601 ctx->block->instructions.emplace_back(std::move(vec));
8602
8603 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8604 new_coords[0], coords[0], tg4_compare_cube_wa64);
8605 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8606 new_coords[1], coords[1], tg4_compare_cube_wa64);
8607 }
8608 coords[0] = new_coords[0];
8609 coords[1] = new_coords[1];
8610 }
8611
8612 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8613 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8614
8615 assert(coords.size() == 1);
8616 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8617 aco_opcode op;
8618 switch (last_bit) {
8619 case 1:
8620 op = aco_opcode::buffer_load_format_x; break;
8621 case 2:
8622 op = aco_opcode::buffer_load_format_xy; break;
8623 case 3:
8624 op = aco_opcode::buffer_load_format_xyz; break;
8625 case 4:
8626 op = aco_opcode::buffer_load_format_xyzw; break;
8627 default:
8628 unreachable("Tex instruction loads more than 4 components.");
8629 }
8630
8631 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8632 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8633 tmp_dst = dst;
8634 else
8635 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8636
8637 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8638 mubuf->operands[0] = Operand(resource);
8639 mubuf->operands[1] = Operand(coords[0]);
8640 mubuf->operands[2] = Operand((uint32_t) 0);
8641 mubuf->definitions[0] = Definition(tmp_dst);
8642 mubuf->idxen = true;
8643 mubuf->can_reorder = true;
8644 ctx->block->instructions.emplace_back(std::move(mubuf));
8645
8646 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8647 return;
8648 }
8649
8650 /* gather MIMG address components */
8651 std::vector<Temp> args;
8652 if (has_offset)
8653 args.emplace_back(offset);
8654 if (has_bias)
8655 args.emplace_back(bias);
8656 if (has_compare)
8657 args.emplace_back(compare);
8658 if (has_derivs)
8659 args.insert(args.end(), derivs.begin(), derivs.end());
8660
8661 args.insert(args.end(), coords.begin(), coords.end());
8662 if (has_sample_index)
8663 args.emplace_back(sample_index);
8664 if (has_lod)
8665 args.emplace_back(lod);
8666
8667 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8668 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8669 vec->definitions[0] = Definition(arg);
8670 for (unsigned i = 0; i < args.size(); i++)
8671 vec->operands[i] = Operand(args[i]);
8672 ctx->block->instructions.emplace_back(std::move(vec));
8673
8674
8675 if (instr->op == nir_texop_txf ||
8676 instr->op == nir_texop_txf_ms ||
8677 instr->op == nir_texop_samples_identical ||
8678 instr->op == nir_texop_fragment_fetch ||
8679 instr->op == nir_texop_fragment_mask_fetch) {
8680 aco_opcode op = level_zero || instr->sampler_dim == GLSL_SAMPLER_DIM_MS || instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS ? aco_opcode::image_load : aco_opcode::image_load_mip;
8681 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8682 tex->operands[0] = Operand(resource);
8683 tex->operands[1] = Operand(s4); /* no sampler */
8684 tex->operands[2] = Operand(arg);
8685 tex->dim = dim;
8686 tex->dmask = dmask;
8687 tex->unrm = true;
8688 tex->da = da;
8689 tex->definitions[0] = Definition(tmp_dst);
8690 tex->can_reorder = true;
8691 ctx->block->instructions.emplace_back(std::move(tex));
8692
8693 if (instr->op == nir_texop_samples_identical) {
8694 assert(dmask == 1 && dst.regClass() == v1);
8695 assert(dst.id() != tmp_dst.id());
8696
8697 Temp tmp = bld.tmp(bld.lm);
8698 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8699 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8700
8701 } else {
8702 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8703 }
8704 return;
8705 }
8706
8707 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8708 aco_opcode opcode = aco_opcode::image_sample;
8709 if (has_offset) { /* image_sample_*_o */
8710 if (has_compare) {
8711 opcode = aco_opcode::image_sample_c_o;
8712 if (has_derivs)
8713 opcode = aco_opcode::image_sample_c_d_o;
8714 if (has_bias)
8715 opcode = aco_opcode::image_sample_c_b_o;
8716 if (level_zero)
8717 opcode = aco_opcode::image_sample_c_lz_o;
8718 if (has_lod)
8719 opcode = aco_opcode::image_sample_c_l_o;
8720 } else {
8721 opcode = aco_opcode::image_sample_o;
8722 if (has_derivs)
8723 opcode = aco_opcode::image_sample_d_o;
8724 if (has_bias)
8725 opcode = aco_opcode::image_sample_b_o;
8726 if (level_zero)
8727 opcode = aco_opcode::image_sample_lz_o;
8728 if (has_lod)
8729 opcode = aco_opcode::image_sample_l_o;
8730 }
8731 } else { /* no offset */
8732 if (has_compare) {
8733 opcode = aco_opcode::image_sample_c;
8734 if (has_derivs)
8735 opcode = aco_opcode::image_sample_c_d;
8736 if (has_bias)
8737 opcode = aco_opcode::image_sample_c_b;
8738 if (level_zero)
8739 opcode = aco_opcode::image_sample_c_lz;
8740 if (has_lod)
8741 opcode = aco_opcode::image_sample_c_l;
8742 } else {
8743 opcode = aco_opcode::image_sample;
8744 if (has_derivs)
8745 opcode = aco_opcode::image_sample_d;
8746 if (has_bias)
8747 opcode = aco_opcode::image_sample_b;
8748 if (level_zero)
8749 opcode = aco_opcode::image_sample_lz;
8750 if (has_lod)
8751 opcode = aco_opcode::image_sample_l;
8752 }
8753 }
8754
8755 if (instr->op == nir_texop_tg4) {
8756 if (has_offset) {
8757 opcode = aco_opcode::image_gather4_lz_o;
8758 if (has_compare)
8759 opcode = aco_opcode::image_gather4_c_lz_o;
8760 } else {
8761 opcode = aco_opcode::image_gather4_lz;
8762 if (has_compare)
8763 opcode = aco_opcode::image_gather4_c_lz;
8764 }
8765 } else if (instr->op == nir_texop_lod) {
8766 opcode = aco_opcode::image_get_lod;
8767 }
8768
8769 /* we don't need the bias, sample index, compare value or offset to be
8770 * computed in WQM but if the p_create_vector copies the coordinates, then it
8771 * needs to be in WQM */
8772 if (ctx->stage == fragment_fs &&
8773 !has_derivs && !has_lod && !level_zero &&
8774 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8775 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8776 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8777
8778 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8779 tex->operands[0] = Operand(resource);
8780 tex->operands[1] = Operand(sampler);
8781 tex->operands[2] = Operand(arg);
8782 tex->dim = dim;
8783 tex->dmask = dmask;
8784 tex->da = da;
8785 tex->definitions[0] = Definition(tmp_dst);
8786 tex->can_reorder = true;
8787 ctx->block->instructions.emplace_back(std::move(tex));
8788
8789 if (tg4_integer_cube_workaround) {
8790 assert(tmp_dst.id() != dst.id());
8791 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8792
8793 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8794 Temp val[4];
8795 for (unsigned i = 0; i < dst.size(); i++) {
8796 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8797 Temp cvt_val;
8798 if (stype == GLSL_TYPE_UINT)
8799 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8800 else
8801 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8802 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8803 }
8804 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8805 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8806 val[0], val[1], val[2], val[3]);
8807 }
8808 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8809 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8810
8811 }
8812
8813
8814 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8815 {
8816 Temp tmp = get_ssa_temp(ctx, ssa);
8817 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8818 return Operand(tmp.regClass());
8819 else
8820 return Operand(tmp);
8821 }
8822
8823 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8824 {
8825 aco_ptr<Pseudo_instruction> phi;
8826 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8827 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8828
8829 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8830 logical |= ctx->block->kind & block_kind_merge;
8831 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8832
8833 /* we want a sorted list of sources, since the predecessor list is also sorted */
8834 std::map<unsigned, nir_ssa_def*> phi_src;
8835 nir_foreach_phi_src(src, instr)
8836 phi_src[src->pred->index] = src->src.ssa;
8837
8838 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8839 unsigned num_operands = 0;
8840 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8841 unsigned num_defined = 0;
8842 unsigned cur_pred_idx = 0;
8843 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8844 if (cur_pred_idx < preds.size()) {
8845 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8846 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8847 unsigned skipped = 0;
8848 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8849 skipped++;
8850 if (cur_pred_idx + skipped < preds.size()) {
8851 for (unsigned i = 0; i < skipped; i++)
8852 operands[num_operands++] = Operand(dst.regClass());
8853 cur_pred_idx += skipped;
8854 } else {
8855 continue;
8856 }
8857 }
8858 /* Handle missing predecessors at the end. This shouldn't happen with loop
8859 * headers and we can't ignore these sources for loop header phis. */
8860 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8861 continue;
8862 cur_pred_idx++;
8863 Operand op = get_phi_operand(ctx, src.second);
8864 operands[num_operands++] = op;
8865 num_defined += !op.isUndefined();
8866 }
8867 /* handle block_kind_continue_or_break at loop exit blocks */
8868 while (cur_pred_idx++ < preds.size())
8869 operands[num_operands++] = Operand(dst.regClass());
8870
8871 /* If the loop ends with a break, still add a linear continue edge in case
8872 * that break is divergent or continue_or_break is used. We'll either remove
8873 * this operand later in visit_loop() if it's not necessary or replace the
8874 * undef with something correct. */
8875 if (!logical && ctx->block->kind & block_kind_loop_header) {
8876 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8877 nir_block *last = nir_loop_last_block(loop);
8878 if (last->successors[0] != instr->instr.block)
8879 operands[num_operands++] = Operand(RegClass());
8880 }
8881
8882 if (num_defined == 0) {
8883 Builder bld(ctx->program, ctx->block);
8884 if (dst.regClass() == s1) {
8885 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8886 } else if (dst.regClass() == v1) {
8887 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8888 } else {
8889 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8890 for (unsigned i = 0; i < dst.size(); i++)
8891 vec->operands[i] = Operand(0u);
8892 vec->definitions[0] = Definition(dst);
8893 ctx->block->instructions.emplace_back(std::move(vec));
8894 }
8895 return;
8896 }
8897
8898 /* we can use a linear phi in some cases if one src is undef */
8899 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8900 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8901
8902 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8903 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8904 assert(invert->kind & block_kind_invert);
8905
8906 unsigned then_block = invert->linear_preds[0];
8907
8908 Block* insert_block = NULL;
8909 for (unsigned i = 0; i < num_operands; i++) {
8910 Operand op = operands[i];
8911 if (op.isUndefined())
8912 continue;
8913 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8914 phi->operands[0] = op;
8915 break;
8916 }
8917 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8918 phi->operands[1] = Operand(dst.regClass());
8919 phi->definitions[0] = Definition(dst);
8920 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8921 return;
8922 }
8923
8924 /* try to scalarize vector phis */
8925 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8926 // TODO: scalarize linear phis on divergent ifs
8927 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8928 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8929 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8930 Operand src = operands[i];
8931 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8932 can_scalarize = false;
8933 }
8934 if (can_scalarize) {
8935 unsigned num_components = instr->dest.ssa.num_components;
8936 assert(dst.size() % num_components == 0);
8937 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8938
8939 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8940 for (unsigned k = 0; k < num_components; k++) {
8941 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8942 for (unsigned i = 0; i < num_operands; i++) {
8943 Operand src = operands[i];
8944 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8945 }
8946 Temp phi_dst = {ctx->program->allocateId(), rc};
8947 phi->definitions[0] = Definition(phi_dst);
8948 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8949 new_vec[k] = phi_dst;
8950 vec->operands[k] = Operand(phi_dst);
8951 }
8952 vec->definitions[0] = Definition(dst);
8953 ctx->block->instructions.emplace_back(std::move(vec));
8954 ctx->allocated_vec.emplace(dst.id(), new_vec);
8955 return;
8956 }
8957 }
8958
8959 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8960 for (unsigned i = 0; i < num_operands; i++)
8961 phi->operands[i] = operands[i];
8962 phi->definitions[0] = Definition(dst);
8963 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8964 }
8965
8966
8967 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8968 {
8969 Temp dst = get_ssa_temp(ctx, &instr->def);
8970
8971 assert(dst.type() == RegType::sgpr);
8972
8973 if (dst.size() == 1) {
8974 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8975 } else {
8976 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8977 for (unsigned i = 0; i < dst.size(); i++)
8978 vec->operands[i] = Operand(0u);
8979 vec->definitions[0] = Definition(dst);
8980 ctx->block->instructions.emplace_back(std::move(vec));
8981 }
8982 }
8983
8984 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8985 {
8986 Builder bld(ctx->program, ctx->block);
8987 Block *logical_target;
8988 append_logical_end(ctx->block);
8989 unsigned idx = ctx->block->index;
8990
8991 switch (instr->type) {
8992 case nir_jump_break:
8993 logical_target = ctx->cf_info.parent_loop.exit;
8994 add_logical_edge(idx, logical_target);
8995 ctx->block->kind |= block_kind_break;
8996
8997 if (!ctx->cf_info.parent_if.is_divergent &&
8998 !ctx->cf_info.parent_loop.has_divergent_continue) {
8999 /* uniform break - directly jump out of the loop */
9000 ctx->block->kind |= block_kind_uniform;
9001 ctx->cf_info.has_branch = true;
9002 bld.branch(aco_opcode::p_branch);
9003 add_linear_edge(idx, logical_target);
9004 return;
9005 }
9006 ctx->cf_info.parent_loop.has_divergent_branch = true;
9007 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9008 break;
9009 case nir_jump_continue:
9010 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9011 add_logical_edge(idx, logical_target);
9012 ctx->block->kind |= block_kind_continue;
9013
9014 if (ctx->cf_info.parent_if.is_divergent) {
9015 /* for potential uniform breaks after this continue,
9016 we must ensure that they are handled correctly */
9017 ctx->cf_info.parent_loop.has_divergent_continue = true;
9018 ctx->cf_info.parent_loop.has_divergent_branch = true;
9019 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9020 } else {
9021 /* uniform continue - directly jump to the loop header */
9022 ctx->block->kind |= block_kind_uniform;
9023 ctx->cf_info.has_branch = true;
9024 bld.branch(aco_opcode::p_branch);
9025 add_linear_edge(idx, logical_target);
9026 return;
9027 }
9028 break;
9029 default:
9030 fprintf(stderr, "Unknown NIR jump instr: ");
9031 nir_print_instr(&instr->instr, stderr);
9032 fprintf(stderr, "\n");
9033 abort();
9034 }
9035
9036 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9037 ctx->cf_info.exec_potentially_empty_break = true;
9038 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9039 }
9040
9041 /* remove critical edges from linear CFG */
9042 bld.branch(aco_opcode::p_branch);
9043 Block* break_block = ctx->program->create_and_insert_block();
9044 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9045 break_block->kind |= block_kind_uniform;
9046 add_linear_edge(idx, break_block);
9047 /* the loop_header pointer might be invalidated by this point */
9048 if (instr->type == nir_jump_continue)
9049 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9050 add_linear_edge(break_block->index, logical_target);
9051 bld.reset(break_block);
9052 bld.branch(aco_opcode::p_branch);
9053
9054 Block* continue_block = ctx->program->create_and_insert_block();
9055 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9056 add_linear_edge(idx, continue_block);
9057 append_logical_start(continue_block);
9058 ctx->block = continue_block;
9059 return;
9060 }
9061
9062 void visit_block(isel_context *ctx, nir_block *block)
9063 {
9064 nir_foreach_instr(instr, block) {
9065 switch (instr->type) {
9066 case nir_instr_type_alu:
9067 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9068 break;
9069 case nir_instr_type_load_const:
9070 visit_load_const(ctx, nir_instr_as_load_const(instr));
9071 break;
9072 case nir_instr_type_intrinsic:
9073 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9074 break;
9075 case nir_instr_type_tex:
9076 visit_tex(ctx, nir_instr_as_tex(instr));
9077 break;
9078 case nir_instr_type_phi:
9079 visit_phi(ctx, nir_instr_as_phi(instr));
9080 break;
9081 case nir_instr_type_ssa_undef:
9082 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9083 break;
9084 case nir_instr_type_deref:
9085 break;
9086 case nir_instr_type_jump:
9087 visit_jump(ctx, nir_instr_as_jump(instr));
9088 break;
9089 default:
9090 fprintf(stderr, "Unknown NIR instr type: ");
9091 nir_print_instr(instr, stderr);
9092 fprintf(stderr, "\n");
9093 //abort();
9094 }
9095 }
9096
9097 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9098 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9099 }
9100
9101
9102
9103 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9104 aco_ptr<Instruction>& header_phi, Operand *vals)
9105 {
9106 vals[0] = Operand(header_phi->definitions[0].getTemp());
9107 RegClass rc = vals[0].regClass();
9108
9109 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9110
9111 unsigned next_pred = 1;
9112
9113 for (unsigned idx = first + 1; idx <= last; idx++) {
9114 Block& block = ctx->program->blocks[idx];
9115 if (block.loop_nest_depth != loop_nest_depth) {
9116 vals[idx - first] = vals[idx - 1 - first];
9117 continue;
9118 }
9119
9120 if (block.kind & block_kind_continue) {
9121 vals[idx - first] = header_phi->operands[next_pred];
9122 next_pred++;
9123 continue;
9124 }
9125
9126 bool all_same = true;
9127 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9128 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9129
9130 Operand val;
9131 if (all_same) {
9132 val = vals[block.linear_preds[0] - first];
9133 } else {
9134 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9135 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9136 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9137 phi->operands[i] = vals[block.linear_preds[i] - first];
9138 val = Operand(Temp(ctx->program->allocateId(), rc));
9139 phi->definitions[0] = Definition(val.getTemp());
9140 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9141 }
9142 vals[idx - first] = val;
9143 }
9144
9145 return vals[last - first];
9146 }
9147
9148 static void visit_loop(isel_context *ctx, nir_loop *loop)
9149 {
9150 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9151 append_logical_end(ctx->block);
9152 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9153 Builder bld(ctx->program, ctx->block);
9154 bld.branch(aco_opcode::p_branch);
9155 unsigned loop_preheader_idx = ctx->block->index;
9156
9157 Block loop_exit = Block();
9158 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9159 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9160
9161 Block* loop_header = ctx->program->create_and_insert_block();
9162 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9163 loop_header->kind |= block_kind_loop_header;
9164 add_edge(loop_preheader_idx, loop_header);
9165 ctx->block = loop_header;
9166
9167 /* emit loop body */
9168 unsigned loop_header_idx = loop_header->index;
9169 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9170 append_logical_start(ctx->block);
9171 bool unreachable = visit_cf_list(ctx, &loop->body);
9172
9173 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9174 if (!ctx->cf_info.has_branch) {
9175 append_logical_end(ctx->block);
9176 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9177 /* Discards can result in code running with an empty exec mask.
9178 * This would result in divergent breaks not ever being taken. As a
9179 * workaround, break the loop when the loop mask is empty instead of
9180 * always continuing. */
9181 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9182 unsigned block_idx = ctx->block->index;
9183
9184 /* create helper blocks to avoid critical edges */
9185 Block *break_block = ctx->program->create_and_insert_block();
9186 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9187 break_block->kind = block_kind_uniform;
9188 bld.reset(break_block);
9189 bld.branch(aco_opcode::p_branch);
9190 add_linear_edge(block_idx, break_block);
9191 add_linear_edge(break_block->index, &loop_exit);
9192
9193 Block *continue_block = ctx->program->create_and_insert_block();
9194 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9195 continue_block->kind = block_kind_uniform;
9196 bld.reset(continue_block);
9197 bld.branch(aco_opcode::p_branch);
9198 add_linear_edge(block_idx, continue_block);
9199 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9200
9201 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9202 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9203 ctx->block = &ctx->program->blocks[block_idx];
9204 } else {
9205 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9206 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9207 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9208 else
9209 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9210 }
9211
9212 bld.reset(ctx->block);
9213 bld.branch(aco_opcode::p_branch);
9214 }
9215
9216 /* Fixup phis in loop header from unreachable blocks.
9217 * has_branch/has_divergent_branch also indicates if the loop ends with a
9218 * break/continue instruction, but we don't emit those if unreachable=true */
9219 if (unreachable) {
9220 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9221 bool linear = ctx->cf_info.has_branch;
9222 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9223 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9224 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9225 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9226 /* the last operand should be the one that needs to be removed */
9227 instr->operands.pop_back();
9228 } else if (!is_phi(instr)) {
9229 break;
9230 }
9231 }
9232 }
9233
9234 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9235 * and the previous one shouldn't both happen at once because a break in the
9236 * merge block would get CSE'd */
9237 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9238 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9239 Operand vals[num_vals];
9240 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9241 if (instr->opcode == aco_opcode::p_linear_phi) {
9242 if (ctx->cf_info.has_branch)
9243 instr->operands.pop_back();
9244 else
9245 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9246 } else if (!is_phi(instr)) {
9247 break;
9248 }
9249 }
9250 }
9251
9252 ctx->cf_info.has_branch = false;
9253
9254 // TODO: if the loop has not a single exit, we must add one °°
9255 /* emit loop successor block */
9256 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9257 append_logical_start(ctx->block);
9258
9259 #if 0
9260 // TODO: check if it is beneficial to not branch on continues
9261 /* trim linear phis in loop header */
9262 for (auto&& instr : loop_entry->instructions) {
9263 if (instr->opcode == aco_opcode::p_linear_phi) {
9264 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9265 new_phi->definitions[0] = instr->definitions[0];
9266 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9267 new_phi->operands[i] = instr->operands[i];
9268 /* check that the remaining operands are all the same */
9269 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9270 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9271 instr.swap(new_phi);
9272 } else if (instr->opcode == aco_opcode::p_phi) {
9273 continue;
9274 } else {
9275 break;
9276 }
9277 }
9278 #endif
9279 }
9280
9281 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9282 {
9283 ic->cond = cond;
9284
9285 append_logical_end(ctx->block);
9286 ctx->block->kind |= block_kind_branch;
9287
9288 /* branch to linear then block */
9289 assert(cond.regClass() == ctx->program->lane_mask);
9290 aco_ptr<Pseudo_branch_instruction> branch;
9291 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9292 branch->operands[0] = Operand(cond);
9293 ctx->block->instructions.push_back(std::move(branch));
9294
9295 ic->BB_if_idx = ctx->block->index;
9296 ic->BB_invert = Block();
9297 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9298 /* Invert blocks are intentionally not marked as top level because they
9299 * are not part of the logical cfg. */
9300 ic->BB_invert.kind |= block_kind_invert;
9301 ic->BB_endif = Block();
9302 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9303 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9304
9305 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9306 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9307 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9308 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9309 ctx->cf_info.parent_if.is_divergent = true;
9310
9311 /* divergent branches use cbranch_execz */
9312 ctx->cf_info.exec_potentially_empty_discard = false;
9313 ctx->cf_info.exec_potentially_empty_break = false;
9314 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9315
9316 /** emit logical then block */
9317 Block* BB_then_logical = ctx->program->create_and_insert_block();
9318 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9319 add_edge(ic->BB_if_idx, BB_then_logical);
9320 ctx->block = BB_then_logical;
9321 append_logical_start(BB_then_logical);
9322 }
9323
9324 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9325 {
9326 Block *BB_then_logical = ctx->block;
9327 append_logical_end(BB_then_logical);
9328 /* branch from logical then block to invert block */
9329 aco_ptr<Pseudo_branch_instruction> branch;
9330 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9331 BB_then_logical->instructions.emplace_back(std::move(branch));
9332 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9333 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9334 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9335 BB_then_logical->kind |= block_kind_uniform;
9336 assert(!ctx->cf_info.has_branch);
9337 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9338 ctx->cf_info.parent_loop.has_divergent_branch = false;
9339
9340 /** emit linear then block */
9341 Block* BB_then_linear = ctx->program->create_and_insert_block();
9342 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9343 BB_then_linear->kind |= block_kind_uniform;
9344 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9345 /* branch from linear then block to invert block */
9346 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9347 BB_then_linear->instructions.emplace_back(std::move(branch));
9348 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9349
9350 /** emit invert merge block */
9351 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9352 ic->invert_idx = ctx->block->index;
9353
9354 /* branch to linear else block (skip else) */
9355 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9356 branch->operands[0] = Operand(ic->cond);
9357 ctx->block->instructions.push_back(std::move(branch));
9358
9359 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9360 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9361 ic->exec_potentially_empty_break_depth_old =
9362 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9363 /* divergent branches use cbranch_execz */
9364 ctx->cf_info.exec_potentially_empty_discard = false;
9365 ctx->cf_info.exec_potentially_empty_break = false;
9366 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9367
9368 /** emit logical else block */
9369 Block* BB_else_logical = ctx->program->create_and_insert_block();
9370 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9371 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9372 add_linear_edge(ic->invert_idx, BB_else_logical);
9373 ctx->block = BB_else_logical;
9374 append_logical_start(BB_else_logical);
9375 }
9376
9377 static void end_divergent_if(isel_context *ctx, if_context *ic)
9378 {
9379 Block *BB_else_logical = ctx->block;
9380 append_logical_end(BB_else_logical);
9381
9382 /* branch from logical else block to endif block */
9383 aco_ptr<Pseudo_branch_instruction> branch;
9384 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9385 BB_else_logical->instructions.emplace_back(std::move(branch));
9386 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9387 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9388 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9389 BB_else_logical->kind |= block_kind_uniform;
9390
9391 assert(!ctx->cf_info.has_branch);
9392 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9393
9394
9395 /** emit linear else block */
9396 Block* BB_else_linear = ctx->program->create_and_insert_block();
9397 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9398 BB_else_linear->kind |= block_kind_uniform;
9399 add_linear_edge(ic->invert_idx, BB_else_linear);
9400
9401 /* branch from linear else block to endif block */
9402 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9403 BB_else_linear->instructions.emplace_back(std::move(branch));
9404 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9405
9406
9407 /** emit endif merge block */
9408 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9409 append_logical_start(ctx->block);
9410
9411
9412 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9413 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9414 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9415 ctx->cf_info.exec_potentially_empty_break_depth =
9416 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9417 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9418 !ctx->cf_info.parent_if.is_divergent) {
9419 ctx->cf_info.exec_potentially_empty_break = false;
9420 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9421 }
9422 /* uniform control flow never has an empty exec-mask */
9423 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9424 ctx->cf_info.exec_potentially_empty_discard = false;
9425 ctx->cf_info.exec_potentially_empty_break = false;
9426 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9427 }
9428 }
9429
9430 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9431 {
9432 assert(cond.regClass() == s1);
9433
9434 append_logical_end(ctx->block);
9435 ctx->block->kind |= block_kind_uniform;
9436
9437 aco_ptr<Pseudo_branch_instruction> branch;
9438 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9439 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9440 branch->operands[0] = Operand(cond);
9441 branch->operands[0].setFixed(scc);
9442 ctx->block->instructions.emplace_back(std::move(branch));
9443
9444 ic->BB_if_idx = ctx->block->index;
9445 ic->BB_endif = Block();
9446 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9447 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9448
9449 ctx->cf_info.has_branch = false;
9450 ctx->cf_info.parent_loop.has_divergent_branch = false;
9451
9452 /** emit then block */
9453 Block* BB_then = ctx->program->create_and_insert_block();
9454 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9455 add_edge(ic->BB_if_idx, BB_then);
9456 append_logical_start(BB_then);
9457 ctx->block = BB_then;
9458 }
9459
9460 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9461 {
9462 Block *BB_then = ctx->block;
9463
9464 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9465 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9466
9467 if (!ic->uniform_has_then_branch) {
9468 append_logical_end(BB_then);
9469 /* branch from then block to endif block */
9470 aco_ptr<Pseudo_branch_instruction> branch;
9471 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9472 BB_then->instructions.emplace_back(std::move(branch));
9473 add_linear_edge(BB_then->index, &ic->BB_endif);
9474 if (!ic->then_branch_divergent)
9475 add_logical_edge(BB_then->index, &ic->BB_endif);
9476 BB_then->kind |= block_kind_uniform;
9477 }
9478
9479 ctx->cf_info.has_branch = false;
9480 ctx->cf_info.parent_loop.has_divergent_branch = false;
9481
9482 /** emit else block */
9483 Block* BB_else = ctx->program->create_and_insert_block();
9484 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9485 add_edge(ic->BB_if_idx, BB_else);
9486 append_logical_start(BB_else);
9487 ctx->block = BB_else;
9488 }
9489
9490 static void end_uniform_if(isel_context *ctx, if_context *ic)
9491 {
9492 Block *BB_else = ctx->block;
9493
9494 if (!ctx->cf_info.has_branch) {
9495 append_logical_end(BB_else);
9496 /* branch from then block to endif block */
9497 aco_ptr<Pseudo_branch_instruction> branch;
9498 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9499 BB_else->instructions.emplace_back(std::move(branch));
9500 add_linear_edge(BB_else->index, &ic->BB_endif);
9501 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9502 add_logical_edge(BB_else->index, &ic->BB_endif);
9503 BB_else->kind |= block_kind_uniform;
9504 }
9505
9506 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9507 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9508
9509 /** emit endif merge block */
9510 if (!ctx->cf_info.has_branch) {
9511 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9512 append_logical_start(ctx->block);
9513 }
9514 }
9515
9516 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9517 {
9518 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9519 Builder bld(ctx->program, ctx->block);
9520 aco_ptr<Pseudo_branch_instruction> branch;
9521 if_context ic;
9522
9523 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9524 /**
9525 * Uniform conditionals are represented in the following way*) :
9526 *
9527 * The linear and logical CFG:
9528 * BB_IF
9529 * / \
9530 * BB_THEN (logical) BB_ELSE (logical)
9531 * \ /
9532 * BB_ENDIF
9533 *
9534 * *) Exceptions may be due to break and continue statements within loops
9535 * If a break/continue happens within uniform control flow, it branches
9536 * to the loop exit/entry block. Otherwise, it branches to the next
9537 * merge block.
9538 **/
9539
9540 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9541 assert(cond.regClass() == ctx->program->lane_mask);
9542 cond = bool_to_scalar_condition(ctx, cond);
9543
9544 begin_uniform_if_then(ctx, &ic, cond);
9545 visit_cf_list(ctx, &if_stmt->then_list);
9546
9547 begin_uniform_if_else(ctx, &ic);
9548 visit_cf_list(ctx, &if_stmt->else_list);
9549
9550 end_uniform_if(ctx, &ic);
9551
9552 return !ctx->cf_info.has_branch;
9553 } else { /* non-uniform condition */
9554 /**
9555 * To maintain a logical and linear CFG without critical edges,
9556 * non-uniform conditionals are represented in the following way*) :
9557 *
9558 * The linear CFG:
9559 * BB_IF
9560 * / \
9561 * BB_THEN (logical) BB_THEN (linear)
9562 * \ /
9563 * BB_INVERT (linear)
9564 * / \
9565 * BB_ELSE (logical) BB_ELSE (linear)
9566 * \ /
9567 * BB_ENDIF
9568 *
9569 * The logical CFG:
9570 * BB_IF
9571 * / \
9572 * BB_THEN (logical) BB_ELSE (logical)
9573 * \ /
9574 * BB_ENDIF
9575 *
9576 * *) Exceptions may be due to break and continue statements within loops
9577 **/
9578
9579 begin_divergent_if_then(ctx, &ic, cond);
9580 visit_cf_list(ctx, &if_stmt->then_list);
9581
9582 begin_divergent_if_else(ctx, &ic);
9583 visit_cf_list(ctx, &if_stmt->else_list);
9584
9585 end_divergent_if(ctx, &ic);
9586
9587 return true;
9588 }
9589 }
9590
9591 static bool visit_cf_list(isel_context *ctx,
9592 struct exec_list *list)
9593 {
9594 foreach_list_typed(nir_cf_node, node, node, list) {
9595 switch (node->type) {
9596 case nir_cf_node_block:
9597 visit_block(ctx, nir_cf_node_as_block(node));
9598 break;
9599 case nir_cf_node_if:
9600 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9601 return true;
9602 break;
9603 case nir_cf_node_loop:
9604 visit_loop(ctx, nir_cf_node_as_loop(node));
9605 break;
9606 default:
9607 unreachable("unimplemented cf list type");
9608 }
9609 }
9610 return false;
9611 }
9612
9613 static void create_null_export(isel_context *ctx)
9614 {
9615 /* Some shader stages always need to have exports.
9616 * So when there is none, we need to add a null export.
9617 */
9618
9619 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9620 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9621 Builder bld(ctx->program, ctx->block);
9622 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9623 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9624 }
9625
9626 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9627 {
9628 assert(ctx->stage == vertex_vs ||
9629 ctx->stage == tess_eval_vs ||
9630 ctx->stage == gs_copy_vs ||
9631 ctx->stage == ngg_vertex_gs ||
9632 ctx->stage == ngg_tess_eval_gs);
9633
9634 int offset = (ctx->stage & sw_tes)
9635 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9636 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9637 uint64_t mask = ctx->outputs.mask[slot];
9638 if (!is_pos && !mask)
9639 return false;
9640 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9641 return false;
9642 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9643 exp->enabled_mask = mask;
9644 for (unsigned i = 0; i < 4; ++i) {
9645 if (mask & (1 << i))
9646 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9647 else
9648 exp->operands[i] = Operand(v1);
9649 }
9650 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9651 * Setting valid_mask=1 prevents it and has no other effect.
9652 */
9653 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9654 exp->done = false;
9655 exp->compressed = false;
9656 if (is_pos)
9657 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9658 else
9659 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9660 ctx->block->instructions.emplace_back(std::move(exp));
9661
9662 return true;
9663 }
9664
9665 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9666 {
9667 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9668 exp->enabled_mask = 0;
9669 for (unsigned i = 0; i < 4; ++i)
9670 exp->operands[i] = Operand(v1);
9671 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9672 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9673 exp->enabled_mask |= 0x1;
9674 }
9675 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9676 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9677 exp->enabled_mask |= 0x4;
9678 }
9679 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9680 if (ctx->options->chip_class < GFX9) {
9681 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9682 exp->enabled_mask |= 0x8;
9683 } else {
9684 Builder bld(ctx->program, ctx->block);
9685
9686 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9687 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9688 if (exp->operands[2].isTemp())
9689 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9690
9691 exp->operands[2] = Operand(out);
9692 exp->enabled_mask |= 0x4;
9693 }
9694 }
9695 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9696 exp->done = false;
9697 exp->compressed = false;
9698 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9699 ctx->block->instructions.emplace_back(std::move(exp));
9700 }
9701
9702 static void create_export_phis(isel_context *ctx)
9703 {
9704 /* Used when exports are needed, but the output temps are defined in a preceding block.
9705 * This function will set up phis in order to access the outputs in the next block.
9706 */
9707
9708 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9709 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9710 ctx->block->instructions.pop_back();
9711
9712 Builder bld(ctx->program, ctx->block);
9713
9714 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9715 uint64_t mask = ctx->outputs.mask[slot];
9716 for (unsigned i = 0; i < 4; ++i) {
9717 if (!(mask & (1 << i)))
9718 continue;
9719
9720 Temp old = ctx->outputs.temps[slot * 4 + i];
9721 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9722 ctx->outputs.temps[slot * 4 + i] = phi;
9723 }
9724 }
9725
9726 bld.insert(std::move(logical_start));
9727 }
9728
9729 static void create_vs_exports(isel_context *ctx)
9730 {
9731 assert(ctx->stage == vertex_vs ||
9732 ctx->stage == tess_eval_vs ||
9733 ctx->stage == gs_copy_vs ||
9734 ctx->stage == ngg_vertex_gs ||
9735 ctx->stage == ngg_tess_eval_gs);
9736
9737 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9738 ? &ctx->program->info->tes.outinfo
9739 : &ctx->program->info->vs.outinfo;
9740
9741 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9742 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9743 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9744 }
9745
9746 if (ctx->options->key.has_multiview_view_index) {
9747 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9748 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9749 }
9750
9751 /* the order these position exports are created is important */
9752 int next_pos = 0;
9753 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9754 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9755 export_vs_psiz_layer_viewport(ctx, &next_pos);
9756 exported_pos = true;
9757 }
9758 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9759 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9760 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9761 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9762
9763 if (ctx->export_clip_dists) {
9764 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9765 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9766 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9767 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9768 }
9769
9770 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9771 if (i < VARYING_SLOT_VAR0 &&
9772 i != VARYING_SLOT_LAYER &&
9773 i != VARYING_SLOT_PRIMITIVE_ID &&
9774 i != VARYING_SLOT_VIEWPORT)
9775 continue;
9776
9777 export_vs_varying(ctx, i, false, NULL);
9778 }
9779
9780 if (!exported_pos)
9781 create_null_export(ctx);
9782 }
9783
9784 static bool export_fs_mrt_z(isel_context *ctx)
9785 {
9786 Builder bld(ctx->program, ctx->block);
9787 unsigned enabled_channels = 0;
9788 bool compr = false;
9789 Operand values[4];
9790
9791 for (unsigned i = 0; i < 4; ++i) {
9792 values[i] = Operand(v1);
9793 }
9794
9795 /* Both stencil and sample mask only need 16-bits. */
9796 if (!ctx->program->info->ps.writes_z &&
9797 (ctx->program->info->ps.writes_stencil ||
9798 ctx->program->info->ps.writes_sample_mask)) {
9799 compr = true; /* COMPR flag */
9800
9801 if (ctx->program->info->ps.writes_stencil) {
9802 /* Stencil should be in X[23:16]. */
9803 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9804 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9805 enabled_channels |= 0x3;
9806 }
9807
9808 if (ctx->program->info->ps.writes_sample_mask) {
9809 /* SampleMask should be in Y[15:0]. */
9810 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9811 enabled_channels |= 0xc;
9812 }
9813 } else {
9814 if (ctx->program->info->ps.writes_z) {
9815 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9816 enabled_channels |= 0x1;
9817 }
9818
9819 if (ctx->program->info->ps.writes_stencil) {
9820 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9821 enabled_channels |= 0x2;
9822 }
9823
9824 if (ctx->program->info->ps.writes_sample_mask) {
9825 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9826 enabled_channels |= 0x4;
9827 }
9828 }
9829
9830 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9831 * writemask component.
9832 */
9833 if (ctx->options->chip_class == GFX6 &&
9834 ctx->options->family != CHIP_OLAND &&
9835 ctx->options->family != CHIP_HAINAN) {
9836 enabled_channels |= 0x1;
9837 }
9838
9839 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9840 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9841
9842 return true;
9843 }
9844
9845 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9846 {
9847 Builder bld(ctx->program, ctx->block);
9848 unsigned write_mask = ctx->outputs.mask[slot];
9849 Operand values[4];
9850
9851 for (unsigned i = 0; i < 4; ++i) {
9852 if (write_mask & (1 << i)) {
9853 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9854 } else {
9855 values[i] = Operand(v1);
9856 }
9857 }
9858
9859 unsigned target, col_format;
9860 unsigned enabled_channels = 0;
9861 aco_opcode compr_op = (aco_opcode)0;
9862
9863 slot -= FRAG_RESULT_DATA0;
9864 target = V_008DFC_SQ_EXP_MRT + slot;
9865 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9866
9867 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9868 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9869
9870 switch (col_format)
9871 {
9872 case V_028714_SPI_SHADER_ZERO:
9873 enabled_channels = 0; /* writemask */
9874 target = V_008DFC_SQ_EXP_NULL;
9875 break;
9876
9877 case V_028714_SPI_SHADER_32_R:
9878 enabled_channels = 1;
9879 break;
9880
9881 case V_028714_SPI_SHADER_32_GR:
9882 enabled_channels = 0x3;
9883 break;
9884
9885 case V_028714_SPI_SHADER_32_AR:
9886 if (ctx->options->chip_class >= GFX10) {
9887 /* Special case: on GFX10, the outputs are different for 32_AR */
9888 enabled_channels = 0x3;
9889 values[1] = values[3];
9890 values[3] = Operand(v1);
9891 } else {
9892 enabled_channels = 0x9;
9893 }
9894 break;
9895
9896 case V_028714_SPI_SHADER_FP16_ABGR:
9897 enabled_channels = 0x5;
9898 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9899 break;
9900
9901 case V_028714_SPI_SHADER_UNORM16_ABGR:
9902 enabled_channels = 0x5;
9903 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9904 break;
9905
9906 case V_028714_SPI_SHADER_SNORM16_ABGR:
9907 enabled_channels = 0x5;
9908 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9909 break;
9910
9911 case V_028714_SPI_SHADER_UINT16_ABGR: {
9912 enabled_channels = 0x5;
9913 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9914 if (is_int8 || is_int10) {
9915 /* clamp */
9916 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9917 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9918
9919 for (unsigned i = 0; i < 4; i++) {
9920 if ((write_mask >> i) & 1) {
9921 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9922 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9923 values[i]);
9924 }
9925 }
9926 }
9927 break;
9928 }
9929
9930 case V_028714_SPI_SHADER_SINT16_ABGR:
9931 enabled_channels = 0x5;
9932 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9933 if (is_int8 || is_int10) {
9934 /* clamp */
9935 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9936 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9937 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9938 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9939
9940 for (unsigned i = 0; i < 4; i++) {
9941 if ((write_mask >> i) & 1) {
9942 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9943 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9944 values[i]);
9945 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9946 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9947 values[i]);
9948 }
9949 }
9950 }
9951 break;
9952
9953 case V_028714_SPI_SHADER_32_ABGR:
9954 enabled_channels = 0xF;
9955 break;
9956
9957 default:
9958 break;
9959 }
9960
9961 if (target == V_008DFC_SQ_EXP_NULL)
9962 return false;
9963
9964 if ((bool) compr_op) {
9965 for (int i = 0; i < 2; i++) {
9966 /* check if at least one of the values to be compressed is enabled */
9967 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9968 if (enabled) {
9969 enabled_channels |= enabled << (i*2);
9970 values[i] = bld.vop3(compr_op, bld.def(v1),
9971 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9972 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9973 } else {
9974 values[i] = Operand(v1);
9975 }
9976 }
9977 values[2] = Operand(v1);
9978 values[3] = Operand(v1);
9979 } else {
9980 for (int i = 0; i < 4; i++)
9981 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
9982 }
9983
9984 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9985 enabled_channels, target, (bool) compr_op);
9986 return true;
9987 }
9988
9989 static void create_fs_exports(isel_context *ctx)
9990 {
9991 bool exported = false;
9992
9993 /* Export depth, stencil and sample mask. */
9994 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
9995 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
9996 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
9997 exported |= export_fs_mrt_z(ctx);
9998
9999 /* Export all color render targets. */
10000 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10001 if (ctx->outputs.mask[i])
10002 exported |= export_fs_mrt_color(ctx, i);
10003
10004 if (!exported)
10005 create_null_export(ctx);
10006 }
10007
10008 static void write_tcs_tess_factors(isel_context *ctx)
10009 {
10010 unsigned outer_comps;
10011 unsigned inner_comps;
10012
10013 switch (ctx->args->options->key.tcs.primitive_mode) {
10014 case GL_ISOLINES:
10015 outer_comps = 2;
10016 inner_comps = 0;
10017 break;
10018 case GL_TRIANGLES:
10019 outer_comps = 3;
10020 inner_comps = 1;
10021 break;
10022 case GL_QUADS:
10023 outer_comps = 4;
10024 inner_comps = 2;
10025 break;
10026 default:
10027 return;
10028 }
10029
10030 Builder bld(ctx->program, ctx->block);
10031
10032 bld.barrier(aco_opcode::p_memory_barrier_shared);
10033 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10034 bld.sopp(aco_opcode::s_barrier);
10035
10036 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10037 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10038
10039 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10040 if_context ic_invocation_id_is_zero;
10041 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10042 bld.reset(ctx->block);
10043
10044 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));
10045
10046 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10047 unsigned stride = inner_comps + outer_comps;
10048 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10049 Temp tf_inner_vec;
10050 Temp tf_outer_vec;
10051 Temp out[6];
10052 assert(stride <= (sizeof(out) / sizeof(Temp)));
10053
10054 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10055 // LINES reversal
10056 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10057 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10058 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10059 } else {
10060 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);
10061 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);
10062
10063 for (unsigned i = 0; i < outer_comps; ++i)
10064 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10065 for (unsigned i = 0; i < inner_comps; ++i)
10066 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10067 }
10068
10069 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10070 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10071 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10072 unsigned tf_const_offset = 0;
10073
10074 if (ctx->program->chip_class <= GFX8) {
10075 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);
10076 if_context ic_rel_patch_id_is_zero;
10077 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10078 bld.reset(ctx->block);
10079
10080 /* Store the dynamic HS control word. */
10081 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10082 bld.mubuf(aco_opcode::buffer_store_dword,
10083 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10084 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10085 /* disable_wqm */ false, /* glc */ true);
10086 tf_const_offset += 4;
10087
10088 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10089 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10090 bld.reset(ctx->block);
10091 }
10092
10093 assert(stride == 2 || stride == 4 || stride == 6);
10094 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10095 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10096
10097 /* Store to offchip for TES to read - only if TES reads them */
10098 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10099 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));
10100 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10101
10102 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10103 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);
10104
10105 if (likely(inner_comps)) {
10106 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10107 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);
10108 }
10109 }
10110
10111 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10112 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10113 }
10114
10115 static void emit_stream_output(isel_context *ctx,
10116 Temp const *so_buffers,
10117 Temp const *so_write_offset,
10118 const struct radv_stream_output *output)
10119 {
10120 unsigned num_comps = util_bitcount(output->component_mask);
10121 unsigned writemask = (1 << num_comps) - 1;
10122 unsigned loc = output->location;
10123 unsigned buf = output->buffer;
10124
10125 assert(num_comps && num_comps <= 4);
10126 if (!num_comps || num_comps > 4)
10127 return;
10128
10129 unsigned start = ffs(output->component_mask) - 1;
10130
10131 Temp out[4];
10132 bool all_undef = true;
10133 assert(ctx->stage & hw_vs);
10134 for (unsigned i = 0; i < num_comps; i++) {
10135 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10136 all_undef = all_undef && !out[i].id();
10137 }
10138 if (all_undef)
10139 return;
10140
10141 while (writemask) {
10142 int start, count;
10143 u_bit_scan_consecutive_range(&writemask, &start, &count);
10144 if (count == 3 && ctx->options->chip_class == GFX6) {
10145 /* GFX6 doesn't support storing vec3, split it. */
10146 writemask |= 1u << (start + 2);
10147 count = 2;
10148 }
10149
10150 unsigned offset = output->offset + start * 4;
10151
10152 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10153 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10154 for (int i = 0; i < count; ++i)
10155 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10156 vec->definitions[0] = Definition(write_data);
10157 ctx->block->instructions.emplace_back(std::move(vec));
10158
10159 aco_opcode opcode;
10160 switch (count) {
10161 case 1:
10162 opcode = aco_opcode::buffer_store_dword;
10163 break;
10164 case 2:
10165 opcode = aco_opcode::buffer_store_dwordx2;
10166 break;
10167 case 3:
10168 opcode = aco_opcode::buffer_store_dwordx3;
10169 break;
10170 case 4:
10171 opcode = aco_opcode::buffer_store_dwordx4;
10172 break;
10173 default:
10174 unreachable("Unsupported dword count.");
10175 }
10176
10177 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10178 store->operands[0] = Operand(so_buffers[buf]);
10179 store->operands[1] = Operand(so_write_offset[buf]);
10180 store->operands[2] = Operand((uint32_t) 0);
10181 store->operands[3] = Operand(write_data);
10182 if (offset > 4095) {
10183 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10184 Builder bld(ctx->program, ctx->block);
10185 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10186 } else {
10187 store->offset = offset;
10188 }
10189 store->offen = true;
10190 store->glc = true;
10191 store->dlc = false;
10192 store->slc = true;
10193 store->can_reorder = true;
10194 ctx->block->instructions.emplace_back(std::move(store));
10195 }
10196 }
10197
10198 static void emit_streamout(isel_context *ctx, unsigned stream)
10199 {
10200 Builder bld(ctx->program, ctx->block);
10201
10202 Temp so_buffers[4];
10203 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10204 for (unsigned i = 0; i < 4; i++) {
10205 unsigned stride = ctx->program->info->so.strides[i];
10206 if (!stride)
10207 continue;
10208
10209 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10210 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10211 }
10212
10213 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10214 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10215
10216 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10217
10218 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10219
10220 if_context ic;
10221 begin_divergent_if_then(ctx, &ic, can_emit);
10222
10223 bld.reset(ctx->block);
10224
10225 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10226
10227 Temp so_write_offset[4];
10228
10229 for (unsigned i = 0; i < 4; i++) {
10230 unsigned stride = ctx->program->info->so.strides[i];
10231 if (!stride)
10232 continue;
10233
10234 if (stride == 1) {
10235 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10236 get_arg(ctx, ctx->args->streamout_write_idx),
10237 get_arg(ctx, ctx->args->streamout_offset[i]));
10238 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10239
10240 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10241 } else {
10242 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10243 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10244 get_arg(ctx, ctx->args->streamout_offset[i]));
10245 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10246 }
10247 }
10248
10249 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10250 struct radv_stream_output *output =
10251 &ctx->program->info->so.outputs[i];
10252 if (stream != output->stream)
10253 continue;
10254
10255 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10256 }
10257
10258 begin_divergent_if_else(ctx, &ic);
10259 end_divergent_if(ctx, &ic);
10260 }
10261
10262 } /* end namespace */
10263
10264 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10265 {
10266 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10267 Builder bld(ctx->program, ctx->block);
10268 constexpr unsigned hs_idx = 1u;
10269 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10270 get_arg(ctx, ctx->args->merged_wave_info),
10271 Operand((8u << 16) | (hs_idx * 8u)));
10272 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10273
10274 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10275
10276 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10277 get_arg(ctx, ctx->args->rel_auto_id),
10278 get_arg(ctx, ctx->args->ac.instance_id),
10279 ls_has_nonzero_hs_threads);
10280 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10281 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10282 get_arg(ctx, ctx->args->rel_auto_id),
10283 ls_has_nonzero_hs_threads);
10284 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10285 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10286 get_arg(ctx, ctx->args->ac.vertex_id),
10287 ls_has_nonzero_hs_threads);
10288
10289 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10290 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10291 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10292 }
10293
10294 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10295 {
10296 /* Split all arguments except for the first (ring_offsets) and the last
10297 * (exec) so that the dead channels don't stay live throughout the program.
10298 */
10299 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10300 if (startpgm->definitions[i].regClass().size() > 1) {
10301 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10302 startpgm->definitions[i].regClass().size());
10303 }
10304 }
10305 }
10306
10307 void handle_bc_optimize(isel_context *ctx)
10308 {
10309 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10310 Builder bld(ctx->program, ctx->block);
10311 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10312 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10313 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10314 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10315 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10316 if (uses_center && uses_centroid) {
10317 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10318 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10319
10320 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10321 Temp new_coord[2];
10322 for (unsigned i = 0; i < 2; i++) {
10323 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10324 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10325 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10326 persp_centroid, persp_center, sel);
10327 }
10328 ctx->persp_centroid = bld.tmp(v2);
10329 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10330 Operand(new_coord[0]), Operand(new_coord[1]));
10331 emit_split_vector(ctx, ctx->persp_centroid, 2);
10332 }
10333
10334 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10335 Temp new_coord[2];
10336 for (unsigned i = 0; i < 2; i++) {
10337 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10338 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10339 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10340 linear_centroid, linear_center, sel);
10341 }
10342 ctx->linear_centroid = bld.tmp(v2);
10343 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10344 Operand(new_coord[0]), Operand(new_coord[1]));
10345 emit_split_vector(ctx, ctx->linear_centroid, 2);
10346 }
10347 }
10348 }
10349
10350 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10351 {
10352 Program *program = ctx->program;
10353
10354 unsigned float_controls = shader->info.float_controls_execution_mode;
10355
10356 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10357 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10358 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10359 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10360 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10361
10362 program->next_fp_mode.must_flush_denorms32 =
10363 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10364 program->next_fp_mode.must_flush_denorms16_64 =
10365 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10366 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10367
10368 program->next_fp_mode.care_about_round32 =
10369 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10370
10371 program->next_fp_mode.care_about_round16_64 =
10372 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10373 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10374
10375 /* default to preserving fp16 and fp64 denorms, since it's free */
10376 if (program->next_fp_mode.must_flush_denorms16_64)
10377 program->next_fp_mode.denorm16_64 = 0;
10378 else
10379 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10380
10381 /* preserving fp32 denorms is expensive, so only do it if asked */
10382 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10383 program->next_fp_mode.denorm32 = fp_denorm_keep;
10384 else
10385 program->next_fp_mode.denorm32 = 0;
10386
10387 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10388 program->next_fp_mode.round32 = fp_round_tz;
10389 else
10390 program->next_fp_mode.round32 = fp_round_ne;
10391
10392 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10393 program->next_fp_mode.round16_64 = fp_round_tz;
10394 else
10395 program->next_fp_mode.round16_64 = fp_round_ne;
10396
10397 ctx->block->fp_mode = program->next_fp_mode;
10398 }
10399
10400 void cleanup_cfg(Program *program)
10401 {
10402 /* create linear_succs/logical_succs */
10403 for (Block& BB : program->blocks) {
10404 for (unsigned idx : BB.linear_preds)
10405 program->blocks[idx].linear_succs.emplace_back(BB.index);
10406 for (unsigned idx : BB.logical_preds)
10407 program->blocks[idx].logical_succs.emplace_back(BB.index);
10408 }
10409 }
10410
10411 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10412 {
10413 Builder bld(ctx->program, ctx->block);
10414
10415 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10416 Temp count = i == 0
10417 ? get_arg(ctx, ctx->args->merged_wave_info)
10418 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10419 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10420
10421 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10422 Temp cond;
10423
10424 if (ctx->program->wave_size == 64) {
10425 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10426 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10427 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10428 } else {
10429 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10430 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10431 }
10432
10433 return cond;
10434 }
10435
10436 bool ngg_early_prim_export(isel_context *ctx)
10437 {
10438 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10439 return true;
10440 }
10441
10442 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10443 {
10444 Builder bld(ctx->program, ctx->block);
10445
10446 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10447 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10448
10449 /* Get the id of the current wave within the threadgroup (workgroup) */
10450 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10451 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10452
10453 /* Execute the following code only on the first wave (wave id 0),
10454 * use the SCC def to tell if the wave id is zero or not.
10455 */
10456 Temp cond = wave_id_in_tg.def(1).getTemp();
10457 if_context ic;
10458 begin_uniform_if_then(ctx, &ic, cond);
10459 begin_uniform_if_else(ctx, &ic);
10460 bld.reset(ctx->block);
10461
10462 /* Number of vertices output by VS/TES */
10463 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10464 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10465 /* Number of primitives output by VS/TES */
10466 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10467 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10468
10469 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10470 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10471 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10472
10473 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10474 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10475
10476 end_uniform_if(ctx, &ic);
10477
10478 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10479 bld.reset(ctx->block);
10480 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10481 }
10482
10483 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10484 {
10485 Builder bld(ctx->program, ctx->block);
10486
10487 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10488 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10489 }
10490
10491 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10492 Temp tmp;
10493
10494 for (unsigned i = 0; i < num_vertices; ++i) {
10495 assert(vtxindex[i].id());
10496
10497 if (i)
10498 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10499 else
10500 tmp = vtxindex[i];
10501
10502 /* The initial edge flag is always false in tess eval shaders. */
10503 if (ctx->stage == ngg_vertex_gs) {
10504 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10505 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10506 }
10507 }
10508
10509 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10510
10511 return tmp;
10512 }
10513
10514 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10515 {
10516 Builder bld(ctx->program, ctx->block);
10517 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10518
10519 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10520 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10521 false /* compressed */, true/* done */, false /* valid mask */);
10522 }
10523
10524 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10525 {
10526 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10527 * These must always come before VS exports.
10528 *
10529 * It is recommended to do these as early as possible. They can be at the beginning when
10530 * there is no SW GS and the shader doesn't write edge flags.
10531 */
10532
10533 if_context ic;
10534 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10535 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10536
10537 Builder bld(ctx->program, ctx->block);
10538 constexpr unsigned max_vertices_per_primitive = 3;
10539 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10540
10541 if (ctx->stage == ngg_vertex_gs) {
10542 /* TODO: optimize for points & lines */
10543 } else if (ctx->stage == ngg_tess_eval_gs) {
10544 if (ctx->shader->info.tess.point_mode)
10545 num_vertices_per_primitive = 1;
10546 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10547 num_vertices_per_primitive = 2;
10548 } else {
10549 unreachable("Unsupported NGG shader stage");
10550 }
10551
10552 Temp vtxindex[max_vertices_per_primitive];
10553 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10554 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10555 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10556 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10557 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10558 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10559 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10560 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10561
10562 /* Export primitive data to the index buffer. */
10563 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10564
10565 /* Export primitive ID. */
10566 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10567 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10568 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10569 Temp provoking_vtx_index = vtxindex[0];
10570 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10571
10572 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10573 }
10574
10575 begin_divergent_if_else(ctx, &ic);
10576 end_divergent_if(ctx, &ic);
10577 }
10578
10579 void ngg_emit_nogs_output(isel_context *ctx)
10580 {
10581 /* Emits NGG GS output, for stages that don't have SW GS. */
10582
10583 if_context ic;
10584 Builder bld(ctx->program, ctx->block);
10585 bool late_prim_export = !ngg_early_prim_export(ctx);
10586
10587 /* NGG streamout is currently disabled by default. */
10588 assert(!ctx->args->shader_info->so.num_outputs);
10589
10590 if (late_prim_export) {
10591 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10592 create_export_phis(ctx);
10593 /* Do what we need to do in the GS threads. */
10594 ngg_emit_nogs_gsthreads(ctx);
10595
10596 /* What comes next should be executed on ES threads. */
10597 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10598 begin_divergent_if_then(ctx, &ic, is_es_thread);
10599 bld.reset(ctx->block);
10600 }
10601
10602 /* Export VS outputs */
10603 ctx->block->kind |= block_kind_export_end;
10604 create_vs_exports(ctx);
10605
10606 /* Export primitive ID */
10607 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10608 Temp prim_id;
10609
10610 if (ctx->stage == ngg_vertex_gs) {
10611 /* Wait for GS threads to store primitive ID in LDS. */
10612 bld.barrier(aco_opcode::p_memory_barrier_shared);
10613 bld.sopp(aco_opcode::s_barrier);
10614
10615 /* Calculate LDS address where the GS threads stored the primitive ID. */
10616 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10617 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10618 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10619 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10620 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10621 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10622
10623 /* Load primitive ID from LDS. */
10624 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10625 } else if (ctx->stage == ngg_tess_eval_gs) {
10626 /* TES: Just use the patch ID as the primitive ID. */
10627 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10628 } else {
10629 unreachable("unsupported NGG shader stage.");
10630 }
10631
10632 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10633 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10634
10635 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10636 }
10637
10638 if (late_prim_export) {
10639 begin_divergent_if_else(ctx, &ic);
10640 end_divergent_if(ctx, &ic);
10641 bld.reset(ctx->block);
10642 }
10643 }
10644
10645 void select_program(Program *program,
10646 unsigned shader_count,
10647 struct nir_shader *const *shaders,
10648 ac_shader_config* config,
10649 struct radv_shader_args *args)
10650 {
10651 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10652 if_context ic_merged_wave_info;
10653 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10654
10655 for (unsigned i = 0; i < shader_count; i++) {
10656 nir_shader *nir = shaders[i];
10657 init_context(&ctx, nir);
10658
10659 setup_fp_mode(&ctx, nir);
10660
10661 if (!i) {
10662 /* needs to be after init_context() for FS */
10663 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10664 append_logical_start(ctx.block);
10665
10666 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10667 fix_ls_vgpr_init_bug(&ctx, startpgm);
10668
10669 split_arguments(&ctx, startpgm);
10670 }
10671
10672 if (ngg_no_gs) {
10673 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10674
10675 if (ngg_early_prim_export(&ctx))
10676 ngg_emit_nogs_gsthreads(&ctx);
10677 }
10678
10679 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10680 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10681 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10682 ((nir->info.stage == MESA_SHADER_VERTEX &&
10683 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10684 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10685 ctx.stage == tess_eval_geometry_gs));
10686
10687 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10688 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10689 if (check_merged_wave_info) {
10690 Temp cond = merged_wave_info_to_mask(&ctx, i);
10691 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10692 }
10693
10694 if (i) {
10695 Builder bld(ctx.program, ctx.block);
10696
10697 bld.barrier(aco_opcode::p_memory_barrier_shared);
10698 bld.sopp(aco_opcode::s_barrier);
10699
10700 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10701 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));
10702 }
10703 } else if (ctx.stage == geometry_gs)
10704 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10705
10706 if (ctx.stage == fragment_fs)
10707 handle_bc_optimize(&ctx);
10708
10709 visit_cf_list(&ctx, &func->body);
10710
10711 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10712 emit_streamout(&ctx, 0);
10713
10714 if (ctx.stage & hw_vs) {
10715 create_vs_exports(&ctx);
10716 ctx.block->kind |= block_kind_export_end;
10717 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10718 ngg_emit_nogs_output(&ctx);
10719 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10720 Builder bld(ctx.program, ctx.block);
10721 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10722 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10723 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10724 write_tcs_tess_factors(&ctx);
10725 }
10726
10727 if (ctx.stage == fragment_fs) {
10728 create_fs_exports(&ctx);
10729 ctx.block->kind |= block_kind_export_end;
10730 }
10731
10732 if (endif_merged_wave_info) {
10733 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10734 end_divergent_if(&ctx, &ic_merged_wave_info);
10735 }
10736
10737 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10738 ngg_emit_nogs_output(&ctx);
10739
10740 ralloc_free(ctx.divergent_vals);
10741
10742 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10743 /* Outputs of the previous stage are inputs to the next stage */
10744 ctx.inputs = ctx.outputs;
10745 ctx.outputs = shader_io_state();
10746 }
10747 }
10748
10749 program->config->float_mode = program->blocks[0].fp_mode.val;
10750
10751 append_logical_end(ctx.block);
10752 ctx.block->kind |= block_kind_uniform;
10753 Builder bld(ctx.program, ctx.block);
10754 if (ctx.program->wb_smem_l1_on_end)
10755 bld.smem(aco_opcode::s_dcache_wb, false);
10756 bld.sopp(aco_opcode::s_endpgm);
10757
10758 cleanup_cfg(program);
10759 }
10760
10761 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10762 ac_shader_config* config,
10763 struct radv_shader_args *args)
10764 {
10765 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10766
10767 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10768 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10769 program->next_fp_mode.must_flush_denorms32 = false;
10770 program->next_fp_mode.must_flush_denorms16_64 = false;
10771 program->next_fp_mode.care_about_round32 = false;
10772 program->next_fp_mode.care_about_round16_64 = false;
10773 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10774 program->next_fp_mode.denorm32 = 0;
10775 program->next_fp_mode.round32 = fp_round_ne;
10776 program->next_fp_mode.round16_64 = fp_round_ne;
10777 ctx.block->fp_mode = program->next_fp_mode;
10778
10779 add_startpgm(&ctx);
10780 append_logical_start(ctx.block);
10781
10782 Builder bld(ctx.program, ctx.block);
10783
10784 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10785
10786 Operand stream_id(0u);
10787 if (args->shader_info->so.num_outputs)
10788 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10789 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10790
10791 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10792
10793 std::stack<Block> endif_blocks;
10794
10795 for (unsigned stream = 0; stream < 4; stream++) {
10796 if (stream_id.isConstant() && stream != stream_id.constantValue())
10797 continue;
10798
10799 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10800 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10801 continue;
10802
10803 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10804
10805 unsigned BB_if_idx = ctx.block->index;
10806 Block BB_endif = Block();
10807 if (!stream_id.isConstant()) {
10808 /* begin IF */
10809 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10810 append_logical_end(ctx.block);
10811 ctx.block->kind |= block_kind_uniform;
10812 bld.branch(aco_opcode::p_cbranch_z, cond);
10813
10814 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10815
10816 ctx.block = ctx.program->create_and_insert_block();
10817 add_edge(BB_if_idx, ctx.block);
10818 bld.reset(ctx.block);
10819 append_logical_start(ctx.block);
10820 }
10821
10822 unsigned offset = 0;
10823 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10824 if (args->shader_info->gs.output_streams[i] != stream)
10825 continue;
10826
10827 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10828 unsigned length = util_last_bit(output_usage_mask);
10829 for (unsigned j = 0; j < length; ++j) {
10830 if (!(output_usage_mask & (1 << j)))
10831 continue;
10832
10833 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10834 Temp voffset = vtx_offset;
10835 if (const_offset >= 4096u) {
10836 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10837 const_offset %= 4096u;
10838 }
10839
10840 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10841 mubuf->definitions[0] = bld.def(v1);
10842 mubuf->operands[0] = Operand(gsvs_ring);
10843 mubuf->operands[1] = Operand(voffset);
10844 mubuf->operands[2] = Operand(0u);
10845 mubuf->offen = true;
10846 mubuf->offset = const_offset;
10847 mubuf->glc = true;
10848 mubuf->slc = true;
10849 mubuf->dlc = args->options->chip_class >= GFX10;
10850 mubuf->barrier = barrier_none;
10851 mubuf->can_reorder = true;
10852
10853 ctx.outputs.mask[i] |= 1 << j;
10854 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10855
10856 bld.insert(std::move(mubuf));
10857
10858 offset++;
10859 }
10860 }
10861
10862 if (args->shader_info->so.num_outputs) {
10863 emit_streamout(&ctx, stream);
10864 bld.reset(ctx.block);
10865 }
10866
10867 if (stream == 0) {
10868 create_vs_exports(&ctx);
10869 ctx.block->kind |= block_kind_export_end;
10870 }
10871
10872 if (!stream_id.isConstant()) {
10873 append_logical_end(ctx.block);
10874
10875 /* branch from then block to endif block */
10876 bld.branch(aco_opcode::p_branch);
10877 add_edge(ctx.block->index, &BB_endif);
10878 ctx.block->kind |= block_kind_uniform;
10879
10880 /* emit else block */
10881 ctx.block = ctx.program->create_and_insert_block();
10882 add_edge(BB_if_idx, ctx.block);
10883 bld.reset(ctx.block);
10884 append_logical_start(ctx.block);
10885
10886 endif_blocks.push(std::move(BB_endif));
10887 }
10888 }
10889
10890 while (!endif_blocks.empty()) {
10891 Block BB_endif = std::move(endif_blocks.top());
10892 endif_blocks.pop();
10893
10894 Block *BB_else = ctx.block;
10895
10896 append_logical_end(BB_else);
10897 /* branch from else block to endif block */
10898 bld.branch(aco_opcode::p_branch);
10899 add_edge(BB_else->index, &BB_endif);
10900 BB_else->kind |= block_kind_uniform;
10901
10902 /** emit endif merge block */
10903 ctx.block = program->insert_block(std::move(BB_endif));
10904 bld.reset(ctx.block);
10905 append_logical_start(ctx.block);
10906 }
10907
10908 program->config->float_mode = program->blocks[0].fp_mode.val;
10909
10910 append_logical_end(ctx.block);
10911 ctx.block->kind |= block_kind_uniform;
10912 bld.sopp(aco_opcode::s_endpgm);
10913
10914 cleanup_cfg(program);
10915 }
10916 }