aco: remove unecessary p_split_vector with v2b reg class
[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 RegClass rc;
308 if (num_components > vec_src.size()) {
309 if (vec_src.type() == RegType::sgpr) {
310 /* should still help get_alu_src() */
311 emit_split_vector(ctx, vec_src, vec_src.size());
312 return;
313 }
314 /* sub-dword split */
315 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
316 } else {
317 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
318 }
319 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
320 split->operands[0] = Operand(vec_src);
321 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
322 for (unsigned i = 0; i < num_components; i++) {
323 elems[i] = {ctx->program->allocateId(), rc};
324 split->definitions[i] = Definition(elems[i]);
325 }
326 ctx->block->instructions.emplace_back(std::move(split));
327 ctx->allocated_vec.emplace(vec_src.id(), elems);
328 }
329
330 /* This vector expansion uses a mask to determine which elements in the new vector
331 * come from the original vector. The other elements are undefined. */
332 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
333 {
334 emit_split_vector(ctx, vec_src, util_bitcount(mask));
335
336 if (vec_src == dst)
337 return;
338
339 Builder bld(ctx->program, ctx->block);
340 if (num_components == 1) {
341 if (dst.type() == RegType::sgpr)
342 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
343 else
344 bld.copy(Definition(dst), vec_src);
345 return;
346 }
347
348 unsigned component_size = dst.size() / num_components;
349 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
350
351 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
352 vec->definitions[0] = Definition(dst);
353 unsigned k = 0;
354 for (unsigned i = 0; i < num_components; i++) {
355 if (mask & (1 << i)) {
356 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
357 if (dst.type() == RegType::sgpr)
358 src = bld.as_uniform(src);
359 vec->operands[i] = Operand(src);
360 } else {
361 vec->operands[i] = Operand(0u);
362 }
363 elems[i] = vec->operands[i].getTemp();
364 }
365 ctx->block->instructions.emplace_back(std::move(vec));
366 ctx->allocated_vec.emplace(dst.id(), elems);
367 }
368
369 /* adjust misaligned small bit size loads */
370 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
371 {
372 Builder bld(ctx->program, ctx->block);
373 Operand shift;
374 Temp select = Temp();
375 if (offset.isConstant()) {
376 assert(offset.constantValue() && offset.constantValue() < 4);
377 shift = Operand(offset.constantValue() * 8);
378 } else {
379 /* bit_offset = 8 * (offset & 0x3) */
380 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
381 select = bld.tmp(s1);
382 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
383 }
384
385 if (vec.size() == 1) {
386 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
387 } else if (vec.size() == 2) {
388 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
389 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
390 if (tmp == dst)
391 emit_split_vector(ctx, dst, 2);
392 else
393 emit_extract_vector(ctx, tmp, 0, dst);
394 } else if (vec.size() == 4) {
395 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
396 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
397 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
398 if (select != Temp())
399 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), select);
400 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
401 Temp mid = bld.tmp(s1);
402 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
403 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
404 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
405 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
406 emit_split_vector(ctx, dst, 2);
407 }
408 }
409
410 /* this function trims subdword vectors:
411 * if dst is vgpr - split the src and create a shrunk version according to the mask.
412 * if dst is sgpr - split the src, but move the original to sgpr. */
413 void trim_subdword_vector(isel_context *ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
414 {
415 assert(vec_src.type() == RegType::vgpr);
416 emit_split_vector(ctx, vec_src, num_components);
417
418 Builder bld(ctx->program, ctx->block);
419 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
420 unsigned component_size = vec_src.bytes() / num_components;
421 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
422
423 unsigned k = 0;
424 for (unsigned i = 0; i < num_components; i++) {
425 if (mask & (1 << i))
426 elems[k++] = emit_extract_vector(ctx, vec_src, i, rc);
427 }
428
429 if (dst.type() == RegType::vgpr) {
430 assert(dst.bytes() == k * component_size);
431 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, k, 1)};
432 for (unsigned i = 0; i < k; i++)
433 vec->operands[i] = Operand(elems[i]);
434 vec->definitions[0] = Definition(dst);
435 bld.insert(std::move(vec));
436 } else {
437 // TODO: alignbyte if mask doesn't start with 1?
438 assert(mask & 1);
439 assert(dst.size() == vec_src.size());
440 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
441 }
442 ctx->allocated_vec.emplace(dst.id(), elems);
443 }
444
445 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
446 {
447 Builder bld(ctx->program, ctx->block);
448 if (!dst.id())
449 dst = bld.tmp(bld.lm);
450
451 assert(val.regClass() == s1);
452 assert(dst.regClass() == bld.lm);
453
454 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
455 }
456
457 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
458 {
459 Builder bld(ctx->program, ctx->block);
460 if (!dst.id())
461 dst = bld.tmp(s1);
462
463 assert(val.regClass() == bld.lm);
464 assert(dst.regClass() == s1);
465
466 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
467 Temp tmp = bld.tmp(s1);
468 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
469 return emit_wqm(ctx, tmp, dst);
470 }
471
472 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
473 {
474 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
475 return get_ssa_temp(ctx, src.src.ssa);
476
477 if (src.src.ssa->num_components == size) {
478 bool identity_swizzle = true;
479 for (unsigned i = 0; identity_swizzle && i < size; i++) {
480 if (src.swizzle[i] != i)
481 identity_swizzle = false;
482 }
483 if (identity_swizzle)
484 return get_ssa_temp(ctx, src.src.ssa);
485 }
486
487 Temp vec = get_ssa_temp(ctx, src.src.ssa);
488 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
489 assert(elem_size > 0);
490 assert(vec.bytes() % elem_size == 0);
491
492 if (elem_size < 4 && vec.type() == RegType::sgpr) {
493 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
494 assert(size == 1);
495 unsigned swizzle = src.swizzle[0];
496 if (vec.size() > 1) {
497 assert(src.src.ssa->bit_size == 16);
498 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
499 swizzle = swizzle & 1;
500 }
501 if (swizzle == 0)
502 return vec;
503
504 Temp dst{ctx->program->allocateId(), s1};
505 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
506 bfe->operands[0] = Operand(vec);
507 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
508 bfe->definitions[0] = Definition(dst);
509 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
510 ctx->block->instructions.emplace_back(std::move(bfe));
511 return dst;
512 }
513
514 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
515 if (size == 1) {
516 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
517 } else {
518 assert(size <= 4);
519 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
520 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
521 for (unsigned i = 0; i < size; ++i) {
522 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
523 vec_instr->operands[i] = Operand{elems[i]};
524 }
525 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
526 vec_instr->definitions[0] = Definition(dst);
527 ctx->block->instructions.emplace_back(std::move(vec_instr));
528 ctx->allocated_vec.emplace(dst.id(), elems);
529 return dst;
530 }
531 }
532
533 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
534 {
535 if (ptr.size() == 2)
536 return ptr;
537 Builder bld(ctx->program, ctx->block);
538 if (ptr.type() == RegType::vgpr)
539 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
540 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
541 ptr, Operand((unsigned)ctx->options->address32_hi));
542 }
543
544 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
545 {
546 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
547 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
548 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
549 sop2->definitions[0] = Definition(dst);
550 if (writes_scc)
551 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
552 ctx->block->instructions.emplace_back(std::move(sop2));
553 }
554
555 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
556 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
557 {
558 Builder bld(ctx->program, ctx->block);
559 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
560 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
561 if (src1.type() == RegType::sgpr) {
562 if (commutative && src0.type() == RegType::vgpr) {
563 Temp t = src0;
564 src0 = src1;
565 src1 = t;
566 } else {
567 src1 = as_vgpr(ctx, src1);
568 }
569 }
570
571 if (flush_denorms && ctx->program->chip_class < GFX9) {
572 assert(dst.size() == 1);
573 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
574 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
575 } else {
576 bld.vop2(op, Definition(dst), src0, src1);
577 }
578 }
579
580 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
581 bool flush_denorms = false)
582 {
583 Temp src0 = get_alu_src(ctx, instr->src[0]);
584 Temp src1 = get_alu_src(ctx, instr->src[1]);
585 Temp src2 = get_alu_src(ctx, instr->src[2]);
586
587 /* ensure that the instruction has at most 1 sgpr operand
588 * The optimizer will inline constants for us */
589 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
590 src0 = as_vgpr(ctx, src0);
591 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
592 src1 = as_vgpr(ctx, src1);
593 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
594 src2 = as_vgpr(ctx, src2);
595
596 Builder bld(ctx->program, ctx->block);
597 if (flush_denorms && ctx->program->chip_class < GFX9) {
598 assert(dst.size() == 1);
599 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
600 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
601 } else {
602 bld.vop3(op, Definition(dst), src0, src1, src2);
603 }
604 }
605
606 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
607 {
608 Builder bld(ctx->program, ctx->block);
609 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
610 }
611
612 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
613 {
614 Temp src0 = get_alu_src(ctx, instr->src[0]);
615 Temp src1 = get_alu_src(ctx, instr->src[1]);
616 assert(src0.size() == src1.size());
617
618 aco_ptr<Instruction> vopc;
619 if (src1.type() == RegType::sgpr) {
620 if (src0.type() == RegType::vgpr) {
621 /* to swap the operands, we might also have to change the opcode */
622 switch (op) {
623 case aco_opcode::v_cmp_lt_f16:
624 op = aco_opcode::v_cmp_gt_f16;
625 break;
626 case aco_opcode::v_cmp_ge_f16:
627 op = aco_opcode::v_cmp_le_f16;
628 break;
629 case aco_opcode::v_cmp_lt_i16:
630 op = aco_opcode::v_cmp_gt_i16;
631 break;
632 case aco_opcode::v_cmp_ge_i16:
633 op = aco_opcode::v_cmp_le_i16;
634 break;
635 case aco_opcode::v_cmp_lt_u16:
636 op = aco_opcode::v_cmp_gt_u16;
637 break;
638 case aco_opcode::v_cmp_ge_u16:
639 op = aco_opcode::v_cmp_le_u16;
640 break;
641 case aco_opcode::v_cmp_lt_f32:
642 op = aco_opcode::v_cmp_gt_f32;
643 break;
644 case aco_opcode::v_cmp_ge_f32:
645 op = aco_opcode::v_cmp_le_f32;
646 break;
647 case aco_opcode::v_cmp_lt_i32:
648 op = aco_opcode::v_cmp_gt_i32;
649 break;
650 case aco_opcode::v_cmp_ge_i32:
651 op = aco_opcode::v_cmp_le_i32;
652 break;
653 case aco_opcode::v_cmp_lt_u32:
654 op = aco_opcode::v_cmp_gt_u32;
655 break;
656 case aco_opcode::v_cmp_ge_u32:
657 op = aco_opcode::v_cmp_le_u32;
658 break;
659 case aco_opcode::v_cmp_lt_f64:
660 op = aco_opcode::v_cmp_gt_f64;
661 break;
662 case aco_opcode::v_cmp_ge_f64:
663 op = aco_opcode::v_cmp_le_f64;
664 break;
665 case aco_opcode::v_cmp_lt_i64:
666 op = aco_opcode::v_cmp_gt_i64;
667 break;
668 case aco_opcode::v_cmp_ge_i64:
669 op = aco_opcode::v_cmp_le_i64;
670 break;
671 case aco_opcode::v_cmp_lt_u64:
672 op = aco_opcode::v_cmp_gt_u64;
673 break;
674 case aco_opcode::v_cmp_ge_u64:
675 op = aco_opcode::v_cmp_le_u64;
676 break;
677 default: /* eq and ne are commutative */
678 break;
679 }
680 Temp t = src0;
681 src0 = src1;
682 src1 = t;
683 } else {
684 src1 = as_vgpr(ctx, src1);
685 }
686 }
687
688 Builder bld(ctx->program, ctx->block);
689 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
690 }
691
692 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
693 {
694 Temp src0 = get_alu_src(ctx, instr->src[0]);
695 Temp src1 = get_alu_src(ctx, instr->src[1]);
696 Builder bld(ctx->program, ctx->block);
697
698 assert(dst.regClass() == bld.lm);
699 assert(src0.type() == RegType::sgpr);
700 assert(src1.type() == RegType::sgpr);
701 assert(src0.regClass() == src1.regClass());
702
703 /* Emit the SALU comparison instruction */
704 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
705 /* Turn the result into a per-lane bool */
706 bool_to_vector_condition(ctx, cmp, dst);
707 }
708
709 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
710 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)
711 {
712 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;
713 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;
714 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
715 bool use_valu = s_op == aco_opcode::num_opcodes ||
716 divergent_vals ||
717 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
718 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
719 aco_opcode op = use_valu ? v_op : s_op;
720 assert(op != aco_opcode::num_opcodes);
721 assert(dst.regClass() == ctx->program->lane_mask);
722
723 if (use_valu)
724 emit_vopc_instruction(ctx, instr, op, dst);
725 else
726 emit_sopc_instruction(ctx, instr, op, dst);
727 }
728
729 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
730 {
731 Builder bld(ctx->program, ctx->block);
732 Temp src0 = get_alu_src(ctx, instr->src[0]);
733 Temp src1 = get_alu_src(ctx, instr->src[1]);
734
735 assert(dst.regClass() == bld.lm);
736 assert(src0.regClass() == bld.lm);
737 assert(src1.regClass() == bld.lm);
738
739 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
740 }
741
742 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
743 {
744 Builder bld(ctx->program, ctx->block);
745 Temp cond = get_alu_src(ctx, instr->src[0]);
746 Temp then = get_alu_src(ctx, instr->src[1]);
747 Temp els = get_alu_src(ctx, instr->src[2]);
748
749 assert(cond.regClass() == bld.lm);
750
751 if (dst.type() == RegType::vgpr) {
752 aco_ptr<Instruction> bcsel;
753 if (dst.size() == 1) {
754 then = as_vgpr(ctx, then);
755 els = as_vgpr(ctx, els);
756
757 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
758 } else if (dst.size() == 2) {
759 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
760 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
761 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
762 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
763
764 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
765 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
766
767 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
768 } else {
769 fprintf(stderr, "Unimplemented NIR instr bit size: ");
770 nir_print_instr(&instr->instr, stderr);
771 fprintf(stderr, "\n");
772 }
773 return;
774 }
775
776 if (instr->dest.dest.ssa.bit_size == 1) {
777 assert(dst.regClass() == bld.lm);
778 assert(then.regClass() == bld.lm);
779 assert(els.regClass() == bld.lm);
780 }
781
782 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
783 if (dst.regClass() == s1 || dst.regClass() == s2) {
784 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
785 assert(dst.size() == then.size());
786 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
787 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
788 } else {
789 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
790 nir_print_instr(&instr->instr, stderr);
791 fprintf(stderr, "\n");
792 }
793 return;
794 }
795
796 /* divergent boolean bcsel
797 * this implements bcsel on bools: dst = s0 ? s1 : s2
798 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
799 assert(instr->dest.dest.ssa.bit_size == 1);
800
801 if (cond.id() != then.id())
802 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
803
804 if (cond.id() == els.id())
805 bld.sop1(Builder::s_mov, Definition(dst), then);
806 else
807 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
808 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
809 }
810
811 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
812 aco_opcode op, uint32_t undo)
813 {
814 /* multiply by 16777216 to handle denormals */
815 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
816 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
817 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
818 scaled = bld.vop1(op, bld.def(v1), scaled);
819 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
820
821 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
822
823 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
824 }
825
826 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
827 {
828 if (ctx->block->fp_mode.denorm32 == 0) {
829 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
830 return;
831 }
832
833 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
834 }
835
836 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
837 {
838 if (ctx->block->fp_mode.denorm32 == 0) {
839 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
840 return;
841 }
842
843 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
844 }
845
846 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
847 {
848 if (ctx->block->fp_mode.denorm32 == 0) {
849 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
850 return;
851 }
852
853 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
854 }
855
856 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
857 {
858 if (ctx->block->fp_mode.denorm32 == 0) {
859 bld.vop1(aco_opcode::v_log_f32, dst, val);
860 return;
861 }
862
863 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
864 }
865
866 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
867 {
868 if (ctx->options->chip_class >= GFX7)
869 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
870
871 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
872 /* TODO: create more efficient code! */
873 if (val.type() == RegType::sgpr)
874 val = as_vgpr(ctx, val);
875
876 /* Split the input value. */
877 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
878 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
879
880 /* Extract the exponent and compute the unbiased value. */
881 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
882
883 /* Extract the fractional part. */
884 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
885 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
886
887 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
888 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
889
890 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
891 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
892 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
893 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
894 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
895
896 /* Get the sign bit. */
897 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
898
899 /* Decide the operation to apply depending on the unbiased exponent. */
900 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
901 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
902 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
903 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
904 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
905 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
906
907 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
908 }
909
910 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
911 {
912 if (ctx->options->chip_class >= GFX7)
913 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
914
915 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
916 Temp src0 = as_vgpr(ctx, val);
917
918 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
919 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
920
921 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
922 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
923 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
924
925 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
926 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
927 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
928 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
929
930 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
931 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
932
933 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
934
935 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
936 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
937
938 return add->definitions[0].getTemp();
939 }
940
941 Temp convert_int(Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
942 if (!dst.id()) {
943 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
944 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
945 else
946 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
947 }
948
949 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
950 return bld.copy(Definition(dst), src);
951 else if (dst.bytes() < src.bytes())
952 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
953
954 Temp tmp = dst;
955 if (dst_bits == 64)
956 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
957
958 if (tmp == src) {
959 } else if (src.regClass() == s1) {
960 if (is_signed)
961 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
962 else
963 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
964 } else {
965 assert(src_bits != 8 || src.regClass() == v1b);
966 assert(src_bits != 16 || src.regClass() == v2b);
967 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
968 sdwa->operands[0] = Operand(src);
969 sdwa->definitions[0] = Definition(tmp);
970 if (is_signed)
971 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
972 else
973 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
974 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
975 bld.insert(std::move(sdwa));
976 }
977
978 if (dst_bits == 64) {
979 if (is_signed && dst.regClass() == s2) {
980 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
981 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
982 } else if (is_signed && dst.regClass() == v2) {
983 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
984 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
985 } else {
986 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
987 }
988 }
989
990 return dst;
991 }
992
993 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
994 {
995 if (!instr->dest.dest.is_ssa) {
996 fprintf(stderr, "nir alu dst not in ssa: ");
997 nir_print_instr(&instr->instr, stderr);
998 fprintf(stderr, "\n");
999 abort();
1000 }
1001 Builder bld(ctx->program, ctx->block);
1002 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1003 switch(instr->op) {
1004 case nir_op_vec2:
1005 case nir_op_vec3:
1006 case nir_op_vec4: {
1007 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1008 unsigned num = instr->dest.dest.ssa.num_components;
1009 for (unsigned i = 0; i < num; ++i)
1010 elems[i] = get_alu_src(ctx, instr->src[i]);
1011
1012 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1013 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1014 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1015 for (unsigned i = 0; i < num; ++i) {
1016 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1017 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1018 else
1019 vec->operands[i] = Operand{elems[i]};
1020 }
1021 vec->definitions[0] = Definition(dst);
1022 ctx->block->instructions.emplace_back(std::move(vec));
1023 ctx->allocated_vec.emplace(dst.id(), elems);
1024 } else {
1025 // TODO: that is a bit suboptimal..
1026 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1027 for (unsigned i = 0; i < num - 1; ++i)
1028 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1029 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1030 for (unsigned i = 0; i < num; ++i) {
1031 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1032 if (bit % 32 == 0) {
1033 elems[bit / 32] = elems[i];
1034 } else {
1035 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1036 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1037 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1038 }
1039 }
1040 if (dst.size() == 1)
1041 bld.copy(Definition(dst), elems[0]);
1042 else
1043 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1044 }
1045 break;
1046 }
1047 case nir_op_mov: {
1048 Temp src = get_alu_src(ctx, instr->src[0]);
1049 aco_ptr<Instruction> mov;
1050 if (dst.type() == RegType::sgpr) {
1051 if (src.type() == RegType::vgpr)
1052 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1053 else if (src.regClass() == s1)
1054 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1055 else if (src.regClass() == s2)
1056 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1057 else
1058 unreachable("wrong src register class for nir_op_imov");
1059 } else if (dst.regClass() == v1) {
1060 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1061 } else if (dst.regClass() == v2) {
1062 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1063 } else {
1064 nir_print_instr(&instr->instr, stderr);
1065 unreachable("Should have been lowered to scalar.");
1066 }
1067 break;
1068 }
1069 case nir_op_inot: {
1070 Temp src = get_alu_src(ctx, instr->src[0]);
1071 if (instr->dest.dest.ssa.bit_size == 1) {
1072 assert(src.regClass() == bld.lm);
1073 assert(dst.regClass() == bld.lm);
1074 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1075 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1076 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1077 } else if (dst.regClass() == v1) {
1078 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1079 } else if (dst.type() == RegType::sgpr) {
1080 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1081 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1082 } else {
1083 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1084 nir_print_instr(&instr->instr, stderr);
1085 fprintf(stderr, "\n");
1086 }
1087 break;
1088 }
1089 case nir_op_ineg: {
1090 Temp src = get_alu_src(ctx, instr->src[0]);
1091 if (dst.regClass() == v1) {
1092 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1093 } else if (dst.regClass() == s1) {
1094 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1095 } else if (dst.size() == 2) {
1096 Temp src0 = bld.tmp(dst.type(), 1);
1097 Temp src1 = bld.tmp(dst.type(), 1);
1098 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1099
1100 if (dst.regClass() == s2) {
1101 Temp carry = bld.tmp(s1);
1102 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1103 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1104 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1105 } else {
1106 Temp lower = bld.tmp(v1);
1107 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1108 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1109 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1110 }
1111 } else {
1112 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1113 nir_print_instr(&instr->instr, stderr);
1114 fprintf(stderr, "\n");
1115 }
1116 break;
1117 }
1118 case nir_op_iabs: {
1119 if (dst.regClass() == s1) {
1120 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1121 } else if (dst.regClass() == v1) {
1122 Temp src = get_alu_src(ctx, instr->src[0]);
1123 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1124 } else {
1125 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1126 nir_print_instr(&instr->instr, stderr);
1127 fprintf(stderr, "\n");
1128 }
1129 break;
1130 }
1131 case nir_op_isign: {
1132 Temp src = get_alu_src(ctx, instr->src[0]);
1133 if (dst.regClass() == s1) {
1134 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1135 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1136 } else if (dst.regClass() == s2) {
1137 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1138 Temp neqz;
1139 if (ctx->program->chip_class >= GFX8)
1140 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1141 else
1142 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1143 /* SCC gets zero-extended to 64 bit */
1144 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1145 } else if (dst.regClass() == v1) {
1146 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1147 } else if (dst.regClass() == v2) {
1148 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1149 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1150 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1151 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1152 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1153 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1154 } else {
1155 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1156 nir_print_instr(&instr->instr, stderr);
1157 fprintf(stderr, "\n");
1158 }
1159 break;
1160 }
1161 case nir_op_imax: {
1162 if (dst.regClass() == v1) {
1163 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1164 } else if (dst.regClass() == s1) {
1165 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1166 } else {
1167 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1168 nir_print_instr(&instr->instr, stderr);
1169 fprintf(stderr, "\n");
1170 }
1171 break;
1172 }
1173 case nir_op_umax: {
1174 if (dst.regClass() == v1) {
1175 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1176 } else if (dst.regClass() == s1) {
1177 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1178 } else {
1179 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1180 nir_print_instr(&instr->instr, stderr);
1181 fprintf(stderr, "\n");
1182 }
1183 break;
1184 }
1185 case nir_op_imin: {
1186 if (dst.regClass() == v1) {
1187 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1188 } else if (dst.regClass() == s1) {
1189 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1190 } else {
1191 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1192 nir_print_instr(&instr->instr, stderr);
1193 fprintf(stderr, "\n");
1194 }
1195 break;
1196 }
1197 case nir_op_umin: {
1198 if (dst.regClass() == v1) {
1199 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1200 } else if (dst.regClass() == s1) {
1201 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1202 } else {
1203 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1204 nir_print_instr(&instr->instr, stderr);
1205 fprintf(stderr, "\n");
1206 }
1207 break;
1208 }
1209 case nir_op_ior: {
1210 if (instr->dest.dest.ssa.bit_size == 1) {
1211 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1212 } else if (dst.regClass() == v1) {
1213 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1214 } else if (dst.regClass() == s1) {
1215 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1216 } else if (dst.regClass() == s2) {
1217 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1218 } else {
1219 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1220 nir_print_instr(&instr->instr, stderr);
1221 fprintf(stderr, "\n");
1222 }
1223 break;
1224 }
1225 case nir_op_iand: {
1226 if (instr->dest.dest.ssa.bit_size == 1) {
1227 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1228 } else if (dst.regClass() == v1) {
1229 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1230 } else if (dst.regClass() == s1) {
1231 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1232 } else if (dst.regClass() == s2) {
1233 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1234 } else {
1235 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1236 nir_print_instr(&instr->instr, stderr);
1237 fprintf(stderr, "\n");
1238 }
1239 break;
1240 }
1241 case nir_op_ixor: {
1242 if (instr->dest.dest.ssa.bit_size == 1) {
1243 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1244 } else if (dst.regClass() == v1) {
1245 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1246 } else if (dst.regClass() == s1) {
1247 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1248 } else if (dst.regClass() == s2) {
1249 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1250 } else {
1251 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1252 nir_print_instr(&instr->instr, stderr);
1253 fprintf(stderr, "\n");
1254 }
1255 break;
1256 }
1257 case nir_op_ushr: {
1258 if (dst.regClass() == v1) {
1259 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1260 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1261 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1262 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1263 } else if (dst.regClass() == v2) {
1264 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1265 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1266 } else if (dst.regClass() == s2) {
1267 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1268 } else if (dst.regClass() == s1) {
1269 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1270 } else {
1271 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1272 nir_print_instr(&instr->instr, stderr);
1273 fprintf(stderr, "\n");
1274 }
1275 break;
1276 }
1277 case nir_op_ishl: {
1278 if (dst.regClass() == v1) {
1279 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1280 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1281 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1282 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1283 } else if (dst.regClass() == v2) {
1284 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1285 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1286 } else if (dst.regClass() == s1) {
1287 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1288 } else if (dst.regClass() == s2) {
1289 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1290 } else {
1291 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1292 nir_print_instr(&instr->instr, stderr);
1293 fprintf(stderr, "\n");
1294 }
1295 break;
1296 }
1297 case nir_op_ishr: {
1298 if (dst.regClass() == v1) {
1299 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1300 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1301 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1302 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1303 } else if (dst.regClass() == v2) {
1304 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1305 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1306 } else if (dst.regClass() == s1) {
1307 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1308 } else if (dst.regClass() == s2) {
1309 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1310 } else {
1311 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1312 nir_print_instr(&instr->instr, stderr);
1313 fprintf(stderr, "\n");
1314 }
1315 break;
1316 }
1317 case nir_op_find_lsb: {
1318 Temp src = get_alu_src(ctx, instr->src[0]);
1319 if (src.regClass() == s1) {
1320 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1321 } else if (src.regClass() == v1) {
1322 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1323 } else if (src.regClass() == s2) {
1324 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1325 } else {
1326 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1327 nir_print_instr(&instr->instr, stderr);
1328 fprintf(stderr, "\n");
1329 }
1330 break;
1331 }
1332 case nir_op_ufind_msb:
1333 case nir_op_ifind_msb: {
1334 Temp src = get_alu_src(ctx, instr->src[0]);
1335 if (src.regClass() == s1 || src.regClass() == s2) {
1336 aco_opcode op = src.regClass() == s2 ?
1337 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1338 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1339 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1340
1341 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1342 Operand(src.size() * 32u - 1u), msb_rev);
1343 Temp msb = sub.def(0).getTemp();
1344 Temp carry = sub.def(1).getTemp();
1345
1346 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1347 } else if (src.regClass() == v1) {
1348 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1349 Temp msb_rev = bld.tmp(v1);
1350 emit_vop1_instruction(ctx, instr, op, msb_rev);
1351 Temp msb = bld.tmp(v1);
1352 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1353 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1354 } else {
1355 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1356 nir_print_instr(&instr->instr, stderr);
1357 fprintf(stderr, "\n");
1358 }
1359 break;
1360 }
1361 case nir_op_bitfield_reverse: {
1362 if (dst.regClass() == s1) {
1363 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1364 } else if (dst.regClass() == v1) {
1365 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1366 } else {
1367 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1368 nir_print_instr(&instr->instr, stderr);
1369 fprintf(stderr, "\n");
1370 }
1371 break;
1372 }
1373 case nir_op_iadd: {
1374 if (dst.regClass() == s1) {
1375 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1376 break;
1377 }
1378
1379 Temp src0 = get_alu_src(ctx, instr->src[0]);
1380 Temp src1 = get_alu_src(ctx, instr->src[1]);
1381 if (dst.regClass() == v1) {
1382 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1383 break;
1384 }
1385
1386 assert(src0.size() == 2 && src1.size() == 2);
1387 Temp src00 = bld.tmp(src0.type(), 1);
1388 Temp src01 = bld.tmp(dst.type(), 1);
1389 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1390 Temp src10 = bld.tmp(src1.type(), 1);
1391 Temp src11 = bld.tmp(dst.type(), 1);
1392 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1393
1394 if (dst.regClass() == s2) {
1395 Temp carry = bld.tmp(s1);
1396 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1397 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1398 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1399 } else if (dst.regClass() == v2) {
1400 Temp dst0 = bld.tmp(v1);
1401 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1402 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1403 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1404 } else {
1405 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1406 nir_print_instr(&instr->instr, stderr);
1407 fprintf(stderr, "\n");
1408 }
1409 break;
1410 }
1411 case nir_op_uadd_sat: {
1412 Temp src0 = get_alu_src(ctx, instr->src[0]);
1413 Temp src1 = get_alu_src(ctx, instr->src[1]);
1414 if (dst.regClass() == s1) {
1415 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1416 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1417 src0, src1);
1418 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1419 } else if (dst.regClass() == v1) {
1420 if (ctx->options->chip_class >= GFX9) {
1421 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1422 add->operands[0] = Operand(src0);
1423 add->operands[1] = Operand(src1);
1424 add->definitions[0] = Definition(dst);
1425 add->clamp = 1;
1426 ctx->block->instructions.emplace_back(std::move(add));
1427 } else {
1428 if (src1.regClass() != v1)
1429 std::swap(src0, src1);
1430 assert(src1.regClass() == v1);
1431 Temp tmp = bld.tmp(v1);
1432 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1433 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1434 }
1435 } else {
1436 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1437 nir_print_instr(&instr->instr, stderr);
1438 fprintf(stderr, "\n");
1439 }
1440 break;
1441 }
1442 case nir_op_uadd_carry: {
1443 Temp src0 = get_alu_src(ctx, instr->src[0]);
1444 Temp src1 = get_alu_src(ctx, instr->src[1]);
1445 if (dst.regClass() == s1) {
1446 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1447 break;
1448 }
1449 if (dst.regClass() == v1) {
1450 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1451 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1452 break;
1453 }
1454
1455 Temp src00 = bld.tmp(src0.type(), 1);
1456 Temp src01 = bld.tmp(dst.type(), 1);
1457 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1458 Temp src10 = bld.tmp(src1.type(), 1);
1459 Temp src11 = bld.tmp(dst.type(), 1);
1460 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1461 if (dst.regClass() == s2) {
1462 Temp carry = bld.tmp(s1);
1463 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1464 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1465 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1466 } else if (dst.regClass() == v2) {
1467 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1468 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1469 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1470 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1471 } else {
1472 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1473 nir_print_instr(&instr->instr, stderr);
1474 fprintf(stderr, "\n");
1475 }
1476 break;
1477 }
1478 case nir_op_isub: {
1479 if (dst.regClass() == s1) {
1480 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1481 break;
1482 }
1483
1484 Temp src0 = get_alu_src(ctx, instr->src[0]);
1485 Temp src1 = get_alu_src(ctx, instr->src[1]);
1486 if (dst.regClass() == v1) {
1487 bld.vsub32(Definition(dst), src0, src1);
1488 break;
1489 }
1490
1491 Temp src00 = bld.tmp(src0.type(), 1);
1492 Temp src01 = bld.tmp(dst.type(), 1);
1493 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1494 Temp src10 = bld.tmp(src1.type(), 1);
1495 Temp src11 = bld.tmp(dst.type(), 1);
1496 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1497 if (dst.regClass() == s2) {
1498 Temp carry = bld.tmp(s1);
1499 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1500 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1501 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1502 } else if (dst.regClass() == v2) {
1503 Temp lower = bld.tmp(v1);
1504 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1505 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1506 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1507 } else {
1508 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1509 nir_print_instr(&instr->instr, stderr);
1510 fprintf(stderr, "\n");
1511 }
1512 break;
1513 }
1514 case nir_op_usub_borrow: {
1515 Temp src0 = get_alu_src(ctx, instr->src[0]);
1516 Temp src1 = get_alu_src(ctx, instr->src[1]);
1517 if (dst.regClass() == s1) {
1518 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1519 break;
1520 } else if (dst.regClass() == v1) {
1521 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1522 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1523 break;
1524 }
1525
1526 Temp src00 = bld.tmp(src0.type(), 1);
1527 Temp src01 = bld.tmp(dst.type(), 1);
1528 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1529 Temp src10 = bld.tmp(src1.type(), 1);
1530 Temp src11 = bld.tmp(dst.type(), 1);
1531 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1532 if (dst.regClass() == s2) {
1533 Temp borrow = bld.tmp(s1);
1534 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1535 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1536 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1537 } else if (dst.regClass() == v2) {
1538 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1539 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1540 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1541 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1542 } else {
1543 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1544 nir_print_instr(&instr->instr, stderr);
1545 fprintf(stderr, "\n");
1546 }
1547 break;
1548 }
1549 case nir_op_imul: {
1550 if (dst.regClass() == v1) {
1551 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1552 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1553 } else if (dst.regClass() == s1) {
1554 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1555 } else {
1556 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1557 nir_print_instr(&instr->instr, stderr);
1558 fprintf(stderr, "\n");
1559 }
1560 break;
1561 }
1562 case nir_op_umul_high: {
1563 if (dst.regClass() == v1) {
1564 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1565 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1566 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1567 } else if (dst.regClass() == s1) {
1568 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1569 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1570 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1571 } else {
1572 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1573 nir_print_instr(&instr->instr, stderr);
1574 fprintf(stderr, "\n");
1575 }
1576 break;
1577 }
1578 case nir_op_imul_high: {
1579 if (dst.regClass() == v1) {
1580 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1581 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1582 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1583 } else if (dst.regClass() == s1) {
1584 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1585 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1586 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1587 } else {
1588 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1589 nir_print_instr(&instr->instr, stderr);
1590 fprintf(stderr, "\n");
1591 }
1592 break;
1593 }
1594 case nir_op_fmul: {
1595 Temp src0 = get_alu_src(ctx, instr->src[0]);
1596 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1597 if (dst.regClass() == v2b) {
1598 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1599 } else if (dst.regClass() == v1) {
1600 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1601 } else if (dst.regClass() == v2) {
1602 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1603 } else {
1604 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1605 nir_print_instr(&instr->instr, stderr);
1606 fprintf(stderr, "\n");
1607 }
1608 break;
1609 }
1610 case nir_op_fadd: {
1611 Temp src0 = get_alu_src(ctx, instr->src[0]);
1612 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1613 if (dst.regClass() == v2b) {
1614 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1615 } else if (dst.regClass() == v1) {
1616 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1617 } else if (dst.regClass() == v2) {
1618 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1619 } else {
1620 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1621 nir_print_instr(&instr->instr, stderr);
1622 fprintf(stderr, "\n");
1623 }
1624 break;
1625 }
1626 case nir_op_fsub: {
1627 Temp src0 = get_alu_src(ctx, instr->src[0]);
1628 Temp src1 = get_alu_src(ctx, instr->src[1]);
1629 if (dst.regClass() == v2b) {
1630 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1631 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1632 else
1633 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1634 } else if (dst.regClass() == v1) {
1635 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1636 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1637 else
1638 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1639 } else if (dst.regClass() == v2) {
1640 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1641 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1642 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1643 sub->neg[1] = true;
1644 } else {
1645 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1646 nir_print_instr(&instr->instr, stderr);
1647 fprintf(stderr, "\n");
1648 }
1649 break;
1650 }
1651 case nir_op_fmax: {
1652 Temp src0 = get_alu_src(ctx, instr->src[0]);
1653 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1654 if (dst.regClass() == v2b) {
1655 // TODO: check fp_mode.must_flush_denorms16_64
1656 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1657 } else if (dst.regClass() == v1) {
1658 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1659 } else if (dst.regClass() == v2) {
1660 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1661 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1662 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1663 } else {
1664 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1665 }
1666 } else {
1667 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1668 nir_print_instr(&instr->instr, stderr);
1669 fprintf(stderr, "\n");
1670 }
1671 break;
1672 }
1673 case nir_op_fmin: {
1674 Temp src0 = get_alu_src(ctx, instr->src[0]);
1675 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1676 if (dst.regClass() == v2b) {
1677 // TODO: check fp_mode.must_flush_denorms16_64
1678 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1679 } else if (dst.regClass() == v1) {
1680 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1681 } else if (dst.regClass() == v2) {
1682 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1683 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1684 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1685 } else {
1686 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1687 }
1688 } else {
1689 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1690 nir_print_instr(&instr->instr, stderr);
1691 fprintf(stderr, "\n");
1692 }
1693 break;
1694 }
1695 case nir_op_fmax3: {
1696 if (dst.regClass() == v2b) {
1697 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1698 } else if (dst.regClass() == v1) {
1699 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1700 } else {
1701 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1702 nir_print_instr(&instr->instr, stderr);
1703 fprintf(stderr, "\n");
1704 }
1705 break;
1706 }
1707 case nir_op_fmin3: {
1708 if (dst.regClass() == v2b) {
1709 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1710 } else if (dst.regClass() == v1) {
1711 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1712 } else {
1713 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1714 nir_print_instr(&instr->instr, stderr);
1715 fprintf(stderr, "\n");
1716 }
1717 break;
1718 }
1719 case nir_op_fmed3: {
1720 if (dst.regClass() == v2b) {
1721 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1722 } else if (dst.regClass() == v1) {
1723 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1724 } else {
1725 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1726 nir_print_instr(&instr->instr, stderr);
1727 fprintf(stderr, "\n");
1728 }
1729 break;
1730 }
1731 case nir_op_umax3: {
1732 if (dst.size() == 1) {
1733 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1734 } else {
1735 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1736 nir_print_instr(&instr->instr, stderr);
1737 fprintf(stderr, "\n");
1738 }
1739 break;
1740 }
1741 case nir_op_umin3: {
1742 if (dst.size() == 1) {
1743 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1744 } else {
1745 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1746 nir_print_instr(&instr->instr, stderr);
1747 fprintf(stderr, "\n");
1748 }
1749 break;
1750 }
1751 case nir_op_umed3: {
1752 if (dst.size() == 1) {
1753 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1754 } else {
1755 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1756 nir_print_instr(&instr->instr, stderr);
1757 fprintf(stderr, "\n");
1758 }
1759 break;
1760 }
1761 case nir_op_imax3: {
1762 if (dst.size() == 1) {
1763 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1764 } else {
1765 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1766 nir_print_instr(&instr->instr, stderr);
1767 fprintf(stderr, "\n");
1768 }
1769 break;
1770 }
1771 case nir_op_imin3: {
1772 if (dst.size() == 1) {
1773 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1774 } else {
1775 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1776 nir_print_instr(&instr->instr, stderr);
1777 fprintf(stderr, "\n");
1778 }
1779 break;
1780 }
1781 case nir_op_imed3: {
1782 if (dst.size() == 1) {
1783 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1784 } else {
1785 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1786 nir_print_instr(&instr->instr, stderr);
1787 fprintf(stderr, "\n");
1788 }
1789 break;
1790 }
1791 case nir_op_cube_face_coord: {
1792 Temp in = get_alu_src(ctx, instr->src[0], 3);
1793 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1794 emit_extract_vector(ctx, in, 1, v1),
1795 emit_extract_vector(ctx, in, 2, v1) };
1796 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1797 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1798 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1799 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1800 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1801 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1802 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1803 break;
1804 }
1805 case nir_op_cube_face_index: {
1806 Temp in = get_alu_src(ctx, instr->src[0], 3);
1807 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1808 emit_extract_vector(ctx, in, 1, v1),
1809 emit_extract_vector(ctx, in, 2, v1) };
1810 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1811 break;
1812 }
1813 case nir_op_bcsel: {
1814 emit_bcsel(ctx, instr, dst);
1815 break;
1816 }
1817 case nir_op_frsq: {
1818 Temp src = get_alu_src(ctx, instr->src[0]);
1819 if (dst.regClass() == v2b) {
1820 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1821 } else if (dst.regClass() == v1) {
1822 emit_rsq(ctx, bld, Definition(dst), src);
1823 } else if (dst.regClass() == v2) {
1824 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1825 } else {
1826 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1827 nir_print_instr(&instr->instr, stderr);
1828 fprintf(stderr, "\n");
1829 }
1830 break;
1831 }
1832 case nir_op_fneg: {
1833 Temp src = get_alu_src(ctx, instr->src[0]);
1834 if (dst.regClass() == v2b) {
1835 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1836 } else if (dst.regClass() == v1) {
1837 if (ctx->block->fp_mode.must_flush_denorms32)
1838 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1839 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1840 } else if (dst.regClass() == v2) {
1841 if (ctx->block->fp_mode.must_flush_denorms16_64)
1842 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1843 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1844 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1845 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1846 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1847 } else {
1848 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1849 nir_print_instr(&instr->instr, stderr);
1850 fprintf(stderr, "\n");
1851 }
1852 break;
1853 }
1854 case nir_op_fabs: {
1855 Temp src = get_alu_src(ctx, instr->src[0]);
1856 if (dst.regClass() == v2b) {
1857 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1858 } else if (dst.regClass() == v1) {
1859 if (ctx->block->fp_mode.must_flush_denorms32)
1860 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1861 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1862 } else if (dst.regClass() == v2) {
1863 if (ctx->block->fp_mode.must_flush_denorms16_64)
1864 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1865 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1866 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1867 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1868 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1869 } else {
1870 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1871 nir_print_instr(&instr->instr, stderr);
1872 fprintf(stderr, "\n");
1873 }
1874 break;
1875 }
1876 case nir_op_fsat: {
1877 Temp src = get_alu_src(ctx, instr->src[0]);
1878 if (dst.regClass() == v2b) {
1879 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1880 } else if (dst.regClass() == v1) {
1881 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1882 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1883 // TODO: confirm that this holds under any circumstances
1884 } else if (dst.regClass() == v2) {
1885 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1886 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1887 vop3->clamp = true;
1888 } else {
1889 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1890 nir_print_instr(&instr->instr, stderr);
1891 fprintf(stderr, "\n");
1892 }
1893 break;
1894 }
1895 case nir_op_flog2: {
1896 Temp src = get_alu_src(ctx, instr->src[0]);
1897 if (dst.regClass() == v2b) {
1898 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
1899 } else if (dst.regClass() == v1) {
1900 emit_log2(ctx, bld, Definition(dst), src);
1901 } else {
1902 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1903 nir_print_instr(&instr->instr, stderr);
1904 fprintf(stderr, "\n");
1905 }
1906 break;
1907 }
1908 case nir_op_frcp: {
1909 Temp src = get_alu_src(ctx, instr->src[0]);
1910 if (dst.regClass() == v2b) {
1911 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
1912 } else if (dst.regClass() == v1) {
1913 emit_rcp(ctx, bld, Definition(dst), src);
1914 } else if (dst.regClass() == v2) {
1915 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1916 } else {
1917 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1918 nir_print_instr(&instr->instr, stderr);
1919 fprintf(stderr, "\n");
1920 }
1921 break;
1922 }
1923 case nir_op_fexp2: {
1924 if (dst.regClass() == v2b) {
1925 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
1926 } else if (dst.regClass() == v1) {
1927 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1928 } else {
1929 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1930 nir_print_instr(&instr->instr, stderr);
1931 fprintf(stderr, "\n");
1932 }
1933 break;
1934 }
1935 case nir_op_fsqrt: {
1936 Temp src = get_alu_src(ctx, instr->src[0]);
1937 if (dst.regClass() == v2b) {
1938 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
1939 } else if (dst.regClass() == v1) {
1940 emit_sqrt(ctx, bld, Definition(dst), src);
1941 } else if (dst.regClass() == v2) {
1942 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1943 } else {
1944 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1945 nir_print_instr(&instr->instr, stderr);
1946 fprintf(stderr, "\n");
1947 }
1948 break;
1949 }
1950 case nir_op_ffract: {
1951 if (dst.regClass() == v2b) {
1952 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
1953 } else if (dst.regClass() == v1) {
1954 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1955 } else if (dst.regClass() == v2) {
1956 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1957 } else {
1958 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1959 nir_print_instr(&instr->instr, stderr);
1960 fprintf(stderr, "\n");
1961 }
1962 break;
1963 }
1964 case nir_op_ffloor: {
1965 Temp src = get_alu_src(ctx, instr->src[0]);
1966 if (dst.regClass() == v2b) {
1967 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
1968 } else if (dst.regClass() == v1) {
1969 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1970 } else if (dst.regClass() == v2) {
1971 emit_floor_f64(ctx, bld, Definition(dst), src);
1972 } else {
1973 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1974 nir_print_instr(&instr->instr, stderr);
1975 fprintf(stderr, "\n");
1976 }
1977 break;
1978 }
1979 case nir_op_fceil: {
1980 Temp src0 = get_alu_src(ctx, instr->src[0]);
1981 if (dst.regClass() == v2b) {
1982 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
1983 } else if (dst.regClass() == v1) {
1984 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1985 } else if (dst.regClass() == v2) {
1986 if (ctx->options->chip_class >= GFX7) {
1987 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1988 } else {
1989 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1990 /* trunc = trunc(src0)
1991 * if (src0 > 0.0 && src0 != trunc)
1992 * trunc += 1.0
1993 */
1994 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
1995 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
1996 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
1997 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
1998 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);
1999 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2000 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2001 }
2002 } else {
2003 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2004 nir_print_instr(&instr->instr, stderr);
2005 fprintf(stderr, "\n");
2006 }
2007 break;
2008 }
2009 case nir_op_ftrunc: {
2010 Temp src = get_alu_src(ctx, instr->src[0]);
2011 if (dst.regClass() == v2b) {
2012 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2013 } else if (dst.regClass() == v1) {
2014 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2015 } else if (dst.regClass() == v2) {
2016 emit_trunc_f64(ctx, bld, Definition(dst), src);
2017 } else {
2018 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2019 nir_print_instr(&instr->instr, stderr);
2020 fprintf(stderr, "\n");
2021 }
2022 break;
2023 }
2024 case nir_op_fround_even: {
2025 Temp src0 = get_alu_src(ctx, instr->src[0]);
2026 if (dst.regClass() == v2b) {
2027 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2028 } else if (dst.regClass() == v1) {
2029 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2030 } else if (dst.regClass() == v2) {
2031 if (ctx->options->chip_class >= GFX7) {
2032 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2033 } else {
2034 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2035 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2036 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2037
2038 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2039 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));
2040 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));
2041 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));
2042 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2043 tmp = sub->definitions[0].getTemp();
2044
2045 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2046 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2047 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2048 Temp cond = vop3->definitions[0].getTemp();
2049
2050 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2051 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2052 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2053 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2054
2055 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2056 }
2057 } else {
2058 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2059 nir_print_instr(&instr->instr, stderr);
2060 fprintf(stderr, "\n");
2061 }
2062 break;
2063 }
2064 case nir_op_fsin:
2065 case nir_op_fcos: {
2066 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2067 aco_ptr<Instruction> norm;
2068 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2069 if (dst.regClass() == v2b) {
2070 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2071 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2072 bld.vop1(opcode, Definition(dst), tmp);
2073 } else if (dst.regClass() == v1) {
2074 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2075
2076 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2077 if (ctx->options->chip_class < GFX9)
2078 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2079
2080 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2081 bld.vop1(opcode, Definition(dst), tmp);
2082 } else {
2083 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2084 nir_print_instr(&instr->instr, stderr);
2085 fprintf(stderr, "\n");
2086 }
2087 break;
2088 }
2089 case nir_op_ldexp: {
2090 Temp src0 = get_alu_src(ctx, instr->src[0]);
2091 Temp src1 = get_alu_src(ctx, instr->src[1]);
2092 if (dst.regClass() == v2b) {
2093 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2094 } else if (dst.regClass() == v1) {
2095 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2096 } else if (dst.regClass() == v2) {
2097 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2098 } else {
2099 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2100 nir_print_instr(&instr->instr, stderr);
2101 fprintf(stderr, "\n");
2102 }
2103 break;
2104 }
2105 case nir_op_frexp_sig: {
2106 Temp src = get_alu_src(ctx, instr->src[0]);
2107 if (dst.regClass() == v2b) {
2108 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2109 } else if (dst.regClass() == v1) {
2110 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2111 } else if (dst.regClass() == v2) {
2112 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
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_frexp_exp: {
2121 Temp src = get_alu_src(ctx, instr->src[0]);
2122 if (instr->src[0].src.ssa->bit_size == 16) {
2123 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2124 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2125 convert_int(bld, tmp, 8, 32, true, dst);
2126 } else if (instr->src[0].src.ssa->bit_size == 32) {
2127 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2128 } else if (instr->src[0].src.ssa->bit_size == 64) {
2129 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2130 } else {
2131 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2132 nir_print_instr(&instr->instr, stderr);
2133 fprintf(stderr, "\n");
2134 }
2135 break;
2136 }
2137 case nir_op_fsign: {
2138 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2139 if (dst.regClass() == v2b) {
2140 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2141 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2142 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2143 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2144 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2145 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2146 } else if (dst.regClass() == v1) {
2147 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2148 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2149 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2150 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2151 } else if (dst.regClass() == v2) {
2152 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2153 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2154 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2155
2156 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2157 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2158 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2159
2160 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2161 } else {
2162 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2163 nir_print_instr(&instr->instr, stderr);
2164 fprintf(stderr, "\n");
2165 }
2166 break;
2167 }
2168 case nir_op_f2f16:
2169 case nir_op_f2f16_rtne: {
2170 Temp src = get_alu_src(ctx, instr->src[0]);
2171 if (instr->src[0].src.ssa->bit_size == 64)
2172 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2173 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2174 break;
2175 }
2176 case nir_op_f2f16_rtz: {
2177 Temp src = get_alu_src(ctx, instr->src[0]);
2178 if (instr->src[0].src.ssa->bit_size == 64)
2179 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2180 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2181 break;
2182 }
2183 case nir_op_f2f32: {
2184 if (instr->src[0].src.ssa->bit_size == 16) {
2185 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2186 } else if (instr->src[0].src.ssa->bit_size == 64) {
2187 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2188 } else {
2189 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2190 nir_print_instr(&instr->instr, stderr);
2191 fprintf(stderr, "\n");
2192 }
2193 break;
2194 }
2195 case nir_op_f2f64: {
2196 Temp src = get_alu_src(ctx, instr->src[0]);
2197 if (instr->src[0].src.ssa->bit_size == 16)
2198 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2199 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2200 break;
2201 }
2202 case nir_op_i2f16: {
2203 assert(dst.regClass() == v2b);
2204 Temp src = get_alu_src(ctx, instr->src[0]);
2205 if (instr->src[0].src.ssa->bit_size == 8)
2206 src = convert_int(bld, src, 8, 16, true);
2207 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2208 break;
2209 }
2210 case nir_op_i2f32: {
2211 assert(dst.size() == 1);
2212 Temp src = get_alu_src(ctx, instr->src[0]);
2213 if (instr->src[0].src.ssa->bit_size <= 16)
2214 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2215 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2216 break;
2217 }
2218 case nir_op_i2f64: {
2219 if (instr->src[0].src.ssa->bit_size <= 32) {
2220 Temp src = get_alu_src(ctx, instr->src[0]);
2221 if (instr->src[0].src.ssa->bit_size <= 16)
2222 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2223 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2224 } else if (instr->src[0].src.ssa->bit_size == 64) {
2225 Temp src = get_alu_src(ctx, instr->src[0]);
2226 RegClass rc = RegClass(src.type(), 1);
2227 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2228 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2229 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2230 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2231 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2232 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2233
2234 } else {
2235 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2236 nir_print_instr(&instr->instr, stderr);
2237 fprintf(stderr, "\n");
2238 }
2239 break;
2240 }
2241 case nir_op_u2f16: {
2242 assert(dst.regClass() == v2b);
2243 Temp src = get_alu_src(ctx, instr->src[0]);
2244 if (instr->src[0].src.ssa->bit_size == 8)
2245 src = convert_int(bld, src, 8, 16, false);
2246 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2247 break;
2248 }
2249 case nir_op_u2f32: {
2250 assert(dst.size() == 1);
2251 Temp src = get_alu_src(ctx, instr->src[0]);
2252 if (instr->src[0].src.ssa->bit_size == 8) {
2253 //TODO: we should use v_cvt_f32_ubyte1/v_cvt_f32_ubyte2/etc depending on the register assignment
2254 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2255 } else {
2256 if (instr->src[0].src.ssa->bit_size == 16)
2257 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2258 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2259 }
2260 break;
2261 }
2262 case nir_op_u2f64: {
2263 if (instr->src[0].src.ssa->bit_size <= 32) {
2264 Temp src = get_alu_src(ctx, instr->src[0]);
2265 if (instr->src[0].src.ssa->bit_size <= 16)
2266 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2267 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2268 } else if (instr->src[0].src.ssa->bit_size == 64) {
2269 Temp src = get_alu_src(ctx, instr->src[0]);
2270 RegClass rc = RegClass(src.type(), 1);
2271 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2272 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2273 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2274 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2275 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2276 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2277 } else {
2278 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2279 nir_print_instr(&instr->instr, stderr);
2280 fprintf(stderr, "\n");
2281 }
2282 break;
2283 }
2284 case nir_op_f2i8:
2285 case nir_op_f2i16: {
2286 Temp src = get_alu_src(ctx, instr->src[0]);
2287 if (instr->src[0].src.ssa->bit_size == 16)
2288 src = bld.vop1(aco_opcode::v_cvt_i16_f16, bld.def(v1), src);
2289 else if (instr->src[0].src.ssa->bit_size == 32)
2290 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2291 else
2292 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2293
2294 if (dst.type() == RegType::vgpr)
2295 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2296 else
2297 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2298 break;
2299 }
2300 case nir_op_f2u8:
2301 case nir_op_f2u16: {
2302 Temp src = get_alu_src(ctx, instr->src[0]);
2303 if (instr->src[0].src.ssa->bit_size == 16)
2304 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2305 else if (instr->src[0].src.ssa->bit_size == 32)
2306 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2307 else
2308 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2309
2310 if (dst.type() == RegType::vgpr)
2311 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2312 else
2313 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2314 break;
2315 }
2316 case nir_op_f2i32: {
2317 Temp src = get_alu_src(ctx, instr->src[0]);
2318 if (instr->src[0].src.ssa->bit_size == 16) {
2319 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2320 if (dst.type() == RegType::vgpr) {
2321 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2322 } else {
2323 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2324 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2325 }
2326 } else if (instr->src[0].src.ssa->bit_size == 32) {
2327 if (dst.type() == RegType::vgpr)
2328 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2329 else
2330 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2331 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2332
2333 } else if (instr->src[0].src.ssa->bit_size == 64) {
2334 if (dst.type() == RegType::vgpr)
2335 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2336 else
2337 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2338 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2339
2340 } else {
2341 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2342 nir_print_instr(&instr->instr, stderr);
2343 fprintf(stderr, "\n");
2344 }
2345 break;
2346 }
2347 case nir_op_f2u32: {
2348 Temp src = get_alu_src(ctx, instr->src[0]);
2349 if (instr->src[0].src.ssa->bit_size == 16) {
2350 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2351 if (dst.type() == RegType::vgpr) {
2352 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2353 } else {
2354 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2355 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2356 }
2357 } else if (instr->src[0].src.ssa->bit_size == 32) {
2358 if (dst.type() == RegType::vgpr)
2359 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2360 else
2361 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2362 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2363
2364 } else if (instr->src[0].src.ssa->bit_size == 64) {
2365 if (dst.type() == RegType::vgpr)
2366 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2367 else
2368 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2369 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2370
2371 } else {
2372 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2373 nir_print_instr(&instr->instr, stderr);
2374 fprintf(stderr, "\n");
2375 }
2376 break;
2377 }
2378 case nir_op_f2i64: {
2379 Temp src = get_alu_src(ctx, instr->src[0]);
2380 if (instr->src[0].src.ssa->bit_size == 16)
2381 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2382
2383 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2384 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2385 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2386 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2387 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2388 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2389 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2390 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2391 Temp new_exponent = bld.tmp(v1);
2392 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2393 if (ctx->program->chip_class >= GFX8)
2394 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2395 else
2396 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2397 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2398 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2399 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2400 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2401 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2402 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2403 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2404 Temp new_lower = bld.tmp(v1);
2405 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2406 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2407 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2408
2409 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2410 if (src.type() == RegType::vgpr)
2411 src = bld.as_uniform(src);
2412 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2413 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2414 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2415 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2416 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2417 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2418 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2419 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2420 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2421 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2422 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2423 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2424 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2425 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2426 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2427 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2428 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2429 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2430 Temp borrow = bld.tmp(s1);
2431 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2432 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2433 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2434
2435 } else if (instr->src[0].src.ssa->bit_size == 64) {
2436 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2437 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2438 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2439 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2440 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2441 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2442 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2443 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2444 if (dst.type() == RegType::sgpr) {
2445 lower = bld.as_uniform(lower);
2446 upper = bld.as_uniform(upper);
2447 }
2448 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2449
2450 } else {
2451 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2452 nir_print_instr(&instr->instr, stderr);
2453 fprintf(stderr, "\n");
2454 }
2455 break;
2456 }
2457 case nir_op_f2u64: {
2458 Temp src = get_alu_src(ctx, instr->src[0]);
2459 if (instr->src[0].src.ssa->bit_size == 16)
2460 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2461
2462 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2463 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2464 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2465 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2466 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2467 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2468 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2469 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2470 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2471 Temp new_exponent = bld.tmp(v1);
2472 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2473 if (ctx->program->chip_class >= GFX8)
2474 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2475 else
2476 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2477 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2478 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2479 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2480 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2481 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2482 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2483 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2484
2485 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2486 if (src.type() == RegType::vgpr)
2487 src = bld.as_uniform(src);
2488 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2489 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2490 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2491 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2492 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2493 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2494 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2495 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2496 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2497 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2498 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2499 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2500 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2501 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2502 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2503 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2504 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2505 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2506
2507 } else if (instr->src[0].src.ssa->bit_size == 64) {
2508 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2509 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2510 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2511 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2512 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2513 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2514 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2515 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2516 if (dst.type() == RegType::sgpr) {
2517 lower = bld.as_uniform(lower);
2518 upper = bld.as_uniform(upper);
2519 }
2520 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2521
2522 } else {
2523 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2524 nir_print_instr(&instr->instr, stderr);
2525 fprintf(stderr, "\n");
2526 }
2527 break;
2528 }
2529 case nir_op_b2f16: {
2530 Temp src = get_alu_src(ctx, instr->src[0]);
2531 assert(src.regClass() == bld.lm);
2532
2533 if (dst.regClass() == s1) {
2534 src = bool_to_scalar_condition(ctx, src);
2535 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2536 } else if (dst.regClass() == v2b) {
2537 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2538 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2539 } else {
2540 unreachable("Wrong destination register class for nir_op_b2f16.");
2541 }
2542 break;
2543 }
2544 case nir_op_b2f32: {
2545 Temp src = get_alu_src(ctx, instr->src[0]);
2546 assert(src.regClass() == bld.lm);
2547
2548 if (dst.regClass() == s1) {
2549 src = bool_to_scalar_condition(ctx, src);
2550 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2551 } else if (dst.regClass() == v1) {
2552 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2553 } else {
2554 unreachable("Wrong destination register class for nir_op_b2f32.");
2555 }
2556 break;
2557 }
2558 case nir_op_b2f64: {
2559 Temp src = get_alu_src(ctx, instr->src[0]);
2560 assert(src.regClass() == bld.lm);
2561
2562 if (dst.regClass() == s2) {
2563 src = bool_to_scalar_condition(ctx, src);
2564 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2565 } else if (dst.regClass() == v2) {
2566 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2567 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2568 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2569 } else {
2570 unreachable("Wrong destination register class for nir_op_b2f64.");
2571 }
2572 break;
2573 }
2574 case nir_op_i2i8:
2575 case nir_op_i2i16:
2576 case nir_op_i2i32:
2577 case nir_op_i2i64: {
2578 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2579 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2580 break;
2581 }
2582 case nir_op_u2u8:
2583 case nir_op_u2u16:
2584 case nir_op_u2u32:
2585 case nir_op_u2u64: {
2586 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2587 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2588 break;
2589 }
2590 case nir_op_b2b32:
2591 case nir_op_b2i32: {
2592 Temp src = get_alu_src(ctx, instr->src[0]);
2593 assert(src.regClass() == bld.lm);
2594
2595 if (dst.regClass() == s1) {
2596 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2597 bool_to_scalar_condition(ctx, src, dst);
2598 } else if (dst.regClass() == v1) {
2599 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2600 } else {
2601 unreachable("Invalid register class for b2i32");
2602 }
2603 break;
2604 }
2605 case nir_op_b2b1:
2606 case nir_op_i2b1: {
2607 Temp src = get_alu_src(ctx, instr->src[0]);
2608 assert(dst.regClass() == bld.lm);
2609
2610 if (src.type() == RegType::vgpr) {
2611 assert(src.regClass() == v1 || src.regClass() == v2);
2612 assert(dst.regClass() == bld.lm);
2613 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2614 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2615 } else {
2616 assert(src.regClass() == s1 || src.regClass() == s2);
2617 Temp tmp;
2618 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2619 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2620 } else {
2621 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2622 bld.scc(bld.def(s1)), Operand(0u), src);
2623 }
2624 bool_to_vector_condition(ctx, tmp, dst);
2625 }
2626 break;
2627 }
2628 case nir_op_pack_64_2x32_split: {
2629 Temp src0 = get_alu_src(ctx, instr->src[0]);
2630 Temp src1 = get_alu_src(ctx, instr->src[1]);
2631
2632 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2633 break;
2634 }
2635 case nir_op_unpack_64_2x32_split_x:
2636 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2637 break;
2638 case nir_op_unpack_64_2x32_split_y:
2639 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2640 break;
2641 case nir_op_unpack_32_2x16_split_x:
2642 if (dst.type() == RegType::vgpr) {
2643 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2644 } else {
2645 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2646 }
2647 break;
2648 case nir_op_unpack_32_2x16_split_y:
2649 if (dst.type() == RegType::vgpr) {
2650 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2651 } else {
2652 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)));
2653 }
2654 break;
2655 case nir_op_pack_32_2x16_split: {
2656 Temp src0 = get_alu_src(ctx, instr->src[0]);
2657 Temp src1 = get_alu_src(ctx, instr->src[1]);
2658 if (dst.regClass() == v1) {
2659 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2660 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2661 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2662 } else {
2663 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2664 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2665 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2666 }
2667 break;
2668 }
2669 case nir_op_pack_half_2x16: {
2670 Temp src = get_alu_src(ctx, instr->src[0], 2);
2671
2672 if (dst.regClass() == v1) {
2673 Temp src0 = bld.tmp(v1);
2674 Temp src1 = bld.tmp(v1);
2675 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2676 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2677 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2678 else
2679 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2680 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2681 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2682 } else {
2683 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2684 nir_print_instr(&instr->instr, stderr);
2685 fprintf(stderr, "\n");
2686 }
2687 break;
2688 }
2689 case nir_op_unpack_half_2x16_split_x: {
2690 if (dst.regClass() == v1) {
2691 Builder bld(ctx->program, ctx->block);
2692 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2693 } else {
2694 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2695 nir_print_instr(&instr->instr, stderr);
2696 fprintf(stderr, "\n");
2697 }
2698 break;
2699 }
2700 case nir_op_unpack_half_2x16_split_y: {
2701 if (dst.regClass() == v1) {
2702 Builder bld(ctx->program, ctx->block);
2703 /* TODO: use SDWA here */
2704 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2705 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2706 } else {
2707 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2708 nir_print_instr(&instr->instr, stderr);
2709 fprintf(stderr, "\n");
2710 }
2711 break;
2712 }
2713 case nir_op_fquantize2f16: {
2714 Temp src = get_alu_src(ctx, instr->src[0]);
2715 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2716 Temp f32, cmp_res;
2717
2718 if (ctx->program->chip_class >= GFX8) {
2719 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2720 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2721 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2722 } else {
2723 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2724 * so compare the result and flush to 0 if it's smaller.
2725 */
2726 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2727 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2728 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2729 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2730 cmp_res = vop3->definitions[0].getTemp();
2731 }
2732
2733 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2734 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2735 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2736 } else {
2737 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2738 }
2739 break;
2740 }
2741 case nir_op_bfm: {
2742 Temp bits = get_alu_src(ctx, instr->src[0]);
2743 Temp offset = get_alu_src(ctx, instr->src[1]);
2744
2745 if (dst.regClass() == s1) {
2746 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2747 } else if (dst.regClass() == v1) {
2748 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2749 } else {
2750 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2751 nir_print_instr(&instr->instr, stderr);
2752 fprintf(stderr, "\n");
2753 }
2754 break;
2755 }
2756 case nir_op_bitfield_select: {
2757 /* (mask & insert) | (~mask & base) */
2758 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2759 Temp insert = get_alu_src(ctx, instr->src[1]);
2760 Temp base = get_alu_src(ctx, instr->src[2]);
2761
2762 /* dst = (insert & bitmask) | (base & ~bitmask) */
2763 if (dst.regClass() == s1) {
2764 aco_ptr<Instruction> sop2;
2765 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2766 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2767 Operand lhs;
2768 if (const_insert && const_bitmask) {
2769 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2770 } else {
2771 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2772 lhs = Operand(insert);
2773 }
2774
2775 Operand rhs;
2776 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2777 if (const_base && const_bitmask) {
2778 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2779 } else {
2780 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2781 rhs = Operand(base);
2782 }
2783
2784 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2785
2786 } else if (dst.regClass() == v1) {
2787 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2788 base = as_vgpr(ctx, base);
2789 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2790 insert = as_vgpr(ctx, insert);
2791
2792 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2793
2794 } else {
2795 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2796 nir_print_instr(&instr->instr, stderr);
2797 fprintf(stderr, "\n");
2798 }
2799 break;
2800 }
2801 case nir_op_ubfe:
2802 case nir_op_ibfe: {
2803 Temp base = get_alu_src(ctx, instr->src[0]);
2804 Temp offset = get_alu_src(ctx, instr->src[1]);
2805 Temp bits = get_alu_src(ctx, instr->src[2]);
2806
2807 if (dst.type() == RegType::sgpr) {
2808 Operand extract;
2809 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2810 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2811 if (const_offset && const_bits) {
2812 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2813 extract = Operand(const_extract);
2814 } else {
2815 Operand width;
2816 if (const_bits) {
2817 width = Operand(const_bits->u32 << 16);
2818 } else {
2819 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2820 }
2821 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2822 }
2823
2824 aco_opcode opcode;
2825 if (dst.regClass() == s1) {
2826 if (instr->op == nir_op_ubfe)
2827 opcode = aco_opcode::s_bfe_u32;
2828 else
2829 opcode = aco_opcode::s_bfe_i32;
2830 } else if (dst.regClass() == s2) {
2831 if (instr->op == nir_op_ubfe)
2832 opcode = aco_opcode::s_bfe_u64;
2833 else
2834 opcode = aco_opcode::s_bfe_i64;
2835 } else {
2836 unreachable("Unsupported BFE bit size");
2837 }
2838
2839 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2840
2841 } else {
2842 aco_opcode opcode;
2843 if (dst.regClass() == v1) {
2844 if (instr->op == nir_op_ubfe)
2845 opcode = aco_opcode::v_bfe_u32;
2846 else
2847 opcode = aco_opcode::v_bfe_i32;
2848 } else {
2849 unreachable("Unsupported BFE bit size");
2850 }
2851
2852 emit_vop3a_instruction(ctx, instr, opcode, dst);
2853 }
2854 break;
2855 }
2856 case nir_op_bit_count: {
2857 Temp src = get_alu_src(ctx, instr->src[0]);
2858 if (src.regClass() == s1) {
2859 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2860 } else if (src.regClass() == v1) {
2861 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2862 } else if (src.regClass() == v2) {
2863 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2864 emit_extract_vector(ctx, src, 1, v1),
2865 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2866 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2867 } else if (src.regClass() == s2) {
2868 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2869 } else {
2870 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2871 nir_print_instr(&instr->instr, stderr);
2872 fprintf(stderr, "\n");
2873 }
2874 break;
2875 }
2876 case nir_op_flt: {
2877 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2878 break;
2879 }
2880 case nir_op_fge: {
2881 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2882 break;
2883 }
2884 case nir_op_feq: {
2885 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2886 break;
2887 }
2888 case nir_op_fne: {
2889 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2890 break;
2891 }
2892 case nir_op_ilt: {
2893 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);
2894 break;
2895 }
2896 case nir_op_ige: {
2897 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);
2898 break;
2899 }
2900 case nir_op_ieq: {
2901 if (instr->src[0].src.ssa->bit_size == 1)
2902 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2903 else
2904 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,
2905 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2906 break;
2907 }
2908 case nir_op_ine: {
2909 if (instr->src[0].src.ssa->bit_size == 1)
2910 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2911 else
2912 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,
2913 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2914 break;
2915 }
2916 case nir_op_ult: {
2917 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);
2918 break;
2919 }
2920 case nir_op_uge: {
2921 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);
2922 break;
2923 }
2924 case nir_op_fddx:
2925 case nir_op_fddy:
2926 case nir_op_fddx_fine:
2927 case nir_op_fddy_fine:
2928 case nir_op_fddx_coarse:
2929 case nir_op_fddy_coarse: {
2930 Temp src = get_alu_src(ctx, instr->src[0]);
2931 uint16_t dpp_ctrl1, dpp_ctrl2;
2932 if (instr->op == nir_op_fddx_fine) {
2933 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2934 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2935 } else if (instr->op == nir_op_fddy_fine) {
2936 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2937 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2938 } else {
2939 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2940 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2941 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2942 else
2943 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2944 }
2945
2946 Temp tmp;
2947 if (ctx->program->chip_class >= GFX8) {
2948 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2949 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2950 } else {
2951 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2952 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2953 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2954 }
2955 emit_wqm(ctx, tmp, dst, true);
2956 break;
2957 }
2958 default:
2959 fprintf(stderr, "Unknown NIR ALU instr: ");
2960 nir_print_instr(&instr->instr, stderr);
2961 fprintf(stderr, "\n");
2962 }
2963 }
2964
2965 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
2966 {
2967 Temp dst = get_ssa_temp(ctx, &instr->def);
2968
2969 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2970 // which get truncated the lsb if double and msb if int
2971 // for now, we only use s_mov_b64 with 64bit inline constants
2972 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
2973 assert(dst.type() == RegType::sgpr);
2974
2975 Builder bld(ctx->program, ctx->block);
2976
2977 if (instr->def.bit_size == 1) {
2978 assert(dst.regClass() == bld.lm);
2979 int val = instr->value[0].b ? -1 : 0;
2980 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
2981 bld.sop1(Builder::s_mov, Definition(dst), op);
2982 } else if (instr->def.bit_size == 8) {
2983 /* ensure that the value is correctly represented in the low byte of the register */
2984 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
2985 } else if (instr->def.bit_size == 16) {
2986 /* ensure that the value is correctly represented in the low half of the register */
2987 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
2988 } else if (dst.size() == 1) {
2989 bld.copy(Definition(dst), Operand(instr->value[0].u32));
2990 } else {
2991 assert(dst.size() != 1);
2992 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
2993 if (instr->def.bit_size == 64)
2994 for (unsigned i = 0; i < dst.size(); i++)
2995 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
2996 else {
2997 for (unsigned i = 0; i < dst.size(); i++)
2998 vec->operands[i] = Operand{instr->value[i].u32};
2999 }
3000 vec->definitions[0] = Definition(dst);
3001 ctx->block->instructions.emplace_back(std::move(vec));
3002 }
3003 }
3004
3005 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3006 {
3007 uint32_t new_mask = 0;
3008 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3009 if (mask & (1u << i))
3010 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3011 return new_mask;
3012 }
3013
3014 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst)
3015 {
3016 Builder bld(ctx->program, ctx->block);
3017 if (offset.isTemp()) {
3018 Temp tmp[3] = {vec, vec, vec};
3019
3020 if (vec.size() == 3) {
3021 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
3022 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
3023 } else if (vec.size() == 2) {
3024 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
3025 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
3026 }
3027 for (unsigned i = 0; i < dst.size(); i++)
3028 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
3029
3030 vec = tmp[0];
3031 if (dst.size() == 2)
3032 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
3033
3034 offset = Operand(0u);
3035 }
3036
3037 if (vec.bytes() == dst.bytes() && offset.constantValue() == 0)
3038 bld.copy(Definition(dst), vec);
3039 else
3040 trim_subdword_vector(ctx, vec, dst, vec.bytes(), ((1 << dst.bytes()) - 1) << offset.constantValue());
3041 }
3042
3043 struct LoadEmitInfo {
3044 Operand offset;
3045 Temp dst;
3046 unsigned num_components;
3047 unsigned component_size;
3048 Temp resource = Temp(0, s1);
3049 unsigned component_stride = 0;
3050 unsigned const_offset = 0;
3051 unsigned align_mul = 0;
3052 unsigned align_offset = 0;
3053
3054 bool glc = false;
3055 unsigned swizzle_component_size = 0;
3056 barrier_interaction barrier = barrier_none;
3057 bool can_reorder = true;
3058 Temp soffset = Temp(0, s1);
3059 };
3060
3061 using LoadCallback = Temp(*)(
3062 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3063 unsigned align, unsigned const_offset, Temp dst_hint);
3064
3065 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3066 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3067 {
3068 unsigned load_size = info->num_components * info->component_size;
3069 unsigned component_size = info->component_size;
3070
3071 unsigned num_vals = 0;
3072 Temp vals[info->dst.bytes()];
3073
3074 unsigned const_offset = info->const_offset;
3075
3076 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3077 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3078
3079 unsigned bytes_read = 0;
3080 while (bytes_read < load_size) {
3081 unsigned bytes_needed = load_size - bytes_read;
3082
3083 /* add buffer for unaligned loads */
3084 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3085
3086 if (byte_align) {
3087 if ((bytes_needed > 2 || !supports_8bit_16bit_loads) && byte_align_loads) {
3088 if (info->component_stride) {
3089 assert(supports_8bit_16bit_loads && "unimplemented");
3090 bytes_needed = 2;
3091 byte_align = 0;
3092 } else {
3093 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3094 bytes_needed = align(bytes_needed, 4);
3095 }
3096 } else {
3097 byte_align = 0;
3098 }
3099 }
3100
3101 if (info->swizzle_component_size)
3102 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3103 if (info->component_stride)
3104 bytes_needed = MIN2(bytes_needed, info->component_size);
3105
3106 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3107
3108 /* reduce constant offset */
3109 Operand offset = info->offset;
3110 unsigned reduced_const_offset = const_offset;
3111 bool remove_const_offset_completely = need_to_align_offset;
3112 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3113 unsigned to_add = const_offset;
3114 if (remove_const_offset_completely) {
3115 reduced_const_offset = 0;
3116 } else {
3117 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3118 reduced_const_offset %= max_const_offset_plus_one;
3119 }
3120 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3121 if (offset.isConstant()) {
3122 offset = Operand(offset.constantValue() + to_add);
3123 } else if (offset_tmp.regClass() == s1) {
3124 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3125 offset_tmp, Operand(to_add));
3126 } else if (offset_tmp.regClass() == v1) {
3127 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3128 } else {
3129 Temp lo = bld.tmp(offset_tmp.type(), 1);
3130 Temp hi = bld.tmp(offset_tmp.type(), 1);
3131 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3132
3133 if (offset_tmp.regClass() == s2) {
3134 Temp carry = bld.tmp(s1);
3135 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3136 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3137 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3138 } else {
3139 Temp new_lo = bld.tmp(v1);
3140 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3141 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3142 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3143 }
3144 }
3145 }
3146
3147 /* align offset down if needed */
3148 Operand aligned_offset = offset;
3149 if (need_to_align_offset) {
3150 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3151 if (offset.isConstant()) {
3152 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3153 } else if (offset_tmp.regClass() == s1) {
3154 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3155 } else if (offset_tmp.regClass() == s2) {
3156 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3157 } else if (offset_tmp.regClass() == v1) {
3158 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3159 } else if (offset_tmp.regClass() == v2) {
3160 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3161 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3162 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3163 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3164 }
3165 }
3166 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3167 bld.copy(bld.def(s1), aligned_offset);
3168
3169 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3170 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3171 reduced_const_offset, byte_align ? Temp() : info->dst);
3172
3173 /* shift result right if needed */
3174 if (byte_align) {
3175 Operand align((uint32_t)byte_align);
3176 if (byte_align == -1) {
3177 if (offset.isConstant())
3178 align = Operand(offset.constantValue() % 4u);
3179 else if (offset.size() == 2)
3180 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3181 else
3182 align = offset;
3183 }
3184
3185 if (align.isTemp() || align.constantValue()) {
3186 assert(val.bytes() >= load_size && "unimplemented");
3187 Temp new_val = bld.tmp(RegClass::get(val.type(), load_size));
3188 if (val.type() == RegType::sgpr)
3189 byte_align_scalar(ctx, val, align, new_val);
3190 else
3191 byte_align_vector(ctx, val, align, new_val);
3192 val = new_val;
3193 }
3194 }
3195
3196 /* add result to list and advance */
3197 if (info->component_stride) {
3198 assert(val.bytes() == info->component_size && "unimplemented");
3199 const_offset += info->component_stride;
3200 align_offset = (align_offset + info->component_stride) % align_mul;
3201 } else {
3202 const_offset += val.bytes();
3203 align_offset = (align_offset + val.bytes()) % align_mul;
3204 }
3205 bytes_read += val.bytes();
3206 vals[num_vals++] = val;
3207 }
3208
3209 /* the callback wrote directly to dst */
3210 if (vals[0] == info->dst) {
3211 assert(num_vals == 1);
3212 emit_split_vector(ctx, info->dst, info->num_components);
3213 return;
3214 }
3215
3216 /* create array of components */
3217 unsigned components_split = 0;
3218 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3219 bool has_vgprs = false;
3220 for (unsigned i = 0; i < num_vals;) {
3221 Temp tmp[num_vals];
3222 unsigned num_tmps = 0;
3223 unsigned tmp_size = 0;
3224 RegType reg_type = RegType::sgpr;
3225 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3226 if (vals[i].type() == RegType::vgpr)
3227 reg_type = RegType::vgpr;
3228 tmp_size += vals[i].bytes();
3229 tmp[num_tmps++] = vals[i++];
3230 }
3231 if (num_tmps > 1) {
3232 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3233 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3234 for (unsigned i = 0; i < num_vals; i++)
3235 vec->operands[i] = Operand(tmp[i]);
3236 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3237 vec->definitions[0] = Definition(tmp[0]);
3238 bld.insert(std::move(vec));
3239 }
3240
3241 if (tmp[0].bytes() % component_size) {
3242 /* trim tmp[0] */
3243 assert(i == num_vals);
3244 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3245 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3246 }
3247
3248 RegClass elem_rc = RegClass::get(reg_type, component_size);
3249
3250 unsigned start = components_split;
3251
3252 if (tmp_size == elem_rc.bytes()) {
3253 allocated_vec[components_split++] = tmp[0];
3254 } else {
3255 assert(tmp_size % elem_rc.bytes() == 0);
3256 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3257 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3258 for (unsigned i = 0; i < split->definitions.size(); i++) {
3259 Temp component = bld.tmp(elem_rc);
3260 allocated_vec[components_split++] = component;
3261 split->definitions[i] = Definition(component);
3262 }
3263 split->operands[0] = Operand(tmp[0]);
3264 bld.insert(std::move(split));
3265 }
3266
3267 /* try to p_as_uniform early so we can create more optimizable code and
3268 * also update allocated_vec */
3269 for (unsigned j = start; j < components_split; j++) {
3270 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3271 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3272 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3273 }
3274 }
3275
3276 /* concatenate components and p_as_uniform() result if needed */
3277 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3278 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3279
3280 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3281
3282 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3283 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3284 for (unsigned i = 0; i < info->num_components; i++)
3285 vec->operands[i] = Operand(allocated_vec[i]);
3286 if (padding_bytes)
3287 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3288 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3289 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3290 vec->definitions[0] = Definition(tmp);
3291 bld.insert(std::move(vec));
3292 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3293 } else {
3294 vec->definitions[0] = Definition(info->dst);
3295 bld.insert(std::move(vec));
3296 }
3297 }
3298
3299 Operand load_lds_size_m0(Builder& bld)
3300 {
3301 /* TODO: m0 does not need to be initialized on GFX9+ */
3302 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3303 }
3304
3305 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3306 Temp offset, unsigned bytes_needed,
3307 unsigned align, unsigned const_offset,
3308 Temp dst_hint)
3309 {
3310 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3311
3312 Operand m = load_lds_size_m0(bld);
3313
3314 bool large_ds_read = bld.program->chip_class >= GFX7;
3315 bool usable_read2 = bld.program->chip_class >= GFX7;
3316
3317 bool read2 = false;
3318 unsigned size = 0;
3319 aco_opcode op;
3320 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3321 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3322 size = 16;
3323 op = aco_opcode::ds_read_b128;
3324 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3325 size = 16;
3326 read2 = true;
3327 op = aco_opcode::ds_read2_b64;
3328 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3329 size = 12;
3330 op = aco_opcode::ds_read_b96;
3331 } else if (bytes_needed >= 8 && align % 8 == 0) {
3332 size = 8;
3333 op = aco_opcode::ds_read_b64;
3334 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3335 size = 8;
3336 read2 = true;
3337 op = aco_opcode::ds_read2_b32;
3338 } else if (bytes_needed >= 4 && align % 4 == 0) {
3339 size = 4;
3340 op = aco_opcode::ds_read_b32;
3341 } else if (bytes_needed >= 2 && align % 2 == 0) {
3342 size = 2;
3343 op = aco_opcode::ds_read_u16;
3344 } else {
3345 size = 1;
3346 op = aco_opcode::ds_read_u8;
3347 }
3348
3349 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3350 if (const_offset >= max_offset_plus_one) {
3351 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3352 const_offset %= max_offset_plus_one;
3353 }
3354
3355 if (read2)
3356 const_offset /= (size / 2u);
3357
3358 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3359 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3360 if (read2)
3361 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3362 else
3363 bld.ds(op, Definition(val), offset, m, const_offset);
3364
3365 if (size < 4)
3366 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3367
3368 return val;
3369 }
3370
3371 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3372
3373 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3374 Temp offset, unsigned bytes_needed,
3375 unsigned align, unsigned const_offset,
3376 Temp dst_hint)
3377 {
3378 unsigned size = 0;
3379 aco_opcode op;
3380 if (bytes_needed <= 4) {
3381 size = 1;
3382 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3383 } else if (bytes_needed <= 8) {
3384 size = 2;
3385 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3386 } else if (bytes_needed <= 16) {
3387 size = 4;
3388 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3389 } else if (bytes_needed <= 32) {
3390 size = 8;
3391 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3392 } else {
3393 size = 16;
3394 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3395 }
3396 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3397 if (info->resource.id()) {
3398 load->operands[0] = Operand(info->resource);
3399 load->operands[1] = Operand(offset);
3400 } else {
3401 load->operands[0] = Operand(offset);
3402 load->operands[1] = Operand(0u);
3403 }
3404 RegClass rc(RegType::sgpr, size);
3405 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3406 load->definitions[0] = Definition(val);
3407 load->glc = info->glc;
3408 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3409 load->barrier = info->barrier;
3410 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3411 bld.insert(std::move(load));
3412 return val;
3413 }
3414
3415 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3416
3417 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3418 Temp offset, unsigned bytes_needed,
3419 unsigned align_, unsigned const_offset,
3420 Temp dst_hint)
3421 {
3422 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3423 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3424
3425 if (info->soffset.id()) {
3426 if (soffset.isTemp())
3427 vaddr = bld.copy(bld.def(v1), soffset);
3428 soffset = Operand(info->soffset);
3429 }
3430
3431 unsigned bytes_size = 0;
3432 aco_opcode op;
3433 if (bytes_needed == 1) {
3434 bytes_size = 1;
3435 op = aco_opcode::buffer_load_ubyte;
3436 } else if (bytes_needed == 2) {
3437 bytes_size = 2;
3438 op = aco_opcode::buffer_load_ushort;
3439 } else if (bytes_needed <= 4) {
3440 bytes_size = 4;
3441 op = aco_opcode::buffer_load_dword;
3442 } else if (bytes_needed <= 8) {
3443 bytes_size = 8;
3444 op = aco_opcode::buffer_load_dwordx2;
3445 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3446 bytes_size = 12;
3447 op = aco_opcode::buffer_load_dwordx3;
3448 } else {
3449 bytes_size = 16;
3450 op = aco_opcode::buffer_load_dwordx4;
3451 }
3452 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3453 mubuf->operands[0] = Operand(info->resource);
3454 mubuf->operands[1] = vaddr;
3455 mubuf->operands[2] = soffset;
3456 mubuf->offen = (offset.type() == RegType::vgpr);
3457 mubuf->glc = info->glc;
3458 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3459 mubuf->barrier = info->barrier;
3460 mubuf->can_reorder = info->can_reorder;
3461 mubuf->offset = const_offset;
3462 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3463 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3464 mubuf->definitions[0] = Definition(val);
3465 bld.insert(std::move(mubuf));
3466
3467 if (bytes_size < 4)
3468 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3469
3470 return val;
3471 }
3472
3473 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3474
3475 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3476 {
3477 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3478 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3479
3480 if (addr.type() == RegType::vgpr)
3481 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3482 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3483 }
3484
3485 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3486 Temp offset, unsigned bytes_needed,
3487 unsigned align_, unsigned const_offset,
3488 Temp dst_hint)
3489 {
3490 unsigned bytes_size = 0;
3491 bool mubuf = bld.program->chip_class == GFX6;
3492 bool global = bld.program->chip_class >= GFX9;
3493 aco_opcode op;
3494 if (bytes_needed == 1) {
3495 bytes_size = 1;
3496 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3497 } else if (bytes_needed == 2) {
3498 bytes_size = 2;
3499 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3500 } else if (bytes_needed <= 4) {
3501 bytes_size = 4;
3502 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3503 } else if (bytes_needed <= 8) {
3504 bytes_size = 8;
3505 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3506 } else if (bytes_needed <= 12 && !mubuf) {
3507 bytes_size = 12;
3508 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3509 } else {
3510 bytes_size = 16;
3511 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3512 }
3513 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3514 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3515 if (mubuf) {
3516 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3517 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3518 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3519 mubuf->operands[2] = Operand(0u);
3520 mubuf->glc = info->glc;
3521 mubuf->dlc = false;
3522 mubuf->offset = 0;
3523 mubuf->addr64 = offset.type() == RegType::vgpr;
3524 mubuf->disable_wqm = false;
3525 mubuf->barrier = info->barrier;
3526 mubuf->definitions[0] = Definition(val);
3527 bld.insert(std::move(mubuf));
3528 } else {
3529 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3530
3531 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3532 flat->operands[0] = Operand(offset);
3533 flat->operands[1] = Operand(s1);
3534 flat->glc = info->glc;
3535 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3536 flat->barrier = info->barrier;
3537 flat->offset = 0u;
3538 flat->definitions[0] = Definition(val);
3539 bld.insert(std::move(flat));
3540 }
3541
3542 if (bytes_size < 4)
3543 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3544
3545 return val;
3546 }
3547
3548 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3549
3550 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3551 Temp address, unsigned base_offset, unsigned align)
3552 {
3553 assert(util_is_power_of_two_nonzero(align));
3554
3555 Builder bld(ctx->program, ctx->block);
3556
3557 unsigned num_components = dst.bytes() / elem_size_bytes;
3558 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3559 info.align_mul = align;
3560 info.align_offset = 0;
3561 info.barrier = barrier_shared;
3562 info.can_reorder = false;
3563 info.const_offset = base_offset;
3564 emit_lds_load(ctx, bld, &info);
3565
3566 return dst;
3567 }
3568
3569 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3570 {
3571 if (!count)
3572 return;
3573
3574 Builder bld(ctx->program, ctx->block);
3575
3576 ASSERTED bool is_subdword = false;
3577 for (unsigned i = 0; i < count; i++)
3578 is_subdword |= offsets[i] % 4;
3579 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3580 assert(!is_subdword || dst_type == RegType::vgpr);
3581
3582 /* count == 1 fast path */
3583 if (count == 1) {
3584 if (dst_type == RegType::sgpr)
3585 dst[0] = bld.as_uniform(src);
3586 else
3587 dst[0] = as_vgpr(ctx, src);
3588 return;
3589 }
3590
3591 for (unsigned i = 0; i < count - 1; i++)
3592 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3593 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3594
3595 if (is_subdword && src.type() == RegType::sgpr) {
3596 src = as_vgpr(ctx, src);
3597 } else {
3598 /* use allocated_vec if possible */
3599 auto it = ctx->allocated_vec.find(src.id());
3600 if (it != ctx->allocated_vec.end()) {
3601 unsigned total_size = 0;
3602 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3603 total_size += it->second[i].bytes();
3604 if (total_size != src.bytes())
3605 goto split;
3606
3607 unsigned elem_size = it->second[0].bytes();
3608
3609 for (unsigned i = 0; i < count; i++) {
3610 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3611 goto split;
3612 }
3613
3614 for (unsigned i = 0; i < count; i++) {
3615 unsigned start_idx = offsets[i] / elem_size;
3616 unsigned op_count = dst[i].bytes() / elem_size;
3617 if (op_count == 1) {
3618 if (dst_type == RegType::sgpr)
3619 dst[i] = bld.as_uniform(it->second[start_idx]);
3620 else
3621 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3622 continue;
3623 }
3624
3625 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3626 for (unsigned j = 0; j < op_count; j++) {
3627 Temp tmp = it->second[start_idx + j];
3628 if (dst_type == RegType::sgpr)
3629 tmp = bld.as_uniform(tmp);
3630 vec->operands[j] = Operand(tmp);
3631 }
3632 vec->definitions[0] = Definition(dst[i]);
3633 bld.insert(std::move(vec));
3634 }
3635 return;
3636 }
3637 }
3638
3639 if (dst_type == RegType::sgpr)
3640 src = bld.as_uniform(src);
3641
3642 split:
3643 /* just split it */
3644 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3645 split->operands[0] = Operand(src);
3646 for (unsigned i = 0; i < count; i++)
3647 split->definitions[i] = Definition(dst[i]);
3648 bld.insert(std::move(split));
3649 }
3650
3651 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3652 int *start, int *count)
3653 {
3654 unsigned start_elem = ffs(todo_mask) - 1;
3655 bool skip = !(mask & (1 << start_elem));
3656 if (skip)
3657 mask = ~mask & todo_mask;
3658
3659 mask &= todo_mask;
3660
3661 u_bit_scan_consecutive_range(&mask, start, count);
3662
3663 return !skip;
3664 }
3665
3666 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3667 {
3668 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3669 }
3670
3671 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3672 Temp address, unsigned base_offset, unsigned align)
3673 {
3674 assert(util_is_power_of_two_nonzero(align));
3675 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3676
3677 Builder bld(ctx->program, ctx->block);
3678 bool large_ds_write = ctx->options->chip_class >= GFX7;
3679 bool usable_write2 = ctx->options->chip_class >= GFX7;
3680
3681 unsigned write_count = 0;
3682 Temp write_datas[32];
3683 unsigned offsets[32];
3684 aco_opcode opcodes[32];
3685
3686 wrmask = widen_mask(wrmask, elem_size_bytes);
3687
3688 uint32_t todo = u_bit_consecutive(0, data.bytes());
3689 while (todo) {
3690 int offset, bytes;
3691 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3692 offsets[write_count] = offset;
3693 opcodes[write_count] = aco_opcode::num_opcodes;
3694 write_count++;
3695 advance_write_mask(&todo, offset, bytes);
3696 continue;
3697 }
3698
3699 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3700 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3701 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3702 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3703
3704 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3705 aco_opcode op = aco_opcode::num_opcodes;
3706 if (bytes >= 16 && aligned16 && large_ds_write) {
3707 op = aco_opcode::ds_write_b128;
3708 bytes = 16;
3709 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3710 op = aco_opcode::ds_write_b96;
3711 bytes = 12;
3712 } else if (bytes >= 8 && aligned8) {
3713 op = aco_opcode::ds_write_b64;
3714 bytes = 8;
3715 } else if (bytes >= 4 && aligned4) {
3716 op = aco_opcode::ds_write_b32;
3717 bytes = 4;
3718 } else if (bytes >= 2 && aligned2) {
3719 op = aco_opcode::ds_write_b16;
3720 bytes = 2;
3721 } else if (bytes >= 1) {
3722 op = aco_opcode::ds_write_b8;
3723 bytes = 1;
3724 } else {
3725 assert(false);
3726 }
3727
3728 offsets[write_count] = offset;
3729 opcodes[write_count] = op;
3730 write_count++;
3731 advance_write_mask(&todo, offset, bytes);
3732 }
3733
3734 Operand m = load_lds_size_m0(bld);
3735
3736 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3737
3738 for (unsigned i = 0; i < write_count; i++) {
3739 aco_opcode op = opcodes[i];
3740 if (op == aco_opcode::num_opcodes)
3741 continue;
3742
3743 Temp data = write_datas[i];
3744
3745 unsigned second = write_count;
3746 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3747 for (second = i + 1; second < write_count; second++) {
3748 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3749 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3750 opcodes[second] = aco_opcode::num_opcodes;
3751 break;
3752 }
3753 }
3754 }
3755
3756 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3757 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3758
3759 unsigned inline_offset = base_offset + offsets[i];
3760 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3761 Temp address_offset = address;
3762 if (inline_offset > max_offset) {
3763 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3764 inline_offset = offsets[i];
3765 }
3766 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3767
3768 if (write2) {
3769 Temp second_data = write_datas[second];
3770 inline_offset /= data.bytes();
3771 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3772 } else {
3773 bld.ds(op, address_offset, data, m, inline_offset);
3774 }
3775 }
3776 }
3777
3778 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3779 {
3780 unsigned align = 16;
3781 if (const_offset)
3782 align = std::min(align, 1u << (ffs(const_offset) - 1));
3783
3784 return align;
3785 }
3786
3787
3788 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3789 {
3790 switch (bytes) {
3791 case 1:
3792 assert(!smem);
3793 return aco_opcode::buffer_store_byte;
3794 case 2:
3795 assert(!smem);
3796 return aco_opcode::buffer_store_short;
3797 case 4:
3798 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3799 case 8:
3800 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3801 case 12:
3802 assert(!smem);
3803 return aco_opcode::buffer_store_dwordx3;
3804 case 16:
3805 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3806 }
3807 unreachable("Unexpected store size");
3808 return aco_opcode::num_opcodes;
3809 }
3810
3811 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3812 Temp data, unsigned writemask, int swizzle_element_size,
3813 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3814 {
3815 unsigned write_count_with_skips = 0;
3816 bool skips[16];
3817
3818 /* determine how to split the data */
3819 unsigned todo = u_bit_consecutive(0, data.bytes());
3820 while (todo) {
3821 int offset, bytes;
3822 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3823 offsets[write_count_with_skips] = offset;
3824 if (skips[write_count_with_skips]) {
3825 advance_write_mask(&todo, offset, bytes);
3826 write_count_with_skips++;
3827 continue;
3828 }
3829
3830 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3831 * larger than swizzle_element_size */
3832 bytes = MIN2(bytes, swizzle_element_size);
3833 if (bytes % 4)
3834 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3835
3836 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3837 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3838 bytes = 8;
3839
3840 /* dword or larger stores have to be dword-aligned */
3841 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3842 unsigned align_offset = instr ? nir_intrinsic_align_mul(instr) : 0;
3843 bool dword_aligned = (align_offset + offset) % 4 == 0 && align_mul % 4 == 0;
3844 if (bytes >= 4 && !dword_aligned)
3845 bytes = MIN2(bytes, 2);
3846
3847 advance_write_mask(&todo, offset, bytes);
3848 write_count_with_skips++;
3849 }
3850
3851 /* actually split data */
3852 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3853
3854 /* remove skips */
3855 for (unsigned i = 0; i < write_count_with_skips; i++) {
3856 if (skips[i])
3857 continue;
3858 write_datas[*write_count] = write_datas[i];
3859 offsets[*write_count] = offsets[i];
3860 (*write_count)++;
3861 }
3862 }
3863
3864 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3865 unsigned split_cnt = 0u, Temp dst = Temp())
3866 {
3867 Builder bld(ctx->program, ctx->block);
3868 unsigned dword_size = elem_size_bytes / 4;
3869
3870 if (!dst.id())
3871 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3872
3873 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3874 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3875 instr->definitions[0] = Definition(dst);
3876
3877 for (unsigned i = 0; i < cnt; ++i) {
3878 if (arr[i].id()) {
3879 assert(arr[i].size() == dword_size);
3880 allocated_vec[i] = arr[i];
3881 instr->operands[i] = Operand(arr[i]);
3882 } else {
3883 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3884 allocated_vec[i] = zero;
3885 instr->operands[i] = Operand(zero);
3886 }
3887 }
3888
3889 bld.insert(std::move(instr));
3890
3891 if (split_cnt)
3892 emit_split_vector(ctx, dst, split_cnt);
3893 else
3894 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3895
3896 return dst;
3897 }
3898
3899 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3900 {
3901 if (const_offset >= 4096) {
3902 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3903 const_offset %= 4096u;
3904
3905 if (!voffset.id())
3906 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3907 else if (unlikely(voffset.regClass() == s1))
3908 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3909 else if (likely(voffset.regClass() == v1))
3910 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3911 else
3912 unreachable("Unsupported register class of voffset");
3913 }
3914
3915 return const_offset;
3916 }
3917
3918 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3919 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3920 {
3921 assert(vdata.id());
3922 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3923 assert(vdata.size() >= 1 && vdata.size() <= 4);
3924
3925 Builder bld(ctx->program, ctx->block);
3926 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
3927 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3928
3929 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3930 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3931 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3932 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3933 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3934
3935 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3936 }
3937
3938 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3939 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3940 bool allow_combining = true, bool reorder = true, bool slc = false)
3941 {
3942 Builder bld(ctx->program, ctx->block);
3943 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3944 assert(write_mask);
3945 write_mask = widen_mask(write_mask, elem_size_bytes);
3946
3947 unsigned write_count = 0;
3948 Temp write_datas[32];
3949 unsigned offsets[32];
3950 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
3951 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
3952
3953 for (unsigned i = 0; i < write_count; i++) {
3954 unsigned const_offset = offsets[i] + base_const_offset;
3955 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc);
3956 }
3957 }
3958
3959 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3960 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3961 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3962 {
3963 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3964 assert((num_components * elem_size_bytes / 4) == dst.size());
3965 assert(!!stride != allow_combining);
3966
3967 Builder bld(ctx->program, ctx->block);
3968
3969 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
3970 info.component_stride = allow_combining ? 0 : stride;
3971 info.glc = true;
3972 info.swizzle_component_size = allow_combining ? 0 : 4;
3973 info.align_mul = MIN2(elem_size_bytes, 4);
3974 info.align_offset = 0;
3975 info.soffset = soffset;
3976 info.const_offset = base_const_offset;
3977 emit_mubuf_load(ctx, bld, &info);
3978 }
3979
3980 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)
3981 {
3982 Builder bld(ctx->program, ctx->block);
3983 Temp offset = base_offset.first;
3984 unsigned const_offset = base_offset.second;
3985
3986 if (!nir_src_is_const(*off_src)) {
3987 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3988 Temp with_stride;
3989
3990 /* Calculate indirect offset with stride */
3991 if (likely(indirect_offset_arg.regClass() == v1))
3992 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
3993 else if (indirect_offset_arg.regClass() == s1)
3994 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3995 else
3996 unreachable("Unsupported register class of indirect offset");
3997
3998 /* Add to the supplied base offset */
3999 if (offset.id() == 0)
4000 offset = with_stride;
4001 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4002 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4003 else if (offset.size() == 1 && with_stride.size() == 1)
4004 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4005 else
4006 unreachable("Unsupported register class of indirect offset");
4007 } else {
4008 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4009 const_offset += const_offset_arg * stride;
4010 }
4011
4012 return std::make_pair(offset, const_offset);
4013 }
4014
4015 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4016 {
4017 Builder bld(ctx->program, ctx->block);
4018 Temp offset;
4019
4020 if (off1.first.id() && off2.first.id()) {
4021 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4022 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4023 else if (off1.first.size() == 1 && off2.first.size() == 1)
4024 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4025 else
4026 unreachable("Unsupported register class of indirect offset");
4027 } else {
4028 offset = off1.first.id() ? off1.first : off2.first;
4029 }
4030
4031 return std::make_pair(offset, off1.second + off2.second);
4032 }
4033
4034 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4035 {
4036 Builder bld(ctx->program, ctx->block);
4037 unsigned const_offset = offs.second * multiplier;
4038
4039 if (!offs.first.id())
4040 return std::make_pair(offs.first, const_offset);
4041
4042 Temp offset = unlikely(offs.first.regClass() == s1)
4043 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4044 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4045
4046 return std::make_pair(offset, const_offset);
4047 }
4048
4049 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4050 {
4051 Builder bld(ctx->program, ctx->block);
4052
4053 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4054 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4055 /* component is in bytes */
4056 const_offset += nir_intrinsic_component(instr) * component_stride;
4057
4058 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4059 nir_src *off_src = nir_get_io_offset_src(instr);
4060 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4061 }
4062
4063 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4064 {
4065 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4066 }
4067
4068 Temp get_tess_rel_patch_id(isel_context *ctx)
4069 {
4070 Builder bld(ctx->program, ctx->block);
4071
4072 switch (ctx->shader->info.stage) {
4073 case MESA_SHADER_TESS_CTRL:
4074 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4075 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4076 case MESA_SHADER_TESS_EVAL:
4077 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4078 default:
4079 unreachable("Unsupported stage in get_tess_rel_patch_id");
4080 }
4081 }
4082
4083 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4084 {
4085 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4086 Builder bld(ctx->program, ctx->block);
4087
4088 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4089 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4090
4091 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4092
4093 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4094 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4095
4096 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4097 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4098 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4099
4100 return offset_mul(ctx, offs, 4u);
4101 }
4102
4103 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4104 {
4105 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4106 Builder bld(ctx->program, ctx->block);
4107
4108 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4109 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4110 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4111 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4112
4113 std::pair<Temp, unsigned> offs = instr
4114 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4115 : std::make_pair(Temp(), 0u);
4116
4117 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4118 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4119
4120 if (per_vertex) {
4121 assert(instr);
4122
4123 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4124 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4125
4126 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4127 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4128 } else {
4129 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4130 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4131 }
4132
4133 return offs;
4134 }
4135
4136 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4137 {
4138 Builder bld(ctx->program, ctx->block);
4139
4140 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4141 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4142
4143 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4144
4145 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4146 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4147 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4148
4149 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4150 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4151
4152 return offs;
4153 }
4154
4155 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4156 {
4157 Builder bld(ctx->program, ctx->block);
4158
4159 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4160 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4161 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4162 unsigned attr_stride = ctx->tcs_num_patches;
4163
4164 std::pair<Temp, unsigned> offs = instr
4165 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4166 : std::make_pair(Temp(), 0u);
4167
4168 if (const_base_offset)
4169 offs.second += const_base_offset * attr_stride;
4170
4171 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4172 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4173 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4174
4175 return offs;
4176 }
4177
4178 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4179 {
4180 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4181
4182 if (mask == 0)
4183 return false;
4184
4185 unsigned drv_loc = nir_intrinsic_base(instr);
4186 nir_src *off_src = nir_get_io_offset_src(instr);
4187
4188 if (!nir_src_is_const(*off_src)) {
4189 *indirect = true;
4190 return false;
4191 }
4192
4193 *indirect = false;
4194 uint64_t slot = per_vertex
4195 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4196 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4197 return (((uint64_t) 1) << slot) & mask;
4198 }
4199
4200 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4201 {
4202 unsigned write_mask = nir_intrinsic_write_mask(instr);
4203 unsigned component = nir_intrinsic_component(instr);
4204 unsigned idx = nir_intrinsic_base(instr) + component;
4205
4206 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4207 if (off_instr->type != nir_instr_type_load_const)
4208 return false;
4209
4210 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4211 idx += nir_src_as_uint(instr->src[1]) * 4u;
4212
4213 if (instr->src[0].ssa->bit_size == 64)
4214 write_mask = widen_mask(write_mask, 2);
4215
4216 for (unsigned i = 0; i < 8; ++i) {
4217 if (write_mask & (1 << i)) {
4218 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4219 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
4220 }
4221 idx++;
4222 }
4223
4224 return true;
4225 }
4226
4227 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4228 {
4229 /* Only TCS per-vertex inputs are supported by this function.
4230 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4231 */
4232 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4233 return false;
4234
4235 nir_src *off_src = nir_get_io_offset_src(instr);
4236 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4237 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4238 bool can_use_temps = nir_src_is_const(*off_src) &&
4239 vertex_index_instr->type == nir_instr_type_intrinsic &&
4240 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4241
4242 if (!can_use_temps)
4243 return false;
4244
4245 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4246 Temp *src = &ctx->inputs.temps[idx];
4247 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4248
4249 return true;
4250 }
4251
4252 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4253 {
4254 Builder bld(ctx->program, ctx->block);
4255
4256 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4257 /* 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. */
4258 bool indirect_write;
4259 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4260 if (temp_only_input && !indirect_write)
4261 return;
4262 }
4263
4264 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4265 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4266 unsigned write_mask = nir_intrinsic_write_mask(instr);
4267 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4268
4269 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4270 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4271 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4272 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4273 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4274 } else {
4275 Temp lds_base;
4276
4277 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4278 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4279 unsigned itemsize = ctx->stage == vertex_geometry_gs
4280 ? ctx->program->info->vs.es_info.esgs_itemsize
4281 : ctx->program->info->tes.es_info.esgs_itemsize;
4282 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4283 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));
4284 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4285 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4286 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4287 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4288 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4289 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4290 */
4291 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4292 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4293 } else {
4294 unreachable("Invalid LS or ES stage");
4295 }
4296
4297 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4298 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4299 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4300 }
4301 }
4302
4303 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4304 {
4305 if (per_vertex)
4306 return false;
4307
4308 unsigned off = nir_intrinsic_base(instr) * 4u;
4309 return off == ctx->tcs_tess_lvl_out_loc ||
4310 off == ctx->tcs_tess_lvl_in_loc;
4311
4312 }
4313
4314 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4315 {
4316 uint64_t mask = per_vertex
4317 ? ctx->program->info->tcs.tes_inputs_read
4318 : ctx->program->info->tcs.tes_patch_inputs_read;
4319
4320 bool indirect_write = false;
4321 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4322 return indirect_write || output_read_by_tes;
4323 }
4324
4325 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4326 {
4327 uint64_t mask = per_vertex
4328 ? ctx->shader->info.outputs_read
4329 : ctx->shader->info.patch_outputs_read;
4330
4331 bool indirect_write = false;
4332 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4333 return indirect_write || output_read;
4334 }
4335
4336 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4337 {
4338 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4339 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4340
4341 Builder bld(ctx->program, ctx->block);
4342
4343 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4344 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4345 unsigned write_mask = nir_intrinsic_write_mask(instr);
4346
4347 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4348 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4349 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4350
4351 if (write_to_vmem) {
4352 std::pair<Temp, unsigned> vmem_offs = per_vertex
4353 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4354 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4355
4356 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));
4357 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4358 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);
4359 }
4360
4361 if (write_to_lds) {
4362 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4363 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4364 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4365 }
4366 }
4367
4368 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4369 {
4370 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4371 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4372
4373 Builder bld(ctx->program, ctx->block);
4374
4375 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4376 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4377 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4378 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4379
4380 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4381 }
4382
4383 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4384 {
4385 if (ctx->stage == vertex_vs ||
4386 ctx->stage == tess_eval_vs ||
4387 ctx->stage == fragment_fs ||
4388 ctx->stage == ngg_vertex_gs ||
4389 ctx->stage == ngg_tess_eval_gs ||
4390 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4391 bool stored_to_temps = store_output_to_temps(ctx, instr);
4392 if (!stored_to_temps) {
4393 fprintf(stderr, "Unimplemented output offset instruction:\n");
4394 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4395 fprintf(stderr, "\n");
4396 abort();
4397 }
4398 } else if (ctx->stage == vertex_es ||
4399 ctx->stage == vertex_ls ||
4400 ctx->stage == tess_eval_es ||
4401 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4402 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4403 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4404 visit_store_ls_or_es_output(ctx, instr);
4405 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4406 visit_store_tcs_output(ctx, instr, false);
4407 } else {
4408 unreachable("Shader stage not implemented");
4409 }
4410 }
4411
4412 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4413 {
4414 visit_load_tcs_output(ctx, instr, false);
4415 }
4416
4417 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4418 {
4419 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4420 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4421
4422 Builder bld(ctx->program, ctx->block);
4423 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
4424 if (ctx->program->has_16bank_lds)
4425 interp_p1.instr->operands[0].setLateKill(true);
4426 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
4427 }
4428
4429 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4430 {
4431 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4432 for (unsigned i = 0; i < num_components; i++)
4433 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4434 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4435 assert(num_components == 4);
4436 Builder bld(ctx->program, ctx->block);
4437 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4438 }
4439
4440 for (Operand& op : vec->operands)
4441 op = op.isUndefined() ? Operand(0u) : op;
4442
4443 vec->definitions[0] = Definition(dst);
4444 ctx->block->instructions.emplace_back(std::move(vec));
4445 emit_split_vector(ctx, dst, num_components);
4446 return;
4447 }
4448
4449 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4450 {
4451 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4452 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4453 unsigned idx = nir_intrinsic_base(instr);
4454 unsigned component = nir_intrinsic_component(instr);
4455 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4456
4457 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4458 if (offset) {
4459 assert(offset->u32 == 0);
4460 } else {
4461 /* the lower 15bit of the prim_mask contain the offset into LDS
4462 * while the upper bits contain the number of prims */
4463 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4464 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4465 Builder bld(ctx->program, ctx->block);
4466 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4467 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4468 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4469 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4470 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4471 }
4472
4473 if (instr->dest.ssa.num_components == 1) {
4474 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4475 } else {
4476 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4477 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4478 {
4479 Temp tmp = {ctx->program->allocateId(), v1};
4480 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4481 vec->operands[i] = Operand(tmp);
4482 }
4483 vec->definitions[0] = Definition(dst);
4484 ctx->block->instructions.emplace_back(std::move(vec));
4485 }
4486 }
4487
4488 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4489 unsigned offset, unsigned stride, unsigned channels)
4490 {
4491 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4492 if (vtx_info->chan_byte_size != 4 && channels == 3)
4493 return false;
4494 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4495 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4496 }
4497
4498 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4499 unsigned offset, unsigned stride, unsigned *channels)
4500 {
4501 if (!vtx_info->chan_byte_size) {
4502 *channels = vtx_info->num_channels;
4503 return vtx_info->chan_format;
4504 }
4505
4506 unsigned num_channels = *channels;
4507 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4508 unsigned new_channels = num_channels + 1;
4509 /* first, assume more loads is worse and try using a larger data format */
4510 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4511 new_channels++;
4512 /* don't make the attribute potentially out-of-bounds */
4513 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4514 new_channels = 5;
4515 }
4516
4517 if (new_channels == 5) {
4518 /* then try decreasing load size (at the cost of more loads) */
4519 new_channels = *channels;
4520 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4521 new_channels--;
4522 }
4523
4524 if (new_channels < *channels)
4525 *channels = new_channels;
4526 num_channels = new_channels;
4527 }
4528
4529 switch (vtx_info->chan_format) {
4530 case V_008F0C_BUF_DATA_FORMAT_8:
4531 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4532 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4533 case V_008F0C_BUF_DATA_FORMAT_16:
4534 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4535 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4536 case V_008F0C_BUF_DATA_FORMAT_32:
4537 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4538 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4539 }
4540 unreachable("shouldn't reach here");
4541 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4542 }
4543
4544 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4545 * so we may need to fix it up. */
4546 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4547 {
4548 Builder bld(ctx->program, ctx->block);
4549
4550 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4551 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4552
4553 /* For the integer-like cases, do a natural sign extension.
4554 *
4555 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4556 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4557 * exponent.
4558 */
4559 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4560 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4561
4562 /* Convert back to the right type. */
4563 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4564 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4565 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4566 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4567 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4568 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4569 }
4570
4571 return alpha;
4572 }
4573
4574 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4575 {
4576 Builder bld(ctx->program, ctx->block);
4577 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4578 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4579
4580 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4581 if (off_instr->type != nir_instr_type_load_const) {
4582 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4583 nir_print_instr(off_instr, stderr);
4584 fprintf(stderr, "\n");
4585 }
4586 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4587
4588 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4589
4590 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4591 unsigned component = nir_intrinsic_component(instr);
4592 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4593 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4594 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4595 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4596
4597 unsigned dfmt = attrib_format & 0xf;
4598 unsigned nfmt = (attrib_format >> 4) & 0x7;
4599 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4600
4601 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4602 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4603 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4604 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4605 if (post_shuffle)
4606 num_channels = MAX2(num_channels, 3);
4607
4608 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4609 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4610
4611 Temp index;
4612 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4613 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4614 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4615 if (divisor) {
4616 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4617 if (divisor != 1) {
4618 Temp divided = bld.tmp(v1);
4619 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4620 index = bld.vadd32(bld.def(v1), start_instance, divided);
4621 } else {
4622 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4623 }
4624 } else {
4625 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4626 }
4627 } else {
4628 index = bld.vadd32(bld.def(v1),
4629 get_arg(ctx, ctx->args->ac.base_vertex),
4630 get_arg(ctx, ctx->args->ac.vertex_id));
4631 }
4632
4633 Temp channels[num_channels];
4634 unsigned channel_start = 0;
4635 bool direct_fetch = false;
4636
4637 /* skip unused channels at the start */
4638 if (vtx_info->chan_byte_size && !post_shuffle) {
4639 channel_start = ffs(mask) - 1;
4640 for (unsigned i = 0; i < channel_start; i++)
4641 channels[i] = Temp(0, s1);
4642 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4643 num_channels = 3 - (ffs(mask) - 1);
4644 }
4645
4646 /* load channels */
4647 while (channel_start < num_channels) {
4648 unsigned fetch_size = num_channels - channel_start;
4649 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4650 bool expanded = false;
4651
4652 /* use MUBUF when possible to avoid possible alignment issues */
4653 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4654 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4655 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4656 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4657 vtx_info->chan_byte_size == 4;
4658 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4659 if (!use_mubuf) {
4660 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4661 } else {
4662 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4663 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4664 fetch_size = 4;
4665 expanded = true;
4666 }
4667 }
4668
4669 Temp fetch_index = index;
4670 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4671 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4672 fetch_offset = fetch_offset % attrib_stride;
4673 }
4674
4675 Operand soffset(0u);
4676 if (fetch_offset >= 4096) {
4677 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4678 fetch_offset %= 4096;
4679 }
4680
4681 aco_opcode opcode;
4682 switch (fetch_size) {
4683 case 1:
4684 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4685 break;
4686 case 2:
4687 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4688 break;
4689 case 3:
4690 assert(ctx->options->chip_class >= GFX7 ||
4691 (!use_mubuf && ctx->options->chip_class == GFX6));
4692 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4693 break;
4694 case 4:
4695 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4696 break;
4697 default:
4698 unreachable("Unimplemented load_input vector size");
4699 }
4700
4701 Temp fetch_dst;
4702 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4703 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4704 num_channels <= 3)) {
4705 direct_fetch = true;
4706 fetch_dst = dst;
4707 } else {
4708 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4709 }
4710
4711 if (use_mubuf) {
4712 Instruction *mubuf = bld.mubuf(opcode,
4713 Definition(fetch_dst), list, fetch_index, soffset,
4714 fetch_offset, false, true).instr;
4715 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4716 } else {
4717 Instruction *mtbuf = bld.mtbuf(opcode,
4718 Definition(fetch_dst), list, fetch_index, soffset,
4719 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4720 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4721 }
4722
4723 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4724
4725 if (fetch_size == 1) {
4726 channels[channel_start] = fetch_dst;
4727 } else {
4728 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4729 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4730 }
4731
4732 channel_start += fetch_size;
4733 }
4734
4735 if (!direct_fetch) {
4736 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4737 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4738
4739 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4740 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4741 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4742
4743 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4744 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4745 unsigned num_temp = 0;
4746 for (unsigned i = 0; i < dst.size(); i++) {
4747 unsigned idx = i + component;
4748 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4749 Temp channel = channels[swizzle[idx]];
4750 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4751 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4752 vec->operands[i] = Operand(channel);
4753
4754 num_temp++;
4755 elems[i] = channel;
4756 } else if (is_float && idx == 3) {
4757 vec->operands[i] = Operand(0x3f800000u);
4758 } else if (!is_float && idx == 3) {
4759 vec->operands[i] = Operand(1u);
4760 } else {
4761 vec->operands[i] = Operand(0u);
4762 }
4763 }
4764 vec->definitions[0] = Definition(dst);
4765 ctx->block->instructions.emplace_back(std::move(vec));
4766 emit_split_vector(ctx, dst, dst.size());
4767
4768 if (num_temp == dst.size())
4769 ctx->allocated_vec.emplace(dst.id(), elems);
4770 }
4771 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4772 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4773 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4774 if (off_instr->type != nir_instr_type_load_const ||
4775 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4776 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4777 nir_print_instr(off_instr, stderr);
4778 fprintf(stderr, "\n");
4779 }
4780
4781 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4782 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4783 if (offset) {
4784 assert(offset->u32 == 0);
4785 } else {
4786 /* the lower 15bit of the prim_mask contain the offset into LDS
4787 * while the upper bits contain the number of prims */
4788 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4789 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4790 Builder bld(ctx->program, ctx->block);
4791 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4792 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4793 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4794 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4795 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4796 }
4797
4798 unsigned idx = nir_intrinsic_base(instr);
4799 unsigned component = nir_intrinsic_component(instr);
4800 unsigned vertex_id = 2; /* P0 */
4801
4802 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4803 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4804 switch (src0->u32) {
4805 case 0:
4806 vertex_id = 2; /* P0 */
4807 break;
4808 case 1:
4809 vertex_id = 0; /* P10 */
4810 break;
4811 case 2:
4812 vertex_id = 1; /* P20 */
4813 break;
4814 default:
4815 unreachable("invalid vertex index");
4816 }
4817 }
4818
4819 if (dst.size() == 1) {
4820 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4821 } else {
4822 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4823 for (unsigned i = 0; i < dst.size(); i++)
4824 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4825 vec->definitions[0] = Definition(dst);
4826 bld.insert(std::move(vec));
4827 }
4828
4829 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4830 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4831 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4832 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4833 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4834
4835 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4836 } else {
4837 unreachable("Shader stage not implemented");
4838 }
4839 }
4840
4841 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4842 {
4843 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4844
4845 Builder bld(ctx->program, ctx->block);
4846 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4847 Temp vertex_offset;
4848
4849 if (!nir_src_is_const(*vertex_src)) {
4850 /* better code could be created, but this case probably doesn't happen
4851 * much in practice */
4852 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4853 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4854 Temp elem;
4855
4856 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4857 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4858 if (i % 2u)
4859 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4860 } else {
4861 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4862 }
4863
4864 if (vertex_offset.id()) {
4865 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4866 Operand(i), indirect_vertex);
4867 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4868 } else {
4869 vertex_offset = elem;
4870 }
4871 }
4872
4873 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4874 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4875 } else {
4876 unsigned vertex = nir_src_as_uint(*vertex_src);
4877 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4878 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4879 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4880 Operand((vertex % 2u) * 16u), Operand(16u));
4881 else
4882 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4883 }
4884
4885 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4886 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4887 return offset_mul(ctx, offs, 4u);
4888 }
4889
4890 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4891 {
4892 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4893
4894 Builder bld(ctx->program, ctx->block);
4895 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4896 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4897
4898 if (ctx->stage == geometry_gs) {
4899 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4900 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4901 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);
4902 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4903 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4904 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4905 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4906 } else {
4907 unreachable("Unsupported GS stage.");
4908 }
4909 }
4910
4911 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4912 {
4913 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4914
4915 Builder bld(ctx->program, ctx->block);
4916 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4917
4918 if (load_input_from_temps(ctx, instr, dst))
4919 return;
4920
4921 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4922 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4923 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4924
4925 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4926 }
4927
4928 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4929 {
4930 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4931
4932 Builder bld(ctx->program, ctx->block);
4933
4934 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4935 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4936 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4937
4938 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4939 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4940
4941 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4942 }
4943
4944 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4945 {
4946 switch (ctx->shader->info.stage) {
4947 case MESA_SHADER_GEOMETRY:
4948 visit_load_gs_per_vertex_input(ctx, instr);
4949 break;
4950 case MESA_SHADER_TESS_CTRL:
4951 visit_load_tcs_per_vertex_input(ctx, instr);
4952 break;
4953 case MESA_SHADER_TESS_EVAL:
4954 visit_load_tes_per_vertex_input(ctx, instr);
4955 break;
4956 default:
4957 unreachable("Unimplemented shader stage");
4958 }
4959 }
4960
4961 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4962 {
4963 visit_load_tcs_output(ctx, instr, true);
4964 }
4965
4966 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4967 {
4968 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4969 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4970
4971 visit_store_tcs_output(ctx, instr, true);
4972 }
4973
4974 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
4975 {
4976 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4977
4978 Builder bld(ctx->program, ctx->block);
4979 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4980
4981 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
4982 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
4983 Operand tes_w(0u);
4984
4985 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
4986 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
4987 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
4988 tes_w = Operand(tmp);
4989 }
4990
4991 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
4992 emit_split_vector(ctx, tess_coord, 3);
4993 }
4994
4995 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
4996 {
4997 if (ctx->program->info->need_indirect_descriptor_sets) {
4998 Builder bld(ctx->program, ctx->block);
4999 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5000 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5001 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5002 }
5003
5004 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5005 }
5006
5007
5008 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5009 {
5010 Builder bld(ctx->program, ctx->block);
5011 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5012 if (!ctx->divergent_vals[instr->dest.ssa.index])
5013 index = bld.as_uniform(index);
5014 unsigned desc_set = nir_intrinsic_desc_set(instr);
5015 unsigned binding = nir_intrinsic_binding(instr);
5016
5017 Temp desc_ptr;
5018 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5019 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5020 unsigned offset = layout->binding[binding].offset;
5021 unsigned stride;
5022 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5023 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5024 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5025 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5026 offset = pipeline_layout->push_constant_size + 16 * idx;
5027 stride = 16;
5028 } else {
5029 desc_ptr = load_desc_ptr(ctx, desc_set);
5030 stride = layout->binding[binding].size;
5031 }
5032
5033 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5034 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5035 if (stride != 1) {
5036 if (nir_const_index) {
5037 const_index = const_index * stride;
5038 } else if (index.type() == RegType::vgpr) {
5039 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5040 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5041 } else {
5042 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5043 }
5044 }
5045 if (offset) {
5046 if (nir_const_index) {
5047 const_index = const_index + offset;
5048 } else if (index.type() == RegType::vgpr) {
5049 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5050 } else {
5051 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5052 }
5053 }
5054
5055 if (nir_const_index && const_index == 0) {
5056 index = desc_ptr;
5057 } else if (index.type() == RegType::vgpr) {
5058 index = bld.vadd32(bld.def(v1),
5059 nir_const_index ? Operand(const_index) : Operand(index),
5060 Operand(desc_ptr));
5061 } else {
5062 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5063 nir_const_index ? Operand(const_index) : Operand(index),
5064 Operand(desc_ptr));
5065 }
5066
5067 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5068 }
5069
5070 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5071 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5072 bool glc=false, bool readonly=true)
5073 {
5074 Builder bld(ctx->program, ctx->block);
5075
5076 bool use_smem = dst.type() != RegType::vgpr && ((ctx->options->chip_class >= GFX8 && component_size >= 4) || readonly);
5077 if (use_smem)
5078 offset = bld.as_uniform(offset);
5079
5080 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5081 info.glc = glc;
5082 info.barrier = readonly ? barrier_none : barrier_buffer;
5083 info.can_reorder = readonly;
5084 info.align_mul = align_mul;
5085 info.align_offset = align_offset;
5086 if (use_smem)
5087 emit_smem_load(ctx, bld, &info);
5088 else
5089 emit_mubuf_load(ctx, bld, &info);
5090 }
5091
5092 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5093 {
5094 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5095 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5096
5097 Builder bld(ctx->program, ctx->block);
5098
5099 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5100 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5101 unsigned binding = nir_intrinsic_binding(idx_instr);
5102 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5103
5104 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5105 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5106 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5107 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5108 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5109 if (ctx->options->chip_class >= GFX10) {
5110 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5111 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5112 S_008F0C_RESOURCE_LEVEL(1);
5113 } else {
5114 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5115 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5116 }
5117 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5118 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5119 Operand(0xFFFFFFFFu),
5120 Operand(desc_type));
5121 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5122 rsrc, upper_dwords);
5123 } else {
5124 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5125 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5126 }
5127 unsigned size = instr->dest.ssa.bit_size / 8;
5128 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5129 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5130 }
5131
5132 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5133 {
5134 Builder bld(ctx->program, ctx->block);
5135 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5136 unsigned offset = nir_intrinsic_base(instr);
5137 unsigned count = instr->dest.ssa.num_components;
5138 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5139
5140 if (index_cv && instr->dest.ssa.bit_size == 32) {
5141 unsigned start = (offset + index_cv->u32) / 4u;
5142 start -= ctx->args->ac.base_inline_push_consts;
5143 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5144 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5145 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5146 for (unsigned i = 0; i < count; ++i) {
5147 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5148 vec->operands[i] = Operand{elems[i]};
5149 }
5150 vec->definitions[0] = Definition(dst);
5151 ctx->block->instructions.emplace_back(std::move(vec));
5152 ctx->allocated_vec.emplace(dst.id(), elems);
5153 return;
5154 }
5155 }
5156
5157 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5158 if (offset != 0) // TODO check if index != 0 as well
5159 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5160 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5161 Temp vec = dst;
5162 bool trim = false;
5163 bool aligned = true;
5164
5165 if (instr->dest.ssa.bit_size == 8) {
5166 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5167 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5168 if (!aligned)
5169 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5170 } else if (instr->dest.ssa.bit_size == 16) {
5171 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5172 if (!aligned)
5173 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5174 }
5175
5176 aco_opcode op;
5177
5178 switch (vec.size()) {
5179 case 1:
5180 op = aco_opcode::s_load_dword;
5181 break;
5182 case 2:
5183 op = aco_opcode::s_load_dwordx2;
5184 break;
5185 case 3:
5186 vec = bld.tmp(s4);
5187 trim = true;
5188 case 4:
5189 op = aco_opcode::s_load_dwordx4;
5190 break;
5191 case 6:
5192 vec = bld.tmp(s8);
5193 trim = true;
5194 case 8:
5195 op = aco_opcode::s_load_dwordx8;
5196 break;
5197 default:
5198 unreachable("unimplemented or forbidden load_push_constant.");
5199 }
5200
5201 bld.smem(op, Definition(vec), ptr, index);
5202
5203 if (!aligned) {
5204 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5205 byte_align_scalar(ctx, vec, byte_offset, dst);
5206 return;
5207 }
5208
5209 if (trim) {
5210 emit_split_vector(ctx, vec, 4);
5211 RegClass rc = dst.size() == 3 ? s1 : s2;
5212 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5213 emit_extract_vector(ctx, vec, 0, rc),
5214 emit_extract_vector(ctx, vec, 1, rc),
5215 emit_extract_vector(ctx, vec, 2, rc));
5216
5217 }
5218 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5219 }
5220
5221 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5222 {
5223 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5224
5225 Builder bld(ctx->program, ctx->block);
5226
5227 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5228 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5229 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5230 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5231 if (ctx->options->chip_class >= GFX10) {
5232 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5233 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5234 S_008F0C_RESOURCE_LEVEL(1);
5235 } else {
5236 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5237 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5238 }
5239
5240 unsigned base = nir_intrinsic_base(instr);
5241 unsigned range = nir_intrinsic_range(instr);
5242
5243 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5244 if (base && offset.type() == RegType::sgpr)
5245 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5246 else if (base && offset.type() == RegType::vgpr)
5247 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5248
5249 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5250 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5251 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5252 Operand(desc_type));
5253 unsigned size = instr->dest.ssa.bit_size / 8;
5254 // TODO: get alignment information for subdword constants
5255 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5256 }
5257
5258 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5259 {
5260 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5261 ctx->cf_info.exec_potentially_empty_discard = true;
5262
5263 ctx->program->needs_exact = true;
5264
5265 // TODO: optimize uniform conditions
5266 Builder bld(ctx->program, ctx->block);
5267 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5268 assert(src.regClass() == bld.lm);
5269 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5270 bld.pseudo(aco_opcode::p_discard_if, src);
5271 ctx->block->kind |= block_kind_uses_discard_if;
5272 return;
5273 }
5274
5275 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5276 {
5277 Builder bld(ctx->program, ctx->block);
5278
5279 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5280 ctx->cf_info.exec_potentially_empty_discard = true;
5281
5282 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5283 ctx->cf_info.parent_loop.has_divergent_continue;
5284
5285 if (ctx->block->loop_nest_depth &&
5286 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5287 /* we handle discards the same way as jump instructions */
5288 append_logical_end(ctx->block);
5289
5290 /* in loops, discard behaves like break */
5291 Block *linear_target = ctx->cf_info.parent_loop.exit;
5292 ctx->block->kind |= block_kind_discard;
5293
5294 if (!divergent) {
5295 /* uniform discard - loop ends here */
5296 assert(nir_instr_is_last(&instr->instr));
5297 ctx->block->kind |= block_kind_uniform;
5298 ctx->cf_info.has_branch = true;
5299 bld.branch(aco_opcode::p_branch);
5300 add_linear_edge(ctx->block->index, linear_target);
5301 return;
5302 }
5303
5304 /* we add a break right behind the discard() instructions */
5305 ctx->block->kind |= block_kind_break;
5306 unsigned idx = ctx->block->index;
5307
5308 ctx->cf_info.parent_loop.has_divergent_branch = true;
5309 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5310
5311 /* remove critical edges from linear CFG */
5312 bld.branch(aco_opcode::p_branch);
5313 Block* break_block = ctx->program->create_and_insert_block();
5314 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5315 break_block->kind |= block_kind_uniform;
5316 add_linear_edge(idx, break_block);
5317 add_linear_edge(break_block->index, linear_target);
5318 bld.reset(break_block);
5319 bld.branch(aco_opcode::p_branch);
5320
5321 Block* continue_block = ctx->program->create_and_insert_block();
5322 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5323 add_linear_edge(idx, continue_block);
5324 append_logical_start(continue_block);
5325 ctx->block = continue_block;
5326
5327 return;
5328 }
5329
5330 /* it can currently happen that NIR doesn't remove the unreachable code */
5331 if (!nir_instr_is_last(&instr->instr)) {
5332 ctx->program->needs_exact = true;
5333 /* save exec somewhere temporarily so that it doesn't get
5334 * overwritten before the discard from outer exec masks */
5335 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5336 bld.pseudo(aco_opcode::p_discard_if, cond);
5337 ctx->block->kind |= block_kind_uses_discard_if;
5338 return;
5339 }
5340
5341 /* This condition is incorrect for uniformly branched discards in a loop
5342 * predicated by a divergent condition, but the above code catches that case
5343 * and the discard would end up turning into a discard_if.
5344 * For example:
5345 * if (divergent) {
5346 * while (...) {
5347 * if (uniform) {
5348 * discard;
5349 * }
5350 * }
5351 * }
5352 */
5353 if (!ctx->cf_info.parent_if.is_divergent) {
5354 /* program just ends here */
5355 ctx->block->kind |= block_kind_uniform;
5356 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5357 0 /* enabled mask */, 9 /* dest */,
5358 false /* compressed */, true/* done */, true /* valid mask */);
5359 bld.sopp(aco_opcode::s_endpgm);
5360 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5361 } else {
5362 ctx->block->kind |= block_kind_discard;
5363 /* branch and linear edge is added by visit_if() */
5364 }
5365 }
5366
5367 enum aco_descriptor_type {
5368 ACO_DESC_IMAGE,
5369 ACO_DESC_FMASK,
5370 ACO_DESC_SAMPLER,
5371 ACO_DESC_BUFFER,
5372 ACO_DESC_PLANE_0,
5373 ACO_DESC_PLANE_1,
5374 ACO_DESC_PLANE_2,
5375 };
5376
5377 static bool
5378 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5379 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5380 return false;
5381 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5382 return dim == ac_image_cube ||
5383 dim == ac_image_1darray ||
5384 dim == ac_image_2darray ||
5385 dim == ac_image_2darraymsaa;
5386 }
5387
5388 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5389 enum aco_descriptor_type desc_type,
5390 const nir_tex_instr *tex_instr, bool image, bool write)
5391 {
5392 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5393 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5394 if (it != ctx->tex_desc.end())
5395 return it->second;
5396 */
5397 Temp index = Temp();
5398 bool index_set = false;
5399 unsigned constant_index = 0;
5400 unsigned descriptor_set;
5401 unsigned base_index;
5402 Builder bld(ctx->program, ctx->block);
5403
5404 if (!deref_instr) {
5405 assert(tex_instr && !image);
5406 descriptor_set = 0;
5407 base_index = tex_instr->sampler_index;
5408 } else {
5409 while(deref_instr->deref_type != nir_deref_type_var) {
5410 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5411 if (!array_size)
5412 array_size = 1;
5413
5414 assert(deref_instr->deref_type == nir_deref_type_array);
5415 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5416 if (const_value) {
5417 constant_index += array_size * const_value->u32;
5418 } else {
5419 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5420 if (indirect.type() == RegType::vgpr)
5421 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5422
5423 if (array_size != 1)
5424 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5425
5426 if (!index_set) {
5427 index = indirect;
5428 index_set = true;
5429 } else {
5430 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5431 }
5432 }
5433
5434 deref_instr = nir_src_as_deref(deref_instr->parent);
5435 }
5436 descriptor_set = deref_instr->var->data.descriptor_set;
5437 base_index = deref_instr->var->data.binding;
5438 }
5439
5440 Temp list = load_desc_ptr(ctx, descriptor_set);
5441 list = convert_pointer_to_64_bit(ctx, list);
5442
5443 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5444 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5445 unsigned offset = binding->offset;
5446 unsigned stride = binding->size;
5447 aco_opcode opcode;
5448 RegClass type;
5449
5450 assert(base_index < layout->binding_count);
5451
5452 switch (desc_type) {
5453 case ACO_DESC_IMAGE:
5454 type = s8;
5455 opcode = aco_opcode::s_load_dwordx8;
5456 break;
5457 case ACO_DESC_FMASK:
5458 type = s8;
5459 opcode = aco_opcode::s_load_dwordx8;
5460 offset += 32;
5461 break;
5462 case ACO_DESC_SAMPLER:
5463 type = s4;
5464 opcode = aco_opcode::s_load_dwordx4;
5465 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5466 offset += radv_combined_image_descriptor_sampler_offset(binding);
5467 break;
5468 case ACO_DESC_BUFFER:
5469 type = s4;
5470 opcode = aco_opcode::s_load_dwordx4;
5471 break;
5472 case ACO_DESC_PLANE_0:
5473 case ACO_DESC_PLANE_1:
5474 type = s8;
5475 opcode = aco_opcode::s_load_dwordx8;
5476 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5477 break;
5478 case ACO_DESC_PLANE_2:
5479 type = s4;
5480 opcode = aco_opcode::s_load_dwordx4;
5481 offset += 64;
5482 break;
5483 default:
5484 unreachable("invalid desc_type\n");
5485 }
5486
5487 offset += constant_index * stride;
5488
5489 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5490 (!index_set || binding->immutable_samplers_equal)) {
5491 if (binding->immutable_samplers_equal)
5492 constant_index = 0;
5493
5494 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5495 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5496 Operand(samplers[constant_index * 4 + 0]),
5497 Operand(samplers[constant_index * 4 + 1]),
5498 Operand(samplers[constant_index * 4 + 2]),
5499 Operand(samplers[constant_index * 4 + 3]));
5500 }
5501
5502 Operand off;
5503 if (!index_set) {
5504 off = bld.copy(bld.def(s1), Operand(offset));
5505 } else {
5506 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5507 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5508 }
5509
5510 Temp res = bld.smem(opcode, bld.def(type), list, off);
5511
5512 if (desc_type == ACO_DESC_PLANE_2) {
5513 Temp components[8];
5514 for (unsigned i = 0; i < 8; i++)
5515 components[i] = bld.tmp(s1);
5516 bld.pseudo(aco_opcode::p_split_vector,
5517 Definition(components[0]),
5518 Definition(components[1]),
5519 Definition(components[2]),
5520 Definition(components[3]),
5521 res);
5522
5523 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5524 bld.pseudo(aco_opcode::p_split_vector,
5525 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5526 Definition(components[4]),
5527 Definition(components[5]),
5528 Definition(components[6]),
5529 Definition(components[7]),
5530 desc2);
5531
5532 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5533 components[0], components[1], components[2], components[3],
5534 components[4], components[5], components[6], components[7]);
5535 }
5536
5537 return res;
5538 }
5539
5540 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5541 {
5542 switch (dim) {
5543 case GLSL_SAMPLER_DIM_BUF:
5544 return 1;
5545 case GLSL_SAMPLER_DIM_1D:
5546 return array ? 2 : 1;
5547 case GLSL_SAMPLER_DIM_2D:
5548 return array ? 3 : 2;
5549 case GLSL_SAMPLER_DIM_MS:
5550 return array ? 4 : 3;
5551 case GLSL_SAMPLER_DIM_3D:
5552 case GLSL_SAMPLER_DIM_CUBE:
5553 return 3;
5554 case GLSL_SAMPLER_DIM_RECT:
5555 case GLSL_SAMPLER_DIM_SUBPASS:
5556 return 2;
5557 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5558 return 3;
5559 default:
5560 break;
5561 }
5562 return 0;
5563 }
5564
5565
5566 /* Adjust the sample index according to FMASK.
5567 *
5568 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5569 * which is the identity mapping. Each nibble says which physical sample
5570 * should be fetched to get that sample.
5571 *
5572 * For example, 0x11111100 means there are only 2 samples stored and
5573 * the second sample covers 3/4 of the pixel. When reading samples 0
5574 * and 1, return physical sample 0 (determined by the first two 0s
5575 * in FMASK), otherwise return physical sample 1.
5576 *
5577 * The sample index should be adjusted as follows:
5578 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5579 */
5580 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5581 {
5582 Builder bld(ctx->program, ctx->block);
5583 Temp fmask = bld.tmp(v1);
5584 unsigned dim = ctx->options->chip_class >= GFX10
5585 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5586 : 0;
5587
5588 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5589 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5590 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5591 load->operands[0] = Operand(fmask_desc_ptr);
5592 load->operands[1] = Operand(s4); /* no sampler */
5593 load->operands[2] = Operand(coord);
5594 load->definitions[0] = Definition(fmask);
5595 load->glc = false;
5596 load->dlc = false;
5597 load->dmask = 0x1;
5598 load->unrm = true;
5599 load->da = da;
5600 load->dim = dim;
5601 load->can_reorder = true; /* fmask images shouldn't be modified */
5602 ctx->block->instructions.emplace_back(std::move(load));
5603
5604 Operand sample_index4;
5605 if (sample_index.isConstant()) {
5606 if (sample_index.constantValue() < 16) {
5607 sample_index4 = Operand(sample_index.constantValue() << 2);
5608 } else {
5609 sample_index4 = Operand(0u);
5610 }
5611 } else if (sample_index.regClass() == s1) {
5612 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5613 } else {
5614 assert(sample_index.regClass() == v1);
5615 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5616 }
5617
5618 Temp final_sample;
5619 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5620 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5621 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5622 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5623 else
5624 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5625
5626 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5627 * resource descriptor is 0 (invalid),
5628 */
5629 Temp compare = bld.tmp(bld.lm);
5630 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5631 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5632
5633 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5634
5635 /* Replace the MSAA sample index. */
5636 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5637 }
5638
5639 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5640 {
5641
5642 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5643 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5644 bool is_array = glsl_sampler_type_is_array(type);
5645 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5646 assert(!add_frag_pos && "Input attachments should be lowered.");
5647 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5648 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5649 int count = image_type_to_components_count(dim, is_array);
5650 std::vector<Temp> coords(count);
5651 Builder bld(ctx->program, ctx->block);
5652
5653 if (is_ms) {
5654 count--;
5655 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5656 /* get sample index */
5657 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5658 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5659 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5660 std::vector<Temp> fmask_load_address;
5661 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5662 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5663
5664 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5665 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5666 } else {
5667 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5668 }
5669 }
5670
5671 if (gfx9_1d) {
5672 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5673 coords.resize(coords.size() + 1);
5674 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5675 if (is_array)
5676 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5677 } else {
5678 for (int i = 0; i < count; i++)
5679 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5680 }
5681
5682 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5683 instr->intrinsic == nir_intrinsic_image_deref_store) {
5684 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5685 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5686
5687 if (!level_zero)
5688 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5689 }
5690
5691 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5692 for (unsigned i = 0; i < coords.size(); i++)
5693 vec->operands[i] = Operand(coords[i]);
5694 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5695 vec->definitions[0] = Definition(res);
5696 ctx->block->instructions.emplace_back(std::move(vec));
5697 return res;
5698 }
5699
5700
5701 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5702 {
5703 Builder bld(ctx->program, ctx->block);
5704 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5705 const struct glsl_type *type = glsl_without_array(var->type);
5706 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5707 bool is_array = glsl_sampler_type_is_array(type);
5708 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5709
5710 if (dim == GLSL_SAMPLER_DIM_BUF) {
5711 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5712 unsigned num_channels = util_last_bit(mask);
5713 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5714 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5715
5716 aco_opcode opcode;
5717 switch (num_channels) {
5718 case 1:
5719 opcode = aco_opcode::buffer_load_format_x;
5720 break;
5721 case 2:
5722 opcode = aco_opcode::buffer_load_format_xy;
5723 break;
5724 case 3:
5725 opcode = aco_opcode::buffer_load_format_xyz;
5726 break;
5727 case 4:
5728 opcode = aco_opcode::buffer_load_format_xyzw;
5729 break;
5730 default:
5731 unreachable(">4 channel buffer image load");
5732 }
5733 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5734 load->operands[0] = Operand(rsrc);
5735 load->operands[1] = Operand(vindex);
5736 load->operands[2] = Operand((uint32_t) 0);
5737 Temp tmp;
5738 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5739 tmp = dst;
5740 else
5741 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5742 load->definitions[0] = Definition(tmp);
5743 load->idxen = true;
5744 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5745 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5746 load->barrier = barrier_image;
5747 ctx->block->instructions.emplace_back(std::move(load));
5748
5749 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5750 return;
5751 }
5752
5753 Temp coords = get_image_coords(ctx, instr, type);
5754 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5755
5756 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5757 unsigned num_components = util_bitcount(dmask);
5758 Temp tmp;
5759 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5760 tmp = dst;
5761 else
5762 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5763
5764 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5765 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5766
5767 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5768 load->operands[0] = Operand(resource);
5769 load->operands[1] = Operand(s4); /* no sampler */
5770 load->operands[2] = Operand(coords);
5771 load->definitions[0] = Definition(tmp);
5772 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5773 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5774 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5775 load->dmask = dmask;
5776 load->unrm = true;
5777 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5778 load->barrier = barrier_image;
5779 ctx->block->instructions.emplace_back(std::move(load));
5780
5781 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5782 return;
5783 }
5784
5785 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5786 {
5787 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5788 const struct glsl_type *type = glsl_without_array(var->type);
5789 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5790 bool is_array = glsl_sampler_type_is_array(type);
5791 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5792
5793 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5794
5795 if (dim == GLSL_SAMPLER_DIM_BUF) {
5796 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5797 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5798 aco_opcode opcode;
5799 switch (data.size()) {
5800 case 1:
5801 opcode = aco_opcode::buffer_store_format_x;
5802 break;
5803 case 2:
5804 opcode = aco_opcode::buffer_store_format_xy;
5805 break;
5806 case 3:
5807 opcode = aco_opcode::buffer_store_format_xyz;
5808 break;
5809 case 4:
5810 opcode = aco_opcode::buffer_store_format_xyzw;
5811 break;
5812 default:
5813 unreachable(">4 channel buffer image store");
5814 }
5815 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5816 store->operands[0] = Operand(rsrc);
5817 store->operands[1] = Operand(vindex);
5818 store->operands[2] = Operand((uint32_t) 0);
5819 store->operands[3] = Operand(data);
5820 store->idxen = true;
5821 store->glc = glc;
5822 store->dlc = false;
5823 store->disable_wqm = true;
5824 store->barrier = barrier_image;
5825 ctx->program->needs_exact = true;
5826 ctx->block->instructions.emplace_back(std::move(store));
5827 return;
5828 }
5829
5830 assert(data.type() == RegType::vgpr);
5831 Temp coords = get_image_coords(ctx, instr, type);
5832 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5833
5834 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5835 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5836
5837 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5838 store->operands[0] = Operand(resource);
5839 store->operands[1] = Operand(data);
5840 store->operands[2] = Operand(coords);
5841 store->glc = glc;
5842 store->dlc = false;
5843 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5844 store->dmask = (1 << data.size()) - 1;
5845 store->unrm = true;
5846 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5847 store->disable_wqm = true;
5848 store->barrier = barrier_image;
5849 ctx->program->needs_exact = true;
5850 ctx->block->instructions.emplace_back(std::move(store));
5851 return;
5852 }
5853
5854 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5855 {
5856 /* return the previous value if dest is ever used */
5857 bool return_previous = false;
5858 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5859 return_previous = true;
5860 break;
5861 }
5862 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5863 return_previous = true;
5864 break;
5865 }
5866
5867 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5868 const struct glsl_type *type = glsl_without_array(var->type);
5869 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5870 bool is_array = glsl_sampler_type_is_array(type);
5871 Builder bld(ctx->program, ctx->block);
5872
5873 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5874 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5875
5876 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5877 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5878
5879 aco_opcode buf_op, image_op;
5880 switch (instr->intrinsic) {
5881 case nir_intrinsic_image_deref_atomic_add:
5882 buf_op = aco_opcode::buffer_atomic_add;
5883 image_op = aco_opcode::image_atomic_add;
5884 break;
5885 case nir_intrinsic_image_deref_atomic_umin:
5886 buf_op = aco_opcode::buffer_atomic_umin;
5887 image_op = aco_opcode::image_atomic_umin;
5888 break;
5889 case nir_intrinsic_image_deref_atomic_imin:
5890 buf_op = aco_opcode::buffer_atomic_smin;
5891 image_op = aco_opcode::image_atomic_smin;
5892 break;
5893 case nir_intrinsic_image_deref_atomic_umax:
5894 buf_op = aco_opcode::buffer_atomic_umax;
5895 image_op = aco_opcode::image_atomic_umax;
5896 break;
5897 case nir_intrinsic_image_deref_atomic_imax:
5898 buf_op = aco_opcode::buffer_atomic_smax;
5899 image_op = aco_opcode::image_atomic_smax;
5900 break;
5901 case nir_intrinsic_image_deref_atomic_and:
5902 buf_op = aco_opcode::buffer_atomic_and;
5903 image_op = aco_opcode::image_atomic_and;
5904 break;
5905 case nir_intrinsic_image_deref_atomic_or:
5906 buf_op = aco_opcode::buffer_atomic_or;
5907 image_op = aco_opcode::image_atomic_or;
5908 break;
5909 case nir_intrinsic_image_deref_atomic_xor:
5910 buf_op = aco_opcode::buffer_atomic_xor;
5911 image_op = aco_opcode::image_atomic_xor;
5912 break;
5913 case nir_intrinsic_image_deref_atomic_exchange:
5914 buf_op = aco_opcode::buffer_atomic_swap;
5915 image_op = aco_opcode::image_atomic_swap;
5916 break;
5917 case nir_intrinsic_image_deref_atomic_comp_swap:
5918 buf_op = aco_opcode::buffer_atomic_cmpswap;
5919 image_op = aco_opcode::image_atomic_cmpswap;
5920 break;
5921 default:
5922 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5923 }
5924
5925 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5926
5927 if (dim == GLSL_SAMPLER_DIM_BUF) {
5928 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5929 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5930 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5931 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5932 mubuf->operands[0] = Operand(resource);
5933 mubuf->operands[1] = Operand(vindex);
5934 mubuf->operands[2] = Operand((uint32_t)0);
5935 mubuf->operands[3] = Operand(data);
5936 if (return_previous)
5937 mubuf->definitions[0] = Definition(dst);
5938 mubuf->offset = 0;
5939 mubuf->idxen = true;
5940 mubuf->glc = return_previous;
5941 mubuf->dlc = false; /* Not needed for atomics */
5942 mubuf->disable_wqm = true;
5943 mubuf->barrier = barrier_image;
5944 ctx->program->needs_exact = true;
5945 ctx->block->instructions.emplace_back(std::move(mubuf));
5946 return;
5947 }
5948
5949 Temp coords = get_image_coords(ctx, instr, type);
5950 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5951 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5952 mimg->operands[0] = Operand(resource);
5953 mimg->operands[1] = Operand(data);
5954 mimg->operands[2] = Operand(coords);
5955 if (return_previous)
5956 mimg->definitions[0] = Definition(dst);
5957 mimg->glc = return_previous;
5958 mimg->dlc = false; /* Not needed for atomics */
5959 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5960 mimg->dmask = (1 << data.size()) - 1;
5961 mimg->unrm = true;
5962 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5963 mimg->disable_wqm = true;
5964 mimg->barrier = barrier_image;
5965 ctx->program->needs_exact = true;
5966 ctx->block->instructions.emplace_back(std::move(mimg));
5967 return;
5968 }
5969
5970 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
5971 {
5972 if (in_elements && ctx->options->chip_class == GFX8) {
5973 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5974 Builder bld(ctx->program, ctx->block);
5975
5976 Temp size = emit_extract_vector(ctx, desc, 2, s1);
5977
5978 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
5979 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
5980
5981 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
5982 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
5983
5984 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
5985 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
5986
5987 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
5988 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
5989 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
5990 if (dst.type() == RegType::vgpr)
5991 bld.copy(Definition(dst), shr_dst);
5992
5993 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5994 } else {
5995 emit_extract_vector(ctx, desc, 2, dst);
5996 }
5997 }
5998
5999 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6000 {
6001 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6002 const struct glsl_type *type = glsl_without_array(var->type);
6003 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6004 bool is_array = glsl_sampler_type_is_array(type);
6005 Builder bld(ctx->program, ctx->block);
6006
6007 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6008 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6009 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6010 }
6011
6012 /* LOD */
6013 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6014
6015 /* Resource */
6016 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6017
6018 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6019
6020 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6021 mimg->operands[0] = Operand(resource);
6022 mimg->operands[1] = Operand(s4); /* no sampler */
6023 mimg->operands[2] = Operand(lod);
6024 uint8_t& dmask = mimg->dmask;
6025 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6026 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6027 mimg->da = glsl_sampler_type_is_array(type);
6028 mimg->can_reorder = true;
6029 Definition& def = mimg->definitions[0];
6030 ctx->block->instructions.emplace_back(std::move(mimg));
6031
6032 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6033 glsl_sampler_type_is_array(type)) {
6034
6035 assert(instr->dest.ssa.num_components == 3);
6036 Temp tmp = {ctx->program->allocateId(), v3};
6037 def = Definition(tmp);
6038 emit_split_vector(ctx, tmp, 3);
6039
6040 /* divide 3rd value by 6 by multiplying with magic number */
6041 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6042 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6043
6044 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6045 emit_extract_vector(ctx, tmp, 0, v1),
6046 emit_extract_vector(ctx, tmp, 1, v1),
6047 by_6);
6048
6049 } else if (ctx->options->chip_class == GFX9 &&
6050 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6051 glsl_sampler_type_is_array(type)) {
6052 assert(instr->dest.ssa.num_components == 2);
6053 def = Definition(dst);
6054 dmask = 0x5;
6055 } else {
6056 def = Definition(dst);
6057 }
6058
6059 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6060 }
6061
6062 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6063 {
6064 Builder bld(ctx->program, ctx->block);
6065 unsigned num_components = instr->num_components;
6066
6067 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6068 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6069 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6070
6071 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6072 unsigned size = instr->dest.ssa.bit_size / 8;
6073 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6074 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false);
6075 }
6076
6077 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6078 {
6079 Builder bld(ctx->program, ctx->block);
6080 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6081 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6082 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6083 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6084
6085 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6086 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6087
6088 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
6089 ctx->options->chip_class >= GFX8 &&
6090 elem_size_bytes >= 4;
6091 if (smem)
6092 offset = bld.as_uniform(offset);
6093 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6094
6095 unsigned write_count = 0;
6096 Temp write_datas[32];
6097 unsigned offsets[32];
6098 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6099 data, writemask, 16, &write_count, write_datas, offsets);
6100
6101 for (unsigned i = 0; i < write_count; i++) {
6102 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6103 if (smem && ctx->stage == fragment_fs)
6104 op = aco_opcode::p_fs_buffer_store_smem;
6105
6106 if (smem) {
6107 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6108 store->operands[0] = Operand(rsrc);
6109 if (offsets[i]) {
6110 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6111 offset, Operand(offsets[i]));
6112 store->operands[1] = Operand(off);
6113 } else {
6114 store->operands[1] = Operand(offset);
6115 }
6116 if (op != aco_opcode::p_fs_buffer_store_smem)
6117 store->operands[1].setFixed(m0);
6118 store->operands[2] = Operand(write_datas[i]);
6119 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6120 store->dlc = false;
6121 store->disable_wqm = true;
6122 store->barrier = barrier_buffer;
6123 ctx->block->instructions.emplace_back(std::move(store));
6124 ctx->program->wb_smem_l1_on_end = true;
6125 if (op == aco_opcode::p_fs_buffer_store_smem) {
6126 ctx->block->kind |= block_kind_needs_lowering;
6127 ctx->program->needs_exact = true;
6128 }
6129 } else {
6130 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6131 store->operands[0] = Operand(rsrc);
6132 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6133 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6134 store->operands[3] = Operand(write_datas[i]);
6135 store->offset = offsets[i];
6136 store->offen = (offset.type() == RegType::vgpr);
6137 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6138 store->dlc = false;
6139 store->disable_wqm = true;
6140 store->barrier = barrier_buffer;
6141 ctx->program->needs_exact = true;
6142 ctx->block->instructions.emplace_back(std::move(store));
6143 }
6144 }
6145 }
6146
6147 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6148 {
6149 /* return the previous value if dest is ever used */
6150 bool return_previous = false;
6151 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6152 return_previous = true;
6153 break;
6154 }
6155 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6156 return_previous = true;
6157 break;
6158 }
6159
6160 Builder bld(ctx->program, ctx->block);
6161 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6162
6163 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6164 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6165 get_ssa_temp(ctx, instr->src[3].ssa), data);
6166
6167 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6168 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6169 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6170
6171 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6172
6173 aco_opcode op32, op64;
6174 switch (instr->intrinsic) {
6175 case nir_intrinsic_ssbo_atomic_add:
6176 op32 = aco_opcode::buffer_atomic_add;
6177 op64 = aco_opcode::buffer_atomic_add_x2;
6178 break;
6179 case nir_intrinsic_ssbo_atomic_imin:
6180 op32 = aco_opcode::buffer_atomic_smin;
6181 op64 = aco_opcode::buffer_atomic_smin_x2;
6182 break;
6183 case nir_intrinsic_ssbo_atomic_umin:
6184 op32 = aco_opcode::buffer_atomic_umin;
6185 op64 = aco_opcode::buffer_atomic_umin_x2;
6186 break;
6187 case nir_intrinsic_ssbo_atomic_imax:
6188 op32 = aco_opcode::buffer_atomic_smax;
6189 op64 = aco_opcode::buffer_atomic_smax_x2;
6190 break;
6191 case nir_intrinsic_ssbo_atomic_umax:
6192 op32 = aco_opcode::buffer_atomic_umax;
6193 op64 = aco_opcode::buffer_atomic_umax_x2;
6194 break;
6195 case nir_intrinsic_ssbo_atomic_and:
6196 op32 = aco_opcode::buffer_atomic_and;
6197 op64 = aco_opcode::buffer_atomic_and_x2;
6198 break;
6199 case nir_intrinsic_ssbo_atomic_or:
6200 op32 = aco_opcode::buffer_atomic_or;
6201 op64 = aco_opcode::buffer_atomic_or_x2;
6202 break;
6203 case nir_intrinsic_ssbo_atomic_xor:
6204 op32 = aco_opcode::buffer_atomic_xor;
6205 op64 = aco_opcode::buffer_atomic_xor_x2;
6206 break;
6207 case nir_intrinsic_ssbo_atomic_exchange:
6208 op32 = aco_opcode::buffer_atomic_swap;
6209 op64 = aco_opcode::buffer_atomic_swap_x2;
6210 break;
6211 case nir_intrinsic_ssbo_atomic_comp_swap:
6212 op32 = aco_opcode::buffer_atomic_cmpswap;
6213 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6214 break;
6215 default:
6216 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6217 }
6218 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6219 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6220 mubuf->operands[0] = Operand(rsrc);
6221 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6222 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6223 mubuf->operands[3] = Operand(data);
6224 if (return_previous)
6225 mubuf->definitions[0] = Definition(dst);
6226 mubuf->offset = 0;
6227 mubuf->offen = (offset.type() == RegType::vgpr);
6228 mubuf->glc = return_previous;
6229 mubuf->dlc = false; /* Not needed for atomics */
6230 mubuf->disable_wqm = true;
6231 mubuf->barrier = barrier_buffer;
6232 ctx->program->needs_exact = true;
6233 ctx->block->instructions.emplace_back(std::move(mubuf));
6234 }
6235
6236 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6237
6238 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6239 Builder bld(ctx->program, ctx->block);
6240 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6241 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6242 }
6243
6244 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6245 {
6246 Builder bld(ctx->program, ctx->block);
6247 unsigned num_components = instr->num_components;
6248 unsigned component_size = instr->dest.ssa.bit_size / 8;
6249
6250 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6251 get_ssa_temp(ctx, &instr->dest.ssa),
6252 num_components, component_size};
6253 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6254 info.align_mul = nir_intrinsic_align_mul(instr);
6255 info.align_offset = nir_intrinsic_align_offset(instr);
6256 info.barrier = barrier_buffer;
6257 info.can_reorder = false;
6258 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6259 * it's safe to use SMEM */
6260 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6261 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6262 emit_global_load(ctx, bld, &info);
6263 } else {
6264 info.offset = Operand(bld.as_uniform(info.offset));
6265 emit_smem_load(ctx, bld, &info);
6266 }
6267 }
6268
6269 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6270 {
6271 Builder bld(ctx->program, ctx->block);
6272 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6273 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6274
6275 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6276 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6277 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6278
6279 if (ctx->options->chip_class >= GFX7)
6280 addr = as_vgpr(ctx, addr);
6281
6282 unsigned write_count = 0;
6283 Temp write_datas[32];
6284 unsigned offsets[32];
6285 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6286 16, &write_count, write_datas, offsets);
6287
6288 for (unsigned i = 0; i < write_count; i++) {
6289 if (ctx->options->chip_class >= GFX7) {
6290 unsigned offset = offsets[i];
6291 Temp store_addr = addr;
6292 if (offset > 0 && ctx->options->chip_class < GFX9) {
6293 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6294 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6295 Temp carry = bld.tmp(bld.lm);
6296 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6297
6298 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6299 Operand(offset), addr0);
6300 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6301 Operand(0u), addr1,
6302 carry).def(1).setHint(vcc);
6303
6304 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6305
6306 offset = 0;
6307 }
6308
6309 bool global = ctx->options->chip_class >= GFX9;
6310 aco_opcode op;
6311 switch (write_datas[i].bytes()) {
6312 case 1:
6313 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6314 break;
6315 case 2:
6316 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6317 break;
6318 case 4:
6319 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6320 break;
6321 case 8:
6322 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6323 break;
6324 case 12:
6325 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6326 break;
6327 case 16:
6328 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6329 break;
6330 default:
6331 unreachable("store_global not implemented for this size.");
6332 }
6333
6334 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6335 flat->operands[0] = Operand(store_addr);
6336 flat->operands[1] = Operand(s1);
6337 flat->operands[2] = Operand(write_datas[i]);
6338 flat->glc = glc;
6339 flat->dlc = false;
6340 flat->offset = offset;
6341 flat->disable_wqm = true;
6342 flat->barrier = barrier_buffer;
6343 ctx->program->needs_exact = true;
6344 ctx->block->instructions.emplace_back(std::move(flat));
6345 } else {
6346 assert(ctx->options->chip_class == GFX6);
6347
6348 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6349
6350 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6351
6352 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6353 mubuf->operands[0] = Operand(rsrc);
6354 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6355 mubuf->operands[2] = Operand(0u);
6356 mubuf->operands[3] = Operand(write_datas[i]);
6357 mubuf->glc = glc;
6358 mubuf->dlc = false;
6359 mubuf->offset = offsets[i];
6360 mubuf->addr64 = addr.type() == RegType::vgpr;
6361 mubuf->disable_wqm = true;
6362 mubuf->barrier = barrier_buffer;
6363 ctx->program->needs_exact = true;
6364 ctx->block->instructions.emplace_back(std::move(mubuf));
6365 }
6366 }
6367 }
6368
6369 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6370 {
6371 /* return the previous value if dest is ever used */
6372 bool return_previous = false;
6373 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6374 return_previous = true;
6375 break;
6376 }
6377 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6378 return_previous = true;
6379 break;
6380 }
6381
6382 Builder bld(ctx->program, ctx->block);
6383 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6384 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6385
6386 if (ctx->options->chip_class >= GFX7)
6387 addr = as_vgpr(ctx, addr);
6388
6389 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6390 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6391 get_ssa_temp(ctx, instr->src[2].ssa), data);
6392
6393 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6394
6395 aco_opcode op32, op64;
6396
6397 if (ctx->options->chip_class >= GFX7) {
6398 bool global = ctx->options->chip_class >= GFX9;
6399 switch (instr->intrinsic) {
6400 case nir_intrinsic_global_atomic_add:
6401 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6402 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6403 break;
6404 case nir_intrinsic_global_atomic_imin:
6405 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6406 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6407 break;
6408 case nir_intrinsic_global_atomic_umin:
6409 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6410 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6411 break;
6412 case nir_intrinsic_global_atomic_imax:
6413 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6414 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6415 break;
6416 case nir_intrinsic_global_atomic_umax:
6417 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6418 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6419 break;
6420 case nir_intrinsic_global_atomic_and:
6421 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6422 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6423 break;
6424 case nir_intrinsic_global_atomic_or:
6425 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6426 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6427 break;
6428 case nir_intrinsic_global_atomic_xor:
6429 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6430 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6431 break;
6432 case nir_intrinsic_global_atomic_exchange:
6433 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6434 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6435 break;
6436 case nir_intrinsic_global_atomic_comp_swap:
6437 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6438 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6439 break;
6440 default:
6441 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6442 }
6443
6444 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6445 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6446 flat->operands[0] = Operand(addr);
6447 flat->operands[1] = Operand(s1);
6448 flat->operands[2] = Operand(data);
6449 if (return_previous)
6450 flat->definitions[0] = Definition(dst);
6451 flat->glc = return_previous;
6452 flat->dlc = false; /* Not needed for atomics */
6453 flat->offset = 0;
6454 flat->disable_wqm = true;
6455 flat->barrier = barrier_buffer;
6456 ctx->program->needs_exact = true;
6457 ctx->block->instructions.emplace_back(std::move(flat));
6458 } else {
6459 assert(ctx->options->chip_class == GFX6);
6460
6461 switch (instr->intrinsic) {
6462 case nir_intrinsic_global_atomic_add:
6463 op32 = aco_opcode::buffer_atomic_add;
6464 op64 = aco_opcode::buffer_atomic_add_x2;
6465 break;
6466 case nir_intrinsic_global_atomic_imin:
6467 op32 = aco_opcode::buffer_atomic_smin;
6468 op64 = aco_opcode::buffer_atomic_smin_x2;
6469 break;
6470 case nir_intrinsic_global_atomic_umin:
6471 op32 = aco_opcode::buffer_atomic_umin;
6472 op64 = aco_opcode::buffer_atomic_umin_x2;
6473 break;
6474 case nir_intrinsic_global_atomic_imax:
6475 op32 = aco_opcode::buffer_atomic_smax;
6476 op64 = aco_opcode::buffer_atomic_smax_x2;
6477 break;
6478 case nir_intrinsic_global_atomic_umax:
6479 op32 = aco_opcode::buffer_atomic_umax;
6480 op64 = aco_opcode::buffer_atomic_umax_x2;
6481 break;
6482 case nir_intrinsic_global_atomic_and:
6483 op32 = aco_opcode::buffer_atomic_and;
6484 op64 = aco_opcode::buffer_atomic_and_x2;
6485 break;
6486 case nir_intrinsic_global_atomic_or:
6487 op32 = aco_opcode::buffer_atomic_or;
6488 op64 = aco_opcode::buffer_atomic_or_x2;
6489 break;
6490 case nir_intrinsic_global_atomic_xor:
6491 op32 = aco_opcode::buffer_atomic_xor;
6492 op64 = aco_opcode::buffer_atomic_xor_x2;
6493 break;
6494 case nir_intrinsic_global_atomic_exchange:
6495 op32 = aco_opcode::buffer_atomic_swap;
6496 op64 = aco_opcode::buffer_atomic_swap_x2;
6497 break;
6498 case nir_intrinsic_global_atomic_comp_swap:
6499 op32 = aco_opcode::buffer_atomic_cmpswap;
6500 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6501 break;
6502 default:
6503 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6504 }
6505
6506 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6507
6508 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6509
6510 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6511 mubuf->operands[0] = Operand(rsrc);
6512 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6513 mubuf->operands[2] = Operand(0u);
6514 mubuf->operands[3] = Operand(data);
6515 if (return_previous)
6516 mubuf->definitions[0] = Definition(dst);
6517 mubuf->glc = return_previous;
6518 mubuf->dlc = false;
6519 mubuf->offset = 0;
6520 mubuf->addr64 = addr.type() == RegType::vgpr;
6521 mubuf->disable_wqm = true;
6522 mubuf->barrier = barrier_buffer;
6523 ctx->program->needs_exact = true;
6524 ctx->block->instructions.emplace_back(std::move(mubuf));
6525 }
6526 }
6527
6528 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6529 Builder bld(ctx->program, ctx->block);
6530 switch(instr->intrinsic) {
6531 case nir_intrinsic_group_memory_barrier:
6532 case nir_intrinsic_memory_barrier:
6533 bld.barrier(aco_opcode::p_memory_barrier_common);
6534 break;
6535 case nir_intrinsic_memory_barrier_buffer:
6536 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6537 break;
6538 case nir_intrinsic_memory_barrier_image:
6539 bld.barrier(aco_opcode::p_memory_barrier_image);
6540 break;
6541 case nir_intrinsic_memory_barrier_tcs_patch:
6542 case nir_intrinsic_memory_barrier_shared:
6543 bld.barrier(aco_opcode::p_memory_barrier_shared);
6544 break;
6545 default:
6546 unreachable("Unimplemented memory barrier intrinsic");
6547 break;
6548 }
6549 }
6550
6551 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6552 {
6553 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6554 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6555 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6556 Builder bld(ctx->program, ctx->block);
6557
6558 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6559 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6560 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6561 }
6562
6563 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6564 {
6565 unsigned writemask = nir_intrinsic_write_mask(instr);
6566 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6567 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6568 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6569
6570 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6571 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6572 }
6573
6574 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6575 {
6576 unsigned offset = nir_intrinsic_base(instr);
6577 Builder bld(ctx->program, ctx->block);
6578 Operand m = load_lds_size_m0(bld);
6579 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6580 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6581
6582 unsigned num_operands = 3;
6583 aco_opcode op32, op64, op32_rtn, op64_rtn;
6584 switch(instr->intrinsic) {
6585 case nir_intrinsic_shared_atomic_add:
6586 op32 = aco_opcode::ds_add_u32;
6587 op64 = aco_opcode::ds_add_u64;
6588 op32_rtn = aco_opcode::ds_add_rtn_u32;
6589 op64_rtn = aco_opcode::ds_add_rtn_u64;
6590 break;
6591 case nir_intrinsic_shared_atomic_imin:
6592 op32 = aco_opcode::ds_min_i32;
6593 op64 = aco_opcode::ds_min_i64;
6594 op32_rtn = aco_opcode::ds_min_rtn_i32;
6595 op64_rtn = aco_opcode::ds_min_rtn_i64;
6596 break;
6597 case nir_intrinsic_shared_atomic_umin:
6598 op32 = aco_opcode::ds_min_u32;
6599 op64 = aco_opcode::ds_min_u64;
6600 op32_rtn = aco_opcode::ds_min_rtn_u32;
6601 op64_rtn = aco_opcode::ds_min_rtn_u64;
6602 break;
6603 case nir_intrinsic_shared_atomic_imax:
6604 op32 = aco_opcode::ds_max_i32;
6605 op64 = aco_opcode::ds_max_i64;
6606 op32_rtn = aco_opcode::ds_max_rtn_i32;
6607 op64_rtn = aco_opcode::ds_max_rtn_i64;
6608 break;
6609 case nir_intrinsic_shared_atomic_umax:
6610 op32 = aco_opcode::ds_max_u32;
6611 op64 = aco_opcode::ds_max_u64;
6612 op32_rtn = aco_opcode::ds_max_rtn_u32;
6613 op64_rtn = aco_opcode::ds_max_rtn_u64;
6614 break;
6615 case nir_intrinsic_shared_atomic_and:
6616 op32 = aco_opcode::ds_and_b32;
6617 op64 = aco_opcode::ds_and_b64;
6618 op32_rtn = aco_opcode::ds_and_rtn_b32;
6619 op64_rtn = aco_opcode::ds_and_rtn_b64;
6620 break;
6621 case nir_intrinsic_shared_atomic_or:
6622 op32 = aco_opcode::ds_or_b32;
6623 op64 = aco_opcode::ds_or_b64;
6624 op32_rtn = aco_opcode::ds_or_rtn_b32;
6625 op64_rtn = aco_opcode::ds_or_rtn_b64;
6626 break;
6627 case nir_intrinsic_shared_atomic_xor:
6628 op32 = aco_opcode::ds_xor_b32;
6629 op64 = aco_opcode::ds_xor_b64;
6630 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6631 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6632 break;
6633 case nir_intrinsic_shared_atomic_exchange:
6634 op32 = aco_opcode::ds_write_b32;
6635 op64 = aco_opcode::ds_write_b64;
6636 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6637 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6638 break;
6639 case nir_intrinsic_shared_atomic_comp_swap:
6640 op32 = aco_opcode::ds_cmpst_b32;
6641 op64 = aco_opcode::ds_cmpst_b64;
6642 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6643 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6644 num_operands = 4;
6645 break;
6646 default:
6647 unreachable("Unhandled shared atomic intrinsic");
6648 }
6649
6650 /* return the previous value if dest is ever used */
6651 bool return_previous = false;
6652 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6653 return_previous = true;
6654 break;
6655 }
6656 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6657 return_previous = true;
6658 break;
6659 }
6660
6661 aco_opcode op;
6662 if (data.size() == 1) {
6663 assert(instr->dest.ssa.bit_size == 32);
6664 op = return_previous ? op32_rtn : op32;
6665 } else {
6666 assert(instr->dest.ssa.bit_size == 64);
6667 op = return_previous ? op64_rtn : op64;
6668 }
6669
6670 if (offset > 65535) {
6671 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6672 offset = 0;
6673 }
6674
6675 aco_ptr<DS_instruction> ds;
6676 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6677 ds->operands[0] = Operand(address);
6678 ds->operands[1] = Operand(data);
6679 if (num_operands == 4)
6680 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6681 ds->operands[num_operands - 1] = m;
6682 ds->offset0 = offset;
6683 if (return_previous)
6684 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6685 ctx->block->instructions.emplace_back(std::move(ds));
6686 }
6687
6688 Temp get_scratch_resource(isel_context *ctx)
6689 {
6690 Builder bld(ctx->program, ctx->block);
6691 Temp scratch_addr = ctx->program->private_segment_buffer;
6692 if (ctx->stage != compute_cs)
6693 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6694
6695 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6696 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6697
6698 if (ctx->program->chip_class >= GFX10) {
6699 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6700 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6701 S_008F0C_RESOURCE_LEVEL(1);
6702 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6703 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6704 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6705 }
6706
6707 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6708 if (ctx->program->chip_class <= GFX8)
6709 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6710
6711 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6712 }
6713
6714 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6715 Builder bld(ctx->program, ctx->block);
6716 Temp rsrc = get_scratch_resource(ctx);
6717 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6718 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6719
6720 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6721 instr->dest.ssa.bit_size / 8u, rsrc};
6722 info.align_mul = nir_intrinsic_align_mul(instr);
6723 info.align_offset = nir_intrinsic_align_offset(instr);
6724 info.swizzle_component_size = 16;
6725 info.can_reorder = false;
6726 info.soffset = ctx->program->scratch_offset;
6727 emit_mubuf_load(ctx, bld, &info);
6728 }
6729
6730 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6731 Builder bld(ctx->program, ctx->block);
6732 Temp rsrc = get_scratch_resource(ctx);
6733 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6734 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6735
6736 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6737 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6738
6739 unsigned write_count = 0;
6740 Temp write_datas[32];
6741 unsigned offsets[32];
6742 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6743 16, &write_count, write_datas, offsets);
6744
6745 for (unsigned i = 0; i < write_count; i++) {
6746 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6747 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true);
6748 }
6749 }
6750
6751 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6752 uint8_t log2_ps_iter_samples;
6753 if (ctx->program->info->ps.force_persample) {
6754 log2_ps_iter_samples =
6755 util_logbase2(ctx->options->key.fs.num_samples);
6756 } else {
6757 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6758 }
6759
6760 /* The bit pattern matches that used by fixed function fragment
6761 * processing. */
6762 static const unsigned ps_iter_masks[] = {
6763 0xffff, /* not used */
6764 0x5555,
6765 0x1111,
6766 0x0101,
6767 0x0001,
6768 };
6769 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6770
6771 Builder bld(ctx->program, ctx->block);
6772
6773 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6774 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6775 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6776 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6777 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6778 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6779 }
6780
6781 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6782 Builder bld(ctx->program, ctx->block);
6783
6784 unsigned stream = nir_intrinsic_stream_id(instr);
6785 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6786 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6787 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6788
6789 /* get GSVS ring */
6790 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6791
6792 unsigned num_components =
6793 ctx->program->info->gs.num_stream_output_components[stream];
6794 assert(num_components);
6795
6796 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6797 unsigned stream_offset = 0;
6798 for (unsigned i = 0; i < stream; i++) {
6799 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6800 stream_offset += prev_stride * ctx->program->wave_size;
6801 }
6802
6803 /* Limit on the stride field for <= GFX7. */
6804 assert(stride < (1 << 14));
6805
6806 Temp gsvs_dwords[4];
6807 for (unsigned i = 0; i < 4; i++)
6808 gsvs_dwords[i] = bld.tmp(s1);
6809 bld.pseudo(aco_opcode::p_split_vector,
6810 Definition(gsvs_dwords[0]),
6811 Definition(gsvs_dwords[1]),
6812 Definition(gsvs_dwords[2]),
6813 Definition(gsvs_dwords[3]),
6814 gsvs_ring);
6815
6816 if (stream_offset) {
6817 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6818
6819 Temp carry = bld.tmp(s1);
6820 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6821 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));
6822 }
6823
6824 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)));
6825 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6826
6827 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6828 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6829
6830 unsigned offset = 0;
6831 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6832 if (ctx->program->info->gs.output_streams[i] != stream)
6833 continue;
6834
6835 for (unsigned j = 0; j < 4; j++) {
6836 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6837 continue;
6838
6839 if (ctx->outputs.mask[i] & (1 << j)) {
6840 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6841 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6842 if (const_offset >= 4096u) {
6843 if (vaddr_offset.isUndefined())
6844 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6845 else
6846 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6847 const_offset %= 4096u;
6848 }
6849
6850 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6851 mtbuf->operands[0] = Operand(gsvs_ring);
6852 mtbuf->operands[1] = vaddr_offset;
6853 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6854 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6855 mtbuf->offen = !vaddr_offset.isUndefined();
6856 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6857 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6858 mtbuf->offset = const_offset;
6859 mtbuf->glc = true;
6860 mtbuf->slc = true;
6861 mtbuf->barrier = barrier_gs_data;
6862 mtbuf->can_reorder = true;
6863 bld.insert(std::move(mtbuf));
6864 }
6865
6866 offset += ctx->shader->info.gs.vertices_out;
6867 }
6868
6869 /* outputs for the next vertex are undefined and keeping them around can
6870 * create invalid IR with control flow */
6871 ctx->outputs.mask[i] = 0;
6872 }
6873
6874 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6875 }
6876
6877 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6878 {
6879 Builder bld(ctx->program, ctx->block);
6880
6881 if (cluster_size == 1) {
6882 return src;
6883 } if (op == nir_op_iand && cluster_size == 4) {
6884 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6885 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6886 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6887 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6888 } else if (op == nir_op_ior && cluster_size == 4) {
6889 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6890 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6891 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6892 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6893 //subgroupAnd(val) -> (exec & ~val) == 0
6894 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6895 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6896 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6897 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6898 //subgroupOr(val) -> (val & exec) != 0
6899 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6900 return bool_to_vector_condition(ctx, tmp);
6901 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6902 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6903 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6904 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6905 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6906 return bool_to_vector_condition(ctx, tmp);
6907 } else {
6908 //subgroupClustered{And,Or,Xor}(val, n) ->
6909 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6910 //cluster_offset = ~(n - 1) & lane_id
6911 //cluster_mask = ((1 << n) - 1)
6912 //subgroupClusteredAnd():
6913 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6914 //subgroupClusteredOr():
6915 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6916 //subgroupClusteredXor():
6917 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6918 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6919 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6920
6921 Temp tmp;
6922 if (op == nir_op_iand)
6923 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6924 else
6925 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6926
6927 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6928
6929 if (ctx->program->chip_class <= GFX7)
6930 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6931 else if (ctx->program->wave_size == 64)
6932 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6933 else
6934 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6935 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6936 if (cluster_mask != 0xffffffff)
6937 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6938
6939 Definition cmp_def = Definition();
6940 if (op == nir_op_iand) {
6941 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6942 } else if (op == nir_op_ior) {
6943 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6944 } else if (op == nir_op_ixor) {
6945 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
6946 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
6947 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6948 }
6949 cmp_def.setHint(vcc);
6950 return cmp_def.getTemp();
6951 }
6952 }
6953
6954 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
6955 {
6956 Builder bld(ctx->program, ctx->block);
6957
6958 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
6959 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
6960 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
6961 Temp tmp;
6962 if (op == nir_op_iand)
6963 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6964 else
6965 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
6966
6967 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
6968 Temp lo = lohi.def(0).getTemp();
6969 Temp hi = lohi.def(1).getTemp();
6970 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
6971
6972 Definition cmp_def = Definition();
6973 if (op == nir_op_iand)
6974 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6975 else if (op == nir_op_ior)
6976 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6977 else if (op == nir_op_ixor)
6978 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
6979 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
6980 cmp_def.setHint(vcc);
6981 return cmp_def.getTemp();
6982 }
6983
6984 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
6985 {
6986 Builder bld(ctx->program, ctx->block);
6987
6988 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
6989 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
6990 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
6991 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
6992 if (op == nir_op_iand)
6993 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6994 else if (op == nir_op_ior)
6995 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6996 else if (op == nir_op_ixor)
6997 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6998
6999 assert(false);
7000 return Temp();
7001 }
7002
7003 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7004 {
7005 Builder bld(ctx->program, ctx->block);
7006 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7007 if (src.regClass().type() == RegType::vgpr) {
7008 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7009 } else if (src.regClass() == s1) {
7010 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7011 } else if (src.regClass() == s2) {
7012 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7013 } else {
7014 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7015 nir_print_instr(&instr->instr, stderr);
7016 fprintf(stderr, "\n");
7017 }
7018 }
7019
7020 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7021 {
7022 Builder bld(ctx->program, ctx->block);
7023 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7024 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7025 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7026
7027 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7028 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7029 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7030 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7031
7032 /* Build DD X/Y */
7033 if (ctx->program->chip_class >= GFX8) {
7034 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7035 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7036 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7037 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7038 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7039 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7040 } else {
7041 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7042 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7043 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7044 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7045 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7046 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7047 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7048 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7049 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7050 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7051 }
7052
7053 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7054 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7055 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7056 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7057 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7058 Temp wqm1 = bld.tmp(v1);
7059 emit_wqm(ctx, tmp1, wqm1, true);
7060 Temp wqm2 = bld.tmp(v1);
7061 emit_wqm(ctx, tmp2, wqm2, true);
7062 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7063 return;
7064 }
7065
7066 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7067 {
7068 Builder bld(ctx->program, ctx->block);
7069 switch(instr->intrinsic) {
7070 case nir_intrinsic_load_barycentric_sample:
7071 case nir_intrinsic_load_barycentric_pixel:
7072 case nir_intrinsic_load_barycentric_centroid: {
7073 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7074 Temp bary = Temp(0, s2);
7075 switch (mode) {
7076 case INTERP_MODE_SMOOTH:
7077 case INTERP_MODE_NONE:
7078 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7079 bary = get_arg(ctx, ctx->args->ac.persp_center);
7080 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7081 bary = ctx->persp_centroid;
7082 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7083 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7084 break;
7085 case INTERP_MODE_NOPERSPECTIVE:
7086 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7087 bary = get_arg(ctx, ctx->args->ac.linear_center);
7088 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7089 bary = ctx->linear_centroid;
7090 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7091 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7092 break;
7093 default:
7094 break;
7095 }
7096 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7097 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7098 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7099 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7100 Operand(p1), Operand(p2));
7101 emit_split_vector(ctx, dst, 2);
7102 break;
7103 }
7104 case nir_intrinsic_load_barycentric_model: {
7105 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7106
7107 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7108 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7109 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7110 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7111 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7112 Operand(p1), Operand(p2), Operand(p3));
7113 emit_split_vector(ctx, dst, 3);
7114 break;
7115 }
7116 case nir_intrinsic_load_barycentric_at_sample: {
7117 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7118 switch (ctx->options->key.fs.num_samples) {
7119 case 2: sample_pos_offset += 1 << 3; break;
7120 case 4: sample_pos_offset += 3 << 3; break;
7121 case 8: sample_pos_offset += 7 << 3; break;
7122 default: break;
7123 }
7124 Temp sample_pos;
7125 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7126 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7127 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7128 if (addr.type() == RegType::sgpr) {
7129 Operand offset;
7130 if (const_addr) {
7131 sample_pos_offset += const_addr->u32 << 3;
7132 offset = Operand(sample_pos_offset);
7133 } else if (ctx->options->chip_class >= GFX9) {
7134 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7135 } else {
7136 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7137 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7138 }
7139
7140 Operand off = bld.copy(bld.def(s1), Operand(offset));
7141 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7142
7143 } else if (ctx->options->chip_class >= GFX9) {
7144 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7145 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7146 } else if (ctx->options->chip_class >= GFX7) {
7147 /* addr += private_segment_buffer + sample_pos_offset */
7148 Temp tmp0 = bld.tmp(s1);
7149 Temp tmp1 = bld.tmp(s1);
7150 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7151 Definition scc_tmp = bld.def(s1, scc);
7152 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7153 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7154 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7155 Temp pck0 = bld.tmp(v1);
7156 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7157 tmp1 = as_vgpr(ctx, tmp1);
7158 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);
7159 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7160
7161 /* sample_pos = flat_load_dwordx2 addr */
7162 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7163 } else {
7164 assert(ctx->options->chip_class == GFX6);
7165
7166 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7167 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7168 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7169
7170 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7171 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7172
7173 sample_pos = bld.tmp(v2);
7174
7175 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7176 load->definitions[0] = Definition(sample_pos);
7177 load->operands[0] = Operand(rsrc);
7178 load->operands[1] = Operand(addr);
7179 load->operands[2] = Operand(0u);
7180 load->offset = sample_pos_offset;
7181 load->offen = 0;
7182 load->addr64 = true;
7183 load->glc = false;
7184 load->dlc = false;
7185 load->disable_wqm = false;
7186 load->barrier = barrier_none;
7187 load->can_reorder = true;
7188 ctx->block->instructions.emplace_back(std::move(load));
7189 }
7190
7191 /* sample_pos -= 0.5 */
7192 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7193 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7194 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7195 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7196 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7197
7198 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7199 break;
7200 }
7201 case nir_intrinsic_load_barycentric_at_offset: {
7202 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7203 RegClass rc = RegClass(offset.type(), 1);
7204 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7205 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7206 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7207 break;
7208 }
7209 case nir_intrinsic_load_front_face: {
7210 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7211 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7212 break;
7213 }
7214 case nir_intrinsic_load_view_index: {
7215 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7216 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7217 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7218 break;
7219 }
7220
7221 /* fallthrough */
7222 }
7223 case nir_intrinsic_load_layer_id: {
7224 unsigned idx = nir_intrinsic_base(instr);
7225 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7226 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7227 break;
7228 }
7229 case nir_intrinsic_load_frag_coord: {
7230 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7231 break;
7232 }
7233 case nir_intrinsic_load_sample_pos: {
7234 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7235 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7236 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7237 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7238 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7239 break;
7240 }
7241 case nir_intrinsic_load_tess_coord:
7242 visit_load_tess_coord(ctx, instr);
7243 break;
7244 case nir_intrinsic_load_interpolated_input:
7245 visit_load_interpolated_input(ctx, instr);
7246 break;
7247 case nir_intrinsic_store_output:
7248 visit_store_output(ctx, instr);
7249 break;
7250 case nir_intrinsic_load_input:
7251 case nir_intrinsic_load_input_vertex:
7252 visit_load_input(ctx, instr);
7253 break;
7254 case nir_intrinsic_load_output:
7255 visit_load_output(ctx, instr);
7256 break;
7257 case nir_intrinsic_load_per_vertex_input:
7258 visit_load_per_vertex_input(ctx, instr);
7259 break;
7260 case nir_intrinsic_load_per_vertex_output:
7261 visit_load_per_vertex_output(ctx, instr);
7262 break;
7263 case nir_intrinsic_store_per_vertex_output:
7264 visit_store_per_vertex_output(ctx, instr);
7265 break;
7266 case nir_intrinsic_load_ubo:
7267 visit_load_ubo(ctx, instr);
7268 break;
7269 case nir_intrinsic_load_push_constant:
7270 visit_load_push_constant(ctx, instr);
7271 break;
7272 case nir_intrinsic_load_constant:
7273 visit_load_constant(ctx, instr);
7274 break;
7275 case nir_intrinsic_vulkan_resource_index:
7276 visit_load_resource(ctx, instr);
7277 break;
7278 case nir_intrinsic_discard:
7279 visit_discard(ctx, instr);
7280 break;
7281 case nir_intrinsic_discard_if:
7282 visit_discard_if(ctx, instr);
7283 break;
7284 case nir_intrinsic_load_shared:
7285 visit_load_shared(ctx, instr);
7286 break;
7287 case nir_intrinsic_store_shared:
7288 visit_store_shared(ctx, instr);
7289 break;
7290 case nir_intrinsic_shared_atomic_add:
7291 case nir_intrinsic_shared_atomic_imin:
7292 case nir_intrinsic_shared_atomic_umin:
7293 case nir_intrinsic_shared_atomic_imax:
7294 case nir_intrinsic_shared_atomic_umax:
7295 case nir_intrinsic_shared_atomic_and:
7296 case nir_intrinsic_shared_atomic_or:
7297 case nir_intrinsic_shared_atomic_xor:
7298 case nir_intrinsic_shared_atomic_exchange:
7299 case nir_intrinsic_shared_atomic_comp_swap:
7300 visit_shared_atomic(ctx, instr);
7301 break;
7302 case nir_intrinsic_image_deref_load:
7303 visit_image_load(ctx, instr);
7304 break;
7305 case nir_intrinsic_image_deref_store:
7306 visit_image_store(ctx, instr);
7307 break;
7308 case nir_intrinsic_image_deref_atomic_add:
7309 case nir_intrinsic_image_deref_atomic_umin:
7310 case nir_intrinsic_image_deref_atomic_imin:
7311 case nir_intrinsic_image_deref_atomic_umax:
7312 case nir_intrinsic_image_deref_atomic_imax:
7313 case nir_intrinsic_image_deref_atomic_and:
7314 case nir_intrinsic_image_deref_atomic_or:
7315 case nir_intrinsic_image_deref_atomic_xor:
7316 case nir_intrinsic_image_deref_atomic_exchange:
7317 case nir_intrinsic_image_deref_atomic_comp_swap:
7318 visit_image_atomic(ctx, instr);
7319 break;
7320 case nir_intrinsic_image_deref_size:
7321 visit_image_size(ctx, instr);
7322 break;
7323 case nir_intrinsic_load_ssbo:
7324 visit_load_ssbo(ctx, instr);
7325 break;
7326 case nir_intrinsic_store_ssbo:
7327 visit_store_ssbo(ctx, instr);
7328 break;
7329 case nir_intrinsic_load_global:
7330 visit_load_global(ctx, instr);
7331 break;
7332 case nir_intrinsic_store_global:
7333 visit_store_global(ctx, instr);
7334 break;
7335 case nir_intrinsic_global_atomic_add:
7336 case nir_intrinsic_global_atomic_imin:
7337 case nir_intrinsic_global_atomic_umin:
7338 case nir_intrinsic_global_atomic_imax:
7339 case nir_intrinsic_global_atomic_umax:
7340 case nir_intrinsic_global_atomic_and:
7341 case nir_intrinsic_global_atomic_or:
7342 case nir_intrinsic_global_atomic_xor:
7343 case nir_intrinsic_global_atomic_exchange:
7344 case nir_intrinsic_global_atomic_comp_swap:
7345 visit_global_atomic(ctx, instr);
7346 break;
7347 case nir_intrinsic_ssbo_atomic_add:
7348 case nir_intrinsic_ssbo_atomic_imin:
7349 case nir_intrinsic_ssbo_atomic_umin:
7350 case nir_intrinsic_ssbo_atomic_imax:
7351 case nir_intrinsic_ssbo_atomic_umax:
7352 case nir_intrinsic_ssbo_atomic_and:
7353 case nir_intrinsic_ssbo_atomic_or:
7354 case nir_intrinsic_ssbo_atomic_xor:
7355 case nir_intrinsic_ssbo_atomic_exchange:
7356 case nir_intrinsic_ssbo_atomic_comp_swap:
7357 visit_atomic_ssbo(ctx, instr);
7358 break;
7359 case nir_intrinsic_load_scratch:
7360 visit_load_scratch(ctx, instr);
7361 break;
7362 case nir_intrinsic_store_scratch:
7363 visit_store_scratch(ctx, instr);
7364 break;
7365 case nir_intrinsic_get_buffer_size:
7366 visit_get_buffer_size(ctx, instr);
7367 break;
7368 case nir_intrinsic_control_barrier: {
7369 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7370 /* GFX6 only (thanks to a hw bug workaround):
7371 * The real barrier instruction isn’t needed, because an entire patch
7372 * always fits into a single wave.
7373 */
7374 break;
7375 }
7376
7377 if (ctx->program->workgroup_size > ctx->program->wave_size)
7378 bld.sopp(aco_opcode::s_barrier);
7379
7380 break;
7381 }
7382 case nir_intrinsic_memory_barrier_tcs_patch:
7383 case nir_intrinsic_group_memory_barrier:
7384 case nir_intrinsic_memory_barrier:
7385 case nir_intrinsic_memory_barrier_buffer:
7386 case nir_intrinsic_memory_barrier_image:
7387 case nir_intrinsic_memory_barrier_shared:
7388 emit_memory_barrier(ctx, instr);
7389 break;
7390 case nir_intrinsic_load_num_work_groups: {
7391 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7392 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7393 emit_split_vector(ctx, dst, 3);
7394 break;
7395 }
7396 case nir_intrinsic_load_local_invocation_id: {
7397 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7398 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7399 emit_split_vector(ctx, dst, 3);
7400 break;
7401 }
7402 case nir_intrinsic_load_work_group_id: {
7403 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7404 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7405 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7406 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7407 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7408 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7409 emit_split_vector(ctx, dst, 3);
7410 break;
7411 }
7412 case nir_intrinsic_load_local_invocation_index: {
7413 Temp id = emit_mbcnt(ctx, bld.def(v1));
7414
7415 /* The tg_size bits [6:11] contain the subgroup id,
7416 * we need this multiplied by the wave size, and then OR the thread id to it.
7417 */
7418 if (ctx->program->wave_size == 64) {
7419 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7420 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7421 get_arg(ctx, ctx->args->ac.tg_size));
7422 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7423 } else {
7424 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7425 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7426 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7427 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7428 }
7429 break;
7430 }
7431 case nir_intrinsic_load_subgroup_id: {
7432 if (ctx->stage == compute_cs) {
7433 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7434 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7435 } else {
7436 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7437 }
7438 break;
7439 }
7440 case nir_intrinsic_load_subgroup_invocation: {
7441 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7442 break;
7443 }
7444 case nir_intrinsic_load_num_subgroups: {
7445 if (ctx->stage == compute_cs)
7446 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7447 get_arg(ctx, ctx->args->ac.tg_size));
7448 else
7449 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7450 break;
7451 }
7452 case nir_intrinsic_ballot: {
7453 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7454 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7455 Definition tmp = bld.def(dst.regClass());
7456 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7457 if (instr->src[0].ssa->bit_size == 1) {
7458 assert(src.regClass() == bld.lm);
7459 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7460 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7461 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7462 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7463 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7464 } else {
7465 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7466 nir_print_instr(&instr->instr, stderr);
7467 fprintf(stderr, "\n");
7468 }
7469 if (dst.size() != bld.lm.size()) {
7470 /* Wave32 with ballot size set to 64 */
7471 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7472 }
7473 emit_wqm(ctx, tmp.getTemp(), dst);
7474 break;
7475 }
7476 case nir_intrinsic_shuffle:
7477 case nir_intrinsic_read_invocation: {
7478 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7479 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7480 emit_uniform_subgroup(ctx, instr, src);
7481 } else {
7482 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7483 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7484 tid = bld.as_uniform(tid);
7485 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7486 if (src.regClass() == v1) {
7487 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7488 } else if (src.regClass() == v2) {
7489 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7490 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7491 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7492 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7493 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7494 emit_split_vector(ctx, dst, 2);
7495 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7496 assert(src.regClass() == bld.lm);
7497 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7498 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7499 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7500 assert(src.regClass() == bld.lm);
7501 Temp tmp;
7502 if (ctx->program->chip_class <= GFX7)
7503 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7504 else if (ctx->program->wave_size == 64)
7505 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7506 else
7507 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7508 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7509 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7510 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7511 } else {
7512 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7513 nir_print_instr(&instr->instr, stderr);
7514 fprintf(stderr, "\n");
7515 }
7516 }
7517 break;
7518 }
7519 case nir_intrinsic_load_sample_id: {
7520 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7521 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7522 break;
7523 }
7524 case nir_intrinsic_load_sample_mask_in: {
7525 visit_load_sample_mask_in(ctx, instr);
7526 break;
7527 }
7528 case nir_intrinsic_read_first_invocation: {
7529 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7530 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7531 if (src.regClass() == v1) {
7532 emit_wqm(ctx,
7533 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7534 dst);
7535 } else if (src.regClass() == v2) {
7536 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7537 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7538 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7539 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7540 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7541 emit_split_vector(ctx, dst, 2);
7542 } else if (instr->dest.ssa.bit_size == 1) {
7543 assert(src.regClass() == bld.lm);
7544 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7545 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7546 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7547 } else if (src.regClass() == s1) {
7548 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7549 } else if (src.regClass() == s2) {
7550 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7551 } else {
7552 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7553 nir_print_instr(&instr->instr, stderr);
7554 fprintf(stderr, "\n");
7555 }
7556 break;
7557 }
7558 case nir_intrinsic_vote_all: {
7559 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7560 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7561 assert(src.regClass() == bld.lm);
7562 assert(dst.regClass() == bld.lm);
7563
7564 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7565 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7566 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7567 break;
7568 }
7569 case nir_intrinsic_vote_any: {
7570 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7571 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7572 assert(src.regClass() == bld.lm);
7573 assert(dst.regClass() == bld.lm);
7574
7575 Temp tmp = bool_to_scalar_condition(ctx, src);
7576 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7577 break;
7578 }
7579 case nir_intrinsic_reduce:
7580 case nir_intrinsic_inclusive_scan:
7581 case nir_intrinsic_exclusive_scan: {
7582 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7583 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7584 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7585 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7586 nir_intrinsic_cluster_size(instr) : 0;
7587 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7588
7589 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7590 emit_uniform_subgroup(ctx, instr, src);
7591 } else if (instr->dest.ssa.bit_size == 1) {
7592 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7593 op = nir_op_iand;
7594 else if (op == nir_op_iadd)
7595 op = nir_op_ixor;
7596 else if (op == nir_op_umax || op == nir_op_imax)
7597 op = nir_op_ior;
7598 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7599
7600 switch (instr->intrinsic) {
7601 case nir_intrinsic_reduce:
7602 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7603 break;
7604 case nir_intrinsic_exclusive_scan:
7605 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7606 break;
7607 case nir_intrinsic_inclusive_scan:
7608 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7609 break;
7610 default:
7611 assert(false);
7612 }
7613 } else if (cluster_size == 1) {
7614 bld.copy(Definition(dst), src);
7615 } else {
7616 src = as_vgpr(ctx, src);
7617
7618 ReduceOp reduce_op;
7619 switch (op) {
7620 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7621 CASE(iadd)
7622 CASE(imul)
7623 CASE(fadd)
7624 CASE(fmul)
7625 CASE(imin)
7626 CASE(umin)
7627 CASE(fmin)
7628 CASE(imax)
7629 CASE(umax)
7630 CASE(fmax)
7631 CASE(iand)
7632 CASE(ior)
7633 CASE(ixor)
7634 default:
7635 unreachable("unknown reduction op");
7636 #undef CASE
7637 }
7638
7639 aco_opcode aco_op;
7640 switch (instr->intrinsic) {
7641 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7642 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7643 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7644 default:
7645 unreachable("unknown reduce intrinsic");
7646 }
7647
7648 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7649 reduce->operands[0] = Operand(src);
7650 // filled in by aco_reduce_assign.cpp, used internally as part of the
7651 // reduce sequence
7652 assert(dst.size() == 1 || dst.size() == 2);
7653 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7654 reduce->operands[2] = Operand(v1.as_linear());
7655
7656 Temp tmp_dst = bld.tmp(dst.regClass());
7657 reduce->definitions[0] = Definition(tmp_dst);
7658 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7659 reduce->definitions[2] = Definition();
7660 reduce->definitions[3] = Definition(scc, s1);
7661 reduce->definitions[4] = Definition();
7662 reduce->reduce_op = reduce_op;
7663 reduce->cluster_size = cluster_size;
7664 ctx->block->instructions.emplace_back(std::move(reduce));
7665
7666 emit_wqm(ctx, tmp_dst, dst);
7667 }
7668 break;
7669 }
7670 case nir_intrinsic_quad_broadcast: {
7671 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7672 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7673 emit_uniform_subgroup(ctx, instr, src);
7674 } else {
7675 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7676 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7677 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7678
7679 if (instr->dest.ssa.bit_size == 1) {
7680 assert(src.regClass() == bld.lm);
7681 assert(dst.regClass() == bld.lm);
7682 uint32_t half_mask = 0x11111111u << lane;
7683 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7684 Temp tmp = bld.tmp(bld.lm);
7685 bld.sop1(Builder::s_wqm, Definition(tmp),
7686 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7687 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7688 emit_wqm(ctx, tmp, dst);
7689 } else if (instr->dest.ssa.bit_size == 32) {
7690 if (ctx->program->chip_class >= GFX8)
7691 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7692 else
7693 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7694 } else if (instr->dest.ssa.bit_size == 64) {
7695 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7696 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7697 if (ctx->program->chip_class >= GFX8) {
7698 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7699 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7700 } else {
7701 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7702 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7703 }
7704 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7705 emit_split_vector(ctx, dst, 2);
7706 } else {
7707 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7708 nir_print_instr(&instr->instr, stderr);
7709 fprintf(stderr, "\n");
7710 }
7711 }
7712 break;
7713 }
7714 case nir_intrinsic_quad_swap_horizontal:
7715 case nir_intrinsic_quad_swap_vertical:
7716 case nir_intrinsic_quad_swap_diagonal:
7717 case nir_intrinsic_quad_swizzle_amd: {
7718 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7719 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7720 emit_uniform_subgroup(ctx, instr, src);
7721 break;
7722 }
7723 uint16_t dpp_ctrl = 0;
7724 switch (instr->intrinsic) {
7725 case nir_intrinsic_quad_swap_horizontal:
7726 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7727 break;
7728 case nir_intrinsic_quad_swap_vertical:
7729 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7730 break;
7731 case nir_intrinsic_quad_swap_diagonal:
7732 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7733 break;
7734 case nir_intrinsic_quad_swizzle_amd:
7735 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7736 break;
7737 default:
7738 break;
7739 }
7740 if (ctx->program->chip_class < GFX8)
7741 dpp_ctrl |= (1 << 15);
7742
7743 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7744 if (instr->dest.ssa.bit_size == 1) {
7745 assert(src.regClass() == bld.lm);
7746 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7747 if (ctx->program->chip_class >= GFX8)
7748 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7749 else
7750 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7751 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7752 emit_wqm(ctx, tmp, dst);
7753 } else if (instr->dest.ssa.bit_size == 32) {
7754 Temp tmp;
7755 if (ctx->program->chip_class >= GFX8)
7756 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7757 else
7758 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7759 emit_wqm(ctx, tmp, dst);
7760 } else if (instr->dest.ssa.bit_size == 64) {
7761 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7762 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7763 if (ctx->program->chip_class >= GFX8) {
7764 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7765 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7766 } else {
7767 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7768 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7769 }
7770 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7771 emit_split_vector(ctx, dst, 2);
7772 } else {
7773 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7774 nir_print_instr(&instr->instr, stderr);
7775 fprintf(stderr, "\n");
7776 }
7777 break;
7778 }
7779 case nir_intrinsic_masked_swizzle_amd: {
7780 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7781 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7782 emit_uniform_subgroup(ctx, instr, src);
7783 break;
7784 }
7785 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7786 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7787 if (dst.regClass() == v1) {
7788 emit_wqm(ctx,
7789 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7790 dst);
7791 } else if (dst.regClass() == v2) {
7792 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7793 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7794 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7795 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7796 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7797 emit_split_vector(ctx, dst, 2);
7798 } else {
7799 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7800 nir_print_instr(&instr->instr, stderr);
7801 fprintf(stderr, "\n");
7802 }
7803 break;
7804 }
7805 case nir_intrinsic_write_invocation_amd: {
7806 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7807 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7808 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7809 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7810 if (dst.regClass() == v1) {
7811 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7812 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7813 } else if (dst.regClass() == v2) {
7814 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7815 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7816 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7817 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7818 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7819 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7820 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7821 emit_split_vector(ctx, dst, 2);
7822 } else {
7823 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7824 nir_print_instr(&instr->instr, stderr);
7825 fprintf(stderr, "\n");
7826 }
7827 break;
7828 }
7829 case nir_intrinsic_mbcnt_amd: {
7830 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7831 RegClass rc = RegClass(src.type(), 1);
7832 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7833 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7834 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7835 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7836 emit_wqm(ctx, wqm_tmp, dst);
7837 break;
7838 }
7839 case nir_intrinsic_load_helper_invocation: {
7840 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7841 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7842 ctx->block->kind |= block_kind_needs_lowering;
7843 ctx->program->needs_exact = true;
7844 break;
7845 }
7846 case nir_intrinsic_is_helper_invocation: {
7847 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7848 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7849 ctx->block->kind |= block_kind_needs_lowering;
7850 ctx->program->needs_exact = true;
7851 break;
7852 }
7853 case nir_intrinsic_demote:
7854 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7855
7856 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7857 ctx->cf_info.exec_potentially_empty_discard = true;
7858 ctx->block->kind |= block_kind_uses_demote;
7859 ctx->program->needs_exact = true;
7860 break;
7861 case nir_intrinsic_demote_if: {
7862 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7863 assert(src.regClass() == bld.lm);
7864 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7865 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7866
7867 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7868 ctx->cf_info.exec_potentially_empty_discard = true;
7869 ctx->block->kind |= block_kind_uses_demote;
7870 ctx->program->needs_exact = true;
7871 break;
7872 }
7873 case nir_intrinsic_first_invocation: {
7874 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7875 get_ssa_temp(ctx, &instr->dest.ssa));
7876 break;
7877 }
7878 case nir_intrinsic_shader_clock:
7879 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7880 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7881 break;
7882 case nir_intrinsic_load_vertex_id_zero_base: {
7883 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7884 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7885 break;
7886 }
7887 case nir_intrinsic_load_first_vertex: {
7888 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7889 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7890 break;
7891 }
7892 case nir_intrinsic_load_base_instance: {
7893 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7894 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7895 break;
7896 }
7897 case nir_intrinsic_load_instance_id: {
7898 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7899 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7900 break;
7901 }
7902 case nir_intrinsic_load_draw_id: {
7903 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7904 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7905 break;
7906 }
7907 case nir_intrinsic_load_invocation_id: {
7908 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7909
7910 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7911 if (ctx->options->chip_class >= GFX10)
7912 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7913 else
7914 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7915 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7916 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7917 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7918 } else {
7919 unreachable("Unsupported stage for load_invocation_id");
7920 }
7921
7922 break;
7923 }
7924 case nir_intrinsic_load_primitive_id: {
7925 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7926
7927 switch (ctx->shader->info.stage) {
7928 case MESA_SHADER_GEOMETRY:
7929 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7930 break;
7931 case MESA_SHADER_TESS_CTRL:
7932 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7933 break;
7934 case MESA_SHADER_TESS_EVAL:
7935 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7936 break;
7937 default:
7938 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7939 }
7940
7941 break;
7942 }
7943 case nir_intrinsic_load_patch_vertices_in: {
7944 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
7945 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
7946
7947 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7948 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
7949 break;
7950 }
7951 case nir_intrinsic_emit_vertex_with_counter: {
7952 visit_emit_vertex_with_counter(ctx, instr);
7953 break;
7954 }
7955 case nir_intrinsic_end_primitive_with_counter: {
7956 unsigned stream = nir_intrinsic_stream_id(instr);
7957 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
7958 break;
7959 }
7960 case nir_intrinsic_set_vertex_count: {
7961 /* unused, the HW keeps track of this for us */
7962 break;
7963 }
7964 default:
7965 fprintf(stderr, "Unimplemented intrinsic instr: ");
7966 nir_print_instr(&instr->instr, stderr);
7967 fprintf(stderr, "\n");
7968 abort();
7969
7970 break;
7971 }
7972 }
7973
7974
7975 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
7976 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
7977 enum glsl_base_type *stype)
7978 {
7979 nir_deref_instr *texture_deref_instr = NULL;
7980 nir_deref_instr *sampler_deref_instr = NULL;
7981 int plane = -1;
7982
7983 for (unsigned i = 0; i < instr->num_srcs; i++) {
7984 switch (instr->src[i].src_type) {
7985 case nir_tex_src_texture_deref:
7986 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
7987 break;
7988 case nir_tex_src_sampler_deref:
7989 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
7990 break;
7991 case nir_tex_src_plane:
7992 plane = nir_src_as_int(instr->src[i].src);
7993 break;
7994 default:
7995 break;
7996 }
7997 }
7998
7999 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8000
8001 if (!sampler_deref_instr)
8002 sampler_deref_instr = texture_deref_instr;
8003
8004 if (plane >= 0) {
8005 assert(instr->op != nir_texop_txf_ms &&
8006 instr->op != nir_texop_samples_identical);
8007 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8008 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8009 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8010 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8011 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8012 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8013 } else {
8014 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8015 }
8016 if (samp_ptr) {
8017 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8018
8019 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8020 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8021 Builder bld(ctx->program, ctx->block);
8022
8023 /* to avoid unnecessary moves, we split and recombine sampler and image */
8024 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8025 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8026 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8027 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8028 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8029 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8030 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8031 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8032
8033 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8034 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8035 img[0], img[1], img[2], img[3],
8036 img[4], img[5], img[6], img[7]);
8037 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8038 samp[0], samp[1], samp[2], samp[3]);
8039 }
8040 }
8041 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8042 instr->op == nir_texop_samples_identical))
8043 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8044 }
8045
8046 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8047 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8048 {
8049 Builder bld(ctx->program, ctx->block);
8050
8051 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8052 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8053 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8054
8055 Operand neg_one(0xbf800000u);
8056 Operand one(0x3f800000u);
8057 Operand two(0x40000000u);
8058 Operand four(0x40800000u);
8059
8060 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8061 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8062 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8063
8064 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8065 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8066 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8067 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);
8068
8069 // select sc
8070 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8071 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8072 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8073 one, is_ma_y);
8074 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8075
8076 // select tc
8077 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8078 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8079 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8080
8081 // select ma
8082 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8083 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8084 deriv_z, is_ma_z);
8085 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8086 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8087 }
8088
8089 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8090 {
8091 Builder bld(ctx->program, ctx->block);
8092 Temp ma, tc, sc, id;
8093
8094 if (is_array) {
8095 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8096
8097 // see comment in ac_prepare_cube_coords()
8098 if (ctx->options->chip_class <= GFX8)
8099 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8100 }
8101
8102 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8103
8104 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8105 vop3a->operands[0] = Operand(ma);
8106 vop3a->abs[0] = true;
8107 Temp invma = bld.tmp(v1);
8108 vop3a->definitions[0] = Definition(invma);
8109 ctx->block->instructions.emplace_back(std::move(vop3a));
8110
8111 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8112 if (!is_deriv)
8113 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8114
8115 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8116 if (!is_deriv)
8117 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8118
8119 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8120
8121 if (is_deriv) {
8122 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8123 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8124
8125 for (unsigned i = 0; i < 2; i++) {
8126 // see comment in ac_prepare_cube_coords()
8127 Temp deriv_ma;
8128 Temp deriv_sc, deriv_tc;
8129 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8130 &deriv_ma, &deriv_sc, &deriv_tc);
8131
8132 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8133
8134 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8135 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8136 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8137 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8138 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8139 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8140 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8141 }
8142
8143 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8144 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8145 }
8146
8147 if (is_array)
8148 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8149 coords.resize(3);
8150 coords[0] = sc;
8151 coords[1] = tc;
8152 coords[2] = id;
8153 }
8154
8155 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8156 {
8157 if (vec->parent_instr->type != nir_instr_type_alu)
8158 return;
8159 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8160 if (vec_instr->op != nir_op_vec(vec->num_components))
8161 return;
8162
8163 for (unsigned i = 0; i < vec->num_components; i++) {
8164 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8165 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8166 }
8167 }
8168
8169 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8170 {
8171 Builder bld(ctx->program, ctx->block);
8172 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8173 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8174 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8175 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8176 std::vector<Temp> coords;
8177 std::vector<Temp> derivs;
8178 nir_const_value *sample_index_cv = NULL;
8179 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8180 enum glsl_base_type stype;
8181 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8182
8183 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8184 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8185 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8186 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8187
8188 for (unsigned i = 0; i < instr->num_srcs; i++) {
8189 switch (instr->src[i].src_type) {
8190 case nir_tex_src_coord: {
8191 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8192 for (unsigned i = 0; i < coord.size(); i++)
8193 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8194 break;
8195 }
8196 case nir_tex_src_bias:
8197 if (instr->op == nir_texop_txb) {
8198 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8199 has_bias = true;
8200 }
8201 break;
8202 case nir_tex_src_lod: {
8203 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8204
8205 if (val && val->f32 <= 0.0) {
8206 level_zero = true;
8207 } else {
8208 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8209 has_lod = true;
8210 }
8211 break;
8212 }
8213 case nir_tex_src_comparator:
8214 if (instr->is_shadow) {
8215 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8216 has_compare = true;
8217 }
8218 break;
8219 case nir_tex_src_offset:
8220 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8221 get_const_vec(instr->src[i].src.ssa, const_offset);
8222 has_offset = true;
8223 break;
8224 case nir_tex_src_ddx:
8225 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8226 has_ddx = true;
8227 break;
8228 case nir_tex_src_ddy:
8229 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8230 has_ddy = true;
8231 break;
8232 case nir_tex_src_ms_index:
8233 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8234 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8235 has_sample_index = true;
8236 break;
8237 case nir_tex_src_texture_offset:
8238 case nir_tex_src_sampler_offset:
8239 default:
8240 break;
8241 }
8242 }
8243
8244 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8245 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8246
8247 if (instr->op == nir_texop_texture_samples) {
8248 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8249
8250 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8251 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8252 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 */));
8253
8254 Operand default_sample = Operand(1u);
8255 if (ctx->options->robust_buffer_access) {
8256 /* Extract the second dword of the descriptor, if it's
8257 * all zero, then it's a null descriptor.
8258 */
8259 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8260 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8261 default_sample = Operand(is_non_null_descriptor);
8262 }
8263
8264 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8265 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8266 samples, default_sample, bld.scc(is_msaa));
8267 return;
8268 }
8269
8270 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8271 aco_ptr<Instruction> tmp_instr;
8272 Temp acc, pack = Temp();
8273
8274 uint32_t pack_const = 0;
8275 for (unsigned i = 0; i < offset.size(); i++) {
8276 if (!const_offset[i])
8277 continue;
8278 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8279 }
8280
8281 if (offset.type() == RegType::sgpr) {
8282 for (unsigned i = 0; i < offset.size(); i++) {
8283 if (const_offset[i])
8284 continue;
8285
8286 acc = emit_extract_vector(ctx, offset, i, s1);
8287 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8288
8289 if (i) {
8290 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8291 }
8292
8293 if (pack == Temp()) {
8294 pack = acc;
8295 } else {
8296 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8297 }
8298 }
8299
8300 if (pack_const && pack != Temp())
8301 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8302 } else {
8303 for (unsigned i = 0; i < offset.size(); i++) {
8304 if (const_offset[i])
8305 continue;
8306
8307 acc = emit_extract_vector(ctx, offset, i, v1);
8308 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8309
8310 if (i) {
8311 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8312 }
8313
8314 if (pack == Temp()) {
8315 pack = acc;
8316 } else {
8317 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8318 }
8319 }
8320
8321 if (pack_const && pack != Temp())
8322 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8323 }
8324 if (pack_const && pack == Temp())
8325 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8326 else if (pack == Temp())
8327 has_offset = false;
8328 else
8329 offset = pack;
8330 }
8331
8332 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8333 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8334
8335 /* pack derivatives */
8336 if (has_ddx || has_ddy) {
8337 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8338 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8339 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8340 derivs = {ddx, zero, ddy, zero};
8341 } else {
8342 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8343 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8344 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8345 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8346 }
8347 has_derivs = true;
8348 }
8349
8350 if (instr->coord_components > 1 &&
8351 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8352 instr->is_array &&
8353 instr->op != nir_texop_txf)
8354 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8355
8356 if (instr->coord_components > 2 &&
8357 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8358 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8359 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8360 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8361 instr->is_array &&
8362 instr->op != nir_texop_txf &&
8363 instr->op != nir_texop_txf_ms &&
8364 instr->op != nir_texop_fragment_fetch &&
8365 instr->op != nir_texop_fragment_mask_fetch)
8366 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8367
8368 if (ctx->options->chip_class == GFX9 &&
8369 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8370 instr->op != nir_texop_lod && instr->coord_components) {
8371 assert(coords.size() > 0 && coords.size() < 3);
8372
8373 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8374 Operand((uint32_t) 0) :
8375 Operand((uint32_t) 0x3f000000)));
8376 }
8377
8378 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8379
8380 if (instr->op == nir_texop_samples_identical)
8381 resource = fmask_ptr;
8382
8383 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8384 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8385 instr->op != nir_texop_txs &&
8386 instr->op != nir_texop_fragment_fetch &&
8387 instr->op != nir_texop_fragment_mask_fetch) {
8388 assert(has_sample_index);
8389 Operand op(sample_index);
8390 if (sample_index_cv)
8391 op = Operand(sample_index_cv->u32);
8392 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8393 }
8394
8395 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8396 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8397 Temp off = emit_extract_vector(ctx, offset, i, v1);
8398 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8399 }
8400 has_offset = false;
8401 }
8402
8403 /* Build tex instruction */
8404 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8405 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8406 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8407 : 0;
8408 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8409 Temp tmp_dst = dst;
8410
8411 /* gather4 selects the component by dmask and always returns vec4 */
8412 if (instr->op == nir_texop_tg4) {
8413 assert(instr->dest.ssa.num_components == 4);
8414 if (instr->is_shadow)
8415 dmask = 1;
8416 else
8417 dmask = 1 << instr->component;
8418 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8419 tmp_dst = bld.tmp(v4);
8420 } else if (instr->op == nir_texop_samples_identical) {
8421 tmp_dst = bld.tmp(v1);
8422 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8423 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8424 }
8425
8426 aco_ptr<MIMG_instruction> tex;
8427 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8428 if (!has_lod)
8429 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8430
8431 bool div_by_6 = instr->op == nir_texop_txs &&
8432 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8433 instr->is_array &&
8434 (dmask & (1 << 2));
8435 if (tmp_dst.id() == dst.id() && div_by_6)
8436 tmp_dst = bld.tmp(tmp_dst.regClass());
8437
8438 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8439 tex->operands[0] = Operand(resource);
8440 tex->operands[1] = Operand(s4); /* no sampler */
8441 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8442 if (ctx->options->chip_class == GFX9 &&
8443 instr->op == nir_texop_txs &&
8444 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8445 instr->is_array) {
8446 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8447 } else if (instr->op == nir_texop_query_levels) {
8448 tex->dmask = 1 << 3;
8449 } else {
8450 tex->dmask = dmask;
8451 }
8452 tex->da = da;
8453 tex->definitions[0] = Definition(tmp_dst);
8454 tex->dim = dim;
8455 tex->can_reorder = true;
8456 ctx->block->instructions.emplace_back(std::move(tex));
8457
8458 if (div_by_6) {
8459 /* divide 3rd value by 6 by multiplying with magic number */
8460 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8461 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8462 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8463 assert(instr->dest.ssa.num_components == 3);
8464 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8465 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8466 emit_extract_vector(ctx, tmp_dst, 0, v1),
8467 emit_extract_vector(ctx, tmp_dst, 1, v1),
8468 by_6);
8469
8470 }
8471
8472 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8473 return;
8474 }
8475
8476 Temp tg4_compare_cube_wa64 = Temp();
8477
8478 if (tg4_integer_workarounds) {
8479 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8480 tex->operands[0] = Operand(resource);
8481 tex->operands[1] = Operand(s4); /* no sampler */
8482 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8483 tex->dim = dim;
8484 tex->dmask = 0x3;
8485 tex->da = da;
8486 Temp size = bld.tmp(v2);
8487 tex->definitions[0] = Definition(size);
8488 tex->can_reorder = true;
8489 ctx->block->instructions.emplace_back(std::move(tex));
8490 emit_split_vector(ctx, size, size.size());
8491
8492 Temp half_texel[2];
8493 for (unsigned i = 0; i < 2; i++) {
8494 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8495 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8496 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8497 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8498 }
8499
8500 Temp new_coords[2] = {
8501 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8502 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8503 };
8504
8505 if (tg4_integer_cube_workaround) {
8506 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8507 Temp desc[resource.size()];
8508 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8509 Format::PSEUDO, 1, resource.size())};
8510 split->operands[0] = Operand(resource);
8511 for (unsigned i = 0; i < resource.size(); i++) {
8512 desc[i] = bld.tmp(s1);
8513 split->definitions[i] = Definition(desc[i]);
8514 }
8515 ctx->block->instructions.emplace_back(std::move(split));
8516
8517 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8518 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8519 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8520
8521 Temp nfmt;
8522 if (stype == GLSL_TYPE_UINT) {
8523 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8524 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8525 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8526 bld.scc(compare_cube_wa));
8527 } else {
8528 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8529 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8530 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8531 bld.scc(compare_cube_wa));
8532 }
8533 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8534 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8535
8536 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8537
8538 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8539 Operand((uint32_t)C_008F14_NUM_FORMAT));
8540 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8541
8542 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8543 Format::PSEUDO, resource.size(), 1)};
8544 for (unsigned i = 0; i < resource.size(); i++)
8545 vec->operands[i] = Operand(desc[i]);
8546 resource = bld.tmp(resource.regClass());
8547 vec->definitions[0] = Definition(resource);
8548 ctx->block->instructions.emplace_back(std::move(vec));
8549
8550 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8551 new_coords[0], coords[0], tg4_compare_cube_wa64);
8552 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8553 new_coords[1], coords[1], tg4_compare_cube_wa64);
8554 }
8555 coords[0] = new_coords[0];
8556 coords[1] = new_coords[1];
8557 }
8558
8559 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8560 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8561
8562 assert(coords.size() == 1);
8563 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8564 aco_opcode op;
8565 switch (last_bit) {
8566 case 1:
8567 op = aco_opcode::buffer_load_format_x; break;
8568 case 2:
8569 op = aco_opcode::buffer_load_format_xy; break;
8570 case 3:
8571 op = aco_opcode::buffer_load_format_xyz; break;
8572 case 4:
8573 op = aco_opcode::buffer_load_format_xyzw; break;
8574 default:
8575 unreachable("Tex instruction loads more than 4 components.");
8576 }
8577
8578 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8579 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8580 tmp_dst = dst;
8581 else
8582 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8583
8584 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8585 mubuf->operands[0] = Operand(resource);
8586 mubuf->operands[1] = Operand(coords[0]);
8587 mubuf->operands[2] = Operand((uint32_t) 0);
8588 mubuf->definitions[0] = Definition(tmp_dst);
8589 mubuf->idxen = true;
8590 mubuf->can_reorder = true;
8591 ctx->block->instructions.emplace_back(std::move(mubuf));
8592
8593 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8594 return;
8595 }
8596
8597 /* gather MIMG address components */
8598 std::vector<Temp> args;
8599 if (has_offset)
8600 args.emplace_back(offset);
8601 if (has_bias)
8602 args.emplace_back(bias);
8603 if (has_compare)
8604 args.emplace_back(compare);
8605 if (has_derivs)
8606 args.insert(args.end(), derivs.begin(), derivs.end());
8607
8608 args.insert(args.end(), coords.begin(), coords.end());
8609 if (has_sample_index)
8610 args.emplace_back(sample_index);
8611 if (has_lod)
8612 args.emplace_back(lod);
8613
8614 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8615 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8616 vec->definitions[0] = Definition(arg);
8617 for (unsigned i = 0; i < args.size(); i++)
8618 vec->operands[i] = Operand(args[i]);
8619 ctx->block->instructions.emplace_back(std::move(vec));
8620
8621
8622 if (instr->op == nir_texop_txf ||
8623 instr->op == nir_texop_txf_ms ||
8624 instr->op == nir_texop_samples_identical ||
8625 instr->op == nir_texop_fragment_fetch ||
8626 instr->op == nir_texop_fragment_mask_fetch) {
8627 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;
8628 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8629 tex->operands[0] = Operand(resource);
8630 tex->operands[1] = Operand(s4); /* no sampler */
8631 tex->operands[2] = Operand(arg);
8632 tex->dim = dim;
8633 tex->dmask = dmask;
8634 tex->unrm = true;
8635 tex->da = da;
8636 tex->definitions[0] = Definition(tmp_dst);
8637 tex->can_reorder = true;
8638 ctx->block->instructions.emplace_back(std::move(tex));
8639
8640 if (instr->op == nir_texop_samples_identical) {
8641 assert(dmask == 1 && dst.regClass() == v1);
8642 assert(dst.id() != tmp_dst.id());
8643
8644 Temp tmp = bld.tmp(bld.lm);
8645 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8646 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8647
8648 } else {
8649 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8650 }
8651 return;
8652 }
8653
8654 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8655 aco_opcode opcode = aco_opcode::image_sample;
8656 if (has_offset) { /* image_sample_*_o */
8657 if (has_compare) {
8658 opcode = aco_opcode::image_sample_c_o;
8659 if (has_derivs)
8660 opcode = aco_opcode::image_sample_c_d_o;
8661 if (has_bias)
8662 opcode = aco_opcode::image_sample_c_b_o;
8663 if (level_zero)
8664 opcode = aco_opcode::image_sample_c_lz_o;
8665 if (has_lod)
8666 opcode = aco_opcode::image_sample_c_l_o;
8667 } else {
8668 opcode = aco_opcode::image_sample_o;
8669 if (has_derivs)
8670 opcode = aco_opcode::image_sample_d_o;
8671 if (has_bias)
8672 opcode = aco_opcode::image_sample_b_o;
8673 if (level_zero)
8674 opcode = aco_opcode::image_sample_lz_o;
8675 if (has_lod)
8676 opcode = aco_opcode::image_sample_l_o;
8677 }
8678 } else { /* no offset */
8679 if (has_compare) {
8680 opcode = aco_opcode::image_sample_c;
8681 if (has_derivs)
8682 opcode = aco_opcode::image_sample_c_d;
8683 if (has_bias)
8684 opcode = aco_opcode::image_sample_c_b;
8685 if (level_zero)
8686 opcode = aco_opcode::image_sample_c_lz;
8687 if (has_lod)
8688 opcode = aco_opcode::image_sample_c_l;
8689 } else {
8690 opcode = aco_opcode::image_sample;
8691 if (has_derivs)
8692 opcode = aco_opcode::image_sample_d;
8693 if (has_bias)
8694 opcode = aco_opcode::image_sample_b;
8695 if (level_zero)
8696 opcode = aco_opcode::image_sample_lz;
8697 if (has_lod)
8698 opcode = aco_opcode::image_sample_l;
8699 }
8700 }
8701
8702 if (instr->op == nir_texop_tg4) {
8703 if (has_offset) {
8704 opcode = aco_opcode::image_gather4_lz_o;
8705 if (has_compare)
8706 opcode = aco_opcode::image_gather4_c_lz_o;
8707 } else {
8708 opcode = aco_opcode::image_gather4_lz;
8709 if (has_compare)
8710 opcode = aco_opcode::image_gather4_c_lz;
8711 }
8712 } else if (instr->op == nir_texop_lod) {
8713 opcode = aco_opcode::image_get_lod;
8714 }
8715
8716 /* we don't need the bias, sample index, compare value or offset to be
8717 * computed in WQM but if the p_create_vector copies the coordinates, then it
8718 * needs to be in WQM */
8719 if (ctx->stage == fragment_fs &&
8720 !has_derivs && !has_lod && !level_zero &&
8721 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8722 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8723 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8724
8725 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8726 tex->operands[0] = Operand(resource);
8727 tex->operands[1] = Operand(sampler);
8728 tex->operands[2] = Operand(arg);
8729 tex->dim = dim;
8730 tex->dmask = dmask;
8731 tex->da = da;
8732 tex->definitions[0] = Definition(tmp_dst);
8733 tex->can_reorder = true;
8734 ctx->block->instructions.emplace_back(std::move(tex));
8735
8736 if (tg4_integer_cube_workaround) {
8737 assert(tmp_dst.id() != dst.id());
8738 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8739
8740 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8741 Temp val[4];
8742 for (unsigned i = 0; i < dst.size(); i++) {
8743 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8744 Temp cvt_val;
8745 if (stype == GLSL_TYPE_UINT)
8746 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8747 else
8748 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8749 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8750 }
8751 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8752 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8753 val[0], val[1], val[2], val[3]);
8754 }
8755 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8756 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8757
8758 }
8759
8760
8761 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8762 {
8763 Temp tmp = get_ssa_temp(ctx, ssa);
8764 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8765 return Operand(tmp.regClass());
8766 else
8767 return Operand(tmp);
8768 }
8769
8770 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8771 {
8772 aco_ptr<Pseudo_instruction> phi;
8773 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8774 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8775
8776 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8777 logical |= ctx->block->kind & block_kind_merge;
8778 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8779
8780 /* we want a sorted list of sources, since the predecessor list is also sorted */
8781 std::map<unsigned, nir_ssa_def*> phi_src;
8782 nir_foreach_phi_src(src, instr)
8783 phi_src[src->pred->index] = src->src.ssa;
8784
8785 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8786 unsigned num_operands = 0;
8787 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8788 unsigned num_defined = 0;
8789 unsigned cur_pred_idx = 0;
8790 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8791 if (cur_pred_idx < preds.size()) {
8792 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8793 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8794 unsigned skipped = 0;
8795 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8796 skipped++;
8797 if (cur_pred_idx + skipped < preds.size()) {
8798 for (unsigned i = 0; i < skipped; i++)
8799 operands[num_operands++] = Operand(dst.regClass());
8800 cur_pred_idx += skipped;
8801 } else {
8802 continue;
8803 }
8804 }
8805 /* Handle missing predecessors at the end. This shouldn't happen with loop
8806 * headers and we can't ignore these sources for loop header phis. */
8807 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8808 continue;
8809 cur_pred_idx++;
8810 Operand op = get_phi_operand(ctx, src.second);
8811 operands[num_operands++] = op;
8812 num_defined += !op.isUndefined();
8813 }
8814 /* handle block_kind_continue_or_break at loop exit blocks */
8815 while (cur_pred_idx++ < preds.size())
8816 operands[num_operands++] = Operand(dst.regClass());
8817
8818 /* If the loop ends with a break, still add a linear continue edge in case
8819 * that break is divergent or continue_or_break is used. We'll either remove
8820 * this operand later in visit_loop() if it's not necessary or replace the
8821 * undef with something correct. */
8822 if (!logical && ctx->block->kind & block_kind_loop_header) {
8823 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8824 nir_block *last = nir_loop_last_block(loop);
8825 if (last->successors[0] != instr->instr.block)
8826 operands[num_operands++] = Operand(RegClass());
8827 }
8828
8829 if (num_defined == 0) {
8830 Builder bld(ctx->program, ctx->block);
8831 if (dst.regClass() == s1) {
8832 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8833 } else if (dst.regClass() == v1) {
8834 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8835 } else {
8836 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8837 for (unsigned i = 0; i < dst.size(); i++)
8838 vec->operands[i] = Operand(0u);
8839 vec->definitions[0] = Definition(dst);
8840 ctx->block->instructions.emplace_back(std::move(vec));
8841 }
8842 return;
8843 }
8844
8845 /* we can use a linear phi in some cases if one src is undef */
8846 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8847 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8848
8849 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8850 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8851 assert(invert->kind & block_kind_invert);
8852
8853 unsigned then_block = invert->linear_preds[0];
8854
8855 Block* insert_block = NULL;
8856 for (unsigned i = 0; i < num_operands; i++) {
8857 Operand op = operands[i];
8858 if (op.isUndefined())
8859 continue;
8860 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8861 phi->operands[0] = op;
8862 break;
8863 }
8864 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8865 phi->operands[1] = Operand(dst.regClass());
8866 phi->definitions[0] = Definition(dst);
8867 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8868 return;
8869 }
8870
8871 /* try to scalarize vector phis */
8872 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8873 // TODO: scalarize linear phis on divergent ifs
8874 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8875 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8876 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8877 Operand src = operands[i];
8878 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8879 can_scalarize = false;
8880 }
8881 if (can_scalarize) {
8882 unsigned num_components = instr->dest.ssa.num_components;
8883 assert(dst.size() % num_components == 0);
8884 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8885
8886 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8887 for (unsigned k = 0; k < num_components; k++) {
8888 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8889 for (unsigned i = 0; i < num_operands; i++) {
8890 Operand src = operands[i];
8891 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8892 }
8893 Temp phi_dst = {ctx->program->allocateId(), rc};
8894 phi->definitions[0] = Definition(phi_dst);
8895 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8896 new_vec[k] = phi_dst;
8897 vec->operands[k] = Operand(phi_dst);
8898 }
8899 vec->definitions[0] = Definition(dst);
8900 ctx->block->instructions.emplace_back(std::move(vec));
8901 ctx->allocated_vec.emplace(dst.id(), new_vec);
8902 return;
8903 }
8904 }
8905
8906 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8907 for (unsigned i = 0; i < num_operands; i++)
8908 phi->operands[i] = operands[i];
8909 phi->definitions[0] = Definition(dst);
8910 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8911 }
8912
8913
8914 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8915 {
8916 Temp dst = get_ssa_temp(ctx, &instr->def);
8917
8918 assert(dst.type() == RegType::sgpr);
8919
8920 if (dst.size() == 1) {
8921 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8922 } else {
8923 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8924 for (unsigned i = 0; i < dst.size(); i++)
8925 vec->operands[i] = Operand(0u);
8926 vec->definitions[0] = Definition(dst);
8927 ctx->block->instructions.emplace_back(std::move(vec));
8928 }
8929 }
8930
8931 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8932 {
8933 Builder bld(ctx->program, ctx->block);
8934 Block *logical_target;
8935 append_logical_end(ctx->block);
8936 unsigned idx = ctx->block->index;
8937
8938 switch (instr->type) {
8939 case nir_jump_break:
8940 logical_target = ctx->cf_info.parent_loop.exit;
8941 add_logical_edge(idx, logical_target);
8942 ctx->block->kind |= block_kind_break;
8943
8944 if (!ctx->cf_info.parent_if.is_divergent &&
8945 !ctx->cf_info.parent_loop.has_divergent_continue) {
8946 /* uniform break - directly jump out of the loop */
8947 ctx->block->kind |= block_kind_uniform;
8948 ctx->cf_info.has_branch = true;
8949 bld.branch(aco_opcode::p_branch);
8950 add_linear_edge(idx, logical_target);
8951 return;
8952 }
8953 ctx->cf_info.parent_loop.has_divergent_branch = true;
8954 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8955 break;
8956 case nir_jump_continue:
8957 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8958 add_logical_edge(idx, logical_target);
8959 ctx->block->kind |= block_kind_continue;
8960
8961 if (ctx->cf_info.parent_if.is_divergent) {
8962 /* for potential uniform breaks after this continue,
8963 we must ensure that they are handled correctly */
8964 ctx->cf_info.parent_loop.has_divergent_continue = true;
8965 ctx->cf_info.parent_loop.has_divergent_branch = true;
8966 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8967 } else {
8968 /* uniform continue - directly jump to the loop header */
8969 ctx->block->kind |= block_kind_uniform;
8970 ctx->cf_info.has_branch = true;
8971 bld.branch(aco_opcode::p_branch);
8972 add_linear_edge(idx, logical_target);
8973 return;
8974 }
8975 break;
8976 default:
8977 fprintf(stderr, "Unknown NIR jump instr: ");
8978 nir_print_instr(&instr->instr, stderr);
8979 fprintf(stderr, "\n");
8980 abort();
8981 }
8982
8983 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
8984 ctx->cf_info.exec_potentially_empty_break = true;
8985 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
8986 }
8987
8988 /* remove critical edges from linear CFG */
8989 bld.branch(aco_opcode::p_branch);
8990 Block* break_block = ctx->program->create_and_insert_block();
8991 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8992 break_block->kind |= block_kind_uniform;
8993 add_linear_edge(idx, break_block);
8994 /* the loop_header pointer might be invalidated by this point */
8995 if (instr->type == nir_jump_continue)
8996 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8997 add_linear_edge(break_block->index, logical_target);
8998 bld.reset(break_block);
8999 bld.branch(aco_opcode::p_branch);
9000
9001 Block* continue_block = ctx->program->create_and_insert_block();
9002 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9003 add_linear_edge(idx, continue_block);
9004 append_logical_start(continue_block);
9005 ctx->block = continue_block;
9006 return;
9007 }
9008
9009 void visit_block(isel_context *ctx, nir_block *block)
9010 {
9011 nir_foreach_instr(instr, block) {
9012 switch (instr->type) {
9013 case nir_instr_type_alu:
9014 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9015 break;
9016 case nir_instr_type_load_const:
9017 visit_load_const(ctx, nir_instr_as_load_const(instr));
9018 break;
9019 case nir_instr_type_intrinsic:
9020 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9021 break;
9022 case nir_instr_type_tex:
9023 visit_tex(ctx, nir_instr_as_tex(instr));
9024 break;
9025 case nir_instr_type_phi:
9026 visit_phi(ctx, nir_instr_as_phi(instr));
9027 break;
9028 case nir_instr_type_ssa_undef:
9029 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9030 break;
9031 case nir_instr_type_deref:
9032 break;
9033 case nir_instr_type_jump:
9034 visit_jump(ctx, nir_instr_as_jump(instr));
9035 break;
9036 default:
9037 fprintf(stderr, "Unknown NIR instr type: ");
9038 nir_print_instr(instr, stderr);
9039 fprintf(stderr, "\n");
9040 //abort();
9041 }
9042 }
9043
9044 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9045 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9046 }
9047
9048
9049
9050 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9051 aco_ptr<Instruction>& header_phi, Operand *vals)
9052 {
9053 vals[0] = Operand(header_phi->definitions[0].getTemp());
9054 RegClass rc = vals[0].regClass();
9055
9056 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9057
9058 unsigned next_pred = 1;
9059
9060 for (unsigned idx = first + 1; idx <= last; idx++) {
9061 Block& block = ctx->program->blocks[idx];
9062 if (block.loop_nest_depth != loop_nest_depth) {
9063 vals[idx - first] = vals[idx - 1 - first];
9064 continue;
9065 }
9066
9067 if (block.kind & block_kind_continue) {
9068 vals[idx - first] = header_phi->operands[next_pred];
9069 next_pred++;
9070 continue;
9071 }
9072
9073 bool all_same = true;
9074 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9075 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9076
9077 Operand val;
9078 if (all_same) {
9079 val = vals[block.linear_preds[0] - first];
9080 } else {
9081 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9082 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9083 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9084 phi->operands[i] = vals[block.linear_preds[i] - first];
9085 val = Operand(Temp(ctx->program->allocateId(), rc));
9086 phi->definitions[0] = Definition(val.getTemp());
9087 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9088 }
9089 vals[idx - first] = val;
9090 }
9091
9092 return vals[last - first];
9093 }
9094
9095 static void visit_loop(isel_context *ctx, nir_loop *loop)
9096 {
9097 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9098 append_logical_end(ctx->block);
9099 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9100 Builder bld(ctx->program, ctx->block);
9101 bld.branch(aco_opcode::p_branch);
9102 unsigned loop_preheader_idx = ctx->block->index;
9103
9104 Block loop_exit = Block();
9105 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9106 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9107
9108 Block* loop_header = ctx->program->create_and_insert_block();
9109 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9110 loop_header->kind |= block_kind_loop_header;
9111 add_edge(loop_preheader_idx, loop_header);
9112 ctx->block = loop_header;
9113
9114 /* emit loop body */
9115 unsigned loop_header_idx = loop_header->index;
9116 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9117 append_logical_start(ctx->block);
9118 bool unreachable = visit_cf_list(ctx, &loop->body);
9119
9120 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9121 if (!ctx->cf_info.has_branch) {
9122 append_logical_end(ctx->block);
9123 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9124 /* Discards can result in code running with an empty exec mask.
9125 * This would result in divergent breaks not ever being taken. As a
9126 * workaround, break the loop when the loop mask is empty instead of
9127 * always continuing. */
9128 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9129 unsigned block_idx = ctx->block->index;
9130
9131 /* create helper blocks to avoid critical edges */
9132 Block *break_block = ctx->program->create_and_insert_block();
9133 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9134 break_block->kind = block_kind_uniform;
9135 bld.reset(break_block);
9136 bld.branch(aco_opcode::p_branch);
9137 add_linear_edge(block_idx, break_block);
9138 add_linear_edge(break_block->index, &loop_exit);
9139
9140 Block *continue_block = ctx->program->create_and_insert_block();
9141 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9142 continue_block->kind = block_kind_uniform;
9143 bld.reset(continue_block);
9144 bld.branch(aco_opcode::p_branch);
9145 add_linear_edge(block_idx, continue_block);
9146 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9147
9148 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9149 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9150 ctx->block = &ctx->program->blocks[block_idx];
9151 } else {
9152 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9153 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9154 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9155 else
9156 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9157 }
9158
9159 bld.reset(ctx->block);
9160 bld.branch(aco_opcode::p_branch);
9161 }
9162
9163 /* Fixup phis in loop header from unreachable blocks.
9164 * has_branch/has_divergent_branch also indicates if the loop ends with a
9165 * break/continue instruction, but we don't emit those if unreachable=true */
9166 if (unreachable) {
9167 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9168 bool linear = ctx->cf_info.has_branch;
9169 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9170 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9171 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9172 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9173 /* the last operand should be the one that needs to be removed */
9174 instr->operands.pop_back();
9175 } else if (!is_phi(instr)) {
9176 break;
9177 }
9178 }
9179 }
9180
9181 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9182 * and the previous one shouldn't both happen at once because a break in the
9183 * merge block would get CSE'd */
9184 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9185 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9186 Operand vals[num_vals];
9187 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9188 if (instr->opcode == aco_opcode::p_linear_phi) {
9189 if (ctx->cf_info.has_branch)
9190 instr->operands.pop_back();
9191 else
9192 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9193 } else if (!is_phi(instr)) {
9194 break;
9195 }
9196 }
9197 }
9198
9199 ctx->cf_info.has_branch = false;
9200
9201 // TODO: if the loop has not a single exit, we must add one °°
9202 /* emit loop successor block */
9203 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9204 append_logical_start(ctx->block);
9205
9206 #if 0
9207 // TODO: check if it is beneficial to not branch on continues
9208 /* trim linear phis in loop header */
9209 for (auto&& instr : loop_entry->instructions) {
9210 if (instr->opcode == aco_opcode::p_linear_phi) {
9211 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9212 new_phi->definitions[0] = instr->definitions[0];
9213 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9214 new_phi->operands[i] = instr->operands[i];
9215 /* check that the remaining operands are all the same */
9216 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9217 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9218 instr.swap(new_phi);
9219 } else if (instr->opcode == aco_opcode::p_phi) {
9220 continue;
9221 } else {
9222 break;
9223 }
9224 }
9225 #endif
9226 }
9227
9228 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9229 {
9230 ic->cond = cond;
9231
9232 append_logical_end(ctx->block);
9233 ctx->block->kind |= block_kind_branch;
9234
9235 /* branch to linear then block */
9236 assert(cond.regClass() == ctx->program->lane_mask);
9237 aco_ptr<Pseudo_branch_instruction> branch;
9238 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9239 branch->operands[0] = Operand(cond);
9240 ctx->block->instructions.push_back(std::move(branch));
9241
9242 ic->BB_if_idx = ctx->block->index;
9243 ic->BB_invert = Block();
9244 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9245 /* Invert blocks are intentionally not marked as top level because they
9246 * are not part of the logical cfg. */
9247 ic->BB_invert.kind |= block_kind_invert;
9248 ic->BB_endif = Block();
9249 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9250 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9251
9252 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9253 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9254 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9255 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9256 ctx->cf_info.parent_if.is_divergent = true;
9257
9258 /* divergent branches use cbranch_execz */
9259 ctx->cf_info.exec_potentially_empty_discard = false;
9260 ctx->cf_info.exec_potentially_empty_break = false;
9261 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9262
9263 /** emit logical then block */
9264 Block* BB_then_logical = ctx->program->create_and_insert_block();
9265 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9266 add_edge(ic->BB_if_idx, BB_then_logical);
9267 ctx->block = BB_then_logical;
9268 append_logical_start(BB_then_logical);
9269 }
9270
9271 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9272 {
9273 Block *BB_then_logical = ctx->block;
9274 append_logical_end(BB_then_logical);
9275 /* branch from logical then block to invert block */
9276 aco_ptr<Pseudo_branch_instruction> branch;
9277 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9278 BB_then_logical->instructions.emplace_back(std::move(branch));
9279 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9280 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9281 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9282 BB_then_logical->kind |= block_kind_uniform;
9283 assert(!ctx->cf_info.has_branch);
9284 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9285 ctx->cf_info.parent_loop.has_divergent_branch = false;
9286
9287 /** emit linear then block */
9288 Block* BB_then_linear = ctx->program->create_and_insert_block();
9289 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9290 BB_then_linear->kind |= block_kind_uniform;
9291 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9292 /* branch from linear then block to invert block */
9293 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9294 BB_then_linear->instructions.emplace_back(std::move(branch));
9295 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9296
9297 /** emit invert merge block */
9298 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9299 ic->invert_idx = ctx->block->index;
9300
9301 /* branch to linear else block (skip else) */
9302 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9303 branch->operands[0] = Operand(ic->cond);
9304 ctx->block->instructions.push_back(std::move(branch));
9305
9306 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9307 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9308 ic->exec_potentially_empty_break_depth_old =
9309 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9310 /* divergent branches use cbranch_execz */
9311 ctx->cf_info.exec_potentially_empty_discard = false;
9312 ctx->cf_info.exec_potentially_empty_break = false;
9313 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9314
9315 /** emit logical else block */
9316 Block* BB_else_logical = ctx->program->create_and_insert_block();
9317 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9318 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9319 add_linear_edge(ic->invert_idx, BB_else_logical);
9320 ctx->block = BB_else_logical;
9321 append_logical_start(BB_else_logical);
9322 }
9323
9324 static void end_divergent_if(isel_context *ctx, if_context *ic)
9325 {
9326 Block *BB_else_logical = ctx->block;
9327 append_logical_end(BB_else_logical);
9328
9329 /* branch from logical else block to endif block */
9330 aco_ptr<Pseudo_branch_instruction> branch;
9331 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9332 BB_else_logical->instructions.emplace_back(std::move(branch));
9333 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9334 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9335 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9336 BB_else_logical->kind |= block_kind_uniform;
9337
9338 assert(!ctx->cf_info.has_branch);
9339 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9340
9341
9342 /** emit linear else block */
9343 Block* BB_else_linear = ctx->program->create_and_insert_block();
9344 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9345 BB_else_linear->kind |= block_kind_uniform;
9346 add_linear_edge(ic->invert_idx, BB_else_linear);
9347
9348 /* branch from linear else block to endif block */
9349 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9350 BB_else_linear->instructions.emplace_back(std::move(branch));
9351 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9352
9353
9354 /** emit endif merge block */
9355 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9356 append_logical_start(ctx->block);
9357
9358
9359 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9360 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9361 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9362 ctx->cf_info.exec_potentially_empty_break_depth =
9363 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9364 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9365 !ctx->cf_info.parent_if.is_divergent) {
9366 ctx->cf_info.exec_potentially_empty_break = false;
9367 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9368 }
9369 /* uniform control flow never has an empty exec-mask */
9370 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9371 ctx->cf_info.exec_potentially_empty_discard = false;
9372 ctx->cf_info.exec_potentially_empty_break = false;
9373 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9374 }
9375 }
9376
9377 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9378 {
9379 assert(cond.regClass() == s1);
9380
9381 append_logical_end(ctx->block);
9382 ctx->block->kind |= block_kind_uniform;
9383
9384 aco_ptr<Pseudo_branch_instruction> branch;
9385 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9386 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9387 branch->operands[0] = Operand(cond);
9388 branch->operands[0].setFixed(scc);
9389 ctx->block->instructions.emplace_back(std::move(branch));
9390
9391 ic->BB_if_idx = ctx->block->index;
9392 ic->BB_endif = Block();
9393 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9394 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9395
9396 ctx->cf_info.has_branch = false;
9397 ctx->cf_info.parent_loop.has_divergent_branch = false;
9398
9399 /** emit then block */
9400 Block* BB_then = ctx->program->create_and_insert_block();
9401 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9402 add_edge(ic->BB_if_idx, BB_then);
9403 append_logical_start(BB_then);
9404 ctx->block = BB_then;
9405 }
9406
9407 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9408 {
9409 Block *BB_then = ctx->block;
9410
9411 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9412 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9413
9414 if (!ic->uniform_has_then_branch) {
9415 append_logical_end(BB_then);
9416 /* branch from then block to endif block */
9417 aco_ptr<Pseudo_branch_instruction> branch;
9418 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9419 BB_then->instructions.emplace_back(std::move(branch));
9420 add_linear_edge(BB_then->index, &ic->BB_endif);
9421 if (!ic->then_branch_divergent)
9422 add_logical_edge(BB_then->index, &ic->BB_endif);
9423 BB_then->kind |= block_kind_uniform;
9424 }
9425
9426 ctx->cf_info.has_branch = false;
9427 ctx->cf_info.parent_loop.has_divergent_branch = false;
9428
9429 /** emit else block */
9430 Block* BB_else = ctx->program->create_and_insert_block();
9431 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9432 add_edge(ic->BB_if_idx, BB_else);
9433 append_logical_start(BB_else);
9434 ctx->block = BB_else;
9435 }
9436
9437 static void end_uniform_if(isel_context *ctx, if_context *ic)
9438 {
9439 Block *BB_else = ctx->block;
9440
9441 if (!ctx->cf_info.has_branch) {
9442 append_logical_end(BB_else);
9443 /* branch from then block to endif block */
9444 aco_ptr<Pseudo_branch_instruction> branch;
9445 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9446 BB_else->instructions.emplace_back(std::move(branch));
9447 add_linear_edge(BB_else->index, &ic->BB_endif);
9448 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9449 add_logical_edge(BB_else->index, &ic->BB_endif);
9450 BB_else->kind |= block_kind_uniform;
9451 }
9452
9453 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9454 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9455
9456 /** emit endif merge block */
9457 if (!ctx->cf_info.has_branch) {
9458 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9459 append_logical_start(ctx->block);
9460 }
9461 }
9462
9463 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9464 {
9465 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9466 Builder bld(ctx->program, ctx->block);
9467 aco_ptr<Pseudo_branch_instruction> branch;
9468 if_context ic;
9469
9470 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9471 /**
9472 * Uniform conditionals are represented in the following way*) :
9473 *
9474 * The linear and logical CFG:
9475 * BB_IF
9476 * / \
9477 * BB_THEN (logical) BB_ELSE (logical)
9478 * \ /
9479 * BB_ENDIF
9480 *
9481 * *) Exceptions may be due to break and continue statements within loops
9482 * If a break/continue happens within uniform control flow, it branches
9483 * to the loop exit/entry block. Otherwise, it branches to the next
9484 * merge block.
9485 **/
9486
9487 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9488 assert(cond.regClass() == ctx->program->lane_mask);
9489 cond = bool_to_scalar_condition(ctx, cond);
9490
9491 begin_uniform_if_then(ctx, &ic, cond);
9492 visit_cf_list(ctx, &if_stmt->then_list);
9493
9494 begin_uniform_if_else(ctx, &ic);
9495 visit_cf_list(ctx, &if_stmt->else_list);
9496
9497 end_uniform_if(ctx, &ic);
9498 } else { /* non-uniform condition */
9499 /**
9500 * To maintain a logical and linear CFG without critical edges,
9501 * non-uniform conditionals are represented in the following way*) :
9502 *
9503 * The linear CFG:
9504 * BB_IF
9505 * / \
9506 * BB_THEN (logical) BB_THEN (linear)
9507 * \ /
9508 * BB_INVERT (linear)
9509 * / \
9510 * BB_ELSE (logical) BB_ELSE (linear)
9511 * \ /
9512 * BB_ENDIF
9513 *
9514 * The logical CFG:
9515 * BB_IF
9516 * / \
9517 * BB_THEN (logical) BB_ELSE (logical)
9518 * \ /
9519 * BB_ENDIF
9520 *
9521 * *) Exceptions may be due to break and continue statements within loops
9522 **/
9523
9524 begin_divergent_if_then(ctx, &ic, cond);
9525 visit_cf_list(ctx, &if_stmt->then_list);
9526
9527 begin_divergent_if_else(ctx, &ic);
9528 visit_cf_list(ctx, &if_stmt->else_list);
9529
9530 end_divergent_if(ctx, &ic);
9531 }
9532
9533 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9534 }
9535
9536 static bool visit_cf_list(isel_context *ctx,
9537 struct exec_list *list)
9538 {
9539 foreach_list_typed(nir_cf_node, node, node, list) {
9540 switch (node->type) {
9541 case nir_cf_node_block:
9542 visit_block(ctx, nir_cf_node_as_block(node));
9543 break;
9544 case nir_cf_node_if:
9545 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9546 return true;
9547 break;
9548 case nir_cf_node_loop:
9549 visit_loop(ctx, nir_cf_node_as_loop(node));
9550 break;
9551 default:
9552 unreachable("unimplemented cf list type");
9553 }
9554 }
9555 return false;
9556 }
9557
9558 static void create_null_export(isel_context *ctx)
9559 {
9560 /* Some shader stages always need to have exports.
9561 * So when there is none, we need to add a null export.
9562 */
9563
9564 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9565 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9566 Builder bld(ctx->program, ctx->block);
9567 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9568 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9569 }
9570
9571 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9572 {
9573 assert(ctx->stage == vertex_vs ||
9574 ctx->stage == tess_eval_vs ||
9575 ctx->stage == gs_copy_vs ||
9576 ctx->stage == ngg_vertex_gs ||
9577 ctx->stage == ngg_tess_eval_gs);
9578
9579 int offset = (ctx->stage & sw_tes)
9580 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9581 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9582 uint64_t mask = ctx->outputs.mask[slot];
9583 if (!is_pos && !mask)
9584 return false;
9585 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9586 return false;
9587 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9588 exp->enabled_mask = mask;
9589 for (unsigned i = 0; i < 4; ++i) {
9590 if (mask & (1 << i))
9591 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9592 else
9593 exp->operands[i] = Operand(v1);
9594 }
9595 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9596 * Setting valid_mask=1 prevents it and has no other effect.
9597 */
9598 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9599 exp->done = false;
9600 exp->compressed = false;
9601 if (is_pos)
9602 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9603 else
9604 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9605 ctx->block->instructions.emplace_back(std::move(exp));
9606
9607 return true;
9608 }
9609
9610 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9611 {
9612 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9613 exp->enabled_mask = 0;
9614 for (unsigned i = 0; i < 4; ++i)
9615 exp->operands[i] = Operand(v1);
9616 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9617 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9618 exp->enabled_mask |= 0x1;
9619 }
9620 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9621 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9622 exp->enabled_mask |= 0x4;
9623 }
9624 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9625 if (ctx->options->chip_class < GFX9) {
9626 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9627 exp->enabled_mask |= 0x8;
9628 } else {
9629 Builder bld(ctx->program, ctx->block);
9630
9631 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9632 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9633 if (exp->operands[2].isTemp())
9634 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9635
9636 exp->operands[2] = Operand(out);
9637 exp->enabled_mask |= 0x4;
9638 }
9639 }
9640 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9641 exp->done = false;
9642 exp->compressed = false;
9643 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9644 ctx->block->instructions.emplace_back(std::move(exp));
9645 }
9646
9647 static void create_export_phis(isel_context *ctx)
9648 {
9649 /* Used when exports are needed, but the output temps are defined in a preceding block.
9650 * This function will set up phis in order to access the outputs in the next block.
9651 */
9652
9653 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9654 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9655 ctx->block->instructions.pop_back();
9656
9657 Builder bld(ctx->program, ctx->block);
9658
9659 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9660 uint64_t mask = ctx->outputs.mask[slot];
9661 for (unsigned i = 0; i < 4; ++i) {
9662 if (!(mask & (1 << i)))
9663 continue;
9664
9665 Temp old = ctx->outputs.temps[slot * 4 + i];
9666 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9667 ctx->outputs.temps[slot * 4 + i] = phi;
9668 }
9669 }
9670
9671 bld.insert(std::move(logical_start));
9672 }
9673
9674 static void create_vs_exports(isel_context *ctx)
9675 {
9676 assert(ctx->stage == vertex_vs ||
9677 ctx->stage == tess_eval_vs ||
9678 ctx->stage == gs_copy_vs ||
9679 ctx->stage == ngg_vertex_gs ||
9680 ctx->stage == ngg_tess_eval_gs);
9681
9682 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9683 ? &ctx->program->info->tes.outinfo
9684 : &ctx->program->info->vs.outinfo;
9685
9686 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9687 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9688 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9689 }
9690
9691 if (ctx->options->key.has_multiview_view_index) {
9692 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9693 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9694 }
9695
9696 /* the order these position exports are created is important */
9697 int next_pos = 0;
9698 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9699 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9700 export_vs_psiz_layer_viewport(ctx, &next_pos);
9701 exported_pos = true;
9702 }
9703 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9704 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9705 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9706 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9707
9708 if (ctx->export_clip_dists) {
9709 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9710 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9711 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9712 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9713 }
9714
9715 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9716 if (i < VARYING_SLOT_VAR0 &&
9717 i != VARYING_SLOT_LAYER &&
9718 i != VARYING_SLOT_PRIMITIVE_ID &&
9719 i != VARYING_SLOT_VIEWPORT)
9720 continue;
9721
9722 export_vs_varying(ctx, i, false, NULL);
9723 }
9724
9725 if (!exported_pos)
9726 create_null_export(ctx);
9727 }
9728
9729 static bool export_fs_mrt_z(isel_context *ctx)
9730 {
9731 Builder bld(ctx->program, ctx->block);
9732 unsigned enabled_channels = 0;
9733 bool compr = false;
9734 Operand values[4];
9735
9736 for (unsigned i = 0; i < 4; ++i) {
9737 values[i] = Operand(v1);
9738 }
9739
9740 /* Both stencil and sample mask only need 16-bits. */
9741 if (!ctx->program->info->ps.writes_z &&
9742 (ctx->program->info->ps.writes_stencil ||
9743 ctx->program->info->ps.writes_sample_mask)) {
9744 compr = true; /* COMPR flag */
9745
9746 if (ctx->program->info->ps.writes_stencil) {
9747 /* Stencil should be in X[23:16]. */
9748 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9749 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9750 enabled_channels |= 0x3;
9751 }
9752
9753 if (ctx->program->info->ps.writes_sample_mask) {
9754 /* SampleMask should be in Y[15:0]. */
9755 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9756 enabled_channels |= 0xc;
9757 }
9758 } else {
9759 if (ctx->program->info->ps.writes_z) {
9760 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9761 enabled_channels |= 0x1;
9762 }
9763
9764 if (ctx->program->info->ps.writes_stencil) {
9765 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9766 enabled_channels |= 0x2;
9767 }
9768
9769 if (ctx->program->info->ps.writes_sample_mask) {
9770 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9771 enabled_channels |= 0x4;
9772 }
9773 }
9774
9775 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9776 * writemask component.
9777 */
9778 if (ctx->options->chip_class == GFX6 &&
9779 ctx->options->family != CHIP_OLAND &&
9780 ctx->options->family != CHIP_HAINAN) {
9781 enabled_channels |= 0x1;
9782 }
9783
9784 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9785 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9786
9787 return true;
9788 }
9789
9790 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9791 {
9792 Builder bld(ctx->program, ctx->block);
9793 unsigned write_mask = ctx->outputs.mask[slot];
9794 Operand values[4];
9795
9796 for (unsigned i = 0; i < 4; ++i) {
9797 if (write_mask & (1 << i)) {
9798 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9799 } else {
9800 values[i] = Operand(v1);
9801 }
9802 }
9803
9804 unsigned target, col_format;
9805 unsigned enabled_channels = 0;
9806 aco_opcode compr_op = (aco_opcode)0;
9807
9808 slot -= FRAG_RESULT_DATA0;
9809 target = V_008DFC_SQ_EXP_MRT + slot;
9810 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9811
9812 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9813 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9814
9815 switch (col_format)
9816 {
9817 case V_028714_SPI_SHADER_ZERO:
9818 enabled_channels = 0; /* writemask */
9819 target = V_008DFC_SQ_EXP_NULL;
9820 break;
9821
9822 case V_028714_SPI_SHADER_32_R:
9823 enabled_channels = 1;
9824 break;
9825
9826 case V_028714_SPI_SHADER_32_GR:
9827 enabled_channels = 0x3;
9828 break;
9829
9830 case V_028714_SPI_SHADER_32_AR:
9831 if (ctx->options->chip_class >= GFX10) {
9832 /* Special case: on GFX10, the outputs are different for 32_AR */
9833 enabled_channels = 0x3;
9834 values[1] = values[3];
9835 values[3] = Operand(v1);
9836 } else {
9837 enabled_channels = 0x9;
9838 }
9839 break;
9840
9841 case V_028714_SPI_SHADER_FP16_ABGR:
9842 enabled_channels = 0x5;
9843 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9844 break;
9845
9846 case V_028714_SPI_SHADER_UNORM16_ABGR:
9847 enabled_channels = 0x5;
9848 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9849 break;
9850
9851 case V_028714_SPI_SHADER_SNORM16_ABGR:
9852 enabled_channels = 0x5;
9853 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9854 break;
9855
9856 case V_028714_SPI_SHADER_UINT16_ABGR: {
9857 enabled_channels = 0x5;
9858 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9859 if (is_int8 || is_int10) {
9860 /* clamp */
9861 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9862 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9863
9864 for (unsigned i = 0; i < 4; i++) {
9865 if ((write_mask >> i) & 1) {
9866 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9867 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9868 values[i]);
9869 }
9870 }
9871 }
9872 break;
9873 }
9874
9875 case V_028714_SPI_SHADER_SINT16_ABGR:
9876 enabled_channels = 0x5;
9877 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9878 if (is_int8 || is_int10) {
9879 /* clamp */
9880 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9881 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9882 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9883 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9884
9885 for (unsigned i = 0; i < 4; i++) {
9886 if ((write_mask >> i) & 1) {
9887 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9888 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9889 values[i]);
9890 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9891 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9892 values[i]);
9893 }
9894 }
9895 }
9896 break;
9897
9898 case V_028714_SPI_SHADER_32_ABGR:
9899 enabled_channels = 0xF;
9900 break;
9901
9902 default:
9903 break;
9904 }
9905
9906 if (target == V_008DFC_SQ_EXP_NULL)
9907 return false;
9908
9909 if ((bool) compr_op) {
9910 for (int i = 0; i < 2; i++) {
9911 /* check if at least one of the values to be compressed is enabled */
9912 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9913 if (enabled) {
9914 enabled_channels |= enabled << (i*2);
9915 values[i] = bld.vop3(compr_op, bld.def(v1),
9916 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9917 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9918 } else {
9919 values[i] = Operand(v1);
9920 }
9921 }
9922 values[2] = Operand(v1);
9923 values[3] = Operand(v1);
9924 } else {
9925 for (int i = 0; i < 4; i++)
9926 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
9927 }
9928
9929 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9930 enabled_channels, target, (bool) compr_op);
9931 return true;
9932 }
9933
9934 static void create_fs_exports(isel_context *ctx)
9935 {
9936 bool exported = false;
9937
9938 /* Export depth, stencil and sample mask. */
9939 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
9940 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
9941 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
9942 exported |= export_fs_mrt_z(ctx);
9943
9944 /* Export all color render targets. */
9945 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
9946 if (ctx->outputs.mask[i])
9947 exported |= export_fs_mrt_color(ctx, i);
9948
9949 if (!exported)
9950 create_null_export(ctx);
9951 }
9952
9953 static void write_tcs_tess_factors(isel_context *ctx)
9954 {
9955 unsigned outer_comps;
9956 unsigned inner_comps;
9957
9958 switch (ctx->args->options->key.tcs.primitive_mode) {
9959 case GL_ISOLINES:
9960 outer_comps = 2;
9961 inner_comps = 0;
9962 break;
9963 case GL_TRIANGLES:
9964 outer_comps = 3;
9965 inner_comps = 1;
9966 break;
9967 case GL_QUADS:
9968 outer_comps = 4;
9969 inner_comps = 2;
9970 break;
9971 default:
9972 return;
9973 }
9974
9975 Builder bld(ctx->program, ctx->block);
9976
9977 bld.barrier(aco_opcode::p_memory_barrier_shared);
9978 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
9979 bld.sopp(aco_opcode::s_barrier);
9980
9981 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
9982 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
9983
9984 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
9985 if_context ic_invocation_id_is_zero;
9986 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
9987 bld.reset(ctx->block);
9988
9989 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));
9990
9991 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
9992 unsigned stride = inner_comps + outer_comps;
9993 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
9994 Temp tf_inner_vec;
9995 Temp tf_outer_vec;
9996 Temp out[6];
9997 assert(stride <= (sizeof(out) / sizeof(Temp)));
9998
9999 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10000 // LINES reversal
10001 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10002 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10003 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10004 } else {
10005 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);
10006 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);
10007
10008 for (unsigned i = 0; i < outer_comps; ++i)
10009 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10010 for (unsigned i = 0; i < inner_comps; ++i)
10011 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10012 }
10013
10014 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10015 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10016 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10017 unsigned tf_const_offset = 0;
10018
10019 if (ctx->program->chip_class <= GFX8) {
10020 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);
10021 if_context ic_rel_patch_id_is_zero;
10022 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10023 bld.reset(ctx->block);
10024
10025 /* Store the dynamic HS control word. */
10026 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10027 bld.mubuf(aco_opcode::buffer_store_dword,
10028 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10029 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10030 /* disable_wqm */ false, /* glc */ true);
10031 tf_const_offset += 4;
10032
10033 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10034 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10035 bld.reset(ctx->block);
10036 }
10037
10038 assert(stride == 2 || stride == 4 || stride == 6);
10039 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10040 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10041
10042 /* Store to offchip for TES to read - only if TES reads them */
10043 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10044 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));
10045 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10046
10047 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10048 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);
10049
10050 if (likely(inner_comps)) {
10051 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10052 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);
10053 }
10054 }
10055
10056 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10057 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10058 }
10059
10060 static void emit_stream_output(isel_context *ctx,
10061 Temp const *so_buffers,
10062 Temp const *so_write_offset,
10063 const struct radv_stream_output *output)
10064 {
10065 unsigned num_comps = util_bitcount(output->component_mask);
10066 unsigned writemask = (1 << num_comps) - 1;
10067 unsigned loc = output->location;
10068 unsigned buf = output->buffer;
10069
10070 assert(num_comps && num_comps <= 4);
10071 if (!num_comps || num_comps > 4)
10072 return;
10073
10074 unsigned start = ffs(output->component_mask) - 1;
10075
10076 Temp out[4];
10077 bool all_undef = true;
10078 assert(ctx->stage & hw_vs);
10079 for (unsigned i = 0; i < num_comps; i++) {
10080 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10081 all_undef = all_undef && !out[i].id();
10082 }
10083 if (all_undef)
10084 return;
10085
10086 while (writemask) {
10087 int start, count;
10088 u_bit_scan_consecutive_range(&writemask, &start, &count);
10089 if (count == 3 && ctx->options->chip_class == GFX6) {
10090 /* GFX6 doesn't support storing vec3, split it. */
10091 writemask |= 1u << (start + 2);
10092 count = 2;
10093 }
10094
10095 unsigned offset = output->offset + start * 4;
10096
10097 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10098 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10099 for (int i = 0; i < count; ++i)
10100 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10101 vec->definitions[0] = Definition(write_data);
10102 ctx->block->instructions.emplace_back(std::move(vec));
10103
10104 aco_opcode opcode;
10105 switch (count) {
10106 case 1:
10107 opcode = aco_opcode::buffer_store_dword;
10108 break;
10109 case 2:
10110 opcode = aco_opcode::buffer_store_dwordx2;
10111 break;
10112 case 3:
10113 opcode = aco_opcode::buffer_store_dwordx3;
10114 break;
10115 case 4:
10116 opcode = aco_opcode::buffer_store_dwordx4;
10117 break;
10118 default:
10119 unreachable("Unsupported dword count.");
10120 }
10121
10122 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10123 store->operands[0] = Operand(so_buffers[buf]);
10124 store->operands[1] = Operand(so_write_offset[buf]);
10125 store->operands[2] = Operand((uint32_t) 0);
10126 store->operands[3] = Operand(write_data);
10127 if (offset > 4095) {
10128 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10129 Builder bld(ctx->program, ctx->block);
10130 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10131 } else {
10132 store->offset = offset;
10133 }
10134 store->offen = true;
10135 store->glc = true;
10136 store->dlc = false;
10137 store->slc = true;
10138 store->can_reorder = true;
10139 ctx->block->instructions.emplace_back(std::move(store));
10140 }
10141 }
10142
10143 static void emit_streamout(isel_context *ctx, unsigned stream)
10144 {
10145 Builder bld(ctx->program, ctx->block);
10146
10147 Temp so_buffers[4];
10148 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10149 for (unsigned i = 0; i < 4; i++) {
10150 unsigned stride = ctx->program->info->so.strides[i];
10151 if (!stride)
10152 continue;
10153
10154 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10155 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10156 }
10157
10158 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10159 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10160
10161 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10162
10163 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10164
10165 if_context ic;
10166 begin_divergent_if_then(ctx, &ic, can_emit);
10167
10168 bld.reset(ctx->block);
10169
10170 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10171
10172 Temp so_write_offset[4];
10173
10174 for (unsigned i = 0; i < 4; i++) {
10175 unsigned stride = ctx->program->info->so.strides[i];
10176 if (!stride)
10177 continue;
10178
10179 if (stride == 1) {
10180 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10181 get_arg(ctx, ctx->args->streamout_write_idx),
10182 get_arg(ctx, ctx->args->streamout_offset[i]));
10183 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10184
10185 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10186 } else {
10187 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10188 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10189 get_arg(ctx, ctx->args->streamout_offset[i]));
10190 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10191 }
10192 }
10193
10194 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10195 struct radv_stream_output *output =
10196 &ctx->program->info->so.outputs[i];
10197 if (stream != output->stream)
10198 continue;
10199
10200 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10201 }
10202
10203 begin_divergent_if_else(ctx, &ic);
10204 end_divergent_if(ctx, &ic);
10205 }
10206
10207 } /* end namespace */
10208
10209 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10210 {
10211 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10212 Builder bld(ctx->program, ctx->block);
10213 constexpr unsigned hs_idx = 1u;
10214 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10215 get_arg(ctx, ctx->args->merged_wave_info),
10216 Operand((8u << 16) | (hs_idx * 8u)));
10217 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10218
10219 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10220
10221 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10222 get_arg(ctx, ctx->args->rel_auto_id),
10223 get_arg(ctx, ctx->args->ac.instance_id),
10224 ls_has_nonzero_hs_threads);
10225 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10226 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10227 get_arg(ctx, ctx->args->rel_auto_id),
10228 ls_has_nonzero_hs_threads);
10229 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10230 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10231 get_arg(ctx, ctx->args->ac.vertex_id),
10232 ls_has_nonzero_hs_threads);
10233
10234 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10235 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10236 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10237 }
10238
10239 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10240 {
10241 /* Split all arguments except for the first (ring_offsets) and the last
10242 * (exec) so that the dead channels don't stay live throughout the program.
10243 */
10244 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10245 if (startpgm->definitions[i].regClass().size() > 1) {
10246 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10247 startpgm->definitions[i].regClass().size());
10248 }
10249 }
10250 }
10251
10252 void handle_bc_optimize(isel_context *ctx)
10253 {
10254 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10255 Builder bld(ctx->program, ctx->block);
10256 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10257 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10258 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10259 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10260 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10261 if (uses_center && uses_centroid) {
10262 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10263 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10264
10265 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10266 Temp new_coord[2];
10267 for (unsigned i = 0; i < 2; i++) {
10268 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10269 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10270 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10271 persp_centroid, persp_center, sel);
10272 }
10273 ctx->persp_centroid = bld.tmp(v2);
10274 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10275 Operand(new_coord[0]), Operand(new_coord[1]));
10276 emit_split_vector(ctx, ctx->persp_centroid, 2);
10277 }
10278
10279 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10280 Temp new_coord[2];
10281 for (unsigned i = 0; i < 2; i++) {
10282 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10283 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10284 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10285 linear_centroid, linear_center, sel);
10286 }
10287 ctx->linear_centroid = bld.tmp(v2);
10288 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10289 Operand(new_coord[0]), Operand(new_coord[1]));
10290 emit_split_vector(ctx, ctx->linear_centroid, 2);
10291 }
10292 }
10293 }
10294
10295 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10296 {
10297 Program *program = ctx->program;
10298
10299 unsigned float_controls = shader->info.float_controls_execution_mode;
10300
10301 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10302 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10303 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10304 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10305 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10306
10307 program->next_fp_mode.must_flush_denorms32 =
10308 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10309 program->next_fp_mode.must_flush_denorms16_64 =
10310 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10311 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10312
10313 program->next_fp_mode.care_about_round32 =
10314 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10315
10316 program->next_fp_mode.care_about_round16_64 =
10317 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10318 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10319
10320 /* default to preserving fp16 and fp64 denorms, since it's free */
10321 if (program->next_fp_mode.must_flush_denorms16_64)
10322 program->next_fp_mode.denorm16_64 = 0;
10323 else
10324 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10325
10326 /* preserving fp32 denorms is expensive, so only do it if asked */
10327 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10328 program->next_fp_mode.denorm32 = fp_denorm_keep;
10329 else
10330 program->next_fp_mode.denorm32 = 0;
10331
10332 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10333 program->next_fp_mode.round32 = fp_round_tz;
10334 else
10335 program->next_fp_mode.round32 = fp_round_ne;
10336
10337 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10338 program->next_fp_mode.round16_64 = fp_round_tz;
10339 else
10340 program->next_fp_mode.round16_64 = fp_round_ne;
10341
10342 ctx->block->fp_mode = program->next_fp_mode;
10343 }
10344
10345 void cleanup_cfg(Program *program)
10346 {
10347 /* create linear_succs/logical_succs */
10348 for (Block& BB : program->blocks) {
10349 for (unsigned idx : BB.linear_preds)
10350 program->blocks[idx].linear_succs.emplace_back(BB.index);
10351 for (unsigned idx : BB.logical_preds)
10352 program->blocks[idx].logical_succs.emplace_back(BB.index);
10353 }
10354 }
10355
10356 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10357 {
10358 Builder bld(ctx->program, ctx->block);
10359
10360 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10361 Temp count = i == 0
10362 ? get_arg(ctx, ctx->args->merged_wave_info)
10363 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10364 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10365
10366 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10367 Temp cond;
10368
10369 if (ctx->program->wave_size == 64) {
10370 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10371 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10372 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10373 } else {
10374 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10375 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10376 }
10377
10378 return cond;
10379 }
10380
10381 bool ngg_early_prim_export(isel_context *ctx)
10382 {
10383 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10384 return true;
10385 }
10386
10387 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10388 {
10389 Builder bld(ctx->program, ctx->block);
10390
10391 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10392 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10393
10394 /* Get the id of the current wave within the threadgroup (workgroup) */
10395 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10396 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10397
10398 /* Execute the following code only on the first wave (wave id 0),
10399 * use the SCC def to tell if the wave id is zero or not.
10400 */
10401 Temp cond = wave_id_in_tg.def(1).getTemp();
10402 if_context ic;
10403 begin_uniform_if_then(ctx, &ic, cond);
10404 begin_uniform_if_else(ctx, &ic);
10405 bld.reset(ctx->block);
10406
10407 /* Number of vertices output by VS/TES */
10408 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10409 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10410 /* Number of primitives output by VS/TES */
10411 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10412 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10413
10414 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10415 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10416 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10417
10418 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10419 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10420
10421 end_uniform_if(ctx, &ic);
10422
10423 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10424 bld.reset(ctx->block);
10425 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10426 }
10427
10428 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10429 {
10430 Builder bld(ctx->program, ctx->block);
10431
10432 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10433 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10434 }
10435
10436 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10437 Temp tmp;
10438
10439 for (unsigned i = 0; i < num_vertices; ++i) {
10440 assert(vtxindex[i].id());
10441
10442 if (i)
10443 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10444 else
10445 tmp = vtxindex[i];
10446
10447 /* The initial edge flag is always false in tess eval shaders. */
10448 if (ctx->stage == ngg_vertex_gs) {
10449 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10450 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10451 }
10452 }
10453
10454 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10455
10456 return tmp;
10457 }
10458
10459 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10460 {
10461 Builder bld(ctx->program, ctx->block);
10462 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10463
10464 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10465 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10466 false /* compressed */, true/* done */, false /* valid mask */);
10467 }
10468
10469 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10470 {
10471 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10472 * These must always come before VS exports.
10473 *
10474 * It is recommended to do these as early as possible. They can be at the beginning when
10475 * there is no SW GS and the shader doesn't write edge flags.
10476 */
10477
10478 if_context ic;
10479 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10480 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10481
10482 Builder bld(ctx->program, ctx->block);
10483 constexpr unsigned max_vertices_per_primitive = 3;
10484 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10485
10486 if (ctx->stage == ngg_vertex_gs) {
10487 /* TODO: optimize for points & lines */
10488 } else if (ctx->stage == ngg_tess_eval_gs) {
10489 if (ctx->shader->info.tess.point_mode)
10490 num_vertices_per_primitive = 1;
10491 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10492 num_vertices_per_primitive = 2;
10493 } else {
10494 unreachable("Unsupported NGG shader stage");
10495 }
10496
10497 Temp vtxindex[max_vertices_per_primitive];
10498 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10499 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10500 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10501 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10502 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10503 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10504 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10505 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10506
10507 /* Export primitive data to the index buffer. */
10508 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10509
10510 /* Export primitive ID. */
10511 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10512 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10513 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10514 Temp provoking_vtx_index = vtxindex[0];
10515 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10516
10517 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10518 }
10519
10520 begin_divergent_if_else(ctx, &ic);
10521 end_divergent_if(ctx, &ic);
10522 }
10523
10524 void ngg_emit_nogs_output(isel_context *ctx)
10525 {
10526 /* Emits NGG GS output, for stages that don't have SW GS. */
10527
10528 if_context ic;
10529 Builder bld(ctx->program, ctx->block);
10530 bool late_prim_export = !ngg_early_prim_export(ctx);
10531
10532 /* NGG streamout is currently disabled by default. */
10533 assert(!ctx->args->shader_info->so.num_outputs);
10534
10535 if (late_prim_export) {
10536 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10537 create_export_phis(ctx);
10538 /* Do what we need to do in the GS threads. */
10539 ngg_emit_nogs_gsthreads(ctx);
10540
10541 /* What comes next should be executed on ES threads. */
10542 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10543 begin_divergent_if_then(ctx, &ic, is_es_thread);
10544 bld.reset(ctx->block);
10545 }
10546
10547 /* Export VS outputs */
10548 ctx->block->kind |= block_kind_export_end;
10549 create_vs_exports(ctx);
10550
10551 /* Export primitive ID */
10552 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10553 Temp prim_id;
10554
10555 if (ctx->stage == ngg_vertex_gs) {
10556 /* Wait for GS threads to store primitive ID in LDS. */
10557 bld.barrier(aco_opcode::p_memory_barrier_shared);
10558 bld.sopp(aco_opcode::s_barrier);
10559
10560 /* Calculate LDS address where the GS threads stored the primitive ID. */
10561 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10562 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10563 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10564 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10565 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10566 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10567
10568 /* Load primitive ID from LDS. */
10569 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10570 } else if (ctx->stage == ngg_tess_eval_gs) {
10571 /* TES: Just use the patch ID as the primitive ID. */
10572 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10573 } else {
10574 unreachable("unsupported NGG shader stage.");
10575 }
10576
10577 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10578 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10579
10580 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10581 }
10582
10583 if (late_prim_export) {
10584 begin_divergent_if_else(ctx, &ic);
10585 end_divergent_if(ctx, &ic);
10586 bld.reset(ctx->block);
10587 }
10588 }
10589
10590 void select_program(Program *program,
10591 unsigned shader_count,
10592 struct nir_shader *const *shaders,
10593 ac_shader_config* config,
10594 struct radv_shader_args *args)
10595 {
10596 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10597 if_context ic_merged_wave_info;
10598 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10599
10600 for (unsigned i = 0; i < shader_count; i++) {
10601 nir_shader *nir = shaders[i];
10602 init_context(&ctx, nir);
10603
10604 setup_fp_mode(&ctx, nir);
10605
10606 if (!i) {
10607 /* needs to be after init_context() for FS */
10608 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10609 append_logical_start(ctx.block);
10610
10611 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10612 fix_ls_vgpr_init_bug(&ctx, startpgm);
10613
10614 split_arguments(&ctx, startpgm);
10615 }
10616
10617 if (ngg_no_gs) {
10618 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10619
10620 if (ngg_early_prim_export(&ctx))
10621 ngg_emit_nogs_gsthreads(&ctx);
10622 }
10623
10624 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10625 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10626 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10627 ((nir->info.stage == MESA_SHADER_VERTEX &&
10628 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10629 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10630 ctx.stage == tess_eval_geometry_gs));
10631
10632 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10633 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10634 if (check_merged_wave_info) {
10635 Temp cond = merged_wave_info_to_mask(&ctx, i);
10636 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10637 }
10638
10639 if (i) {
10640 Builder bld(ctx.program, ctx.block);
10641
10642 bld.barrier(aco_opcode::p_memory_barrier_shared);
10643 bld.sopp(aco_opcode::s_barrier);
10644
10645 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10646 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));
10647 }
10648 } else if (ctx.stage == geometry_gs)
10649 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10650
10651 if (ctx.stage == fragment_fs)
10652 handle_bc_optimize(&ctx);
10653
10654 visit_cf_list(&ctx, &func->body);
10655
10656 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10657 emit_streamout(&ctx, 0);
10658
10659 if (ctx.stage & hw_vs) {
10660 create_vs_exports(&ctx);
10661 ctx.block->kind |= block_kind_export_end;
10662 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10663 ngg_emit_nogs_output(&ctx);
10664 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10665 Builder bld(ctx.program, ctx.block);
10666 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10667 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10668 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10669 write_tcs_tess_factors(&ctx);
10670 }
10671
10672 if (ctx.stage == fragment_fs) {
10673 create_fs_exports(&ctx);
10674 ctx.block->kind |= block_kind_export_end;
10675 }
10676
10677 if (endif_merged_wave_info) {
10678 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10679 end_divergent_if(&ctx, &ic_merged_wave_info);
10680 }
10681
10682 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10683 ngg_emit_nogs_output(&ctx);
10684
10685 ralloc_free(ctx.divergent_vals);
10686
10687 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10688 /* Outputs of the previous stage are inputs to the next stage */
10689 ctx.inputs = ctx.outputs;
10690 ctx.outputs = shader_io_state();
10691 }
10692 }
10693
10694 program->config->float_mode = program->blocks[0].fp_mode.val;
10695
10696 append_logical_end(ctx.block);
10697 ctx.block->kind |= block_kind_uniform;
10698 Builder bld(ctx.program, ctx.block);
10699 if (ctx.program->wb_smem_l1_on_end)
10700 bld.smem(aco_opcode::s_dcache_wb, false);
10701 bld.sopp(aco_opcode::s_endpgm);
10702
10703 cleanup_cfg(program);
10704 }
10705
10706 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10707 ac_shader_config* config,
10708 struct radv_shader_args *args)
10709 {
10710 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10711
10712 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10713 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10714 program->next_fp_mode.must_flush_denorms32 = false;
10715 program->next_fp_mode.must_flush_denorms16_64 = false;
10716 program->next_fp_mode.care_about_round32 = false;
10717 program->next_fp_mode.care_about_round16_64 = false;
10718 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10719 program->next_fp_mode.denorm32 = 0;
10720 program->next_fp_mode.round32 = fp_round_ne;
10721 program->next_fp_mode.round16_64 = fp_round_ne;
10722 ctx.block->fp_mode = program->next_fp_mode;
10723
10724 add_startpgm(&ctx);
10725 append_logical_start(ctx.block);
10726
10727 Builder bld(ctx.program, ctx.block);
10728
10729 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10730
10731 Operand stream_id(0u);
10732 if (args->shader_info->so.num_outputs)
10733 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10734 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10735
10736 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10737
10738 std::stack<Block> endif_blocks;
10739
10740 for (unsigned stream = 0; stream < 4; stream++) {
10741 if (stream_id.isConstant() && stream != stream_id.constantValue())
10742 continue;
10743
10744 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10745 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10746 continue;
10747
10748 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10749
10750 unsigned BB_if_idx = ctx.block->index;
10751 Block BB_endif = Block();
10752 if (!stream_id.isConstant()) {
10753 /* begin IF */
10754 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10755 append_logical_end(ctx.block);
10756 ctx.block->kind |= block_kind_uniform;
10757 bld.branch(aco_opcode::p_cbranch_z, cond);
10758
10759 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10760
10761 ctx.block = ctx.program->create_and_insert_block();
10762 add_edge(BB_if_idx, ctx.block);
10763 bld.reset(ctx.block);
10764 append_logical_start(ctx.block);
10765 }
10766
10767 unsigned offset = 0;
10768 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10769 if (args->shader_info->gs.output_streams[i] != stream)
10770 continue;
10771
10772 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10773 unsigned length = util_last_bit(output_usage_mask);
10774 for (unsigned j = 0; j < length; ++j) {
10775 if (!(output_usage_mask & (1 << j)))
10776 continue;
10777
10778 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10779 Temp voffset = vtx_offset;
10780 if (const_offset >= 4096u) {
10781 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10782 const_offset %= 4096u;
10783 }
10784
10785 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10786 mubuf->definitions[0] = bld.def(v1);
10787 mubuf->operands[0] = Operand(gsvs_ring);
10788 mubuf->operands[1] = Operand(voffset);
10789 mubuf->operands[2] = Operand(0u);
10790 mubuf->offen = true;
10791 mubuf->offset = const_offset;
10792 mubuf->glc = true;
10793 mubuf->slc = true;
10794 mubuf->dlc = args->options->chip_class >= GFX10;
10795 mubuf->barrier = barrier_none;
10796 mubuf->can_reorder = true;
10797
10798 ctx.outputs.mask[i] |= 1 << j;
10799 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10800
10801 bld.insert(std::move(mubuf));
10802
10803 offset++;
10804 }
10805 }
10806
10807 if (args->shader_info->so.num_outputs) {
10808 emit_streamout(&ctx, stream);
10809 bld.reset(ctx.block);
10810 }
10811
10812 if (stream == 0) {
10813 create_vs_exports(&ctx);
10814 ctx.block->kind |= block_kind_export_end;
10815 }
10816
10817 if (!stream_id.isConstant()) {
10818 append_logical_end(ctx.block);
10819
10820 /* branch from then block to endif block */
10821 bld.branch(aco_opcode::p_branch);
10822 add_edge(ctx.block->index, &BB_endif);
10823 ctx.block->kind |= block_kind_uniform;
10824
10825 /* emit else block */
10826 ctx.block = ctx.program->create_and_insert_block();
10827 add_edge(BB_if_idx, ctx.block);
10828 bld.reset(ctx.block);
10829 append_logical_start(ctx.block);
10830
10831 endif_blocks.push(std::move(BB_endif));
10832 }
10833 }
10834
10835 while (!endif_blocks.empty()) {
10836 Block BB_endif = std::move(endif_blocks.top());
10837 endif_blocks.pop();
10838
10839 Block *BB_else = ctx.block;
10840
10841 append_logical_end(BB_else);
10842 /* branch from else block to endif block */
10843 bld.branch(aco_opcode::p_branch);
10844 add_edge(BB_else->index, &BB_endif);
10845 BB_else->kind |= block_kind_uniform;
10846
10847 /** emit endif merge block */
10848 ctx.block = program->insert_block(std::move(BB_endif));
10849 bld.reset(ctx.block);
10850 append_logical_start(ctx.block);
10851 }
10852
10853 program->config->float_mode = program->blocks[0].fp_mode.val;
10854
10855 append_logical_end(ctx.block);
10856 ctx.block->kind |= block_kind_uniform;
10857 bld.sopp(aco_opcode::s_endpgm);
10858
10859 cleanup_cfg(program);
10860 }
10861 }