3e27759cc0c4d2e5c4813dabfc1ba2b396916c11
[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 convert_int(isel_context *ctx, Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp())
541 {
542 if (!dst.id()) {
543 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
544 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
545 else
546 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
547 }
548
549 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
550 return bld.copy(Definition(dst), src);
551 else if (dst.bytes() < src.bytes())
552 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
553
554 Temp tmp = dst;
555 if (dst_bits == 64)
556 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
557
558 if (tmp == src) {
559 } else if (src.regClass() == s1) {
560 if (is_signed)
561 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
562 else
563 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
564 } else if (ctx->options->chip_class >= GFX8) {
565 assert(src_bits != 8 || src.regClass() == v1b);
566 assert(src_bits != 16 || src.regClass() == v2b);
567 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
568 sdwa->operands[0] = Operand(src);
569 sdwa->definitions[0] = Definition(tmp);
570 if (is_signed)
571 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
572 else
573 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
574 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
575 bld.insert(std::move(sdwa));
576 } else {
577 assert(ctx->options->chip_class == GFX6 || ctx->options->chip_class == GFX7);
578 aco_opcode opcode = is_signed ? aco_opcode::v_bfe_i32 : aco_opcode::v_bfe_u32;
579 bld.vop3(opcode, Definition(tmp), src, Operand(0u), Operand(src_bits == 8 ? 8u : 16u));
580 }
581
582 if (dst_bits == 64) {
583 if (is_signed && dst.regClass() == s2) {
584 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
585 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
586 } else if (is_signed && dst.regClass() == v2) {
587 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
588 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
589 } else {
590 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
591 }
592 }
593
594 return dst;
595 }
596
597 enum sgpr_extract_mode {
598 sgpr_extract_sext,
599 sgpr_extract_zext,
600 sgpr_extract_undef,
601 };
602
603 Temp extract_8_16_bit_sgpr_element(isel_context *ctx, Temp dst, nir_alu_src *src, sgpr_extract_mode mode)
604 {
605 Temp vec = get_ssa_temp(ctx, src->src.ssa);
606 unsigned src_size = src->src.ssa->bit_size;
607 unsigned swizzle = src->swizzle[0];
608
609 if (vec.size() > 1) {
610 assert(src_size == 16);
611 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
612 swizzle = swizzle & 1;
613 }
614
615 Builder bld(ctx->program, ctx->block);
616 unsigned offset = src_size * swizzle;
617 Temp tmp = dst.regClass() == s2 ? bld.tmp(s1) : dst;
618
619 if (mode == sgpr_extract_undef && swizzle == 0) {
620 bld.copy(Definition(tmp), vec);
621 } else if (mode == sgpr_extract_undef || (offset == 24 && mode == sgpr_extract_zext)) {
622 bld.sop2(aco_opcode::s_lshr_b32, Definition(tmp), bld.def(s1, scc), vec, Operand(offset));
623 } else if (src_size == 8 && swizzle == 0 && mode == sgpr_extract_sext) {
624 bld.sop1(aco_opcode::s_sext_i32_i8, Definition(tmp), vec);
625 } else if (src_size == 16 && swizzle == 0 && mode == sgpr_extract_sext) {
626 bld.sop1(aco_opcode::s_sext_i32_i16, Definition(tmp), vec);
627 } else {
628 aco_opcode op = mode == sgpr_extract_zext ? aco_opcode::s_bfe_u32 : aco_opcode::s_bfe_i32;
629 bld.sop2(op, Definition(tmp), bld.def(s1, scc), vec, Operand((src_size << 16) | offset));
630 }
631
632 if (dst.regClass() == s2)
633 convert_int(ctx, bld, tmp, 32, 64, mode == sgpr_extract_sext, dst);
634
635 return dst;
636 }
637
638 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
639 {
640 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
641 return get_ssa_temp(ctx, src.src.ssa);
642
643 if (src.src.ssa->num_components == size) {
644 bool identity_swizzle = true;
645 for (unsigned i = 0; identity_swizzle && i < size; i++) {
646 if (src.swizzle[i] != i)
647 identity_swizzle = false;
648 }
649 if (identity_swizzle)
650 return get_ssa_temp(ctx, src.src.ssa);
651 }
652
653 Temp vec = get_ssa_temp(ctx, src.src.ssa);
654 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
655 assert(elem_size > 0);
656 assert(vec.bytes() % elem_size == 0);
657
658 if (elem_size < 4 && vec.type() == RegType::sgpr) {
659 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
660 assert(size == 1);
661 return extract_8_16_bit_sgpr_element(
662 ctx, Temp(ctx->program->allocateId(), s1), &src, sgpr_extract_undef);
663 }
664
665 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
666 if (size == 1) {
667 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
668 } else {
669 assert(size <= 4);
670 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
671 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
672 for (unsigned i = 0; i < size; ++i) {
673 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
674 vec_instr->operands[i] = Operand{elems[i]};
675 }
676 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
677 vec_instr->definitions[0] = Definition(dst);
678 ctx->block->instructions.emplace_back(std::move(vec_instr));
679 ctx->allocated_vec.emplace(dst.id(), elems);
680 return dst;
681 }
682 }
683
684 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
685 {
686 if (ptr.size() == 2)
687 return ptr;
688 Builder bld(ctx->program, ctx->block);
689 if (ptr.type() == RegType::vgpr)
690 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
691 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
692 ptr, Operand((unsigned)ctx->options->address32_hi));
693 }
694
695 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
696 {
697 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
698 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
699 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
700 sop2->definitions[0] = Definition(dst);
701 if (instr->no_unsigned_wrap)
702 sop2->definitions[0].setNUW(true);
703 if (writes_scc)
704 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
705 ctx->block->instructions.emplace_back(std::move(sop2));
706 }
707
708 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
709 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
710 {
711 Builder bld(ctx->program, ctx->block);
712 bld.is_precise = instr->exact;
713
714 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
715 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
716 if (src1.type() == RegType::sgpr) {
717 if (commutative && src0.type() == RegType::vgpr) {
718 Temp t = src0;
719 src0 = src1;
720 src1 = t;
721 } else {
722 src1 = as_vgpr(ctx, src1);
723 }
724 }
725
726 if (flush_denorms && ctx->program->chip_class < GFX9) {
727 assert(dst.size() == 1);
728 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
729 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
730 } else {
731 bld.vop2(op, Definition(dst), src0, src1);
732 }
733 }
734
735 void emit_vop2_instruction_logic64(isel_context *ctx, nir_alu_instr *instr,
736 aco_opcode op, Temp dst)
737 {
738 Builder bld(ctx->program, ctx->block);
739 bld.is_precise = instr->exact;
740
741 Temp src0 = get_alu_src(ctx, instr->src[0]);
742 Temp src1 = get_alu_src(ctx, instr->src[1]);
743
744 if (src1.type() == RegType::sgpr) {
745 assert(src0.type() == RegType::vgpr);
746 std::swap(src0, src1);
747 }
748
749 Temp src00 = bld.tmp(src0.type(), 1);
750 Temp src01 = bld.tmp(src0.type(), 1);
751 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
752 Temp src10 = bld.tmp(v1);
753 Temp src11 = bld.tmp(v1);
754 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
755 Temp lo = bld.vop2(op, bld.def(v1), src00, src10);
756 Temp hi = bld.vop2(op, bld.def(v1), src01, src11);
757 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
758 }
759
760 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
761 bool flush_denorms = false)
762 {
763 Temp src0 = get_alu_src(ctx, instr->src[0]);
764 Temp src1 = get_alu_src(ctx, instr->src[1]);
765 Temp src2 = get_alu_src(ctx, instr->src[2]);
766
767 /* ensure that the instruction has at most 1 sgpr operand
768 * The optimizer will inline constants for us */
769 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
770 src0 = as_vgpr(ctx, src0);
771 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
772 src1 = as_vgpr(ctx, src1);
773 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
774 src2 = as_vgpr(ctx, src2);
775
776 Builder bld(ctx->program, ctx->block);
777 bld.is_precise = instr->exact;
778 if (flush_denorms && ctx->program->chip_class < GFX9) {
779 assert(dst.size() == 1);
780 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
781 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
782 } else {
783 bld.vop3(op, Definition(dst), src0, src1, src2);
784 }
785 }
786
787 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
788 {
789 Builder bld(ctx->program, ctx->block);
790 bld.is_precise = instr->exact;
791 if (dst.type() == RegType::sgpr)
792 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
793 bld.vop1(op, bld.def(RegType::vgpr, dst.size()), get_alu_src(ctx, instr->src[0])));
794 else
795 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
796 }
797
798 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
799 {
800 Temp src0 = get_alu_src(ctx, instr->src[0]);
801 Temp src1 = get_alu_src(ctx, instr->src[1]);
802 assert(src0.size() == src1.size());
803
804 aco_ptr<Instruction> vopc;
805 if (src1.type() == RegType::sgpr) {
806 if (src0.type() == RegType::vgpr) {
807 /* to swap the operands, we might also have to change the opcode */
808 switch (op) {
809 case aco_opcode::v_cmp_lt_f16:
810 op = aco_opcode::v_cmp_gt_f16;
811 break;
812 case aco_opcode::v_cmp_ge_f16:
813 op = aco_opcode::v_cmp_le_f16;
814 break;
815 case aco_opcode::v_cmp_lt_i16:
816 op = aco_opcode::v_cmp_gt_i16;
817 break;
818 case aco_opcode::v_cmp_ge_i16:
819 op = aco_opcode::v_cmp_le_i16;
820 break;
821 case aco_opcode::v_cmp_lt_u16:
822 op = aco_opcode::v_cmp_gt_u16;
823 break;
824 case aco_opcode::v_cmp_ge_u16:
825 op = aco_opcode::v_cmp_le_u16;
826 break;
827 case aco_opcode::v_cmp_lt_f32:
828 op = aco_opcode::v_cmp_gt_f32;
829 break;
830 case aco_opcode::v_cmp_ge_f32:
831 op = aco_opcode::v_cmp_le_f32;
832 break;
833 case aco_opcode::v_cmp_lt_i32:
834 op = aco_opcode::v_cmp_gt_i32;
835 break;
836 case aco_opcode::v_cmp_ge_i32:
837 op = aco_opcode::v_cmp_le_i32;
838 break;
839 case aco_opcode::v_cmp_lt_u32:
840 op = aco_opcode::v_cmp_gt_u32;
841 break;
842 case aco_opcode::v_cmp_ge_u32:
843 op = aco_opcode::v_cmp_le_u32;
844 break;
845 case aco_opcode::v_cmp_lt_f64:
846 op = aco_opcode::v_cmp_gt_f64;
847 break;
848 case aco_opcode::v_cmp_ge_f64:
849 op = aco_opcode::v_cmp_le_f64;
850 break;
851 case aco_opcode::v_cmp_lt_i64:
852 op = aco_opcode::v_cmp_gt_i64;
853 break;
854 case aco_opcode::v_cmp_ge_i64:
855 op = aco_opcode::v_cmp_le_i64;
856 break;
857 case aco_opcode::v_cmp_lt_u64:
858 op = aco_opcode::v_cmp_gt_u64;
859 break;
860 case aco_opcode::v_cmp_ge_u64:
861 op = aco_opcode::v_cmp_le_u64;
862 break;
863 default: /* eq and ne are commutative */
864 break;
865 }
866 Temp t = src0;
867 src0 = src1;
868 src1 = t;
869 } else {
870 src1 = as_vgpr(ctx, src1);
871 }
872 }
873
874 Builder bld(ctx->program, ctx->block);
875 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
876 }
877
878 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
879 {
880 Temp src0 = get_alu_src(ctx, instr->src[0]);
881 Temp src1 = get_alu_src(ctx, instr->src[1]);
882 Builder bld(ctx->program, ctx->block);
883
884 assert(dst.regClass() == bld.lm);
885 assert(src0.type() == RegType::sgpr);
886 assert(src1.type() == RegType::sgpr);
887 assert(src0.regClass() == src1.regClass());
888
889 /* Emit the SALU comparison instruction */
890 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
891 /* Turn the result into a per-lane bool */
892 bool_to_vector_condition(ctx, cmp, dst);
893 }
894
895 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
896 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)
897 {
898 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;
899 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;
900 bool use_valu = s_op == aco_opcode::num_opcodes ||
901 nir_dest_is_divergent(instr->dest.dest) ||
902 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
903 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
904 aco_opcode op = use_valu ? v_op : s_op;
905 assert(op != aco_opcode::num_opcodes);
906 assert(dst.regClass() == ctx->program->lane_mask);
907
908 if (use_valu)
909 emit_vopc_instruction(ctx, instr, op, dst);
910 else
911 emit_sopc_instruction(ctx, instr, op, dst);
912 }
913
914 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
915 {
916 Builder bld(ctx->program, ctx->block);
917 Temp src0 = get_alu_src(ctx, instr->src[0]);
918 Temp src1 = get_alu_src(ctx, instr->src[1]);
919
920 assert(dst.regClass() == bld.lm);
921 assert(src0.regClass() == bld.lm);
922 assert(src1.regClass() == bld.lm);
923
924 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
925 }
926
927 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
928 {
929 Builder bld(ctx->program, ctx->block);
930 Temp cond = get_alu_src(ctx, instr->src[0]);
931 Temp then = get_alu_src(ctx, instr->src[1]);
932 Temp els = get_alu_src(ctx, instr->src[2]);
933
934 assert(cond.regClass() == bld.lm);
935
936 if (dst.type() == RegType::vgpr) {
937 aco_ptr<Instruction> bcsel;
938 if (dst.size() == 1) {
939 then = as_vgpr(ctx, then);
940 els = as_vgpr(ctx, els);
941
942 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
943 } else if (dst.size() == 2) {
944 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
945 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
946 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
947 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
948
949 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
950 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
951
952 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
953 } else {
954 fprintf(stderr, "Unimplemented NIR instr bit size: ");
955 nir_print_instr(&instr->instr, stderr);
956 fprintf(stderr, "\n");
957 }
958 return;
959 }
960
961 if (instr->dest.dest.ssa.bit_size == 1) {
962 assert(dst.regClass() == bld.lm);
963 assert(then.regClass() == bld.lm);
964 assert(els.regClass() == bld.lm);
965 }
966
967 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
968 if (dst.regClass() == s1 || dst.regClass() == s2) {
969 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
970 assert(dst.size() == then.size());
971 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
972 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
973 } else {
974 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
975 nir_print_instr(&instr->instr, stderr);
976 fprintf(stderr, "\n");
977 }
978 return;
979 }
980
981 /* divergent boolean bcsel
982 * this implements bcsel on bools: dst = s0 ? s1 : s2
983 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
984 assert(instr->dest.dest.ssa.bit_size == 1);
985
986 if (cond.id() != then.id())
987 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
988
989 if (cond.id() == els.id())
990 bld.sop1(Builder::s_mov, Definition(dst), then);
991 else
992 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
993 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
994 }
995
996 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
997 aco_opcode op, uint32_t undo)
998 {
999 /* multiply by 16777216 to handle denormals */
1000 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
1001 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
1002 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
1003 scaled = bld.vop1(op, bld.def(v1), scaled);
1004 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
1005
1006 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
1007
1008 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
1009 }
1010
1011 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1012 {
1013 if (ctx->block->fp_mode.denorm32 == 0) {
1014 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
1015 return;
1016 }
1017
1018 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
1019 }
1020
1021 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1022 {
1023 if (ctx->block->fp_mode.denorm32 == 0) {
1024 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
1025 return;
1026 }
1027
1028 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
1029 }
1030
1031 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1032 {
1033 if (ctx->block->fp_mode.denorm32 == 0) {
1034 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
1035 return;
1036 }
1037
1038 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
1039 }
1040
1041 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1042 {
1043 if (ctx->block->fp_mode.denorm32 == 0) {
1044 bld.vop1(aco_opcode::v_log_f32, dst, val);
1045 return;
1046 }
1047
1048 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
1049 }
1050
1051 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1052 {
1053 if (ctx->options->chip_class >= GFX7)
1054 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
1055
1056 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
1057 /* TODO: create more efficient code! */
1058 if (val.type() == RegType::sgpr)
1059 val = as_vgpr(ctx, val);
1060
1061 /* Split the input value. */
1062 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
1063 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
1064
1065 /* Extract the exponent and compute the unbiased value. */
1066 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
1067 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
1068
1069 /* Extract the fractional part. */
1070 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
1071 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
1072
1073 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
1074 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
1075
1076 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
1077 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
1078 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
1079 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
1080 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
1081
1082 /* Get the sign bit. */
1083 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
1084
1085 /* Decide the operation to apply depending on the unbiased exponent. */
1086 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
1087 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
1088 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
1089 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
1090 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
1091 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
1092
1093 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
1094 }
1095
1096 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1097 {
1098 if (ctx->options->chip_class >= GFX7)
1099 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
1100
1101 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1102 * lowered at NIR level for precision reasons). */
1103 Temp src0 = as_vgpr(ctx, val);
1104
1105 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
1106 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
1107
1108 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
1109 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
1110 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
1111
1112 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
1113 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
1114 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
1115 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
1116
1117 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
1118 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
1119
1120 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
1121
1122 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
1123 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
1124
1125 return add->definitions[0].getTemp();
1126 }
1127
1128 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1129 {
1130 if (!instr->dest.dest.is_ssa) {
1131 fprintf(stderr, "nir alu dst not in ssa: ");
1132 nir_print_instr(&instr->instr, stderr);
1133 fprintf(stderr, "\n");
1134 abort();
1135 }
1136 Builder bld(ctx->program, ctx->block);
1137 bld.is_precise = instr->exact;
1138 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1139 switch(instr->op) {
1140 case nir_op_vec2:
1141 case nir_op_vec3:
1142 case nir_op_vec4: {
1143 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1144 unsigned num = instr->dest.dest.ssa.num_components;
1145 for (unsigned i = 0; i < num; ++i)
1146 elems[i] = get_alu_src(ctx, instr->src[i]);
1147
1148 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1149 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1150 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1151 for (unsigned i = 0; i < num; ++i) {
1152 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1153 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1154 else
1155 vec->operands[i] = Operand{elems[i]};
1156 }
1157 vec->definitions[0] = Definition(dst);
1158 ctx->block->instructions.emplace_back(std::move(vec));
1159 ctx->allocated_vec.emplace(dst.id(), elems);
1160 } else {
1161 // TODO: that is a bit suboptimal..
1162 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1163 for (unsigned i = 0; i < num - 1; ++i)
1164 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1165 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1166 for (unsigned i = 0; i < num; ++i) {
1167 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1168 if (bit % 32 == 0) {
1169 elems[bit / 32] = elems[i];
1170 } else {
1171 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1172 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1173 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1174 }
1175 }
1176 if (dst.size() == 1)
1177 bld.copy(Definition(dst), elems[0]);
1178 else
1179 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1180 }
1181 break;
1182 }
1183 case nir_op_mov: {
1184 Temp src = get_alu_src(ctx, instr->src[0]);
1185 aco_ptr<Instruction> mov;
1186 if (dst.type() == RegType::sgpr) {
1187 if (src.type() == RegType::vgpr)
1188 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1189 else if (src.regClass() == s1)
1190 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1191 else if (src.regClass() == s2)
1192 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1193 else
1194 unreachable("wrong src register class for nir_op_imov");
1195 } else {
1196 if (dst.regClass() == v1)
1197 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1198 else if (dst.regClass() == v1b ||
1199 dst.regClass() == v2b ||
1200 dst.regClass() == v2)
1201 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1202 else
1203 unreachable("wrong src register class for nir_op_imov");
1204 }
1205 break;
1206 }
1207 case nir_op_inot: {
1208 Temp src = get_alu_src(ctx, instr->src[0]);
1209 if (instr->dest.dest.ssa.bit_size == 1) {
1210 assert(src.regClass() == bld.lm);
1211 assert(dst.regClass() == bld.lm);
1212 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1213 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1214 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1215 } else if (dst.regClass() == v1) {
1216 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1217 } else if (dst.regClass() == v2) {
1218 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
1219 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
1220 lo = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), lo);
1221 hi = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), hi);
1222 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
1223 } else if (dst.type() == RegType::sgpr) {
1224 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1225 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1226 } else {
1227 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1228 nir_print_instr(&instr->instr, stderr);
1229 fprintf(stderr, "\n");
1230 }
1231 break;
1232 }
1233 case nir_op_ineg: {
1234 Temp src = get_alu_src(ctx, instr->src[0]);
1235 if (dst.regClass() == v1) {
1236 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1237 } else if (dst.regClass() == s1) {
1238 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1239 } else if (dst.size() == 2) {
1240 Temp src0 = bld.tmp(dst.type(), 1);
1241 Temp src1 = bld.tmp(dst.type(), 1);
1242 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1243
1244 if (dst.regClass() == s2) {
1245 Temp carry = bld.tmp(s1);
1246 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1247 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1248 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1249 } else {
1250 Temp lower = bld.tmp(v1);
1251 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1252 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1253 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1254 }
1255 } else {
1256 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1257 nir_print_instr(&instr->instr, stderr);
1258 fprintf(stderr, "\n");
1259 }
1260 break;
1261 }
1262 case nir_op_iabs: {
1263 if (dst.regClass() == s1) {
1264 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1265 } else if (dst.regClass() == v1) {
1266 Temp src = get_alu_src(ctx, instr->src[0]);
1267 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1268 } else {
1269 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1270 nir_print_instr(&instr->instr, stderr);
1271 fprintf(stderr, "\n");
1272 }
1273 break;
1274 }
1275 case nir_op_isign: {
1276 Temp src = get_alu_src(ctx, instr->src[0]);
1277 if (dst.regClass() == s1) {
1278 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1279 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1280 } else if (dst.regClass() == s2) {
1281 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1282 Temp neqz;
1283 if (ctx->program->chip_class >= GFX8)
1284 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1285 else
1286 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1287 /* SCC gets zero-extended to 64 bit */
1288 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1289 } else if (dst.regClass() == v1) {
1290 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1291 } else if (dst.regClass() == v2) {
1292 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1293 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1294 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1295 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1296 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1297 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1298 } else {
1299 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1300 nir_print_instr(&instr->instr, stderr);
1301 fprintf(stderr, "\n");
1302 }
1303 break;
1304 }
1305 case nir_op_imax: {
1306 if (dst.regClass() == v1) {
1307 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1308 } else if (dst.regClass() == s1) {
1309 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1310 } else {
1311 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1312 nir_print_instr(&instr->instr, stderr);
1313 fprintf(stderr, "\n");
1314 }
1315 break;
1316 }
1317 case nir_op_umax: {
1318 if (dst.regClass() == v1) {
1319 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1320 } else if (dst.regClass() == s1) {
1321 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1322 } else {
1323 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1324 nir_print_instr(&instr->instr, stderr);
1325 fprintf(stderr, "\n");
1326 }
1327 break;
1328 }
1329 case nir_op_imin: {
1330 if (dst.regClass() == v1) {
1331 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1332 } else if (dst.regClass() == s1) {
1333 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1334 } else {
1335 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1336 nir_print_instr(&instr->instr, stderr);
1337 fprintf(stderr, "\n");
1338 }
1339 break;
1340 }
1341 case nir_op_umin: {
1342 if (dst.regClass() == v1) {
1343 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1344 } else if (dst.regClass() == s1) {
1345 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1346 } else {
1347 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1348 nir_print_instr(&instr->instr, stderr);
1349 fprintf(stderr, "\n");
1350 }
1351 break;
1352 }
1353 case nir_op_ior: {
1354 if (instr->dest.dest.ssa.bit_size == 1) {
1355 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1356 } else if (dst.regClass() == v1) {
1357 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1358 } else if (dst.regClass() == v2) {
1359 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_or_b32, dst);
1360 } else if (dst.regClass() == s1) {
1361 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1362 } else if (dst.regClass() == s2) {
1363 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1364 } else {
1365 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1366 nir_print_instr(&instr->instr, stderr);
1367 fprintf(stderr, "\n");
1368 }
1369 break;
1370 }
1371 case nir_op_iand: {
1372 if (instr->dest.dest.ssa.bit_size == 1) {
1373 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1374 } else if (dst.regClass() == v1) {
1375 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1376 } else if (dst.regClass() == v2) {
1377 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_and_b32, dst);
1378 } else if (dst.regClass() == s1) {
1379 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1380 } else if (dst.regClass() == s2) {
1381 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1382 } else {
1383 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1384 nir_print_instr(&instr->instr, stderr);
1385 fprintf(stderr, "\n");
1386 }
1387 break;
1388 }
1389 case nir_op_ixor: {
1390 if (instr->dest.dest.ssa.bit_size == 1) {
1391 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1392 } else if (dst.regClass() == v1) {
1393 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1394 } else if (dst.regClass() == v2) {
1395 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_xor_b32, dst);
1396 } else if (dst.regClass() == s1) {
1397 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1398 } else if (dst.regClass() == s2) {
1399 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1400 } else {
1401 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1402 nir_print_instr(&instr->instr, stderr);
1403 fprintf(stderr, "\n");
1404 }
1405 break;
1406 }
1407 case nir_op_ushr: {
1408 if (dst.regClass() == v1) {
1409 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1410 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1411 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1412 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1413 } else if (dst.regClass() == v2) {
1414 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1415 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1416 } else if (dst.regClass() == s2) {
1417 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1418 } else if (dst.regClass() == s1) {
1419 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1420 } else {
1421 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1422 nir_print_instr(&instr->instr, stderr);
1423 fprintf(stderr, "\n");
1424 }
1425 break;
1426 }
1427 case nir_op_ishl: {
1428 if (dst.regClass() == v1) {
1429 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1430 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1431 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1432 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1433 } else if (dst.regClass() == v2) {
1434 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1435 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1436 } else if (dst.regClass() == s1) {
1437 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1438 } else if (dst.regClass() == s2) {
1439 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1440 } else {
1441 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1442 nir_print_instr(&instr->instr, stderr);
1443 fprintf(stderr, "\n");
1444 }
1445 break;
1446 }
1447 case nir_op_ishr: {
1448 if (dst.regClass() == v1) {
1449 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1450 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1451 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1452 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1453 } else if (dst.regClass() == v2) {
1454 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1455 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1456 } else if (dst.regClass() == s1) {
1457 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1458 } else if (dst.regClass() == s2) {
1459 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1460 } else {
1461 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1462 nir_print_instr(&instr->instr, stderr);
1463 fprintf(stderr, "\n");
1464 }
1465 break;
1466 }
1467 case nir_op_find_lsb: {
1468 Temp src = get_alu_src(ctx, instr->src[0]);
1469 if (src.regClass() == s1) {
1470 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1471 } else if (src.regClass() == v1) {
1472 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1473 } else if (src.regClass() == s2) {
1474 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1475 } else {
1476 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1477 nir_print_instr(&instr->instr, stderr);
1478 fprintf(stderr, "\n");
1479 }
1480 break;
1481 }
1482 case nir_op_ufind_msb:
1483 case nir_op_ifind_msb: {
1484 Temp src = get_alu_src(ctx, instr->src[0]);
1485 if (src.regClass() == s1 || src.regClass() == s2) {
1486 aco_opcode op = src.regClass() == s2 ?
1487 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1488 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1489 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1490
1491 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1492 Operand(src.size() * 32u - 1u), msb_rev);
1493 Temp msb = sub.def(0).getTemp();
1494 Temp carry = sub.def(1).getTemp();
1495
1496 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1497 } else if (src.regClass() == v1) {
1498 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1499 Temp msb_rev = bld.tmp(v1);
1500 emit_vop1_instruction(ctx, instr, op, msb_rev);
1501 Temp msb = bld.tmp(v1);
1502 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1503 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1504 } else {
1505 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1506 nir_print_instr(&instr->instr, stderr);
1507 fprintf(stderr, "\n");
1508 }
1509 break;
1510 }
1511 case nir_op_bitfield_reverse: {
1512 if (dst.regClass() == s1) {
1513 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1514 } else if (dst.regClass() == v1) {
1515 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1516 } else {
1517 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1518 nir_print_instr(&instr->instr, stderr);
1519 fprintf(stderr, "\n");
1520 }
1521 break;
1522 }
1523 case nir_op_iadd: {
1524 if (dst.regClass() == s1) {
1525 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1526 break;
1527 }
1528
1529 Temp src0 = get_alu_src(ctx, instr->src[0]);
1530 Temp src1 = get_alu_src(ctx, instr->src[1]);
1531 if (dst.regClass() == v1) {
1532 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1533 break;
1534 }
1535
1536 assert(src0.size() == 2 && src1.size() == 2);
1537 Temp src00 = bld.tmp(src0.type(), 1);
1538 Temp src01 = bld.tmp(dst.type(), 1);
1539 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1540 Temp src10 = bld.tmp(src1.type(), 1);
1541 Temp src11 = bld.tmp(dst.type(), 1);
1542 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1543
1544 if (dst.regClass() == s2) {
1545 Temp carry = bld.tmp(s1);
1546 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1547 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1548 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1549 } else if (dst.regClass() == v2) {
1550 Temp dst0 = bld.tmp(v1);
1551 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1552 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1553 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1554 } else {
1555 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1556 nir_print_instr(&instr->instr, stderr);
1557 fprintf(stderr, "\n");
1558 }
1559 break;
1560 }
1561 case nir_op_uadd_sat: {
1562 Temp src0 = get_alu_src(ctx, instr->src[0]);
1563 Temp src1 = get_alu_src(ctx, instr->src[1]);
1564 if (dst.regClass() == s1) {
1565 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1566 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1567 src0, src1);
1568 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1569 } else if (dst.regClass() == v1) {
1570 if (ctx->options->chip_class >= GFX9) {
1571 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1572 add->operands[0] = Operand(src0);
1573 add->operands[1] = Operand(src1);
1574 add->definitions[0] = Definition(dst);
1575 add->clamp = 1;
1576 ctx->block->instructions.emplace_back(std::move(add));
1577 } else {
1578 if (src1.regClass() != v1)
1579 std::swap(src0, src1);
1580 assert(src1.regClass() == v1);
1581 Temp tmp = bld.tmp(v1);
1582 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1583 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1584 }
1585 } else {
1586 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1587 nir_print_instr(&instr->instr, stderr);
1588 fprintf(stderr, "\n");
1589 }
1590 break;
1591 }
1592 case nir_op_uadd_carry: {
1593 Temp src0 = get_alu_src(ctx, instr->src[0]);
1594 Temp src1 = get_alu_src(ctx, instr->src[1]);
1595 if (dst.regClass() == s1) {
1596 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1597 break;
1598 }
1599 if (dst.regClass() == v1) {
1600 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1601 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1602 break;
1603 }
1604
1605 Temp src00 = bld.tmp(src0.type(), 1);
1606 Temp src01 = bld.tmp(dst.type(), 1);
1607 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1608 Temp src10 = bld.tmp(src1.type(), 1);
1609 Temp src11 = bld.tmp(dst.type(), 1);
1610 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1611 if (dst.regClass() == s2) {
1612 Temp carry = bld.tmp(s1);
1613 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1614 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1615 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1616 } else if (dst.regClass() == v2) {
1617 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1618 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1619 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1620 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1621 } else {
1622 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1623 nir_print_instr(&instr->instr, stderr);
1624 fprintf(stderr, "\n");
1625 }
1626 break;
1627 }
1628 case nir_op_isub: {
1629 if (dst.regClass() == s1) {
1630 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1631 break;
1632 }
1633
1634 Temp src0 = get_alu_src(ctx, instr->src[0]);
1635 Temp src1 = get_alu_src(ctx, instr->src[1]);
1636 if (dst.regClass() == v1) {
1637 bld.vsub32(Definition(dst), src0, src1);
1638 break;
1639 }
1640
1641 Temp src00 = bld.tmp(src0.type(), 1);
1642 Temp src01 = bld.tmp(dst.type(), 1);
1643 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1644 Temp src10 = bld.tmp(src1.type(), 1);
1645 Temp src11 = bld.tmp(dst.type(), 1);
1646 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1647 if (dst.regClass() == s2) {
1648 Temp carry = bld.tmp(s1);
1649 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1650 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1651 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1652 } else if (dst.regClass() == v2) {
1653 Temp lower = bld.tmp(v1);
1654 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1655 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1656 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1657 } else {
1658 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1659 nir_print_instr(&instr->instr, stderr);
1660 fprintf(stderr, "\n");
1661 }
1662 break;
1663 }
1664 case nir_op_usub_borrow: {
1665 Temp src0 = get_alu_src(ctx, instr->src[0]);
1666 Temp src1 = get_alu_src(ctx, instr->src[1]);
1667 if (dst.regClass() == s1) {
1668 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1669 break;
1670 } else if (dst.regClass() == v1) {
1671 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1672 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1673 break;
1674 }
1675
1676 Temp src00 = bld.tmp(src0.type(), 1);
1677 Temp src01 = bld.tmp(dst.type(), 1);
1678 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1679 Temp src10 = bld.tmp(src1.type(), 1);
1680 Temp src11 = bld.tmp(dst.type(), 1);
1681 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1682 if (dst.regClass() == s2) {
1683 Temp borrow = bld.tmp(s1);
1684 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1685 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1686 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1687 } else if (dst.regClass() == v2) {
1688 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1689 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1690 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1691 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1692 } else {
1693 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1694 nir_print_instr(&instr->instr, stderr);
1695 fprintf(stderr, "\n");
1696 }
1697 break;
1698 }
1699 case nir_op_imul: {
1700 if (dst.regClass() == v1) {
1701 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1702 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1703 } else if (dst.regClass() == s1) {
1704 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1705 } else {
1706 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1707 nir_print_instr(&instr->instr, stderr);
1708 fprintf(stderr, "\n");
1709 }
1710 break;
1711 }
1712 case nir_op_umul_high: {
1713 if (dst.regClass() == v1) {
1714 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1715 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1716 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1717 } else if (dst.regClass() == s1) {
1718 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1719 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1720 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1721 } else {
1722 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1723 nir_print_instr(&instr->instr, stderr);
1724 fprintf(stderr, "\n");
1725 }
1726 break;
1727 }
1728 case nir_op_imul_high: {
1729 if (dst.regClass() == v1) {
1730 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1731 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1732 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1733 } else if (dst.regClass() == s1) {
1734 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1735 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1736 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1737 } else {
1738 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1739 nir_print_instr(&instr->instr, stderr);
1740 fprintf(stderr, "\n");
1741 }
1742 break;
1743 }
1744 case nir_op_fmul: {
1745 Temp src0 = get_alu_src(ctx, instr->src[0]);
1746 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1747 if (dst.regClass() == v2b) {
1748 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1749 } else if (dst.regClass() == v1) {
1750 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1751 } else if (dst.regClass() == v2) {
1752 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1753 } else {
1754 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1755 nir_print_instr(&instr->instr, stderr);
1756 fprintf(stderr, "\n");
1757 }
1758 break;
1759 }
1760 case nir_op_fadd: {
1761 Temp src0 = get_alu_src(ctx, instr->src[0]);
1762 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1763 if (dst.regClass() == v2b) {
1764 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1765 } else if (dst.regClass() == v1) {
1766 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1767 } else if (dst.regClass() == v2) {
1768 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1769 } else {
1770 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1771 nir_print_instr(&instr->instr, stderr);
1772 fprintf(stderr, "\n");
1773 }
1774 break;
1775 }
1776 case nir_op_fsub: {
1777 Temp src0 = get_alu_src(ctx, instr->src[0]);
1778 Temp src1 = get_alu_src(ctx, instr->src[1]);
1779 if (dst.regClass() == v2b) {
1780 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1781 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1782 else
1783 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1784 } else if (dst.regClass() == v1) {
1785 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1786 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1787 else
1788 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1789 } else if (dst.regClass() == v2) {
1790 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1791 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1792 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1793 sub->neg[1] = true;
1794 } else {
1795 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1796 nir_print_instr(&instr->instr, stderr);
1797 fprintf(stderr, "\n");
1798 }
1799 break;
1800 }
1801 case nir_op_fmax: {
1802 Temp src0 = get_alu_src(ctx, instr->src[0]);
1803 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1804 if (dst.regClass() == v2b) {
1805 // TODO: check fp_mode.must_flush_denorms16_64
1806 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1807 } else if (dst.regClass() == v1) {
1808 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1809 } else if (dst.regClass() == v2) {
1810 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1811 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1812 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1813 } else {
1814 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1815 }
1816 } else {
1817 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1818 nir_print_instr(&instr->instr, stderr);
1819 fprintf(stderr, "\n");
1820 }
1821 break;
1822 }
1823 case nir_op_fmin: {
1824 Temp src0 = get_alu_src(ctx, instr->src[0]);
1825 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1826 if (dst.regClass() == v2b) {
1827 // TODO: check fp_mode.must_flush_denorms16_64
1828 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1829 } else if (dst.regClass() == v1) {
1830 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1831 } else if (dst.regClass() == v2) {
1832 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1833 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1834 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1835 } else {
1836 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1837 }
1838 } else {
1839 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1840 nir_print_instr(&instr->instr, stderr);
1841 fprintf(stderr, "\n");
1842 }
1843 break;
1844 }
1845 case nir_op_fmax3: {
1846 if (dst.regClass() == v2b) {
1847 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1848 } else if (dst.regClass() == v1) {
1849 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1850 } else {
1851 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1852 nir_print_instr(&instr->instr, stderr);
1853 fprintf(stderr, "\n");
1854 }
1855 break;
1856 }
1857 case nir_op_fmin3: {
1858 if (dst.regClass() == v2b) {
1859 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1860 } else if (dst.regClass() == v1) {
1861 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1862 } else {
1863 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1864 nir_print_instr(&instr->instr, stderr);
1865 fprintf(stderr, "\n");
1866 }
1867 break;
1868 }
1869 case nir_op_fmed3: {
1870 if (dst.regClass() == v2b) {
1871 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1872 } else if (dst.regClass() == v1) {
1873 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1874 } else {
1875 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1876 nir_print_instr(&instr->instr, stderr);
1877 fprintf(stderr, "\n");
1878 }
1879 break;
1880 }
1881 case nir_op_umax3: {
1882 if (dst.size() == 1) {
1883 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1884 } else {
1885 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1886 nir_print_instr(&instr->instr, stderr);
1887 fprintf(stderr, "\n");
1888 }
1889 break;
1890 }
1891 case nir_op_umin3: {
1892 if (dst.size() == 1) {
1893 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1894 } else {
1895 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1896 nir_print_instr(&instr->instr, stderr);
1897 fprintf(stderr, "\n");
1898 }
1899 break;
1900 }
1901 case nir_op_umed3: {
1902 if (dst.size() == 1) {
1903 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1904 } else {
1905 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1906 nir_print_instr(&instr->instr, stderr);
1907 fprintf(stderr, "\n");
1908 }
1909 break;
1910 }
1911 case nir_op_imax3: {
1912 if (dst.size() == 1) {
1913 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1914 } else {
1915 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1916 nir_print_instr(&instr->instr, stderr);
1917 fprintf(stderr, "\n");
1918 }
1919 break;
1920 }
1921 case nir_op_imin3: {
1922 if (dst.size() == 1) {
1923 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1924 } else {
1925 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1926 nir_print_instr(&instr->instr, stderr);
1927 fprintf(stderr, "\n");
1928 }
1929 break;
1930 }
1931 case nir_op_imed3: {
1932 if (dst.size() == 1) {
1933 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1934 } else {
1935 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1936 nir_print_instr(&instr->instr, stderr);
1937 fprintf(stderr, "\n");
1938 }
1939 break;
1940 }
1941 case nir_op_cube_face_coord: {
1942 Temp in = get_alu_src(ctx, instr->src[0], 3);
1943 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1944 emit_extract_vector(ctx, in, 1, v1),
1945 emit_extract_vector(ctx, in, 2, v1) };
1946 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1947 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1948 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1949 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1950 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1951 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1952 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1953 break;
1954 }
1955 case nir_op_cube_face_index: {
1956 Temp in = get_alu_src(ctx, instr->src[0], 3);
1957 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1958 emit_extract_vector(ctx, in, 1, v1),
1959 emit_extract_vector(ctx, in, 2, v1) };
1960 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1961 break;
1962 }
1963 case nir_op_bcsel: {
1964 emit_bcsel(ctx, instr, dst);
1965 break;
1966 }
1967 case nir_op_frsq: {
1968 Temp src = get_alu_src(ctx, instr->src[0]);
1969 if (dst.regClass() == v2b) {
1970 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1971 } else if (dst.regClass() == v1) {
1972 emit_rsq(ctx, bld, Definition(dst), src);
1973 } else if (dst.regClass() == v2) {
1974 /* Lowered at NIR level for precision reasons. */
1975 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1976 } else {
1977 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1978 nir_print_instr(&instr->instr, stderr);
1979 fprintf(stderr, "\n");
1980 }
1981 break;
1982 }
1983 case nir_op_fneg: {
1984 Temp src = get_alu_src(ctx, instr->src[0]);
1985 if (dst.regClass() == v2b) {
1986 if (ctx->block->fp_mode.must_flush_denorms16_64)
1987 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1988 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1989 } else if (dst.regClass() == v1) {
1990 if (ctx->block->fp_mode.must_flush_denorms32)
1991 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1992 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1993 } else if (dst.regClass() == v2) {
1994 if (ctx->block->fp_mode.must_flush_denorms16_64)
1995 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1996 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1997 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1998 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1999 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2000 } else {
2001 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2002 nir_print_instr(&instr->instr, stderr);
2003 fprintf(stderr, "\n");
2004 }
2005 break;
2006 }
2007 case nir_op_fabs: {
2008 Temp src = get_alu_src(ctx, instr->src[0]);
2009 if (dst.regClass() == v2b) {
2010 if (ctx->block->fp_mode.must_flush_denorms16_64)
2011 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
2012 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
2013 } else if (dst.regClass() == v1) {
2014 if (ctx->block->fp_mode.must_flush_denorms32)
2015 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
2016 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
2017 } else if (dst.regClass() == v2) {
2018 if (ctx->block->fp_mode.must_flush_denorms16_64)
2019 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
2020 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
2021 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2022 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
2023 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2024 } else {
2025 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2026 nir_print_instr(&instr->instr, stderr);
2027 fprintf(stderr, "\n");
2028 }
2029 break;
2030 }
2031 case nir_op_fsat: {
2032 Temp src = get_alu_src(ctx, instr->src[0]);
2033 if (dst.regClass() == v2b) {
2034 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src);
2035 } else if (dst.regClass() == v1) {
2036 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2037 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
2038 // TODO: confirm that this holds under any circumstances
2039 } else if (dst.regClass() == v2) {
2040 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
2041 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
2042 vop3->clamp = true;
2043 } else {
2044 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2045 nir_print_instr(&instr->instr, stderr);
2046 fprintf(stderr, "\n");
2047 }
2048 break;
2049 }
2050 case nir_op_flog2: {
2051 Temp src = get_alu_src(ctx, instr->src[0]);
2052 if (dst.regClass() == v2b) {
2053 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
2054 } else if (dst.regClass() == v1) {
2055 emit_log2(ctx, bld, Definition(dst), src);
2056 } else {
2057 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2058 nir_print_instr(&instr->instr, stderr);
2059 fprintf(stderr, "\n");
2060 }
2061 break;
2062 }
2063 case nir_op_frcp: {
2064 Temp src = get_alu_src(ctx, instr->src[0]);
2065 if (dst.regClass() == v2b) {
2066 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
2067 } else if (dst.regClass() == v1) {
2068 emit_rcp(ctx, bld, Definition(dst), src);
2069 } else if (dst.regClass() == v2) {
2070 /* Lowered at NIR level for precision reasons. */
2071 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
2072 } else {
2073 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2074 nir_print_instr(&instr->instr, stderr);
2075 fprintf(stderr, "\n");
2076 }
2077 break;
2078 }
2079 case nir_op_fexp2: {
2080 if (dst.regClass() == v2b) {
2081 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
2082 } else if (dst.regClass() == v1) {
2083 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
2084 } else {
2085 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2086 nir_print_instr(&instr->instr, stderr);
2087 fprintf(stderr, "\n");
2088 }
2089 break;
2090 }
2091 case nir_op_fsqrt: {
2092 Temp src = get_alu_src(ctx, instr->src[0]);
2093 if (dst.regClass() == v2b) {
2094 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
2095 } else if (dst.regClass() == v1) {
2096 emit_sqrt(ctx, bld, Definition(dst), src);
2097 } else if (dst.regClass() == v2) {
2098 /* Lowered at NIR level for precision reasons. */
2099 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
2100 } else {
2101 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2102 nir_print_instr(&instr->instr, stderr);
2103 fprintf(stderr, "\n");
2104 }
2105 break;
2106 }
2107 case nir_op_ffract: {
2108 if (dst.regClass() == v2b) {
2109 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
2110 } else if (dst.regClass() == v1) {
2111 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
2112 } else if (dst.regClass() == v2) {
2113 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
2114 } else {
2115 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2116 nir_print_instr(&instr->instr, stderr);
2117 fprintf(stderr, "\n");
2118 }
2119 break;
2120 }
2121 case nir_op_ffloor: {
2122 Temp src = get_alu_src(ctx, instr->src[0]);
2123 if (dst.regClass() == v2b) {
2124 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
2125 } else if (dst.regClass() == v1) {
2126 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
2127 } else if (dst.regClass() == v2) {
2128 emit_floor_f64(ctx, bld, Definition(dst), src);
2129 } else {
2130 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2131 nir_print_instr(&instr->instr, stderr);
2132 fprintf(stderr, "\n");
2133 }
2134 break;
2135 }
2136 case nir_op_fceil: {
2137 Temp src0 = get_alu_src(ctx, instr->src[0]);
2138 if (dst.regClass() == v2b) {
2139 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
2140 } else if (dst.regClass() == v1) {
2141 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2142 } else if (dst.regClass() == v2) {
2143 if (ctx->options->chip_class >= GFX7) {
2144 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2145 } else {
2146 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2147 /* trunc = trunc(src0)
2148 * if (src0 > 0.0 && src0 != trunc)
2149 * trunc += 1.0
2150 */
2151 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2152 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2153 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2154 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2155 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);
2156 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2157 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2158 }
2159 } else {
2160 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2161 nir_print_instr(&instr->instr, stderr);
2162 fprintf(stderr, "\n");
2163 }
2164 break;
2165 }
2166 case nir_op_ftrunc: {
2167 Temp src = get_alu_src(ctx, instr->src[0]);
2168 if (dst.regClass() == v2b) {
2169 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2170 } else if (dst.regClass() == v1) {
2171 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2172 } else if (dst.regClass() == v2) {
2173 emit_trunc_f64(ctx, bld, Definition(dst), src);
2174 } else {
2175 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2176 nir_print_instr(&instr->instr, stderr);
2177 fprintf(stderr, "\n");
2178 }
2179 break;
2180 }
2181 case nir_op_fround_even: {
2182 Temp src0 = get_alu_src(ctx, instr->src[0]);
2183 if (dst.regClass() == v2b) {
2184 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2185 } else if (dst.regClass() == v1) {
2186 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2187 } else if (dst.regClass() == v2) {
2188 if (ctx->options->chip_class >= GFX7) {
2189 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2190 } else {
2191 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2192 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2193 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2194
2195 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2196 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));
2197 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));
2198 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));
2199 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2200 tmp = sub->definitions[0].getTemp();
2201
2202 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2203 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2204 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2205 Temp cond = vop3->definitions[0].getTemp();
2206
2207 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2208 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2209 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2210 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2211
2212 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2213 }
2214 } else {
2215 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2216 nir_print_instr(&instr->instr, stderr);
2217 fprintf(stderr, "\n");
2218 }
2219 break;
2220 }
2221 case nir_op_fsin:
2222 case nir_op_fcos: {
2223 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2224 aco_ptr<Instruction> norm;
2225 if (dst.regClass() == v2b) {
2226 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3118u));
2227 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2228 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2229 bld.vop1(opcode, Definition(dst), tmp);
2230 } else if (dst.regClass() == v1) {
2231 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2232 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2233
2234 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2235 if (ctx->options->chip_class < GFX9)
2236 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2237
2238 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2239 bld.vop1(opcode, Definition(dst), tmp);
2240 } else {
2241 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2242 nir_print_instr(&instr->instr, stderr);
2243 fprintf(stderr, "\n");
2244 }
2245 break;
2246 }
2247 case nir_op_ldexp: {
2248 Temp src0 = get_alu_src(ctx, instr->src[0]);
2249 Temp src1 = get_alu_src(ctx, instr->src[1]);
2250 if (dst.regClass() == v2b) {
2251 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2252 } else if (dst.regClass() == v1) {
2253 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2254 } else if (dst.regClass() == v2) {
2255 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2256 } else {
2257 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2258 nir_print_instr(&instr->instr, stderr);
2259 fprintf(stderr, "\n");
2260 }
2261 break;
2262 }
2263 case nir_op_frexp_sig: {
2264 Temp src = get_alu_src(ctx, instr->src[0]);
2265 if (dst.regClass() == v2b) {
2266 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2267 } else if (dst.regClass() == v1) {
2268 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2269 } else if (dst.regClass() == v2) {
2270 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2271 } else {
2272 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2273 nir_print_instr(&instr->instr, stderr);
2274 fprintf(stderr, "\n");
2275 }
2276 break;
2277 }
2278 case nir_op_frexp_exp: {
2279 Temp src = get_alu_src(ctx, instr->src[0]);
2280 if (instr->src[0].src.ssa->bit_size == 16) {
2281 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2282 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2283 convert_int(ctx, bld, tmp, 8, 32, true, dst);
2284 } else if (instr->src[0].src.ssa->bit_size == 32) {
2285 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2286 } else if (instr->src[0].src.ssa->bit_size == 64) {
2287 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2288 } else {
2289 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2290 nir_print_instr(&instr->instr, stderr);
2291 fprintf(stderr, "\n");
2292 }
2293 break;
2294 }
2295 case nir_op_fsign: {
2296 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2297 if (dst.regClass() == v2b) {
2298 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2299 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2300 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2301 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2302 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2303 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2304 } else if (dst.regClass() == v1) {
2305 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2306 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2307 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2308 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2309 } else if (dst.regClass() == v2) {
2310 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2311 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2312 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2313
2314 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2315 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2316 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2317
2318 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2319 } else {
2320 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2321 nir_print_instr(&instr->instr, stderr);
2322 fprintf(stderr, "\n");
2323 }
2324 break;
2325 }
2326 case nir_op_f2f16:
2327 case nir_op_f2f16_rtne: {
2328 Temp src = get_alu_src(ctx, instr->src[0]);
2329 if (instr->src[0].src.ssa->bit_size == 64)
2330 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2331 if (instr->op == nir_op_f2f16_rtne && ctx->block->fp_mode.round16_64 != fp_round_ne)
2332 /* We emit s_round_mode/s_setreg_imm32 in lower_to_hw_instr to
2333 * keep value numbering and the scheduler simpler.
2334 */
2335 bld.vop1(aco_opcode::p_cvt_f16_f32_rtne, Definition(dst), src);
2336 else
2337 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2338 break;
2339 }
2340 case nir_op_f2f16_rtz: {
2341 Temp src = get_alu_src(ctx, instr->src[0]);
2342 if (instr->src[0].src.ssa->bit_size == 64)
2343 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2344 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2345 break;
2346 }
2347 case nir_op_f2f32: {
2348 if (instr->src[0].src.ssa->bit_size == 16) {
2349 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2350 } else if (instr->src[0].src.ssa->bit_size == 64) {
2351 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2352 } else {
2353 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2354 nir_print_instr(&instr->instr, stderr);
2355 fprintf(stderr, "\n");
2356 }
2357 break;
2358 }
2359 case nir_op_f2f64: {
2360 Temp src = get_alu_src(ctx, instr->src[0]);
2361 if (instr->src[0].src.ssa->bit_size == 16)
2362 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2363 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2364 break;
2365 }
2366 case nir_op_i2f16: {
2367 assert(dst.regClass() == v2b);
2368 Temp src = get_alu_src(ctx, instr->src[0]);
2369 if (instr->src[0].src.ssa->bit_size == 8)
2370 src = convert_int(ctx, bld, src, 8, 16, true);
2371 else if (instr->src[0].src.ssa->bit_size == 64)
2372 src = convert_int(ctx, bld, src, 64, 32, false);
2373 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2374 break;
2375 }
2376 case nir_op_i2f32: {
2377 assert(dst.size() == 1);
2378 Temp src = get_alu_src(ctx, instr->src[0]);
2379 if (instr->src[0].src.ssa->bit_size <= 16)
2380 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2381 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2382 break;
2383 }
2384 case nir_op_i2f64: {
2385 if (instr->src[0].src.ssa->bit_size <= 32) {
2386 Temp src = get_alu_src(ctx, instr->src[0]);
2387 if (instr->src[0].src.ssa->bit_size <= 16)
2388 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2389 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2390 } else if (instr->src[0].src.ssa->bit_size == 64) {
2391 Temp src = get_alu_src(ctx, instr->src[0]);
2392 RegClass rc = RegClass(src.type(), 1);
2393 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2394 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2395 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2396 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2397 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2398 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2399
2400 } else {
2401 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2402 nir_print_instr(&instr->instr, stderr);
2403 fprintf(stderr, "\n");
2404 }
2405 break;
2406 }
2407 case nir_op_u2f16: {
2408 assert(dst.regClass() == v2b);
2409 Temp src = get_alu_src(ctx, instr->src[0]);
2410 if (instr->src[0].src.ssa->bit_size == 8)
2411 src = convert_int(ctx, bld, src, 8, 16, false);
2412 else if (instr->src[0].src.ssa->bit_size == 64)
2413 src = convert_int(ctx, bld, src, 64, 32, false);
2414 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2415 break;
2416 }
2417 case nir_op_u2f32: {
2418 assert(dst.size() == 1);
2419 Temp src = get_alu_src(ctx, instr->src[0]);
2420 if (instr->src[0].src.ssa->bit_size == 8) {
2421 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2422 } else {
2423 if (instr->src[0].src.ssa->bit_size == 16)
2424 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2425 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2426 }
2427 break;
2428 }
2429 case nir_op_u2f64: {
2430 if (instr->src[0].src.ssa->bit_size <= 32) {
2431 Temp src = get_alu_src(ctx, instr->src[0]);
2432 if (instr->src[0].src.ssa->bit_size <= 16)
2433 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2434 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2435 } else if (instr->src[0].src.ssa->bit_size == 64) {
2436 Temp src = get_alu_src(ctx, instr->src[0]);
2437 RegClass rc = RegClass(src.type(), 1);
2438 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2439 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2440 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2441 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2442 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2443 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2444 } else {
2445 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2446 nir_print_instr(&instr->instr, stderr);
2447 fprintf(stderr, "\n");
2448 }
2449 break;
2450 }
2451 case nir_op_f2i8:
2452 case nir_op_f2i16: {
2453 if (instr->src[0].src.ssa->bit_size == 16)
2454 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i16_f16, dst);
2455 else if (instr->src[0].src.ssa->bit_size == 32)
2456 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2457 else
2458 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2459 break;
2460 }
2461 case nir_op_f2u8:
2462 case nir_op_f2u16: {
2463 if (instr->src[0].src.ssa->bit_size == 16)
2464 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u16_f16, dst);
2465 else if (instr->src[0].src.ssa->bit_size == 32)
2466 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2467 else
2468 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2469 break;
2470 }
2471 case nir_op_f2i32: {
2472 Temp src = get_alu_src(ctx, instr->src[0]);
2473 if (instr->src[0].src.ssa->bit_size == 16) {
2474 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2475 if (dst.type() == RegType::vgpr) {
2476 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2477 } else {
2478 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2479 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2480 }
2481 } else if (instr->src[0].src.ssa->bit_size == 32) {
2482 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2483 } else if (instr->src[0].src.ssa->bit_size == 64) {
2484 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2485 } else {
2486 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2487 nir_print_instr(&instr->instr, stderr);
2488 fprintf(stderr, "\n");
2489 }
2490 break;
2491 }
2492 case nir_op_f2u32: {
2493 Temp src = get_alu_src(ctx, instr->src[0]);
2494 if (instr->src[0].src.ssa->bit_size == 16) {
2495 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2496 if (dst.type() == RegType::vgpr) {
2497 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2498 } else {
2499 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2500 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2501 }
2502 } else if (instr->src[0].src.ssa->bit_size == 32) {
2503 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2504 } else if (instr->src[0].src.ssa->bit_size == 64) {
2505 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2506 } else {
2507 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2508 nir_print_instr(&instr->instr, stderr);
2509 fprintf(stderr, "\n");
2510 }
2511 break;
2512 }
2513 case nir_op_f2i64: {
2514 Temp src = get_alu_src(ctx, instr->src[0]);
2515 if (instr->src[0].src.ssa->bit_size == 16)
2516 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2517
2518 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2519 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2520 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2521 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2522 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2523 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2524 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2525 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2526 Temp new_exponent = bld.tmp(v1);
2527 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2528 if (ctx->program->chip_class >= GFX8)
2529 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2530 else
2531 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2532 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2533 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2534 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2535 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2536 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2537 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2538 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2539 Temp new_lower = bld.tmp(v1);
2540 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2541 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2542 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2543
2544 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2545 if (src.type() == RegType::vgpr)
2546 src = bld.as_uniform(src);
2547 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2548 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2549 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2550 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2551 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2552 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2553 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2554 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2555 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2556 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2557 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2558 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2559 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2560 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2561 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2562 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2563 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2564 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2565 Temp borrow = bld.tmp(s1);
2566 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2567 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2568 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2569
2570 } else if (instr->src[0].src.ssa->bit_size == 64) {
2571 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2572 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2573 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2574 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2575 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2576 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2577 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2578 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2579 if (dst.type() == RegType::sgpr) {
2580 lower = bld.as_uniform(lower);
2581 upper = bld.as_uniform(upper);
2582 }
2583 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2584
2585 } else {
2586 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2587 nir_print_instr(&instr->instr, stderr);
2588 fprintf(stderr, "\n");
2589 }
2590 break;
2591 }
2592 case nir_op_f2u64: {
2593 Temp src = get_alu_src(ctx, instr->src[0]);
2594 if (instr->src[0].src.ssa->bit_size == 16)
2595 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2596
2597 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2598 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2599 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2600 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2601 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2602 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2603 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2604 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2605 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2606 Temp new_exponent = bld.tmp(v1);
2607 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2608 if (ctx->program->chip_class >= GFX8)
2609 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2610 else
2611 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2612 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2613 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2614 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2615 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2616 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2617 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2618 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2619
2620 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2621 if (src.type() == RegType::vgpr)
2622 src = bld.as_uniform(src);
2623 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2624 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2625 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2626 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2627 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2628 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2629 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2630 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2631 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2632 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2633 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2634 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2635 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2636 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2637 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2638 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2639 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2640 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2641
2642 } else if (instr->src[0].src.ssa->bit_size == 64) {
2643 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2644 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2645 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2646 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2647 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2648 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2649 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2650 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2651 if (dst.type() == RegType::sgpr) {
2652 lower = bld.as_uniform(lower);
2653 upper = bld.as_uniform(upper);
2654 }
2655 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2656
2657 } else {
2658 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2659 nir_print_instr(&instr->instr, stderr);
2660 fprintf(stderr, "\n");
2661 }
2662 break;
2663 }
2664 case nir_op_b2f16: {
2665 Temp src = get_alu_src(ctx, instr->src[0]);
2666 assert(src.regClass() == bld.lm);
2667
2668 if (dst.regClass() == s1) {
2669 src = bool_to_scalar_condition(ctx, src);
2670 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2671 } else if (dst.regClass() == v2b) {
2672 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2673 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2674 } else {
2675 unreachable("Wrong destination register class for nir_op_b2f16.");
2676 }
2677 break;
2678 }
2679 case nir_op_b2f32: {
2680 Temp src = get_alu_src(ctx, instr->src[0]);
2681 assert(src.regClass() == bld.lm);
2682
2683 if (dst.regClass() == s1) {
2684 src = bool_to_scalar_condition(ctx, src);
2685 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2686 } else if (dst.regClass() == v1) {
2687 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2688 } else {
2689 unreachable("Wrong destination register class for nir_op_b2f32.");
2690 }
2691 break;
2692 }
2693 case nir_op_b2f64: {
2694 Temp src = get_alu_src(ctx, instr->src[0]);
2695 assert(src.regClass() == bld.lm);
2696
2697 if (dst.regClass() == s2) {
2698 src = bool_to_scalar_condition(ctx, src);
2699 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2700 } else if (dst.regClass() == v2) {
2701 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2702 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2703 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2704 } else {
2705 unreachable("Wrong destination register class for nir_op_b2f64.");
2706 }
2707 break;
2708 }
2709 case nir_op_i2i8:
2710 case nir_op_i2i16:
2711 case nir_op_i2i32:
2712 case nir_op_i2i64: {
2713 if (dst.type() == RegType::sgpr && instr->src[0].src.ssa->bit_size < 32) {
2714 /* no need to do the extract in get_alu_src() */
2715 sgpr_extract_mode mode = instr->dest.dest.ssa.bit_size > instr->src[0].src.ssa->bit_size ?
2716 sgpr_extract_sext : sgpr_extract_undef;
2717 extract_8_16_bit_sgpr_element(ctx, dst, &instr->src[0], mode);
2718 } else {
2719 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2720 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2721 }
2722 break;
2723 }
2724 case nir_op_u2u8:
2725 case nir_op_u2u16:
2726 case nir_op_u2u32:
2727 case nir_op_u2u64: {
2728 if (dst.type() == RegType::sgpr && instr->src[0].src.ssa->bit_size < 32) {
2729 /* no need to do the extract in get_alu_src() */
2730 sgpr_extract_mode mode = instr->dest.dest.ssa.bit_size > instr->src[0].src.ssa->bit_size ?
2731 sgpr_extract_zext : sgpr_extract_undef;
2732 extract_8_16_bit_sgpr_element(ctx, dst, &instr->src[0], mode);
2733 } else {
2734 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2735 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2736 }
2737 break;
2738 }
2739 case nir_op_b2b32:
2740 case nir_op_b2i8:
2741 case nir_op_b2i16:
2742 case nir_op_b2i32:
2743 case nir_op_b2i64: {
2744 Temp src = get_alu_src(ctx, instr->src[0]);
2745 assert(src.regClass() == bld.lm);
2746
2747 Temp tmp = dst.bytes() == 8 ? bld.tmp(RegClass::get(dst.type(), 4)) : dst;
2748 if (tmp.regClass() == s1) {
2749 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2750 bool_to_scalar_condition(ctx, src, tmp);
2751 } else if (tmp.type() == RegType::vgpr) {
2752 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(tmp), Operand(0u), Operand(1u), src);
2753 } else {
2754 unreachable("Invalid register class for b2i32");
2755 }
2756
2757 if (tmp != dst)
2758 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
2759 break;
2760 }
2761 case nir_op_b2b1:
2762 case nir_op_i2b1: {
2763 Temp src = get_alu_src(ctx, instr->src[0]);
2764 assert(dst.regClass() == bld.lm);
2765
2766 if (src.type() == RegType::vgpr) {
2767 assert(src.regClass() == v1 || src.regClass() == v2);
2768 assert(dst.regClass() == bld.lm);
2769 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2770 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2771 } else {
2772 assert(src.regClass() == s1 || src.regClass() == s2);
2773 Temp tmp;
2774 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2775 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2776 } else {
2777 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2778 bld.scc(bld.def(s1)), Operand(0u), src);
2779 }
2780 bool_to_vector_condition(ctx, tmp, dst);
2781 }
2782 break;
2783 }
2784 case nir_op_pack_64_2x32_split: {
2785 Temp src0 = get_alu_src(ctx, instr->src[0]);
2786 Temp src1 = get_alu_src(ctx, instr->src[1]);
2787
2788 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2789 break;
2790 }
2791 case nir_op_unpack_64_2x32_split_x:
2792 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2793 break;
2794 case nir_op_unpack_64_2x32_split_y:
2795 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2796 break;
2797 case nir_op_unpack_32_2x16_split_x:
2798 if (dst.type() == RegType::vgpr) {
2799 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2800 } else {
2801 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2802 }
2803 break;
2804 case nir_op_unpack_32_2x16_split_y:
2805 if (dst.type() == RegType::vgpr) {
2806 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2807 } else {
2808 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)));
2809 }
2810 break;
2811 case nir_op_pack_32_2x16_split: {
2812 Temp src0 = get_alu_src(ctx, instr->src[0]);
2813 Temp src1 = get_alu_src(ctx, instr->src[1]);
2814 if (dst.regClass() == v1) {
2815 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2816 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2817 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2818 } else {
2819 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2820 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2821 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2822 }
2823 break;
2824 }
2825 case nir_op_pack_half_2x16: {
2826 Temp src = get_alu_src(ctx, instr->src[0], 2);
2827
2828 if (dst.regClass() == v1) {
2829 Temp src0 = bld.tmp(v1);
2830 Temp src1 = bld.tmp(v1);
2831 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2832 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2833 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2834 else
2835 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2836 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2837 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2838 } else {
2839 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2840 nir_print_instr(&instr->instr, stderr);
2841 fprintf(stderr, "\n");
2842 }
2843 break;
2844 }
2845 case nir_op_unpack_half_2x16_split_x: {
2846 if (dst.regClass() == v1) {
2847 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2848 } else {
2849 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2850 nir_print_instr(&instr->instr, stderr);
2851 fprintf(stderr, "\n");
2852 }
2853 break;
2854 }
2855 case nir_op_unpack_half_2x16_split_y: {
2856 if (dst.regClass() == v1) {
2857 /* TODO: use SDWA here */
2858 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2859 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2860 } else {
2861 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2862 nir_print_instr(&instr->instr, stderr);
2863 fprintf(stderr, "\n");
2864 }
2865 break;
2866 }
2867 case nir_op_fquantize2f16: {
2868 Temp src = get_alu_src(ctx, instr->src[0]);
2869 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2870 Temp f32, cmp_res;
2871
2872 if (ctx->program->chip_class >= GFX8) {
2873 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2874 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2875 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2876 } else {
2877 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2878 * so compare the result and flush to 0 if it's smaller.
2879 */
2880 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2881 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2882 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2883 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2884 cmp_res = vop3->definitions[0].getTemp();
2885 }
2886
2887 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2888 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2889 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2890 } else {
2891 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2892 }
2893 break;
2894 }
2895 case nir_op_bfm: {
2896 Temp bits = get_alu_src(ctx, instr->src[0]);
2897 Temp offset = get_alu_src(ctx, instr->src[1]);
2898
2899 if (dst.regClass() == s1) {
2900 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2901 } else if (dst.regClass() == v1) {
2902 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2903 } else {
2904 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2905 nir_print_instr(&instr->instr, stderr);
2906 fprintf(stderr, "\n");
2907 }
2908 break;
2909 }
2910 case nir_op_bitfield_select: {
2911 /* (mask & insert) | (~mask & base) */
2912 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2913 Temp insert = get_alu_src(ctx, instr->src[1]);
2914 Temp base = get_alu_src(ctx, instr->src[2]);
2915
2916 /* dst = (insert & bitmask) | (base & ~bitmask) */
2917 if (dst.regClass() == s1) {
2918 aco_ptr<Instruction> sop2;
2919 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2920 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2921 Operand lhs;
2922 if (const_insert && const_bitmask) {
2923 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2924 } else {
2925 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2926 lhs = Operand(insert);
2927 }
2928
2929 Operand rhs;
2930 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2931 if (const_base && const_bitmask) {
2932 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2933 } else {
2934 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2935 rhs = Operand(base);
2936 }
2937
2938 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2939
2940 } else if (dst.regClass() == v1) {
2941 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2942 base = as_vgpr(ctx, base);
2943 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2944 insert = as_vgpr(ctx, insert);
2945
2946 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2947
2948 } else {
2949 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2950 nir_print_instr(&instr->instr, stderr);
2951 fprintf(stderr, "\n");
2952 }
2953 break;
2954 }
2955 case nir_op_ubfe:
2956 case nir_op_ibfe: {
2957 Temp base = get_alu_src(ctx, instr->src[0]);
2958 Temp offset = get_alu_src(ctx, instr->src[1]);
2959 Temp bits = get_alu_src(ctx, instr->src[2]);
2960
2961 if (dst.type() == RegType::sgpr) {
2962 Operand extract;
2963 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2964 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2965 if (const_offset && const_bits) {
2966 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2967 extract = Operand(const_extract);
2968 } else {
2969 Operand width;
2970 if (const_bits) {
2971 width = Operand(const_bits->u32 << 16);
2972 } else {
2973 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2974 }
2975 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2976 }
2977
2978 aco_opcode opcode;
2979 if (dst.regClass() == s1) {
2980 if (instr->op == nir_op_ubfe)
2981 opcode = aco_opcode::s_bfe_u32;
2982 else
2983 opcode = aco_opcode::s_bfe_i32;
2984 } else if (dst.regClass() == s2) {
2985 if (instr->op == nir_op_ubfe)
2986 opcode = aco_opcode::s_bfe_u64;
2987 else
2988 opcode = aco_opcode::s_bfe_i64;
2989 } else {
2990 unreachable("Unsupported BFE bit size");
2991 }
2992
2993 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2994
2995 } else {
2996 aco_opcode opcode;
2997 if (dst.regClass() == v1) {
2998 if (instr->op == nir_op_ubfe)
2999 opcode = aco_opcode::v_bfe_u32;
3000 else
3001 opcode = aco_opcode::v_bfe_i32;
3002 } else {
3003 unreachable("Unsupported BFE bit size");
3004 }
3005
3006 emit_vop3a_instruction(ctx, instr, opcode, dst);
3007 }
3008 break;
3009 }
3010 case nir_op_bit_count: {
3011 Temp src = get_alu_src(ctx, instr->src[0]);
3012 if (src.regClass() == s1) {
3013 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
3014 } else if (src.regClass() == v1) {
3015 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
3016 } else if (src.regClass() == v2) {
3017 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
3018 emit_extract_vector(ctx, src, 1, v1),
3019 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
3020 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
3021 } else if (src.regClass() == s2) {
3022 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
3023 } else {
3024 fprintf(stderr, "Unimplemented NIR instr bit size: ");
3025 nir_print_instr(&instr->instr, stderr);
3026 fprintf(stderr, "\n");
3027 }
3028 break;
3029 }
3030 case nir_op_flt: {
3031 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
3032 break;
3033 }
3034 case nir_op_fge: {
3035 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
3036 break;
3037 }
3038 case nir_op_feq: {
3039 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
3040 break;
3041 }
3042 case nir_op_fne: {
3043 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
3044 break;
3045 }
3046 case nir_op_ilt: {
3047 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);
3048 break;
3049 }
3050 case nir_op_ige: {
3051 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);
3052 break;
3053 }
3054 case nir_op_ieq: {
3055 if (instr->src[0].src.ssa->bit_size == 1)
3056 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
3057 else
3058 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,
3059 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
3060 break;
3061 }
3062 case nir_op_ine: {
3063 if (instr->src[0].src.ssa->bit_size == 1)
3064 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
3065 else
3066 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,
3067 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
3068 break;
3069 }
3070 case nir_op_ult: {
3071 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);
3072 break;
3073 }
3074 case nir_op_uge: {
3075 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);
3076 break;
3077 }
3078 case nir_op_fddx:
3079 case nir_op_fddy:
3080 case nir_op_fddx_fine:
3081 case nir_op_fddy_fine:
3082 case nir_op_fddx_coarse:
3083 case nir_op_fddy_coarse: {
3084 Temp src = get_alu_src(ctx, instr->src[0]);
3085 uint16_t dpp_ctrl1, dpp_ctrl2;
3086 if (instr->op == nir_op_fddx_fine) {
3087 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
3088 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
3089 } else if (instr->op == nir_op_fddy_fine) {
3090 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
3091 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
3092 } else {
3093 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
3094 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
3095 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
3096 else
3097 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
3098 }
3099
3100 Temp tmp;
3101 if (ctx->program->chip_class >= GFX8) {
3102 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
3103 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
3104 } else {
3105 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
3106 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3107 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3108 }
3109 emit_wqm(ctx, tmp, dst, true);
3110 break;
3111 }
3112 default:
3113 fprintf(stderr, "Unknown NIR ALU instr: ");
3114 nir_print_instr(&instr->instr, stderr);
3115 fprintf(stderr, "\n");
3116 }
3117 }
3118
3119 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3120 {
3121 Temp dst = get_ssa_temp(ctx, &instr->def);
3122
3123 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3124 // which get truncated the lsb if double and msb if int
3125 // for now, we only use s_mov_b64 with 64bit inline constants
3126 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3127 assert(dst.type() == RegType::sgpr);
3128
3129 Builder bld(ctx->program, ctx->block);
3130
3131 if (instr->def.bit_size == 1) {
3132 assert(dst.regClass() == bld.lm);
3133 int val = instr->value[0].b ? -1 : 0;
3134 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3135 bld.sop1(Builder::s_mov, Definition(dst), op);
3136 } else if (instr->def.bit_size == 8) {
3137 /* ensure that the value is correctly represented in the low byte of the register */
3138 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3139 } else if (instr->def.bit_size == 16) {
3140 /* ensure that the value is correctly represented in the low half of the register */
3141 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3142 } else if (dst.size() == 1) {
3143 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3144 } else {
3145 assert(dst.size() != 1);
3146 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3147 if (instr->def.bit_size == 64)
3148 for (unsigned i = 0; i < dst.size(); i++)
3149 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3150 else {
3151 for (unsigned i = 0; i < dst.size(); i++)
3152 vec->operands[i] = Operand{instr->value[i].u32};
3153 }
3154 vec->definitions[0] = Definition(dst);
3155 ctx->block->instructions.emplace_back(std::move(vec));
3156 }
3157 }
3158
3159 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3160 {
3161 uint32_t new_mask = 0;
3162 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3163 if (mask & (1u << i))
3164 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3165 return new_mask;
3166 }
3167
3168 struct LoadEmitInfo {
3169 Operand offset;
3170 Temp dst;
3171 unsigned num_components;
3172 unsigned component_size;
3173 Temp resource = Temp(0, s1);
3174 unsigned component_stride = 0;
3175 unsigned const_offset = 0;
3176 unsigned align_mul = 0;
3177 unsigned align_offset = 0;
3178
3179 bool glc = false;
3180 unsigned swizzle_component_size = 0;
3181 memory_sync_info sync;
3182 Temp soffset = Temp(0, s1);
3183 };
3184
3185 using LoadCallback = Temp(*)(
3186 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3187 unsigned align, unsigned const_offset, Temp dst_hint);
3188
3189 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3190 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3191 {
3192 unsigned load_size = info->num_components * info->component_size;
3193 unsigned component_size = info->component_size;
3194
3195 unsigned num_vals = 0;
3196 Temp vals[info->dst.bytes()];
3197
3198 unsigned const_offset = info->const_offset;
3199
3200 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3201 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3202
3203 unsigned bytes_read = 0;
3204 while (bytes_read < load_size) {
3205 unsigned bytes_needed = load_size - bytes_read;
3206
3207 /* add buffer for unaligned loads */
3208 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3209
3210 if (byte_align) {
3211 if ((bytes_needed > 2 ||
3212 (bytes_needed == 2 && (align_mul % 2 || align_offset % 2)) ||
3213 !supports_8bit_16bit_loads) && byte_align_loads) {
3214 if (info->component_stride) {
3215 assert(supports_8bit_16bit_loads && "unimplemented");
3216 bytes_needed = 2;
3217 byte_align = 0;
3218 } else {
3219 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3220 bytes_needed = align(bytes_needed, 4);
3221 }
3222 } else {
3223 byte_align = 0;
3224 }
3225 }
3226
3227 if (info->swizzle_component_size)
3228 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3229 if (info->component_stride)
3230 bytes_needed = MIN2(bytes_needed, info->component_size);
3231
3232 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3233
3234 /* reduce constant offset */
3235 Operand offset = info->offset;
3236 unsigned reduced_const_offset = const_offset;
3237 bool remove_const_offset_completely = need_to_align_offset;
3238 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3239 unsigned to_add = const_offset;
3240 if (remove_const_offset_completely) {
3241 reduced_const_offset = 0;
3242 } else {
3243 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3244 reduced_const_offset %= max_const_offset_plus_one;
3245 }
3246 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3247 if (offset.isConstant()) {
3248 offset = Operand(offset.constantValue() + to_add);
3249 } else if (offset_tmp.regClass() == s1) {
3250 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3251 offset_tmp, Operand(to_add));
3252 } else if (offset_tmp.regClass() == v1) {
3253 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3254 } else {
3255 Temp lo = bld.tmp(offset_tmp.type(), 1);
3256 Temp hi = bld.tmp(offset_tmp.type(), 1);
3257 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3258
3259 if (offset_tmp.regClass() == s2) {
3260 Temp carry = bld.tmp(s1);
3261 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3262 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3263 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3264 } else {
3265 Temp new_lo = bld.tmp(v1);
3266 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3267 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3268 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3269 }
3270 }
3271 }
3272
3273 /* align offset down if needed */
3274 Operand aligned_offset = offset;
3275 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3276 if (need_to_align_offset) {
3277 align = 4;
3278 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3279 if (offset.isConstant()) {
3280 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3281 } else if (offset_tmp.regClass() == s1) {
3282 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3283 } else if (offset_tmp.regClass() == s2) {
3284 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3285 } else if (offset_tmp.regClass() == v1) {
3286 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3287 } else if (offset_tmp.regClass() == v2) {
3288 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3289 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3290 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3291 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3292 }
3293 }
3294 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3295 bld.copy(bld.def(s1), aligned_offset);
3296
3297 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3298 reduced_const_offset, byte_align ? Temp() : info->dst);
3299
3300 /* the callback wrote directly to dst */
3301 if (val == info->dst) {
3302 assert(num_vals == 0);
3303 emit_split_vector(ctx, info->dst, info->num_components);
3304 return;
3305 }
3306
3307 /* shift result right if needed */
3308 if (info->component_size < 4 && byte_align_loads) {
3309 Operand align((uint32_t)byte_align);
3310 if (byte_align == -1) {
3311 if (offset.isConstant())
3312 align = Operand(offset.constantValue() % 4u);
3313 else if (offset.size() == 2)
3314 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3315 else
3316 align = offset;
3317 }
3318
3319 assert(val.bytes() >= load_size && "unimplemented");
3320 if (val.type() == RegType::sgpr)
3321 byte_align_scalar(ctx, val, align, info->dst);
3322 else
3323 byte_align_vector(ctx, val, align, info->dst, component_size);
3324 return;
3325 }
3326
3327 /* add result to list and advance */
3328 if (info->component_stride) {
3329 assert(val.bytes() == info->component_size && "unimplemented");
3330 const_offset += info->component_stride;
3331 align_offset = (align_offset + info->component_stride) % align_mul;
3332 } else {
3333 const_offset += val.bytes();
3334 align_offset = (align_offset + val.bytes()) % align_mul;
3335 }
3336 bytes_read += val.bytes();
3337 vals[num_vals++] = val;
3338 }
3339
3340 /* create array of components */
3341 unsigned components_split = 0;
3342 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3343 bool has_vgprs = false;
3344 for (unsigned i = 0; i < num_vals;) {
3345 Temp tmp[num_vals];
3346 unsigned num_tmps = 0;
3347 unsigned tmp_size = 0;
3348 RegType reg_type = RegType::sgpr;
3349 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3350 if (vals[i].type() == RegType::vgpr)
3351 reg_type = RegType::vgpr;
3352 tmp_size += vals[i].bytes();
3353 tmp[num_tmps++] = vals[i++];
3354 }
3355 if (num_tmps > 1) {
3356 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3357 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3358 for (unsigned i = 0; i < num_tmps; i++)
3359 vec->operands[i] = Operand(tmp[i]);
3360 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3361 vec->definitions[0] = Definition(tmp[0]);
3362 bld.insert(std::move(vec));
3363 }
3364
3365 if (tmp[0].bytes() % component_size) {
3366 /* trim tmp[0] */
3367 assert(i == num_vals);
3368 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3369 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3370 }
3371
3372 RegClass elem_rc = RegClass::get(reg_type, component_size);
3373
3374 unsigned start = components_split;
3375
3376 if (tmp_size == elem_rc.bytes()) {
3377 allocated_vec[components_split++] = tmp[0];
3378 } else {
3379 assert(tmp_size % elem_rc.bytes() == 0);
3380 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3381 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3382 for (unsigned i = 0; i < split->definitions.size(); i++) {
3383 Temp component = bld.tmp(elem_rc);
3384 allocated_vec[components_split++] = component;
3385 split->definitions[i] = Definition(component);
3386 }
3387 split->operands[0] = Operand(tmp[0]);
3388 bld.insert(std::move(split));
3389 }
3390
3391 /* try to p_as_uniform early so we can create more optimizable code and
3392 * also update allocated_vec */
3393 for (unsigned j = start; j < components_split; j++) {
3394 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3395 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3396 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3397 }
3398 }
3399
3400 /* concatenate components and p_as_uniform() result if needed */
3401 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3402 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3403
3404 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3405
3406 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3407 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3408 for (unsigned i = 0; i < info->num_components; i++)
3409 vec->operands[i] = Operand(allocated_vec[i]);
3410 if (padding_bytes)
3411 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3412 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3413 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3414 vec->definitions[0] = Definition(tmp);
3415 bld.insert(std::move(vec));
3416 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3417 } else {
3418 vec->definitions[0] = Definition(info->dst);
3419 bld.insert(std::move(vec));
3420 }
3421 }
3422
3423 Operand load_lds_size_m0(Builder& bld)
3424 {
3425 /* TODO: m0 does not need to be initialized on GFX9+ */
3426 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3427 }
3428
3429 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3430 Temp offset, unsigned bytes_needed,
3431 unsigned align, unsigned const_offset,
3432 Temp dst_hint)
3433 {
3434 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3435
3436 Operand m = load_lds_size_m0(bld);
3437
3438 bool large_ds_read = bld.program->chip_class >= GFX7;
3439 bool usable_read2 = bld.program->chip_class >= GFX7;
3440
3441 bool read2 = false;
3442 unsigned size = 0;
3443 aco_opcode op;
3444 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3445 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3446 size = 16;
3447 op = aco_opcode::ds_read_b128;
3448 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3449 size = 16;
3450 read2 = true;
3451 op = aco_opcode::ds_read2_b64;
3452 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3453 size = 12;
3454 op = aco_opcode::ds_read_b96;
3455 } else if (bytes_needed >= 8 && align % 8 == 0) {
3456 size = 8;
3457 op = aco_opcode::ds_read_b64;
3458 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3459 size = 8;
3460 read2 = true;
3461 op = aco_opcode::ds_read2_b32;
3462 } else if (bytes_needed >= 4 && align % 4 == 0) {
3463 size = 4;
3464 op = aco_opcode::ds_read_b32;
3465 } else if (bytes_needed >= 2 && align % 2 == 0) {
3466 size = 2;
3467 op = aco_opcode::ds_read_u16;
3468 } else {
3469 size = 1;
3470 op = aco_opcode::ds_read_u8;
3471 }
3472
3473 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3474 if (const_offset >= max_offset_plus_one) {
3475 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3476 const_offset %= max_offset_plus_one;
3477 }
3478
3479 if (read2)
3480 const_offset /= (size / 2u);
3481
3482 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3483 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3484 Instruction *instr;
3485 if (read2)
3486 instr = bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3487 else
3488 instr = bld.ds(op, Definition(val), offset, m, const_offset);
3489 static_cast<DS_instruction *>(instr)->sync = info->sync;
3490
3491 if (size < 4)
3492 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3493
3494 return val;
3495 }
3496
3497 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3498
3499 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3500 Temp offset, unsigned bytes_needed,
3501 unsigned align, unsigned const_offset,
3502 Temp dst_hint)
3503 {
3504 unsigned size = 0;
3505 aco_opcode op;
3506 if (bytes_needed <= 4) {
3507 size = 1;
3508 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3509 } else if (bytes_needed <= 8) {
3510 size = 2;
3511 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3512 } else if (bytes_needed <= 16) {
3513 size = 4;
3514 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3515 } else if (bytes_needed <= 32) {
3516 size = 8;
3517 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3518 } else {
3519 size = 16;
3520 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3521 }
3522 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3523 if (info->resource.id()) {
3524 load->operands[0] = Operand(info->resource);
3525 load->operands[1] = Operand(offset);
3526 } else {
3527 load->operands[0] = Operand(offset);
3528 load->operands[1] = Operand(0u);
3529 }
3530 RegClass rc(RegType::sgpr, size);
3531 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3532 load->definitions[0] = Definition(val);
3533 load->glc = info->glc;
3534 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3535 load->sync = info->sync;
3536 bld.insert(std::move(load));
3537 return val;
3538 }
3539
3540 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3541
3542 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3543 Temp offset, unsigned bytes_needed,
3544 unsigned align_, unsigned const_offset,
3545 Temp dst_hint)
3546 {
3547 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3548 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3549
3550 if (info->soffset.id()) {
3551 if (soffset.isTemp())
3552 vaddr = bld.copy(bld.def(v1), soffset);
3553 soffset = Operand(info->soffset);
3554 }
3555
3556 unsigned bytes_size = 0;
3557 aco_opcode op;
3558 if (bytes_needed == 1 || align_ % 2) {
3559 bytes_size = 1;
3560 op = aco_opcode::buffer_load_ubyte;
3561 } else if (bytes_needed == 2 || align_ % 4) {
3562 bytes_size = 2;
3563 op = aco_opcode::buffer_load_ushort;
3564 } else if (bytes_needed <= 4) {
3565 bytes_size = 4;
3566 op = aco_opcode::buffer_load_dword;
3567 } else if (bytes_needed <= 8) {
3568 bytes_size = 8;
3569 op = aco_opcode::buffer_load_dwordx2;
3570 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3571 bytes_size = 12;
3572 op = aco_opcode::buffer_load_dwordx3;
3573 } else {
3574 bytes_size = 16;
3575 op = aco_opcode::buffer_load_dwordx4;
3576 }
3577 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3578 mubuf->operands[0] = Operand(info->resource);
3579 mubuf->operands[1] = vaddr;
3580 mubuf->operands[2] = soffset;
3581 mubuf->offen = (offset.type() == RegType::vgpr);
3582 mubuf->glc = info->glc;
3583 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3584 mubuf->sync = info->sync;
3585 mubuf->offset = const_offset;
3586 mubuf->swizzled = info->swizzle_component_size != 0;
3587 RegClass rc = RegClass::get(RegType::vgpr, bytes_size);
3588 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3589 mubuf->definitions[0] = Definition(val);
3590 bld.insert(std::move(mubuf));
3591
3592 return val;
3593 }
3594
3595 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3596 static auto emit_scratch_load = emit_load<mubuf_load_callback, false, true, 4096>;
3597
3598 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3599 {
3600 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3601 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3602
3603 if (addr.type() == RegType::vgpr)
3604 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3605 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3606 }
3607
3608 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3609 Temp offset, unsigned bytes_needed,
3610 unsigned align_, unsigned const_offset,
3611 Temp dst_hint)
3612 {
3613 unsigned bytes_size = 0;
3614 bool mubuf = bld.program->chip_class == GFX6;
3615 bool global = bld.program->chip_class >= GFX9;
3616 aco_opcode op;
3617 if (bytes_needed == 1) {
3618 bytes_size = 1;
3619 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3620 } else if (bytes_needed == 2) {
3621 bytes_size = 2;
3622 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3623 } else if (bytes_needed <= 4) {
3624 bytes_size = 4;
3625 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3626 } else if (bytes_needed <= 8) {
3627 bytes_size = 8;
3628 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3629 } else if (bytes_needed <= 12 && !mubuf) {
3630 bytes_size = 12;
3631 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3632 } else {
3633 bytes_size = 16;
3634 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3635 }
3636 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3637 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3638 if (mubuf) {
3639 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3640 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3641 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3642 mubuf->operands[2] = Operand(0u);
3643 mubuf->glc = info->glc;
3644 mubuf->dlc = false;
3645 mubuf->offset = 0;
3646 mubuf->addr64 = offset.type() == RegType::vgpr;
3647 mubuf->disable_wqm = false;
3648 mubuf->sync = info->sync;
3649 mubuf->definitions[0] = Definition(val);
3650 bld.insert(std::move(mubuf));
3651 } else {
3652 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3653
3654 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3655 flat->operands[0] = Operand(offset);
3656 flat->operands[1] = Operand(s1);
3657 flat->glc = info->glc;
3658 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3659 flat->sync = info->sync;
3660 flat->offset = 0u;
3661 flat->definitions[0] = Definition(val);
3662 bld.insert(std::move(flat));
3663 }
3664
3665 return val;
3666 }
3667
3668 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3669
3670 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3671 Temp address, unsigned base_offset, unsigned align)
3672 {
3673 assert(util_is_power_of_two_nonzero(align));
3674
3675 Builder bld(ctx->program, ctx->block);
3676
3677 unsigned num_components = dst.bytes() / elem_size_bytes;
3678 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3679 info.align_mul = align;
3680 info.align_offset = 0;
3681 info.sync = memory_sync_info(storage_shared);
3682 info.const_offset = base_offset;
3683 emit_lds_load(ctx, bld, &info);
3684
3685 return dst;
3686 }
3687
3688 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3689 {
3690 if (!count)
3691 return;
3692
3693 Builder bld(ctx->program, ctx->block);
3694
3695 ASSERTED bool is_subdword = false;
3696 for (unsigned i = 0; i < count; i++)
3697 is_subdword |= offsets[i] % 4;
3698 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3699 assert(!is_subdword || dst_type == RegType::vgpr);
3700
3701 /* count == 1 fast path */
3702 if (count == 1) {
3703 if (dst_type == RegType::sgpr)
3704 dst[0] = bld.as_uniform(src);
3705 else
3706 dst[0] = as_vgpr(ctx, src);
3707 return;
3708 }
3709
3710 for (unsigned i = 0; i < count - 1; i++)
3711 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3712 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3713
3714 if (is_subdword && src.type() == RegType::sgpr) {
3715 src = as_vgpr(ctx, src);
3716 } else {
3717 /* use allocated_vec if possible */
3718 auto it = ctx->allocated_vec.find(src.id());
3719 if (it != ctx->allocated_vec.end()) {
3720 if (!it->second[0].id())
3721 goto split;
3722 unsigned elem_size = it->second[0].bytes();
3723 assert(src.bytes() % elem_size == 0);
3724
3725 for (unsigned i = 0; i < src.bytes() / elem_size; i++) {
3726 if (!it->second[i].id())
3727 goto split;
3728 }
3729
3730 for (unsigned i = 0; i < count; i++) {
3731 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3732 goto split;
3733 }
3734
3735 for (unsigned i = 0; i < count; i++) {
3736 unsigned start_idx = offsets[i] / elem_size;
3737 unsigned op_count = dst[i].bytes() / elem_size;
3738 if (op_count == 1) {
3739 if (dst_type == RegType::sgpr)
3740 dst[i] = bld.as_uniform(it->second[start_idx]);
3741 else
3742 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3743 continue;
3744 }
3745
3746 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3747 for (unsigned j = 0; j < op_count; j++) {
3748 Temp tmp = it->second[start_idx + j];
3749 if (dst_type == RegType::sgpr)
3750 tmp = bld.as_uniform(tmp);
3751 vec->operands[j] = Operand(tmp);
3752 }
3753 vec->definitions[0] = Definition(dst[i]);
3754 bld.insert(std::move(vec));
3755 }
3756 return;
3757 }
3758 }
3759
3760 split:
3761
3762 if (dst_type == RegType::sgpr)
3763 src = bld.as_uniform(src);
3764
3765 /* just split it */
3766 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3767 split->operands[0] = Operand(src);
3768 for (unsigned i = 0; i < count; i++)
3769 split->definitions[i] = Definition(dst[i]);
3770 bld.insert(std::move(split));
3771 }
3772
3773 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3774 int *start, int *count)
3775 {
3776 unsigned start_elem = ffs(todo_mask) - 1;
3777 bool skip = !(mask & (1 << start_elem));
3778 if (skip)
3779 mask = ~mask & todo_mask;
3780
3781 mask &= todo_mask;
3782
3783 u_bit_scan_consecutive_range(&mask, start, count);
3784
3785 return !skip;
3786 }
3787
3788 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3789 {
3790 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3791 }
3792
3793 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3794 Temp address, unsigned base_offset, unsigned align)
3795 {
3796 assert(util_is_power_of_two_nonzero(align));
3797 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3798
3799 Builder bld(ctx->program, ctx->block);
3800 bool large_ds_write = ctx->options->chip_class >= GFX7;
3801 bool usable_write2 = ctx->options->chip_class >= GFX7;
3802
3803 unsigned write_count = 0;
3804 Temp write_datas[32];
3805 unsigned offsets[32];
3806 aco_opcode opcodes[32];
3807
3808 wrmask = widen_mask(wrmask, elem_size_bytes);
3809
3810 uint32_t todo = u_bit_consecutive(0, data.bytes());
3811 while (todo) {
3812 int offset, bytes;
3813 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3814 offsets[write_count] = offset;
3815 opcodes[write_count] = aco_opcode::num_opcodes;
3816 write_count++;
3817 advance_write_mask(&todo, offset, bytes);
3818 continue;
3819 }
3820
3821 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3822 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3823 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3824 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3825
3826 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3827 aco_opcode op = aco_opcode::num_opcodes;
3828 if (bytes >= 16 && aligned16 && large_ds_write) {
3829 op = aco_opcode::ds_write_b128;
3830 bytes = 16;
3831 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3832 op = aco_opcode::ds_write_b96;
3833 bytes = 12;
3834 } else if (bytes >= 8 && aligned8) {
3835 op = aco_opcode::ds_write_b64;
3836 bytes = 8;
3837 } else if (bytes >= 4 && aligned4) {
3838 op = aco_opcode::ds_write_b32;
3839 bytes = 4;
3840 } else if (bytes >= 2 && aligned2) {
3841 op = aco_opcode::ds_write_b16;
3842 bytes = 2;
3843 } else if (bytes >= 1) {
3844 op = aco_opcode::ds_write_b8;
3845 bytes = 1;
3846 } else {
3847 assert(false);
3848 }
3849
3850 offsets[write_count] = offset;
3851 opcodes[write_count] = op;
3852 write_count++;
3853 advance_write_mask(&todo, offset, bytes);
3854 }
3855
3856 Operand m = load_lds_size_m0(bld);
3857
3858 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3859
3860 for (unsigned i = 0; i < write_count; i++) {
3861 aco_opcode op = opcodes[i];
3862 if (op == aco_opcode::num_opcodes)
3863 continue;
3864
3865 Temp data = write_datas[i];
3866
3867 unsigned second = write_count;
3868 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3869 for (second = i + 1; second < write_count; second++) {
3870 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3871 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3872 opcodes[second] = aco_opcode::num_opcodes;
3873 break;
3874 }
3875 }
3876 }
3877
3878 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3879 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3880
3881 unsigned inline_offset = base_offset + offsets[i];
3882 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3883 Temp address_offset = address;
3884 if (inline_offset > max_offset) {
3885 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3886 inline_offset = offsets[i];
3887 }
3888 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3889
3890 Instruction *instr;
3891 if (write2) {
3892 Temp second_data = write_datas[second];
3893 inline_offset /= data.bytes();
3894 instr = bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3895 } else {
3896 instr = bld.ds(op, address_offset, data, m, inline_offset);
3897 }
3898 static_cast<DS_instruction *>(instr)->sync =
3899 memory_sync_info(storage_shared);
3900 }
3901 }
3902
3903 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3904 {
3905 unsigned align = 16;
3906 if (const_offset)
3907 align = std::min(align, 1u << (ffs(const_offset) - 1));
3908
3909 return align;
3910 }
3911
3912
3913 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3914 {
3915 switch (bytes) {
3916 case 1:
3917 assert(!smem);
3918 return aco_opcode::buffer_store_byte;
3919 case 2:
3920 assert(!smem);
3921 return aco_opcode::buffer_store_short;
3922 case 4:
3923 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3924 case 8:
3925 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3926 case 12:
3927 assert(!smem);
3928 return aco_opcode::buffer_store_dwordx3;
3929 case 16:
3930 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3931 }
3932 unreachable("Unexpected store size");
3933 return aco_opcode::num_opcodes;
3934 }
3935
3936 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3937 Temp data, unsigned writemask, int swizzle_element_size,
3938 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3939 {
3940 unsigned write_count_with_skips = 0;
3941 bool skips[16];
3942
3943 /* determine how to split the data */
3944 unsigned todo = u_bit_consecutive(0, data.bytes());
3945 while (todo) {
3946 int offset, bytes;
3947 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3948 offsets[write_count_with_skips] = offset;
3949 if (skips[write_count_with_skips]) {
3950 advance_write_mask(&todo, offset, bytes);
3951 write_count_with_skips++;
3952 continue;
3953 }
3954
3955 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3956 * larger than swizzle_element_size */
3957 bytes = MIN2(bytes, swizzle_element_size);
3958 if (bytes % 4)
3959 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3960
3961 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3962 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3963 bytes = 8;
3964
3965 /* dword or larger stores have to be dword-aligned */
3966 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3967 unsigned align_offset = (instr ? nir_intrinsic_align_offset(instr) : 0) + offset;
3968 bool dword_aligned = align_offset % 4 == 0 && align_mul % 4 == 0;
3969 if (!dword_aligned)
3970 bytes = MIN2(bytes, (align_offset % 2 == 0 && align_mul % 2 == 0) ? 2 : 1);
3971
3972 advance_write_mask(&todo, offset, bytes);
3973 write_count_with_skips++;
3974 }
3975
3976 /* actually split data */
3977 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3978
3979 /* remove skips */
3980 for (unsigned i = 0; i < write_count_with_skips; i++) {
3981 if (skips[i])
3982 continue;
3983 write_datas[*write_count] = write_datas[i];
3984 offsets[*write_count] = offsets[i];
3985 (*write_count)++;
3986 }
3987 }
3988
3989 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3990 unsigned split_cnt = 0u, Temp dst = Temp())
3991 {
3992 Builder bld(ctx->program, ctx->block);
3993 unsigned dword_size = elem_size_bytes / 4;
3994
3995 if (!dst.id())
3996 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3997
3998 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3999 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
4000 instr->definitions[0] = Definition(dst);
4001
4002 for (unsigned i = 0; i < cnt; ++i) {
4003 if (arr[i].id()) {
4004 assert(arr[i].size() == dword_size);
4005 allocated_vec[i] = arr[i];
4006 instr->operands[i] = Operand(arr[i]);
4007 } else {
4008 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
4009 allocated_vec[i] = zero;
4010 instr->operands[i] = Operand(zero);
4011 }
4012 }
4013
4014 bld.insert(std::move(instr));
4015
4016 if (split_cnt)
4017 emit_split_vector(ctx, dst, split_cnt);
4018 else
4019 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
4020
4021 return dst;
4022 }
4023
4024 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
4025 {
4026 if (const_offset >= 4096) {
4027 unsigned excess_const_offset = const_offset / 4096u * 4096u;
4028 const_offset %= 4096u;
4029
4030 if (!voffset.id())
4031 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
4032 else if (unlikely(voffset.regClass() == s1))
4033 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
4034 else if (likely(voffset.regClass() == v1))
4035 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
4036 else
4037 unreachable("Unsupported register class of voffset");
4038 }
4039
4040 return const_offset;
4041 }
4042
4043 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
4044 unsigned const_offset = 0u, memory_sync_info sync=memory_sync_info(),
4045 bool slc = false, bool swizzled = false)
4046 {
4047 assert(vdata.id());
4048 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
4049 assert(vdata.size() >= 1 && vdata.size() <= 4);
4050
4051 Builder bld(ctx->program, ctx->block);
4052 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
4053 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
4054
4055 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
4056 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
4057 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
4058 /* offen */ !voffset_op.isUndefined(), /* swizzled */ swizzled,
4059 /* idxen*/ false, /* addr64 */ false, /* disable_wqm */ false, /* glc */ true,
4060 /* dlc*/ false, /* slc */ slc);
4061
4062 static_cast<MUBUF_instruction *>(r.instr)->sync = sync;
4063 }
4064
4065 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
4066 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
4067 bool allow_combining = true, memory_sync_info sync=memory_sync_info(), bool slc = false)
4068 {
4069 Builder bld(ctx->program, ctx->block);
4070 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4071 assert(write_mask);
4072 write_mask = widen_mask(write_mask, elem_size_bytes);
4073
4074 unsigned write_count = 0;
4075 Temp write_datas[32];
4076 unsigned offsets[32];
4077 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
4078 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
4079
4080 for (unsigned i = 0; i < write_count; i++) {
4081 unsigned const_offset = offsets[i] + base_const_offset;
4082 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, sync, slc, !allow_combining);
4083 }
4084 }
4085
4086 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4087 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4088 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4089 {
4090 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4091 assert((num_components * elem_size_bytes) == dst.bytes());
4092 assert(!!stride != allow_combining);
4093
4094 Builder bld(ctx->program, ctx->block);
4095
4096 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4097 info.component_stride = allow_combining ? 0 : stride;
4098 info.glc = true;
4099 info.swizzle_component_size = allow_combining ? 0 : 4;
4100 info.align_mul = MIN2(elem_size_bytes, 4);
4101 info.align_offset = 0;
4102 info.soffset = soffset;
4103 info.const_offset = base_const_offset;
4104 emit_mubuf_load(ctx, bld, &info);
4105 }
4106
4107 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)
4108 {
4109 Builder bld(ctx->program, ctx->block);
4110 Temp offset = base_offset.first;
4111 unsigned const_offset = base_offset.second;
4112
4113 if (!nir_src_is_const(*off_src)) {
4114 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4115 Temp with_stride;
4116
4117 /* Calculate indirect offset with stride */
4118 if (likely(indirect_offset_arg.regClass() == v1))
4119 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4120 else if (indirect_offset_arg.regClass() == s1)
4121 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4122 else
4123 unreachable("Unsupported register class of indirect offset");
4124
4125 /* Add to the supplied base offset */
4126 if (offset.id() == 0)
4127 offset = with_stride;
4128 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4129 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4130 else if (offset.size() == 1 && with_stride.size() == 1)
4131 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4132 else
4133 unreachable("Unsupported register class of indirect offset");
4134 } else {
4135 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4136 const_offset += const_offset_arg * stride;
4137 }
4138
4139 return std::make_pair(offset, const_offset);
4140 }
4141
4142 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4143 {
4144 Builder bld(ctx->program, ctx->block);
4145 Temp offset;
4146
4147 if (off1.first.id() && off2.first.id()) {
4148 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4149 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4150 else if (off1.first.size() == 1 && off2.first.size() == 1)
4151 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4152 else
4153 unreachable("Unsupported register class of indirect offset");
4154 } else {
4155 offset = off1.first.id() ? off1.first : off2.first;
4156 }
4157
4158 return std::make_pair(offset, off1.second + off2.second);
4159 }
4160
4161 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4162 {
4163 Builder bld(ctx->program, ctx->block);
4164 unsigned const_offset = offs.second * multiplier;
4165
4166 if (!offs.first.id())
4167 return std::make_pair(offs.first, const_offset);
4168
4169 Temp offset = unlikely(offs.first.regClass() == s1)
4170 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4171 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4172
4173 return std::make_pair(offset, const_offset);
4174 }
4175
4176 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4177 {
4178 Builder bld(ctx->program, ctx->block);
4179
4180 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4181 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4182 /* component is in bytes */
4183 const_offset += nir_intrinsic_component(instr) * component_stride;
4184
4185 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4186 nir_src *off_src = nir_get_io_offset_src(instr);
4187 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4188 }
4189
4190 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4191 {
4192 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4193 }
4194
4195 Temp get_tess_rel_patch_id(isel_context *ctx)
4196 {
4197 Builder bld(ctx->program, ctx->block);
4198
4199 switch (ctx->shader->info.stage) {
4200 case MESA_SHADER_TESS_CTRL:
4201 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4202 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4203 case MESA_SHADER_TESS_EVAL:
4204 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4205 default:
4206 unreachable("Unsupported stage in get_tess_rel_patch_id");
4207 }
4208 }
4209
4210 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4211 {
4212 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4213 Builder bld(ctx->program, ctx->block);
4214
4215 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4216 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4217
4218 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4219
4220 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4221 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4222
4223 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4224 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4225 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4226
4227 return offset_mul(ctx, offs, 4u);
4228 }
4229
4230 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4231 {
4232 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4233 Builder bld(ctx->program, ctx->block);
4234
4235 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4236 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4237 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4238 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4239
4240 std::pair<Temp, unsigned> offs = instr
4241 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4242 : std::make_pair(Temp(), 0u);
4243
4244 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4245 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4246
4247 if (per_vertex) {
4248 assert(instr);
4249
4250 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4251 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4252
4253 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4254 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4255 } else {
4256 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4257 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4258 }
4259
4260 return offs;
4261 }
4262
4263 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4264 {
4265 Builder bld(ctx->program, ctx->block);
4266
4267 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4268 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4269
4270 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4271
4272 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4273 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4274 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4275
4276 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4277 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4278
4279 return offs;
4280 }
4281
4282 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4283 {
4284 Builder bld(ctx->program, ctx->block);
4285
4286 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4287 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4288 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4289 unsigned attr_stride = ctx->tcs_num_patches;
4290
4291 std::pair<Temp, unsigned> offs = instr
4292 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4293 : std::make_pair(Temp(), 0u);
4294
4295 if (const_base_offset)
4296 offs.second += const_base_offset * attr_stride;
4297
4298 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4299 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4300 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4301
4302 return offs;
4303 }
4304
4305 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4306 {
4307 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4308
4309 if (mask == 0)
4310 return false;
4311
4312 unsigned drv_loc = nir_intrinsic_base(instr);
4313 nir_src *off_src = nir_get_io_offset_src(instr);
4314
4315 if (!nir_src_is_const(*off_src)) {
4316 *indirect = true;
4317 return false;
4318 }
4319
4320 *indirect = false;
4321 uint64_t slot = per_vertex
4322 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4323 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4324 return (((uint64_t) 1) << slot) & mask;
4325 }
4326
4327 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4328 {
4329 unsigned write_mask = nir_intrinsic_write_mask(instr);
4330 unsigned component = nir_intrinsic_component(instr);
4331 unsigned idx = nir_intrinsic_base(instr) + component;
4332
4333 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4334 if (off_instr->type != nir_instr_type_load_const)
4335 return false;
4336
4337 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4338 idx += nir_src_as_uint(instr->src[1]) * 4u;
4339
4340 if (instr->src[0].ssa->bit_size == 64)
4341 write_mask = widen_mask(write_mask, 2);
4342
4343 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4344
4345 for (unsigned i = 0; i < 8; ++i) {
4346 if (write_mask & (1 << i)) {
4347 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4348 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4349 }
4350 idx++;
4351 }
4352
4353 return true;
4354 }
4355
4356 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4357 {
4358 /* Only TCS per-vertex inputs are supported by this function.
4359 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4360 */
4361 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4362 return false;
4363
4364 nir_src *off_src = nir_get_io_offset_src(instr);
4365 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4366 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4367 bool can_use_temps = nir_src_is_const(*off_src) &&
4368 vertex_index_instr->type == nir_instr_type_intrinsic &&
4369 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4370
4371 if (!can_use_temps)
4372 return false;
4373
4374 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4375 Temp *src = &ctx->inputs.temps[idx];
4376 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4377
4378 return true;
4379 }
4380
4381 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4382 {
4383 Builder bld(ctx->program, ctx->block);
4384
4385 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4386 /* 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. */
4387 bool indirect_write;
4388 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4389 if (temp_only_input && !indirect_write)
4390 return;
4391 }
4392
4393 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4394 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4395 unsigned write_mask = nir_intrinsic_write_mask(instr);
4396 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4397
4398 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4399 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4400 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4401 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4402 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, memory_sync_info(), true);
4403 } else {
4404 Temp lds_base;
4405
4406 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4407 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4408 unsigned itemsize = ctx->stage == vertex_geometry_gs
4409 ? ctx->program->info->vs.es_info.esgs_itemsize
4410 : ctx->program->info->tes.es_info.esgs_itemsize;
4411 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4412 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));
4413 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4414 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4415 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4416 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4417 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4418 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4419 */
4420 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4421 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4422 } else {
4423 unreachable("Invalid LS or ES stage");
4424 }
4425
4426 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4427 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4428 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4429 }
4430 }
4431
4432 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4433 {
4434 if (per_vertex)
4435 return false;
4436
4437 unsigned off = nir_intrinsic_base(instr) * 4u;
4438 return off == ctx->tcs_tess_lvl_out_loc ||
4439 off == ctx->tcs_tess_lvl_in_loc;
4440
4441 }
4442
4443 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4444 {
4445 uint64_t mask = per_vertex
4446 ? ctx->program->info->tcs.tes_inputs_read
4447 : ctx->program->info->tcs.tes_patch_inputs_read;
4448
4449 bool indirect_write = false;
4450 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4451 return indirect_write || output_read_by_tes;
4452 }
4453
4454 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4455 {
4456 uint64_t mask = per_vertex
4457 ? ctx->shader->info.outputs_read
4458 : ctx->shader->info.patch_outputs_read;
4459
4460 bool indirect_write = false;
4461 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4462 return indirect_write || output_read;
4463 }
4464
4465 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4466 {
4467 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4468 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4469
4470 Builder bld(ctx->program, ctx->block);
4471
4472 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4473 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4474 unsigned write_mask = nir_intrinsic_write_mask(instr);
4475
4476 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4477 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4478 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4479
4480 if (write_to_vmem) {
4481 std::pair<Temp, unsigned> vmem_offs = per_vertex
4482 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4483 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4484
4485 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));
4486 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4487 store_vmem_mubuf(ctx, store_val, hs_ring_tess_offchip, vmem_offs.first, oc_lds, vmem_offs.second, elem_size_bytes, write_mask, true, memory_sync_info(storage_vmem_output));
4488 }
4489
4490 if (write_to_lds) {
4491 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4492 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4493 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4494 }
4495 }
4496
4497 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4498 {
4499 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4500 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4501
4502 Builder bld(ctx->program, ctx->block);
4503
4504 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4505 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4506 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4507 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4508
4509 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4510 }
4511
4512 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4513 {
4514 if (ctx->stage == vertex_vs ||
4515 ctx->stage == tess_eval_vs ||
4516 ctx->stage == fragment_fs ||
4517 ctx->stage == ngg_vertex_gs ||
4518 ctx->stage == ngg_tess_eval_gs ||
4519 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4520 bool stored_to_temps = store_output_to_temps(ctx, instr);
4521 if (!stored_to_temps) {
4522 fprintf(stderr, "Unimplemented output offset instruction:\n");
4523 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4524 fprintf(stderr, "\n");
4525 abort();
4526 }
4527 } else if (ctx->stage == vertex_es ||
4528 ctx->stage == vertex_ls ||
4529 ctx->stage == tess_eval_es ||
4530 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4531 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4532 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4533 visit_store_ls_or_es_output(ctx, instr);
4534 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4535 visit_store_tcs_output(ctx, instr, false);
4536 } else {
4537 unreachable("Shader stage not implemented");
4538 }
4539 }
4540
4541 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4542 {
4543 visit_load_tcs_output(ctx, instr, false);
4544 }
4545
4546 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4547 {
4548 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4549 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4550
4551 Builder bld(ctx->program, ctx->block);
4552
4553 if (dst.regClass() == v2b) {
4554 if (ctx->program->has_16bank_lds) {
4555 assert(ctx->options->chip_class <= GFX8);
4556 Builder::Result interp_p1 =
4557 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4558 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4559 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4560 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4561 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4562 bld.m0(prim_mask), interp_p1, idx, component);
4563 } else {
4564 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4565
4566 if (ctx->options->chip_class == GFX8)
4567 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4568
4569 Builder::Result interp_p1 =
4570 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4571 coord1, bld.m0(prim_mask), idx, component);
4572 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4573 interp_p1, idx, component);
4574 }
4575 } else {
4576 Builder::Result interp_p1 =
4577 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4578 bld.m0(prim_mask), idx, component);
4579
4580 if (ctx->program->has_16bank_lds)
4581 interp_p1.instr->operands[0].setLateKill(true);
4582
4583 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4584 bld.m0(prim_mask), interp_p1, idx, component);
4585 }
4586 }
4587
4588 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4589 {
4590 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4591 for (unsigned i = 0; i < num_components; i++)
4592 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4593 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4594 assert(num_components == 4);
4595 Builder bld(ctx->program, ctx->block);
4596 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4597 }
4598
4599 for (Operand& op : vec->operands)
4600 op = op.isUndefined() ? Operand(0u) : op;
4601
4602 vec->definitions[0] = Definition(dst);
4603 ctx->block->instructions.emplace_back(std::move(vec));
4604 emit_split_vector(ctx, dst, num_components);
4605 return;
4606 }
4607
4608 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4609 {
4610 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4611 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4612 unsigned idx = nir_intrinsic_base(instr);
4613 unsigned component = nir_intrinsic_component(instr);
4614 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4615
4616 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4617 if (offset) {
4618 assert(offset->u32 == 0);
4619 } else {
4620 /* the lower 15bit of the prim_mask contain the offset into LDS
4621 * while the upper bits contain the number of prims */
4622 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4623 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4624 Builder bld(ctx->program, ctx->block);
4625 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4626 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4627 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4628 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4629 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4630 }
4631
4632 if (instr->dest.ssa.num_components == 1) {
4633 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4634 } else {
4635 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4636 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4637 {
4638 Temp tmp = {ctx->program->allocateId(), v1};
4639 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4640 vec->operands[i] = Operand(tmp);
4641 }
4642 vec->definitions[0] = Definition(dst);
4643 ctx->block->instructions.emplace_back(std::move(vec));
4644 }
4645 }
4646
4647 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4648 unsigned offset, unsigned stride, unsigned channels)
4649 {
4650 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4651 if (vtx_info->chan_byte_size != 4 && channels == 3)
4652 return false;
4653 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4654 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4655 }
4656
4657 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4658 unsigned offset, unsigned stride, unsigned *channels)
4659 {
4660 if (!vtx_info->chan_byte_size) {
4661 *channels = vtx_info->num_channels;
4662 return vtx_info->chan_format;
4663 }
4664
4665 unsigned num_channels = *channels;
4666 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4667 unsigned new_channels = num_channels + 1;
4668 /* first, assume more loads is worse and try using a larger data format */
4669 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4670 new_channels++;
4671 /* don't make the attribute potentially out-of-bounds */
4672 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4673 new_channels = 5;
4674 }
4675
4676 if (new_channels == 5) {
4677 /* then try decreasing load size (at the cost of more loads) */
4678 new_channels = *channels;
4679 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4680 new_channels--;
4681 }
4682
4683 if (new_channels < *channels)
4684 *channels = new_channels;
4685 num_channels = new_channels;
4686 }
4687
4688 switch (vtx_info->chan_format) {
4689 case V_008F0C_BUF_DATA_FORMAT_8:
4690 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4691 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4692 case V_008F0C_BUF_DATA_FORMAT_16:
4693 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4694 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4695 case V_008F0C_BUF_DATA_FORMAT_32:
4696 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4697 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4698 }
4699 unreachable("shouldn't reach here");
4700 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4701 }
4702
4703 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4704 * so we may need to fix it up. */
4705 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4706 {
4707 Builder bld(ctx->program, ctx->block);
4708
4709 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4710 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4711
4712 /* For the integer-like cases, do a natural sign extension.
4713 *
4714 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4715 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4716 * exponent.
4717 */
4718 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4719 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4720
4721 /* Convert back to the right type. */
4722 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4723 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4724 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4725 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4726 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4727 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4728 }
4729
4730 return alpha;
4731 }
4732
4733 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4734 {
4735 Builder bld(ctx->program, ctx->block);
4736 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4737 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4738
4739 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4740 if (off_instr->type != nir_instr_type_load_const) {
4741 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4742 nir_print_instr(off_instr, stderr);
4743 fprintf(stderr, "\n");
4744 }
4745 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4746
4747 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4748
4749 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4750 unsigned component = nir_intrinsic_component(instr);
4751 unsigned bitsize = instr->dest.ssa.bit_size;
4752 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4753 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4754 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4755 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4756
4757 unsigned dfmt = attrib_format & 0xf;
4758 unsigned nfmt = (attrib_format >> 4) & 0x7;
4759 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4760
4761 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4762 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4763 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4764 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4765 if (post_shuffle)
4766 num_channels = MAX2(num_channels, 3);
4767
4768 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4769 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4770
4771 Temp index;
4772 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4773 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4774 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4775 if (divisor) {
4776 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4777 if (divisor != 1) {
4778 Temp divided = bld.tmp(v1);
4779 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4780 index = bld.vadd32(bld.def(v1), start_instance, divided);
4781 } else {
4782 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4783 }
4784 } else {
4785 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4786 }
4787 } else {
4788 index = bld.vadd32(bld.def(v1),
4789 get_arg(ctx, ctx->args->ac.base_vertex),
4790 get_arg(ctx, ctx->args->ac.vertex_id));
4791 }
4792
4793 Temp channels[num_channels];
4794 unsigned channel_start = 0;
4795 bool direct_fetch = false;
4796
4797 /* skip unused channels at the start */
4798 if (vtx_info->chan_byte_size && !post_shuffle) {
4799 channel_start = ffs(mask) - 1;
4800 for (unsigned i = 0; i < channel_start; i++)
4801 channels[i] = Temp(0, s1);
4802 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4803 num_channels = 3 - (ffs(mask) - 1);
4804 }
4805
4806 /* load channels */
4807 while (channel_start < num_channels) {
4808 unsigned fetch_component = num_channels - channel_start;
4809 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4810 bool expanded = false;
4811
4812 /* use MUBUF when possible to avoid possible alignment issues */
4813 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4814 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4815 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4816 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4817 vtx_info->chan_byte_size == 4;
4818 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4819 if (!use_mubuf) {
4820 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4821 } else {
4822 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4823 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4824 fetch_component = 4;
4825 expanded = true;
4826 }
4827 }
4828
4829 unsigned fetch_bytes = fetch_component * bitsize / 8;
4830
4831 Temp fetch_index = index;
4832 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4833 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4834 fetch_offset = fetch_offset % attrib_stride;
4835 }
4836
4837 Operand soffset(0u);
4838 if (fetch_offset >= 4096) {
4839 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4840 fetch_offset %= 4096;
4841 }
4842
4843 aco_opcode opcode;
4844 switch (fetch_bytes) {
4845 case 2:
4846 assert(!use_mubuf && bitsize == 16);
4847 opcode = aco_opcode::tbuffer_load_format_d16_x;
4848 break;
4849 case 4:
4850 if (bitsize == 16) {
4851 assert(!use_mubuf);
4852 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4853 } else {
4854 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4855 }
4856 break;
4857 case 6:
4858 assert(!use_mubuf && bitsize == 16);
4859 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4860 break;
4861 case 8:
4862 if (bitsize == 16) {
4863 assert(!use_mubuf);
4864 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4865 } else {
4866 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4867 }
4868 break;
4869 case 12:
4870 assert(ctx->options->chip_class >= GFX7 ||
4871 (!use_mubuf && ctx->options->chip_class == GFX6));
4872 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4873 break;
4874 case 16:
4875 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4876 break;
4877 default:
4878 unreachable("Unimplemented load_input vector size");
4879 }
4880
4881 Temp fetch_dst;
4882 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4883 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4884 num_channels <= 3)) {
4885 direct_fetch = true;
4886 fetch_dst = dst;
4887 } else {
4888 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4889 }
4890
4891 if (use_mubuf) {
4892 bld.mubuf(opcode,
4893 Definition(fetch_dst), list, fetch_index, soffset,
4894 fetch_offset, false, false, true).instr;
4895 } else {
4896 bld.mtbuf(opcode,
4897 Definition(fetch_dst), list, fetch_index, soffset,
4898 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4899 }
4900
4901 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4902
4903 if (fetch_component == 1) {
4904 channels[channel_start] = fetch_dst;
4905 } else {
4906 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4907 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4908 bitsize == 16 ? v2b : v1);
4909 }
4910
4911 channel_start += fetch_component;
4912 }
4913
4914 if (!direct_fetch) {
4915 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4916 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4917
4918 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4919 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4920 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4921
4922 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4923 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4924 unsigned num_temp = 0;
4925 for (unsigned i = 0; i < dst.size(); i++) {
4926 unsigned idx = i + component;
4927 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4928 Temp channel = channels[swizzle[idx]];
4929 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4930 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4931 vec->operands[i] = Operand(channel);
4932
4933 num_temp++;
4934 elems[i] = channel;
4935 } else if (is_float && idx == 3) {
4936 vec->operands[i] = Operand(0x3f800000u);
4937 } else if (!is_float && idx == 3) {
4938 vec->operands[i] = Operand(1u);
4939 } else {
4940 vec->operands[i] = Operand(0u);
4941 }
4942 }
4943 vec->definitions[0] = Definition(dst);
4944 ctx->block->instructions.emplace_back(std::move(vec));
4945 emit_split_vector(ctx, dst, dst.size());
4946
4947 if (num_temp == dst.size())
4948 ctx->allocated_vec.emplace(dst.id(), elems);
4949 }
4950 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4951 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4952 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4953 if (off_instr->type != nir_instr_type_load_const ||
4954 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4955 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4956 nir_print_instr(off_instr, stderr);
4957 fprintf(stderr, "\n");
4958 }
4959
4960 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4961 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4962 if (offset) {
4963 assert(offset->u32 == 0);
4964 } else {
4965 /* the lower 15bit of the prim_mask contain the offset into LDS
4966 * while the upper bits contain the number of prims */
4967 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4968 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4969 Builder bld(ctx->program, ctx->block);
4970 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4971 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4972 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4973 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4974 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4975 }
4976
4977 unsigned idx = nir_intrinsic_base(instr);
4978 unsigned component = nir_intrinsic_component(instr);
4979 unsigned vertex_id = 2; /* P0 */
4980
4981 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4982 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4983 switch (src0->u32) {
4984 case 0:
4985 vertex_id = 2; /* P0 */
4986 break;
4987 case 1:
4988 vertex_id = 0; /* P10 */
4989 break;
4990 case 2:
4991 vertex_id = 1; /* P20 */
4992 break;
4993 default:
4994 unreachable("invalid vertex index");
4995 }
4996 }
4997
4998 if (dst.size() == 1) {
4999 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
5000 } else {
5001 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
5002 for (unsigned i = 0; i < dst.size(); i++)
5003 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
5004 vec->definitions[0] = Definition(dst);
5005 bld.insert(std::move(vec));
5006 }
5007
5008 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
5009 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5010 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
5011 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
5012 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
5013
5014 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
5015 } else {
5016 unreachable("Shader stage not implemented");
5017 }
5018 }
5019
5020 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
5021 {
5022 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
5023
5024 Builder bld(ctx->program, ctx->block);
5025 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
5026 Temp vertex_offset;
5027
5028 if (!nir_src_is_const(*vertex_src)) {
5029 /* better code could be created, but this case probably doesn't happen
5030 * much in practice */
5031 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
5032 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
5033 Temp elem;
5034
5035 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
5036 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
5037 if (i % 2u)
5038 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
5039 } else {
5040 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
5041 }
5042
5043 if (vertex_offset.id()) {
5044 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
5045 Operand(i), indirect_vertex);
5046 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
5047 } else {
5048 vertex_offset = elem;
5049 }
5050 }
5051
5052 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5053 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
5054 } else {
5055 unsigned vertex = nir_src_as_uint(*vertex_src);
5056 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5057 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5058 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
5059 Operand((vertex % 2u) * 16u), Operand(16u));
5060 else
5061 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
5062 }
5063
5064 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
5065 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
5066 return offset_mul(ctx, offs, 4u);
5067 }
5068
5069 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5070 {
5071 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
5072
5073 Builder bld(ctx->program, ctx->block);
5074 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5075 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5076
5077 if (ctx->stage == geometry_gs) {
5078 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
5079 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
5080 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);
5081 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
5082 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
5083 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5084 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5085 } else {
5086 unreachable("Unsupported GS stage.");
5087 }
5088 }
5089
5090 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5091 {
5092 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5093
5094 Builder bld(ctx->program, ctx->block);
5095 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5096
5097 if (load_input_from_temps(ctx, instr, dst))
5098 return;
5099
5100 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
5101 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5102 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5103
5104 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5105 }
5106
5107 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5108 {
5109 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5110
5111 Builder bld(ctx->program, ctx->block);
5112
5113 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5114 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
5115 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5116
5117 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5118 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5119
5120 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5121 }
5122
5123 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5124 {
5125 switch (ctx->shader->info.stage) {
5126 case MESA_SHADER_GEOMETRY:
5127 visit_load_gs_per_vertex_input(ctx, instr);
5128 break;
5129 case MESA_SHADER_TESS_CTRL:
5130 visit_load_tcs_per_vertex_input(ctx, instr);
5131 break;
5132 case MESA_SHADER_TESS_EVAL:
5133 visit_load_tes_per_vertex_input(ctx, instr);
5134 break;
5135 default:
5136 unreachable("Unimplemented shader stage");
5137 }
5138 }
5139
5140 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5141 {
5142 visit_load_tcs_output(ctx, instr, true);
5143 }
5144
5145 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5146 {
5147 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5148 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5149
5150 visit_store_tcs_output(ctx, instr, true);
5151 }
5152
5153 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5154 {
5155 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5156
5157 Builder bld(ctx->program, ctx->block);
5158 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5159
5160 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5161 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5162 Operand tes_w(0u);
5163
5164 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5165 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5166 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5167 tes_w = Operand(tmp);
5168 }
5169
5170 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5171 emit_split_vector(ctx, tess_coord, 3);
5172 }
5173
5174 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5175 {
5176 if (ctx->program->info->need_indirect_descriptor_sets) {
5177 Builder bld(ctx->program, ctx->block);
5178 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5179 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5180 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5181 }
5182
5183 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5184 }
5185
5186
5187 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5188 {
5189 Builder bld(ctx->program, ctx->block);
5190 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5191 if (!nir_dest_is_divergent(instr->dest))
5192 index = bld.as_uniform(index);
5193 unsigned desc_set = nir_intrinsic_desc_set(instr);
5194 unsigned binding = nir_intrinsic_binding(instr);
5195
5196 Temp desc_ptr;
5197 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5198 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5199 unsigned offset = layout->binding[binding].offset;
5200 unsigned stride;
5201 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5202 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5203 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5204 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5205 offset = pipeline_layout->push_constant_size + 16 * idx;
5206 stride = 16;
5207 } else {
5208 desc_ptr = load_desc_ptr(ctx, desc_set);
5209 stride = layout->binding[binding].size;
5210 }
5211
5212 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5213 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5214 if (stride != 1) {
5215 if (nir_const_index) {
5216 const_index = const_index * stride;
5217 } else if (index.type() == RegType::vgpr) {
5218 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5219 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5220 } else {
5221 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5222 }
5223 }
5224 if (offset) {
5225 if (nir_const_index) {
5226 const_index = const_index + offset;
5227 } else if (index.type() == RegType::vgpr) {
5228 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5229 } else {
5230 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5231 }
5232 }
5233
5234 if (nir_const_index && const_index == 0) {
5235 index = desc_ptr;
5236 } else if (index.type() == RegType::vgpr) {
5237 index = bld.vadd32(bld.def(v1),
5238 nir_const_index ? Operand(const_index) : Operand(index),
5239 Operand(desc_ptr));
5240 } else {
5241 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5242 nir_const_index ? Operand(const_index) : Operand(index),
5243 Operand(desc_ptr));
5244 }
5245
5246 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5247 }
5248
5249 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5250 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5251 bool glc=false, bool allow_smem=true, memory_sync_info sync=memory_sync_info())
5252 {
5253 Builder bld(ctx->program, ctx->block);
5254
5255 bool use_smem = dst.type() != RegType::vgpr && (!glc || ctx->options->chip_class >= GFX8) && allow_smem;
5256 if (use_smem)
5257 offset = bld.as_uniform(offset);
5258
5259 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5260 info.glc = glc;
5261 info.sync = sync;
5262 info.align_mul = align_mul;
5263 info.align_offset = align_offset;
5264 if (use_smem)
5265 emit_smem_load(ctx, bld, &info);
5266 else
5267 emit_mubuf_load(ctx, bld, &info);
5268 }
5269
5270 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5271 {
5272 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5273 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5274
5275 Builder bld(ctx->program, ctx->block);
5276
5277 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5278 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5279 unsigned binding = nir_intrinsic_binding(idx_instr);
5280 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5281
5282 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5283 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5284 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5285 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5286 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5287 if (ctx->options->chip_class >= GFX10) {
5288 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5289 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5290 S_008F0C_RESOURCE_LEVEL(1);
5291 } else {
5292 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5293 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5294 }
5295 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5296 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5297 Operand(0xFFFFFFFFu),
5298 Operand(desc_type));
5299 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5300 rsrc, upper_dwords);
5301 } else {
5302 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5303 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5304 }
5305 unsigned size = instr->dest.ssa.bit_size / 8;
5306 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5307 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5308 }
5309
5310 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5311 {
5312 Builder bld(ctx->program, ctx->block);
5313 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5314 unsigned offset = nir_intrinsic_base(instr);
5315 unsigned count = instr->dest.ssa.num_components;
5316 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5317
5318 if (index_cv && instr->dest.ssa.bit_size == 32) {
5319 unsigned start = (offset + index_cv->u32) / 4u;
5320 start -= ctx->args->ac.base_inline_push_consts;
5321 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5322 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5323 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5324 for (unsigned i = 0; i < count; ++i) {
5325 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5326 vec->operands[i] = Operand{elems[i]};
5327 }
5328 vec->definitions[0] = Definition(dst);
5329 ctx->block->instructions.emplace_back(std::move(vec));
5330 ctx->allocated_vec.emplace(dst.id(), elems);
5331 return;
5332 }
5333 }
5334
5335 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5336 if (offset != 0) // TODO check if index != 0 as well
5337 index = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5338 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5339 Temp vec = dst;
5340 bool trim = false;
5341 bool aligned = true;
5342
5343 if (instr->dest.ssa.bit_size == 8) {
5344 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5345 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5346 if (!aligned)
5347 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5348 } else if (instr->dest.ssa.bit_size == 16) {
5349 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5350 if (!aligned)
5351 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5352 }
5353
5354 aco_opcode op;
5355
5356 switch (vec.size()) {
5357 case 1:
5358 op = aco_opcode::s_load_dword;
5359 break;
5360 case 2:
5361 op = aco_opcode::s_load_dwordx2;
5362 break;
5363 case 3:
5364 vec = bld.tmp(s4);
5365 trim = true;
5366 case 4:
5367 op = aco_opcode::s_load_dwordx4;
5368 break;
5369 case 6:
5370 vec = bld.tmp(s8);
5371 trim = true;
5372 case 8:
5373 op = aco_opcode::s_load_dwordx8;
5374 break;
5375 default:
5376 unreachable("unimplemented or forbidden load_push_constant.");
5377 }
5378
5379 static_cast<SMEM_instruction*>(bld.smem(op, Definition(vec), ptr, index).instr)->prevent_overflow = true;
5380
5381 if (!aligned) {
5382 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5383 byte_align_scalar(ctx, vec, byte_offset, dst);
5384 return;
5385 }
5386
5387 if (trim) {
5388 emit_split_vector(ctx, vec, 4);
5389 RegClass rc = dst.size() == 3 ? s1 : s2;
5390 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5391 emit_extract_vector(ctx, vec, 0, rc),
5392 emit_extract_vector(ctx, vec, 1, rc),
5393 emit_extract_vector(ctx, vec, 2, rc));
5394
5395 }
5396 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5397 }
5398
5399 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5400 {
5401 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5402
5403 Builder bld(ctx->program, ctx->block);
5404
5405 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5406 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5407 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5408 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5409 if (ctx->options->chip_class >= GFX10) {
5410 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5411 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5412 S_008F0C_RESOURCE_LEVEL(1);
5413 } else {
5414 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5415 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5416 }
5417
5418 unsigned base = nir_intrinsic_base(instr);
5419 unsigned range = nir_intrinsic_range(instr);
5420
5421 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5422 if (base && offset.type() == RegType::sgpr)
5423 offset = bld.nuw().sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5424 else if (base && offset.type() == RegType::vgpr)
5425 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5426
5427 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5428 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5429 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5430 Operand(desc_type));
5431 unsigned size = instr->dest.ssa.bit_size / 8;
5432 // TODO: get alignment information for subdword constants
5433 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5434 }
5435
5436 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5437 {
5438 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5439 ctx->cf_info.exec_potentially_empty_discard = true;
5440
5441 ctx->program->needs_exact = true;
5442
5443 // TODO: optimize uniform conditions
5444 Builder bld(ctx->program, ctx->block);
5445 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5446 assert(src.regClass() == bld.lm);
5447 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5448 bld.pseudo(aco_opcode::p_discard_if, src);
5449 ctx->block->kind |= block_kind_uses_discard_if;
5450 return;
5451 }
5452
5453 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5454 {
5455 Builder bld(ctx->program, ctx->block);
5456
5457 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5458 ctx->cf_info.exec_potentially_empty_discard = true;
5459
5460 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5461 ctx->cf_info.parent_loop.has_divergent_continue;
5462
5463 if (ctx->block->loop_nest_depth &&
5464 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5465 /* we handle discards the same way as jump instructions */
5466 append_logical_end(ctx->block);
5467
5468 /* in loops, discard behaves like break */
5469 Block *linear_target = ctx->cf_info.parent_loop.exit;
5470 ctx->block->kind |= block_kind_discard;
5471
5472 if (!divergent) {
5473 /* uniform discard - loop ends here */
5474 assert(nir_instr_is_last(&instr->instr));
5475 ctx->block->kind |= block_kind_uniform;
5476 ctx->cf_info.has_branch = true;
5477 bld.branch(aco_opcode::p_branch);
5478 add_linear_edge(ctx->block->index, linear_target);
5479 return;
5480 }
5481
5482 /* we add a break right behind the discard() instructions */
5483 ctx->block->kind |= block_kind_break;
5484 unsigned idx = ctx->block->index;
5485
5486 ctx->cf_info.parent_loop.has_divergent_branch = true;
5487 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5488
5489 /* remove critical edges from linear CFG */
5490 bld.branch(aco_opcode::p_branch);
5491 Block* break_block = ctx->program->create_and_insert_block();
5492 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5493 break_block->kind |= block_kind_uniform;
5494 add_linear_edge(idx, break_block);
5495 add_linear_edge(break_block->index, linear_target);
5496 bld.reset(break_block);
5497 bld.branch(aco_opcode::p_branch);
5498
5499 Block* continue_block = ctx->program->create_and_insert_block();
5500 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5501 add_linear_edge(idx, continue_block);
5502 append_logical_start(continue_block);
5503 ctx->block = continue_block;
5504
5505 return;
5506 }
5507
5508 /* it can currently happen that NIR doesn't remove the unreachable code */
5509 if (!nir_instr_is_last(&instr->instr)) {
5510 ctx->program->needs_exact = true;
5511 /* save exec somewhere temporarily so that it doesn't get
5512 * overwritten before the discard from outer exec masks */
5513 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5514 bld.pseudo(aco_opcode::p_discard_if, cond);
5515 ctx->block->kind |= block_kind_uses_discard_if;
5516 return;
5517 }
5518
5519 /* This condition is incorrect for uniformly branched discards in a loop
5520 * predicated by a divergent condition, but the above code catches that case
5521 * and the discard would end up turning into a discard_if.
5522 * For example:
5523 * if (divergent) {
5524 * while (...) {
5525 * if (uniform) {
5526 * discard;
5527 * }
5528 * }
5529 * }
5530 */
5531 if (!ctx->cf_info.parent_if.is_divergent) {
5532 /* program just ends here */
5533 ctx->block->kind |= block_kind_uniform;
5534 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5535 0 /* enabled mask */, 9 /* dest */,
5536 false /* compressed */, true/* done */, true /* valid mask */);
5537 bld.sopp(aco_opcode::s_endpgm);
5538 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5539 } else {
5540 ctx->block->kind |= block_kind_discard;
5541 /* branch and linear edge is added by visit_if() */
5542 }
5543 }
5544
5545 enum aco_descriptor_type {
5546 ACO_DESC_IMAGE,
5547 ACO_DESC_FMASK,
5548 ACO_DESC_SAMPLER,
5549 ACO_DESC_BUFFER,
5550 ACO_DESC_PLANE_0,
5551 ACO_DESC_PLANE_1,
5552 ACO_DESC_PLANE_2,
5553 };
5554
5555 static bool
5556 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5557 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5558 return false;
5559 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5560 return dim == ac_image_cube ||
5561 dim == ac_image_1darray ||
5562 dim == ac_image_2darray ||
5563 dim == ac_image_2darraymsaa;
5564 }
5565
5566 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5567 enum aco_descriptor_type desc_type,
5568 const nir_tex_instr *tex_instr, bool image, bool write)
5569 {
5570 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5571 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5572 if (it != ctx->tex_desc.end())
5573 return it->second;
5574 */
5575 Temp index = Temp();
5576 bool index_set = false;
5577 unsigned constant_index = 0;
5578 unsigned descriptor_set;
5579 unsigned base_index;
5580 Builder bld(ctx->program, ctx->block);
5581
5582 if (!deref_instr) {
5583 assert(tex_instr && !image);
5584 descriptor_set = 0;
5585 base_index = tex_instr->sampler_index;
5586 } else {
5587 while(deref_instr->deref_type != nir_deref_type_var) {
5588 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5589 if (!array_size)
5590 array_size = 1;
5591
5592 assert(deref_instr->deref_type == nir_deref_type_array);
5593 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5594 if (const_value) {
5595 constant_index += array_size * const_value->u32;
5596 } else {
5597 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5598 if (indirect.type() == RegType::vgpr)
5599 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5600
5601 if (array_size != 1)
5602 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5603
5604 if (!index_set) {
5605 index = indirect;
5606 index_set = true;
5607 } else {
5608 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5609 }
5610 }
5611
5612 deref_instr = nir_src_as_deref(deref_instr->parent);
5613 }
5614 descriptor_set = deref_instr->var->data.descriptor_set;
5615 base_index = deref_instr->var->data.binding;
5616 }
5617
5618 Temp list = load_desc_ptr(ctx, descriptor_set);
5619 list = convert_pointer_to_64_bit(ctx, list);
5620
5621 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5622 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5623 unsigned offset = binding->offset;
5624 unsigned stride = binding->size;
5625 aco_opcode opcode;
5626 RegClass type;
5627
5628 assert(base_index < layout->binding_count);
5629
5630 switch (desc_type) {
5631 case ACO_DESC_IMAGE:
5632 type = s8;
5633 opcode = aco_opcode::s_load_dwordx8;
5634 break;
5635 case ACO_DESC_FMASK:
5636 type = s8;
5637 opcode = aco_opcode::s_load_dwordx8;
5638 offset += 32;
5639 break;
5640 case ACO_DESC_SAMPLER:
5641 type = s4;
5642 opcode = aco_opcode::s_load_dwordx4;
5643 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5644 offset += radv_combined_image_descriptor_sampler_offset(binding);
5645 break;
5646 case ACO_DESC_BUFFER:
5647 type = s4;
5648 opcode = aco_opcode::s_load_dwordx4;
5649 break;
5650 case ACO_DESC_PLANE_0:
5651 case ACO_DESC_PLANE_1:
5652 type = s8;
5653 opcode = aco_opcode::s_load_dwordx8;
5654 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5655 break;
5656 case ACO_DESC_PLANE_2:
5657 type = s4;
5658 opcode = aco_opcode::s_load_dwordx4;
5659 offset += 64;
5660 break;
5661 default:
5662 unreachable("invalid desc_type\n");
5663 }
5664
5665 offset += constant_index * stride;
5666
5667 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5668 (!index_set || binding->immutable_samplers_equal)) {
5669 if (binding->immutable_samplers_equal)
5670 constant_index = 0;
5671
5672 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5673 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5674 Operand(samplers[constant_index * 4 + 0]),
5675 Operand(samplers[constant_index * 4 + 1]),
5676 Operand(samplers[constant_index * 4 + 2]),
5677 Operand(samplers[constant_index * 4 + 3]));
5678 }
5679
5680 Operand off;
5681 if (!index_set) {
5682 off = bld.copy(bld.def(s1), Operand(offset));
5683 } else {
5684 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5685 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5686 }
5687
5688 Temp res = bld.smem(opcode, bld.def(type), list, off);
5689
5690 if (desc_type == ACO_DESC_PLANE_2) {
5691 Temp components[8];
5692 for (unsigned i = 0; i < 8; i++)
5693 components[i] = bld.tmp(s1);
5694 bld.pseudo(aco_opcode::p_split_vector,
5695 Definition(components[0]),
5696 Definition(components[1]),
5697 Definition(components[2]),
5698 Definition(components[3]),
5699 res);
5700
5701 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5702 bld.pseudo(aco_opcode::p_split_vector,
5703 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5704 Definition(components[4]),
5705 Definition(components[5]),
5706 Definition(components[6]),
5707 Definition(components[7]),
5708 desc2);
5709
5710 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5711 components[0], components[1], components[2], components[3],
5712 components[4], components[5], components[6], components[7]);
5713 }
5714
5715 return res;
5716 }
5717
5718 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5719 {
5720 switch (dim) {
5721 case GLSL_SAMPLER_DIM_BUF:
5722 return 1;
5723 case GLSL_SAMPLER_DIM_1D:
5724 return array ? 2 : 1;
5725 case GLSL_SAMPLER_DIM_2D:
5726 return array ? 3 : 2;
5727 case GLSL_SAMPLER_DIM_MS:
5728 return array ? 4 : 3;
5729 case GLSL_SAMPLER_DIM_3D:
5730 case GLSL_SAMPLER_DIM_CUBE:
5731 return 3;
5732 case GLSL_SAMPLER_DIM_RECT:
5733 case GLSL_SAMPLER_DIM_SUBPASS:
5734 return 2;
5735 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5736 return 3;
5737 default:
5738 break;
5739 }
5740 return 0;
5741 }
5742
5743
5744 /* Adjust the sample index according to FMASK.
5745 *
5746 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5747 * which is the identity mapping. Each nibble says which physical sample
5748 * should be fetched to get that sample.
5749 *
5750 * For example, 0x11111100 means there are only 2 samples stored and
5751 * the second sample covers 3/4 of the pixel. When reading samples 0
5752 * and 1, return physical sample 0 (determined by the first two 0s
5753 * in FMASK), otherwise return physical sample 1.
5754 *
5755 * The sample index should be adjusted as follows:
5756 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5757 */
5758 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5759 {
5760 Builder bld(ctx->program, ctx->block);
5761 Temp fmask = bld.tmp(v1);
5762 unsigned dim = ctx->options->chip_class >= GFX10
5763 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5764 : 0;
5765
5766 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5767 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5768 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5769 load->operands[0] = Operand(fmask_desc_ptr);
5770 load->operands[1] = Operand(s4); /* no sampler */
5771 load->operands[2] = Operand(coord);
5772 load->definitions[0] = Definition(fmask);
5773 load->glc = false;
5774 load->dlc = false;
5775 load->dmask = 0x1;
5776 load->unrm = true;
5777 load->da = da;
5778 load->dim = dim;
5779 ctx->block->instructions.emplace_back(std::move(load));
5780
5781 Operand sample_index4;
5782 if (sample_index.isConstant()) {
5783 if (sample_index.constantValue() < 16) {
5784 sample_index4 = Operand(sample_index.constantValue() << 2);
5785 } else {
5786 sample_index4 = Operand(0u);
5787 }
5788 } else if (sample_index.regClass() == s1) {
5789 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5790 } else {
5791 assert(sample_index.regClass() == v1);
5792 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5793 }
5794
5795 Temp final_sample;
5796 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5797 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5798 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5799 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5800 else
5801 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5802
5803 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5804 * resource descriptor is 0 (invalid),
5805 */
5806 Temp compare = bld.tmp(bld.lm);
5807 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5808 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5809
5810 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5811
5812 /* Replace the MSAA sample index. */
5813 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5814 }
5815
5816 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5817 {
5818
5819 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5820 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5821 bool is_array = glsl_sampler_type_is_array(type);
5822 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5823 assert(!add_frag_pos && "Input attachments should be lowered.");
5824 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5825 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5826 int count = image_type_to_components_count(dim, is_array);
5827 std::vector<Temp> coords(count);
5828 Builder bld(ctx->program, ctx->block);
5829
5830 if (is_ms) {
5831 count--;
5832 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5833 /* get sample index */
5834 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5835 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5836 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5837 std::vector<Temp> fmask_load_address;
5838 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5839 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5840
5841 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5842 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5843 } else {
5844 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5845 }
5846 }
5847
5848 if (gfx9_1d) {
5849 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5850 coords.resize(coords.size() + 1);
5851 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5852 if (is_array)
5853 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5854 } else {
5855 for (int i = 0; i < count; i++)
5856 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5857 }
5858
5859 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5860 instr->intrinsic == nir_intrinsic_image_deref_store) {
5861 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5862 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5863
5864 if (!level_zero)
5865 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5866 }
5867
5868 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5869 for (unsigned i = 0; i < coords.size(); i++)
5870 vec->operands[i] = Operand(coords[i]);
5871 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5872 vec->definitions[0] = Definition(res);
5873 ctx->block->instructions.emplace_back(std::move(vec));
5874 return res;
5875 }
5876
5877
5878 memory_sync_info get_memory_sync_info(nir_intrinsic_instr *instr, storage_class storage, unsigned semantics)
5879 {
5880 /* atomicrmw might not have NIR_INTRINSIC_ACCESS and there's nothing interesting there anyway */
5881 if (semantics & semantic_atomicrmw)
5882 return memory_sync_info(storage, semantics);
5883
5884 unsigned access = nir_intrinsic_access(instr);
5885
5886 if (access & ACCESS_VOLATILE)
5887 semantics |= semantic_volatile;
5888 if (access & ACCESS_CAN_REORDER)
5889 semantics |= semantic_can_reorder | semantic_private;
5890
5891 return memory_sync_info(storage, semantics);
5892 }
5893
5894 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5895 {
5896 Builder bld(ctx->program, ctx->block);
5897 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5898 const struct glsl_type *type = glsl_without_array(var->type);
5899 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5900 bool is_array = glsl_sampler_type_is_array(type);
5901 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5902
5903 memory_sync_info sync = get_memory_sync_info(instr, storage_image, 0);
5904 unsigned access = var->data.access | nir_intrinsic_access(instr);
5905
5906 if (dim == GLSL_SAMPLER_DIM_BUF) {
5907 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5908 unsigned num_channels = util_last_bit(mask);
5909 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5910 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5911
5912 aco_opcode opcode;
5913 switch (num_channels) {
5914 case 1:
5915 opcode = aco_opcode::buffer_load_format_x;
5916 break;
5917 case 2:
5918 opcode = aco_opcode::buffer_load_format_xy;
5919 break;
5920 case 3:
5921 opcode = aco_opcode::buffer_load_format_xyz;
5922 break;
5923 case 4:
5924 opcode = aco_opcode::buffer_load_format_xyzw;
5925 break;
5926 default:
5927 unreachable(">4 channel buffer image load");
5928 }
5929 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5930 load->operands[0] = Operand(rsrc);
5931 load->operands[1] = Operand(vindex);
5932 load->operands[2] = Operand((uint32_t) 0);
5933 Temp tmp;
5934 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5935 tmp = dst;
5936 else
5937 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5938 load->definitions[0] = Definition(tmp);
5939 load->idxen = true;
5940 load->glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5941 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5942 load->sync = sync;
5943 ctx->block->instructions.emplace_back(std::move(load));
5944
5945 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5946 return;
5947 }
5948
5949 Temp coords = get_image_coords(ctx, instr, type);
5950 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5951
5952 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5953 unsigned num_components = util_bitcount(dmask);
5954 Temp tmp;
5955 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5956 tmp = dst;
5957 else
5958 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5959
5960 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5961 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5962
5963 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5964 load->operands[0] = Operand(resource);
5965 load->operands[1] = Operand(s4); /* no sampler */
5966 load->operands[2] = Operand(coords);
5967 load->definitions[0] = Definition(tmp);
5968 load->glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5969 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5970 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5971 load->dmask = dmask;
5972 load->unrm = true;
5973 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5974 load->sync = sync;
5975 ctx->block->instructions.emplace_back(std::move(load));
5976
5977 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5978 return;
5979 }
5980
5981 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5982 {
5983 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5984 const struct glsl_type *type = glsl_without_array(var->type);
5985 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5986 bool is_array = glsl_sampler_type_is_array(type);
5987 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5988
5989 memory_sync_info sync = get_memory_sync_info(instr, storage_image, 0);
5990 unsigned access = var->data.access | nir_intrinsic_access(instr);
5991 bool glc = ctx->options->chip_class == GFX6 || access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5992
5993 if (dim == GLSL_SAMPLER_DIM_BUF) {
5994 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5995 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5996 aco_opcode opcode;
5997 switch (data.size()) {
5998 case 1:
5999 opcode = aco_opcode::buffer_store_format_x;
6000 break;
6001 case 2:
6002 opcode = aco_opcode::buffer_store_format_xy;
6003 break;
6004 case 3:
6005 opcode = aco_opcode::buffer_store_format_xyz;
6006 break;
6007 case 4:
6008 opcode = aco_opcode::buffer_store_format_xyzw;
6009 break;
6010 default:
6011 unreachable(">4 channel buffer image store");
6012 }
6013 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
6014 store->operands[0] = Operand(rsrc);
6015 store->operands[1] = Operand(vindex);
6016 store->operands[2] = Operand((uint32_t) 0);
6017 store->operands[3] = Operand(data);
6018 store->idxen = true;
6019 store->glc = glc;
6020 store->dlc = false;
6021 store->disable_wqm = true;
6022 store->sync = sync;
6023 ctx->program->needs_exact = true;
6024 ctx->block->instructions.emplace_back(std::move(store));
6025 return;
6026 }
6027
6028 assert(data.type() == RegType::vgpr);
6029 Temp coords = get_image_coords(ctx, instr, type);
6030 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6031
6032 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
6033 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
6034
6035 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
6036 store->operands[0] = Operand(resource);
6037 store->operands[1] = Operand(data);
6038 store->operands[2] = Operand(coords);
6039 store->glc = glc;
6040 store->dlc = false;
6041 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6042 store->dmask = (1 << data.size()) - 1;
6043 store->unrm = true;
6044 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6045 store->disable_wqm = true;
6046 store->sync = sync;
6047 ctx->program->needs_exact = true;
6048 ctx->block->instructions.emplace_back(std::move(store));
6049 return;
6050 }
6051
6052 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6053 {
6054 /* return the previous value if dest is ever used */
6055 bool return_previous = false;
6056 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6057 return_previous = true;
6058 break;
6059 }
6060 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6061 return_previous = true;
6062 break;
6063 }
6064
6065 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6066 const struct glsl_type *type = glsl_without_array(var->type);
6067 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6068 bool is_array = glsl_sampler_type_is_array(type);
6069 Builder bld(ctx->program, ctx->block);
6070
6071 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
6072 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
6073
6074 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
6075 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
6076
6077 aco_opcode buf_op, image_op;
6078 switch (instr->intrinsic) {
6079 case nir_intrinsic_image_deref_atomic_add:
6080 buf_op = aco_opcode::buffer_atomic_add;
6081 image_op = aco_opcode::image_atomic_add;
6082 break;
6083 case nir_intrinsic_image_deref_atomic_umin:
6084 buf_op = aco_opcode::buffer_atomic_umin;
6085 image_op = aco_opcode::image_atomic_umin;
6086 break;
6087 case nir_intrinsic_image_deref_atomic_imin:
6088 buf_op = aco_opcode::buffer_atomic_smin;
6089 image_op = aco_opcode::image_atomic_smin;
6090 break;
6091 case nir_intrinsic_image_deref_atomic_umax:
6092 buf_op = aco_opcode::buffer_atomic_umax;
6093 image_op = aco_opcode::image_atomic_umax;
6094 break;
6095 case nir_intrinsic_image_deref_atomic_imax:
6096 buf_op = aco_opcode::buffer_atomic_smax;
6097 image_op = aco_opcode::image_atomic_smax;
6098 break;
6099 case nir_intrinsic_image_deref_atomic_and:
6100 buf_op = aco_opcode::buffer_atomic_and;
6101 image_op = aco_opcode::image_atomic_and;
6102 break;
6103 case nir_intrinsic_image_deref_atomic_or:
6104 buf_op = aco_opcode::buffer_atomic_or;
6105 image_op = aco_opcode::image_atomic_or;
6106 break;
6107 case nir_intrinsic_image_deref_atomic_xor:
6108 buf_op = aco_opcode::buffer_atomic_xor;
6109 image_op = aco_opcode::image_atomic_xor;
6110 break;
6111 case nir_intrinsic_image_deref_atomic_exchange:
6112 buf_op = aco_opcode::buffer_atomic_swap;
6113 image_op = aco_opcode::image_atomic_swap;
6114 break;
6115 case nir_intrinsic_image_deref_atomic_comp_swap:
6116 buf_op = aco_opcode::buffer_atomic_cmpswap;
6117 image_op = aco_opcode::image_atomic_cmpswap;
6118 break;
6119 default:
6120 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
6121 }
6122
6123 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6124 memory_sync_info sync = get_memory_sync_info(instr, storage_image, semantic_atomicrmw);
6125
6126 if (dim == GLSL_SAMPLER_DIM_BUF) {
6127 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
6128 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
6129 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6130 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6131 mubuf->operands[0] = Operand(resource);
6132 mubuf->operands[1] = Operand(vindex);
6133 mubuf->operands[2] = Operand((uint32_t)0);
6134 mubuf->operands[3] = Operand(data);
6135 if (return_previous)
6136 mubuf->definitions[0] = Definition(dst);
6137 mubuf->offset = 0;
6138 mubuf->idxen = true;
6139 mubuf->glc = return_previous;
6140 mubuf->dlc = false; /* Not needed for atomics */
6141 mubuf->disable_wqm = true;
6142 mubuf->sync = sync;
6143 ctx->program->needs_exact = true;
6144 ctx->block->instructions.emplace_back(std::move(mubuf));
6145 return;
6146 }
6147
6148 Temp coords = get_image_coords(ctx, instr, type);
6149 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6150 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6151 mimg->operands[0] = Operand(resource);
6152 mimg->operands[1] = Operand(data);
6153 mimg->operands[2] = Operand(coords);
6154 if (return_previous)
6155 mimg->definitions[0] = Definition(dst);
6156 mimg->glc = return_previous;
6157 mimg->dlc = false; /* Not needed for atomics */
6158 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6159 mimg->dmask = (1 << data.size()) - 1;
6160 mimg->unrm = true;
6161 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6162 mimg->disable_wqm = true;
6163 mimg->sync = sync;
6164 ctx->program->needs_exact = true;
6165 ctx->block->instructions.emplace_back(std::move(mimg));
6166 return;
6167 }
6168
6169 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6170 {
6171 if (in_elements && ctx->options->chip_class == GFX8) {
6172 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6173 Builder bld(ctx->program, ctx->block);
6174
6175 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6176
6177 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6178 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6179
6180 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6181 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6182
6183 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6184 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6185
6186 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6187 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6188 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6189 if (dst.type() == RegType::vgpr)
6190 bld.copy(Definition(dst), shr_dst);
6191
6192 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6193 } else {
6194 emit_extract_vector(ctx, desc, 2, dst);
6195 }
6196 }
6197
6198 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6199 {
6200 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6201 const struct glsl_type *type = glsl_without_array(var->type);
6202 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6203 bool is_array = glsl_sampler_type_is_array(type);
6204 Builder bld(ctx->program, ctx->block);
6205
6206 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6207 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6208 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6209 }
6210
6211 /* LOD */
6212 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6213
6214 /* Resource */
6215 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6216
6217 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6218
6219 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6220 mimg->operands[0] = Operand(resource);
6221 mimg->operands[1] = Operand(s4); /* no sampler */
6222 mimg->operands[2] = Operand(lod);
6223 uint8_t& dmask = mimg->dmask;
6224 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6225 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6226 mimg->da = glsl_sampler_type_is_array(type);
6227 Definition& def = mimg->definitions[0];
6228 ctx->block->instructions.emplace_back(std::move(mimg));
6229
6230 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6231 glsl_sampler_type_is_array(type)) {
6232
6233 assert(instr->dest.ssa.num_components == 3);
6234 Temp tmp = {ctx->program->allocateId(), v3};
6235 def = Definition(tmp);
6236 emit_split_vector(ctx, tmp, 3);
6237
6238 /* divide 3rd value by 6 by multiplying with magic number */
6239 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6240 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6241
6242 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6243 emit_extract_vector(ctx, tmp, 0, v1),
6244 emit_extract_vector(ctx, tmp, 1, v1),
6245 by_6);
6246
6247 } else if (ctx->options->chip_class == GFX9 &&
6248 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6249 glsl_sampler_type_is_array(type)) {
6250 assert(instr->dest.ssa.num_components == 2);
6251 def = Definition(dst);
6252 dmask = 0x5;
6253 } else {
6254 def = Definition(dst);
6255 }
6256
6257 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6258 }
6259
6260 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6261 {
6262 Builder bld(ctx->program, ctx->block);
6263 unsigned num_components = instr->num_components;
6264
6265 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6266 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6267 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6268
6269 unsigned access = nir_intrinsic_access(instr);
6270 bool glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
6271 unsigned size = instr->dest.ssa.bit_size / 8;
6272
6273 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[0].ssa, access);
6274 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6275 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6276 */
6277 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_store : has_vmem_store));
6278 allow_smem |= ((access & ACCESS_RESTRICT) && (access & ACCESS_NON_WRITEABLE)) || (access & ACCESS_CAN_REORDER);
6279
6280 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6281 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, allow_smem,
6282 get_memory_sync_info(instr, storage_buffer, 0));
6283 }
6284
6285 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6286 {
6287 Builder bld(ctx->program, ctx->block);
6288 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6289 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6290 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6291 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6292
6293 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6294 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6295
6296 memory_sync_info sync = get_memory_sync_info(instr, storage_buffer, 0);
6297 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6298 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[1].ssa, nir_intrinsic_access(instr));
6299 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6300 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6301 */
6302 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_loadstore : has_vmem_loadstore));
6303
6304 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6305 ctx->options->chip_class >= GFX8 &&
6306 (elem_size_bytes >= 4 || can_subdword_ssbo_store_use_smem(instr)) &&
6307 allow_smem;
6308 if (smem)
6309 offset = bld.as_uniform(offset);
6310 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6311
6312 unsigned write_count = 0;
6313 Temp write_datas[32];
6314 unsigned offsets[32];
6315 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6316 data, writemask, 16, &write_count, write_datas, offsets);
6317
6318 for (unsigned i = 0; i < write_count; i++) {
6319 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6320 if (smem && ctx->stage == fragment_fs)
6321 op = aco_opcode::p_fs_buffer_store_smem;
6322
6323 if (smem) {
6324 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6325 store->operands[0] = Operand(rsrc);
6326 if (offsets[i]) {
6327 Temp off = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6328 offset, Operand(offsets[i]));
6329 store->operands[1] = Operand(off);
6330 } else {
6331 store->operands[1] = Operand(offset);
6332 }
6333 if (op != aco_opcode::p_fs_buffer_store_smem)
6334 store->operands[1].setFixed(m0);
6335 store->operands[2] = Operand(write_datas[i]);
6336 store->glc = glc;
6337 store->dlc = false;
6338 store->disable_wqm = true;
6339 store->sync = sync;
6340 ctx->block->instructions.emplace_back(std::move(store));
6341 ctx->program->wb_smem_l1_on_end = true;
6342 if (op == aco_opcode::p_fs_buffer_store_smem) {
6343 ctx->block->kind |= block_kind_needs_lowering;
6344 ctx->program->needs_exact = true;
6345 }
6346 } else {
6347 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6348 store->operands[0] = Operand(rsrc);
6349 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6350 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6351 store->operands[3] = Operand(write_datas[i]);
6352 store->offset = offsets[i];
6353 store->offen = (offset.type() == RegType::vgpr);
6354 store->glc = glc;
6355 store->dlc = false;
6356 store->disable_wqm = true;
6357 store->sync = sync;
6358 ctx->program->needs_exact = true;
6359 ctx->block->instructions.emplace_back(std::move(store));
6360 }
6361 }
6362 }
6363
6364 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6365 {
6366 /* return the previous value if dest is ever used */
6367 bool return_previous = false;
6368 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6369 return_previous = true;
6370 break;
6371 }
6372 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6373 return_previous = true;
6374 break;
6375 }
6376
6377 Builder bld(ctx->program, ctx->block);
6378 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6379
6380 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6381 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6382 get_ssa_temp(ctx, instr->src[3].ssa), data);
6383
6384 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6385 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6386 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6387
6388 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6389
6390 aco_opcode op32, op64;
6391 switch (instr->intrinsic) {
6392 case nir_intrinsic_ssbo_atomic_add:
6393 op32 = aco_opcode::buffer_atomic_add;
6394 op64 = aco_opcode::buffer_atomic_add_x2;
6395 break;
6396 case nir_intrinsic_ssbo_atomic_imin:
6397 op32 = aco_opcode::buffer_atomic_smin;
6398 op64 = aco_opcode::buffer_atomic_smin_x2;
6399 break;
6400 case nir_intrinsic_ssbo_atomic_umin:
6401 op32 = aco_opcode::buffer_atomic_umin;
6402 op64 = aco_opcode::buffer_atomic_umin_x2;
6403 break;
6404 case nir_intrinsic_ssbo_atomic_imax:
6405 op32 = aco_opcode::buffer_atomic_smax;
6406 op64 = aco_opcode::buffer_atomic_smax_x2;
6407 break;
6408 case nir_intrinsic_ssbo_atomic_umax:
6409 op32 = aco_opcode::buffer_atomic_umax;
6410 op64 = aco_opcode::buffer_atomic_umax_x2;
6411 break;
6412 case nir_intrinsic_ssbo_atomic_and:
6413 op32 = aco_opcode::buffer_atomic_and;
6414 op64 = aco_opcode::buffer_atomic_and_x2;
6415 break;
6416 case nir_intrinsic_ssbo_atomic_or:
6417 op32 = aco_opcode::buffer_atomic_or;
6418 op64 = aco_opcode::buffer_atomic_or_x2;
6419 break;
6420 case nir_intrinsic_ssbo_atomic_xor:
6421 op32 = aco_opcode::buffer_atomic_xor;
6422 op64 = aco_opcode::buffer_atomic_xor_x2;
6423 break;
6424 case nir_intrinsic_ssbo_atomic_exchange:
6425 op32 = aco_opcode::buffer_atomic_swap;
6426 op64 = aco_opcode::buffer_atomic_swap_x2;
6427 break;
6428 case nir_intrinsic_ssbo_atomic_comp_swap:
6429 op32 = aco_opcode::buffer_atomic_cmpswap;
6430 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6431 break;
6432 default:
6433 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6434 }
6435 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6436 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6437 mubuf->operands[0] = Operand(rsrc);
6438 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6439 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6440 mubuf->operands[3] = Operand(data);
6441 if (return_previous)
6442 mubuf->definitions[0] = Definition(dst);
6443 mubuf->offset = 0;
6444 mubuf->offen = (offset.type() == RegType::vgpr);
6445 mubuf->glc = return_previous;
6446 mubuf->dlc = false; /* Not needed for atomics */
6447 mubuf->disable_wqm = true;
6448 mubuf->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6449 ctx->program->needs_exact = true;
6450 ctx->block->instructions.emplace_back(std::move(mubuf));
6451 }
6452
6453 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6454
6455 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6456 Builder bld(ctx->program, ctx->block);
6457 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6458 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6459 }
6460
6461 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6462 {
6463 Builder bld(ctx->program, ctx->block);
6464 unsigned num_components = instr->num_components;
6465 unsigned component_size = instr->dest.ssa.bit_size / 8;
6466
6467 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6468 get_ssa_temp(ctx, &instr->dest.ssa),
6469 num_components, component_size};
6470 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6471 info.align_mul = nir_intrinsic_align_mul(instr);
6472 info.align_offset = nir_intrinsic_align_offset(instr);
6473 info.sync = get_memory_sync_info(instr, storage_buffer, 0);
6474 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6475 * it's safe to use SMEM */
6476 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6477 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6478 emit_global_load(ctx, bld, &info);
6479 } else {
6480 info.offset = Operand(bld.as_uniform(info.offset));
6481 emit_smem_load(ctx, bld, &info);
6482 }
6483 }
6484
6485 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6486 {
6487 Builder bld(ctx->program, ctx->block);
6488 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6489 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6490
6491 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6492 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6493 memory_sync_info sync = get_memory_sync_info(instr, storage_buffer, 0);
6494 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6495
6496 if (ctx->options->chip_class >= GFX7)
6497 addr = as_vgpr(ctx, addr);
6498
6499 unsigned write_count = 0;
6500 Temp write_datas[32];
6501 unsigned offsets[32];
6502 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6503 16, &write_count, write_datas, offsets);
6504
6505 for (unsigned i = 0; i < write_count; i++) {
6506 if (ctx->options->chip_class >= GFX7) {
6507 unsigned offset = offsets[i];
6508 Temp store_addr = addr;
6509 if (offset > 0 && ctx->options->chip_class < GFX9) {
6510 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6511 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6512 Temp carry = bld.tmp(bld.lm);
6513 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6514
6515 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6516 Operand(offset), addr0);
6517 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6518 Operand(0u), addr1,
6519 carry).def(1).setHint(vcc);
6520
6521 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6522
6523 offset = 0;
6524 }
6525
6526 bool global = ctx->options->chip_class >= GFX9;
6527 aco_opcode op;
6528 switch (write_datas[i].bytes()) {
6529 case 1:
6530 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6531 break;
6532 case 2:
6533 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6534 break;
6535 case 4:
6536 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6537 break;
6538 case 8:
6539 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6540 break;
6541 case 12:
6542 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6543 break;
6544 case 16:
6545 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6546 break;
6547 default:
6548 unreachable("store_global not implemented for this size.");
6549 }
6550
6551 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6552 flat->operands[0] = Operand(store_addr);
6553 flat->operands[1] = Operand(s1);
6554 flat->operands[2] = Operand(write_datas[i]);
6555 flat->glc = glc;
6556 flat->dlc = false;
6557 flat->offset = offset;
6558 flat->disable_wqm = true;
6559 flat->sync = sync;
6560 ctx->program->needs_exact = true;
6561 ctx->block->instructions.emplace_back(std::move(flat));
6562 } else {
6563 assert(ctx->options->chip_class == GFX6);
6564
6565 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6566
6567 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6568
6569 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6570 mubuf->operands[0] = Operand(rsrc);
6571 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6572 mubuf->operands[2] = Operand(0u);
6573 mubuf->operands[3] = Operand(write_datas[i]);
6574 mubuf->glc = glc;
6575 mubuf->dlc = false;
6576 mubuf->offset = offsets[i];
6577 mubuf->addr64 = addr.type() == RegType::vgpr;
6578 mubuf->disable_wqm = true;
6579 mubuf->sync = sync;
6580 ctx->program->needs_exact = true;
6581 ctx->block->instructions.emplace_back(std::move(mubuf));
6582 }
6583 }
6584 }
6585
6586 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6587 {
6588 /* return the previous value if dest is ever used */
6589 bool return_previous = false;
6590 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6591 return_previous = true;
6592 break;
6593 }
6594 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6595 return_previous = true;
6596 break;
6597 }
6598
6599 Builder bld(ctx->program, ctx->block);
6600 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6601 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6602
6603 if (ctx->options->chip_class >= GFX7)
6604 addr = as_vgpr(ctx, addr);
6605
6606 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6607 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6608 get_ssa_temp(ctx, instr->src[2].ssa), data);
6609
6610 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6611
6612 aco_opcode op32, op64;
6613
6614 if (ctx->options->chip_class >= GFX7) {
6615 bool global = ctx->options->chip_class >= GFX9;
6616 switch (instr->intrinsic) {
6617 case nir_intrinsic_global_atomic_add:
6618 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6619 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6620 break;
6621 case nir_intrinsic_global_atomic_imin:
6622 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6623 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6624 break;
6625 case nir_intrinsic_global_atomic_umin:
6626 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6627 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6628 break;
6629 case nir_intrinsic_global_atomic_imax:
6630 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6631 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6632 break;
6633 case nir_intrinsic_global_atomic_umax:
6634 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6635 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6636 break;
6637 case nir_intrinsic_global_atomic_and:
6638 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6639 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6640 break;
6641 case nir_intrinsic_global_atomic_or:
6642 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6643 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6644 break;
6645 case nir_intrinsic_global_atomic_xor:
6646 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6647 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6648 break;
6649 case nir_intrinsic_global_atomic_exchange:
6650 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6651 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6652 break;
6653 case nir_intrinsic_global_atomic_comp_swap:
6654 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6655 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6656 break;
6657 default:
6658 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6659 }
6660
6661 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6662 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6663 flat->operands[0] = Operand(addr);
6664 flat->operands[1] = Operand(s1);
6665 flat->operands[2] = Operand(data);
6666 if (return_previous)
6667 flat->definitions[0] = Definition(dst);
6668 flat->glc = return_previous;
6669 flat->dlc = false; /* Not needed for atomics */
6670 flat->offset = 0;
6671 flat->disable_wqm = true;
6672 flat->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6673 ctx->program->needs_exact = true;
6674 ctx->block->instructions.emplace_back(std::move(flat));
6675 } else {
6676 assert(ctx->options->chip_class == GFX6);
6677
6678 switch (instr->intrinsic) {
6679 case nir_intrinsic_global_atomic_add:
6680 op32 = aco_opcode::buffer_atomic_add;
6681 op64 = aco_opcode::buffer_atomic_add_x2;
6682 break;
6683 case nir_intrinsic_global_atomic_imin:
6684 op32 = aco_opcode::buffer_atomic_smin;
6685 op64 = aco_opcode::buffer_atomic_smin_x2;
6686 break;
6687 case nir_intrinsic_global_atomic_umin:
6688 op32 = aco_opcode::buffer_atomic_umin;
6689 op64 = aco_opcode::buffer_atomic_umin_x2;
6690 break;
6691 case nir_intrinsic_global_atomic_imax:
6692 op32 = aco_opcode::buffer_atomic_smax;
6693 op64 = aco_opcode::buffer_atomic_smax_x2;
6694 break;
6695 case nir_intrinsic_global_atomic_umax:
6696 op32 = aco_opcode::buffer_atomic_umax;
6697 op64 = aco_opcode::buffer_atomic_umax_x2;
6698 break;
6699 case nir_intrinsic_global_atomic_and:
6700 op32 = aco_opcode::buffer_atomic_and;
6701 op64 = aco_opcode::buffer_atomic_and_x2;
6702 break;
6703 case nir_intrinsic_global_atomic_or:
6704 op32 = aco_opcode::buffer_atomic_or;
6705 op64 = aco_opcode::buffer_atomic_or_x2;
6706 break;
6707 case nir_intrinsic_global_atomic_xor:
6708 op32 = aco_opcode::buffer_atomic_xor;
6709 op64 = aco_opcode::buffer_atomic_xor_x2;
6710 break;
6711 case nir_intrinsic_global_atomic_exchange:
6712 op32 = aco_opcode::buffer_atomic_swap;
6713 op64 = aco_opcode::buffer_atomic_swap_x2;
6714 break;
6715 case nir_intrinsic_global_atomic_comp_swap:
6716 op32 = aco_opcode::buffer_atomic_cmpswap;
6717 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6718 break;
6719 default:
6720 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6721 }
6722
6723 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6724
6725 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6726
6727 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6728 mubuf->operands[0] = Operand(rsrc);
6729 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6730 mubuf->operands[2] = Operand(0u);
6731 mubuf->operands[3] = Operand(data);
6732 if (return_previous)
6733 mubuf->definitions[0] = Definition(dst);
6734 mubuf->glc = return_previous;
6735 mubuf->dlc = false;
6736 mubuf->offset = 0;
6737 mubuf->addr64 = addr.type() == RegType::vgpr;
6738 mubuf->disable_wqm = true;
6739 mubuf->sync = get_memory_sync_info(instr, storage_buffer, semantic_atomicrmw);
6740 ctx->program->needs_exact = true;
6741 ctx->block->instructions.emplace_back(std::move(mubuf));
6742 }
6743 }
6744
6745 sync_scope translate_nir_scope(nir_scope scope)
6746 {
6747 switch (scope) {
6748 case NIR_SCOPE_NONE:
6749 case NIR_SCOPE_INVOCATION:
6750 return scope_invocation;
6751 case NIR_SCOPE_SUBGROUP:
6752 return scope_subgroup;
6753 case NIR_SCOPE_WORKGROUP:
6754 return scope_workgroup;
6755 case NIR_SCOPE_QUEUE_FAMILY:
6756 return scope_queuefamily;
6757 case NIR_SCOPE_DEVICE:
6758 return scope_device;
6759 }
6760 unreachable("invalid scope");
6761 }
6762
6763 void emit_scoped_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6764 Builder bld(ctx->program, ctx->block);
6765
6766 unsigned semantics = 0;
6767 unsigned storage = 0;
6768 sync_scope mem_scope = translate_nir_scope(nir_intrinsic_memory_scope(instr));
6769 sync_scope exec_scope = translate_nir_scope(nir_intrinsic_execution_scope(instr));
6770
6771 unsigned nir_storage = nir_intrinsic_memory_modes(instr);
6772 if (nir_storage & (nir_var_mem_ssbo | nir_var_mem_global))
6773 storage |= storage_buffer | storage_image; //TODO: split this when NIR gets nir_var_mem_image
6774 if (ctx->shader->info.stage == MESA_SHADER_COMPUTE && (nir_storage & nir_var_mem_shared))
6775 storage |= storage_shared;
6776 if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL && (nir_storage & nir_var_shader_out))
6777 storage |= storage_shared;
6778
6779 unsigned nir_semantics = nir_intrinsic_memory_semantics(instr);
6780 if (nir_semantics & NIR_MEMORY_ACQUIRE)
6781 semantics |= semantic_acquire | semantic_release;
6782 if (nir_semantics & NIR_MEMORY_RELEASE)
6783 semantics |= semantic_acquire | semantic_release;
6784
6785 assert(!(nir_semantics & (NIR_MEMORY_MAKE_AVAILABLE | NIR_MEMORY_MAKE_VISIBLE)));
6786
6787 bld.barrier(aco_opcode::p_barrier,
6788 memory_sync_info((storage_class)storage, (memory_semantics)semantics, mem_scope),
6789 exec_scope);
6790 }
6791
6792 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6793 {
6794 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6795 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6796 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6797 Builder bld(ctx->program, ctx->block);
6798
6799 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6800 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6801 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6802 }
6803
6804 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6805 {
6806 unsigned writemask = nir_intrinsic_write_mask(instr);
6807 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6808 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6809 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6810
6811 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6812 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6813 }
6814
6815 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6816 {
6817 unsigned offset = nir_intrinsic_base(instr);
6818 Builder bld(ctx->program, ctx->block);
6819 Operand m = load_lds_size_m0(bld);
6820 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6821 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6822
6823 unsigned num_operands = 3;
6824 aco_opcode op32, op64, op32_rtn, op64_rtn;
6825 switch(instr->intrinsic) {
6826 case nir_intrinsic_shared_atomic_add:
6827 op32 = aco_opcode::ds_add_u32;
6828 op64 = aco_opcode::ds_add_u64;
6829 op32_rtn = aco_opcode::ds_add_rtn_u32;
6830 op64_rtn = aco_opcode::ds_add_rtn_u64;
6831 break;
6832 case nir_intrinsic_shared_atomic_imin:
6833 op32 = aco_opcode::ds_min_i32;
6834 op64 = aco_opcode::ds_min_i64;
6835 op32_rtn = aco_opcode::ds_min_rtn_i32;
6836 op64_rtn = aco_opcode::ds_min_rtn_i64;
6837 break;
6838 case nir_intrinsic_shared_atomic_umin:
6839 op32 = aco_opcode::ds_min_u32;
6840 op64 = aco_opcode::ds_min_u64;
6841 op32_rtn = aco_opcode::ds_min_rtn_u32;
6842 op64_rtn = aco_opcode::ds_min_rtn_u64;
6843 break;
6844 case nir_intrinsic_shared_atomic_imax:
6845 op32 = aco_opcode::ds_max_i32;
6846 op64 = aco_opcode::ds_max_i64;
6847 op32_rtn = aco_opcode::ds_max_rtn_i32;
6848 op64_rtn = aco_opcode::ds_max_rtn_i64;
6849 break;
6850 case nir_intrinsic_shared_atomic_umax:
6851 op32 = aco_opcode::ds_max_u32;
6852 op64 = aco_opcode::ds_max_u64;
6853 op32_rtn = aco_opcode::ds_max_rtn_u32;
6854 op64_rtn = aco_opcode::ds_max_rtn_u64;
6855 break;
6856 case nir_intrinsic_shared_atomic_and:
6857 op32 = aco_opcode::ds_and_b32;
6858 op64 = aco_opcode::ds_and_b64;
6859 op32_rtn = aco_opcode::ds_and_rtn_b32;
6860 op64_rtn = aco_opcode::ds_and_rtn_b64;
6861 break;
6862 case nir_intrinsic_shared_atomic_or:
6863 op32 = aco_opcode::ds_or_b32;
6864 op64 = aco_opcode::ds_or_b64;
6865 op32_rtn = aco_opcode::ds_or_rtn_b32;
6866 op64_rtn = aco_opcode::ds_or_rtn_b64;
6867 break;
6868 case nir_intrinsic_shared_atomic_xor:
6869 op32 = aco_opcode::ds_xor_b32;
6870 op64 = aco_opcode::ds_xor_b64;
6871 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6872 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6873 break;
6874 case nir_intrinsic_shared_atomic_exchange:
6875 op32 = aco_opcode::ds_write_b32;
6876 op64 = aco_opcode::ds_write_b64;
6877 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6878 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6879 break;
6880 case nir_intrinsic_shared_atomic_comp_swap:
6881 op32 = aco_opcode::ds_cmpst_b32;
6882 op64 = aco_opcode::ds_cmpst_b64;
6883 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6884 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6885 num_operands = 4;
6886 break;
6887 case nir_intrinsic_shared_atomic_fadd:
6888 op32 = aco_opcode::ds_add_f32;
6889 op32_rtn = aco_opcode::ds_add_rtn_f32;
6890 op64 = aco_opcode::num_opcodes;
6891 op64_rtn = aco_opcode::num_opcodes;
6892 break;
6893 default:
6894 unreachable("Unhandled shared atomic intrinsic");
6895 }
6896
6897 /* return the previous value if dest is ever used */
6898 bool return_previous = false;
6899 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6900 return_previous = true;
6901 break;
6902 }
6903 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6904 return_previous = true;
6905 break;
6906 }
6907
6908 aco_opcode op;
6909 if (data.size() == 1) {
6910 assert(instr->dest.ssa.bit_size == 32);
6911 op = return_previous ? op32_rtn : op32;
6912 } else {
6913 assert(instr->dest.ssa.bit_size == 64);
6914 op = return_previous ? op64_rtn : op64;
6915 }
6916
6917 if (offset > 65535) {
6918 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6919 offset = 0;
6920 }
6921
6922 aco_ptr<DS_instruction> ds;
6923 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6924 ds->operands[0] = Operand(address);
6925 ds->operands[1] = Operand(data);
6926 if (num_operands == 4)
6927 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6928 ds->operands[num_operands - 1] = m;
6929 ds->offset0 = offset;
6930 if (return_previous)
6931 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6932 ds->sync = memory_sync_info(storage_shared, semantic_atomicrmw);
6933 ctx->block->instructions.emplace_back(std::move(ds));
6934 }
6935
6936 Temp get_scratch_resource(isel_context *ctx)
6937 {
6938 Builder bld(ctx->program, ctx->block);
6939 Temp scratch_addr = ctx->program->private_segment_buffer;
6940 if (ctx->stage != compute_cs)
6941 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6942
6943 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6944 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);
6945
6946 if (ctx->program->chip_class >= GFX10) {
6947 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6948 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6949 S_008F0C_RESOURCE_LEVEL(1);
6950 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6951 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6952 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6953 }
6954
6955 /* older generations need element size = 4 bytes. element size removed in GFX9 */
6956 if (ctx->program->chip_class <= GFX8)
6957 rsrc_conf |= S_008F0C_ELEMENT_SIZE(1);
6958
6959 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6960 }
6961
6962 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6963 Builder bld(ctx->program, ctx->block);
6964 Temp rsrc = get_scratch_resource(ctx);
6965 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6966 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6967
6968 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6969 instr->dest.ssa.bit_size / 8u, rsrc};
6970 info.align_mul = nir_intrinsic_align_mul(instr);
6971 info.align_offset = nir_intrinsic_align_offset(instr);
6972 info.swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 0;
6973 info.sync = memory_sync_info(storage_scratch, semantic_private);
6974 info.soffset = ctx->program->scratch_offset;
6975 emit_scratch_load(ctx, bld, &info);
6976 }
6977
6978 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6979 Builder bld(ctx->program, ctx->block);
6980 Temp rsrc = get_scratch_resource(ctx);
6981 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6982 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6983
6984 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6985 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6986
6987 unsigned write_count = 0;
6988 Temp write_datas[32];
6989 unsigned offsets[32];
6990 unsigned swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 16;
6991 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6992 swizzle_component_size, &write_count, write_datas, offsets);
6993
6994 for (unsigned i = 0; i < write_count; i++) {
6995 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6996 Instruction *instr = bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true, true);
6997 static_cast<MUBUF_instruction *>(instr)->sync = memory_sync_info(storage_scratch, semantic_private);
6998 }
6999 }
7000
7001 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
7002 uint8_t log2_ps_iter_samples;
7003 if (ctx->program->info->ps.force_persample) {
7004 log2_ps_iter_samples =
7005 util_logbase2(ctx->options->key.fs.num_samples);
7006 } else {
7007 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
7008 }
7009
7010 /* The bit pattern matches that used by fixed function fragment
7011 * processing. */
7012 static const unsigned ps_iter_masks[] = {
7013 0xffff, /* not used */
7014 0x5555,
7015 0x1111,
7016 0x0101,
7017 0x0001,
7018 };
7019 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
7020
7021 Builder bld(ctx->program, ctx->block);
7022
7023 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
7024 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7025 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
7026 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
7027 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7028 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
7029 }
7030
7031 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
7032 Builder bld(ctx->program, ctx->block);
7033
7034 unsigned stream = nir_intrinsic_stream_id(instr);
7035 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7036 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
7037 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
7038
7039 /* get GSVS ring */
7040 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
7041
7042 unsigned num_components =
7043 ctx->program->info->gs.num_stream_output_components[stream];
7044 assert(num_components);
7045
7046 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
7047 unsigned stream_offset = 0;
7048 for (unsigned i = 0; i < stream; i++) {
7049 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
7050 stream_offset += prev_stride * ctx->program->wave_size;
7051 }
7052
7053 /* Limit on the stride field for <= GFX7. */
7054 assert(stride < (1 << 14));
7055
7056 Temp gsvs_dwords[4];
7057 for (unsigned i = 0; i < 4; i++)
7058 gsvs_dwords[i] = bld.tmp(s1);
7059 bld.pseudo(aco_opcode::p_split_vector,
7060 Definition(gsvs_dwords[0]),
7061 Definition(gsvs_dwords[1]),
7062 Definition(gsvs_dwords[2]),
7063 Definition(gsvs_dwords[3]),
7064 gsvs_ring);
7065
7066 if (stream_offset) {
7067 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
7068
7069 Temp carry = bld.tmp(s1);
7070 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
7071 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));
7072 }
7073
7074 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)));
7075 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
7076
7077 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
7078 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
7079
7080 unsigned offset = 0;
7081 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
7082 if (ctx->program->info->gs.output_streams[i] != stream)
7083 continue;
7084
7085 for (unsigned j = 0; j < 4; j++) {
7086 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
7087 continue;
7088
7089 if (ctx->outputs.mask[i] & (1 << j)) {
7090 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
7091 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
7092 if (const_offset >= 4096u) {
7093 if (vaddr_offset.isUndefined())
7094 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
7095 else
7096 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
7097 const_offset %= 4096u;
7098 }
7099
7100 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
7101 mtbuf->operands[0] = Operand(gsvs_ring);
7102 mtbuf->operands[1] = vaddr_offset;
7103 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
7104 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
7105 mtbuf->offen = !vaddr_offset.isUndefined();
7106 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
7107 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
7108 mtbuf->offset = const_offset;
7109 mtbuf->glc = true;
7110 mtbuf->slc = true;
7111 mtbuf->sync = memory_sync_info(storage_vmem_output, semantic_can_reorder);
7112 bld.insert(std::move(mtbuf));
7113 }
7114
7115 offset += ctx->shader->info.gs.vertices_out;
7116 }
7117
7118 /* outputs for the next vertex are undefined and keeping them around can
7119 * create invalid IR with control flow */
7120 ctx->outputs.mask[i] = 0;
7121 }
7122
7123 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
7124 }
7125
7126 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
7127 {
7128 Builder bld(ctx->program, ctx->block);
7129
7130 if (cluster_size == 1) {
7131 return src;
7132 } if (op == nir_op_iand && cluster_size == 4) {
7133 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7134 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7135 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
7136 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
7137 } else if (op == nir_op_ior && cluster_size == 4) {
7138 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7139 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
7140 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
7141 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
7142 //subgroupAnd(val) -> (exec & ~val) == 0
7143 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7144 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7145 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
7146 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
7147 //subgroupOr(val) -> (val & exec) != 0
7148 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
7149 return bool_to_vector_condition(ctx, tmp);
7150 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
7151 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7152 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7153 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
7154 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
7155 return bool_to_vector_condition(ctx, tmp);
7156 } else {
7157 //subgroupClustered{And,Or,Xor}(val, n) ->
7158 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7159 //cluster_offset = ~(n - 1) & lane_id
7160 //cluster_mask = ((1 << n) - 1)
7161 //subgroupClusteredAnd():
7162 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7163 //subgroupClusteredOr():
7164 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7165 //subgroupClusteredXor():
7166 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7167 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7168 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7169
7170 Temp tmp;
7171 if (op == nir_op_iand)
7172 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7173 else
7174 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7175
7176 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7177
7178 if (ctx->program->chip_class <= GFX7)
7179 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7180 else if (ctx->program->wave_size == 64)
7181 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7182 else
7183 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7184 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7185 if (cluster_mask != 0xffffffff)
7186 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7187
7188 Definition cmp_def = Definition();
7189 if (op == nir_op_iand) {
7190 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7191 } else if (op == nir_op_ior) {
7192 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7193 } else if (op == nir_op_ixor) {
7194 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7195 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7196 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7197 }
7198 cmp_def.setHint(vcc);
7199 return cmp_def.getTemp();
7200 }
7201 }
7202
7203 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7204 {
7205 Builder bld(ctx->program, ctx->block);
7206
7207 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7208 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7209 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7210 Temp tmp;
7211 if (op == nir_op_iand)
7212 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7213 else
7214 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7215
7216 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7217 Temp lo = lohi.def(0).getTemp();
7218 Temp hi = lohi.def(1).getTemp();
7219 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7220
7221 Definition cmp_def = Definition();
7222 if (op == nir_op_iand)
7223 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7224 else if (op == nir_op_ior)
7225 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7226 else if (op == nir_op_ixor)
7227 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7228 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7229 cmp_def.setHint(vcc);
7230 return cmp_def.getTemp();
7231 }
7232
7233 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7234 {
7235 Builder bld(ctx->program, ctx->block);
7236
7237 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7238 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7239 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7240 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7241 if (op == nir_op_iand)
7242 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7243 else if (op == nir_op_ior)
7244 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7245 else if (op == nir_op_ixor)
7246 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7247
7248 assert(false);
7249 return Temp();
7250 }
7251
7252 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7253 {
7254 Builder bld(ctx->program, ctx->block);
7255 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7256 if (src.regClass().type() == RegType::vgpr) {
7257 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7258 } else if (src.regClass() == s1) {
7259 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7260 } else if (src.regClass() == s2) {
7261 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7262 } else {
7263 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7264 nir_print_instr(&instr->instr, stderr);
7265 fprintf(stderr, "\n");
7266 }
7267 }
7268
7269 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7270 {
7271 Builder bld(ctx->program, ctx->block);
7272 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7273 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7274 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7275
7276 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7277 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7278 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7279 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7280
7281 /* Build DD X/Y */
7282 if (ctx->program->chip_class >= GFX8) {
7283 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7284 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7285 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7286 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7287 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7288 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7289 } else {
7290 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7291 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7292 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7293 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7294 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7295 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7296 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7297 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7298 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7299 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7300 }
7301
7302 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7303 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7304 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7305 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7306 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7307 Temp wqm1 = bld.tmp(v1);
7308 emit_wqm(ctx, tmp1, wqm1, true);
7309 Temp wqm2 = bld.tmp(v1);
7310 emit_wqm(ctx, tmp2, wqm2, true);
7311 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7312 return;
7313 }
7314
7315 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7316 {
7317 Builder bld(ctx->program, ctx->block);
7318 switch(instr->intrinsic) {
7319 case nir_intrinsic_load_barycentric_sample:
7320 case nir_intrinsic_load_barycentric_pixel:
7321 case nir_intrinsic_load_barycentric_centroid: {
7322 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7323 Temp bary = Temp(0, s2);
7324 switch (mode) {
7325 case INTERP_MODE_SMOOTH:
7326 case INTERP_MODE_NONE:
7327 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7328 bary = get_arg(ctx, ctx->args->ac.persp_center);
7329 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7330 bary = ctx->persp_centroid;
7331 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7332 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7333 break;
7334 case INTERP_MODE_NOPERSPECTIVE:
7335 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7336 bary = get_arg(ctx, ctx->args->ac.linear_center);
7337 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7338 bary = ctx->linear_centroid;
7339 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7340 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7341 break;
7342 default:
7343 break;
7344 }
7345 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7346 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7347 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7348 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7349 Operand(p1), Operand(p2));
7350 emit_split_vector(ctx, dst, 2);
7351 break;
7352 }
7353 case nir_intrinsic_load_barycentric_model: {
7354 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7355
7356 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7357 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7358 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7359 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7360 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7361 Operand(p1), Operand(p2), Operand(p3));
7362 emit_split_vector(ctx, dst, 3);
7363 break;
7364 }
7365 case nir_intrinsic_load_barycentric_at_sample: {
7366 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7367 switch (ctx->options->key.fs.num_samples) {
7368 case 2: sample_pos_offset += 1 << 3; break;
7369 case 4: sample_pos_offset += 3 << 3; break;
7370 case 8: sample_pos_offset += 7 << 3; break;
7371 default: break;
7372 }
7373 Temp sample_pos;
7374 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7375 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7376 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7377 //TODO: bounds checking?
7378 if (addr.type() == RegType::sgpr) {
7379 Operand offset;
7380 if (const_addr) {
7381 sample_pos_offset += const_addr->u32 << 3;
7382 offset = Operand(sample_pos_offset);
7383 } else if (ctx->options->chip_class >= GFX9) {
7384 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7385 } else {
7386 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7387 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7388 }
7389
7390 Operand off = bld.copy(bld.def(s1), Operand(offset));
7391 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7392
7393 } else if (ctx->options->chip_class >= GFX9) {
7394 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7395 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7396 } else if (ctx->options->chip_class >= GFX7) {
7397 /* addr += private_segment_buffer + sample_pos_offset */
7398 Temp tmp0 = bld.tmp(s1);
7399 Temp tmp1 = bld.tmp(s1);
7400 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7401 Definition scc_tmp = bld.def(s1, scc);
7402 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7403 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7404 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7405 Temp pck0 = bld.tmp(v1);
7406 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7407 tmp1 = as_vgpr(ctx, tmp1);
7408 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);
7409 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7410
7411 /* sample_pos = flat_load_dwordx2 addr */
7412 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7413 } else {
7414 assert(ctx->options->chip_class == GFX6);
7415
7416 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7417 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7418 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7419
7420 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7421 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7422
7423 sample_pos = bld.tmp(v2);
7424
7425 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7426 load->definitions[0] = Definition(sample_pos);
7427 load->operands[0] = Operand(rsrc);
7428 load->operands[1] = Operand(addr);
7429 load->operands[2] = Operand(0u);
7430 load->offset = sample_pos_offset;
7431 load->offen = 0;
7432 load->addr64 = true;
7433 load->glc = false;
7434 load->dlc = false;
7435 load->disable_wqm = false;
7436 ctx->block->instructions.emplace_back(std::move(load));
7437 }
7438
7439 /* sample_pos -= 0.5 */
7440 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7441 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7442 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7443 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7444 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7445
7446 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7447 break;
7448 }
7449 case nir_intrinsic_load_barycentric_at_offset: {
7450 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7451 RegClass rc = RegClass(offset.type(), 1);
7452 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7453 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7454 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7455 break;
7456 }
7457 case nir_intrinsic_load_front_face: {
7458 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7459 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7460 break;
7461 }
7462 case nir_intrinsic_load_view_index: {
7463 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7464 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7465 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7466 break;
7467 }
7468
7469 /* fallthrough */
7470 }
7471 case nir_intrinsic_load_layer_id: {
7472 unsigned idx = nir_intrinsic_base(instr);
7473 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7474 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7475 break;
7476 }
7477 case nir_intrinsic_load_frag_coord: {
7478 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7479 break;
7480 }
7481 case nir_intrinsic_load_sample_pos: {
7482 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7483 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7484 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7485 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7486 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7487 break;
7488 }
7489 case nir_intrinsic_load_tess_coord:
7490 visit_load_tess_coord(ctx, instr);
7491 break;
7492 case nir_intrinsic_load_interpolated_input:
7493 visit_load_interpolated_input(ctx, instr);
7494 break;
7495 case nir_intrinsic_store_output:
7496 visit_store_output(ctx, instr);
7497 break;
7498 case nir_intrinsic_load_input:
7499 case nir_intrinsic_load_input_vertex:
7500 visit_load_input(ctx, instr);
7501 break;
7502 case nir_intrinsic_load_output:
7503 visit_load_output(ctx, instr);
7504 break;
7505 case nir_intrinsic_load_per_vertex_input:
7506 visit_load_per_vertex_input(ctx, instr);
7507 break;
7508 case nir_intrinsic_load_per_vertex_output:
7509 visit_load_per_vertex_output(ctx, instr);
7510 break;
7511 case nir_intrinsic_store_per_vertex_output:
7512 visit_store_per_vertex_output(ctx, instr);
7513 break;
7514 case nir_intrinsic_load_ubo:
7515 visit_load_ubo(ctx, instr);
7516 break;
7517 case nir_intrinsic_load_push_constant:
7518 visit_load_push_constant(ctx, instr);
7519 break;
7520 case nir_intrinsic_load_constant:
7521 visit_load_constant(ctx, instr);
7522 break;
7523 case nir_intrinsic_vulkan_resource_index:
7524 visit_load_resource(ctx, instr);
7525 break;
7526 case nir_intrinsic_discard:
7527 visit_discard(ctx, instr);
7528 break;
7529 case nir_intrinsic_discard_if:
7530 visit_discard_if(ctx, instr);
7531 break;
7532 case nir_intrinsic_load_shared:
7533 visit_load_shared(ctx, instr);
7534 break;
7535 case nir_intrinsic_store_shared:
7536 visit_store_shared(ctx, instr);
7537 break;
7538 case nir_intrinsic_shared_atomic_add:
7539 case nir_intrinsic_shared_atomic_imin:
7540 case nir_intrinsic_shared_atomic_umin:
7541 case nir_intrinsic_shared_atomic_imax:
7542 case nir_intrinsic_shared_atomic_umax:
7543 case nir_intrinsic_shared_atomic_and:
7544 case nir_intrinsic_shared_atomic_or:
7545 case nir_intrinsic_shared_atomic_xor:
7546 case nir_intrinsic_shared_atomic_exchange:
7547 case nir_intrinsic_shared_atomic_comp_swap:
7548 case nir_intrinsic_shared_atomic_fadd:
7549 visit_shared_atomic(ctx, instr);
7550 break;
7551 case nir_intrinsic_image_deref_load:
7552 visit_image_load(ctx, instr);
7553 break;
7554 case nir_intrinsic_image_deref_store:
7555 visit_image_store(ctx, instr);
7556 break;
7557 case nir_intrinsic_image_deref_atomic_add:
7558 case nir_intrinsic_image_deref_atomic_umin:
7559 case nir_intrinsic_image_deref_atomic_imin:
7560 case nir_intrinsic_image_deref_atomic_umax:
7561 case nir_intrinsic_image_deref_atomic_imax:
7562 case nir_intrinsic_image_deref_atomic_and:
7563 case nir_intrinsic_image_deref_atomic_or:
7564 case nir_intrinsic_image_deref_atomic_xor:
7565 case nir_intrinsic_image_deref_atomic_exchange:
7566 case nir_intrinsic_image_deref_atomic_comp_swap:
7567 visit_image_atomic(ctx, instr);
7568 break;
7569 case nir_intrinsic_image_deref_size:
7570 visit_image_size(ctx, instr);
7571 break;
7572 case nir_intrinsic_load_ssbo:
7573 visit_load_ssbo(ctx, instr);
7574 break;
7575 case nir_intrinsic_store_ssbo:
7576 visit_store_ssbo(ctx, instr);
7577 break;
7578 case nir_intrinsic_load_global:
7579 visit_load_global(ctx, instr);
7580 break;
7581 case nir_intrinsic_store_global:
7582 visit_store_global(ctx, instr);
7583 break;
7584 case nir_intrinsic_global_atomic_add:
7585 case nir_intrinsic_global_atomic_imin:
7586 case nir_intrinsic_global_atomic_umin:
7587 case nir_intrinsic_global_atomic_imax:
7588 case nir_intrinsic_global_atomic_umax:
7589 case nir_intrinsic_global_atomic_and:
7590 case nir_intrinsic_global_atomic_or:
7591 case nir_intrinsic_global_atomic_xor:
7592 case nir_intrinsic_global_atomic_exchange:
7593 case nir_intrinsic_global_atomic_comp_swap:
7594 visit_global_atomic(ctx, instr);
7595 break;
7596 case nir_intrinsic_ssbo_atomic_add:
7597 case nir_intrinsic_ssbo_atomic_imin:
7598 case nir_intrinsic_ssbo_atomic_umin:
7599 case nir_intrinsic_ssbo_atomic_imax:
7600 case nir_intrinsic_ssbo_atomic_umax:
7601 case nir_intrinsic_ssbo_atomic_and:
7602 case nir_intrinsic_ssbo_atomic_or:
7603 case nir_intrinsic_ssbo_atomic_xor:
7604 case nir_intrinsic_ssbo_atomic_exchange:
7605 case nir_intrinsic_ssbo_atomic_comp_swap:
7606 visit_atomic_ssbo(ctx, instr);
7607 break;
7608 case nir_intrinsic_load_scratch:
7609 visit_load_scratch(ctx, instr);
7610 break;
7611 case nir_intrinsic_store_scratch:
7612 visit_store_scratch(ctx, instr);
7613 break;
7614 case nir_intrinsic_get_buffer_size:
7615 visit_get_buffer_size(ctx, instr);
7616 break;
7617 case nir_intrinsic_scoped_barrier:
7618 emit_scoped_barrier(ctx, instr);
7619 break;
7620 case nir_intrinsic_load_num_work_groups: {
7621 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7622 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7623 emit_split_vector(ctx, dst, 3);
7624 break;
7625 }
7626 case nir_intrinsic_load_local_invocation_id: {
7627 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7628 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7629 emit_split_vector(ctx, dst, 3);
7630 break;
7631 }
7632 case nir_intrinsic_load_work_group_id: {
7633 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7634 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7635 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7636 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7637 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7638 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7639 emit_split_vector(ctx, dst, 3);
7640 break;
7641 }
7642 case nir_intrinsic_load_local_invocation_index: {
7643 Temp id = emit_mbcnt(ctx, bld.def(v1));
7644
7645 /* The tg_size bits [6:11] contain the subgroup id,
7646 * we need this multiplied by the wave size, and then OR the thread id to it.
7647 */
7648 if (ctx->program->wave_size == 64) {
7649 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7650 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7651 get_arg(ctx, ctx->args->ac.tg_size));
7652 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7653 } else {
7654 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7655 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7656 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7657 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7658 }
7659 break;
7660 }
7661 case nir_intrinsic_load_subgroup_id: {
7662 if (ctx->stage == compute_cs) {
7663 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7664 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7665 } else {
7666 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7667 }
7668 break;
7669 }
7670 case nir_intrinsic_load_subgroup_invocation: {
7671 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7672 break;
7673 }
7674 case nir_intrinsic_load_num_subgroups: {
7675 if (ctx->stage == compute_cs)
7676 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7677 get_arg(ctx, ctx->args->ac.tg_size));
7678 else
7679 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7680 break;
7681 }
7682 case nir_intrinsic_ballot: {
7683 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7684 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7685 Definition tmp = bld.def(dst.regClass());
7686 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7687 if (instr->src[0].ssa->bit_size == 1) {
7688 assert(src.regClass() == bld.lm);
7689 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7690 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7691 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7692 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7693 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7694 } else {
7695 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7696 nir_print_instr(&instr->instr, stderr);
7697 fprintf(stderr, "\n");
7698 }
7699 if (dst.size() != bld.lm.size()) {
7700 /* Wave32 with ballot size set to 64 */
7701 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7702 }
7703 emit_wqm(ctx, tmp.getTemp(), dst);
7704 break;
7705 }
7706 case nir_intrinsic_shuffle:
7707 case nir_intrinsic_read_invocation: {
7708 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7709 if (!nir_src_is_divergent(instr->src[0])) {
7710 emit_uniform_subgroup(ctx, instr, src);
7711 } else {
7712 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7713 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7714 tid = bld.as_uniform(tid);
7715 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7716 if (src.regClass() == v1b || src.regClass() == v2b) {
7717 Temp tmp = bld.tmp(v1);
7718 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7719 if (dst.type() == RegType::vgpr)
7720 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7721 else
7722 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7723 } else if (src.regClass() == v1) {
7724 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7725 } else if (src.regClass() == v2) {
7726 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7727 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7728 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7729 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7730 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7731 emit_split_vector(ctx, dst, 2);
7732 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7733 assert(src.regClass() == bld.lm);
7734 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7735 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7736 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7737 assert(src.regClass() == bld.lm);
7738 Temp tmp;
7739 if (ctx->program->chip_class <= GFX7)
7740 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7741 else if (ctx->program->wave_size == 64)
7742 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7743 else
7744 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7745 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7746 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7747 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7748 } else {
7749 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7750 nir_print_instr(&instr->instr, stderr);
7751 fprintf(stderr, "\n");
7752 }
7753 }
7754 break;
7755 }
7756 case nir_intrinsic_load_sample_id: {
7757 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7758 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7759 break;
7760 }
7761 case nir_intrinsic_load_sample_mask_in: {
7762 visit_load_sample_mask_in(ctx, instr);
7763 break;
7764 }
7765 case nir_intrinsic_read_first_invocation: {
7766 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7767 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7768 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7769 emit_wqm(ctx,
7770 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7771 dst);
7772 } else if (src.regClass() == v2) {
7773 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7774 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7775 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7776 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7777 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7778 emit_split_vector(ctx, dst, 2);
7779 } else if (instr->dest.ssa.bit_size == 1) {
7780 assert(src.regClass() == bld.lm);
7781 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7782 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7783 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7784 } else if (src.regClass() == s1) {
7785 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7786 } else if (src.regClass() == s2) {
7787 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7788 } else {
7789 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7790 nir_print_instr(&instr->instr, stderr);
7791 fprintf(stderr, "\n");
7792 }
7793 break;
7794 }
7795 case nir_intrinsic_vote_all: {
7796 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7797 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7798 assert(src.regClass() == bld.lm);
7799 assert(dst.regClass() == bld.lm);
7800
7801 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7802 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7803 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7804 break;
7805 }
7806 case nir_intrinsic_vote_any: {
7807 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7808 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7809 assert(src.regClass() == bld.lm);
7810 assert(dst.regClass() == bld.lm);
7811
7812 Temp tmp = bool_to_scalar_condition(ctx, src);
7813 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7814 break;
7815 }
7816 case nir_intrinsic_reduce:
7817 case nir_intrinsic_inclusive_scan:
7818 case nir_intrinsic_exclusive_scan: {
7819 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7820 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7821 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7822 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7823 nir_intrinsic_cluster_size(instr) : 0;
7824 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7825
7826 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7827 emit_uniform_subgroup(ctx, instr, src);
7828 } else if (instr->dest.ssa.bit_size == 1) {
7829 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7830 op = nir_op_iand;
7831 else if (op == nir_op_iadd)
7832 op = nir_op_ixor;
7833 else if (op == nir_op_umax || op == nir_op_imax)
7834 op = nir_op_ior;
7835 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7836
7837 switch (instr->intrinsic) {
7838 case nir_intrinsic_reduce:
7839 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7840 break;
7841 case nir_intrinsic_exclusive_scan:
7842 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7843 break;
7844 case nir_intrinsic_inclusive_scan:
7845 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7846 break;
7847 default:
7848 assert(false);
7849 }
7850 } else if (cluster_size == 1) {
7851 bld.copy(Definition(dst), src);
7852 } else {
7853 unsigned bit_size = instr->src[0].ssa->bit_size;
7854
7855 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7856
7857 ReduceOp reduce_op;
7858 switch (op) {
7859 #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;
7860 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7861 CASEI(iadd)
7862 CASEI(imul)
7863 CASEI(imin)
7864 CASEI(umin)
7865 CASEI(imax)
7866 CASEI(umax)
7867 CASEI(iand)
7868 CASEI(ior)
7869 CASEI(ixor)
7870 CASEF(fadd)
7871 CASEF(fmul)
7872 CASEF(fmin)
7873 CASEF(fmax)
7874 default:
7875 unreachable("unknown reduction op");
7876 #undef CASEI
7877 #undef CASEF
7878 }
7879
7880 aco_opcode aco_op;
7881 switch (instr->intrinsic) {
7882 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7883 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7884 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7885 default:
7886 unreachable("unknown reduce intrinsic");
7887 }
7888
7889 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7890 reduce->operands[0] = Operand(src);
7891 // filled in by aco_reduce_assign.cpp, used internally as part of the
7892 // reduce sequence
7893 assert(dst.size() == 1 || dst.size() == 2);
7894 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7895 reduce->operands[2] = Operand(v1.as_linear());
7896
7897 Temp tmp_dst = bld.tmp(dst.regClass());
7898 reduce->definitions[0] = Definition(tmp_dst);
7899 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7900 reduce->definitions[2] = Definition();
7901 reduce->definitions[3] = Definition(scc, s1);
7902 reduce->definitions[4] = Definition();
7903 reduce->reduce_op = reduce_op;
7904 reduce->cluster_size = cluster_size;
7905 ctx->block->instructions.emplace_back(std::move(reduce));
7906
7907 emit_wqm(ctx, tmp_dst, dst);
7908 }
7909 break;
7910 }
7911 case nir_intrinsic_quad_broadcast: {
7912 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7913 if (!nir_dest_is_divergent(instr->dest)) {
7914 emit_uniform_subgroup(ctx, instr, src);
7915 } else {
7916 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7917 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7918 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7919
7920 if (instr->dest.ssa.bit_size == 1) {
7921 assert(src.regClass() == bld.lm);
7922 assert(dst.regClass() == bld.lm);
7923 uint32_t half_mask = 0x11111111u << lane;
7924 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7925 Temp tmp = bld.tmp(bld.lm);
7926 bld.sop1(Builder::s_wqm, Definition(tmp),
7927 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7928 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7929 emit_wqm(ctx, tmp, dst);
7930 } else if (instr->dest.ssa.bit_size == 8) {
7931 Temp tmp = bld.tmp(v1);
7932 if (ctx->program->chip_class >= GFX8)
7933 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7934 else
7935 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7936 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7937 } else if (instr->dest.ssa.bit_size == 16) {
7938 Temp tmp = bld.tmp(v1);
7939 if (ctx->program->chip_class >= GFX8)
7940 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7941 else
7942 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7943 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7944 } else if (instr->dest.ssa.bit_size == 32) {
7945 if (ctx->program->chip_class >= GFX8)
7946 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7947 else
7948 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7949 } else if (instr->dest.ssa.bit_size == 64) {
7950 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7951 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7952 if (ctx->program->chip_class >= GFX8) {
7953 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7954 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7955 } else {
7956 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7957 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7958 }
7959 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7960 emit_split_vector(ctx, dst, 2);
7961 } else {
7962 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7963 nir_print_instr(&instr->instr, stderr);
7964 fprintf(stderr, "\n");
7965 }
7966 }
7967 break;
7968 }
7969 case nir_intrinsic_quad_swap_horizontal:
7970 case nir_intrinsic_quad_swap_vertical:
7971 case nir_intrinsic_quad_swap_diagonal:
7972 case nir_intrinsic_quad_swizzle_amd: {
7973 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7974 if (!nir_dest_is_divergent(instr->dest)) {
7975 emit_uniform_subgroup(ctx, instr, src);
7976 break;
7977 }
7978 uint16_t dpp_ctrl = 0;
7979 switch (instr->intrinsic) {
7980 case nir_intrinsic_quad_swap_horizontal:
7981 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7982 break;
7983 case nir_intrinsic_quad_swap_vertical:
7984 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7985 break;
7986 case nir_intrinsic_quad_swap_diagonal:
7987 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7988 break;
7989 case nir_intrinsic_quad_swizzle_amd:
7990 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7991 break;
7992 default:
7993 break;
7994 }
7995 if (ctx->program->chip_class < GFX8)
7996 dpp_ctrl |= (1 << 15);
7997
7998 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7999 if (instr->dest.ssa.bit_size == 1) {
8000 assert(src.regClass() == bld.lm);
8001 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
8002 if (ctx->program->chip_class >= GFX8)
8003 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
8004 else
8005 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
8006 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
8007 emit_wqm(ctx, tmp, dst);
8008 } else if (instr->dest.ssa.bit_size == 8) {
8009 Temp tmp = bld.tmp(v1);
8010 if (ctx->program->chip_class >= GFX8)
8011 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
8012 else
8013 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
8014 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
8015 } else if (instr->dest.ssa.bit_size == 16) {
8016 Temp tmp = bld.tmp(v1);
8017 if (ctx->program->chip_class >= GFX8)
8018 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
8019 else
8020 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
8021 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
8022 } else if (instr->dest.ssa.bit_size == 32) {
8023 Temp tmp;
8024 if (ctx->program->chip_class >= GFX8)
8025 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
8026 else
8027 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
8028 emit_wqm(ctx, tmp, dst);
8029 } else if (instr->dest.ssa.bit_size == 64) {
8030 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
8031 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
8032 if (ctx->program->chip_class >= GFX8) {
8033 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
8034 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
8035 } else {
8036 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
8037 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
8038 }
8039 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8040 emit_split_vector(ctx, dst, 2);
8041 } else {
8042 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8043 nir_print_instr(&instr->instr, stderr);
8044 fprintf(stderr, "\n");
8045 }
8046 break;
8047 }
8048 case nir_intrinsic_masked_swizzle_amd: {
8049 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8050 if (!nir_dest_is_divergent(instr->dest)) {
8051 emit_uniform_subgroup(ctx, instr, src);
8052 break;
8053 }
8054 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8055 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
8056 if (instr->dest.ssa.bit_size == 1) {
8057 assert(src.regClass() == bld.lm);
8058 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
8059 src = emit_masked_swizzle(ctx, bld, src, mask);
8060 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
8061 emit_wqm(ctx, tmp, dst);
8062 } else if (dst.regClass() == v1b) {
8063 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
8064 emit_extract_vector(ctx, tmp, 0, dst);
8065 } else if (dst.regClass() == v2b) {
8066 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
8067 emit_extract_vector(ctx, tmp, 0, dst);
8068 } else if (dst.regClass() == v1) {
8069 emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask), dst);
8070 } else if (dst.regClass() == v2) {
8071 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
8072 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
8073 lo = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, lo, mask));
8074 hi = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, hi, mask));
8075 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8076 emit_split_vector(ctx, dst, 2);
8077 } else {
8078 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8079 nir_print_instr(&instr->instr, stderr);
8080 fprintf(stderr, "\n");
8081 }
8082 break;
8083 }
8084 case nir_intrinsic_write_invocation_amd: {
8085 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
8086 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
8087 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
8088 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8089 if (dst.regClass() == v1) {
8090 /* src2 is ignored for writelane. RA assigns the same reg for dst */
8091 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
8092 } else if (dst.regClass() == v2) {
8093 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
8094 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
8095 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
8096 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
8097 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
8098 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
8099 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8100 emit_split_vector(ctx, dst, 2);
8101 } else {
8102 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8103 nir_print_instr(&instr->instr, stderr);
8104 fprintf(stderr, "\n");
8105 }
8106 break;
8107 }
8108 case nir_intrinsic_mbcnt_amd: {
8109 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8110 RegClass rc = RegClass(src.type(), 1);
8111 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
8112 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
8113 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8114 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
8115 emit_wqm(ctx, wqm_tmp, dst);
8116 break;
8117 }
8118 case nir_intrinsic_load_helper_invocation: {
8119 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8120 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
8121 ctx->block->kind |= block_kind_needs_lowering;
8122 ctx->program->needs_exact = true;
8123 break;
8124 }
8125 case nir_intrinsic_is_helper_invocation: {
8126 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8127 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
8128 ctx->block->kind |= block_kind_needs_lowering;
8129 ctx->program->needs_exact = true;
8130 break;
8131 }
8132 case nir_intrinsic_demote:
8133 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
8134
8135 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8136 ctx->cf_info.exec_potentially_empty_discard = true;
8137 ctx->block->kind |= block_kind_uses_demote;
8138 ctx->program->needs_exact = true;
8139 break;
8140 case nir_intrinsic_demote_if: {
8141 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8142 assert(src.regClass() == bld.lm);
8143 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
8144 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
8145
8146 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8147 ctx->cf_info.exec_potentially_empty_discard = true;
8148 ctx->block->kind |= block_kind_uses_demote;
8149 ctx->program->needs_exact = true;
8150 break;
8151 }
8152 case nir_intrinsic_first_invocation: {
8153 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
8154 get_ssa_temp(ctx, &instr->dest.ssa));
8155 break;
8156 }
8157 case nir_intrinsic_shader_clock: {
8158 aco_opcode opcode =
8159 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
8160 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
8161 bld.smem(opcode, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), memory_sync_info(0, semantic_volatile));
8162 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
8163 break;
8164 }
8165 case nir_intrinsic_load_vertex_id_zero_base: {
8166 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8167 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
8168 break;
8169 }
8170 case nir_intrinsic_load_first_vertex: {
8171 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8172 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
8173 break;
8174 }
8175 case nir_intrinsic_load_base_instance: {
8176 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8177 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
8178 break;
8179 }
8180 case nir_intrinsic_load_instance_id: {
8181 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8182 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
8183 break;
8184 }
8185 case nir_intrinsic_load_draw_id: {
8186 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8187 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8188 break;
8189 }
8190 case nir_intrinsic_load_invocation_id: {
8191 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8192
8193 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8194 if (ctx->options->chip_class >= GFX10)
8195 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8196 else
8197 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8198 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8199 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8200 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8201 } else {
8202 unreachable("Unsupported stage for load_invocation_id");
8203 }
8204
8205 break;
8206 }
8207 case nir_intrinsic_load_primitive_id: {
8208 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8209
8210 switch (ctx->shader->info.stage) {
8211 case MESA_SHADER_GEOMETRY:
8212 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8213 break;
8214 case MESA_SHADER_TESS_CTRL:
8215 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8216 break;
8217 case MESA_SHADER_TESS_EVAL:
8218 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8219 break;
8220 default:
8221 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8222 }
8223
8224 break;
8225 }
8226 case nir_intrinsic_load_patch_vertices_in: {
8227 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8228 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8229
8230 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8231 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8232 break;
8233 }
8234 case nir_intrinsic_emit_vertex_with_counter: {
8235 visit_emit_vertex_with_counter(ctx, instr);
8236 break;
8237 }
8238 case nir_intrinsic_end_primitive_with_counter: {
8239 unsigned stream = nir_intrinsic_stream_id(instr);
8240 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8241 break;
8242 }
8243 case nir_intrinsic_set_vertex_count: {
8244 /* unused, the HW keeps track of this for us */
8245 break;
8246 }
8247 default:
8248 fprintf(stderr, "Unimplemented intrinsic instr: ");
8249 nir_print_instr(&instr->instr, stderr);
8250 fprintf(stderr, "\n");
8251 abort();
8252
8253 break;
8254 }
8255 }
8256
8257
8258 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8259 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8260 enum glsl_base_type *stype)
8261 {
8262 nir_deref_instr *texture_deref_instr = NULL;
8263 nir_deref_instr *sampler_deref_instr = NULL;
8264 int plane = -1;
8265
8266 for (unsigned i = 0; i < instr->num_srcs; i++) {
8267 switch (instr->src[i].src_type) {
8268 case nir_tex_src_texture_deref:
8269 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8270 break;
8271 case nir_tex_src_sampler_deref:
8272 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8273 break;
8274 case nir_tex_src_plane:
8275 plane = nir_src_as_int(instr->src[i].src);
8276 break;
8277 default:
8278 break;
8279 }
8280 }
8281
8282 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8283
8284 if (!sampler_deref_instr)
8285 sampler_deref_instr = texture_deref_instr;
8286
8287 if (plane >= 0) {
8288 assert(instr->op != nir_texop_txf_ms &&
8289 instr->op != nir_texop_samples_identical);
8290 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8291 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8292 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8293 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8294 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8295 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8296 } else {
8297 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8298 }
8299 if (samp_ptr) {
8300 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8301
8302 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8303 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8304 Builder bld(ctx->program, ctx->block);
8305
8306 /* to avoid unnecessary moves, we split and recombine sampler and image */
8307 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8308 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8309 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8310 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8311 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8312 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8313 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8314 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8315
8316 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8317 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8318 img[0], img[1], img[2], img[3],
8319 img[4], img[5], img[6], img[7]);
8320 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8321 samp[0], samp[1], samp[2], samp[3]);
8322 }
8323 }
8324 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8325 instr->op == nir_texop_samples_identical))
8326 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8327 }
8328
8329 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8330 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8331 {
8332 Builder bld(ctx->program, ctx->block);
8333
8334 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8335 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8336 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8337
8338 Operand neg_one(0xbf800000u);
8339 Operand one(0x3f800000u);
8340 Operand two(0x40000000u);
8341 Operand four(0x40800000u);
8342
8343 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8344 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8345 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8346
8347 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8348 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8349 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8350 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);
8351
8352 // select sc
8353 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8354 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8355 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8356 one, is_ma_y);
8357 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8358
8359 // select tc
8360 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8361 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8362 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8363
8364 // select ma
8365 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8366 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8367 deriv_z, is_ma_z);
8368 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8369 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8370 }
8371
8372 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8373 {
8374 Builder bld(ctx->program, ctx->block);
8375 Temp ma, tc, sc, id;
8376
8377 if (is_array) {
8378 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8379
8380 // see comment in ac_prepare_cube_coords()
8381 if (ctx->options->chip_class <= GFX8)
8382 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8383 }
8384
8385 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8386
8387 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8388 vop3a->operands[0] = Operand(ma);
8389 vop3a->abs[0] = true;
8390 Temp invma = bld.tmp(v1);
8391 vop3a->definitions[0] = Definition(invma);
8392 ctx->block->instructions.emplace_back(std::move(vop3a));
8393
8394 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8395 if (!is_deriv)
8396 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8397
8398 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8399 if (!is_deriv)
8400 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8401
8402 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8403
8404 if (is_deriv) {
8405 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8406 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8407
8408 for (unsigned i = 0; i < 2; i++) {
8409 // see comment in ac_prepare_cube_coords()
8410 Temp deriv_ma;
8411 Temp deriv_sc, deriv_tc;
8412 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8413 &deriv_ma, &deriv_sc, &deriv_tc);
8414
8415 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8416
8417 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8418 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8419 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8420 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8421 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8422 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8423 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8424 }
8425
8426 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8427 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8428 }
8429
8430 if (is_array)
8431 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8432 coords.resize(3);
8433 coords[0] = sc;
8434 coords[1] = tc;
8435 coords[2] = id;
8436 }
8437
8438 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8439 {
8440 if (vec->parent_instr->type != nir_instr_type_alu)
8441 return;
8442 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8443 if (vec_instr->op != nir_op_vec(vec->num_components))
8444 return;
8445
8446 for (unsigned i = 0; i < vec->num_components; i++) {
8447 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8448 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8449 }
8450 }
8451
8452 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8453 {
8454 Builder bld(ctx->program, ctx->block);
8455 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8456 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8457 has_clamped_lod = false;
8458 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8459 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8460 clamped_lod = Temp();
8461 std::vector<Temp> coords;
8462 std::vector<Temp> derivs;
8463 nir_const_value *sample_index_cv = NULL;
8464 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8465 enum glsl_base_type stype;
8466 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8467
8468 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8469 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8470 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8471 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8472
8473 for (unsigned i = 0; i < instr->num_srcs; i++) {
8474 switch (instr->src[i].src_type) {
8475 case nir_tex_src_coord: {
8476 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8477 for (unsigned i = 0; i < coord.size(); i++)
8478 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8479 break;
8480 }
8481 case nir_tex_src_bias:
8482 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8483 has_bias = true;
8484 break;
8485 case nir_tex_src_lod: {
8486 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8487
8488 if (val && val->f32 <= 0.0) {
8489 level_zero = true;
8490 } else {
8491 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8492 has_lod = true;
8493 }
8494 break;
8495 }
8496 case nir_tex_src_min_lod:
8497 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8498 has_clamped_lod = true;
8499 break;
8500 case nir_tex_src_comparator:
8501 if (instr->is_shadow) {
8502 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8503 has_compare = true;
8504 }
8505 break;
8506 case nir_tex_src_offset:
8507 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8508 get_const_vec(instr->src[i].src.ssa, const_offset);
8509 has_offset = true;
8510 break;
8511 case nir_tex_src_ddx:
8512 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8513 has_ddx = true;
8514 break;
8515 case nir_tex_src_ddy:
8516 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8517 has_ddy = true;
8518 break;
8519 case nir_tex_src_ms_index:
8520 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8521 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8522 has_sample_index = true;
8523 break;
8524 case nir_tex_src_texture_offset:
8525 case nir_tex_src_sampler_offset:
8526 default:
8527 break;
8528 }
8529 }
8530
8531 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8532 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8533
8534 if (instr->op == nir_texop_texture_samples) {
8535 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8536
8537 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8538 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8539 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 */));
8540
8541 Operand default_sample = Operand(1u);
8542 if (ctx->options->robust_buffer_access) {
8543 /* Extract the second dword of the descriptor, if it's
8544 * all zero, then it's a null descriptor.
8545 */
8546 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8547 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8548 default_sample = Operand(is_non_null_descriptor);
8549 }
8550
8551 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8552 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8553 samples, default_sample, bld.scc(is_msaa));
8554 return;
8555 }
8556
8557 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8558 aco_ptr<Instruction> tmp_instr;
8559 Temp acc, pack = Temp();
8560
8561 uint32_t pack_const = 0;
8562 for (unsigned i = 0; i < offset.size(); i++) {
8563 if (!const_offset[i])
8564 continue;
8565 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8566 }
8567
8568 if (offset.type() == RegType::sgpr) {
8569 for (unsigned i = 0; i < offset.size(); i++) {
8570 if (const_offset[i])
8571 continue;
8572
8573 acc = emit_extract_vector(ctx, offset, i, s1);
8574 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8575
8576 if (i) {
8577 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8578 }
8579
8580 if (pack == Temp()) {
8581 pack = acc;
8582 } else {
8583 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8584 }
8585 }
8586
8587 if (pack_const && pack != Temp())
8588 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8589 } else {
8590 for (unsigned i = 0; i < offset.size(); i++) {
8591 if (const_offset[i])
8592 continue;
8593
8594 acc = emit_extract_vector(ctx, offset, i, v1);
8595 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8596
8597 if (i) {
8598 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8599 }
8600
8601 if (pack == Temp()) {
8602 pack = acc;
8603 } else {
8604 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8605 }
8606 }
8607
8608 if (pack_const && pack != Temp())
8609 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8610 }
8611 if (pack_const && pack == Temp())
8612 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8613 else if (pack == Temp())
8614 has_offset = false;
8615 else
8616 offset = pack;
8617 }
8618
8619 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8620 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8621
8622 /* pack derivatives */
8623 if (has_ddx || has_ddy) {
8624 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8625 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8626 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8627 derivs = {ddx, zero, ddy, zero};
8628 } else {
8629 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8630 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8631 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8632 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8633 }
8634 has_derivs = true;
8635 }
8636
8637 if (instr->coord_components > 1 &&
8638 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8639 instr->is_array &&
8640 instr->op != nir_texop_txf)
8641 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8642
8643 if (instr->coord_components > 2 &&
8644 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8645 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8646 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8647 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8648 instr->is_array &&
8649 instr->op != nir_texop_txf &&
8650 instr->op != nir_texop_txf_ms &&
8651 instr->op != nir_texop_fragment_fetch &&
8652 instr->op != nir_texop_fragment_mask_fetch)
8653 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8654
8655 if (ctx->options->chip_class == GFX9 &&
8656 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8657 instr->op != nir_texop_lod && instr->coord_components) {
8658 assert(coords.size() > 0 && coords.size() < 3);
8659
8660 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8661 Operand((uint32_t) 0) :
8662 Operand((uint32_t) 0x3f000000)));
8663 }
8664
8665 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8666
8667 if (instr->op == nir_texop_samples_identical)
8668 resource = fmask_ptr;
8669
8670 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8671 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8672 instr->op != nir_texop_txs &&
8673 instr->op != nir_texop_fragment_fetch &&
8674 instr->op != nir_texop_fragment_mask_fetch) {
8675 assert(has_sample_index);
8676 Operand op(sample_index);
8677 if (sample_index_cv)
8678 op = Operand(sample_index_cv->u32);
8679 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8680 }
8681
8682 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8683 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8684 Temp off = emit_extract_vector(ctx, offset, i, v1);
8685 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8686 }
8687 has_offset = false;
8688 }
8689
8690 /* Build tex instruction */
8691 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8692 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8693 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8694 : 0;
8695 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8696 Temp tmp_dst = dst;
8697
8698 /* gather4 selects the component by dmask and always returns vec4 */
8699 if (instr->op == nir_texop_tg4) {
8700 assert(instr->dest.ssa.num_components == 4);
8701 if (instr->is_shadow)
8702 dmask = 1;
8703 else
8704 dmask = 1 << instr->component;
8705 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8706 tmp_dst = bld.tmp(v4);
8707 } else if (instr->op == nir_texop_samples_identical) {
8708 tmp_dst = bld.tmp(v1);
8709 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8710 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8711 }
8712
8713 aco_ptr<MIMG_instruction> tex;
8714 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8715 if (!has_lod)
8716 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8717
8718 bool div_by_6 = instr->op == nir_texop_txs &&
8719 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8720 instr->is_array &&
8721 (dmask & (1 << 2));
8722 if (tmp_dst.id() == dst.id() && div_by_6)
8723 tmp_dst = bld.tmp(tmp_dst.regClass());
8724
8725 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8726 tex->operands[0] = Operand(resource);
8727 tex->operands[1] = Operand(s4); /* no sampler */
8728 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8729 if (ctx->options->chip_class == GFX9 &&
8730 instr->op == nir_texop_txs &&
8731 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8732 instr->is_array) {
8733 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8734 } else if (instr->op == nir_texop_query_levels) {
8735 tex->dmask = 1 << 3;
8736 } else {
8737 tex->dmask = dmask;
8738 }
8739 tex->da = da;
8740 tex->definitions[0] = Definition(tmp_dst);
8741 tex->dim = dim;
8742 ctx->block->instructions.emplace_back(std::move(tex));
8743
8744 if (div_by_6) {
8745 /* divide 3rd value by 6 by multiplying with magic number */
8746 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8747 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8748 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8749 assert(instr->dest.ssa.num_components == 3);
8750 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8751 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8752 emit_extract_vector(ctx, tmp_dst, 0, v1),
8753 emit_extract_vector(ctx, tmp_dst, 1, v1),
8754 by_6);
8755
8756 }
8757
8758 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8759 return;
8760 }
8761
8762 Temp tg4_compare_cube_wa64 = Temp();
8763
8764 if (tg4_integer_workarounds) {
8765 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8766 tex->operands[0] = Operand(resource);
8767 tex->operands[1] = Operand(s4); /* no sampler */
8768 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8769 tex->dim = dim;
8770 tex->dmask = 0x3;
8771 tex->da = da;
8772 Temp size = bld.tmp(v2);
8773 tex->definitions[0] = Definition(size);
8774 ctx->block->instructions.emplace_back(std::move(tex));
8775 emit_split_vector(ctx, size, size.size());
8776
8777 Temp half_texel[2];
8778 for (unsigned i = 0; i < 2; i++) {
8779 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8780 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8781 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8782 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8783 }
8784
8785 Temp new_coords[2] = {
8786 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8787 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8788 };
8789
8790 if (tg4_integer_cube_workaround) {
8791 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8792 Temp desc[resource.size()];
8793 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8794 Format::PSEUDO, 1, resource.size())};
8795 split->operands[0] = Operand(resource);
8796 for (unsigned i = 0; i < resource.size(); i++) {
8797 desc[i] = bld.tmp(s1);
8798 split->definitions[i] = Definition(desc[i]);
8799 }
8800 ctx->block->instructions.emplace_back(std::move(split));
8801
8802 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8803 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8804 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8805
8806 Temp nfmt;
8807 if (stype == GLSL_TYPE_UINT) {
8808 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8809 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8810 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8811 bld.scc(compare_cube_wa));
8812 } else {
8813 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8814 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8815 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8816 bld.scc(compare_cube_wa));
8817 }
8818 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8819 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8820
8821 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8822
8823 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8824 Operand((uint32_t)C_008F14_NUM_FORMAT));
8825 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8826
8827 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8828 Format::PSEUDO, resource.size(), 1)};
8829 for (unsigned i = 0; i < resource.size(); i++)
8830 vec->operands[i] = Operand(desc[i]);
8831 resource = bld.tmp(resource.regClass());
8832 vec->definitions[0] = Definition(resource);
8833 ctx->block->instructions.emplace_back(std::move(vec));
8834
8835 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8836 new_coords[0], coords[0], tg4_compare_cube_wa64);
8837 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8838 new_coords[1], coords[1], tg4_compare_cube_wa64);
8839 }
8840 coords[0] = new_coords[0];
8841 coords[1] = new_coords[1];
8842 }
8843
8844 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8845 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8846
8847 assert(coords.size() == 1);
8848 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8849 aco_opcode op;
8850 switch (last_bit) {
8851 case 1:
8852 op = aco_opcode::buffer_load_format_x; break;
8853 case 2:
8854 op = aco_opcode::buffer_load_format_xy; break;
8855 case 3:
8856 op = aco_opcode::buffer_load_format_xyz; break;
8857 case 4:
8858 op = aco_opcode::buffer_load_format_xyzw; break;
8859 default:
8860 unreachable("Tex instruction loads more than 4 components.");
8861 }
8862
8863 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8864 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8865 tmp_dst = dst;
8866 else
8867 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8868
8869 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8870 mubuf->operands[0] = Operand(resource);
8871 mubuf->operands[1] = Operand(coords[0]);
8872 mubuf->operands[2] = Operand((uint32_t) 0);
8873 mubuf->definitions[0] = Definition(tmp_dst);
8874 mubuf->idxen = true;
8875 ctx->block->instructions.emplace_back(std::move(mubuf));
8876
8877 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8878 return;
8879 }
8880
8881 /* gather MIMG address components */
8882 std::vector<Temp> args;
8883 if (has_offset)
8884 args.emplace_back(offset);
8885 if (has_bias)
8886 args.emplace_back(bias);
8887 if (has_compare)
8888 args.emplace_back(compare);
8889 if (has_derivs)
8890 args.insert(args.end(), derivs.begin(), derivs.end());
8891
8892 args.insert(args.end(), coords.begin(), coords.end());
8893 if (has_sample_index)
8894 args.emplace_back(sample_index);
8895 if (has_lod)
8896 args.emplace_back(lod);
8897 if (has_clamped_lod)
8898 args.emplace_back(clamped_lod);
8899
8900 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8901 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8902 vec->definitions[0] = Definition(arg);
8903 for (unsigned i = 0; i < args.size(); i++)
8904 vec->operands[i] = Operand(args[i]);
8905 ctx->block->instructions.emplace_back(std::move(vec));
8906
8907
8908 if (instr->op == nir_texop_txf ||
8909 instr->op == nir_texop_txf_ms ||
8910 instr->op == nir_texop_samples_identical ||
8911 instr->op == nir_texop_fragment_fetch ||
8912 instr->op == nir_texop_fragment_mask_fetch) {
8913 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;
8914 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8915 tex->operands[0] = Operand(resource);
8916 tex->operands[1] = Operand(s4); /* no sampler */
8917 tex->operands[2] = Operand(arg);
8918 tex->dim = dim;
8919 tex->dmask = dmask;
8920 tex->unrm = true;
8921 tex->da = da;
8922 tex->definitions[0] = Definition(tmp_dst);
8923 ctx->block->instructions.emplace_back(std::move(tex));
8924
8925 if (instr->op == nir_texop_samples_identical) {
8926 assert(dmask == 1 && dst.regClass() == v1);
8927 assert(dst.id() != tmp_dst.id());
8928
8929 Temp tmp = bld.tmp(bld.lm);
8930 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8931 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8932
8933 } else {
8934 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8935 }
8936 return;
8937 }
8938
8939 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8940 aco_opcode opcode = aco_opcode::image_sample;
8941 if (has_offset) { /* image_sample_*_o */
8942 if (has_clamped_lod) {
8943 if (has_compare) {
8944 opcode = aco_opcode::image_sample_c_cl_o;
8945 if (has_derivs)
8946 opcode = aco_opcode::image_sample_c_d_cl_o;
8947 if (has_bias)
8948 opcode = aco_opcode::image_sample_c_b_cl_o;
8949 } else {
8950 opcode = aco_opcode::image_sample_cl_o;
8951 if (has_derivs)
8952 opcode = aco_opcode::image_sample_d_cl_o;
8953 if (has_bias)
8954 opcode = aco_opcode::image_sample_b_cl_o;
8955 }
8956 } else if (has_compare) {
8957 opcode = aco_opcode::image_sample_c_o;
8958 if (has_derivs)
8959 opcode = aco_opcode::image_sample_c_d_o;
8960 if (has_bias)
8961 opcode = aco_opcode::image_sample_c_b_o;
8962 if (level_zero)
8963 opcode = aco_opcode::image_sample_c_lz_o;
8964 if (has_lod)
8965 opcode = aco_opcode::image_sample_c_l_o;
8966 } else {
8967 opcode = aco_opcode::image_sample_o;
8968 if (has_derivs)
8969 opcode = aco_opcode::image_sample_d_o;
8970 if (has_bias)
8971 opcode = aco_opcode::image_sample_b_o;
8972 if (level_zero)
8973 opcode = aco_opcode::image_sample_lz_o;
8974 if (has_lod)
8975 opcode = aco_opcode::image_sample_l_o;
8976 }
8977 } else if (has_clamped_lod) { /* image_sample_*_cl */
8978 if (has_compare) {
8979 opcode = aco_opcode::image_sample_c_cl;
8980 if (has_derivs)
8981 opcode = aco_opcode::image_sample_c_d_cl;
8982 if (has_bias)
8983 opcode = aco_opcode::image_sample_c_b_cl;
8984 } else {
8985 opcode = aco_opcode::image_sample_cl;
8986 if (has_derivs)
8987 opcode = aco_opcode::image_sample_d_cl;
8988 if (has_bias)
8989 opcode = aco_opcode::image_sample_b_cl;
8990 }
8991 } else { /* no offset */
8992 if (has_compare) {
8993 opcode = aco_opcode::image_sample_c;
8994 if (has_derivs)
8995 opcode = aco_opcode::image_sample_c_d;
8996 if (has_bias)
8997 opcode = aco_opcode::image_sample_c_b;
8998 if (level_zero)
8999 opcode = aco_opcode::image_sample_c_lz;
9000 if (has_lod)
9001 opcode = aco_opcode::image_sample_c_l;
9002 } else {
9003 opcode = aco_opcode::image_sample;
9004 if (has_derivs)
9005 opcode = aco_opcode::image_sample_d;
9006 if (has_bias)
9007 opcode = aco_opcode::image_sample_b;
9008 if (level_zero)
9009 opcode = aco_opcode::image_sample_lz;
9010 if (has_lod)
9011 opcode = aco_opcode::image_sample_l;
9012 }
9013 }
9014
9015 if (instr->op == nir_texop_tg4) {
9016 if (has_offset) { /* image_gather4_*_o */
9017 if (has_compare) {
9018 opcode = aco_opcode::image_gather4_c_lz_o;
9019 if (has_lod)
9020 opcode = aco_opcode::image_gather4_c_l_o;
9021 if (has_bias)
9022 opcode = aco_opcode::image_gather4_c_b_o;
9023 } else {
9024 opcode = aco_opcode::image_gather4_lz_o;
9025 if (has_lod)
9026 opcode = aco_opcode::image_gather4_l_o;
9027 if (has_bias)
9028 opcode = aco_opcode::image_gather4_b_o;
9029 }
9030 } else {
9031 if (has_compare) {
9032 opcode = aco_opcode::image_gather4_c_lz;
9033 if (has_lod)
9034 opcode = aco_opcode::image_gather4_c_l;
9035 if (has_bias)
9036 opcode = aco_opcode::image_gather4_c_b;
9037 } else {
9038 opcode = aco_opcode::image_gather4_lz;
9039 if (has_lod)
9040 opcode = aco_opcode::image_gather4_l;
9041 if (has_bias)
9042 opcode = aco_opcode::image_gather4_b;
9043 }
9044 }
9045 } else if (instr->op == nir_texop_lod) {
9046 opcode = aco_opcode::image_get_lod;
9047 }
9048
9049 /* we don't need the bias, sample index, compare value or offset to be
9050 * computed in WQM but if the p_create_vector copies the coordinates, then it
9051 * needs to be in WQM */
9052 if (ctx->stage == fragment_fs &&
9053 !has_derivs && !has_lod && !level_zero &&
9054 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
9055 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
9056 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
9057
9058 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
9059 tex->operands[0] = Operand(resource);
9060 tex->operands[1] = Operand(sampler);
9061 tex->operands[2] = Operand(arg);
9062 tex->dim = dim;
9063 tex->dmask = dmask;
9064 tex->da = da;
9065 tex->definitions[0] = Definition(tmp_dst);
9066 ctx->block->instructions.emplace_back(std::move(tex));
9067
9068 if (tg4_integer_cube_workaround) {
9069 assert(tmp_dst.id() != dst.id());
9070 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
9071
9072 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
9073 Temp val[4];
9074 for (unsigned i = 0; i < dst.size(); i++) {
9075 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
9076 Temp cvt_val;
9077 if (stype == GLSL_TYPE_UINT)
9078 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
9079 else
9080 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
9081 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
9082 }
9083 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
9084 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
9085 val[0], val[1], val[2], val[3]);
9086 }
9087 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
9088 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
9089
9090 }
9091
9092
9093 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa, RegClass rc, bool logical)
9094 {
9095 Temp tmp = get_ssa_temp(ctx, ssa);
9096 if (ssa->parent_instr->type == nir_instr_type_ssa_undef) {
9097 return Operand(rc);
9098 } else if (logical && ssa->bit_size == 1 && ssa->parent_instr->type == nir_instr_type_load_const) {
9099 if (ctx->program->wave_size == 64)
9100 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT64_MAX : 0u);
9101 else
9102 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT32_MAX : 0u);
9103 } else {
9104 return Operand(tmp);
9105 }
9106 }
9107
9108 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
9109 {
9110 aco_ptr<Pseudo_instruction> phi;
9111 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
9112 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
9113
9114 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
9115 logical |= ctx->block->kind & block_kind_merge;
9116 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
9117
9118 /* we want a sorted list of sources, since the predecessor list is also sorted */
9119 std::map<unsigned, nir_ssa_def*> phi_src;
9120 nir_foreach_phi_src(src, instr)
9121 phi_src[src->pred->index] = src->src.ssa;
9122
9123 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
9124 unsigned num_operands = 0;
9125 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
9126 unsigned num_defined = 0;
9127 unsigned cur_pred_idx = 0;
9128 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
9129 if (cur_pred_idx < preds.size()) {
9130 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
9131 unsigned block = ctx->cf_info.nir_to_aco[src.first];
9132 unsigned skipped = 0;
9133 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
9134 skipped++;
9135 if (cur_pred_idx + skipped < preds.size()) {
9136 for (unsigned i = 0; i < skipped; i++)
9137 operands[num_operands++] = Operand(dst.regClass());
9138 cur_pred_idx += skipped;
9139 } else {
9140 continue;
9141 }
9142 }
9143 /* Handle missing predecessors at the end. This shouldn't happen with loop
9144 * headers and we can't ignore these sources for loop header phis. */
9145 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
9146 continue;
9147 cur_pred_idx++;
9148 Operand op = get_phi_operand(ctx, src.second, dst.regClass(), logical);
9149 operands[num_operands++] = op;
9150 num_defined += !op.isUndefined();
9151 }
9152 /* handle block_kind_continue_or_break at loop exit blocks */
9153 while (cur_pred_idx++ < preds.size())
9154 operands[num_operands++] = Operand(dst.regClass());
9155
9156 /* If the loop ends with a break, still add a linear continue edge in case
9157 * that break is divergent or continue_or_break is used. We'll either remove
9158 * this operand later in visit_loop() if it's not necessary or replace the
9159 * undef with something correct. */
9160 if (!logical && ctx->block->kind & block_kind_loop_header) {
9161 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
9162 nir_block *last = nir_loop_last_block(loop);
9163 if (last->successors[0] != instr->instr.block)
9164 operands[num_operands++] = Operand(RegClass());
9165 }
9166
9167 if (num_defined == 0) {
9168 Builder bld(ctx->program, ctx->block);
9169 if (dst.regClass() == s1) {
9170 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
9171 } else if (dst.regClass() == v1) {
9172 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
9173 } else {
9174 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9175 for (unsigned i = 0; i < dst.size(); i++)
9176 vec->operands[i] = Operand(0u);
9177 vec->definitions[0] = Definition(dst);
9178 ctx->block->instructions.emplace_back(std::move(vec));
9179 }
9180 return;
9181 }
9182
9183 /* we can use a linear phi in some cases if one src is undef */
9184 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9185 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9186
9187 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9188 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9189 assert(invert->kind & block_kind_invert);
9190
9191 unsigned then_block = invert->linear_preds[0];
9192
9193 Block* insert_block = NULL;
9194 for (unsigned i = 0; i < num_operands; i++) {
9195 Operand op = operands[i];
9196 if (op.isUndefined())
9197 continue;
9198 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9199 phi->operands[0] = op;
9200 break;
9201 }
9202 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9203 phi->operands[1] = Operand(dst.regClass());
9204 phi->definitions[0] = Definition(dst);
9205 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9206 return;
9207 }
9208
9209 /* try to scalarize vector phis */
9210 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9211 // TODO: scalarize linear phis on divergent ifs
9212 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9213 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9214 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9215 Operand src = operands[i];
9216 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9217 can_scalarize = false;
9218 }
9219 if (can_scalarize) {
9220 unsigned num_components = instr->dest.ssa.num_components;
9221 assert(dst.size() % num_components == 0);
9222 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9223
9224 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9225 for (unsigned k = 0; k < num_components; k++) {
9226 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9227 for (unsigned i = 0; i < num_operands; i++) {
9228 Operand src = operands[i];
9229 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9230 }
9231 Temp phi_dst = {ctx->program->allocateId(), rc};
9232 phi->definitions[0] = Definition(phi_dst);
9233 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9234 new_vec[k] = phi_dst;
9235 vec->operands[k] = Operand(phi_dst);
9236 }
9237 vec->definitions[0] = Definition(dst);
9238 ctx->block->instructions.emplace_back(std::move(vec));
9239 ctx->allocated_vec.emplace(dst.id(), new_vec);
9240 return;
9241 }
9242 }
9243
9244 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9245 for (unsigned i = 0; i < num_operands; i++)
9246 phi->operands[i] = operands[i];
9247 phi->definitions[0] = Definition(dst);
9248 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9249 }
9250
9251
9252 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9253 {
9254 Temp dst = get_ssa_temp(ctx, &instr->def);
9255
9256 assert(dst.type() == RegType::sgpr);
9257
9258 if (dst.size() == 1) {
9259 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9260 } else {
9261 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9262 for (unsigned i = 0; i < dst.size(); i++)
9263 vec->operands[i] = Operand(0u);
9264 vec->definitions[0] = Definition(dst);
9265 ctx->block->instructions.emplace_back(std::move(vec));
9266 }
9267 }
9268
9269 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9270 {
9271 Builder bld(ctx->program, ctx->block);
9272 Block *logical_target;
9273 append_logical_end(ctx->block);
9274 unsigned idx = ctx->block->index;
9275
9276 switch (instr->type) {
9277 case nir_jump_break:
9278 logical_target = ctx->cf_info.parent_loop.exit;
9279 add_logical_edge(idx, logical_target);
9280 ctx->block->kind |= block_kind_break;
9281
9282 if (!ctx->cf_info.parent_if.is_divergent &&
9283 !ctx->cf_info.parent_loop.has_divergent_continue) {
9284 /* uniform break - directly jump out of the loop */
9285 ctx->block->kind |= block_kind_uniform;
9286 ctx->cf_info.has_branch = true;
9287 bld.branch(aco_opcode::p_branch);
9288 add_linear_edge(idx, logical_target);
9289 return;
9290 }
9291 ctx->cf_info.parent_loop.has_divergent_branch = true;
9292 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9293 break;
9294 case nir_jump_continue:
9295 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9296 add_logical_edge(idx, logical_target);
9297 ctx->block->kind |= block_kind_continue;
9298
9299 if (ctx->cf_info.parent_if.is_divergent) {
9300 /* for potential uniform breaks after this continue,
9301 we must ensure that they are handled correctly */
9302 ctx->cf_info.parent_loop.has_divergent_continue = true;
9303 ctx->cf_info.parent_loop.has_divergent_branch = true;
9304 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9305 } else {
9306 /* uniform continue - directly jump to the loop header */
9307 ctx->block->kind |= block_kind_uniform;
9308 ctx->cf_info.has_branch = true;
9309 bld.branch(aco_opcode::p_branch);
9310 add_linear_edge(idx, logical_target);
9311 return;
9312 }
9313 break;
9314 default:
9315 fprintf(stderr, "Unknown NIR jump instr: ");
9316 nir_print_instr(&instr->instr, stderr);
9317 fprintf(stderr, "\n");
9318 abort();
9319 }
9320
9321 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9322 ctx->cf_info.exec_potentially_empty_break = true;
9323 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9324 }
9325
9326 /* remove critical edges from linear CFG */
9327 bld.branch(aco_opcode::p_branch);
9328 Block* break_block = ctx->program->create_and_insert_block();
9329 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9330 break_block->kind |= block_kind_uniform;
9331 add_linear_edge(idx, break_block);
9332 /* the loop_header pointer might be invalidated by this point */
9333 if (instr->type == nir_jump_continue)
9334 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9335 add_linear_edge(break_block->index, logical_target);
9336 bld.reset(break_block);
9337 bld.branch(aco_opcode::p_branch);
9338
9339 Block* continue_block = ctx->program->create_and_insert_block();
9340 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9341 add_linear_edge(idx, continue_block);
9342 append_logical_start(continue_block);
9343 ctx->block = continue_block;
9344 return;
9345 }
9346
9347 void visit_block(isel_context *ctx, nir_block *block)
9348 {
9349 nir_foreach_instr(instr, block) {
9350 switch (instr->type) {
9351 case nir_instr_type_alu:
9352 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9353 break;
9354 case nir_instr_type_load_const:
9355 visit_load_const(ctx, nir_instr_as_load_const(instr));
9356 break;
9357 case nir_instr_type_intrinsic:
9358 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9359 break;
9360 case nir_instr_type_tex:
9361 visit_tex(ctx, nir_instr_as_tex(instr));
9362 break;
9363 case nir_instr_type_phi:
9364 visit_phi(ctx, nir_instr_as_phi(instr));
9365 break;
9366 case nir_instr_type_ssa_undef:
9367 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9368 break;
9369 case nir_instr_type_deref:
9370 break;
9371 case nir_instr_type_jump:
9372 visit_jump(ctx, nir_instr_as_jump(instr));
9373 break;
9374 default:
9375 fprintf(stderr, "Unknown NIR instr type: ");
9376 nir_print_instr(instr, stderr);
9377 fprintf(stderr, "\n");
9378 //abort();
9379 }
9380 }
9381
9382 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9383 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9384 }
9385
9386
9387
9388 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9389 aco_ptr<Instruction>& header_phi, Operand *vals)
9390 {
9391 vals[0] = Operand(header_phi->definitions[0].getTemp());
9392 RegClass rc = vals[0].regClass();
9393
9394 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9395
9396 unsigned next_pred = 1;
9397
9398 for (unsigned idx = first + 1; idx <= last; idx++) {
9399 Block& block = ctx->program->blocks[idx];
9400 if (block.loop_nest_depth != loop_nest_depth) {
9401 vals[idx - first] = vals[idx - 1 - first];
9402 continue;
9403 }
9404
9405 if (block.kind & block_kind_continue) {
9406 vals[idx - first] = header_phi->operands[next_pred];
9407 next_pred++;
9408 continue;
9409 }
9410
9411 bool all_same = true;
9412 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9413 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9414
9415 Operand val;
9416 if (all_same) {
9417 val = vals[block.linear_preds[0] - first];
9418 } else {
9419 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9420 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9421 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9422 phi->operands[i] = vals[block.linear_preds[i] - first];
9423 val = Operand(Temp(ctx->program->allocateId(), rc));
9424 phi->definitions[0] = Definition(val.getTemp());
9425 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9426 }
9427 vals[idx - first] = val;
9428 }
9429
9430 return vals[last - first];
9431 }
9432
9433 static void visit_loop(isel_context *ctx, nir_loop *loop)
9434 {
9435 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9436 append_logical_end(ctx->block);
9437 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9438 Builder bld(ctx->program, ctx->block);
9439 bld.branch(aco_opcode::p_branch);
9440 unsigned loop_preheader_idx = ctx->block->index;
9441
9442 Block loop_exit = Block();
9443 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9444 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9445
9446 Block* loop_header = ctx->program->create_and_insert_block();
9447 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9448 loop_header->kind |= block_kind_loop_header;
9449 add_edge(loop_preheader_idx, loop_header);
9450 ctx->block = loop_header;
9451
9452 /* emit loop body */
9453 unsigned loop_header_idx = loop_header->index;
9454 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9455 append_logical_start(ctx->block);
9456 bool unreachable = visit_cf_list(ctx, &loop->body);
9457
9458 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9459 if (!ctx->cf_info.has_branch) {
9460 append_logical_end(ctx->block);
9461 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9462 /* Discards can result in code running with an empty exec mask.
9463 * This would result in divergent breaks not ever being taken. As a
9464 * workaround, break the loop when the loop mask is empty instead of
9465 * always continuing. */
9466 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9467 unsigned block_idx = ctx->block->index;
9468
9469 /* create helper blocks to avoid critical edges */
9470 Block *break_block = ctx->program->create_and_insert_block();
9471 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9472 break_block->kind = block_kind_uniform;
9473 bld.reset(break_block);
9474 bld.branch(aco_opcode::p_branch);
9475 add_linear_edge(block_idx, break_block);
9476 add_linear_edge(break_block->index, &loop_exit);
9477
9478 Block *continue_block = ctx->program->create_and_insert_block();
9479 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9480 continue_block->kind = block_kind_uniform;
9481 bld.reset(continue_block);
9482 bld.branch(aco_opcode::p_branch);
9483 add_linear_edge(block_idx, continue_block);
9484 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9485
9486 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9487 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9488 ctx->block = &ctx->program->blocks[block_idx];
9489 } else {
9490 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9491 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9492 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9493 else
9494 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9495 }
9496
9497 bld.reset(ctx->block);
9498 bld.branch(aco_opcode::p_branch);
9499 }
9500
9501 /* Fixup phis in loop header from unreachable blocks.
9502 * has_branch/has_divergent_branch also indicates if the loop ends with a
9503 * break/continue instruction, but we don't emit those if unreachable=true */
9504 if (unreachable) {
9505 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9506 bool linear = ctx->cf_info.has_branch;
9507 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9508 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9509 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9510 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9511 /* the last operand should be the one that needs to be removed */
9512 instr->operands.pop_back();
9513 } else if (!is_phi(instr)) {
9514 break;
9515 }
9516 }
9517 }
9518
9519 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9520 * and the previous one shouldn't both happen at once because a break in the
9521 * merge block would get CSE'd */
9522 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9523 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9524 Operand vals[num_vals];
9525 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9526 if (instr->opcode == aco_opcode::p_linear_phi) {
9527 if (ctx->cf_info.has_branch)
9528 instr->operands.pop_back();
9529 else
9530 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9531 } else if (!is_phi(instr)) {
9532 break;
9533 }
9534 }
9535 }
9536
9537 ctx->cf_info.has_branch = false;
9538
9539 // TODO: if the loop has not a single exit, we must add one °°
9540 /* emit loop successor block */
9541 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9542 append_logical_start(ctx->block);
9543
9544 #if 0
9545 // TODO: check if it is beneficial to not branch on continues
9546 /* trim linear phis in loop header */
9547 for (auto&& instr : loop_entry->instructions) {
9548 if (instr->opcode == aco_opcode::p_linear_phi) {
9549 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9550 new_phi->definitions[0] = instr->definitions[0];
9551 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9552 new_phi->operands[i] = instr->operands[i];
9553 /* check that the remaining operands are all the same */
9554 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9555 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9556 instr.swap(new_phi);
9557 } else if (instr->opcode == aco_opcode::p_phi) {
9558 continue;
9559 } else {
9560 break;
9561 }
9562 }
9563 #endif
9564 }
9565
9566 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9567 {
9568 ic->cond = cond;
9569
9570 append_logical_end(ctx->block);
9571 ctx->block->kind |= block_kind_branch;
9572
9573 /* branch to linear then block */
9574 assert(cond.regClass() == ctx->program->lane_mask);
9575 aco_ptr<Pseudo_branch_instruction> branch;
9576 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9577 branch->operands[0] = Operand(cond);
9578 ctx->block->instructions.push_back(std::move(branch));
9579
9580 ic->BB_if_idx = ctx->block->index;
9581 ic->BB_invert = Block();
9582 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9583 /* Invert blocks are intentionally not marked as top level because they
9584 * are not part of the logical cfg. */
9585 ic->BB_invert.kind |= block_kind_invert;
9586 ic->BB_endif = Block();
9587 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9588 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9589
9590 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9591 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9592 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9593 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9594 ctx->cf_info.parent_if.is_divergent = true;
9595
9596 /* divergent branches use cbranch_execz */
9597 ctx->cf_info.exec_potentially_empty_discard = false;
9598 ctx->cf_info.exec_potentially_empty_break = false;
9599 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9600
9601 /** emit logical then block */
9602 Block* BB_then_logical = ctx->program->create_and_insert_block();
9603 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9604 add_edge(ic->BB_if_idx, BB_then_logical);
9605 ctx->block = BB_then_logical;
9606 append_logical_start(BB_then_logical);
9607 }
9608
9609 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9610 {
9611 Block *BB_then_logical = ctx->block;
9612 append_logical_end(BB_then_logical);
9613 /* branch from logical then block to invert block */
9614 aco_ptr<Pseudo_branch_instruction> branch;
9615 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9616 BB_then_logical->instructions.emplace_back(std::move(branch));
9617 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9618 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9619 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9620 BB_then_logical->kind |= block_kind_uniform;
9621 assert(!ctx->cf_info.has_branch);
9622 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9623 ctx->cf_info.parent_loop.has_divergent_branch = false;
9624
9625 /** emit linear then block */
9626 Block* BB_then_linear = ctx->program->create_and_insert_block();
9627 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9628 BB_then_linear->kind |= block_kind_uniform;
9629 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9630 /* branch from linear then block to invert block */
9631 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9632 BB_then_linear->instructions.emplace_back(std::move(branch));
9633 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9634
9635 /** emit invert merge block */
9636 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9637 ic->invert_idx = ctx->block->index;
9638
9639 /* branch to linear else block (skip else) */
9640 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9641 branch->operands[0] = Operand(ic->cond);
9642 ctx->block->instructions.push_back(std::move(branch));
9643
9644 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9645 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9646 ic->exec_potentially_empty_break_depth_old =
9647 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9648 /* divergent branches use cbranch_execz */
9649 ctx->cf_info.exec_potentially_empty_discard = false;
9650 ctx->cf_info.exec_potentially_empty_break = false;
9651 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9652
9653 /** emit logical else block */
9654 Block* BB_else_logical = ctx->program->create_and_insert_block();
9655 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9656 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9657 add_linear_edge(ic->invert_idx, BB_else_logical);
9658 ctx->block = BB_else_logical;
9659 append_logical_start(BB_else_logical);
9660 }
9661
9662 static void end_divergent_if(isel_context *ctx, if_context *ic)
9663 {
9664 Block *BB_else_logical = ctx->block;
9665 append_logical_end(BB_else_logical);
9666
9667 /* branch from logical else block to endif block */
9668 aco_ptr<Pseudo_branch_instruction> branch;
9669 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9670 BB_else_logical->instructions.emplace_back(std::move(branch));
9671 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9672 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9673 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9674 BB_else_logical->kind |= block_kind_uniform;
9675
9676 assert(!ctx->cf_info.has_branch);
9677 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9678
9679
9680 /** emit linear else block */
9681 Block* BB_else_linear = ctx->program->create_and_insert_block();
9682 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9683 BB_else_linear->kind |= block_kind_uniform;
9684 add_linear_edge(ic->invert_idx, BB_else_linear);
9685
9686 /* branch from linear else block to endif block */
9687 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9688 BB_else_linear->instructions.emplace_back(std::move(branch));
9689 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9690
9691
9692 /** emit endif merge block */
9693 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9694 append_logical_start(ctx->block);
9695
9696
9697 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9698 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9699 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9700 ctx->cf_info.exec_potentially_empty_break_depth =
9701 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9702 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9703 !ctx->cf_info.parent_if.is_divergent) {
9704 ctx->cf_info.exec_potentially_empty_break = false;
9705 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9706 }
9707 /* uniform control flow never has an empty exec-mask */
9708 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9709 ctx->cf_info.exec_potentially_empty_discard = false;
9710 ctx->cf_info.exec_potentially_empty_break = false;
9711 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9712 }
9713 }
9714
9715 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9716 {
9717 assert(cond.regClass() == s1);
9718
9719 append_logical_end(ctx->block);
9720 ctx->block->kind |= block_kind_uniform;
9721
9722 aco_ptr<Pseudo_branch_instruction> branch;
9723 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9724 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9725 branch->operands[0] = Operand(cond);
9726 branch->operands[0].setFixed(scc);
9727 ctx->block->instructions.emplace_back(std::move(branch));
9728
9729 ic->BB_if_idx = ctx->block->index;
9730 ic->BB_endif = Block();
9731 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9732 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9733
9734 ctx->cf_info.has_branch = false;
9735 ctx->cf_info.parent_loop.has_divergent_branch = false;
9736
9737 /** emit then block */
9738 Block* BB_then = ctx->program->create_and_insert_block();
9739 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9740 add_edge(ic->BB_if_idx, BB_then);
9741 append_logical_start(BB_then);
9742 ctx->block = BB_then;
9743 }
9744
9745 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9746 {
9747 Block *BB_then = ctx->block;
9748
9749 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9750 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9751
9752 if (!ic->uniform_has_then_branch) {
9753 append_logical_end(BB_then);
9754 /* branch from then block to endif block */
9755 aco_ptr<Pseudo_branch_instruction> branch;
9756 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9757 BB_then->instructions.emplace_back(std::move(branch));
9758 add_linear_edge(BB_then->index, &ic->BB_endif);
9759 if (!ic->then_branch_divergent)
9760 add_logical_edge(BB_then->index, &ic->BB_endif);
9761 BB_then->kind |= block_kind_uniform;
9762 }
9763
9764 ctx->cf_info.has_branch = false;
9765 ctx->cf_info.parent_loop.has_divergent_branch = false;
9766
9767 /** emit else block */
9768 Block* BB_else = ctx->program->create_and_insert_block();
9769 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9770 add_edge(ic->BB_if_idx, BB_else);
9771 append_logical_start(BB_else);
9772 ctx->block = BB_else;
9773 }
9774
9775 static void end_uniform_if(isel_context *ctx, if_context *ic)
9776 {
9777 Block *BB_else = ctx->block;
9778
9779 if (!ctx->cf_info.has_branch) {
9780 append_logical_end(BB_else);
9781 /* branch from then block to endif block */
9782 aco_ptr<Pseudo_branch_instruction> branch;
9783 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9784 BB_else->instructions.emplace_back(std::move(branch));
9785 add_linear_edge(BB_else->index, &ic->BB_endif);
9786 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9787 add_logical_edge(BB_else->index, &ic->BB_endif);
9788 BB_else->kind |= block_kind_uniform;
9789 }
9790
9791 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9792 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9793
9794 /** emit endif merge block */
9795 if (!ctx->cf_info.has_branch) {
9796 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9797 append_logical_start(ctx->block);
9798 }
9799 }
9800
9801 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9802 {
9803 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9804 Builder bld(ctx->program, ctx->block);
9805 aco_ptr<Pseudo_branch_instruction> branch;
9806 if_context ic;
9807
9808 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9809 /**
9810 * Uniform conditionals are represented in the following way*) :
9811 *
9812 * The linear and logical CFG:
9813 * BB_IF
9814 * / \
9815 * BB_THEN (logical) BB_ELSE (logical)
9816 * \ /
9817 * BB_ENDIF
9818 *
9819 * *) Exceptions may be due to break and continue statements within loops
9820 * If a break/continue happens within uniform control flow, it branches
9821 * to the loop exit/entry block. Otherwise, it branches to the next
9822 * merge block.
9823 **/
9824
9825 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9826 assert(cond.regClass() == ctx->program->lane_mask);
9827 cond = bool_to_scalar_condition(ctx, cond);
9828
9829 begin_uniform_if_then(ctx, &ic, cond);
9830 visit_cf_list(ctx, &if_stmt->then_list);
9831
9832 begin_uniform_if_else(ctx, &ic);
9833 visit_cf_list(ctx, &if_stmt->else_list);
9834
9835 end_uniform_if(ctx, &ic);
9836 } else { /* non-uniform condition */
9837 /**
9838 * To maintain a logical and linear CFG without critical edges,
9839 * non-uniform conditionals are represented in the following way*) :
9840 *
9841 * The linear CFG:
9842 * BB_IF
9843 * / \
9844 * BB_THEN (logical) BB_THEN (linear)
9845 * \ /
9846 * BB_INVERT (linear)
9847 * / \
9848 * BB_ELSE (logical) BB_ELSE (linear)
9849 * \ /
9850 * BB_ENDIF
9851 *
9852 * The logical CFG:
9853 * BB_IF
9854 * / \
9855 * BB_THEN (logical) BB_ELSE (logical)
9856 * \ /
9857 * BB_ENDIF
9858 *
9859 * *) Exceptions may be due to break and continue statements within loops
9860 **/
9861
9862 begin_divergent_if_then(ctx, &ic, cond);
9863 visit_cf_list(ctx, &if_stmt->then_list);
9864
9865 begin_divergent_if_else(ctx, &ic);
9866 visit_cf_list(ctx, &if_stmt->else_list);
9867
9868 end_divergent_if(ctx, &ic);
9869 }
9870
9871 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9872 }
9873
9874 static bool visit_cf_list(isel_context *ctx,
9875 struct exec_list *list)
9876 {
9877 foreach_list_typed(nir_cf_node, node, node, list) {
9878 switch (node->type) {
9879 case nir_cf_node_block:
9880 visit_block(ctx, nir_cf_node_as_block(node));
9881 break;
9882 case nir_cf_node_if:
9883 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9884 return true;
9885 break;
9886 case nir_cf_node_loop:
9887 visit_loop(ctx, nir_cf_node_as_loop(node));
9888 break;
9889 default:
9890 unreachable("unimplemented cf list type");
9891 }
9892 }
9893 return false;
9894 }
9895
9896 static void create_null_export(isel_context *ctx)
9897 {
9898 /* Some shader stages always need to have exports.
9899 * So when there is none, we need to add a null export.
9900 */
9901
9902 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9903 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9904 Builder bld(ctx->program, ctx->block);
9905 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9906 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9907 }
9908
9909 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9910 {
9911 assert(ctx->stage == vertex_vs ||
9912 ctx->stage == tess_eval_vs ||
9913 ctx->stage == gs_copy_vs ||
9914 ctx->stage == ngg_vertex_gs ||
9915 ctx->stage == ngg_tess_eval_gs);
9916
9917 int offset = (ctx->stage & sw_tes)
9918 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9919 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9920 uint64_t mask = ctx->outputs.mask[slot];
9921 if (!is_pos && !mask)
9922 return false;
9923 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9924 return false;
9925 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9926 exp->enabled_mask = mask;
9927 for (unsigned i = 0; i < 4; ++i) {
9928 if (mask & (1 << i))
9929 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9930 else
9931 exp->operands[i] = Operand(v1);
9932 }
9933 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9934 * Setting valid_mask=1 prevents it and has no other effect.
9935 */
9936 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9937 exp->done = false;
9938 exp->compressed = false;
9939 if (is_pos)
9940 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9941 else
9942 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9943 ctx->block->instructions.emplace_back(std::move(exp));
9944
9945 return true;
9946 }
9947
9948 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9949 {
9950 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9951 exp->enabled_mask = 0;
9952 for (unsigned i = 0; i < 4; ++i)
9953 exp->operands[i] = Operand(v1);
9954 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9955 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9956 exp->enabled_mask |= 0x1;
9957 }
9958 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9959 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9960 exp->enabled_mask |= 0x4;
9961 }
9962 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9963 if (ctx->options->chip_class < GFX9) {
9964 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9965 exp->enabled_mask |= 0x8;
9966 } else {
9967 Builder bld(ctx->program, ctx->block);
9968
9969 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9970 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9971 if (exp->operands[2].isTemp())
9972 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9973
9974 exp->operands[2] = Operand(out);
9975 exp->enabled_mask |= 0x4;
9976 }
9977 }
9978 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9979 exp->done = false;
9980 exp->compressed = false;
9981 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9982 ctx->block->instructions.emplace_back(std::move(exp));
9983 }
9984
9985 static void create_export_phis(isel_context *ctx)
9986 {
9987 /* Used when exports are needed, but the output temps are defined in a preceding block.
9988 * This function will set up phis in order to access the outputs in the next block.
9989 */
9990
9991 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9992 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9993 ctx->block->instructions.pop_back();
9994
9995 Builder bld(ctx->program, ctx->block);
9996
9997 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9998 uint64_t mask = ctx->outputs.mask[slot];
9999 for (unsigned i = 0; i < 4; ++i) {
10000 if (!(mask & (1 << i)))
10001 continue;
10002
10003 Temp old = ctx->outputs.temps[slot * 4 + i];
10004 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
10005 ctx->outputs.temps[slot * 4 + i] = phi;
10006 }
10007 }
10008
10009 bld.insert(std::move(logical_start));
10010 }
10011
10012 static void create_vs_exports(isel_context *ctx)
10013 {
10014 assert(ctx->stage == vertex_vs ||
10015 ctx->stage == tess_eval_vs ||
10016 ctx->stage == gs_copy_vs ||
10017 ctx->stage == ngg_vertex_gs ||
10018 ctx->stage == ngg_tess_eval_gs);
10019
10020 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
10021 ? &ctx->program->info->tes.outinfo
10022 : &ctx->program->info->vs.outinfo;
10023
10024 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
10025 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10026 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
10027 }
10028
10029 if (ctx->options->key.has_multiview_view_index) {
10030 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
10031 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
10032 }
10033
10034 /* the order these position exports are created is important */
10035 int next_pos = 0;
10036 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
10037 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
10038 export_vs_psiz_layer_viewport(ctx, &next_pos);
10039 exported_pos = true;
10040 }
10041 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
10042 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
10043 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
10044 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
10045
10046 if (ctx->export_clip_dists) {
10047 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
10048 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
10049 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
10050 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
10051 }
10052
10053 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10054 if (i < VARYING_SLOT_VAR0 &&
10055 i != VARYING_SLOT_LAYER &&
10056 i != VARYING_SLOT_PRIMITIVE_ID &&
10057 i != VARYING_SLOT_VIEWPORT)
10058 continue;
10059
10060 export_vs_varying(ctx, i, false, NULL);
10061 }
10062
10063 if (!exported_pos)
10064 create_null_export(ctx);
10065 }
10066
10067 static bool export_fs_mrt_z(isel_context *ctx)
10068 {
10069 Builder bld(ctx->program, ctx->block);
10070 unsigned enabled_channels = 0;
10071 bool compr = false;
10072 Operand values[4];
10073
10074 for (unsigned i = 0; i < 4; ++i) {
10075 values[i] = Operand(v1);
10076 }
10077
10078 /* Both stencil and sample mask only need 16-bits. */
10079 if (!ctx->program->info->ps.writes_z &&
10080 (ctx->program->info->ps.writes_stencil ||
10081 ctx->program->info->ps.writes_sample_mask)) {
10082 compr = true; /* COMPR flag */
10083
10084 if (ctx->program->info->ps.writes_stencil) {
10085 /* Stencil should be in X[23:16]. */
10086 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10087 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
10088 enabled_channels |= 0x3;
10089 }
10090
10091 if (ctx->program->info->ps.writes_sample_mask) {
10092 /* SampleMask should be in Y[15:0]. */
10093 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10094 enabled_channels |= 0xc;
10095 }
10096 } else {
10097 if (ctx->program->info->ps.writes_z) {
10098 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
10099 enabled_channels |= 0x1;
10100 }
10101
10102 if (ctx->program->info->ps.writes_stencil) {
10103 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10104 enabled_channels |= 0x2;
10105 }
10106
10107 if (ctx->program->info->ps.writes_sample_mask) {
10108 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10109 enabled_channels |= 0x4;
10110 }
10111 }
10112
10113 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
10114 * writemask component.
10115 */
10116 if (ctx->options->chip_class == GFX6 &&
10117 ctx->options->family != CHIP_OLAND &&
10118 ctx->options->family != CHIP_HAINAN) {
10119 enabled_channels |= 0x1;
10120 }
10121
10122 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10123 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
10124
10125 return true;
10126 }
10127
10128 static bool export_fs_mrt_color(isel_context *ctx, int slot)
10129 {
10130 Builder bld(ctx->program, ctx->block);
10131 unsigned write_mask = ctx->outputs.mask[slot];
10132 Operand values[4];
10133
10134 for (unsigned i = 0; i < 4; ++i) {
10135 if (write_mask & (1 << i)) {
10136 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
10137 } else {
10138 values[i] = Operand(v1);
10139 }
10140 }
10141
10142 unsigned target, col_format;
10143 unsigned enabled_channels = 0;
10144 aco_opcode compr_op = (aco_opcode)0;
10145
10146 slot -= FRAG_RESULT_DATA0;
10147 target = V_008DFC_SQ_EXP_MRT + slot;
10148 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
10149
10150 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
10151 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
10152 bool is_16bit = values[0].regClass() == v2b;
10153
10154 switch (col_format)
10155 {
10156 case V_028714_SPI_SHADER_ZERO:
10157 enabled_channels = 0; /* writemask */
10158 target = V_008DFC_SQ_EXP_NULL;
10159 break;
10160
10161 case V_028714_SPI_SHADER_32_R:
10162 enabled_channels = 1;
10163 break;
10164
10165 case V_028714_SPI_SHADER_32_GR:
10166 enabled_channels = 0x3;
10167 break;
10168
10169 case V_028714_SPI_SHADER_32_AR:
10170 if (ctx->options->chip_class >= GFX10) {
10171 /* Special case: on GFX10, the outputs are different for 32_AR */
10172 enabled_channels = 0x3;
10173 values[1] = values[3];
10174 values[3] = Operand(v1);
10175 } else {
10176 enabled_channels = 0x9;
10177 }
10178 break;
10179
10180 case V_028714_SPI_SHADER_FP16_ABGR:
10181 enabled_channels = 0x5;
10182 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
10183 if (is_16bit) {
10184 if (ctx->options->chip_class >= GFX9) {
10185 /* Pack the FP16 values together instead of converting them to
10186 * FP32 and back to FP16.
10187 * TODO: use p_create_vector and let the compiler optimizes.
10188 */
10189 compr_op = aco_opcode::v_pack_b32_f16;
10190 } else {
10191 for (unsigned i = 0; i < 4; i++) {
10192 if ((write_mask >> i) & 1)
10193 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
10194 }
10195 }
10196 }
10197 break;
10198
10199 case V_028714_SPI_SHADER_UNORM16_ABGR:
10200 enabled_channels = 0x5;
10201 if (is_16bit && ctx->options->chip_class >= GFX9) {
10202 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
10203 } else {
10204 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10205 }
10206 break;
10207
10208 case V_028714_SPI_SHADER_SNORM16_ABGR:
10209 enabled_channels = 0x5;
10210 if (is_16bit && ctx->options->chip_class >= GFX9) {
10211 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
10212 } else {
10213 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10214 }
10215 break;
10216
10217 case V_028714_SPI_SHADER_UINT16_ABGR: {
10218 enabled_channels = 0x5;
10219 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10220 if (is_int8 || is_int10) {
10221 /* clamp */
10222 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10223 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10224
10225 for (unsigned i = 0; i < 4; i++) {
10226 if ((write_mask >> i) & 1) {
10227 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10228 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10229 values[i]);
10230 }
10231 }
10232 } else if (is_16bit) {
10233 for (unsigned i = 0; i < 4; i++) {
10234 if ((write_mask >> i) & 1) {
10235 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, false);
10236 values[i] = Operand(tmp);
10237 }
10238 }
10239 }
10240 break;
10241 }
10242
10243 case V_028714_SPI_SHADER_SINT16_ABGR:
10244 enabled_channels = 0x5;
10245 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10246 if (is_int8 || is_int10) {
10247 /* clamp */
10248 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10249 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10250 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10251 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10252
10253 for (unsigned i = 0; i < 4; i++) {
10254 if ((write_mask >> i) & 1) {
10255 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10256 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10257 values[i]);
10258 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10259 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10260 values[i]);
10261 }
10262 }
10263 } else if (is_16bit) {
10264 for (unsigned i = 0; i < 4; i++) {
10265 if ((write_mask >> i) & 1) {
10266 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, true);
10267 values[i] = Operand(tmp);
10268 }
10269 }
10270 }
10271 break;
10272
10273 case V_028714_SPI_SHADER_32_ABGR:
10274 enabled_channels = 0xF;
10275 break;
10276
10277 default:
10278 break;
10279 }
10280
10281 if (target == V_008DFC_SQ_EXP_NULL)
10282 return false;
10283
10284 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10285 if (ctx->options->enable_mrt_output_nan_fixup &&
10286 !is_16bit &&
10287 (col_format == V_028714_SPI_SHADER_32_R ||
10288 col_format == V_028714_SPI_SHADER_32_GR ||
10289 col_format == V_028714_SPI_SHADER_32_AR ||
10290 col_format == V_028714_SPI_SHADER_32_ABGR ||
10291 col_format == V_028714_SPI_SHADER_FP16_ABGR)) {
10292 for (int i = 0; i < 4; i++) {
10293 if (!(write_mask & (1 << i)))
10294 continue;
10295
10296 Temp isnan = bld.vopc(aco_opcode::v_cmp_class_f32,
10297 bld.hint_vcc(bld.def(bld.lm)), values[i],
10298 bld.copy(bld.def(v1), Operand(3u)));
10299 values[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), values[i],
10300 bld.copy(bld.def(v1), Operand(0u)), isnan);
10301 }
10302 }
10303
10304 if ((bool) compr_op) {
10305 for (int i = 0; i < 2; i++) {
10306 /* check if at least one of the values to be compressed is enabled */
10307 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10308 if (enabled) {
10309 enabled_channels |= enabled << (i*2);
10310 values[i] = bld.vop3(compr_op, bld.def(v1),
10311 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10312 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10313 } else {
10314 values[i] = Operand(v1);
10315 }
10316 }
10317 values[2] = Operand(v1);
10318 values[3] = Operand(v1);
10319 } else {
10320 for (int i = 0; i < 4; i++)
10321 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10322 }
10323
10324 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10325 enabled_channels, target, (bool) compr_op);
10326 return true;
10327 }
10328
10329 static void create_fs_exports(isel_context *ctx)
10330 {
10331 bool exported = false;
10332
10333 /* Export depth, stencil and sample mask. */
10334 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10335 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10336 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10337 exported |= export_fs_mrt_z(ctx);
10338
10339 /* Export all color render targets. */
10340 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10341 if (ctx->outputs.mask[i])
10342 exported |= export_fs_mrt_color(ctx, i);
10343
10344 if (!exported)
10345 create_null_export(ctx);
10346 }
10347
10348 static void create_workgroup_barrier(Builder& bld)
10349 {
10350 bld.barrier(aco_opcode::p_barrier,
10351 memory_sync_info(storage_shared, semantic_acqrel, scope_workgroup),
10352 scope_workgroup);
10353 }
10354
10355 static void write_tcs_tess_factors(isel_context *ctx)
10356 {
10357 unsigned outer_comps;
10358 unsigned inner_comps;
10359
10360 switch (ctx->args->options->key.tcs.primitive_mode) {
10361 case GL_ISOLINES:
10362 outer_comps = 2;
10363 inner_comps = 0;
10364 break;
10365 case GL_TRIANGLES:
10366 outer_comps = 3;
10367 inner_comps = 1;
10368 break;
10369 case GL_QUADS:
10370 outer_comps = 4;
10371 inner_comps = 2;
10372 break;
10373 default:
10374 return;
10375 }
10376
10377 Builder bld(ctx->program, ctx->block);
10378
10379 create_workgroup_barrier(bld);
10380
10381 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10382 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10383
10384 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10385 if_context ic_invocation_id_is_zero;
10386 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10387 bld.reset(ctx->block);
10388
10389 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));
10390
10391 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10392 unsigned stride = inner_comps + outer_comps;
10393 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10394 Temp tf_inner_vec;
10395 Temp tf_outer_vec;
10396 Temp out[6];
10397 assert(stride <= (sizeof(out) / sizeof(Temp)));
10398
10399 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10400 // LINES reversal
10401 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10402 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10403 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10404 } else {
10405 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);
10406 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);
10407
10408 for (unsigned i = 0; i < outer_comps; ++i)
10409 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10410 for (unsigned i = 0; i < inner_comps; ++i)
10411 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10412 }
10413
10414 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10415 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10416 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10417 unsigned tf_const_offset = 0;
10418
10419 if (ctx->program->chip_class <= GFX8) {
10420 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);
10421 if_context ic_rel_patch_id_is_zero;
10422 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10423 bld.reset(ctx->block);
10424
10425 /* Store the dynamic HS control word. */
10426 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10427 bld.mubuf(aco_opcode::buffer_store_dword,
10428 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10429 /* immediate OFFSET */ 0, /* OFFEN */ false, /* swizzled */ false, /* idxen*/ false,
10430 /* addr64 */ false, /* disable_wqm */ false, /* glc */ true);
10431 tf_const_offset += 4;
10432
10433 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10434 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10435 bld.reset(ctx->block);
10436 }
10437
10438 assert(stride == 2 || stride == 4 || stride == 6);
10439 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10440 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, memory_sync_info());
10441
10442 /* Store to offchip for TES to read - only if TES reads them */
10443 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10444 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));
10445 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10446
10447 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10448 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, memory_sync_info(storage_vmem_output));
10449
10450 if (likely(inner_comps)) {
10451 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10452 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, memory_sync_info(storage_vmem_output));
10453 }
10454 }
10455
10456 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10457 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10458 }
10459
10460 static void emit_stream_output(isel_context *ctx,
10461 Temp const *so_buffers,
10462 Temp const *so_write_offset,
10463 const struct radv_stream_output *output)
10464 {
10465 unsigned num_comps = util_bitcount(output->component_mask);
10466 unsigned writemask = (1 << num_comps) - 1;
10467 unsigned loc = output->location;
10468 unsigned buf = output->buffer;
10469
10470 assert(num_comps && num_comps <= 4);
10471 if (!num_comps || num_comps > 4)
10472 return;
10473
10474 unsigned start = ffs(output->component_mask) - 1;
10475
10476 Temp out[4];
10477 bool all_undef = true;
10478 assert(ctx->stage & hw_vs);
10479 for (unsigned i = 0; i < num_comps; i++) {
10480 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10481 all_undef = all_undef && !out[i].id();
10482 }
10483 if (all_undef)
10484 return;
10485
10486 while (writemask) {
10487 int start, count;
10488 u_bit_scan_consecutive_range(&writemask, &start, &count);
10489 if (count == 3 && ctx->options->chip_class == GFX6) {
10490 /* GFX6 doesn't support storing vec3, split it. */
10491 writemask |= 1u << (start + 2);
10492 count = 2;
10493 }
10494
10495 unsigned offset = output->offset + start * 4;
10496
10497 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10498 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10499 for (int i = 0; i < count; ++i)
10500 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10501 vec->definitions[0] = Definition(write_data);
10502 ctx->block->instructions.emplace_back(std::move(vec));
10503
10504 aco_opcode opcode;
10505 switch (count) {
10506 case 1:
10507 opcode = aco_opcode::buffer_store_dword;
10508 break;
10509 case 2:
10510 opcode = aco_opcode::buffer_store_dwordx2;
10511 break;
10512 case 3:
10513 opcode = aco_opcode::buffer_store_dwordx3;
10514 break;
10515 case 4:
10516 opcode = aco_opcode::buffer_store_dwordx4;
10517 break;
10518 default:
10519 unreachable("Unsupported dword count.");
10520 }
10521
10522 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10523 store->operands[0] = Operand(so_buffers[buf]);
10524 store->operands[1] = Operand(so_write_offset[buf]);
10525 store->operands[2] = Operand((uint32_t) 0);
10526 store->operands[3] = Operand(write_data);
10527 if (offset > 4095) {
10528 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10529 Builder bld(ctx->program, ctx->block);
10530 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10531 } else {
10532 store->offset = offset;
10533 }
10534 store->offen = true;
10535 store->glc = true;
10536 store->dlc = false;
10537 store->slc = true;
10538 ctx->block->instructions.emplace_back(std::move(store));
10539 }
10540 }
10541
10542 static void emit_streamout(isel_context *ctx, unsigned stream)
10543 {
10544 Builder bld(ctx->program, ctx->block);
10545
10546 Temp so_buffers[4];
10547 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10548 for (unsigned i = 0; i < 4; i++) {
10549 unsigned stride = ctx->program->info->so.strides[i];
10550 if (!stride)
10551 continue;
10552
10553 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10554 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10555 }
10556
10557 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10558 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10559
10560 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10561
10562 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10563
10564 if_context ic;
10565 begin_divergent_if_then(ctx, &ic, can_emit);
10566
10567 bld.reset(ctx->block);
10568
10569 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10570
10571 Temp so_write_offset[4];
10572
10573 for (unsigned i = 0; i < 4; i++) {
10574 unsigned stride = ctx->program->info->so.strides[i];
10575 if (!stride)
10576 continue;
10577
10578 if (stride == 1) {
10579 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10580 get_arg(ctx, ctx->args->streamout_write_idx),
10581 get_arg(ctx, ctx->args->streamout_offset[i]));
10582 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10583
10584 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10585 } else {
10586 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10587 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10588 get_arg(ctx, ctx->args->streamout_offset[i]));
10589 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10590 }
10591 }
10592
10593 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10594 struct radv_stream_output *output =
10595 &ctx->program->info->so.outputs[i];
10596 if (stream != output->stream)
10597 continue;
10598
10599 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10600 }
10601
10602 begin_divergent_if_else(ctx, &ic);
10603 end_divergent_if(ctx, &ic);
10604 }
10605
10606 } /* end namespace */
10607
10608 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10609 {
10610 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10611 Builder bld(ctx->program, ctx->block);
10612 constexpr unsigned hs_idx = 1u;
10613 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10614 get_arg(ctx, ctx->args->merged_wave_info),
10615 Operand((8u << 16) | (hs_idx * 8u)));
10616 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10617
10618 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10619
10620 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10621 get_arg(ctx, ctx->args->rel_auto_id),
10622 get_arg(ctx, ctx->args->ac.instance_id),
10623 ls_has_nonzero_hs_threads);
10624 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10625 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10626 get_arg(ctx, ctx->args->rel_auto_id),
10627 ls_has_nonzero_hs_threads);
10628 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10629 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10630 get_arg(ctx, ctx->args->ac.vertex_id),
10631 ls_has_nonzero_hs_threads);
10632
10633 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10634 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10635 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10636 }
10637
10638 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10639 {
10640 /* Split all arguments except for the first (ring_offsets) and the last
10641 * (exec) so that the dead channels don't stay live throughout the program.
10642 */
10643 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10644 if (startpgm->definitions[i].regClass().size() > 1) {
10645 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10646 startpgm->definitions[i].regClass().size());
10647 }
10648 }
10649 }
10650
10651 void handle_bc_optimize(isel_context *ctx)
10652 {
10653 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10654 Builder bld(ctx->program, ctx->block);
10655 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10656 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10657 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10658 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10659 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10660 if (uses_center && uses_centroid) {
10661 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10662 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10663
10664 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10665 Temp new_coord[2];
10666 for (unsigned i = 0; i < 2; i++) {
10667 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10668 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10669 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10670 persp_centroid, persp_center, sel);
10671 }
10672 ctx->persp_centroid = bld.tmp(v2);
10673 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10674 Operand(new_coord[0]), Operand(new_coord[1]));
10675 emit_split_vector(ctx, ctx->persp_centroid, 2);
10676 }
10677
10678 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10679 Temp new_coord[2];
10680 for (unsigned i = 0; i < 2; i++) {
10681 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10682 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10683 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10684 linear_centroid, linear_center, sel);
10685 }
10686 ctx->linear_centroid = bld.tmp(v2);
10687 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10688 Operand(new_coord[0]), Operand(new_coord[1]));
10689 emit_split_vector(ctx, ctx->linear_centroid, 2);
10690 }
10691 }
10692 }
10693
10694 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10695 {
10696 Program *program = ctx->program;
10697
10698 unsigned float_controls = shader->info.float_controls_execution_mode;
10699
10700 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10701 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10702 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10703 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10704 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10705
10706 program->next_fp_mode.must_flush_denorms32 =
10707 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10708 program->next_fp_mode.must_flush_denorms16_64 =
10709 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10710 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10711
10712 program->next_fp_mode.care_about_round32 =
10713 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10714
10715 program->next_fp_mode.care_about_round16_64 =
10716 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10717 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10718
10719 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10720 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10721 if (program->next_fp_mode.must_flush_denorms16_64)
10722 program->next_fp_mode.denorm16_64 = 0;
10723 else
10724 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10725
10726 /* preserving fp32 denorms is expensive, so only do it if asked */
10727 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10728 program->next_fp_mode.denorm32 = fp_denorm_keep;
10729 else
10730 program->next_fp_mode.denorm32 = 0;
10731
10732 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10733 program->next_fp_mode.round32 = fp_round_tz;
10734 else
10735 program->next_fp_mode.round32 = fp_round_ne;
10736
10737 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10738 program->next_fp_mode.round16_64 = fp_round_tz;
10739 else
10740 program->next_fp_mode.round16_64 = fp_round_ne;
10741
10742 ctx->block->fp_mode = program->next_fp_mode;
10743 }
10744
10745 void cleanup_cfg(Program *program)
10746 {
10747 /* create linear_succs/logical_succs */
10748 for (Block& BB : program->blocks) {
10749 for (unsigned idx : BB.linear_preds)
10750 program->blocks[idx].linear_succs.emplace_back(BB.index);
10751 for (unsigned idx : BB.logical_preds)
10752 program->blocks[idx].logical_succs.emplace_back(BB.index);
10753 }
10754 }
10755
10756 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10757 {
10758 Builder bld(ctx->program, ctx->block);
10759
10760 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10761 Temp count = i == 0
10762 ? get_arg(ctx, ctx->args->merged_wave_info)
10763 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10764 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10765
10766 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10767 Temp cond;
10768
10769 if (ctx->program->wave_size == 64) {
10770 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10771 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10772 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10773 } else {
10774 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10775 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10776 }
10777
10778 return cond;
10779 }
10780
10781 bool ngg_early_prim_export(isel_context *ctx)
10782 {
10783 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10784 return true;
10785 }
10786
10787 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10788 {
10789 Builder bld(ctx->program, ctx->block);
10790
10791 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10792 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10793
10794 /* Get the id of the current wave within the threadgroup (workgroup) */
10795 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10796 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10797
10798 /* Execute the following code only on the first wave (wave id 0),
10799 * use the SCC def to tell if the wave id is zero or not.
10800 */
10801 Temp cond = wave_id_in_tg.def(1).getTemp();
10802 if_context ic;
10803 begin_uniform_if_then(ctx, &ic, cond);
10804 begin_uniform_if_else(ctx, &ic);
10805 bld.reset(ctx->block);
10806
10807 /* Number of vertices output by VS/TES */
10808 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10809 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10810 /* Number of primitives output by VS/TES */
10811 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10812 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10813
10814 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10815 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10816 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10817
10818 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10819 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10820
10821 end_uniform_if(ctx, &ic);
10822
10823 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10824 bld.reset(ctx->block);
10825 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10826 }
10827
10828 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10829 {
10830 Builder bld(ctx->program, ctx->block);
10831
10832 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10833 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10834 }
10835
10836 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10837 Temp tmp;
10838
10839 for (unsigned i = 0; i < num_vertices; ++i) {
10840 assert(vtxindex[i].id());
10841
10842 if (i)
10843 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10844 else
10845 tmp = vtxindex[i];
10846
10847 /* The initial edge flag is always false in tess eval shaders. */
10848 if (ctx->stage == ngg_vertex_gs) {
10849 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10850 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10851 }
10852 }
10853
10854 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10855
10856 return tmp;
10857 }
10858
10859 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10860 {
10861 Builder bld(ctx->program, ctx->block);
10862 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10863
10864 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10865 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10866 false /* compressed */, true/* done */, false /* valid mask */);
10867 }
10868
10869 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10870 {
10871 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10872 * These must always come before VS exports.
10873 *
10874 * It is recommended to do these as early as possible. They can be at the beginning when
10875 * there is no SW GS and the shader doesn't write edge flags.
10876 */
10877
10878 if_context ic;
10879 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10880 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10881
10882 Builder bld(ctx->program, ctx->block);
10883 constexpr unsigned max_vertices_per_primitive = 3;
10884 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10885
10886 if (ctx->stage == ngg_vertex_gs) {
10887 /* TODO: optimize for points & lines */
10888 } else if (ctx->stage == ngg_tess_eval_gs) {
10889 if (ctx->shader->info.tess.point_mode)
10890 num_vertices_per_primitive = 1;
10891 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10892 num_vertices_per_primitive = 2;
10893 } else {
10894 unreachable("Unsupported NGG shader stage");
10895 }
10896
10897 Temp vtxindex[max_vertices_per_primitive];
10898 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10899 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10900 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10901 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10902 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10903 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10904 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10905 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10906
10907 /* Export primitive data to the index buffer. */
10908 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10909
10910 /* Export primitive ID. */
10911 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10912 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10913 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10914 Temp provoking_vtx_index = vtxindex[0];
10915 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10916
10917 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10918 }
10919
10920 begin_divergent_if_else(ctx, &ic);
10921 end_divergent_if(ctx, &ic);
10922 }
10923
10924 void ngg_emit_nogs_output(isel_context *ctx)
10925 {
10926 /* Emits NGG GS output, for stages that don't have SW GS. */
10927
10928 if_context ic;
10929 Builder bld(ctx->program, ctx->block);
10930 bool late_prim_export = !ngg_early_prim_export(ctx);
10931
10932 /* NGG streamout is currently disabled by default. */
10933 assert(!ctx->args->shader_info->so.num_outputs);
10934
10935 if (late_prim_export) {
10936 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10937 create_export_phis(ctx);
10938 /* Do what we need to do in the GS threads. */
10939 ngg_emit_nogs_gsthreads(ctx);
10940
10941 /* What comes next should be executed on ES threads. */
10942 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10943 begin_divergent_if_then(ctx, &ic, is_es_thread);
10944 bld.reset(ctx->block);
10945 }
10946
10947 /* Export VS outputs */
10948 ctx->block->kind |= block_kind_export_end;
10949 create_vs_exports(ctx);
10950
10951 /* Export primitive ID */
10952 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10953 Temp prim_id;
10954
10955 if (ctx->stage == ngg_vertex_gs) {
10956 /* Wait for GS threads to store primitive ID in LDS. */
10957 create_workgroup_barrier(bld);
10958
10959 /* Calculate LDS address where the GS threads stored the primitive ID. */
10960 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10961 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10962 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10963 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10964 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10965 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10966
10967 /* Load primitive ID from LDS. */
10968 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10969 } else if (ctx->stage == ngg_tess_eval_gs) {
10970 /* TES: Just use the patch ID as the primitive ID. */
10971 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10972 } else {
10973 unreachable("unsupported NGG shader stage.");
10974 }
10975
10976 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10977 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10978
10979 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10980 }
10981
10982 if (late_prim_export) {
10983 begin_divergent_if_else(ctx, &ic);
10984 end_divergent_if(ctx, &ic);
10985 bld.reset(ctx->block);
10986 }
10987 }
10988
10989 void select_program(Program *program,
10990 unsigned shader_count,
10991 struct nir_shader *const *shaders,
10992 ac_shader_config* config,
10993 struct radv_shader_args *args)
10994 {
10995 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10996 if_context ic_merged_wave_info;
10997 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10998
10999 for (unsigned i = 0; i < shader_count; i++) {
11000 nir_shader *nir = shaders[i];
11001 init_context(&ctx, nir);
11002
11003 setup_fp_mode(&ctx, nir);
11004
11005 if (!i) {
11006 /* needs to be after init_context() for FS */
11007 Pseudo_instruction *startpgm = add_startpgm(&ctx);
11008 append_logical_start(ctx.block);
11009
11010 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
11011 fix_ls_vgpr_init_bug(&ctx, startpgm);
11012
11013 split_arguments(&ctx, startpgm);
11014 }
11015
11016 if (ngg_no_gs) {
11017 ngg_emit_sendmsg_gs_alloc_req(&ctx);
11018
11019 if (ngg_early_prim_export(&ctx))
11020 ngg_emit_nogs_gsthreads(&ctx);
11021 }
11022
11023 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
11024 nir_function_impl *func = nir_shader_get_entrypoint(nir);
11025 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
11026 ((nir->info.stage == MESA_SHADER_VERTEX &&
11027 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
11028 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
11029 ctx.stage == tess_eval_geometry_gs));
11030
11031 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
11032 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
11033 if (check_merged_wave_info) {
11034 Temp cond = merged_wave_info_to_mask(&ctx, i);
11035 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
11036 }
11037
11038 if (i) {
11039 Builder bld(ctx.program, ctx.block);
11040
11041 create_workgroup_barrier(bld);
11042
11043 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
11044 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));
11045 }
11046 } else if (ctx.stage == geometry_gs)
11047 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
11048
11049 if (ctx.stage == fragment_fs)
11050 handle_bc_optimize(&ctx);
11051
11052 visit_cf_list(&ctx, &func->body);
11053
11054 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
11055 emit_streamout(&ctx, 0);
11056
11057 if (ctx.stage & hw_vs) {
11058 create_vs_exports(&ctx);
11059 ctx.block->kind |= block_kind_export_end;
11060 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
11061 ngg_emit_nogs_output(&ctx);
11062 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
11063 Builder bld(ctx.program, ctx.block);
11064 bld.barrier(aco_opcode::p_barrier,
11065 memory_sync_info(storage_vmem_output, semantic_release, scope_device));
11066 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
11067 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
11068 write_tcs_tess_factors(&ctx);
11069 }
11070
11071 if (ctx.stage == fragment_fs) {
11072 create_fs_exports(&ctx);
11073 ctx.block->kind |= block_kind_export_end;
11074 }
11075
11076 if (endif_merged_wave_info) {
11077 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
11078 end_divergent_if(&ctx, &ic_merged_wave_info);
11079 }
11080
11081 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
11082 ngg_emit_nogs_output(&ctx);
11083
11084 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
11085 /* Outputs of the previous stage are inputs to the next stage */
11086 ctx.inputs = ctx.outputs;
11087 ctx.outputs = shader_io_state();
11088 }
11089 }
11090
11091 program->config->float_mode = program->blocks[0].fp_mode.val;
11092
11093 append_logical_end(ctx.block);
11094 ctx.block->kind |= block_kind_uniform;
11095 Builder bld(ctx.program, ctx.block);
11096 if (ctx.program->wb_smem_l1_on_end)
11097 bld.smem(aco_opcode::s_dcache_wb, memory_sync_info(storage_buffer, semantic_volatile));
11098 bld.sopp(aco_opcode::s_endpgm);
11099
11100 cleanup_cfg(program);
11101 }
11102
11103 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
11104 ac_shader_config* config,
11105 struct radv_shader_args *args)
11106 {
11107 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
11108
11109 ctx.block->fp_mode = program->next_fp_mode;
11110
11111 add_startpgm(&ctx);
11112 append_logical_start(ctx.block);
11113
11114 Builder bld(ctx.program, ctx.block);
11115
11116 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
11117
11118 Operand stream_id(0u);
11119 if (args->shader_info->so.num_outputs)
11120 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
11121 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
11122
11123 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
11124
11125 std::stack<Block> endif_blocks;
11126
11127 for (unsigned stream = 0; stream < 4; stream++) {
11128 if (stream_id.isConstant() && stream != stream_id.constantValue())
11129 continue;
11130
11131 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
11132 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
11133 continue;
11134
11135 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
11136
11137 unsigned BB_if_idx = ctx.block->index;
11138 Block BB_endif = Block();
11139 if (!stream_id.isConstant()) {
11140 /* begin IF */
11141 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
11142 append_logical_end(ctx.block);
11143 ctx.block->kind |= block_kind_uniform;
11144 bld.branch(aco_opcode::p_cbranch_z, cond);
11145
11146 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
11147
11148 ctx.block = ctx.program->create_and_insert_block();
11149 add_edge(BB_if_idx, ctx.block);
11150 bld.reset(ctx.block);
11151 append_logical_start(ctx.block);
11152 }
11153
11154 unsigned offset = 0;
11155 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
11156 if (args->shader_info->gs.output_streams[i] != stream)
11157 continue;
11158
11159 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
11160 unsigned length = util_last_bit(output_usage_mask);
11161 for (unsigned j = 0; j < length; ++j) {
11162 if (!(output_usage_mask & (1 << j)))
11163 continue;
11164
11165 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
11166 Temp voffset = vtx_offset;
11167 if (const_offset >= 4096u) {
11168 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
11169 const_offset %= 4096u;
11170 }
11171
11172 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
11173 mubuf->definitions[0] = bld.def(v1);
11174 mubuf->operands[0] = Operand(gsvs_ring);
11175 mubuf->operands[1] = Operand(voffset);
11176 mubuf->operands[2] = Operand(0u);
11177 mubuf->offen = true;
11178 mubuf->offset = const_offset;
11179 mubuf->glc = true;
11180 mubuf->slc = true;
11181 mubuf->dlc = args->options->chip_class >= GFX10;
11182
11183 ctx.outputs.mask[i] |= 1 << j;
11184 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
11185
11186 bld.insert(std::move(mubuf));
11187
11188 offset++;
11189 }
11190 }
11191
11192 if (args->shader_info->so.num_outputs) {
11193 emit_streamout(&ctx, stream);
11194 bld.reset(ctx.block);
11195 }
11196
11197 if (stream == 0) {
11198 create_vs_exports(&ctx);
11199 ctx.block->kind |= block_kind_export_end;
11200 }
11201
11202 if (!stream_id.isConstant()) {
11203 append_logical_end(ctx.block);
11204
11205 /* branch from then block to endif block */
11206 bld.branch(aco_opcode::p_branch);
11207 add_edge(ctx.block->index, &BB_endif);
11208 ctx.block->kind |= block_kind_uniform;
11209
11210 /* emit else block */
11211 ctx.block = ctx.program->create_and_insert_block();
11212 add_edge(BB_if_idx, ctx.block);
11213 bld.reset(ctx.block);
11214 append_logical_start(ctx.block);
11215
11216 endif_blocks.push(std::move(BB_endif));
11217 }
11218 }
11219
11220 while (!endif_blocks.empty()) {
11221 Block BB_endif = std::move(endif_blocks.top());
11222 endif_blocks.pop();
11223
11224 Block *BB_else = ctx.block;
11225
11226 append_logical_end(BB_else);
11227 /* branch from else block to endif block */
11228 bld.branch(aco_opcode::p_branch);
11229 add_edge(BB_else->index, &BB_endif);
11230 BB_else->kind |= block_kind_uniform;
11231
11232 /** emit endif merge block */
11233 ctx.block = program->insert_block(std::move(BB_endif));
11234 bld.reset(ctx.block);
11235 append_logical_start(ctx.block);
11236 }
11237
11238 program->config->float_mode = program->blocks[0].fp_mode.val;
11239
11240 append_logical_end(ctx.block);
11241 ctx.block->kind |= block_kind_uniform;
11242 bld.sopp(aco_opcode::s_endpgm);
11243
11244 cleanup_cfg(program);
11245 }
11246 }