aco: allow overflow for some SMEM instructions
[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 if (ctx->program->chip_class <= GFX7) {
140 Temp thread_id_hi = bld.vop2(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 } else {
143 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32_e64, dst, mask_hi, thread_id_lo);
144 return thread_id_hi;
145 }
146 }
147
148 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
149 {
150 Builder bld(ctx->program, ctx->block);
151
152 if (!dst.id())
153 dst = bld.tmp(src.regClass());
154
155 assert(src.size() == dst.size());
156
157 if (ctx->stage != fragment_fs) {
158 if (!dst.id())
159 return src;
160
161 bld.copy(Definition(dst), src);
162 return dst;
163 }
164
165 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
166 ctx->program->needs_wqm |= program_needs_wqm;
167 return dst;
168 }
169
170 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
171 {
172 if (index.regClass() == s1)
173 return bld.readlane(bld.def(s1), data, index);
174
175 if (ctx->options->chip_class <= GFX7) {
176 /* GFX6-7: there is no bpermute instruction */
177 Operand index_op(index);
178 Operand input_data(data);
179 index_op.setLateKill(true);
180 input_data.setLateKill(true);
181
182 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(bld.lm), bld.def(bld.lm, vcc), index_op, input_data);
183 } else if (ctx->options->chip_class >= GFX10 && ctx->program->wave_size == 64) {
184 /* GFX10 wave64 mode: emulate full-wave bpermute */
185 if (!ctx->has_gfx10_wave64_bpermute) {
186 ctx->has_gfx10_wave64_bpermute = true;
187 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
188 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
189 }
190
191 Temp index_is_lo = bld.vopc(aco_opcode::v_cmp_ge_u32, bld.def(bld.lm), Operand(31u), index);
192 Builder::Result index_is_lo_split = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), index_is_lo);
193 Temp index_is_lo_n1 = bld.sop1(aco_opcode::s_not_b32, bld.def(s1), bld.def(s1, scc), index_is_lo_split.def(1).getTemp());
194 Operand same_half = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), index_is_lo_split.def(0).getTemp(), index_is_lo_n1);
195 Operand index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
196 Operand input_data(data);
197
198 index_x4.setLateKill(true);
199 input_data.setLateKill(true);
200 same_half.setLateKill(true);
201
202 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc), index_x4, input_data, same_half);
203 } else {
204 /* GFX8-9 or GFX10 wave32: bpermute works normally */
205 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
206 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
207 }
208 }
209
210 static Temp emit_masked_swizzle(isel_context *ctx, Builder &bld, Temp src, unsigned mask)
211 {
212 if (ctx->options->chip_class >= GFX8) {
213 unsigned and_mask = mask & 0x1f;
214 unsigned or_mask = (mask >> 5) & 0x1f;
215 unsigned xor_mask = (mask >> 10) & 0x1f;
216
217 uint16_t dpp_ctrl = 0xffff;
218
219 // TODO: we could use DPP8 for some swizzles
220 if (and_mask == 0x1f && or_mask < 4 && xor_mask < 4) {
221 unsigned res[4] = {0, 1, 2, 3};
222 for (unsigned i = 0; i < 4; i++)
223 res[i] = ((res[i] | or_mask) ^ xor_mask) & 0x3;
224 dpp_ctrl = dpp_quad_perm(res[0], res[1], res[2], res[3]);
225 } else if (and_mask == 0x1f && !or_mask && xor_mask == 8) {
226 dpp_ctrl = dpp_row_rr(8);
227 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0xf) {
228 dpp_ctrl = dpp_row_mirror;
229 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0x7) {
230 dpp_ctrl = dpp_row_half_mirror;
231 }
232
233 if (dpp_ctrl != 0xffff)
234 return bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
235 }
236
237 return bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false);
238 }
239
240 Temp as_vgpr(isel_context *ctx, Temp val)
241 {
242 if (val.type() == RegType::sgpr) {
243 Builder bld(ctx->program, ctx->block);
244 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
245 }
246 assert(val.type() == RegType::vgpr);
247 return val;
248 }
249
250 //assumes a != 0xffffffff
251 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
252 {
253 assert(b != 0);
254 Builder bld(ctx->program, ctx->block);
255
256 if (util_is_power_of_two_or_zero(b)) {
257 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
258 return;
259 }
260
261 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
262
263 assert(info.multiplier <= 0xffffffff);
264
265 bool pre_shift = info.pre_shift != 0;
266 bool increment = info.increment != 0;
267 bool multiply = true;
268 bool post_shift = info.post_shift != 0;
269
270 if (!pre_shift && !increment && !multiply && !post_shift) {
271 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
272 return;
273 }
274
275 Temp pre_shift_dst = a;
276 if (pre_shift) {
277 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
278 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
279 }
280
281 Temp increment_dst = pre_shift_dst;
282 if (increment) {
283 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
284 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
285 }
286
287 Temp multiply_dst = increment_dst;
288 if (multiply) {
289 multiply_dst = post_shift ? bld.tmp(v1) : dst;
290 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
291 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
292 }
293
294 if (post_shift) {
295 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
296 }
297 }
298
299 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
300 {
301 Builder bld(ctx->program, ctx->block);
302 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
303 }
304
305
306 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
307 {
308 /* no need to extract the whole vector */
309 if (src.regClass() == dst_rc) {
310 assert(idx == 0);
311 return src;
312 }
313
314 assert(src.bytes() > (idx * dst_rc.bytes()));
315 Builder bld(ctx->program, ctx->block);
316 auto it = ctx->allocated_vec.find(src.id());
317 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
318 if (it->second[idx].regClass() == dst_rc) {
319 return it->second[idx];
320 } else {
321 assert(!dst_rc.is_subdword());
322 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
323 return bld.copy(bld.def(dst_rc), it->second[idx]);
324 }
325 }
326
327 if (dst_rc.is_subdword())
328 src = as_vgpr(ctx, src);
329
330 if (src.bytes() == dst_rc.bytes()) {
331 assert(idx == 0);
332 return bld.copy(bld.def(dst_rc), src);
333 } else {
334 Temp dst = bld.tmp(dst_rc);
335 emit_extract_vector(ctx, src, idx, dst);
336 return dst;
337 }
338 }
339
340 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
341 {
342 if (num_components == 1)
343 return;
344 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
345 return;
346 RegClass rc;
347 if (num_components > vec_src.size()) {
348 if (vec_src.type() == RegType::sgpr) {
349 /* should still help get_alu_src() */
350 emit_split_vector(ctx, vec_src, vec_src.size());
351 return;
352 }
353 /* sub-dword split */
354 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
355 } else {
356 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
357 }
358 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
359 split->operands[0] = Operand(vec_src);
360 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
361 for (unsigned i = 0; i < num_components; i++) {
362 elems[i] = {ctx->program->allocateId(), rc};
363 split->definitions[i] = Definition(elems[i]);
364 }
365 ctx->block->instructions.emplace_back(std::move(split));
366 ctx->allocated_vec.emplace(vec_src.id(), elems);
367 }
368
369 /* This vector expansion uses a mask to determine which elements in the new vector
370 * come from the original vector. The other elements are undefined. */
371 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
372 {
373 emit_split_vector(ctx, vec_src, util_bitcount(mask));
374
375 if (vec_src == dst)
376 return;
377
378 Builder bld(ctx->program, ctx->block);
379 if (num_components == 1) {
380 if (dst.type() == RegType::sgpr)
381 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
382 else
383 bld.copy(Definition(dst), vec_src);
384 return;
385 }
386
387 unsigned component_size = dst.size() / num_components;
388 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
389
390 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
391 vec->definitions[0] = Definition(dst);
392 unsigned k = 0;
393 for (unsigned i = 0; i < num_components; i++) {
394 if (mask & (1 << i)) {
395 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
396 if (dst.type() == RegType::sgpr)
397 src = bld.as_uniform(src);
398 vec->operands[i] = Operand(src);
399 } else {
400 vec->operands[i] = Operand(0u);
401 }
402 elems[i] = vec->operands[i].getTemp();
403 }
404 ctx->block->instructions.emplace_back(std::move(vec));
405 ctx->allocated_vec.emplace(dst.id(), elems);
406 }
407
408 /* adjust misaligned small bit size loads */
409 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
410 {
411 Builder bld(ctx->program, ctx->block);
412 Operand shift;
413 Temp select = Temp();
414 if (offset.isConstant()) {
415 assert(offset.constantValue() && offset.constantValue() < 4);
416 shift = Operand(offset.constantValue() * 8);
417 } else {
418 /* bit_offset = 8 * (offset & 0x3) */
419 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
420 select = bld.tmp(s1);
421 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
422 }
423
424 if (vec.size() == 1) {
425 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
426 } else if (vec.size() == 2) {
427 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
428 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
429 if (tmp == dst)
430 emit_split_vector(ctx, dst, 2);
431 else
432 emit_extract_vector(ctx, tmp, 0, dst);
433 } else if (vec.size() == 4) {
434 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
435 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
436 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
437 if (select != Temp())
438 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), bld.scc(select));
439 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
440 Temp mid = bld.tmp(s1);
441 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
442 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
443 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
444 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
445 emit_split_vector(ctx, dst, 2);
446 }
447 }
448
449 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst, unsigned component_size)
450 {
451 Builder bld(ctx->program, ctx->block);
452 if (offset.isTemp()) {
453 Temp tmp[4] = {vec, vec, vec, vec};
454
455 if (vec.size() == 4) {
456 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1), tmp[3] = bld.tmp(v1);
457 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), Definition(tmp[3]), vec);
458 } else if (vec.size() == 3) {
459 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
460 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
461 } else if (vec.size() == 2) {
462 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
463 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
464 }
465 for (unsigned i = 0; i < dst.size(); i++)
466 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
467
468 vec = tmp[0];
469 if (dst.size() == 2)
470 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
471
472 offset = Operand(0u);
473 }
474
475 unsigned num_components = dst.bytes() / component_size;
476 if (vec.regClass() == dst.regClass()) {
477 assert(offset.constantValue() == 0);
478 bld.copy(Definition(dst), vec);
479 emit_split_vector(ctx, dst, num_components);
480 return;
481 }
482
483 emit_split_vector(ctx, vec, vec.bytes() / component_size);
484 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
485 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
486
487 assert(offset.constantValue() % component_size == 0);
488 unsigned skip = offset.constantValue() / component_size;
489 for (unsigned i = 0; i < num_components; i++)
490 elems[i] = emit_extract_vector(ctx, vec, i + skip, rc);
491
492 /* if dst is vgpr - split the src and create a shrunk version according to the mask. */
493 if (dst.type() == RegType::vgpr) {
494 aco_ptr<Pseudo_instruction> create_vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
495 for (unsigned i = 0; i < num_components; i++)
496 create_vec->operands[i] = Operand(elems[i]);
497 create_vec->definitions[0] = Definition(dst);
498 bld.insert(std::move(create_vec));
499
500 /* if dst is sgpr - split the src, but move the original to sgpr. */
501 } else if (skip) {
502 vec = bld.pseudo(aco_opcode::p_as_uniform, bld.def(RegClass(RegType::sgpr, vec.size())), vec);
503 byte_align_scalar(ctx, vec, offset, dst);
504 } else {
505 assert(dst.size() == vec.size());
506 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
507 }
508
509 ctx->allocated_vec.emplace(dst.id(), elems);
510 }
511
512 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
513 {
514 Builder bld(ctx->program, ctx->block);
515 if (!dst.id())
516 dst = bld.tmp(bld.lm);
517
518 assert(val.regClass() == s1);
519 assert(dst.regClass() == bld.lm);
520
521 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
522 }
523
524 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
525 {
526 Builder bld(ctx->program, ctx->block);
527 if (!dst.id())
528 dst = bld.tmp(s1);
529
530 assert(val.regClass() == bld.lm);
531 assert(dst.regClass() == s1);
532
533 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
534 Temp tmp = bld.tmp(s1);
535 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
536 return emit_wqm(ctx, tmp, dst);
537 }
538
539 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
540 {
541 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
542 return get_ssa_temp(ctx, src.src.ssa);
543
544 if (src.src.ssa->num_components == size) {
545 bool identity_swizzle = true;
546 for (unsigned i = 0; identity_swizzle && i < size; i++) {
547 if (src.swizzle[i] != i)
548 identity_swizzle = false;
549 }
550 if (identity_swizzle)
551 return get_ssa_temp(ctx, src.src.ssa);
552 }
553
554 Temp vec = get_ssa_temp(ctx, src.src.ssa);
555 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
556 assert(elem_size > 0);
557 assert(vec.bytes() % elem_size == 0);
558
559 if (elem_size < 4 && vec.type() == RegType::sgpr) {
560 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
561 assert(size == 1);
562 unsigned swizzle = src.swizzle[0];
563 if (vec.size() > 1) {
564 assert(src.src.ssa->bit_size == 16);
565 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
566 swizzle = swizzle & 1;
567 }
568 if (swizzle == 0)
569 return vec;
570
571 Temp dst{ctx->program->allocateId(), s1};
572 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
573 bfe->operands[0] = Operand(vec);
574 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
575 bfe->definitions[0] = Definition(dst);
576 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
577 ctx->block->instructions.emplace_back(std::move(bfe));
578 return dst;
579 }
580
581 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
582 if (size == 1) {
583 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
584 } else {
585 assert(size <= 4);
586 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
587 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
588 for (unsigned i = 0; i < size; ++i) {
589 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
590 vec_instr->operands[i] = Operand{elems[i]};
591 }
592 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
593 vec_instr->definitions[0] = Definition(dst);
594 ctx->block->instructions.emplace_back(std::move(vec_instr));
595 ctx->allocated_vec.emplace(dst.id(), elems);
596 return dst;
597 }
598 }
599
600 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
601 {
602 if (ptr.size() == 2)
603 return ptr;
604 Builder bld(ctx->program, ctx->block);
605 if (ptr.type() == RegType::vgpr)
606 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
607 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
608 ptr, Operand((unsigned)ctx->options->address32_hi));
609 }
610
611 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
612 {
613 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
614 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
615 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
616 sop2->definitions[0] = Definition(dst);
617 if (writes_scc)
618 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
619 ctx->block->instructions.emplace_back(std::move(sop2));
620 }
621
622 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
623 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
624 {
625 Builder bld(ctx->program, ctx->block);
626 bld.is_precise = instr->exact;
627
628 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
629 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
630 if (src1.type() == RegType::sgpr) {
631 if (commutative && src0.type() == RegType::vgpr) {
632 Temp t = src0;
633 src0 = src1;
634 src1 = t;
635 } else {
636 src1 = as_vgpr(ctx, src1);
637 }
638 }
639
640 if (flush_denorms && ctx->program->chip_class < GFX9) {
641 assert(dst.size() == 1);
642 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
643 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
644 } else {
645 bld.vop2(op, Definition(dst), src0, src1);
646 }
647 }
648
649 void emit_vop2_instruction_logic64(isel_context *ctx, nir_alu_instr *instr,
650 aco_opcode op, Temp dst)
651 {
652 Builder bld(ctx->program, ctx->block);
653 bld.is_precise = instr->exact;
654
655 Temp src0 = get_alu_src(ctx, instr->src[0]);
656 Temp src1 = get_alu_src(ctx, instr->src[1]);
657
658 if (src1.type() == RegType::sgpr) {
659 assert(src0.type() == RegType::vgpr);
660 std::swap(src0, src1);
661 }
662
663 Temp src00 = bld.tmp(src0.type(), 1);
664 Temp src01 = bld.tmp(src0.type(), 1);
665 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
666 Temp src10 = bld.tmp(v1);
667 Temp src11 = bld.tmp(v1);
668 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
669 Temp lo = bld.vop2(op, bld.def(v1), src00, src10);
670 Temp hi = bld.vop2(op, bld.def(v1), src01, src11);
671 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
672 }
673
674 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
675 bool flush_denorms = false)
676 {
677 Temp src0 = get_alu_src(ctx, instr->src[0]);
678 Temp src1 = get_alu_src(ctx, instr->src[1]);
679 Temp src2 = get_alu_src(ctx, instr->src[2]);
680
681 /* ensure that the instruction has at most 1 sgpr operand
682 * The optimizer will inline constants for us */
683 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
684 src0 = as_vgpr(ctx, src0);
685 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
686 src1 = as_vgpr(ctx, src1);
687 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
688 src2 = as_vgpr(ctx, src2);
689
690 Builder bld(ctx->program, ctx->block);
691 bld.is_precise = instr->exact;
692 if (flush_denorms && ctx->program->chip_class < GFX9) {
693 assert(dst.size() == 1);
694 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
695 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
696 } else {
697 bld.vop3(op, Definition(dst), src0, src1, src2);
698 }
699 }
700
701 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
702 {
703 Builder bld(ctx->program, ctx->block);
704 bld.is_precise = instr->exact;
705 if (dst.type() == RegType::sgpr)
706 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
707 bld.vop1(op, bld.def(RegType::vgpr, dst.size()), get_alu_src(ctx, instr->src[0])));
708 else
709 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
710 }
711
712 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
713 {
714 Temp src0 = get_alu_src(ctx, instr->src[0]);
715 Temp src1 = get_alu_src(ctx, instr->src[1]);
716 assert(src0.size() == src1.size());
717
718 aco_ptr<Instruction> vopc;
719 if (src1.type() == RegType::sgpr) {
720 if (src0.type() == RegType::vgpr) {
721 /* to swap the operands, we might also have to change the opcode */
722 switch (op) {
723 case aco_opcode::v_cmp_lt_f16:
724 op = aco_opcode::v_cmp_gt_f16;
725 break;
726 case aco_opcode::v_cmp_ge_f16:
727 op = aco_opcode::v_cmp_le_f16;
728 break;
729 case aco_opcode::v_cmp_lt_i16:
730 op = aco_opcode::v_cmp_gt_i16;
731 break;
732 case aco_opcode::v_cmp_ge_i16:
733 op = aco_opcode::v_cmp_le_i16;
734 break;
735 case aco_opcode::v_cmp_lt_u16:
736 op = aco_opcode::v_cmp_gt_u16;
737 break;
738 case aco_opcode::v_cmp_ge_u16:
739 op = aco_opcode::v_cmp_le_u16;
740 break;
741 case aco_opcode::v_cmp_lt_f32:
742 op = aco_opcode::v_cmp_gt_f32;
743 break;
744 case aco_opcode::v_cmp_ge_f32:
745 op = aco_opcode::v_cmp_le_f32;
746 break;
747 case aco_opcode::v_cmp_lt_i32:
748 op = aco_opcode::v_cmp_gt_i32;
749 break;
750 case aco_opcode::v_cmp_ge_i32:
751 op = aco_opcode::v_cmp_le_i32;
752 break;
753 case aco_opcode::v_cmp_lt_u32:
754 op = aco_opcode::v_cmp_gt_u32;
755 break;
756 case aco_opcode::v_cmp_ge_u32:
757 op = aco_opcode::v_cmp_le_u32;
758 break;
759 case aco_opcode::v_cmp_lt_f64:
760 op = aco_opcode::v_cmp_gt_f64;
761 break;
762 case aco_opcode::v_cmp_ge_f64:
763 op = aco_opcode::v_cmp_le_f64;
764 break;
765 case aco_opcode::v_cmp_lt_i64:
766 op = aco_opcode::v_cmp_gt_i64;
767 break;
768 case aco_opcode::v_cmp_ge_i64:
769 op = aco_opcode::v_cmp_le_i64;
770 break;
771 case aco_opcode::v_cmp_lt_u64:
772 op = aco_opcode::v_cmp_gt_u64;
773 break;
774 case aco_opcode::v_cmp_ge_u64:
775 op = aco_opcode::v_cmp_le_u64;
776 break;
777 default: /* eq and ne are commutative */
778 break;
779 }
780 Temp t = src0;
781 src0 = src1;
782 src1 = t;
783 } else {
784 src1 = as_vgpr(ctx, src1);
785 }
786 }
787
788 Builder bld(ctx->program, ctx->block);
789 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
790 }
791
792 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
793 {
794 Temp src0 = get_alu_src(ctx, instr->src[0]);
795 Temp src1 = get_alu_src(ctx, instr->src[1]);
796 Builder bld(ctx->program, ctx->block);
797
798 assert(dst.regClass() == bld.lm);
799 assert(src0.type() == RegType::sgpr);
800 assert(src1.type() == RegType::sgpr);
801 assert(src0.regClass() == src1.regClass());
802
803 /* Emit the SALU comparison instruction */
804 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
805 /* Turn the result into a per-lane bool */
806 bool_to_vector_condition(ctx, cmp, dst);
807 }
808
809 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
810 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)
811 {
812 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;
813 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;
814 bool use_valu = s_op == aco_opcode::num_opcodes ||
815 nir_dest_is_divergent(instr->dest.dest) ||
816 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
817 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
818 aco_opcode op = use_valu ? v_op : s_op;
819 assert(op != aco_opcode::num_opcodes);
820 assert(dst.regClass() == ctx->program->lane_mask);
821
822 if (use_valu)
823 emit_vopc_instruction(ctx, instr, op, dst);
824 else
825 emit_sopc_instruction(ctx, instr, op, dst);
826 }
827
828 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
829 {
830 Builder bld(ctx->program, ctx->block);
831 Temp src0 = get_alu_src(ctx, instr->src[0]);
832 Temp src1 = get_alu_src(ctx, instr->src[1]);
833
834 assert(dst.regClass() == bld.lm);
835 assert(src0.regClass() == bld.lm);
836 assert(src1.regClass() == bld.lm);
837
838 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
839 }
840
841 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
842 {
843 Builder bld(ctx->program, ctx->block);
844 Temp cond = get_alu_src(ctx, instr->src[0]);
845 Temp then = get_alu_src(ctx, instr->src[1]);
846 Temp els = get_alu_src(ctx, instr->src[2]);
847
848 assert(cond.regClass() == bld.lm);
849
850 if (dst.type() == RegType::vgpr) {
851 aco_ptr<Instruction> bcsel;
852 if (dst.size() == 1) {
853 then = as_vgpr(ctx, then);
854 els = as_vgpr(ctx, els);
855
856 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
857 } else if (dst.size() == 2) {
858 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
859 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
860 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
861 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
862
863 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
864 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
865
866 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
867 } else {
868 fprintf(stderr, "Unimplemented NIR instr bit size: ");
869 nir_print_instr(&instr->instr, stderr);
870 fprintf(stderr, "\n");
871 }
872 return;
873 }
874
875 if (instr->dest.dest.ssa.bit_size == 1) {
876 assert(dst.regClass() == bld.lm);
877 assert(then.regClass() == bld.lm);
878 assert(els.regClass() == bld.lm);
879 }
880
881 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
882 if (dst.regClass() == s1 || dst.regClass() == s2) {
883 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
884 assert(dst.size() == then.size());
885 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
886 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
887 } else {
888 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
889 nir_print_instr(&instr->instr, stderr);
890 fprintf(stderr, "\n");
891 }
892 return;
893 }
894
895 /* divergent boolean bcsel
896 * this implements bcsel on bools: dst = s0 ? s1 : s2
897 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
898 assert(instr->dest.dest.ssa.bit_size == 1);
899
900 if (cond.id() != then.id())
901 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
902
903 if (cond.id() == els.id())
904 bld.sop1(Builder::s_mov, Definition(dst), then);
905 else
906 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
907 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
908 }
909
910 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
911 aco_opcode op, uint32_t undo)
912 {
913 /* multiply by 16777216 to handle denormals */
914 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
915 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
916 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
917 scaled = bld.vop1(op, bld.def(v1), scaled);
918 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
919
920 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
921
922 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
923 }
924
925 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
926 {
927 if (ctx->block->fp_mode.denorm32 == 0) {
928 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
929 return;
930 }
931
932 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
933 }
934
935 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
936 {
937 if (ctx->block->fp_mode.denorm32 == 0) {
938 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
939 return;
940 }
941
942 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
943 }
944
945 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
946 {
947 if (ctx->block->fp_mode.denorm32 == 0) {
948 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
949 return;
950 }
951
952 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
953 }
954
955 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
956 {
957 if (ctx->block->fp_mode.denorm32 == 0) {
958 bld.vop1(aco_opcode::v_log_f32, dst, val);
959 return;
960 }
961
962 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
963 }
964
965 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
966 {
967 if (ctx->options->chip_class >= GFX7)
968 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
969
970 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
971 /* TODO: create more efficient code! */
972 if (val.type() == RegType::sgpr)
973 val = as_vgpr(ctx, val);
974
975 /* Split the input value. */
976 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
977 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
978
979 /* Extract the exponent and compute the unbiased value. */
980 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
981 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
982
983 /* Extract the fractional part. */
984 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
985 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
986
987 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
988 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
989
990 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
991 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
992 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
993 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
994 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
995
996 /* Get the sign bit. */
997 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
998
999 /* Decide the operation to apply depending on the unbiased exponent. */
1000 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
1001 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
1002 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
1003 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
1004 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
1005 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
1006
1007 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
1008 }
1009
1010 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1011 {
1012 if (ctx->options->chip_class >= GFX7)
1013 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
1014
1015 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1016 * lowered at NIR level for precision reasons). */
1017 Temp src0 = as_vgpr(ctx, val);
1018
1019 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
1020 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
1021
1022 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
1023 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
1024 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
1025
1026 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
1027 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
1028 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
1029 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
1030
1031 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
1032 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
1033
1034 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
1035
1036 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
1037 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
1038
1039 return add->definitions[0].getTemp();
1040 }
1041
1042 Temp convert_int(isel_context *ctx, Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
1043 if (!dst.id()) {
1044 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
1045 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
1046 else
1047 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
1048 }
1049
1050 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
1051 return bld.copy(Definition(dst), src);
1052 else if (dst.bytes() < src.bytes())
1053 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
1054
1055 Temp tmp = dst;
1056 if (dst_bits == 64)
1057 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
1058
1059 if (tmp == src) {
1060 } else if (src.regClass() == s1) {
1061 if (is_signed)
1062 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
1063 else
1064 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
1065 } else if (ctx->options->chip_class >= GFX8) {
1066 assert(src_bits != 8 || src.regClass() == v1b);
1067 assert(src_bits != 16 || src.regClass() == v2b);
1068 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
1069 sdwa->operands[0] = Operand(src);
1070 sdwa->definitions[0] = Definition(tmp);
1071 if (is_signed)
1072 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
1073 else
1074 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
1075 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
1076 bld.insert(std::move(sdwa));
1077 } else {
1078 assert(ctx->options->chip_class == GFX6 || ctx->options->chip_class == GFX7);
1079 aco_opcode opcode = is_signed ? aco_opcode::v_bfe_i32 : aco_opcode::v_bfe_u32;
1080 bld.vop3(opcode, Definition(tmp), src, Operand(0u), Operand(src_bits == 8 ? 8u : 16u));
1081 }
1082
1083 if (dst_bits == 64) {
1084 if (is_signed && dst.regClass() == s2) {
1085 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
1086 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1087 } else if (is_signed && dst.regClass() == v2) {
1088 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
1089 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1090 } else {
1091 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
1092 }
1093 }
1094
1095 return dst;
1096 }
1097
1098 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1099 {
1100 if (!instr->dest.dest.is_ssa) {
1101 fprintf(stderr, "nir alu dst not in ssa: ");
1102 nir_print_instr(&instr->instr, stderr);
1103 fprintf(stderr, "\n");
1104 abort();
1105 }
1106 Builder bld(ctx->program, ctx->block);
1107 bld.is_precise = instr->exact;
1108 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1109 switch(instr->op) {
1110 case nir_op_vec2:
1111 case nir_op_vec3:
1112 case nir_op_vec4: {
1113 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1114 unsigned num = instr->dest.dest.ssa.num_components;
1115 for (unsigned i = 0; i < num; ++i)
1116 elems[i] = get_alu_src(ctx, instr->src[i]);
1117
1118 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1119 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1120 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1121 for (unsigned i = 0; i < num; ++i) {
1122 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1123 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1124 else
1125 vec->operands[i] = Operand{elems[i]};
1126 }
1127 vec->definitions[0] = Definition(dst);
1128 ctx->block->instructions.emplace_back(std::move(vec));
1129 ctx->allocated_vec.emplace(dst.id(), elems);
1130 } else {
1131 // TODO: that is a bit suboptimal..
1132 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1133 for (unsigned i = 0; i < num - 1; ++i)
1134 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1135 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1136 for (unsigned i = 0; i < num; ++i) {
1137 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1138 if (bit % 32 == 0) {
1139 elems[bit / 32] = elems[i];
1140 } else {
1141 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1142 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1143 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1144 }
1145 }
1146 if (dst.size() == 1)
1147 bld.copy(Definition(dst), elems[0]);
1148 else
1149 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1150 }
1151 break;
1152 }
1153 case nir_op_mov: {
1154 Temp src = get_alu_src(ctx, instr->src[0]);
1155 aco_ptr<Instruction> mov;
1156 if (dst.type() == RegType::sgpr) {
1157 if (src.type() == RegType::vgpr)
1158 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1159 else if (src.regClass() == s1)
1160 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1161 else if (src.regClass() == s2)
1162 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1163 else
1164 unreachable("wrong src register class for nir_op_imov");
1165 } else {
1166 if (dst.regClass() == v1)
1167 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1168 else if (dst.regClass() == v1b ||
1169 dst.regClass() == v2b ||
1170 dst.regClass() == v2)
1171 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1172 else
1173 unreachable("wrong src register class for nir_op_imov");
1174 }
1175 break;
1176 }
1177 case nir_op_inot: {
1178 Temp src = get_alu_src(ctx, instr->src[0]);
1179 if (instr->dest.dest.ssa.bit_size == 1) {
1180 assert(src.regClass() == bld.lm);
1181 assert(dst.regClass() == bld.lm);
1182 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1183 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1184 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1185 } else if (dst.regClass() == v1) {
1186 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1187 } else if (dst.regClass() == v2) {
1188 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
1189 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
1190 lo = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), lo);
1191 hi = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), hi);
1192 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
1193 } else if (dst.type() == RegType::sgpr) {
1194 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1195 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1196 } else {
1197 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1198 nir_print_instr(&instr->instr, stderr);
1199 fprintf(stderr, "\n");
1200 }
1201 break;
1202 }
1203 case nir_op_ineg: {
1204 Temp src = get_alu_src(ctx, instr->src[0]);
1205 if (dst.regClass() == v1) {
1206 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1207 } else if (dst.regClass() == s1) {
1208 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1209 } else if (dst.size() == 2) {
1210 Temp src0 = bld.tmp(dst.type(), 1);
1211 Temp src1 = bld.tmp(dst.type(), 1);
1212 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1213
1214 if (dst.regClass() == s2) {
1215 Temp carry = bld.tmp(s1);
1216 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1217 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1218 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1219 } else {
1220 Temp lower = bld.tmp(v1);
1221 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1222 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1223 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1224 }
1225 } else {
1226 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1227 nir_print_instr(&instr->instr, stderr);
1228 fprintf(stderr, "\n");
1229 }
1230 break;
1231 }
1232 case nir_op_iabs: {
1233 if (dst.regClass() == s1) {
1234 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1235 } else if (dst.regClass() == v1) {
1236 Temp src = get_alu_src(ctx, instr->src[0]);
1237 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1238 } else {
1239 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1240 nir_print_instr(&instr->instr, stderr);
1241 fprintf(stderr, "\n");
1242 }
1243 break;
1244 }
1245 case nir_op_isign: {
1246 Temp src = get_alu_src(ctx, instr->src[0]);
1247 if (dst.regClass() == s1) {
1248 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1249 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1250 } else if (dst.regClass() == s2) {
1251 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1252 Temp neqz;
1253 if (ctx->program->chip_class >= GFX8)
1254 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1255 else
1256 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1257 /* SCC gets zero-extended to 64 bit */
1258 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1259 } else if (dst.regClass() == v1) {
1260 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1261 } else if (dst.regClass() == v2) {
1262 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1263 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1264 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1265 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1266 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1267 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1268 } else {
1269 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1270 nir_print_instr(&instr->instr, stderr);
1271 fprintf(stderr, "\n");
1272 }
1273 break;
1274 }
1275 case nir_op_imax: {
1276 if (dst.regClass() == v1) {
1277 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1278 } else if (dst.regClass() == s1) {
1279 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1280 } else {
1281 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1282 nir_print_instr(&instr->instr, stderr);
1283 fprintf(stderr, "\n");
1284 }
1285 break;
1286 }
1287 case nir_op_umax: {
1288 if (dst.regClass() == v1) {
1289 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1290 } else if (dst.regClass() == s1) {
1291 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1292 } else {
1293 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1294 nir_print_instr(&instr->instr, stderr);
1295 fprintf(stderr, "\n");
1296 }
1297 break;
1298 }
1299 case nir_op_imin: {
1300 if (dst.regClass() == v1) {
1301 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1302 } else if (dst.regClass() == s1) {
1303 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1304 } else {
1305 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1306 nir_print_instr(&instr->instr, stderr);
1307 fprintf(stderr, "\n");
1308 }
1309 break;
1310 }
1311 case nir_op_umin: {
1312 if (dst.regClass() == v1) {
1313 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1314 } else if (dst.regClass() == s1) {
1315 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1316 } else {
1317 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1318 nir_print_instr(&instr->instr, stderr);
1319 fprintf(stderr, "\n");
1320 }
1321 break;
1322 }
1323 case nir_op_ior: {
1324 if (instr->dest.dest.ssa.bit_size == 1) {
1325 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1326 } else if (dst.regClass() == v1) {
1327 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1328 } else if (dst.regClass() == v2) {
1329 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_or_b32, dst);
1330 } else if (dst.regClass() == s1) {
1331 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1332 } else if (dst.regClass() == s2) {
1333 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1334 } else {
1335 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1336 nir_print_instr(&instr->instr, stderr);
1337 fprintf(stderr, "\n");
1338 }
1339 break;
1340 }
1341 case nir_op_iand: {
1342 if (instr->dest.dest.ssa.bit_size == 1) {
1343 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1344 } else if (dst.regClass() == v1) {
1345 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1346 } else if (dst.regClass() == v2) {
1347 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_and_b32, dst);
1348 } else if (dst.regClass() == s1) {
1349 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1350 } else if (dst.regClass() == s2) {
1351 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1352 } else {
1353 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1354 nir_print_instr(&instr->instr, stderr);
1355 fprintf(stderr, "\n");
1356 }
1357 break;
1358 }
1359 case nir_op_ixor: {
1360 if (instr->dest.dest.ssa.bit_size == 1) {
1361 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1362 } else if (dst.regClass() == v1) {
1363 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1364 } else if (dst.regClass() == v2) {
1365 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_xor_b32, dst);
1366 } else if (dst.regClass() == s1) {
1367 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1368 } else if (dst.regClass() == s2) {
1369 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1370 } else {
1371 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1372 nir_print_instr(&instr->instr, stderr);
1373 fprintf(stderr, "\n");
1374 }
1375 break;
1376 }
1377 case nir_op_ushr: {
1378 if (dst.regClass() == v1) {
1379 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1380 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1381 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1382 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1383 } else if (dst.regClass() == v2) {
1384 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1385 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1386 } else if (dst.regClass() == s2) {
1387 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1388 } else if (dst.regClass() == s1) {
1389 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1390 } else {
1391 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1392 nir_print_instr(&instr->instr, stderr);
1393 fprintf(stderr, "\n");
1394 }
1395 break;
1396 }
1397 case nir_op_ishl: {
1398 if (dst.regClass() == v1) {
1399 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1400 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1401 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1402 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1403 } else if (dst.regClass() == v2) {
1404 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1405 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1406 } else if (dst.regClass() == s1) {
1407 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1408 } else if (dst.regClass() == s2) {
1409 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1410 } else {
1411 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1412 nir_print_instr(&instr->instr, stderr);
1413 fprintf(stderr, "\n");
1414 }
1415 break;
1416 }
1417 case nir_op_ishr: {
1418 if (dst.regClass() == v1) {
1419 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1420 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1421 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1422 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1423 } else if (dst.regClass() == v2) {
1424 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1425 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1426 } else if (dst.regClass() == s1) {
1427 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1428 } else if (dst.regClass() == s2) {
1429 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1430 } else {
1431 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1432 nir_print_instr(&instr->instr, stderr);
1433 fprintf(stderr, "\n");
1434 }
1435 break;
1436 }
1437 case nir_op_find_lsb: {
1438 Temp src = get_alu_src(ctx, instr->src[0]);
1439 if (src.regClass() == s1) {
1440 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1441 } else if (src.regClass() == v1) {
1442 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1443 } else if (src.regClass() == s2) {
1444 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1445 } else {
1446 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1447 nir_print_instr(&instr->instr, stderr);
1448 fprintf(stderr, "\n");
1449 }
1450 break;
1451 }
1452 case nir_op_ufind_msb:
1453 case nir_op_ifind_msb: {
1454 Temp src = get_alu_src(ctx, instr->src[0]);
1455 if (src.regClass() == s1 || src.regClass() == s2) {
1456 aco_opcode op = src.regClass() == s2 ?
1457 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1458 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1459 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1460
1461 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1462 Operand(src.size() * 32u - 1u), msb_rev);
1463 Temp msb = sub.def(0).getTemp();
1464 Temp carry = sub.def(1).getTemp();
1465
1466 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1467 } else if (src.regClass() == v1) {
1468 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1469 Temp msb_rev = bld.tmp(v1);
1470 emit_vop1_instruction(ctx, instr, op, msb_rev);
1471 Temp msb = bld.tmp(v1);
1472 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1473 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1474 } else {
1475 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1476 nir_print_instr(&instr->instr, stderr);
1477 fprintf(stderr, "\n");
1478 }
1479 break;
1480 }
1481 case nir_op_bitfield_reverse: {
1482 if (dst.regClass() == s1) {
1483 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1484 } else if (dst.regClass() == v1) {
1485 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1486 } else {
1487 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1488 nir_print_instr(&instr->instr, stderr);
1489 fprintf(stderr, "\n");
1490 }
1491 break;
1492 }
1493 case nir_op_iadd: {
1494 if (dst.regClass() == s1) {
1495 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1496 break;
1497 }
1498
1499 Temp src0 = get_alu_src(ctx, instr->src[0]);
1500 Temp src1 = get_alu_src(ctx, instr->src[1]);
1501 if (dst.regClass() == v1) {
1502 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1503 break;
1504 }
1505
1506 assert(src0.size() == 2 && src1.size() == 2);
1507 Temp src00 = bld.tmp(src0.type(), 1);
1508 Temp src01 = bld.tmp(dst.type(), 1);
1509 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1510 Temp src10 = bld.tmp(src1.type(), 1);
1511 Temp src11 = bld.tmp(dst.type(), 1);
1512 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1513
1514 if (dst.regClass() == s2) {
1515 Temp carry = bld.tmp(s1);
1516 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1517 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1518 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1519 } else if (dst.regClass() == v2) {
1520 Temp dst0 = bld.tmp(v1);
1521 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1522 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1523 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1524 } else {
1525 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1526 nir_print_instr(&instr->instr, stderr);
1527 fprintf(stderr, "\n");
1528 }
1529 break;
1530 }
1531 case nir_op_uadd_sat: {
1532 Temp src0 = get_alu_src(ctx, instr->src[0]);
1533 Temp src1 = get_alu_src(ctx, instr->src[1]);
1534 if (dst.regClass() == s1) {
1535 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1536 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1537 src0, src1);
1538 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1539 } else if (dst.regClass() == v1) {
1540 if (ctx->options->chip_class >= GFX9) {
1541 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1542 add->operands[0] = Operand(src0);
1543 add->operands[1] = Operand(src1);
1544 add->definitions[0] = Definition(dst);
1545 add->clamp = 1;
1546 ctx->block->instructions.emplace_back(std::move(add));
1547 } else {
1548 if (src1.regClass() != v1)
1549 std::swap(src0, src1);
1550 assert(src1.regClass() == v1);
1551 Temp tmp = bld.tmp(v1);
1552 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1553 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1554 }
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_uadd_carry: {
1563 Temp src0 = get_alu_src(ctx, instr->src[0]);
1564 Temp src1 = get_alu_src(ctx, instr->src[1]);
1565 if (dst.regClass() == s1) {
1566 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1567 break;
1568 }
1569 if (dst.regClass() == v1) {
1570 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1571 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1572 break;
1573 }
1574
1575 Temp src00 = bld.tmp(src0.type(), 1);
1576 Temp src01 = bld.tmp(dst.type(), 1);
1577 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1578 Temp src10 = bld.tmp(src1.type(), 1);
1579 Temp src11 = bld.tmp(dst.type(), 1);
1580 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1581 if (dst.regClass() == s2) {
1582 Temp carry = bld.tmp(s1);
1583 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1584 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1585 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1586 } else if (dst.regClass() == v2) {
1587 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1588 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1589 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1590 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1591 } else {
1592 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1593 nir_print_instr(&instr->instr, stderr);
1594 fprintf(stderr, "\n");
1595 }
1596 break;
1597 }
1598 case nir_op_isub: {
1599 if (dst.regClass() == s1) {
1600 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1601 break;
1602 }
1603
1604 Temp src0 = get_alu_src(ctx, instr->src[0]);
1605 Temp src1 = get_alu_src(ctx, instr->src[1]);
1606 if (dst.regClass() == v1) {
1607 bld.vsub32(Definition(dst), src0, src1);
1608 break;
1609 }
1610
1611 Temp src00 = bld.tmp(src0.type(), 1);
1612 Temp src01 = bld.tmp(dst.type(), 1);
1613 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1614 Temp src10 = bld.tmp(src1.type(), 1);
1615 Temp src11 = bld.tmp(dst.type(), 1);
1616 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1617 if (dst.regClass() == s2) {
1618 Temp carry = bld.tmp(s1);
1619 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1620 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1621 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1622 } else if (dst.regClass() == v2) {
1623 Temp lower = bld.tmp(v1);
1624 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1625 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1626 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1627 } else {
1628 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1629 nir_print_instr(&instr->instr, stderr);
1630 fprintf(stderr, "\n");
1631 }
1632 break;
1633 }
1634 case nir_op_usub_borrow: {
1635 Temp src0 = get_alu_src(ctx, instr->src[0]);
1636 Temp src1 = get_alu_src(ctx, instr->src[1]);
1637 if (dst.regClass() == s1) {
1638 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1639 break;
1640 } else if (dst.regClass() == v1) {
1641 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1642 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1643 break;
1644 }
1645
1646 Temp src00 = bld.tmp(src0.type(), 1);
1647 Temp src01 = bld.tmp(dst.type(), 1);
1648 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1649 Temp src10 = bld.tmp(src1.type(), 1);
1650 Temp src11 = bld.tmp(dst.type(), 1);
1651 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1652 if (dst.regClass() == s2) {
1653 Temp borrow = bld.tmp(s1);
1654 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1655 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1656 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1657 } else if (dst.regClass() == v2) {
1658 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1659 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1660 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1661 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1662 } else {
1663 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1664 nir_print_instr(&instr->instr, stderr);
1665 fprintf(stderr, "\n");
1666 }
1667 break;
1668 }
1669 case nir_op_imul: {
1670 if (dst.regClass() == v1) {
1671 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1672 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1673 } else if (dst.regClass() == s1) {
1674 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1675 } else {
1676 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1677 nir_print_instr(&instr->instr, stderr);
1678 fprintf(stderr, "\n");
1679 }
1680 break;
1681 }
1682 case nir_op_umul_high: {
1683 if (dst.regClass() == v1) {
1684 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1685 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1686 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1687 } else if (dst.regClass() == s1) {
1688 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1689 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1690 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1691 } else {
1692 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1693 nir_print_instr(&instr->instr, stderr);
1694 fprintf(stderr, "\n");
1695 }
1696 break;
1697 }
1698 case nir_op_imul_high: {
1699 if (dst.regClass() == v1) {
1700 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1701 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1702 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1703 } else if (dst.regClass() == s1) {
1704 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1705 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1706 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1707 } else {
1708 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1709 nir_print_instr(&instr->instr, stderr);
1710 fprintf(stderr, "\n");
1711 }
1712 break;
1713 }
1714 case nir_op_fmul: {
1715 Temp src0 = get_alu_src(ctx, instr->src[0]);
1716 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1717 if (dst.regClass() == v2b) {
1718 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1719 } else if (dst.regClass() == v1) {
1720 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1721 } else if (dst.regClass() == v2) {
1722 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1723 } else {
1724 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1725 nir_print_instr(&instr->instr, stderr);
1726 fprintf(stderr, "\n");
1727 }
1728 break;
1729 }
1730 case nir_op_fadd: {
1731 Temp src0 = get_alu_src(ctx, instr->src[0]);
1732 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1733 if (dst.regClass() == v2b) {
1734 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1735 } else if (dst.regClass() == v1) {
1736 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1737 } else if (dst.regClass() == v2) {
1738 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1739 } else {
1740 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1741 nir_print_instr(&instr->instr, stderr);
1742 fprintf(stderr, "\n");
1743 }
1744 break;
1745 }
1746 case nir_op_fsub: {
1747 Temp src0 = get_alu_src(ctx, instr->src[0]);
1748 Temp src1 = get_alu_src(ctx, instr->src[1]);
1749 if (dst.regClass() == v2b) {
1750 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1751 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1752 else
1753 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1754 } else if (dst.regClass() == v1) {
1755 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1756 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1757 else
1758 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1759 } else if (dst.regClass() == v2) {
1760 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1761 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1762 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1763 sub->neg[1] = true;
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_fmax: {
1772 Temp src0 = get_alu_src(ctx, instr->src[0]);
1773 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1774 if (dst.regClass() == v2b) {
1775 // TODO: check fp_mode.must_flush_denorms16_64
1776 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1777 } else if (dst.regClass() == v1) {
1778 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1779 } else if (dst.regClass() == v2) {
1780 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1781 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1782 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1783 } else {
1784 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1785 }
1786 } else {
1787 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1788 nir_print_instr(&instr->instr, stderr);
1789 fprintf(stderr, "\n");
1790 }
1791 break;
1792 }
1793 case nir_op_fmin: {
1794 Temp src0 = get_alu_src(ctx, instr->src[0]);
1795 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1796 if (dst.regClass() == v2b) {
1797 // TODO: check fp_mode.must_flush_denorms16_64
1798 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1799 } else if (dst.regClass() == v1) {
1800 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1801 } else if (dst.regClass() == v2) {
1802 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1803 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1804 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1805 } else {
1806 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1807 }
1808 } else {
1809 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1810 nir_print_instr(&instr->instr, stderr);
1811 fprintf(stderr, "\n");
1812 }
1813 break;
1814 }
1815 case nir_op_fmax3: {
1816 if (dst.regClass() == v2b) {
1817 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1818 } else if (dst.regClass() == v1) {
1819 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1820 } else {
1821 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1822 nir_print_instr(&instr->instr, stderr);
1823 fprintf(stderr, "\n");
1824 }
1825 break;
1826 }
1827 case nir_op_fmin3: {
1828 if (dst.regClass() == v2b) {
1829 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1830 } else if (dst.regClass() == v1) {
1831 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1832 } else {
1833 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1834 nir_print_instr(&instr->instr, stderr);
1835 fprintf(stderr, "\n");
1836 }
1837 break;
1838 }
1839 case nir_op_fmed3: {
1840 if (dst.regClass() == v2b) {
1841 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1842 } else if (dst.regClass() == v1) {
1843 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1844 } else {
1845 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1846 nir_print_instr(&instr->instr, stderr);
1847 fprintf(stderr, "\n");
1848 }
1849 break;
1850 }
1851 case nir_op_umax3: {
1852 if (dst.size() == 1) {
1853 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1854 } else {
1855 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1856 nir_print_instr(&instr->instr, stderr);
1857 fprintf(stderr, "\n");
1858 }
1859 break;
1860 }
1861 case nir_op_umin3: {
1862 if (dst.size() == 1) {
1863 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1864 } else {
1865 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1866 nir_print_instr(&instr->instr, stderr);
1867 fprintf(stderr, "\n");
1868 }
1869 break;
1870 }
1871 case nir_op_umed3: {
1872 if (dst.size() == 1) {
1873 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1874 } else {
1875 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1876 nir_print_instr(&instr->instr, stderr);
1877 fprintf(stderr, "\n");
1878 }
1879 break;
1880 }
1881 case nir_op_imax3: {
1882 if (dst.size() == 1) {
1883 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1884 } else {
1885 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1886 nir_print_instr(&instr->instr, stderr);
1887 fprintf(stderr, "\n");
1888 }
1889 break;
1890 }
1891 case nir_op_imin3: {
1892 if (dst.size() == 1) {
1893 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1894 } else {
1895 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1896 nir_print_instr(&instr->instr, stderr);
1897 fprintf(stderr, "\n");
1898 }
1899 break;
1900 }
1901 case nir_op_imed3: {
1902 if (dst.size() == 1) {
1903 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1904 } else {
1905 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1906 nir_print_instr(&instr->instr, stderr);
1907 fprintf(stderr, "\n");
1908 }
1909 break;
1910 }
1911 case nir_op_cube_face_coord: {
1912 Temp in = get_alu_src(ctx, instr->src[0], 3);
1913 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1914 emit_extract_vector(ctx, in, 1, v1),
1915 emit_extract_vector(ctx, in, 2, v1) };
1916 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1917 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1918 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1919 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1920 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1921 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1922 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1923 break;
1924 }
1925 case nir_op_cube_face_index: {
1926 Temp in = get_alu_src(ctx, instr->src[0], 3);
1927 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1928 emit_extract_vector(ctx, in, 1, v1),
1929 emit_extract_vector(ctx, in, 2, v1) };
1930 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1931 break;
1932 }
1933 case nir_op_bcsel: {
1934 emit_bcsel(ctx, instr, dst);
1935 break;
1936 }
1937 case nir_op_frsq: {
1938 Temp src = get_alu_src(ctx, instr->src[0]);
1939 if (dst.regClass() == v2b) {
1940 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1941 } else if (dst.regClass() == v1) {
1942 emit_rsq(ctx, bld, Definition(dst), src);
1943 } else if (dst.regClass() == v2) {
1944 /* Lowered at NIR level for precision reasons. */
1945 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1946 } else {
1947 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1948 nir_print_instr(&instr->instr, stderr);
1949 fprintf(stderr, "\n");
1950 }
1951 break;
1952 }
1953 case nir_op_fneg: {
1954 Temp src = get_alu_src(ctx, instr->src[0]);
1955 if (dst.regClass() == v2b) {
1956 if (ctx->block->fp_mode.must_flush_denorms16_64)
1957 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1958 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1959 } else if (dst.regClass() == v1) {
1960 if (ctx->block->fp_mode.must_flush_denorms32)
1961 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1962 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1963 } else if (dst.regClass() == v2) {
1964 if (ctx->block->fp_mode.must_flush_denorms16_64)
1965 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1966 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1967 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1968 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1969 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1970 } else {
1971 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1972 nir_print_instr(&instr->instr, stderr);
1973 fprintf(stderr, "\n");
1974 }
1975 break;
1976 }
1977 case nir_op_fabs: {
1978 Temp src = get_alu_src(ctx, instr->src[0]);
1979 if (dst.regClass() == v2b) {
1980 if (ctx->block->fp_mode.must_flush_denorms16_64)
1981 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1982 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1983 } else if (dst.regClass() == v1) {
1984 if (ctx->block->fp_mode.must_flush_denorms32)
1985 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1986 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1987 } else if (dst.regClass() == v2) {
1988 if (ctx->block->fp_mode.must_flush_denorms16_64)
1989 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1990 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1991 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1992 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1993 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1994 } else {
1995 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1996 nir_print_instr(&instr->instr, stderr);
1997 fprintf(stderr, "\n");
1998 }
1999 break;
2000 }
2001 case nir_op_fsat: {
2002 Temp src = get_alu_src(ctx, instr->src[0]);
2003 if (dst.regClass() == v2b) {
2004 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src);
2005 } else if (dst.regClass() == v1) {
2006 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2007 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
2008 // TODO: confirm that this holds under any circumstances
2009 } else if (dst.regClass() == v2) {
2010 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
2011 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
2012 vop3->clamp = true;
2013 } else {
2014 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2015 nir_print_instr(&instr->instr, stderr);
2016 fprintf(stderr, "\n");
2017 }
2018 break;
2019 }
2020 case nir_op_flog2: {
2021 Temp src = get_alu_src(ctx, instr->src[0]);
2022 if (dst.regClass() == v2b) {
2023 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
2024 } else if (dst.regClass() == v1) {
2025 emit_log2(ctx, bld, Definition(dst), src);
2026 } else {
2027 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2028 nir_print_instr(&instr->instr, stderr);
2029 fprintf(stderr, "\n");
2030 }
2031 break;
2032 }
2033 case nir_op_frcp: {
2034 Temp src = get_alu_src(ctx, instr->src[0]);
2035 if (dst.regClass() == v2b) {
2036 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
2037 } else if (dst.regClass() == v1) {
2038 emit_rcp(ctx, bld, Definition(dst), src);
2039 } else if (dst.regClass() == v2) {
2040 /* Lowered at NIR level for precision reasons. */
2041 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
2042 } else {
2043 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2044 nir_print_instr(&instr->instr, stderr);
2045 fprintf(stderr, "\n");
2046 }
2047 break;
2048 }
2049 case nir_op_fexp2: {
2050 if (dst.regClass() == v2b) {
2051 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
2052 } else if (dst.regClass() == v1) {
2053 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
2054 } else {
2055 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2056 nir_print_instr(&instr->instr, stderr);
2057 fprintf(stderr, "\n");
2058 }
2059 break;
2060 }
2061 case nir_op_fsqrt: {
2062 Temp src = get_alu_src(ctx, instr->src[0]);
2063 if (dst.regClass() == v2b) {
2064 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
2065 } else if (dst.regClass() == v1) {
2066 emit_sqrt(ctx, bld, Definition(dst), src);
2067 } else if (dst.regClass() == v2) {
2068 /* Lowered at NIR level for precision reasons. */
2069 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
2070 } else {
2071 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2072 nir_print_instr(&instr->instr, stderr);
2073 fprintf(stderr, "\n");
2074 }
2075 break;
2076 }
2077 case nir_op_ffract: {
2078 if (dst.regClass() == v2b) {
2079 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
2080 } else if (dst.regClass() == v1) {
2081 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
2082 } else if (dst.regClass() == v2) {
2083 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
2084 } else {
2085 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2086 nir_print_instr(&instr->instr, stderr);
2087 fprintf(stderr, "\n");
2088 }
2089 break;
2090 }
2091 case nir_op_ffloor: {
2092 Temp src = get_alu_src(ctx, instr->src[0]);
2093 if (dst.regClass() == v2b) {
2094 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
2095 } else if (dst.regClass() == v1) {
2096 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
2097 } else if (dst.regClass() == v2) {
2098 emit_floor_f64(ctx, bld, Definition(dst), src);
2099 } else {
2100 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2101 nir_print_instr(&instr->instr, stderr);
2102 fprintf(stderr, "\n");
2103 }
2104 break;
2105 }
2106 case nir_op_fceil: {
2107 Temp src0 = get_alu_src(ctx, instr->src[0]);
2108 if (dst.regClass() == v2b) {
2109 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
2110 } else if (dst.regClass() == v1) {
2111 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2112 } else if (dst.regClass() == v2) {
2113 if (ctx->options->chip_class >= GFX7) {
2114 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2115 } else {
2116 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2117 /* trunc = trunc(src0)
2118 * if (src0 > 0.0 && src0 != trunc)
2119 * trunc += 1.0
2120 */
2121 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2122 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2123 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2124 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2125 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);
2126 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2127 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2128 }
2129 } else {
2130 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2131 nir_print_instr(&instr->instr, stderr);
2132 fprintf(stderr, "\n");
2133 }
2134 break;
2135 }
2136 case nir_op_ftrunc: {
2137 Temp src = get_alu_src(ctx, instr->src[0]);
2138 if (dst.regClass() == v2b) {
2139 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2140 } else if (dst.regClass() == v1) {
2141 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2142 } else if (dst.regClass() == v2) {
2143 emit_trunc_f64(ctx, bld, Definition(dst), src);
2144 } else {
2145 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2146 nir_print_instr(&instr->instr, stderr);
2147 fprintf(stderr, "\n");
2148 }
2149 break;
2150 }
2151 case nir_op_fround_even: {
2152 Temp src0 = get_alu_src(ctx, instr->src[0]);
2153 if (dst.regClass() == v2b) {
2154 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2155 } else if (dst.regClass() == v1) {
2156 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2157 } else if (dst.regClass() == v2) {
2158 if (ctx->options->chip_class >= GFX7) {
2159 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2160 } else {
2161 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2162 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2163 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2164
2165 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2166 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));
2167 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));
2168 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));
2169 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2170 tmp = sub->definitions[0].getTemp();
2171
2172 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2173 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2174 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2175 Temp cond = vop3->definitions[0].getTemp();
2176
2177 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2178 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2179 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2180 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2181
2182 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2183 }
2184 } else {
2185 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2186 nir_print_instr(&instr->instr, stderr);
2187 fprintf(stderr, "\n");
2188 }
2189 break;
2190 }
2191 case nir_op_fsin:
2192 case nir_op_fcos: {
2193 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2194 aco_ptr<Instruction> norm;
2195 if (dst.regClass() == v2b) {
2196 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3118u));
2197 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2198 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2199 bld.vop1(opcode, Definition(dst), tmp);
2200 } else if (dst.regClass() == v1) {
2201 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2202 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2203
2204 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2205 if (ctx->options->chip_class < GFX9)
2206 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2207
2208 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2209 bld.vop1(opcode, Definition(dst), tmp);
2210 } else {
2211 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2212 nir_print_instr(&instr->instr, stderr);
2213 fprintf(stderr, "\n");
2214 }
2215 break;
2216 }
2217 case nir_op_ldexp: {
2218 Temp src0 = get_alu_src(ctx, instr->src[0]);
2219 Temp src1 = get_alu_src(ctx, instr->src[1]);
2220 if (dst.regClass() == v2b) {
2221 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2222 } else if (dst.regClass() == v1) {
2223 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2224 } else if (dst.regClass() == v2) {
2225 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2226 } else {
2227 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2228 nir_print_instr(&instr->instr, stderr);
2229 fprintf(stderr, "\n");
2230 }
2231 break;
2232 }
2233 case nir_op_frexp_sig: {
2234 Temp src = get_alu_src(ctx, instr->src[0]);
2235 if (dst.regClass() == v2b) {
2236 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2237 } else if (dst.regClass() == v1) {
2238 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2239 } else if (dst.regClass() == v2) {
2240 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2241 } else {
2242 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2243 nir_print_instr(&instr->instr, stderr);
2244 fprintf(stderr, "\n");
2245 }
2246 break;
2247 }
2248 case nir_op_frexp_exp: {
2249 Temp src = get_alu_src(ctx, instr->src[0]);
2250 if (instr->src[0].src.ssa->bit_size == 16) {
2251 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2252 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2253 convert_int(ctx, bld, tmp, 8, 32, true, dst);
2254 } else if (instr->src[0].src.ssa->bit_size == 32) {
2255 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2256 } else if (instr->src[0].src.ssa->bit_size == 64) {
2257 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2258 } else {
2259 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2260 nir_print_instr(&instr->instr, stderr);
2261 fprintf(stderr, "\n");
2262 }
2263 break;
2264 }
2265 case nir_op_fsign: {
2266 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2267 if (dst.regClass() == v2b) {
2268 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2269 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2270 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2271 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2272 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2273 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2274 } else if (dst.regClass() == v1) {
2275 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2276 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2277 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2278 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2279 } else if (dst.regClass() == v2) {
2280 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2281 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2282 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2283
2284 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2285 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2286 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2287
2288 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2289 } else {
2290 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2291 nir_print_instr(&instr->instr, stderr);
2292 fprintf(stderr, "\n");
2293 }
2294 break;
2295 }
2296 case nir_op_f2f16:
2297 case nir_op_f2f16_rtne: {
2298 Temp src = get_alu_src(ctx, instr->src[0]);
2299 if (instr->src[0].src.ssa->bit_size == 64)
2300 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2301 if (instr->op == nir_op_f2f16_rtne && ctx->block->fp_mode.round16_64 != fp_round_ne)
2302 /* We emit s_round_mode/s_setreg_imm32 in lower_to_hw_instr to
2303 * keep value numbering and the scheduler simpler.
2304 */
2305 bld.vop1(aco_opcode::p_cvt_f16_f32_rtne, Definition(dst), src);
2306 else
2307 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2308 break;
2309 }
2310 case nir_op_f2f16_rtz: {
2311 Temp src = get_alu_src(ctx, instr->src[0]);
2312 if (instr->src[0].src.ssa->bit_size == 64)
2313 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2314 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2315 break;
2316 }
2317 case nir_op_f2f32: {
2318 if (instr->src[0].src.ssa->bit_size == 16) {
2319 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2320 } else if (instr->src[0].src.ssa->bit_size == 64) {
2321 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2322 } else {
2323 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2324 nir_print_instr(&instr->instr, stderr);
2325 fprintf(stderr, "\n");
2326 }
2327 break;
2328 }
2329 case nir_op_f2f64: {
2330 Temp src = get_alu_src(ctx, instr->src[0]);
2331 if (instr->src[0].src.ssa->bit_size == 16)
2332 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2333 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2334 break;
2335 }
2336 case nir_op_i2f16: {
2337 assert(dst.regClass() == v2b);
2338 Temp src = get_alu_src(ctx, instr->src[0]);
2339 if (instr->src[0].src.ssa->bit_size == 8)
2340 src = convert_int(ctx, bld, src, 8, 16, true);
2341 else if (instr->src[0].src.ssa->bit_size == 64)
2342 src = convert_int(ctx, bld, src, 64, 32, false);
2343 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2344 break;
2345 }
2346 case nir_op_i2f32: {
2347 assert(dst.size() == 1);
2348 Temp src = get_alu_src(ctx, instr->src[0]);
2349 if (instr->src[0].src.ssa->bit_size <= 16)
2350 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2351 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2352 break;
2353 }
2354 case nir_op_i2f64: {
2355 if (instr->src[0].src.ssa->bit_size <= 32) {
2356 Temp src = get_alu_src(ctx, instr->src[0]);
2357 if (instr->src[0].src.ssa->bit_size <= 16)
2358 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2359 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2360 } else if (instr->src[0].src.ssa->bit_size == 64) {
2361 Temp src = get_alu_src(ctx, instr->src[0]);
2362 RegClass rc = RegClass(src.type(), 1);
2363 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2364 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2365 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2366 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2367 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2368 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2369
2370 } else {
2371 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2372 nir_print_instr(&instr->instr, stderr);
2373 fprintf(stderr, "\n");
2374 }
2375 break;
2376 }
2377 case nir_op_u2f16: {
2378 assert(dst.regClass() == v2b);
2379 Temp src = get_alu_src(ctx, instr->src[0]);
2380 if (instr->src[0].src.ssa->bit_size == 8)
2381 src = convert_int(ctx, bld, src, 8, 16, false);
2382 else if (instr->src[0].src.ssa->bit_size == 64)
2383 src = convert_int(ctx, bld, src, 64, 32, false);
2384 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2385 break;
2386 }
2387 case nir_op_u2f32: {
2388 assert(dst.size() == 1);
2389 Temp src = get_alu_src(ctx, instr->src[0]);
2390 if (instr->src[0].src.ssa->bit_size == 8) {
2391 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2392 } else {
2393 if (instr->src[0].src.ssa->bit_size == 16)
2394 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2395 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2396 }
2397 break;
2398 }
2399 case nir_op_u2f64: {
2400 if (instr->src[0].src.ssa->bit_size <= 32) {
2401 Temp src = get_alu_src(ctx, instr->src[0]);
2402 if (instr->src[0].src.ssa->bit_size <= 16)
2403 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2404 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2405 } else if (instr->src[0].src.ssa->bit_size == 64) {
2406 Temp src = get_alu_src(ctx, instr->src[0]);
2407 RegClass rc = RegClass(src.type(), 1);
2408 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2409 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2410 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2411 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2412 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2413 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2414 } else {
2415 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2416 nir_print_instr(&instr->instr, stderr);
2417 fprintf(stderr, "\n");
2418 }
2419 break;
2420 }
2421 case nir_op_f2i8:
2422 case nir_op_f2i16: {
2423 if (instr->src[0].src.ssa->bit_size == 16)
2424 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i16_f16, dst);
2425 else if (instr->src[0].src.ssa->bit_size == 32)
2426 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2427 else
2428 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2429 break;
2430 }
2431 case nir_op_f2u8:
2432 case nir_op_f2u16: {
2433 if (instr->src[0].src.ssa->bit_size == 16)
2434 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u16_f16, dst);
2435 else if (instr->src[0].src.ssa->bit_size == 32)
2436 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2437 else
2438 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2439 break;
2440 }
2441 case nir_op_f2i32: {
2442 Temp src = get_alu_src(ctx, instr->src[0]);
2443 if (instr->src[0].src.ssa->bit_size == 16) {
2444 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2445 if (dst.type() == RegType::vgpr) {
2446 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2447 } else {
2448 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2449 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2450 }
2451 } else if (instr->src[0].src.ssa->bit_size == 32) {
2452 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2453 } else if (instr->src[0].src.ssa->bit_size == 64) {
2454 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2455 } else {
2456 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2457 nir_print_instr(&instr->instr, stderr);
2458 fprintf(stderr, "\n");
2459 }
2460 break;
2461 }
2462 case nir_op_f2u32: {
2463 Temp src = get_alu_src(ctx, instr->src[0]);
2464 if (instr->src[0].src.ssa->bit_size == 16) {
2465 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2466 if (dst.type() == RegType::vgpr) {
2467 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2468 } else {
2469 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2470 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2471 }
2472 } else if (instr->src[0].src.ssa->bit_size == 32) {
2473 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2474 } else if (instr->src[0].src.ssa->bit_size == 64) {
2475 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2476 } else {
2477 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2478 nir_print_instr(&instr->instr, stderr);
2479 fprintf(stderr, "\n");
2480 }
2481 break;
2482 }
2483 case nir_op_f2i64: {
2484 Temp src = get_alu_src(ctx, instr->src[0]);
2485 if (instr->src[0].src.ssa->bit_size == 16)
2486 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2487
2488 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2489 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2490 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2491 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2492 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2493 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2494 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2495 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2496 Temp new_exponent = bld.tmp(v1);
2497 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2498 if (ctx->program->chip_class >= GFX8)
2499 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2500 else
2501 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2502 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2503 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2504 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2505 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2506 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2507 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2508 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2509 Temp new_lower = bld.tmp(v1);
2510 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2511 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2512 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2513
2514 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2515 if (src.type() == RegType::vgpr)
2516 src = bld.as_uniform(src);
2517 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2518 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2519 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2520 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2521 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2522 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2523 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2524 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2525 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2526 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2527 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2528 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2529 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2530 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2531 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2532 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2533 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2534 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2535 Temp borrow = bld.tmp(s1);
2536 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2537 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2538 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2539
2540 } else if (instr->src[0].src.ssa->bit_size == 64) {
2541 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2542 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2543 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2544 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2545 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2546 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2547 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2548 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2549 if (dst.type() == RegType::sgpr) {
2550 lower = bld.as_uniform(lower);
2551 upper = bld.as_uniform(upper);
2552 }
2553 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2554
2555 } else {
2556 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2557 nir_print_instr(&instr->instr, stderr);
2558 fprintf(stderr, "\n");
2559 }
2560 break;
2561 }
2562 case nir_op_f2u64: {
2563 Temp src = get_alu_src(ctx, instr->src[0]);
2564 if (instr->src[0].src.ssa->bit_size == 16)
2565 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2566
2567 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2568 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2569 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2570 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2571 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2572 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2573 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2574 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2575 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2576 Temp new_exponent = bld.tmp(v1);
2577 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2578 if (ctx->program->chip_class >= GFX8)
2579 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2580 else
2581 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2582 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2583 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2584 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2585 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2586 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2587 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2588 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2589
2590 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2591 if (src.type() == RegType::vgpr)
2592 src = bld.as_uniform(src);
2593 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2594 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2595 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2596 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2597 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2598 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2599 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2600 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2601 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2602 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2603 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2604 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2605 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2606 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2607 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2608 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2609 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2610 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2611
2612 } else if (instr->src[0].src.ssa->bit_size == 64) {
2613 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2614 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2615 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2616 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2617 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2618 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2619 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2620 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2621 if (dst.type() == RegType::sgpr) {
2622 lower = bld.as_uniform(lower);
2623 upper = bld.as_uniform(upper);
2624 }
2625 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2626
2627 } else {
2628 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2629 nir_print_instr(&instr->instr, stderr);
2630 fprintf(stderr, "\n");
2631 }
2632 break;
2633 }
2634 case nir_op_b2f16: {
2635 Temp src = get_alu_src(ctx, instr->src[0]);
2636 assert(src.regClass() == bld.lm);
2637
2638 if (dst.regClass() == s1) {
2639 src = bool_to_scalar_condition(ctx, src);
2640 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2641 } else if (dst.regClass() == v2b) {
2642 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2643 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2644 } else {
2645 unreachable("Wrong destination register class for nir_op_b2f16.");
2646 }
2647 break;
2648 }
2649 case nir_op_b2f32: {
2650 Temp src = get_alu_src(ctx, instr->src[0]);
2651 assert(src.regClass() == bld.lm);
2652
2653 if (dst.regClass() == s1) {
2654 src = bool_to_scalar_condition(ctx, src);
2655 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2656 } else if (dst.regClass() == v1) {
2657 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2658 } else {
2659 unreachable("Wrong destination register class for nir_op_b2f32.");
2660 }
2661 break;
2662 }
2663 case nir_op_b2f64: {
2664 Temp src = get_alu_src(ctx, instr->src[0]);
2665 assert(src.regClass() == bld.lm);
2666
2667 if (dst.regClass() == s2) {
2668 src = bool_to_scalar_condition(ctx, src);
2669 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2670 } else if (dst.regClass() == v2) {
2671 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2672 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2673 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2674 } else {
2675 unreachable("Wrong destination register class for nir_op_b2f64.");
2676 }
2677 break;
2678 }
2679 case nir_op_i2i8:
2680 case nir_op_i2i16:
2681 case nir_op_i2i32:
2682 case nir_op_i2i64: {
2683 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2684 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2685 break;
2686 }
2687 case nir_op_u2u8:
2688 case nir_op_u2u16:
2689 case nir_op_u2u32:
2690 case nir_op_u2u64: {
2691 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2692 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2693 break;
2694 }
2695 case nir_op_b2b32:
2696 case nir_op_b2i8:
2697 case nir_op_b2i16:
2698 case nir_op_b2i32:
2699 case nir_op_b2i64: {
2700 Temp src = get_alu_src(ctx, instr->src[0]);
2701 assert(src.regClass() == bld.lm);
2702
2703 Temp tmp = dst.bytes() == 8 ? bld.tmp(RegClass::get(dst.type(), 4)) : dst;
2704 if (tmp.regClass() == s1) {
2705 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2706 bool_to_scalar_condition(ctx, src, tmp);
2707 } else if (tmp.type() == RegType::vgpr) {
2708 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(tmp), Operand(0u), Operand(1u), src);
2709 } else {
2710 unreachable("Invalid register class for b2i32");
2711 }
2712
2713 if (tmp != dst)
2714 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
2715 break;
2716 }
2717 case nir_op_b2b1:
2718 case nir_op_i2b1: {
2719 Temp src = get_alu_src(ctx, instr->src[0]);
2720 assert(dst.regClass() == bld.lm);
2721
2722 if (src.type() == RegType::vgpr) {
2723 assert(src.regClass() == v1 || src.regClass() == v2);
2724 assert(dst.regClass() == bld.lm);
2725 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2726 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2727 } else {
2728 assert(src.regClass() == s1 || src.regClass() == s2);
2729 Temp tmp;
2730 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2731 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2732 } else {
2733 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2734 bld.scc(bld.def(s1)), Operand(0u), src);
2735 }
2736 bool_to_vector_condition(ctx, tmp, dst);
2737 }
2738 break;
2739 }
2740 case nir_op_pack_64_2x32_split: {
2741 Temp src0 = get_alu_src(ctx, instr->src[0]);
2742 Temp src1 = get_alu_src(ctx, instr->src[1]);
2743
2744 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2745 break;
2746 }
2747 case nir_op_unpack_64_2x32_split_x:
2748 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2749 break;
2750 case nir_op_unpack_64_2x32_split_y:
2751 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2752 break;
2753 case nir_op_unpack_32_2x16_split_x:
2754 if (dst.type() == RegType::vgpr) {
2755 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2756 } else {
2757 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2758 }
2759 break;
2760 case nir_op_unpack_32_2x16_split_y:
2761 if (dst.type() == RegType::vgpr) {
2762 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2763 } else {
2764 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)));
2765 }
2766 break;
2767 case nir_op_pack_32_2x16_split: {
2768 Temp src0 = get_alu_src(ctx, instr->src[0]);
2769 Temp src1 = get_alu_src(ctx, instr->src[1]);
2770 if (dst.regClass() == v1) {
2771 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2772 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2773 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2774 } else {
2775 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2776 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2777 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2778 }
2779 break;
2780 }
2781 case nir_op_pack_half_2x16: {
2782 Temp src = get_alu_src(ctx, instr->src[0], 2);
2783
2784 if (dst.regClass() == v1) {
2785 Temp src0 = bld.tmp(v1);
2786 Temp src1 = bld.tmp(v1);
2787 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2788 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2789 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2790 else
2791 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2792 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2793 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
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_unpack_half_2x16_split_x: {
2802 if (dst.regClass() == v1) {
2803 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2804 } else {
2805 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2806 nir_print_instr(&instr->instr, stderr);
2807 fprintf(stderr, "\n");
2808 }
2809 break;
2810 }
2811 case nir_op_unpack_half_2x16_split_y: {
2812 if (dst.regClass() == v1) {
2813 /* TODO: use SDWA here */
2814 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2815 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2816 } else {
2817 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2818 nir_print_instr(&instr->instr, stderr);
2819 fprintf(stderr, "\n");
2820 }
2821 break;
2822 }
2823 case nir_op_fquantize2f16: {
2824 Temp src = get_alu_src(ctx, instr->src[0]);
2825 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2826 Temp f32, cmp_res;
2827
2828 if (ctx->program->chip_class >= GFX8) {
2829 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2830 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2831 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2832 } else {
2833 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2834 * so compare the result and flush to 0 if it's smaller.
2835 */
2836 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2837 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2838 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2839 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2840 cmp_res = vop3->definitions[0].getTemp();
2841 }
2842
2843 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2844 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2845 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2846 } else {
2847 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2848 }
2849 break;
2850 }
2851 case nir_op_bfm: {
2852 Temp bits = get_alu_src(ctx, instr->src[0]);
2853 Temp offset = get_alu_src(ctx, instr->src[1]);
2854
2855 if (dst.regClass() == s1) {
2856 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2857 } else if (dst.regClass() == v1) {
2858 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2859 } else {
2860 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2861 nir_print_instr(&instr->instr, stderr);
2862 fprintf(stderr, "\n");
2863 }
2864 break;
2865 }
2866 case nir_op_bitfield_select: {
2867 /* (mask & insert) | (~mask & base) */
2868 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2869 Temp insert = get_alu_src(ctx, instr->src[1]);
2870 Temp base = get_alu_src(ctx, instr->src[2]);
2871
2872 /* dst = (insert & bitmask) | (base & ~bitmask) */
2873 if (dst.regClass() == s1) {
2874 aco_ptr<Instruction> sop2;
2875 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2876 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2877 Operand lhs;
2878 if (const_insert && const_bitmask) {
2879 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2880 } else {
2881 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2882 lhs = Operand(insert);
2883 }
2884
2885 Operand rhs;
2886 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2887 if (const_base && const_bitmask) {
2888 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2889 } else {
2890 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2891 rhs = Operand(base);
2892 }
2893
2894 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2895
2896 } else if (dst.regClass() == v1) {
2897 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2898 base = as_vgpr(ctx, base);
2899 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2900 insert = as_vgpr(ctx, insert);
2901
2902 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2903
2904 } else {
2905 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2906 nir_print_instr(&instr->instr, stderr);
2907 fprintf(stderr, "\n");
2908 }
2909 break;
2910 }
2911 case nir_op_ubfe:
2912 case nir_op_ibfe: {
2913 Temp base = get_alu_src(ctx, instr->src[0]);
2914 Temp offset = get_alu_src(ctx, instr->src[1]);
2915 Temp bits = get_alu_src(ctx, instr->src[2]);
2916
2917 if (dst.type() == RegType::sgpr) {
2918 Operand extract;
2919 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2920 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2921 if (const_offset && const_bits) {
2922 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2923 extract = Operand(const_extract);
2924 } else {
2925 Operand width;
2926 if (const_bits) {
2927 width = Operand(const_bits->u32 << 16);
2928 } else {
2929 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2930 }
2931 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2932 }
2933
2934 aco_opcode opcode;
2935 if (dst.regClass() == s1) {
2936 if (instr->op == nir_op_ubfe)
2937 opcode = aco_opcode::s_bfe_u32;
2938 else
2939 opcode = aco_opcode::s_bfe_i32;
2940 } else if (dst.regClass() == s2) {
2941 if (instr->op == nir_op_ubfe)
2942 opcode = aco_opcode::s_bfe_u64;
2943 else
2944 opcode = aco_opcode::s_bfe_i64;
2945 } else {
2946 unreachable("Unsupported BFE bit size");
2947 }
2948
2949 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2950
2951 } else {
2952 aco_opcode opcode;
2953 if (dst.regClass() == v1) {
2954 if (instr->op == nir_op_ubfe)
2955 opcode = aco_opcode::v_bfe_u32;
2956 else
2957 opcode = aco_opcode::v_bfe_i32;
2958 } else {
2959 unreachable("Unsupported BFE bit size");
2960 }
2961
2962 emit_vop3a_instruction(ctx, instr, opcode, dst);
2963 }
2964 break;
2965 }
2966 case nir_op_bit_count: {
2967 Temp src = get_alu_src(ctx, instr->src[0]);
2968 if (src.regClass() == s1) {
2969 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2970 } else if (src.regClass() == v1) {
2971 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2972 } else if (src.regClass() == v2) {
2973 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2974 emit_extract_vector(ctx, src, 1, v1),
2975 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2976 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2977 } else if (src.regClass() == s2) {
2978 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2979 } else {
2980 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2981 nir_print_instr(&instr->instr, stderr);
2982 fprintf(stderr, "\n");
2983 }
2984 break;
2985 }
2986 case nir_op_flt: {
2987 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2988 break;
2989 }
2990 case nir_op_fge: {
2991 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2992 break;
2993 }
2994 case nir_op_feq: {
2995 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2996 break;
2997 }
2998 case nir_op_fne: {
2999 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
3000 break;
3001 }
3002 case nir_op_ilt: {
3003 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);
3004 break;
3005 }
3006 case nir_op_ige: {
3007 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);
3008 break;
3009 }
3010 case nir_op_ieq: {
3011 if (instr->src[0].src.ssa->bit_size == 1)
3012 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
3013 else
3014 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,
3015 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
3016 break;
3017 }
3018 case nir_op_ine: {
3019 if (instr->src[0].src.ssa->bit_size == 1)
3020 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
3021 else
3022 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,
3023 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
3024 break;
3025 }
3026 case nir_op_ult: {
3027 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);
3028 break;
3029 }
3030 case nir_op_uge: {
3031 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);
3032 break;
3033 }
3034 case nir_op_fddx:
3035 case nir_op_fddy:
3036 case nir_op_fddx_fine:
3037 case nir_op_fddy_fine:
3038 case nir_op_fddx_coarse:
3039 case nir_op_fddy_coarse: {
3040 Temp src = get_alu_src(ctx, instr->src[0]);
3041 uint16_t dpp_ctrl1, dpp_ctrl2;
3042 if (instr->op == nir_op_fddx_fine) {
3043 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
3044 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
3045 } else if (instr->op == nir_op_fddy_fine) {
3046 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
3047 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
3048 } else {
3049 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
3050 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
3051 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
3052 else
3053 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
3054 }
3055
3056 Temp tmp;
3057 if (ctx->program->chip_class >= GFX8) {
3058 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
3059 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
3060 } else {
3061 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
3062 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3063 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3064 }
3065 emit_wqm(ctx, tmp, dst, true);
3066 break;
3067 }
3068 default:
3069 fprintf(stderr, "Unknown NIR ALU instr: ");
3070 nir_print_instr(&instr->instr, stderr);
3071 fprintf(stderr, "\n");
3072 }
3073 }
3074
3075 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3076 {
3077 Temp dst = get_ssa_temp(ctx, &instr->def);
3078
3079 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3080 // which get truncated the lsb if double and msb if int
3081 // for now, we only use s_mov_b64 with 64bit inline constants
3082 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3083 assert(dst.type() == RegType::sgpr);
3084
3085 Builder bld(ctx->program, ctx->block);
3086
3087 if (instr->def.bit_size == 1) {
3088 assert(dst.regClass() == bld.lm);
3089 int val = instr->value[0].b ? -1 : 0;
3090 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3091 bld.sop1(Builder::s_mov, Definition(dst), op);
3092 } else if (instr->def.bit_size == 8) {
3093 /* ensure that the value is correctly represented in the low byte of the register */
3094 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3095 } else if (instr->def.bit_size == 16) {
3096 /* ensure that the value is correctly represented in the low half of the register */
3097 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3098 } else if (dst.size() == 1) {
3099 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3100 } else {
3101 assert(dst.size() != 1);
3102 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3103 if (instr->def.bit_size == 64)
3104 for (unsigned i = 0; i < dst.size(); i++)
3105 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3106 else {
3107 for (unsigned i = 0; i < dst.size(); i++)
3108 vec->operands[i] = Operand{instr->value[i].u32};
3109 }
3110 vec->definitions[0] = Definition(dst);
3111 ctx->block->instructions.emplace_back(std::move(vec));
3112 }
3113 }
3114
3115 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3116 {
3117 uint32_t new_mask = 0;
3118 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3119 if (mask & (1u << i))
3120 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3121 return new_mask;
3122 }
3123
3124 struct LoadEmitInfo {
3125 Operand offset;
3126 Temp dst;
3127 unsigned num_components;
3128 unsigned component_size;
3129 Temp resource = Temp(0, s1);
3130 unsigned component_stride = 0;
3131 unsigned const_offset = 0;
3132 unsigned align_mul = 0;
3133 unsigned align_offset = 0;
3134
3135 bool glc = false;
3136 unsigned swizzle_component_size = 0;
3137 barrier_interaction barrier = barrier_none;
3138 bool can_reorder = true;
3139 Temp soffset = Temp(0, s1);
3140 };
3141
3142 using LoadCallback = Temp(*)(
3143 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3144 unsigned align, unsigned const_offset, Temp dst_hint);
3145
3146 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3147 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3148 {
3149 unsigned load_size = info->num_components * info->component_size;
3150 unsigned component_size = info->component_size;
3151
3152 unsigned num_vals = 0;
3153 Temp vals[info->dst.bytes()];
3154
3155 unsigned const_offset = info->const_offset;
3156
3157 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3158 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3159
3160 unsigned bytes_read = 0;
3161 while (bytes_read < load_size) {
3162 unsigned bytes_needed = load_size - bytes_read;
3163
3164 /* add buffer for unaligned loads */
3165 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3166
3167 if (byte_align) {
3168 if ((bytes_needed > 2 ||
3169 (bytes_needed == 2 && (align_mul % 2 || align_offset % 2)) ||
3170 !supports_8bit_16bit_loads) && byte_align_loads) {
3171 if (info->component_stride) {
3172 assert(supports_8bit_16bit_loads && "unimplemented");
3173 bytes_needed = 2;
3174 byte_align = 0;
3175 } else {
3176 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3177 bytes_needed = align(bytes_needed, 4);
3178 }
3179 } else {
3180 byte_align = 0;
3181 }
3182 }
3183
3184 if (info->swizzle_component_size)
3185 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3186 if (info->component_stride)
3187 bytes_needed = MIN2(bytes_needed, info->component_size);
3188
3189 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3190
3191 /* reduce constant offset */
3192 Operand offset = info->offset;
3193 unsigned reduced_const_offset = const_offset;
3194 bool remove_const_offset_completely = need_to_align_offset;
3195 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3196 unsigned to_add = const_offset;
3197 if (remove_const_offset_completely) {
3198 reduced_const_offset = 0;
3199 } else {
3200 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3201 reduced_const_offset %= max_const_offset_plus_one;
3202 }
3203 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3204 if (offset.isConstant()) {
3205 offset = Operand(offset.constantValue() + to_add);
3206 } else if (offset_tmp.regClass() == s1) {
3207 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3208 offset_tmp, Operand(to_add));
3209 } else if (offset_tmp.regClass() == v1) {
3210 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3211 } else {
3212 Temp lo = bld.tmp(offset_tmp.type(), 1);
3213 Temp hi = bld.tmp(offset_tmp.type(), 1);
3214 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3215
3216 if (offset_tmp.regClass() == s2) {
3217 Temp carry = bld.tmp(s1);
3218 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3219 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3220 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3221 } else {
3222 Temp new_lo = bld.tmp(v1);
3223 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3224 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3225 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3226 }
3227 }
3228 }
3229
3230 /* align offset down if needed */
3231 Operand aligned_offset = offset;
3232 if (need_to_align_offset) {
3233 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3234 if (offset.isConstant()) {
3235 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3236 } else if (offset_tmp.regClass() == s1) {
3237 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3238 } else if (offset_tmp.regClass() == s2) {
3239 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3240 } else if (offset_tmp.regClass() == v1) {
3241 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3242 } else if (offset_tmp.regClass() == v2) {
3243 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3244 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3245 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3246 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3247 }
3248 }
3249 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3250 bld.copy(bld.def(s1), aligned_offset);
3251
3252 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3253 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3254 reduced_const_offset, byte_align ? Temp() : info->dst);
3255
3256 /* the callback wrote directly to dst */
3257 if (val == info->dst) {
3258 assert(num_vals == 0);
3259 emit_split_vector(ctx, info->dst, info->num_components);
3260 return;
3261 }
3262
3263 /* shift result right if needed */
3264 if (info->component_size < 4 && byte_align_loads) {
3265 Operand align((uint32_t)byte_align);
3266 if (byte_align == -1) {
3267 if (offset.isConstant())
3268 align = Operand(offset.constantValue() % 4u);
3269 else if (offset.size() == 2)
3270 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3271 else
3272 align = offset;
3273 }
3274
3275 assert(val.bytes() >= load_size && "unimplemented");
3276 if (val.type() == RegType::sgpr)
3277 byte_align_scalar(ctx, val, align, info->dst);
3278 else
3279 byte_align_vector(ctx, val, align, info->dst, component_size);
3280 return;
3281 }
3282
3283 /* add result to list and advance */
3284 if (info->component_stride) {
3285 assert(val.bytes() == info->component_size && "unimplemented");
3286 const_offset += info->component_stride;
3287 align_offset = (align_offset + info->component_stride) % align_mul;
3288 } else {
3289 const_offset += val.bytes();
3290 align_offset = (align_offset + val.bytes()) % align_mul;
3291 }
3292 bytes_read += val.bytes();
3293 vals[num_vals++] = val;
3294 }
3295
3296 /* create array of components */
3297 unsigned components_split = 0;
3298 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3299 bool has_vgprs = false;
3300 for (unsigned i = 0; i < num_vals;) {
3301 Temp tmp[num_vals];
3302 unsigned num_tmps = 0;
3303 unsigned tmp_size = 0;
3304 RegType reg_type = RegType::sgpr;
3305 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3306 if (vals[i].type() == RegType::vgpr)
3307 reg_type = RegType::vgpr;
3308 tmp_size += vals[i].bytes();
3309 tmp[num_tmps++] = vals[i++];
3310 }
3311 if (num_tmps > 1) {
3312 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3313 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3314 for (unsigned i = 0; i < num_vals; i++)
3315 vec->operands[i] = Operand(tmp[i]);
3316 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3317 vec->definitions[0] = Definition(tmp[0]);
3318 bld.insert(std::move(vec));
3319 }
3320
3321 if (tmp[0].bytes() % component_size) {
3322 /* trim tmp[0] */
3323 assert(i == num_vals);
3324 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3325 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3326 }
3327
3328 RegClass elem_rc = RegClass::get(reg_type, component_size);
3329
3330 unsigned start = components_split;
3331
3332 if (tmp_size == elem_rc.bytes()) {
3333 allocated_vec[components_split++] = tmp[0];
3334 } else {
3335 assert(tmp_size % elem_rc.bytes() == 0);
3336 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3337 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3338 for (unsigned i = 0; i < split->definitions.size(); i++) {
3339 Temp component = bld.tmp(elem_rc);
3340 allocated_vec[components_split++] = component;
3341 split->definitions[i] = Definition(component);
3342 }
3343 split->operands[0] = Operand(tmp[0]);
3344 bld.insert(std::move(split));
3345 }
3346
3347 /* try to p_as_uniform early so we can create more optimizable code and
3348 * also update allocated_vec */
3349 for (unsigned j = start; j < components_split; j++) {
3350 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3351 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3352 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3353 }
3354 }
3355
3356 /* concatenate components and p_as_uniform() result if needed */
3357 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3358 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3359
3360 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3361
3362 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3363 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3364 for (unsigned i = 0; i < info->num_components; i++)
3365 vec->operands[i] = Operand(allocated_vec[i]);
3366 if (padding_bytes)
3367 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3368 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3369 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3370 vec->definitions[0] = Definition(tmp);
3371 bld.insert(std::move(vec));
3372 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3373 } else {
3374 vec->definitions[0] = Definition(info->dst);
3375 bld.insert(std::move(vec));
3376 }
3377 }
3378
3379 Operand load_lds_size_m0(Builder& bld)
3380 {
3381 /* TODO: m0 does not need to be initialized on GFX9+ */
3382 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3383 }
3384
3385 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3386 Temp offset, unsigned bytes_needed,
3387 unsigned align, unsigned const_offset,
3388 Temp dst_hint)
3389 {
3390 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3391
3392 Operand m = load_lds_size_m0(bld);
3393
3394 bool large_ds_read = bld.program->chip_class >= GFX7;
3395 bool usable_read2 = bld.program->chip_class >= GFX7;
3396
3397 bool read2 = false;
3398 unsigned size = 0;
3399 aco_opcode op;
3400 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3401 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3402 size = 16;
3403 op = aco_opcode::ds_read_b128;
3404 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3405 size = 16;
3406 read2 = true;
3407 op = aco_opcode::ds_read2_b64;
3408 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3409 size = 12;
3410 op = aco_opcode::ds_read_b96;
3411 } else if (bytes_needed >= 8 && align % 8 == 0) {
3412 size = 8;
3413 op = aco_opcode::ds_read_b64;
3414 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3415 size = 8;
3416 read2 = true;
3417 op = aco_opcode::ds_read2_b32;
3418 } else if (bytes_needed >= 4 && align % 4 == 0) {
3419 size = 4;
3420 op = aco_opcode::ds_read_b32;
3421 } else if (bytes_needed >= 2 && align % 2 == 0) {
3422 size = 2;
3423 op = aco_opcode::ds_read_u16;
3424 } else {
3425 size = 1;
3426 op = aco_opcode::ds_read_u8;
3427 }
3428
3429 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3430 if (const_offset >= max_offset_plus_one) {
3431 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3432 const_offset %= max_offset_plus_one;
3433 }
3434
3435 if (read2)
3436 const_offset /= (size / 2u);
3437
3438 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3439 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3440 if (read2)
3441 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3442 else
3443 bld.ds(op, Definition(val), offset, m, const_offset);
3444
3445 if (size < 4)
3446 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3447
3448 return val;
3449 }
3450
3451 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3452
3453 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3454 Temp offset, unsigned bytes_needed,
3455 unsigned align, unsigned const_offset,
3456 Temp dst_hint)
3457 {
3458 unsigned size = 0;
3459 aco_opcode op;
3460 if (bytes_needed <= 4) {
3461 size = 1;
3462 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3463 } else if (bytes_needed <= 8) {
3464 size = 2;
3465 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3466 } else if (bytes_needed <= 16) {
3467 size = 4;
3468 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3469 } else if (bytes_needed <= 32) {
3470 size = 8;
3471 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3472 } else {
3473 size = 16;
3474 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3475 }
3476 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3477 if (info->resource.id()) {
3478 load->operands[0] = Operand(info->resource);
3479 load->operands[1] = Operand(offset);
3480 } else {
3481 load->operands[0] = Operand(offset);
3482 load->operands[1] = Operand(0u);
3483 }
3484 RegClass rc(RegType::sgpr, size);
3485 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3486 load->definitions[0] = Definition(val);
3487 load->glc = info->glc;
3488 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3489 load->barrier = info->barrier;
3490 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3491 bld.insert(std::move(load));
3492 return val;
3493 }
3494
3495 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3496
3497 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3498 Temp offset, unsigned bytes_needed,
3499 unsigned align_, unsigned const_offset,
3500 Temp dst_hint)
3501 {
3502 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3503 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3504
3505 if (info->soffset.id()) {
3506 if (soffset.isTemp())
3507 vaddr = bld.copy(bld.def(v1), soffset);
3508 soffset = Operand(info->soffset);
3509 }
3510
3511 unsigned bytes_size = 0;
3512 aco_opcode op;
3513 if (bytes_needed == 1) {
3514 bytes_size = 1;
3515 op = aco_opcode::buffer_load_ubyte;
3516 } else if (bytes_needed == 2) {
3517 bytes_size = 2;
3518 op = aco_opcode::buffer_load_ushort;
3519 } else if (bytes_needed <= 4) {
3520 bytes_size = 4;
3521 op = aco_opcode::buffer_load_dword;
3522 } else if (bytes_needed <= 8) {
3523 bytes_size = 8;
3524 op = aco_opcode::buffer_load_dwordx2;
3525 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3526 bytes_size = 12;
3527 op = aco_opcode::buffer_load_dwordx3;
3528 } else {
3529 bytes_size = 16;
3530 op = aco_opcode::buffer_load_dwordx4;
3531 }
3532 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3533 mubuf->operands[0] = Operand(info->resource);
3534 mubuf->operands[1] = vaddr;
3535 mubuf->operands[2] = soffset;
3536 mubuf->offen = (offset.type() == RegType::vgpr);
3537 mubuf->glc = info->glc;
3538 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3539 mubuf->barrier = info->barrier;
3540 mubuf->can_reorder = info->can_reorder;
3541 mubuf->offset = const_offset;
3542 mubuf->swizzled = info->swizzle_component_size != 0;
3543 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3544 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3545 mubuf->definitions[0] = Definition(val);
3546 bld.insert(std::move(mubuf));
3547
3548 return val;
3549 }
3550
3551 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3552
3553 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3554 {
3555 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3556 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3557
3558 if (addr.type() == RegType::vgpr)
3559 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3560 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3561 }
3562
3563 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3564 Temp offset, unsigned bytes_needed,
3565 unsigned align_, unsigned const_offset,
3566 Temp dst_hint)
3567 {
3568 unsigned bytes_size = 0;
3569 bool mubuf = bld.program->chip_class == GFX6;
3570 bool global = bld.program->chip_class >= GFX9;
3571 aco_opcode op;
3572 if (bytes_needed == 1) {
3573 bytes_size = 1;
3574 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3575 } else if (bytes_needed == 2) {
3576 bytes_size = 2;
3577 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3578 } else if (bytes_needed <= 4) {
3579 bytes_size = 4;
3580 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3581 } else if (bytes_needed <= 8) {
3582 bytes_size = 8;
3583 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3584 } else if (bytes_needed <= 12 && !mubuf) {
3585 bytes_size = 12;
3586 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3587 } else {
3588 bytes_size = 16;
3589 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3590 }
3591 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3592 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3593 if (mubuf) {
3594 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3595 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3596 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3597 mubuf->operands[2] = Operand(0u);
3598 mubuf->glc = info->glc;
3599 mubuf->dlc = false;
3600 mubuf->offset = 0;
3601 mubuf->addr64 = offset.type() == RegType::vgpr;
3602 mubuf->disable_wqm = false;
3603 mubuf->barrier = info->barrier;
3604 mubuf->definitions[0] = Definition(val);
3605 bld.insert(std::move(mubuf));
3606 } else {
3607 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3608
3609 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3610 flat->operands[0] = Operand(offset);
3611 flat->operands[1] = Operand(s1);
3612 flat->glc = info->glc;
3613 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3614 flat->barrier = info->barrier;
3615 flat->offset = 0u;
3616 flat->definitions[0] = Definition(val);
3617 bld.insert(std::move(flat));
3618 }
3619
3620 return val;
3621 }
3622
3623 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3624
3625 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3626 Temp address, unsigned base_offset, unsigned align)
3627 {
3628 assert(util_is_power_of_two_nonzero(align));
3629
3630 Builder bld(ctx->program, ctx->block);
3631
3632 unsigned num_components = dst.bytes() / elem_size_bytes;
3633 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3634 info.align_mul = align;
3635 info.align_offset = 0;
3636 info.barrier = barrier_shared;
3637 info.can_reorder = false;
3638 info.const_offset = base_offset;
3639 emit_lds_load(ctx, bld, &info);
3640
3641 return dst;
3642 }
3643
3644 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3645 {
3646 if (!count)
3647 return;
3648
3649 Builder bld(ctx->program, ctx->block);
3650
3651 ASSERTED bool is_subdword = false;
3652 for (unsigned i = 0; i < count; i++)
3653 is_subdword |= offsets[i] % 4;
3654 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3655 assert(!is_subdword || dst_type == RegType::vgpr);
3656
3657 /* count == 1 fast path */
3658 if (count == 1) {
3659 if (dst_type == RegType::sgpr)
3660 dst[0] = bld.as_uniform(src);
3661 else
3662 dst[0] = as_vgpr(ctx, src);
3663 return;
3664 }
3665
3666 for (unsigned i = 0; i < count - 1; i++)
3667 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3668 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3669
3670 if (is_subdword && src.type() == RegType::sgpr) {
3671 src = as_vgpr(ctx, src);
3672 } else {
3673 /* use allocated_vec if possible */
3674 auto it = ctx->allocated_vec.find(src.id());
3675 if (it != ctx->allocated_vec.end()) {
3676 unsigned total_size = 0;
3677 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3678 total_size += it->second[i].bytes();
3679 if (total_size != src.bytes())
3680 goto split;
3681
3682 unsigned elem_size = it->second[0].bytes();
3683
3684 for (unsigned i = 0; i < count; i++) {
3685 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3686 goto split;
3687 }
3688
3689 for (unsigned i = 0; i < count; i++) {
3690 unsigned start_idx = offsets[i] / elem_size;
3691 unsigned op_count = dst[i].bytes() / elem_size;
3692 if (op_count == 1) {
3693 if (dst_type == RegType::sgpr)
3694 dst[i] = bld.as_uniform(it->second[start_idx]);
3695 else
3696 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3697 continue;
3698 }
3699
3700 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3701 for (unsigned j = 0; j < op_count; j++) {
3702 Temp tmp = it->second[start_idx + j];
3703 if (dst_type == RegType::sgpr)
3704 tmp = bld.as_uniform(tmp);
3705 vec->operands[j] = Operand(tmp);
3706 }
3707 vec->definitions[0] = Definition(dst[i]);
3708 bld.insert(std::move(vec));
3709 }
3710 return;
3711 }
3712 }
3713
3714 if (dst_type == RegType::sgpr)
3715 src = bld.as_uniform(src);
3716
3717 split:
3718 /* just split it */
3719 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3720 split->operands[0] = Operand(src);
3721 for (unsigned i = 0; i < count; i++)
3722 split->definitions[i] = Definition(dst[i]);
3723 bld.insert(std::move(split));
3724 }
3725
3726 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3727 int *start, int *count)
3728 {
3729 unsigned start_elem = ffs(todo_mask) - 1;
3730 bool skip = !(mask & (1 << start_elem));
3731 if (skip)
3732 mask = ~mask & todo_mask;
3733
3734 mask &= todo_mask;
3735
3736 u_bit_scan_consecutive_range(&mask, start, count);
3737
3738 return !skip;
3739 }
3740
3741 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3742 {
3743 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3744 }
3745
3746 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3747 Temp address, unsigned base_offset, unsigned align)
3748 {
3749 assert(util_is_power_of_two_nonzero(align));
3750 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3751
3752 Builder bld(ctx->program, ctx->block);
3753 bool large_ds_write = ctx->options->chip_class >= GFX7;
3754 bool usable_write2 = ctx->options->chip_class >= GFX7;
3755
3756 unsigned write_count = 0;
3757 Temp write_datas[32];
3758 unsigned offsets[32];
3759 aco_opcode opcodes[32];
3760
3761 wrmask = widen_mask(wrmask, elem_size_bytes);
3762
3763 uint32_t todo = u_bit_consecutive(0, data.bytes());
3764 while (todo) {
3765 int offset, bytes;
3766 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3767 offsets[write_count] = offset;
3768 opcodes[write_count] = aco_opcode::num_opcodes;
3769 write_count++;
3770 advance_write_mask(&todo, offset, bytes);
3771 continue;
3772 }
3773
3774 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3775 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3776 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3777 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3778
3779 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3780 aco_opcode op = aco_opcode::num_opcodes;
3781 if (bytes >= 16 && aligned16 && large_ds_write) {
3782 op = aco_opcode::ds_write_b128;
3783 bytes = 16;
3784 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3785 op = aco_opcode::ds_write_b96;
3786 bytes = 12;
3787 } else if (bytes >= 8 && aligned8) {
3788 op = aco_opcode::ds_write_b64;
3789 bytes = 8;
3790 } else if (bytes >= 4 && aligned4) {
3791 op = aco_opcode::ds_write_b32;
3792 bytes = 4;
3793 } else if (bytes >= 2 && aligned2) {
3794 op = aco_opcode::ds_write_b16;
3795 bytes = 2;
3796 } else if (bytes >= 1) {
3797 op = aco_opcode::ds_write_b8;
3798 bytes = 1;
3799 } else {
3800 assert(false);
3801 }
3802
3803 offsets[write_count] = offset;
3804 opcodes[write_count] = op;
3805 write_count++;
3806 advance_write_mask(&todo, offset, bytes);
3807 }
3808
3809 Operand m = load_lds_size_m0(bld);
3810
3811 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3812
3813 for (unsigned i = 0; i < write_count; i++) {
3814 aco_opcode op = opcodes[i];
3815 if (op == aco_opcode::num_opcodes)
3816 continue;
3817
3818 Temp data = write_datas[i];
3819
3820 unsigned second = write_count;
3821 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3822 for (second = i + 1; second < write_count; second++) {
3823 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3824 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3825 opcodes[second] = aco_opcode::num_opcodes;
3826 break;
3827 }
3828 }
3829 }
3830
3831 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3832 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3833
3834 unsigned inline_offset = base_offset + offsets[i];
3835 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3836 Temp address_offset = address;
3837 if (inline_offset > max_offset) {
3838 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3839 inline_offset = offsets[i];
3840 }
3841 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3842
3843 if (write2) {
3844 Temp second_data = write_datas[second];
3845 inline_offset /= data.bytes();
3846 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3847 } else {
3848 bld.ds(op, address_offset, data, m, inline_offset);
3849 }
3850 }
3851 }
3852
3853 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3854 {
3855 unsigned align = 16;
3856 if (const_offset)
3857 align = std::min(align, 1u << (ffs(const_offset) - 1));
3858
3859 return align;
3860 }
3861
3862
3863 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3864 {
3865 switch (bytes) {
3866 case 1:
3867 assert(!smem);
3868 return aco_opcode::buffer_store_byte;
3869 case 2:
3870 assert(!smem);
3871 return aco_opcode::buffer_store_short;
3872 case 4:
3873 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3874 case 8:
3875 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3876 case 12:
3877 assert(!smem);
3878 return aco_opcode::buffer_store_dwordx3;
3879 case 16:
3880 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3881 }
3882 unreachable("Unexpected store size");
3883 return aco_opcode::num_opcodes;
3884 }
3885
3886 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3887 Temp data, unsigned writemask, int swizzle_element_size,
3888 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3889 {
3890 unsigned write_count_with_skips = 0;
3891 bool skips[16];
3892
3893 /* determine how to split the data */
3894 unsigned todo = u_bit_consecutive(0, data.bytes());
3895 while (todo) {
3896 int offset, bytes;
3897 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3898 offsets[write_count_with_skips] = offset;
3899 if (skips[write_count_with_skips]) {
3900 advance_write_mask(&todo, offset, bytes);
3901 write_count_with_skips++;
3902 continue;
3903 }
3904
3905 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3906 * larger than swizzle_element_size */
3907 bytes = MIN2(bytes, swizzle_element_size);
3908 if (bytes % 4)
3909 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3910
3911 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3912 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3913 bytes = 8;
3914
3915 /* dword or larger stores have to be dword-aligned */
3916 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3917 unsigned align_offset = (instr ? nir_intrinsic_align_offset(instr) : 0) + offset;
3918 bool dword_aligned = align_offset % 4 == 0 && align_mul % 4 == 0;
3919 if (!dword_aligned)
3920 bytes = MIN2(bytes, (align_offset % 2 == 0 && align_mul % 2 == 0) ? 2 : 1);
3921
3922 advance_write_mask(&todo, offset, bytes);
3923 write_count_with_skips++;
3924 }
3925
3926 /* actually split data */
3927 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3928
3929 /* remove skips */
3930 for (unsigned i = 0; i < write_count_with_skips; i++) {
3931 if (skips[i])
3932 continue;
3933 write_datas[*write_count] = write_datas[i];
3934 offsets[*write_count] = offsets[i];
3935 (*write_count)++;
3936 }
3937 }
3938
3939 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3940 unsigned split_cnt = 0u, Temp dst = Temp())
3941 {
3942 Builder bld(ctx->program, ctx->block);
3943 unsigned dword_size = elem_size_bytes / 4;
3944
3945 if (!dst.id())
3946 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3947
3948 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3949 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3950 instr->definitions[0] = Definition(dst);
3951
3952 for (unsigned i = 0; i < cnt; ++i) {
3953 if (arr[i].id()) {
3954 assert(arr[i].size() == dword_size);
3955 allocated_vec[i] = arr[i];
3956 instr->operands[i] = Operand(arr[i]);
3957 } else {
3958 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3959 allocated_vec[i] = zero;
3960 instr->operands[i] = Operand(zero);
3961 }
3962 }
3963
3964 bld.insert(std::move(instr));
3965
3966 if (split_cnt)
3967 emit_split_vector(ctx, dst, split_cnt);
3968 else
3969 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3970
3971 return dst;
3972 }
3973
3974 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3975 {
3976 if (const_offset >= 4096) {
3977 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3978 const_offset %= 4096u;
3979
3980 if (!voffset.id())
3981 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3982 else if (unlikely(voffset.regClass() == s1))
3983 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3984 else if (likely(voffset.regClass() == v1))
3985 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3986 else
3987 unreachable("Unsupported register class of voffset");
3988 }
3989
3990 return const_offset;
3991 }
3992
3993 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3994 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false,
3995 bool swizzled = false)
3996 {
3997 assert(vdata.id());
3998 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3999 assert(vdata.size() >= 1 && vdata.size() <= 4);
4000
4001 Builder bld(ctx->program, ctx->block);
4002 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
4003 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
4004
4005 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
4006 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
4007 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
4008 /* offen */ !voffset_op.isUndefined(), /* swizzled */ swizzled,
4009 /* idxen*/ false, /* addr64 */ false, /* disable_wqm */ false, /* glc */ true,
4010 /* dlc*/ false, /* slc */ slc);
4011
4012 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
4013 }
4014
4015 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
4016 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
4017 bool allow_combining = true, bool reorder = true, bool slc = false)
4018 {
4019 Builder bld(ctx->program, ctx->block);
4020 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4021 assert(write_mask);
4022 write_mask = widen_mask(write_mask, elem_size_bytes);
4023
4024 unsigned write_count = 0;
4025 Temp write_datas[32];
4026 unsigned offsets[32];
4027 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
4028 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
4029
4030 for (unsigned i = 0; i < write_count; i++) {
4031 unsigned const_offset = offsets[i] + base_const_offset;
4032 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc, !allow_combining);
4033 }
4034 }
4035
4036 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4037 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4038 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4039 {
4040 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4041 assert((num_components * elem_size_bytes) == dst.bytes());
4042 assert(!!stride != allow_combining);
4043
4044 Builder bld(ctx->program, ctx->block);
4045
4046 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4047 info.component_stride = allow_combining ? 0 : stride;
4048 info.glc = true;
4049 info.swizzle_component_size = allow_combining ? 0 : 4;
4050 info.align_mul = MIN2(elem_size_bytes, 4);
4051 info.align_offset = 0;
4052 info.soffset = soffset;
4053 info.const_offset = base_const_offset;
4054 emit_mubuf_load(ctx, bld, &info);
4055 }
4056
4057 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)
4058 {
4059 Builder bld(ctx->program, ctx->block);
4060 Temp offset = base_offset.first;
4061 unsigned const_offset = base_offset.second;
4062
4063 if (!nir_src_is_const(*off_src)) {
4064 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4065 Temp with_stride;
4066
4067 /* Calculate indirect offset with stride */
4068 if (likely(indirect_offset_arg.regClass() == v1))
4069 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4070 else if (indirect_offset_arg.regClass() == s1)
4071 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4072 else
4073 unreachable("Unsupported register class of indirect offset");
4074
4075 /* Add to the supplied base offset */
4076 if (offset.id() == 0)
4077 offset = with_stride;
4078 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4079 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4080 else if (offset.size() == 1 && with_stride.size() == 1)
4081 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4082 else
4083 unreachable("Unsupported register class of indirect offset");
4084 } else {
4085 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4086 const_offset += const_offset_arg * stride;
4087 }
4088
4089 return std::make_pair(offset, const_offset);
4090 }
4091
4092 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4093 {
4094 Builder bld(ctx->program, ctx->block);
4095 Temp offset;
4096
4097 if (off1.first.id() && off2.first.id()) {
4098 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4099 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4100 else if (off1.first.size() == 1 && off2.first.size() == 1)
4101 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4102 else
4103 unreachable("Unsupported register class of indirect offset");
4104 } else {
4105 offset = off1.first.id() ? off1.first : off2.first;
4106 }
4107
4108 return std::make_pair(offset, off1.second + off2.second);
4109 }
4110
4111 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4112 {
4113 Builder bld(ctx->program, ctx->block);
4114 unsigned const_offset = offs.second * multiplier;
4115
4116 if (!offs.first.id())
4117 return std::make_pair(offs.first, const_offset);
4118
4119 Temp offset = unlikely(offs.first.regClass() == s1)
4120 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4121 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4122
4123 return std::make_pair(offset, const_offset);
4124 }
4125
4126 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4127 {
4128 Builder bld(ctx->program, ctx->block);
4129
4130 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4131 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4132 /* component is in bytes */
4133 const_offset += nir_intrinsic_component(instr) * component_stride;
4134
4135 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4136 nir_src *off_src = nir_get_io_offset_src(instr);
4137 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4138 }
4139
4140 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4141 {
4142 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4143 }
4144
4145 Temp get_tess_rel_patch_id(isel_context *ctx)
4146 {
4147 Builder bld(ctx->program, ctx->block);
4148
4149 switch (ctx->shader->info.stage) {
4150 case MESA_SHADER_TESS_CTRL:
4151 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4152 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4153 case MESA_SHADER_TESS_EVAL:
4154 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4155 default:
4156 unreachable("Unsupported stage in get_tess_rel_patch_id");
4157 }
4158 }
4159
4160 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4161 {
4162 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4163 Builder bld(ctx->program, ctx->block);
4164
4165 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4166 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4167
4168 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4169
4170 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4171 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4172
4173 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4174 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4175 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4176
4177 return offset_mul(ctx, offs, 4u);
4178 }
4179
4180 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4181 {
4182 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4183 Builder bld(ctx->program, ctx->block);
4184
4185 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4186 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4187 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4188 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4189
4190 std::pair<Temp, unsigned> offs = instr
4191 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4192 : std::make_pair(Temp(), 0u);
4193
4194 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4195 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4196
4197 if (per_vertex) {
4198 assert(instr);
4199
4200 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4201 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4202
4203 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4204 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4205 } else {
4206 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4207 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4208 }
4209
4210 return offs;
4211 }
4212
4213 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4214 {
4215 Builder bld(ctx->program, ctx->block);
4216
4217 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4218 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4219
4220 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4221
4222 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4223 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4224 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4225
4226 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4227 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4228
4229 return offs;
4230 }
4231
4232 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4233 {
4234 Builder bld(ctx->program, ctx->block);
4235
4236 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4237 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4238 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4239 unsigned attr_stride = ctx->tcs_num_patches;
4240
4241 std::pair<Temp, unsigned> offs = instr
4242 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4243 : std::make_pair(Temp(), 0u);
4244
4245 if (const_base_offset)
4246 offs.second += const_base_offset * attr_stride;
4247
4248 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4249 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4250 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4251
4252 return offs;
4253 }
4254
4255 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4256 {
4257 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4258
4259 if (mask == 0)
4260 return false;
4261
4262 unsigned drv_loc = nir_intrinsic_base(instr);
4263 nir_src *off_src = nir_get_io_offset_src(instr);
4264
4265 if (!nir_src_is_const(*off_src)) {
4266 *indirect = true;
4267 return false;
4268 }
4269
4270 *indirect = false;
4271 uint64_t slot = per_vertex
4272 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4273 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4274 return (((uint64_t) 1) << slot) & mask;
4275 }
4276
4277 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4278 {
4279 unsigned write_mask = nir_intrinsic_write_mask(instr);
4280 unsigned component = nir_intrinsic_component(instr);
4281 unsigned idx = nir_intrinsic_base(instr) + component;
4282
4283 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4284 if (off_instr->type != nir_instr_type_load_const)
4285 return false;
4286
4287 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4288 idx += nir_src_as_uint(instr->src[1]) * 4u;
4289
4290 if (instr->src[0].ssa->bit_size == 64)
4291 write_mask = widen_mask(write_mask, 2);
4292
4293 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4294
4295 for (unsigned i = 0; i < 8; ++i) {
4296 if (write_mask & (1 << i)) {
4297 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4298 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4299 }
4300 idx++;
4301 }
4302
4303 return true;
4304 }
4305
4306 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4307 {
4308 /* Only TCS per-vertex inputs are supported by this function.
4309 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4310 */
4311 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4312 return false;
4313
4314 nir_src *off_src = nir_get_io_offset_src(instr);
4315 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4316 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4317 bool can_use_temps = nir_src_is_const(*off_src) &&
4318 vertex_index_instr->type == nir_instr_type_intrinsic &&
4319 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4320
4321 if (!can_use_temps)
4322 return false;
4323
4324 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4325 Temp *src = &ctx->inputs.temps[idx];
4326 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4327
4328 return true;
4329 }
4330
4331 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4332 {
4333 Builder bld(ctx->program, ctx->block);
4334
4335 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4336 /* 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. */
4337 bool indirect_write;
4338 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4339 if (temp_only_input && !indirect_write)
4340 return;
4341 }
4342
4343 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4344 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4345 unsigned write_mask = nir_intrinsic_write_mask(instr);
4346 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4347
4348 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4349 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4350 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4351 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4352 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4353 } else {
4354 Temp lds_base;
4355
4356 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4357 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4358 unsigned itemsize = ctx->stage == vertex_geometry_gs
4359 ? ctx->program->info->vs.es_info.esgs_itemsize
4360 : ctx->program->info->tes.es_info.esgs_itemsize;
4361 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4362 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));
4363 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4364 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4365 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4366 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4367 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4368 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4369 */
4370 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4371 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4372 } else {
4373 unreachable("Invalid LS or ES stage");
4374 }
4375
4376 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4377 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4378 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4379 }
4380 }
4381
4382 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4383 {
4384 if (per_vertex)
4385 return false;
4386
4387 unsigned off = nir_intrinsic_base(instr) * 4u;
4388 return off == ctx->tcs_tess_lvl_out_loc ||
4389 off == ctx->tcs_tess_lvl_in_loc;
4390
4391 }
4392
4393 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4394 {
4395 uint64_t mask = per_vertex
4396 ? ctx->program->info->tcs.tes_inputs_read
4397 : ctx->program->info->tcs.tes_patch_inputs_read;
4398
4399 bool indirect_write = false;
4400 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4401 return indirect_write || output_read_by_tes;
4402 }
4403
4404 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4405 {
4406 uint64_t mask = per_vertex
4407 ? ctx->shader->info.outputs_read
4408 : ctx->shader->info.patch_outputs_read;
4409
4410 bool indirect_write = false;
4411 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4412 return indirect_write || output_read;
4413 }
4414
4415 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4416 {
4417 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4418 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4419
4420 Builder bld(ctx->program, ctx->block);
4421
4422 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4423 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4424 unsigned write_mask = nir_intrinsic_write_mask(instr);
4425
4426 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4427 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4428 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4429
4430 if (write_to_vmem) {
4431 std::pair<Temp, unsigned> vmem_offs = per_vertex
4432 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4433 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4434
4435 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));
4436 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4437 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);
4438 }
4439
4440 if (write_to_lds) {
4441 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4442 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4443 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4444 }
4445 }
4446
4447 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4448 {
4449 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4450 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4451
4452 Builder bld(ctx->program, ctx->block);
4453
4454 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4455 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4456 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4457 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4458
4459 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4460 }
4461
4462 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4463 {
4464 if (ctx->stage == vertex_vs ||
4465 ctx->stage == tess_eval_vs ||
4466 ctx->stage == fragment_fs ||
4467 ctx->stage == ngg_vertex_gs ||
4468 ctx->stage == ngg_tess_eval_gs ||
4469 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4470 bool stored_to_temps = store_output_to_temps(ctx, instr);
4471 if (!stored_to_temps) {
4472 fprintf(stderr, "Unimplemented output offset instruction:\n");
4473 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4474 fprintf(stderr, "\n");
4475 abort();
4476 }
4477 } else if (ctx->stage == vertex_es ||
4478 ctx->stage == vertex_ls ||
4479 ctx->stage == tess_eval_es ||
4480 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4481 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4482 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4483 visit_store_ls_or_es_output(ctx, instr);
4484 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4485 visit_store_tcs_output(ctx, instr, false);
4486 } else {
4487 unreachable("Shader stage not implemented");
4488 }
4489 }
4490
4491 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4492 {
4493 visit_load_tcs_output(ctx, instr, false);
4494 }
4495
4496 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4497 {
4498 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4499 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4500
4501 Builder bld(ctx->program, ctx->block);
4502
4503 if (dst.regClass() == v2b) {
4504 if (ctx->program->has_16bank_lds) {
4505 assert(ctx->options->chip_class <= GFX8);
4506 Builder::Result interp_p1 =
4507 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4508 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4509 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4510 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4511 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4512 bld.m0(prim_mask), interp_p1, idx, component);
4513 } else {
4514 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4515
4516 if (ctx->options->chip_class == GFX8)
4517 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4518
4519 Builder::Result interp_p1 =
4520 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4521 coord1, bld.m0(prim_mask), idx, component);
4522 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4523 interp_p1, idx, component);
4524 }
4525 } else {
4526 Builder::Result interp_p1 =
4527 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4528 bld.m0(prim_mask), idx, component);
4529
4530 if (ctx->program->has_16bank_lds)
4531 interp_p1.instr->operands[0].setLateKill(true);
4532
4533 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4534 bld.m0(prim_mask), interp_p1, idx, component);
4535 }
4536 }
4537
4538 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4539 {
4540 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4541 for (unsigned i = 0; i < num_components; i++)
4542 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4543 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4544 assert(num_components == 4);
4545 Builder bld(ctx->program, ctx->block);
4546 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4547 }
4548
4549 for (Operand& op : vec->operands)
4550 op = op.isUndefined() ? Operand(0u) : op;
4551
4552 vec->definitions[0] = Definition(dst);
4553 ctx->block->instructions.emplace_back(std::move(vec));
4554 emit_split_vector(ctx, dst, num_components);
4555 return;
4556 }
4557
4558 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4559 {
4560 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4561 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4562 unsigned idx = nir_intrinsic_base(instr);
4563 unsigned component = nir_intrinsic_component(instr);
4564 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4565
4566 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4567 if (offset) {
4568 assert(offset->u32 == 0);
4569 } else {
4570 /* the lower 15bit of the prim_mask contain the offset into LDS
4571 * while the upper bits contain the number of prims */
4572 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4573 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4574 Builder bld(ctx->program, ctx->block);
4575 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4576 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4577 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4578 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4579 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4580 }
4581
4582 if (instr->dest.ssa.num_components == 1) {
4583 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4584 } else {
4585 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4586 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4587 {
4588 Temp tmp = {ctx->program->allocateId(), v1};
4589 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4590 vec->operands[i] = Operand(tmp);
4591 }
4592 vec->definitions[0] = Definition(dst);
4593 ctx->block->instructions.emplace_back(std::move(vec));
4594 }
4595 }
4596
4597 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4598 unsigned offset, unsigned stride, unsigned channels)
4599 {
4600 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4601 if (vtx_info->chan_byte_size != 4 && channels == 3)
4602 return false;
4603 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4604 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4605 }
4606
4607 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4608 unsigned offset, unsigned stride, unsigned *channels)
4609 {
4610 if (!vtx_info->chan_byte_size) {
4611 *channels = vtx_info->num_channels;
4612 return vtx_info->chan_format;
4613 }
4614
4615 unsigned num_channels = *channels;
4616 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4617 unsigned new_channels = num_channels + 1;
4618 /* first, assume more loads is worse and try using a larger data format */
4619 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4620 new_channels++;
4621 /* don't make the attribute potentially out-of-bounds */
4622 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4623 new_channels = 5;
4624 }
4625
4626 if (new_channels == 5) {
4627 /* then try decreasing load size (at the cost of more loads) */
4628 new_channels = *channels;
4629 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4630 new_channels--;
4631 }
4632
4633 if (new_channels < *channels)
4634 *channels = new_channels;
4635 num_channels = new_channels;
4636 }
4637
4638 switch (vtx_info->chan_format) {
4639 case V_008F0C_BUF_DATA_FORMAT_8:
4640 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4641 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4642 case V_008F0C_BUF_DATA_FORMAT_16:
4643 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4644 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4645 case V_008F0C_BUF_DATA_FORMAT_32:
4646 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4647 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4648 }
4649 unreachable("shouldn't reach here");
4650 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4651 }
4652
4653 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4654 * so we may need to fix it up. */
4655 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4656 {
4657 Builder bld(ctx->program, ctx->block);
4658
4659 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4660 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4661
4662 /* For the integer-like cases, do a natural sign extension.
4663 *
4664 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4665 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4666 * exponent.
4667 */
4668 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4669 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4670
4671 /* Convert back to the right type. */
4672 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4673 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4674 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4675 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4676 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4677 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4678 }
4679
4680 return alpha;
4681 }
4682
4683 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4684 {
4685 Builder bld(ctx->program, ctx->block);
4686 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4687 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4688
4689 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4690 if (off_instr->type != nir_instr_type_load_const) {
4691 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4692 nir_print_instr(off_instr, stderr);
4693 fprintf(stderr, "\n");
4694 }
4695 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4696
4697 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4698
4699 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4700 unsigned component = nir_intrinsic_component(instr);
4701 unsigned bitsize = instr->dest.ssa.bit_size;
4702 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4703 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4704 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4705 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4706
4707 unsigned dfmt = attrib_format & 0xf;
4708 unsigned nfmt = (attrib_format >> 4) & 0x7;
4709 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4710
4711 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4712 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4713 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4714 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4715 if (post_shuffle)
4716 num_channels = MAX2(num_channels, 3);
4717
4718 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4719 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4720
4721 Temp index;
4722 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4723 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4724 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4725 if (divisor) {
4726 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4727 if (divisor != 1) {
4728 Temp divided = bld.tmp(v1);
4729 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4730 index = bld.vadd32(bld.def(v1), start_instance, divided);
4731 } else {
4732 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4733 }
4734 } else {
4735 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4736 }
4737 } else {
4738 index = bld.vadd32(bld.def(v1),
4739 get_arg(ctx, ctx->args->ac.base_vertex),
4740 get_arg(ctx, ctx->args->ac.vertex_id));
4741 }
4742
4743 Temp channels[num_channels];
4744 unsigned channel_start = 0;
4745 bool direct_fetch = false;
4746
4747 /* skip unused channels at the start */
4748 if (vtx_info->chan_byte_size && !post_shuffle) {
4749 channel_start = ffs(mask) - 1;
4750 for (unsigned i = 0; i < channel_start; i++)
4751 channels[i] = Temp(0, s1);
4752 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4753 num_channels = 3 - (ffs(mask) - 1);
4754 }
4755
4756 /* load channels */
4757 while (channel_start < num_channels) {
4758 unsigned fetch_component = num_channels - channel_start;
4759 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4760 bool expanded = false;
4761
4762 /* use MUBUF when possible to avoid possible alignment issues */
4763 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4764 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4765 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4766 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4767 vtx_info->chan_byte_size == 4;
4768 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4769 if (!use_mubuf) {
4770 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4771 } else {
4772 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4773 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4774 fetch_component = 4;
4775 expanded = true;
4776 }
4777 }
4778
4779 unsigned fetch_bytes = fetch_component * bitsize / 8;
4780
4781 Temp fetch_index = index;
4782 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4783 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4784 fetch_offset = fetch_offset % attrib_stride;
4785 }
4786
4787 Operand soffset(0u);
4788 if (fetch_offset >= 4096) {
4789 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4790 fetch_offset %= 4096;
4791 }
4792
4793 aco_opcode opcode;
4794 switch (fetch_bytes) {
4795 case 2:
4796 assert(!use_mubuf && bitsize == 16);
4797 opcode = aco_opcode::tbuffer_load_format_d16_x;
4798 break;
4799 case 4:
4800 if (bitsize == 16) {
4801 assert(!use_mubuf);
4802 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4803 } else {
4804 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4805 }
4806 break;
4807 case 6:
4808 assert(!use_mubuf && bitsize == 16);
4809 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4810 break;
4811 case 8:
4812 if (bitsize == 16) {
4813 assert(!use_mubuf);
4814 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4815 } else {
4816 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4817 }
4818 break;
4819 case 12:
4820 assert(ctx->options->chip_class >= GFX7 ||
4821 (!use_mubuf && ctx->options->chip_class == GFX6));
4822 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4823 break;
4824 case 16:
4825 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4826 break;
4827 default:
4828 unreachable("Unimplemented load_input vector size");
4829 }
4830
4831 Temp fetch_dst;
4832 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4833 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4834 num_channels <= 3)) {
4835 direct_fetch = true;
4836 fetch_dst = dst;
4837 } else {
4838 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4839 }
4840
4841 if (use_mubuf) {
4842 Instruction *mubuf = bld.mubuf(opcode,
4843 Definition(fetch_dst), list, fetch_index, soffset,
4844 fetch_offset, false, false, true).instr;
4845 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4846 } else {
4847 Instruction *mtbuf = bld.mtbuf(opcode,
4848 Definition(fetch_dst), list, fetch_index, soffset,
4849 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4850 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4851 }
4852
4853 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4854
4855 if (fetch_component == 1) {
4856 channels[channel_start] = fetch_dst;
4857 } else {
4858 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4859 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4860 bitsize == 16 ? v2b : v1);
4861 }
4862
4863 channel_start += fetch_component;
4864 }
4865
4866 if (!direct_fetch) {
4867 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4868 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4869
4870 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4871 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4872 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4873
4874 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4875 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4876 unsigned num_temp = 0;
4877 for (unsigned i = 0; i < dst.size(); i++) {
4878 unsigned idx = i + component;
4879 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4880 Temp channel = channels[swizzle[idx]];
4881 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4882 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4883 vec->operands[i] = Operand(channel);
4884
4885 num_temp++;
4886 elems[i] = channel;
4887 } else if (is_float && idx == 3) {
4888 vec->operands[i] = Operand(0x3f800000u);
4889 } else if (!is_float && idx == 3) {
4890 vec->operands[i] = Operand(1u);
4891 } else {
4892 vec->operands[i] = Operand(0u);
4893 }
4894 }
4895 vec->definitions[0] = Definition(dst);
4896 ctx->block->instructions.emplace_back(std::move(vec));
4897 emit_split_vector(ctx, dst, dst.size());
4898
4899 if (num_temp == dst.size())
4900 ctx->allocated_vec.emplace(dst.id(), elems);
4901 }
4902 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4903 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4904 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4905 if (off_instr->type != nir_instr_type_load_const ||
4906 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4907 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4908 nir_print_instr(off_instr, stderr);
4909 fprintf(stderr, "\n");
4910 }
4911
4912 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4913 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4914 if (offset) {
4915 assert(offset->u32 == 0);
4916 } else {
4917 /* the lower 15bit of the prim_mask contain the offset into LDS
4918 * while the upper bits contain the number of prims */
4919 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4920 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4921 Builder bld(ctx->program, ctx->block);
4922 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4923 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4924 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4925 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4926 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4927 }
4928
4929 unsigned idx = nir_intrinsic_base(instr);
4930 unsigned component = nir_intrinsic_component(instr);
4931 unsigned vertex_id = 2; /* P0 */
4932
4933 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4934 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4935 switch (src0->u32) {
4936 case 0:
4937 vertex_id = 2; /* P0 */
4938 break;
4939 case 1:
4940 vertex_id = 0; /* P10 */
4941 break;
4942 case 2:
4943 vertex_id = 1; /* P20 */
4944 break;
4945 default:
4946 unreachable("invalid vertex index");
4947 }
4948 }
4949
4950 if (dst.size() == 1) {
4951 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4952 } else {
4953 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4954 for (unsigned i = 0; i < dst.size(); i++)
4955 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4956 vec->definitions[0] = Definition(dst);
4957 bld.insert(std::move(vec));
4958 }
4959
4960 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4961 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4962 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4963 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4964 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4965
4966 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4967 } else {
4968 unreachable("Shader stage not implemented");
4969 }
4970 }
4971
4972 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4973 {
4974 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4975
4976 Builder bld(ctx->program, ctx->block);
4977 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4978 Temp vertex_offset;
4979
4980 if (!nir_src_is_const(*vertex_src)) {
4981 /* better code could be created, but this case probably doesn't happen
4982 * much in practice */
4983 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4984 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4985 Temp elem;
4986
4987 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4988 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4989 if (i % 2u)
4990 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4991 } else {
4992 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4993 }
4994
4995 if (vertex_offset.id()) {
4996 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4997 Operand(i), indirect_vertex);
4998 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4999 } else {
5000 vertex_offset = elem;
5001 }
5002 }
5003
5004 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5005 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
5006 } else {
5007 unsigned vertex = nir_src_as_uint(*vertex_src);
5008 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5009 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5010 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
5011 Operand((vertex % 2u) * 16u), Operand(16u));
5012 else
5013 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
5014 }
5015
5016 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
5017 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
5018 return offset_mul(ctx, offs, 4u);
5019 }
5020
5021 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5022 {
5023 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
5024
5025 Builder bld(ctx->program, ctx->block);
5026 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5027 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5028
5029 if (ctx->stage == geometry_gs) {
5030 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
5031 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
5032 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);
5033 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
5034 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
5035 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5036 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5037 } else {
5038 unreachable("Unsupported GS stage.");
5039 }
5040 }
5041
5042 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5043 {
5044 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5045
5046 Builder bld(ctx->program, ctx->block);
5047 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5048
5049 if (load_input_from_temps(ctx, instr, dst))
5050 return;
5051
5052 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
5053 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5054 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5055
5056 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5057 }
5058
5059 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5060 {
5061 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5062
5063 Builder bld(ctx->program, ctx->block);
5064
5065 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5066 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
5067 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5068
5069 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5070 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5071
5072 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5073 }
5074
5075 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5076 {
5077 switch (ctx->shader->info.stage) {
5078 case MESA_SHADER_GEOMETRY:
5079 visit_load_gs_per_vertex_input(ctx, instr);
5080 break;
5081 case MESA_SHADER_TESS_CTRL:
5082 visit_load_tcs_per_vertex_input(ctx, instr);
5083 break;
5084 case MESA_SHADER_TESS_EVAL:
5085 visit_load_tes_per_vertex_input(ctx, instr);
5086 break;
5087 default:
5088 unreachable("Unimplemented shader stage");
5089 }
5090 }
5091
5092 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5093 {
5094 visit_load_tcs_output(ctx, instr, true);
5095 }
5096
5097 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5098 {
5099 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5100 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5101
5102 visit_store_tcs_output(ctx, instr, true);
5103 }
5104
5105 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5106 {
5107 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5108
5109 Builder bld(ctx->program, ctx->block);
5110 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5111
5112 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5113 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5114 Operand tes_w(0u);
5115
5116 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5117 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5118 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5119 tes_w = Operand(tmp);
5120 }
5121
5122 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5123 emit_split_vector(ctx, tess_coord, 3);
5124 }
5125
5126 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5127 {
5128 if (ctx->program->info->need_indirect_descriptor_sets) {
5129 Builder bld(ctx->program, ctx->block);
5130 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5131 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5132 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5133 }
5134
5135 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5136 }
5137
5138
5139 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5140 {
5141 Builder bld(ctx->program, ctx->block);
5142 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5143 if (!nir_dest_is_divergent(instr->dest))
5144 index = bld.as_uniform(index);
5145 unsigned desc_set = nir_intrinsic_desc_set(instr);
5146 unsigned binding = nir_intrinsic_binding(instr);
5147
5148 Temp desc_ptr;
5149 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5150 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5151 unsigned offset = layout->binding[binding].offset;
5152 unsigned stride;
5153 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5154 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5155 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5156 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5157 offset = pipeline_layout->push_constant_size + 16 * idx;
5158 stride = 16;
5159 } else {
5160 desc_ptr = load_desc_ptr(ctx, desc_set);
5161 stride = layout->binding[binding].size;
5162 }
5163
5164 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5165 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5166 if (stride != 1) {
5167 if (nir_const_index) {
5168 const_index = const_index * stride;
5169 } else if (index.type() == RegType::vgpr) {
5170 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5171 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5172 } else {
5173 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5174 }
5175 }
5176 if (offset) {
5177 if (nir_const_index) {
5178 const_index = const_index + offset;
5179 } else if (index.type() == RegType::vgpr) {
5180 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5181 } else {
5182 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5183 }
5184 }
5185
5186 if (nir_const_index && const_index == 0) {
5187 index = desc_ptr;
5188 } else if (index.type() == RegType::vgpr) {
5189 index = bld.vadd32(bld.def(v1),
5190 nir_const_index ? Operand(const_index) : Operand(index),
5191 Operand(desc_ptr));
5192 } else {
5193 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5194 nir_const_index ? Operand(const_index) : Operand(index),
5195 Operand(desc_ptr));
5196 }
5197
5198 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5199 }
5200
5201 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5202 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5203 bool glc=false, bool readonly=true, bool allow_smem=true)
5204 {
5205 Builder bld(ctx->program, ctx->block);
5206
5207 bool use_smem = dst.type() != RegType::vgpr && (!glc || ctx->options->chip_class >= GFX8) && allow_smem;
5208 if (use_smem)
5209 offset = bld.as_uniform(offset);
5210
5211 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5212 info.glc = glc;
5213 info.barrier = readonly ? barrier_none : barrier_buffer;
5214 info.can_reorder = readonly;
5215 info.align_mul = align_mul;
5216 info.align_offset = align_offset;
5217 if (use_smem)
5218 emit_smem_load(ctx, bld, &info);
5219 else
5220 emit_mubuf_load(ctx, bld, &info);
5221 }
5222
5223 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5224 {
5225 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5226 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5227
5228 Builder bld(ctx->program, ctx->block);
5229
5230 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5231 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5232 unsigned binding = nir_intrinsic_binding(idx_instr);
5233 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5234
5235 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5236 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5237 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5238 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5239 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5240 if (ctx->options->chip_class >= GFX10) {
5241 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5242 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5243 S_008F0C_RESOURCE_LEVEL(1);
5244 } else {
5245 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5246 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5247 }
5248 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5249 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5250 Operand(0xFFFFFFFFu),
5251 Operand(desc_type));
5252 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5253 rsrc, upper_dwords);
5254 } else {
5255 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5256 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5257 }
5258 unsigned size = instr->dest.ssa.bit_size / 8;
5259 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5260 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5261 }
5262
5263 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5264 {
5265 Builder bld(ctx->program, ctx->block);
5266 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5267 unsigned offset = nir_intrinsic_base(instr);
5268 unsigned count = instr->dest.ssa.num_components;
5269 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5270
5271 if (index_cv && instr->dest.ssa.bit_size == 32) {
5272 unsigned start = (offset + index_cv->u32) / 4u;
5273 start -= ctx->args->ac.base_inline_push_consts;
5274 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5275 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5276 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5277 for (unsigned i = 0; i < count; ++i) {
5278 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5279 vec->operands[i] = Operand{elems[i]};
5280 }
5281 vec->definitions[0] = Definition(dst);
5282 ctx->block->instructions.emplace_back(std::move(vec));
5283 ctx->allocated_vec.emplace(dst.id(), elems);
5284 return;
5285 }
5286 }
5287
5288 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5289 if (offset != 0) // TODO check if index != 0 as well
5290 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5291 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5292 Temp vec = dst;
5293 bool trim = false;
5294 bool aligned = true;
5295
5296 if (instr->dest.ssa.bit_size == 8) {
5297 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5298 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5299 if (!aligned)
5300 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5301 } else if (instr->dest.ssa.bit_size == 16) {
5302 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5303 if (!aligned)
5304 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5305 }
5306
5307 aco_opcode op;
5308
5309 switch (vec.size()) {
5310 case 1:
5311 op = aco_opcode::s_load_dword;
5312 break;
5313 case 2:
5314 op = aco_opcode::s_load_dwordx2;
5315 break;
5316 case 3:
5317 vec = bld.tmp(s4);
5318 trim = true;
5319 case 4:
5320 op = aco_opcode::s_load_dwordx4;
5321 break;
5322 case 6:
5323 vec = bld.tmp(s8);
5324 trim = true;
5325 case 8:
5326 op = aco_opcode::s_load_dwordx8;
5327 break;
5328 default:
5329 unreachable("unimplemented or forbidden load_push_constant.");
5330 }
5331
5332 static_cast<SMEM_instruction*>(bld.smem(op, Definition(vec), ptr, index).instr)->prevent_overflow = true;
5333
5334 if (!aligned) {
5335 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5336 byte_align_scalar(ctx, vec, byte_offset, dst);
5337 return;
5338 }
5339
5340 if (trim) {
5341 emit_split_vector(ctx, vec, 4);
5342 RegClass rc = dst.size() == 3 ? s1 : s2;
5343 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5344 emit_extract_vector(ctx, vec, 0, rc),
5345 emit_extract_vector(ctx, vec, 1, rc),
5346 emit_extract_vector(ctx, vec, 2, rc));
5347
5348 }
5349 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5350 }
5351
5352 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5353 {
5354 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5355
5356 Builder bld(ctx->program, ctx->block);
5357
5358 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5359 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5360 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5361 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5362 if (ctx->options->chip_class >= GFX10) {
5363 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5364 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5365 S_008F0C_RESOURCE_LEVEL(1);
5366 } else {
5367 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5368 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5369 }
5370
5371 unsigned base = nir_intrinsic_base(instr);
5372 unsigned range = nir_intrinsic_range(instr);
5373
5374 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5375 if (base && offset.type() == RegType::sgpr)
5376 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5377 else if (base && offset.type() == RegType::vgpr)
5378 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5379
5380 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5381 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5382 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5383 Operand(desc_type));
5384 unsigned size = instr->dest.ssa.bit_size / 8;
5385 // TODO: get alignment information for subdword constants
5386 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5387 }
5388
5389 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5390 {
5391 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5392 ctx->cf_info.exec_potentially_empty_discard = true;
5393
5394 ctx->program->needs_exact = true;
5395
5396 // TODO: optimize uniform conditions
5397 Builder bld(ctx->program, ctx->block);
5398 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5399 assert(src.regClass() == bld.lm);
5400 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5401 bld.pseudo(aco_opcode::p_discard_if, src);
5402 ctx->block->kind |= block_kind_uses_discard_if;
5403 return;
5404 }
5405
5406 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5407 {
5408 Builder bld(ctx->program, ctx->block);
5409
5410 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5411 ctx->cf_info.exec_potentially_empty_discard = true;
5412
5413 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5414 ctx->cf_info.parent_loop.has_divergent_continue;
5415
5416 if (ctx->block->loop_nest_depth &&
5417 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5418 /* we handle discards the same way as jump instructions */
5419 append_logical_end(ctx->block);
5420
5421 /* in loops, discard behaves like break */
5422 Block *linear_target = ctx->cf_info.parent_loop.exit;
5423 ctx->block->kind |= block_kind_discard;
5424
5425 if (!divergent) {
5426 /* uniform discard - loop ends here */
5427 assert(nir_instr_is_last(&instr->instr));
5428 ctx->block->kind |= block_kind_uniform;
5429 ctx->cf_info.has_branch = true;
5430 bld.branch(aco_opcode::p_branch);
5431 add_linear_edge(ctx->block->index, linear_target);
5432 return;
5433 }
5434
5435 /* we add a break right behind the discard() instructions */
5436 ctx->block->kind |= block_kind_break;
5437 unsigned idx = ctx->block->index;
5438
5439 ctx->cf_info.parent_loop.has_divergent_branch = true;
5440 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5441
5442 /* remove critical edges from linear CFG */
5443 bld.branch(aco_opcode::p_branch);
5444 Block* break_block = ctx->program->create_and_insert_block();
5445 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5446 break_block->kind |= block_kind_uniform;
5447 add_linear_edge(idx, break_block);
5448 add_linear_edge(break_block->index, linear_target);
5449 bld.reset(break_block);
5450 bld.branch(aco_opcode::p_branch);
5451
5452 Block* continue_block = ctx->program->create_and_insert_block();
5453 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5454 add_linear_edge(idx, continue_block);
5455 append_logical_start(continue_block);
5456 ctx->block = continue_block;
5457
5458 return;
5459 }
5460
5461 /* it can currently happen that NIR doesn't remove the unreachable code */
5462 if (!nir_instr_is_last(&instr->instr)) {
5463 ctx->program->needs_exact = true;
5464 /* save exec somewhere temporarily so that it doesn't get
5465 * overwritten before the discard from outer exec masks */
5466 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5467 bld.pseudo(aco_opcode::p_discard_if, cond);
5468 ctx->block->kind |= block_kind_uses_discard_if;
5469 return;
5470 }
5471
5472 /* This condition is incorrect for uniformly branched discards in a loop
5473 * predicated by a divergent condition, but the above code catches that case
5474 * and the discard would end up turning into a discard_if.
5475 * For example:
5476 * if (divergent) {
5477 * while (...) {
5478 * if (uniform) {
5479 * discard;
5480 * }
5481 * }
5482 * }
5483 */
5484 if (!ctx->cf_info.parent_if.is_divergent) {
5485 /* program just ends here */
5486 ctx->block->kind |= block_kind_uniform;
5487 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5488 0 /* enabled mask */, 9 /* dest */,
5489 false /* compressed */, true/* done */, true /* valid mask */);
5490 bld.sopp(aco_opcode::s_endpgm);
5491 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5492 } else {
5493 ctx->block->kind |= block_kind_discard;
5494 /* branch and linear edge is added by visit_if() */
5495 }
5496 }
5497
5498 enum aco_descriptor_type {
5499 ACO_DESC_IMAGE,
5500 ACO_DESC_FMASK,
5501 ACO_DESC_SAMPLER,
5502 ACO_DESC_BUFFER,
5503 ACO_DESC_PLANE_0,
5504 ACO_DESC_PLANE_1,
5505 ACO_DESC_PLANE_2,
5506 };
5507
5508 static bool
5509 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5510 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5511 return false;
5512 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5513 return dim == ac_image_cube ||
5514 dim == ac_image_1darray ||
5515 dim == ac_image_2darray ||
5516 dim == ac_image_2darraymsaa;
5517 }
5518
5519 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5520 enum aco_descriptor_type desc_type,
5521 const nir_tex_instr *tex_instr, bool image, bool write)
5522 {
5523 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5524 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5525 if (it != ctx->tex_desc.end())
5526 return it->second;
5527 */
5528 Temp index = Temp();
5529 bool index_set = false;
5530 unsigned constant_index = 0;
5531 unsigned descriptor_set;
5532 unsigned base_index;
5533 Builder bld(ctx->program, ctx->block);
5534
5535 if (!deref_instr) {
5536 assert(tex_instr && !image);
5537 descriptor_set = 0;
5538 base_index = tex_instr->sampler_index;
5539 } else {
5540 while(deref_instr->deref_type != nir_deref_type_var) {
5541 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5542 if (!array_size)
5543 array_size = 1;
5544
5545 assert(deref_instr->deref_type == nir_deref_type_array);
5546 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5547 if (const_value) {
5548 constant_index += array_size * const_value->u32;
5549 } else {
5550 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5551 if (indirect.type() == RegType::vgpr)
5552 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5553
5554 if (array_size != 1)
5555 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5556
5557 if (!index_set) {
5558 index = indirect;
5559 index_set = true;
5560 } else {
5561 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5562 }
5563 }
5564
5565 deref_instr = nir_src_as_deref(deref_instr->parent);
5566 }
5567 descriptor_set = deref_instr->var->data.descriptor_set;
5568 base_index = deref_instr->var->data.binding;
5569 }
5570
5571 Temp list = load_desc_ptr(ctx, descriptor_set);
5572 list = convert_pointer_to_64_bit(ctx, list);
5573
5574 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5575 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5576 unsigned offset = binding->offset;
5577 unsigned stride = binding->size;
5578 aco_opcode opcode;
5579 RegClass type;
5580
5581 assert(base_index < layout->binding_count);
5582
5583 switch (desc_type) {
5584 case ACO_DESC_IMAGE:
5585 type = s8;
5586 opcode = aco_opcode::s_load_dwordx8;
5587 break;
5588 case ACO_DESC_FMASK:
5589 type = s8;
5590 opcode = aco_opcode::s_load_dwordx8;
5591 offset += 32;
5592 break;
5593 case ACO_DESC_SAMPLER:
5594 type = s4;
5595 opcode = aco_opcode::s_load_dwordx4;
5596 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5597 offset += radv_combined_image_descriptor_sampler_offset(binding);
5598 break;
5599 case ACO_DESC_BUFFER:
5600 type = s4;
5601 opcode = aco_opcode::s_load_dwordx4;
5602 break;
5603 case ACO_DESC_PLANE_0:
5604 case ACO_DESC_PLANE_1:
5605 type = s8;
5606 opcode = aco_opcode::s_load_dwordx8;
5607 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5608 break;
5609 case ACO_DESC_PLANE_2:
5610 type = s4;
5611 opcode = aco_opcode::s_load_dwordx4;
5612 offset += 64;
5613 break;
5614 default:
5615 unreachable("invalid desc_type\n");
5616 }
5617
5618 offset += constant_index * stride;
5619
5620 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5621 (!index_set || binding->immutable_samplers_equal)) {
5622 if (binding->immutable_samplers_equal)
5623 constant_index = 0;
5624
5625 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5626 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5627 Operand(samplers[constant_index * 4 + 0]),
5628 Operand(samplers[constant_index * 4 + 1]),
5629 Operand(samplers[constant_index * 4 + 2]),
5630 Operand(samplers[constant_index * 4 + 3]));
5631 }
5632
5633 Operand off;
5634 if (!index_set) {
5635 off = bld.copy(bld.def(s1), Operand(offset));
5636 } else {
5637 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5638 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5639 }
5640
5641 Temp res = bld.smem(opcode, bld.def(type), list, off);
5642
5643 if (desc_type == ACO_DESC_PLANE_2) {
5644 Temp components[8];
5645 for (unsigned i = 0; i < 8; i++)
5646 components[i] = bld.tmp(s1);
5647 bld.pseudo(aco_opcode::p_split_vector,
5648 Definition(components[0]),
5649 Definition(components[1]),
5650 Definition(components[2]),
5651 Definition(components[3]),
5652 res);
5653
5654 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5655 bld.pseudo(aco_opcode::p_split_vector,
5656 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5657 Definition(components[4]),
5658 Definition(components[5]),
5659 Definition(components[6]),
5660 Definition(components[7]),
5661 desc2);
5662
5663 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5664 components[0], components[1], components[2], components[3],
5665 components[4], components[5], components[6], components[7]);
5666 }
5667
5668 return res;
5669 }
5670
5671 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5672 {
5673 switch (dim) {
5674 case GLSL_SAMPLER_DIM_BUF:
5675 return 1;
5676 case GLSL_SAMPLER_DIM_1D:
5677 return array ? 2 : 1;
5678 case GLSL_SAMPLER_DIM_2D:
5679 return array ? 3 : 2;
5680 case GLSL_SAMPLER_DIM_MS:
5681 return array ? 4 : 3;
5682 case GLSL_SAMPLER_DIM_3D:
5683 case GLSL_SAMPLER_DIM_CUBE:
5684 return 3;
5685 case GLSL_SAMPLER_DIM_RECT:
5686 case GLSL_SAMPLER_DIM_SUBPASS:
5687 return 2;
5688 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5689 return 3;
5690 default:
5691 break;
5692 }
5693 return 0;
5694 }
5695
5696
5697 /* Adjust the sample index according to FMASK.
5698 *
5699 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5700 * which is the identity mapping. Each nibble says which physical sample
5701 * should be fetched to get that sample.
5702 *
5703 * For example, 0x11111100 means there are only 2 samples stored and
5704 * the second sample covers 3/4 of the pixel. When reading samples 0
5705 * and 1, return physical sample 0 (determined by the first two 0s
5706 * in FMASK), otherwise return physical sample 1.
5707 *
5708 * The sample index should be adjusted as follows:
5709 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5710 */
5711 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5712 {
5713 Builder bld(ctx->program, ctx->block);
5714 Temp fmask = bld.tmp(v1);
5715 unsigned dim = ctx->options->chip_class >= GFX10
5716 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5717 : 0;
5718
5719 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5720 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5721 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5722 load->operands[0] = Operand(fmask_desc_ptr);
5723 load->operands[1] = Operand(s4); /* no sampler */
5724 load->operands[2] = Operand(coord);
5725 load->definitions[0] = Definition(fmask);
5726 load->glc = false;
5727 load->dlc = false;
5728 load->dmask = 0x1;
5729 load->unrm = true;
5730 load->da = da;
5731 load->dim = dim;
5732 load->can_reorder = true; /* fmask images shouldn't be modified */
5733 ctx->block->instructions.emplace_back(std::move(load));
5734
5735 Operand sample_index4;
5736 if (sample_index.isConstant()) {
5737 if (sample_index.constantValue() < 16) {
5738 sample_index4 = Operand(sample_index.constantValue() << 2);
5739 } else {
5740 sample_index4 = Operand(0u);
5741 }
5742 } else if (sample_index.regClass() == s1) {
5743 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5744 } else {
5745 assert(sample_index.regClass() == v1);
5746 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5747 }
5748
5749 Temp final_sample;
5750 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5751 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5752 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5753 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5754 else
5755 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5756
5757 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5758 * resource descriptor is 0 (invalid),
5759 */
5760 Temp compare = bld.tmp(bld.lm);
5761 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5762 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5763
5764 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5765
5766 /* Replace the MSAA sample index. */
5767 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5768 }
5769
5770 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5771 {
5772
5773 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5774 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5775 bool is_array = glsl_sampler_type_is_array(type);
5776 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5777 assert(!add_frag_pos && "Input attachments should be lowered.");
5778 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5779 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5780 int count = image_type_to_components_count(dim, is_array);
5781 std::vector<Temp> coords(count);
5782 Builder bld(ctx->program, ctx->block);
5783
5784 if (is_ms) {
5785 count--;
5786 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5787 /* get sample index */
5788 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5789 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5790 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5791 std::vector<Temp> fmask_load_address;
5792 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5793 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5794
5795 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5796 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5797 } else {
5798 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5799 }
5800 }
5801
5802 if (gfx9_1d) {
5803 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5804 coords.resize(coords.size() + 1);
5805 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5806 if (is_array)
5807 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5808 } else {
5809 for (int i = 0; i < count; i++)
5810 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5811 }
5812
5813 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5814 instr->intrinsic == nir_intrinsic_image_deref_store) {
5815 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5816 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5817
5818 if (!level_zero)
5819 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5820 }
5821
5822 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5823 for (unsigned i = 0; i < coords.size(); i++)
5824 vec->operands[i] = Operand(coords[i]);
5825 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5826 vec->definitions[0] = Definition(res);
5827 ctx->block->instructions.emplace_back(std::move(vec));
5828 return res;
5829 }
5830
5831
5832 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5833 {
5834 Builder bld(ctx->program, ctx->block);
5835 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5836 const struct glsl_type *type = glsl_without_array(var->type);
5837 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5838 bool is_array = glsl_sampler_type_is_array(type);
5839 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5840
5841 if (dim == GLSL_SAMPLER_DIM_BUF) {
5842 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5843 unsigned num_channels = util_last_bit(mask);
5844 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5845 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5846
5847 aco_opcode opcode;
5848 switch (num_channels) {
5849 case 1:
5850 opcode = aco_opcode::buffer_load_format_x;
5851 break;
5852 case 2:
5853 opcode = aco_opcode::buffer_load_format_xy;
5854 break;
5855 case 3:
5856 opcode = aco_opcode::buffer_load_format_xyz;
5857 break;
5858 case 4:
5859 opcode = aco_opcode::buffer_load_format_xyzw;
5860 break;
5861 default:
5862 unreachable(">4 channel buffer image load");
5863 }
5864 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5865 load->operands[0] = Operand(rsrc);
5866 load->operands[1] = Operand(vindex);
5867 load->operands[2] = Operand((uint32_t) 0);
5868 Temp tmp;
5869 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5870 tmp = dst;
5871 else
5872 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5873 load->definitions[0] = Definition(tmp);
5874 load->idxen = true;
5875 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5876 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5877 load->barrier = barrier_image;
5878 ctx->block->instructions.emplace_back(std::move(load));
5879
5880 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5881 return;
5882 }
5883
5884 Temp coords = get_image_coords(ctx, instr, type);
5885 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5886
5887 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5888 unsigned num_components = util_bitcount(dmask);
5889 Temp tmp;
5890 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5891 tmp = dst;
5892 else
5893 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5894
5895 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5896 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5897
5898 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5899 load->operands[0] = Operand(resource);
5900 load->operands[1] = Operand(s4); /* no sampler */
5901 load->operands[2] = Operand(coords);
5902 load->definitions[0] = Definition(tmp);
5903 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5904 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5905 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5906 load->dmask = dmask;
5907 load->unrm = true;
5908 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5909 load->barrier = barrier_image;
5910 ctx->block->instructions.emplace_back(std::move(load));
5911
5912 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5913 return;
5914 }
5915
5916 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5917 {
5918 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5919 const struct glsl_type *type = glsl_without_array(var->type);
5920 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5921 bool is_array = glsl_sampler_type_is_array(type);
5922 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5923
5924 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5925
5926 if (dim == GLSL_SAMPLER_DIM_BUF) {
5927 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5928 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5929 aco_opcode opcode;
5930 switch (data.size()) {
5931 case 1:
5932 opcode = aco_opcode::buffer_store_format_x;
5933 break;
5934 case 2:
5935 opcode = aco_opcode::buffer_store_format_xy;
5936 break;
5937 case 3:
5938 opcode = aco_opcode::buffer_store_format_xyz;
5939 break;
5940 case 4:
5941 opcode = aco_opcode::buffer_store_format_xyzw;
5942 break;
5943 default:
5944 unreachable(">4 channel buffer image store");
5945 }
5946 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5947 store->operands[0] = Operand(rsrc);
5948 store->operands[1] = Operand(vindex);
5949 store->operands[2] = Operand((uint32_t) 0);
5950 store->operands[3] = Operand(data);
5951 store->idxen = true;
5952 store->glc = glc;
5953 store->dlc = false;
5954 store->disable_wqm = true;
5955 store->barrier = barrier_image;
5956 ctx->program->needs_exact = true;
5957 ctx->block->instructions.emplace_back(std::move(store));
5958 return;
5959 }
5960
5961 assert(data.type() == RegType::vgpr);
5962 Temp coords = get_image_coords(ctx, instr, type);
5963 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5964
5965 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5966 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5967
5968 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5969 store->operands[0] = Operand(resource);
5970 store->operands[1] = Operand(data);
5971 store->operands[2] = Operand(coords);
5972 store->glc = glc;
5973 store->dlc = false;
5974 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5975 store->dmask = (1 << data.size()) - 1;
5976 store->unrm = true;
5977 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5978 store->disable_wqm = true;
5979 store->barrier = barrier_image;
5980 ctx->program->needs_exact = true;
5981 ctx->block->instructions.emplace_back(std::move(store));
5982 return;
5983 }
5984
5985 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5986 {
5987 /* return the previous value if dest is ever used */
5988 bool return_previous = false;
5989 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5990 return_previous = true;
5991 break;
5992 }
5993 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5994 return_previous = true;
5995 break;
5996 }
5997
5998 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5999 const struct glsl_type *type = glsl_without_array(var->type);
6000 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6001 bool is_array = glsl_sampler_type_is_array(type);
6002 Builder bld(ctx->program, ctx->block);
6003
6004 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
6005 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
6006
6007 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
6008 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
6009
6010 aco_opcode buf_op, image_op;
6011 switch (instr->intrinsic) {
6012 case nir_intrinsic_image_deref_atomic_add:
6013 buf_op = aco_opcode::buffer_atomic_add;
6014 image_op = aco_opcode::image_atomic_add;
6015 break;
6016 case nir_intrinsic_image_deref_atomic_umin:
6017 buf_op = aco_opcode::buffer_atomic_umin;
6018 image_op = aco_opcode::image_atomic_umin;
6019 break;
6020 case nir_intrinsic_image_deref_atomic_imin:
6021 buf_op = aco_opcode::buffer_atomic_smin;
6022 image_op = aco_opcode::image_atomic_smin;
6023 break;
6024 case nir_intrinsic_image_deref_atomic_umax:
6025 buf_op = aco_opcode::buffer_atomic_umax;
6026 image_op = aco_opcode::image_atomic_umax;
6027 break;
6028 case nir_intrinsic_image_deref_atomic_imax:
6029 buf_op = aco_opcode::buffer_atomic_smax;
6030 image_op = aco_opcode::image_atomic_smax;
6031 break;
6032 case nir_intrinsic_image_deref_atomic_and:
6033 buf_op = aco_opcode::buffer_atomic_and;
6034 image_op = aco_opcode::image_atomic_and;
6035 break;
6036 case nir_intrinsic_image_deref_atomic_or:
6037 buf_op = aco_opcode::buffer_atomic_or;
6038 image_op = aco_opcode::image_atomic_or;
6039 break;
6040 case nir_intrinsic_image_deref_atomic_xor:
6041 buf_op = aco_opcode::buffer_atomic_xor;
6042 image_op = aco_opcode::image_atomic_xor;
6043 break;
6044 case nir_intrinsic_image_deref_atomic_exchange:
6045 buf_op = aco_opcode::buffer_atomic_swap;
6046 image_op = aco_opcode::image_atomic_swap;
6047 break;
6048 case nir_intrinsic_image_deref_atomic_comp_swap:
6049 buf_op = aco_opcode::buffer_atomic_cmpswap;
6050 image_op = aco_opcode::image_atomic_cmpswap;
6051 break;
6052 default:
6053 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
6054 }
6055
6056 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6057
6058 if (dim == GLSL_SAMPLER_DIM_BUF) {
6059 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
6060 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
6061 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6062 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6063 mubuf->operands[0] = Operand(resource);
6064 mubuf->operands[1] = Operand(vindex);
6065 mubuf->operands[2] = Operand((uint32_t)0);
6066 mubuf->operands[3] = Operand(data);
6067 if (return_previous)
6068 mubuf->definitions[0] = Definition(dst);
6069 mubuf->offset = 0;
6070 mubuf->idxen = true;
6071 mubuf->glc = return_previous;
6072 mubuf->dlc = false; /* Not needed for atomics */
6073 mubuf->disable_wqm = true;
6074 mubuf->barrier = barrier_image;
6075 ctx->program->needs_exact = true;
6076 ctx->block->instructions.emplace_back(std::move(mubuf));
6077 return;
6078 }
6079
6080 Temp coords = get_image_coords(ctx, instr, type);
6081 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6082 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6083 mimg->operands[0] = Operand(resource);
6084 mimg->operands[1] = Operand(data);
6085 mimg->operands[2] = Operand(coords);
6086 if (return_previous)
6087 mimg->definitions[0] = Definition(dst);
6088 mimg->glc = return_previous;
6089 mimg->dlc = false; /* Not needed for atomics */
6090 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6091 mimg->dmask = (1 << data.size()) - 1;
6092 mimg->unrm = true;
6093 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6094 mimg->disable_wqm = true;
6095 mimg->barrier = barrier_image;
6096 ctx->program->needs_exact = true;
6097 ctx->block->instructions.emplace_back(std::move(mimg));
6098 return;
6099 }
6100
6101 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6102 {
6103 if (in_elements && ctx->options->chip_class == GFX8) {
6104 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6105 Builder bld(ctx->program, ctx->block);
6106
6107 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6108
6109 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6110 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6111
6112 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6113 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6114
6115 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6116 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6117
6118 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6119 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6120 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6121 if (dst.type() == RegType::vgpr)
6122 bld.copy(Definition(dst), shr_dst);
6123
6124 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6125 } else {
6126 emit_extract_vector(ctx, desc, 2, dst);
6127 }
6128 }
6129
6130 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6131 {
6132 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6133 const struct glsl_type *type = glsl_without_array(var->type);
6134 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6135 bool is_array = glsl_sampler_type_is_array(type);
6136 Builder bld(ctx->program, ctx->block);
6137
6138 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6139 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6140 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6141 }
6142
6143 /* LOD */
6144 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6145
6146 /* Resource */
6147 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6148
6149 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6150
6151 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6152 mimg->operands[0] = Operand(resource);
6153 mimg->operands[1] = Operand(s4); /* no sampler */
6154 mimg->operands[2] = Operand(lod);
6155 uint8_t& dmask = mimg->dmask;
6156 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6157 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6158 mimg->da = glsl_sampler_type_is_array(type);
6159 mimg->can_reorder = true;
6160 Definition& def = mimg->definitions[0];
6161 ctx->block->instructions.emplace_back(std::move(mimg));
6162
6163 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6164 glsl_sampler_type_is_array(type)) {
6165
6166 assert(instr->dest.ssa.num_components == 3);
6167 Temp tmp = {ctx->program->allocateId(), v3};
6168 def = Definition(tmp);
6169 emit_split_vector(ctx, tmp, 3);
6170
6171 /* divide 3rd value by 6 by multiplying with magic number */
6172 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6173 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6174
6175 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6176 emit_extract_vector(ctx, tmp, 0, v1),
6177 emit_extract_vector(ctx, tmp, 1, v1),
6178 by_6);
6179
6180 } else if (ctx->options->chip_class == GFX9 &&
6181 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6182 glsl_sampler_type_is_array(type)) {
6183 assert(instr->dest.ssa.num_components == 2);
6184 def = Definition(dst);
6185 dmask = 0x5;
6186 } else {
6187 def = Definition(dst);
6188 }
6189
6190 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6191 }
6192
6193 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6194 {
6195 Builder bld(ctx->program, ctx->block);
6196 unsigned num_components = instr->num_components;
6197
6198 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6199 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6200 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6201
6202 unsigned access = nir_intrinsic_access(instr);
6203 bool glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
6204 unsigned size = instr->dest.ssa.bit_size / 8;
6205
6206 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[0].ssa, access);
6207 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6208 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6209 */
6210 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_store : has_vmem_store));
6211 allow_smem |= ((access & ACCESS_RESTRICT) && (access & ACCESS_NON_WRITEABLE)) || (access & ACCESS_CAN_REORDER);
6212
6213 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6214 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false, allow_smem);
6215 }
6216
6217 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6218 {
6219 Builder bld(ctx->program, ctx->block);
6220 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6221 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6222 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6223 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6224
6225 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6226 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6227
6228 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6229 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[1].ssa, nir_intrinsic_access(instr));
6230 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6231 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6232 */
6233 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_loadstore : has_vmem_loadstore));
6234
6235 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6236 ctx->options->chip_class >= GFX8 &&
6237 (elem_size_bytes >= 4 || can_subdword_ssbo_store_use_smem(instr)) &&
6238 allow_smem;
6239 if (smem)
6240 offset = bld.as_uniform(offset);
6241 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6242
6243 unsigned write_count = 0;
6244 Temp write_datas[32];
6245 unsigned offsets[32];
6246 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6247 data, writemask, 16, &write_count, write_datas, offsets);
6248
6249 for (unsigned i = 0; i < write_count; i++) {
6250 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6251 if (smem && ctx->stage == fragment_fs)
6252 op = aco_opcode::p_fs_buffer_store_smem;
6253
6254 if (smem) {
6255 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6256 store->operands[0] = Operand(rsrc);
6257 if (offsets[i]) {
6258 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6259 offset, Operand(offsets[i]));
6260 store->operands[1] = Operand(off);
6261 } else {
6262 store->operands[1] = Operand(offset);
6263 }
6264 if (op != aco_opcode::p_fs_buffer_store_smem)
6265 store->operands[1].setFixed(m0);
6266 store->operands[2] = Operand(write_datas[i]);
6267 store->glc = glc;
6268 store->dlc = false;
6269 store->disable_wqm = true;
6270 store->barrier = barrier_buffer;
6271 ctx->block->instructions.emplace_back(std::move(store));
6272 ctx->program->wb_smem_l1_on_end = true;
6273 if (op == aco_opcode::p_fs_buffer_store_smem) {
6274 ctx->block->kind |= block_kind_needs_lowering;
6275 ctx->program->needs_exact = true;
6276 }
6277 } else {
6278 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6279 store->operands[0] = Operand(rsrc);
6280 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6281 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6282 store->operands[3] = Operand(write_datas[i]);
6283 store->offset = offsets[i];
6284 store->offen = (offset.type() == RegType::vgpr);
6285 store->glc = glc;
6286 store->dlc = false;
6287 store->disable_wqm = true;
6288 store->barrier = barrier_buffer;
6289 ctx->program->needs_exact = true;
6290 ctx->block->instructions.emplace_back(std::move(store));
6291 }
6292 }
6293 }
6294
6295 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6296 {
6297 /* return the previous value if dest is ever used */
6298 bool return_previous = false;
6299 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6300 return_previous = true;
6301 break;
6302 }
6303 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6304 return_previous = true;
6305 break;
6306 }
6307
6308 Builder bld(ctx->program, ctx->block);
6309 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6310
6311 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6312 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6313 get_ssa_temp(ctx, instr->src[3].ssa), data);
6314
6315 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6316 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6317 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6318
6319 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6320
6321 aco_opcode op32, op64;
6322 switch (instr->intrinsic) {
6323 case nir_intrinsic_ssbo_atomic_add:
6324 op32 = aco_opcode::buffer_atomic_add;
6325 op64 = aco_opcode::buffer_atomic_add_x2;
6326 break;
6327 case nir_intrinsic_ssbo_atomic_imin:
6328 op32 = aco_opcode::buffer_atomic_smin;
6329 op64 = aco_opcode::buffer_atomic_smin_x2;
6330 break;
6331 case nir_intrinsic_ssbo_atomic_umin:
6332 op32 = aco_opcode::buffer_atomic_umin;
6333 op64 = aco_opcode::buffer_atomic_umin_x2;
6334 break;
6335 case nir_intrinsic_ssbo_atomic_imax:
6336 op32 = aco_opcode::buffer_atomic_smax;
6337 op64 = aco_opcode::buffer_atomic_smax_x2;
6338 break;
6339 case nir_intrinsic_ssbo_atomic_umax:
6340 op32 = aco_opcode::buffer_atomic_umax;
6341 op64 = aco_opcode::buffer_atomic_umax_x2;
6342 break;
6343 case nir_intrinsic_ssbo_atomic_and:
6344 op32 = aco_opcode::buffer_atomic_and;
6345 op64 = aco_opcode::buffer_atomic_and_x2;
6346 break;
6347 case nir_intrinsic_ssbo_atomic_or:
6348 op32 = aco_opcode::buffer_atomic_or;
6349 op64 = aco_opcode::buffer_atomic_or_x2;
6350 break;
6351 case nir_intrinsic_ssbo_atomic_xor:
6352 op32 = aco_opcode::buffer_atomic_xor;
6353 op64 = aco_opcode::buffer_atomic_xor_x2;
6354 break;
6355 case nir_intrinsic_ssbo_atomic_exchange:
6356 op32 = aco_opcode::buffer_atomic_swap;
6357 op64 = aco_opcode::buffer_atomic_swap_x2;
6358 break;
6359 case nir_intrinsic_ssbo_atomic_comp_swap:
6360 op32 = aco_opcode::buffer_atomic_cmpswap;
6361 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6362 break;
6363 default:
6364 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6365 }
6366 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6367 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6368 mubuf->operands[0] = Operand(rsrc);
6369 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6370 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6371 mubuf->operands[3] = Operand(data);
6372 if (return_previous)
6373 mubuf->definitions[0] = Definition(dst);
6374 mubuf->offset = 0;
6375 mubuf->offen = (offset.type() == RegType::vgpr);
6376 mubuf->glc = return_previous;
6377 mubuf->dlc = false; /* Not needed for atomics */
6378 mubuf->disable_wqm = true;
6379 mubuf->barrier = barrier_buffer;
6380 ctx->program->needs_exact = true;
6381 ctx->block->instructions.emplace_back(std::move(mubuf));
6382 }
6383
6384 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6385
6386 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6387 Builder bld(ctx->program, ctx->block);
6388 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6389 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6390 }
6391
6392 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6393 {
6394 Builder bld(ctx->program, ctx->block);
6395 unsigned num_components = instr->num_components;
6396 unsigned component_size = instr->dest.ssa.bit_size / 8;
6397
6398 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6399 get_ssa_temp(ctx, &instr->dest.ssa),
6400 num_components, component_size};
6401 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6402 info.align_mul = nir_intrinsic_align_mul(instr);
6403 info.align_offset = nir_intrinsic_align_offset(instr);
6404 info.barrier = barrier_buffer;
6405 info.can_reorder = false;
6406 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6407 * it's safe to use SMEM */
6408 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6409 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6410 emit_global_load(ctx, bld, &info);
6411 } else {
6412 info.offset = Operand(bld.as_uniform(info.offset));
6413 emit_smem_load(ctx, bld, &info);
6414 }
6415 }
6416
6417 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6418 {
6419 Builder bld(ctx->program, ctx->block);
6420 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6421 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6422
6423 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6424 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6425 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6426
6427 if (ctx->options->chip_class >= GFX7)
6428 addr = as_vgpr(ctx, addr);
6429
6430 unsigned write_count = 0;
6431 Temp write_datas[32];
6432 unsigned offsets[32];
6433 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6434 16, &write_count, write_datas, offsets);
6435
6436 for (unsigned i = 0; i < write_count; i++) {
6437 if (ctx->options->chip_class >= GFX7) {
6438 unsigned offset = offsets[i];
6439 Temp store_addr = addr;
6440 if (offset > 0 && ctx->options->chip_class < GFX9) {
6441 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6442 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6443 Temp carry = bld.tmp(bld.lm);
6444 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6445
6446 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6447 Operand(offset), addr0);
6448 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6449 Operand(0u), addr1,
6450 carry).def(1).setHint(vcc);
6451
6452 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6453
6454 offset = 0;
6455 }
6456
6457 bool global = ctx->options->chip_class >= GFX9;
6458 aco_opcode op;
6459 switch (write_datas[i].bytes()) {
6460 case 1:
6461 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6462 break;
6463 case 2:
6464 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6465 break;
6466 case 4:
6467 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6468 break;
6469 case 8:
6470 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6471 break;
6472 case 12:
6473 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6474 break;
6475 case 16:
6476 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6477 break;
6478 default:
6479 unreachable("store_global not implemented for this size.");
6480 }
6481
6482 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6483 flat->operands[0] = Operand(store_addr);
6484 flat->operands[1] = Operand(s1);
6485 flat->operands[2] = Operand(write_datas[i]);
6486 flat->glc = glc;
6487 flat->dlc = false;
6488 flat->offset = offset;
6489 flat->disable_wqm = true;
6490 flat->barrier = barrier_buffer;
6491 ctx->program->needs_exact = true;
6492 ctx->block->instructions.emplace_back(std::move(flat));
6493 } else {
6494 assert(ctx->options->chip_class == GFX6);
6495
6496 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6497
6498 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6499
6500 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6501 mubuf->operands[0] = Operand(rsrc);
6502 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6503 mubuf->operands[2] = Operand(0u);
6504 mubuf->operands[3] = Operand(write_datas[i]);
6505 mubuf->glc = glc;
6506 mubuf->dlc = false;
6507 mubuf->offset = offsets[i];
6508 mubuf->addr64 = addr.type() == RegType::vgpr;
6509 mubuf->disable_wqm = true;
6510 mubuf->barrier = barrier_buffer;
6511 ctx->program->needs_exact = true;
6512 ctx->block->instructions.emplace_back(std::move(mubuf));
6513 }
6514 }
6515 }
6516
6517 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6518 {
6519 /* return the previous value if dest is ever used */
6520 bool return_previous = false;
6521 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6522 return_previous = true;
6523 break;
6524 }
6525 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6526 return_previous = true;
6527 break;
6528 }
6529
6530 Builder bld(ctx->program, ctx->block);
6531 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6532 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6533
6534 if (ctx->options->chip_class >= GFX7)
6535 addr = as_vgpr(ctx, addr);
6536
6537 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6538 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6539 get_ssa_temp(ctx, instr->src[2].ssa), data);
6540
6541 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6542
6543 aco_opcode op32, op64;
6544
6545 if (ctx->options->chip_class >= GFX7) {
6546 bool global = ctx->options->chip_class >= GFX9;
6547 switch (instr->intrinsic) {
6548 case nir_intrinsic_global_atomic_add:
6549 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6550 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6551 break;
6552 case nir_intrinsic_global_atomic_imin:
6553 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6554 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6555 break;
6556 case nir_intrinsic_global_atomic_umin:
6557 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6558 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6559 break;
6560 case nir_intrinsic_global_atomic_imax:
6561 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6562 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6563 break;
6564 case nir_intrinsic_global_atomic_umax:
6565 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6566 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6567 break;
6568 case nir_intrinsic_global_atomic_and:
6569 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6570 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6571 break;
6572 case nir_intrinsic_global_atomic_or:
6573 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6574 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6575 break;
6576 case nir_intrinsic_global_atomic_xor:
6577 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6578 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6579 break;
6580 case nir_intrinsic_global_atomic_exchange:
6581 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6582 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6583 break;
6584 case nir_intrinsic_global_atomic_comp_swap:
6585 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6586 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6587 break;
6588 default:
6589 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6590 }
6591
6592 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6593 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6594 flat->operands[0] = Operand(addr);
6595 flat->operands[1] = Operand(s1);
6596 flat->operands[2] = Operand(data);
6597 if (return_previous)
6598 flat->definitions[0] = Definition(dst);
6599 flat->glc = return_previous;
6600 flat->dlc = false; /* Not needed for atomics */
6601 flat->offset = 0;
6602 flat->disable_wqm = true;
6603 flat->barrier = barrier_buffer;
6604 ctx->program->needs_exact = true;
6605 ctx->block->instructions.emplace_back(std::move(flat));
6606 } else {
6607 assert(ctx->options->chip_class == GFX6);
6608
6609 switch (instr->intrinsic) {
6610 case nir_intrinsic_global_atomic_add:
6611 op32 = aco_opcode::buffer_atomic_add;
6612 op64 = aco_opcode::buffer_atomic_add_x2;
6613 break;
6614 case nir_intrinsic_global_atomic_imin:
6615 op32 = aco_opcode::buffer_atomic_smin;
6616 op64 = aco_opcode::buffer_atomic_smin_x2;
6617 break;
6618 case nir_intrinsic_global_atomic_umin:
6619 op32 = aco_opcode::buffer_atomic_umin;
6620 op64 = aco_opcode::buffer_atomic_umin_x2;
6621 break;
6622 case nir_intrinsic_global_atomic_imax:
6623 op32 = aco_opcode::buffer_atomic_smax;
6624 op64 = aco_opcode::buffer_atomic_smax_x2;
6625 break;
6626 case nir_intrinsic_global_atomic_umax:
6627 op32 = aco_opcode::buffer_atomic_umax;
6628 op64 = aco_opcode::buffer_atomic_umax_x2;
6629 break;
6630 case nir_intrinsic_global_atomic_and:
6631 op32 = aco_opcode::buffer_atomic_and;
6632 op64 = aco_opcode::buffer_atomic_and_x2;
6633 break;
6634 case nir_intrinsic_global_atomic_or:
6635 op32 = aco_opcode::buffer_atomic_or;
6636 op64 = aco_opcode::buffer_atomic_or_x2;
6637 break;
6638 case nir_intrinsic_global_atomic_xor:
6639 op32 = aco_opcode::buffer_atomic_xor;
6640 op64 = aco_opcode::buffer_atomic_xor_x2;
6641 break;
6642 case nir_intrinsic_global_atomic_exchange:
6643 op32 = aco_opcode::buffer_atomic_swap;
6644 op64 = aco_opcode::buffer_atomic_swap_x2;
6645 break;
6646 case nir_intrinsic_global_atomic_comp_swap:
6647 op32 = aco_opcode::buffer_atomic_cmpswap;
6648 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6649 break;
6650 default:
6651 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6652 }
6653
6654 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6655
6656 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6657
6658 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6659 mubuf->operands[0] = Operand(rsrc);
6660 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6661 mubuf->operands[2] = Operand(0u);
6662 mubuf->operands[3] = Operand(data);
6663 if (return_previous)
6664 mubuf->definitions[0] = Definition(dst);
6665 mubuf->glc = return_previous;
6666 mubuf->dlc = false;
6667 mubuf->offset = 0;
6668 mubuf->addr64 = addr.type() == RegType::vgpr;
6669 mubuf->disable_wqm = true;
6670 mubuf->barrier = barrier_buffer;
6671 ctx->program->needs_exact = true;
6672 ctx->block->instructions.emplace_back(std::move(mubuf));
6673 }
6674 }
6675
6676 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6677 Builder bld(ctx->program, ctx->block);
6678 switch(instr->intrinsic) {
6679 case nir_intrinsic_group_memory_barrier:
6680 case nir_intrinsic_memory_barrier:
6681 bld.barrier(aco_opcode::p_memory_barrier_common);
6682 break;
6683 case nir_intrinsic_memory_barrier_buffer:
6684 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6685 break;
6686 case nir_intrinsic_memory_barrier_image:
6687 bld.barrier(aco_opcode::p_memory_barrier_image);
6688 break;
6689 case nir_intrinsic_memory_barrier_tcs_patch:
6690 case nir_intrinsic_memory_barrier_shared:
6691 bld.barrier(aco_opcode::p_memory_barrier_shared);
6692 break;
6693 default:
6694 unreachable("Unimplemented memory barrier intrinsic");
6695 break;
6696 }
6697 }
6698
6699 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6700 {
6701 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6702 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6703 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6704 Builder bld(ctx->program, ctx->block);
6705
6706 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6707 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6708 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6709 }
6710
6711 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6712 {
6713 unsigned writemask = nir_intrinsic_write_mask(instr);
6714 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6715 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6716 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6717
6718 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6719 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6720 }
6721
6722 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6723 {
6724 unsigned offset = nir_intrinsic_base(instr);
6725 Builder bld(ctx->program, ctx->block);
6726 Operand m = load_lds_size_m0(bld);
6727 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6728 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6729
6730 unsigned num_operands = 3;
6731 aco_opcode op32, op64, op32_rtn, op64_rtn;
6732 switch(instr->intrinsic) {
6733 case nir_intrinsic_shared_atomic_add:
6734 op32 = aco_opcode::ds_add_u32;
6735 op64 = aco_opcode::ds_add_u64;
6736 op32_rtn = aco_opcode::ds_add_rtn_u32;
6737 op64_rtn = aco_opcode::ds_add_rtn_u64;
6738 break;
6739 case nir_intrinsic_shared_atomic_imin:
6740 op32 = aco_opcode::ds_min_i32;
6741 op64 = aco_opcode::ds_min_i64;
6742 op32_rtn = aco_opcode::ds_min_rtn_i32;
6743 op64_rtn = aco_opcode::ds_min_rtn_i64;
6744 break;
6745 case nir_intrinsic_shared_atomic_umin:
6746 op32 = aco_opcode::ds_min_u32;
6747 op64 = aco_opcode::ds_min_u64;
6748 op32_rtn = aco_opcode::ds_min_rtn_u32;
6749 op64_rtn = aco_opcode::ds_min_rtn_u64;
6750 break;
6751 case nir_intrinsic_shared_atomic_imax:
6752 op32 = aco_opcode::ds_max_i32;
6753 op64 = aco_opcode::ds_max_i64;
6754 op32_rtn = aco_opcode::ds_max_rtn_i32;
6755 op64_rtn = aco_opcode::ds_max_rtn_i64;
6756 break;
6757 case nir_intrinsic_shared_atomic_umax:
6758 op32 = aco_opcode::ds_max_u32;
6759 op64 = aco_opcode::ds_max_u64;
6760 op32_rtn = aco_opcode::ds_max_rtn_u32;
6761 op64_rtn = aco_opcode::ds_max_rtn_u64;
6762 break;
6763 case nir_intrinsic_shared_atomic_and:
6764 op32 = aco_opcode::ds_and_b32;
6765 op64 = aco_opcode::ds_and_b64;
6766 op32_rtn = aco_opcode::ds_and_rtn_b32;
6767 op64_rtn = aco_opcode::ds_and_rtn_b64;
6768 break;
6769 case nir_intrinsic_shared_atomic_or:
6770 op32 = aco_opcode::ds_or_b32;
6771 op64 = aco_opcode::ds_or_b64;
6772 op32_rtn = aco_opcode::ds_or_rtn_b32;
6773 op64_rtn = aco_opcode::ds_or_rtn_b64;
6774 break;
6775 case nir_intrinsic_shared_atomic_xor:
6776 op32 = aco_opcode::ds_xor_b32;
6777 op64 = aco_opcode::ds_xor_b64;
6778 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6779 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6780 break;
6781 case nir_intrinsic_shared_atomic_exchange:
6782 op32 = aco_opcode::ds_write_b32;
6783 op64 = aco_opcode::ds_write_b64;
6784 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6785 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6786 break;
6787 case nir_intrinsic_shared_atomic_comp_swap:
6788 op32 = aco_opcode::ds_cmpst_b32;
6789 op64 = aco_opcode::ds_cmpst_b64;
6790 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6791 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6792 num_operands = 4;
6793 break;
6794 default:
6795 unreachable("Unhandled shared atomic intrinsic");
6796 }
6797
6798 /* return the previous value if dest is ever used */
6799 bool return_previous = false;
6800 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6801 return_previous = true;
6802 break;
6803 }
6804 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6805 return_previous = true;
6806 break;
6807 }
6808
6809 aco_opcode op;
6810 if (data.size() == 1) {
6811 assert(instr->dest.ssa.bit_size == 32);
6812 op = return_previous ? op32_rtn : op32;
6813 } else {
6814 assert(instr->dest.ssa.bit_size == 64);
6815 op = return_previous ? op64_rtn : op64;
6816 }
6817
6818 if (offset > 65535) {
6819 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6820 offset = 0;
6821 }
6822
6823 aco_ptr<DS_instruction> ds;
6824 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6825 ds->operands[0] = Operand(address);
6826 ds->operands[1] = Operand(data);
6827 if (num_operands == 4)
6828 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6829 ds->operands[num_operands - 1] = m;
6830 ds->offset0 = offset;
6831 if (return_previous)
6832 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6833 ctx->block->instructions.emplace_back(std::move(ds));
6834 }
6835
6836 Temp get_scratch_resource(isel_context *ctx)
6837 {
6838 Builder bld(ctx->program, ctx->block);
6839 Temp scratch_addr = ctx->program->private_segment_buffer;
6840 if (ctx->stage != compute_cs)
6841 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6842
6843 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6844 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6845
6846 if (ctx->program->chip_class >= GFX10) {
6847 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6848 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6849 S_008F0C_RESOURCE_LEVEL(1);
6850 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6851 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6852 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6853 }
6854
6855 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6856 if (ctx->program->chip_class <= GFX8)
6857 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6858
6859 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6860 }
6861
6862 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6863 Builder bld(ctx->program, ctx->block);
6864 Temp rsrc = get_scratch_resource(ctx);
6865 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6866 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6867
6868 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6869 instr->dest.ssa.bit_size / 8u, rsrc};
6870 info.align_mul = nir_intrinsic_align_mul(instr);
6871 info.align_offset = nir_intrinsic_align_offset(instr);
6872 info.swizzle_component_size = 16;
6873 info.can_reorder = false;
6874 info.soffset = ctx->program->scratch_offset;
6875 emit_mubuf_load(ctx, bld, &info);
6876 }
6877
6878 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6879 Builder bld(ctx->program, ctx->block);
6880 Temp rsrc = get_scratch_resource(ctx);
6881 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6882 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6883
6884 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6885 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6886
6887 unsigned write_count = 0;
6888 Temp write_datas[32];
6889 unsigned offsets[32];
6890 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6891 16, &write_count, write_datas, offsets);
6892
6893 for (unsigned i = 0; i < write_count; i++) {
6894 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6895 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true, true);
6896 }
6897 }
6898
6899 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6900 uint8_t log2_ps_iter_samples;
6901 if (ctx->program->info->ps.force_persample) {
6902 log2_ps_iter_samples =
6903 util_logbase2(ctx->options->key.fs.num_samples);
6904 } else {
6905 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6906 }
6907
6908 /* The bit pattern matches that used by fixed function fragment
6909 * processing. */
6910 static const unsigned ps_iter_masks[] = {
6911 0xffff, /* not used */
6912 0x5555,
6913 0x1111,
6914 0x0101,
6915 0x0001,
6916 };
6917 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6918
6919 Builder bld(ctx->program, ctx->block);
6920
6921 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6922 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6923 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6924 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6925 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6926 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6927 }
6928
6929 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6930 Builder bld(ctx->program, ctx->block);
6931
6932 unsigned stream = nir_intrinsic_stream_id(instr);
6933 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6934 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6935 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6936
6937 /* get GSVS ring */
6938 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6939
6940 unsigned num_components =
6941 ctx->program->info->gs.num_stream_output_components[stream];
6942 assert(num_components);
6943
6944 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6945 unsigned stream_offset = 0;
6946 for (unsigned i = 0; i < stream; i++) {
6947 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6948 stream_offset += prev_stride * ctx->program->wave_size;
6949 }
6950
6951 /* Limit on the stride field for <= GFX7. */
6952 assert(stride < (1 << 14));
6953
6954 Temp gsvs_dwords[4];
6955 for (unsigned i = 0; i < 4; i++)
6956 gsvs_dwords[i] = bld.tmp(s1);
6957 bld.pseudo(aco_opcode::p_split_vector,
6958 Definition(gsvs_dwords[0]),
6959 Definition(gsvs_dwords[1]),
6960 Definition(gsvs_dwords[2]),
6961 Definition(gsvs_dwords[3]),
6962 gsvs_ring);
6963
6964 if (stream_offset) {
6965 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6966
6967 Temp carry = bld.tmp(s1);
6968 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6969 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));
6970 }
6971
6972 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)));
6973 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6974
6975 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6976 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6977
6978 unsigned offset = 0;
6979 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6980 if (ctx->program->info->gs.output_streams[i] != stream)
6981 continue;
6982
6983 for (unsigned j = 0; j < 4; j++) {
6984 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6985 continue;
6986
6987 if (ctx->outputs.mask[i] & (1 << j)) {
6988 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6989 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6990 if (const_offset >= 4096u) {
6991 if (vaddr_offset.isUndefined())
6992 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6993 else
6994 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6995 const_offset %= 4096u;
6996 }
6997
6998 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6999 mtbuf->operands[0] = Operand(gsvs_ring);
7000 mtbuf->operands[1] = vaddr_offset;
7001 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
7002 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
7003 mtbuf->offen = !vaddr_offset.isUndefined();
7004 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
7005 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
7006 mtbuf->offset = const_offset;
7007 mtbuf->glc = true;
7008 mtbuf->slc = true;
7009 mtbuf->barrier = barrier_gs_data;
7010 mtbuf->can_reorder = true;
7011 bld.insert(std::move(mtbuf));
7012 }
7013
7014 offset += ctx->shader->info.gs.vertices_out;
7015 }
7016
7017 /* outputs for the next vertex are undefined and keeping them around can
7018 * create invalid IR with control flow */
7019 ctx->outputs.mask[i] = 0;
7020 }
7021
7022 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
7023 }
7024
7025 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
7026 {
7027 Builder bld(ctx->program, ctx->block);
7028
7029 if (cluster_size == 1) {
7030 return src;
7031 } if (op == nir_op_iand && cluster_size == 4) {
7032 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7033 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7034 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
7035 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
7036 } else if (op == nir_op_ior && cluster_size == 4) {
7037 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7038 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
7039 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
7040 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
7041 //subgroupAnd(val) -> (exec & ~val) == 0
7042 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7043 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7044 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
7045 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
7046 //subgroupOr(val) -> (val & exec) != 0
7047 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
7048 return bool_to_vector_condition(ctx, tmp);
7049 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
7050 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7051 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7052 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
7053 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
7054 return bool_to_vector_condition(ctx, tmp);
7055 } else {
7056 //subgroupClustered{And,Or,Xor}(val, n) ->
7057 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7058 //cluster_offset = ~(n - 1) & lane_id
7059 //cluster_mask = ((1 << n) - 1)
7060 //subgroupClusteredAnd():
7061 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7062 //subgroupClusteredOr():
7063 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7064 //subgroupClusteredXor():
7065 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7066 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7067 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7068
7069 Temp tmp;
7070 if (op == nir_op_iand)
7071 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7072 else
7073 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7074
7075 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7076
7077 if (ctx->program->chip_class <= GFX7)
7078 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7079 else if (ctx->program->wave_size == 64)
7080 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7081 else
7082 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7083 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7084 if (cluster_mask != 0xffffffff)
7085 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7086
7087 Definition cmp_def = Definition();
7088 if (op == nir_op_iand) {
7089 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7090 } else if (op == nir_op_ior) {
7091 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7092 } else if (op == nir_op_ixor) {
7093 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7094 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7095 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7096 }
7097 cmp_def.setHint(vcc);
7098 return cmp_def.getTemp();
7099 }
7100 }
7101
7102 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7103 {
7104 Builder bld(ctx->program, ctx->block);
7105
7106 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7107 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7108 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7109 Temp tmp;
7110 if (op == nir_op_iand)
7111 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7112 else
7113 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7114
7115 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7116 Temp lo = lohi.def(0).getTemp();
7117 Temp hi = lohi.def(1).getTemp();
7118 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7119
7120 Definition cmp_def = Definition();
7121 if (op == nir_op_iand)
7122 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7123 else if (op == nir_op_ior)
7124 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7125 else if (op == nir_op_ixor)
7126 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7127 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7128 cmp_def.setHint(vcc);
7129 return cmp_def.getTemp();
7130 }
7131
7132 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7133 {
7134 Builder bld(ctx->program, ctx->block);
7135
7136 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7137 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7138 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7139 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7140 if (op == nir_op_iand)
7141 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7142 else if (op == nir_op_ior)
7143 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7144 else if (op == nir_op_ixor)
7145 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7146
7147 assert(false);
7148 return Temp();
7149 }
7150
7151 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7152 {
7153 Builder bld(ctx->program, ctx->block);
7154 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7155 if (src.regClass().type() == RegType::vgpr) {
7156 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7157 } else if (src.regClass() == s1) {
7158 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7159 } else if (src.regClass() == s2) {
7160 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7161 } else {
7162 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7163 nir_print_instr(&instr->instr, stderr);
7164 fprintf(stderr, "\n");
7165 }
7166 }
7167
7168 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7169 {
7170 Builder bld(ctx->program, ctx->block);
7171 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7172 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7173 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7174
7175 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7176 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7177 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7178 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7179
7180 /* Build DD X/Y */
7181 if (ctx->program->chip_class >= GFX8) {
7182 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7183 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7184 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7185 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7186 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7187 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7188 } else {
7189 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7190 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7191 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7192 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7193 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7194 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7195 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7196 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7197 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7198 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7199 }
7200
7201 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7202 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7203 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7204 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7205 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7206 Temp wqm1 = bld.tmp(v1);
7207 emit_wqm(ctx, tmp1, wqm1, true);
7208 Temp wqm2 = bld.tmp(v1);
7209 emit_wqm(ctx, tmp2, wqm2, true);
7210 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7211 return;
7212 }
7213
7214 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7215 {
7216 Builder bld(ctx->program, ctx->block);
7217 switch(instr->intrinsic) {
7218 case nir_intrinsic_load_barycentric_sample:
7219 case nir_intrinsic_load_barycentric_pixel:
7220 case nir_intrinsic_load_barycentric_centroid: {
7221 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7222 Temp bary = Temp(0, s2);
7223 switch (mode) {
7224 case INTERP_MODE_SMOOTH:
7225 case INTERP_MODE_NONE:
7226 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7227 bary = get_arg(ctx, ctx->args->ac.persp_center);
7228 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7229 bary = ctx->persp_centroid;
7230 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7231 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7232 break;
7233 case INTERP_MODE_NOPERSPECTIVE:
7234 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7235 bary = get_arg(ctx, ctx->args->ac.linear_center);
7236 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7237 bary = ctx->linear_centroid;
7238 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7239 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7240 break;
7241 default:
7242 break;
7243 }
7244 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7245 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7246 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7247 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7248 Operand(p1), Operand(p2));
7249 emit_split_vector(ctx, dst, 2);
7250 break;
7251 }
7252 case nir_intrinsic_load_barycentric_model: {
7253 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7254
7255 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7256 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7257 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7258 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7259 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7260 Operand(p1), Operand(p2), Operand(p3));
7261 emit_split_vector(ctx, dst, 3);
7262 break;
7263 }
7264 case nir_intrinsic_load_barycentric_at_sample: {
7265 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7266 switch (ctx->options->key.fs.num_samples) {
7267 case 2: sample_pos_offset += 1 << 3; break;
7268 case 4: sample_pos_offset += 3 << 3; break;
7269 case 8: sample_pos_offset += 7 << 3; break;
7270 default: break;
7271 }
7272 Temp sample_pos;
7273 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7274 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7275 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7276 if (addr.type() == RegType::sgpr) {
7277 Operand offset;
7278 if (const_addr) {
7279 sample_pos_offset += const_addr->u32 << 3;
7280 offset = Operand(sample_pos_offset);
7281 } else if (ctx->options->chip_class >= GFX9) {
7282 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7283 } else {
7284 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7285 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7286 }
7287
7288 Operand off = bld.copy(bld.def(s1), Operand(offset));
7289 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7290
7291 } else if (ctx->options->chip_class >= GFX9) {
7292 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7293 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7294 } else if (ctx->options->chip_class >= GFX7) {
7295 /* addr += private_segment_buffer + sample_pos_offset */
7296 Temp tmp0 = bld.tmp(s1);
7297 Temp tmp1 = bld.tmp(s1);
7298 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7299 Definition scc_tmp = bld.def(s1, scc);
7300 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7301 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7302 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7303 Temp pck0 = bld.tmp(v1);
7304 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7305 tmp1 = as_vgpr(ctx, tmp1);
7306 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);
7307 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7308
7309 /* sample_pos = flat_load_dwordx2 addr */
7310 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7311 } else {
7312 assert(ctx->options->chip_class == GFX6);
7313
7314 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7315 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7316 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7317
7318 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7319 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7320
7321 sample_pos = bld.tmp(v2);
7322
7323 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7324 load->definitions[0] = Definition(sample_pos);
7325 load->operands[0] = Operand(rsrc);
7326 load->operands[1] = Operand(addr);
7327 load->operands[2] = Operand(0u);
7328 load->offset = sample_pos_offset;
7329 load->offen = 0;
7330 load->addr64 = true;
7331 load->glc = false;
7332 load->dlc = false;
7333 load->disable_wqm = false;
7334 load->barrier = barrier_none;
7335 load->can_reorder = true;
7336 ctx->block->instructions.emplace_back(std::move(load));
7337 }
7338
7339 /* sample_pos -= 0.5 */
7340 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7341 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7342 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7343 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7344 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7345
7346 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7347 break;
7348 }
7349 case nir_intrinsic_load_barycentric_at_offset: {
7350 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7351 RegClass rc = RegClass(offset.type(), 1);
7352 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7353 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7354 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7355 break;
7356 }
7357 case nir_intrinsic_load_front_face: {
7358 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7359 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7360 break;
7361 }
7362 case nir_intrinsic_load_view_index: {
7363 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7364 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7365 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7366 break;
7367 }
7368
7369 /* fallthrough */
7370 }
7371 case nir_intrinsic_load_layer_id: {
7372 unsigned idx = nir_intrinsic_base(instr);
7373 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7374 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7375 break;
7376 }
7377 case nir_intrinsic_load_frag_coord: {
7378 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7379 break;
7380 }
7381 case nir_intrinsic_load_sample_pos: {
7382 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7383 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7384 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7385 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7386 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7387 break;
7388 }
7389 case nir_intrinsic_load_tess_coord:
7390 visit_load_tess_coord(ctx, instr);
7391 break;
7392 case nir_intrinsic_load_interpolated_input:
7393 visit_load_interpolated_input(ctx, instr);
7394 break;
7395 case nir_intrinsic_store_output:
7396 visit_store_output(ctx, instr);
7397 break;
7398 case nir_intrinsic_load_input:
7399 case nir_intrinsic_load_input_vertex:
7400 visit_load_input(ctx, instr);
7401 break;
7402 case nir_intrinsic_load_output:
7403 visit_load_output(ctx, instr);
7404 break;
7405 case nir_intrinsic_load_per_vertex_input:
7406 visit_load_per_vertex_input(ctx, instr);
7407 break;
7408 case nir_intrinsic_load_per_vertex_output:
7409 visit_load_per_vertex_output(ctx, instr);
7410 break;
7411 case nir_intrinsic_store_per_vertex_output:
7412 visit_store_per_vertex_output(ctx, instr);
7413 break;
7414 case nir_intrinsic_load_ubo:
7415 visit_load_ubo(ctx, instr);
7416 break;
7417 case nir_intrinsic_load_push_constant:
7418 visit_load_push_constant(ctx, instr);
7419 break;
7420 case nir_intrinsic_load_constant:
7421 visit_load_constant(ctx, instr);
7422 break;
7423 case nir_intrinsic_vulkan_resource_index:
7424 visit_load_resource(ctx, instr);
7425 break;
7426 case nir_intrinsic_discard:
7427 visit_discard(ctx, instr);
7428 break;
7429 case nir_intrinsic_discard_if:
7430 visit_discard_if(ctx, instr);
7431 break;
7432 case nir_intrinsic_load_shared:
7433 visit_load_shared(ctx, instr);
7434 break;
7435 case nir_intrinsic_store_shared:
7436 visit_store_shared(ctx, instr);
7437 break;
7438 case nir_intrinsic_shared_atomic_add:
7439 case nir_intrinsic_shared_atomic_imin:
7440 case nir_intrinsic_shared_atomic_umin:
7441 case nir_intrinsic_shared_atomic_imax:
7442 case nir_intrinsic_shared_atomic_umax:
7443 case nir_intrinsic_shared_atomic_and:
7444 case nir_intrinsic_shared_atomic_or:
7445 case nir_intrinsic_shared_atomic_xor:
7446 case nir_intrinsic_shared_atomic_exchange:
7447 case nir_intrinsic_shared_atomic_comp_swap:
7448 visit_shared_atomic(ctx, instr);
7449 break;
7450 case nir_intrinsic_image_deref_load:
7451 visit_image_load(ctx, instr);
7452 break;
7453 case nir_intrinsic_image_deref_store:
7454 visit_image_store(ctx, instr);
7455 break;
7456 case nir_intrinsic_image_deref_atomic_add:
7457 case nir_intrinsic_image_deref_atomic_umin:
7458 case nir_intrinsic_image_deref_atomic_imin:
7459 case nir_intrinsic_image_deref_atomic_umax:
7460 case nir_intrinsic_image_deref_atomic_imax:
7461 case nir_intrinsic_image_deref_atomic_and:
7462 case nir_intrinsic_image_deref_atomic_or:
7463 case nir_intrinsic_image_deref_atomic_xor:
7464 case nir_intrinsic_image_deref_atomic_exchange:
7465 case nir_intrinsic_image_deref_atomic_comp_swap:
7466 visit_image_atomic(ctx, instr);
7467 break;
7468 case nir_intrinsic_image_deref_size:
7469 visit_image_size(ctx, instr);
7470 break;
7471 case nir_intrinsic_load_ssbo:
7472 visit_load_ssbo(ctx, instr);
7473 break;
7474 case nir_intrinsic_store_ssbo:
7475 visit_store_ssbo(ctx, instr);
7476 break;
7477 case nir_intrinsic_load_global:
7478 visit_load_global(ctx, instr);
7479 break;
7480 case nir_intrinsic_store_global:
7481 visit_store_global(ctx, instr);
7482 break;
7483 case nir_intrinsic_global_atomic_add:
7484 case nir_intrinsic_global_atomic_imin:
7485 case nir_intrinsic_global_atomic_umin:
7486 case nir_intrinsic_global_atomic_imax:
7487 case nir_intrinsic_global_atomic_umax:
7488 case nir_intrinsic_global_atomic_and:
7489 case nir_intrinsic_global_atomic_or:
7490 case nir_intrinsic_global_atomic_xor:
7491 case nir_intrinsic_global_atomic_exchange:
7492 case nir_intrinsic_global_atomic_comp_swap:
7493 visit_global_atomic(ctx, instr);
7494 break;
7495 case nir_intrinsic_ssbo_atomic_add:
7496 case nir_intrinsic_ssbo_atomic_imin:
7497 case nir_intrinsic_ssbo_atomic_umin:
7498 case nir_intrinsic_ssbo_atomic_imax:
7499 case nir_intrinsic_ssbo_atomic_umax:
7500 case nir_intrinsic_ssbo_atomic_and:
7501 case nir_intrinsic_ssbo_atomic_or:
7502 case nir_intrinsic_ssbo_atomic_xor:
7503 case nir_intrinsic_ssbo_atomic_exchange:
7504 case nir_intrinsic_ssbo_atomic_comp_swap:
7505 visit_atomic_ssbo(ctx, instr);
7506 break;
7507 case nir_intrinsic_load_scratch:
7508 visit_load_scratch(ctx, instr);
7509 break;
7510 case nir_intrinsic_store_scratch:
7511 visit_store_scratch(ctx, instr);
7512 break;
7513 case nir_intrinsic_get_buffer_size:
7514 visit_get_buffer_size(ctx, instr);
7515 break;
7516 case nir_intrinsic_control_barrier: {
7517 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7518 /* GFX6 only (thanks to a hw bug workaround):
7519 * The real barrier instruction isn’t needed, because an entire patch
7520 * always fits into a single wave.
7521 */
7522 break;
7523 }
7524
7525 if (ctx->program->workgroup_size > ctx->program->wave_size)
7526 bld.sopp(aco_opcode::s_barrier);
7527
7528 break;
7529 }
7530 case nir_intrinsic_memory_barrier_tcs_patch:
7531 case nir_intrinsic_group_memory_barrier:
7532 case nir_intrinsic_memory_barrier:
7533 case nir_intrinsic_memory_barrier_buffer:
7534 case nir_intrinsic_memory_barrier_image:
7535 case nir_intrinsic_memory_barrier_shared:
7536 emit_memory_barrier(ctx, instr);
7537 break;
7538 case nir_intrinsic_load_num_work_groups: {
7539 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7540 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7541 emit_split_vector(ctx, dst, 3);
7542 break;
7543 }
7544 case nir_intrinsic_load_local_invocation_id: {
7545 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7546 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7547 emit_split_vector(ctx, dst, 3);
7548 break;
7549 }
7550 case nir_intrinsic_load_work_group_id: {
7551 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7552 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7553 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7554 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7555 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7556 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7557 emit_split_vector(ctx, dst, 3);
7558 break;
7559 }
7560 case nir_intrinsic_load_local_invocation_index: {
7561 Temp id = emit_mbcnt(ctx, bld.def(v1));
7562
7563 /* The tg_size bits [6:11] contain the subgroup id,
7564 * we need this multiplied by the wave size, and then OR the thread id to it.
7565 */
7566 if (ctx->program->wave_size == 64) {
7567 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7568 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7569 get_arg(ctx, ctx->args->ac.tg_size));
7570 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7571 } else {
7572 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7573 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7574 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7575 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7576 }
7577 break;
7578 }
7579 case nir_intrinsic_load_subgroup_id: {
7580 if (ctx->stage == compute_cs) {
7581 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7582 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7583 } else {
7584 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7585 }
7586 break;
7587 }
7588 case nir_intrinsic_load_subgroup_invocation: {
7589 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7590 break;
7591 }
7592 case nir_intrinsic_load_num_subgroups: {
7593 if (ctx->stage == compute_cs)
7594 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7595 get_arg(ctx, ctx->args->ac.tg_size));
7596 else
7597 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7598 break;
7599 }
7600 case nir_intrinsic_ballot: {
7601 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7602 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7603 Definition tmp = bld.def(dst.regClass());
7604 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7605 if (instr->src[0].ssa->bit_size == 1) {
7606 assert(src.regClass() == bld.lm);
7607 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7608 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7609 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7610 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7611 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7612 } else {
7613 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7614 nir_print_instr(&instr->instr, stderr);
7615 fprintf(stderr, "\n");
7616 }
7617 if (dst.size() != bld.lm.size()) {
7618 /* Wave32 with ballot size set to 64 */
7619 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7620 }
7621 emit_wqm(ctx, tmp.getTemp(), dst);
7622 break;
7623 }
7624 case nir_intrinsic_shuffle:
7625 case nir_intrinsic_read_invocation: {
7626 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7627 if (!nir_src_is_divergent(instr->src[0])) {
7628 emit_uniform_subgroup(ctx, instr, src);
7629 } else {
7630 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7631 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7632 tid = bld.as_uniform(tid);
7633 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7634 if (src.regClass() == v1b || src.regClass() == v2b) {
7635 Temp tmp = bld.tmp(v1);
7636 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7637 if (dst.type() == RegType::vgpr)
7638 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7639 else
7640 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7641 } else if (src.regClass() == v1) {
7642 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7643 } else if (src.regClass() == v2) {
7644 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7645 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7646 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7647 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7648 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7649 emit_split_vector(ctx, dst, 2);
7650 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7651 assert(src.regClass() == bld.lm);
7652 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7653 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7654 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7655 assert(src.regClass() == bld.lm);
7656 Temp tmp;
7657 if (ctx->program->chip_class <= GFX7)
7658 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7659 else if (ctx->program->wave_size == 64)
7660 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7661 else
7662 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7663 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7664 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7665 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7666 } else {
7667 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7668 nir_print_instr(&instr->instr, stderr);
7669 fprintf(stderr, "\n");
7670 }
7671 }
7672 break;
7673 }
7674 case nir_intrinsic_load_sample_id: {
7675 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7676 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7677 break;
7678 }
7679 case nir_intrinsic_load_sample_mask_in: {
7680 visit_load_sample_mask_in(ctx, instr);
7681 break;
7682 }
7683 case nir_intrinsic_read_first_invocation: {
7684 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7685 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7686 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7687 emit_wqm(ctx,
7688 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7689 dst);
7690 } else if (src.regClass() == v2) {
7691 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7692 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7693 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7694 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7695 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7696 emit_split_vector(ctx, dst, 2);
7697 } else if (instr->dest.ssa.bit_size == 1) {
7698 assert(src.regClass() == bld.lm);
7699 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7700 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7701 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7702 } else if (src.regClass() == s1) {
7703 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7704 } else if (src.regClass() == s2) {
7705 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7706 } else {
7707 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7708 nir_print_instr(&instr->instr, stderr);
7709 fprintf(stderr, "\n");
7710 }
7711 break;
7712 }
7713 case nir_intrinsic_vote_all: {
7714 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7715 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7716 assert(src.regClass() == bld.lm);
7717 assert(dst.regClass() == bld.lm);
7718
7719 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7720 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7721 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7722 break;
7723 }
7724 case nir_intrinsic_vote_any: {
7725 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7726 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7727 assert(src.regClass() == bld.lm);
7728 assert(dst.regClass() == bld.lm);
7729
7730 Temp tmp = bool_to_scalar_condition(ctx, src);
7731 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7732 break;
7733 }
7734 case nir_intrinsic_reduce:
7735 case nir_intrinsic_inclusive_scan:
7736 case nir_intrinsic_exclusive_scan: {
7737 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7738 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7739 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7740 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7741 nir_intrinsic_cluster_size(instr) : 0;
7742 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7743
7744 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7745 emit_uniform_subgroup(ctx, instr, src);
7746 } else if (instr->dest.ssa.bit_size == 1) {
7747 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7748 op = nir_op_iand;
7749 else if (op == nir_op_iadd)
7750 op = nir_op_ixor;
7751 else if (op == nir_op_umax || op == nir_op_imax)
7752 op = nir_op_ior;
7753 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7754
7755 switch (instr->intrinsic) {
7756 case nir_intrinsic_reduce:
7757 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7758 break;
7759 case nir_intrinsic_exclusive_scan:
7760 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7761 break;
7762 case nir_intrinsic_inclusive_scan:
7763 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7764 break;
7765 default:
7766 assert(false);
7767 }
7768 } else if (cluster_size == 1) {
7769 bld.copy(Definition(dst), src);
7770 } else {
7771 unsigned bit_size = instr->src[0].ssa->bit_size;
7772
7773 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7774
7775 ReduceOp reduce_op;
7776 switch (op) {
7777 #define CASEI(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : (bit_size == 8) ? name##8 : name##64; break;
7778 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7779 CASEI(iadd)
7780 CASEI(imul)
7781 CASEI(imin)
7782 CASEI(umin)
7783 CASEI(imax)
7784 CASEI(umax)
7785 CASEI(iand)
7786 CASEI(ior)
7787 CASEI(ixor)
7788 CASEF(fadd)
7789 CASEF(fmul)
7790 CASEF(fmin)
7791 CASEF(fmax)
7792 default:
7793 unreachable("unknown reduction op");
7794 #undef CASEI
7795 #undef CASEF
7796 }
7797
7798 aco_opcode aco_op;
7799 switch (instr->intrinsic) {
7800 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7801 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7802 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7803 default:
7804 unreachable("unknown reduce intrinsic");
7805 }
7806
7807 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7808 reduce->operands[0] = Operand(src);
7809 // filled in by aco_reduce_assign.cpp, used internally as part of the
7810 // reduce sequence
7811 assert(dst.size() == 1 || dst.size() == 2);
7812 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7813 reduce->operands[2] = Operand(v1.as_linear());
7814
7815 Temp tmp_dst = bld.tmp(dst.regClass());
7816 reduce->definitions[0] = Definition(tmp_dst);
7817 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7818 reduce->definitions[2] = Definition();
7819 reduce->definitions[3] = Definition(scc, s1);
7820 reduce->definitions[4] = Definition();
7821 reduce->reduce_op = reduce_op;
7822 reduce->cluster_size = cluster_size;
7823 ctx->block->instructions.emplace_back(std::move(reduce));
7824
7825 emit_wqm(ctx, tmp_dst, dst);
7826 }
7827 break;
7828 }
7829 case nir_intrinsic_quad_broadcast: {
7830 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7831 if (!nir_dest_is_divergent(instr->dest)) {
7832 emit_uniform_subgroup(ctx, instr, src);
7833 } else {
7834 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7835 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7836 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7837
7838 if (instr->dest.ssa.bit_size == 1) {
7839 assert(src.regClass() == bld.lm);
7840 assert(dst.regClass() == bld.lm);
7841 uint32_t half_mask = 0x11111111u << lane;
7842 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7843 Temp tmp = bld.tmp(bld.lm);
7844 bld.sop1(Builder::s_wqm, Definition(tmp),
7845 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7846 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7847 emit_wqm(ctx, tmp, dst);
7848 } else if (instr->dest.ssa.bit_size == 8) {
7849 Temp tmp = bld.tmp(v1);
7850 if (ctx->program->chip_class >= GFX8)
7851 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7852 else
7853 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7854 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7855 } else if (instr->dest.ssa.bit_size == 16) {
7856 Temp tmp = bld.tmp(v1);
7857 if (ctx->program->chip_class >= GFX8)
7858 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7859 else
7860 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7861 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7862 } else if (instr->dest.ssa.bit_size == 32) {
7863 if (ctx->program->chip_class >= GFX8)
7864 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7865 else
7866 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7867 } else if (instr->dest.ssa.bit_size == 64) {
7868 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7869 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7870 if (ctx->program->chip_class >= GFX8) {
7871 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7872 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7873 } else {
7874 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7875 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7876 }
7877 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7878 emit_split_vector(ctx, dst, 2);
7879 } else {
7880 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7881 nir_print_instr(&instr->instr, stderr);
7882 fprintf(stderr, "\n");
7883 }
7884 }
7885 break;
7886 }
7887 case nir_intrinsic_quad_swap_horizontal:
7888 case nir_intrinsic_quad_swap_vertical:
7889 case nir_intrinsic_quad_swap_diagonal:
7890 case nir_intrinsic_quad_swizzle_amd: {
7891 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7892 if (!nir_dest_is_divergent(instr->dest)) {
7893 emit_uniform_subgroup(ctx, instr, src);
7894 break;
7895 }
7896 uint16_t dpp_ctrl = 0;
7897 switch (instr->intrinsic) {
7898 case nir_intrinsic_quad_swap_horizontal:
7899 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7900 break;
7901 case nir_intrinsic_quad_swap_vertical:
7902 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7903 break;
7904 case nir_intrinsic_quad_swap_diagonal:
7905 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7906 break;
7907 case nir_intrinsic_quad_swizzle_amd:
7908 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7909 break;
7910 default:
7911 break;
7912 }
7913 if (ctx->program->chip_class < GFX8)
7914 dpp_ctrl |= (1 << 15);
7915
7916 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7917 if (instr->dest.ssa.bit_size == 1) {
7918 assert(src.regClass() == bld.lm);
7919 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7920 if (ctx->program->chip_class >= GFX8)
7921 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7922 else
7923 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7924 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7925 emit_wqm(ctx, tmp, dst);
7926 } else if (instr->dest.ssa.bit_size == 8) {
7927 Temp tmp = bld.tmp(v1);
7928 if (ctx->program->chip_class >= GFX8)
7929 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7930 else
7931 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7932 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7933 } else if (instr->dest.ssa.bit_size == 16) {
7934 Temp tmp = bld.tmp(v1);
7935 if (ctx->program->chip_class >= GFX8)
7936 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7937 else
7938 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7939 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7940 } else if (instr->dest.ssa.bit_size == 32) {
7941 Temp tmp;
7942 if (ctx->program->chip_class >= GFX8)
7943 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7944 else
7945 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7946 emit_wqm(ctx, tmp, dst);
7947 } else if (instr->dest.ssa.bit_size == 64) {
7948 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7949 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7950 if (ctx->program->chip_class >= GFX8) {
7951 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7952 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7953 } else {
7954 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7955 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7956 }
7957 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7958 emit_split_vector(ctx, dst, 2);
7959 } else {
7960 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7961 nir_print_instr(&instr->instr, stderr);
7962 fprintf(stderr, "\n");
7963 }
7964 break;
7965 }
7966 case nir_intrinsic_masked_swizzle_amd: {
7967 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7968 if (!nir_dest_is_divergent(instr->dest)) {
7969 emit_uniform_subgroup(ctx, instr, src);
7970 break;
7971 }
7972 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7973 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7974 if (instr->dest.ssa.bit_size == 1) {
7975 assert(src.regClass() == bld.lm);
7976 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7977 src = emit_masked_swizzle(ctx, bld, src, mask);
7978 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7979 emit_wqm(ctx, tmp, dst);
7980 } else if (dst.regClass() == v1b) {
7981 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7982 emit_extract_vector(ctx, tmp, 0, dst);
7983 } else if (dst.regClass() == v2b) {
7984 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7985 emit_extract_vector(ctx, tmp, 0, dst);
7986 } else if (dst.regClass() == v1) {
7987 emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask), dst);
7988 } else if (dst.regClass() == v2) {
7989 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7990 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7991 lo = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, lo, mask));
7992 hi = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, hi, mask));
7993 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7994 emit_split_vector(ctx, dst, 2);
7995 } else {
7996 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7997 nir_print_instr(&instr->instr, stderr);
7998 fprintf(stderr, "\n");
7999 }
8000 break;
8001 }
8002 case nir_intrinsic_write_invocation_amd: {
8003 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
8004 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
8005 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
8006 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8007 if (dst.regClass() == v1) {
8008 /* src2 is ignored for writelane. RA assigns the same reg for dst */
8009 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
8010 } else if (dst.regClass() == v2) {
8011 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
8012 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
8013 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
8014 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
8015 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
8016 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
8017 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8018 emit_split_vector(ctx, dst, 2);
8019 } else {
8020 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8021 nir_print_instr(&instr->instr, stderr);
8022 fprintf(stderr, "\n");
8023 }
8024 break;
8025 }
8026 case nir_intrinsic_mbcnt_amd: {
8027 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8028 RegClass rc = RegClass(src.type(), 1);
8029 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
8030 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
8031 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8032 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
8033 emit_wqm(ctx, wqm_tmp, dst);
8034 break;
8035 }
8036 case nir_intrinsic_load_helper_invocation: {
8037 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8038 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
8039 ctx->block->kind |= block_kind_needs_lowering;
8040 ctx->program->needs_exact = true;
8041 break;
8042 }
8043 case nir_intrinsic_is_helper_invocation: {
8044 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8045 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
8046 ctx->block->kind |= block_kind_needs_lowering;
8047 ctx->program->needs_exact = true;
8048 break;
8049 }
8050 case nir_intrinsic_demote:
8051 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
8052
8053 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8054 ctx->cf_info.exec_potentially_empty_discard = true;
8055 ctx->block->kind |= block_kind_uses_demote;
8056 ctx->program->needs_exact = true;
8057 break;
8058 case nir_intrinsic_demote_if: {
8059 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8060 assert(src.regClass() == bld.lm);
8061 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
8062 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
8063
8064 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8065 ctx->cf_info.exec_potentially_empty_discard = true;
8066 ctx->block->kind |= block_kind_uses_demote;
8067 ctx->program->needs_exact = true;
8068 break;
8069 }
8070 case nir_intrinsic_first_invocation: {
8071 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
8072 get_ssa_temp(ctx, &instr->dest.ssa));
8073 break;
8074 }
8075 case nir_intrinsic_shader_clock: {
8076 aco_opcode opcode =
8077 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
8078 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
8079 bld.smem(opcode, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
8080 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
8081 break;
8082 }
8083 case nir_intrinsic_load_vertex_id_zero_base: {
8084 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8085 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
8086 break;
8087 }
8088 case nir_intrinsic_load_first_vertex: {
8089 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8090 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
8091 break;
8092 }
8093 case nir_intrinsic_load_base_instance: {
8094 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8095 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
8096 break;
8097 }
8098 case nir_intrinsic_load_instance_id: {
8099 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8100 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
8101 break;
8102 }
8103 case nir_intrinsic_load_draw_id: {
8104 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8105 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8106 break;
8107 }
8108 case nir_intrinsic_load_invocation_id: {
8109 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8110
8111 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8112 if (ctx->options->chip_class >= GFX10)
8113 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8114 else
8115 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8116 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8117 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8118 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8119 } else {
8120 unreachable("Unsupported stage for load_invocation_id");
8121 }
8122
8123 break;
8124 }
8125 case nir_intrinsic_load_primitive_id: {
8126 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8127
8128 switch (ctx->shader->info.stage) {
8129 case MESA_SHADER_GEOMETRY:
8130 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8131 break;
8132 case MESA_SHADER_TESS_CTRL:
8133 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8134 break;
8135 case MESA_SHADER_TESS_EVAL:
8136 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8137 break;
8138 default:
8139 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8140 }
8141
8142 break;
8143 }
8144 case nir_intrinsic_load_patch_vertices_in: {
8145 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8146 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8147
8148 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8149 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8150 break;
8151 }
8152 case nir_intrinsic_emit_vertex_with_counter: {
8153 visit_emit_vertex_with_counter(ctx, instr);
8154 break;
8155 }
8156 case nir_intrinsic_end_primitive_with_counter: {
8157 unsigned stream = nir_intrinsic_stream_id(instr);
8158 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8159 break;
8160 }
8161 case nir_intrinsic_set_vertex_count: {
8162 /* unused, the HW keeps track of this for us */
8163 break;
8164 }
8165 default:
8166 fprintf(stderr, "Unimplemented intrinsic instr: ");
8167 nir_print_instr(&instr->instr, stderr);
8168 fprintf(stderr, "\n");
8169 abort();
8170
8171 break;
8172 }
8173 }
8174
8175
8176 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8177 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8178 enum glsl_base_type *stype)
8179 {
8180 nir_deref_instr *texture_deref_instr = NULL;
8181 nir_deref_instr *sampler_deref_instr = NULL;
8182 int plane = -1;
8183
8184 for (unsigned i = 0; i < instr->num_srcs; i++) {
8185 switch (instr->src[i].src_type) {
8186 case nir_tex_src_texture_deref:
8187 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8188 break;
8189 case nir_tex_src_sampler_deref:
8190 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8191 break;
8192 case nir_tex_src_plane:
8193 plane = nir_src_as_int(instr->src[i].src);
8194 break;
8195 default:
8196 break;
8197 }
8198 }
8199
8200 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8201
8202 if (!sampler_deref_instr)
8203 sampler_deref_instr = texture_deref_instr;
8204
8205 if (plane >= 0) {
8206 assert(instr->op != nir_texop_txf_ms &&
8207 instr->op != nir_texop_samples_identical);
8208 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8209 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8210 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8211 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8212 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8213 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8214 } else {
8215 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8216 }
8217 if (samp_ptr) {
8218 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8219
8220 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8221 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8222 Builder bld(ctx->program, ctx->block);
8223
8224 /* to avoid unnecessary moves, we split and recombine sampler and image */
8225 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8226 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8227 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8228 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8229 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8230 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8231 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8232 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8233
8234 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8235 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8236 img[0], img[1], img[2], img[3],
8237 img[4], img[5], img[6], img[7]);
8238 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8239 samp[0], samp[1], samp[2], samp[3]);
8240 }
8241 }
8242 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8243 instr->op == nir_texop_samples_identical))
8244 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8245 }
8246
8247 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8248 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8249 {
8250 Builder bld(ctx->program, ctx->block);
8251
8252 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8253 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8254 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8255
8256 Operand neg_one(0xbf800000u);
8257 Operand one(0x3f800000u);
8258 Operand two(0x40000000u);
8259 Operand four(0x40800000u);
8260
8261 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8262 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8263 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8264
8265 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8266 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8267 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8268 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);
8269
8270 // select sc
8271 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8272 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8273 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8274 one, is_ma_y);
8275 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8276
8277 // select tc
8278 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8279 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8280 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8281
8282 // select ma
8283 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8284 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8285 deriv_z, is_ma_z);
8286 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8287 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8288 }
8289
8290 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8291 {
8292 Builder bld(ctx->program, ctx->block);
8293 Temp ma, tc, sc, id;
8294
8295 if (is_array) {
8296 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8297
8298 // see comment in ac_prepare_cube_coords()
8299 if (ctx->options->chip_class <= GFX8)
8300 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8301 }
8302
8303 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8304
8305 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8306 vop3a->operands[0] = Operand(ma);
8307 vop3a->abs[0] = true;
8308 Temp invma = bld.tmp(v1);
8309 vop3a->definitions[0] = Definition(invma);
8310 ctx->block->instructions.emplace_back(std::move(vop3a));
8311
8312 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8313 if (!is_deriv)
8314 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8315
8316 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8317 if (!is_deriv)
8318 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8319
8320 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8321
8322 if (is_deriv) {
8323 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8324 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8325
8326 for (unsigned i = 0; i < 2; i++) {
8327 // see comment in ac_prepare_cube_coords()
8328 Temp deriv_ma;
8329 Temp deriv_sc, deriv_tc;
8330 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8331 &deriv_ma, &deriv_sc, &deriv_tc);
8332
8333 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8334
8335 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8336 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8337 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8338 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8339 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8340 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8341 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8342 }
8343
8344 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8345 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8346 }
8347
8348 if (is_array)
8349 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8350 coords.resize(3);
8351 coords[0] = sc;
8352 coords[1] = tc;
8353 coords[2] = id;
8354 }
8355
8356 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8357 {
8358 if (vec->parent_instr->type != nir_instr_type_alu)
8359 return;
8360 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8361 if (vec_instr->op != nir_op_vec(vec->num_components))
8362 return;
8363
8364 for (unsigned i = 0; i < vec->num_components; i++) {
8365 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8366 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8367 }
8368 }
8369
8370 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8371 {
8372 Builder bld(ctx->program, ctx->block);
8373 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8374 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8375 has_clamped_lod = false;
8376 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8377 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8378 clamped_lod = Temp();
8379 std::vector<Temp> coords;
8380 std::vector<Temp> derivs;
8381 nir_const_value *sample_index_cv = NULL;
8382 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8383 enum glsl_base_type stype;
8384 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8385
8386 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8387 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8388 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8389 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8390
8391 for (unsigned i = 0; i < instr->num_srcs; i++) {
8392 switch (instr->src[i].src_type) {
8393 case nir_tex_src_coord: {
8394 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8395 for (unsigned i = 0; i < coord.size(); i++)
8396 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8397 break;
8398 }
8399 case nir_tex_src_bias:
8400 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8401 has_bias = true;
8402 break;
8403 case nir_tex_src_lod: {
8404 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8405
8406 if (val && val->f32 <= 0.0) {
8407 level_zero = true;
8408 } else {
8409 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8410 has_lod = true;
8411 }
8412 break;
8413 }
8414 case nir_tex_src_min_lod:
8415 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8416 has_clamped_lod = true;
8417 break;
8418 case nir_tex_src_comparator:
8419 if (instr->is_shadow) {
8420 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8421 has_compare = true;
8422 }
8423 break;
8424 case nir_tex_src_offset:
8425 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8426 get_const_vec(instr->src[i].src.ssa, const_offset);
8427 has_offset = true;
8428 break;
8429 case nir_tex_src_ddx:
8430 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8431 has_ddx = true;
8432 break;
8433 case nir_tex_src_ddy:
8434 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8435 has_ddy = true;
8436 break;
8437 case nir_tex_src_ms_index:
8438 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8439 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8440 has_sample_index = true;
8441 break;
8442 case nir_tex_src_texture_offset:
8443 case nir_tex_src_sampler_offset:
8444 default:
8445 break;
8446 }
8447 }
8448
8449 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8450 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8451
8452 if (instr->op == nir_texop_texture_samples) {
8453 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8454
8455 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8456 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8457 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 */));
8458
8459 Operand default_sample = Operand(1u);
8460 if (ctx->options->robust_buffer_access) {
8461 /* Extract the second dword of the descriptor, if it's
8462 * all zero, then it's a null descriptor.
8463 */
8464 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8465 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8466 default_sample = Operand(is_non_null_descriptor);
8467 }
8468
8469 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8470 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8471 samples, default_sample, bld.scc(is_msaa));
8472 return;
8473 }
8474
8475 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8476 aco_ptr<Instruction> tmp_instr;
8477 Temp acc, pack = Temp();
8478
8479 uint32_t pack_const = 0;
8480 for (unsigned i = 0; i < offset.size(); i++) {
8481 if (!const_offset[i])
8482 continue;
8483 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8484 }
8485
8486 if (offset.type() == RegType::sgpr) {
8487 for (unsigned i = 0; i < offset.size(); i++) {
8488 if (const_offset[i])
8489 continue;
8490
8491 acc = emit_extract_vector(ctx, offset, i, s1);
8492 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8493
8494 if (i) {
8495 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8496 }
8497
8498 if (pack == Temp()) {
8499 pack = acc;
8500 } else {
8501 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8502 }
8503 }
8504
8505 if (pack_const && pack != Temp())
8506 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8507 } else {
8508 for (unsigned i = 0; i < offset.size(); i++) {
8509 if (const_offset[i])
8510 continue;
8511
8512 acc = emit_extract_vector(ctx, offset, i, v1);
8513 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8514
8515 if (i) {
8516 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8517 }
8518
8519 if (pack == Temp()) {
8520 pack = acc;
8521 } else {
8522 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8523 }
8524 }
8525
8526 if (pack_const && pack != Temp())
8527 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8528 }
8529 if (pack_const && pack == Temp())
8530 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8531 else if (pack == Temp())
8532 has_offset = false;
8533 else
8534 offset = pack;
8535 }
8536
8537 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8538 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8539
8540 /* pack derivatives */
8541 if (has_ddx || has_ddy) {
8542 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8543 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8544 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8545 derivs = {ddx, zero, ddy, zero};
8546 } else {
8547 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8548 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8549 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8550 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8551 }
8552 has_derivs = true;
8553 }
8554
8555 if (instr->coord_components > 1 &&
8556 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8557 instr->is_array &&
8558 instr->op != nir_texop_txf)
8559 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8560
8561 if (instr->coord_components > 2 &&
8562 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8563 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8564 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8565 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8566 instr->is_array &&
8567 instr->op != nir_texop_txf &&
8568 instr->op != nir_texop_txf_ms &&
8569 instr->op != nir_texop_fragment_fetch &&
8570 instr->op != nir_texop_fragment_mask_fetch)
8571 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8572
8573 if (ctx->options->chip_class == GFX9 &&
8574 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8575 instr->op != nir_texop_lod && instr->coord_components) {
8576 assert(coords.size() > 0 && coords.size() < 3);
8577
8578 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8579 Operand((uint32_t) 0) :
8580 Operand((uint32_t) 0x3f000000)));
8581 }
8582
8583 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8584
8585 if (instr->op == nir_texop_samples_identical)
8586 resource = fmask_ptr;
8587
8588 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8589 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8590 instr->op != nir_texop_txs &&
8591 instr->op != nir_texop_fragment_fetch &&
8592 instr->op != nir_texop_fragment_mask_fetch) {
8593 assert(has_sample_index);
8594 Operand op(sample_index);
8595 if (sample_index_cv)
8596 op = Operand(sample_index_cv->u32);
8597 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8598 }
8599
8600 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8601 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8602 Temp off = emit_extract_vector(ctx, offset, i, v1);
8603 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8604 }
8605 has_offset = false;
8606 }
8607
8608 /* Build tex instruction */
8609 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8610 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8611 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8612 : 0;
8613 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8614 Temp tmp_dst = dst;
8615
8616 /* gather4 selects the component by dmask and always returns vec4 */
8617 if (instr->op == nir_texop_tg4) {
8618 assert(instr->dest.ssa.num_components == 4);
8619 if (instr->is_shadow)
8620 dmask = 1;
8621 else
8622 dmask = 1 << instr->component;
8623 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8624 tmp_dst = bld.tmp(v4);
8625 } else if (instr->op == nir_texop_samples_identical) {
8626 tmp_dst = bld.tmp(v1);
8627 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8628 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8629 }
8630
8631 aco_ptr<MIMG_instruction> tex;
8632 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8633 if (!has_lod)
8634 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8635
8636 bool div_by_6 = instr->op == nir_texop_txs &&
8637 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8638 instr->is_array &&
8639 (dmask & (1 << 2));
8640 if (tmp_dst.id() == dst.id() && div_by_6)
8641 tmp_dst = bld.tmp(tmp_dst.regClass());
8642
8643 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8644 tex->operands[0] = Operand(resource);
8645 tex->operands[1] = Operand(s4); /* no sampler */
8646 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8647 if (ctx->options->chip_class == GFX9 &&
8648 instr->op == nir_texop_txs &&
8649 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8650 instr->is_array) {
8651 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8652 } else if (instr->op == nir_texop_query_levels) {
8653 tex->dmask = 1 << 3;
8654 } else {
8655 tex->dmask = dmask;
8656 }
8657 tex->da = da;
8658 tex->definitions[0] = Definition(tmp_dst);
8659 tex->dim = dim;
8660 tex->can_reorder = true;
8661 ctx->block->instructions.emplace_back(std::move(tex));
8662
8663 if (div_by_6) {
8664 /* divide 3rd value by 6 by multiplying with magic number */
8665 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8666 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8667 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8668 assert(instr->dest.ssa.num_components == 3);
8669 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8670 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8671 emit_extract_vector(ctx, tmp_dst, 0, v1),
8672 emit_extract_vector(ctx, tmp_dst, 1, v1),
8673 by_6);
8674
8675 }
8676
8677 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8678 return;
8679 }
8680
8681 Temp tg4_compare_cube_wa64 = Temp();
8682
8683 if (tg4_integer_workarounds) {
8684 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8685 tex->operands[0] = Operand(resource);
8686 tex->operands[1] = Operand(s4); /* no sampler */
8687 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8688 tex->dim = dim;
8689 tex->dmask = 0x3;
8690 tex->da = da;
8691 Temp size = bld.tmp(v2);
8692 tex->definitions[0] = Definition(size);
8693 tex->can_reorder = true;
8694 ctx->block->instructions.emplace_back(std::move(tex));
8695 emit_split_vector(ctx, size, size.size());
8696
8697 Temp half_texel[2];
8698 for (unsigned i = 0; i < 2; i++) {
8699 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8700 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8701 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8702 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8703 }
8704
8705 Temp new_coords[2] = {
8706 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8707 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8708 };
8709
8710 if (tg4_integer_cube_workaround) {
8711 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8712 Temp desc[resource.size()];
8713 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8714 Format::PSEUDO, 1, resource.size())};
8715 split->operands[0] = Operand(resource);
8716 for (unsigned i = 0; i < resource.size(); i++) {
8717 desc[i] = bld.tmp(s1);
8718 split->definitions[i] = Definition(desc[i]);
8719 }
8720 ctx->block->instructions.emplace_back(std::move(split));
8721
8722 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8723 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8724 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8725
8726 Temp nfmt;
8727 if (stype == GLSL_TYPE_UINT) {
8728 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8729 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8730 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8731 bld.scc(compare_cube_wa));
8732 } else {
8733 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8734 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8735 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8736 bld.scc(compare_cube_wa));
8737 }
8738 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8739 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8740
8741 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8742
8743 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8744 Operand((uint32_t)C_008F14_NUM_FORMAT));
8745 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8746
8747 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8748 Format::PSEUDO, resource.size(), 1)};
8749 for (unsigned i = 0; i < resource.size(); i++)
8750 vec->operands[i] = Operand(desc[i]);
8751 resource = bld.tmp(resource.regClass());
8752 vec->definitions[0] = Definition(resource);
8753 ctx->block->instructions.emplace_back(std::move(vec));
8754
8755 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8756 new_coords[0], coords[0], tg4_compare_cube_wa64);
8757 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8758 new_coords[1], coords[1], tg4_compare_cube_wa64);
8759 }
8760 coords[0] = new_coords[0];
8761 coords[1] = new_coords[1];
8762 }
8763
8764 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8765 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8766
8767 assert(coords.size() == 1);
8768 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8769 aco_opcode op;
8770 switch (last_bit) {
8771 case 1:
8772 op = aco_opcode::buffer_load_format_x; break;
8773 case 2:
8774 op = aco_opcode::buffer_load_format_xy; break;
8775 case 3:
8776 op = aco_opcode::buffer_load_format_xyz; break;
8777 case 4:
8778 op = aco_opcode::buffer_load_format_xyzw; break;
8779 default:
8780 unreachable("Tex instruction loads more than 4 components.");
8781 }
8782
8783 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8784 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8785 tmp_dst = dst;
8786 else
8787 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8788
8789 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8790 mubuf->operands[0] = Operand(resource);
8791 mubuf->operands[1] = Operand(coords[0]);
8792 mubuf->operands[2] = Operand((uint32_t) 0);
8793 mubuf->definitions[0] = Definition(tmp_dst);
8794 mubuf->idxen = true;
8795 mubuf->can_reorder = true;
8796 ctx->block->instructions.emplace_back(std::move(mubuf));
8797
8798 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8799 return;
8800 }
8801
8802 /* gather MIMG address components */
8803 std::vector<Temp> args;
8804 if (has_offset)
8805 args.emplace_back(offset);
8806 if (has_bias)
8807 args.emplace_back(bias);
8808 if (has_compare)
8809 args.emplace_back(compare);
8810 if (has_derivs)
8811 args.insert(args.end(), derivs.begin(), derivs.end());
8812
8813 args.insert(args.end(), coords.begin(), coords.end());
8814 if (has_sample_index)
8815 args.emplace_back(sample_index);
8816 if (has_lod)
8817 args.emplace_back(lod);
8818 if (has_clamped_lod)
8819 args.emplace_back(clamped_lod);
8820
8821 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8822 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8823 vec->definitions[0] = Definition(arg);
8824 for (unsigned i = 0; i < args.size(); i++)
8825 vec->operands[i] = Operand(args[i]);
8826 ctx->block->instructions.emplace_back(std::move(vec));
8827
8828
8829 if (instr->op == nir_texop_txf ||
8830 instr->op == nir_texop_txf_ms ||
8831 instr->op == nir_texop_samples_identical ||
8832 instr->op == nir_texop_fragment_fetch ||
8833 instr->op == nir_texop_fragment_mask_fetch) {
8834 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;
8835 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8836 tex->operands[0] = Operand(resource);
8837 tex->operands[1] = Operand(s4); /* no sampler */
8838 tex->operands[2] = Operand(arg);
8839 tex->dim = dim;
8840 tex->dmask = dmask;
8841 tex->unrm = true;
8842 tex->da = da;
8843 tex->definitions[0] = Definition(tmp_dst);
8844 tex->can_reorder = true;
8845 ctx->block->instructions.emplace_back(std::move(tex));
8846
8847 if (instr->op == nir_texop_samples_identical) {
8848 assert(dmask == 1 && dst.regClass() == v1);
8849 assert(dst.id() != tmp_dst.id());
8850
8851 Temp tmp = bld.tmp(bld.lm);
8852 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8853 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8854
8855 } else {
8856 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8857 }
8858 return;
8859 }
8860
8861 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8862 aco_opcode opcode = aco_opcode::image_sample;
8863 if (has_offset) { /* image_sample_*_o */
8864 if (has_clamped_lod) {
8865 if (has_compare) {
8866 opcode = aco_opcode::image_sample_c_cl_o;
8867 if (has_derivs)
8868 opcode = aco_opcode::image_sample_c_d_cl_o;
8869 if (has_bias)
8870 opcode = aco_opcode::image_sample_c_b_cl_o;
8871 } else {
8872 opcode = aco_opcode::image_sample_cl_o;
8873 if (has_derivs)
8874 opcode = aco_opcode::image_sample_d_cl_o;
8875 if (has_bias)
8876 opcode = aco_opcode::image_sample_b_cl_o;
8877 }
8878 } else if (has_compare) {
8879 opcode = aco_opcode::image_sample_c_o;
8880 if (has_derivs)
8881 opcode = aco_opcode::image_sample_c_d_o;
8882 if (has_bias)
8883 opcode = aco_opcode::image_sample_c_b_o;
8884 if (level_zero)
8885 opcode = aco_opcode::image_sample_c_lz_o;
8886 if (has_lod)
8887 opcode = aco_opcode::image_sample_c_l_o;
8888 } else {
8889 opcode = aco_opcode::image_sample_o;
8890 if (has_derivs)
8891 opcode = aco_opcode::image_sample_d_o;
8892 if (has_bias)
8893 opcode = aco_opcode::image_sample_b_o;
8894 if (level_zero)
8895 opcode = aco_opcode::image_sample_lz_o;
8896 if (has_lod)
8897 opcode = aco_opcode::image_sample_l_o;
8898 }
8899 } else if (has_clamped_lod) { /* image_sample_*_cl */
8900 if (has_compare) {
8901 opcode = aco_opcode::image_sample_c_cl;
8902 if (has_derivs)
8903 opcode = aco_opcode::image_sample_c_d_cl;
8904 if (has_bias)
8905 opcode = aco_opcode::image_sample_c_b_cl;
8906 } else {
8907 opcode = aco_opcode::image_sample_cl;
8908 if (has_derivs)
8909 opcode = aco_opcode::image_sample_d_cl;
8910 if (has_bias)
8911 opcode = aco_opcode::image_sample_b_cl;
8912 }
8913 } else { /* no offset */
8914 if (has_compare) {
8915 opcode = aco_opcode::image_sample_c;
8916 if (has_derivs)
8917 opcode = aco_opcode::image_sample_c_d;
8918 if (has_bias)
8919 opcode = aco_opcode::image_sample_c_b;
8920 if (level_zero)
8921 opcode = aco_opcode::image_sample_c_lz;
8922 if (has_lod)
8923 opcode = aco_opcode::image_sample_c_l;
8924 } else {
8925 opcode = aco_opcode::image_sample;
8926 if (has_derivs)
8927 opcode = aco_opcode::image_sample_d;
8928 if (has_bias)
8929 opcode = aco_opcode::image_sample_b;
8930 if (level_zero)
8931 opcode = aco_opcode::image_sample_lz;
8932 if (has_lod)
8933 opcode = aco_opcode::image_sample_l;
8934 }
8935 }
8936
8937 if (instr->op == nir_texop_tg4) {
8938 if (has_offset) { /* image_gather4_*_o */
8939 if (has_compare) {
8940 opcode = aco_opcode::image_gather4_c_lz_o;
8941 if (has_lod)
8942 opcode = aco_opcode::image_gather4_c_l_o;
8943 if (has_bias)
8944 opcode = aco_opcode::image_gather4_c_b_o;
8945 } else {
8946 opcode = aco_opcode::image_gather4_lz_o;
8947 if (has_lod)
8948 opcode = aco_opcode::image_gather4_l_o;
8949 if (has_bias)
8950 opcode = aco_opcode::image_gather4_b_o;
8951 }
8952 } else {
8953 if (has_compare) {
8954 opcode = aco_opcode::image_gather4_c_lz;
8955 if (has_lod)
8956 opcode = aco_opcode::image_gather4_c_l;
8957 if (has_bias)
8958 opcode = aco_opcode::image_gather4_c_b;
8959 } else {
8960 opcode = aco_opcode::image_gather4_lz;
8961 if (has_lod)
8962 opcode = aco_opcode::image_gather4_l;
8963 if (has_bias)
8964 opcode = aco_opcode::image_gather4_b;
8965 }
8966 }
8967 } else if (instr->op == nir_texop_lod) {
8968 opcode = aco_opcode::image_get_lod;
8969 }
8970
8971 /* we don't need the bias, sample index, compare value or offset to be
8972 * computed in WQM but if the p_create_vector copies the coordinates, then it
8973 * needs to be in WQM */
8974 if (ctx->stage == fragment_fs &&
8975 !has_derivs && !has_lod && !level_zero &&
8976 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8977 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8978 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8979
8980 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8981 tex->operands[0] = Operand(resource);
8982 tex->operands[1] = Operand(sampler);
8983 tex->operands[2] = Operand(arg);
8984 tex->dim = dim;
8985 tex->dmask = dmask;
8986 tex->da = da;
8987 tex->definitions[0] = Definition(tmp_dst);
8988 tex->can_reorder = true;
8989 ctx->block->instructions.emplace_back(std::move(tex));
8990
8991 if (tg4_integer_cube_workaround) {
8992 assert(tmp_dst.id() != dst.id());
8993 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8994
8995 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8996 Temp val[4];
8997 for (unsigned i = 0; i < dst.size(); i++) {
8998 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8999 Temp cvt_val;
9000 if (stype == GLSL_TYPE_UINT)
9001 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
9002 else
9003 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
9004 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
9005 }
9006 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
9007 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
9008 val[0], val[1], val[2], val[3]);
9009 }
9010 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
9011 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
9012
9013 }
9014
9015
9016 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa, RegClass rc, bool logical)
9017 {
9018 Temp tmp = get_ssa_temp(ctx, ssa);
9019 if (ssa->parent_instr->type == nir_instr_type_ssa_undef) {
9020 return Operand(rc);
9021 } else if (logical && ssa->bit_size == 1 && ssa->parent_instr->type == nir_instr_type_load_const) {
9022 if (ctx->program->wave_size == 64)
9023 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT64_MAX : 0u);
9024 else
9025 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT32_MAX : 0u);
9026 } else {
9027 return Operand(tmp);
9028 }
9029 }
9030
9031 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
9032 {
9033 aco_ptr<Pseudo_instruction> phi;
9034 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
9035 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
9036
9037 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
9038 logical |= ctx->block->kind & block_kind_merge;
9039 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
9040
9041 /* we want a sorted list of sources, since the predecessor list is also sorted */
9042 std::map<unsigned, nir_ssa_def*> phi_src;
9043 nir_foreach_phi_src(src, instr)
9044 phi_src[src->pred->index] = src->src.ssa;
9045
9046 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
9047 unsigned num_operands = 0;
9048 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
9049 unsigned num_defined = 0;
9050 unsigned cur_pred_idx = 0;
9051 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
9052 if (cur_pred_idx < preds.size()) {
9053 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
9054 unsigned block = ctx->cf_info.nir_to_aco[src.first];
9055 unsigned skipped = 0;
9056 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
9057 skipped++;
9058 if (cur_pred_idx + skipped < preds.size()) {
9059 for (unsigned i = 0; i < skipped; i++)
9060 operands[num_operands++] = Operand(dst.regClass());
9061 cur_pred_idx += skipped;
9062 } else {
9063 continue;
9064 }
9065 }
9066 /* Handle missing predecessors at the end. This shouldn't happen with loop
9067 * headers and we can't ignore these sources for loop header phis. */
9068 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
9069 continue;
9070 cur_pred_idx++;
9071 Operand op = get_phi_operand(ctx, src.second, dst.regClass(), logical);
9072 operands[num_operands++] = op;
9073 num_defined += !op.isUndefined();
9074 }
9075 /* handle block_kind_continue_or_break at loop exit blocks */
9076 while (cur_pred_idx++ < preds.size())
9077 operands[num_operands++] = Operand(dst.regClass());
9078
9079 /* If the loop ends with a break, still add a linear continue edge in case
9080 * that break is divergent or continue_or_break is used. We'll either remove
9081 * this operand later in visit_loop() if it's not necessary or replace the
9082 * undef with something correct. */
9083 if (!logical && ctx->block->kind & block_kind_loop_header) {
9084 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
9085 nir_block *last = nir_loop_last_block(loop);
9086 if (last->successors[0] != instr->instr.block)
9087 operands[num_operands++] = Operand(RegClass());
9088 }
9089
9090 if (num_defined == 0) {
9091 Builder bld(ctx->program, ctx->block);
9092 if (dst.regClass() == s1) {
9093 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
9094 } else if (dst.regClass() == v1) {
9095 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
9096 } else {
9097 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9098 for (unsigned i = 0; i < dst.size(); i++)
9099 vec->operands[i] = Operand(0u);
9100 vec->definitions[0] = Definition(dst);
9101 ctx->block->instructions.emplace_back(std::move(vec));
9102 }
9103 return;
9104 }
9105
9106 /* we can use a linear phi in some cases if one src is undef */
9107 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9108 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9109
9110 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9111 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9112 assert(invert->kind & block_kind_invert);
9113
9114 unsigned then_block = invert->linear_preds[0];
9115
9116 Block* insert_block = NULL;
9117 for (unsigned i = 0; i < num_operands; i++) {
9118 Operand op = operands[i];
9119 if (op.isUndefined())
9120 continue;
9121 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9122 phi->operands[0] = op;
9123 break;
9124 }
9125 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9126 phi->operands[1] = Operand(dst.regClass());
9127 phi->definitions[0] = Definition(dst);
9128 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9129 return;
9130 }
9131
9132 /* try to scalarize vector phis */
9133 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9134 // TODO: scalarize linear phis on divergent ifs
9135 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9136 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9137 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9138 Operand src = operands[i];
9139 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9140 can_scalarize = false;
9141 }
9142 if (can_scalarize) {
9143 unsigned num_components = instr->dest.ssa.num_components;
9144 assert(dst.size() % num_components == 0);
9145 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9146
9147 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9148 for (unsigned k = 0; k < num_components; k++) {
9149 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9150 for (unsigned i = 0; i < num_operands; i++) {
9151 Operand src = operands[i];
9152 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9153 }
9154 Temp phi_dst = {ctx->program->allocateId(), rc};
9155 phi->definitions[0] = Definition(phi_dst);
9156 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9157 new_vec[k] = phi_dst;
9158 vec->operands[k] = Operand(phi_dst);
9159 }
9160 vec->definitions[0] = Definition(dst);
9161 ctx->block->instructions.emplace_back(std::move(vec));
9162 ctx->allocated_vec.emplace(dst.id(), new_vec);
9163 return;
9164 }
9165 }
9166
9167 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9168 for (unsigned i = 0; i < num_operands; i++)
9169 phi->operands[i] = operands[i];
9170 phi->definitions[0] = Definition(dst);
9171 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9172 }
9173
9174
9175 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9176 {
9177 Temp dst = get_ssa_temp(ctx, &instr->def);
9178
9179 assert(dst.type() == RegType::sgpr);
9180
9181 if (dst.size() == 1) {
9182 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9183 } else {
9184 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9185 for (unsigned i = 0; i < dst.size(); i++)
9186 vec->operands[i] = Operand(0u);
9187 vec->definitions[0] = Definition(dst);
9188 ctx->block->instructions.emplace_back(std::move(vec));
9189 }
9190 }
9191
9192 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9193 {
9194 Builder bld(ctx->program, ctx->block);
9195 Block *logical_target;
9196 append_logical_end(ctx->block);
9197 unsigned idx = ctx->block->index;
9198
9199 switch (instr->type) {
9200 case nir_jump_break:
9201 logical_target = ctx->cf_info.parent_loop.exit;
9202 add_logical_edge(idx, logical_target);
9203 ctx->block->kind |= block_kind_break;
9204
9205 if (!ctx->cf_info.parent_if.is_divergent &&
9206 !ctx->cf_info.parent_loop.has_divergent_continue) {
9207 /* uniform break - directly jump out of the loop */
9208 ctx->block->kind |= block_kind_uniform;
9209 ctx->cf_info.has_branch = true;
9210 bld.branch(aco_opcode::p_branch);
9211 add_linear_edge(idx, logical_target);
9212 return;
9213 }
9214 ctx->cf_info.parent_loop.has_divergent_branch = true;
9215 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9216 break;
9217 case nir_jump_continue:
9218 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9219 add_logical_edge(idx, logical_target);
9220 ctx->block->kind |= block_kind_continue;
9221
9222 if (ctx->cf_info.parent_if.is_divergent) {
9223 /* for potential uniform breaks after this continue,
9224 we must ensure that they are handled correctly */
9225 ctx->cf_info.parent_loop.has_divergent_continue = true;
9226 ctx->cf_info.parent_loop.has_divergent_branch = true;
9227 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9228 } else {
9229 /* uniform continue - directly jump to the loop header */
9230 ctx->block->kind |= block_kind_uniform;
9231 ctx->cf_info.has_branch = true;
9232 bld.branch(aco_opcode::p_branch);
9233 add_linear_edge(idx, logical_target);
9234 return;
9235 }
9236 break;
9237 default:
9238 fprintf(stderr, "Unknown NIR jump instr: ");
9239 nir_print_instr(&instr->instr, stderr);
9240 fprintf(stderr, "\n");
9241 abort();
9242 }
9243
9244 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9245 ctx->cf_info.exec_potentially_empty_break = true;
9246 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9247 }
9248
9249 /* remove critical edges from linear CFG */
9250 bld.branch(aco_opcode::p_branch);
9251 Block* break_block = ctx->program->create_and_insert_block();
9252 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9253 break_block->kind |= block_kind_uniform;
9254 add_linear_edge(idx, break_block);
9255 /* the loop_header pointer might be invalidated by this point */
9256 if (instr->type == nir_jump_continue)
9257 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9258 add_linear_edge(break_block->index, logical_target);
9259 bld.reset(break_block);
9260 bld.branch(aco_opcode::p_branch);
9261
9262 Block* continue_block = ctx->program->create_and_insert_block();
9263 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9264 add_linear_edge(idx, continue_block);
9265 append_logical_start(continue_block);
9266 ctx->block = continue_block;
9267 return;
9268 }
9269
9270 void visit_block(isel_context *ctx, nir_block *block)
9271 {
9272 nir_foreach_instr(instr, block) {
9273 switch (instr->type) {
9274 case nir_instr_type_alu:
9275 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9276 break;
9277 case nir_instr_type_load_const:
9278 visit_load_const(ctx, nir_instr_as_load_const(instr));
9279 break;
9280 case nir_instr_type_intrinsic:
9281 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9282 break;
9283 case nir_instr_type_tex:
9284 visit_tex(ctx, nir_instr_as_tex(instr));
9285 break;
9286 case nir_instr_type_phi:
9287 visit_phi(ctx, nir_instr_as_phi(instr));
9288 break;
9289 case nir_instr_type_ssa_undef:
9290 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9291 break;
9292 case nir_instr_type_deref:
9293 break;
9294 case nir_instr_type_jump:
9295 visit_jump(ctx, nir_instr_as_jump(instr));
9296 break;
9297 default:
9298 fprintf(stderr, "Unknown NIR instr type: ");
9299 nir_print_instr(instr, stderr);
9300 fprintf(stderr, "\n");
9301 //abort();
9302 }
9303 }
9304
9305 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9306 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9307 }
9308
9309
9310
9311 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9312 aco_ptr<Instruction>& header_phi, Operand *vals)
9313 {
9314 vals[0] = Operand(header_phi->definitions[0].getTemp());
9315 RegClass rc = vals[0].regClass();
9316
9317 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9318
9319 unsigned next_pred = 1;
9320
9321 for (unsigned idx = first + 1; idx <= last; idx++) {
9322 Block& block = ctx->program->blocks[idx];
9323 if (block.loop_nest_depth != loop_nest_depth) {
9324 vals[idx - first] = vals[idx - 1 - first];
9325 continue;
9326 }
9327
9328 if (block.kind & block_kind_continue) {
9329 vals[idx - first] = header_phi->operands[next_pred];
9330 next_pred++;
9331 continue;
9332 }
9333
9334 bool all_same = true;
9335 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9336 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9337
9338 Operand val;
9339 if (all_same) {
9340 val = vals[block.linear_preds[0] - first];
9341 } else {
9342 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9343 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9344 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9345 phi->operands[i] = vals[block.linear_preds[i] - first];
9346 val = Operand(Temp(ctx->program->allocateId(), rc));
9347 phi->definitions[0] = Definition(val.getTemp());
9348 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9349 }
9350 vals[idx - first] = val;
9351 }
9352
9353 return vals[last - first];
9354 }
9355
9356 static void visit_loop(isel_context *ctx, nir_loop *loop)
9357 {
9358 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9359 append_logical_end(ctx->block);
9360 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9361 Builder bld(ctx->program, ctx->block);
9362 bld.branch(aco_opcode::p_branch);
9363 unsigned loop_preheader_idx = ctx->block->index;
9364
9365 Block loop_exit = Block();
9366 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9367 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9368
9369 Block* loop_header = ctx->program->create_and_insert_block();
9370 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9371 loop_header->kind |= block_kind_loop_header;
9372 add_edge(loop_preheader_idx, loop_header);
9373 ctx->block = loop_header;
9374
9375 /* emit loop body */
9376 unsigned loop_header_idx = loop_header->index;
9377 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9378 append_logical_start(ctx->block);
9379 bool unreachable = visit_cf_list(ctx, &loop->body);
9380
9381 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9382 if (!ctx->cf_info.has_branch) {
9383 append_logical_end(ctx->block);
9384 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9385 /* Discards can result in code running with an empty exec mask.
9386 * This would result in divergent breaks not ever being taken. As a
9387 * workaround, break the loop when the loop mask is empty instead of
9388 * always continuing. */
9389 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9390 unsigned block_idx = ctx->block->index;
9391
9392 /* create helper blocks to avoid critical edges */
9393 Block *break_block = ctx->program->create_and_insert_block();
9394 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9395 break_block->kind = block_kind_uniform;
9396 bld.reset(break_block);
9397 bld.branch(aco_opcode::p_branch);
9398 add_linear_edge(block_idx, break_block);
9399 add_linear_edge(break_block->index, &loop_exit);
9400
9401 Block *continue_block = ctx->program->create_and_insert_block();
9402 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9403 continue_block->kind = block_kind_uniform;
9404 bld.reset(continue_block);
9405 bld.branch(aco_opcode::p_branch);
9406 add_linear_edge(block_idx, continue_block);
9407 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9408
9409 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9410 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9411 ctx->block = &ctx->program->blocks[block_idx];
9412 } else {
9413 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9414 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9415 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9416 else
9417 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9418 }
9419
9420 bld.reset(ctx->block);
9421 bld.branch(aco_opcode::p_branch);
9422 }
9423
9424 /* Fixup phis in loop header from unreachable blocks.
9425 * has_branch/has_divergent_branch also indicates if the loop ends with a
9426 * break/continue instruction, but we don't emit those if unreachable=true */
9427 if (unreachable) {
9428 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9429 bool linear = ctx->cf_info.has_branch;
9430 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9431 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9432 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9433 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9434 /* the last operand should be the one that needs to be removed */
9435 instr->operands.pop_back();
9436 } else if (!is_phi(instr)) {
9437 break;
9438 }
9439 }
9440 }
9441
9442 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9443 * and the previous one shouldn't both happen at once because a break in the
9444 * merge block would get CSE'd */
9445 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9446 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9447 Operand vals[num_vals];
9448 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9449 if (instr->opcode == aco_opcode::p_linear_phi) {
9450 if (ctx->cf_info.has_branch)
9451 instr->operands.pop_back();
9452 else
9453 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9454 } else if (!is_phi(instr)) {
9455 break;
9456 }
9457 }
9458 }
9459
9460 ctx->cf_info.has_branch = false;
9461
9462 // TODO: if the loop has not a single exit, we must add one °°
9463 /* emit loop successor block */
9464 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9465 append_logical_start(ctx->block);
9466
9467 #if 0
9468 // TODO: check if it is beneficial to not branch on continues
9469 /* trim linear phis in loop header */
9470 for (auto&& instr : loop_entry->instructions) {
9471 if (instr->opcode == aco_opcode::p_linear_phi) {
9472 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9473 new_phi->definitions[0] = instr->definitions[0];
9474 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9475 new_phi->operands[i] = instr->operands[i];
9476 /* check that the remaining operands are all the same */
9477 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9478 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9479 instr.swap(new_phi);
9480 } else if (instr->opcode == aco_opcode::p_phi) {
9481 continue;
9482 } else {
9483 break;
9484 }
9485 }
9486 #endif
9487 }
9488
9489 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9490 {
9491 ic->cond = cond;
9492
9493 append_logical_end(ctx->block);
9494 ctx->block->kind |= block_kind_branch;
9495
9496 /* branch to linear then block */
9497 assert(cond.regClass() == ctx->program->lane_mask);
9498 aco_ptr<Pseudo_branch_instruction> branch;
9499 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9500 branch->operands[0] = Operand(cond);
9501 ctx->block->instructions.push_back(std::move(branch));
9502
9503 ic->BB_if_idx = ctx->block->index;
9504 ic->BB_invert = Block();
9505 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9506 /* Invert blocks are intentionally not marked as top level because they
9507 * are not part of the logical cfg. */
9508 ic->BB_invert.kind |= block_kind_invert;
9509 ic->BB_endif = Block();
9510 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9511 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9512
9513 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9514 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9515 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9516 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9517 ctx->cf_info.parent_if.is_divergent = true;
9518
9519 /* divergent branches use cbranch_execz */
9520 ctx->cf_info.exec_potentially_empty_discard = false;
9521 ctx->cf_info.exec_potentially_empty_break = false;
9522 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9523
9524 /** emit logical then block */
9525 Block* BB_then_logical = ctx->program->create_and_insert_block();
9526 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9527 add_edge(ic->BB_if_idx, BB_then_logical);
9528 ctx->block = BB_then_logical;
9529 append_logical_start(BB_then_logical);
9530 }
9531
9532 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9533 {
9534 Block *BB_then_logical = ctx->block;
9535 append_logical_end(BB_then_logical);
9536 /* branch from logical then block to invert block */
9537 aco_ptr<Pseudo_branch_instruction> branch;
9538 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9539 BB_then_logical->instructions.emplace_back(std::move(branch));
9540 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9541 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9542 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9543 BB_then_logical->kind |= block_kind_uniform;
9544 assert(!ctx->cf_info.has_branch);
9545 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9546 ctx->cf_info.parent_loop.has_divergent_branch = false;
9547
9548 /** emit linear then block */
9549 Block* BB_then_linear = ctx->program->create_and_insert_block();
9550 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9551 BB_then_linear->kind |= block_kind_uniform;
9552 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9553 /* branch from linear then block to invert block */
9554 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9555 BB_then_linear->instructions.emplace_back(std::move(branch));
9556 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9557
9558 /** emit invert merge block */
9559 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9560 ic->invert_idx = ctx->block->index;
9561
9562 /* branch to linear else block (skip else) */
9563 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9564 branch->operands[0] = Operand(ic->cond);
9565 ctx->block->instructions.push_back(std::move(branch));
9566
9567 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9568 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9569 ic->exec_potentially_empty_break_depth_old =
9570 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9571 /* divergent branches use cbranch_execz */
9572 ctx->cf_info.exec_potentially_empty_discard = false;
9573 ctx->cf_info.exec_potentially_empty_break = false;
9574 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9575
9576 /** emit logical else block */
9577 Block* BB_else_logical = ctx->program->create_and_insert_block();
9578 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9579 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9580 add_linear_edge(ic->invert_idx, BB_else_logical);
9581 ctx->block = BB_else_logical;
9582 append_logical_start(BB_else_logical);
9583 }
9584
9585 static void end_divergent_if(isel_context *ctx, if_context *ic)
9586 {
9587 Block *BB_else_logical = ctx->block;
9588 append_logical_end(BB_else_logical);
9589
9590 /* branch from logical else block to endif block */
9591 aco_ptr<Pseudo_branch_instruction> branch;
9592 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9593 BB_else_logical->instructions.emplace_back(std::move(branch));
9594 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9595 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9596 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9597 BB_else_logical->kind |= block_kind_uniform;
9598
9599 assert(!ctx->cf_info.has_branch);
9600 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9601
9602
9603 /** emit linear else block */
9604 Block* BB_else_linear = ctx->program->create_and_insert_block();
9605 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9606 BB_else_linear->kind |= block_kind_uniform;
9607 add_linear_edge(ic->invert_idx, BB_else_linear);
9608
9609 /* branch from linear else block to endif block */
9610 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9611 BB_else_linear->instructions.emplace_back(std::move(branch));
9612 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9613
9614
9615 /** emit endif merge block */
9616 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9617 append_logical_start(ctx->block);
9618
9619
9620 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9621 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9622 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9623 ctx->cf_info.exec_potentially_empty_break_depth =
9624 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9625 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9626 !ctx->cf_info.parent_if.is_divergent) {
9627 ctx->cf_info.exec_potentially_empty_break = false;
9628 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9629 }
9630 /* uniform control flow never has an empty exec-mask */
9631 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9632 ctx->cf_info.exec_potentially_empty_discard = false;
9633 ctx->cf_info.exec_potentially_empty_break = false;
9634 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9635 }
9636 }
9637
9638 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9639 {
9640 assert(cond.regClass() == s1);
9641
9642 append_logical_end(ctx->block);
9643 ctx->block->kind |= block_kind_uniform;
9644
9645 aco_ptr<Pseudo_branch_instruction> branch;
9646 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9647 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9648 branch->operands[0] = Operand(cond);
9649 branch->operands[0].setFixed(scc);
9650 ctx->block->instructions.emplace_back(std::move(branch));
9651
9652 ic->BB_if_idx = ctx->block->index;
9653 ic->BB_endif = Block();
9654 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9655 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9656
9657 ctx->cf_info.has_branch = false;
9658 ctx->cf_info.parent_loop.has_divergent_branch = false;
9659
9660 /** emit then block */
9661 Block* BB_then = ctx->program->create_and_insert_block();
9662 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9663 add_edge(ic->BB_if_idx, BB_then);
9664 append_logical_start(BB_then);
9665 ctx->block = BB_then;
9666 }
9667
9668 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9669 {
9670 Block *BB_then = ctx->block;
9671
9672 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9673 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9674
9675 if (!ic->uniform_has_then_branch) {
9676 append_logical_end(BB_then);
9677 /* branch from then block to endif block */
9678 aco_ptr<Pseudo_branch_instruction> branch;
9679 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9680 BB_then->instructions.emplace_back(std::move(branch));
9681 add_linear_edge(BB_then->index, &ic->BB_endif);
9682 if (!ic->then_branch_divergent)
9683 add_logical_edge(BB_then->index, &ic->BB_endif);
9684 BB_then->kind |= block_kind_uniform;
9685 }
9686
9687 ctx->cf_info.has_branch = false;
9688 ctx->cf_info.parent_loop.has_divergent_branch = false;
9689
9690 /** emit else block */
9691 Block* BB_else = ctx->program->create_and_insert_block();
9692 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9693 add_edge(ic->BB_if_idx, BB_else);
9694 append_logical_start(BB_else);
9695 ctx->block = BB_else;
9696 }
9697
9698 static void end_uniform_if(isel_context *ctx, if_context *ic)
9699 {
9700 Block *BB_else = ctx->block;
9701
9702 if (!ctx->cf_info.has_branch) {
9703 append_logical_end(BB_else);
9704 /* branch from then block to endif block */
9705 aco_ptr<Pseudo_branch_instruction> branch;
9706 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9707 BB_else->instructions.emplace_back(std::move(branch));
9708 add_linear_edge(BB_else->index, &ic->BB_endif);
9709 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9710 add_logical_edge(BB_else->index, &ic->BB_endif);
9711 BB_else->kind |= block_kind_uniform;
9712 }
9713
9714 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9715 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9716
9717 /** emit endif merge block */
9718 if (!ctx->cf_info.has_branch) {
9719 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9720 append_logical_start(ctx->block);
9721 }
9722 }
9723
9724 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9725 {
9726 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9727 Builder bld(ctx->program, ctx->block);
9728 aco_ptr<Pseudo_branch_instruction> branch;
9729 if_context ic;
9730
9731 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9732 /**
9733 * Uniform conditionals are represented in the following way*) :
9734 *
9735 * The linear and logical CFG:
9736 * BB_IF
9737 * / \
9738 * BB_THEN (logical) BB_ELSE (logical)
9739 * \ /
9740 * BB_ENDIF
9741 *
9742 * *) Exceptions may be due to break and continue statements within loops
9743 * If a break/continue happens within uniform control flow, it branches
9744 * to the loop exit/entry block. Otherwise, it branches to the next
9745 * merge block.
9746 **/
9747
9748 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9749 assert(cond.regClass() == ctx->program->lane_mask);
9750 cond = bool_to_scalar_condition(ctx, cond);
9751
9752 begin_uniform_if_then(ctx, &ic, cond);
9753 visit_cf_list(ctx, &if_stmt->then_list);
9754
9755 begin_uniform_if_else(ctx, &ic);
9756 visit_cf_list(ctx, &if_stmt->else_list);
9757
9758 end_uniform_if(ctx, &ic);
9759 } else { /* non-uniform condition */
9760 /**
9761 * To maintain a logical and linear CFG without critical edges,
9762 * non-uniform conditionals are represented in the following way*) :
9763 *
9764 * The linear CFG:
9765 * BB_IF
9766 * / \
9767 * BB_THEN (logical) BB_THEN (linear)
9768 * \ /
9769 * BB_INVERT (linear)
9770 * / \
9771 * BB_ELSE (logical) BB_ELSE (linear)
9772 * \ /
9773 * BB_ENDIF
9774 *
9775 * The logical CFG:
9776 * BB_IF
9777 * / \
9778 * BB_THEN (logical) BB_ELSE (logical)
9779 * \ /
9780 * BB_ENDIF
9781 *
9782 * *) Exceptions may be due to break and continue statements within loops
9783 **/
9784
9785 begin_divergent_if_then(ctx, &ic, cond);
9786 visit_cf_list(ctx, &if_stmt->then_list);
9787
9788 begin_divergent_if_else(ctx, &ic);
9789 visit_cf_list(ctx, &if_stmt->else_list);
9790
9791 end_divergent_if(ctx, &ic);
9792 }
9793
9794 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9795 }
9796
9797 static bool visit_cf_list(isel_context *ctx,
9798 struct exec_list *list)
9799 {
9800 foreach_list_typed(nir_cf_node, node, node, list) {
9801 switch (node->type) {
9802 case nir_cf_node_block:
9803 visit_block(ctx, nir_cf_node_as_block(node));
9804 break;
9805 case nir_cf_node_if:
9806 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9807 return true;
9808 break;
9809 case nir_cf_node_loop:
9810 visit_loop(ctx, nir_cf_node_as_loop(node));
9811 break;
9812 default:
9813 unreachable("unimplemented cf list type");
9814 }
9815 }
9816 return false;
9817 }
9818
9819 static void create_null_export(isel_context *ctx)
9820 {
9821 /* Some shader stages always need to have exports.
9822 * So when there is none, we need to add a null export.
9823 */
9824
9825 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9826 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9827 Builder bld(ctx->program, ctx->block);
9828 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9829 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9830 }
9831
9832 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9833 {
9834 assert(ctx->stage == vertex_vs ||
9835 ctx->stage == tess_eval_vs ||
9836 ctx->stage == gs_copy_vs ||
9837 ctx->stage == ngg_vertex_gs ||
9838 ctx->stage == ngg_tess_eval_gs);
9839
9840 int offset = (ctx->stage & sw_tes)
9841 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9842 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9843 uint64_t mask = ctx->outputs.mask[slot];
9844 if (!is_pos && !mask)
9845 return false;
9846 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9847 return false;
9848 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9849 exp->enabled_mask = mask;
9850 for (unsigned i = 0; i < 4; ++i) {
9851 if (mask & (1 << i))
9852 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9853 else
9854 exp->operands[i] = Operand(v1);
9855 }
9856 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9857 * Setting valid_mask=1 prevents it and has no other effect.
9858 */
9859 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9860 exp->done = false;
9861 exp->compressed = false;
9862 if (is_pos)
9863 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9864 else
9865 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9866 ctx->block->instructions.emplace_back(std::move(exp));
9867
9868 return true;
9869 }
9870
9871 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9872 {
9873 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9874 exp->enabled_mask = 0;
9875 for (unsigned i = 0; i < 4; ++i)
9876 exp->operands[i] = Operand(v1);
9877 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9878 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9879 exp->enabled_mask |= 0x1;
9880 }
9881 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9882 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9883 exp->enabled_mask |= 0x4;
9884 }
9885 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9886 if (ctx->options->chip_class < GFX9) {
9887 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9888 exp->enabled_mask |= 0x8;
9889 } else {
9890 Builder bld(ctx->program, ctx->block);
9891
9892 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9893 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9894 if (exp->operands[2].isTemp())
9895 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9896
9897 exp->operands[2] = Operand(out);
9898 exp->enabled_mask |= 0x4;
9899 }
9900 }
9901 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9902 exp->done = false;
9903 exp->compressed = false;
9904 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9905 ctx->block->instructions.emplace_back(std::move(exp));
9906 }
9907
9908 static void create_export_phis(isel_context *ctx)
9909 {
9910 /* Used when exports are needed, but the output temps are defined in a preceding block.
9911 * This function will set up phis in order to access the outputs in the next block.
9912 */
9913
9914 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9915 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9916 ctx->block->instructions.pop_back();
9917
9918 Builder bld(ctx->program, ctx->block);
9919
9920 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9921 uint64_t mask = ctx->outputs.mask[slot];
9922 for (unsigned i = 0; i < 4; ++i) {
9923 if (!(mask & (1 << i)))
9924 continue;
9925
9926 Temp old = ctx->outputs.temps[slot * 4 + i];
9927 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9928 ctx->outputs.temps[slot * 4 + i] = phi;
9929 }
9930 }
9931
9932 bld.insert(std::move(logical_start));
9933 }
9934
9935 static void create_vs_exports(isel_context *ctx)
9936 {
9937 assert(ctx->stage == vertex_vs ||
9938 ctx->stage == tess_eval_vs ||
9939 ctx->stage == gs_copy_vs ||
9940 ctx->stage == ngg_vertex_gs ||
9941 ctx->stage == ngg_tess_eval_gs);
9942
9943 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9944 ? &ctx->program->info->tes.outinfo
9945 : &ctx->program->info->vs.outinfo;
9946
9947 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9948 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9949 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9950 }
9951
9952 if (ctx->options->key.has_multiview_view_index) {
9953 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9954 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9955 }
9956
9957 /* the order these position exports are created is important */
9958 int next_pos = 0;
9959 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9960 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9961 export_vs_psiz_layer_viewport(ctx, &next_pos);
9962 exported_pos = true;
9963 }
9964 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9965 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9966 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9967 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9968
9969 if (ctx->export_clip_dists) {
9970 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9971 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9972 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9973 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9974 }
9975
9976 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9977 if (i < VARYING_SLOT_VAR0 &&
9978 i != VARYING_SLOT_LAYER &&
9979 i != VARYING_SLOT_PRIMITIVE_ID &&
9980 i != VARYING_SLOT_VIEWPORT)
9981 continue;
9982
9983 export_vs_varying(ctx, i, false, NULL);
9984 }
9985
9986 if (!exported_pos)
9987 create_null_export(ctx);
9988 }
9989
9990 static bool export_fs_mrt_z(isel_context *ctx)
9991 {
9992 Builder bld(ctx->program, ctx->block);
9993 unsigned enabled_channels = 0;
9994 bool compr = false;
9995 Operand values[4];
9996
9997 for (unsigned i = 0; i < 4; ++i) {
9998 values[i] = Operand(v1);
9999 }
10000
10001 /* Both stencil and sample mask only need 16-bits. */
10002 if (!ctx->program->info->ps.writes_z &&
10003 (ctx->program->info->ps.writes_stencil ||
10004 ctx->program->info->ps.writes_sample_mask)) {
10005 compr = true; /* COMPR flag */
10006
10007 if (ctx->program->info->ps.writes_stencil) {
10008 /* Stencil should be in X[23:16]. */
10009 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10010 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
10011 enabled_channels |= 0x3;
10012 }
10013
10014 if (ctx->program->info->ps.writes_sample_mask) {
10015 /* SampleMask should be in Y[15:0]. */
10016 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10017 enabled_channels |= 0xc;
10018 }
10019 } else {
10020 if (ctx->program->info->ps.writes_z) {
10021 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
10022 enabled_channels |= 0x1;
10023 }
10024
10025 if (ctx->program->info->ps.writes_stencil) {
10026 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10027 enabled_channels |= 0x2;
10028 }
10029
10030 if (ctx->program->info->ps.writes_sample_mask) {
10031 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10032 enabled_channels |= 0x4;
10033 }
10034 }
10035
10036 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
10037 * writemask component.
10038 */
10039 if (ctx->options->chip_class == GFX6 &&
10040 ctx->options->family != CHIP_OLAND &&
10041 ctx->options->family != CHIP_HAINAN) {
10042 enabled_channels |= 0x1;
10043 }
10044
10045 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10046 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
10047
10048 return true;
10049 }
10050
10051 static bool export_fs_mrt_color(isel_context *ctx, int slot)
10052 {
10053 Builder bld(ctx->program, ctx->block);
10054 unsigned write_mask = ctx->outputs.mask[slot];
10055 Operand values[4];
10056
10057 for (unsigned i = 0; i < 4; ++i) {
10058 if (write_mask & (1 << i)) {
10059 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
10060 } else {
10061 values[i] = Operand(v1);
10062 }
10063 }
10064
10065 unsigned target, col_format;
10066 unsigned enabled_channels = 0;
10067 aco_opcode compr_op = (aco_opcode)0;
10068
10069 slot -= FRAG_RESULT_DATA0;
10070 target = V_008DFC_SQ_EXP_MRT + slot;
10071 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
10072
10073 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
10074 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
10075 bool is_16bit = values[0].regClass() == v2b;
10076
10077 switch (col_format)
10078 {
10079 case V_028714_SPI_SHADER_ZERO:
10080 enabled_channels = 0; /* writemask */
10081 target = V_008DFC_SQ_EXP_NULL;
10082 break;
10083
10084 case V_028714_SPI_SHADER_32_R:
10085 enabled_channels = 1;
10086 break;
10087
10088 case V_028714_SPI_SHADER_32_GR:
10089 enabled_channels = 0x3;
10090 break;
10091
10092 case V_028714_SPI_SHADER_32_AR:
10093 if (ctx->options->chip_class >= GFX10) {
10094 /* Special case: on GFX10, the outputs are different for 32_AR */
10095 enabled_channels = 0x3;
10096 values[1] = values[3];
10097 values[3] = Operand(v1);
10098 } else {
10099 enabled_channels = 0x9;
10100 }
10101 break;
10102
10103 case V_028714_SPI_SHADER_FP16_ABGR:
10104 enabled_channels = 0x5;
10105 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
10106 if (is_16bit) {
10107 if (ctx->options->chip_class >= GFX9) {
10108 /* Pack the FP16 values together instead of converting them to
10109 * FP32 and back to FP16.
10110 * TODO: use p_create_vector and let the compiler optimizes.
10111 */
10112 compr_op = aco_opcode::v_pack_b32_f16;
10113 } else {
10114 for (unsigned i = 0; i < 4; i++) {
10115 if ((write_mask >> i) & 1)
10116 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
10117 }
10118 }
10119 }
10120 break;
10121
10122 case V_028714_SPI_SHADER_UNORM16_ABGR:
10123 enabled_channels = 0x5;
10124 if (is_16bit && ctx->options->chip_class >= GFX9) {
10125 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
10126 } else {
10127 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10128 }
10129 break;
10130
10131 case V_028714_SPI_SHADER_SNORM16_ABGR:
10132 enabled_channels = 0x5;
10133 if (is_16bit && ctx->options->chip_class >= GFX9) {
10134 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
10135 } else {
10136 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10137 }
10138 break;
10139
10140 case V_028714_SPI_SHADER_UINT16_ABGR: {
10141 enabled_channels = 0x5;
10142 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10143 if (is_int8 || is_int10) {
10144 /* clamp */
10145 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10146 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10147
10148 for (unsigned i = 0; i < 4; i++) {
10149 if ((write_mask >> i) & 1) {
10150 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10151 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10152 values[i]);
10153 }
10154 }
10155 } else if (is_16bit) {
10156 for (unsigned i = 0; i < 4; i++) {
10157 if ((write_mask >> i) & 1) {
10158 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, false);
10159 values[i] = Operand(tmp);
10160 }
10161 }
10162 }
10163 break;
10164 }
10165
10166 case V_028714_SPI_SHADER_SINT16_ABGR:
10167 enabled_channels = 0x5;
10168 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10169 if (is_int8 || is_int10) {
10170 /* clamp */
10171 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10172 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10173 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10174 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10175
10176 for (unsigned i = 0; i < 4; i++) {
10177 if ((write_mask >> i) & 1) {
10178 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10179 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10180 values[i]);
10181 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10182 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10183 values[i]);
10184 }
10185 }
10186 } else if (is_16bit) {
10187 for (unsigned i = 0; i < 4; i++) {
10188 if ((write_mask >> i) & 1) {
10189 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, true);
10190 values[i] = Operand(tmp);
10191 }
10192 }
10193 }
10194 break;
10195
10196 case V_028714_SPI_SHADER_32_ABGR:
10197 enabled_channels = 0xF;
10198 break;
10199
10200 default:
10201 break;
10202 }
10203
10204 if (target == V_008DFC_SQ_EXP_NULL)
10205 return false;
10206
10207 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10208 if (ctx->options->enable_mrt_output_nan_fixup &&
10209 !is_16bit &&
10210 (col_format == V_028714_SPI_SHADER_32_R ||
10211 col_format == V_028714_SPI_SHADER_32_GR ||
10212 col_format == V_028714_SPI_SHADER_32_AR ||
10213 col_format == V_028714_SPI_SHADER_32_ABGR ||
10214 col_format == V_028714_SPI_SHADER_FP16_ABGR)) {
10215 for (int i = 0; i < 4; i++) {
10216 if (!(write_mask & (1 << i)))
10217 continue;
10218
10219 Temp isnan = bld.vopc(aco_opcode::v_cmp_class_f32,
10220 bld.hint_vcc(bld.def(bld.lm)), values[i],
10221 bld.copy(bld.def(v1), Operand(3u)));
10222 values[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), values[i],
10223 bld.copy(bld.def(v1), Operand(0u)), isnan);
10224 }
10225 }
10226
10227 if ((bool) compr_op) {
10228 for (int i = 0; i < 2; i++) {
10229 /* check if at least one of the values to be compressed is enabled */
10230 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10231 if (enabled) {
10232 enabled_channels |= enabled << (i*2);
10233 values[i] = bld.vop3(compr_op, bld.def(v1),
10234 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10235 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10236 } else {
10237 values[i] = Operand(v1);
10238 }
10239 }
10240 values[2] = Operand(v1);
10241 values[3] = Operand(v1);
10242 } else {
10243 for (int i = 0; i < 4; i++)
10244 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10245 }
10246
10247 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10248 enabled_channels, target, (bool) compr_op);
10249 return true;
10250 }
10251
10252 static void create_fs_exports(isel_context *ctx)
10253 {
10254 bool exported = false;
10255
10256 /* Export depth, stencil and sample mask. */
10257 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10258 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10259 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10260 exported |= export_fs_mrt_z(ctx);
10261
10262 /* Export all color render targets. */
10263 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10264 if (ctx->outputs.mask[i])
10265 exported |= export_fs_mrt_color(ctx, i);
10266
10267 if (!exported)
10268 create_null_export(ctx);
10269 }
10270
10271 static void write_tcs_tess_factors(isel_context *ctx)
10272 {
10273 unsigned outer_comps;
10274 unsigned inner_comps;
10275
10276 switch (ctx->args->options->key.tcs.primitive_mode) {
10277 case GL_ISOLINES:
10278 outer_comps = 2;
10279 inner_comps = 0;
10280 break;
10281 case GL_TRIANGLES:
10282 outer_comps = 3;
10283 inner_comps = 1;
10284 break;
10285 case GL_QUADS:
10286 outer_comps = 4;
10287 inner_comps = 2;
10288 break;
10289 default:
10290 return;
10291 }
10292
10293 Builder bld(ctx->program, ctx->block);
10294
10295 bld.barrier(aco_opcode::p_memory_barrier_shared);
10296 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10297 bld.sopp(aco_opcode::s_barrier);
10298
10299 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10300 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10301
10302 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10303 if_context ic_invocation_id_is_zero;
10304 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10305 bld.reset(ctx->block);
10306
10307 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));
10308
10309 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10310 unsigned stride = inner_comps + outer_comps;
10311 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10312 Temp tf_inner_vec;
10313 Temp tf_outer_vec;
10314 Temp out[6];
10315 assert(stride <= (sizeof(out) / sizeof(Temp)));
10316
10317 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10318 // LINES reversal
10319 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10320 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10321 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10322 } else {
10323 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);
10324 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);
10325
10326 for (unsigned i = 0; i < outer_comps; ++i)
10327 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10328 for (unsigned i = 0; i < inner_comps; ++i)
10329 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10330 }
10331
10332 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10333 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10334 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10335 unsigned tf_const_offset = 0;
10336
10337 if (ctx->program->chip_class <= GFX8) {
10338 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);
10339 if_context ic_rel_patch_id_is_zero;
10340 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10341 bld.reset(ctx->block);
10342
10343 /* Store the dynamic HS control word. */
10344 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10345 bld.mubuf(aco_opcode::buffer_store_dword,
10346 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10347 /* immediate OFFSET */ 0, /* OFFEN */ false, /* swizzled */ false, /* idxen*/ false,
10348 /* addr64 */ false, /* disable_wqm */ false, /* glc */ true);
10349 tf_const_offset += 4;
10350
10351 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10352 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10353 bld.reset(ctx->block);
10354 }
10355
10356 assert(stride == 2 || stride == 4 || stride == 6);
10357 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10358 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10359
10360 /* Store to offchip for TES to read - only if TES reads them */
10361 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10362 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));
10363 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10364
10365 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10366 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);
10367
10368 if (likely(inner_comps)) {
10369 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10370 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);
10371 }
10372 }
10373
10374 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10375 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10376 }
10377
10378 static void emit_stream_output(isel_context *ctx,
10379 Temp const *so_buffers,
10380 Temp const *so_write_offset,
10381 const struct radv_stream_output *output)
10382 {
10383 unsigned num_comps = util_bitcount(output->component_mask);
10384 unsigned writemask = (1 << num_comps) - 1;
10385 unsigned loc = output->location;
10386 unsigned buf = output->buffer;
10387
10388 assert(num_comps && num_comps <= 4);
10389 if (!num_comps || num_comps > 4)
10390 return;
10391
10392 unsigned start = ffs(output->component_mask) - 1;
10393
10394 Temp out[4];
10395 bool all_undef = true;
10396 assert(ctx->stage & hw_vs);
10397 for (unsigned i = 0; i < num_comps; i++) {
10398 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10399 all_undef = all_undef && !out[i].id();
10400 }
10401 if (all_undef)
10402 return;
10403
10404 while (writemask) {
10405 int start, count;
10406 u_bit_scan_consecutive_range(&writemask, &start, &count);
10407 if (count == 3 && ctx->options->chip_class == GFX6) {
10408 /* GFX6 doesn't support storing vec3, split it. */
10409 writemask |= 1u << (start + 2);
10410 count = 2;
10411 }
10412
10413 unsigned offset = output->offset + start * 4;
10414
10415 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10416 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10417 for (int i = 0; i < count; ++i)
10418 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10419 vec->definitions[0] = Definition(write_data);
10420 ctx->block->instructions.emplace_back(std::move(vec));
10421
10422 aco_opcode opcode;
10423 switch (count) {
10424 case 1:
10425 opcode = aco_opcode::buffer_store_dword;
10426 break;
10427 case 2:
10428 opcode = aco_opcode::buffer_store_dwordx2;
10429 break;
10430 case 3:
10431 opcode = aco_opcode::buffer_store_dwordx3;
10432 break;
10433 case 4:
10434 opcode = aco_opcode::buffer_store_dwordx4;
10435 break;
10436 default:
10437 unreachable("Unsupported dword count.");
10438 }
10439
10440 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10441 store->operands[0] = Operand(so_buffers[buf]);
10442 store->operands[1] = Operand(so_write_offset[buf]);
10443 store->operands[2] = Operand((uint32_t) 0);
10444 store->operands[3] = Operand(write_data);
10445 if (offset > 4095) {
10446 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10447 Builder bld(ctx->program, ctx->block);
10448 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10449 } else {
10450 store->offset = offset;
10451 }
10452 store->offen = true;
10453 store->glc = true;
10454 store->dlc = false;
10455 store->slc = true;
10456 store->can_reorder = true;
10457 ctx->block->instructions.emplace_back(std::move(store));
10458 }
10459 }
10460
10461 static void emit_streamout(isel_context *ctx, unsigned stream)
10462 {
10463 Builder bld(ctx->program, ctx->block);
10464
10465 Temp so_buffers[4];
10466 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10467 for (unsigned i = 0; i < 4; i++) {
10468 unsigned stride = ctx->program->info->so.strides[i];
10469 if (!stride)
10470 continue;
10471
10472 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10473 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10474 }
10475
10476 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10477 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10478
10479 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10480
10481 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10482
10483 if_context ic;
10484 begin_divergent_if_then(ctx, &ic, can_emit);
10485
10486 bld.reset(ctx->block);
10487
10488 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10489
10490 Temp so_write_offset[4];
10491
10492 for (unsigned i = 0; i < 4; i++) {
10493 unsigned stride = ctx->program->info->so.strides[i];
10494 if (!stride)
10495 continue;
10496
10497 if (stride == 1) {
10498 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10499 get_arg(ctx, ctx->args->streamout_write_idx),
10500 get_arg(ctx, ctx->args->streamout_offset[i]));
10501 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10502
10503 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10504 } else {
10505 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10506 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10507 get_arg(ctx, ctx->args->streamout_offset[i]));
10508 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10509 }
10510 }
10511
10512 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10513 struct radv_stream_output *output =
10514 &ctx->program->info->so.outputs[i];
10515 if (stream != output->stream)
10516 continue;
10517
10518 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10519 }
10520
10521 begin_divergent_if_else(ctx, &ic);
10522 end_divergent_if(ctx, &ic);
10523 }
10524
10525 } /* end namespace */
10526
10527 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10528 {
10529 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10530 Builder bld(ctx->program, ctx->block);
10531 constexpr unsigned hs_idx = 1u;
10532 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10533 get_arg(ctx, ctx->args->merged_wave_info),
10534 Operand((8u << 16) | (hs_idx * 8u)));
10535 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10536
10537 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10538
10539 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10540 get_arg(ctx, ctx->args->rel_auto_id),
10541 get_arg(ctx, ctx->args->ac.instance_id),
10542 ls_has_nonzero_hs_threads);
10543 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10544 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10545 get_arg(ctx, ctx->args->rel_auto_id),
10546 ls_has_nonzero_hs_threads);
10547 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10548 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10549 get_arg(ctx, ctx->args->ac.vertex_id),
10550 ls_has_nonzero_hs_threads);
10551
10552 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10553 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10554 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10555 }
10556
10557 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10558 {
10559 /* Split all arguments except for the first (ring_offsets) and the last
10560 * (exec) so that the dead channels don't stay live throughout the program.
10561 */
10562 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10563 if (startpgm->definitions[i].regClass().size() > 1) {
10564 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10565 startpgm->definitions[i].regClass().size());
10566 }
10567 }
10568 }
10569
10570 void handle_bc_optimize(isel_context *ctx)
10571 {
10572 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10573 Builder bld(ctx->program, ctx->block);
10574 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10575 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10576 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10577 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10578 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10579 if (uses_center && uses_centroid) {
10580 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10581 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10582
10583 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10584 Temp new_coord[2];
10585 for (unsigned i = 0; i < 2; i++) {
10586 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10587 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10588 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10589 persp_centroid, persp_center, sel);
10590 }
10591 ctx->persp_centroid = bld.tmp(v2);
10592 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10593 Operand(new_coord[0]), Operand(new_coord[1]));
10594 emit_split_vector(ctx, ctx->persp_centroid, 2);
10595 }
10596
10597 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10598 Temp new_coord[2];
10599 for (unsigned i = 0; i < 2; i++) {
10600 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10601 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10602 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10603 linear_centroid, linear_center, sel);
10604 }
10605 ctx->linear_centroid = bld.tmp(v2);
10606 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10607 Operand(new_coord[0]), Operand(new_coord[1]));
10608 emit_split_vector(ctx, ctx->linear_centroid, 2);
10609 }
10610 }
10611 }
10612
10613 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10614 {
10615 Program *program = ctx->program;
10616
10617 unsigned float_controls = shader->info.float_controls_execution_mode;
10618
10619 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10620 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10621 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10622 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10623 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10624
10625 program->next_fp_mode.must_flush_denorms32 =
10626 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10627 program->next_fp_mode.must_flush_denorms16_64 =
10628 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10629 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10630
10631 program->next_fp_mode.care_about_round32 =
10632 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10633
10634 program->next_fp_mode.care_about_round16_64 =
10635 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10636 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10637
10638 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10639 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10640 if (program->next_fp_mode.must_flush_denorms16_64)
10641 program->next_fp_mode.denorm16_64 = 0;
10642 else
10643 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10644
10645 /* preserving fp32 denorms is expensive, so only do it if asked */
10646 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10647 program->next_fp_mode.denorm32 = fp_denorm_keep;
10648 else
10649 program->next_fp_mode.denorm32 = 0;
10650
10651 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10652 program->next_fp_mode.round32 = fp_round_tz;
10653 else
10654 program->next_fp_mode.round32 = fp_round_ne;
10655
10656 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10657 program->next_fp_mode.round16_64 = fp_round_tz;
10658 else
10659 program->next_fp_mode.round16_64 = fp_round_ne;
10660
10661 ctx->block->fp_mode = program->next_fp_mode;
10662 }
10663
10664 void cleanup_cfg(Program *program)
10665 {
10666 /* create linear_succs/logical_succs */
10667 for (Block& BB : program->blocks) {
10668 for (unsigned idx : BB.linear_preds)
10669 program->blocks[idx].linear_succs.emplace_back(BB.index);
10670 for (unsigned idx : BB.logical_preds)
10671 program->blocks[idx].logical_succs.emplace_back(BB.index);
10672 }
10673 }
10674
10675 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10676 {
10677 Builder bld(ctx->program, ctx->block);
10678
10679 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10680 Temp count = i == 0
10681 ? get_arg(ctx, ctx->args->merged_wave_info)
10682 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10683 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10684
10685 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10686 Temp cond;
10687
10688 if (ctx->program->wave_size == 64) {
10689 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10690 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10691 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10692 } else {
10693 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10694 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10695 }
10696
10697 return cond;
10698 }
10699
10700 bool ngg_early_prim_export(isel_context *ctx)
10701 {
10702 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10703 return true;
10704 }
10705
10706 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10707 {
10708 Builder bld(ctx->program, ctx->block);
10709
10710 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10711 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10712
10713 /* Get the id of the current wave within the threadgroup (workgroup) */
10714 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10715 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10716
10717 /* Execute the following code only on the first wave (wave id 0),
10718 * use the SCC def to tell if the wave id is zero or not.
10719 */
10720 Temp cond = wave_id_in_tg.def(1).getTemp();
10721 if_context ic;
10722 begin_uniform_if_then(ctx, &ic, cond);
10723 begin_uniform_if_else(ctx, &ic);
10724 bld.reset(ctx->block);
10725
10726 /* Number of vertices output by VS/TES */
10727 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10728 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10729 /* Number of primitives output by VS/TES */
10730 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10731 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10732
10733 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10734 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10735 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10736
10737 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10738 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10739
10740 end_uniform_if(ctx, &ic);
10741
10742 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10743 bld.reset(ctx->block);
10744 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10745 }
10746
10747 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10748 {
10749 Builder bld(ctx->program, ctx->block);
10750
10751 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10752 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10753 }
10754
10755 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10756 Temp tmp;
10757
10758 for (unsigned i = 0; i < num_vertices; ++i) {
10759 assert(vtxindex[i].id());
10760
10761 if (i)
10762 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10763 else
10764 tmp = vtxindex[i];
10765
10766 /* The initial edge flag is always false in tess eval shaders. */
10767 if (ctx->stage == ngg_vertex_gs) {
10768 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10769 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10770 }
10771 }
10772
10773 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10774
10775 return tmp;
10776 }
10777
10778 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10779 {
10780 Builder bld(ctx->program, ctx->block);
10781 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10782
10783 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10784 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10785 false /* compressed */, true/* done */, false /* valid mask */);
10786 }
10787
10788 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10789 {
10790 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10791 * These must always come before VS exports.
10792 *
10793 * It is recommended to do these as early as possible. They can be at the beginning when
10794 * there is no SW GS and the shader doesn't write edge flags.
10795 */
10796
10797 if_context ic;
10798 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10799 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10800
10801 Builder bld(ctx->program, ctx->block);
10802 constexpr unsigned max_vertices_per_primitive = 3;
10803 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10804
10805 if (ctx->stage == ngg_vertex_gs) {
10806 /* TODO: optimize for points & lines */
10807 } else if (ctx->stage == ngg_tess_eval_gs) {
10808 if (ctx->shader->info.tess.point_mode)
10809 num_vertices_per_primitive = 1;
10810 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10811 num_vertices_per_primitive = 2;
10812 } else {
10813 unreachable("Unsupported NGG shader stage");
10814 }
10815
10816 Temp vtxindex[max_vertices_per_primitive];
10817 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10818 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10819 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10820 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10821 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10822 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10823 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10824 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10825
10826 /* Export primitive data to the index buffer. */
10827 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10828
10829 /* Export primitive ID. */
10830 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10831 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10832 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10833 Temp provoking_vtx_index = vtxindex[0];
10834 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10835
10836 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10837 }
10838
10839 begin_divergent_if_else(ctx, &ic);
10840 end_divergent_if(ctx, &ic);
10841 }
10842
10843 void ngg_emit_nogs_output(isel_context *ctx)
10844 {
10845 /* Emits NGG GS output, for stages that don't have SW GS. */
10846
10847 if_context ic;
10848 Builder bld(ctx->program, ctx->block);
10849 bool late_prim_export = !ngg_early_prim_export(ctx);
10850
10851 /* NGG streamout is currently disabled by default. */
10852 assert(!ctx->args->shader_info->so.num_outputs);
10853
10854 if (late_prim_export) {
10855 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10856 create_export_phis(ctx);
10857 /* Do what we need to do in the GS threads. */
10858 ngg_emit_nogs_gsthreads(ctx);
10859
10860 /* What comes next should be executed on ES threads. */
10861 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10862 begin_divergent_if_then(ctx, &ic, is_es_thread);
10863 bld.reset(ctx->block);
10864 }
10865
10866 /* Export VS outputs */
10867 ctx->block->kind |= block_kind_export_end;
10868 create_vs_exports(ctx);
10869
10870 /* Export primitive ID */
10871 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10872 Temp prim_id;
10873
10874 if (ctx->stage == ngg_vertex_gs) {
10875 /* Wait for GS threads to store primitive ID in LDS. */
10876 bld.barrier(aco_opcode::p_memory_barrier_shared);
10877 bld.sopp(aco_opcode::s_barrier);
10878
10879 /* Calculate LDS address where the GS threads stored the primitive ID. */
10880 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10881 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10882 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10883 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10884 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10885 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10886
10887 /* Load primitive ID from LDS. */
10888 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10889 } else if (ctx->stage == ngg_tess_eval_gs) {
10890 /* TES: Just use the patch ID as the primitive ID. */
10891 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10892 } else {
10893 unreachable("unsupported NGG shader stage.");
10894 }
10895
10896 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10897 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10898
10899 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10900 }
10901
10902 if (late_prim_export) {
10903 begin_divergent_if_else(ctx, &ic);
10904 end_divergent_if(ctx, &ic);
10905 bld.reset(ctx->block);
10906 }
10907 }
10908
10909 void select_program(Program *program,
10910 unsigned shader_count,
10911 struct nir_shader *const *shaders,
10912 ac_shader_config* config,
10913 struct radv_shader_args *args)
10914 {
10915 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10916 if_context ic_merged_wave_info;
10917 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10918
10919 for (unsigned i = 0; i < shader_count; i++) {
10920 nir_shader *nir = shaders[i];
10921 init_context(&ctx, nir);
10922
10923 setup_fp_mode(&ctx, nir);
10924
10925 if (!i) {
10926 /* needs to be after init_context() for FS */
10927 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10928 append_logical_start(ctx.block);
10929
10930 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10931 fix_ls_vgpr_init_bug(&ctx, startpgm);
10932
10933 split_arguments(&ctx, startpgm);
10934 }
10935
10936 if (ngg_no_gs) {
10937 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10938
10939 if (ngg_early_prim_export(&ctx))
10940 ngg_emit_nogs_gsthreads(&ctx);
10941 }
10942
10943 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10944 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10945 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10946 ((nir->info.stage == MESA_SHADER_VERTEX &&
10947 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10948 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10949 ctx.stage == tess_eval_geometry_gs));
10950
10951 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10952 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10953 if (check_merged_wave_info) {
10954 Temp cond = merged_wave_info_to_mask(&ctx, i);
10955 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10956 }
10957
10958 if (i) {
10959 Builder bld(ctx.program, ctx.block);
10960
10961 bld.barrier(aco_opcode::p_memory_barrier_shared);
10962 bld.sopp(aco_opcode::s_barrier);
10963
10964 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10965 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));
10966 }
10967 } else if (ctx.stage == geometry_gs)
10968 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10969
10970 if (ctx.stage == fragment_fs)
10971 handle_bc_optimize(&ctx);
10972
10973 visit_cf_list(&ctx, &func->body);
10974
10975 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10976 emit_streamout(&ctx, 0);
10977
10978 if (ctx.stage & hw_vs) {
10979 create_vs_exports(&ctx);
10980 ctx.block->kind |= block_kind_export_end;
10981 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10982 ngg_emit_nogs_output(&ctx);
10983 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10984 Builder bld(ctx.program, ctx.block);
10985 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10986 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10987 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10988 write_tcs_tess_factors(&ctx);
10989 }
10990
10991 if (ctx.stage == fragment_fs) {
10992 create_fs_exports(&ctx);
10993 ctx.block->kind |= block_kind_export_end;
10994 }
10995
10996 if (endif_merged_wave_info) {
10997 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10998 end_divergent_if(&ctx, &ic_merged_wave_info);
10999 }
11000
11001 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
11002 ngg_emit_nogs_output(&ctx);
11003
11004 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
11005 /* Outputs of the previous stage are inputs to the next stage */
11006 ctx.inputs = ctx.outputs;
11007 ctx.outputs = shader_io_state();
11008 }
11009 }
11010
11011 program->config->float_mode = program->blocks[0].fp_mode.val;
11012
11013 append_logical_end(ctx.block);
11014 ctx.block->kind |= block_kind_uniform;
11015 Builder bld(ctx.program, ctx.block);
11016 if (ctx.program->wb_smem_l1_on_end)
11017 bld.smem(aco_opcode::s_dcache_wb, false);
11018 bld.sopp(aco_opcode::s_endpgm);
11019
11020 cleanup_cfg(program);
11021 }
11022
11023 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
11024 ac_shader_config* config,
11025 struct radv_shader_args *args)
11026 {
11027 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
11028
11029 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
11030 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
11031 program->next_fp_mode.must_flush_denorms32 = false;
11032 program->next_fp_mode.must_flush_denorms16_64 = false;
11033 program->next_fp_mode.care_about_round32 = false;
11034 program->next_fp_mode.care_about_round16_64 = false;
11035 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
11036 program->next_fp_mode.denorm32 = 0;
11037 program->next_fp_mode.round32 = fp_round_ne;
11038 program->next_fp_mode.round16_64 = fp_round_ne;
11039 ctx.block->fp_mode = program->next_fp_mode;
11040
11041 add_startpgm(&ctx);
11042 append_logical_start(ctx.block);
11043
11044 Builder bld(ctx.program, ctx.block);
11045
11046 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
11047
11048 Operand stream_id(0u);
11049 if (args->shader_info->so.num_outputs)
11050 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
11051 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
11052
11053 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
11054
11055 std::stack<Block> endif_blocks;
11056
11057 for (unsigned stream = 0; stream < 4; stream++) {
11058 if (stream_id.isConstant() && stream != stream_id.constantValue())
11059 continue;
11060
11061 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
11062 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
11063 continue;
11064
11065 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
11066
11067 unsigned BB_if_idx = ctx.block->index;
11068 Block BB_endif = Block();
11069 if (!stream_id.isConstant()) {
11070 /* begin IF */
11071 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
11072 append_logical_end(ctx.block);
11073 ctx.block->kind |= block_kind_uniform;
11074 bld.branch(aco_opcode::p_cbranch_z, cond);
11075
11076 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
11077
11078 ctx.block = ctx.program->create_and_insert_block();
11079 add_edge(BB_if_idx, ctx.block);
11080 bld.reset(ctx.block);
11081 append_logical_start(ctx.block);
11082 }
11083
11084 unsigned offset = 0;
11085 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
11086 if (args->shader_info->gs.output_streams[i] != stream)
11087 continue;
11088
11089 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
11090 unsigned length = util_last_bit(output_usage_mask);
11091 for (unsigned j = 0; j < length; ++j) {
11092 if (!(output_usage_mask & (1 << j)))
11093 continue;
11094
11095 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
11096 Temp voffset = vtx_offset;
11097 if (const_offset >= 4096u) {
11098 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
11099 const_offset %= 4096u;
11100 }
11101
11102 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
11103 mubuf->definitions[0] = bld.def(v1);
11104 mubuf->operands[0] = Operand(gsvs_ring);
11105 mubuf->operands[1] = Operand(voffset);
11106 mubuf->operands[2] = Operand(0u);
11107 mubuf->offen = true;
11108 mubuf->offset = const_offset;
11109 mubuf->glc = true;
11110 mubuf->slc = true;
11111 mubuf->dlc = args->options->chip_class >= GFX10;
11112 mubuf->barrier = barrier_none;
11113 mubuf->can_reorder = true;
11114
11115 ctx.outputs.mask[i] |= 1 << j;
11116 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
11117
11118 bld.insert(std::move(mubuf));
11119
11120 offset++;
11121 }
11122 }
11123
11124 if (args->shader_info->so.num_outputs) {
11125 emit_streamout(&ctx, stream);
11126 bld.reset(ctx.block);
11127 }
11128
11129 if (stream == 0) {
11130 create_vs_exports(&ctx);
11131 ctx.block->kind |= block_kind_export_end;
11132 }
11133
11134 if (!stream_id.isConstant()) {
11135 append_logical_end(ctx.block);
11136
11137 /* branch from then block to endif block */
11138 bld.branch(aco_opcode::p_branch);
11139 add_edge(ctx.block->index, &BB_endif);
11140 ctx.block->kind |= block_kind_uniform;
11141
11142 /* emit else block */
11143 ctx.block = ctx.program->create_and_insert_block();
11144 add_edge(BB_if_idx, ctx.block);
11145 bld.reset(ctx.block);
11146 append_logical_start(ctx.block);
11147
11148 endif_blocks.push(std::move(BB_endif));
11149 }
11150 }
11151
11152 while (!endif_blocks.empty()) {
11153 Block BB_endif = std::move(endif_blocks.top());
11154 endif_blocks.pop();
11155
11156 Block *BB_else = ctx.block;
11157
11158 append_logical_end(BB_else);
11159 /* branch from else block to endif block */
11160 bld.branch(aco_opcode::p_branch);
11161 add_edge(BB_else->index, &BB_endif);
11162 BB_else->kind |= block_kind_uniform;
11163
11164 /** emit endif merge block */
11165 ctx.block = program->insert_block(std::move(BB_endif));
11166 bld.reset(ctx.block);
11167 append_logical_start(ctx.block);
11168 }
11169
11170 program->config->float_mode = program->blocks[0].fp_mode.val;
11171
11172 append_logical_end(ctx.block);
11173 ctx.block->kind |= block_kind_uniform;
11174 bld.sopp(aco_opcode::s_endpgm);
11175
11176 cleanup_cfg(program);
11177 }
11178 }