aco: consider intrinsic access in visit_{load,store}_image
[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 = vec.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, num_components);
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 = skip; i < num_components; i++)
490 elems[i - skip] = emit_extract_vector(ctx, vec, i, 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 num_components = dst.bytes() / component_size;
495 aco_ptr<Pseudo_instruction> create_vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
496 for (unsigned i = 0; i < num_components; i++)
497 create_vec->operands[i] = Operand(elems[i]);
498 create_vec->definitions[0] = Definition(dst);
499 bld.insert(std::move(create_vec));
500
501 /* if dst is sgpr - split the src, but move the original to sgpr. */
502 } else if (skip) {
503 vec = bld.pseudo(aco_opcode::p_as_uniform, bld.def(RegClass(RegType::sgpr, vec.size())), vec);
504 byte_align_scalar(ctx, vec, offset, dst);
505 } else {
506 assert(dst.size() == vec.size());
507 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
508 }
509
510 ctx->allocated_vec.emplace(dst.id(), elems);
511 }
512
513 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
514 {
515 Builder bld(ctx->program, ctx->block);
516 if (!dst.id())
517 dst = bld.tmp(bld.lm);
518
519 assert(val.regClass() == s1);
520 assert(dst.regClass() == bld.lm);
521
522 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
523 }
524
525 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
526 {
527 Builder bld(ctx->program, ctx->block);
528 if (!dst.id())
529 dst = bld.tmp(s1);
530
531 assert(val.regClass() == bld.lm);
532 assert(dst.regClass() == s1);
533
534 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
535 Temp tmp = bld.tmp(s1);
536 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
537 return emit_wqm(ctx, tmp, dst);
538 }
539
540 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
541 {
542 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
543 return get_ssa_temp(ctx, src.src.ssa);
544
545 if (src.src.ssa->num_components == size) {
546 bool identity_swizzle = true;
547 for (unsigned i = 0; identity_swizzle && i < size; i++) {
548 if (src.swizzle[i] != i)
549 identity_swizzle = false;
550 }
551 if (identity_swizzle)
552 return get_ssa_temp(ctx, src.src.ssa);
553 }
554
555 Temp vec = get_ssa_temp(ctx, src.src.ssa);
556 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
557 assert(elem_size > 0);
558 assert(vec.bytes() % elem_size == 0);
559
560 if (elem_size < 4 && vec.type() == RegType::sgpr) {
561 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
562 assert(size == 1);
563 unsigned swizzle = src.swizzle[0];
564 if (vec.size() > 1) {
565 assert(src.src.ssa->bit_size == 16);
566 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
567 swizzle = swizzle & 1;
568 }
569 if (swizzle == 0)
570 return vec;
571
572 Temp dst{ctx->program->allocateId(), s1};
573 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
574 bfe->operands[0] = Operand(vec);
575 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
576 bfe->definitions[0] = Definition(dst);
577 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
578 ctx->block->instructions.emplace_back(std::move(bfe));
579 return dst;
580 }
581
582 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
583 if (size == 1) {
584 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
585 } else {
586 assert(size <= 4);
587 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
588 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
589 for (unsigned i = 0; i < size; ++i) {
590 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
591 vec_instr->operands[i] = Operand{elems[i]};
592 }
593 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
594 vec_instr->definitions[0] = Definition(dst);
595 ctx->block->instructions.emplace_back(std::move(vec_instr));
596 ctx->allocated_vec.emplace(dst.id(), elems);
597 return dst;
598 }
599 }
600
601 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
602 {
603 if (ptr.size() == 2)
604 return ptr;
605 Builder bld(ctx->program, ctx->block);
606 if (ptr.type() == RegType::vgpr)
607 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
608 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
609 ptr, Operand((unsigned)ctx->options->address32_hi));
610 }
611
612 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
613 {
614 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
615 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
616 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
617 sop2->definitions[0] = Definition(dst);
618 if (instr->no_unsigned_wrap)
619 sop2->definitions[0].setNUW(true);
620 if (writes_scc)
621 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
622 ctx->block->instructions.emplace_back(std::move(sop2));
623 }
624
625 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
626 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
627 {
628 Builder bld(ctx->program, ctx->block);
629 bld.is_precise = instr->exact;
630
631 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
632 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
633 if (src1.type() == RegType::sgpr) {
634 if (commutative && src0.type() == RegType::vgpr) {
635 Temp t = src0;
636 src0 = src1;
637 src1 = t;
638 } else {
639 src1 = as_vgpr(ctx, src1);
640 }
641 }
642
643 if (flush_denorms && ctx->program->chip_class < GFX9) {
644 assert(dst.size() == 1);
645 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
646 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
647 } else {
648 bld.vop2(op, Definition(dst), src0, src1);
649 }
650 }
651
652 void emit_vop2_instruction_logic64(isel_context *ctx, nir_alu_instr *instr,
653 aco_opcode op, Temp dst)
654 {
655 Builder bld(ctx->program, ctx->block);
656 bld.is_precise = instr->exact;
657
658 Temp src0 = get_alu_src(ctx, instr->src[0]);
659 Temp src1 = get_alu_src(ctx, instr->src[1]);
660
661 if (src1.type() == RegType::sgpr) {
662 assert(src0.type() == RegType::vgpr);
663 std::swap(src0, src1);
664 }
665
666 Temp src00 = bld.tmp(src0.type(), 1);
667 Temp src01 = bld.tmp(src0.type(), 1);
668 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
669 Temp src10 = bld.tmp(v1);
670 Temp src11 = bld.tmp(v1);
671 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
672 Temp lo = bld.vop2(op, bld.def(v1), src00, src10);
673 Temp hi = bld.vop2(op, bld.def(v1), src01, src11);
674 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
675 }
676
677 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
678 bool flush_denorms = false)
679 {
680 Temp src0 = get_alu_src(ctx, instr->src[0]);
681 Temp src1 = get_alu_src(ctx, instr->src[1]);
682 Temp src2 = get_alu_src(ctx, instr->src[2]);
683
684 /* ensure that the instruction has at most 1 sgpr operand
685 * The optimizer will inline constants for us */
686 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
687 src0 = as_vgpr(ctx, src0);
688 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
689 src1 = as_vgpr(ctx, src1);
690 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
691 src2 = as_vgpr(ctx, src2);
692
693 Builder bld(ctx->program, ctx->block);
694 bld.is_precise = instr->exact;
695 if (flush_denorms && ctx->program->chip_class < GFX9) {
696 assert(dst.size() == 1);
697 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
698 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
699 } else {
700 bld.vop3(op, Definition(dst), src0, src1, src2);
701 }
702 }
703
704 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
705 {
706 Builder bld(ctx->program, ctx->block);
707 bld.is_precise = instr->exact;
708 if (dst.type() == RegType::sgpr)
709 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
710 bld.vop1(op, bld.def(RegType::vgpr, dst.size()), get_alu_src(ctx, instr->src[0])));
711 else
712 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
713 }
714
715 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
716 {
717 Temp src0 = get_alu_src(ctx, instr->src[0]);
718 Temp src1 = get_alu_src(ctx, instr->src[1]);
719 assert(src0.size() == src1.size());
720
721 aco_ptr<Instruction> vopc;
722 if (src1.type() == RegType::sgpr) {
723 if (src0.type() == RegType::vgpr) {
724 /* to swap the operands, we might also have to change the opcode */
725 switch (op) {
726 case aco_opcode::v_cmp_lt_f16:
727 op = aco_opcode::v_cmp_gt_f16;
728 break;
729 case aco_opcode::v_cmp_ge_f16:
730 op = aco_opcode::v_cmp_le_f16;
731 break;
732 case aco_opcode::v_cmp_lt_i16:
733 op = aco_opcode::v_cmp_gt_i16;
734 break;
735 case aco_opcode::v_cmp_ge_i16:
736 op = aco_opcode::v_cmp_le_i16;
737 break;
738 case aco_opcode::v_cmp_lt_u16:
739 op = aco_opcode::v_cmp_gt_u16;
740 break;
741 case aco_opcode::v_cmp_ge_u16:
742 op = aco_opcode::v_cmp_le_u16;
743 break;
744 case aco_opcode::v_cmp_lt_f32:
745 op = aco_opcode::v_cmp_gt_f32;
746 break;
747 case aco_opcode::v_cmp_ge_f32:
748 op = aco_opcode::v_cmp_le_f32;
749 break;
750 case aco_opcode::v_cmp_lt_i32:
751 op = aco_opcode::v_cmp_gt_i32;
752 break;
753 case aco_opcode::v_cmp_ge_i32:
754 op = aco_opcode::v_cmp_le_i32;
755 break;
756 case aco_opcode::v_cmp_lt_u32:
757 op = aco_opcode::v_cmp_gt_u32;
758 break;
759 case aco_opcode::v_cmp_ge_u32:
760 op = aco_opcode::v_cmp_le_u32;
761 break;
762 case aco_opcode::v_cmp_lt_f64:
763 op = aco_opcode::v_cmp_gt_f64;
764 break;
765 case aco_opcode::v_cmp_ge_f64:
766 op = aco_opcode::v_cmp_le_f64;
767 break;
768 case aco_opcode::v_cmp_lt_i64:
769 op = aco_opcode::v_cmp_gt_i64;
770 break;
771 case aco_opcode::v_cmp_ge_i64:
772 op = aco_opcode::v_cmp_le_i64;
773 break;
774 case aco_opcode::v_cmp_lt_u64:
775 op = aco_opcode::v_cmp_gt_u64;
776 break;
777 case aco_opcode::v_cmp_ge_u64:
778 op = aco_opcode::v_cmp_le_u64;
779 break;
780 default: /* eq and ne are commutative */
781 break;
782 }
783 Temp t = src0;
784 src0 = src1;
785 src1 = t;
786 } else {
787 src1 = as_vgpr(ctx, src1);
788 }
789 }
790
791 Builder bld(ctx->program, ctx->block);
792 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
793 }
794
795 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
796 {
797 Temp src0 = get_alu_src(ctx, instr->src[0]);
798 Temp src1 = get_alu_src(ctx, instr->src[1]);
799 Builder bld(ctx->program, ctx->block);
800
801 assert(dst.regClass() == bld.lm);
802 assert(src0.type() == RegType::sgpr);
803 assert(src1.type() == RegType::sgpr);
804 assert(src0.regClass() == src1.regClass());
805
806 /* Emit the SALU comparison instruction */
807 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
808 /* Turn the result into a per-lane bool */
809 bool_to_vector_condition(ctx, cmp, dst);
810 }
811
812 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
813 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)
814 {
815 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;
816 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;
817 bool use_valu = s_op == aco_opcode::num_opcodes ||
818 nir_dest_is_divergent(instr->dest.dest) ||
819 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
820 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
821 aco_opcode op = use_valu ? v_op : s_op;
822 assert(op != aco_opcode::num_opcodes);
823 assert(dst.regClass() == ctx->program->lane_mask);
824
825 if (use_valu)
826 emit_vopc_instruction(ctx, instr, op, dst);
827 else
828 emit_sopc_instruction(ctx, instr, op, dst);
829 }
830
831 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
832 {
833 Builder bld(ctx->program, ctx->block);
834 Temp src0 = get_alu_src(ctx, instr->src[0]);
835 Temp src1 = get_alu_src(ctx, instr->src[1]);
836
837 assert(dst.regClass() == bld.lm);
838 assert(src0.regClass() == bld.lm);
839 assert(src1.regClass() == bld.lm);
840
841 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
842 }
843
844 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
845 {
846 Builder bld(ctx->program, ctx->block);
847 Temp cond = get_alu_src(ctx, instr->src[0]);
848 Temp then = get_alu_src(ctx, instr->src[1]);
849 Temp els = get_alu_src(ctx, instr->src[2]);
850
851 assert(cond.regClass() == bld.lm);
852
853 if (dst.type() == RegType::vgpr) {
854 aco_ptr<Instruction> bcsel;
855 if (dst.size() == 1) {
856 then = as_vgpr(ctx, then);
857 els = as_vgpr(ctx, els);
858
859 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
860 } else if (dst.size() == 2) {
861 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
862 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
863 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
864 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
865
866 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
867 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
868
869 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
870 } else {
871 fprintf(stderr, "Unimplemented NIR instr bit size: ");
872 nir_print_instr(&instr->instr, stderr);
873 fprintf(stderr, "\n");
874 }
875 return;
876 }
877
878 if (instr->dest.dest.ssa.bit_size == 1) {
879 assert(dst.regClass() == bld.lm);
880 assert(then.regClass() == bld.lm);
881 assert(els.regClass() == bld.lm);
882 }
883
884 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
885 if (dst.regClass() == s1 || dst.regClass() == s2) {
886 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
887 assert(dst.size() == then.size());
888 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
889 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
890 } else {
891 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
892 nir_print_instr(&instr->instr, stderr);
893 fprintf(stderr, "\n");
894 }
895 return;
896 }
897
898 /* divergent boolean bcsel
899 * this implements bcsel on bools: dst = s0 ? s1 : s2
900 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
901 assert(instr->dest.dest.ssa.bit_size == 1);
902
903 if (cond.id() != then.id())
904 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
905
906 if (cond.id() == els.id())
907 bld.sop1(Builder::s_mov, Definition(dst), then);
908 else
909 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
910 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
911 }
912
913 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
914 aco_opcode op, uint32_t undo)
915 {
916 /* multiply by 16777216 to handle denormals */
917 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
918 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
919 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
920 scaled = bld.vop1(op, bld.def(v1), scaled);
921 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
922
923 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
924
925 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
926 }
927
928 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
929 {
930 if (ctx->block->fp_mode.denorm32 == 0) {
931 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
932 return;
933 }
934
935 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
936 }
937
938 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
939 {
940 if (ctx->block->fp_mode.denorm32 == 0) {
941 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
942 return;
943 }
944
945 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
946 }
947
948 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
949 {
950 if (ctx->block->fp_mode.denorm32 == 0) {
951 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
952 return;
953 }
954
955 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
956 }
957
958 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
959 {
960 if (ctx->block->fp_mode.denorm32 == 0) {
961 bld.vop1(aco_opcode::v_log_f32, dst, val);
962 return;
963 }
964
965 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
966 }
967
968 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
969 {
970 if (ctx->options->chip_class >= GFX7)
971 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
972
973 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
974 /* TODO: create more efficient code! */
975 if (val.type() == RegType::sgpr)
976 val = as_vgpr(ctx, val);
977
978 /* Split the input value. */
979 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
980 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
981
982 /* Extract the exponent and compute the unbiased value. */
983 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
984 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
985
986 /* Extract the fractional part. */
987 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
988 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
989
990 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
991 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
992
993 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
994 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
995 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
996 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
997 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
998
999 /* Get the sign bit. */
1000 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
1001
1002 /* Decide the operation to apply depending on the unbiased exponent. */
1003 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
1004 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
1005 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
1006 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
1007 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
1008 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
1009
1010 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
1011 }
1012
1013 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1014 {
1015 if (ctx->options->chip_class >= GFX7)
1016 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
1017
1018 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1019 * lowered at NIR level for precision reasons). */
1020 Temp src0 = as_vgpr(ctx, val);
1021
1022 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
1023 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
1024
1025 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
1026 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
1027 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
1028
1029 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
1030 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
1031 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
1032 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
1033
1034 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
1035 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
1036
1037 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
1038
1039 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
1040 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
1041
1042 return add->definitions[0].getTemp();
1043 }
1044
1045 Temp convert_int(isel_context *ctx, Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
1046 if (!dst.id()) {
1047 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
1048 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
1049 else
1050 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
1051 }
1052
1053 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
1054 return bld.copy(Definition(dst), src);
1055 else if (dst.bytes() < src.bytes())
1056 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
1057
1058 Temp tmp = dst;
1059 if (dst_bits == 64)
1060 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
1061
1062 if (tmp == src) {
1063 } else if (src.regClass() == s1) {
1064 if (is_signed)
1065 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
1066 else
1067 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
1068 } else if (ctx->options->chip_class >= GFX8) {
1069 assert(src_bits != 8 || src.regClass() == v1b);
1070 assert(src_bits != 16 || src.regClass() == v2b);
1071 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
1072 sdwa->operands[0] = Operand(src);
1073 sdwa->definitions[0] = Definition(tmp);
1074 if (is_signed)
1075 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
1076 else
1077 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
1078 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
1079 bld.insert(std::move(sdwa));
1080 } else {
1081 assert(ctx->options->chip_class == GFX6 || ctx->options->chip_class == GFX7);
1082 aco_opcode opcode = is_signed ? aco_opcode::v_bfe_i32 : aco_opcode::v_bfe_u32;
1083 bld.vop3(opcode, Definition(tmp), src, Operand(0u), Operand(src_bits == 8 ? 8u : 16u));
1084 }
1085
1086 if (dst_bits == 64) {
1087 if (is_signed && dst.regClass() == s2) {
1088 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
1089 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1090 } else if (is_signed && dst.regClass() == v2) {
1091 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
1092 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1093 } else {
1094 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
1095 }
1096 }
1097
1098 return dst;
1099 }
1100
1101 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1102 {
1103 if (!instr->dest.dest.is_ssa) {
1104 fprintf(stderr, "nir alu dst not in ssa: ");
1105 nir_print_instr(&instr->instr, stderr);
1106 fprintf(stderr, "\n");
1107 abort();
1108 }
1109 Builder bld(ctx->program, ctx->block);
1110 bld.is_precise = instr->exact;
1111 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1112 switch(instr->op) {
1113 case nir_op_vec2:
1114 case nir_op_vec3:
1115 case nir_op_vec4: {
1116 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1117 unsigned num = instr->dest.dest.ssa.num_components;
1118 for (unsigned i = 0; i < num; ++i)
1119 elems[i] = get_alu_src(ctx, instr->src[i]);
1120
1121 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1122 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1123 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1124 for (unsigned i = 0; i < num; ++i) {
1125 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1126 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1127 else
1128 vec->operands[i] = Operand{elems[i]};
1129 }
1130 vec->definitions[0] = Definition(dst);
1131 ctx->block->instructions.emplace_back(std::move(vec));
1132 ctx->allocated_vec.emplace(dst.id(), elems);
1133 } else {
1134 // TODO: that is a bit suboptimal..
1135 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1136 for (unsigned i = 0; i < num - 1; ++i)
1137 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1138 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1139 for (unsigned i = 0; i < num; ++i) {
1140 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1141 if (bit % 32 == 0) {
1142 elems[bit / 32] = elems[i];
1143 } else {
1144 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1145 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1146 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1147 }
1148 }
1149 if (dst.size() == 1)
1150 bld.copy(Definition(dst), elems[0]);
1151 else
1152 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1153 }
1154 break;
1155 }
1156 case nir_op_mov: {
1157 Temp src = get_alu_src(ctx, instr->src[0]);
1158 aco_ptr<Instruction> mov;
1159 if (dst.type() == RegType::sgpr) {
1160 if (src.type() == RegType::vgpr)
1161 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1162 else if (src.regClass() == s1)
1163 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1164 else if (src.regClass() == s2)
1165 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1166 else
1167 unreachable("wrong src register class for nir_op_imov");
1168 } else {
1169 if (dst.regClass() == v1)
1170 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1171 else if (dst.regClass() == v1b ||
1172 dst.regClass() == v2b ||
1173 dst.regClass() == v2)
1174 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1175 else
1176 unreachable("wrong src register class for nir_op_imov");
1177 }
1178 break;
1179 }
1180 case nir_op_inot: {
1181 Temp src = get_alu_src(ctx, instr->src[0]);
1182 if (instr->dest.dest.ssa.bit_size == 1) {
1183 assert(src.regClass() == bld.lm);
1184 assert(dst.regClass() == bld.lm);
1185 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1186 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1187 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1188 } else if (dst.regClass() == v1) {
1189 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1190 } else if (dst.regClass() == v2) {
1191 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
1192 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
1193 lo = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), lo);
1194 hi = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), hi);
1195 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
1196 } else if (dst.type() == RegType::sgpr) {
1197 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1198 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1199 } else {
1200 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1201 nir_print_instr(&instr->instr, stderr);
1202 fprintf(stderr, "\n");
1203 }
1204 break;
1205 }
1206 case nir_op_ineg: {
1207 Temp src = get_alu_src(ctx, instr->src[0]);
1208 if (dst.regClass() == v1) {
1209 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1210 } else if (dst.regClass() == s1) {
1211 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1212 } else if (dst.size() == 2) {
1213 Temp src0 = bld.tmp(dst.type(), 1);
1214 Temp src1 = bld.tmp(dst.type(), 1);
1215 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1216
1217 if (dst.regClass() == s2) {
1218 Temp carry = bld.tmp(s1);
1219 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1220 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1221 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1222 } else {
1223 Temp lower = bld.tmp(v1);
1224 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1225 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1226 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1227 }
1228 } else {
1229 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1230 nir_print_instr(&instr->instr, stderr);
1231 fprintf(stderr, "\n");
1232 }
1233 break;
1234 }
1235 case nir_op_iabs: {
1236 if (dst.regClass() == s1) {
1237 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1238 } else if (dst.regClass() == v1) {
1239 Temp src = get_alu_src(ctx, instr->src[0]);
1240 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1241 } else {
1242 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1243 nir_print_instr(&instr->instr, stderr);
1244 fprintf(stderr, "\n");
1245 }
1246 break;
1247 }
1248 case nir_op_isign: {
1249 Temp src = get_alu_src(ctx, instr->src[0]);
1250 if (dst.regClass() == s1) {
1251 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1252 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1253 } else if (dst.regClass() == s2) {
1254 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1255 Temp neqz;
1256 if (ctx->program->chip_class >= GFX8)
1257 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1258 else
1259 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1260 /* SCC gets zero-extended to 64 bit */
1261 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1262 } else if (dst.regClass() == v1) {
1263 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1264 } else if (dst.regClass() == v2) {
1265 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1266 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1267 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1268 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1269 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1270 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1271 } else {
1272 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1273 nir_print_instr(&instr->instr, stderr);
1274 fprintf(stderr, "\n");
1275 }
1276 break;
1277 }
1278 case nir_op_imax: {
1279 if (dst.regClass() == v1) {
1280 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1281 } else if (dst.regClass() == s1) {
1282 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1283 } else {
1284 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1285 nir_print_instr(&instr->instr, stderr);
1286 fprintf(stderr, "\n");
1287 }
1288 break;
1289 }
1290 case nir_op_umax: {
1291 if (dst.regClass() == v1) {
1292 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1293 } else if (dst.regClass() == s1) {
1294 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1295 } else {
1296 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1297 nir_print_instr(&instr->instr, stderr);
1298 fprintf(stderr, "\n");
1299 }
1300 break;
1301 }
1302 case nir_op_imin: {
1303 if (dst.regClass() == v1) {
1304 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1305 } else if (dst.regClass() == s1) {
1306 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1307 } else {
1308 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1309 nir_print_instr(&instr->instr, stderr);
1310 fprintf(stderr, "\n");
1311 }
1312 break;
1313 }
1314 case nir_op_umin: {
1315 if (dst.regClass() == v1) {
1316 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1317 } else if (dst.regClass() == s1) {
1318 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1319 } else {
1320 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1321 nir_print_instr(&instr->instr, stderr);
1322 fprintf(stderr, "\n");
1323 }
1324 break;
1325 }
1326 case nir_op_ior: {
1327 if (instr->dest.dest.ssa.bit_size == 1) {
1328 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1329 } else if (dst.regClass() == v1) {
1330 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1331 } else if (dst.regClass() == v2) {
1332 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_or_b32, dst);
1333 } else if (dst.regClass() == s1) {
1334 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1335 } else if (dst.regClass() == s2) {
1336 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1337 } else {
1338 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1339 nir_print_instr(&instr->instr, stderr);
1340 fprintf(stderr, "\n");
1341 }
1342 break;
1343 }
1344 case nir_op_iand: {
1345 if (instr->dest.dest.ssa.bit_size == 1) {
1346 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1347 } else if (dst.regClass() == v1) {
1348 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1349 } else if (dst.regClass() == v2) {
1350 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_and_b32, dst);
1351 } else if (dst.regClass() == s1) {
1352 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1353 } else if (dst.regClass() == s2) {
1354 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1355 } else {
1356 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1357 nir_print_instr(&instr->instr, stderr);
1358 fprintf(stderr, "\n");
1359 }
1360 break;
1361 }
1362 case nir_op_ixor: {
1363 if (instr->dest.dest.ssa.bit_size == 1) {
1364 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1365 } else if (dst.regClass() == v1) {
1366 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1367 } else if (dst.regClass() == v2) {
1368 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_xor_b32, dst);
1369 } else if (dst.regClass() == s1) {
1370 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1371 } else if (dst.regClass() == s2) {
1372 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1373 } else {
1374 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1375 nir_print_instr(&instr->instr, stderr);
1376 fprintf(stderr, "\n");
1377 }
1378 break;
1379 }
1380 case nir_op_ushr: {
1381 if (dst.regClass() == v1) {
1382 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1383 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1384 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1385 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1386 } else if (dst.regClass() == v2) {
1387 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1388 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1389 } else if (dst.regClass() == s2) {
1390 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1391 } else if (dst.regClass() == s1) {
1392 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1393 } else {
1394 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1395 nir_print_instr(&instr->instr, stderr);
1396 fprintf(stderr, "\n");
1397 }
1398 break;
1399 }
1400 case nir_op_ishl: {
1401 if (dst.regClass() == v1) {
1402 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1403 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1404 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1405 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1406 } else if (dst.regClass() == v2) {
1407 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1408 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1409 } else if (dst.regClass() == s1) {
1410 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1411 } else if (dst.regClass() == s2) {
1412 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1413 } else {
1414 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1415 nir_print_instr(&instr->instr, stderr);
1416 fprintf(stderr, "\n");
1417 }
1418 break;
1419 }
1420 case nir_op_ishr: {
1421 if (dst.regClass() == v1) {
1422 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1423 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1424 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1425 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1426 } else if (dst.regClass() == v2) {
1427 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1428 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1429 } else if (dst.regClass() == s1) {
1430 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1431 } else if (dst.regClass() == s2) {
1432 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1433 } else {
1434 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1435 nir_print_instr(&instr->instr, stderr);
1436 fprintf(stderr, "\n");
1437 }
1438 break;
1439 }
1440 case nir_op_find_lsb: {
1441 Temp src = get_alu_src(ctx, instr->src[0]);
1442 if (src.regClass() == s1) {
1443 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1444 } else if (src.regClass() == v1) {
1445 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1446 } else if (src.regClass() == s2) {
1447 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1448 } else {
1449 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1450 nir_print_instr(&instr->instr, stderr);
1451 fprintf(stderr, "\n");
1452 }
1453 break;
1454 }
1455 case nir_op_ufind_msb:
1456 case nir_op_ifind_msb: {
1457 Temp src = get_alu_src(ctx, instr->src[0]);
1458 if (src.regClass() == s1 || src.regClass() == s2) {
1459 aco_opcode op = src.regClass() == s2 ?
1460 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1461 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1462 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1463
1464 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1465 Operand(src.size() * 32u - 1u), msb_rev);
1466 Temp msb = sub.def(0).getTemp();
1467 Temp carry = sub.def(1).getTemp();
1468
1469 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1470 } else if (src.regClass() == v1) {
1471 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1472 Temp msb_rev = bld.tmp(v1);
1473 emit_vop1_instruction(ctx, instr, op, msb_rev);
1474 Temp msb = bld.tmp(v1);
1475 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1476 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1477 } else {
1478 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1479 nir_print_instr(&instr->instr, stderr);
1480 fprintf(stderr, "\n");
1481 }
1482 break;
1483 }
1484 case nir_op_bitfield_reverse: {
1485 if (dst.regClass() == s1) {
1486 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1487 } else if (dst.regClass() == v1) {
1488 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1489 } else {
1490 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1491 nir_print_instr(&instr->instr, stderr);
1492 fprintf(stderr, "\n");
1493 }
1494 break;
1495 }
1496 case nir_op_iadd: {
1497 if (dst.regClass() == s1) {
1498 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1499 break;
1500 }
1501
1502 Temp src0 = get_alu_src(ctx, instr->src[0]);
1503 Temp src1 = get_alu_src(ctx, instr->src[1]);
1504 if (dst.regClass() == v1) {
1505 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1506 break;
1507 }
1508
1509 assert(src0.size() == 2 && src1.size() == 2);
1510 Temp src00 = bld.tmp(src0.type(), 1);
1511 Temp src01 = bld.tmp(dst.type(), 1);
1512 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1513 Temp src10 = bld.tmp(src1.type(), 1);
1514 Temp src11 = bld.tmp(dst.type(), 1);
1515 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1516
1517 if (dst.regClass() == s2) {
1518 Temp carry = bld.tmp(s1);
1519 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1520 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1521 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1522 } else if (dst.regClass() == v2) {
1523 Temp dst0 = bld.tmp(v1);
1524 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1525 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1526 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1527 } else {
1528 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1529 nir_print_instr(&instr->instr, stderr);
1530 fprintf(stderr, "\n");
1531 }
1532 break;
1533 }
1534 case nir_op_uadd_sat: {
1535 Temp src0 = get_alu_src(ctx, instr->src[0]);
1536 Temp src1 = get_alu_src(ctx, instr->src[1]);
1537 if (dst.regClass() == s1) {
1538 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1539 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1540 src0, src1);
1541 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1542 } else if (dst.regClass() == v1) {
1543 if (ctx->options->chip_class >= GFX9) {
1544 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1545 add->operands[0] = Operand(src0);
1546 add->operands[1] = Operand(src1);
1547 add->definitions[0] = Definition(dst);
1548 add->clamp = 1;
1549 ctx->block->instructions.emplace_back(std::move(add));
1550 } else {
1551 if (src1.regClass() != v1)
1552 std::swap(src0, src1);
1553 assert(src1.regClass() == v1);
1554 Temp tmp = bld.tmp(v1);
1555 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1556 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1557 }
1558 } else {
1559 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1560 nir_print_instr(&instr->instr, stderr);
1561 fprintf(stderr, "\n");
1562 }
1563 break;
1564 }
1565 case nir_op_uadd_carry: {
1566 Temp src0 = get_alu_src(ctx, instr->src[0]);
1567 Temp src1 = get_alu_src(ctx, instr->src[1]);
1568 if (dst.regClass() == s1) {
1569 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1570 break;
1571 }
1572 if (dst.regClass() == v1) {
1573 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1574 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1575 break;
1576 }
1577
1578 Temp src00 = bld.tmp(src0.type(), 1);
1579 Temp src01 = bld.tmp(dst.type(), 1);
1580 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1581 Temp src10 = bld.tmp(src1.type(), 1);
1582 Temp src11 = bld.tmp(dst.type(), 1);
1583 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1584 if (dst.regClass() == s2) {
1585 Temp carry = bld.tmp(s1);
1586 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1587 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1588 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1589 } else if (dst.regClass() == v2) {
1590 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1591 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1592 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1593 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1594 } else {
1595 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1596 nir_print_instr(&instr->instr, stderr);
1597 fprintf(stderr, "\n");
1598 }
1599 break;
1600 }
1601 case nir_op_isub: {
1602 if (dst.regClass() == s1) {
1603 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1604 break;
1605 }
1606
1607 Temp src0 = get_alu_src(ctx, instr->src[0]);
1608 Temp src1 = get_alu_src(ctx, instr->src[1]);
1609 if (dst.regClass() == v1) {
1610 bld.vsub32(Definition(dst), src0, src1);
1611 break;
1612 }
1613
1614 Temp src00 = bld.tmp(src0.type(), 1);
1615 Temp src01 = bld.tmp(dst.type(), 1);
1616 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1617 Temp src10 = bld.tmp(src1.type(), 1);
1618 Temp src11 = bld.tmp(dst.type(), 1);
1619 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1620 if (dst.regClass() == s2) {
1621 Temp carry = bld.tmp(s1);
1622 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1623 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1624 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1625 } else if (dst.regClass() == v2) {
1626 Temp lower = bld.tmp(v1);
1627 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1628 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1629 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1630 } else {
1631 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1632 nir_print_instr(&instr->instr, stderr);
1633 fprintf(stderr, "\n");
1634 }
1635 break;
1636 }
1637 case nir_op_usub_borrow: {
1638 Temp src0 = get_alu_src(ctx, instr->src[0]);
1639 Temp src1 = get_alu_src(ctx, instr->src[1]);
1640 if (dst.regClass() == s1) {
1641 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1642 break;
1643 } else if (dst.regClass() == v1) {
1644 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1645 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1646 break;
1647 }
1648
1649 Temp src00 = bld.tmp(src0.type(), 1);
1650 Temp src01 = bld.tmp(dst.type(), 1);
1651 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1652 Temp src10 = bld.tmp(src1.type(), 1);
1653 Temp src11 = bld.tmp(dst.type(), 1);
1654 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1655 if (dst.regClass() == s2) {
1656 Temp borrow = bld.tmp(s1);
1657 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1658 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1659 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1660 } else if (dst.regClass() == v2) {
1661 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1662 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1663 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1664 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1665 } else {
1666 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1667 nir_print_instr(&instr->instr, stderr);
1668 fprintf(stderr, "\n");
1669 }
1670 break;
1671 }
1672 case nir_op_imul: {
1673 if (dst.regClass() == v1) {
1674 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1675 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1676 } else if (dst.regClass() == s1) {
1677 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1678 } else {
1679 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1680 nir_print_instr(&instr->instr, stderr);
1681 fprintf(stderr, "\n");
1682 }
1683 break;
1684 }
1685 case nir_op_umul_high: {
1686 if (dst.regClass() == v1) {
1687 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1688 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1689 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1690 } else if (dst.regClass() == s1) {
1691 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1692 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1693 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1694 } else {
1695 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1696 nir_print_instr(&instr->instr, stderr);
1697 fprintf(stderr, "\n");
1698 }
1699 break;
1700 }
1701 case nir_op_imul_high: {
1702 if (dst.regClass() == v1) {
1703 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1704 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1705 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1706 } else if (dst.regClass() == s1) {
1707 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1708 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1709 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1710 } else {
1711 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1712 nir_print_instr(&instr->instr, stderr);
1713 fprintf(stderr, "\n");
1714 }
1715 break;
1716 }
1717 case nir_op_fmul: {
1718 Temp src0 = get_alu_src(ctx, instr->src[0]);
1719 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1720 if (dst.regClass() == v2b) {
1721 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1722 } else if (dst.regClass() == v1) {
1723 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1724 } else if (dst.regClass() == v2) {
1725 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1726 } else {
1727 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1728 nir_print_instr(&instr->instr, stderr);
1729 fprintf(stderr, "\n");
1730 }
1731 break;
1732 }
1733 case nir_op_fadd: {
1734 Temp src0 = get_alu_src(ctx, instr->src[0]);
1735 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1736 if (dst.regClass() == v2b) {
1737 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1738 } else if (dst.regClass() == v1) {
1739 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1740 } else if (dst.regClass() == v2) {
1741 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1742 } else {
1743 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1744 nir_print_instr(&instr->instr, stderr);
1745 fprintf(stderr, "\n");
1746 }
1747 break;
1748 }
1749 case nir_op_fsub: {
1750 Temp src0 = get_alu_src(ctx, instr->src[0]);
1751 Temp src1 = get_alu_src(ctx, instr->src[1]);
1752 if (dst.regClass() == v2b) {
1753 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1754 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1755 else
1756 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1757 } else if (dst.regClass() == v1) {
1758 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1759 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1760 else
1761 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1762 } else if (dst.regClass() == v2) {
1763 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1764 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1765 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1766 sub->neg[1] = true;
1767 } else {
1768 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1769 nir_print_instr(&instr->instr, stderr);
1770 fprintf(stderr, "\n");
1771 }
1772 break;
1773 }
1774 case nir_op_fmax: {
1775 Temp src0 = get_alu_src(ctx, instr->src[0]);
1776 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1777 if (dst.regClass() == v2b) {
1778 // TODO: check fp_mode.must_flush_denorms16_64
1779 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1780 } else if (dst.regClass() == v1) {
1781 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1782 } else if (dst.regClass() == v2) {
1783 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1784 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1785 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1786 } else {
1787 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1788 }
1789 } else {
1790 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1791 nir_print_instr(&instr->instr, stderr);
1792 fprintf(stderr, "\n");
1793 }
1794 break;
1795 }
1796 case nir_op_fmin: {
1797 Temp src0 = get_alu_src(ctx, instr->src[0]);
1798 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1799 if (dst.regClass() == v2b) {
1800 // TODO: check fp_mode.must_flush_denorms16_64
1801 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1802 } else if (dst.regClass() == v1) {
1803 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1804 } else if (dst.regClass() == v2) {
1805 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1806 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1807 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1808 } else {
1809 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1810 }
1811 } else {
1812 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1813 nir_print_instr(&instr->instr, stderr);
1814 fprintf(stderr, "\n");
1815 }
1816 break;
1817 }
1818 case nir_op_fmax3: {
1819 if (dst.regClass() == v2b) {
1820 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1821 } else if (dst.regClass() == v1) {
1822 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1823 } else {
1824 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1825 nir_print_instr(&instr->instr, stderr);
1826 fprintf(stderr, "\n");
1827 }
1828 break;
1829 }
1830 case nir_op_fmin3: {
1831 if (dst.regClass() == v2b) {
1832 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1833 } else if (dst.regClass() == v1) {
1834 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1835 } else {
1836 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1837 nir_print_instr(&instr->instr, stderr);
1838 fprintf(stderr, "\n");
1839 }
1840 break;
1841 }
1842 case nir_op_fmed3: {
1843 if (dst.regClass() == v2b) {
1844 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1845 } else if (dst.regClass() == v1) {
1846 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1847 } else {
1848 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1849 nir_print_instr(&instr->instr, stderr);
1850 fprintf(stderr, "\n");
1851 }
1852 break;
1853 }
1854 case nir_op_umax3: {
1855 if (dst.size() == 1) {
1856 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1857 } else {
1858 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1859 nir_print_instr(&instr->instr, stderr);
1860 fprintf(stderr, "\n");
1861 }
1862 break;
1863 }
1864 case nir_op_umin3: {
1865 if (dst.size() == 1) {
1866 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1867 } else {
1868 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1869 nir_print_instr(&instr->instr, stderr);
1870 fprintf(stderr, "\n");
1871 }
1872 break;
1873 }
1874 case nir_op_umed3: {
1875 if (dst.size() == 1) {
1876 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1877 } else {
1878 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1879 nir_print_instr(&instr->instr, stderr);
1880 fprintf(stderr, "\n");
1881 }
1882 break;
1883 }
1884 case nir_op_imax3: {
1885 if (dst.size() == 1) {
1886 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1887 } else {
1888 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1889 nir_print_instr(&instr->instr, stderr);
1890 fprintf(stderr, "\n");
1891 }
1892 break;
1893 }
1894 case nir_op_imin3: {
1895 if (dst.size() == 1) {
1896 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1897 } else {
1898 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1899 nir_print_instr(&instr->instr, stderr);
1900 fprintf(stderr, "\n");
1901 }
1902 break;
1903 }
1904 case nir_op_imed3: {
1905 if (dst.size() == 1) {
1906 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1907 } else {
1908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1909 nir_print_instr(&instr->instr, stderr);
1910 fprintf(stderr, "\n");
1911 }
1912 break;
1913 }
1914 case nir_op_cube_face_coord: {
1915 Temp in = get_alu_src(ctx, instr->src[0], 3);
1916 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1917 emit_extract_vector(ctx, in, 1, v1),
1918 emit_extract_vector(ctx, in, 2, v1) };
1919 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1920 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1921 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1922 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1923 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1924 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1925 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1926 break;
1927 }
1928 case nir_op_cube_face_index: {
1929 Temp in = get_alu_src(ctx, instr->src[0], 3);
1930 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1931 emit_extract_vector(ctx, in, 1, v1),
1932 emit_extract_vector(ctx, in, 2, v1) };
1933 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1934 break;
1935 }
1936 case nir_op_bcsel: {
1937 emit_bcsel(ctx, instr, dst);
1938 break;
1939 }
1940 case nir_op_frsq: {
1941 Temp src = get_alu_src(ctx, instr->src[0]);
1942 if (dst.regClass() == v2b) {
1943 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1944 } else if (dst.regClass() == v1) {
1945 emit_rsq(ctx, bld, Definition(dst), src);
1946 } else if (dst.regClass() == v2) {
1947 /* Lowered at NIR level for precision reasons. */
1948 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1949 } else {
1950 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1951 nir_print_instr(&instr->instr, stderr);
1952 fprintf(stderr, "\n");
1953 }
1954 break;
1955 }
1956 case nir_op_fneg: {
1957 Temp src = get_alu_src(ctx, instr->src[0]);
1958 if (dst.regClass() == v2b) {
1959 if (ctx->block->fp_mode.must_flush_denorms16_64)
1960 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1961 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1962 } else if (dst.regClass() == v1) {
1963 if (ctx->block->fp_mode.must_flush_denorms32)
1964 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1965 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1966 } else if (dst.regClass() == v2) {
1967 if (ctx->block->fp_mode.must_flush_denorms16_64)
1968 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1969 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1970 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1971 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1972 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1973 } else {
1974 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1975 nir_print_instr(&instr->instr, stderr);
1976 fprintf(stderr, "\n");
1977 }
1978 break;
1979 }
1980 case nir_op_fabs: {
1981 Temp src = get_alu_src(ctx, instr->src[0]);
1982 if (dst.regClass() == v2b) {
1983 if (ctx->block->fp_mode.must_flush_denorms16_64)
1984 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1985 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1986 } else if (dst.regClass() == v1) {
1987 if (ctx->block->fp_mode.must_flush_denorms32)
1988 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1989 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1990 } else if (dst.regClass() == v2) {
1991 if (ctx->block->fp_mode.must_flush_denorms16_64)
1992 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1993 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1994 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1995 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1996 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1997 } else {
1998 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1999 nir_print_instr(&instr->instr, stderr);
2000 fprintf(stderr, "\n");
2001 }
2002 break;
2003 }
2004 case nir_op_fsat: {
2005 Temp src = get_alu_src(ctx, instr->src[0]);
2006 if (dst.regClass() == v2b) {
2007 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src);
2008 } else if (dst.regClass() == v1) {
2009 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2010 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
2011 // TODO: confirm that this holds under any circumstances
2012 } else if (dst.regClass() == v2) {
2013 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
2014 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
2015 vop3->clamp = true;
2016 } else {
2017 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2018 nir_print_instr(&instr->instr, stderr);
2019 fprintf(stderr, "\n");
2020 }
2021 break;
2022 }
2023 case nir_op_flog2: {
2024 Temp src = get_alu_src(ctx, instr->src[0]);
2025 if (dst.regClass() == v2b) {
2026 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
2027 } else if (dst.regClass() == v1) {
2028 emit_log2(ctx, bld, Definition(dst), src);
2029 } else {
2030 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2031 nir_print_instr(&instr->instr, stderr);
2032 fprintf(stderr, "\n");
2033 }
2034 break;
2035 }
2036 case nir_op_frcp: {
2037 Temp src = get_alu_src(ctx, instr->src[0]);
2038 if (dst.regClass() == v2b) {
2039 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
2040 } else if (dst.regClass() == v1) {
2041 emit_rcp(ctx, bld, Definition(dst), src);
2042 } else if (dst.regClass() == v2) {
2043 /* Lowered at NIR level for precision reasons. */
2044 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
2045 } else {
2046 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2047 nir_print_instr(&instr->instr, stderr);
2048 fprintf(stderr, "\n");
2049 }
2050 break;
2051 }
2052 case nir_op_fexp2: {
2053 if (dst.regClass() == v2b) {
2054 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
2055 } else if (dst.regClass() == v1) {
2056 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
2057 } else {
2058 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2059 nir_print_instr(&instr->instr, stderr);
2060 fprintf(stderr, "\n");
2061 }
2062 break;
2063 }
2064 case nir_op_fsqrt: {
2065 Temp src = get_alu_src(ctx, instr->src[0]);
2066 if (dst.regClass() == v2b) {
2067 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
2068 } else if (dst.regClass() == v1) {
2069 emit_sqrt(ctx, bld, Definition(dst), src);
2070 } else if (dst.regClass() == v2) {
2071 /* Lowered at NIR level for precision reasons. */
2072 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
2073 } else {
2074 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2075 nir_print_instr(&instr->instr, stderr);
2076 fprintf(stderr, "\n");
2077 }
2078 break;
2079 }
2080 case nir_op_ffract: {
2081 if (dst.regClass() == v2b) {
2082 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
2083 } else if (dst.regClass() == v1) {
2084 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
2085 } else if (dst.regClass() == v2) {
2086 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
2087 } else {
2088 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr->instr, stderr);
2090 fprintf(stderr, "\n");
2091 }
2092 break;
2093 }
2094 case nir_op_ffloor: {
2095 Temp src = get_alu_src(ctx, instr->src[0]);
2096 if (dst.regClass() == v2b) {
2097 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
2098 } else if (dst.regClass() == v1) {
2099 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
2100 } else if (dst.regClass() == v2) {
2101 emit_floor_f64(ctx, bld, Definition(dst), src);
2102 } else {
2103 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2104 nir_print_instr(&instr->instr, stderr);
2105 fprintf(stderr, "\n");
2106 }
2107 break;
2108 }
2109 case nir_op_fceil: {
2110 Temp src0 = get_alu_src(ctx, instr->src[0]);
2111 if (dst.regClass() == v2b) {
2112 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
2113 } else if (dst.regClass() == v1) {
2114 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2115 } else if (dst.regClass() == v2) {
2116 if (ctx->options->chip_class >= GFX7) {
2117 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2118 } else {
2119 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2120 /* trunc = trunc(src0)
2121 * if (src0 > 0.0 && src0 != trunc)
2122 * trunc += 1.0
2123 */
2124 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2125 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2126 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2127 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2128 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);
2129 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2130 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2131 }
2132 } else {
2133 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2134 nir_print_instr(&instr->instr, stderr);
2135 fprintf(stderr, "\n");
2136 }
2137 break;
2138 }
2139 case nir_op_ftrunc: {
2140 Temp src = get_alu_src(ctx, instr->src[0]);
2141 if (dst.regClass() == v2b) {
2142 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2143 } else if (dst.regClass() == v1) {
2144 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2145 } else if (dst.regClass() == v2) {
2146 emit_trunc_f64(ctx, bld, Definition(dst), src);
2147 } else {
2148 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2149 nir_print_instr(&instr->instr, stderr);
2150 fprintf(stderr, "\n");
2151 }
2152 break;
2153 }
2154 case nir_op_fround_even: {
2155 Temp src0 = get_alu_src(ctx, instr->src[0]);
2156 if (dst.regClass() == v2b) {
2157 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2158 } else if (dst.regClass() == v1) {
2159 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2160 } else if (dst.regClass() == v2) {
2161 if (ctx->options->chip_class >= GFX7) {
2162 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2163 } else {
2164 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2165 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2166 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2167
2168 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2169 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));
2170 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));
2171 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));
2172 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2173 tmp = sub->definitions[0].getTemp();
2174
2175 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2176 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2177 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2178 Temp cond = vop3->definitions[0].getTemp();
2179
2180 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2181 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2182 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2183 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2184
2185 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2186 }
2187 } else {
2188 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2189 nir_print_instr(&instr->instr, stderr);
2190 fprintf(stderr, "\n");
2191 }
2192 break;
2193 }
2194 case nir_op_fsin:
2195 case nir_op_fcos: {
2196 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2197 aco_ptr<Instruction> norm;
2198 if (dst.regClass() == v2b) {
2199 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3118u));
2200 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2201 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2202 bld.vop1(opcode, Definition(dst), tmp);
2203 } else if (dst.regClass() == v1) {
2204 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2205 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2206
2207 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2208 if (ctx->options->chip_class < GFX9)
2209 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2210
2211 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2212 bld.vop1(opcode, Definition(dst), tmp);
2213 } else {
2214 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2215 nir_print_instr(&instr->instr, stderr);
2216 fprintf(stderr, "\n");
2217 }
2218 break;
2219 }
2220 case nir_op_ldexp: {
2221 Temp src0 = get_alu_src(ctx, instr->src[0]);
2222 Temp src1 = get_alu_src(ctx, instr->src[1]);
2223 if (dst.regClass() == v2b) {
2224 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2225 } else if (dst.regClass() == v1) {
2226 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2227 } else if (dst.regClass() == v2) {
2228 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2229 } else {
2230 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2231 nir_print_instr(&instr->instr, stderr);
2232 fprintf(stderr, "\n");
2233 }
2234 break;
2235 }
2236 case nir_op_frexp_sig: {
2237 Temp src = get_alu_src(ctx, instr->src[0]);
2238 if (dst.regClass() == v2b) {
2239 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2240 } else if (dst.regClass() == v1) {
2241 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2242 } else if (dst.regClass() == v2) {
2243 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2244 } else {
2245 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2246 nir_print_instr(&instr->instr, stderr);
2247 fprintf(stderr, "\n");
2248 }
2249 break;
2250 }
2251 case nir_op_frexp_exp: {
2252 Temp src = get_alu_src(ctx, instr->src[0]);
2253 if (instr->src[0].src.ssa->bit_size == 16) {
2254 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2255 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2256 convert_int(ctx, bld, tmp, 8, 32, true, dst);
2257 } else if (instr->src[0].src.ssa->bit_size == 32) {
2258 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2259 } else if (instr->src[0].src.ssa->bit_size == 64) {
2260 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2261 } else {
2262 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2263 nir_print_instr(&instr->instr, stderr);
2264 fprintf(stderr, "\n");
2265 }
2266 break;
2267 }
2268 case nir_op_fsign: {
2269 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2270 if (dst.regClass() == v2b) {
2271 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2272 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2273 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2274 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2275 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2276 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2277 } else if (dst.regClass() == v1) {
2278 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2279 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2280 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2281 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2282 } else if (dst.regClass() == v2) {
2283 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2284 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2285 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2286
2287 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2288 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2289 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2290
2291 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2292 } else {
2293 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2294 nir_print_instr(&instr->instr, stderr);
2295 fprintf(stderr, "\n");
2296 }
2297 break;
2298 }
2299 case nir_op_f2f16:
2300 case nir_op_f2f16_rtne: {
2301 Temp src = get_alu_src(ctx, instr->src[0]);
2302 if (instr->src[0].src.ssa->bit_size == 64)
2303 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2304 if (instr->op == nir_op_f2f16_rtne && ctx->block->fp_mode.round16_64 != fp_round_ne)
2305 /* We emit s_round_mode/s_setreg_imm32 in lower_to_hw_instr to
2306 * keep value numbering and the scheduler simpler.
2307 */
2308 bld.vop1(aco_opcode::p_cvt_f16_f32_rtne, Definition(dst), src);
2309 else
2310 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2311 break;
2312 }
2313 case nir_op_f2f16_rtz: {
2314 Temp src = get_alu_src(ctx, instr->src[0]);
2315 if (instr->src[0].src.ssa->bit_size == 64)
2316 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2317 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2318 break;
2319 }
2320 case nir_op_f2f32: {
2321 if (instr->src[0].src.ssa->bit_size == 16) {
2322 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2323 } else if (instr->src[0].src.ssa->bit_size == 64) {
2324 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2325 } else {
2326 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2327 nir_print_instr(&instr->instr, stderr);
2328 fprintf(stderr, "\n");
2329 }
2330 break;
2331 }
2332 case nir_op_f2f64: {
2333 Temp src = get_alu_src(ctx, instr->src[0]);
2334 if (instr->src[0].src.ssa->bit_size == 16)
2335 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2336 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2337 break;
2338 }
2339 case nir_op_i2f16: {
2340 assert(dst.regClass() == v2b);
2341 Temp src = get_alu_src(ctx, instr->src[0]);
2342 if (instr->src[0].src.ssa->bit_size == 8)
2343 src = convert_int(ctx, bld, src, 8, 16, true);
2344 else if (instr->src[0].src.ssa->bit_size == 64)
2345 src = convert_int(ctx, bld, src, 64, 32, false);
2346 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2347 break;
2348 }
2349 case nir_op_i2f32: {
2350 assert(dst.size() == 1);
2351 Temp src = get_alu_src(ctx, instr->src[0]);
2352 if (instr->src[0].src.ssa->bit_size <= 16)
2353 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2354 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2355 break;
2356 }
2357 case nir_op_i2f64: {
2358 if (instr->src[0].src.ssa->bit_size <= 32) {
2359 Temp src = get_alu_src(ctx, instr->src[0]);
2360 if (instr->src[0].src.ssa->bit_size <= 16)
2361 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2362 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2363 } else if (instr->src[0].src.ssa->bit_size == 64) {
2364 Temp src = get_alu_src(ctx, instr->src[0]);
2365 RegClass rc = RegClass(src.type(), 1);
2366 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2367 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2368 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2369 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2370 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2371 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2372
2373 } else {
2374 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2375 nir_print_instr(&instr->instr, stderr);
2376 fprintf(stderr, "\n");
2377 }
2378 break;
2379 }
2380 case nir_op_u2f16: {
2381 assert(dst.regClass() == v2b);
2382 Temp src = get_alu_src(ctx, instr->src[0]);
2383 if (instr->src[0].src.ssa->bit_size == 8)
2384 src = convert_int(ctx, bld, src, 8, 16, false);
2385 else if (instr->src[0].src.ssa->bit_size == 64)
2386 src = convert_int(ctx, bld, src, 64, 32, false);
2387 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2388 break;
2389 }
2390 case nir_op_u2f32: {
2391 assert(dst.size() == 1);
2392 Temp src = get_alu_src(ctx, instr->src[0]);
2393 if (instr->src[0].src.ssa->bit_size == 8) {
2394 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2395 } else {
2396 if (instr->src[0].src.ssa->bit_size == 16)
2397 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2398 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2399 }
2400 break;
2401 }
2402 case nir_op_u2f64: {
2403 if (instr->src[0].src.ssa->bit_size <= 32) {
2404 Temp src = get_alu_src(ctx, instr->src[0]);
2405 if (instr->src[0].src.ssa->bit_size <= 16)
2406 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2407 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2408 } else if (instr->src[0].src.ssa->bit_size == 64) {
2409 Temp src = get_alu_src(ctx, instr->src[0]);
2410 RegClass rc = RegClass(src.type(), 1);
2411 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2412 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2413 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2414 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2415 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2416 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2417 } else {
2418 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2419 nir_print_instr(&instr->instr, stderr);
2420 fprintf(stderr, "\n");
2421 }
2422 break;
2423 }
2424 case nir_op_f2i8:
2425 case nir_op_f2i16: {
2426 if (instr->src[0].src.ssa->bit_size == 16)
2427 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i16_f16, dst);
2428 else if (instr->src[0].src.ssa->bit_size == 32)
2429 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2430 else
2431 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2432 break;
2433 }
2434 case nir_op_f2u8:
2435 case nir_op_f2u16: {
2436 if (instr->src[0].src.ssa->bit_size == 16)
2437 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u16_f16, dst);
2438 else if (instr->src[0].src.ssa->bit_size == 32)
2439 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2440 else
2441 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2442 break;
2443 }
2444 case nir_op_f2i32: {
2445 Temp src = get_alu_src(ctx, instr->src[0]);
2446 if (instr->src[0].src.ssa->bit_size == 16) {
2447 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2448 if (dst.type() == RegType::vgpr) {
2449 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2450 } else {
2451 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2452 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2453 }
2454 } else if (instr->src[0].src.ssa->bit_size == 32) {
2455 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2456 } else if (instr->src[0].src.ssa->bit_size == 64) {
2457 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2458 } else {
2459 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2460 nir_print_instr(&instr->instr, stderr);
2461 fprintf(stderr, "\n");
2462 }
2463 break;
2464 }
2465 case nir_op_f2u32: {
2466 Temp src = get_alu_src(ctx, instr->src[0]);
2467 if (instr->src[0].src.ssa->bit_size == 16) {
2468 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2469 if (dst.type() == RegType::vgpr) {
2470 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2471 } else {
2472 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2473 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2474 }
2475 } else if (instr->src[0].src.ssa->bit_size == 32) {
2476 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2477 } else if (instr->src[0].src.ssa->bit_size == 64) {
2478 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2479 } else {
2480 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2481 nir_print_instr(&instr->instr, stderr);
2482 fprintf(stderr, "\n");
2483 }
2484 break;
2485 }
2486 case nir_op_f2i64: {
2487 Temp src = get_alu_src(ctx, instr->src[0]);
2488 if (instr->src[0].src.ssa->bit_size == 16)
2489 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2490
2491 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2492 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2493 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2494 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2495 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2496 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2497 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2498 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2499 Temp new_exponent = bld.tmp(v1);
2500 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2501 if (ctx->program->chip_class >= GFX8)
2502 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2503 else
2504 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2505 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2506 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2507 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2508 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2509 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2510 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2511 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2512 Temp new_lower = bld.tmp(v1);
2513 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2514 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2515 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2516
2517 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2518 if (src.type() == RegType::vgpr)
2519 src = bld.as_uniform(src);
2520 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2521 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2522 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2523 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2524 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2525 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2526 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2527 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2528 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2529 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2530 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2531 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2532 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2533 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2534 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2535 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2536 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2537 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2538 Temp borrow = bld.tmp(s1);
2539 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2540 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2541 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2542
2543 } else if (instr->src[0].src.ssa->bit_size == 64) {
2544 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2545 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2546 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2547 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2548 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2549 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2550 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2551 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2552 if (dst.type() == RegType::sgpr) {
2553 lower = bld.as_uniform(lower);
2554 upper = bld.as_uniform(upper);
2555 }
2556 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2557
2558 } else {
2559 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2560 nir_print_instr(&instr->instr, stderr);
2561 fprintf(stderr, "\n");
2562 }
2563 break;
2564 }
2565 case nir_op_f2u64: {
2566 Temp src = get_alu_src(ctx, instr->src[0]);
2567 if (instr->src[0].src.ssa->bit_size == 16)
2568 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2569
2570 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2571 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2572 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2573 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2574 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2575 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2576 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2577 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2578 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2579 Temp new_exponent = bld.tmp(v1);
2580 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2581 if (ctx->program->chip_class >= GFX8)
2582 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2583 else
2584 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2585 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2586 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2587 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2588 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2589 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2590 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2591 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2592
2593 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2594 if (src.type() == RegType::vgpr)
2595 src = bld.as_uniform(src);
2596 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2597 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2598 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2599 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2600 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2601 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2602 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2603 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2604 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2605 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2606 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2607 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2608 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2609 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2610 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2611 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2612 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2613 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2614
2615 } else if (instr->src[0].src.ssa->bit_size == 64) {
2616 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2617 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2618 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2619 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2620 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2621 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2622 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2623 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2624 if (dst.type() == RegType::sgpr) {
2625 lower = bld.as_uniform(lower);
2626 upper = bld.as_uniform(upper);
2627 }
2628 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2629
2630 } else {
2631 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2632 nir_print_instr(&instr->instr, stderr);
2633 fprintf(stderr, "\n");
2634 }
2635 break;
2636 }
2637 case nir_op_b2f16: {
2638 Temp src = get_alu_src(ctx, instr->src[0]);
2639 assert(src.regClass() == bld.lm);
2640
2641 if (dst.regClass() == s1) {
2642 src = bool_to_scalar_condition(ctx, src);
2643 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2644 } else if (dst.regClass() == v2b) {
2645 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2646 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2647 } else {
2648 unreachable("Wrong destination register class for nir_op_b2f16.");
2649 }
2650 break;
2651 }
2652 case nir_op_b2f32: {
2653 Temp src = get_alu_src(ctx, instr->src[0]);
2654 assert(src.regClass() == bld.lm);
2655
2656 if (dst.regClass() == s1) {
2657 src = bool_to_scalar_condition(ctx, src);
2658 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2659 } else if (dst.regClass() == v1) {
2660 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2661 } else {
2662 unreachable("Wrong destination register class for nir_op_b2f32.");
2663 }
2664 break;
2665 }
2666 case nir_op_b2f64: {
2667 Temp src = get_alu_src(ctx, instr->src[0]);
2668 assert(src.regClass() == bld.lm);
2669
2670 if (dst.regClass() == s2) {
2671 src = bool_to_scalar_condition(ctx, src);
2672 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2673 } else if (dst.regClass() == v2) {
2674 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2675 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2676 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2677 } else {
2678 unreachable("Wrong destination register class for nir_op_b2f64.");
2679 }
2680 break;
2681 }
2682 case nir_op_i2i8:
2683 case nir_op_i2i16:
2684 case nir_op_i2i32:
2685 case nir_op_i2i64: {
2686 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2687 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2688 break;
2689 }
2690 case nir_op_u2u8:
2691 case nir_op_u2u16:
2692 case nir_op_u2u32:
2693 case nir_op_u2u64: {
2694 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2695 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2696 break;
2697 }
2698 case nir_op_b2b32:
2699 case nir_op_b2i8:
2700 case nir_op_b2i16:
2701 case nir_op_b2i32:
2702 case nir_op_b2i64: {
2703 Temp src = get_alu_src(ctx, instr->src[0]);
2704 assert(src.regClass() == bld.lm);
2705
2706 Temp tmp = dst.bytes() == 8 ? bld.tmp(RegClass::get(dst.type(), 4)) : dst;
2707 if (tmp.regClass() == s1) {
2708 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2709 bool_to_scalar_condition(ctx, src, tmp);
2710 } else if (tmp.type() == RegType::vgpr) {
2711 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(tmp), Operand(0u), Operand(1u), src);
2712 } else {
2713 unreachable("Invalid register class for b2i32");
2714 }
2715
2716 if (tmp != dst)
2717 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
2718 break;
2719 }
2720 case nir_op_b2b1:
2721 case nir_op_i2b1: {
2722 Temp src = get_alu_src(ctx, instr->src[0]);
2723 assert(dst.regClass() == bld.lm);
2724
2725 if (src.type() == RegType::vgpr) {
2726 assert(src.regClass() == v1 || src.regClass() == v2);
2727 assert(dst.regClass() == bld.lm);
2728 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2729 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2730 } else {
2731 assert(src.regClass() == s1 || src.regClass() == s2);
2732 Temp tmp;
2733 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2734 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2735 } else {
2736 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2737 bld.scc(bld.def(s1)), Operand(0u), src);
2738 }
2739 bool_to_vector_condition(ctx, tmp, dst);
2740 }
2741 break;
2742 }
2743 case nir_op_pack_64_2x32_split: {
2744 Temp src0 = get_alu_src(ctx, instr->src[0]);
2745 Temp src1 = get_alu_src(ctx, instr->src[1]);
2746
2747 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2748 break;
2749 }
2750 case nir_op_unpack_64_2x32_split_x:
2751 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2752 break;
2753 case nir_op_unpack_64_2x32_split_y:
2754 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2755 break;
2756 case nir_op_unpack_32_2x16_split_x:
2757 if (dst.type() == RegType::vgpr) {
2758 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2759 } else {
2760 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2761 }
2762 break;
2763 case nir_op_unpack_32_2x16_split_y:
2764 if (dst.type() == RegType::vgpr) {
2765 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2766 } else {
2767 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)));
2768 }
2769 break;
2770 case nir_op_pack_32_2x16_split: {
2771 Temp src0 = get_alu_src(ctx, instr->src[0]);
2772 Temp src1 = get_alu_src(ctx, instr->src[1]);
2773 if (dst.regClass() == v1) {
2774 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2775 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2776 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2777 } else {
2778 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2779 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2780 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2781 }
2782 break;
2783 }
2784 case nir_op_pack_half_2x16: {
2785 Temp src = get_alu_src(ctx, instr->src[0], 2);
2786
2787 if (dst.regClass() == v1) {
2788 Temp src0 = bld.tmp(v1);
2789 Temp src1 = bld.tmp(v1);
2790 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2791 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2792 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2793 else
2794 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2795 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2796 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2797 } else {
2798 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2799 nir_print_instr(&instr->instr, stderr);
2800 fprintf(stderr, "\n");
2801 }
2802 break;
2803 }
2804 case nir_op_unpack_half_2x16_split_x: {
2805 if (dst.regClass() == v1) {
2806 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2807 } else {
2808 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2809 nir_print_instr(&instr->instr, stderr);
2810 fprintf(stderr, "\n");
2811 }
2812 break;
2813 }
2814 case nir_op_unpack_half_2x16_split_y: {
2815 if (dst.regClass() == v1) {
2816 /* TODO: use SDWA here */
2817 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2818 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2819 } else {
2820 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2821 nir_print_instr(&instr->instr, stderr);
2822 fprintf(stderr, "\n");
2823 }
2824 break;
2825 }
2826 case nir_op_fquantize2f16: {
2827 Temp src = get_alu_src(ctx, instr->src[0]);
2828 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2829 Temp f32, cmp_res;
2830
2831 if (ctx->program->chip_class >= GFX8) {
2832 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2833 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2834 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2835 } else {
2836 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2837 * so compare the result and flush to 0 if it's smaller.
2838 */
2839 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2840 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2841 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2842 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2843 cmp_res = vop3->definitions[0].getTemp();
2844 }
2845
2846 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2847 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2848 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2849 } else {
2850 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2851 }
2852 break;
2853 }
2854 case nir_op_bfm: {
2855 Temp bits = get_alu_src(ctx, instr->src[0]);
2856 Temp offset = get_alu_src(ctx, instr->src[1]);
2857
2858 if (dst.regClass() == s1) {
2859 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2860 } else if (dst.regClass() == v1) {
2861 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2862 } else {
2863 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2864 nir_print_instr(&instr->instr, stderr);
2865 fprintf(stderr, "\n");
2866 }
2867 break;
2868 }
2869 case nir_op_bitfield_select: {
2870 /* (mask & insert) | (~mask & base) */
2871 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2872 Temp insert = get_alu_src(ctx, instr->src[1]);
2873 Temp base = get_alu_src(ctx, instr->src[2]);
2874
2875 /* dst = (insert & bitmask) | (base & ~bitmask) */
2876 if (dst.regClass() == s1) {
2877 aco_ptr<Instruction> sop2;
2878 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2879 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2880 Operand lhs;
2881 if (const_insert && const_bitmask) {
2882 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2883 } else {
2884 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2885 lhs = Operand(insert);
2886 }
2887
2888 Operand rhs;
2889 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2890 if (const_base && const_bitmask) {
2891 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2892 } else {
2893 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2894 rhs = Operand(base);
2895 }
2896
2897 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2898
2899 } else if (dst.regClass() == v1) {
2900 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2901 base = as_vgpr(ctx, base);
2902 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2903 insert = as_vgpr(ctx, insert);
2904
2905 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2906
2907 } else {
2908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2909 nir_print_instr(&instr->instr, stderr);
2910 fprintf(stderr, "\n");
2911 }
2912 break;
2913 }
2914 case nir_op_ubfe:
2915 case nir_op_ibfe: {
2916 Temp base = get_alu_src(ctx, instr->src[0]);
2917 Temp offset = get_alu_src(ctx, instr->src[1]);
2918 Temp bits = get_alu_src(ctx, instr->src[2]);
2919
2920 if (dst.type() == RegType::sgpr) {
2921 Operand extract;
2922 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2923 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2924 if (const_offset && const_bits) {
2925 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2926 extract = Operand(const_extract);
2927 } else {
2928 Operand width;
2929 if (const_bits) {
2930 width = Operand(const_bits->u32 << 16);
2931 } else {
2932 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2933 }
2934 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2935 }
2936
2937 aco_opcode opcode;
2938 if (dst.regClass() == s1) {
2939 if (instr->op == nir_op_ubfe)
2940 opcode = aco_opcode::s_bfe_u32;
2941 else
2942 opcode = aco_opcode::s_bfe_i32;
2943 } else if (dst.regClass() == s2) {
2944 if (instr->op == nir_op_ubfe)
2945 opcode = aco_opcode::s_bfe_u64;
2946 else
2947 opcode = aco_opcode::s_bfe_i64;
2948 } else {
2949 unreachable("Unsupported BFE bit size");
2950 }
2951
2952 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2953
2954 } else {
2955 aco_opcode opcode;
2956 if (dst.regClass() == v1) {
2957 if (instr->op == nir_op_ubfe)
2958 opcode = aco_opcode::v_bfe_u32;
2959 else
2960 opcode = aco_opcode::v_bfe_i32;
2961 } else {
2962 unreachable("Unsupported BFE bit size");
2963 }
2964
2965 emit_vop3a_instruction(ctx, instr, opcode, dst);
2966 }
2967 break;
2968 }
2969 case nir_op_bit_count: {
2970 Temp src = get_alu_src(ctx, instr->src[0]);
2971 if (src.regClass() == s1) {
2972 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2973 } else if (src.regClass() == v1) {
2974 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2975 } else if (src.regClass() == v2) {
2976 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2977 emit_extract_vector(ctx, src, 1, v1),
2978 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2979 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2980 } else if (src.regClass() == s2) {
2981 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2982 } else {
2983 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2984 nir_print_instr(&instr->instr, stderr);
2985 fprintf(stderr, "\n");
2986 }
2987 break;
2988 }
2989 case nir_op_flt: {
2990 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2991 break;
2992 }
2993 case nir_op_fge: {
2994 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2995 break;
2996 }
2997 case nir_op_feq: {
2998 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2999 break;
3000 }
3001 case nir_op_fne: {
3002 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
3003 break;
3004 }
3005 case nir_op_ilt: {
3006 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);
3007 break;
3008 }
3009 case nir_op_ige: {
3010 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);
3011 break;
3012 }
3013 case nir_op_ieq: {
3014 if (instr->src[0].src.ssa->bit_size == 1)
3015 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
3016 else
3017 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,
3018 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
3019 break;
3020 }
3021 case nir_op_ine: {
3022 if (instr->src[0].src.ssa->bit_size == 1)
3023 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
3024 else
3025 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,
3026 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
3027 break;
3028 }
3029 case nir_op_ult: {
3030 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);
3031 break;
3032 }
3033 case nir_op_uge: {
3034 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);
3035 break;
3036 }
3037 case nir_op_fddx:
3038 case nir_op_fddy:
3039 case nir_op_fddx_fine:
3040 case nir_op_fddy_fine:
3041 case nir_op_fddx_coarse:
3042 case nir_op_fddy_coarse: {
3043 Temp src = get_alu_src(ctx, instr->src[0]);
3044 uint16_t dpp_ctrl1, dpp_ctrl2;
3045 if (instr->op == nir_op_fddx_fine) {
3046 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
3047 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
3048 } else if (instr->op == nir_op_fddy_fine) {
3049 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
3050 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
3051 } else {
3052 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
3053 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
3054 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
3055 else
3056 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
3057 }
3058
3059 Temp tmp;
3060 if (ctx->program->chip_class >= GFX8) {
3061 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
3062 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
3063 } else {
3064 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
3065 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3066 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3067 }
3068 emit_wqm(ctx, tmp, dst, true);
3069 break;
3070 }
3071 default:
3072 fprintf(stderr, "Unknown NIR ALU instr: ");
3073 nir_print_instr(&instr->instr, stderr);
3074 fprintf(stderr, "\n");
3075 }
3076 }
3077
3078 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3079 {
3080 Temp dst = get_ssa_temp(ctx, &instr->def);
3081
3082 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3083 // which get truncated the lsb if double and msb if int
3084 // for now, we only use s_mov_b64 with 64bit inline constants
3085 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3086 assert(dst.type() == RegType::sgpr);
3087
3088 Builder bld(ctx->program, ctx->block);
3089
3090 if (instr->def.bit_size == 1) {
3091 assert(dst.regClass() == bld.lm);
3092 int val = instr->value[0].b ? -1 : 0;
3093 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3094 bld.sop1(Builder::s_mov, Definition(dst), op);
3095 } else if (instr->def.bit_size == 8) {
3096 /* ensure that the value is correctly represented in the low byte of the register */
3097 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3098 } else if (instr->def.bit_size == 16) {
3099 /* ensure that the value is correctly represented in the low half of the register */
3100 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3101 } else if (dst.size() == 1) {
3102 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3103 } else {
3104 assert(dst.size() != 1);
3105 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3106 if (instr->def.bit_size == 64)
3107 for (unsigned i = 0; i < dst.size(); i++)
3108 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3109 else {
3110 for (unsigned i = 0; i < dst.size(); i++)
3111 vec->operands[i] = Operand{instr->value[i].u32};
3112 }
3113 vec->definitions[0] = Definition(dst);
3114 ctx->block->instructions.emplace_back(std::move(vec));
3115 }
3116 }
3117
3118 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3119 {
3120 uint32_t new_mask = 0;
3121 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3122 if (mask & (1u << i))
3123 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3124 return new_mask;
3125 }
3126
3127 struct LoadEmitInfo {
3128 Operand offset;
3129 Temp dst;
3130 unsigned num_components;
3131 unsigned component_size;
3132 Temp resource = Temp(0, s1);
3133 unsigned component_stride = 0;
3134 unsigned const_offset = 0;
3135 unsigned align_mul = 0;
3136 unsigned align_offset = 0;
3137
3138 bool glc = false;
3139 unsigned swizzle_component_size = 0;
3140 memory_sync_info sync;
3141 Temp soffset = Temp(0, s1);
3142 };
3143
3144 using LoadCallback = Temp(*)(
3145 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3146 unsigned align, unsigned const_offset, Temp dst_hint);
3147
3148 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3149 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3150 {
3151 unsigned load_size = info->num_components * info->component_size;
3152 unsigned component_size = info->component_size;
3153
3154 unsigned num_vals = 0;
3155 Temp vals[info->dst.bytes()];
3156
3157 unsigned const_offset = info->const_offset;
3158
3159 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3160 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3161
3162 unsigned bytes_read = 0;
3163 while (bytes_read < load_size) {
3164 unsigned bytes_needed = load_size - bytes_read;
3165
3166 /* add buffer for unaligned loads */
3167 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3168
3169 if (byte_align) {
3170 if ((bytes_needed > 2 ||
3171 (bytes_needed == 2 && (align_mul % 2 || align_offset % 2)) ||
3172 !supports_8bit_16bit_loads) && byte_align_loads) {
3173 if (info->component_stride) {
3174 assert(supports_8bit_16bit_loads && "unimplemented");
3175 bytes_needed = 2;
3176 byte_align = 0;
3177 } else {
3178 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3179 bytes_needed = align(bytes_needed, 4);
3180 }
3181 } else {
3182 byte_align = 0;
3183 }
3184 }
3185
3186 if (info->swizzle_component_size)
3187 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3188 if (info->component_stride)
3189 bytes_needed = MIN2(bytes_needed, info->component_size);
3190
3191 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3192
3193 /* reduce constant offset */
3194 Operand offset = info->offset;
3195 unsigned reduced_const_offset = const_offset;
3196 bool remove_const_offset_completely = need_to_align_offset;
3197 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3198 unsigned to_add = const_offset;
3199 if (remove_const_offset_completely) {
3200 reduced_const_offset = 0;
3201 } else {
3202 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3203 reduced_const_offset %= max_const_offset_plus_one;
3204 }
3205 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3206 if (offset.isConstant()) {
3207 offset = Operand(offset.constantValue() + to_add);
3208 } else if (offset_tmp.regClass() == s1) {
3209 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3210 offset_tmp, Operand(to_add));
3211 } else if (offset_tmp.regClass() == v1) {
3212 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3213 } else {
3214 Temp lo = bld.tmp(offset_tmp.type(), 1);
3215 Temp hi = bld.tmp(offset_tmp.type(), 1);
3216 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3217
3218 if (offset_tmp.regClass() == s2) {
3219 Temp carry = bld.tmp(s1);
3220 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3221 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3222 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3223 } else {
3224 Temp new_lo = bld.tmp(v1);
3225 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3226 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3227 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3228 }
3229 }
3230 }
3231
3232 /* align offset down if needed */
3233 Operand aligned_offset = offset;
3234 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3235 if (need_to_align_offset) {
3236 align = 4;
3237 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3238 if (offset.isConstant()) {
3239 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3240 } else if (offset_tmp.regClass() == s1) {
3241 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3242 } else if (offset_tmp.regClass() == s2) {
3243 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3244 } else if (offset_tmp.regClass() == v1) {
3245 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3246 } else if (offset_tmp.regClass() == v2) {
3247 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3248 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3249 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3250 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3251 }
3252 }
3253 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3254 bld.copy(bld.def(s1), aligned_offset);
3255
3256 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3257 reduced_const_offset, byte_align ? Temp() : info->dst);
3258
3259 /* the callback wrote directly to dst */
3260 if (val == info->dst) {
3261 assert(num_vals == 0);
3262 emit_split_vector(ctx, info->dst, info->num_components);
3263 return;
3264 }
3265
3266 /* shift result right if needed */
3267 if (info->component_size < 4 && byte_align_loads) {
3268 Operand align((uint32_t)byte_align);
3269 if (byte_align == -1) {
3270 if (offset.isConstant())
3271 align = Operand(offset.constantValue() % 4u);
3272 else if (offset.size() == 2)
3273 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3274 else
3275 align = offset;
3276 }
3277
3278 assert(val.bytes() >= load_size && "unimplemented");
3279 if (val.type() == RegType::sgpr)
3280 byte_align_scalar(ctx, val, align, info->dst);
3281 else
3282 byte_align_vector(ctx, val, align, info->dst, component_size);
3283 return;
3284 }
3285
3286 /* add result to list and advance */
3287 if (info->component_stride) {
3288 assert(val.bytes() == info->component_size && "unimplemented");
3289 const_offset += info->component_stride;
3290 align_offset = (align_offset + info->component_stride) % align_mul;
3291 } else {
3292 const_offset += val.bytes();
3293 align_offset = (align_offset + val.bytes()) % align_mul;
3294 }
3295 bytes_read += val.bytes();
3296 vals[num_vals++] = val;
3297 }
3298
3299 /* create array of components */
3300 unsigned components_split = 0;
3301 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3302 bool has_vgprs = false;
3303 for (unsigned i = 0; i < num_vals;) {
3304 Temp tmp[num_vals];
3305 unsigned num_tmps = 0;
3306 unsigned tmp_size = 0;
3307 RegType reg_type = RegType::sgpr;
3308 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3309 if (vals[i].type() == RegType::vgpr)
3310 reg_type = RegType::vgpr;
3311 tmp_size += vals[i].bytes();
3312 tmp[num_tmps++] = vals[i++];
3313 }
3314 if (num_tmps > 1) {
3315 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3316 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3317 for (unsigned i = 0; i < num_tmps; i++)
3318 vec->operands[i] = Operand(tmp[i]);
3319 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3320 vec->definitions[0] = Definition(tmp[0]);
3321 bld.insert(std::move(vec));
3322 }
3323
3324 if (tmp[0].bytes() % component_size) {
3325 /* trim tmp[0] */
3326 assert(i == num_vals);
3327 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3328 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3329 }
3330
3331 RegClass elem_rc = RegClass::get(reg_type, component_size);
3332
3333 unsigned start = components_split;
3334
3335 if (tmp_size == elem_rc.bytes()) {
3336 allocated_vec[components_split++] = tmp[0];
3337 } else {
3338 assert(tmp_size % elem_rc.bytes() == 0);
3339 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3340 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3341 for (unsigned i = 0; i < split->definitions.size(); i++) {
3342 Temp component = bld.tmp(elem_rc);
3343 allocated_vec[components_split++] = component;
3344 split->definitions[i] = Definition(component);
3345 }
3346 split->operands[0] = Operand(tmp[0]);
3347 bld.insert(std::move(split));
3348 }
3349
3350 /* try to p_as_uniform early so we can create more optimizable code and
3351 * also update allocated_vec */
3352 for (unsigned j = start; j < components_split; j++) {
3353 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3354 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3355 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3356 }
3357 }
3358
3359 /* concatenate components and p_as_uniform() result if needed */
3360 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3361 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3362
3363 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3364
3365 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3366 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3367 for (unsigned i = 0; i < info->num_components; i++)
3368 vec->operands[i] = Operand(allocated_vec[i]);
3369 if (padding_bytes)
3370 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3371 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3372 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3373 vec->definitions[0] = Definition(tmp);
3374 bld.insert(std::move(vec));
3375 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3376 } else {
3377 vec->definitions[0] = Definition(info->dst);
3378 bld.insert(std::move(vec));
3379 }
3380 }
3381
3382 Operand load_lds_size_m0(Builder& bld)
3383 {
3384 /* TODO: m0 does not need to be initialized on GFX9+ */
3385 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3386 }
3387
3388 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3389 Temp offset, unsigned bytes_needed,
3390 unsigned align, unsigned const_offset,
3391 Temp dst_hint)
3392 {
3393 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3394
3395 Operand m = load_lds_size_m0(bld);
3396
3397 bool large_ds_read = bld.program->chip_class >= GFX7;
3398 bool usable_read2 = bld.program->chip_class >= GFX7;
3399
3400 bool read2 = false;
3401 unsigned size = 0;
3402 aco_opcode op;
3403 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3404 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3405 size = 16;
3406 op = aco_opcode::ds_read_b128;
3407 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3408 size = 16;
3409 read2 = true;
3410 op = aco_opcode::ds_read2_b64;
3411 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3412 size = 12;
3413 op = aco_opcode::ds_read_b96;
3414 } else if (bytes_needed >= 8 && align % 8 == 0) {
3415 size = 8;
3416 op = aco_opcode::ds_read_b64;
3417 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3418 size = 8;
3419 read2 = true;
3420 op = aco_opcode::ds_read2_b32;
3421 } else if (bytes_needed >= 4 && align % 4 == 0) {
3422 size = 4;
3423 op = aco_opcode::ds_read_b32;
3424 } else if (bytes_needed >= 2 && align % 2 == 0) {
3425 size = 2;
3426 op = aco_opcode::ds_read_u16;
3427 } else {
3428 size = 1;
3429 op = aco_opcode::ds_read_u8;
3430 }
3431
3432 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3433 if (const_offset >= max_offset_plus_one) {
3434 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3435 const_offset %= max_offset_plus_one;
3436 }
3437
3438 if (read2)
3439 const_offset /= (size / 2u);
3440
3441 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3442 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3443 Instruction *instr;
3444 if (read2)
3445 instr = bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3446 else
3447 instr = bld.ds(op, Definition(val), offset, m, const_offset);
3448 static_cast<DS_instruction *>(instr)->sync = info->sync;
3449
3450 if (size < 4)
3451 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3452
3453 return val;
3454 }
3455
3456 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3457
3458 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3459 Temp offset, unsigned bytes_needed,
3460 unsigned align, unsigned const_offset,
3461 Temp dst_hint)
3462 {
3463 unsigned size = 0;
3464 aco_opcode op;
3465 if (bytes_needed <= 4) {
3466 size = 1;
3467 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3468 } else if (bytes_needed <= 8) {
3469 size = 2;
3470 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3471 } else if (bytes_needed <= 16) {
3472 size = 4;
3473 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3474 } else if (bytes_needed <= 32) {
3475 size = 8;
3476 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3477 } else {
3478 size = 16;
3479 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3480 }
3481 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3482 if (info->resource.id()) {
3483 load->operands[0] = Operand(info->resource);
3484 load->operands[1] = Operand(offset);
3485 } else {
3486 load->operands[0] = Operand(offset);
3487 load->operands[1] = Operand(0u);
3488 }
3489 RegClass rc(RegType::sgpr, size);
3490 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3491 load->definitions[0] = Definition(val);
3492 load->glc = info->glc;
3493 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3494 load->sync = info->sync;
3495 bld.insert(std::move(load));
3496 return val;
3497 }
3498
3499 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3500
3501 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3502 Temp offset, unsigned bytes_needed,
3503 unsigned align_, unsigned const_offset,
3504 Temp dst_hint)
3505 {
3506 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3507 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3508
3509 if (info->soffset.id()) {
3510 if (soffset.isTemp())
3511 vaddr = bld.copy(bld.def(v1), soffset);
3512 soffset = Operand(info->soffset);
3513 }
3514
3515 unsigned bytes_size = 0;
3516 aco_opcode op;
3517 if (bytes_needed == 1 || align_ % 2) {
3518 bytes_size = 1;
3519 op = aco_opcode::buffer_load_ubyte;
3520 } else if (bytes_needed == 2 || align_ % 4) {
3521 bytes_size = 2;
3522 op = aco_opcode::buffer_load_ushort;
3523 } else if (bytes_needed <= 4) {
3524 bytes_size = 4;
3525 op = aco_opcode::buffer_load_dword;
3526 } else if (bytes_needed <= 8) {
3527 bytes_size = 8;
3528 op = aco_opcode::buffer_load_dwordx2;
3529 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3530 bytes_size = 12;
3531 op = aco_opcode::buffer_load_dwordx3;
3532 } else {
3533 bytes_size = 16;
3534 op = aco_opcode::buffer_load_dwordx4;
3535 }
3536 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3537 mubuf->operands[0] = Operand(info->resource);
3538 mubuf->operands[1] = vaddr;
3539 mubuf->operands[2] = soffset;
3540 mubuf->offen = (offset.type() == RegType::vgpr);
3541 mubuf->glc = info->glc;
3542 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3543 mubuf->sync = info->sync;
3544 mubuf->offset = const_offset;
3545 mubuf->swizzled = info->swizzle_component_size != 0;
3546 RegClass rc = RegClass::get(RegType::vgpr, bytes_size);
3547 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3548 mubuf->definitions[0] = Definition(val);
3549 bld.insert(std::move(mubuf));
3550
3551 return val;
3552 }
3553
3554 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3555 static auto emit_scratch_load = emit_load<mubuf_load_callback, false, true, 4096>;
3556
3557 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3558 {
3559 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3560 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3561
3562 if (addr.type() == RegType::vgpr)
3563 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3564 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3565 }
3566
3567 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3568 Temp offset, unsigned bytes_needed,
3569 unsigned align_, unsigned const_offset,
3570 Temp dst_hint)
3571 {
3572 unsigned bytes_size = 0;
3573 bool mubuf = bld.program->chip_class == GFX6;
3574 bool global = bld.program->chip_class >= GFX9;
3575 aco_opcode op;
3576 if (bytes_needed == 1) {
3577 bytes_size = 1;
3578 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3579 } else if (bytes_needed == 2) {
3580 bytes_size = 2;
3581 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3582 } else if (bytes_needed <= 4) {
3583 bytes_size = 4;
3584 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3585 } else if (bytes_needed <= 8) {
3586 bytes_size = 8;
3587 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3588 } else if (bytes_needed <= 12 && !mubuf) {
3589 bytes_size = 12;
3590 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3591 } else {
3592 bytes_size = 16;
3593 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3594 }
3595 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3596 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3597 if (mubuf) {
3598 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3599 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3600 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3601 mubuf->operands[2] = Operand(0u);
3602 mubuf->glc = info->glc;
3603 mubuf->dlc = false;
3604 mubuf->offset = 0;
3605 mubuf->addr64 = offset.type() == RegType::vgpr;
3606 mubuf->disable_wqm = false;
3607 mubuf->sync = info->sync;
3608 mubuf->definitions[0] = Definition(val);
3609 bld.insert(std::move(mubuf));
3610 } else {
3611 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3612
3613 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3614 flat->operands[0] = Operand(offset);
3615 flat->operands[1] = Operand(s1);
3616 flat->glc = info->glc;
3617 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3618 flat->sync = info->sync;
3619 flat->offset = 0u;
3620 flat->definitions[0] = Definition(val);
3621 bld.insert(std::move(flat));
3622 }
3623
3624 return val;
3625 }
3626
3627 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3628
3629 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3630 Temp address, unsigned base_offset, unsigned align)
3631 {
3632 assert(util_is_power_of_two_nonzero(align));
3633
3634 Builder bld(ctx->program, ctx->block);
3635
3636 unsigned num_components = dst.bytes() / elem_size_bytes;
3637 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3638 info.align_mul = align;
3639 info.align_offset = 0;
3640 info.sync = memory_sync_info(storage_shared);
3641 info.const_offset = base_offset;
3642 emit_lds_load(ctx, bld, &info);
3643
3644 return dst;
3645 }
3646
3647 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3648 {
3649 if (!count)
3650 return;
3651
3652 Builder bld(ctx->program, ctx->block);
3653
3654 ASSERTED bool is_subdword = false;
3655 for (unsigned i = 0; i < count; i++)
3656 is_subdword |= offsets[i] % 4;
3657 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3658 assert(!is_subdword || dst_type == RegType::vgpr);
3659
3660 /* count == 1 fast path */
3661 if (count == 1) {
3662 if (dst_type == RegType::sgpr)
3663 dst[0] = bld.as_uniform(src);
3664 else
3665 dst[0] = as_vgpr(ctx, src);
3666 return;
3667 }
3668
3669 for (unsigned i = 0; i < count - 1; i++)
3670 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3671 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3672
3673 if (is_subdword && src.type() == RegType::sgpr) {
3674 src = as_vgpr(ctx, src);
3675 } else {
3676 /* use allocated_vec if possible */
3677 auto it = ctx->allocated_vec.find(src.id());
3678 if (it != ctx->allocated_vec.end()) {
3679 if (!it->second[0].id())
3680 goto split;
3681 unsigned elem_size = it->second[0].bytes();
3682 assert(src.bytes() % elem_size == 0);
3683
3684 for (unsigned i = 0; i < src.bytes() / elem_size; i++) {
3685 if (!it->second[i].id())
3686 goto split;
3687 }
3688
3689 for (unsigned i = 0; i < count; i++) {
3690 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3691 goto split;
3692 }
3693
3694 for (unsigned i = 0; i < count; i++) {
3695 unsigned start_idx = offsets[i] / elem_size;
3696 unsigned op_count = dst[i].bytes() / elem_size;
3697 if (op_count == 1) {
3698 if (dst_type == RegType::sgpr)
3699 dst[i] = bld.as_uniform(it->second[start_idx]);
3700 else
3701 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3702 continue;
3703 }
3704
3705 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3706 for (unsigned j = 0; j < op_count; j++) {
3707 Temp tmp = it->second[start_idx + j];
3708 if (dst_type == RegType::sgpr)
3709 tmp = bld.as_uniform(tmp);
3710 vec->operands[j] = Operand(tmp);
3711 }
3712 vec->definitions[0] = Definition(dst[i]);
3713 bld.insert(std::move(vec));
3714 }
3715 return;
3716 }
3717 }
3718
3719 split:
3720
3721 if (dst_type == RegType::sgpr)
3722 src = bld.as_uniform(src);
3723
3724 /* just split it */
3725 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3726 split->operands[0] = Operand(src);
3727 for (unsigned i = 0; i < count; i++)
3728 split->definitions[i] = Definition(dst[i]);
3729 bld.insert(std::move(split));
3730 }
3731
3732 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3733 int *start, int *count)
3734 {
3735 unsigned start_elem = ffs(todo_mask) - 1;
3736 bool skip = !(mask & (1 << start_elem));
3737 if (skip)
3738 mask = ~mask & todo_mask;
3739
3740 mask &= todo_mask;
3741
3742 u_bit_scan_consecutive_range(&mask, start, count);
3743
3744 return !skip;
3745 }
3746
3747 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3748 {
3749 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3750 }
3751
3752 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3753 Temp address, unsigned base_offset, unsigned align)
3754 {
3755 assert(util_is_power_of_two_nonzero(align));
3756 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3757
3758 Builder bld(ctx->program, ctx->block);
3759 bool large_ds_write = ctx->options->chip_class >= GFX7;
3760 bool usable_write2 = ctx->options->chip_class >= GFX7;
3761
3762 unsigned write_count = 0;
3763 Temp write_datas[32];
3764 unsigned offsets[32];
3765 aco_opcode opcodes[32];
3766
3767 wrmask = widen_mask(wrmask, elem_size_bytes);
3768
3769 uint32_t todo = u_bit_consecutive(0, data.bytes());
3770 while (todo) {
3771 int offset, bytes;
3772 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3773 offsets[write_count] = offset;
3774 opcodes[write_count] = aco_opcode::num_opcodes;
3775 write_count++;
3776 advance_write_mask(&todo, offset, bytes);
3777 continue;
3778 }
3779
3780 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3781 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3782 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3783 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3784
3785 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3786 aco_opcode op = aco_opcode::num_opcodes;
3787 if (bytes >= 16 && aligned16 && large_ds_write) {
3788 op = aco_opcode::ds_write_b128;
3789 bytes = 16;
3790 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3791 op = aco_opcode::ds_write_b96;
3792 bytes = 12;
3793 } else if (bytes >= 8 && aligned8) {
3794 op = aco_opcode::ds_write_b64;
3795 bytes = 8;
3796 } else if (bytes >= 4 && aligned4) {
3797 op = aco_opcode::ds_write_b32;
3798 bytes = 4;
3799 } else if (bytes >= 2 && aligned2) {
3800 op = aco_opcode::ds_write_b16;
3801 bytes = 2;
3802 } else if (bytes >= 1) {
3803 op = aco_opcode::ds_write_b8;
3804 bytes = 1;
3805 } else {
3806 assert(false);
3807 }
3808
3809 offsets[write_count] = offset;
3810 opcodes[write_count] = op;
3811 write_count++;
3812 advance_write_mask(&todo, offset, bytes);
3813 }
3814
3815 Operand m = load_lds_size_m0(bld);
3816
3817 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3818
3819 for (unsigned i = 0; i < write_count; i++) {
3820 aco_opcode op = opcodes[i];
3821 if (op == aco_opcode::num_opcodes)
3822 continue;
3823
3824 Temp data = write_datas[i];
3825
3826 unsigned second = write_count;
3827 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3828 for (second = i + 1; second < write_count; second++) {
3829 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3830 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3831 opcodes[second] = aco_opcode::num_opcodes;
3832 break;
3833 }
3834 }
3835 }
3836
3837 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3838 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3839
3840 unsigned inline_offset = base_offset + offsets[i];
3841 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3842 Temp address_offset = address;
3843 if (inline_offset > max_offset) {
3844 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3845 inline_offset = offsets[i];
3846 }
3847 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3848
3849 Instruction *instr;
3850 if (write2) {
3851 Temp second_data = write_datas[second];
3852 inline_offset /= data.bytes();
3853 instr = bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3854 } else {
3855 instr = bld.ds(op, address_offset, data, m, inline_offset);
3856 }
3857 static_cast<DS_instruction *>(instr)->sync =
3858 memory_sync_info(storage_shared);
3859 }
3860 }
3861
3862 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3863 {
3864 unsigned align = 16;
3865 if (const_offset)
3866 align = std::min(align, 1u << (ffs(const_offset) - 1));
3867
3868 return align;
3869 }
3870
3871
3872 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3873 {
3874 switch (bytes) {
3875 case 1:
3876 assert(!smem);
3877 return aco_opcode::buffer_store_byte;
3878 case 2:
3879 assert(!smem);
3880 return aco_opcode::buffer_store_short;
3881 case 4:
3882 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3883 case 8:
3884 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3885 case 12:
3886 assert(!smem);
3887 return aco_opcode::buffer_store_dwordx3;
3888 case 16:
3889 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3890 }
3891 unreachable("Unexpected store size");
3892 return aco_opcode::num_opcodes;
3893 }
3894
3895 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3896 Temp data, unsigned writemask, int swizzle_element_size,
3897 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3898 {
3899 unsigned write_count_with_skips = 0;
3900 bool skips[16];
3901
3902 /* determine how to split the data */
3903 unsigned todo = u_bit_consecutive(0, data.bytes());
3904 while (todo) {
3905 int offset, bytes;
3906 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3907 offsets[write_count_with_skips] = offset;
3908 if (skips[write_count_with_skips]) {
3909 advance_write_mask(&todo, offset, bytes);
3910 write_count_with_skips++;
3911 continue;
3912 }
3913
3914 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3915 * larger than swizzle_element_size */
3916 bytes = MIN2(bytes, swizzle_element_size);
3917 if (bytes % 4)
3918 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3919
3920 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3921 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3922 bytes = 8;
3923
3924 /* dword or larger stores have to be dword-aligned */
3925 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3926 unsigned align_offset = (instr ? nir_intrinsic_align_offset(instr) : 0) + offset;
3927 bool dword_aligned = align_offset % 4 == 0 && align_mul % 4 == 0;
3928 if (!dword_aligned)
3929 bytes = MIN2(bytes, (align_offset % 2 == 0 && align_mul % 2 == 0) ? 2 : 1);
3930
3931 advance_write_mask(&todo, offset, bytes);
3932 write_count_with_skips++;
3933 }
3934
3935 /* actually split data */
3936 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3937
3938 /* remove skips */
3939 for (unsigned i = 0; i < write_count_with_skips; i++) {
3940 if (skips[i])
3941 continue;
3942 write_datas[*write_count] = write_datas[i];
3943 offsets[*write_count] = offsets[i];
3944 (*write_count)++;
3945 }
3946 }
3947
3948 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3949 unsigned split_cnt = 0u, Temp dst = Temp())
3950 {
3951 Builder bld(ctx->program, ctx->block);
3952 unsigned dword_size = elem_size_bytes / 4;
3953
3954 if (!dst.id())
3955 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3956
3957 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3958 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3959 instr->definitions[0] = Definition(dst);
3960
3961 for (unsigned i = 0; i < cnt; ++i) {
3962 if (arr[i].id()) {
3963 assert(arr[i].size() == dword_size);
3964 allocated_vec[i] = arr[i];
3965 instr->operands[i] = Operand(arr[i]);
3966 } else {
3967 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3968 allocated_vec[i] = zero;
3969 instr->operands[i] = Operand(zero);
3970 }
3971 }
3972
3973 bld.insert(std::move(instr));
3974
3975 if (split_cnt)
3976 emit_split_vector(ctx, dst, split_cnt);
3977 else
3978 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3979
3980 return dst;
3981 }
3982
3983 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3984 {
3985 if (const_offset >= 4096) {
3986 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3987 const_offset %= 4096u;
3988
3989 if (!voffset.id())
3990 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3991 else if (unlikely(voffset.regClass() == s1))
3992 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3993 else if (likely(voffset.regClass() == v1))
3994 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3995 else
3996 unreachable("Unsupported register class of voffset");
3997 }
3998
3999 return const_offset;
4000 }
4001
4002 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
4003 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false,
4004 bool swizzled = false)
4005 {
4006 assert(vdata.id());
4007 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
4008 assert(vdata.size() >= 1 && vdata.size() <= 4);
4009
4010 Builder bld(ctx->program, ctx->block);
4011 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
4012 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
4013
4014 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
4015 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
4016 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
4017 /* offen */ !voffset_op.isUndefined(), /* swizzled */ swizzled,
4018 /* idxen*/ false, /* addr64 */ false, /* disable_wqm */ false, /* glc */ true,
4019 /* dlc*/ false, /* slc */ slc);
4020
4021 if (!allow_reorder)
4022 static_cast<MUBUF_instruction *>(r.instr)->sync = memory_sync_info(storage_buffer, semantic_private);
4023 }
4024
4025 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
4026 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
4027 bool allow_combining = true, bool reorder = true, bool slc = false)
4028 {
4029 Builder bld(ctx->program, ctx->block);
4030 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4031 assert(write_mask);
4032 write_mask = widen_mask(write_mask, elem_size_bytes);
4033
4034 unsigned write_count = 0;
4035 Temp write_datas[32];
4036 unsigned offsets[32];
4037 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
4038 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
4039
4040 for (unsigned i = 0; i < write_count; i++) {
4041 unsigned const_offset = offsets[i] + base_const_offset;
4042 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc, !allow_combining);
4043 }
4044 }
4045
4046 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4047 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4048 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4049 {
4050 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4051 assert((num_components * elem_size_bytes) == dst.bytes());
4052 assert(!!stride != allow_combining);
4053
4054 Builder bld(ctx->program, ctx->block);
4055
4056 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4057 info.component_stride = allow_combining ? 0 : stride;
4058 info.glc = true;
4059 info.swizzle_component_size = allow_combining ? 0 : 4;
4060 info.align_mul = MIN2(elem_size_bytes, 4);
4061 info.align_offset = 0;
4062 info.soffset = soffset;
4063 info.const_offset = base_const_offset;
4064 emit_mubuf_load(ctx, bld, &info);
4065 }
4066
4067 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)
4068 {
4069 Builder bld(ctx->program, ctx->block);
4070 Temp offset = base_offset.first;
4071 unsigned const_offset = base_offset.second;
4072
4073 if (!nir_src_is_const(*off_src)) {
4074 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4075 Temp with_stride;
4076
4077 /* Calculate indirect offset with stride */
4078 if (likely(indirect_offset_arg.regClass() == v1))
4079 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4080 else if (indirect_offset_arg.regClass() == s1)
4081 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4082 else
4083 unreachable("Unsupported register class of indirect offset");
4084
4085 /* Add to the supplied base offset */
4086 if (offset.id() == 0)
4087 offset = with_stride;
4088 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4089 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4090 else if (offset.size() == 1 && with_stride.size() == 1)
4091 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4092 else
4093 unreachable("Unsupported register class of indirect offset");
4094 } else {
4095 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4096 const_offset += const_offset_arg * stride;
4097 }
4098
4099 return std::make_pair(offset, const_offset);
4100 }
4101
4102 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4103 {
4104 Builder bld(ctx->program, ctx->block);
4105 Temp offset;
4106
4107 if (off1.first.id() && off2.first.id()) {
4108 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4109 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4110 else if (off1.first.size() == 1 && off2.first.size() == 1)
4111 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4112 else
4113 unreachable("Unsupported register class of indirect offset");
4114 } else {
4115 offset = off1.first.id() ? off1.first : off2.first;
4116 }
4117
4118 return std::make_pair(offset, off1.second + off2.second);
4119 }
4120
4121 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4122 {
4123 Builder bld(ctx->program, ctx->block);
4124 unsigned const_offset = offs.second * multiplier;
4125
4126 if (!offs.first.id())
4127 return std::make_pair(offs.first, const_offset);
4128
4129 Temp offset = unlikely(offs.first.regClass() == s1)
4130 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4131 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4132
4133 return std::make_pair(offset, const_offset);
4134 }
4135
4136 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4137 {
4138 Builder bld(ctx->program, ctx->block);
4139
4140 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4141 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4142 /* component is in bytes */
4143 const_offset += nir_intrinsic_component(instr) * component_stride;
4144
4145 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4146 nir_src *off_src = nir_get_io_offset_src(instr);
4147 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4148 }
4149
4150 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4151 {
4152 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4153 }
4154
4155 Temp get_tess_rel_patch_id(isel_context *ctx)
4156 {
4157 Builder bld(ctx->program, ctx->block);
4158
4159 switch (ctx->shader->info.stage) {
4160 case MESA_SHADER_TESS_CTRL:
4161 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4162 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4163 case MESA_SHADER_TESS_EVAL:
4164 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4165 default:
4166 unreachable("Unsupported stage in get_tess_rel_patch_id");
4167 }
4168 }
4169
4170 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4171 {
4172 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4173 Builder bld(ctx->program, ctx->block);
4174
4175 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4176 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4177
4178 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4179
4180 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4181 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4182
4183 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4184 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4185 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4186
4187 return offset_mul(ctx, offs, 4u);
4188 }
4189
4190 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4191 {
4192 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4193 Builder bld(ctx->program, ctx->block);
4194
4195 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4196 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4197 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4198 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4199
4200 std::pair<Temp, unsigned> offs = instr
4201 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4202 : std::make_pair(Temp(), 0u);
4203
4204 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4205 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4206
4207 if (per_vertex) {
4208 assert(instr);
4209
4210 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4211 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4212
4213 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4214 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4215 } else {
4216 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4217 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4218 }
4219
4220 return offs;
4221 }
4222
4223 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4224 {
4225 Builder bld(ctx->program, ctx->block);
4226
4227 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4228 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4229
4230 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4231
4232 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4233 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4234 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4235
4236 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4237 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4238
4239 return offs;
4240 }
4241
4242 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4243 {
4244 Builder bld(ctx->program, ctx->block);
4245
4246 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4247 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4248 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4249 unsigned attr_stride = ctx->tcs_num_patches;
4250
4251 std::pair<Temp, unsigned> offs = instr
4252 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4253 : std::make_pair(Temp(), 0u);
4254
4255 if (const_base_offset)
4256 offs.second += const_base_offset * attr_stride;
4257
4258 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4259 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4260 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4261
4262 return offs;
4263 }
4264
4265 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4266 {
4267 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4268
4269 if (mask == 0)
4270 return false;
4271
4272 unsigned drv_loc = nir_intrinsic_base(instr);
4273 nir_src *off_src = nir_get_io_offset_src(instr);
4274
4275 if (!nir_src_is_const(*off_src)) {
4276 *indirect = true;
4277 return false;
4278 }
4279
4280 *indirect = false;
4281 uint64_t slot = per_vertex
4282 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4283 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4284 return (((uint64_t) 1) << slot) & mask;
4285 }
4286
4287 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4288 {
4289 unsigned write_mask = nir_intrinsic_write_mask(instr);
4290 unsigned component = nir_intrinsic_component(instr);
4291 unsigned idx = nir_intrinsic_base(instr) + component;
4292
4293 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4294 if (off_instr->type != nir_instr_type_load_const)
4295 return false;
4296
4297 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4298 idx += nir_src_as_uint(instr->src[1]) * 4u;
4299
4300 if (instr->src[0].ssa->bit_size == 64)
4301 write_mask = widen_mask(write_mask, 2);
4302
4303 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4304
4305 for (unsigned i = 0; i < 8; ++i) {
4306 if (write_mask & (1 << i)) {
4307 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4308 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4309 }
4310 idx++;
4311 }
4312
4313 return true;
4314 }
4315
4316 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4317 {
4318 /* Only TCS per-vertex inputs are supported by this function.
4319 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4320 */
4321 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4322 return false;
4323
4324 nir_src *off_src = nir_get_io_offset_src(instr);
4325 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4326 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4327 bool can_use_temps = nir_src_is_const(*off_src) &&
4328 vertex_index_instr->type == nir_instr_type_intrinsic &&
4329 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4330
4331 if (!can_use_temps)
4332 return false;
4333
4334 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4335 Temp *src = &ctx->inputs.temps[idx];
4336 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4337
4338 return true;
4339 }
4340
4341 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4342 {
4343 Builder bld(ctx->program, ctx->block);
4344
4345 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4346 /* 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. */
4347 bool indirect_write;
4348 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4349 if (temp_only_input && !indirect_write)
4350 return;
4351 }
4352
4353 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4354 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4355 unsigned write_mask = nir_intrinsic_write_mask(instr);
4356 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4357
4358 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4359 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4360 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4361 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4362 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4363 } else {
4364 Temp lds_base;
4365
4366 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4367 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4368 unsigned itemsize = ctx->stage == vertex_geometry_gs
4369 ? ctx->program->info->vs.es_info.esgs_itemsize
4370 : ctx->program->info->tes.es_info.esgs_itemsize;
4371 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4372 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));
4373 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4374 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4375 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4376 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4377 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4378 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4379 */
4380 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4381 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4382 } else {
4383 unreachable("Invalid LS or ES stage");
4384 }
4385
4386 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4387 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4388 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4389 }
4390 }
4391
4392 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4393 {
4394 if (per_vertex)
4395 return false;
4396
4397 unsigned off = nir_intrinsic_base(instr) * 4u;
4398 return off == ctx->tcs_tess_lvl_out_loc ||
4399 off == ctx->tcs_tess_lvl_in_loc;
4400
4401 }
4402
4403 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4404 {
4405 uint64_t mask = per_vertex
4406 ? ctx->program->info->tcs.tes_inputs_read
4407 : ctx->program->info->tcs.tes_patch_inputs_read;
4408
4409 bool indirect_write = false;
4410 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4411 return indirect_write || output_read_by_tes;
4412 }
4413
4414 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4415 {
4416 uint64_t mask = per_vertex
4417 ? ctx->shader->info.outputs_read
4418 : ctx->shader->info.patch_outputs_read;
4419
4420 bool indirect_write = false;
4421 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4422 return indirect_write || output_read;
4423 }
4424
4425 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4426 {
4427 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4428 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4429
4430 Builder bld(ctx->program, ctx->block);
4431
4432 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4433 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4434 unsigned write_mask = nir_intrinsic_write_mask(instr);
4435
4436 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4437 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4438 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4439
4440 if (write_to_vmem) {
4441 std::pair<Temp, unsigned> vmem_offs = per_vertex
4442 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4443 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4444
4445 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));
4446 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4447 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);
4448 }
4449
4450 if (write_to_lds) {
4451 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4452 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4453 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4454 }
4455 }
4456
4457 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4458 {
4459 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4460 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4461
4462 Builder bld(ctx->program, ctx->block);
4463
4464 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4465 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4466 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4467 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4468
4469 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4470 }
4471
4472 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4473 {
4474 if (ctx->stage == vertex_vs ||
4475 ctx->stage == tess_eval_vs ||
4476 ctx->stage == fragment_fs ||
4477 ctx->stage == ngg_vertex_gs ||
4478 ctx->stage == ngg_tess_eval_gs ||
4479 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4480 bool stored_to_temps = store_output_to_temps(ctx, instr);
4481 if (!stored_to_temps) {
4482 fprintf(stderr, "Unimplemented output offset instruction:\n");
4483 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4484 fprintf(stderr, "\n");
4485 abort();
4486 }
4487 } else if (ctx->stage == vertex_es ||
4488 ctx->stage == vertex_ls ||
4489 ctx->stage == tess_eval_es ||
4490 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4491 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4492 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4493 visit_store_ls_or_es_output(ctx, instr);
4494 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4495 visit_store_tcs_output(ctx, instr, false);
4496 } else {
4497 unreachable("Shader stage not implemented");
4498 }
4499 }
4500
4501 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4502 {
4503 visit_load_tcs_output(ctx, instr, false);
4504 }
4505
4506 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4507 {
4508 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4509 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4510
4511 Builder bld(ctx->program, ctx->block);
4512
4513 if (dst.regClass() == v2b) {
4514 if (ctx->program->has_16bank_lds) {
4515 assert(ctx->options->chip_class <= GFX8);
4516 Builder::Result interp_p1 =
4517 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4518 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4519 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4520 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4521 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4522 bld.m0(prim_mask), interp_p1, idx, component);
4523 } else {
4524 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4525
4526 if (ctx->options->chip_class == GFX8)
4527 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4528
4529 Builder::Result interp_p1 =
4530 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4531 coord1, bld.m0(prim_mask), idx, component);
4532 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4533 interp_p1, idx, component);
4534 }
4535 } else {
4536 Builder::Result interp_p1 =
4537 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4538 bld.m0(prim_mask), idx, component);
4539
4540 if (ctx->program->has_16bank_lds)
4541 interp_p1.instr->operands[0].setLateKill(true);
4542
4543 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4544 bld.m0(prim_mask), interp_p1, idx, component);
4545 }
4546 }
4547
4548 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4549 {
4550 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4551 for (unsigned i = 0; i < num_components; i++)
4552 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4553 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4554 assert(num_components == 4);
4555 Builder bld(ctx->program, ctx->block);
4556 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4557 }
4558
4559 for (Operand& op : vec->operands)
4560 op = op.isUndefined() ? Operand(0u) : op;
4561
4562 vec->definitions[0] = Definition(dst);
4563 ctx->block->instructions.emplace_back(std::move(vec));
4564 emit_split_vector(ctx, dst, num_components);
4565 return;
4566 }
4567
4568 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4569 {
4570 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4571 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4572 unsigned idx = nir_intrinsic_base(instr);
4573 unsigned component = nir_intrinsic_component(instr);
4574 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4575
4576 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4577 if (offset) {
4578 assert(offset->u32 == 0);
4579 } else {
4580 /* the lower 15bit of the prim_mask contain the offset into LDS
4581 * while the upper bits contain the number of prims */
4582 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4583 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4584 Builder bld(ctx->program, ctx->block);
4585 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4586 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4587 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4588 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4589 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4590 }
4591
4592 if (instr->dest.ssa.num_components == 1) {
4593 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4594 } else {
4595 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4596 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4597 {
4598 Temp tmp = {ctx->program->allocateId(), v1};
4599 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4600 vec->operands[i] = Operand(tmp);
4601 }
4602 vec->definitions[0] = Definition(dst);
4603 ctx->block->instructions.emplace_back(std::move(vec));
4604 }
4605 }
4606
4607 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4608 unsigned offset, unsigned stride, unsigned channels)
4609 {
4610 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4611 if (vtx_info->chan_byte_size != 4 && channels == 3)
4612 return false;
4613 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4614 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4615 }
4616
4617 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4618 unsigned offset, unsigned stride, unsigned *channels)
4619 {
4620 if (!vtx_info->chan_byte_size) {
4621 *channels = vtx_info->num_channels;
4622 return vtx_info->chan_format;
4623 }
4624
4625 unsigned num_channels = *channels;
4626 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4627 unsigned new_channels = num_channels + 1;
4628 /* first, assume more loads is worse and try using a larger data format */
4629 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4630 new_channels++;
4631 /* don't make the attribute potentially out-of-bounds */
4632 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4633 new_channels = 5;
4634 }
4635
4636 if (new_channels == 5) {
4637 /* then try decreasing load size (at the cost of more loads) */
4638 new_channels = *channels;
4639 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4640 new_channels--;
4641 }
4642
4643 if (new_channels < *channels)
4644 *channels = new_channels;
4645 num_channels = new_channels;
4646 }
4647
4648 switch (vtx_info->chan_format) {
4649 case V_008F0C_BUF_DATA_FORMAT_8:
4650 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4651 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4652 case V_008F0C_BUF_DATA_FORMAT_16:
4653 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4654 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4655 case V_008F0C_BUF_DATA_FORMAT_32:
4656 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4657 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4658 }
4659 unreachable("shouldn't reach here");
4660 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4661 }
4662
4663 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4664 * so we may need to fix it up. */
4665 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4666 {
4667 Builder bld(ctx->program, ctx->block);
4668
4669 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4670 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4671
4672 /* For the integer-like cases, do a natural sign extension.
4673 *
4674 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4675 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4676 * exponent.
4677 */
4678 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4679 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4680
4681 /* Convert back to the right type. */
4682 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4683 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4684 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4685 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4686 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4687 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4688 }
4689
4690 return alpha;
4691 }
4692
4693 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4694 {
4695 Builder bld(ctx->program, ctx->block);
4696 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4697 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4698
4699 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4700 if (off_instr->type != nir_instr_type_load_const) {
4701 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4702 nir_print_instr(off_instr, stderr);
4703 fprintf(stderr, "\n");
4704 }
4705 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4706
4707 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4708
4709 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4710 unsigned component = nir_intrinsic_component(instr);
4711 unsigned bitsize = instr->dest.ssa.bit_size;
4712 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4713 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4714 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4715 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4716
4717 unsigned dfmt = attrib_format & 0xf;
4718 unsigned nfmt = (attrib_format >> 4) & 0x7;
4719 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4720
4721 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4722 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4723 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4724 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4725 if (post_shuffle)
4726 num_channels = MAX2(num_channels, 3);
4727
4728 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4729 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4730
4731 Temp index;
4732 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4733 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4734 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4735 if (divisor) {
4736 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4737 if (divisor != 1) {
4738 Temp divided = bld.tmp(v1);
4739 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4740 index = bld.vadd32(bld.def(v1), start_instance, divided);
4741 } else {
4742 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4743 }
4744 } else {
4745 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4746 }
4747 } else {
4748 index = bld.vadd32(bld.def(v1),
4749 get_arg(ctx, ctx->args->ac.base_vertex),
4750 get_arg(ctx, ctx->args->ac.vertex_id));
4751 }
4752
4753 Temp channels[num_channels];
4754 unsigned channel_start = 0;
4755 bool direct_fetch = false;
4756
4757 /* skip unused channels at the start */
4758 if (vtx_info->chan_byte_size && !post_shuffle) {
4759 channel_start = ffs(mask) - 1;
4760 for (unsigned i = 0; i < channel_start; i++)
4761 channels[i] = Temp(0, s1);
4762 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4763 num_channels = 3 - (ffs(mask) - 1);
4764 }
4765
4766 /* load channels */
4767 while (channel_start < num_channels) {
4768 unsigned fetch_component = num_channels - channel_start;
4769 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4770 bool expanded = false;
4771
4772 /* use MUBUF when possible to avoid possible alignment issues */
4773 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4774 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4775 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4776 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4777 vtx_info->chan_byte_size == 4;
4778 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4779 if (!use_mubuf) {
4780 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4781 } else {
4782 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4783 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4784 fetch_component = 4;
4785 expanded = true;
4786 }
4787 }
4788
4789 unsigned fetch_bytes = fetch_component * bitsize / 8;
4790
4791 Temp fetch_index = index;
4792 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4793 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4794 fetch_offset = fetch_offset % attrib_stride;
4795 }
4796
4797 Operand soffset(0u);
4798 if (fetch_offset >= 4096) {
4799 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4800 fetch_offset %= 4096;
4801 }
4802
4803 aco_opcode opcode;
4804 switch (fetch_bytes) {
4805 case 2:
4806 assert(!use_mubuf && bitsize == 16);
4807 opcode = aco_opcode::tbuffer_load_format_d16_x;
4808 break;
4809 case 4:
4810 if (bitsize == 16) {
4811 assert(!use_mubuf);
4812 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4813 } else {
4814 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4815 }
4816 break;
4817 case 6:
4818 assert(!use_mubuf && bitsize == 16);
4819 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4820 break;
4821 case 8:
4822 if (bitsize == 16) {
4823 assert(!use_mubuf);
4824 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4825 } else {
4826 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4827 }
4828 break;
4829 case 12:
4830 assert(ctx->options->chip_class >= GFX7 ||
4831 (!use_mubuf && ctx->options->chip_class == GFX6));
4832 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4833 break;
4834 case 16:
4835 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4836 break;
4837 default:
4838 unreachable("Unimplemented load_input vector size");
4839 }
4840
4841 Temp fetch_dst;
4842 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4843 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4844 num_channels <= 3)) {
4845 direct_fetch = true;
4846 fetch_dst = dst;
4847 } else {
4848 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4849 }
4850
4851 if (use_mubuf) {
4852 bld.mubuf(opcode,
4853 Definition(fetch_dst), list, fetch_index, soffset,
4854 fetch_offset, false, false, true).instr;
4855 } else {
4856 bld.mtbuf(opcode,
4857 Definition(fetch_dst), list, fetch_index, soffset,
4858 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4859 }
4860
4861 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4862
4863 if (fetch_component == 1) {
4864 channels[channel_start] = fetch_dst;
4865 } else {
4866 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4867 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4868 bitsize == 16 ? v2b : v1);
4869 }
4870
4871 channel_start += fetch_component;
4872 }
4873
4874 if (!direct_fetch) {
4875 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4876 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4877
4878 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4879 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4880 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4881
4882 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4883 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4884 unsigned num_temp = 0;
4885 for (unsigned i = 0; i < dst.size(); i++) {
4886 unsigned idx = i + component;
4887 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4888 Temp channel = channels[swizzle[idx]];
4889 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4890 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4891 vec->operands[i] = Operand(channel);
4892
4893 num_temp++;
4894 elems[i] = channel;
4895 } else if (is_float && idx == 3) {
4896 vec->operands[i] = Operand(0x3f800000u);
4897 } else if (!is_float && idx == 3) {
4898 vec->operands[i] = Operand(1u);
4899 } else {
4900 vec->operands[i] = Operand(0u);
4901 }
4902 }
4903 vec->definitions[0] = Definition(dst);
4904 ctx->block->instructions.emplace_back(std::move(vec));
4905 emit_split_vector(ctx, dst, dst.size());
4906
4907 if (num_temp == dst.size())
4908 ctx->allocated_vec.emplace(dst.id(), elems);
4909 }
4910 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4911 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4912 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4913 if (off_instr->type != nir_instr_type_load_const ||
4914 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4915 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4916 nir_print_instr(off_instr, stderr);
4917 fprintf(stderr, "\n");
4918 }
4919
4920 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4921 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4922 if (offset) {
4923 assert(offset->u32 == 0);
4924 } else {
4925 /* the lower 15bit of the prim_mask contain the offset into LDS
4926 * while the upper bits contain the number of prims */
4927 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4928 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4929 Builder bld(ctx->program, ctx->block);
4930 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4931 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4932 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4933 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4934 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4935 }
4936
4937 unsigned idx = nir_intrinsic_base(instr);
4938 unsigned component = nir_intrinsic_component(instr);
4939 unsigned vertex_id = 2; /* P0 */
4940
4941 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4942 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4943 switch (src0->u32) {
4944 case 0:
4945 vertex_id = 2; /* P0 */
4946 break;
4947 case 1:
4948 vertex_id = 0; /* P10 */
4949 break;
4950 case 2:
4951 vertex_id = 1; /* P20 */
4952 break;
4953 default:
4954 unreachable("invalid vertex index");
4955 }
4956 }
4957
4958 if (dst.size() == 1) {
4959 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4960 } else {
4961 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4962 for (unsigned i = 0; i < dst.size(); i++)
4963 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4964 vec->definitions[0] = Definition(dst);
4965 bld.insert(std::move(vec));
4966 }
4967
4968 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4969 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4970 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4971 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4972 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4973
4974 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4975 } else {
4976 unreachable("Shader stage not implemented");
4977 }
4978 }
4979
4980 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4981 {
4982 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4983
4984 Builder bld(ctx->program, ctx->block);
4985 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4986 Temp vertex_offset;
4987
4988 if (!nir_src_is_const(*vertex_src)) {
4989 /* better code could be created, but this case probably doesn't happen
4990 * much in practice */
4991 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4992 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4993 Temp elem;
4994
4995 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4996 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4997 if (i % 2u)
4998 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4999 } else {
5000 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
5001 }
5002
5003 if (vertex_offset.id()) {
5004 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
5005 Operand(i), indirect_vertex);
5006 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
5007 } else {
5008 vertex_offset = elem;
5009 }
5010 }
5011
5012 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5013 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
5014 } else {
5015 unsigned vertex = nir_src_as_uint(*vertex_src);
5016 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5017 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5018 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
5019 Operand((vertex % 2u) * 16u), Operand(16u));
5020 else
5021 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
5022 }
5023
5024 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
5025 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
5026 return offset_mul(ctx, offs, 4u);
5027 }
5028
5029 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5030 {
5031 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
5032
5033 Builder bld(ctx->program, ctx->block);
5034 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5035 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5036
5037 if (ctx->stage == geometry_gs) {
5038 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
5039 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
5040 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);
5041 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
5042 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
5043 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5044 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5045 } else {
5046 unreachable("Unsupported GS stage.");
5047 }
5048 }
5049
5050 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5051 {
5052 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5053
5054 Builder bld(ctx->program, ctx->block);
5055 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5056
5057 if (load_input_from_temps(ctx, instr, dst))
5058 return;
5059
5060 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
5061 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5062 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5063
5064 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5065 }
5066
5067 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5068 {
5069 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5070
5071 Builder bld(ctx->program, ctx->block);
5072
5073 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5074 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
5075 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5076
5077 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5078 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5079
5080 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5081 }
5082
5083 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5084 {
5085 switch (ctx->shader->info.stage) {
5086 case MESA_SHADER_GEOMETRY:
5087 visit_load_gs_per_vertex_input(ctx, instr);
5088 break;
5089 case MESA_SHADER_TESS_CTRL:
5090 visit_load_tcs_per_vertex_input(ctx, instr);
5091 break;
5092 case MESA_SHADER_TESS_EVAL:
5093 visit_load_tes_per_vertex_input(ctx, instr);
5094 break;
5095 default:
5096 unreachable("Unimplemented shader stage");
5097 }
5098 }
5099
5100 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5101 {
5102 visit_load_tcs_output(ctx, instr, true);
5103 }
5104
5105 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5106 {
5107 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5108 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5109
5110 visit_store_tcs_output(ctx, instr, true);
5111 }
5112
5113 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5114 {
5115 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5116
5117 Builder bld(ctx->program, ctx->block);
5118 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5119
5120 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5121 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5122 Operand tes_w(0u);
5123
5124 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5125 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5126 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5127 tes_w = Operand(tmp);
5128 }
5129
5130 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5131 emit_split_vector(ctx, tess_coord, 3);
5132 }
5133
5134 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5135 {
5136 if (ctx->program->info->need_indirect_descriptor_sets) {
5137 Builder bld(ctx->program, ctx->block);
5138 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5139 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5140 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5141 }
5142
5143 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5144 }
5145
5146
5147 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5148 {
5149 Builder bld(ctx->program, ctx->block);
5150 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5151 if (!nir_dest_is_divergent(instr->dest))
5152 index = bld.as_uniform(index);
5153 unsigned desc_set = nir_intrinsic_desc_set(instr);
5154 unsigned binding = nir_intrinsic_binding(instr);
5155
5156 Temp desc_ptr;
5157 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5158 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5159 unsigned offset = layout->binding[binding].offset;
5160 unsigned stride;
5161 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5162 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5163 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5164 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5165 offset = pipeline_layout->push_constant_size + 16 * idx;
5166 stride = 16;
5167 } else {
5168 desc_ptr = load_desc_ptr(ctx, desc_set);
5169 stride = layout->binding[binding].size;
5170 }
5171
5172 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5173 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5174 if (stride != 1) {
5175 if (nir_const_index) {
5176 const_index = const_index * stride;
5177 } else if (index.type() == RegType::vgpr) {
5178 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5179 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5180 } else {
5181 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5182 }
5183 }
5184 if (offset) {
5185 if (nir_const_index) {
5186 const_index = const_index + offset;
5187 } else if (index.type() == RegType::vgpr) {
5188 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5189 } else {
5190 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5191 }
5192 }
5193
5194 if (nir_const_index && const_index == 0) {
5195 index = desc_ptr;
5196 } else if (index.type() == RegType::vgpr) {
5197 index = bld.vadd32(bld.def(v1),
5198 nir_const_index ? Operand(const_index) : Operand(index),
5199 Operand(desc_ptr));
5200 } else {
5201 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5202 nir_const_index ? Operand(const_index) : Operand(index),
5203 Operand(desc_ptr));
5204 }
5205
5206 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5207 }
5208
5209 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5210 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5211 bool glc=false, bool allow_smem=true, memory_sync_info sync=memory_sync_info())
5212 {
5213 Builder bld(ctx->program, ctx->block);
5214
5215 bool use_smem = dst.type() != RegType::vgpr && (!glc || ctx->options->chip_class >= GFX8) && allow_smem;
5216 if (use_smem)
5217 offset = bld.as_uniform(offset);
5218
5219 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5220 info.glc = glc;
5221 info.sync = sync;
5222 info.align_mul = align_mul;
5223 info.align_offset = align_offset;
5224 if (use_smem)
5225 emit_smem_load(ctx, bld, &info);
5226 else
5227 emit_mubuf_load(ctx, bld, &info);
5228 }
5229
5230 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5231 {
5232 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5233 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5234
5235 Builder bld(ctx->program, ctx->block);
5236
5237 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5238 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5239 unsigned binding = nir_intrinsic_binding(idx_instr);
5240 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5241
5242 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5243 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5244 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5245 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5246 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5247 if (ctx->options->chip_class >= GFX10) {
5248 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5249 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5250 S_008F0C_RESOURCE_LEVEL(1);
5251 } else {
5252 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5253 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5254 }
5255 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5256 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5257 Operand(0xFFFFFFFFu),
5258 Operand(desc_type));
5259 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5260 rsrc, upper_dwords);
5261 } else {
5262 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5263 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5264 }
5265 unsigned size = instr->dest.ssa.bit_size / 8;
5266 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5267 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5268 }
5269
5270 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5271 {
5272 Builder bld(ctx->program, ctx->block);
5273 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5274 unsigned offset = nir_intrinsic_base(instr);
5275 unsigned count = instr->dest.ssa.num_components;
5276 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5277
5278 if (index_cv && instr->dest.ssa.bit_size == 32) {
5279 unsigned start = (offset + index_cv->u32) / 4u;
5280 start -= ctx->args->ac.base_inline_push_consts;
5281 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5282 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5283 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5284 for (unsigned i = 0; i < count; ++i) {
5285 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5286 vec->operands[i] = Operand{elems[i]};
5287 }
5288 vec->definitions[0] = Definition(dst);
5289 ctx->block->instructions.emplace_back(std::move(vec));
5290 ctx->allocated_vec.emplace(dst.id(), elems);
5291 return;
5292 }
5293 }
5294
5295 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5296 if (offset != 0) // TODO check if index != 0 as well
5297 index = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5298 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5299 Temp vec = dst;
5300 bool trim = false;
5301 bool aligned = true;
5302
5303 if (instr->dest.ssa.bit_size == 8) {
5304 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5305 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5306 if (!aligned)
5307 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5308 } else if (instr->dest.ssa.bit_size == 16) {
5309 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5310 if (!aligned)
5311 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5312 }
5313
5314 aco_opcode op;
5315
5316 switch (vec.size()) {
5317 case 1:
5318 op = aco_opcode::s_load_dword;
5319 break;
5320 case 2:
5321 op = aco_opcode::s_load_dwordx2;
5322 break;
5323 case 3:
5324 vec = bld.tmp(s4);
5325 trim = true;
5326 case 4:
5327 op = aco_opcode::s_load_dwordx4;
5328 break;
5329 case 6:
5330 vec = bld.tmp(s8);
5331 trim = true;
5332 case 8:
5333 op = aco_opcode::s_load_dwordx8;
5334 break;
5335 default:
5336 unreachable("unimplemented or forbidden load_push_constant.");
5337 }
5338
5339 static_cast<SMEM_instruction*>(bld.smem(op, Definition(vec), ptr, index).instr)->prevent_overflow = true;
5340
5341 if (!aligned) {
5342 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5343 byte_align_scalar(ctx, vec, byte_offset, dst);
5344 return;
5345 }
5346
5347 if (trim) {
5348 emit_split_vector(ctx, vec, 4);
5349 RegClass rc = dst.size() == 3 ? s1 : s2;
5350 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5351 emit_extract_vector(ctx, vec, 0, rc),
5352 emit_extract_vector(ctx, vec, 1, rc),
5353 emit_extract_vector(ctx, vec, 2, rc));
5354
5355 }
5356 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5357 }
5358
5359 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5360 {
5361 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5362
5363 Builder bld(ctx->program, ctx->block);
5364
5365 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5366 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5367 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5368 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5369 if (ctx->options->chip_class >= GFX10) {
5370 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5371 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5372 S_008F0C_RESOURCE_LEVEL(1);
5373 } else {
5374 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5375 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5376 }
5377
5378 unsigned base = nir_intrinsic_base(instr);
5379 unsigned range = nir_intrinsic_range(instr);
5380
5381 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5382 if (base && offset.type() == RegType::sgpr)
5383 offset = bld.nuw().sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5384 else if (base && offset.type() == RegType::vgpr)
5385 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5386
5387 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5388 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5389 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5390 Operand(desc_type));
5391 unsigned size = instr->dest.ssa.bit_size / 8;
5392 // TODO: get alignment information for subdword constants
5393 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5394 }
5395
5396 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5397 {
5398 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5399 ctx->cf_info.exec_potentially_empty_discard = true;
5400
5401 ctx->program->needs_exact = true;
5402
5403 // TODO: optimize uniform conditions
5404 Builder bld(ctx->program, ctx->block);
5405 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5406 assert(src.regClass() == bld.lm);
5407 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5408 bld.pseudo(aco_opcode::p_discard_if, src);
5409 ctx->block->kind |= block_kind_uses_discard_if;
5410 return;
5411 }
5412
5413 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5414 {
5415 Builder bld(ctx->program, ctx->block);
5416
5417 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5418 ctx->cf_info.exec_potentially_empty_discard = true;
5419
5420 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5421 ctx->cf_info.parent_loop.has_divergent_continue;
5422
5423 if (ctx->block->loop_nest_depth &&
5424 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5425 /* we handle discards the same way as jump instructions */
5426 append_logical_end(ctx->block);
5427
5428 /* in loops, discard behaves like break */
5429 Block *linear_target = ctx->cf_info.parent_loop.exit;
5430 ctx->block->kind |= block_kind_discard;
5431
5432 if (!divergent) {
5433 /* uniform discard - loop ends here */
5434 assert(nir_instr_is_last(&instr->instr));
5435 ctx->block->kind |= block_kind_uniform;
5436 ctx->cf_info.has_branch = true;
5437 bld.branch(aco_opcode::p_branch);
5438 add_linear_edge(ctx->block->index, linear_target);
5439 return;
5440 }
5441
5442 /* we add a break right behind the discard() instructions */
5443 ctx->block->kind |= block_kind_break;
5444 unsigned idx = ctx->block->index;
5445
5446 ctx->cf_info.parent_loop.has_divergent_branch = true;
5447 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5448
5449 /* remove critical edges from linear CFG */
5450 bld.branch(aco_opcode::p_branch);
5451 Block* break_block = ctx->program->create_and_insert_block();
5452 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5453 break_block->kind |= block_kind_uniform;
5454 add_linear_edge(idx, break_block);
5455 add_linear_edge(break_block->index, linear_target);
5456 bld.reset(break_block);
5457 bld.branch(aco_opcode::p_branch);
5458
5459 Block* continue_block = ctx->program->create_and_insert_block();
5460 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5461 add_linear_edge(idx, continue_block);
5462 append_logical_start(continue_block);
5463 ctx->block = continue_block;
5464
5465 return;
5466 }
5467
5468 /* it can currently happen that NIR doesn't remove the unreachable code */
5469 if (!nir_instr_is_last(&instr->instr)) {
5470 ctx->program->needs_exact = true;
5471 /* save exec somewhere temporarily so that it doesn't get
5472 * overwritten before the discard from outer exec masks */
5473 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5474 bld.pseudo(aco_opcode::p_discard_if, cond);
5475 ctx->block->kind |= block_kind_uses_discard_if;
5476 return;
5477 }
5478
5479 /* This condition is incorrect for uniformly branched discards in a loop
5480 * predicated by a divergent condition, but the above code catches that case
5481 * and the discard would end up turning into a discard_if.
5482 * For example:
5483 * if (divergent) {
5484 * while (...) {
5485 * if (uniform) {
5486 * discard;
5487 * }
5488 * }
5489 * }
5490 */
5491 if (!ctx->cf_info.parent_if.is_divergent) {
5492 /* program just ends here */
5493 ctx->block->kind |= block_kind_uniform;
5494 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5495 0 /* enabled mask */, 9 /* dest */,
5496 false /* compressed */, true/* done */, true /* valid mask */);
5497 bld.sopp(aco_opcode::s_endpgm);
5498 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5499 } else {
5500 ctx->block->kind |= block_kind_discard;
5501 /* branch and linear edge is added by visit_if() */
5502 }
5503 }
5504
5505 enum aco_descriptor_type {
5506 ACO_DESC_IMAGE,
5507 ACO_DESC_FMASK,
5508 ACO_DESC_SAMPLER,
5509 ACO_DESC_BUFFER,
5510 ACO_DESC_PLANE_0,
5511 ACO_DESC_PLANE_1,
5512 ACO_DESC_PLANE_2,
5513 };
5514
5515 static bool
5516 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5517 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5518 return false;
5519 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5520 return dim == ac_image_cube ||
5521 dim == ac_image_1darray ||
5522 dim == ac_image_2darray ||
5523 dim == ac_image_2darraymsaa;
5524 }
5525
5526 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5527 enum aco_descriptor_type desc_type,
5528 const nir_tex_instr *tex_instr, bool image, bool write)
5529 {
5530 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5531 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5532 if (it != ctx->tex_desc.end())
5533 return it->second;
5534 */
5535 Temp index = Temp();
5536 bool index_set = false;
5537 unsigned constant_index = 0;
5538 unsigned descriptor_set;
5539 unsigned base_index;
5540 Builder bld(ctx->program, ctx->block);
5541
5542 if (!deref_instr) {
5543 assert(tex_instr && !image);
5544 descriptor_set = 0;
5545 base_index = tex_instr->sampler_index;
5546 } else {
5547 while(deref_instr->deref_type != nir_deref_type_var) {
5548 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5549 if (!array_size)
5550 array_size = 1;
5551
5552 assert(deref_instr->deref_type == nir_deref_type_array);
5553 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5554 if (const_value) {
5555 constant_index += array_size * const_value->u32;
5556 } else {
5557 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5558 if (indirect.type() == RegType::vgpr)
5559 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5560
5561 if (array_size != 1)
5562 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5563
5564 if (!index_set) {
5565 index = indirect;
5566 index_set = true;
5567 } else {
5568 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5569 }
5570 }
5571
5572 deref_instr = nir_src_as_deref(deref_instr->parent);
5573 }
5574 descriptor_set = deref_instr->var->data.descriptor_set;
5575 base_index = deref_instr->var->data.binding;
5576 }
5577
5578 Temp list = load_desc_ptr(ctx, descriptor_set);
5579 list = convert_pointer_to_64_bit(ctx, list);
5580
5581 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5582 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5583 unsigned offset = binding->offset;
5584 unsigned stride = binding->size;
5585 aco_opcode opcode;
5586 RegClass type;
5587
5588 assert(base_index < layout->binding_count);
5589
5590 switch (desc_type) {
5591 case ACO_DESC_IMAGE:
5592 type = s8;
5593 opcode = aco_opcode::s_load_dwordx8;
5594 break;
5595 case ACO_DESC_FMASK:
5596 type = s8;
5597 opcode = aco_opcode::s_load_dwordx8;
5598 offset += 32;
5599 break;
5600 case ACO_DESC_SAMPLER:
5601 type = s4;
5602 opcode = aco_opcode::s_load_dwordx4;
5603 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5604 offset += radv_combined_image_descriptor_sampler_offset(binding);
5605 break;
5606 case ACO_DESC_BUFFER:
5607 type = s4;
5608 opcode = aco_opcode::s_load_dwordx4;
5609 break;
5610 case ACO_DESC_PLANE_0:
5611 case ACO_DESC_PLANE_1:
5612 type = s8;
5613 opcode = aco_opcode::s_load_dwordx8;
5614 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5615 break;
5616 case ACO_DESC_PLANE_2:
5617 type = s4;
5618 opcode = aco_opcode::s_load_dwordx4;
5619 offset += 64;
5620 break;
5621 default:
5622 unreachable("invalid desc_type\n");
5623 }
5624
5625 offset += constant_index * stride;
5626
5627 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5628 (!index_set || binding->immutable_samplers_equal)) {
5629 if (binding->immutable_samplers_equal)
5630 constant_index = 0;
5631
5632 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5633 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5634 Operand(samplers[constant_index * 4 + 0]),
5635 Operand(samplers[constant_index * 4 + 1]),
5636 Operand(samplers[constant_index * 4 + 2]),
5637 Operand(samplers[constant_index * 4 + 3]));
5638 }
5639
5640 Operand off;
5641 if (!index_set) {
5642 off = bld.copy(bld.def(s1), Operand(offset));
5643 } else {
5644 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5645 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5646 }
5647
5648 Temp res = bld.smem(opcode, bld.def(type), list, off);
5649
5650 if (desc_type == ACO_DESC_PLANE_2) {
5651 Temp components[8];
5652 for (unsigned i = 0; i < 8; i++)
5653 components[i] = bld.tmp(s1);
5654 bld.pseudo(aco_opcode::p_split_vector,
5655 Definition(components[0]),
5656 Definition(components[1]),
5657 Definition(components[2]),
5658 Definition(components[3]),
5659 res);
5660
5661 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5662 bld.pseudo(aco_opcode::p_split_vector,
5663 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5664 Definition(components[4]),
5665 Definition(components[5]),
5666 Definition(components[6]),
5667 Definition(components[7]),
5668 desc2);
5669
5670 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5671 components[0], components[1], components[2], components[3],
5672 components[4], components[5], components[6], components[7]);
5673 }
5674
5675 return res;
5676 }
5677
5678 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5679 {
5680 switch (dim) {
5681 case GLSL_SAMPLER_DIM_BUF:
5682 return 1;
5683 case GLSL_SAMPLER_DIM_1D:
5684 return array ? 2 : 1;
5685 case GLSL_SAMPLER_DIM_2D:
5686 return array ? 3 : 2;
5687 case GLSL_SAMPLER_DIM_MS:
5688 return array ? 4 : 3;
5689 case GLSL_SAMPLER_DIM_3D:
5690 case GLSL_SAMPLER_DIM_CUBE:
5691 return 3;
5692 case GLSL_SAMPLER_DIM_RECT:
5693 case GLSL_SAMPLER_DIM_SUBPASS:
5694 return 2;
5695 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5696 return 3;
5697 default:
5698 break;
5699 }
5700 return 0;
5701 }
5702
5703
5704 /* Adjust the sample index according to FMASK.
5705 *
5706 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5707 * which is the identity mapping. Each nibble says which physical sample
5708 * should be fetched to get that sample.
5709 *
5710 * For example, 0x11111100 means there are only 2 samples stored and
5711 * the second sample covers 3/4 of the pixel. When reading samples 0
5712 * and 1, return physical sample 0 (determined by the first two 0s
5713 * in FMASK), otherwise return physical sample 1.
5714 *
5715 * The sample index should be adjusted as follows:
5716 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5717 */
5718 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5719 {
5720 Builder bld(ctx->program, ctx->block);
5721 Temp fmask = bld.tmp(v1);
5722 unsigned dim = ctx->options->chip_class >= GFX10
5723 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5724 : 0;
5725
5726 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5727 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5728 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5729 load->operands[0] = Operand(fmask_desc_ptr);
5730 load->operands[1] = Operand(s4); /* no sampler */
5731 load->operands[2] = Operand(coord);
5732 load->definitions[0] = Definition(fmask);
5733 load->glc = false;
5734 load->dlc = false;
5735 load->dmask = 0x1;
5736 load->unrm = true;
5737 load->da = da;
5738 load->dim = dim;
5739 ctx->block->instructions.emplace_back(std::move(load));
5740
5741 Operand sample_index4;
5742 if (sample_index.isConstant()) {
5743 if (sample_index.constantValue() < 16) {
5744 sample_index4 = Operand(sample_index.constantValue() << 2);
5745 } else {
5746 sample_index4 = Operand(0u);
5747 }
5748 } else if (sample_index.regClass() == s1) {
5749 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5750 } else {
5751 assert(sample_index.regClass() == v1);
5752 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5753 }
5754
5755 Temp final_sample;
5756 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5757 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5758 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5759 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5760 else
5761 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5762
5763 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5764 * resource descriptor is 0 (invalid),
5765 */
5766 Temp compare = bld.tmp(bld.lm);
5767 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5768 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5769
5770 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5771
5772 /* Replace the MSAA sample index. */
5773 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5774 }
5775
5776 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5777 {
5778
5779 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5780 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5781 bool is_array = glsl_sampler_type_is_array(type);
5782 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5783 assert(!add_frag_pos && "Input attachments should be lowered.");
5784 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5785 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5786 int count = image_type_to_components_count(dim, is_array);
5787 std::vector<Temp> coords(count);
5788 Builder bld(ctx->program, ctx->block);
5789
5790 if (is_ms) {
5791 count--;
5792 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5793 /* get sample index */
5794 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5795 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5796 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5797 std::vector<Temp> fmask_load_address;
5798 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5799 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5800
5801 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5802 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5803 } else {
5804 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5805 }
5806 }
5807
5808 if (gfx9_1d) {
5809 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5810 coords.resize(coords.size() + 1);
5811 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5812 if (is_array)
5813 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5814 } else {
5815 for (int i = 0; i < count; i++)
5816 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5817 }
5818
5819 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5820 instr->intrinsic == nir_intrinsic_image_deref_store) {
5821 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5822 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5823
5824 if (!level_zero)
5825 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5826 }
5827
5828 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5829 for (unsigned i = 0; i < coords.size(); i++)
5830 vec->operands[i] = Operand(coords[i]);
5831 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5832 vec->definitions[0] = Definition(res);
5833 ctx->block->instructions.emplace_back(std::move(vec));
5834 return res;
5835 }
5836
5837
5838 memory_sync_info get_memory_sync_info(nir_intrinsic_instr *instr, storage_class storage, unsigned semantics)
5839 {
5840 /* atomicrmw might not have NIR_INTRINSIC_ACCESS and there's nothing interesting there anyway */
5841 if (semantics & semantic_atomicrmw)
5842 return memory_sync_info(storage, semantics);
5843
5844 unsigned access = nir_intrinsic_access(instr);
5845
5846 if (access & ACCESS_VOLATILE)
5847 semantics |= semantic_volatile;
5848 if (access & ACCESS_CAN_REORDER)
5849 semantics |= semantic_can_reorder | semantic_private;
5850
5851 return memory_sync_info(storage, semantics);
5852 }
5853
5854 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5855 {
5856 Builder bld(ctx->program, ctx->block);
5857 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5858 const struct glsl_type *type = glsl_without_array(var->type);
5859 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5860 bool is_array = glsl_sampler_type_is_array(type);
5861 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5862
5863 memory_sync_info sync = get_memory_sync_info(instr, storage_image, 0);
5864 unsigned access = var->data.access | nir_intrinsic_access(instr);
5865
5866 if (dim == GLSL_SAMPLER_DIM_BUF) {
5867 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5868 unsigned num_channels = util_last_bit(mask);
5869 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5870 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5871
5872 aco_opcode opcode;
5873 switch (num_channels) {
5874 case 1:
5875 opcode = aco_opcode::buffer_load_format_x;
5876 break;
5877 case 2:
5878 opcode = aco_opcode::buffer_load_format_xy;
5879 break;
5880 case 3:
5881 opcode = aco_opcode::buffer_load_format_xyz;
5882 break;
5883 case 4:
5884 opcode = aco_opcode::buffer_load_format_xyzw;
5885 break;
5886 default:
5887 unreachable(">4 channel buffer image load");
5888 }
5889 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5890 load->operands[0] = Operand(rsrc);
5891 load->operands[1] = Operand(vindex);
5892 load->operands[2] = Operand((uint32_t) 0);
5893 Temp tmp;
5894 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5895 tmp = dst;
5896 else
5897 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5898 load->definitions[0] = Definition(tmp);
5899 load->idxen = true;
5900 load->glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5901 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5902 load->sync = sync;
5903 ctx->block->instructions.emplace_back(std::move(load));
5904
5905 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5906 return;
5907 }
5908
5909 Temp coords = get_image_coords(ctx, instr, type);
5910 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5911
5912 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5913 unsigned num_components = util_bitcount(dmask);
5914 Temp tmp;
5915 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5916 tmp = dst;
5917 else
5918 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5919
5920 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5921 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5922
5923 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5924 load->operands[0] = Operand(resource);
5925 load->operands[1] = Operand(s4); /* no sampler */
5926 load->operands[2] = Operand(coords);
5927 load->definitions[0] = Definition(tmp);
5928 load->glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5929 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5930 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5931 load->dmask = dmask;
5932 load->unrm = true;
5933 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5934 load->sync = sync;
5935 ctx->block->instructions.emplace_back(std::move(load));
5936
5937 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5938 return;
5939 }
5940
5941 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5942 {
5943 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5944 const struct glsl_type *type = glsl_without_array(var->type);
5945 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5946 bool is_array = glsl_sampler_type_is_array(type);
5947 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5948
5949 memory_sync_info sync = get_memory_sync_info(instr, storage_image, 0);
5950 unsigned access = var->data.access | nir_intrinsic_access(instr);
5951 bool glc = ctx->options->chip_class == GFX6 || access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5952
5953 if (dim == GLSL_SAMPLER_DIM_BUF) {
5954 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5955 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5956 aco_opcode opcode;
5957 switch (data.size()) {
5958 case 1:
5959 opcode = aco_opcode::buffer_store_format_x;
5960 break;
5961 case 2:
5962 opcode = aco_opcode::buffer_store_format_xy;
5963 break;
5964 case 3:
5965 opcode = aco_opcode::buffer_store_format_xyz;
5966 break;
5967 case 4:
5968 opcode = aco_opcode::buffer_store_format_xyzw;
5969 break;
5970 default:
5971 unreachable(">4 channel buffer image store");
5972 }
5973 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5974 store->operands[0] = Operand(rsrc);
5975 store->operands[1] = Operand(vindex);
5976 store->operands[2] = Operand((uint32_t) 0);
5977 store->operands[3] = Operand(data);
5978 store->idxen = true;
5979 store->glc = glc;
5980 store->dlc = false;
5981 store->disable_wqm = true;
5982 store->sync = sync;
5983 ctx->program->needs_exact = true;
5984 ctx->block->instructions.emplace_back(std::move(store));
5985 return;
5986 }
5987
5988 assert(data.type() == RegType::vgpr);
5989 Temp coords = get_image_coords(ctx, instr, type);
5990 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5991
5992 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5993 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5994
5995 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5996 store->operands[0] = Operand(resource);
5997 store->operands[1] = Operand(data);
5998 store->operands[2] = Operand(coords);
5999 store->glc = glc;
6000 store->dlc = false;
6001 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6002 store->dmask = (1 << data.size()) - 1;
6003 store->unrm = true;
6004 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6005 store->disable_wqm = true;
6006 store->sync = sync;
6007 ctx->program->needs_exact = true;
6008 ctx->block->instructions.emplace_back(std::move(store));
6009 return;
6010 }
6011
6012 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6013 {
6014 /* return the previous value if dest is ever used */
6015 bool return_previous = false;
6016 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6017 return_previous = true;
6018 break;
6019 }
6020 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6021 return_previous = true;
6022 break;
6023 }
6024
6025 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6026 const struct glsl_type *type = glsl_without_array(var->type);
6027 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6028 bool is_array = glsl_sampler_type_is_array(type);
6029 Builder bld(ctx->program, ctx->block);
6030
6031 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
6032 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
6033
6034 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
6035 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
6036
6037 aco_opcode buf_op, image_op;
6038 switch (instr->intrinsic) {
6039 case nir_intrinsic_image_deref_atomic_add:
6040 buf_op = aco_opcode::buffer_atomic_add;
6041 image_op = aco_opcode::image_atomic_add;
6042 break;
6043 case nir_intrinsic_image_deref_atomic_umin:
6044 buf_op = aco_opcode::buffer_atomic_umin;
6045 image_op = aco_opcode::image_atomic_umin;
6046 break;
6047 case nir_intrinsic_image_deref_atomic_imin:
6048 buf_op = aco_opcode::buffer_atomic_smin;
6049 image_op = aco_opcode::image_atomic_smin;
6050 break;
6051 case nir_intrinsic_image_deref_atomic_umax:
6052 buf_op = aco_opcode::buffer_atomic_umax;
6053 image_op = aco_opcode::image_atomic_umax;
6054 break;
6055 case nir_intrinsic_image_deref_atomic_imax:
6056 buf_op = aco_opcode::buffer_atomic_smax;
6057 image_op = aco_opcode::image_atomic_smax;
6058 break;
6059 case nir_intrinsic_image_deref_atomic_and:
6060 buf_op = aco_opcode::buffer_atomic_and;
6061 image_op = aco_opcode::image_atomic_and;
6062 break;
6063 case nir_intrinsic_image_deref_atomic_or:
6064 buf_op = aco_opcode::buffer_atomic_or;
6065 image_op = aco_opcode::image_atomic_or;
6066 break;
6067 case nir_intrinsic_image_deref_atomic_xor:
6068 buf_op = aco_opcode::buffer_atomic_xor;
6069 image_op = aco_opcode::image_atomic_xor;
6070 break;
6071 case nir_intrinsic_image_deref_atomic_exchange:
6072 buf_op = aco_opcode::buffer_atomic_swap;
6073 image_op = aco_opcode::image_atomic_swap;
6074 break;
6075 case nir_intrinsic_image_deref_atomic_comp_swap:
6076 buf_op = aco_opcode::buffer_atomic_cmpswap;
6077 image_op = aco_opcode::image_atomic_cmpswap;
6078 break;
6079 default:
6080 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
6081 }
6082
6083 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6084 memory_sync_info sync = get_memory_sync_info(instr, storage_image, semantic_atomicrmw);
6085
6086 if (dim == GLSL_SAMPLER_DIM_BUF) {
6087 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
6088 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
6089 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6090 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6091 mubuf->operands[0] = Operand(resource);
6092 mubuf->operands[1] = Operand(vindex);
6093 mubuf->operands[2] = Operand((uint32_t)0);
6094 mubuf->operands[3] = Operand(data);
6095 if (return_previous)
6096 mubuf->definitions[0] = Definition(dst);
6097 mubuf->offset = 0;
6098 mubuf->idxen = true;
6099 mubuf->glc = return_previous;
6100 mubuf->dlc = false; /* Not needed for atomics */
6101 mubuf->disable_wqm = true;
6102 mubuf->sync = sync;
6103 ctx->program->needs_exact = true;
6104 ctx->block->instructions.emplace_back(std::move(mubuf));
6105 return;
6106 }
6107
6108 Temp coords = get_image_coords(ctx, instr, type);
6109 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6110 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6111 mimg->operands[0] = Operand(resource);
6112 mimg->operands[1] = Operand(data);
6113 mimg->operands[2] = Operand(coords);
6114 if (return_previous)
6115 mimg->definitions[0] = Definition(dst);
6116 mimg->glc = return_previous;
6117 mimg->dlc = false; /* Not needed for atomics */
6118 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6119 mimg->dmask = (1 << data.size()) - 1;
6120 mimg->unrm = true;
6121 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6122 mimg->disable_wqm = true;
6123 mimg->sync = sync;
6124 ctx->program->needs_exact = true;
6125 ctx->block->instructions.emplace_back(std::move(mimg));
6126 return;
6127 }
6128
6129 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6130 {
6131 if (in_elements && ctx->options->chip_class == GFX8) {
6132 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6133 Builder bld(ctx->program, ctx->block);
6134
6135 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6136
6137 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6138 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6139
6140 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6141 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6142
6143 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6144 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6145
6146 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6147 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6148 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6149 if (dst.type() == RegType::vgpr)
6150 bld.copy(Definition(dst), shr_dst);
6151
6152 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6153 } else {
6154 emit_extract_vector(ctx, desc, 2, dst);
6155 }
6156 }
6157
6158 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6159 {
6160 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6161 const struct glsl_type *type = glsl_without_array(var->type);
6162 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6163 bool is_array = glsl_sampler_type_is_array(type);
6164 Builder bld(ctx->program, ctx->block);
6165
6166 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6167 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6168 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6169 }
6170
6171 /* LOD */
6172 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6173
6174 /* Resource */
6175 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6176
6177 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6178
6179 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6180 mimg->operands[0] = Operand(resource);
6181 mimg->operands[1] = Operand(s4); /* no sampler */
6182 mimg->operands[2] = Operand(lod);
6183 uint8_t& dmask = mimg->dmask;
6184 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6185 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6186 mimg->da = glsl_sampler_type_is_array(type);
6187 Definition& def = mimg->definitions[0];
6188 ctx->block->instructions.emplace_back(std::move(mimg));
6189
6190 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6191 glsl_sampler_type_is_array(type)) {
6192
6193 assert(instr->dest.ssa.num_components == 3);
6194 Temp tmp = {ctx->program->allocateId(), v3};
6195 def = Definition(tmp);
6196 emit_split_vector(ctx, tmp, 3);
6197
6198 /* divide 3rd value by 6 by multiplying with magic number */
6199 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6200 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6201
6202 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6203 emit_extract_vector(ctx, tmp, 0, v1),
6204 emit_extract_vector(ctx, tmp, 1, v1),
6205 by_6);
6206
6207 } else if (ctx->options->chip_class == GFX9 &&
6208 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6209 glsl_sampler_type_is_array(type)) {
6210 assert(instr->dest.ssa.num_components == 2);
6211 def = Definition(dst);
6212 dmask = 0x5;
6213 } else {
6214 def = Definition(dst);
6215 }
6216
6217 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6218 }
6219
6220 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6221 {
6222 Builder bld(ctx->program, ctx->block);
6223 unsigned num_components = instr->num_components;
6224
6225 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6226 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6227 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6228
6229 unsigned access = nir_intrinsic_access(instr);
6230 bool glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
6231 unsigned size = instr->dest.ssa.bit_size / 8;
6232
6233 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[0].ssa, access);
6234 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6235 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6236 */
6237 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_store : has_vmem_store));
6238 allow_smem |= ((access & ACCESS_RESTRICT) && (access & ACCESS_NON_WRITEABLE)) || (access & ACCESS_CAN_REORDER);
6239
6240 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6241 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, allow_smem,
6242 get_memory_sync_info(instr, storage_buffer, 0));
6243 }
6244
6245 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6246 {
6247 Builder bld(ctx->program, ctx->block);
6248 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6249 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6250 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6251 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6252
6253 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6254 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6255
6256 memory_sync_info sync = get_memory_sync_info(instr, storage_buffer, 0);
6257 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6258 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[1].ssa, nir_intrinsic_access(instr));
6259 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6260 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6261 */
6262 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_loadstore : has_vmem_loadstore));
6263
6264 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6265 ctx->options->chip_class >= GFX8 &&
6266 (elem_size_bytes >= 4 || can_subdword_ssbo_store_use_smem(instr)) &&
6267 allow_smem;
6268 if (smem)
6269 offset = bld.as_uniform(offset);
6270 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6271
6272 unsigned write_count = 0;
6273 Temp write_datas[32];
6274 unsigned offsets[32];
6275 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6276 data, writemask, 16, &write_count, write_datas, offsets);
6277
6278 for (unsigned i = 0; i < write_count; i++) {
6279 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6280 if (smem && ctx->stage == fragment_fs)
6281 op = aco_opcode::p_fs_buffer_store_smem;
6282
6283 if (smem) {
6284 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6285 store->operands[0] = Operand(rsrc);
6286 if (offsets[i]) {
6287 Temp off = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6288 offset, Operand(offsets[i]));
6289 store->operands[1] = Operand(off);
6290 } else {
6291 store->operands[1] = Operand(offset);
6292 }
6293 if (op != aco_opcode::p_fs_buffer_store_smem)
6294 store->operands[1].setFixed(m0);
6295 store->operands[2] = Operand(write_datas[i]);
6296 store->glc = glc;
6297 store->dlc = false;
6298 store->disable_wqm = true;
6299 store->sync = sync;
6300 ctx->block->instructions.emplace_back(std::move(store));
6301 ctx->program->wb_smem_l1_on_end = true;
6302 if (op == aco_opcode::p_fs_buffer_store_smem) {
6303 ctx->block->kind |= block_kind_needs_lowering;
6304 ctx->program->needs_exact = true;
6305 }
6306 } else {
6307 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6308 store->operands[0] = Operand(rsrc);
6309 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6310 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6311 store->operands[3] = Operand(write_datas[i]);
6312 store->offset = offsets[i];
6313 store->offen = (offset.type() == RegType::vgpr);
6314 store->glc = glc;
6315 store->dlc = false;
6316 store->disable_wqm = true;
6317 store->sync = sync;
6318 ctx->program->needs_exact = true;
6319 ctx->block->instructions.emplace_back(std::move(store));
6320 }
6321 }
6322 }
6323
6324 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6325 {
6326 /* return the previous value if dest is ever used */
6327 bool return_previous = false;
6328 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6329 return_previous = true;
6330 break;
6331 }
6332 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6333 return_previous = true;
6334 break;
6335 }
6336
6337 Builder bld(ctx->program, ctx->block);
6338 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6339
6340 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6341 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6342 get_ssa_temp(ctx, instr->src[3].ssa), data);
6343
6344 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6345 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6346 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6347
6348 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6349
6350 aco_opcode op32, op64;
6351 switch (instr->intrinsic) {
6352 case nir_intrinsic_ssbo_atomic_add:
6353 op32 = aco_opcode::buffer_atomic_add;
6354 op64 = aco_opcode::buffer_atomic_add_x2;
6355 break;
6356 case nir_intrinsic_ssbo_atomic_imin:
6357 op32 = aco_opcode::buffer_atomic_smin;
6358 op64 = aco_opcode::buffer_atomic_smin_x2;
6359 break;
6360 case nir_intrinsic_ssbo_atomic_umin:
6361 op32 = aco_opcode::buffer_atomic_umin;
6362 op64 = aco_opcode::buffer_atomic_umin_x2;
6363 break;
6364 case nir_intrinsic_ssbo_atomic_imax:
6365 op32 = aco_opcode::buffer_atomic_smax;
6366 op64 = aco_opcode::buffer_atomic_smax_x2;
6367 break;
6368 case nir_intrinsic_ssbo_atomic_umax:
6369 op32 = aco_opcode::buffer_atomic_umax;
6370 op64 = aco_opcode::buffer_atomic_umax_x2;
6371 break;
6372 case nir_intrinsic_ssbo_atomic_and:
6373 op32 = aco_opcode::buffer_atomic_and;
6374 op64 = aco_opcode::buffer_atomic_and_x2;
6375 break;
6376 case nir_intrinsic_ssbo_atomic_or:
6377 op32 = aco_opcode::buffer_atomic_or;
6378 op64 = aco_opcode::buffer_atomic_or_x2;
6379 break;
6380 case nir_intrinsic_ssbo_atomic_xor:
6381 op32 = aco_opcode::buffer_atomic_xor;
6382 op64 = aco_opcode::buffer_atomic_xor_x2;
6383 break;
6384 case nir_intrinsic_ssbo_atomic_exchange:
6385 op32 = aco_opcode::buffer_atomic_swap;
6386 op64 = aco_opcode::buffer_atomic_swap_x2;
6387 break;
6388 case nir_intrinsic_ssbo_atomic_comp_swap:
6389 op32 = aco_opcode::buffer_atomic_cmpswap;
6390 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6391 break;
6392 default:
6393 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6394 }
6395 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6396 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6397 mubuf->operands[0] = Operand(rsrc);
6398 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6399 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6400 mubuf->operands[3] = Operand(data);
6401 if (return_previous)
6402 mubuf->definitions[0] = Definition(dst);
6403 mubuf->offset = 0;
6404 mubuf->offen = (offset.type() == RegType::vgpr);
6405 mubuf->glc = return_previous;
6406 mubuf->dlc = false; /* Not needed for atomics */
6407 mubuf->disable_wqm = true;
6408 mubuf->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6409 ctx->program->needs_exact = true;
6410 ctx->block->instructions.emplace_back(std::move(mubuf));
6411 }
6412
6413 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6414
6415 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6416 Builder bld(ctx->program, ctx->block);
6417 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6418 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6419 }
6420
6421 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6422 {
6423 Builder bld(ctx->program, ctx->block);
6424 unsigned num_components = instr->num_components;
6425 unsigned component_size = instr->dest.ssa.bit_size / 8;
6426
6427 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6428 get_ssa_temp(ctx, &instr->dest.ssa),
6429 num_components, component_size};
6430 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6431 info.align_mul = nir_intrinsic_align_mul(instr);
6432 info.align_offset = nir_intrinsic_align_offset(instr);
6433 info.sync = get_memory_sync_info(instr, storage_buffer, 0);
6434 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6435 * it's safe to use SMEM */
6436 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6437 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6438 emit_global_load(ctx, bld, &info);
6439 } else {
6440 info.offset = Operand(bld.as_uniform(info.offset));
6441 emit_smem_load(ctx, bld, &info);
6442 }
6443 }
6444
6445 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6446 {
6447 Builder bld(ctx->program, ctx->block);
6448 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6449 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6450
6451 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6452 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6453 memory_sync_info sync = get_memory_sync_info(instr, storage_buffer, 0);
6454 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6455
6456 if (ctx->options->chip_class >= GFX7)
6457 addr = as_vgpr(ctx, addr);
6458
6459 unsigned write_count = 0;
6460 Temp write_datas[32];
6461 unsigned offsets[32];
6462 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6463 16, &write_count, write_datas, offsets);
6464
6465 for (unsigned i = 0; i < write_count; i++) {
6466 if (ctx->options->chip_class >= GFX7) {
6467 unsigned offset = offsets[i];
6468 Temp store_addr = addr;
6469 if (offset > 0 && ctx->options->chip_class < GFX9) {
6470 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6471 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6472 Temp carry = bld.tmp(bld.lm);
6473 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6474
6475 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6476 Operand(offset), addr0);
6477 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6478 Operand(0u), addr1,
6479 carry).def(1).setHint(vcc);
6480
6481 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6482
6483 offset = 0;
6484 }
6485
6486 bool global = ctx->options->chip_class >= GFX9;
6487 aco_opcode op;
6488 switch (write_datas[i].bytes()) {
6489 case 1:
6490 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6491 break;
6492 case 2:
6493 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6494 break;
6495 case 4:
6496 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6497 break;
6498 case 8:
6499 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6500 break;
6501 case 12:
6502 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6503 break;
6504 case 16:
6505 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6506 break;
6507 default:
6508 unreachable("store_global not implemented for this size.");
6509 }
6510
6511 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6512 flat->operands[0] = Operand(store_addr);
6513 flat->operands[1] = Operand(s1);
6514 flat->operands[2] = Operand(write_datas[i]);
6515 flat->glc = glc;
6516 flat->dlc = false;
6517 flat->offset = offset;
6518 flat->disable_wqm = true;
6519 flat->sync = sync;
6520 ctx->program->needs_exact = true;
6521 ctx->block->instructions.emplace_back(std::move(flat));
6522 } else {
6523 assert(ctx->options->chip_class == GFX6);
6524
6525 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6526
6527 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6528
6529 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6530 mubuf->operands[0] = Operand(rsrc);
6531 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6532 mubuf->operands[2] = Operand(0u);
6533 mubuf->operands[3] = Operand(write_datas[i]);
6534 mubuf->glc = glc;
6535 mubuf->dlc = false;
6536 mubuf->offset = offsets[i];
6537 mubuf->addr64 = addr.type() == RegType::vgpr;
6538 mubuf->disable_wqm = true;
6539 mubuf->sync = sync;
6540 ctx->program->needs_exact = true;
6541 ctx->block->instructions.emplace_back(std::move(mubuf));
6542 }
6543 }
6544 }
6545
6546 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6547 {
6548 /* return the previous value if dest is ever used */
6549 bool return_previous = false;
6550 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6551 return_previous = true;
6552 break;
6553 }
6554 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6555 return_previous = true;
6556 break;
6557 }
6558
6559 Builder bld(ctx->program, ctx->block);
6560 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6561 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6562
6563 if (ctx->options->chip_class >= GFX7)
6564 addr = as_vgpr(ctx, addr);
6565
6566 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6567 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6568 get_ssa_temp(ctx, instr->src[2].ssa), data);
6569
6570 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6571
6572 aco_opcode op32, op64;
6573
6574 if (ctx->options->chip_class >= GFX7) {
6575 bool global = ctx->options->chip_class >= GFX9;
6576 switch (instr->intrinsic) {
6577 case nir_intrinsic_global_atomic_add:
6578 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6579 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6580 break;
6581 case nir_intrinsic_global_atomic_imin:
6582 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6583 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6584 break;
6585 case nir_intrinsic_global_atomic_umin:
6586 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6587 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6588 break;
6589 case nir_intrinsic_global_atomic_imax:
6590 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6591 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6592 break;
6593 case nir_intrinsic_global_atomic_umax:
6594 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6595 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6596 break;
6597 case nir_intrinsic_global_atomic_and:
6598 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6599 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6600 break;
6601 case nir_intrinsic_global_atomic_or:
6602 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6603 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6604 break;
6605 case nir_intrinsic_global_atomic_xor:
6606 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6607 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6608 break;
6609 case nir_intrinsic_global_atomic_exchange:
6610 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6611 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6612 break;
6613 case nir_intrinsic_global_atomic_comp_swap:
6614 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6615 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6616 break;
6617 default:
6618 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6619 }
6620
6621 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6622 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6623 flat->operands[0] = Operand(addr);
6624 flat->operands[1] = Operand(s1);
6625 flat->operands[2] = Operand(data);
6626 if (return_previous)
6627 flat->definitions[0] = Definition(dst);
6628 flat->glc = return_previous;
6629 flat->dlc = false; /* Not needed for atomics */
6630 flat->offset = 0;
6631 flat->disable_wqm = true;
6632 flat->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6633 ctx->program->needs_exact = true;
6634 ctx->block->instructions.emplace_back(std::move(flat));
6635 } else {
6636 assert(ctx->options->chip_class == GFX6);
6637
6638 switch (instr->intrinsic) {
6639 case nir_intrinsic_global_atomic_add:
6640 op32 = aco_opcode::buffer_atomic_add;
6641 op64 = aco_opcode::buffer_atomic_add_x2;
6642 break;
6643 case nir_intrinsic_global_atomic_imin:
6644 op32 = aco_opcode::buffer_atomic_smin;
6645 op64 = aco_opcode::buffer_atomic_smin_x2;
6646 break;
6647 case nir_intrinsic_global_atomic_umin:
6648 op32 = aco_opcode::buffer_atomic_umin;
6649 op64 = aco_opcode::buffer_atomic_umin_x2;
6650 break;
6651 case nir_intrinsic_global_atomic_imax:
6652 op32 = aco_opcode::buffer_atomic_smax;
6653 op64 = aco_opcode::buffer_atomic_smax_x2;
6654 break;
6655 case nir_intrinsic_global_atomic_umax:
6656 op32 = aco_opcode::buffer_atomic_umax;
6657 op64 = aco_opcode::buffer_atomic_umax_x2;
6658 break;
6659 case nir_intrinsic_global_atomic_and:
6660 op32 = aco_opcode::buffer_atomic_and;
6661 op64 = aco_opcode::buffer_atomic_and_x2;
6662 break;
6663 case nir_intrinsic_global_atomic_or:
6664 op32 = aco_opcode::buffer_atomic_or;
6665 op64 = aco_opcode::buffer_atomic_or_x2;
6666 break;
6667 case nir_intrinsic_global_atomic_xor:
6668 op32 = aco_opcode::buffer_atomic_xor;
6669 op64 = aco_opcode::buffer_atomic_xor_x2;
6670 break;
6671 case nir_intrinsic_global_atomic_exchange:
6672 op32 = aco_opcode::buffer_atomic_swap;
6673 op64 = aco_opcode::buffer_atomic_swap_x2;
6674 break;
6675 case nir_intrinsic_global_atomic_comp_swap:
6676 op32 = aco_opcode::buffer_atomic_cmpswap;
6677 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6678 break;
6679 default:
6680 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6681 }
6682
6683 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6684
6685 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6686
6687 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6688 mubuf->operands[0] = Operand(rsrc);
6689 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6690 mubuf->operands[2] = Operand(0u);
6691 mubuf->operands[3] = Operand(data);
6692 if (return_previous)
6693 mubuf->definitions[0] = Definition(dst);
6694 mubuf->glc = return_previous;
6695 mubuf->dlc = false;
6696 mubuf->offset = 0;
6697 mubuf->addr64 = addr.type() == RegType::vgpr;
6698 mubuf->disable_wqm = true;
6699 mubuf->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6700 ctx->program->needs_exact = true;
6701 ctx->block->instructions.emplace_back(std::move(mubuf));
6702 }
6703 }
6704
6705 sync_scope translate_nir_scope(nir_scope scope)
6706 {
6707 switch (scope) {
6708 case NIR_SCOPE_NONE:
6709 case NIR_SCOPE_INVOCATION:
6710 return scope_invocation;
6711 case NIR_SCOPE_SUBGROUP:
6712 return scope_subgroup;
6713 case NIR_SCOPE_WORKGROUP:
6714 return scope_workgroup;
6715 case NIR_SCOPE_QUEUE_FAMILY:
6716 return scope_queuefamily;
6717 case NIR_SCOPE_DEVICE:
6718 return scope_device;
6719 }
6720 unreachable("invalid scope");
6721 }
6722
6723 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6724 Builder bld(ctx->program, ctx->block);
6725 storage_class all_mem = (storage_class)(storage_buffer | storage_image | storage_atomic_counter | storage_shared);
6726 switch(instr->intrinsic) {
6727 case nir_intrinsic_group_memory_barrier:
6728 bld.barrier(aco_opcode::p_barrier,
6729 memory_sync_info(all_mem, semantic_acqrel, scope_workgroup));
6730 break;
6731 case nir_intrinsic_memory_barrier:
6732 bld.barrier(aco_opcode::p_barrier,
6733 memory_sync_info(all_mem, semantic_acqrel, scope_device));
6734 break;
6735 case nir_intrinsic_memory_barrier_buffer:
6736 bld.barrier(aco_opcode::p_barrier,
6737 memory_sync_info((storage_class)storage_buffer, semantic_acqrel, scope_device));
6738 case nir_intrinsic_memory_barrier_image:
6739 bld.barrier(aco_opcode::p_barrier,
6740 memory_sync_info((storage_class)storage_image, semantic_acqrel, scope_device));
6741 break;
6742 case nir_intrinsic_memory_barrier_tcs_patch:
6743 case nir_intrinsic_memory_barrier_shared:
6744 bld.barrier(aco_opcode::p_barrier,
6745 memory_sync_info(storage_shared, semantic_acqrel, scope_workgroup));
6746 break;
6747 case nir_intrinsic_scoped_barrier: {
6748 unsigned semantics = 0;
6749 unsigned storage = 0;
6750 sync_scope mem_scope = translate_nir_scope(nir_intrinsic_memory_scope(instr));
6751 sync_scope exec_scope = translate_nir_scope(nir_intrinsic_execution_scope(instr));
6752
6753 unsigned nir_storage = nir_intrinsic_memory_modes(instr);
6754 if (nir_storage & (nir_var_mem_ssbo | nir_var_mem_global))
6755 storage |= storage_buffer | storage_image; //TODO: split this when NIR gets nir_var_mem_image
6756 if (ctx->shader->info.stage == MESA_SHADER_COMPUTE && (nir_storage & nir_var_mem_shared))
6757 storage |= storage_shared;
6758 if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL && (nir_storage & nir_var_shader_out))
6759 storage |= storage_shared;
6760
6761 unsigned nir_semantics = nir_intrinsic_memory_semantics(instr);
6762 if (nir_semantics & NIR_MEMORY_ACQUIRE)
6763 semantics |= semantic_acquire;
6764 if (nir_semantics & NIR_MEMORY_RELEASE)
6765 semantics |= semantic_release;
6766
6767 assert(!(nir_semantics & (NIR_MEMORY_MAKE_AVAILABLE | NIR_MEMORY_MAKE_VISIBLE)));
6768
6769 bld.barrier(aco_opcode::p_barrier,
6770 memory_sync_info((storage_class)storage, (memory_semantics)semantics, mem_scope),
6771 exec_scope);
6772 break;
6773 }
6774 default:
6775 unreachable("Unimplemented memory barrier intrinsic");
6776 break;
6777 }
6778 }
6779
6780 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6781 {
6782 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6783 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6784 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6785 Builder bld(ctx->program, ctx->block);
6786
6787 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6788 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6789 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6790 }
6791
6792 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6793 {
6794 unsigned writemask = nir_intrinsic_write_mask(instr);
6795 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6796 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6797 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6798
6799 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6800 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6801 }
6802
6803 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6804 {
6805 unsigned offset = nir_intrinsic_base(instr);
6806 Builder bld(ctx->program, ctx->block);
6807 Operand m = load_lds_size_m0(bld);
6808 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6809 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6810
6811 unsigned num_operands = 3;
6812 aco_opcode op32, op64, op32_rtn, op64_rtn;
6813 switch(instr->intrinsic) {
6814 case nir_intrinsic_shared_atomic_add:
6815 op32 = aco_opcode::ds_add_u32;
6816 op64 = aco_opcode::ds_add_u64;
6817 op32_rtn = aco_opcode::ds_add_rtn_u32;
6818 op64_rtn = aco_opcode::ds_add_rtn_u64;
6819 break;
6820 case nir_intrinsic_shared_atomic_imin:
6821 op32 = aco_opcode::ds_min_i32;
6822 op64 = aco_opcode::ds_min_i64;
6823 op32_rtn = aco_opcode::ds_min_rtn_i32;
6824 op64_rtn = aco_opcode::ds_min_rtn_i64;
6825 break;
6826 case nir_intrinsic_shared_atomic_umin:
6827 op32 = aco_opcode::ds_min_u32;
6828 op64 = aco_opcode::ds_min_u64;
6829 op32_rtn = aco_opcode::ds_min_rtn_u32;
6830 op64_rtn = aco_opcode::ds_min_rtn_u64;
6831 break;
6832 case nir_intrinsic_shared_atomic_imax:
6833 op32 = aco_opcode::ds_max_i32;
6834 op64 = aco_opcode::ds_max_i64;
6835 op32_rtn = aco_opcode::ds_max_rtn_i32;
6836 op64_rtn = aco_opcode::ds_max_rtn_i64;
6837 break;
6838 case nir_intrinsic_shared_atomic_umax:
6839 op32 = aco_opcode::ds_max_u32;
6840 op64 = aco_opcode::ds_max_u64;
6841 op32_rtn = aco_opcode::ds_max_rtn_u32;
6842 op64_rtn = aco_opcode::ds_max_rtn_u64;
6843 break;
6844 case nir_intrinsic_shared_atomic_and:
6845 op32 = aco_opcode::ds_and_b32;
6846 op64 = aco_opcode::ds_and_b64;
6847 op32_rtn = aco_opcode::ds_and_rtn_b32;
6848 op64_rtn = aco_opcode::ds_and_rtn_b64;
6849 break;
6850 case nir_intrinsic_shared_atomic_or:
6851 op32 = aco_opcode::ds_or_b32;
6852 op64 = aco_opcode::ds_or_b64;
6853 op32_rtn = aco_opcode::ds_or_rtn_b32;
6854 op64_rtn = aco_opcode::ds_or_rtn_b64;
6855 break;
6856 case nir_intrinsic_shared_atomic_xor:
6857 op32 = aco_opcode::ds_xor_b32;
6858 op64 = aco_opcode::ds_xor_b64;
6859 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6860 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6861 break;
6862 case nir_intrinsic_shared_atomic_exchange:
6863 op32 = aco_opcode::ds_write_b32;
6864 op64 = aco_opcode::ds_write_b64;
6865 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6866 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6867 break;
6868 case nir_intrinsic_shared_atomic_comp_swap:
6869 op32 = aco_opcode::ds_cmpst_b32;
6870 op64 = aco_opcode::ds_cmpst_b64;
6871 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6872 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6873 num_operands = 4;
6874 break;
6875 case nir_intrinsic_shared_atomic_fadd:
6876 op32 = aco_opcode::ds_add_f32;
6877 op32_rtn = aco_opcode::ds_add_rtn_f32;
6878 op64 = aco_opcode::num_opcodes;
6879 op64_rtn = aco_opcode::num_opcodes;
6880 break;
6881 default:
6882 unreachable("Unhandled shared atomic intrinsic");
6883 }
6884
6885 /* return the previous value if dest is ever used */
6886 bool return_previous = false;
6887 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6888 return_previous = true;
6889 break;
6890 }
6891 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6892 return_previous = true;
6893 break;
6894 }
6895
6896 aco_opcode op;
6897 if (data.size() == 1) {
6898 assert(instr->dest.ssa.bit_size == 32);
6899 op = return_previous ? op32_rtn : op32;
6900 } else {
6901 assert(instr->dest.ssa.bit_size == 64);
6902 op = return_previous ? op64_rtn : op64;
6903 }
6904
6905 if (offset > 65535) {
6906 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6907 offset = 0;
6908 }
6909
6910 aco_ptr<DS_instruction> ds;
6911 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6912 ds->operands[0] = Operand(address);
6913 ds->operands[1] = Operand(data);
6914 if (num_operands == 4)
6915 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6916 ds->operands[num_operands - 1] = m;
6917 ds->offset0 = offset;
6918 if (return_previous)
6919 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6920 ds->sync = memory_sync_info(storage_shared, semantic_atomicrmw);
6921 ctx->block->instructions.emplace_back(std::move(ds));
6922 }
6923
6924 Temp get_scratch_resource(isel_context *ctx)
6925 {
6926 Builder bld(ctx->program, ctx->block);
6927 Temp scratch_addr = ctx->program->private_segment_buffer;
6928 if (ctx->stage != compute_cs)
6929 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6930
6931 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6932 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);
6933
6934 if (ctx->program->chip_class >= GFX10) {
6935 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6936 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6937 S_008F0C_RESOURCE_LEVEL(1);
6938 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6939 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6940 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6941 }
6942
6943 /* older generations need element size = 4 bytes. element size removed in GFX9 */
6944 if (ctx->program->chip_class <= GFX8)
6945 rsrc_conf |= S_008F0C_ELEMENT_SIZE(1);
6946
6947 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6948 }
6949
6950 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6951 Builder bld(ctx->program, ctx->block);
6952 Temp rsrc = get_scratch_resource(ctx);
6953 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6954 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6955
6956 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6957 instr->dest.ssa.bit_size / 8u, rsrc};
6958 info.align_mul = nir_intrinsic_align_mul(instr);
6959 info.align_offset = nir_intrinsic_align_offset(instr);
6960 info.swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 0;
6961 info.sync = memory_sync_info(storage_buffer, semantic_private);
6962 info.soffset = ctx->program->scratch_offset;
6963 emit_scratch_load(ctx, bld, &info);
6964 }
6965
6966 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6967 Builder bld(ctx->program, ctx->block);
6968 Temp rsrc = get_scratch_resource(ctx);
6969 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6970 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6971
6972 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6973 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6974
6975 unsigned write_count = 0;
6976 Temp write_datas[32];
6977 unsigned offsets[32];
6978 unsigned swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 16;
6979 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6980 swizzle_component_size, &write_count, write_datas, offsets);
6981
6982 for (unsigned i = 0; i < write_count; i++) {
6983 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6984 Instruction *instr = bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true, true);
6985 static_cast<MUBUF_instruction *>(instr)->sync = memory_sync_info(storage_buffer, semantic_private);
6986 }
6987 }
6988
6989 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6990 uint8_t log2_ps_iter_samples;
6991 if (ctx->program->info->ps.force_persample) {
6992 log2_ps_iter_samples =
6993 util_logbase2(ctx->options->key.fs.num_samples);
6994 } else {
6995 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6996 }
6997
6998 /* The bit pattern matches that used by fixed function fragment
6999 * processing. */
7000 static const unsigned ps_iter_masks[] = {
7001 0xffff, /* not used */
7002 0x5555,
7003 0x1111,
7004 0x0101,
7005 0x0001,
7006 };
7007 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
7008
7009 Builder bld(ctx->program, ctx->block);
7010
7011 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
7012 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7013 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
7014 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
7015 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7016 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
7017 }
7018
7019 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
7020 Builder bld(ctx->program, ctx->block);
7021
7022 unsigned stream = nir_intrinsic_stream_id(instr);
7023 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7024 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
7025 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
7026
7027 /* get GSVS ring */
7028 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
7029
7030 unsigned num_components =
7031 ctx->program->info->gs.num_stream_output_components[stream];
7032 assert(num_components);
7033
7034 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
7035 unsigned stream_offset = 0;
7036 for (unsigned i = 0; i < stream; i++) {
7037 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
7038 stream_offset += prev_stride * ctx->program->wave_size;
7039 }
7040
7041 /* Limit on the stride field for <= GFX7. */
7042 assert(stride < (1 << 14));
7043
7044 Temp gsvs_dwords[4];
7045 for (unsigned i = 0; i < 4; i++)
7046 gsvs_dwords[i] = bld.tmp(s1);
7047 bld.pseudo(aco_opcode::p_split_vector,
7048 Definition(gsvs_dwords[0]),
7049 Definition(gsvs_dwords[1]),
7050 Definition(gsvs_dwords[2]),
7051 Definition(gsvs_dwords[3]),
7052 gsvs_ring);
7053
7054 if (stream_offset) {
7055 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
7056
7057 Temp carry = bld.tmp(s1);
7058 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
7059 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));
7060 }
7061
7062 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)));
7063 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
7064
7065 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
7066 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
7067
7068 unsigned offset = 0;
7069 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
7070 if (ctx->program->info->gs.output_streams[i] != stream)
7071 continue;
7072
7073 for (unsigned j = 0; j < 4; j++) {
7074 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
7075 continue;
7076
7077 if (ctx->outputs.mask[i] & (1 << j)) {
7078 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
7079 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
7080 if (const_offset >= 4096u) {
7081 if (vaddr_offset.isUndefined())
7082 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
7083 else
7084 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
7085 const_offset %= 4096u;
7086 }
7087
7088 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
7089 mtbuf->operands[0] = Operand(gsvs_ring);
7090 mtbuf->operands[1] = vaddr_offset;
7091 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
7092 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
7093 mtbuf->offen = !vaddr_offset.isUndefined();
7094 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
7095 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
7096 mtbuf->offset = const_offset;
7097 mtbuf->glc = true;
7098 mtbuf->slc = true;
7099 mtbuf->sync = memory_sync_info(storage_vmem_output, semantic_can_reorder);
7100 bld.insert(std::move(mtbuf));
7101 }
7102
7103 offset += ctx->shader->info.gs.vertices_out;
7104 }
7105
7106 /* outputs for the next vertex are undefined and keeping them around can
7107 * create invalid IR with control flow */
7108 ctx->outputs.mask[i] = 0;
7109 }
7110
7111 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
7112 }
7113
7114 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
7115 {
7116 Builder bld(ctx->program, ctx->block);
7117
7118 if (cluster_size == 1) {
7119 return src;
7120 } if (op == nir_op_iand && cluster_size == 4) {
7121 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7122 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7123 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
7124 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
7125 } else if (op == nir_op_ior && cluster_size == 4) {
7126 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7127 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
7128 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
7129 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
7130 //subgroupAnd(val) -> (exec & ~val) == 0
7131 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7132 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7133 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
7134 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
7135 //subgroupOr(val) -> (val & exec) != 0
7136 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
7137 return bool_to_vector_condition(ctx, tmp);
7138 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
7139 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7140 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7141 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
7142 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
7143 return bool_to_vector_condition(ctx, tmp);
7144 } else {
7145 //subgroupClustered{And,Or,Xor}(val, n) ->
7146 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7147 //cluster_offset = ~(n - 1) & lane_id
7148 //cluster_mask = ((1 << n) - 1)
7149 //subgroupClusteredAnd():
7150 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7151 //subgroupClusteredOr():
7152 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7153 //subgroupClusteredXor():
7154 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7155 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7156 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7157
7158 Temp tmp;
7159 if (op == nir_op_iand)
7160 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7161 else
7162 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7163
7164 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7165
7166 if (ctx->program->chip_class <= GFX7)
7167 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7168 else if (ctx->program->wave_size == 64)
7169 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7170 else
7171 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7172 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7173 if (cluster_mask != 0xffffffff)
7174 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7175
7176 Definition cmp_def = Definition();
7177 if (op == nir_op_iand) {
7178 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7179 } else if (op == nir_op_ior) {
7180 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7181 } else if (op == nir_op_ixor) {
7182 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7183 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7184 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7185 }
7186 cmp_def.setHint(vcc);
7187 return cmp_def.getTemp();
7188 }
7189 }
7190
7191 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7192 {
7193 Builder bld(ctx->program, ctx->block);
7194
7195 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7196 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7197 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7198 Temp tmp;
7199 if (op == nir_op_iand)
7200 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7201 else
7202 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7203
7204 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7205 Temp lo = lohi.def(0).getTemp();
7206 Temp hi = lohi.def(1).getTemp();
7207 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7208
7209 Definition cmp_def = Definition();
7210 if (op == nir_op_iand)
7211 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7212 else if (op == nir_op_ior)
7213 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7214 else if (op == nir_op_ixor)
7215 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7216 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7217 cmp_def.setHint(vcc);
7218 return cmp_def.getTemp();
7219 }
7220
7221 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7222 {
7223 Builder bld(ctx->program, ctx->block);
7224
7225 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7226 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7227 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7228 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7229 if (op == nir_op_iand)
7230 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7231 else if (op == nir_op_ior)
7232 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7233 else if (op == nir_op_ixor)
7234 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7235
7236 assert(false);
7237 return Temp();
7238 }
7239
7240 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7241 {
7242 Builder bld(ctx->program, ctx->block);
7243 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7244 if (src.regClass().type() == RegType::vgpr) {
7245 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7246 } else if (src.regClass() == s1) {
7247 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7248 } else if (src.regClass() == s2) {
7249 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7250 } else {
7251 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7252 nir_print_instr(&instr->instr, stderr);
7253 fprintf(stderr, "\n");
7254 }
7255 }
7256
7257 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7258 {
7259 Builder bld(ctx->program, ctx->block);
7260 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7261 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7262 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7263
7264 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7265 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7266 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7267 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7268
7269 /* Build DD X/Y */
7270 if (ctx->program->chip_class >= GFX8) {
7271 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7272 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7273 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7274 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7275 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7276 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7277 } else {
7278 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7279 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7280 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7281 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7282 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7283 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7284 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7285 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7286 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7287 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7288 }
7289
7290 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7291 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7292 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7293 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7294 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7295 Temp wqm1 = bld.tmp(v1);
7296 emit_wqm(ctx, tmp1, wqm1, true);
7297 Temp wqm2 = bld.tmp(v1);
7298 emit_wqm(ctx, tmp2, wqm2, true);
7299 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7300 return;
7301 }
7302
7303 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7304 {
7305 Builder bld(ctx->program, ctx->block);
7306 switch(instr->intrinsic) {
7307 case nir_intrinsic_load_barycentric_sample:
7308 case nir_intrinsic_load_barycentric_pixel:
7309 case nir_intrinsic_load_barycentric_centroid: {
7310 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7311 Temp bary = Temp(0, s2);
7312 switch (mode) {
7313 case INTERP_MODE_SMOOTH:
7314 case INTERP_MODE_NONE:
7315 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7316 bary = get_arg(ctx, ctx->args->ac.persp_center);
7317 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7318 bary = ctx->persp_centroid;
7319 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7320 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7321 break;
7322 case INTERP_MODE_NOPERSPECTIVE:
7323 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7324 bary = get_arg(ctx, ctx->args->ac.linear_center);
7325 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7326 bary = ctx->linear_centroid;
7327 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7328 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7329 break;
7330 default:
7331 break;
7332 }
7333 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7334 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7335 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7336 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7337 Operand(p1), Operand(p2));
7338 emit_split_vector(ctx, dst, 2);
7339 break;
7340 }
7341 case nir_intrinsic_load_barycentric_model: {
7342 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7343
7344 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7345 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7346 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7347 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7348 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7349 Operand(p1), Operand(p2), Operand(p3));
7350 emit_split_vector(ctx, dst, 3);
7351 break;
7352 }
7353 case nir_intrinsic_load_barycentric_at_sample: {
7354 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7355 switch (ctx->options->key.fs.num_samples) {
7356 case 2: sample_pos_offset += 1 << 3; break;
7357 case 4: sample_pos_offset += 3 << 3; break;
7358 case 8: sample_pos_offset += 7 << 3; break;
7359 default: break;
7360 }
7361 Temp sample_pos;
7362 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7363 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7364 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7365 //TODO: bounds checking?
7366 if (addr.type() == RegType::sgpr) {
7367 Operand offset;
7368 if (const_addr) {
7369 sample_pos_offset += const_addr->u32 << 3;
7370 offset = Operand(sample_pos_offset);
7371 } else if (ctx->options->chip_class >= GFX9) {
7372 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7373 } else {
7374 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7375 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7376 }
7377
7378 Operand off = bld.copy(bld.def(s1), Operand(offset));
7379 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7380
7381 } else if (ctx->options->chip_class >= GFX9) {
7382 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7383 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7384 } else if (ctx->options->chip_class >= GFX7) {
7385 /* addr += private_segment_buffer + sample_pos_offset */
7386 Temp tmp0 = bld.tmp(s1);
7387 Temp tmp1 = bld.tmp(s1);
7388 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7389 Definition scc_tmp = bld.def(s1, scc);
7390 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7391 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7392 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7393 Temp pck0 = bld.tmp(v1);
7394 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7395 tmp1 = as_vgpr(ctx, tmp1);
7396 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);
7397 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7398
7399 /* sample_pos = flat_load_dwordx2 addr */
7400 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7401 } else {
7402 assert(ctx->options->chip_class == GFX6);
7403
7404 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7405 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7406 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7407
7408 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7409 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7410
7411 sample_pos = bld.tmp(v2);
7412
7413 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7414 load->definitions[0] = Definition(sample_pos);
7415 load->operands[0] = Operand(rsrc);
7416 load->operands[1] = Operand(addr);
7417 load->operands[2] = Operand(0u);
7418 load->offset = sample_pos_offset;
7419 load->offen = 0;
7420 load->addr64 = true;
7421 load->glc = false;
7422 load->dlc = false;
7423 load->disable_wqm = false;
7424 ctx->block->instructions.emplace_back(std::move(load));
7425 }
7426
7427 /* sample_pos -= 0.5 */
7428 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7429 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7430 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7431 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7432 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7433
7434 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7435 break;
7436 }
7437 case nir_intrinsic_load_barycentric_at_offset: {
7438 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7439 RegClass rc = RegClass(offset.type(), 1);
7440 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7441 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7442 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7443 break;
7444 }
7445 case nir_intrinsic_load_front_face: {
7446 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7447 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7448 break;
7449 }
7450 case nir_intrinsic_load_view_index: {
7451 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7452 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7453 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7454 break;
7455 }
7456
7457 /* fallthrough */
7458 }
7459 case nir_intrinsic_load_layer_id: {
7460 unsigned idx = nir_intrinsic_base(instr);
7461 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7462 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7463 break;
7464 }
7465 case nir_intrinsic_load_frag_coord: {
7466 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7467 break;
7468 }
7469 case nir_intrinsic_load_sample_pos: {
7470 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7471 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7472 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7473 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7474 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7475 break;
7476 }
7477 case nir_intrinsic_load_tess_coord:
7478 visit_load_tess_coord(ctx, instr);
7479 break;
7480 case nir_intrinsic_load_interpolated_input:
7481 visit_load_interpolated_input(ctx, instr);
7482 break;
7483 case nir_intrinsic_store_output:
7484 visit_store_output(ctx, instr);
7485 break;
7486 case nir_intrinsic_load_input:
7487 case nir_intrinsic_load_input_vertex:
7488 visit_load_input(ctx, instr);
7489 break;
7490 case nir_intrinsic_load_output:
7491 visit_load_output(ctx, instr);
7492 break;
7493 case nir_intrinsic_load_per_vertex_input:
7494 visit_load_per_vertex_input(ctx, instr);
7495 break;
7496 case nir_intrinsic_load_per_vertex_output:
7497 visit_load_per_vertex_output(ctx, instr);
7498 break;
7499 case nir_intrinsic_store_per_vertex_output:
7500 visit_store_per_vertex_output(ctx, instr);
7501 break;
7502 case nir_intrinsic_load_ubo:
7503 visit_load_ubo(ctx, instr);
7504 break;
7505 case nir_intrinsic_load_push_constant:
7506 visit_load_push_constant(ctx, instr);
7507 break;
7508 case nir_intrinsic_load_constant:
7509 visit_load_constant(ctx, instr);
7510 break;
7511 case nir_intrinsic_vulkan_resource_index:
7512 visit_load_resource(ctx, instr);
7513 break;
7514 case nir_intrinsic_discard:
7515 visit_discard(ctx, instr);
7516 break;
7517 case nir_intrinsic_discard_if:
7518 visit_discard_if(ctx, instr);
7519 break;
7520 case nir_intrinsic_load_shared:
7521 visit_load_shared(ctx, instr);
7522 break;
7523 case nir_intrinsic_store_shared:
7524 visit_store_shared(ctx, instr);
7525 break;
7526 case nir_intrinsic_shared_atomic_add:
7527 case nir_intrinsic_shared_atomic_imin:
7528 case nir_intrinsic_shared_atomic_umin:
7529 case nir_intrinsic_shared_atomic_imax:
7530 case nir_intrinsic_shared_atomic_umax:
7531 case nir_intrinsic_shared_atomic_and:
7532 case nir_intrinsic_shared_atomic_or:
7533 case nir_intrinsic_shared_atomic_xor:
7534 case nir_intrinsic_shared_atomic_exchange:
7535 case nir_intrinsic_shared_atomic_comp_swap:
7536 case nir_intrinsic_shared_atomic_fadd:
7537 visit_shared_atomic(ctx, instr);
7538 break;
7539 case nir_intrinsic_image_deref_load:
7540 visit_image_load(ctx, instr);
7541 break;
7542 case nir_intrinsic_image_deref_store:
7543 visit_image_store(ctx, instr);
7544 break;
7545 case nir_intrinsic_image_deref_atomic_add:
7546 case nir_intrinsic_image_deref_atomic_umin:
7547 case nir_intrinsic_image_deref_atomic_imin:
7548 case nir_intrinsic_image_deref_atomic_umax:
7549 case nir_intrinsic_image_deref_atomic_imax:
7550 case nir_intrinsic_image_deref_atomic_and:
7551 case nir_intrinsic_image_deref_atomic_or:
7552 case nir_intrinsic_image_deref_atomic_xor:
7553 case nir_intrinsic_image_deref_atomic_exchange:
7554 case nir_intrinsic_image_deref_atomic_comp_swap:
7555 visit_image_atomic(ctx, instr);
7556 break;
7557 case nir_intrinsic_image_deref_size:
7558 visit_image_size(ctx, instr);
7559 break;
7560 case nir_intrinsic_load_ssbo:
7561 visit_load_ssbo(ctx, instr);
7562 break;
7563 case nir_intrinsic_store_ssbo:
7564 visit_store_ssbo(ctx, instr);
7565 break;
7566 case nir_intrinsic_load_global:
7567 visit_load_global(ctx, instr);
7568 break;
7569 case nir_intrinsic_store_global:
7570 visit_store_global(ctx, instr);
7571 break;
7572 case nir_intrinsic_global_atomic_add:
7573 case nir_intrinsic_global_atomic_imin:
7574 case nir_intrinsic_global_atomic_umin:
7575 case nir_intrinsic_global_atomic_imax:
7576 case nir_intrinsic_global_atomic_umax:
7577 case nir_intrinsic_global_atomic_and:
7578 case nir_intrinsic_global_atomic_or:
7579 case nir_intrinsic_global_atomic_xor:
7580 case nir_intrinsic_global_atomic_exchange:
7581 case nir_intrinsic_global_atomic_comp_swap:
7582 visit_global_atomic(ctx, instr);
7583 break;
7584 case nir_intrinsic_ssbo_atomic_add:
7585 case nir_intrinsic_ssbo_atomic_imin:
7586 case nir_intrinsic_ssbo_atomic_umin:
7587 case nir_intrinsic_ssbo_atomic_imax:
7588 case nir_intrinsic_ssbo_atomic_umax:
7589 case nir_intrinsic_ssbo_atomic_and:
7590 case nir_intrinsic_ssbo_atomic_or:
7591 case nir_intrinsic_ssbo_atomic_xor:
7592 case nir_intrinsic_ssbo_atomic_exchange:
7593 case nir_intrinsic_ssbo_atomic_comp_swap:
7594 visit_atomic_ssbo(ctx, instr);
7595 break;
7596 case nir_intrinsic_load_scratch:
7597 visit_load_scratch(ctx, instr);
7598 break;
7599 case nir_intrinsic_store_scratch:
7600 visit_store_scratch(ctx, instr);
7601 break;
7602 case nir_intrinsic_get_buffer_size:
7603 visit_get_buffer_size(ctx, instr);
7604 break;
7605 case nir_intrinsic_control_barrier: {
7606 bld.barrier(aco_opcode::p_barrier, memory_sync_info(0, 0, scope_invocation), scope_workgroup);
7607 break;
7608 }
7609 case nir_intrinsic_memory_barrier_tcs_patch:
7610 case nir_intrinsic_group_memory_barrier:
7611 case nir_intrinsic_memory_barrier:
7612 case nir_intrinsic_memory_barrier_buffer:
7613 case nir_intrinsic_memory_barrier_image:
7614 case nir_intrinsic_memory_barrier_shared:
7615 case nir_intrinsic_scoped_barrier:
7616 emit_memory_barrier(ctx, instr);
7617 break;
7618 case nir_intrinsic_load_num_work_groups: {
7619 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7620 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7621 emit_split_vector(ctx, dst, 3);
7622 break;
7623 }
7624 case nir_intrinsic_load_local_invocation_id: {
7625 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7626 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7627 emit_split_vector(ctx, dst, 3);
7628 break;
7629 }
7630 case nir_intrinsic_load_work_group_id: {
7631 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7632 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7633 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7634 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7635 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7636 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7637 emit_split_vector(ctx, dst, 3);
7638 break;
7639 }
7640 case nir_intrinsic_load_local_invocation_index: {
7641 Temp id = emit_mbcnt(ctx, bld.def(v1));
7642
7643 /* The tg_size bits [6:11] contain the subgroup id,
7644 * we need this multiplied by the wave size, and then OR the thread id to it.
7645 */
7646 if (ctx->program->wave_size == 64) {
7647 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7648 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7649 get_arg(ctx, ctx->args->ac.tg_size));
7650 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7651 } else {
7652 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7653 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7654 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7655 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7656 }
7657 break;
7658 }
7659 case nir_intrinsic_load_subgroup_id: {
7660 if (ctx->stage == compute_cs) {
7661 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7662 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7663 } else {
7664 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7665 }
7666 break;
7667 }
7668 case nir_intrinsic_load_subgroup_invocation: {
7669 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7670 break;
7671 }
7672 case nir_intrinsic_load_num_subgroups: {
7673 if (ctx->stage == compute_cs)
7674 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7675 get_arg(ctx, ctx->args->ac.tg_size));
7676 else
7677 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7678 break;
7679 }
7680 case nir_intrinsic_ballot: {
7681 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7682 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7683 Definition tmp = bld.def(dst.regClass());
7684 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7685 if (instr->src[0].ssa->bit_size == 1) {
7686 assert(src.regClass() == bld.lm);
7687 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7688 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7689 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7690 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7691 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7692 } else {
7693 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7694 nir_print_instr(&instr->instr, stderr);
7695 fprintf(stderr, "\n");
7696 }
7697 if (dst.size() != bld.lm.size()) {
7698 /* Wave32 with ballot size set to 64 */
7699 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7700 }
7701 emit_wqm(ctx, tmp.getTemp(), dst);
7702 break;
7703 }
7704 case nir_intrinsic_shuffle:
7705 case nir_intrinsic_read_invocation: {
7706 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7707 if (!nir_src_is_divergent(instr->src[0])) {
7708 emit_uniform_subgroup(ctx, instr, src);
7709 } else {
7710 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7711 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7712 tid = bld.as_uniform(tid);
7713 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7714 if (src.regClass() == v1b || src.regClass() == v2b) {
7715 Temp tmp = bld.tmp(v1);
7716 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7717 if (dst.type() == RegType::vgpr)
7718 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7719 else
7720 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7721 } else if (src.regClass() == v1) {
7722 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7723 } else if (src.regClass() == v2) {
7724 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7725 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7726 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7727 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7728 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7729 emit_split_vector(ctx, dst, 2);
7730 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7731 assert(src.regClass() == bld.lm);
7732 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7733 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7734 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7735 assert(src.regClass() == bld.lm);
7736 Temp tmp;
7737 if (ctx->program->chip_class <= GFX7)
7738 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7739 else if (ctx->program->wave_size == 64)
7740 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7741 else
7742 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7743 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7744 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7745 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7746 } else {
7747 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7748 nir_print_instr(&instr->instr, stderr);
7749 fprintf(stderr, "\n");
7750 }
7751 }
7752 break;
7753 }
7754 case nir_intrinsic_load_sample_id: {
7755 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7756 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7757 break;
7758 }
7759 case nir_intrinsic_load_sample_mask_in: {
7760 visit_load_sample_mask_in(ctx, instr);
7761 break;
7762 }
7763 case nir_intrinsic_read_first_invocation: {
7764 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7765 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7766 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7767 emit_wqm(ctx,
7768 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7769 dst);
7770 } else if (src.regClass() == v2) {
7771 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7772 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7773 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7774 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7775 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7776 emit_split_vector(ctx, dst, 2);
7777 } else if (instr->dest.ssa.bit_size == 1) {
7778 assert(src.regClass() == bld.lm);
7779 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7780 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7781 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7782 } else if (src.regClass() == s1) {
7783 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7784 } else if (src.regClass() == s2) {
7785 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7786 } else {
7787 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7788 nir_print_instr(&instr->instr, stderr);
7789 fprintf(stderr, "\n");
7790 }
7791 break;
7792 }
7793 case nir_intrinsic_vote_all: {
7794 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7795 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7796 assert(src.regClass() == bld.lm);
7797 assert(dst.regClass() == bld.lm);
7798
7799 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7800 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7801 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7802 break;
7803 }
7804 case nir_intrinsic_vote_any: {
7805 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7806 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7807 assert(src.regClass() == bld.lm);
7808 assert(dst.regClass() == bld.lm);
7809
7810 Temp tmp = bool_to_scalar_condition(ctx, src);
7811 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7812 break;
7813 }
7814 case nir_intrinsic_reduce:
7815 case nir_intrinsic_inclusive_scan:
7816 case nir_intrinsic_exclusive_scan: {
7817 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7818 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7819 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7820 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7821 nir_intrinsic_cluster_size(instr) : 0;
7822 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7823
7824 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7825 emit_uniform_subgroup(ctx, instr, src);
7826 } else if (instr->dest.ssa.bit_size == 1) {
7827 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7828 op = nir_op_iand;
7829 else if (op == nir_op_iadd)
7830 op = nir_op_ixor;
7831 else if (op == nir_op_umax || op == nir_op_imax)
7832 op = nir_op_ior;
7833 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7834
7835 switch (instr->intrinsic) {
7836 case nir_intrinsic_reduce:
7837 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7838 break;
7839 case nir_intrinsic_exclusive_scan:
7840 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7841 break;
7842 case nir_intrinsic_inclusive_scan:
7843 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7844 break;
7845 default:
7846 assert(false);
7847 }
7848 } else if (cluster_size == 1) {
7849 bld.copy(Definition(dst), src);
7850 } else {
7851 unsigned bit_size = instr->src[0].ssa->bit_size;
7852
7853 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7854
7855 ReduceOp reduce_op;
7856 switch (op) {
7857 #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;
7858 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7859 CASEI(iadd)
7860 CASEI(imul)
7861 CASEI(imin)
7862 CASEI(umin)
7863 CASEI(imax)
7864 CASEI(umax)
7865 CASEI(iand)
7866 CASEI(ior)
7867 CASEI(ixor)
7868 CASEF(fadd)
7869 CASEF(fmul)
7870 CASEF(fmin)
7871 CASEF(fmax)
7872 default:
7873 unreachable("unknown reduction op");
7874 #undef CASEI
7875 #undef CASEF
7876 }
7877
7878 aco_opcode aco_op;
7879 switch (instr->intrinsic) {
7880 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7881 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7882 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7883 default:
7884 unreachable("unknown reduce intrinsic");
7885 }
7886
7887 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7888 reduce->operands[0] = Operand(src);
7889 // filled in by aco_reduce_assign.cpp, used internally as part of the
7890 // reduce sequence
7891 assert(dst.size() == 1 || dst.size() == 2);
7892 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7893 reduce->operands[2] = Operand(v1.as_linear());
7894
7895 Temp tmp_dst = bld.tmp(dst.regClass());
7896 reduce->definitions[0] = Definition(tmp_dst);
7897 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7898 reduce->definitions[2] = Definition();
7899 reduce->definitions[3] = Definition(scc, s1);
7900 reduce->definitions[4] = Definition();
7901 reduce->reduce_op = reduce_op;
7902 reduce->cluster_size = cluster_size;
7903 ctx->block->instructions.emplace_back(std::move(reduce));
7904
7905 emit_wqm(ctx, tmp_dst, dst);
7906 }
7907 break;
7908 }
7909 case nir_intrinsic_quad_broadcast: {
7910 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7911 if (!nir_dest_is_divergent(instr->dest)) {
7912 emit_uniform_subgroup(ctx, instr, src);
7913 } else {
7914 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7915 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7916 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7917
7918 if (instr->dest.ssa.bit_size == 1) {
7919 assert(src.regClass() == bld.lm);
7920 assert(dst.regClass() == bld.lm);
7921 uint32_t half_mask = 0x11111111u << lane;
7922 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7923 Temp tmp = bld.tmp(bld.lm);
7924 bld.sop1(Builder::s_wqm, Definition(tmp),
7925 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7926 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7927 emit_wqm(ctx, tmp, dst);
7928 } else if (instr->dest.ssa.bit_size == 8) {
7929 Temp tmp = bld.tmp(v1);
7930 if (ctx->program->chip_class >= GFX8)
7931 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7932 else
7933 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7934 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7935 } else if (instr->dest.ssa.bit_size == 16) {
7936 Temp tmp = bld.tmp(v1);
7937 if (ctx->program->chip_class >= GFX8)
7938 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7939 else
7940 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7941 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7942 } else if (instr->dest.ssa.bit_size == 32) {
7943 if (ctx->program->chip_class >= GFX8)
7944 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7945 else
7946 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), 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, (1 << 15) | dpp_ctrl));
7955 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | 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 }
7965 break;
7966 }
7967 case nir_intrinsic_quad_swap_horizontal:
7968 case nir_intrinsic_quad_swap_vertical:
7969 case nir_intrinsic_quad_swap_diagonal:
7970 case nir_intrinsic_quad_swizzle_amd: {
7971 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7972 if (!nir_dest_is_divergent(instr->dest)) {
7973 emit_uniform_subgroup(ctx, instr, src);
7974 break;
7975 }
7976 uint16_t dpp_ctrl = 0;
7977 switch (instr->intrinsic) {
7978 case nir_intrinsic_quad_swap_horizontal:
7979 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7980 break;
7981 case nir_intrinsic_quad_swap_vertical:
7982 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7983 break;
7984 case nir_intrinsic_quad_swap_diagonal:
7985 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7986 break;
7987 case nir_intrinsic_quad_swizzle_amd:
7988 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7989 break;
7990 default:
7991 break;
7992 }
7993 if (ctx->program->chip_class < GFX8)
7994 dpp_ctrl |= (1 << 15);
7995
7996 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7997 if (instr->dest.ssa.bit_size == 1) {
7998 assert(src.regClass() == bld.lm);
7999 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
8000 if (ctx->program->chip_class >= GFX8)
8001 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
8002 else
8003 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
8004 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
8005 emit_wqm(ctx, tmp, dst);
8006 } else if (instr->dest.ssa.bit_size == 8) {
8007 Temp tmp = bld.tmp(v1);
8008 if (ctx->program->chip_class >= GFX8)
8009 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
8010 else
8011 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
8012 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
8013 } else if (instr->dest.ssa.bit_size == 16) {
8014 Temp tmp = bld.tmp(v1);
8015 if (ctx->program->chip_class >= GFX8)
8016 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
8017 else
8018 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
8019 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
8020 } else if (instr->dest.ssa.bit_size == 32) {
8021 Temp tmp;
8022 if (ctx->program->chip_class >= GFX8)
8023 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
8024 else
8025 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
8026 emit_wqm(ctx, tmp, dst);
8027 } else if (instr->dest.ssa.bit_size == 64) {
8028 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
8029 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
8030 if (ctx->program->chip_class >= GFX8) {
8031 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
8032 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
8033 } else {
8034 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
8035 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
8036 }
8037 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8038 emit_split_vector(ctx, dst, 2);
8039 } else {
8040 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8041 nir_print_instr(&instr->instr, stderr);
8042 fprintf(stderr, "\n");
8043 }
8044 break;
8045 }
8046 case nir_intrinsic_masked_swizzle_amd: {
8047 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8048 if (!nir_dest_is_divergent(instr->dest)) {
8049 emit_uniform_subgroup(ctx, instr, src);
8050 break;
8051 }
8052 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8053 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
8054 if (instr->dest.ssa.bit_size == 1) {
8055 assert(src.regClass() == bld.lm);
8056 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
8057 src = emit_masked_swizzle(ctx, bld, src, mask);
8058 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
8059 emit_wqm(ctx, tmp, dst);
8060 } else if (dst.regClass() == v1b) {
8061 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
8062 emit_extract_vector(ctx, tmp, 0, dst);
8063 } else if (dst.regClass() == v2b) {
8064 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
8065 emit_extract_vector(ctx, tmp, 0, dst);
8066 } else if (dst.regClass() == v1) {
8067 emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask), dst);
8068 } else if (dst.regClass() == v2) {
8069 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
8070 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
8071 lo = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, lo, mask));
8072 hi = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, hi, mask));
8073 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8074 emit_split_vector(ctx, dst, 2);
8075 } else {
8076 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8077 nir_print_instr(&instr->instr, stderr);
8078 fprintf(stderr, "\n");
8079 }
8080 break;
8081 }
8082 case nir_intrinsic_write_invocation_amd: {
8083 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
8084 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
8085 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
8086 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8087 if (dst.regClass() == v1) {
8088 /* src2 is ignored for writelane. RA assigns the same reg for dst */
8089 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
8090 } else if (dst.regClass() == v2) {
8091 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
8092 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
8093 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
8094 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
8095 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
8096 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
8097 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8098 emit_split_vector(ctx, dst, 2);
8099 } else {
8100 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8101 nir_print_instr(&instr->instr, stderr);
8102 fprintf(stderr, "\n");
8103 }
8104 break;
8105 }
8106 case nir_intrinsic_mbcnt_amd: {
8107 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8108 RegClass rc = RegClass(src.type(), 1);
8109 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
8110 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
8111 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8112 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
8113 emit_wqm(ctx, wqm_tmp, dst);
8114 break;
8115 }
8116 case nir_intrinsic_load_helper_invocation: {
8117 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8118 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
8119 ctx->block->kind |= block_kind_needs_lowering;
8120 ctx->program->needs_exact = true;
8121 break;
8122 }
8123 case nir_intrinsic_is_helper_invocation: {
8124 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8125 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
8126 ctx->block->kind |= block_kind_needs_lowering;
8127 ctx->program->needs_exact = true;
8128 break;
8129 }
8130 case nir_intrinsic_demote:
8131 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
8132
8133 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8134 ctx->cf_info.exec_potentially_empty_discard = true;
8135 ctx->block->kind |= block_kind_uses_demote;
8136 ctx->program->needs_exact = true;
8137 break;
8138 case nir_intrinsic_demote_if: {
8139 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8140 assert(src.regClass() == bld.lm);
8141 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
8142 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
8143
8144 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8145 ctx->cf_info.exec_potentially_empty_discard = true;
8146 ctx->block->kind |= block_kind_uses_demote;
8147 ctx->program->needs_exact = true;
8148 break;
8149 }
8150 case nir_intrinsic_first_invocation: {
8151 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
8152 get_ssa_temp(ctx, &instr->dest.ssa));
8153 break;
8154 }
8155 case nir_intrinsic_shader_clock: {
8156 aco_opcode opcode =
8157 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
8158 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
8159 bld.smem(opcode, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), memory_sync_info(0, semantic_volatile));
8160 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
8161 break;
8162 }
8163 case nir_intrinsic_load_vertex_id_zero_base: {
8164 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8165 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
8166 break;
8167 }
8168 case nir_intrinsic_load_first_vertex: {
8169 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8170 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
8171 break;
8172 }
8173 case nir_intrinsic_load_base_instance: {
8174 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8175 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
8176 break;
8177 }
8178 case nir_intrinsic_load_instance_id: {
8179 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8180 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
8181 break;
8182 }
8183 case nir_intrinsic_load_draw_id: {
8184 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8185 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8186 break;
8187 }
8188 case nir_intrinsic_load_invocation_id: {
8189 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8190
8191 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8192 if (ctx->options->chip_class >= GFX10)
8193 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8194 else
8195 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8196 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8197 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8198 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8199 } else {
8200 unreachable("Unsupported stage for load_invocation_id");
8201 }
8202
8203 break;
8204 }
8205 case nir_intrinsic_load_primitive_id: {
8206 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8207
8208 switch (ctx->shader->info.stage) {
8209 case MESA_SHADER_GEOMETRY:
8210 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8211 break;
8212 case MESA_SHADER_TESS_CTRL:
8213 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8214 break;
8215 case MESA_SHADER_TESS_EVAL:
8216 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8217 break;
8218 default:
8219 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8220 }
8221
8222 break;
8223 }
8224 case nir_intrinsic_load_patch_vertices_in: {
8225 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8226 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8227
8228 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8229 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8230 break;
8231 }
8232 case nir_intrinsic_emit_vertex_with_counter: {
8233 visit_emit_vertex_with_counter(ctx, instr);
8234 break;
8235 }
8236 case nir_intrinsic_end_primitive_with_counter: {
8237 unsigned stream = nir_intrinsic_stream_id(instr);
8238 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8239 break;
8240 }
8241 case nir_intrinsic_set_vertex_count: {
8242 /* unused, the HW keeps track of this for us */
8243 break;
8244 }
8245 default:
8246 fprintf(stderr, "Unimplemented intrinsic instr: ");
8247 nir_print_instr(&instr->instr, stderr);
8248 fprintf(stderr, "\n");
8249 abort();
8250
8251 break;
8252 }
8253 }
8254
8255
8256 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8257 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8258 enum glsl_base_type *stype)
8259 {
8260 nir_deref_instr *texture_deref_instr = NULL;
8261 nir_deref_instr *sampler_deref_instr = NULL;
8262 int plane = -1;
8263
8264 for (unsigned i = 0; i < instr->num_srcs; i++) {
8265 switch (instr->src[i].src_type) {
8266 case nir_tex_src_texture_deref:
8267 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8268 break;
8269 case nir_tex_src_sampler_deref:
8270 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8271 break;
8272 case nir_tex_src_plane:
8273 plane = nir_src_as_int(instr->src[i].src);
8274 break;
8275 default:
8276 break;
8277 }
8278 }
8279
8280 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8281
8282 if (!sampler_deref_instr)
8283 sampler_deref_instr = texture_deref_instr;
8284
8285 if (plane >= 0) {
8286 assert(instr->op != nir_texop_txf_ms &&
8287 instr->op != nir_texop_samples_identical);
8288 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8289 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8290 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8291 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8292 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8293 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8294 } else {
8295 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8296 }
8297 if (samp_ptr) {
8298 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8299
8300 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8301 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8302 Builder bld(ctx->program, ctx->block);
8303
8304 /* to avoid unnecessary moves, we split and recombine sampler and image */
8305 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8306 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8307 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8308 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8309 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8310 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8311 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8312 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8313
8314 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8315 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8316 img[0], img[1], img[2], img[3],
8317 img[4], img[5], img[6], img[7]);
8318 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8319 samp[0], samp[1], samp[2], samp[3]);
8320 }
8321 }
8322 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8323 instr->op == nir_texop_samples_identical))
8324 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8325 }
8326
8327 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8328 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8329 {
8330 Builder bld(ctx->program, ctx->block);
8331
8332 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8333 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8334 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8335
8336 Operand neg_one(0xbf800000u);
8337 Operand one(0x3f800000u);
8338 Operand two(0x40000000u);
8339 Operand four(0x40800000u);
8340
8341 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8342 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8343 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8344
8345 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8346 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8347 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8348 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);
8349
8350 // select sc
8351 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8352 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8353 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8354 one, is_ma_y);
8355 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8356
8357 // select tc
8358 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8359 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8360 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8361
8362 // select ma
8363 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8364 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8365 deriv_z, is_ma_z);
8366 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8367 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8368 }
8369
8370 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8371 {
8372 Builder bld(ctx->program, ctx->block);
8373 Temp ma, tc, sc, id;
8374
8375 if (is_array) {
8376 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8377
8378 // see comment in ac_prepare_cube_coords()
8379 if (ctx->options->chip_class <= GFX8)
8380 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8381 }
8382
8383 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8384
8385 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8386 vop3a->operands[0] = Operand(ma);
8387 vop3a->abs[0] = true;
8388 Temp invma = bld.tmp(v1);
8389 vop3a->definitions[0] = Definition(invma);
8390 ctx->block->instructions.emplace_back(std::move(vop3a));
8391
8392 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8393 if (!is_deriv)
8394 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8395
8396 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8397 if (!is_deriv)
8398 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8399
8400 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8401
8402 if (is_deriv) {
8403 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8404 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8405
8406 for (unsigned i = 0; i < 2; i++) {
8407 // see comment in ac_prepare_cube_coords()
8408 Temp deriv_ma;
8409 Temp deriv_sc, deriv_tc;
8410 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8411 &deriv_ma, &deriv_sc, &deriv_tc);
8412
8413 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8414
8415 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8416 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8417 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8418 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8419 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8420 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8421 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8422 }
8423
8424 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8425 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8426 }
8427
8428 if (is_array)
8429 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8430 coords.resize(3);
8431 coords[0] = sc;
8432 coords[1] = tc;
8433 coords[2] = id;
8434 }
8435
8436 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8437 {
8438 if (vec->parent_instr->type != nir_instr_type_alu)
8439 return;
8440 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8441 if (vec_instr->op != nir_op_vec(vec->num_components))
8442 return;
8443
8444 for (unsigned i = 0; i < vec->num_components; i++) {
8445 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8446 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8447 }
8448 }
8449
8450 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8451 {
8452 Builder bld(ctx->program, ctx->block);
8453 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8454 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8455 has_clamped_lod = false;
8456 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8457 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8458 clamped_lod = Temp();
8459 std::vector<Temp> coords;
8460 std::vector<Temp> derivs;
8461 nir_const_value *sample_index_cv = NULL;
8462 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8463 enum glsl_base_type stype;
8464 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8465
8466 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8467 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8468 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8469 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8470
8471 for (unsigned i = 0; i < instr->num_srcs; i++) {
8472 switch (instr->src[i].src_type) {
8473 case nir_tex_src_coord: {
8474 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8475 for (unsigned i = 0; i < coord.size(); i++)
8476 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8477 break;
8478 }
8479 case nir_tex_src_bias:
8480 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8481 has_bias = true;
8482 break;
8483 case nir_tex_src_lod: {
8484 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8485
8486 if (val && val->f32 <= 0.0) {
8487 level_zero = true;
8488 } else {
8489 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8490 has_lod = true;
8491 }
8492 break;
8493 }
8494 case nir_tex_src_min_lod:
8495 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8496 has_clamped_lod = true;
8497 break;
8498 case nir_tex_src_comparator:
8499 if (instr->is_shadow) {
8500 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8501 has_compare = true;
8502 }
8503 break;
8504 case nir_tex_src_offset:
8505 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8506 get_const_vec(instr->src[i].src.ssa, const_offset);
8507 has_offset = true;
8508 break;
8509 case nir_tex_src_ddx:
8510 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8511 has_ddx = true;
8512 break;
8513 case nir_tex_src_ddy:
8514 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8515 has_ddy = true;
8516 break;
8517 case nir_tex_src_ms_index:
8518 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8519 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8520 has_sample_index = true;
8521 break;
8522 case nir_tex_src_texture_offset:
8523 case nir_tex_src_sampler_offset:
8524 default:
8525 break;
8526 }
8527 }
8528
8529 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8530 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8531
8532 if (instr->op == nir_texop_texture_samples) {
8533 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8534
8535 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8536 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8537 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 */));
8538
8539 Operand default_sample = Operand(1u);
8540 if (ctx->options->robust_buffer_access) {
8541 /* Extract the second dword of the descriptor, if it's
8542 * all zero, then it's a null descriptor.
8543 */
8544 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8545 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8546 default_sample = Operand(is_non_null_descriptor);
8547 }
8548
8549 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8550 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8551 samples, default_sample, bld.scc(is_msaa));
8552 return;
8553 }
8554
8555 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8556 aco_ptr<Instruction> tmp_instr;
8557 Temp acc, pack = Temp();
8558
8559 uint32_t pack_const = 0;
8560 for (unsigned i = 0; i < offset.size(); i++) {
8561 if (!const_offset[i])
8562 continue;
8563 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8564 }
8565
8566 if (offset.type() == RegType::sgpr) {
8567 for (unsigned i = 0; i < offset.size(); i++) {
8568 if (const_offset[i])
8569 continue;
8570
8571 acc = emit_extract_vector(ctx, offset, i, s1);
8572 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8573
8574 if (i) {
8575 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8576 }
8577
8578 if (pack == Temp()) {
8579 pack = acc;
8580 } else {
8581 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8582 }
8583 }
8584
8585 if (pack_const && pack != Temp())
8586 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8587 } else {
8588 for (unsigned i = 0; i < offset.size(); i++) {
8589 if (const_offset[i])
8590 continue;
8591
8592 acc = emit_extract_vector(ctx, offset, i, v1);
8593 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8594
8595 if (i) {
8596 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8597 }
8598
8599 if (pack == Temp()) {
8600 pack = acc;
8601 } else {
8602 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8603 }
8604 }
8605
8606 if (pack_const && pack != Temp())
8607 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8608 }
8609 if (pack_const && pack == Temp())
8610 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8611 else if (pack == Temp())
8612 has_offset = false;
8613 else
8614 offset = pack;
8615 }
8616
8617 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8618 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8619
8620 /* pack derivatives */
8621 if (has_ddx || has_ddy) {
8622 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8623 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8624 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8625 derivs = {ddx, zero, ddy, zero};
8626 } else {
8627 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8628 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8629 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8630 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8631 }
8632 has_derivs = true;
8633 }
8634
8635 if (instr->coord_components > 1 &&
8636 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8637 instr->is_array &&
8638 instr->op != nir_texop_txf)
8639 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8640
8641 if (instr->coord_components > 2 &&
8642 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8643 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8644 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8645 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8646 instr->is_array &&
8647 instr->op != nir_texop_txf &&
8648 instr->op != nir_texop_txf_ms &&
8649 instr->op != nir_texop_fragment_fetch &&
8650 instr->op != nir_texop_fragment_mask_fetch)
8651 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8652
8653 if (ctx->options->chip_class == GFX9 &&
8654 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8655 instr->op != nir_texop_lod && instr->coord_components) {
8656 assert(coords.size() > 0 && coords.size() < 3);
8657
8658 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8659 Operand((uint32_t) 0) :
8660 Operand((uint32_t) 0x3f000000)));
8661 }
8662
8663 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8664
8665 if (instr->op == nir_texop_samples_identical)
8666 resource = fmask_ptr;
8667
8668 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8669 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8670 instr->op != nir_texop_txs &&
8671 instr->op != nir_texop_fragment_fetch &&
8672 instr->op != nir_texop_fragment_mask_fetch) {
8673 assert(has_sample_index);
8674 Operand op(sample_index);
8675 if (sample_index_cv)
8676 op = Operand(sample_index_cv->u32);
8677 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8678 }
8679
8680 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8681 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8682 Temp off = emit_extract_vector(ctx, offset, i, v1);
8683 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8684 }
8685 has_offset = false;
8686 }
8687
8688 /* Build tex instruction */
8689 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8690 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8691 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8692 : 0;
8693 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8694 Temp tmp_dst = dst;
8695
8696 /* gather4 selects the component by dmask and always returns vec4 */
8697 if (instr->op == nir_texop_tg4) {
8698 assert(instr->dest.ssa.num_components == 4);
8699 if (instr->is_shadow)
8700 dmask = 1;
8701 else
8702 dmask = 1 << instr->component;
8703 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8704 tmp_dst = bld.tmp(v4);
8705 } else if (instr->op == nir_texop_samples_identical) {
8706 tmp_dst = bld.tmp(v1);
8707 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8708 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8709 }
8710
8711 aco_ptr<MIMG_instruction> tex;
8712 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8713 if (!has_lod)
8714 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8715
8716 bool div_by_6 = instr->op == nir_texop_txs &&
8717 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8718 instr->is_array &&
8719 (dmask & (1 << 2));
8720 if (tmp_dst.id() == dst.id() && div_by_6)
8721 tmp_dst = bld.tmp(tmp_dst.regClass());
8722
8723 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8724 tex->operands[0] = Operand(resource);
8725 tex->operands[1] = Operand(s4); /* no sampler */
8726 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8727 if (ctx->options->chip_class == GFX9 &&
8728 instr->op == nir_texop_txs &&
8729 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8730 instr->is_array) {
8731 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8732 } else if (instr->op == nir_texop_query_levels) {
8733 tex->dmask = 1 << 3;
8734 } else {
8735 tex->dmask = dmask;
8736 }
8737 tex->da = da;
8738 tex->definitions[0] = Definition(tmp_dst);
8739 tex->dim = dim;
8740 ctx->block->instructions.emplace_back(std::move(tex));
8741
8742 if (div_by_6) {
8743 /* divide 3rd value by 6 by multiplying with magic number */
8744 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8745 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8746 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8747 assert(instr->dest.ssa.num_components == 3);
8748 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8749 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8750 emit_extract_vector(ctx, tmp_dst, 0, v1),
8751 emit_extract_vector(ctx, tmp_dst, 1, v1),
8752 by_6);
8753
8754 }
8755
8756 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8757 return;
8758 }
8759
8760 Temp tg4_compare_cube_wa64 = Temp();
8761
8762 if (tg4_integer_workarounds) {
8763 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8764 tex->operands[0] = Operand(resource);
8765 tex->operands[1] = Operand(s4); /* no sampler */
8766 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8767 tex->dim = dim;
8768 tex->dmask = 0x3;
8769 tex->da = da;
8770 Temp size = bld.tmp(v2);
8771 tex->definitions[0] = Definition(size);
8772 ctx->block->instructions.emplace_back(std::move(tex));
8773 emit_split_vector(ctx, size, size.size());
8774
8775 Temp half_texel[2];
8776 for (unsigned i = 0; i < 2; i++) {
8777 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8778 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8779 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8780 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8781 }
8782
8783 Temp new_coords[2] = {
8784 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8785 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8786 };
8787
8788 if (tg4_integer_cube_workaround) {
8789 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8790 Temp desc[resource.size()];
8791 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8792 Format::PSEUDO, 1, resource.size())};
8793 split->operands[0] = Operand(resource);
8794 for (unsigned i = 0; i < resource.size(); i++) {
8795 desc[i] = bld.tmp(s1);
8796 split->definitions[i] = Definition(desc[i]);
8797 }
8798 ctx->block->instructions.emplace_back(std::move(split));
8799
8800 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8801 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8802 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8803
8804 Temp nfmt;
8805 if (stype == GLSL_TYPE_UINT) {
8806 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8807 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8808 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8809 bld.scc(compare_cube_wa));
8810 } else {
8811 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8812 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8813 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8814 bld.scc(compare_cube_wa));
8815 }
8816 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8817 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8818
8819 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8820
8821 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8822 Operand((uint32_t)C_008F14_NUM_FORMAT));
8823 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8824
8825 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8826 Format::PSEUDO, resource.size(), 1)};
8827 for (unsigned i = 0; i < resource.size(); i++)
8828 vec->operands[i] = Operand(desc[i]);
8829 resource = bld.tmp(resource.regClass());
8830 vec->definitions[0] = Definition(resource);
8831 ctx->block->instructions.emplace_back(std::move(vec));
8832
8833 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8834 new_coords[0], coords[0], tg4_compare_cube_wa64);
8835 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8836 new_coords[1], coords[1], tg4_compare_cube_wa64);
8837 }
8838 coords[0] = new_coords[0];
8839 coords[1] = new_coords[1];
8840 }
8841
8842 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8843 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8844
8845 assert(coords.size() == 1);
8846 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8847 aco_opcode op;
8848 switch (last_bit) {
8849 case 1:
8850 op = aco_opcode::buffer_load_format_x; break;
8851 case 2:
8852 op = aco_opcode::buffer_load_format_xy; break;
8853 case 3:
8854 op = aco_opcode::buffer_load_format_xyz; break;
8855 case 4:
8856 op = aco_opcode::buffer_load_format_xyzw; break;
8857 default:
8858 unreachable("Tex instruction loads more than 4 components.");
8859 }
8860
8861 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8862 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8863 tmp_dst = dst;
8864 else
8865 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8866
8867 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8868 mubuf->operands[0] = Operand(resource);
8869 mubuf->operands[1] = Operand(coords[0]);
8870 mubuf->operands[2] = Operand((uint32_t) 0);
8871 mubuf->definitions[0] = Definition(tmp_dst);
8872 mubuf->idxen = true;
8873 ctx->block->instructions.emplace_back(std::move(mubuf));
8874
8875 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8876 return;
8877 }
8878
8879 /* gather MIMG address components */
8880 std::vector<Temp> args;
8881 if (has_offset)
8882 args.emplace_back(offset);
8883 if (has_bias)
8884 args.emplace_back(bias);
8885 if (has_compare)
8886 args.emplace_back(compare);
8887 if (has_derivs)
8888 args.insert(args.end(), derivs.begin(), derivs.end());
8889
8890 args.insert(args.end(), coords.begin(), coords.end());
8891 if (has_sample_index)
8892 args.emplace_back(sample_index);
8893 if (has_lod)
8894 args.emplace_back(lod);
8895 if (has_clamped_lod)
8896 args.emplace_back(clamped_lod);
8897
8898 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8899 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8900 vec->definitions[0] = Definition(arg);
8901 for (unsigned i = 0; i < args.size(); i++)
8902 vec->operands[i] = Operand(args[i]);
8903 ctx->block->instructions.emplace_back(std::move(vec));
8904
8905
8906 if (instr->op == nir_texop_txf ||
8907 instr->op == nir_texop_txf_ms ||
8908 instr->op == nir_texop_samples_identical ||
8909 instr->op == nir_texop_fragment_fetch ||
8910 instr->op == nir_texop_fragment_mask_fetch) {
8911 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;
8912 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8913 tex->operands[0] = Operand(resource);
8914 tex->operands[1] = Operand(s4); /* no sampler */
8915 tex->operands[2] = Operand(arg);
8916 tex->dim = dim;
8917 tex->dmask = dmask;
8918 tex->unrm = true;
8919 tex->da = da;
8920 tex->definitions[0] = Definition(tmp_dst);
8921 ctx->block->instructions.emplace_back(std::move(tex));
8922
8923 if (instr->op == nir_texop_samples_identical) {
8924 assert(dmask == 1 && dst.regClass() == v1);
8925 assert(dst.id() != tmp_dst.id());
8926
8927 Temp tmp = bld.tmp(bld.lm);
8928 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8929 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8930
8931 } else {
8932 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8933 }
8934 return;
8935 }
8936
8937 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8938 aco_opcode opcode = aco_opcode::image_sample;
8939 if (has_offset) { /* image_sample_*_o */
8940 if (has_clamped_lod) {
8941 if (has_compare) {
8942 opcode = aco_opcode::image_sample_c_cl_o;
8943 if (has_derivs)
8944 opcode = aco_opcode::image_sample_c_d_cl_o;
8945 if (has_bias)
8946 opcode = aco_opcode::image_sample_c_b_cl_o;
8947 } else {
8948 opcode = aco_opcode::image_sample_cl_o;
8949 if (has_derivs)
8950 opcode = aco_opcode::image_sample_d_cl_o;
8951 if (has_bias)
8952 opcode = aco_opcode::image_sample_b_cl_o;
8953 }
8954 } else if (has_compare) {
8955 opcode = aco_opcode::image_sample_c_o;
8956 if (has_derivs)
8957 opcode = aco_opcode::image_sample_c_d_o;
8958 if (has_bias)
8959 opcode = aco_opcode::image_sample_c_b_o;
8960 if (level_zero)
8961 opcode = aco_opcode::image_sample_c_lz_o;
8962 if (has_lod)
8963 opcode = aco_opcode::image_sample_c_l_o;
8964 } else {
8965 opcode = aco_opcode::image_sample_o;
8966 if (has_derivs)
8967 opcode = aco_opcode::image_sample_d_o;
8968 if (has_bias)
8969 opcode = aco_opcode::image_sample_b_o;
8970 if (level_zero)
8971 opcode = aco_opcode::image_sample_lz_o;
8972 if (has_lod)
8973 opcode = aco_opcode::image_sample_l_o;
8974 }
8975 } else if (has_clamped_lod) { /* image_sample_*_cl */
8976 if (has_compare) {
8977 opcode = aco_opcode::image_sample_c_cl;
8978 if (has_derivs)
8979 opcode = aco_opcode::image_sample_c_d_cl;
8980 if (has_bias)
8981 opcode = aco_opcode::image_sample_c_b_cl;
8982 } else {
8983 opcode = aco_opcode::image_sample_cl;
8984 if (has_derivs)
8985 opcode = aco_opcode::image_sample_d_cl;
8986 if (has_bias)
8987 opcode = aco_opcode::image_sample_b_cl;
8988 }
8989 } else { /* no offset */
8990 if (has_compare) {
8991 opcode = aco_opcode::image_sample_c;
8992 if (has_derivs)
8993 opcode = aco_opcode::image_sample_c_d;
8994 if (has_bias)
8995 opcode = aco_opcode::image_sample_c_b;
8996 if (level_zero)
8997 opcode = aco_opcode::image_sample_c_lz;
8998 if (has_lod)
8999 opcode = aco_opcode::image_sample_c_l;
9000 } else {
9001 opcode = aco_opcode::image_sample;
9002 if (has_derivs)
9003 opcode = aco_opcode::image_sample_d;
9004 if (has_bias)
9005 opcode = aco_opcode::image_sample_b;
9006 if (level_zero)
9007 opcode = aco_opcode::image_sample_lz;
9008 if (has_lod)
9009 opcode = aco_opcode::image_sample_l;
9010 }
9011 }
9012
9013 if (instr->op == nir_texop_tg4) {
9014 if (has_offset) { /* image_gather4_*_o */
9015 if (has_compare) {
9016 opcode = aco_opcode::image_gather4_c_lz_o;
9017 if (has_lod)
9018 opcode = aco_opcode::image_gather4_c_l_o;
9019 if (has_bias)
9020 opcode = aco_opcode::image_gather4_c_b_o;
9021 } else {
9022 opcode = aco_opcode::image_gather4_lz_o;
9023 if (has_lod)
9024 opcode = aco_opcode::image_gather4_l_o;
9025 if (has_bias)
9026 opcode = aco_opcode::image_gather4_b_o;
9027 }
9028 } else {
9029 if (has_compare) {
9030 opcode = aco_opcode::image_gather4_c_lz;
9031 if (has_lod)
9032 opcode = aco_opcode::image_gather4_c_l;
9033 if (has_bias)
9034 opcode = aco_opcode::image_gather4_c_b;
9035 } else {
9036 opcode = aco_opcode::image_gather4_lz;
9037 if (has_lod)
9038 opcode = aco_opcode::image_gather4_l;
9039 if (has_bias)
9040 opcode = aco_opcode::image_gather4_b;
9041 }
9042 }
9043 } else if (instr->op == nir_texop_lod) {
9044 opcode = aco_opcode::image_get_lod;
9045 }
9046
9047 /* we don't need the bias, sample index, compare value or offset to be
9048 * computed in WQM but if the p_create_vector copies the coordinates, then it
9049 * needs to be in WQM */
9050 if (ctx->stage == fragment_fs &&
9051 !has_derivs && !has_lod && !level_zero &&
9052 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
9053 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
9054 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
9055
9056 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
9057 tex->operands[0] = Operand(resource);
9058 tex->operands[1] = Operand(sampler);
9059 tex->operands[2] = Operand(arg);
9060 tex->dim = dim;
9061 tex->dmask = dmask;
9062 tex->da = da;
9063 tex->definitions[0] = Definition(tmp_dst);
9064 ctx->block->instructions.emplace_back(std::move(tex));
9065
9066 if (tg4_integer_cube_workaround) {
9067 assert(tmp_dst.id() != dst.id());
9068 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
9069
9070 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
9071 Temp val[4];
9072 for (unsigned i = 0; i < dst.size(); i++) {
9073 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
9074 Temp cvt_val;
9075 if (stype == GLSL_TYPE_UINT)
9076 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
9077 else
9078 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
9079 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
9080 }
9081 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
9082 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
9083 val[0], val[1], val[2], val[3]);
9084 }
9085 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
9086 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
9087
9088 }
9089
9090
9091 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa, RegClass rc, bool logical)
9092 {
9093 Temp tmp = get_ssa_temp(ctx, ssa);
9094 if (ssa->parent_instr->type == nir_instr_type_ssa_undef) {
9095 return Operand(rc);
9096 } else if (logical && ssa->bit_size == 1 && ssa->parent_instr->type == nir_instr_type_load_const) {
9097 if (ctx->program->wave_size == 64)
9098 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT64_MAX : 0u);
9099 else
9100 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT32_MAX : 0u);
9101 } else {
9102 return Operand(tmp);
9103 }
9104 }
9105
9106 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
9107 {
9108 aco_ptr<Pseudo_instruction> phi;
9109 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
9110 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
9111
9112 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
9113 logical |= ctx->block->kind & block_kind_merge;
9114 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
9115
9116 /* we want a sorted list of sources, since the predecessor list is also sorted */
9117 std::map<unsigned, nir_ssa_def*> phi_src;
9118 nir_foreach_phi_src(src, instr)
9119 phi_src[src->pred->index] = src->src.ssa;
9120
9121 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
9122 unsigned num_operands = 0;
9123 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
9124 unsigned num_defined = 0;
9125 unsigned cur_pred_idx = 0;
9126 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
9127 if (cur_pred_idx < preds.size()) {
9128 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
9129 unsigned block = ctx->cf_info.nir_to_aco[src.first];
9130 unsigned skipped = 0;
9131 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
9132 skipped++;
9133 if (cur_pred_idx + skipped < preds.size()) {
9134 for (unsigned i = 0; i < skipped; i++)
9135 operands[num_operands++] = Operand(dst.regClass());
9136 cur_pred_idx += skipped;
9137 } else {
9138 continue;
9139 }
9140 }
9141 /* Handle missing predecessors at the end. This shouldn't happen with loop
9142 * headers and we can't ignore these sources for loop header phis. */
9143 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
9144 continue;
9145 cur_pred_idx++;
9146 Operand op = get_phi_operand(ctx, src.second, dst.regClass(), logical);
9147 operands[num_operands++] = op;
9148 num_defined += !op.isUndefined();
9149 }
9150 /* handle block_kind_continue_or_break at loop exit blocks */
9151 while (cur_pred_idx++ < preds.size())
9152 operands[num_operands++] = Operand(dst.regClass());
9153
9154 /* If the loop ends with a break, still add a linear continue edge in case
9155 * that break is divergent or continue_or_break is used. We'll either remove
9156 * this operand later in visit_loop() if it's not necessary or replace the
9157 * undef with something correct. */
9158 if (!logical && ctx->block->kind & block_kind_loop_header) {
9159 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
9160 nir_block *last = nir_loop_last_block(loop);
9161 if (last->successors[0] != instr->instr.block)
9162 operands[num_operands++] = Operand(RegClass());
9163 }
9164
9165 if (num_defined == 0) {
9166 Builder bld(ctx->program, ctx->block);
9167 if (dst.regClass() == s1) {
9168 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
9169 } else if (dst.regClass() == v1) {
9170 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
9171 } else {
9172 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9173 for (unsigned i = 0; i < dst.size(); i++)
9174 vec->operands[i] = Operand(0u);
9175 vec->definitions[0] = Definition(dst);
9176 ctx->block->instructions.emplace_back(std::move(vec));
9177 }
9178 return;
9179 }
9180
9181 /* we can use a linear phi in some cases if one src is undef */
9182 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9183 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9184
9185 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9186 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9187 assert(invert->kind & block_kind_invert);
9188
9189 unsigned then_block = invert->linear_preds[0];
9190
9191 Block* insert_block = NULL;
9192 for (unsigned i = 0; i < num_operands; i++) {
9193 Operand op = operands[i];
9194 if (op.isUndefined())
9195 continue;
9196 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9197 phi->operands[0] = op;
9198 break;
9199 }
9200 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9201 phi->operands[1] = Operand(dst.regClass());
9202 phi->definitions[0] = Definition(dst);
9203 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9204 return;
9205 }
9206
9207 /* try to scalarize vector phis */
9208 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9209 // TODO: scalarize linear phis on divergent ifs
9210 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9211 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9212 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9213 Operand src = operands[i];
9214 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9215 can_scalarize = false;
9216 }
9217 if (can_scalarize) {
9218 unsigned num_components = instr->dest.ssa.num_components;
9219 assert(dst.size() % num_components == 0);
9220 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9221
9222 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9223 for (unsigned k = 0; k < num_components; k++) {
9224 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9225 for (unsigned i = 0; i < num_operands; i++) {
9226 Operand src = operands[i];
9227 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9228 }
9229 Temp phi_dst = {ctx->program->allocateId(), rc};
9230 phi->definitions[0] = Definition(phi_dst);
9231 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9232 new_vec[k] = phi_dst;
9233 vec->operands[k] = Operand(phi_dst);
9234 }
9235 vec->definitions[0] = Definition(dst);
9236 ctx->block->instructions.emplace_back(std::move(vec));
9237 ctx->allocated_vec.emplace(dst.id(), new_vec);
9238 return;
9239 }
9240 }
9241
9242 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9243 for (unsigned i = 0; i < num_operands; i++)
9244 phi->operands[i] = operands[i];
9245 phi->definitions[0] = Definition(dst);
9246 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9247 }
9248
9249
9250 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9251 {
9252 Temp dst = get_ssa_temp(ctx, &instr->def);
9253
9254 assert(dst.type() == RegType::sgpr);
9255
9256 if (dst.size() == 1) {
9257 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9258 } else {
9259 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9260 for (unsigned i = 0; i < dst.size(); i++)
9261 vec->operands[i] = Operand(0u);
9262 vec->definitions[0] = Definition(dst);
9263 ctx->block->instructions.emplace_back(std::move(vec));
9264 }
9265 }
9266
9267 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9268 {
9269 Builder bld(ctx->program, ctx->block);
9270 Block *logical_target;
9271 append_logical_end(ctx->block);
9272 unsigned idx = ctx->block->index;
9273
9274 switch (instr->type) {
9275 case nir_jump_break:
9276 logical_target = ctx->cf_info.parent_loop.exit;
9277 add_logical_edge(idx, logical_target);
9278 ctx->block->kind |= block_kind_break;
9279
9280 if (!ctx->cf_info.parent_if.is_divergent &&
9281 !ctx->cf_info.parent_loop.has_divergent_continue) {
9282 /* uniform break - directly jump out of the loop */
9283 ctx->block->kind |= block_kind_uniform;
9284 ctx->cf_info.has_branch = true;
9285 bld.branch(aco_opcode::p_branch);
9286 add_linear_edge(idx, logical_target);
9287 return;
9288 }
9289 ctx->cf_info.parent_loop.has_divergent_branch = true;
9290 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9291 break;
9292 case nir_jump_continue:
9293 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9294 add_logical_edge(idx, logical_target);
9295 ctx->block->kind |= block_kind_continue;
9296
9297 if (ctx->cf_info.parent_if.is_divergent) {
9298 /* for potential uniform breaks after this continue,
9299 we must ensure that they are handled correctly */
9300 ctx->cf_info.parent_loop.has_divergent_continue = true;
9301 ctx->cf_info.parent_loop.has_divergent_branch = true;
9302 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9303 } else {
9304 /* uniform continue - directly jump to the loop header */
9305 ctx->block->kind |= block_kind_uniform;
9306 ctx->cf_info.has_branch = true;
9307 bld.branch(aco_opcode::p_branch);
9308 add_linear_edge(idx, logical_target);
9309 return;
9310 }
9311 break;
9312 default:
9313 fprintf(stderr, "Unknown NIR jump instr: ");
9314 nir_print_instr(&instr->instr, stderr);
9315 fprintf(stderr, "\n");
9316 abort();
9317 }
9318
9319 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9320 ctx->cf_info.exec_potentially_empty_break = true;
9321 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9322 }
9323
9324 /* remove critical edges from linear CFG */
9325 bld.branch(aco_opcode::p_branch);
9326 Block* break_block = ctx->program->create_and_insert_block();
9327 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9328 break_block->kind |= block_kind_uniform;
9329 add_linear_edge(idx, break_block);
9330 /* the loop_header pointer might be invalidated by this point */
9331 if (instr->type == nir_jump_continue)
9332 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9333 add_linear_edge(break_block->index, logical_target);
9334 bld.reset(break_block);
9335 bld.branch(aco_opcode::p_branch);
9336
9337 Block* continue_block = ctx->program->create_and_insert_block();
9338 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9339 add_linear_edge(idx, continue_block);
9340 append_logical_start(continue_block);
9341 ctx->block = continue_block;
9342 return;
9343 }
9344
9345 void visit_block(isel_context *ctx, nir_block *block)
9346 {
9347 nir_foreach_instr(instr, block) {
9348 switch (instr->type) {
9349 case nir_instr_type_alu:
9350 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9351 break;
9352 case nir_instr_type_load_const:
9353 visit_load_const(ctx, nir_instr_as_load_const(instr));
9354 break;
9355 case nir_instr_type_intrinsic:
9356 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9357 break;
9358 case nir_instr_type_tex:
9359 visit_tex(ctx, nir_instr_as_tex(instr));
9360 break;
9361 case nir_instr_type_phi:
9362 visit_phi(ctx, nir_instr_as_phi(instr));
9363 break;
9364 case nir_instr_type_ssa_undef:
9365 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9366 break;
9367 case nir_instr_type_deref:
9368 break;
9369 case nir_instr_type_jump:
9370 visit_jump(ctx, nir_instr_as_jump(instr));
9371 break;
9372 default:
9373 fprintf(stderr, "Unknown NIR instr type: ");
9374 nir_print_instr(instr, stderr);
9375 fprintf(stderr, "\n");
9376 //abort();
9377 }
9378 }
9379
9380 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9381 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9382 }
9383
9384
9385
9386 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9387 aco_ptr<Instruction>& header_phi, Operand *vals)
9388 {
9389 vals[0] = Operand(header_phi->definitions[0].getTemp());
9390 RegClass rc = vals[0].regClass();
9391
9392 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9393
9394 unsigned next_pred = 1;
9395
9396 for (unsigned idx = first + 1; idx <= last; idx++) {
9397 Block& block = ctx->program->blocks[idx];
9398 if (block.loop_nest_depth != loop_nest_depth) {
9399 vals[idx - first] = vals[idx - 1 - first];
9400 continue;
9401 }
9402
9403 if (block.kind & block_kind_continue) {
9404 vals[idx - first] = header_phi->operands[next_pred];
9405 next_pred++;
9406 continue;
9407 }
9408
9409 bool all_same = true;
9410 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9411 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9412
9413 Operand val;
9414 if (all_same) {
9415 val = vals[block.linear_preds[0] - first];
9416 } else {
9417 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9418 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9419 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9420 phi->operands[i] = vals[block.linear_preds[i] - first];
9421 val = Operand(Temp(ctx->program->allocateId(), rc));
9422 phi->definitions[0] = Definition(val.getTemp());
9423 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9424 }
9425 vals[idx - first] = val;
9426 }
9427
9428 return vals[last - first];
9429 }
9430
9431 static void visit_loop(isel_context *ctx, nir_loop *loop)
9432 {
9433 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9434 append_logical_end(ctx->block);
9435 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9436 Builder bld(ctx->program, ctx->block);
9437 bld.branch(aco_opcode::p_branch);
9438 unsigned loop_preheader_idx = ctx->block->index;
9439
9440 Block loop_exit = Block();
9441 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9442 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9443
9444 Block* loop_header = ctx->program->create_and_insert_block();
9445 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9446 loop_header->kind |= block_kind_loop_header;
9447 add_edge(loop_preheader_idx, loop_header);
9448 ctx->block = loop_header;
9449
9450 /* emit loop body */
9451 unsigned loop_header_idx = loop_header->index;
9452 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9453 append_logical_start(ctx->block);
9454 bool unreachable = visit_cf_list(ctx, &loop->body);
9455
9456 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9457 if (!ctx->cf_info.has_branch) {
9458 append_logical_end(ctx->block);
9459 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9460 /* Discards can result in code running with an empty exec mask.
9461 * This would result in divergent breaks not ever being taken. As a
9462 * workaround, break the loop when the loop mask is empty instead of
9463 * always continuing. */
9464 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9465 unsigned block_idx = ctx->block->index;
9466
9467 /* create helper blocks to avoid critical edges */
9468 Block *break_block = ctx->program->create_and_insert_block();
9469 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9470 break_block->kind = block_kind_uniform;
9471 bld.reset(break_block);
9472 bld.branch(aco_opcode::p_branch);
9473 add_linear_edge(block_idx, break_block);
9474 add_linear_edge(break_block->index, &loop_exit);
9475
9476 Block *continue_block = ctx->program->create_and_insert_block();
9477 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9478 continue_block->kind = block_kind_uniform;
9479 bld.reset(continue_block);
9480 bld.branch(aco_opcode::p_branch);
9481 add_linear_edge(block_idx, continue_block);
9482 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9483
9484 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9485 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9486 ctx->block = &ctx->program->blocks[block_idx];
9487 } else {
9488 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9489 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9490 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9491 else
9492 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9493 }
9494
9495 bld.reset(ctx->block);
9496 bld.branch(aco_opcode::p_branch);
9497 }
9498
9499 /* Fixup phis in loop header from unreachable blocks.
9500 * has_branch/has_divergent_branch also indicates if the loop ends with a
9501 * break/continue instruction, but we don't emit those if unreachable=true */
9502 if (unreachable) {
9503 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9504 bool linear = ctx->cf_info.has_branch;
9505 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9506 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9507 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9508 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9509 /* the last operand should be the one that needs to be removed */
9510 instr->operands.pop_back();
9511 } else if (!is_phi(instr)) {
9512 break;
9513 }
9514 }
9515 }
9516
9517 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9518 * and the previous one shouldn't both happen at once because a break in the
9519 * merge block would get CSE'd */
9520 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9521 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9522 Operand vals[num_vals];
9523 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9524 if (instr->opcode == aco_opcode::p_linear_phi) {
9525 if (ctx->cf_info.has_branch)
9526 instr->operands.pop_back();
9527 else
9528 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9529 } else if (!is_phi(instr)) {
9530 break;
9531 }
9532 }
9533 }
9534
9535 ctx->cf_info.has_branch = false;
9536
9537 // TODO: if the loop has not a single exit, we must add one °°
9538 /* emit loop successor block */
9539 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9540 append_logical_start(ctx->block);
9541
9542 #if 0
9543 // TODO: check if it is beneficial to not branch on continues
9544 /* trim linear phis in loop header */
9545 for (auto&& instr : loop_entry->instructions) {
9546 if (instr->opcode == aco_opcode::p_linear_phi) {
9547 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9548 new_phi->definitions[0] = instr->definitions[0];
9549 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9550 new_phi->operands[i] = instr->operands[i];
9551 /* check that the remaining operands are all the same */
9552 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9553 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9554 instr.swap(new_phi);
9555 } else if (instr->opcode == aco_opcode::p_phi) {
9556 continue;
9557 } else {
9558 break;
9559 }
9560 }
9561 #endif
9562 }
9563
9564 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9565 {
9566 ic->cond = cond;
9567
9568 append_logical_end(ctx->block);
9569 ctx->block->kind |= block_kind_branch;
9570
9571 /* branch to linear then block */
9572 assert(cond.regClass() == ctx->program->lane_mask);
9573 aco_ptr<Pseudo_branch_instruction> branch;
9574 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9575 branch->operands[0] = Operand(cond);
9576 ctx->block->instructions.push_back(std::move(branch));
9577
9578 ic->BB_if_idx = ctx->block->index;
9579 ic->BB_invert = Block();
9580 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9581 /* Invert blocks are intentionally not marked as top level because they
9582 * are not part of the logical cfg. */
9583 ic->BB_invert.kind |= block_kind_invert;
9584 ic->BB_endif = Block();
9585 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9586 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9587
9588 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9589 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9590 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9591 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9592 ctx->cf_info.parent_if.is_divergent = true;
9593
9594 /* divergent branches use cbranch_execz */
9595 ctx->cf_info.exec_potentially_empty_discard = false;
9596 ctx->cf_info.exec_potentially_empty_break = false;
9597 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9598
9599 /** emit logical then block */
9600 Block* BB_then_logical = ctx->program->create_and_insert_block();
9601 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9602 add_edge(ic->BB_if_idx, BB_then_logical);
9603 ctx->block = BB_then_logical;
9604 append_logical_start(BB_then_logical);
9605 }
9606
9607 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9608 {
9609 Block *BB_then_logical = ctx->block;
9610 append_logical_end(BB_then_logical);
9611 /* branch from logical then block to invert block */
9612 aco_ptr<Pseudo_branch_instruction> branch;
9613 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9614 BB_then_logical->instructions.emplace_back(std::move(branch));
9615 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9616 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9617 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9618 BB_then_logical->kind |= block_kind_uniform;
9619 assert(!ctx->cf_info.has_branch);
9620 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9621 ctx->cf_info.parent_loop.has_divergent_branch = false;
9622
9623 /** emit linear then block */
9624 Block* BB_then_linear = ctx->program->create_and_insert_block();
9625 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9626 BB_then_linear->kind |= block_kind_uniform;
9627 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9628 /* branch from linear then block to invert block */
9629 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9630 BB_then_linear->instructions.emplace_back(std::move(branch));
9631 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9632
9633 /** emit invert merge block */
9634 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9635 ic->invert_idx = ctx->block->index;
9636
9637 /* branch to linear else block (skip else) */
9638 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9639 branch->operands[0] = Operand(ic->cond);
9640 ctx->block->instructions.push_back(std::move(branch));
9641
9642 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9643 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9644 ic->exec_potentially_empty_break_depth_old =
9645 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9646 /* divergent branches use cbranch_execz */
9647 ctx->cf_info.exec_potentially_empty_discard = false;
9648 ctx->cf_info.exec_potentially_empty_break = false;
9649 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9650
9651 /** emit logical else block */
9652 Block* BB_else_logical = ctx->program->create_and_insert_block();
9653 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9654 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9655 add_linear_edge(ic->invert_idx, BB_else_logical);
9656 ctx->block = BB_else_logical;
9657 append_logical_start(BB_else_logical);
9658 }
9659
9660 static void end_divergent_if(isel_context *ctx, if_context *ic)
9661 {
9662 Block *BB_else_logical = ctx->block;
9663 append_logical_end(BB_else_logical);
9664
9665 /* branch from logical else block to endif block */
9666 aco_ptr<Pseudo_branch_instruction> branch;
9667 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9668 BB_else_logical->instructions.emplace_back(std::move(branch));
9669 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9670 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9671 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9672 BB_else_logical->kind |= block_kind_uniform;
9673
9674 assert(!ctx->cf_info.has_branch);
9675 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9676
9677
9678 /** emit linear else block */
9679 Block* BB_else_linear = ctx->program->create_and_insert_block();
9680 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9681 BB_else_linear->kind |= block_kind_uniform;
9682 add_linear_edge(ic->invert_idx, BB_else_linear);
9683
9684 /* branch from linear else block to endif block */
9685 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9686 BB_else_linear->instructions.emplace_back(std::move(branch));
9687 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9688
9689
9690 /** emit endif merge block */
9691 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9692 append_logical_start(ctx->block);
9693
9694
9695 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9696 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9697 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9698 ctx->cf_info.exec_potentially_empty_break_depth =
9699 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9700 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9701 !ctx->cf_info.parent_if.is_divergent) {
9702 ctx->cf_info.exec_potentially_empty_break = false;
9703 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9704 }
9705 /* uniform control flow never has an empty exec-mask */
9706 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9707 ctx->cf_info.exec_potentially_empty_discard = false;
9708 ctx->cf_info.exec_potentially_empty_break = false;
9709 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9710 }
9711 }
9712
9713 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9714 {
9715 assert(cond.regClass() == s1);
9716
9717 append_logical_end(ctx->block);
9718 ctx->block->kind |= block_kind_uniform;
9719
9720 aco_ptr<Pseudo_branch_instruction> branch;
9721 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9722 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9723 branch->operands[0] = Operand(cond);
9724 branch->operands[0].setFixed(scc);
9725 ctx->block->instructions.emplace_back(std::move(branch));
9726
9727 ic->BB_if_idx = ctx->block->index;
9728 ic->BB_endif = Block();
9729 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9730 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9731
9732 ctx->cf_info.has_branch = false;
9733 ctx->cf_info.parent_loop.has_divergent_branch = false;
9734
9735 /** emit then block */
9736 Block* BB_then = ctx->program->create_and_insert_block();
9737 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9738 add_edge(ic->BB_if_idx, BB_then);
9739 append_logical_start(BB_then);
9740 ctx->block = BB_then;
9741 }
9742
9743 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9744 {
9745 Block *BB_then = ctx->block;
9746
9747 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9748 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9749
9750 if (!ic->uniform_has_then_branch) {
9751 append_logical_end(BB_then);
9752 /* branch from then block to endif block */
9753 aco_ptr<Pseudo_branch_instruction> branch;
9754 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9755 BB_then->instructions.emplace_back(std::move(branch));
9756 add_linear_edge(BB_then->index, &ic->BB_endif);
9757 if (!ic->then_branch_divergent)
9758 add_logical_edge(BB_then->index, &ic->BB_endif);
9759 BB_then->kind |= block_kind_uniform;
9760 }
9761
9762 ctx->cf_info.has_branch = false;
9763 ctx->cf_info.parent_loop.has_divergent_branch = false;
9764
9765 /** emit else block */
9766 Block* BB_else = ctx->program->create_and_insert_block();
9767 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9768 add_edge(ic->BB_if_idx, BB_else);
9769 append_logical_start(BB_else);
9770 ctx->block = BB_else;
9771 }
9772
9773 static void end_uniform_if(isel_context *ctx, if_context *ic)
9774 {
9775 Block *BB_else = ctx->block;
9776
9777 if (!ctx->cf_info.has_branch) {
9778 append_logical_end(BB_else);
9779 /* branch from then block to endif block */
9780 aco_ptr<Pseudo_branch_instruction> branch;
9781 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9782 BB_else->instructions.emplace_back(std::move(branch));
9783 add_linear_edge(BB_else->index, &ic->BB_endif);
9784 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9785 add_logical_edge(BB_else->index, &ic->BB_endif);
9786 BB_else->kind |= block_kind_uniform;
9787 }
9788
9789 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9790 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9791
9792 /** emit endif merge block */
9793 if (!ctx->cf_info.has_branch) {
9794 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9795 append_logical_start(ctx->block);
9796 }
9797 }
9798
9799 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9800 {
9801 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9802 Builder bld(ctx->program, ctx->block);
9803 aco_ptr<Pseudo_branch_instruction> branch;
9804 if_context ic;
9805
9806 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9807 /**
9808 * Uniform conditionals are represented in the following way*) :
9809 *
9810 * The linear and logical CFG:
9811 * BB_IF
9812 * / \
9813 * BB_THEN (logical) BB_ELSE (logical)
9814 * \ /
9815 * BB_ENDIF
9816 *
9817 * *) Exceptions may be due to break and continue statements within loops
9818 * If a break/continue happens within uniform control flow, it branches
9819 * to the loop exit/entry block. Otherwise, it branches to the next
9820 * merge block.
9821 **/
9822
9823 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9824 assert(cond.regClass() == ctx->program->lane_mask);
9825 cond = bool_to_scalar_condition(ctx, cond);
9826
9827 begin_uniform_if_then(ctx, &ic, cond);
9828 visit_cf_list(ctx, &if_stmt->then_list);
9829
9830 begin_uniform_if_else(ctx, &ic);
9831 visit_cf_list(ctx, &if_stmt->else_list);
9832
9833 end_uniform_if(ctx, &ic);
9834 } else { /* non-uniform condition */
9835 /**
9836 * To maintain a logical and linear CFG without critical edges,
9837 * non-uniform conditionals are represented in the following way*) :
9838 *
9839 * The linear CFG:
9840 * BB_IF
9841 * / \
9842 * BB_THEN (logical) BB_THEN (linear)
9843 * \ /
9844 * BB_INVERT (linear)
9845 * / \
9846 * BB_ELSE (logical) BB_ELSE (linear)
9847 * \ /
9848 * BB_ENDIF
9849 *
9850 * The logical CFG:
9851 * BB_IF
9852 * / \
9853 * BB_THEN (logical) BB_ELSE (logical)
9854 * \ /
9855 * BB_ENDIF
9856 *
9857 * *) Exceptions may be due to break and continue statements within loops
9858 **/
9859
9860 begin_divergent_if_then(ctx, &ic, cond);
9861 visit_cf_list(ctx, &if_stmt->then_list);
9862
9863 begin_divergent_if_else(ctx, &ic);
9864 visit_cf_list(ctx, &if_stmt->else_list);
9865
9866 end_divergent_if(ctx, &ic);
9867 }
9868
9869 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9870 }
9871
9872 static bool visit_cf_list(isel_context *ctx,
9873 struct exec_list *list)
9874 {
9875 foreach_list_typed(nir_cf_node, node, node, list) {
9876 switch (node->type) {
9877 case nir_cf_node_block:
9878 visit_block(ctx, nir_cf_node_as_block(node));
9879 break;
9880 case nir_cf_node_if:
9881 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9882 return true;
9883 break;
9884 case nir_cf_node_loop:
9885 visit_loop(ctx, nir_cf_node_as_loop(node));
9886 break;
9887 default:
9888 unreachable("unimplemented cf list type");
9889 }
9890 }
9891 return false;
9892 }
9893
9894 static void create_null_export(isel_context *ctx)
9895 {
9896 /* Some shader stages always need to have exports.
9897 * So when there is none, we need to add a null export.
9898 */
9899
9900 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9901 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9902 Builder bld(ctx->program, ctx->block);
9903 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9904 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9905 }
9906
9907 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9908 {
9909 assert(ctx->stage == vertex_vs ||
9910 ctx->stage == tess_eval_vs ||
9911 ctx->stage == gs_copy_vs ||
9912 ctx->stage == ngg_vertex_gs ||
9913 ctx->stage == ngg_tess_eval_gs);
9914
9915 int offset = (ctx->stage & sw_tes)
9916 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9917 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9918 uint64_t mask = ctx->outputs.mask[slot];
9919 if (!is_pos && !mask)
9920 return false;
9921 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9922 return false;
9923 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9924 exp->enabled_mask = mask;
9925 for (unsigned i = 0; i < 4; ++i) {
9926 if (mask & (1 << i))
9927 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9928 else
9929 exp->operands[i] = Operand(v1);
9930 }
9931 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9932 * Setting valid_mask=1 prevents it and has no other effect.
9933 */
9934 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9935 exp->done = false;
9936 exp->compressed = false;
9937 if (is_pos)
9938 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9939 else
9940 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9941 ctx->block->instructions.emplace_back(std::move(exp));
9942
9943 return true;
9944 }
9945
9946 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9947 {
9948 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9949 exp->enabled_mask = 0;
9950 for (unsigned i = 0; i < 4; ++i)
9951 exp->operands[i] = Operand(v1);
9952 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9953 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9954 exp->enabled_mask |= 0x1;
9955 }
9956 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9957 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9958 exp->enabled_mask |= 0x4;
9959 }
9960 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9961 if (ctx->options->chip_class < GFX9) {
9962 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9963 exp->enabled_mask |= 0x8;
9964 } else {
9965 Builder bld(ctx->program, ctx->block);
9966
9967 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9968 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9969 if (exp->operands[2].isTemp())
9970 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9971
9972 exp->operands[2] = Operand(out);
9973 exp->enabled_mask |= 0x4;
9974 }
9975 }
9976 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9977 exp->done = false;
9978 exp->compressed = false;
9979 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9980 ctx->block->instructions.emplace_back(std::move(exp));
9981 }
9982
9983 static void create_export_phis(isel_context *ctx)
9984 {
9985 /* Used when exports are needed, but the output temps are defined in a preceding block.
9986 * This function will set up phis in order to access the outputs in the next block.
9987 */
9988
9989 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9990 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9991 ctx->block->instructions.pop_back();
9992
9993 Builder bld(ctx->program, ctx->block);
9994
9995 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9996 uint64_t mask = ctx->outputs.mask[slot];
9997 for (unsigned i = 0; i < 4; ++i) {
9998 if (!(mask & (1 << i)))
9999 continue;
10000
10001 Temp old = ctx->outputs.temps[slot * 4 + i];
10002 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
10003 ctx->outputs.temps[slot * 4 + i] = phi;
10004 }
10005 }
10006
10007 bld.insert(std::move(logical_start));
10008 }
10009
10010 static void create_vs_exports(isel_context *ctx)
10011 {
10012 assert(ctx->stage == vertex_vs ||
10013 ctx->stage == tess_eval_vs ||
10014 ctx->stage == gs_copy_vs ||
10015 ctx->stage == ngg_vertex_gs ||
10016 ctx->stage == ngg_tess_eval_gs);
10017
10018 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
10019 ? &ctx->program->info->tes.outinfo
10020 : &ctx->program->info->vs.outinfo;
10021
10022 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
10023 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10024 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
10025 }
10026
10027 if (ctx->options->key.has_multiview_view_index) {
10028 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
10029 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
10030 }
10031
10032 /* the order these position exports are created is important */
10033 int next_pos = 0;
10034 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
10035 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
10036 export_vs_psiz_layer_viewport(ctx, &next_pos);
10037 exported_pos = true;
10038 }
10039 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
10040 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
10041 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
10042 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
10043
10044 if (ctx->export_clip_dists) {
10045 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
10046 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
10047 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
10048 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
10049 }
10050
10051 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10052 if (i < VARYING_SLOT_VAR0 &&
10053 i != VARYING_SLOT_LAYER &&
10054 i != VARYING_SLOT_PRIMITIVE_ID &&
10055 i != VARYING_SLOT_VIEWPORT)
10056 continue;
10057
10058 export_vs_varying(ctx, i, false, NULL);
10059 }
10060
10061 if (!exported_pos)
10062 create_null_export(ctx);
10063 }
10064
10065 static bool export_fs_mrt_z(isel_context *ctx)
10066 {
10067 Builder bld(ctx->program, ctx->block);
10068 unsigned enabled_channels = 0;
10069 bool compr = false;
10070 Operand values[4];
10071
10072 for (unsigned i = 0; i < 4; ++i) {
10073 values[i] = Operand(v1);
10074 }
10075
10076 /* Both stencil and sample mask only need 16-bits. */
10077 if (!ctx->program->info->ps.writes_z &&
10078 (ctx->program->info->ps.writes_stencil ||
10079 ctx->program->info->ps.writes_sample_mask)) {
10080 compr = true; /* COMPR flag */
10081
10082 if (ctx->program->info->ps.writes_stencil) {
10083 /* Stencil should be in X[23:16]. */
10084 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10085 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
10086 enabled_channels |= 0x3;
10087 }
10088
10089 if (ctx->program->info->ps.writes_sample_mask) {
10090 /* SampleMask should be in Y[15:0]. */
10091 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10092 enabled_channels |= 0xc;
10093 }
10094 } else {
10095 if (ctx->program->info->ps.writes_z) {
10096 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
10097 enabled_channels |= 0x1;
10098 }
10099
10100 if (ctx->program->info->ps.writes_stencil) {
10101 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10102 enabled_channels |= 0x2;
10103 }
10104
10105 if (ctx->program->info->ps.writes_sample_mask) {
10106 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10107 enabled_channels |= 0x4;
10108 }
10109 }
10110
10111 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
10112 * writemask component.
10113 */
10114 if (ctx->options->chip_class == GFX6 &&
10115 ctx->options->family != CHIP_OLAND &&
10116 ctx->options->family != CHIP_HAINAN) {
10117 enabled_channels |= 0x1;
10118 }
10119
10120 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10121 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
10122
10123 return true;
10124 }
10125
10126 static bool export_fs_mrt_color(isel_context *ctx, int slot)
10127 {
10128 Builder bld(ctx->program, ctx->block);
10129 unsigned write_mask = ctx->outputs.mask[slot];
10130 Operand values[4];
10131
10132 for (unsigned i = 0; i < 4; ++i) {
10133 if (write_mask & (1 << i)) {
10134 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
10135 } else {
10136 values[i] = Operand(v1);
10137 }
10138 }
10139
10140 unsigned target, col_format;
10141 unsigned enabled_channels = 0;
10142 aco_opcode compr_op = (aco_opcode)0;
10143
10144 slot -= FRAG_RESULT_DATA0;
10145 target = V_008DFC_SQ_EXP_MRT + slot;
10146 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
10147
10148 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
10149 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
10150 bool is_16bit = values[0].regClass() == v2b;
10151
10152 switch (col_format)
10153 {
10154 case V_028714_SPI_SHADER_ZERO:
10155 enabled_channels = 0; /* writemask */
10156 target = V_008DFC_SQ_EXP_NULL;
10157 break;
10158
10159 case V_028714_SPI_SHADER_32_R:
10160 enabled_channels = 1;
10161 break;
10162
10163 case V_028714_SPI_SHADER_32_GR:
10164 enabled_channels = 0x3;
10165 break;
10166
10167 case V_028714_SPI_SHADER_32_AR:
10168 if (ctx->options->chip_class >= GFX10) {
10169 /* Special case: on GFX10, the outputs are different for 32_AR */
10170 enabled_channels = 0x3;
10171 values[1] = values[3];
10172 values[3] = Operand(v1);
10173 } else {
10174 enabled_channels = 0x9;
10175 }
10176 break;
10177
10178 case V_028714_SPI_SHADER_FP16_ABGR:
10179 enabled_channels = 0x5;
10180 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
10181 if (is_16bit) {
10182 if (ctx->options->chip_class >= GFX9) {
10183 /* Pack the FP16 values together instead of converting them to
10184 * FP32 and back to FP16.
10185 * TODO: use p_create_vector and let the compiler optimizes.
10186 */
10187 compr_op = aco_opcode::v_pack_b32_f16;
10188 } else {
10189 for (unsigned i = 0; i < 4; i++) {
10190 if ((write_mask >> i) & 1)
10191 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
10192 }
10193 }
10194 }
10195 break;
10196
10197 case V_028714_SPI_SHADER_UNORM16_ABGR:
10198 enabled_channels = 0x5;
10199 if (is_16bit && ctx->options->chip_class >= GFX9) {
10200 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
10201 } else {
10202 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10203 }
10204 break;
10205
10206 case V_028714_SPI_SHADER_SNORM16_ABGR:
10207 enabled_channels = 0x5;
10208 if (is_16bit && ctx->options->chip_class >= GFX9) {
10209 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
10210 } else {
10211 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10212 }
10213 break;
10214
10215 case V_028714_SPI_SHADER_UINT16_ABGR: {
10216 enabled_channels = 0x5;
10217 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10218 if (is_int8 || is_int10) {
10219 /* clamp */
10220 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10221 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10222
10223 for (unsigned i = 0; i < 4; i++) {
10224 if ((write_mask >> i) & 1) {
10225 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10226 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10227 values[i]);
10228 }
10229 }
10230 } else if (is_16bit) {
10231 for (unsigned i = 0; i < 4; i++) {
10232 if ((write_mask >> i) & 1) {
10233 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, false);
10234 values[i] = Operand(tmp);
10235 }
10236 }
10237 }
10238 break;
10239 }
10240
10241 case V_028714_SPI_SHADER_SINT16_ABGR:
10242 enabled_channels = 0x5;
10243 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10244 if (is_int8 || is_int10) {
10245 /* clamp */
10246 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10247 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10248 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10249 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10250
10251 for (unsigned i = 0; i < 4; i++) {
10252 if ((write_mask >> i) & 1) {
10253 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10254 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10255 values[i]);
10256 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10257 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10258 values[i]);
10259 }
10260 }
10261 } else if (is_16bit) {
10262 for (unsigned i = 0; i < 4; i++) {
10263 if ((write_mask >> i) & 1) {
10264 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, true);
10265 values[i] = Operand(tmp);
10266 }
10267 }
10268 }
10269 break;
10270
10271 case V_028714_SPI_SHADER_32_ABGR:
10272 enabled_channels = 0xF;
10273 break;
10274
10275 default:
10276 break;
10277 }
10278
10279 if (target == V_008DFC_SQ_EXP_NULL)
10280 return false;
10281
10282 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10283 if (ctx->options->enable_mrt_output_nan_fixup &&
10284 !is_16bit &&
10285 (col_format == V_028714_SPI_SHADER_32_R ||
10286 col_format == V_028714_SPI_SHADER_32_GR ||
10287 col_format == V_028714_SPI_SHADER_32_AR ||
10288 col_format == V_028714_SPI_SHADER_32_ABGR ||
10289 col_format == V_028714_SPI_SHADER_FP16_ABGR)) {
10290 for (int i = 0; i < 4; i++) {
10291 if (!(write_mask & (1 << i)))
10292 continue;
10293
10294 Temp isnan = bld.vopc(aco_opcode::v_cmp_class_f32,
10295 bld.hint_vcc(bld.def(bld.lm)), values[i],
10296 bld.copy(bld.def(v1), Operand(3u)));
10297 values[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), values[i],
10298 bld.copy(bld.def(v1), Operand(0u)), isnan);
10299 }
10300 }
10301
10302 if ((bool) compr_op) {
10303 for (int i = 0; i < 2; i++) {
10304 /* check if at least one of the values to be compressed is enabled */
10305 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10306 if (enabled) {
10307 enabled_channels |= enabled << (i*2);
10308 values[i] = bld.vop3(compr_op, bld.def(v1),
10309 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10310 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10311 } else {
10312 values[i] = Operand(v1);
10313 }
10314 }
10315 values[2] = Operand(v1);
10316 values[3] = Operand(v1);
10317 } else {
10318 for (int i = 0; i < 4; i++)
10319 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10320 }
10321
10322 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10323 enabled_channels, target, (bool) compr_op);
10324 return true;
10325 }
10326
10327 static void create_fs_exports(isel_context *ctx)
10328 {
10329 bool exported = false;
10330
10331 /* Export depth, stencil and sample mask. */
10332 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10333 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10334 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10335 exported |= export_fs_mrt_z(ctx);
10336
10337 /* Export all color render targets. */
10338 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10339 if (ctx->outputs.mask[i])
10340 exported |= export_fs_mrt_color(ctx, i);
10341
10342 if (!exported)
10343 create_null_export(ctx);
10344 }
10345
10346 static void create_workgroup_barrier(Builder& bld)
10347 {
10348 bld.barrier(aco_opcode::p_barrier,
10349 memory_sync_info(storage_shared, semantic_acqrel, scope_workgroup),
10350 scope_workgroup);
10351 }
10352
10353 static void write_tcs_tess_factors(isel_context *ctx)
10354 {
10355 unsigned outer_comps;
10356 unsigned inner_comps;
10357
10358 switch (ctx->args->options->key.tcs.primitive_mode) {
10359 case GL_ISOLINES:
10360 outer_comps = 2;
10361 inner_comps = 0;
10362 break;
10363 case GL_TRIANGLES:
10364 outer_comps = 3;
10365 inner_comps = 1;
10366 break;
10367 case GL_QUADS:
10368 outer_comps = 4;
10369 inner_comps = 2;
10370 break;
10371 default:
10372 return;
10373 }
10374
10375 Builder bld(ctx->program, ctx->block);
10376
10377 create_workgroup_barrier(bld);
10378
10379 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10380 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10381
10382 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10383 if_context ic_invocation_id_is_zero;
10384 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10385 bld.reset(ctx->block);
10386
10387 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));
10388
10389 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10390 unsigned stride = inner_comps + outer_comps;
10391 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10392 Temp tf_inner_vec;
10393 Temp tf_outer_vec;
10394 Temp out[6];
10395 assert(stride <= (sizeof(out) / sizeof(Temp)));
10396
10397 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10398 // LINES reversal
10399 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10400 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10401 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10402 } else {
10403 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);
10404 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);
10405
10406 for (unsigned i = 0; i < outer_comps; ++i)
10407 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10408 for (unsigned i = 0; i < inner_comps; ++i)
10409 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10410 }
10411
10412 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10413 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10414 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10415 unsigned tf_const_offset = 0;
10416
10417 if (ctx->program->chip_class <= GFX8) {
10418 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);
10419 if_context ic_rel_patch_id_is_zero;
10420 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10421 bld.reset(ctx->block);
10422
10423 /* Store the dynamic HS control word. */
10424 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10425 bld.mubuf(aco_opcode::buffer_store_dword,
10426 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10427 /* immediate OFFSET */ 0, /* OFFEN */ false, /* swizzled */ false, /* idxen*/ false,
10428 /* addr64 */ false, /* disable_wqm */ false, /* glc */ true);
10429 tf_const_offset += 4;
10430
10431 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10432 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10433 bld.reset(ctx->block);
10434 }
10435
10436 assert(stride == 2 || stride == 4 || stride == 6);
10437 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10438 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10439
10440 /* Store to offchip for TES to read - only if TES reads them */
10441 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10442 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));
10443 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10444
10445 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10446 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);
10447
10448 if (likely(inner_comps)) {
10449 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10450 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);
10451 }
10452 }
10453
10454 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10455 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10456 }
10457
10458 static void emit_stream_output(isel_context *ctx,
10459 Temp const *so_buffers,
10460 Temp const *so_write_offset,
10461 const struct radv_stream_output *output)
10462 {
10463 unsigned num_comps = util_bitcount(output->component_mask);
10464 unsigned writemask = (1 << num_comps) - 1;
10465 unsigned loc = output->location;
10466 unsigned buf = output->buffer;
10467
10468 assert(num_comps && num_comps <= 4);
10469 if (!num_comps || num_comps > 4)
10470 return;
10471
10472 unsigned start = ffs(output->component_mask) - 1;
10473
10474 Temp out[4];
10475 bool all_undef = true;
10476 assert(ctx->stage & hw_vs);
10477 for (unsigned i = 0; i < num_comps; i++) {
10478 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10479 all_undef = all_undef && !out[i].id();
10480 }
10481 if (all_undef)
10482 return;
10483
10484 while (writemask) {
10485 int start, count;
10486 u_bit_scan_consecutive_range(&writemask, &start, &count);
10487 if (count == 3 && ctx->options->chip_class == GFX6) {
10488 /* GFX6 doesn't support storing vec3, split it. */
10489 writemask |= 1u << (start + 2);
10490 count = 2;
10491 }
10492
10493 unsigned offset = output->offset + start * 4;
10494
10495 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10496 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10497 for (int i = 0; i < count; ++i)
10498 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10499 vec->definitions[0] = Definition(write_data);
10500 ctx->block->instructions.emplace_back(std::move(vec));
10501
10502 aco_opcode opcode;
10503 switch (count) {
10504 case 1:
10505 opcode = aco_opcode::buffer_store_dword;
10506 break;
10507 case 2:
10508 opcode = aco_opcode::buffer_store_dwordx2;
10509 break;
10510 case 3:
10511 opcode = aco_opcode::buffer_store_dwordx3;
10512 break;
10513 case 4:
10514 opcode = aco_opcode::buffer_store_dwordx4;
10515 break;
10516 default:
10517 unreachable("Unsupported dword count.");
10518 }
10519
10520 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10521 store->operands[0] = Operand(so_buffers[buf]);
10522 store->operands[1] = Operand(so_write_offset[buf]);
10523 store->operands[2] = Operand((uint32_t) 0);
10524 store->operands[3] = Operand(write_data);
10525 if (offset > 4095) {
10526 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10527 Builder bld(ctx->program, ctx->block);
10528 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10529 } else {
10530 store->offset = offset;
10531 }
10532 store->offen = true;
10533 store->glc = true;
10534 store->dlc = false;
10535 store->slc = true;
10536 ctx->block->instructions.emplace_back(std::move(store));
10537 }
10538 }
10539
10540 static void emit_streamout(isel_context *ctx, unsigned stream)
10541 {
10542 Builder bld(ctx->program, ctx->block);
10543
10544 Temp so_buffers[4];
10545 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10546 for (unsigned i = 0; i < 4; i++) {
10547 unsigned stride = ctx->program->info->so.strides[i];
10548 if (!stride)
10549 continue;
10550
10551 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10552 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10553 }
10554
10555 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10556 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10557
10558 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10559
10560 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10561
10562 if_context ic;
10563 begin_divergent_if_then(ctx, &ic, can_emit);
10564
10565 bld.reset(ctx->block);
10566
10567 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10568
10569 Temp so_write_offset[4];
10570
10571 for (unsigned i = 0; i < 4; i++) {
10572 unsigned stride = ctx->program->info->so.strides[i];
10573 if (!stride)
10574 continue;
10575
10576 if (stride == 1) {
10577 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10578 get_arg(ctx, ctx->args->streamout_write_idx),
10579 get_arg(ctx, ctx->args->streamout_offset[i]));
10580 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10581
10582 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10583 } else {
10584 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10585 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10586 get_arg(ctx, ctx->args->streamout_offset[i]));
10587 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10588 }
10589 }
10590
10591 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10592 struct radv_stream_output *output =
10593 &ctx->program->info->so.outputs[i];
10594 if (stream != output->stream)
10595 continue;
10596
10597 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10598 }
10599
10600 begin_divergent_if_else(ctx, &ic);
10601 end_divergent_if(ctx, &ic);
10602 }
10603
10604 } /* end namespace */
10605
10606 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10607 {
10608 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10609 Builder bld(ctx->program, ctx->block);
10610 constexpr unsigned hs_idx = 1u;
10611 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10612 get_arg(ctx, ctx->args->merged_wave_info),
10613 Operand((8u << 16) | (hs_idx * 8u)));
10614 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10615
10616 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10617
10618 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10619 get_arg(ctx, ctx->args->rel_auto_id),
10620 get_arg(ctx, ctx->args->ac.instance_id),
10621 ls_has_nonzero_hs_threads);
10622 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10623 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10624 get_arg(ctx, ctx->args->rel_auto_id),
10625 ls_has_nonzero_hs_threads);
10626 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10627 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10628 get_arg(ctx, ctx->args->ac.vertex_id),
10629 ls_has_nonzero_hs_threads);
10630
10631 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10632 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10633 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10634 }
10635
10636 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10637 {
10638 /* Split all arguments except for the first (ring_offsets) and the last
10639 * (exec) so that the dead channels don't stay live throughout the program.
10640 */
10641 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10642 if (startpgm->definitions[i].regClass().size() > 1) {
10643 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10644 startpgm->definitions[i].regClass().size());
10645 }
10646 }
10647 }
10648
10649 void handle_bc_optimize(isel_context *ctx)
10650 {
10651 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10652 Builder bld(ctx->program, ctx->block);
10653 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10654 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10655 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10656 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10657 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10658 if (uses_center && uses_centroid) {
10659 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10660 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10661
10662 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10663 Temp new_coord[2];
10664 for (unsigned i = 0; i < 2; i++) {
10665 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10666 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10667 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10668 persp_centroid, persp_center, sel);
10669 }
10670 ctx->persp_centroid = bld.tmp(v2);
10671 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10672 Operand(new_coord[0]), Operand(new_coord[1]));
10673 emit_split_vector(ctx, ctx->persp_centroid, 2);
10674 }
10675
10676 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10677 Temp new_coord[2];
10678 for (unsigned i = 0; i < 2; i++) {
10679 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10680 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10681 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10682 linear_centroid, linear_center, sel);
10683 }
10684 ctx->linear_centroid = bld.tmp(v2);
10685 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10686 Operand(new_coord[0]), Operand(new_coord[1]));
10687 emit_split_vector(ctx, ctx->linear_centroid, 2);
10688 }
10689 }
10690 }
10691
10692 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10693 {
10694 Program *program = ctx->program;
10695
10696 unsigned float_controls = shader->info.float_controls_execution_mode;
10697
10698 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10699 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10700 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10701 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10702 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10703
10704 program->next_fp_mode.must_flush_denorms32 =
10705 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10706 program->next_fp_mode.must_flush_denorms16_64 =
10707 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10708 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10709
10710 program->next_fp_mode.care_about_round32 =
10711 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10712
10713 program->next_fp_mode.care_about_round16_64 =
10714 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10715 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10716
10717 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10718 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10719 if (program->next_fp_mode.must_flush_denorms16_64)
10720 program->next_fp_mode.denorm16_64 = 0;
10721 else
10722 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10723
10724 /* preserving fp32 denorms is expensive, so only do it if asked */
10725 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10726 program->next_fp_mode.denorm32 = fp_denorm_keep;
10727 else
10728 program->next_fp_mode.denorm32 = 0;
10729
10730 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10731 program->next_fp_mode.round32 = fp_round_tz;
10732 else
10733 program->next_fp_mode.round32 = fp_round_ne;
10734
10735 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10736 program->next_fp_mode.round16_64 = fp_round_tz;
10737 else
10738 program->next_fp_mode.round16_64 = fp_round_ne;
10739
10740 ctx->block->fp_mode = program->next_fp_mode;
10741 }
10742
10743 void cleanup_cfg(Program *program)
10744 {
10745 /* create linear_succs/logical_succs */
10746 for (Block& BB : program->blocks) {
10747 for (unsigned idx : BB.linear_preds)
10748 program->blocks[idx].linear_succs.emplace_back(BB.index);
10749 for (unsigned idx : BB.logical_preds)
10750 program->blocks[idx].logical_succs.emplace_back(BB.index);
10751 }
10752 }
10753
10754 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10755 {
10756 Builder bld(ctx->program, ctx->block);
10757
10758 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10759 Temp count = i == 0
10760 ? get_arg(ctx, ctx->args->merged_wave_info)
10761 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10762 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10763
10764 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10765 Temp cond;
10766
10767 if (ctx->program->wave_size == 64) {
10768 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10769 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10770 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10771 } else {
10772 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10773 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10774 }
10775
10776 return cond;
10777 }
10778
10779 bool ngg_early_prim_export(isel_context *ctx)
10780 {
10781 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10782 return true;
10783 }
10784
10785 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10786 {
10787 Builder bld(ctx->program, ctx->block);
10788
10789 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10790 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10791
10792 /* Get the id of the current wave within the threadgroup (workgroup) */
10793 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10794 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10795
10796 /* Execute the following code only on the first wave (wave id 0),
10797 * use the SCC def to tell if the wave id is zero or not.
10798 */
10799 Temp cond = wave_id_in_tg.def(1).getTemp();
10800 if_context ic;
10801 begin_uniform_if_then(ctx, &ic, cond);
10802 begin_uniform_if_else(ctx, &ic);
10803 bld.reset(ctx->block);
10804
10805 /* Number of vertices output by VS/TES */
10806 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10807 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10808 /* Number of primitives output by VS/TES */
10809 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10810 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10811
10812 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10813 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10814 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10815
10816 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10817 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10818
10819 end_uniform_if(ctx, &ic);
10820
10821 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10822 bld.reset(ctx->block);
10823 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10824 }
10825
10826 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10827 {
10828 Builder bld(ctx->program, ctx->block);
10829
10830 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10831 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10832 }
10833
10834 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10835 Temp tmp;
10836
10837 for (unsigned i = 0; i < num_vertices; ++i) {
10838 assert(vtxindex[i].id());
10839
10840 if (i)
10841 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10842 else
10843 tmp = vtxindex[i];
10844
10845 /* The initial edge flag is always false in tess eval shaders. */
10846 if (ctx->stage == ngg_vertex_gs) {
10847 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10848 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10849 }
10850 }
10851
10852 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10853
10854 return tmp;
10855 }
10856
10857 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10858 {
10859 Builder bld(ctx->program, ctx->block);
10860 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10861
10862 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10863 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10864 false /* compressed */, true/* done */, false /* valid mask */);
10865 }
10866
10867 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10868 {
10869 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10870 * These must always come before VS exports.
10871 *
10872 * It is recommended to do these as early as possible. They can be at the beginning when
10873 * there is no SW GS and the shader doesn't write edge flags.
10874 */
10875
10876 if_context ic;
10877 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10878 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10879
10880 Builder bld(ctx->program, ctx->block);
10881 constexpr unsigned max_vertices_per_primitive = 3;
10882 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10883
10884 if (ctx->stage == ngg_vertex_gs) {
10885 /* TODO: optimize for points & lines */
10886 } else if (ctx->stage == ngg_tess_eval_gs) {
10887 if (ctx->shader->info.tess.point_mode)
10888 num_vertices_per_primitive = 1;
10889 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10890 num_vertices_per_primitive = 2;
10891 } else {
10892 unreachable("Unsupported NGG shader stage");
10893 }
10894
10895 Temp vtxindex[max_vertices_per_primitive];
10896 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10897 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10898 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10899 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10900 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10901 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10902 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10903 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10904
10905 /* Export primitive data to the index buffer. */
10906 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10907
10908 /* Export primitive ID. */
10909 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10910 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10911 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10912 Temp provoking_vtx_index = vtxindex[0];
10913 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10914
10915 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10916 }
10917
10918 begin_divergent_if_else(ctx, &ic);
10919 end_divergent_if(ctx, &ic);
10920 }
10921
10922 void ngg_emit_nogs_output(isel_context *ctx)
10923 {
10924 /* Emits NGG GS output, for stages that don't have SW GS. */
10925
10926 if_context ic;
10927 Builder bld(ctx->program, ctx->block);
10928 bool late_prim_export = !ngg_early_prim_export(ctx);
10929
10930 /* NGG streamout is currently disabled by default. */
10931 assert(!ctx->args->shader_info->so.num_outputs);
10932
10933 if (late_prim_export) {
10934 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10935 create_export_phis(ctx);
10936 /* Do what we need to do in the GS threads. */
10937 ngg_emit_nogs_gsthreads(ctx);
10938
10939 /* What comes next should be executed on ES threads. */
10940 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10941 begin_divergent_if_then(ctx, &ic, is_es_thread);
10942 bld.reset(ctx->block);
10943 }
10944
10945 /* Export VS outputs */
10946 ctx->block->kind |= block_kind_export_end;
10947 create_vs_exports(ctx);
10948
10949 /* Export primitive ID */
10950 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10951 Temp prim_id;
10952
10953 if (ctx->stage == ngg_vertex_gs) {
10954 /* Wait for GS threads to store primitive ID in LDS. */
10955 create_workgroup_barrier(bld);
10956
10957 /* Calculate LDS address where the GS threads stored the primitive ID. */
10958 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10959 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10960 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10961 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10962 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10963 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10964
10965 /* Load primitive ID from LDS. */
10966 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10967 } else if (ctx->stage == ngg_tess_eval_gs) {
10968 /* TES: Just use the patch ID as the primitive ID. */
10969 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10970 } else {
10971 unreachable("unsupported NGG shader stage.");
10972 }
10973
10974 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10975 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10976
10977 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10978 }
10979
10980 if (late_prim_export) {
10981 begin_divergent_if_else(ctx, &ic);
10982 end_divergent_if(ctx, &ic);
10983 bld.reset(ctx->block);
10984 }
10985 }
10986
10987 void select_program(Program *program,
10988 unsigned shader_count,
10989 struct nir_shader *const *shaders,
10990 ac_shader_config* config,
10991 struct radv_shader_args *args)
10992 {
10993 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10994 if_context ic_merged_wave_info;
10995 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10996
10997 for (unsigned i = 0; i < shader_count; i++) {
10998 nir_shader *nir = shaders[i];
10999 init_context(&ctx, nir);
11000
11001 setup_fp_mode(&ctx, nir);
11002
11003 if (!i) {
11004 /* needs to be after init_context() for FS */
11005 Pseudo_instruction *startpgm = add_startpgm(&ctx);
11006 append_logical_start(ctx.block);
11007
11008 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
11009 fix_ls_vgpr_init_bug(&ctx, startpgm);
11010
11011 split_arguments(&ctx, startpgm);
11012 }
11013
11014 if (ngg_no_gs) {
11015 ngg_emit_sendmsg_gs_alloc_req(&ctx);
11016
11017 if (ngg_early_prim_export(&ctx))
11018 ngg_emit_nogs_gsthreads(&ctx);
11019 }
11020
11021 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
11022 nir_function_impl *func = nir_shader_get_entrypoint(nir);
11023 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
11024 ((nir->info.stage == MESA_SHADER_VERTEX &&
11025 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
11026 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
11027 ctx.stage == tess_eval_geometry_gs));
11028
11029 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
11030 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
11031 if (check_merged_wave_info) {
11032 Temp cond = merged_wave_info_to_mask(&ctx, i);
11033 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
11034 }
11035
11036 if (i) {
11037 Builder bld(ctx.program, ctx.block);
11038
11039 create_workgroup_barrier(bld);
11040
11041 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
11042 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));
11043 }
11044 } else if (ctx.stage == geometry_gs)
11045 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
11046
11047 if (ctx.stage == fragment_fs)
11048 handle_bc_optimize(&ctx);
11049
11050 visit_cf_list(&ctx, &func->body);
11051
11052 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
11053 emit_streamout(&ctx, 0);
11054
11055 if (ctx.stage & hw_vs) {
11056 create_vs_exports(&ctx);
11057 ctx.block->kind |= block_kind_export_end;
11058 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
11059 ngg_emit_nogs_output(&ctx);
11060 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
11061 Builder bld(ctx.program, ctx.block);
11062 bld.barrier(aco_opcode::p_barrier,
11063 memory_sync_info(storage_vmem_output, semantic_release, scope_device));
11064 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
11065 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
11066 write_tcs_tess_factors(&ctx);
11067 }
11068
11069 if (ctx.stage == fragment_fs) {
11070 create_fs_exports(&ctx);
11071 ctx.block->kind |= block_kind_export_end;
11072 }
11073
11074 if (endif_merged_wave_info) {
11075 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
11076 end_divergent_if(&ctx, &ic_merged_wave_info);
11077 }
11078
11079 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
11080 ngg_emit_nogs_output(&ctx);
11081
11082 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
11083 /* Outputs of the previous stage are inputs to the next stage */
11084 ctx.inputs = ctx.outputs;
11085 ctx.outputs = shader_io_state();
11086 }
11087 }
11088
11089 program->config->float_mode = program->blocks[0].fp_mode.val;
11090
11091 append_logical_end(ctx.block);
11092 ctx.block->kind |= block_kind_uniform;
11093 Builder bld(ctx.program, ctx.block);
11094 if (ctx.program->wb_smem_l1_on_end)
11095 bld.smem(aco_opcode::s_dcache_wb, memory_sync_info(storage_buffer, semantic_volatile));
11096 bld.sopp(aco_opcode::s_endpgm);
11097
11098 cleanup_cfg(program);
11099 }
11100
11101 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
11102 ac_shader_config* config,
11103 struct radv_shader_args *args)
11104 {
11105 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
11106
11107 ctx.block->fp_mode = program->next_fp_mode;
11108
11109 add_startpgm(&ctx);
11110 append_logical_start(ctx.block);
11111
11112 Builder bld(ctx.program, ctx.block);
11113
11114 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
11115
11116 Operand stream_id(0u);
11117 if (args->shader_info->so.num_outputs)
11118 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
11119 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
11120
11121 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
11122
11123 std::stack<Block> endif_blocks;
11124
11125 for (unsigned stream = 0; stream < 4; stream++) {
11126 if (stream_id.isConstant() && stream != stream_id.constantValue())
11127 continue;
11128
11129 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
11130 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
11131 continue;
11132
11133 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
11134
11135 unsigned BB_if_idx = ctx.block->index;
11136 Block BB_endif = Block();
11137 if (!stream_id.isConstant()) {
11138 /* begin IF */
11139 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
11140 append_logical_end(ctx.block);
11141 ctx.block->kind |= block_kind_uniform;
11142 bld.branch(aco_opcode::p_cbranch_z, cond);
11143
11144 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
11145
11146 ctx.block = ctx.program->create_and_insert_block();
11147 add_edge(BB_if_idx, ctx.block);
11148 bld.reset(ctx.block);
11149 append_logical_start(ctx.block);
11150 }
11151
11152 unsigned offset = 0;
11153 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
11154 if (args->shader_info->gs.output_streams[i] != stream)
11155 continue;
11156
11157 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
11158 unsigned length = util_last_bit(output_usage_mask);
11159 for (unsigned j = 0; j < length; ++j) {
11160 if (!(output_usage_mask & (1 << j)))
11161 continue;
11162
11163 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
11164 Temp voffset = vtx_offset;
11165 if (const_offset >= 4096u) {
11166 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
11167 const_offset %= 4096u;
11168 }
11169
11170 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
11171 mubuf->definitions[0] = bld.def(v1);
11172 mubuf->operands[0] = Operand(gsvs_ring);
11173 mubuf->operands[1] = Operand(voffset);
11174 mubuf->operands[2] = Operand(0u);
11175 mubuf->offen = true;
11176 mubuf->offset = const_offset;
11177 mubuf->glc = true;
11178 mubuf->slc = true;
11179 mubuf->dlc = args->options->chip_class >= GFX10;
11180
11181 ctx.outputs.mask[i] |= 1 << j;
11182 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
11183
11184 bld.insert(std::move(mubuf));
11185
11186 offset++;
11187 }
11188 }
11189
11190 if (args->shader_info->so.num_outputs) {
11191 emit_streamout(&ctx, stream);
11192 bld.reset(ctx.block);
11193 }
11194
11195 if (stream == 0) {
11196 create_vs_exports(&ctx);
11197 ctx.block->kind |= block_kind_export_end;
11198 }
11199
11200 if (!stream_id.isConstant()) {
11201 append_logical_end(ctx.block);
11202
11203 /* branch from then block to endif block */
11204 bld.branch(aco_opcode::p_branch);
11205 add_edge(ctx.block->index, &BB_endif);
11206 ctx.block->kind |= block_kind_uniform;
11207
11208 /* emit else block */
11209 ctx.block = ctx.program->create_and_insert_block();
11210 add_edge(BB_if_idx, ctx.block);
11211 bld.reset(ctx.block);
11212 append_logical_start(ctx.block);
11213
11214 endif_blocks.push(std::move(BB_endif));
11215 }
11216 }
11217
11218 while (!endif_blocks.empty()) {
11219 Block BB_endif = std::move(endif_blocks.top());
11220 endif_blocks.pop();
11221
11222 Block *BB_else = ctx.block;
11223
11224 append_logical_end(BB_else);
11225 /* branch from else block to endif block */
11226 bld.branch(aco_opcode::p_branch);
11227 add_edge(BB_else->index, &BB_endif);
11228 BB_else->kind |= block_kind_uniform;
11229
11230 /** emit endif merge block */
11231 ctx.block = program->insert_block(std::move(BB_endif));
11232 bld.reset(ctx.block);
11233 append_logical_start(ctx.block);
11234 }
11235
11236 program->config->float_mode = program->blocks[0].fp_mode.val;
11237
11238 append_logical_end(ctx.block);
11239 ctx.block->kind |= block_kind_uniform;
11240 bld.sopp(aco_opcode::s_endpgm);
11241
11242 cleanup_cfg(program);
11243 }
11244 }