aco: ensure to not extract more components than have been fetched
[mesa.git] / src / amd / compiler / aco_instruction_selection.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <algorithm>
27 #include <array>
28 #include <stack>
29 #include <map>
30
31 #include "ac_shader_util.h"
32 #include "aco_ir.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
37
38 namespace aco {
39 namespace {
40
41 class loop_info_RAII {
42 isel_context* ctx;
43 unsigned header_idx_old;
44 Block* exit_old;
45 bool divergent_cont_old;
46 bool divergent_branch_old;
47 bool divergent_if_old;
48
49 public:
50 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
51 : ctx(ctx),
52 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
53 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
54 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
55 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
56 {
57 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
58 ctx->cf_info.parent_loop.exit = loop_exit;
59 ctx->cf_info.parent_loop.has_divergent_continue = false;
60 ctx->cf_info.parent_loop.has_divergent_branch = false;
61 ctx->cf_info.parent_if.is_divergent = false;
62 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
63 }
64
65 ~loop_info_RAII()
66 {
67 ctx->cf_info.parent_loop.header_idx = header_idx_old;
68 ctx->cf_info.parent_loop.exit = exit_old;
69 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
70 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
71 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
72 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
73 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
74 ctx->cf_info.exec_potentially_empty_discard = false;
75 }
76 };
77
78 struct if_context {
79 Temp cond;
80
81 bool divergent_old;
82 bool exec_potentially_empty_discard_old;
83 bool exec_potentially_empty_break_old;
84 uint16_t exec_potentially_empty_break_depth_old;
85
86 unsigned BB_if_idx;
87 unsigned invert_idx;
88 bool uniform_has_then_branch;
89 bool then_branch_divergent;
90 Block BB_invert;
91 Block BB_endif;
92 };
93
94 static bool visit_cf_list(struct isel_context *ctx,
95 struct exec_list *list);
96
97 static void add_logical_edge(unsigned pred_idx, Block *succ)
98 {
99 succ->logical_preds.emplace_back(pred_idx);
100 }
101
102
103 static void add_linear_edge(unsigned pred_idx, Block *succ)
104 {
105 succ->linear_preds.emplace_back(pred_idx);
106 }
107
108 static void add_edge(unsigned pred_idx, Block *succ)
109 {
110 add_logical_edge(pred_idx, succ);
111 add_linear_edge(pred_idx, succ);
112 }
113
114 static void append_logical_start(Block *b)
115 {
116 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
117 }
118
119 static void append_logical_end(Block *b)
120 {
121 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
122 }
123
124 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
125 {
126 assert(ctx->allocated[def->index].id());
127 return ctx->allocated[def->index];
128 }
129
130 Temp emit_mbcnt(isel_context *ctx, Definition dst,
131 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
132 {
133 Builder bld(ctx->program, ctx->block);
134 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
135 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
136
137 if (ctx->program->wave_size == 32) {
138 return thread_id_lo;
139 } else if (ctx->program->chip_class <= GFX7) {
140 Temp thread_id_hi = bld.vop2(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 } else {
143 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32_e64, dst, mask_hi, thread_id_lo);
144 return thread_id_hi;
145 }
146 }
147
148 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
149 {
150 Builder bld(ctx->program, ctx->block);
151
152 if (!dst.id())
153 dst = bld.tmp(src.regClass());
154
155 assert(src.size() == dst.size());
156
157 if (ctx->stage != fragment_fs) {
158 if (!dst.id())
159 return src;
160
161 bld.copy(Definition(dst), src);
162 return dst;
163 }
164
165 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
166 ctx->program->needs_wqm |= program_needs_wqm;
167 return dst;
168 }
169
170 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
171 {
172 if (index.regClass() == s1)
173 return bld.readlane(bld.def(s1), data, index);
174
175 if (ctx->options->chip_class <= GFX7) {
176 /* GFX6-7: there is no bpermute instruction */
177 Operand index_op(index);
178 Operand input_data(data);
179 index_op.setLateKill(true);
180 input_data.setLateKill(true);
181
182 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(bld.lm), bld.def(bld.lm, vcc), index_op, input_data);
183 } else if (ctx->options->chip_class >= GFX10 && ctx->program->wave_size == 64) {
184 /* GFX10 wave64 mode: emulate full-wave bpermute */
185 if (!ctx->has_gfx10_wave64_bpermute) {
186 ctx->has_gfx10_wave64_bpermute = true;
187 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
188 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
189 }
190
191 Temp index_is_lo = bld.vopc(aco_opcode::v_cmp_ge_u32, bld.def(bld.lm), Operand(31u), index);
192 Builder::Result index_is_lo_split = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), index_is_lo);
193 Temp index_is_lo_n1 = bld.sop1(aco_opcode::s_not_b32, bld.def(s1), bld.def(s1, scc), index_is_lo_split.def(1).getTemp());
194 Operand same_half = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), index_is_lo_split.def(0).getTemp(), index_is_lo_n1);
195 Operand index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
196 Operand input_data(data);
197
198 index_x4.setLateKill(true);
199 input_data.setLateKill(true);
200 same_half.setLateKill(true);
201
202 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc), index_x4, input_data, same_half);
203 } else {
204 /* GFX8-9 or GFX10 wave32: bpermute works normally */
205 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
206 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
207 }
208 }
209
210 static Temp emit_masked_swizzle(isel_context *ctx, Builder &bld, Temp src, unsigned mask)
211 {
212 if (ctx->options->chip_class >= GFX8) {
213 unsigned and_mask = mask & 0x1f;
214 unsigned or_mask = (mask >> 5) & 0x1f;
215 unsigned xor_mask = (mask >> 10) & 0x1f;
216
217 uint16_t dpp_ctrl = 0xffff;
218
219 // TODO: we could use DPP8 for some swizzles
220 if (and_mask == 0x1f && or_mask < 4 && xor_mask < 4) {
221 unsigned res[4] = {0, 1, 2, 3};
222 for (unsigned i = 0; i < 4; i++)
223 res[i] = ((res[i] | or_mask) ^ xor_mask) & 0x3;
224 dpp_ctrl = dpp_quad_perm(res[0], res[1], res[2], res[3]);
225 } else if (and_mask == 0x1f && !or_mask && xor_mask == 8) {
226 dpp_ctrl = dpp_row_rr(8);
227 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0xf) {
228 dpp_ctrl = dpp_row_mirror;
229 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0x7) {
230 dpp_ctrl = dpp_row_half_mirror;
231 }
232
233 if (dpp_ctrl != 0xffff)
234 return bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
235 }
236
237 return bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false);
238 }
239
240 Temp as_vgpr(isel_context *ctx, Temp val)
241 {
242 if (val.type() == RegType::sgpr) {
243 Builder bld(ctx->program, ctx->block);
244 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
245 }
246 assert(val.type() == RegType::vgpr);
247 return val;
248 }
249
250 //assumes a != 0xffffffff
251 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
252 {
253 assert(b != 0);
254 Builder bld(ctx->program, ctx->block);
255
256 if (util_is_power_of_two_or_zero(b)) {
257 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
258 return;
259 }
260
261 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
262
263 assert(info.multiplier <= 0xffffffff);
264
265 bool pre_shift = info.pre_shift != 0;
266 bool increment = info.increment != 0;
267 bool multiply = true;
268 bool post_shift = info.post_shift != 0;
269
270 if (!pre_shift && !increment && !multiply && !post_shift) {
271 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
272 return;
273 }
274
275 Temp pre_shift_dst = a;
276 if (pre_shift) {
277 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
278 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
279 }
280
281 Temp increment_dst = pre_shift_dst;
282 if (increment) {
283 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
284 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
285 }
286
287 Temp multiply_dst = increment_dst;
288 if (multiply) {
289 multiply_dst = post_shift ? bld.tmp(v1) : dst;
290 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
291 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
292 }
293
294 if (post_shift) {
295 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
296 }
297 }
298
299 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
300 {
301 Builder bld(ctx->program, ctx->block);
302 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
303 }
304
305
306 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
307 {
308 /* no need to extract the whole vector */
309 if (src.regClass() == dst_rc) {
310 assert(idx == 0);
311 return src;
312 }
313
314 assert(src.bytes() > (idx * dst_rc.bytes()));
315 Builder bld(ctx->program, ctx->block);
316 auto it = ctx->allocated_vec.find(src.id());
317 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
318 if (it->second[idx].regClass() == dst_rc) {
319 return it->second[idx];
320 } else {
321 assert(!dst_rc.is_subdword());
322 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
323 return bld.copy(bld.def(dst_rc), it->second[idx]);
324 }
325 }
326
327 if (dst_rc.is_subdword())
328 src = as_vgpr(ctx, src);
329
330 if (src.bytes() == dst_rc.bytes()) {
331 assert(idx == 0);
332 return bld.copy(bld.def(dst_rc), src);
333 } else {
334 Temp dst = bld.tmp(dst_rc);
335 emit_extract_vector(ctx, src, idx, dst);
336 return dst;
337 }
338 }
339
340 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
341 {
342 if (num_components == 1)
343 return;
344 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
345 return;
346 RegClass rc;
347 if (num_components > vec_src.size()) {
348 if (vec_src.type() == RegType::sgpr) {
349 /* should still help get_alu_src() */
350 emit_split_vector(ctx, vec_src, vec_src.size());
351 return;
352 }
353 /* sub-dword split */
354 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
355 } else {
356 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
357 }
358 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
359 split->operands[0] = Operand(vec_src);
360 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
361 for (unsigned i = 0; i < num_components; i++) {
362 elems[i] = {ctx->program->allocateId(), rc};
363 split->definitions[i] = Definition(elems[i]);
364 }
365 ctx->block->instructions.emplace_back(std::move(split));
366 ctx->allocated_vec.emplace(vec_src.id(), elems);
367 }
368
369 /* This vector expansion uses a mask to determine which elements in the new vector
370 * come from the original vector. The other elements are undefined. */
371 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
372 {
373 emit_split_vector(ctx, vec_src, util_bitcount(mask));
374
375 if (vec_src == dst)
376 return;
377
378 Builder bld(ctx->program, ctx->block);
379 if (num_components == 1) {
380 if (dst.type() == RegType::sgpr)
381 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
382 else
383 bld.copy(Definition(dst), vec_src);
384 return;
385 }
386
387 unsigned component_size = dst.size() / num_components;
388 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
389
390 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
391 vec->definitions[0] = Definition(dst);
392 unsigned k = 0;
393 for (unsigned i = 0; i < num_components; i++) {
394 if (mask & (1 << i)) {
395 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
396 if (dst.type() == RegType::sgpr)
397 src = bld.as_uniform(src);
398 vec->operands[i] = Operand(src);
399 } else {
400 vec->operands[i] = Operand(0u);
401 }
402 elems[i] = vec->operands[i].getTemp();
403 }
404 ctx->block->instructions.emplace_back(std::move(vec));
405 ctx->allocated_vec.emplace(dst.id(), elems);
406 }
407
408 /* adjust misaligned small bit size loads */
409 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
410 {
411 Builder bld(ctx->program, ctx->block);
412 Operand shift;
413 Temp select = Temp();
414 if (offset.isConstant()) {
415 assert(offset.constantValue() && offset.constantValue() < 4);
416 shift = Operand(offset.constantValue() * 8);
417 } else {
418 /* bit_offset = 8 * (offset & 0x3) */
419 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
420 select = bld.tmp(s1);
421 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
422 }
423
424 if (vec.size() == 1) {
425 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
426 } else if (vec.size() == 2) {
427 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
428 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
429 if (tmp == dst)
430 emit_split_vector(ctx, dst, 2);
431 else
432 emit_extract_vector(ctx, tmp, 0, dst);
433 } else if (vec.size() == 4) {
434 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
435 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
436 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
437 if (select != Temp())
438 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), bld.scc(select));
439 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
440 Temp mid = bld.tmp(s1);
441 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
442 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
443 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
444 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
445 emit_split_vector(ctx, dst, 2);
446 }
447 }
448
449 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst, unsigned component_size)
450 {
451 Builder bld(ctx->program, ctx->block);
452 if (offset.isTemp()) {
453 Temp tmp[4] = {vec, vec, vec, vec};
454
455 if (vec.size() == 4) {
456 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1), tmp[3] = bld.tmp(v1);
457 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), Definition(tmp[3]), vec);
458 } else if (vec.size() == 3) {
459 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
460 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
461 } else if (vec.size() == 2) {
462 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
463 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
464 }
465 for (unsigned i = 0; i < dst.size(); i++)
466 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
467
468 vec = tmp[0];
469 if (dst.size() == 2)
470 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
471
472 offset = Operand(0u);
473 }
474
475 unsigned num_components = vec.bytes() / component_size;
476 if (vec.regClass() == dst.regClass()) {
477 assert(offset.constantValue() == 0);
478 bld.copy(Definition(dst), vec);
479 emit_split_vector(ctx, dst, num_components);
480 return;
481 }
482
483 emit_split_vector(ctx, vec, num_components);
484 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
485 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
486
487 assert(offset.constantValue() % component_size == 0);
488 unsigned skip = offset.constantValue() / component_size;
489 for (unsigned i = skip; i < num_components; i++)
490 elems[i - skip] = emit_extract_vector(ctx, vec, i, rc);
491
492 /* if dst is vgpr - split the src and create a shrunk version according to the mask. */
493 if (dst.type() == RegType::vgpr) {
494 num_components = dst.bytes() / component_size;
495 aco_ptr<Pseudo_instruction> create_vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
496 for (unsigned i = 0; i < num_components; i++)
497 create_vec->operands[i] = Operand(elems[i]);
498 create_vec->definitions[0] = Definition(dst);
499 bld.insert(std::move(create_vec));
500
501 /* if dst is sgpr - split the src, but move the original to sgpr. */
502 } else if (skip) {
503 vec = bld.pseudo(aco_opcode::p_as_uniform, bld.def(RegClass(RegType::sgpr, vec.size())), vec);
504 byte_align_scalar(ctx, vec, offset, dst);
505 } else {
506 assert(dst.size() == vec.size());
507 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
508 }
509
510 ctx->allocated_vec.emplace(dst.id(), elems);
511 }
512
513 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
514 {
515 Builder bld(ctx->program, ctx->block);
516 if (!dst.id())
517 dst = bld.tmp(bld.lm);
518
519 assert(val.regClass() == s1);
520 assert(dst.regClass() == bld.lm);
521
522 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
523 }
524
525 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
526 {
527 Builder bld(ctx->program, ctx->block);
528 if (!dst.id())
529 dst = bld.tmp(s1);
530
531 assert(val.regClass() == bld.lm);
532 assert(dst.regClass() == s1);
533
534 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
535 Temp tmp = bld.tmp(s1);
536 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
537 return emit_wqm(ctx, tmp, dst);
538 }
539
540 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
541 {
542 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
543 return get_ssa_temp(ctx, src.src.ssa);
544
545 if (src.src.ssa->num_components == size) {
546 bool identity_swizzle = true;
547 for (unsigned i = 0; identity_swizzle && i < size; i++) {
548 if (src.swizzle[i] != i)
549 identity_swizzle = false;
550 }
551 if (identity_swizzle)
552 return get_ssa_temp(ctx, src.src.ssa);
553 }
554
555 Temp vec = get_ssa_temp(ctx, src.src.ssa);
556 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
557 assert(elem_size > 0);
558 assert(vec.bytes() % elem_size == 0);
559
560 if (elem_size < 4 && vec.type() == RegType::sgpr) {
561 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
562 assert(size == 1);
563 unsigned swizzle = src.swizzle[0];
564 if (vec.size() > 1) {
565 assert(src.src.ssa->bit_size == 16);
566 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
567 swizzle = swizzle & 1;
568 }
569 if (swizzle == 0)
570 return vec;
571
572 Temp dst{ctx->program->allocateId(), s1};
573 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
574 bfe->operands[0] = Operand(vec);
575 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
576 bfe->definitions[0] = Definition(dst);
577 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
578 ctx->block->instructions.emplace_back(std::move(bfe));
579 return dst;
580 }
581
582 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
583 if (size == 1) {
584 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
585 } else {
586 assert(size <= 4);
587 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
588 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
589 for (unsigned i = 0; i < size; ++i) {
590 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
591 vec_instr->operands[i] = Operand{elems[i]};
592 }
593 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
594 vec_instr->definitions[0] = Definition(dst);
595 ctx->block->instructions.emplace_back(std::move(vec_instr));
596 ctx->allocated_vec.emplace(dst.id(), elems);
597 return dst;
598 }
599 }
600
601 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
602 {
603 if (ptr.size() == 2)
604 return ptr;
605 Builder bld(ctx->program, ctx->block);
606 if (ptr.type() == RegType::vgpr)
607 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
608 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
609 ptr, Operand((unsigned)ctx->options->address32_hi));
610 }
611
612 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
613 {
614 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
615 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
616 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
617 sop2->definitions[0] = Definition(dst);
618 if (instr->no_unsigned_wrap)
619 sop2->definitions[0].setNUW(true);
620 if (writes_scc)
621 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
622 ctx->block->instructions.emplace_back(std::move(sop2));
623 }
624
625 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
626 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
627 {
628 Builder bld(ctx->program, ctx->block);
629 bld.is_precise = instr->exact;
630
631 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
632 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
633 if (src1.type() == RegType::sgpr) {
634 if (commutative && src0.type() == RegType::vgpr) {
635 Temp t = src0;
636 src0 = src1;
637 src1 = t;
638 } else {
639 src1 = as_vgpr(ctx, src1);
640 }
641 }
642
643 if (flush_denorms && ctx->program->chip_class < GFX9) {
644 assert(dst.size() == 1);
645 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
646 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
647 } else {
648 bld.vop2(op, Definition(dst), src0, src1);
649 }
650 }
651
652 void emit_vop2_instruction_logic64(isel_context *ctx, nir_alu_instr *instr,
653 aco_opcode op, Temp dst)
654 {
655 Builder bld(ctx->program, ctx->block);
656 bld.is_precise = instr->exact;
657
658 Temp src0 = get_alu_src(ctx, instr->src[0]);
659 Temp src1 = get_alu_src(ctx, instr->src[1]);
660
661 if (src1.type() == RegType::sgpr) {
662 assert(src0.type() == RegType::vgpr);
663 std::swap(src0, src1);
664 }
665
666 Temp src00 = bld.tmp(src0.type(), 1);
667 Temp src01 = bld.tmp(src0.type(), 1);
668 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
669 Temp src10 = bld.tmp(v1);
670 Temp src11 = bld.tmp(v1);
671 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
672 Temp lo = bld.vop2(op, bld.def(v1), src00, src10);
673 Temp hi = bld.vop2(op, bld.def(v1), src01, src11);
674 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
675 }
676
677 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
678 bool flush_denorms = false)
679 {
680 Temp src0 = get_alu_src(ctx, instr->src[0]);
681 Temp src1 = get_alu_src(ctx, instr->src[1]);
682 Temp src2 = get_alu_src(ctx, instr->src[2]);
683
684 /* ensure that the instruction has at most 1 sgpr operand
685 * The optimizer will inline constants for us */
686 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
687 src0 = as_vgpr(ctx, src0);
688 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
689 src1 = as_vgpr(ctx, src1);
690 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
691 src2 = as_vgpr(ctx, src2);
692
693 Builder bld(ctx->program, ctx->block);
694 bld.is_precise = instr->exact;
695 if (flush_denorms && ctx->program->chip_class < GFX9) {
696 assert(dst.size() == 1);
697 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
698 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
699 } else {
700 bld.vop3(op, Definition(dst), src0, src1, src2);
701 }
702 }
703
704 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
705 {
706 Builder bld(ctx->program, ctx->block);
707 bld.is_precise = instr->exact;
708 if (dst.type() == RegType::sgpr)
709 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
710 bld.vop1(op, bld.def(RegType::vgpr, dst.size()), get_alu_src(ctx, instr->src[0])));
711 else
712 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
713 }
714
715 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
716 {
717 Temp src0 = get_alu_src(ctx, instr->src[0]);
718 Temp src1 = get_alu_src(ctx, instr->src[1]);
719 assert(src0.size() == src1.size());
720
721 aco_ptr<Instruction> vopc;
722 if (src1.type() == RegType::sgpr) {
723 if (src0.type() == RegType::vgpr) {
724 /* to swap the operands, we might also have to change the opcode */
725 switch (op) {
726 case aco_opcode::v_cmp_lt_f16:
727 op = aco_opcode::v_cmp_gt_f16;
728 break;
729 case aco_opcode::v_cmp_ge_f16:
730 op = aco_opcode::v_cmp_le_f16;
731 break;
732 case aco_opcode::v_cmp_lt_i16:
733 op = aco_opcode::v_cmp_gt_i16;
734 break;
735 case aco_opcode::v_cmp_ge_i16:
736 op = aco_opcode::v_cmp_le_i16;
737 break;
738 case aco_opcode::v_cmp_lt_u16:
739 op = aco_opcode::v_cmp_gt_u16;
740 break;
741 case aco_opcode::v_cmp_ge_u16:
742 op = aco_opcode::v_cmp_le_u16;
743 break;
744 case aco_opcode::v_cmp_lt_f32:
745 op = aco_opcode::v_cmp_gt_f32;
746 break;
747 case aco_opcode::v_cmp_ge_f32:
748 op = aco_opcode::v_cmp_le_f32;
749 break;
750 case aco_opcode::v_cmp_lt_i32:
751 op = aco_opcode::v_cmp_gt_i32;
752 break;
753 case aco_opcode::v_cmp_ge_i32:
754 op = aco_opcode::v_cmp_le_i32;
755 break;
756 case aco_opcode::v_cmp_lt_u32:
757 op = aco_opcode::v_cmp_gt_u32;
758 break;
759 case aco_opcode::v_cmp_ge_u32:
760 op = aco_opcode::v_cmp_le_u32;
761 break;
762 case aco_opcode::v_cmp_lt_f64:
763 op = aco_opcode::v_cmp_gt_f64;
764 break;
765 case aco_opcode::v_cmp_ge_f64:
766 op = aco_opcode::v_cmp_le_f64;
767 break;
768 case aco_opcode::v_cmp_lt_i64:
769 op = aco_opcode::v_cmp_gt_i64;
770 break;
771 case aco_opcode::v_cmp_ge_i64:
772 op = aco_opcode::v_cmp_le_i64;
773 break;
774 case aco_opcode::v_cmp_lt_u64:
775 op = aco_opcode::v_cmp_gt_u64;
776 break;
777 case aco_opcode::v_cmp_ge_u64:
778 op = aco_opcode::v_cmp_le_u64;
779 break;
780 default: /* eq and ne are commutative */
781 break;
782 }
783 Temp t = src0;
784 src0 = src1;
785 src1 = t;
786 } else {
787 src1 = as_vgpr(ctx, src1);
788 }
789 }
790
791 Builder bld(ctx->program, ctx->block);
792 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
793 }
794
795 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
796 {
797 Temp src0 = get_alu_src(ctx, instr->src[0]);
798 Temp src1 = get_alu_src(ctx, instr->src[1]);
799 Builder bld(ctx->program, ctx->block);
800
801 assert(dst.regClass() == bld.lm);
802 assert(src0.type() == RegType::sgpr);
803 assert(src1.type() == RegType::sgpr);
804 assert(src0.regClass() == src1.regClass());
805
806 /* Emit the SALU comparison instruction */
807 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
808 /* Turn the result into a per-lane bool */
809 bool_to_vector_condition(ctx, cmp, dst);
810 }
811
812 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
813 aco_opcode v16_op, aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
814 {
815 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : instr->src[0].src.ssa->bit_size == 32 ? s32_op : aco_opcode::num_opcodes;
816 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : instr->src[0].src.ssa->bit_size == 32 ? v32_op : v16_op;
817 bool use_valu = s_op == aco_opcode::num_opcodes ||
818 nir_dest_is_divergent(instr->dest.dest) ||
819 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
820 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
821 aco_opcode op = use_valu ? v_op : s_op;
822 assert(op != aco_opcode::num_opcodes);
823 assert(dst.regClass() == ctx->program->lane_mask);
824
825 if (use_valu)
826 emit_vopc_instruction(ctx, instr, op, dst);
827 else
828 emit_sopc_instruction(ctx, instr, op, dst);
829 }
830
831 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
832 {
833 Builder bld(ctx->program, ctx->block);
834 Temp src0 = get_alu_src(ctx, instr->src[0]);
835 Temp src1 = get_alu_src(ctx, instr->src[1]);
836
837 assert(dst.regClass() == bld.lm);
838 assert(src0.regClass() == bld.lm);
839 assert(src1.regClass() == bld.lm);
840
841 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
842 }
843
844 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
845 {
846 Builder bld(ctx->program, ctx->block);
847 Temp cond = get_alu_src(ctx, instr->src[0]);
848 Temp then = get_alu_src(ctx, instr->src[1]);
849 Temp els = get_alu_src(ctx, instr->src[2]);
850
851 assert(cond.regClass() == bld.lm);
852
853 if (dst.type() == RegType::vgpr) {
854 aco_ptr<Instruction> bcsel;
855 if (dst.size() == 1) {
856 then = as_vgpr(ctx, then);
857 els = as_vgpr(ctx, els);
858
859 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
860 } else if (dst.size() == 2) {
861 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
862 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
863 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
864 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
865
866 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
867 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
868
869 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
870 } else {
871 fprintf(stderr, "Unimplemented NIR instr bit size: ");
872 nir_print_instr(&instr->instr, stderr);
873 fprintf(stderr, "\n");
874 }
875 return;
876 }
877
878 if (instr->dest.dest.ssa.bit_size == 1) {
879 assert(dst.regClass() == bld.lm);
880 assert(then.regClass() == bld.lm);
881 assert(els.regClass() == bld.lm);
882 }
883
884 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
885 if (dst.regClass() == s1 || dst.regClass() == s2) {
886 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
887 assert(dst.size() == then.size());
888 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
889 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
890 } else {
891 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
892 nir_print_instr(&instr->instr, stderr);
893 fprintf(stderr, "\n");
894 }
895 return;
896 }
897
898 /* divergent boolean bcsel
899 * this implements bcsel on bools: dst = s0 ? s1 : s2
900 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
901 assert(instr->dest.dest.ssa.bit_size == 1);
902
903 if (cond.id() != then.id())
904 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
905
906 if (cond.id() == els.id())
907 bld.sop1(Builder::s_mov, Definition(dst), then);
908 else
909 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
910 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
911 }
912
913 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
914 aco_opcode op, uint32_t undo)
915 {
916 /* multiply by 16777216 to handle denormals */
917 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
918 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
919 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
920 scaled = bld.vop1(op, bld.def(v1), scaled);
921 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
922
923 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
924
925 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
926 }
927
928 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
929 {
930 if (ctx->block->fp_mode.denorm32 == 0) {
931 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
932 return;
933 }
934
935 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
936 }
937
938 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
939 {
940 if (ctx->block->fp_mode.denorm32 == 0) {
941 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
942 return;
943 }
944
945 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
946 }
947
948 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
949 {
950 if (ctx->block->fp_mode.denorm32 == 0) {
951 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
952 return;
953 }
954
955 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
956 }
957
958 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
959 {
960 if (ctx->block->fp_mode.denorm32 == 0) {
961 bld.vop1(aco_opcode::v_log_f32, dst, val);
962 return;
963 }
964
965 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
966 }
967
968 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
969 {
970 if (ctx->options->chip_class >= GFX7)
971 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
972
973 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
974 /* TODO: create more efficient code! */
975 if (val.type() == RegType::sgpr)
976 val = as_vgpr(ctx, val);
977
978 /* Split the input value. */
979 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
980 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
981
982 /* Extract the exponent and compute the unbiased value. */
983 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
984 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
985
986 /* Extract the fractional part. */
987 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
988 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
989
990 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
991 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
992
993 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
994 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
995 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
996 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
997 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
998
999 /* Get the sign bit. */
1000 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
1001
1002 /* Decide the operation to apply depending on the unbiased exponent. */
1003 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
1004 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
1005 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
1006 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
1007 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
1008 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
1009
1010 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
1011 }
1012
1013 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1014 {
1015 if (ctx->options->chip_class >= GFX7)
1016 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
1017
1018 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1019 * lowered at NIR level for precision reasons). */
1020 Temp src0 = as_vgpr(ctx, val);
1021
1022 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
1023 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
1024
1025 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
1026 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
1027 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
1028
1029 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
1030 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
1031 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
1032 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
1033
1034 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
1035 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
1036
1037 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
1038
1039 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
1040 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
1041
1042 return add->definitions[0].getTemp();
1043 }
1044
1045 Temp convert_int(isel_context *ctx, Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
1046 if (!dst.id()) {
1047 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
1048 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
1049 else
1050 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
1051 }
1052
1053 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
1054 return bld.copy(Definition(dst), src);
1055 else if (dst.bytes() < src.bytes())
1056 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
1057
1058 Temp tmp = dst;
1059 if (dst_bits == 64)
1060 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
1061
1062 if (tmp == src) {
1063 } else if (src.regClass() == s1) {
1064 if (is_signed)
1065 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
1066 else
1067 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
1068 } else if (ctx->options->chip_class >= GFX8) {
1069 assert(src_bits != 8 || src.regClass() == v1b);
1070 assert(src_bits != 16 || src.regClass() == v2b);
1071 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
1072 sdwa->operands[0] = Operand(src);
1073 sdwa->definitions[0] = Definition(tmp);
1074 if (is_signed)
1075 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
1076 else
1077 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
1078 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
1079 bld.insert(std::move(sdwa));
1080 } else {
1081 assert(ctx->options->chip_class == GFX6 || ctx->options->chip_class == GFX7);
1082 aco_opcode opcode = is_signed ? aco_opcode::v_bfe_i32 : aco_opcode::v_bfe_u32;
1083 bld.vop3(opcode, Definition(tmp), src, Operand(0u), Operand(src_bits == 8 ? 8u : 16u));
1084 }
1085
1086 if (dst_bits == 64) {
1087 if (is_signed && dst.regClass() == s2) {
1088 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
1089 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1090 } else if (is_signed && dst.regClass() == v2) {
1091 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
1092 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1093 } else {
1094 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
1095 }
1096 }
1097
1098 return dst;
1099 }
1100
1101 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1102 {
1103 if (!instr->dest.dest.is_ssa) {
1104 fprintf(stderr, "nir alu dst not in ssa: ");
1105 nir_print_instr(&instr->instr, stderr);
1106 fprintf(stderr, "\n");
1107 abort();
1108 }
1109 Builder bld(ctx->program, ctx->block);
1110 bld.is_precise = instr->exact;
1111 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1112 switch(instr->op) {
1113 case nir_op_vec2:
1114 case nir_op_vec3:
1115 case nir_op_vec4: {
1116 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1117 unsigned num = instr->dest.dest.ssa.num_components;
1118 for (unsigned i = 0; i < num; ++i)
1119 elems[i] = get_alu_src(ctx, instr->src[i]);
1120
1121 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1122 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1123 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1124 for (unsigned i = 0; i < num; ++i) {
1125 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1126 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1127 else
1128 vec->operands[i] = Operand{elems[i]};
1129 }
1130 vec->definitions[0] = Definition(dst);
1131 ctx->block->instructions.emplace_back(std::move(vec));
1132 ctx->allocated_vec.emplace(dst.id(), elems);
1133 } else {
1134 // TODO: that is a bit suboptimal..
1135 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1136 for (unsigned i = 0; i < num - 1; ++i)
1137 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1138 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1139 for (unsigned i = 0; i < num; ++i) {
1140 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1141 if (bit % 32 == 0) {
1142 elems[bit / 32] = elems[i];
1143 } else {
1144 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1145 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1146 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1147 }
1148 }
1149 if (dst.size() == 1)
1150 bld.copy(Definition(dst), elems[0]);
1151 else
1152 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1153 }
1154 break;
1155 }
1156 case nir_op_mov: {
1157 Temp src = get_alu_src(ctx, instr->src[0]);
1158 aco_ptr<Instruction> mov;
1159 if (dst.type() == RegType::sgpr) {
1160 if (src.type() == RegType::vgpr)
1161 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1162 else if (src.regClass() == s1)
1163 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1164 else if (src.regClass() == s2)
1165 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1166 else
1167 unreachable("wrong src register class for nir_op_imov");
1168 } else {
1169 if (dst.regClass() == v1)
1170 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1171 else if (dst.regClass() == v1b ||
1172 dst.regClass() == v2b ||
1173 dst.regClass() == v2)
1174 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1175 else
1176 unreachable("wrong src register class for nir_op_imov");
1177 }
1178 break;
1179 }
1180 case nir_op_inot: {
1181 Temp src = get_alu_src(ctx, instr->src[0]);
1182 if (instr->dest.dest.ssa.bit_size == 1) {
1183 assert(src.regClass() == bld.lm);
1184 assert(dst.regClass() == bld.lm);
1185 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1186 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1187 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1188 } else if (dst.regClass() == v1) {
1189 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1190 } else if (dst.regClass() == v2) {
1191 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
1192 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
1193 lo = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), lo);
1194 hi = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), hi);
1195 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
1196 } else if (dst.type() == RegType::sgpr) {
1197 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1198 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1199 } else {
1200 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1201 nir_print_instr(&instr->instr, stderr);
1202 fprintf(stderr, "\n");
1203 }
1204 break;
1205 }
1206 case nir_op_ineg: {
1207 Temp src = get_alu_src(ctx, instr->src[0]);
1208 if (dst.regClass() == v1) {
1209 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1210 } else if (dst.regClass() == s1) {
1211 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1212 } else if (dst.size() == 2) {
1213 Temp src0 = bld.tmp(dst.type(), 1);
1214 Temp src1 = bld.tmp(dst.type(), 1);
1215 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1216
1217 if (dst.regClass() == s2) {
1218 Temp carry = bld.tmp(s1);
1219 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1220 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1221 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1222 } else {
1223 Temp lower = bld.tmp(v1);
1224 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1225 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1226 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1227 }
1228 } else {
1229 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1230 nir_print_instr(&instr->instr, stderr);
1231 fprintf(stderr, "\n");
1232 }
1233 break;
1234 }
1235 case nir_op_iabs: {
1236 if (dst.regClass() == s1) {
1237 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1238 } else if (dst.regClass() == v1) {
1239 Temp src = get_alu_src(ctx, instr->src[0]);
1240 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1241 } else {
1242 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1243 nir_print_instr(&instr->instr, stderr);
1244 fprintf(stderr, "\n");
1245 }
1246 break;
1247 }
1248 case nir_op_isign: {
1249 Temp src = get_alu_src(ctx, instr->src[0]);
1250 if (dst.regClass() == s1) {
1251 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1252 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1253 } else if (dst.regClass() == s2) {
1254 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1255 Temp neqz;
1256 if (ctx->program->chip_class >= GFX8)
1257 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1258 else
1259 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1260 /* SCC gets zero-extended to 64 bit */
1261 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1262 } else if (dst.regClass() == v1) {
1263 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1264 } else if (dst.regClass() == v2) {
1265 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1266 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1267 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1268 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1269 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1270 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1271 } else {
1272 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1273 nir_print_instr(&instr->instr, stderr);
1274 fprintf(stderr, "\n");
1275 }
1276 break;
1277 }
1278 case nir_op_imax: {
1279 if (dst.regClass() == v1) {
1280 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1281 } else if (dst.regClass() == s1) {
1282 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1283 } else {
1284 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1285 nir_print_instr(&instr->instr, stderr);
1286 fprintf(stderr, "\n");
1287 }
1288 break;
1289 }
1290 case nir_op_umax: {
1291 if (dst.regClass() == v1) {
1292 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1293 } else if (dst.regClass() == s1) {
1294 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1295 } else {
1296 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1297 nir_print_instr(&instr->instr, stderr);
1298 fprintf(stderr, "\n");
1299 }
1300 break;
1301 }
1302 case nir_op_imin: {
1303 if (dst.regClass() == v1) {
1304 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1305 } else if (dst.regClass() == s1) {
1306 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1307 } else {
1308 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1309 nir_print_instr(&instr->instr, stderr);
1310 fprintf(stderr, "\n");
1311 }
1312 break;
1313 }
1314 case nir_op_umin: {
1315 if (dst.regClass() == v1) {
1316 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1317 } else if (dst.regClass() == s1) {
1318 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1319 } else {
1320 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1321 nir_print_instr(&instr->instr, stderr);
1322 fprintf(stderr, "\n");
1323 }
1324 break;
1325 }
1326 case nir_op_ior: {
1327 if (instr->dest.dest.ssa.bit_size == 1) {
1328 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1329 } else if (dst.regClass() == v1) {
1330 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1331 } else if (dst.regClass() == v2) {
1332 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_or_b32, dst);
1333 } else if (dst.regClass() == s1) {
1334 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1335 } else if (dst.regClass() == s2) {
1336 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1337 } else {
1338 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1339 nir_print_instr(&instr->instr, stderr);
1340 fprintf(stderr, "\n");
1341 }
1342 break;
1343 }
1344 case nir_op_iand: {
1345 if (instr->dest.dest.ssa.bit_size == 1) {
1346 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1347 } else if (dst.regClass() == v1) {
1348 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1349 } else if (dst.regClass() == v2) {
1350 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_and_b32, dst);
1351 } else if (dst.regClass() == s1) {
1352 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1353 } else if (dst.regClass() == s2) {
1354 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1355 } else {
1356 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1357 nir_print_instr(&instr->instr, stderr);
1358 fprintf(stderr, "\n");
1359 }
1360 break;
1361 }
1362 case nir_op_ixor: {
1363 if (instr->dest.dest.ssa.bit_size == 1) {
1364 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1365 } else if (dst.regClass() == v1) {
1366 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1367 } else if (dst.regClass() == v2) {
1368 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_xor_b32, dst);
1369 } else if (dst.regClass() == s1) {
1370 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1371 } else if (dst.regClass() == s2) {
1372 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1373 } else {
1374 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1375 nir_print_instr(&instr->instr, stderr);
1376 fprintf(stderr, "\n");
1377 }
1378 break;
1379 }
1380 case nir_op_ushr: {
1381 if (dst.regClass() == v1) {
1382 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1383 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1384 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1385 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1386 } else if (dst.regClass() == v2) {
1387 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1388 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1389 } else if (dst.regClass() == s2) {
1390 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1391 } else if (dst.regClass() == s1) {
1392 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1393 } else {
1394 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1395 nir_print_instr(&instr->instr, stderr);
1396 fprintf(stderr, "\n");
1397 }
1398 break;
1399 }
1400 case nir_op_ishl: {
1401 if (dst.regClass() == v1) {
1402 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1403 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1404 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1405 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1406 } else if (dst.regClass() == v2) {
1407 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1408 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1409 } else if (dst.regClass() == s1) {
1410 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1411 } else if (dst.regClass() == s2) {
1412 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1413 } else {
1414 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1415 nir_print_instr(&instr->instr, stderr);
1416 fprintf(stderr, "\n");
1417 }
1418 break;
1419 }
1420 case nir_op_ishr: {
1421 if (dst.regClass() == v1) {
1422 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1423 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1424 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1425 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1426 } else if (dst.regClass() == v2) {
1427 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1428 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1429 } else if (dst.regClass() == s1) {
1430 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1431 } else if (dst.regClass() == s2) {
1432 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1433 } else {
1434 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1435 nir_print_instr(&instr->instr, stderr);
1436 fprintf(stderr, "\n");
1437 }
1438 break;
1439 }
1440 case nir_op_find_lsb: {
1441 Temp src = get_alu_src(ctx, instr->src[0]);
1442 if (src.regClass() == s1) {
1443 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1444 } else if (src.regClass() == v1) {
1445 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1446 } else if (src.regClass() == s2) {
1447 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1448 } else {
1449 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1450 nir_print_instr(&instr->instr, stderr);
1451 fprintf(stderr, "\n");
1452 }
1453 break;
1454 }
1455 case nir_op_ufind_msb:
1456 case nir_op_ifind_msb: {
1457 Temp src = get_alu_src(ctx, instr->src[0]);
1458 if (src.regClass() == s1 || src.regClass() == s2) {
1459 aco_opcode op = src.regClass() == s2 ?
1460 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1461 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1462 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1463
1464 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1465 Operand(src.size() * 32u - 1u), msb_rev);
1466 Temp msb = sub.def(0).getTemp();
1467 Temp carry = sub.def(1).getTemp();
1468
1469 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1470 } else if (src.regClass() == v1) {
1471 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1472 Temp msb_rev = bld.tmp(v1);
1473 emit_vop1_instruction(ctx, instr, op, msb_rev);
1474 Temp msb = bld.tmp(v1);
1475 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1476 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1477 } else {
1478 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1479 nir_print_instr(&instr->instr, stderr);
1480 fprintf(stderr, "\n");
1481 }
1482 break;
1483 }
1484 case nir_op_bitfield_reverse: {
1485 if (dst.regClass() == s1) {
1486 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1487 } else if (dst.regClass() == v1) {
1488 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1489 } else {
1490 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1491 nir_print_instr(&instr->instr, stderr);
1492 fprintf(stderr, "\n");
1493 }
1494 break;
1495 }
1496 case nir_op_iadd: {
1497 if (dst.regClass() == s1) {
1498 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1499 break;
1500 }
1501
1502 Temp src0 = get_alu_src(ctx, instr->src[0]);
1503 Temp src1 = get_alu_src(ctx, instr->src[1]);
1504 if (dst.regClass() == v1) {
1505 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1506 break;
1507 }
1508
1509 assert(src0.size() == 2 && src1.size() == 2);
1510 Temp src00 = bld.tmp(src0.type(), 1);
1511 Temp src01 = bld.tmp(dst.type(), 1);
1512 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1513 Temp src10 = bld.tmp(src1.type(), 1);
1514 Temp src11 = bld.tmp(dst.type(), 1);
1515 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1516
1517 if (dst.regClass() == s2) {
1518 Temp carry = bld.tmp(s1);
1519 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1520 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1521 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1522 } else if (dst.regClass() == v2) {
1523 Temp dst0 = bld.tmp(v1);
1524 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1525 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1526 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1527 } else {
1528 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1529 nir_print_instr(&instr->instr, stderr);
1530 fprintf(stderr, "\n");
1531 }
1532 break;
1533 }
1534 case nir_op_uadd_sat: {
1535 Temp src0 = get_alu_src(ctx, instr->src[0]);
1536 Temp src1 = get_alu_src(ctx, instr->src[1]);
1537 if (dst.regClass() == s1) {
1538 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1539 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1540 src0, src1);
1541 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1542 } else if (dst.regClass() == v1) {
1543 if (ctx->options->chip_class >= GFX9) {
1544 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1545 add->operands[0] = Operand(src0);
1546 add->operands[1] = Operand(src1);
1547 add->definitions[0] = Definition(dst);
1548 add->clamp = 1;
1549 ctx->block->instructions.emplace_back(std::move(add));
1550 } else {
1551 if (src1.regClass() != v1)
1552 std::swap(src0, src1);
1553 assert(src1.regClass() == v1);
1554 Temp tmp = bld.tmp(v1);
1555 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1556 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1557 }
1558 } else {
1559 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1560 nir_print_instr(&instr->instr, stderr);
1561 fprintf(stderr, "\n");
1562 }
1563 break;
1564 }
1565 case nir_op_uadd_carry: {
1566 Temp src0 = get_alu_src(ctx, instr->src[0]);
1567 Temp src1 = get_alu_src(ctx, instr->src[1]);
1568 if (dst.regClass() == s1) {
1569 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1570 break;
1571 }
1572 if (dst.regClass() == v1) {
1573 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1574 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1575 break;
1576 }
1577
1578 Temp src00 = bld.tmp(src0.type(), 1);
1579 Temp src01 = bld.tmp(dst.type(), 1);
1580 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1581 Temp src10 = bld.tmp(src1.type(), 1);
1582 Temp src11 = bld.tmp(dst.type(), 1);
1583 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1584 if (dst.regClass() == s2) {
1585 Temp carry = bld.tmp(s1);
1586 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1587 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1588 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1589 } else if (dst.regClass() == v2) {
1590 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1591 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1592 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1593 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1594 } else {
1595 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1596 nir_print_instr(&instr->instr, stderr);
1597 fprintf(stderr, "\n");
1598 }
1599 break;
1600 }
1601 case nir_op_isub: {
1602 if (dst.regClass() == s1) {
1603 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1604 break;
1605 }
1606
1607 Temp src0 = get_alu_src(ctx, instr->src[0]);
1608 Temp src1 = get_alu_src(ctx, instr->src[1]);
1609 if (dst.regClass() == v1) {
1610 bld.vsub32(Definition(dst), src0, src1);
1611 break;
1612 }
1613
1614 Temp src00 = bld.tmp(src0.type(), 1);
1615 Temp src01 = bld.tmp(dst.type(), 1);
1616 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1617 Temp src10 = bld.tmp(src1.type(), 1);
1618 Temp src11 = bld.tmp(dst.type(), 1);
1619 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1620 if (dst.regClass() == s2) {
1621 Temp carry = bld.tmp(s1);
1622 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1623 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1624 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1625 } else if (dst.regClass() == v2) {
1626 Temp lower = bld.tmp(v1);
1627 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1628 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1629 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1630 } else {
1631 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1632 nir_print_instr(&instr->instr, stderr);
1633 fprintf(stderr, "\n");
1634 }
1635 break;
1636 }
1637 case nir_op_usub_borrow: {
1638 Temp src0 = get_alu_src(ctx, instr->src[0]);
1639 Temp src1 = get_alu_src(ctx, instr->src[1]);
1640 if (dst.regClass() == s1) {
1641 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1642 break;
1643 } else if (dst.regClass() == v1) {
1644 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1645 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1646 break;
1647 }
1648
1649 Temp src00 = bld.tmp(src0.type(), 1);
1650 Temp src01 = bld.tmp(dst.type(), 1);
1651 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1652 Temp src10 = bld.tmp(src1.type(), 1);
1653 Temp src11 = bld.tmp(dst.type(), 1);
1654 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1655 if (dst.regClass() == s2) {
1656 Temp borrow = bld.tmp(s1);
1657 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1658 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1659 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1660 } else if (dst.regClass() == v2) {
1661 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1662 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1663 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1664 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1665 } else {
1666 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1667 nir_print_instr(&instr->instr, stderr);
1668 fprintf(stderr, "\n");
1669 }
1670 break;
1671 }
1672 case nir_op_imul: {
1673 if (dst.regClass() == v1) {
1674 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1675 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1676 } else if (dst.regClass() == s1) {
1677 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1678 } else {
1679 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1680 nir_print_instr(&instr->instr, stderr);
1681 fprintf(stderr, "\n");
1682 }
1683 break;
1684 }
1685 case nir_op_umul_high: {
1686 if (dst.regClass() == v1) {
1687 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1688 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1689 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1690 } else if (dst.regClass() == s1) {
1691 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1692 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1693 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1694 } else {
1695 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1696 nir_print_instr(&instr->instr, stderr);
1697 fprintf(stderr, "\n");
1698 }
1699 break;
1700 }
1701 case nir_op_imul_high: {
1702 if (dst.regClass() == v1) {
1703 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1704 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1705 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1706 } else if (dst.regClass() == s1) {
1707 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1708 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1709 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1710 } else {
1711 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1712 nir_print_instr(&instr->instr, stderr);
1713 fprintf(stderr, "\n");
1714 }
1715 break;
1716 }
1717 case nir_op_fmul: {
1718 Temp src0 = get_alu_src(ctx, instr->src[0]);
1719 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1720 if (dst.regClass() == v2b) {
1721 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1722 } else if (dst.regClass() == v1) {
1723 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1724 } else if (dst.regClass() == v2) {
1725 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1726 } else {
1727 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1728 nir_print_instr(&instr->instr, stderr);
1729 fprintf(stderr, "\n");
1730 }
1731 break;
1732 }
1733 case nir_op_fadd: {
1734 Temp src0 = get_alu_src(ctx, instr->src[0]);
1735 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1736 if (dst.regClass() == v2b) {
1737 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1738 } else if (dst.regClass() == v1) {
1739 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1740 } else if (dst.regClass() == v2) {
1741 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1742 } else {
1743 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1744 nir_print_instr(&instr->instr, stderr);
1745 fprintf(stderr, "\n");
1746 }
1747 break;
1748 }
1749 case nir_op_fsub: {
1750 Temp src0 = get_alu_src(ctx, instr->src[0]);
1751 Temp src1 = get_alu_src(ctx, instr->src[1]);
1752 if (dst.regClass() == v2b) {
1753 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1754 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1755 else
1756 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1757 } else if (dst.regClass() == v1) {
1758 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1759 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1760 else
1761 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1762 } else if (dst.regClass() == v2) {
1763 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1764 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1765 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1766 sub->neg[1] = true;
1767 } else {
1768 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1769 nir_print_instr(&instr->instr, stderr);
1770 fprintf(stderr, "\n");
1771 }
1772 break;
1773 }
1774 case nir_op_fmax: {
1775 Temp src0 = get_alu_src(ctx, instr->src[0]);
1776 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1777 if (dst.regClass() == v2b) {
1778 // TODO: check fp_mode.must_flush_denorms16_64
1779 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1780 } else if (dst.regClass() == v1) {
1781 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1782 } else if (dst.regClass() == v2) {
1783 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1784 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1785 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1786 } else {
1787 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1788 }
1789 } else {
1790 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1791 nir_print_instr(&instr->instr, stderr);
1792 fprintf(stderr, "\n");
1793 }
1794 break;
1795 }
1796 case nir_op_fmin: {
1797 Temp src0 = get_alu_src(ctx, instr->src[0]);
1798 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1799 if (dst.regClass() == v2b) {
1800 // TODO: check fp_mode.must_flush_denorms16_64
1801 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1802 } else if (dst.regClass() == v1) {
1803 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1804 } else if (dst.regClass() == v2) {
1805 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1806 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1807 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1808 } else {
1809 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1810 }
1811 } else {
1812 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1813 nir_print_instr(&instr->instr, stderr);
1814 fprintf(stderr, "\n");
1815 }
1816 break;
1817 }
1818 case nir_op_fmax3: {
1819 if (dst.regClass() == v2b) {
1820 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1821 } else if (dst.regClass() == v1) {
1822 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1823 } else {
1824 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1825 nir_print_instr(&instr->instr, stderr);
1826 fprintf(stderr, "\n");
1827 }
1828 break;
1829 }
1830 case nir_op_fmin3: {
1831 if (dst.regClass() == v2b) {
1832 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1833 } else if (dst.regClass() == v1) {
1834 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1835 } else {
1836 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1837 nir_print_instr(&instr->instr, stderr);
1838 fprintf(stderr, "\n");
1839 }
1840 break;
1841 }
1842 case nir_op_fmed3: {
1843 if (dst.regClass() == v2b) {
1844 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1845 } else if (dst.regClass() == v1) {
1846 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1847 } else {
1848 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1849 nir_print_instr(&instr->instr, stderr);
1850 fprintf(stderr, "\n");
1851 }
1852 break;
1853 }
1854 case nir_op_umax3: {
1855 if (dst.size() == 1) {
1856 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1857 } else {
1858 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1859 nir_print_instr(&instr->instr, stderr);
1860 fprintf(stderr, "\n");
1861 }
1862 break;
1863 }
1864 case nir_op_umin3: {
1865 if (dst.size() == 1) {
1866 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1867 } else {
1868 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1869 nir_print_instr(&instr->instr, stderr);
1870 fprintf(stderr, "\n");
1871 }
1872 break;
1873 }
1874 case nir_op_umed3: {
1875 if (dst.size() == 1) {
1876 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1877 } else {
1878 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1879 nir_print_instr(&instr->instr, stderr);
1880 fprintf(stderr, "\n");
1881 }
1882 break;
1883 }
1884 case nir_op_imax3: {
1885 if (dst.size() == 1) {
1886 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1887 } else {
1888 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1889 nir_print_instr(&instr->instr, stderr);
1890 fprintf(stderr, "\n");
1891 }
1892 break;
1893 }
1894 case nir_op_imin3: {
1895 if (dst.size() == 1) {
1896 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1897 } else {
1898 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1899 nir_print_instr(&instr->instr, stderr);
1900 fprintf(stderr, "\n");
1901 }
1902 break;
1903 }
1904 case nir_op_imed3: {
1905 if (dst.size() == 1) {
1906 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1907 } else {
1908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1909 nir_print_instr(&instr->instr, stderr);
1910 fprintf(stderr, "\n");
1911 }
1912 break;
1913 }
1914 case nir_op_cube_face_coord: {
1915 Temp in = get_alu_src(ctx, instr->src[0], 3);
1916 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1917 emit_extract_vector(ctx, in, 1, v1),
1918 emit_extract_vector(ctx, in, 2, v1) };
1919 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1920 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1921 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1922 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1923 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1924 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1925 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1926 break;
1927 }
1928 case nir_op_cube_face_index: {
1929 Temp in = get_alu_src(ctx, instr->src[0], 3);
1930 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1931 emit_extract_vector(ctx, in, 1, v1),
1932 emit_extract_vector(ctx, in, 2, v1) };
1933 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1934 break;
1935 }
1936 case nir_op_bcsel: {
1937 emit_bcsel(ctx, instr, dst);
1938 break;
1939 }
1940 case nir_op_frsq: {
1941 Temp src = get_alu_src(ctx, instr->src[0]);
1942 if (dst.regClass() == v2b) {
1943 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1944 } else if (dst.regClass() == v1) {
1945 emit_rsq(ctx, bld, Definition(dst), src);
1946 } else if (dst.regClass() == v2) {
1947 /* Lowered at NIR level for precision reasons. */
1948 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1949 } else {
1950 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1951 nir_print_instr(&instr->instr, stderr);
1952 fprintf(stderr, "\n");
1953 }
1954 break;
1955 }
1956 case nir_op_fneg: {
1957 Temp src = get_alu_src(ctx, instr->src[0]);
1958 if (dst.regClass() == v2b) {
1959 if (ctx->block->fp_mode.must_flush_denorms16_64)
1960 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1961 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1962 } else if (dst.regClass() == v1) {
1963 if (ctx->block->fp_mode.must_flush_denorms32)
1964 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1965 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1966 } else if (dst.regClass() == v2) {
1967 if (ctx->block->fp_mode.must_flush_denorms16_64)
1968 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1969 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1970 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1971 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1972 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1973 } else {
1974 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1975 nir_print_instr(&instr->instr, stderr);
1976 fprintf(stderr, "\n");
1977 }
1978 break;
1979 }
1980 case nir_op_fabs: {
1981 Temp src = get_alu_src(ctx, instr->src[0]);
1982 if (dst.regClass() == v2b) {
1983 if (ctx->block->fp_mode.must_flush_denorms16_64)
1984 src = bld.vop2(aco_opcode::v_mul_f16, bld.def(v2b), Operand((uint16_t)0x3C00), as_vgpr(ctx, src));
1985 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1986 } else if (dst.regClass() == v1) {
1987 if (ctx->block->fp_mode.must_flush_denorms32)
1988 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1989 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1990 } else if (dst.regClass() == v2) {
1991 if (ctx->block->fp_mode.must_flush_denorms16_64)
1992 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1993 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1994 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1995 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1996 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1997 } else {
1998 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1999 nir_print_instr(&instr->instr, stderr);
2000 fprintf(stderr, "\n");
2001 }
2002 break;
2003 }
2004 case nir_op_fsat: {
2005 Temp src = get_alu_src(ctx, instr->src[0]);
2006 if (dst.regClass() == v2b) {
2007 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src);
2008 } else if (dst.regClass() == v1) {
2009 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2010 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
2011 // TODO: confirm that this holds under any circumstances
2012 } else if (dst.regClass() == v2) {
2013 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
2014 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
2015 vop3->clamp = true;
2016 } else {
2017 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2018 nir_print_instr(&instr->instr, stderr);
2019 fprintf(stderr, "\n");
2020 }
2021 break;
2022 }
2023 case nir_op_flog2: {
2024 Temp src = get_alu_src(ctx, instr->src[0]);
2025 if (dst.regClass() == v2b) {
2026 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
2027 } else if (dst.regClass() == v1) {
2028 emit_log2(ctx, bld, Definition(dst), src);
2029 } else {
2030 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2031 nir_print_instr(&instr->instr, stderr);
2032 fprintf(stderr, "\n");
2033 }
2034 break;
2035 }
2036 case nir_op_frcp: {
2037 Temp src = get_alu_src(ctx, instr->src[0]);
2038 if (dst.regClass() == v2b) {
2039 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
2040 } else if (dst.regClass() == v1) {
2041 emit_rcp(ctx, bld, Definition(dst), src);
2042 } else if (dst.regClass() == v2) {
2043 /* Lowered at NIR level for precision reasons. */
2044 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
2045 } else {
2046 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2047 nir_print_instr(&instr->instr, stderr);
2048 fprintf(stderr, "\n");
2049 }
2050 break;
2051 }
2052 case nir_op_fexp2: {
2053 if (dst.regClass() == v2b) {
2054 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
2055 } else if (dst.regClass() == v1) {
2056 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
2057 } else {
2058 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2059 nir_print_instr(&instr->instr, stderr);
2060 fprintf(stderr, "\n");
2061 }
2062 break;
2063 }
2064 case nir_op_fsqrt: {
2065 Temp src = get_alu_src(ctx, instr->src[0]);
2066 if (dst.regClass() == v2b) {
2067 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
2068 } else if (dst.regClass() == v1) {
2069 emit_sqrt(ctx, bld, Definition(dst), src);
2070 } else if (dst.regClass() == v2) {
2071 /* Lowered at NIR level for precision reasons. */
2072 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
2073 } else {
2074 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2075 nir_print_instr(&instr->instr, stderr);
2076 fprintf(stderr, "\n");
2077 }
2078 break;
2079 }
2080 case nir_op_ffract: {
2081 if (dst.regClass() == v2b) {
2082 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
2083 } else if (dst.regClass() == v1) {
2084 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
2085 } else if (dst.regClass() == v2) {
2086 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
2087 } else {
2088 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr->instr, stderr);
2090 fprintf(stderr, "\n");
2091 }
2092 break;
2093 }
2094 case nir_op_ffloor: {
2095 Temp src = get_alu_src(ctx, instr->src[0]);
2096 if (dst.regClass() == v2b) {
2097 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
2098 } else if (dst.regClass() == v1) {
2099 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
2100 } else if (dst.regClass() == v2) {
2101 emit_floor_f64(ctx, bld, Definition(dst), src);
2102 } else {
2103 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2104 nir_print_instr(&instr->instr, stderr);
2105 fprintf(stderr, "\n");
2106 }
2107 break;
2108 }
2109 case nir_op_fceil: {
2110 Temp src0 = get_alu_src(ctx, instr->src[0]);
2111 if (dst.regClass() == v2b) {
2112 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
2113 } else if (dst.regClass() == v1) {
2114 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2115 } else if (dst.regClass() == v2) {
2116 if (ctx->options->chip_class >= GFX7) {
2117 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2118 } else {
2119 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2120 /* trunc = trunc(src0)
2121 * if (src0 > 0.0 && src0 != trunc)
2122 * trunc += 1.0
2123 */
2124 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2125 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2126 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2127 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2128 Temp add = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), bld.copy(bld.def(v1), Operand(0u)), bld.copy(bld.def(v1), Operand(0x3ff00000u)), cond);
2129 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2130 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2131 }
2132 } else {
2133 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2134 nir_print_instr(&instr->instr, stderr);
2135 fprintf(stderr, "\n");
2136 }
2137 break;
2138 }
2139 case nir_op_ftrunc: {
2140 Temp src = get_alu_src(ctx, instr->src[0]);
2141 if (dst.regClass() == v2b) {
2142 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2143 } else if (dst.regClass() == v1) {
2144 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2145 } else if (dst.regClass() == v2) {
2146 emit_trunc_f64(ctx, bld, Definition(dst), src);
2147 } else {
2148 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2149 nir_print_instr(&instr->instr, stderr);
2150 fprintf(stderr, "\n");
2151 }
2152 break;
2153 }
2154 case nir_op_fround_even: {
2155 Temp src0 = get_alu_src(ctx, instr->src[0]);
2156 if (dst.regClass() == v2b) {
2157 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2158 } else if (dst.regClass() == v1) {
2159 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2160 } else if (dst.regClass() == v2) {
2161 if (ctx->options->chip_class >= GFX7) {
2162 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2163 } else {
2164 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2165 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2166 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2167
2168 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2169 Temp bfi = bld.vop3(aco_opcode::v_bfi_b32, bld.def(v1), bitmask, bld.copy(bld.def(v1), Operand(0x43300000u)), as_vgpr(ctx, src0_hi));
2170 Temp tmp = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), src0, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
2171 Instruction *sub = bld.vop3(aco_opcode::v_add_f64, bld.def(v2), tmp, bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), bfi));
2172 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2173 tmp = sub->definitions[0].getTemp();
2174
2175 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2176 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2177 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2178 Temp cond = vop3->definitions[0].getTemp();
2179
2180 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2181 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2182 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2183 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2184
2185 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2186 }
2187 } else {
2188 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2189 nir_print_instr(&instr->instr, stderr);
2190 fprintf(stderr, "\n");
2191 }
2192 break;
2193 }
2194 case nir_op_fsin:
2195 case nir_op_fcos: {
2196 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2197 aco_ptr<Instruction> norm;
2198 if (dst.regClass() == v2b) {
2199 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3118u));
2200 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2201 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2202 bld.vop1(opcode, Definition(dst), tmp);
2203 } else if (dst.regClass() == v1) {
2204 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2205 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2206
2207 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2208 if (ctx->options->chip_class < GFX9)
2209 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2210
2211 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2212 bld.vop1(opcode, Definition(dst), tmp);
2213 } else {
2214 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2215 nir_print_instr(&instr->instr, stderr);
2216 fprintf(stderr, "\n");
2217 }
2218 break;
2219 }
2220 case nir_op_ldexp: {
2221 Temp src0 = get_alu_src(ctx, instr->src[0]);
2222 Temp src1 = get_alu_src(ctx, instr->src[1]);
2223 if (dst.regClass() == v2b) {
2224 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2225 } else if (dst.regClass() == v1) {
2226 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2227 } else if (dst.regClass() == v2) {
2228 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2229 } else {
2230 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2231 nir_print_instr(&instr->instr, stderr);
2232 fprintf(stderr, "\n");
2233 }
2234 break;
2235 }
2236 case nir_op_frexp_sig: {
2237 Temp src = get_alu_src(ctx, instr->src[0]);
2238 if (dst.regClass() == v2b) {
2239 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2240 } else if (dst.regClass() == v1) {
2241 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2242 } else if (dst.regClass() == v2) {
2243 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2244 } else {
2245 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2246 nir_print_instr(&instr->instr, stderr);
2247 fprintf(stderr, "\n");
2248 }
2249 break;
2250 }
2251 case nir_op_frexp_exp: {
2252 Temp src = get_alu_src(ctx, instr->src[0]);
2253 if (instr->src[0].src.ssa->bit_size == 16) {
2254 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2255 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2256 convert_int(ctx, bld, tmp, 8, 32, true, dst);
2257 } else if (instr->src[0].src.ssa->bit_size == 32) {
2258 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2259 } else if (instr->src[0].src.ssa->bit_size == 64) {
2260 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2261 } else {
2262 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2263 nir_print_instr(&instr->instr, stderr);
2264 fprintf(stderr, "\n");
2265 }
2266 break;
2267 }
2268 case nir_op_fsign: {
2269 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2270 if (dst.regClass() == v2b) {
2271 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2272 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2273 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2274 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2275 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2276 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2277 } else if (dst.regClass() == v1) {
2278 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2279 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2280 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2281 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2282 } else if (dst.regClass() == v2) {
2283 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2284 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2285 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2286
2287 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2288 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2289 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2290
2291 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2292 } else {
2293 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2294 nir_print_instr(&instr->instr, stderr);
2295 fprintf(stderr, "\n");
2296 }
2297 break;
2298 }
2299 case nir_op_f2f16:
2300 case nir_op_f2f16_rtne: {
2301 Temp src = get_alu_src(ctx, instr->src[0]);
2302 if (instr->src[0].src.ssa->bit_size == 64)
2303 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2304 if (instr->op == nir_op_f2f16_rtne && ctx->block->fp_mode.round16_64 != fp_round_ne)
2305 /* We emit s_round_mode/s_setreg_imm32 in lower_to_hw_instr to
2306 * keep value numbering and the scheduler simpler.
2307 */
2308 bld.vop1(aco_opcode::p_cvt_f16_f32_rtne, Definition(dst), src);
2309 else
2310 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2311 break;
2312 }
2313 case nir_op_f2f16_rtz: {
2314 Temp src = get_alu_src(ctx, instr->src[0]);
2315 if (instr->src[0].src.ssa->bit_size == 64)
2316 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2317 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2318 break;
2319 }
2320 case nir_op_f2f32: {
2321 if (instr->src[0].src.ssa->bit_size == 16) {
2322 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2323 } else if (instr->src[0].src.ssa->bit_size == 64) {
2324 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2325 } else {
2326 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2327 nir_print_instr(&instr->instr, stderr);
2328 fprintf(stderr, "\n");
2329 }
2330 break;
2331 }
2332 case nir_op_f2f64: {
2333 Temp src = get_alu_src(ctx, instr->src[0]);
2334 if (instr->src[0].src.ssa->bit_size == 16)
2335 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2336 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2337 break;
2338 }
2339 case nir_op_i2f16: {
2340 assert(dst.regClass() == v2b);
2341 Temp src = get_alu_src(ctx, instr->src[0]);
2342 if (instr->src[0].src.ssa->bit_size == 8)
2343 src = convert_int(ctx, bld, src, 8, 16, true);
2344 else if (instr->src[0].src.ssa->bit_size == 64)
2345 src = convert_int(ctx, bld, src, 64, 32, false);
2346 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2347 break;
2348 }
2349 case nir_op_i2f32: {
2350 assert(dst.size() == 1);
2351 Temp src = get_alu_src(ctx, instr->src[0]);
2352 if (instr->src[0].src.ssa->bit_size <= 16)
2353 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2354 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2355 break;
2356 }
2357 case nir_op_i2f64: {
2358 if (instr->src[0].src.ssa->bit_size <= 32) {
2359 Temp src = get_alu_src(ctx, instr->src[0]);
2360 if (instr->src[0].src.ssa->bit_size <= 16)
2361 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2362 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2363 } else if (instr->src[0].src.ssa->bit_size == 64) {
2364 Temp src = get_alu_src(ctx, instr->src[0]);
2365 RegClass rc = RegClass(src.type(), 1);
2366 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2367 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2368 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2369 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2370 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2371 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2372
2373 } else {
2374 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2375 nir_print_instr(&instr->instr, stderr);
2376 fprintf(stderr, "\n");
2377 }
2378 break;
2379 }
2380 case nir_op_u2f16: {
2381 assert(dst.regClass() == v2b);
2382 Temp src = get_alu_src(ctx, instr->src[0]);
2383 if (instr->src[0].src.ssa->bit_size == 8)
2384 src = convert_int(ctx, bld, src, 8, 16, false);
2385 else if (instr->src[0].src.ssa->bit_size == 64)
2386 src = convert_int(ctx, bld, src, 64, 32, false);
2387 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2388 break;
2389 }
2390 case nir_op_u2f32: {
2391 assert(dst.size() == 1);
2392 Temp src = get_alu_src(ctx, instr->src[0]);
2393 if (instr->src[0].src.ssa->bit_size == 8) {
2394 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2395 } else {
2396 if (instr->src[0].src.ssa->bit_size == 16)
2397 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2398 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2399 }
2400 break;
2401 }
2402 case nir_op_u2f64: {
2403 if (instr->src[0].src.ssa->bit_size <= 32) {
2404 Temp src = get_alu_src(ctx, instr->src[0]);
2405 if (instr->src[0].src.ssa->bit_size <= 16)
2406 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2407 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2408 } else if (instr->src[0].src.ssa->bit_size == 64) {
2409 Temp src = get_alu_src(ctx, instr->src[0]);
2410 RegClass rc = RegClass(src.type(), 1);
2411 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2412 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2413 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2414 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2415 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2416 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2417 } else {
2418 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2419 nir_print_instr(&instr->instr, stderr);
2420 fprintf(stderr, "\n");
2421 }
2422 break;
2423 }
2424 case nir_op_f2i8:
2425 case nir_op_f2i16: {
2426 if (instr->src[0].src.ssa->bit_size == 16)
2427 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i16_f16, dst);
2428 else if (instr->src[0].src.ssa->bit_size == 32)
2429 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2430 else
2431 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2432 break;
2433 }
2434 case nir_op_f2u8:
2435 case nir_op_f2u16: {
2436 if (instr->src[0].src.ssa->bit_size == 16)
2437 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u16_f16, dst);
2438 else if (instr->src[0].src.ssa->bit_size == 32)
2439 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2440 else
2441 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2442 break;
2443 }
2444 case nir_op_f2i32: {
2445 Temp src = get_alu_src(ctx, instr->src[0]);
2446 if (instr->src[0].src.ssa->bit_size == 16) {
2447 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2448 if (dst.type() == RegType::vgpr) {
2449 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2450 } else {
2451 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2452 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2453 }
2454 } else if (instr->src[0].src.ssa->bit_size == 32) {
2455 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2456 } else if (instr->src[0].src.ssa->bit_size == 64) {
2457 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2458 } else {
2459 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2460 nir_print_instr(&instr->instr, stderr);
2461 fprintf(stderr, "\n");
2462 }
2463 break;
2464 }
2465 case nir_op_f2u32: {
2466 Temp src = get_alu_src(ctx, instr->src[0]);
2467 if (instr->src[0].src.ssa->bit_size == 16) {
2468 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2469 if (dst.type() == RegType::vgpr) {
2470 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2471 } else {
2472 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2473 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2474 }
2475 } else if (instr->src[0].src.ssa->bit_size == 32) {
2476 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2477 } else if (instr->src[0].src.ssa->bit_size == 64) {
2478 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2479 } else {
2480 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2481 nir_print_instr(&instr->instr, stderr);
2482 fprintf(stderr, "\n");
2483 }
2484 break;
2485 }
2486 case nir_op_f2i64: {
2487 Temp src = get_alu_src(ctx, instr->src[0]);
2488 if (instr->src[0].src.ssa->bit_size == 16)
2489 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2490
2491 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2492 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2493 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2494 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2495 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2496 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2497 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2498 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2499 Temp new_exponent = bld.tmp(v1);
2500 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2501 if (ctx->program->chip_class >= GFX8)
2502 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2503 else
2504 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2505 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2506 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2507 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2508 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2509 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2510 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2511 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2512 Temp new_lower = bld.tmp(v1);
2513 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2514 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2515 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2516
2517 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2518 if (src.type() == RegType::vgpr)
2519 src = bld.as_uniform(src);
2520 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2521 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2522 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2523 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2524 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2525 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2526 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2527 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2528 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2529 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2530 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2531 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2532 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2533 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2534 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2535 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2536 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2537 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2538 Temp borrow = bld.tmp(s1);
2539 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2540 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2541 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2542
2543 } else if (instr->src[0].src.ssa->bit_size == 64) {
2544 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2545 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2546 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2547 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2548 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2549 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2550 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2551 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2552 if (dst.type() == RegType::sgpr) {
2553 lower = bld.as_uniform(lower);
2554 upper = bld.as_uniform(upper);
2555 }
2556 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2557
2558 } else {
2559 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2560 nir_print_instr(&instr->instr, stderr);
2561 fprintf(stderr, "\n");
2562 }
2563 break;
2564 }
2565 case nir_op_f2u64: {
2566 Temp src = get_alu_src(ctx, instr->src[0]);
2567 if (instr->src[0].src.ssa->bit_size == 16)
2568 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2569
2570 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2571 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2572 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2573 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2574 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2575 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2576 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2577 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2578 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2579 Temp new_exponent = bld.tmp(v1);
2580 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2581 if (ctx->program->chip_class >= GFX8)
2582 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2583 else
2584 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2585 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2586 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2587 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2588 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2589 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2590 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2591 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2592
2593 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2594 if (src.type() == RegType::vgpr)
2595 src = bld.as_uniform(src);
2596 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2597 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2598 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2599 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2600 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2601 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2602 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2603 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2604 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2605 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2606 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2607 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2608 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2609 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2610 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2611 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2612 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2613 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2614
2615 } else if (instr->src[0].src.ssa->bit_size == 64) {
2616 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2617 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2618 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2619 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2620 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2621 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2622 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2623 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2624 if (dst.type() == RegType::sgpr) {
2625 lower = bld.as_uniform(lower);
2626 upper = bld.as_uniform(upper);
2627 }
2628 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2629
2630 } else {
2631 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2632 nir_print_instr(&instr->instr, stderr);
2633 fprintf(stderr, "\n");
2634 }
2635 break;
2636 }
2637 case nir_op_b2f16: {
2638 Temp src = get_alu_src(ctx, instr->src[0]);
2639 assert(src.regClass() == bld.lm);
2640
2641 if (dst.regClass() == s1) {
2642 src = bool_to_scalar_condition(ctx, src);
2643 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2644 } else if (dst.regClass() == v2b) {
2645 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2646 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2647 } else {
2648 unreachable("Wrong destination register class for nir_op_b2f16.");
2649 }
2650 break;
2651 }
2652 case nir_op_b2f32: {
2653 Temp src = get_alu_src(ctx, instr->src[0]);
2654 assert(src.regClass() == bld.lm);
2655
2656 if (dst.regClass() == s1) {
2657 src = bool_to_scalar_condition(ctx, src);
2658 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2659 } else if (dst.regClass() == v1) {
2660 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2661 } else {
2662 unreachable("Wrong destination register class for nir_op_b2f32.");
2663 }
2664 break;
2665 }
2666 case nir_op_b2f64: {
2667 Temp src = get_alu_src(ctx, instr->src[0]);
2668 assert(src.regClass() == bld.lm);
2669
2670 if (dst.regClass() == s2) {
2671 src = bool_to_scalar_condition(ctx, src);
2672 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2673 } else if (dst.regClass() == v2) {
2674 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2675 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2676 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2677 } else {
2678 unreachable("Wrong destination register class for nir_op_b2f64.");
2679 }
2680 break;
2681 }
2682 case nir_op_i2i8:
2683 case nir_op_i2i16:
2684 case nir_op_i2i32:
2685 case nir_op_i2i64: {
2686 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2687 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2688 break;
2689 }
2690 case nir_op_u2u8:
2691 case nir_op_u2u16:
2692 case nir_op_u2u32:
2693 case nir_op_u2u64: {
2694 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2695 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2696 break;
2697 }
2698 case nir_op_b2b32:
2699 case nir_op_b2i8:
2700 case nir_op_b2i16:
2701 case nir_op_b2i32:
2702 case nir_op_b2i64: {
2703 Temp src = get_alu_src(ctx, instr->src[0]);
2704 assert(src.regClass() == bld.lm);
2705
2706 Temp tmp = dst.bytes() == 8 ? bld.tmp(RegClass::get(dst.type(), 4)) : dst;
2707 if (tmp.regClass() == s1) {
2708 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2709 bool_to_scalar_condition(ctx, src, tmp);
2710 } else if (tmp.type() == RegType::vgpr) {
2711 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(tmp), Operand(0u), Operand(1u), src);
2712 } else {
2713 unreachable("Invalid register class for b2i32");
2714 }
2715
2716 if (tmp != dst)
2717 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
2718 break;
2719 }
2720 case nir_op_b2b1:
2721 case nir_op_i2b1: {
2722 Temp src = get_alu_src(ctx, instr->src[0]);
2723 assert(dst.regClass() == bld.lm);
2724
2725 if (src.type() == RegType::vgpr) {
2726 assert(src.regClass() == v1 || src.regClass() == v2);
2727 assert(dst.regClass() == bld.lm);
2728 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2729 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2730 } else {
2731 assert(src.regClass() == s1 || src.regClass() == s2);
2732 Temp tmp;
2733 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2734 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2735 } else {
2736 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2737 bld.scc(bld.def(s1)), Operand(0u), src);
2738 }
2739 bool_to_vector_condition(ctx, tmp, dst);
2740 }
2741 break;
2742 }
2743 case nir_op_pack_64_2x32_split: {
2744 Temp src0 = get_alu_src(ctx, instr->src[0]);
2745 Temp src1 = get_alu_src(ctx, instr->src[1]);
2746
2747 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2748 break;
2749 }
2750 case nir_op_unpack_64_2x32_split_x:
2751 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2752 break;
2753 case nir_op_unpack_64_2x32_split_y:
2754 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2755 break;
2756 case nir_op_unpack_32_2x16_split_x:
2757 if (dst.type() == RegType::vgpr) {
2758 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2759 } else {
2760 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2761 }
2762 break;
2763 case nir_op_unpack_32_2x16_split_y:
2764 if (dst.type() == RegType::vgpr) {
2765 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2766 } else {
2767 bld.sop2(aco_opcode::s_bfe_u32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]), Operand(uint32_t(16 << 16 | 16)));
2768 }
2769 break;
2770 case nir_op_pack_32_2x16_split: {
2771 Temp src0 = get_alu_src(ctx, instr->src[0]);
2772 Temp src1 = get_alu_src(ctx, instr->src[1]);
2773 if (dst.regClass() == v1) {
2774 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2775 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2776 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2777 } else {
2778 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2779 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2780 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2781 }
2782 break;
2783 }
2784 case nir_op_pack_half_2x16: {
2785 Temp src = get_alu_src(ctx, instr->src[0], 2);
2786
2787 if (dst.regClass() == v1) {
2788 Temp src0 = bld.tmp(v1);
2789 Temp src1 = bld.tmp(v1);
2790 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2791 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2792 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2793 else
2794 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2795 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2796 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2797 } else {
2798 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2799 nir_print_instr(&instr->instr, stderr);
2800 fprintf(stderr, "\n");
2801 }
2802 break;
2803 }
2804 case nir_op_unpack_half_2x16_split_x: {
2805 if (dst.regClass() == v1) {
2806 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2807 } else {
2808 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2809 nir_print_instr(&instr->instr, stderr);
2810 fprintf(stderr, "\n");
2811 }
2812 break;
2813 }
2814 case nir_op_unpack_half_2x16_split_y: {
2815 if (dst.regClass() == v1) {
2816 /* TODO: use SDWA here */
2817 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2818 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2819 } else {
2820 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2821 nir_print_instr(&instr->instr, stderr);
2822 fprintf(stderr, "\n");
2823 }
2824 break;
2825 }
2826 case nir_op_fquantize2f16: {
2827 Temp src = get_alu_src(ctx, instr->src[0]);
2828 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2829 Temp f32, cmp_res;
2830
2831 if (ctx->program->chip_class >= GFX8) {
2832 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2833 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2834 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2835 } else {
2836 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2837 * so compare the result and flush to 0 if it's smaller.
2838 */
2839 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2840 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2841 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2842 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2843 cmp_res = vop3->definitions[0].getTemp();
2844 }
2845
2846 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2847 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2848 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2849 } else {
2850 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2851 }
2852 break;
2853 }
2854 case nir_op_bfm: {
2855 Temp bits = get_alu_src(ctx, instr->src[0]);
2856 Temp offset = get_alu_src(ctx, instr->src[1]);
2857
2858 if (dst.regClass() == s1) {
2859 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2860 } else if (dst.regClass() == v1) {
2861 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2862 } else {
2863 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2864 nir_print_instr(&instr->instr, stderr);
2865 fprintf(stderr, "\n");
2866 }
2867 break;
2868 }
2869 case nir_op_bitfield_select: {
2870 /* (mask & insert) | (~mask & base) */
2871 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2872 Temp insert = get_alu_src(ctx, instr->src[1]);
2873 Temp base = get_alu_src(ctx, instr->src[2]);
2874
2875 /* dst = (insert & bitmask) | (base & ~bitmask) */
2876 if (dst.regClass() == s1) {
2877 aco_ptr<Instruction> sop2;
2878 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2879 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2880 Operand lhs;
2881 if (const_insert && const_bitmask) {
2882 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2883 } else {
2884 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2885 lhs = Operand(insert);
2886 }
2887
2888 Operand rhs;
2889 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2890 if (const_base && const_bitmask) {
2891 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2892 } else {
2893 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2894 rhs = Operand(base);
2895 }
2896
2897 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2898
2899 } else if (dst.regClass() == v1) {
2900 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2901 base = as_vgpr(ctx, base);
2902 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2903 insert = as_vgpr(ctx, insert);
2904
2905 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2906
2907 } else {
2908 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2909 nir_print_instr(&instr->instr, stderr);
2910 fprintf(stderr, "\n");
2911 }
2912 break;
2913 }
2914 case nir_op_ubfe:
2915 case nir_op_ibfe: {
2916 Temp base = get_alu_src(ctx, instr->src[0]);
2917 Temp offset = get_alu_src(ctx, instr->src[1]);
2918 Temp bits = get_alu_src(ctx, instr->src[2]);
2919
2920 if (dst.type() == RegType::sgpr) {
2921 Operand extract;
2922 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2923 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2924 if (const_offset && const_bits) {
2925 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2926 extract = Operand(const_extract);
2927 } else {
2928 Operand width;
2929 if (const_bits) {
2930 width = Operand(const_bits->u32 << 16);
2931 } else {
2932 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2933 }
2934 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2935 }
2936
2937 aco_opcode opcode;
2938 if (dst.regClass() == s1) {
2939 if (instr->op == nir_op_ubfe)
2940 opcode = aco_opcode::s_bfe_u32;
2941 else
2942 opcode = aco_opcode::s_bfe_i32;
2943 } else if (dst.regClass() == s2) {
2944 if (instr->op == nir_op_ubfe)
2945 opcode = aco_opcode::s_bfe_u64;
2946 else
2947 opcode = aco_opcode::s_bfe_i64;
2948 } else {
2949 unreachable("Unsupported BFE bit size");
2950 }
2951
2952 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2953
2954 } else {
2955 aco_opcode opcode;
2956 if (dst.regClass() == v1) {
2957 if (instr->op == nir_op_ubfe)
2958 opcode = aco_opcode::v_bfe_u32;
2959 else
2960 opcode = aco_opcode::v_bfe_i32;
2961 } else {
2962 unreachable("Unsupported BFE bit size");
2963 }
2964
2965 emit_vop3a_instruction(ctx, instr, opcode, dst);
2966 }
2967 break;
2968 }
2969 case nir_op_bit_count: {
2970 Temp src = get_alu_src(ctx, instr->src[0]);
2971 if (src.regClass() == s1) {
2972 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2973 } else if (src.regClass() == v1) {
2974 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2975 } else if (src.regClass() == v2) {
2976 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2977 emit_extract_vector(ctx, src, 1, v1),
2978 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2979 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2980 } else if (src.regClass() == s2) {
2981 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2982 } else {
2983 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2984 nir_print_instr(&instr->instr, stderr);
2985 fprintf(stderr, "\n");
2986 }
2987 break;
2988 }
2989 case nir_op_flt: {
2990 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2991 break;
2992 }
2993 case nir_op_fge: {
2994 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2995 break;
2996 }
2997 case nir_op_feq: {
2998 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2999 break;
3000 }
3001 case nir_op_fne: {
3002 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
3003 break;
3004 }
3005 case nir_op_ilt: {
3006 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_i16, aco_opcode::v_cmp_lt_i32, aco_opcode::v_cmp_lt_i64, aco_opcode::s_cmp_lt_i32);
3007 break;
3008 }
3009 case nir_op_ige: {
3010 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_i16, aco_opcode::v_cmp_ge_i32, aco_opcode::v_cmp_ge_i64, aco_opcode::s_cmp_ge_i32);
3011 break;
3012 }
3013 case nir_op_ieq: {
3014 if (instr->src[0].src.ssa->bit_size == 1)
3015 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
3016 else
3017 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_i16, aco_opcode::v_cmp_eq_i32, aco_opcode::v_cmp_eq_i64, aco_opcode::s_cmp_eq_i32,
3018 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
3019 break;
3020 }
3021 case nir_op_ine: {
3022 if (instr->src[0].src.ssa->bit_size == 1)
3023 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
3024 else
3025 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lg_i16, aco_opcode::v_cmp_lg_i32, aco_opcode::v_cmp_lg_i64, aco_opcode::s_cmp_lg_i32,
3026 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
3027 break;
3028 }
3029 case nir_op_ult: {
3030 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_u16, aco_opcode::v_cmp_lt_u32, aco_opcode::v_cmp_lt_u64, aco_opcode::s_cmp_lt_u32);
3031 break;
3032 }
3033 case nir_op_uge: {
3034 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_u16, aco_opcode::v_cmp_ge_u32, aco_opcode::v_cmp_ge_u64, aco_opcode::s_cmp_ge_u32);
3035 break;
3036 }
3037 case nir_op_fddx:
3038 case nir_op_fddy:
3039 case nir_op_fddx_fine:
3040 case nir_op_fddy_fine:
3041 case nir_op_fddx_coarse:
3042 case nir_op_fddy_coarse: {
3043 Temp src = get_alu_src(ctx, instr->src[0]);
3044 uint16_t dpp_ctrl1, dpp_ctrl2;
3045 if (instr->op == nir_op_fddx_fine) {
3046 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
3047 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
3048 } else if (instr->op == nir_op_fddy_fine) {
3049 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
3050 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
3051 } else {
3052 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
3053 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
3054 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
3055 else
3056 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
3057 }
3058
3059 Temp tmp;
3060 if (ctx->program->chip_class >= GFX8) {
3061 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
3062 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
3063 } else {
3064 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
3065 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3066 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3067 }
3068 emit_wqm(ctx, tmp, dst, true);
3069 break;
3070 }
3071 default:
3072 fprintf(stderr, "Unknown NIR ALU instr: ");
3073 nir_print_instr(&instr->instr, stderr);
3074 fprintf(stderr, "\n");
3075 }
3076 }
3077
3078 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3079 {
3080 Temp dst = get_ssa_temp(ctx, &instr->def);
3081
3082 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3083 // which get truncated the lsb if double and msb if int
3084 // for now, we only use s_mov_b64 with 64bit inline constants
3085 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3086 assert(dst.type() == RegType::sgpr);
3087
3088 Builder bld(ctx->program, ctx->block);
3089
3090 if (instr->def.bit_size == 1) {
3091 assert(dst.regClass() == bld.lm);
3092 int val = instr->value[0].b ? -1 : 0;
3093 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3094 bld.sop1(Builder::s_mov, Definition(dst), op);
3095 } else if (instr->def.bit_size == 8) {
3096 /* ensure that the value is correctly represented in the low byte of the register */
3097 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3098 } else if (instr->def.bit_size == 16) {
3099 /* ensure that the value is correctly represented in the low half of the register */
3100 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3101 } else if (dst.size() == 1) {
3102 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3103 } else {
3104 assert(dst.size() != 1);
3105 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3106 if (instr->def.bit_size == 64)
3107 for (unsigned i = 0; i < dst.size(); i++)
3108 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3109 else {
3110 for (unsigned i = 0; i < dst.size(); i++)
3111 vec->operands[i] = Operand{instr->value[i].u32};
3112 }
3113 vec->definitions[0] = Definition(dst);
3114 ctx->block->instructions.emplace_back(std::move(vec));
3115 }
3116 }
3117
3118 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3119 {
3120 uint32_t new_mask = 0;
3121 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3122 if (mask & (1u << i))
3123 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3124 return new_mask;
3125 }
3126
3127 struct LoadEmitInfo {
3128 Operand offset;
3129 Temp dst;
3130 unsigned num_components;
3131 unsigned component_size;
3132 Temp resource = Temp(0, s1);
3133 unsigned component_stride = 0;
3134 unsigned const_offset = 0;
3135 unsigned align_mul = 0;
3136 unsigned align_offset = 0;
3137
3138 bool glc = false;
3139 unsigned swizzle_component_size = 0;
3140 barrier_interaction barrier = barrier_none;
3141 bool can_reorder = true;
3142 Temp soffset = Temp(0, s1);
3143 };
3144
3145 using LoadCallback = Temp(*)(
3146 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3147 unsigned align, unsigned const_offset, Temp dst_hint);
3148
3149 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3150 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3151 {
3152 unsigned load_size = info->num_components * info->component_size;
3153 unsigned component_size = info->component_size;
3154
3155 unsigned num_vals = 0;
3156 Temp vals[info->dst.bytes()];
3157
3158 unsigned const_offset = info->const_offset;
3159
3160 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3161 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3162
3163 unsigned bytes_read = 0;
3164 while (bytes_read < load_size) {
3165 unsigned bytes_needed = load_size - bytes_read;
3166
3167 /* add buffer for unaligned loads */
3168 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3169
3170 if (byte_align) {
3171 if ((bytes_needed > 2 ||
3172 (bytes_needed == 2 && (align_mul % 2 || align_offset % 2)) ||
3173 !supports_8bit_16bit_loads) && byte_align_loads) {
3174 if (info->component_stride) {
3175 assert(supports_8bit_16bit_loads && "unimplemented");
3176 bytes_needed = 2;
3177 byte_align = 0;
3178 } else {
3179 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3180 bytes_needed = align(bytes_needed, 4);
3181 }
3182 } else {
3183 byte_align = 0;
3184 }
3185 }
3186
3187 if (info->swizzle_component_size)
3188 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3189 if (info->component_stride)
3190 bytes_needed = MIN2(bytes_needed, info->component_size);
3191
3192 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3193
3194 /* reduce constant offset */
3195 Operand offset = info->offset;
3196 unsigned reduced_const_offset = const_offset;
3197 bool remove_const_offset_completely = need_to_align_offset;
3198 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3199 unsigned to_add = const_offset;
3200 if (remove_const_offset_completely) {
3201 reduced_const_offset = 0;
3202 } else {
3203 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3204 reduced_const_offset %= max_const_offset_plus_one;
3205 }
3206 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3207 if (offset.isConstant()) {
3208 offset = Operand(offset.constantValue() + to_add);
3209 } else if (offset_tmp.regClass() == s1) {
3210 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3211 offset_tmp, Operand(to_add));
3212 } else if (offset_tmp.regClass() == v1) {
3213 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3214 } else {
3215 Temp lo = bld.tmp(offset_tmp.type(), 1);
3216 Temp hi = bld.tmp(offset_tmp.type(), 1);
3217 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3218
3219 if (offset_tmp.regClass() == s2) {
3220 Temp carry = bld.tmp(s1);
3221 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3222 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3223 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3224 } else {
3225 Temp new_lo = bld.tmp(v1);
3226 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3227 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3228 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3229 }
3230 }
3231 }
3232
3233 /* align offset down if needed */
3234 Operand aligned_offset = offset;
3235 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3236 if (need_to_align_offset) {
3237 align = 4;
3238 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3239 if (offset.isConstant()) {
3240 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3241 } else if (offset_tmp.regClass() == s1) {
3242 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3243 } else if (offset_tmp.regClass() == s2) {
3244 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3245 } else if (offset_tmp.regClass() == v1) {
3246 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3247 } else if (offset_tmp.regClass() == v2) {
3248 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3249 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3250 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3251 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3252 }
3253 }
3254 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3255 bld.copy(bld.def(s1), aligned_offset);
3256
3257 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3258 reduced_const_offset, byte_align ? Temp() : info->dst);
3259
3260 /* the callback wrote directly to dst */
3261 if (val == info->dst) {
3262 assert(num_vals == 0);
3263 emit_split_vector(ctx, info->dst, info->num_components);
3264 return;
3265 }
3266
3267 /* shift result right if needed */
3268 if (info->component_size < 4 && byte_align_loads) {
3269 Operand align((uint32_t)byte_align);
3270 if (byte_align == -1) {
3271 if (offset.isConstant())
3272 align = Operand(offset.constantValue() % 4u);
3273 else if (offset.size() == 2)
3274 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3275 else
3276 align = offset;
3277 }
3278
3279 assert(val.bytes() >= load_size && "unimplemented");
3280 if (val.type() == RegType::sgpr)
3281 byte_align_scalar(ctx, val, align, info->dst);
3282 else
3283 byte_align_vector(ctx, val, align, info->dst, component_size);
3284 return;
3285 }
3286
3287 /* add result to list and advance */
3288 if (info->component_stride) {
3289 assert(val.bytes() == info->component_size && "unimplemented");
3290 const_offset += info->component_stride;
3291 align_offset = (align_offset + info->component_stride) % align_mul;
3292 } else {
3293 const_offset += val.bytes();
3294 align_offset = (align_offset + val.bytes()) % align_mul;
3295 }
3296 bytes_read += val.bytes();
3297 vals[num_vals++] = val;
3298 }
3299
3300 /* create array of components */
3301 unsigned components_split = 0;
3302 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3303 bool has_vgprs = false;
3304 for (unsigned i = 0; i < num_vals;) {
3305 Temp tmp[num_vals];
3306 unsigned num_tmps = 0;
3307 unsigned tmp_size = 0;
3308 RegType reg_type = RegType::sgpr;
3309 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3310 if (vals[i].type() == RegType::vgpr)
3311 reg_type = RegType::vgpr;
3312 tmp_size += vals[i].bytes();
3313 tmp[num_tmps++] = vals[i++];
3314 }
3315 if (num_tmps > 1) {
3316 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3317 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3318 for (unsigned i = 0; i < num_tmps; i++)
3319 vec->operands[i] = Operand(tmp[i]);
3320 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3321 vec->definitions[0] = Definition(tmp[0]);
3322 bld.insert(std::move(vec));
3323 }
3324
3325 if (tmp[0].bytes() % component_size) {
3326 /* trim tmp[0] */
3327 assert(i == num_vals);
3328 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3329 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3330 }
3331
3332 RegClass elem_rc = RegClass::get(reg_type, component_size);
3333
3334 unsigned start = components_split;
3335
3336 if (tmp_size == elem_rc.bytes()) {
3337 allocated_vec[components_split++] = tmp[0];
3338 } else {
3339 assert(tmp_size % elem_rc.bytes() == 0);
3340 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3341 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3342 for (unsigned i = 0; i < split->definitions.size(); i++) {
3343 Temp component = bld.tmp(elem_rc);
3344 allocated_vec[components_split++] = component;
3345 split->definitions[i] = Definition(component);
3346 }
3347 split->operands[0] = Operand(tmp[0]);
3348 bld.insert(std::move(split));
3349 }
3350
3351 /* try to p_as_uniform early so we can create more optimizable code and
3352 * also update allocated_vec */
3353 for (unsigned j = start; j < components_split; j++) {
3354 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3355 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3356 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3357 }
3358 }
3359
3360 /* concatenate components and p_as_uniform() result if needed */
3361 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3362 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3363
3364 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3365
3366 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3367 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3368 for (unsigned i = 0; i < info->num_components; i++)
3369 vec->operands[i] = Operand(allocated_vec[i]);
3370 if (padding_bytes)
3371 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3372 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3373 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3374 vec->definitions[0] = Definition(tmp);
3375 bld.insert(std::move(vec));
3376 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3377 } else {
3378 vec->definitions[0] = Definition(info->dst);
3379 bld.insert(std::move(vec));
3380 }
3381 }
3382
3383 Operand load_lds_size_m0(Builder& bld)
3384 {
3385 /* TODO: m0 does not need to be initialized on GFX9+ */
3386 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3387 }
3388
3389 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3390 Temp offset, unsigned bytes_needed,
3391 unsigned align, unsigned const_offset,
3392 Temp dst_hint)
3393 {
3394 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3395
3396 Operand m = load_lds_size_m0(bld);
3397
3398 bool large_ds_read = bld.program->chip_class >= GFX7;
3399 bool usable_read2 = bld.program->chip_class >= GFX7;
3400
3401 bool read2 = false;
3402 unsigned size = 0;
3403 aco_opcode op;
3404 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3405 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3406 size = 16;
3407 op = aco_opcode::ds_read_b128;
3408 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3409 size = 16;
3410 read2 = true;
3411 op = aco_opcode::ds_read2_b64;
3412 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3413 size = 12;
3414 op = aco_opcode::ds_read_b96;
3415 } else if (bytes_needed >= 8 && align % 8 == 0) {
3416 size = 8;
3417 op = aco_opcode::ds_read_b64;
3418 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3419 size = 8;
3420 read2 = true;
3421 op = aco_opcode::ds_read2_b32;
3422 } else if (bytes_needed >= 4 && align % 4 == 0) {
3423 size = 4;
3424 op = aco_opcode::ds_read_b32;
3425 } else if (bytes_needed >= 2 && align % 2 == 0) {
3426 size = 2;
3427 op = aco_opcode::ds_read_u16;
3428 } else {
3429 size = 1;
3430 op = aco_opcode::ds_read_u8;
3431 }
3432
3433 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3434 if (const_offset >= max_offset_plus_one) {
3435 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3436 const_offset %= max_offset_plus_one;
3437 }
3438
3439 if (read2)
3440 const_offset /= (size / 2u);
3441
3442 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3443 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3444 if (read2)
3445 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3446 else
3447 bld.ds(op, Definition(val), offset, m, const_offset);
3448
3449 if (size < 4)
3450 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3451
3452 return val;
3453 }
3454
3455 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3456
3457 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3458 Temp offset, unsigned bytes_needed,
3459 unsigned align, unsigned const_offset,
3460 Temp dst_hint)
3461 {
3462 unsigned size = 0;
3463 aco_opcode op;
3464 if (bytes_needed <= 4) {
3465 size = 1;
3466 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3467 } else if (bytes_needed <= 8) {
3468 size = 2;
3469 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3470 } else if (bytes_needed <= 16) {
3471 size = 4;
3472 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3473 } else if (bytes_needed <= 32) {
3474 size = 8;
3475 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3476 } else {
3477 size = 16;
3478 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3479 }
3480 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3481 if (info->resource.id()) {
3482 load->operands[0] = Operand(info->resource);
3483 load->operands[1] = Operand(offset);
3484 } else {
3485 load->operands[0] = Operand(offset);
3486 load->operands[1] = Operand(0u);
3487 }
3488 RegClass rc(RegType::sgpr, size);
3489 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3490 load->definitions[0] = Definition(val);
3491 load->glc = info->glc;
3492 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3493 load->barrier = info->barrier;
3494 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3495 bld.insert(std::move(load));
3496 return val;
3497 }
3498
3499 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3500
3501 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3502 Temp offset, unsigned bytes_needed,
3503 unsigned align_, unsigned const_offset,
3504 Temp dst_hint)
3505 {
3506 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3507 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3508
3509 if (info->soffset.id()) {
3510 if (soffset.isTemp())
3511 vaddr = bld.copy(bld.def(v1), soffset);
3512 soffset = Operand(info->soffset);
3513 }
3514
3515 unsigned bytes_size = 0;
3516 aco_opcode op;
3517 if (bytes_needed == 1 || align_ % 2) {
3518 bytes_size = 1;
3519 op = aco_opcode::buffer_load_ubyte;
3520 } else if (bytes_needed == 2 || align_ % 4) {
3521 bytes_size = 2;
3522 op = aco_opcode::buffer_load_ushort;
3523 } else if (bytes_needed <= 4) {
3524 bytes_size = 4;
3525 op = aco_opcode::buffer_load_dword;
3526 } else if (bytes_needed <= 8) {
3527 bytes_size = 8;
3528 op = aco_opcode::buffer_load_dwordx2;
3529 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3530 bytes_size = 12;
3531 op = aco_opcode::buffer_load_dwordx3;
3532 } else {
3533 bytes_size = 16;
3534 op = aco_opcode::buffer_load_dwordx4;
3535 }
3536 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3537 mubuf->operands[0] = Operand(info->resource);
3538 mubuf->operands[1] = vaddr;
3539 mubuf->operands[2] = soffset;
3540 mubuf->offen = (offset.type() == RegType::vgpr);
3541 mubuf->glc = info->glc;
3542 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3543 mubuf->barrier = info->barrier;
3544 mubuf->can_reorder = info->can_reorder;
3545 mubuf->offset = const_offset;
3546 mubuf->swizzled = info->swizzle_component_size != 0;
3547 RegClass rc = RegClass::get(RegType::vgpr, bytes_size);
3548 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3549 mubuf->definitions[0] = Definition(val);
3550 bld.insert(std::move(mubuf));
3551
3552 return val;
3553 }
3554
3555 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3556 static auto emit_scratch_load = emit_load<mubuf_load_callback, false, true, 4096>;
3557
3558 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3559 {
3560 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3561 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3562
3563 if (addr.type() == RegType::vgpr)
3564 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3565 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3566 }
3567
3568 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3569 Temp offset, unsigned bytes_needed,
3570 unsigned align_, unsigned const_offset,
3571 Temp dst_hint)
3572 {
3573 unsigned bytes_size = 0;
3574 bool mubuf = bld.program->chip_class == GFX6;
3575 bool global = bld.program->chip_class >= GFX9;
3576 aco_opcode op;
3577 if (bytes_needed == 1) {
3578 bytes_size = 1;
3579 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3580 } else if (bytes_needed == 2) {
3581 bytes_size = 2;
3582 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3583 } else if (bytes_needed <= 4) {
3584 bytes_size = 4;
3585 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3586 } else if (bytes_needed <= 8) {
3587 bytes_size = 8;
3588 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3589 } else if (bytes_needed <= 12 && !mubuf) {
3590 bytes_size = 12;
3591 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3592 } else {
3593 bytes_size = 16;
3594 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3595 }
3596 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3597 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3598 if (mubuf) {
3599 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3600 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3601 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3602 mubuf->operands[2] = Operand(0u);
3603 mubuf->glc = info->glc;
3604 mubuf->dlc = false;
3605 mubuf->offset = 0;
3606 mubuf->addr64 = offset.type() == RegType::vgpr;
3607 mubuf->disable_wqm = false;
3608 mubuf->barrier = info->barrier;
3609 mubuf->definitions[0] = Definition(val);
3610 bld.insert(std::move(mubuf));
3611 } else {
3612 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3613
3614 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3615 flat->operands[0] = Operand(offset);
3616 flat->operands[1] = Operand(s1);
3617 flat->glc = info->glc;
3618 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3619 flat->barrier = info->barrier;
3620 flat->offset = 0u;
3621 flat->definitions[0] = Definition(val);
3622 bld.insert(std::move(flat));
3623 }
3624
3625 return val;
3626 }
3627
3628 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3629
3630 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3631 Temp address, unsigned base_offset, unsigned align)
3632 {
3633 assert(util_is_power_of_two_nonzero(align));
3634
3635 Builder bld(ctx->program, ctx->block);
3636
3637 unsigned num_components = dst.bytes() / elem_size_bytes;
3638 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3639 info.align_mul = align;
3640 info.align_offset = 0;
3641 info.barrier = barrier_shared;
3642 info.can_reorder = false;
3643 info.const_offset = base_offset;
3644 emit_lds_load(ctx, bld, &info);
3645
3646 return dst;
3647 }
3648
3649 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3650 {
3651 if (!count)
3652 return;
3653
3654 Builder bld(ctx->program, ctx->block);
3655
3656 ASSERTED bool is_subdword = false;
3657 for (unsigned i = 0; i < count; i++)
3658 is_subdword |= offsets[i] % 4;
3659 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3660 assert(!is_subdword || dst_type == RegType::vgpr);
3661
3662 /* count == 1 fast path */
3663 if (count == 1) {
3664 if (dst_type == RegType::sgpr)
3665 dst[0] = bld.as_uniform(src);
3666 else
3667 dst[0] = as_vgpr(ctx, src);
3668 return;
3669 }
3670
3671 for (unsigned i = 0; i < count - 1; i++)
3672 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3673 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3674
3675 if (is_subdword && src.type() == RegType::sgpr) {
3676 src = as_vgpr(ctx, src);
3677 } else {
3678 /* use allocated_vec if possible */
3679 auto it = ctx->allocated_vec.find(src.id());
3680 if (it != ctx->allocated_vec.end()) {
3681 unsigned total_size = 0;
3682 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3683 total_size += it->second[i].bytes();
3684 if (total_size != src.bytes())
3685 goto split;
3686
3687 unsigned elem_size = it->second[0].bytes();
3688
3689 for (unsigned i = 0; i < count; i++) {
3690 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3691 goto split;
3692 }
3693
3694 for (unsigned i = 0; i < count; i++) {
3695 unsigned start_idx = offsets[i] / elem_size;
3696 unsigned op_count = dst[i].bytes() / elem_size;
3697 if (op_count == 1) {
3698 if (dst_type == RegType::sgpr)
3699 dst[i] = bld.as_uniform(it->second[start_idx]);
3700 else
3701 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3702 continue;
3703 }
3704
3705 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3706 for (unsigned j = 0; j < op_count; j++) {
3707 Temp tmp = it->second[start_idx + j];
3708 if (dst_type == RegType::sgpr)
3709 tmp = bld.as_uniform(tmp);
3710 vec->operands[j] = Operand(tmp);
3711 }
3712 vec->definitions[0] = Definition(dst[i]);
3713 bld.insert(std::move(vec));
3714 }
3715 return;
3716 }
3717 }
3718
3719 if (dst_type == RegType::sgpr)
3720 src = bld.as_uniform(src);
3721
3722 split:
3723 /* just split it */
3724 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3725 split->operands[0] = Operand(src);
3726 for (unsigned i = 0; i < count; i++)
3727 split->definitions[i] = Definition(dst[i]);
3728 bld.insert(std::move(split));
3729 }
3730
3731 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3732 int *start, int *count)
3733 {
3734 unsigned start_elem = ffs(todo_mask) - 1;
3735 bool skip = !(mask & (1 << start_elem));
3736 if (skip)
3737 mask = ~mask & todo_mask;
3738
3739 mask &= todo_mask;
3740
3741 u_bit_scan_consecutive_range(&mask, start, count);
3742
3743 return !skip;
3744 }
3745
3746 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3747 {
3748 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3749 }
3750
3751 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3752 Temp address, unsigned base_offset, unsigned align)
3753 {
3754 assert(util_is_power_of_two_nonzero(align));
3755 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3756
3757 Builder bld(ctx->program, ctx->block);
3758 bool large_ds_write = ctx->options->chip_class >= GFX7;
3759 bool usable_write2 = ctx->options->chip_class >= GFX7;
3760
3761 unsigned write_count = 0;
3762 Temp write_datas[32];
3763 unsigned offsets[32];
3764 aco_opcode opcodes[32];
3765
3766 wrmask = widen_mask(wrmask, elem_size_bytes);
3767
3768 uint32_t todo = u_bit_consecutive(0, data.bytes());
3769 while (todo) {
3770 int offset, bytes;
3771 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3772 offsets[write_count] = offset;
3773 opcodes[write_count] = aco_opcode::num_opcodes;
3774 write_count++;
3775 advance_write_mask(&todo, offset, bytes);
3776 continue;
3777 }
3778
3779 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3780 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3781 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3782 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3783
3784 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3785 aco_opcode op = aco_opcode::num_opcodes;
3786 if (bytes >= 16 && aligned16 && large_ds_write) {
3787 op = aco_opcode::ds_write_b128;
3788 bytes = 16;
3789 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3790 op = aco_opcode::ds_write_b96;
3791 bytes = 12;
3792 } else if (bytes >= 8 && aligned8) {
3793 op = aco_opcode::ds_write_b64;
3794 bytes = 8;
3795 } else if (bytes >= 4 && aligned4) {
3796 op = aco_opcode::ds_write_b32;
3797 bytes = 4;
3798 } else if (bytes >= 2 && aligned2) {
3799 op = aco_opcode::ds_write_b16;
3800 bytes = 2;
3801 } else if (bytes >= 1) {
3802 op = aco_opcode::ds_write_b8;
3803 bytes = 1;
3804 } else {
3805 assert(false);
3806 }
3807
3808 offsets[write_count] = offset;
3809 opcodes[write_count] = op;
3810 write_count++;
3811 advance_write_mask(&todo, offset, bytes);
3812 }
3813
3814 Operand m = load_lds_size_m0(bld);
3815
3816 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3817
3818 for (unsigned i = 0; i < write_count; i++) {
3819 aco_opcode op = opcodes[i];
3820 if (op == aco_opcode::num_opcodes)
3821 continue;
3822
3823 Temp data = write_datas[i];
3824
3825 unsigned second = write_count;
3826 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3827 for (second = i + 1; second < write_count; second++) {
3828 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3829 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3830 opcodes[second] = aco_opcode::num_opcodes;
3831 break;
3832 }
3833 }
3834 }
3835
3836 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3837 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3838
3839 unsigned inline_offset = base_offset + offsets[i];
3840 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3841 Temp address_offset = address;
3842 if (inline_offset > max_offset) {
3843 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3844 inline_offset = offsets[i];
3845 }
3846 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3847
3848 if (write2) {
3849 Temp second_data = write_datas[second];
3850 inline_offset /= data.bytes();
3851 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3852 } else {
3853 bld.ds(op, address_offset, data, m, inline_offset);
3854 }
3855 }
3856 }
3857
3858 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3859 {
3860 unsigned align = 16;
3861 if (const_offset)
3862 align = std::min(align, 1u << (ffs(const_offset) - 1));
3863
3864 return align;
3865 }
3866
3867
3868 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3869 {
3870 switch (bytes) {
3871 case 1:
3872 assert(!smem);
3873 return aco_opcode::buffer_store_byte;
3874 case 2:
3875 assert(!smem);
3876 return aco_opcode::buffer_store_short;
3877 case 4:
3878 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3879 case 8:
3880 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3881 case 12:
3882 assert(!smem);
3883 return aco_opcode::buffer_store_dwordx3;
3884 case 16:
3885 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3886 }
3887 unreachable("Unexpected store size");
3888 return aco_opcode::num_opcodes;
3889 }
3890
3891 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3892 Temp data, unsigned writemask, int swizzle_element_size,
3893 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3894 {
3895 unsigned write_count_with_skips = 0;
3896 bool skips[16];
3897
3898 /* determine how to split the data */
3899 unsigned todo = u_bit_consecutive(0, data.bytes());
3900 while (todo) {
3901 int offset, bytes;
3902 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3903 offsets[write_count_with_skips] = offset;
3904 if (skips[write_count_with_skips]) {
3905 advance_write_mask(&todo, offset, bytes);
3906 write_count_with_skips++;
3907 continue;
3908 }
3909
3910 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3911 * larger than swizzle_element_size */
3912 bytes = MIN2(bytes, swizzle_element_size);
3913 if (bytes % 4)
3914 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3915
3916 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3917 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3918 bytes = 8;
3919
3920 /* dword or larger stores have to be dword-aligned */
3921 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3922 unsigned align_offset = (instr ? nir_intrinsic_align_offset(instr) : 0) + offset;
3923 bool dword_aligned = align_offset % 4 == 0 && align_mul % 4 == 0;
3924 if (!dword_aligned)
3925 bytes = MIN2(bytes, (align_offset % 2 == 0 && align_mul % 2 == 0) ? 2 : 1);
3926
3927 advance_write_mask(&todo, offset, bytes);
3928 write_count_with_skips++;
3929 }
3930
3931 /* actually split data */
3932 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3933
3934 /* remove skips */
3935 for (unsigned i = 0; i < write_count_with_skips; i++) {
3936 if (skips[i])
3937 continue;
3938 write_datas[*write_count] = write_datas[i];
3939 offsets[*write_count] = offsets[i];
3940 (*write_count)++;
3941 }
3942 }
3943
3944 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3945 unsigned split_cnt = 0u, Temp dst = Temp())
3946 {
3947 Builder bld(ctx->program, ctx->block);
3948 unsigned dword_size = elem_size_bytes / 4;
3949
3950 if (!dst.id())
3951 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3952
3953 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3954 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3955 instr->definitions[0] = Definition(dst);
3956
3957 for (unsigned i = 0; i < cnt; ++i) {
3958 if (arr[i].id()) {
3959 assert(arr[i].size() == dword_size);
3960 allocated_vec[i] = arr[i];
3961 instr->operands[i] = Operand(arr[i]);
3962 } else {
3963 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3964 allocated_vec[i] = zero;
3965 instr->operands[i] = Operand(zero);
3966 }
3967 }
3968
3969 bld.insert(std::move(instr));
3970
3971 if (split_cnt)
3972 emit_split_vector(ctx, dst, split_cnt);
3973 else
3974 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3975
3976 return dst;
3977 }
3978
3979 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3980 {
3981 if (const_offset >= 4096) {
3982 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3983 const_offset %= 4096u;
3984
3985 if (!voffset.id())
3986 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3987 else if (unlikely(voffset.regClass() == s1))
3988 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3989 else if (likely(voffset.regClass() == v1))
3990 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3991 else
3992 unreachable("Unsupported register class of voffset");
3993 }
3994
3995 return const_offset;
3996 }
3997
3998 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3999 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false,
4000 bool swizzled = false)
4001 {
4002 assert(vdata.id());
4003 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
4004 assert(vdata.size() >= 1 && vdata.size() <= 4);
4005
4006 Builder bld(ctx->program, ctx->block);
4007 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
4008 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
4009
4010 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
4011 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
4012 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
4013 /* offen */ !voffset_op.isUndefined(), /* swizzled */ swizzled,
4014 /* idxen*/ false, /* addr64 */ false, /* disable_wqm */ false, /* glc */ true,
4015 /* dlc*/ false, /* slc */ slc);
4016
4017 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
4018 }
4019
4020 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
4021 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
4022 bool allow_combining = true, bool reorder = true, bool slc = false)
4023 {
4024 Builder bld(ctx->program, ctx->block);
4025 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4026 assert(write_mask);
4027 write_mask = widen_mask(write_mask, elem_size_bytes);
4028
4029 unsigned write_count = 0;
4030 Temp write_datas[32];
4031 unsigned offsets[32];
4032 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
4033 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
4034
4035 for (unsigned i = 0; i < write_count; i++) {
4036 unsigned const_offset = offsets[i] + base_const_offset;
4037 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc, !allow_combining);
4038 }
4039 }
4040
4041 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4042 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4043 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4044 {
4045 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4046 assert((num_components * elem_size_bytes) == dst.bytes());
4047 assert(!!stride != allow_combining);
4048
4049 Builder bld(ctx->program, ctx->block);
4050
4051 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4052 info.component_stride = allow_combining ? 0 : stride;
4053 info.glc = true;
4054 info.swizzle_component_size = allow_combining ? 0 : 4;
4055 info.align_mul = MIN2(elem_size_bytes, 4);
4056 info.align_offset = 0;
4057 info.soffset = soffset;
4058 info.const_offset = base_const_offset;
4059 emit_mubuf_load(ctx, bld, &info);
4060 }
4061
4062 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)
4063 {
4064 Builder bld(ctx->program, ctx->block);
4065 Temp offset = base_offset.first;
4066 unsigned const_offset = base_offset.second;
4067
4068 if (!nir_src_is_const(*off_src)) {
4069 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4070 Temp with_stride;
4071
4072 /* Calculate indirect offset with stride */
4073 if (likely(indirect_offset_arg.regClass() == v1))
4074 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4075 else if (indirect_offset_arg.regClass() == s1)
4076 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4077 else
4078 unreachable("Unsupported register class of indirect offset");
4079
4080 /* Add to the supplied base offset */
4081 if (offset.id() == 0)
4082 offset = with_stride;
4083 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4084 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4085 else if (offset.size() == 1 && with_stride.size() == 1)
4086 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4087 else
4088 unreachable("Unsupported register class of indirect offset");
4089 } else {
4090 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4091 const_offset += const_offset_arg * stride;
4092 }
4093
4094 return std::make_pair(offset, const_offset);
4095 }
4096
4097 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4098 {
4099 Builder bld(ctx->program, ctx->block);
4100 Temp offset;
4101
4102 if (off1.first.id() && off2.first.id()) {
4103 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4104 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4105 else if (off1.first.size() == 1 && off2.first.size() == 1)
4106 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4107 else
4108 unreachable("Unsupported register class of indirect offset");
4109 } else {
4110 offset = off1.first.id() ? off1.first : off2.first;
4111 }
4112
4113 return std::make_pair(offset, off1.second + off2.second);
4114 }
4115
4116 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4117 {
4118 Builder bld(ctx->program, ctx->block);
4119 unsigned const_offset = offs.second * multiplier;
4120
4121 if (!offs.first.id())
4122 return std::make_pair(offs.first, const_offset);
4123
4124 Temp offset = unlikely(offs.first.regClass() == s1)
4125 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4126 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4127
4128 return std::make_pair(offset, const_offset);
4129 }
4130
4131 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4132 {
4133 Builder bld(ctx->program, ctx->block);
4134
4135 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4136 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4137 /* component is in bytes */
4138 const_offset += nir_intrinsic_component(instr) * component_stride;
4139
4140 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4141 nir_src *off_src = nir_get_io_offset_src(instr);
4142 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4143 }
4144
4145 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4146 {
4147 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4148 }
4149
4150 Temp get_tess_rel_patch_id(isel_context *ctx)
4151 {
4152 Builder bld(ctx->program, ctx->block);
4153
4154 switch (ctx->shader->info.stage) {
4155 case MESA_SHADER_TESS_CTRL:
4156 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4157 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4158 case MESA_SHADER_TESS_EVAL:
4159 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4160 default:
4161 unreachable("Unsupported stage in get_tess_rel_patch_id");
4162 }
4163 }
4164
4165 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4166 {
4167 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4168 Builder bld(ctx->program, ctx->block);
4169
4170 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4171 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4172
4173 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4174
4175 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4176 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4177
4178 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4179 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4180 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4181
4182 return offset_mul(ctx, offs, 4u);
4183 }
4184
4185 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4186 {
4187 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4188 Builder bld(ctx->program, ctx->block);
4189
4190 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4191 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4192 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4193 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4194
4195 std::pair<Temp, unsigned> offs = instr
4196 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4197 : std::make_pair(Temp(), 0u);
4198
4199 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4200 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4201
4202 if (per_vertex) {
4203 assert(instr);
4204
4205 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4206 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4207
4208 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4209 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4210 } else {
4211 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4212 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4213 }
4214
4215 return offs;
4216 }
4217
4218 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4219 {
4220 Builder bld(ctx->program, ctx->block);
4221
4222 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4223 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4224
4225 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4226
4227 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4228 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4229 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4230
4231 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4232 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4233
4234 return offs;
4235 }
4236
4237 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4238 {
4239 Builder bld(ctx->program, ctx->block);
4240
4241 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4242 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4243 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4244 unsigned attr_stride = ctx->tcs_num_patches;
4245
4246 std::pair<Temp, unsigned> offs = instr
4247 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4248 : std::make_pair(Temp(), 0u);
4249
4250 if (const_base_offset)
4251 offs.second += const_base_offset * attr_stride;
4252
4253 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4254 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4255 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4256
4257 return offs;
4258 }
4259
4260 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4261 {
4262 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4263
4264 if (mask == 0)
4265 return false;
4266
4267 unsigned drv_loc = nir_intrinsic_base(instr);
4268 nir_src *off_src = nir_get_io_offset_src(instr);
4269
4270 if (!nir_src_is_const(*off_src)) {
4271 *indirect = true;
4272 return false;
4273 }
4274
4275 *indirect = false;
4276 uint64_t slot = per_vertex
4277 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4278 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4279 return (((uint64_t) 1) << slot) & mask;
4280 }
4281
4282 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4283 {
4284 unsigned write_mask = nir_intrinsic_write_mask(instr);
4285 unsigned component = nir_intrinsic_component(instr);
4286 unsigned idx = nir_intrinsic_base(instr) + component;
4287
4288 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4289 if (off_instr->type != nir_instr_type_load_const)
4290 return false;
4291
4292 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4293 idx += nir_src_as_uint(instr->src[1]) * 4u;
4294
4295 if (instr->src[0].ssa->bit_size == 64)
4296 write_mask = widen_mask(write_mask, 2);
4297
4298 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4299
4300 for (unsigned i = 0; i < 8; ++i) {
4301 if (write_mask & (1 << i)) {
4302 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4303 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4304 }
4305 idx++;
4306 }
4307
4308 return true;
4309 }
4310
4311 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4312 {
4313 /* Only TCS per-vertex inputs are supported by this function.
4314 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4315 */
4316 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4317 return false;
4318
4319 nir_src *off_src = nir_get_io_offset_src(instr);
4320 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4321 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4322 bool can_use_temps = nir_src_is_const(*off_src) &&
4323 vertex_index_instr->type == nir_instr_type_intrinsic &&
4324 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4325
4326 if (!can_use_temps)
4327 return false;
4328
4329 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4330 Temp *src = &ctx->inputs.temps[idx];
4331 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4332
4333 return true;
4334 }
4335
4336 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4337 {
4338 Builder bld(ctx->program, ctx->block);
4339
4340 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4341 /* 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. */
4342 bool indirect_write;
4343 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4344 if (temp_only_input && !indirect_write)
4345 return;
4346 }
4347
4348 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4349 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4350 unsigned write_mask = nir_intrinsic_write_mask(instr);
4351 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4352
4353 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4354 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4355 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4356 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4357 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4358 } else {
4359 Temp lds_base;
4360
4361 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4362 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4363 unsigned itemsize = ctx->stage == vertex_geometry_gs
4364 ? ctx->program->info->vs.es_info.esgs_itemsize
4365 : ctx->program->info->tes.es_info.esgs_itemsize;
4366 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4367 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));
4368 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4369 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4370 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4371 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4372 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4373 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4374 */
4375 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4376 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4377 } else {
4378 unreachable("Invalid LS or ES stage");
4379 }
4380
4381 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4382 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4383 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4384 }
4385 }
4386
4387 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4388 {
4389 if (per_vertex)
4390 return false;
4391
4392 unsigned off = nir_intrinsic_base(instr) * 4u;
4393 return off == ctx->tcs_tess_lvl_out_loc ||
4394 off == ctx->tcs_tess_lvl_in_loc;
4395
4396 }
4397
4398 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4399 {
4400 uint64_t mask = per_vertex
4401 ? ctx->program->info->tcs.tes_inputs_read
4402 : ctx->program->info->tcs.tes_patch_inputs_read;
4403
4404 bool indirect_write = false;
4405 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4406 return indirect_write || output_read_by_tes;
4407 }
4408
4409 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4410 {
4411 uint64_t mask = per_vertex
4412 ? ctx->shader->info.outputs_read
4413 : ctx->shader->info.patch_outputs_read;
4414
4415 bool indirect_write = false;
4416 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4417 return indirect_write || output_read;
4418 }
4419
4420 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4421 {
4422 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4423 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4424
4425 Builder bld(ctx->program, ctx->block);
4426
4427 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4428 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4429 unsigned write_mask = nir_intrinsic_write_mask(instr);
4430
4431 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4432 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4433 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4434
4435 if (write_to_vmem) {
4436 std::pair<Temp, unsigned> vmem_offs = per_vertex
4437 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4438 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4439
4440 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));
4441 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4442 store_vmem_mubuf(ctx, store_val, hs_ring_tess_offchip, vmem_offs.first, oc_lds, vmem_offs.second, elem_size_bytes, write_mask, true, false);
4443 }
4444
4445 if (write_to_lds) {
4446 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4447 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4448 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4449 }
4450 }
4451
4452 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4453 {
4454 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4455 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4456
4457 Builder bld(ctx->program, ctx->block);
4458
4459 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4460 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4461 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4462 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4463
4464 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4465 }
4466
4467 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4468 {
4469 if (ctx->stage == vertex_vs ||
4470 ctx->stage == tess_eval_vs ||
4471 ctx->stage == fragment_fs ||
4472 ctx->stage == ngg_vertex_gs ||
4473 ctx->stage == ngg_tess_eval_gs ||
4474 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4475 bool stored_to_temps = store_output_to_temps(ctx, instr);
4476 if (!stored_to_temps) {
4477 fprintf(stderr, "Unimplemented output offset instruction:\n");
4478 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4479 fprintf(stderr, "\n");
4480 abort();
4481 }
4482 } else if (ctx->stage == vertex_es ||
4483 ctx->stage == vertex_ls ||
4484 ctx->stage == tess_eval_es ||
4485 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4486 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4487 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4488 visit_store_ls_or_es_output(ctx, instr);
4489 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4490 visit_store_tcs_output(ctx, instr, false);
4491 } else {
4492 unreachable("Shader stage not implemented");
4493 }
4494 }
4495
4496 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4497 {
4498 visit_load_tcs_output(ctx, instr, false);
4499 }
4500
4501 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4502 {
4503 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4504 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4505
4506 Builder bld(ctx->program, ctx->block);
4507
4508 if (dst.regClass() == v2b) {
4509 if (ctx->program->has_16bank_lds) {
4510 assert(ctx->options->chip_class <= GFX8);
4511 Builder::Result interp_p1 =
4512 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4513 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4514 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4515 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4516 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4517 bld.m0(prim_mask), interp_p1, idx, component);
4518 } else {
4519 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4520
4521 if (ctx->options->chip_class == GFX8)
4522 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4523
4524 Builder::Result interp_p1 =
4525 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4526 coord1, bld.m0(prim_mask), idx, component);
4527 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4528 interp_p1, idx, component);
4529 }
4530 } else {
4531 Builder::Result interp_p1 =
4532 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4533 bld.m0(prim_mask), idx, component);
4534
4535 if (ctx->program->has_16bank_lds)
4536 interp_p1.instr->operands[0].setLateKill(true);
4537
4538 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4539 bld.m0(prim_mask), interp_p1, idx, component);
4540 }
4541 }
4542
4543 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4544 {
4545 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4546 for (unsigned i = 0; i < num_components; i++)
4547 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4548 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4549 assert(num_components == 4);
4550 Builder bld(ctx->program, ctx->block);
4551 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4552 }
4553
4554 for (Operand& op : vec->operands)
4555 op = op.isUndefined() ? Operand(0u) : op;
4556
4557 vec->definitions[0] = Definition(dst);
4558 ctx->block->instructions.emplace_back(std::move(vec));
4559 emit_split_vector(ctx, dst, num_components);
4560 return;
4561 }
4562
4563 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4564 {
4565 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4566 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4567 unsigned idx = nir_intrinsic_base(instr);
4568 unsigned component = nir_intrinsic_component(instr);
4569 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4570
4571 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4572 if (offset) {
4573 assert(offset->u32 == 0);
4574 } else {
4575 /* the lower 15bit of the prim_mask contain the offset into LDS
4576 * while the upper bits contain the number of prims */
4577 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4578 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4579 Builder bld(ctx->program, ctx->block);
4580 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4581 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4582 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4583 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4584 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4585 }
4586
4587 if (instr->dest.ssa.num_components == 1) {
4588 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4589 } else {
4590 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4591 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4592 {
4593 Temp tmp = {ctx->program->allocateId(), v1};
4594 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4595 vec->operands[i] = Operand(tmp);
4596 }
4597 vec->definitions[0] = Definition(dst);
4598 ctx->block->instructions.emplace_back(std::move(vec));
4599 }
4600 }
4601
4602 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4603 unsigned offset, unsigned stride, unsigned channels)
4604 {
4605 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4606 if (vtx_info->chan_byte_size != 4 && channels == 3)
4607 return false;
4608 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4609 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4610 }
4611
4612 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4613 unsigned offset, unsigned stride, unsigned *channels)
4614 {
4615 if (!vtx_info->chan_byte_size) {
4616 *channels = vtx_info->num_channels;
4617 return vtx_info->chan_format;
4618 }
4619
4620 unsigned num_channels = *channels;
4621 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4622 unsigned new_channels = num_channels + 1;
4623 /* first, assume more loads is worse and try using a larger data format */
4624 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4625 new_channels++;
4626 /* don't make the attribute potentially out-of-bounds */
4627 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4628 new_channels = 5;
4629 }
4630
4631 if (new_channels == 5) {
4632 /* then try decreasing load size (at the cost of more loads) */
4633 new_channels = *channels;
4634 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4635 new_channels--;
4636 }
4637
4638 if (new_channels < *channels)
4639 *channels = new_channels;
4640 num_channels = new_channels;
4641 }
4642
4643 switch (vtx_info->chan_format) {
4644 case V_008F0C_BUF_DATA_FORMAT_8:
4645 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4646 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4647 case V_008F0C_BUF_DATA_FORMAT_16:
4648 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4649 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4650 case V_008F0C_BUF_DATA_FORMAT_32:
4651 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4652 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4653 }
4654 unreachable("shouldn't reach here");
4655 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4656 }
4657
4658 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4659 * so we may need to fix it up. */
4660 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4661 {
4662 Builder bld(ctx->program, ctx->block);
4663
4664 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4665 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4666
4667 /* For the integer-like cases, do a natural sign extension.
4668 *
4669 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4670 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4671 * exponent.
4672 */
4673 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4674 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4675
4676 /* Convert back to the right type. */
4677 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4678 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4679 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4680 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4681 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4682 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4683 }
4684
4685 return alpha;
4686 }
4687
4688 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4689 {
4690 Builder bld(ctx->program, ctx->block);
4691 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4692 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4693
4694 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4695 if (off_instr->type != nir_instr_type_load_const) {
4696 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4697 nir_print_instr(off_instr, stderr);
4698 fprintf(stderr, "\n");
4699 }
4700 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4701
4702 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4703
4704 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4705 unsigned component = nir_intrinsic_component(instr);
4706 unsigned bitsize = instr->dest.ssa.bit_size;
4707 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4708 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4709 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4710 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4711
4712 unsigned dfmt = attrib_format & 0xf;
4713 unsigned nfmt = (attrib_format >> 4) & 0x7;
4714 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4715
4716 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4717 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4718 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4719 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4720 if (post_shuffle)
4721 num_channels = MAX2(num_channels, 3);
4722
4723 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4724 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4725
4726 Temp index;
4727 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4728 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4729 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4730 if (divisor) {
4731 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4732 if (divisor != 1) {
4733 Temp divided = bld.tmp(v1);
4734 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4735 index = bld.vadd32(bld.def(v1), start_instance, divided);
4736 } else {
4737 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4738 }
4739 } else {
4740 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4741 }
4742 } else {
4743 index = bld.vadd32(bld.def(v1),
4744 get_arg(ctx, ctx->args->ac.base_vertex),
4745 get_arg(ctx, ctx->args->ac.vertex_id));
4746 }
4747
4748 Temp channels[num_channels];
4749 unsigned channel_start = 0;
4750 bool direct_fetch = false;
4751
4752 /* skip unused channels at the start */
4753 if (vtx_info->chan_byte_size && !post_shuffle) {
4754 channel_start = ffs(mask) - 1;
4755 for (unsigned i = 0; i < channel_start; i++)
4756 channels[i] = Temp(0, s1);
4757 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4758 num_channels = 3 - (ffs(mask) - 1);
4759 }
4760
4761 /* load channels */
4762 while (channel_start < num_channels) {
4763 unsigned fetch_component = num_channels - channel_start;
4764 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4765 bool expanded = false;
4766
4767 /* use MUBUF when possible to avoid possible alignment issues */
4768 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4769 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4770 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4771 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4772 vtx_info->chan_byte_size == 4;
4773 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4774 if (!use_mubuf) {
4775 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4776 } else {
4777 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4778 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4779 fetch_component = 4;
4780 expanded = true;
4781 }
4782 }
4783
4784 unsigned fetch_bytes = fetch_component * bitsize / 8;
4785
4786 Temp fetch_index = index;
4787 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4788 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4789 fetch_offset = fetch_offset % attrib_stride;
4790 }
4791
4792 Operand soffset(0u);
4793 if (fetch_offset >= 4096) {
4794 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4795 fetch_offset %= 4096;
4796 }
4797
4798 aco_opcode opcode;
4799 switch (fetch_bytes) {
4800 case 2:
4801 assert(!use_mubuf && bitsize == 16);
4802 opcode = aco_opcode::tbuffer_load_format_d16_x;
4803 break;
4804 case 4:
4805 if (bitsize == 16) {
4806 assert(!use_mubuf);
4807 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4808 } else {
4809 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4810 }
4811 break;
4812 case 6:
4813 assert(!use_mubuf && bitsize == 16);
4814 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4815 break;
4816 case 8:
4817 if (bitsize == 16) {
4818 assert(!use_mubuf);
4819 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4820 } else {
4821 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4822 }
4823 break;
4824 case 12:
4825 assert(ctx->options->chip_class >= GFX7 ||
4826 (!use_mubuf && ctx->options->chip_class == GFX6));
4827 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4828 break;
4829 case 16:
4830 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4831 break;
4832 default:
4833 unreachable("Unimplemented load_input vector size");
4834 }
4835
4836 Temp fetch_dst;
4837 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4838 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4839 num_channels <= 3)) {
4840 direct_fetch = true;
4841 fetch_dst = dst;
4842 } else {
4843 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4844 }
4845
4846 if (use_mubuf) {
4847 Instruction *mubuf = bld.mubuf(opcode,
4848 Definition(fetch_dst), list, fetch_index, soffset,
4849 fetch_offset, false, false, true).instr;
4850 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4851 } else {
4852 Instruction *mtbuf = bld.mtbuf(opcode,
4853 Definition(fetch_dst), list, fetch_index, soffset,
4854 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4855 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4856 }
4857
4858 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4859
4860 if (fetch_component == 1) {
4861 channels[channel_start] = fetch_dst;
4862 } else {
4863 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4864 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4865 bitsize == 16 ? v2b : v1);
4866 }
4867
4868 channel_start += fetch_component;
4869 }
4870
4871 if (!direct_fetch) {
4872 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4873 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4874
4875 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4876 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4877 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4878
4879 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4880 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4881 unsigned num_temp = 0;
4882 for (unsigned i = 0; i < dst.size(); i++) {
4883 unsigned idx = i + component;
4884 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4885 Temp channel = channels[swizzle[idx]];
4886 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4887 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4888 vec->operands[i] = Operand(channel);
4889
4890 num_temp++;
4891 elems[i] = channel;
4892 } else if (is_float && idx == 3) {
4893 vec->operands[i] = Operand(0x3f800000u);
4894 } else if (!is_float && idx == 3) {
4895 vec->operands[i] = Operand(1u);
4896 } else {
4897 vec->operands[i] = Operand(0u);
4898 }
4899 }
4900 vec->definitions[0] = Definition(dst);
4901 ctx->block->instructions.emplace_back(std::move(vec));
4902 emit_split_vector(ctx, dst, dst.size());
4903
4904 if (num_temp == dst.size())
4905 ctx->allocated_vec.emplace(dst.id(), elems);
4906 }
4907 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4908 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4909 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4910 if (off_instr->type != nir_instr_type_load_const ||
4911 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4912 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4913 nir_print_instr(off_instr, stderr);
4914 fprintf(stderr, "\n");
4915 }
4916
4917 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4918 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4919 if (offset) {
4920 assert(offset->u32 == 0);
4921 } else {
4922 /* the lower 15bit of the prim_mask contain the offset into LDS
4923 * while the upper bits contain the number of prims */
4924 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4925 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4926 Builder bld(ctx->program, ctx->block);
4927 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4928 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4929 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4930 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4931 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4932 }
4933
4934 unsigned idx = nir_intrinsic_base(instr);
4935 unsigned component = nir_intrinsic_component(instr);
4936 unsigned vertex_id = 2; /* P0 */
4937
4938 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4939 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4940 switch (src0->u32) {
4941 case 0:
4942 vertex_id = 2; /* P0 */
4943 break;
4944 case 1:
4945 vertex_id = 0; /* P10 */
4946 break;
4947 case 2:
4948 vertex_id = 1; /* P20 */
4949 break;
4950 default:
4951 unreachable("invalid vertex index");
4952 }
4953 }
4954
4955 if (dst.size() == 1) {
4956 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4957 } else {
4958 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4959 for (unsigned i = 0; i < dst.size(); i++)
4960 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4961 vec->definitions[0] = Definition(dst);
4962 bld.insert(std::move(vec));
4963 }
4964
4965 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4966 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4967 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4968 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4969 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4970
4971 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4972 } else {
4973 unreachable("Shader stage not implemented");
4974 }
4975 }
4976
4977 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4978 {
4979 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4980
4981 Builder bld(ctx->program, ctx->block);
4982 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4983 Temp vertex_offset;
4984
4985 if (!nir_src_is_const(*vertex_src)) {
4986 /* better code could be created, but this case probably doesn't happen
4987 * much in practice */
4988 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4989 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4990 Temp elem;
4991
4992 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4993 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4994 if (i % 2u)
4995 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4996 } else {
4997 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4998 }
4999
5000 if (vertex_offset.id()) {
5001 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
5002 Operand(i), indirect_vertex);
5003 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
5004 } else {
5005 vertex_offset = elem;
5006 }
5007 }
5008
5009 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5010 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
5011 } else {
5012 unsigned vertex = nir_src_as_uint(*vertex_src);
5013 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
5014 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
5015 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
5016 Operand((vertex % 2u) * 16u), Operand(16u));
5017 else
5018 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
5019 }
5020
5021 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
5022 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
5023 return offset_mul(ctx, offs, 4u);
5024 }
5025
5026 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5027 {
5028 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
5029
5030 Builder bld(ctx->program, ctx->block);
5031 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5032 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5033
5034 if (ctx->stage == geometry_gs) {
5035 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
5036 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
5037 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);
5038 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
5039 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
5040 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5041 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5042 } else {
5043 unreachable("Unsupported GS stage.");
5044 }
5045 }
5046
5047 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5048 {
5049 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5050
5051 Builder bld(ctx->program, ctx->block);
5052 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5053
5054 if (load_input_from_temps(ctx, instr, dst))
5055 return;
5056
5057 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
5058 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5059 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5060
5061 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5062 }
5063
5064 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5065 {
5066 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5067
5068 Builder bld(ctx->program, ctx->block);
5069
5070 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5071 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
5072 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5073
5074 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5075 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5076
5077 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5078 }
5079
5080 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5081 {
5082 switch (ctx->shader->info.stage) {
5083 case MESA_SHADER_GEOMETRY:
5084 visit_load_gs_per_vertex_input(ctx, instr);
5085 break;
5086 case MESA_SHADER_TESS_CTRL:
5087 visit_load_tcs_per_vertex_input(ctx, instr);
5088 break;
5089 case MESA_SHADER_TESS_EVAL:
5090 visit_load_tes_per_vertex_input(ctx, instr);
5091 break;
5092 default:
5093 unreachable("Unimplemented shader stage");
5094 }
5095 }
5096
5097 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5098 {
5099 visit_load_tcs_output(ctx, instr, true);
5100 }
5101
5102 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5103 {
5104 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5105 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5106
5107 visit_store_tcs_output(ctx, instr, true);
5108 }
5109
5110 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5111 {
5112 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5113
5114 Builder bld(ctx->program, ctx->block);
5115 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5116
5117 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5118 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5119 Operand tes_w(0u);
5120
5121 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5122 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5123 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5124 tes_w = Operand(tmp);
5125 }
5126
5127 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5128 emit_split_vector(ctx, tess_coord, 3);
5129 }
5130
5131 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5132 {
5133 if (ctx->program->info->need_indirect_descriptor_sets) {
5134 Builder bld(ctx->program, ctx->block);
5135 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5136 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5137 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5138 }
5139
5140 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5141 }
5142
5143
5144 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5145 {
5146 Builder bld(ctx->program, ctx->block);
5147 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5148 if (!nir_dest_is_divergent(instr->dest))
5149 index = bld.as_uniform(index);
5150 unsigned desc_set = nir_intrinsic_desc_set(instr);
5151 unsigned binding = nir_intrinsic_binding(instr);
5152
5153 Temp desc_ptr;
5154 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5155 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5156 unsigned offset = layout->binding[binding].offset;
5157 unsigned stride;
5158 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5159 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5160 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5161 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5162 offset = pipeline_layout->push_constant_size + 16 * idx;
5163 stride = 16;
5164 } else {
5165 desc_ptr = load_desc_ptr(ctx, desc_set);
5166 stride = layout->binding[binding].size;
5167 }
5168
5169 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5170 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5171 if (stride != 1) {
5172 if (nir_const_index) {
5173 const_index = const_index * stride;
5174 } else if (index.type() == RegType::vgpr) {
5175 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5176 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5177 } else {
5178 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5179 }
5180 }
5181 if (offset) {
5182 if (nir_const_index) {
5183 const_index = const_index + offset;
5184 } else if (index.type() == RegType::vgpr) {
5185 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5186 } else {
5187 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5188 }
5189 }
5190
5191 if (nir_const_index && const_index == 0) {
5192 index = desc_ptr;
5193 } else if (index.type() == RegType::vgpr) {
5194 index = bld.vadd32(bld.def(v1),
5195 nir_const_index ? Operand(const_index) : Operand(index),
5196 Operand(desc_ptr));
5197 } else {
5198 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5199 nir_const_index ? Operand(const_index) : Operand(index),
5200 Operand(desc_ptr));
5201 }
5202
5203 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5204 }
5205
5206 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5207 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5208 bool glc=false, bool readonly=true, bool allow_smem=true)
5209 {
5210 Builder bld(ctx->program, ctx->block);
5211
5212 bool use_smem = dst.type() != RegType::vgpr && (!glc || ctx->options->chip_class >= GFX8) && allow_smem;
5213 if (use_smem)
5214 offset = bld.as_uniform(offset);
5215
5216 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5217 info.glc = glc;
5218 info.barrier = readonly ? barrier_none : barrier_buffer;
5219 info.can_reorder = readonly;
5220 info.align_mul = align_mul;
5221 info.align_offset = align_offset;
5222 if (use_smem)
5223 emit_smem_load(ctx, bld, &info);
5224 else
5225 emit_mubuf_load(ctx, bld, &info);
5226 }
5227
5228 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5229 {
5230 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5231 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5232
5233 Builder bld(ctx->program, ctx->block);
5234
5235 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5236 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5237 unsigned binding = nir_intrinsic_binding(idx_instr);
5238 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5239
5240 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5241 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5242 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5243 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5244 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5245 if (ctx->options->chip_class >= GFX10) {
5246 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5247 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5248 S_008F0C_RESOURCE_LEVEL(1);
5249 } else {
5250 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5251 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5252 }
5253 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5254 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5255 Operand(0xFFFFFFFFu),
5256 Operand(desc_type));
5257 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5258 rsrc, upper_dwords);
5259 } else {
5260 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5261 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5262 }
5263 unsigned size = instr->dest.ssa.bit_size / 8;
5264 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5265 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5266 }
5267
5268 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5269 {
5270 Builder bld(ctx->program, ctx->block);
5271 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5272 unsigned offset = nir_intrinsic_base(instr);
5273 unsigned count = instr->dest.ssa.num_components;
5274 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5275
5276 if (index_cv && instr->dest.ssa.bit_size == 32) {
5277 unsigned start = (offset + index_cv->u32) / 4u;
5278 start -= ctx->args->ac.base_inline_push_consts;
5279 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5280 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5281 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5282 for (unsigned i = 0; i < count; ++i) {
5283 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5284 vec->operands[i] = Operand{elems[i]};
5285 }
5286 vec->definitions[0] = Definition(dst);
5287 ctx->block->instructions.emplace_back(std::move(vec));
5288 ctx->allocated_vec.emplace(dst.id(), elems);
5289 return;
5290 }
5291 }
5292
5293 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5294 if (offset != 0) // TODO check if index != 0 as well
5295 index = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5296 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5297 Temp vec = dst;
5298 bool trim = false;
5299 bool aligned = true;
5300
5301 if (instr->dest.ssa.bit_size == 8) {
5302 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5303 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5304 if (!aligned)
5305 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5306 } else if (instr->dest.ssa.bit_size == 16) {
5307 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5308 if (!aligned)
5309 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5310 }
5311
5312 aco_opcode op;
5313
5314 switch (vec.size()) {
5315 case 1:
5316 op = aco_opcode::s_load_dword;
5317 break;
5318 case 2:
5319 op = aco_opcode::s_load_dwordx2;
5320 break;
5321 case 3:
5322 vec = bld.tmp(s4);
5323 trim = true;
5324 case 4:
5325 op = aco_opcode::s_load_dwordx4;
5326 break;
5327 case 6:
5328 vec = bld.tmp(s8);
5329 trim = true;
5330 case 8:
5331 op = aco_opcode::s_load_dwordx8;
5332 break;
5333 default:
5334 unreachable("unimplemented or forbidden load_push_constant.");
5335 }
5336
5337 static_cast<SMEM_instruction*>(bld.smem(op, Definition(vec), ptr, index).instr)->prevent_overflow = true;
5338
5339 if (!aligned) {
5340 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5341 byte_align_scalar(ctx, vec, byte_offset, dst);
5342 return;
5343 }
5344
5345 if (trim) {
5346 emit_split_vector(ctx, vec, 4);
5347 RegClass rc = dst.size() == 3 ? s1 : s2;
5348 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5349 emit_extract_vector(ctx, vec, 0, rc),
5350 emit_extract_vector(ctx, vec, 1, rc),
5351 emit_extract_vector(ctx, vec, 2, rc));
5352
5353 }
5354 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5355 }
5356
5357 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5358 {
5359 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5360
5361 Builder bld(ctx->program, ctx->block);
5362
5363 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5364 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5365 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5366 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5367 if (ctx->options->chip_class >= GFX10) {
5368 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5369 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5370 S_008F0C_RESOURCE_LEVEL(1);
5371 } else {
5372 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5373 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5374 }
5375
5376 unsigned base = nir_intrinsic_base(instr);
5377 unsigned range = nir_intrinsic_range(instr);
5378
5379 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5380 if (base && offset.type() == RegType::sgpr)
5381 offset = bld.nuw().sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5382 else if (base && offset.type() == RegType::vgpr)
5383 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5384
5385 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5386 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5387 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5388 Operand(desc_type));
5389 unsigned size = instr->dest.ssa.bit_size / 8;
5390 // TODO: get alignment information for subdword constants
5391 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5392 }
5393
5394 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5395 {
5396 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5397 ctx->cf_info.exec_potentially_empty_discard = true;
5398
5399 ctx->program->needs_exact = true;
5400
5401 // TODO: optimize uniform conditions
5402 Builder bld(ctx->program, ctx->block);
5403 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5404 assert(src.regClass() == bld.lm);
5405 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5406 bld.pseudo(aco_opcode::p_discard_if, src);
5407 ctx->block->kind |= block_kind_uses_discard_if;
5408 return;
5409 }
5410
5411 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5412 {
5413 Builder bld(ctx->program, ctx->block);
5414
5415 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5416 ctx->cf_info.exec_potentially_empty_discard = true;
5417
5418 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5419 ctx->cf_info.parent_loop.has_divergent_continue;
5420
5421 if (ctx->block->loop_nest_depth &&
5422 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5423 /* we handle discards the same way as jump instructions */
5424 append_logical_end(ctx->block);
5425
5426 /* in loops, discard behaves like break */
5427 Block *linear_target = ctx->cf_info.parent_loop.exit;
5428 ctx->block->kind |= block_kind_discard;
5429
5430 if (!divergent) {
5431 /* uniform discard - loop ends here */
5432 assert(nir_instr_is_last(&instr->instr));
5433 ctx->block->kind |= block_kind_uniform;
5434 ctx->cf_info.has_branch = true;
5435 bld.branch(aco_opcode::p_branch);
5436 add_linear_edge(ctx->block->index, linear_target);
5437 return;
5438 }
5439
5440 /* we add a break right behind the discard() instructions */
5441 ctx->block->kind |= block_kind_break;
5442 unsigned idx = ctx->block->index;
5443
5444 ctx->cf_info.parent_loop.has_divergent_branch = true;
5445 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5446
5447 /* remove critical edges from linear CFG */
5448 bld.branch(aco_opcode::p_branch);
5449 Block* break_block = ctx->program->create_and_insert_block();
5450 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5451 break_block->kind |= block_kind_uniform;
5452 add_linear_edge(idx, break_block);
5453 add_linear_edge(break_block->index, linear_target);
5454 bld.reset(break_block);
5455 bld.branch(aco_opcode::p_branch);
5456
5457 Block* continue_block = ctx->program->create_and_insert_block();
5458 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5459 add_linear_edge(idx, continue_block);
5460 append_logical_start(continue_block);
5461 ctx->block = continue_block;
5462
5463 return;
5464 }
5465
5466 /* it can currently happen that NIR doesn't remove the unreachable code */
5467 if (!nir_instr_is_last(&instr->instr)) {
5468 ctx->program->needs_exact = true;
5469 /* save exec somewhere temporarily so that it doesn't get
5470 * overwritten before the discard from outer exec masks */
5471 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5472 bld.pseudo(aco_opcode::p_discard_if, cond);
5473 ctx->block->kind |= block_kind_uses_discard_if;
5474 return;
5475 }
5476
5477 /* This condition is incorrect for uniformly branched discards in a loop
5478 * predicated by a divergent condition, but the above code catches that case
5479 * and the discard would end up turning into a discard_if.
5480 * For example:
5481 * if (divergent) {
5482 * while (...) {
5483 * if (uniform) {
5484 * discard;
5485 * }
5486 * }
5487 * }
5488 */
5489 if (!ctx->cf_info.parent_if.is_divergent) {
5490 /* program just ends here */
5491 ctx->block->kind |= block_kind_uniform;
5492 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5493 0 /* enabled mask */, 9 /* dest */,
5494 false /* compressed */, true/* done */, true /* valid mask */);
5495 bld.sopp(aco_opcode::s_endpgm);
5496 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5497 } else {
5498 ctx->block->kind |= block_kind_discard;
5499 /* branch and linear edge is added by visit_if() */
5500 }
5501 }
5502
5503 enum aco_descriptor_type {
5504 ACO_DESC_IMAGE,
5505 ACO_DESC_FMASK,
5506 ACO_DESC_SAMPLER,
5507 ACO_DESC_BUFFER,
5508 ACO_DESC_PLANE_0,
5509 ACO_DESC_PLANE_1,
5510 ACO_DESC_PLANE_2,
5511 };
5512
5513 static bool
5514 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5515 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5516 return false;
5517 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5518 return dim == ac_image_cube ||
5519 dim == ac_image_1darray ||
5520 dim == ac_image_2darray ||
5521 dim == ac_image_2darraymsaa;
5522 }
5523
5524 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5525 enum aco_descriptor_type desc_type,
5526 const nir_tex_instr *tex_instr, bool image, bool write)
5527 {
5528 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5529 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5530 if (it != ctx->tex_desc.end())
5531 return it->second;
5532 */
5533 Temp index = Temp();
5534 bool index_set = false;
5535 unsigned constant_index = 0;
5536 unsigned descriptor_set;
5537 unsigned base_index;
5538 Builder bld(ctx->program, ctx->block);
5539
5540 if (!deref_instr) {
5541 assert(tex_instr && !image);
5542 descriptor_set = 0;
5543 base_index = tex_instr->sampler_index;
5544 } else {
5545 while(deref_instr->deref_type != nir_deref_type_var) {
5546 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5547 if (!array_size)
5548 array_size = 1;
5549
5550 assert(deref_instr->deref_type == nir_deref_type_array);
5551 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5552 if (const_value) {
5553 constant_index += array_size * const_value->u32;
5554 } else {
5555 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5556 if (indirect.type() == RegType::vgpr)
5557 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5558
5559 if (array_size != 1)
5560 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5561
5562 if (!index_set) {
5563 index = indirect;
5564 index_set = true;
5565 } else {
5566 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5567 }
5568 }
5569
5570 deref_instr = nir_src_as_deref(deref_instr->parent);
5571 }
5572 descriptor_set = deref_instr->var->data.descriptor_set;
5573 base_index = deref_instr->var->data.binding;
5574 }
5575
5576 Temp list = load_desc_ptr(ctx, descriptor_set);
5577 list = convert_pointer_to_64_bit(ctx, list);
5578
5579 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5580 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5581 unsigned offset = binding->offset;
5582 unsigned stride = binding->size;
5583 aco_opcode opcode;
5584 RegClass type;
5585
5586 assert(base_index < layout->binding_count);
5587
5588 switch (desc_type) {
5589 case ACO_DESC_IMAGE:
5590 type = s8;
5591 opcode = aco_opcode::s_load_dwordx8;
5592 break;
5593 case ACO_DESC_FMASK:
5594 type = s8;
5595 opcode = aco_opcode::s_load_dwordx8;
5596 offset += 32;
5597 break;
5598 case ACO_DESC_SAMPLER:
5599 type = s4;
5600 opcode = aco_opcode::s_load_dwordx4;
5601 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5602 offset += radv_combined_image_descriptor_sampler_offset(binding);
5603 break;
5604 case ACO_DESC_BUFFER:
5605 type = s4;
5606 opcode = aco_opcode::s_load_dwordx4;
5607 break;
5608 case ACO_DESC_PLANE_0:
5609 case ACO_DESC_PLANE_1:
5610 type = s8;
5611 opcode = aco_opcode::s_load_dwordx8;
5612 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5613 break;
5614 case ACO_DESC_PLANE_2:
5615 type = s4;
5616 opcode = aco_opcode::s_load_dwordx4;
5617 offset += 64;
5618 break;
5619 default:
5620 unreachable("invalid desc_type\n");
5621 }
5622
5623 offset += constant_index * stride;
5624
5625 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5626 (!index_set || binding->immutable_samplers_equal)) {
5627 if (binding->immutable_samplers_equal)
5628 constant_index = 0;
5629
5630 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5631 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5632 Operand(samplers[constant_index * 4 + 0]),
5633 Operand(samplers[constant_index * 4 + 1]),
5634 Operand(samplers[constant_index * 4 + 2]),
5635 Operand(samplers[constant_index * 4 + 3]));
5636 }
5637
5638 Operand off;
5639 if (!index_set) {
5640 off = bld.copy(bld.def(s1), Operand(offset));
5641 } else {
5642 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5643 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5644 }
5645
5646 Temp res = bld.smem(opcode, bld.def(type), list, off);
5647
5648 if (desc_type == ACO_DESC_PLANE_2) {
5649 Temp components[8];
5650 for (unsigned i = 0; i < 8; i++)
5651 components[i] = bld.tmp(s1);
5652 bld.pseudo(aco_opcode::p_split_vector,
5653 Definition(components[0]),
5654 Definition(components[1]),
5655 Definition(components[2]),
5656 Definition(components[3]),
5657 res);
5658
5659 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5660 bld.pseudo(aco_opcode::p_split_vector,
5661 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5662 Definition(components[4]),
5663 Definition(components[5]),
5664 Definition(components[6]),
5665 Definition(components[7]),
5666 desc2);
5667
5668 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5669 components[0], components[1], components[2], components[3],
5670 components[4], components[5], components[6], components[7]);
5671 }
5672
5673 return res;
5674 }
5675
5676 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5677 {
5678 switch (dim) {
5679 case GLSL_SAMPLER_DIM_BUF:
5680 return 1;
5681 case GLSL_SAMPLER_DIM_1D:
5682 return array ? 2 : 1;
5683 case GLSL_SAMPLER_DIM_2D:
5684 return array ? 3 : 2;
5685 case GLSL_SAMPLER_DIM_MS:
5686 return array ? 4 : 3;
5687 case GLSL_SAMPLER_DIM_3D:
5688 case GLSL_SAMPLER_DIM_CUBE:
5689 return 3;
5690 case GLSL_SAMPLER_DIM_RECT:
5691 case GLSL_SAMPLER_DIM_SUBPASS:
5692 return 2;
5693 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5694 return 3;
5695 default:
5696 break;
5697 }
5698 return 0;
5699 }
5700
5701
5702 /* Adjust the sample index according to FMASK.
5703 *
5704 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5705 * which is the identity mapping. Each nibble says which physical sample
5706 * should be fetched to get that sample.
5707 *
5708 * For example, 0x11111100 means there are only 2 samples stored and
5709 * the second sample covers 3/4 of the pixel. When reading samples 0
5710 * and 1, return physical sample 0 (determined by the first two 0s
5711 * in FMASK), otherwise return physical sample 1.
5712 *
5713 * The sample index should be adjusted as follows:
5714 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5715 */
5716 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5717 {
5718 Builder bld(ctx->program, ctx->block);
5719 Temp fmask = bld.tmp(v1);
5720 unsigned dim = ctx->options->chip_class >= GFX10
5721 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5722 : 0;
5723
5724 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5725 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5726 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5727 load->operands[0] = Operand(fmask_desc_ptr);
5728 load->operands[1] = Operand(s4); /* no sampler */
5729 load->operands[2] = Operand(coord);
5730 load->definitions[0] = Definition(fmask);
5731 load->glc = false;
5732 load->dlc = false;
5733 load->dmask = 0x1;
5734 load->unrm = true;
5735 load->da = da;
5736 load->dim = dim;
5737 load->can_reorder = true; /* fmask images shouldn't be modified */
5738 ctx->block->instructions.emplace_back(std::move(load));
5739
5740 Operand sample_index4;
5741 if (sample_index.isConstant()) {
5742 if (sample_index.constantValue() < 16) {
5743 sample_index4 = Operand(sample_index.constantValue() << 2);
5744 } else {
5745 sample_index4 = Operand(0u);
5746 }
5747 } else if (sample_index.regClass() == s1) {
5748 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5749 } else {
5750 assert(sample_index.regClass() == v1);
5751 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5752 }
5753
5754 Temp final_sample;
5755 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5756 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5757 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5758 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5759 else
5760 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5761
5762 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5763 * resource descriptor is 0 (invalid),
5764 */
5765 Temp compare = bld.tmp(bld.lm);
5766 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5767 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5768
5769 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5770
5771 /* Replace the MSAA sample index. */
5772 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5773 }
5774
5775 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5776 {
5777
5778 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5779 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5780 bool is_array = glsl_sampler_type_is_array(type);
5781 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5782 assert(!add_frag_pos && "Input attachments should be lowered.");
5783 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5784 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5785 int count = image_type_to_components_count(dim, is_array);
5786 std::vector<Temp> coords(count);
5787 Builder bld(ctx->program, ctx->block);
5788
5789 if (is_ms) {
5790 count--;
5791 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5792 /* get sample index */
5793 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5794 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5795 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5796 std::vector<Temp> fmask_load_address;
5797 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5798 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5799
5800 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5801 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5802 } else {
5803 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5804 }
5805 }
5806
5807 if (gfx9_1d) {
5808 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5809 coords.resize(coords.size() + 1);
5810 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5811 if (is_array)
5812 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5813 } else {
5814 for (int i = 0; i < count; i++)
5815 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5816 }
5817
5818 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5819 instr->intrinsic == nir_intrinsic_image_deref_store) {
5820 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5821 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5822
5823 if (!level_zero)
5824 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5825 }
5826
5827 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5828 for (unsigned i = 0; i < coords.size(); i++)
5829 vec->operands[i] = Operand(coords[i]);
5830 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5831 vec->definitions[0] = Definition(res);
5832 ctx->block->instructions.emplace_back(std::move(vec));
5833 return res;
5834 }
5835
5836
5837 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5838 {
5839 Builder bld(ctx->program, ctx->block);
5840 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5841 const struct glsl_type *type = glsl_without_array(var->type);
5842 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5843 bool is_array = glsl_sampler_type_is_array(type);
5844 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5845
5846 if (dim == GLSL_SAMPLER_DIM_BUF) {
5847 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5848 unsigned num_channels = util_last_bit(mask);
5849 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5850 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5851
5852 aco_opcode opcode;
5853 switch (num_channels) {
5854 case 1:
5855 opcode = aco_opcode::buffer_load_format_x;
5856 break;
5857 case 2:
5858 opcode = aco_opcode::buffer_load_format_xy;
5859 break;
5860 case 3:
5861 opcode = aco_opcode::buffer_load_format_xyz;
5862 break;
5863 case 4:
5864 opcode = aco_opcode::buffer_load_format_xyzw;
5865 break;
5866 default:
5867 unreachable(">4 channel buffer image load");
5868 }
5869 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5870 load->operands[0] = Operand(rsrc);
5871 load->operands[1] = Operand(vindex);
5872 load->operands[2] = Operand((uint32_t) 0);
5873 Temp tmp;
5874 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5875 tmp = dst;
5876 else
5877 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5878 load->definitions[0] = Definition(tmp);
5879 load->idxen = true;
5880 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5881 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5882 load->barrier = barrier_image;
5883 ctx->block->instructions.emplace_back(std::move(load));
5884
5885 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5886 return;
5887 }
5888
5889 Temp coords = get_image_coords(ctx, instr, type);
5890 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5891
5892 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5893 unsigned num_components = util_bitcount(dmask);
5894 Temp tmp;
5895 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5896 tmp = dst;
5897 else
5898 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5899
5900 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5901 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5902
5903 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5904 load->operands[0] = Operand(resource);
5905 load->operands[1] = Operand(s4); /* no sampler */
5906 load->operands[2] = Operand(coords);
5907 load->definitions[0] = Definition(tmp);
5908 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5909 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5910 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5911 load->dmask = dmask;
5912 load->unrm = true;
5913 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5914 load->barrier = barrier_image;
5915 ctx->block->instructions.emplace_back(std::move(load));
5916
5917 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5918 return;
5919 }
5920
5921 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5922 {
5923 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5924 const struct glsl_type *type = glsl_without_array(var->type);
5925 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5926 bool is_array = glsl_sampler_type_is_array(type);
5927 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5928
5929 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5930
5931 if (dim == GLSL_SAMPLER_DIM_BUF) {
5932 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5933 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5934 aco_opcode opcode;
5935 switch (data.size()) {
5936 case 1:
5937 opcode = aco_opcode::buffer_store_format_x;
5938 break;
5939 case 2:
5940 opcode = aco_opcode::buffer_store_format_xy;
5941 break;
5942 case 3:
5943 opcode = aco_opcode::buffer_store_format_xyz;
5944 break;
5945 case 4:
5946 opcode = aco_opcode::buffer_store_format_xyzw;
5947 break;
5948 default:
5949 unreachable(">4 channel buffer image store");
5950 }
5951 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5952 store->operands[0] = Operand(rsrc);
5953 store->operands[1] = Operand(vindex);
5954 store->operands[2] = Operand((uint32_t) 0);
5955 store->operands[3] = Operand(data);
5956 store->idxen = true;
5957 store->glc = glc;
5958 store->dlc = false;
5959 store->disable_wqm = true;
5960 store->barrier = barrier_image;
5961 ctx->program->needs_exact = true;
5962 ctx->block->instructions.emplace_back(std::move(store));
5963 return;
5964 }
5965
5966 assert(data.type() == RegType::vgpr);
5967 Temp coords = get_image_coords(ctx, instr, type);
5968 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5969
5970 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5971 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5972
5973 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5974 store->operands[0] = Operand(resource);
5975 store->operands[1] = Operand(data);
5976 store->operands[2] = Operand(coords);
5977 store->glc = glc;
5978 store->dlc = false;
5979 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5980 store->dmask = (1 << data.size()) - 1;
5981 store->unrm = true;
5982 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5983 store->disable_wqm = true;
5984 store->barrier = barrier_image;
5985 ctx->program->needs_exact = true;
5986 ctx->block->instructions.emplace_back(std::move(store));
5987 return;
5988 }
5989
5990 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5991 {
5992 /* return the previous value if dest is ever used */
5993 bool return_previous = false;
5994 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5995 return_previous = true;
5996 break;
5997 }
5998 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5999 return_previous = true;
6000 break;
6001 }
6002
6003 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6004 const struct glsl_type *type = glsl_without_array(var->type);
6005 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6006 bool is_array = glsl_sampler_type_is_array(type);
6007 Builder bld(ctx->program, ctx->block);
6008
6009 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
6010 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
6011
6012 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
6013 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
6014
6015 aco_opcode buf_op, image_op;
6016 switch (instr->intrinsic) {
6017 case nir_intrinsic_image_deref_atomic_add:
6018 buf_op = aco_opcode::buffer_atomic_add;
6019 image_op = aco_opcode::image_atomic_add;
6020 break;
6021 case nir_intrinsic_image_deref_atomic_umin:
6022 buf_op = aco_opcode::buffer_atomic_umin;
6023 image_op = aco_opcode::image_atomic_umin;
6024 break;
6025 case nir_intrinsic_image_deref_atomic_imin:
6026 buf_op = aco_opcode::buffer_atomic_smin;
6027 image_op = aco_opcode::image_atomic_smin;
6028 break;
6029 case nir_intrinsic_image_deref_atomic_umax:
6030 buf_op = aco_opcode::buffer_atomic_umax;
6031 image_op = aco_opcode::image_atomic_umax;
6032 break;
6033 case nir_intrinsic_image_deref_atomic_imax:
6034 buf_op = aco_opcode::buffer_atomic_smax;
6035 image_op = aco_opcode::image_atomic_smax;
6036 break;
6037 case nir_intrinsic_image_deref_atomic_and:
6038 buf_op = aco_opcode::buffer_atomic_and;
6039 image_op = aco_opcode::image_atomic_and;
6040 break;
6041 case nir_intrinsic_image_deref_atomic_or:
6042 buf_op = aco_opcode::buffer_atomic_or;
6043 image_op = aco_opcode::image_atomic_or;
6044 break;
6045 case nir_intrinsic_image_deref_atomic_xor:
6046 buf_op = aco_opcode::buffer_atomic_xor;
6047 image_op = aco_opcode::image_atomic_xor;
6048 break;
6049 case nir_intrinsic_image_deref_atomic_exchange:
6050 buf_op = aco_opcode::buffer_atomic_swap;
6051 image_op = aco_opcode::image_atomic_swap;
6052 break;
6053 case nir_intrinsic_image_deref_atomic_comp_swap:
6054 buf_op = aco_opcode::buffer_atomic_cmpswap;
6055 image_op = aco_opcode::image_atomic_cmpswap;
6056 break;
6057 default:
6058 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
6059 }
6060
6061 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6062
6063 if (dim == GLSL_SAMPLER_DIM_BUF) {
6064 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
6065 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
6066 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6067 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6068 mubuf->operands[0] = Operand(resource);
6069 mubuf->operands[1] = Operand(vindex);
6070 mubuf->operands[2] = Operand((uint32_t)0);
6071 mubuf->operands[3] = Operand(data);
6072 if (return_previous)
6073 mubuf->definitions[0] = Definition(dst);
6074 mubuf->offset = 0;
6075 mubuf->idxen = true;
6076 mubuf->glc = return_previous;
6077 mubuf->dlc = false; /* Not needed for atomics */
6078 mubuf->disable_wqm = true;
6079 mubuf->barrier = barrier_image;
6080 ctx->program->needs_exact = true;
6081 ctx->block->instructions.emplace_back(std::move(mubuf));
6082 return;
6083 }
6084
6085 Temp coords = get_image_coords(ctx, instr, type);
6086 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6087 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6088 mimg->operands[0] = Operand(resource);
6089 mimg->operands[1] = Operand(data);
6090 mimg->operands[2] = Operand(coords);
6091 if (return_previous)
6092 mimg->definitions[0] = Definition(dst);
6093 mimg->glc = return_previous;
6094 mimg->dlc = false; /* Not needed for atomics */
6095 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6096 mimg->dmask = (1 << data.size()) - 1;
6097 mimg->unrm = true;
6098 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6099 mimg->disable_wqm = true;
6100 mimg->barrier = barrier_image;
6101 ctx->program->needs_exact = true;
6102 ctx->block->instructions.emplace_back(std::move(mimg));
6103 return;
6104 }
6105
6106 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6107 {
6108 if (in_elements && ctx->options->chip_class == GFX8) {
6109 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6110 Builder bld(ctx->program, ctx->block);
6111
6112 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6113
6114 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6115 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6116
6117 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6118 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6119
6120 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6121 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6122
6123 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6124 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6125 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6126 if (dst.type() == RegType::vgpr)
6127 bld.copy(Definition(dst), shr_dst);
6128
6129 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6130 } else {
6131 emit_extract_vector(ctx, desc, 2, dst);
6132 }
6133 }
6134
6135 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6136 {
6137 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6138 const struct glsl_type *type = glsl_without_array(var->type);
6139 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6140 bool is_array = glsl_sampler_type_is_array(type);
6141 Builder bld(ctx->program, ctx->block);
6142
6143 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6144 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6145 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6146 }
6147
6148 /* LOD */
6149 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6150
6151 /* Resource */
6152 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6153
6154 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6155
6156 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6157 mimg->operands[0] = Operand(resource);
6158 mimg->operands[1] = Operand(s4); /* no sampler */
6159 mimg->operands[2] = Operand(lod);
6160 uint8_t& dmask = mimg->dmask;
6161 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6162 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6163 mimg->da = glsl_sampler_type_is_array(type);
6164 mimg->can_reorder = true;
6165 Definition& def = mimg->definitions[0];
6166 ctx->block->instructions.emplace_back(std::move(mimg));
6167
6168 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6169 glsl_sampler_type_is_array(type)) {
6170
6171 assert(instr->dest.ssa.num_components == 3);
6172 Temp tmp = {ctx->program->allocateId(), v3};
6173 def = Definition(tmp);
6174 emit_split_vector(ctx, tmp, 3);
6175
6176 /* divide 3rd value by 6 by multiplying with magic number */
6177 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6178 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6179
6180 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6181 emit_extract_vector(ctx, tmp, 0, v1),
6182 emit_extract_vector(ctx, tmp, 1, v1),
6183 by_6);
6184
6185 } else if (ctx->options->chip_class == GFX9 &&
6186 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6187 glsl_sampler_type_is_array(type)) {
6188 assert(instr->dest.ssa.num_components == 2);
6189 def = Definition(dst);
6190 dmask = 0x5;
6191 } else {
6192 def = Definition(dst);
6193 }
6194
6195 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6196 }
6197
6198 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6199 {
6200 Builder bld(ctx->program, ctx->block);
6201 unsigned num_components = instr->num_components;
6202
6203 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6204 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6205 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6206
6207 unsigned access = nir_intrinsic_access(instr);
6208 bool glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
6209 unsigned size = instr->dest.ssa.bit_size / 8;
6210
6211 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[0].ssa, access);
6212 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6213 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6214 */
6215 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_store : has_vmem_store));
6216 allow_smem |= ((access & ACCESS_RESTRICT) && (access & ACCESS_NON_WRITEABLE)) || (access & ACCESS_CAN_REORDER);
6217
6218 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6219 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false, allow_smem);
6220 }
6221
6222 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6223 {
6224 Builder bld(ctx->program, ctx->block);
6225 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6226 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6227 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6228 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6229
6230 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6231 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6232
6233 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6234 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[1].ssa, nir_intrinsic_access(instr));
6235 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6236 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6237 */
6238 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_loadstore : has_vmem_loadstore));
6239
6240 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6241 ctx->options->chip_class >= GFX8 &&
6242 (elem_size_bytes >= 4 || can_subdword_ssbo_store_use_smem(instr)) &&
6243 allow_smem;
6244 if (smem)
6245 offset = bld.as_uniform(offset);
6246 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6247
6248 unsigned write_count = 0;
6249 Temp write_datas[32];
6250 unsigned offsets[32];
6251 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6252 data, writemask, 16, &write_count, write_datas, offsets);
6253
6254 for (unsigned i = 0; i < write_count; i++) {
6255 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6256 if (smem && ctx->stage == fragment_fs)
6257 op = aco_opcode::p_fs_buffer_store_smem;
6258
6259 if (smem) {
6260 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6261 store->operands[0] = Operand(rsrc);
6262 if (offsets[i]) {
6263 Temp off = bld.nuw().sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6264 offset, Operand(offsets[i]));
6265 store->operands[1] = Operand(off);
6266 } else {
6267 store->operands[1] = Operand(offset);
6268 }
6269 if (op != aco_opcode::p_fs_buffer_store_smem)
6270 store->operands[1].setFixed(m0);
6271 store->operands[2] = Operand(write_datas[i]);
6272 store->glc = glc;
6273 store->dlc = false;
6274 store->disable_wqm = true;
6275 store->barrier = barrier_buffer;
6276 ctx->block->instructions.emplace_back(std::move(store));
6277 ctx->program->wb_smem_l1_on_end = true;
6278 if (op == aco_opcode::p_fs_buffer_store_smem) {
6279 ctx->block->kind |= block_kind_needs_lowering;
6280 ctx->program->needs_exact = true;
6281 }
6282 } else {
6283 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6284 store->operands[0] = Operand(rsrc);
6285 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6286 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6287 store->operands[3] = Operand(write_datas[i]);
6288 store->offset = offsets[i];
6289 store->offen = (offset.type() == RegType::vgpr);
6290 store->glc = glc;
6291 store->dlc = false;
6292 store->disable_wqm = true;
6293 store->barrier = barrier_buffer;
6294 ctx->program->needs_exact = true;
6295 ctx->block->instructions.emplace_back(std::move(store));
6296 }
6297 }
6298 }
6299
6300 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6301 {
6302 /* return the previous value if dest is ever used */
6303 bool return_previous = false;
6304 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6305 return_previous = true;
6306 break;
6307 }
6308 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6309 return_previous = true;
6310 break;
6311 }
6312
6313 Builder bld(ctx->program, ctx->block);
6314 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6315
6316 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6317 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6318 get_ssa_temp(ctx, instr->src[3].ssa), data);
6319
6320 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6321 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6322 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6323
6324 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6325
6326 aco_opcode op32, op64;
6327 switch (instr->intrinsic) {
6328 case nir_intrinsic_ssbo_atomic_add:
6329 op32 = aco_opcode::buffer_atomic_add;
6330 op64 = aco_opcode::buffer_atomic_add_x2;
6331 break;
6332 case nir_intrinsic_ssbo_atomic_imin:
6333 op32 = aco_opcode::buffer_atomic_smin;
6334 op64 = aco_opcode::buffer_atomic_smin_x2;
6335 break;
6336 case nir_intrinsic_ssbo_atomic_umin:
6337 op32 = aco_opcode::buffer_atomic_umin;
6338 op64 = aco_opcode::buffer_atomic_umin_x2;
6339 break;
6340 case nir_intrinsic_ssbo_atomic_imax:
6341 op32 = aco_opcode::buffer_atomic_smax;
6342 op64 = aco_opcode::buffer_atomic_smax_x2;
6343 break;
6344 case nir_intrinsic_ssbo_atomic_umax:
6345 op32 = aco_opcode::buffer_atomic_umax;
6346 op64 = aco_opcode::buffer_atomic_umax_x2;
6347 break;
6348 case nir_intrinsic_ssbo_atomic_and:
6349 op32 = aco_opcode::buffer_atomic_and;
6350 op64 = aco_opcode::buffer_atomic_and_x2;
6351 break;
6352 case nir_intrinsic_ssbo_atomic_or:
6353 op32 = aco_opcode::buffer_atomic_or;
6354 op64 = aco_opcode::buffer_atomic_or_x2;
6355 break;
6356 case nir_intrinsic_ssbo_atomic_xor:
6357 op32 = aco_opcode::buffer_atomic_xor;
6358 op64 = aco_opcode::buffer_atomic_xor_x2;
6359 break;
6360 case nir_intrinsic_ssbo_atomic_exchange:
6361 op32 = aco_opcode::buffer_atomic_swap;
6362 op64 = aco_opcode::buffer_atomic_swap_x2;
6363 break;
6364 case nir_intrinsic_ssbo_atomic_comp_swap:
6365 op32 = aco_opcode::buffer_atomic_cmpswap;
6366 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6367 break;
6368 default:
6369 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6370 }
6371 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6372 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6373 mubuf->operands[0] = Operand(rsrc);
6374 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6375 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6376 mubuf->operands[3] = Operand(data);
6377 if (return_previous)
6378 mubuf->definitions[0] = Definition(dst);
6379 mubuf->offset = 0;
6380 mubuf->offen = (offset.type() == RegType::vgpr);
6381 mubuf->glc = return_previous;
6382 mubuf->dlc = false; /* Not needed for atomics */
6383 mubuf->disable_wqm = true;
6384 mubuf->barrier = barrier_buffer;
6385 ctx->program->needs_exact = true;
6386 ctx->block->instructions.emplace_back(std::move(mubuf));
6387 }
6388
6389 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6390
6391 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6392 Builder bld(ctx->program, ctx->block);
6393 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6394 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6395 }
6396
6397 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6398 {
6399 Builder bld(ctx->program, ctx->block);
6400 unsigned num_components = instr->num_components;
6401 unsigned component_size = instr->dest.ssa.bit_size / 8;
6402
6403 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6404 get_ssa_temp(ctx, &instr->dest.ssa),
6405 num_components, component_size};
6406 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6407 info.align_mul = nir_intrinsic_align_mul(instr);
6408 info.align_offset = nir_intrinsic_align_offset(instr);
6409 info.barrier = barrier_buffer;
6410 info.can_reorder = false;
6411 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6412 * it's safe to use SMEM */
6413 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6414 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6415 emit_global_load(ctx, bld, &info);
6416 } else {
6417 info.offset = Operand(bld.as_uniform(info.offset));
6418 emit_smem_load(ctx, bld, &info);
6419 }
6420 }
6421
6422 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6423 {
6424 Builder bld(ctx->program, ctx->block);
6425 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6426 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6427
6428 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6429 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6430 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6431
6432 if (ctx->options->chip_class >= GFX7)
6433 addr = as_vgpr(ctx, addr);
6434
6435 unsigned write_count = 0;
6436 Temp write_datas[32];
6437 unsigned offsets[32];
6438 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6439 16, &write_count, write_datas, offsets);
6440
6441 for (unsigned i = 0; i < write_count; i++) {
6442 if (ctx->options->chip_class >= GFX7) {
6443 unsigned offset = offsets[i];
6444 Temp store_addr = addr;
6445 if (offset > 0 && ctx->options->chip_class < GFX9) {
6446 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6447 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6448 Temp carry = bld.tmp(bld.lm);
6449 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6450
6451 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6452 Operand(offset), addr0);
6453 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6454 Operand(0u), addr1,
6455 carry).def(1).setHint(vcc);
6456
6457 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6458
6459 offset = 0;
6460 }
6461
6462 bool global = ctx->options->chip_class >= GFX9;
6463 aco_opcode op;
6464 switch (write_datas[i].bytes()) {
6465 case 1:
6466 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6467 break;
6468 case 2:
6469 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6470 break;
6471 case 4:
6472 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6473 break;
6474 case 8:
6475 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6476 break;
6477 case 12:
6478 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6479 break;
6480 case 16:
6481 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6482 break;
6483 default:
6484 unreachable("store_global not implemented for this size.");
6485 }
6486
6487 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6488 flat->operands[0] = Operand(store_addr);
6489 flat->operands[1] = Operand(s1);
6490 flat->operands[2] = Operand(write_datas[i]);
6491 flat->glc = glc;
6492 flat->dlc = false;
6493 flat->offset = offset;
6494 flat->disable_wqm = true;
6495 flat->barrier = barrier_buffer;
6496 ctx->program->needs_exact = true;
6497 ctx->block->instructions.emplace_back(std::move(flat));
6498 } else {
6499 assert(ctx->options->chip_class == GFX6);
6500
6501 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6502
6503 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6504
6505 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6506 mubuf->operands[0] = Operand(rsrc);
6507 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6508 mubuf->operands[2] = Operand(0u);
6509 mubuf->operands[3] = Operand(write_datas[i]);
6510 mubuf->glc = glc;
6511 mubuf->dlc = false;
6512 mubuf->offset = offsets[i];
6513 mubuf->addr64 = addr.type() == RegType::vgpr;
6514 mubuf->disable_wqm = true;
6515 mubuf->barrier = barrier_buffer;
6516 ctx->program->needs_exact = true;
6517 ctx->block->instructions.emplace_back(std::move(mubuf));
6518 }
6519 }
6520 }
6521
6522 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6523 {
6524 /* return the previous value if dest is ever used */
6525 bool return_previous = false;
6526 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6527 return_previous = true;
6528 break;
6529 }
6530 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6531 return_previous = true;
6532 break;
6533 }
6534
6535 Builder bld(ctx->program, ctx->block);
6536 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6537 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6538
6539 if (ctx->options->chip_class >= GFX7)
6540 addr = as_vgpr(ctx, addr);
6541
6542 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6543 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6544 get_ssa_temp(ctx, instr->src[2].ssa), data);
6545
6546 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6547
6548 aco_opcode op32, op64;
6549
6550 if (ctx->options->chip_class >= GFX7) {
6551 bool global = ctx->options->chip_class >= GFX9;
6552 switch (instr->intrinsic) {
6553 case nir_intrinsic_global_atomic_add:
6554 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6555 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6556 break;
6557 case nir_intrinsic_global_atomic_imin:
6558 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6559 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6560 break;
6561 case nir_intrinsic_global_atomic_umin:
6562 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6563 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6564 break;
6565 case nir_intrinsic_global_atomic_imax:
6566 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6567 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6568 break;
6569 case nir_intrinsic_global_atomic_umax:
6570 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6571 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6572 break;
6573 case nir_intrinsic_global_atomic_and:
6574 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6575 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6576 break;
6577 case nir_intrinsic_global_atomic_or:
6578 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6579 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6580 break;
6581 case nir_intrinsic_global_atomic_xor:
6582 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6583 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6584 break;
6585 case nir_intrinsic_global_atomic_exchange:
6586 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6587 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6588 break;
6589 case nir_intrinsic_global_atomic_comp_swap:
6590 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6591 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6592 break;
6593 default:
6594 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6595 }
6596
6597 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6598 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6599 flat->operands[0] = Operand(addr);
6600 flat->operands[1] = Operand(s1);
6601 flat->operands[2] = Operand(data);
6602 if (return_previous)
6603 flat->definitions[0] = Definition(dst);
6604 flat->glc = return_previous;
6605 flat->dlc = false; /* Not needed for atomics */
6606 flat->offset = 0;
6607 flat->disable_wqm = true;
6608 flat->barrier = barrier_buffer;
6609 ctx->program->needs_exact = true;
6610 ctx->block->instructions.emplace_back(std::move(flat));
6611 } else {
6612 assert(ctx->options->chip_class == GFX6);
6613
6614 switch (instr->intrinsic) {
6615 case nir_intrinsic_global_atomic_add:
6616 op32 = aco_opcode::buffer_atomic_add;
6617 op64 = aco_opcode::buffer_atomic_add_x2;
6618 break;
6619 case nir_intrinsic_global_atomic_imin:
6620 op32 = aco_opcode::buffer_atomic_smin;
6621 op64 = aco_opcode::buffer_atomic_smin_x2;
6622 break;
6623 case nir_intrinsic_global_atomic_umin:
6624 op32 = aco_opcode::buffer_atomic_umin;
6625 op64 = aco_opcode::buffer_atomic_umin_x2;
6626 break;
6627 case nir_intrinsic_global_atomic_imax:
6628 op32 = aco_opcode::buffer_atomic_smax;
6629 op64 = aco_opcode::buffer_atomic_smax_x2;
6630 break;
6631 case nir_intrinsic_global_atomic_umax:
6632 op32 = aco_opcode::buffer_atomic_umax;
6633 op64 = aco_opcode::buffer_atomic_umax_x2;
6634 break;
6635 case nir_intrinsic_global_atomic_and:
6636 op32 = aco_opcode::buffer_atomic_and;
6637 op64 = aco_opcode::buffer_atomic_and_x2;
6638 break;
6639 case nir_intrinsic_global_atomic_or:
6640 op32 = aco_opcode::buffer_atomic_or;
6641 op64 = aco_opcode::buffer_atomic_or_x2;
6642 break;
6643 case nir_intrinsic_global_atomic_xor:
6644 op32 = aco_opcode::buffer_atomic_xor;
6645 op64 = aco_opcode::buffer_atomic_xor_x2;
6646 break;
6647 case nir_intrinsic_global_atomic_exchange:
6648 op32 = aco_opcode::buffer_atomic_swap;
6649 op64 = aco_opcode::buffer_atomic_swap_x2;
6650 break;
6651 case nir_intrinsic_global_atomic_comp_swap:
6652 op32 = aco_opcode::buffer_atomic_cmpswap;
6653 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6654 break;
6655 default:
6656 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6657 }
6658
6659 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6660
6661 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6662
6663 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6664 mubuf->operands[0] = Operand(rsrc);
6665 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6666 mubuf->operands[2] = Operand(0u);
6667 mubuf->operands[3] = Operand(data);
6668 if (return_previous)
6669 mubuf->definitions[0] = Definition(dst);
6670 mubuf->glc = return_previous;
6671 mubuf->dlc = false;
6672 mubuf->offset = 0;
6673 mubuf->addr64 = addr.type() == RegType::vgpr;
6674 mubuf->disable_wqm = true;
6675 mubuf->barrier = barrier_buffer;
6676 ctx->program->needs_exact = true;
6677 ctx->block->instructions.emplace_back(std::move(mubuf));
6678 }
6679 }
6680
6681 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6682 Builder bld(ctx->program, ctx->block);
6683 switch(instr->intrinsic) {
6684 case nir_intrinsic_group_memory_barrier:
6685 case nir_intrinsic_memory_barrier:
6686 bld.barrier(aco_opcode::p_memory_barrier_common);
6687 break;
6688 case nir_intrinsic_memory_barrier_buffer:
6689 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6690 break;
6691 case nir_intrinsic_memory_barrier_image:
6692 bld.barrier(aco_opcode::p_memory_barrier_image);
6693 break;
6694 case nir_intrinsic_memory_barrier_tcs_patch:
6695 case nir_intrinsic_memory_barrier_shared:
6696 bld.barrier(aco_opcode::p_memory_barrier_shared);
6697 break;
6698 default:
6699 unreachable("Unimplemented memory barrier intrinsic");
6700 break;
6701 }
6702 }
6703
6704 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6705 {
6706 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6707 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6708 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6709 Builder bld(ctx->program, ctx->block);
6710
6711 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6712 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6713 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6714 }
6715
6716 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6717 {
6718 unsigned writemask = nir_intrinsic_write_mask(instr);
6719 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6720 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6721 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6722
6723 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6724 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6725 }
6726
6727 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6728 {
6729 unsigned offset = nir_intrinsic_base(instr);
6730 Builder bld(ctx->program, ctx->block);
6731 Operand m = load_lds_size_m0(bld);
6732 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6733 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6734
6735 unsigned num_operands = 3;
6736 aco_opcode op32, op64, op32_rtn, op64_rtn;
6737 switch(instr->intrinsic) {
6738 case nir_intrinsic_shared_atomic_add:
6739 op32 = aco_opcode::ds_add_u32;
6740 op64 = aco_opcode::ds_add_u64;
6741 op32_rtn = aco_opcode::ds_add_rtn_u32;
6742 op64_rtn = aco_opcode::ds_add_rtn_u64;
6743 break;
6744 case nir_intrinsic_shared_atomic_imin:
6745 op32 = aco_opcode::ds_min_i32;
6746 op64 = aco_opcode::ds_min_i64;
6747 op32_rtn = aco_opcode::ds_min_rtn_i32;
6748 op64_rtn = aco_opcode::ds_min_rtn_i64;
6749 break;
6750 case nir_intrinsic_shared_atomic_umin:
6751 op32 = aco_opcode::ds_min_u32;
6752 op64 = aco_opcode::ds_min_u64;
6753 op32_rtn = aco_opcode::ds_min_rtn_u32;
6754 op64_rtn = aco_opcode::ds_min_rtn_u64;
6755 break;
6756 case nir_intrinsic_shared_atomic_imax:
6757 op32 = aco_opcode::ds_max_i32;
6758 op64 = aco_opcode::ds_max_i64;
6759 op32_rtn = aco_opcode::ds_max_rtn_i32;
6760 op64_rtn = aco_opcode::ds_max_rtn_i64;
6761 break;
6762 case nir_intrinsic_shared_atomic_umax:
6763 op32 = aco_opcode::ds_max_u32;
6764 op64 = aco_opcode::ds_max_u64;
6765 op32_rtn = aco_opcode::ds_max_rtn_u32;
6766 op64_rtn = aco_opcode::ds_max_rtn_u64;
6767 break;
6768 case nir_intrinsic_shared_atomic_and:
6769 op32 = aco_opcode::ds_and_b32;
6770 op64 = aco_opcode::ds_and_b64;
6771 op32_rtn = aco_opcode::ds_and_rtn_b32;
6772 op64_rtn = aco_opcode::ds_and_rtn_b64;
6773 break;
6774 case nir_intrinsic_shared_atomic_or:
6775 op32 = aco_opcode::ds_or_b32;
6776 op64 = aco_opcode::ds_or_b64;
6777 op32_rtn = aco_opcode::ds_or_rtn_b32;
6778 op64_rtn = aco_opcode::ds_or_rtn_b64;
6779 break;
6780 case nir_intrinsic_shared_atomic_xor:
6781 op32 = aco_opcode::ds_xor_b32;
6782 op64 = aco_opcode::ds_xor_b64;
6783 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6784 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6785 break;
6786 case nir_intrinsic_shared_atomic_exchange:
6787 op32 = aco_opcode::ds_write_b32;
6788 op64 = aco_opcode::ds_write_b64;
6789 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6790 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6791 break;
6792 case nir_intrinsic_shared_atomic_comp_swap:
6793 op32 = aco_opcode::ds_cmpst_b32;
6794 op64 = aco_opcode::ds_cmpst_b64;
6795 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6796 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6797 num_operands = 4;
6798 break;
6799 case nir_intrinsic_shared_atomic_fadd:
6800 op32 = aco_opcode::ds_add_f32;
6801 op32_rtn = aco_opcode::ds_add_rtn_f32;
6802 op64 = aco_opcode::num_opcodes;
6803 op64_rtn = aco_opcode::num_opcodes;
6804 break;
6805 default:
6806 unreachable("Unhandled shared atomic intrinsic");
6807 }
6808
6809 /* return the previous value if dest is ever used */
6810 bool return_previous = false;
6811 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6812 return_previous = true;
6813 break;
6814 }
6815 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6816 return_previous = true;
6817 break;
6818 }
6819
6820 aco_opcode op;
6821 if (data.size() == 1) {
6822 assert(instr->dest.ssa.bit_size == 32);
6823 op = return_previous ? op32_rtn : op32;
6824 } else {
6825 assert(instr->dest.ssa.bit_size == 64);
6826 op = return_previous ? op64_rtn : op64;
6827 }
6828
6829 if (offset > 65535) {
6830 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6831 offset = 0;
6832 }
6833
6834 aco_ptr<DS_instruction> ds;
6835 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6836 ds->operands[0] = Operand(address);
6837 ds->operands[1] = Operand(data);
6838 if (num_operands == 4)
6839 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6840 ds->operands[num_operands - 1] = m;
6841 ds->offset0 = offset;
6842 if (return_previous)
6843 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6844 ctx->block->instructions.emplace_back(std::move(ds));
6845 }
6846
6847 Temp get_scratch_resource(isel_context *ctx)
6848 {
6849 Builder bld(ctx->program, ctx->block);
6850 Temp scratch_addr = ctx->program->private_segment_buffer;
6851 if (ctx->stage != compute_cs)
6852 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6853
6854 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6855 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);
6856
6857 if (ctx->program->chip_class >= GFX10) {
6858 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6859 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6860 S_008F0C_RESOURCE_LEVEL(1);
6861 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6862 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6863 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6864 }
6865
6866 /* older generations need element size = 4 bytes. element size removed in GFX9 */
6867 if (ctx->program->chip_class <= GFX8)
6868 rsrc_conf |= S_008F0C_ELEMENT_SIZE(1);
6869
6870 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6871 }
6872
6873 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6874 Builder bld(ctx->program, ctx->block);
6875 Temp rsrc = get_scratch_resource(ctx);
6876 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6877 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6878
6879 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6880 instr->dest.ssa.bit_size / 8u, rsrc};
6881 info.align_mul = nir_intrinsic_align_mul(instr);
6882 info.align_offset = nir_intrinsic_align_offset(instr);
6883 info.swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 0;
6884 info.can_reorder = false;
6885 info.soffset = ctx->program->scratch_offset;
6886 emit_scratch_load(ctx, bld, &info);
6887 }
6888
6889 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6890 Builder bld(ctx->program, ctx->block);
6891 Temp rsrc = get_scratch_resource(ctx);
6892 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6893 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6894
6895 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6896 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6897
6898 unsigned write_count = 0;
6899 Temp write_datas[32];
6900 unsigned offsets[32];
6901 unsigned swizzle_component_size = ctx->program->chip_class <= GFX8 ? 4 : 16;
6902 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6903 swizzle_component_size, &write_count, write_datas, offsets);
6904
6905 for (unsigned i = 0; i < write_count; i++) {
6906 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6907 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true, true);
6908 }
6909 }
6910
6911 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6912 uint8_t log2_ps_iter_samples;
6913 if (ctx->program->info->ps.force_persample) {
6914 log2_ps_iter_samples =
6915 util_logbase2(ctx->options->key.fs.num_samples);
6916 } else {
6917 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6918 }
6919
6920 /* The bit pattern matches that used by fixed function fragment
6921 * processing. */
6922 static const unsigned ps_iter_masks[] = {
6923 0xffff, /* not used */
6924 0x5555,
6925 0x1111,
6926 0x0101,
6927 0x0001,
6928 };
6929 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6930
6931 Builder bld(ctx->program, ctx->block);
6932
6933 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6934 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6935 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6936 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6937 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6938 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6939 }
6940
6941 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6942 Builder bld(ctx->program, ctx->block);
6943
6944 unsigned stream = nir_intrinsic_stream_id(instr);
6945 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6946 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6947 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6948
6949 /* get GSVS ring */
6950 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6951
6952 unsigned num_components =
6953 ctx->program->info->gs.num_stream_output_components[stream];
6954 assert(num_components);
6955
6956 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6957 unsigned stream_offset = 0;
6958 for (unsigned i = 0; i < stream; i++) {
6959 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6960 stream_offset += prev_stride * ctx->program->wave_size;
6961 }
6962
6963 /* Limit on the stride field for <= GFX7. */
6964 assert(stride < (1 << 14));
6965
6966 Temp gsvs_dwords[4];
6967 for (unsigned i = 0; i < 4; i++)
6968 gsvs_dwords[i] = bld.tmp(s1);
6969 bld.pseudo(aco_opcode::p_split_vector,
6970 Definition(gsvs_dwords[0]),
6971 Definition(gsvs_dwords[1]),
6972 Definition(gsvs_dwords[2]),
6973 Definition(gsvs_dwords[3]),
6974 gsvs_ring);
6975
6976 if (stream_offset) {
6977 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6978
6979 Temp carry = bld.tmp(s1);
6980 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6981 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));
6982 }
6983
6984 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)));
6985 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6986
6987 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6988 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6989
6990 unsigned offset = 0;
6991 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6992 if (ctx->program->info->gs.output_streams[i] != stream)
6993 continue;
6994
6995 for (unsigned j = 0; j < 4; j++) {
6996 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6997 continue;
6998
6999 if (ctx->outputs.mask[i] & (1 << j)) {
7000 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
7001 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
7002 if (const_offset >= 4096u) {
7003 if (vaddr_offset.isUndefined())
7004 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
7005 else
7006 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
7007 const_offset %= 4096u;
7008 }
7009
7010 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
7011 mtbuf->operands[0] = Operand(gsvs_ring);
7012 mtbuf->operands[1] = vaddr_offset;
7013 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
7014 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
7015 mtbuf->offen = !vaddr_offset.isUndefined();
7016 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
7017 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
7018 mtbuf->offset = const_offset;
7019 mtbuf->glc = true;
7020 mtbuf->slc = true;
7021 mtbuf->barrier = barrier_gs_data;
7022 mtbuf->can_reorder = true;
7023 bld.insert(std::move(mtbuf));
7024 }
7025
7026 offset += ctx->shader->info.gs.vertices_out;
7027 }
7028
7029 /* outputs for the next vertex are undefined and keeping them around can
7030 * create invalid IR with control flow */
7031 ctx->outputs.mask[i] = 0;
7032 }
7033
7034 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
7035 }
7036
7037 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
7038 {
7039 Builder bld(ctx->program, ctx->block);
7040
7041 if (cluster_size == 1) {
7042 return src;
7043 } if (op == nir_op_iand && cluster_size == 4) {
7044 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7045 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7046 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
7047 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
7048 } else if (op == nir_op_ior && cluster_size == 4) {
7049 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7050 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
7051 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
7052 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
7053 //subgroupAnd(val) -> (exec & ~val) == 0
7054 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7055 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7056 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
7057 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
7058 //subgroupOr(val) -> (val & exec) != 0
7059 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
7060 return bool_to_vector_condition(ctx, tmp);
7061 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
7062 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7063 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7064 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
7065 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
7066 return bool_to_vector_condition(ctx, tmp);
7067 } else {
7068 //subgroupClustered{And,Or,Xor}(val, n) ->
7069 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7070 //cluster_offset = ~(n - 1) & lane_id
7071 //cluster_mask = ((1 << n) - 1)
7072 //subgroupClusteredAnd():
7073 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7074 //subgroupClusteredOr():
7075 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7076 //subgroupClusteredXor():
7077 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7078 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7079 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7080
7081 Temp tmp;
7082 if (op == nir_op_iand)
7083 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7084 else
7085 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7086
7087 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7088
7089 if (ctx->program->chip_class <= GFX7)
7090 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7091 else if (ctx->program->wave_size == 64)
7092 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7093 else
7094 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7095 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7096 if (cluster_mask != 0xffffffff)
7097 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7098
7099 Definition cmp_def = Definition();
7100 if (op == nir_op_iand) {
7101 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7102 } else if (op == nir_op_ior) {
7103 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7104 } else if (op == nir_op_ixor) {
7105 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7106 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7107 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7108 }
7109 cmp_def.setHint(vcc);
7110 return cmp_def.getTemp();
7111 }
7112 }
7113
7114 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7115 {
7116 Builder bld(ctx->program, ctx->block);
7117
7118 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7119 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7120 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7121 Temp tmp;
7122 if (op == nir_op_iand)
7123 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7124 else
7125 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7126
7127 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7128 Temp lo = lohi.def(0).getTemp();
7129 Temp hi = lohi.def(1).getTemp();
7130 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7131
7132 Definition cmp_def = Definition();
7133 if (op == nir_op_iand)
7134 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7135 else if (op == nir_op_ior)
7136 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7137 else if (op == nir_op_ixor)
7138 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7139 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7140 cmp_def.setHint(vcc);
7141 return cmp_def.getTemp();
7142 }
7143
7144 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7145 {
7146 Builder bld(ctx->program, ctx->block);
7147
7148 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7149 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7150 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7151 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7152 if (op == nir_op_iand)
7153 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7154 else if (op == nir_op_ior)
7155 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7156 else if (op == nir_op_ixor)
7157 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7158
7159 assert(false);
7160 return Temp();
7161 }
7162
7163 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7164 {
7165 Builder bld(ctx->program, ctx->block);
7166 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7167 if (src.regClass().type() == RegType::vgpr) {
7168 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7169 } else if (src.regClass() == s1) {
7170 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7171 } else if (src.regClass() == s2) {
7172 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7173 } else {
7174 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7175 nir_print_instr(&instr->instr, stderr);
7176 fprintf(stderr, "\n");
7177 }
7178 }
7179
7180 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7181 {
7182 Builder bld(ctx->program, ctx->block);
7183 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7184 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7185 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7186
7187 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7188 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7189 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7190 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7191
7192 /* Build DD X/Y */
7193 if (ctx->program->chip_class >= GFX8) {
7194 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7195 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7196 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7197 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7198 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7199 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7200 } else {
7201 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7202 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7203 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7204 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7205 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7206 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7207 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7208 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7209 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7210 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7211 }
7212
7213 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7214 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7215 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7216 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7217 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7218 Temp wqm1 = bld.tmp(v1);
7219 emit_wqm(ctx, tmp1, wqm1, true);
7220 Temp wqm2 = bld.tmp(v1);
7221 emit_wqm(ctx, tmp2, wqm2, true);
7222 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7223 return;
7224 }
7225
7226 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7227 {
7228 Builder bld(ctx->program, ctx->block);
7229 switch(instr->intrinsic) {
7230 case nir_intrinsic_load_barycentric_sample:
7231 case nir_intrinsic_load_barycentric_pixel:
7232 case nir_intrinsic_load_barycentric_centroid: {
7233 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7234 Temp bary = Temp(0, s2);
7235 switch (mode) {
7236 case INTERP_MODE_SMOOTH:
7237 case INTERP_MODE_NONE:
7238 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7239 bary = get_arg(ctx, ctx->args->ac.persp_center);
7240 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7241 bary = ctx->persp_centroid;
7242 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7243 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7244 break;
7245 case INTERP_MODE_NOPERSPECTIVE:
7246 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7247 bary = get_arg(ctx, ctx->args->ac.linear_center);
7248 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7249 bary = ctx->linear_centroid;
7250 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7251 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7252 break;
7253 default:
7254 break;
7255 }
7256 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7257 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7258 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7259 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7260 Operand(p1), Operand(p2));
7261 emit_split_vector(ctx, dst, 2);
7262 break;
7263 }
7264 case nir_intrinsic_load_barycentric_model: {
7265 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7266
7267 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7268 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7269 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7270 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7271 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7272 Operand(p1), Operand(p2), Operand(p3));
7273 emit_split_vector(ctx, dst, 3);
7274 break;
7275 }
7276 case nir_intrinsic_load_barycentric_at_sample: {
7277 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7278 switch (ctx->options->key.fs.num_samples) {
7279 case 2: sample_pos_offset += 1 << 3; break;
7280 case 4: sample_pos_offset += 3 << 3; break;
7281 case 8: sample_pos_offset += 7 << 3; break;
7282 default: break;
7283 }
7284 Temp sample_pos;
7285 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7286 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7287 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7288 //TODO: bounds checking?
7289 if (addr.type() == RegType::sgpr) {
7290 Operand offset;
7291 if (const_addr) {
7292 sample_pos_offset += const_addr->u32 << 3;
7293 offset = Operand(sample_pos_offset);
7294 } else if (ctx->options->chip_class >= GFX9) {
7295 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7296 } else {
7297 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7298 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7299 }
7300
7301 Operand off = bld.copy(bld.def(s1), Operand(offset));
7302 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7303
7304 } else if (ctx->options->chip_class >= GFX9) {
7305 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7306 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7307 } else if (ctx->options->chip_class >= GFX7) {
7308 /* addr += private_segment_buffer + sample_pos_offset */
7309 Temp tmp0 = bld.tmp(s1);
7310 Temp tmp1 = bld.tmp(s1);
7311 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7312 Definition scc_tmp = bld.def(s1, scc);
7313 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7314 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7315 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7316 Temp pck0 = bld.tmp(v1);
7317 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7318 tmp1 = as_vgpr(ctx, tmp1);
7319 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);
7320 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7321
7322 /* sample_pos = flat_load_dwordx2 addr */
7323 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7324 } else {
7325 assert(ctx->options->chip_class == GFX6);
7326
7327 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7328 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7329 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7330
7331 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7332 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7333
7334 sample_pos = bld.tmp(v2);
7335
7336 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7337 load->definitions[0] = Definition(sample_pos);
7338 load->operands[0] = Operand(rsrc);
7339 load->operands[1] = Operand(addr);
7340 load->operands[2] = Operand(0u);
7341 load->offset = sample_pos_offset;
7342 load->offen = 0;
7343 load->addr64 = true;
7344 load->glc = false;
7345 load->dlc = false;
7346 load->disable_wqm = false;
7347 load->barrier = barrier_none;
7348 load->can_reorder = true;
7349 ctx->block->instructions.emplace_back(std::move(load));
7350 }
7351
7352 /* sample_pos -= 0.5 */
7353 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7354 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7355 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7356 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7357 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7358
7359 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7360 break;
7361 }
7362 case nir_intrinsic_load_barycentric_at_offset: {
7363 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7364 RegClass rc = RegClass(offset.type(), 1);
7365 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7366 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7367 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7368 break;
7369 }
7370 case nir_intrinsic_load_front_face: {
7371 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7372 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7373 break;
7374 }
7375 case nir_intrinsic_load_view_index: {
7376 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7377 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7378 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7379 break;
7380 }
7381
7382 /* fallthrough */
7383 }
7384 case nir_intrinsic_load_layer_id: {
7385 unsigned idx = nir_intrinsic_base(instr);
7386 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7387 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7388 break;
7389 }
7390 case nir_intrinsic_load_frag_coord: {
7391 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7392 break;
7393 }
7394 case nir_intrinsic_load_sample_pos: {
7395 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7396 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7397 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7398 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7399 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7400 break;
7401 }
7402 case nir_intrinsic_load_tess_coord:
7403 visit_load_tess_coord(ctx, instr);
7404 break;
7405 case nir_intrinsic_load_interpolated_input:
7406 visit_load_interpolated_input(ctx, instr);
7407 break;
7408 case nir_intrinsic_store_output:
7409 visit_store_output(ctx, instr);
7410 break;
7411 case nir_intrinsic_load_input:
7412 case nir_intrinsic_load_input_vertex:
7413 visit_load_input(ctx, instr);
7414 break;
7415 case nir_intrinsic_load_output:
7416 visit_load_output(ctx, instr);
7417 break;
7418 case nir_intrinsic_load_per_vertex_input:
7419 visit_load_per_vertex_input(ctx, instr);
7420 break;
7421 case nir_intrinsic_load_per_vertex_output:
7422 visit_load_per_vertex_output(ctx, instr);
7423 break;
7424 case nir_intrinsic_store_per_vertex_output:
7425 visit_store_per_vertex_output(ctx, instr);
7426 break;
7427 case nir_intrinsic_load_ubo:
7428 visit_load_ubo(ctx, instr);
7429 break;
7430 case nir_intrinsic_load_push_constant:
7431 visit_load_push_constant(ctx, instr);
7432 break;
7433 case nir_intrinsic_load_constant:
7434 visit_load_constant(ctx, instr);
7435 break;
7436 case nir_intrinsic_vulkan_resource_index:
7437 visit_load_resource(ctx, instr);
7438 break;
7439 case nir_intrinsic_discard:
7440 visit_discard(ctx, instr);
7441 break;
7442 case nir_intrinsic_discard_if:
7443 visit_discard_if(ctx, instr);
7444 break;
7445 case nir_intrinsic_load_shared:
7446 visit_load_shared(ctx, instr);
7447 break;
7448 case nir_intrinsic_store_shared:
7449 visit_store_shared(ctx, instr);
7450 break;
7451 case nir_intrinsic_shared_atomic_add:
7452 case nir_intrinsic_shared_atomic_imin:
7453 case nir_intrinsic_shared_atomic_umin:
7454 case nir_intrinsic_shared_atomic_imax:
7455 case nir_intrinsic_shared_atomic_umax:
7456 case nir_intrinsic_shared_atomic_and:
7457 case nir_intrinsic_shared_atomic_or:
7458 case nir_intrinsic_shared_atomic_xor:
7459 case nir_intrinsic_shared_atomic_exchange:
7460 case nir_intrinsic_shared_atomic_comp_swap:
7461 case nir_intrinsic_shared_atomic_fadd:
7462 visit_shared_atomic(ctx, instr);
7463 break;
7464 case nir_intrinsic_image_deref_load:
7465 visit_image_load(ctx, instr);
7466 break;
7467 case nir_intrinsic_image_deref_store:
7468 visit_image_store(ctx, instr);
7469 break;
7470 case nir_intrinsic_image_deref_atomic_add:
7471 case nir_intrinsic_image_deref_atomic_umin:
7472 case nir_intrinsic_image_deref_atomic_imin:
7473 case nir_intrinsic_image_deref_atomic_umax:
7474 case nir_intrinsic_image_deref_atomic_imax:
7475 case nir_intrinsic_image_deref_atomic_and:
7476 case nir_intrinsic_image_deref_atomic_or:
7477 case nir_intrinsic_image_deref_atomic_xor:
7478 case nir_intrinsic_image_deref_atomic_exchange:
7479 case nir_intrinsic_image_deref_atomic_comp_swap:
7480 visit_image_atomic(ctx, instr);
7481 break;
7482 case nir_intrinsic_image_deref_size:
7483 visit_image_size(ctx, instr);
7484 break;
7485 case nir_intrinsic_load_ssbo:
7486 visit_load_ssbo(ctx, instr);
7487 break;
7488 case nir_intrinsic_store_ssbo:
7489 visit_store_ssbo(ctx, instr);
7490 break;
7491 case nir_intrinsic_load_global:
7492 visit_load_global(ctx, instr);
7493 break;
7494 case nir_intrinsic_store_global:
7495 visit_store_global(ctx, instr);
7496 break;
7497 case nir_intrinsic_global_atomic_add:
7498 case nir_intrinsic_global_atomic_imin:
7499 case nir_intrinsic_global_atomic_umin:
7500 case nir_intrinsic_global_atomic_imax:
7501 case nir_intrinsic_global_atomic_umax:
7502 case nir_intrinsic_global_atomic_and:
7503 case nir_intrinsic_global_atomic_or:
7504 case nir_intrinsic_global_atomic_xor:
7505 case nir_intrinsic_global_atomic_exchange:
7506 case nir_intrinsic_global_atomic_comp_swap:
7507 visit_global_atomic(ctx, instr);
7508 break;
7509 case nir_intrinsic_ssbo_atomic_add:
7510 case nir_intrinsic_ssbo_atomic_imin:
7511 case nir_intrinsic_ssbo_atomic_umin:
7512 case nir_intrinsic_ssbo_atomic_imax:
7513 case nir_intrinsic_ssbo_atomic_umax:
7514 case nir_intrinsic_ssbo_atomic_and:
7515 case nir_intrinsic_ssbo_atomic_or:
7516 case nir_intrinsic_ssbo_atomic_xor:
7517 case nir_intrinsic_ssbo_atomic_exchange:
7518 case nir_intrinsic_ssbo_atomic_comp_swap:
7519 visit_atomic_ssbo(ctx, instr);
7520 break;
7521 case nir_intrinsic_load_scratch:
7522 visit_load_scratch(ctx, instr);
7523 break;
7524 case nir_intrinsic_store_scratch:
7525 visit_store_scratch(ctx, instr);
7526 break;
7527 case nir_intrinsic_get_buffer_size:
7528 visit_get_buffer_size(ctx, instr);
7529 break;
7530 case nir_intrinsic_control_barrier: {
7531 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7532 /* GFX6 only (thanks to a hw bug workaround):
7533 * The real barrier instruction isn’t needed, because an entire patch
7534 * always fits into a single wave.
7535 */
7536 break;
7537 }
7538
7539 if (ctx->program->workgroup_size > ctx->program->wave_size)
7540 bld.sopp(aco_opcode::s_barrier);
7541
7542 break;
7543 }
7544 case nir_intrinsic_memory_barrier_tcs_patch:
7545 case nir_intrinsic_group_memory_barrier:
7546 case nir_intrinsic_memory_barrier:
7547 case nir_intrinsic_memory_barrier_buffer:
7548 case nir_intrinsic_memory_barrier_image:
7549 case nir_intrinsic_memory_barrier_shared:
7550 emit_memory_barrier(ctx, instr);
7551 break;
7552 case nir_intrinsic_load_num_work_groups: {
7553 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7554 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7555 emit_split_vector(ctx, dst, 3);
7556 break;
7557 }
7558 case nir_intrinsic_load_local_invocation_id: {
7559 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7560 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7561 emit_split_vector(ctx, dst, 3);
7562 break;
7563 }
7564 case nir_intrinsic_load_work_group_id: {
7565 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7566 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7567 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7568 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7569 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7570 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7571 emit_split_vector(ctx, dst, 3);
7572 break;
7573 }
7574 case nir_intrinsic_load_local_invocation_index: {
7575 Temp id = emit_mbcnt(ctx, bld.def(v1));
7576
7577 /* The tg_size bits [6:11] contain the subgroup id,
7578 * we need this multiplied by the wave size, and then OR the thread id to it.
7579 */
7580 if (ctx->program->wave_size == 64) {
7581 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7582 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7583 get_arg(ctx, ctx->args->ac.tg_size));
7584 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7585 } else {
7586 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7587 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7588 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7589 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7590 }
7591 break;
7592 }
7593 case nir_intrinsic_load_subgroup_id: {
7594 if (ctx->stage == compute_cs) {
7595 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7596 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7597 } else {
7598 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7599 }
7600 break;
7601 }
7602 case nir_intrinsic_load_subgroup_invocation: {
7603 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7604 break;
7605 }
7606 case nir_intrinsic_load_num_subgroups: {
7607 if (ctx->stage == compute_cs)
7608 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7609 get_arg(ctx, ctx->args->ac.tg_size));
7610 else
7611 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7612 break;
7613 }
7614 case nir_intrinsic_ballot: {
7615 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7616 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7617 Definition tmp = bld.def(dst.regClass());
7618 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7619 if (instr->src[0].ssa->bit_size == 1) {
7620 assert(src.regClass() == bld.lm);
7621 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7622 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7623 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7624 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7625 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7626 } else {
7627 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7628 nir_print_instr(&instr->instr, stderr);
7629 fprintf(stderr, "\n");
7630 }
7631 if (dst.size() != bld.lm.size()) {
7632 /* Wave32 with ballot size set to 64 */
7633 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7634 }
7635 emit_wqm(ctx, tmp.getTemp(), dst);
7636 break;
7637 }
7638 case nir_intrinsic_shuffle:
7639 case nir_intrinsic_read_invocation: {
7640 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7641 if (!nir_src_is_divergent(instr->src[0])) {
7642 emit_uniform_subgroup(ctx, instr, src);
7643 } else {
7644 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7645 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7646 tid = bld.as_uniform(tid);
7647 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7648 if (src.regClass() == v1b || src.regClass() == v2b) {
7649 Temp tmp = bld.tmp(v1);
7650 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7651 if (dst.type() == RegType::vgpr)
7652 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7653 else
7654 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7655 } else if (src.regClass() == v1) {
7656 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7657 } else if (src.regClass() == v2) {
7658 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7659 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7660 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7661 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7662 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7663 emit_split_vector(ctx, dst, 2);
7664 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7665 assert(src.regClass() == bld.lm);
7666 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7667 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7668 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7669 assert(src.regClass() == bld.lm);
7670 Temp tmp;
7671 if (ctx->program->chip_class <= GFX7)
7672 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7673 else if (ctx->program->wave_size == 64)
7674 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7675 else
7676 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7677 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7678 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7679 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7680 } else {
7681 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7682 nir_print_instr(&instr->instr, stderr);
7683 fprintf(stderr, "\n");
7684 }
7685 }
7686 break;
7687 }
7688 case nir_intrinsic_load_sample_id: {
7689 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7690 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7691 break;
7692 }
7693 case nir_intrinsic_load_sample_mask_in: {
7694 visit_load_sample_mask_in(ctx, instr);
7695 break;
7696 }
7697 case nir_intrinsic_read_first_invocation: {
7698 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7699 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7700 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7701 emit_wqm(ctx,
7702 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7703 dst);
7704 } else if (src.regClass() == v2) {
7705 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7706 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7707 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7708 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7709 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7710 emit_split_vector(ctx, dst, 2);
7711 } else if (instr->dest.ssa.bit_size == 1) {
7712 assert(src.regClass() == bld.lm);
7713 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7714 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7715 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7716 } else if (src.regClass() == s1) {
7717 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7718 } else if (src.regClass() == s2) {
7719 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7720 } else {
7721 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7722 nir_print_instr(&instr->instr, stderr);
7723 fprintf(stderr, "\n");
7724 }
7725 break;
7726 }
7727 case nir_intrinsic_vote_all: {
7728 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7729 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7730 assert(src.regClass() == bld.lm);
7731 assert(dst.regClass() == bld.lm);
7732
7733 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7734 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7735 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7736 break;
7737 }
7738 case nir_intrinsic_vote_any: {
7739 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7740 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7741 assert(src.regClass() == bld.lm);
7742 assert(dst.regClass() == bld.lm);
7743
7744 Temp tmp = bool_to_scalar_condition(ctx, src);
7745 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7746 break;
7747 }
7748 case nir_intrinsic_reduce:
7749 case nir_intrinsic_inclusive_scan:
7750 case nir_intrinsic_exclusive_scan: {
7751 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7752 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7753 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7754 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7755 nir_intrinsic_cluster_size(instr) : 0;
7756 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7757
7758 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7759 emit_uniform_subgroup(ctx, instr, src);
7760 } else if (instr->dest.ssa.bit_size == 1) {
7761 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7762 op = nir_op_iand;
7763 else if (op == nir_op_iadd)
7764 op = nir_op_ixor;
7765 else if (op == nir_op_umax || op == nir_op_imax)
7766 op = nir_op_ior;
7767 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7768
7769 switch (instr->intrinsic) {
7770 case nir_intrinsic_reduce:
7771 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7772 break;
7773 case nir_intrinsic_exclusive_scan:
7774 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7775 break;
7776 case nir_intrinsic_inclusive_scan:
7777 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7778 break;
7779 default:
7780 assert(false);
7781 }
7782 } else if (cluster_size == 1) {
7783 bld.copy(Definition(dst), src);
7784 } else {
7785 unsigned bit_size = instr->src[0].ssa->bit_size;
7786
7787 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7788
7789 ReduceOp reduce_op;
7790 switch (op) {
7791 #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;
7792 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7793 CASEI(iadd)
7794 CASEI(imul)
7795 CASEI(imin)
7796 CASEI(umin)
7797 CASEI(imax)
7798 CASEI(umax)
7799 CASEI(iand)
7800 CASEI(ior)
7801 CASEI(ixor)
7802 CASEF(fadd)
7803 CASEF(fmul)
7804 CASEF(fmin)
7805 CASEF(fmax)
7806 default:
7807 unreachable("unknown reduction op");
7808 #undef CASEI
7809 #undef CASEF
7810 }
7811
7812 aco_opcode aco_op;
7813 switch (instr->intrinsic) {
7814 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7815 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7816 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7817 default:
7818 unreachable("unknown reduce intrinsic");
7819 }
7820
7821 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7822 reduce->operands[0] = Operand(src);
7823 // filled in by aco_reduce_assign.cpp, used internally as part of the
7824 // reduce sequence
7825 assert(dst.size() == 1 || dst.size() == 2);
7826 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7827 reduce->operands[2] = Operand(v1.as_linear());
7828
7829 Temp tmp_dst = bld.tmp(dst.regClass());
7830 reduce->definitions[0] = Definition(tmp_dst);
7831 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7832 reduce->definitions[2] = Definition();
7833 reduce->definitions[3] = Definition(scc, s1);
7834 reduce->definitions[4] = Definition();
7835 reduce->reduce_op = reduce_op;
7836 reduce->cluster_size = cluster_size;
7837 ctx->block->instructions.emplace_back(std::move(reduce));
7838
7839 emit_wqm(ctx, tmp_dst, dst);
7840 }
7841 break;
7842 }
7843 case nir_intrinsic_quad_broadcast: {
7844 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7845 if (!nir_dest_is_divergent(instr->dest)) {
7846 emit_uniform_subgroup(ctx, instr, src);
7847 } else {
7848 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7849 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7850 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7851
7852 if (instr->dest.ssa.bit_size == 1) {
7853 assert(src.regClass() == bld.lm);
7854 assert(dst.regClass() == bld.lm);
7855 uint32_t half_mask = 0x11111111u << lane;
7856 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7857 Temp tmp = bld.tmp(bld.lm);
7858 bld.sop1(Builder::s_wqm, Definition(tmp),
7859 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7860 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7861 emit_wqm(ctx, tmp, dst);
7862 } else if (instr->dest.ssa.bit_size == 8) {
7863 Temp tmp = bld.tmp(v1);
7864 if (ctx->program->chip_class >= GFX8)
7865 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7866 else
7867 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7868 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7869 } else if (instr->dest.ssa.bit_size == 16) {
7870 Temp tmp = bld.tmp(v1);
7871 if (ctx->program->chip_class >= GFX8)
7872 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7873 else
7874 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7875 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7876 } else if (instr->dest.ssa.bit_size == 32) {
7877 if (ctx->program->chip_class >= GFX8)
7878 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7879 else
7880 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7881 } else if (instr->dest.ssa.bit_size == 64) {
7882 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7883 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7884 if (ctx->program->chip_class >= GFX8) {
7885 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7886 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7887 } else {
7888 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7889 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7890 }
7891 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7892 emit_split_vector(ctx, dst, 2);
7893 } else {
7894 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7895 nir_print_instr(&instr->instr, stderr);
7896 fprintf(stderr, "\n");
7897 }
7898 }
7899 break;
7900 }
7901 case nir_intrinsic_quad_swap_horizontal:
7902 case nir_intrinsic_quad_swap_vertical:
7903 case nir_intrinsic_quad_swap_diagonal:
7904 case nir_intrinsic_quad_swizzle_amd: {
7905 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7906 if (!nir_dest_is_divergent(instr->dest)) {
7907 emit_uniform_subgroup(ctx, instr, src);
7908 break;
7909 }
7910 uint16_t dpp_ctrl = 0;
7911 switch (instr->intrinsic) {
7912 case nir_intrinsic_quad_swap_horizontal:
7913 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7914 break;
7915 case nir_intrinsic_quad_swap_vertical:
7916 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7917 break;
7918 case nir_intrinsic_quad_swap_diagonal:
7919 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7920 break;
7921 case nir_intrinsic_quad_swizzle_amd:
7922 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7923 break;
7924 default:
7925 break;
7926 }
7927 if (ctx->program->chip_class < GFX8)
7928 dpp_ctrl |= (1 << 15);
7929
7930 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7931 if (instr->dest.ssa.bit_size == 1) {
7932 assert(src.regClass() == bld.lm);
7933 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7934 if (ctx->program->chip_class >= GFX8)
7935 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7936 else
7937 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7938 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7939 emit_wqm(ctx, tmp, dst);
7940 } else if (instr->dest.ssa.bit_size == 8) {
7941 Temp tmp = bld.tmp(v1);
7942 if (ctx->program->chip_class >= GFX8)
7943 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7944 else
7945 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7946 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7947 } else if (instr->dest.ssa.bit_size == 16) {
7948 Temp tmp = bld.tmp(v1);
7949 if (ctx->program->chip_class >= GFX8)
7950 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7951 else
7952 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7953 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7954 } else if (instr->dest.ssa.bit_size == 32) {
7955 Temp tmp;
7956 if (ctx->program->chip_class >= GFX8)
7957 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7958 else
7959 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7960 emit_wqm(ctx, tmp, dst);
7961 } else if (instr->dest.ssa.bit_size == 64) {
7962 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7963 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7964 if (ctx->program->chip_class >= GFX8) {
7965 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7966 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7967 } else {
7968 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7969 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7970 }
7971 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7972 emit_split_vector(ctx, dst, 2);
7973 } else {
7974 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7975 nir_print_instr(&instr->instr, stderr);
7976 fprintf(stderr, "\n");
7977 }
7978 break;
7979 }
7980 case nir_intrinsic_masked_swizzle_amd: {
7981 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7982 if (!nir_dest_is_divergent(instr->dest)) {
7983 emit_uniform_subgroup(ctx, instr, src);
7984 break;
7985 }
7986 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7987 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7988 if (instr->dest.ssa.bit_size == 1) {
7989 assert(src.regClass() == bld.lm);
7990 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7991 src = emit_masked_swizzle(ctx, bld, src, mask);
7992 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7993 emit_wqm(ctx, tmp, dst);
7994 } else if (dst.regClass() == v1b) {
7995 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7996 emit_extract_vector(ctx, tmp, 0, dst);
7997 } else if (dst.regClass() == v2b) {
7998 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7999 emit_extract_vector(ctx, tmp, 0, dst);
8000 } else if (dst.regClass() == v1) {
8001 emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask), dst);
8002 } else if (dst.regClass() == v2) {
8003 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
8004 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
8005 lo = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, lo, mask));
8006 hi = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, hi, mask));
8007 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8008 emit_split_vector(ctx, dst, 2);
8009 } else {
8010 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8011 nir_print_instr(&instr->instr, stderr);
8012 fprintf(stderr, "\n");
8013 }
8014 break;
8015 }
8016 case nir_intrinsic_write_invocation_amd: {
8017 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
8018 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
8019 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
8020 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8021 if (dst.regClass() == v1) {
8022 /* src2 is ignored for writelane. RA assigns the same reg for dst */
8023 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
8024 } else if (dst.regClass() == v2) {
8025 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
8026 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
8027 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
8028 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
8029 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
8030 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
8031 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
8032 emit_split_vector(ctx, dst, 2);
8033 } else {
8034 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8035 nir_print_instr(&instr->instr, stderr);
8036 fprintf(stderr, "\n");
8037 }
8038 break;
8039 }
8040 case nir_intrinsic_mbcnt_amd: {
8041 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8042 RegClass rc = RegClass(src.type(), 1);
8043 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
8044 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
8045 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8046 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
8047 emit_wqm(ctx, wqm_tmp, dst);
8048 break;
8049 }
8050 case nir_intrinsic_load_helper_invocation: {
8051 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8052 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
8053 ctx->block->kind |= block_kind_needs_lowering;
8054 ctx->program->needs_exact = true;
8055 break;
8056 }
8057 case nir_intrinsic_is_helper_invocation: {
8058 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8059 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
8060 ctx->block->kind |= block_kind_needs_lowering;
8061 ctx->program->needs_exact = true;
8062 break;
8063 }
8064 case nir_intrinsic_demote:
8065 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
8066
8067 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8068 ctx->cf_info.exec_potentially_empty_discard = true;
8069 ctx->block->kind |= block_kind_uses_demote;
8070 ctx->program->needs_exact = true;
8071 break;
8072 case nir_intrinsic_demote_if: {
8073 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8074 assert(src.regClass() == bld.lm);
8075 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
8076 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
8077
8078 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8079 ctx->cf_info.exec_potentially_empty_discard = true;
8080 ctx->block->kind |= block_kind_uses_demote;
8081 ctx->program->needs_exact = true;
8082 break;
8083 }
8084 case nir_intrinsic_first_invocation: {
8085 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
8086 get_ssa_temp(ctx, &instr->dest.ssa));
8087 break;
8088 }
8089 case nir_intrinsic_shader_clock: {
8090 aco_opcode opcode =
8091 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
8092 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
8093 bld.smem(opcode, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
8094 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
8095 break;
8096 }
8097 case nir_intrinsic_load_vertex_id_zero_base: {
8098 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8099 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
8100 break;
8101 }
8102 case nir_intrinsic_load_first_vertex: {
8103 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8104 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
8105 break;
8106 }
8107 case nir_intrinsic_load_base_instance: {
8108 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8109 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
8110 break;
8111 }
8112 case nir_intrinsic_load_instance_id: {
8113 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8114 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
8115 break;
8116 }
8117 case nir_intrinsic_load_draw_id: {
8118 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8119 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8120 break;
8121 }
8122 case nir_intrinsic_load_invocation_id: {
8123 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8124
8125 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8126 if (ctx->options->chip_class >= GFX10)
8127 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8128 else
8129 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8130 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8131 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8132 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8133 } else {
8134 unreachable("Unsupported stage for load_invocation_id");
8135 }
8136
8137 break;
8138 }
8139 case nir_intrinsic_load_primitive_id: {
8140 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8141
8142 switch (ctx->shader->info.stage) {
8143 case MESA_SHADER_GEOMETRY:
8144 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8145 break;
8146 case MESA_SHADER_TESS_CTRL:
8147 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8148 break;
8149 case MESA_SHADER_TESS_EVAL:
8150 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8151 break;
8152 default:
8153 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8154 }
8155
8156 break;
8157 }
8158 case nir_intrinsic_load_patch_vertices_in: {
8159 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8160 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8161
8162 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8163 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8164 break;
8165 }
8166 case nir_intrinsic_emit_vertex_with_counter: {
8167 visit_emit_vertex_with_counter(ctx, instr);
8168 break;
8169 }
8170 case nir_intrinsic_end_primitive_with_counter: {
8171 unsigned stream = nir_intrinsic_stream_id(instr);
8172 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8173 break;
8174 }
8175 case nir_intrinsic_set_vertex_count: {
8176 /* unused, the HW keeps track of this for us */
8177 break;
8178 }
8179 default:
8180 fprintf(stderr, "Unimplemented intrinsic instr: ");
8181 nir_print_instr(&instr->instr, stderr);
8182 fprintf(stderr, "\n");
8183 abort();
8184
8185 break;
8186 }
8187 }
8188
8189
8190 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8191 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8192 enum glsl_base_type *stype)
8193 {
8194 nir_deref_instr *texture_deref_instr = NULL;
8195 nir_deref_instr *sampler_deref_instr = NULL;
8196 int plane = -1;
8197
8198 for (unsigned i = 0; i < instr->num_srcs; i++) {
8199 switch (instr->src[i].src_type) {
8200 case nir_tex_src_texture_deref:
8201 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8202 break;
8203 case nir_tex_src_sampler_deref:
8204 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8205 break;
8206 case nir_tex_src_plane:
8207 plane = nir_src_as_int(instr->src[i].src);
8208 break;
8209 default:
8210 break;
8211 }
8212 }
8213
8214 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8215
8216 if (!sampler_deref_instr)
8217 sampler_deref_instr = texture_deref_instr;
8218
8219 if (plane >= 0) {
8220 assert(instr->op != nir_texop_txf_ms &&
8221 instr->op != nir_texop_samples_identical);
8222 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8223 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8224 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8225 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8226 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8227 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8228 } else {
8229 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8230 }
8231 if (samp_ptr) {
8232 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8233
8234 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8235 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8236 Builder bld(ctx->program, ctx->block);
8237
8238 /* to avoid unnecessary moves, we split and recombine sampler and image */
8239 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8240 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8241 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8242 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8243 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8244 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8245 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8246 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8247
8248 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8249 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8250 img[0], img[1], img[2], img[3],
8251 img[4], img[5], img[6], img[7]);
8252 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8253 samp[0], samp[1], samp[2], samp[3]);
8254 }
8255 }
8256 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8257 instr->op == nir_texop_samples_identical))
8258 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8259 }
8260
8261 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8262 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8263 {
8264 Builder bld(ctx->program, ctx->block);
8265
8266 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8267 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8268 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8269
8270 Operand neg_one(0xbf800000u);
8271 Operand one(0x3f800000u);
8272 Operand two(0x40000000u);
8273 Operand four(0x40800000u);
8274
8275 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8276 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8277 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8278
8279 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8280 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8281 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8282 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);
8283
8284 // select sc
8285 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8286 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8287 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8288 one, is_ma_y);
8289 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8290
8291 // select tc
8292 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8293 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8294 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8295
8296 // select ma
8297 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8298 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8299 deriv_z, is_ma_z);
8300 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8301 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8302 }
8303
8304 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8305 {
8306 Builder bld(ctx->program, ctx->block);
8307 Temp ma, tc, sc, id;
8308
8309 if (is_array) {
8310 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8311
8312 // see comment in ac_prepare_cube_coords()
8313 if (ctx->options->chip_class <= GFX8)
8314 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8315 }
8316
8317 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8318
8319 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8320 vop3a->operands[0] = Operand(ma);
8321 vop3a->abs[0] = true;
8322 Temp invma = bld.tmp(v1);
8323 vop3a->definitions[0] = Definition(invma);
8324 ctx->block->instructions.emplace_back(std::move(vop3a));
8325
8326 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8327 if (!is_deriv)
8328 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8329
8330 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8331 if (!is_deriv)
8332 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8333
8334 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8335
8336 if (is_deriv) {
8337 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8338 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8339
8340 for (unsigned i = 0; i < 2; i++) {
8341 // see comment in ac_prepare_cube_coords()
8342 Temp deriv_ma;
8343 Temp deriv_sc, deriv_tc;
8344 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8345 &deriv_ma, &deriv_sc, &deriv_tc);
8346
8347 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8348
8349 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8350 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8351 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8352 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8353 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8354 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8355 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8356 }
8357
8358 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8359 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8360 }
8361
8362 if (is_array)
8363 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8364 coords.resize(3);
8365 coords[0] = sc;
8366 coords[1] = tc;
8367 coords[2] = id;
8368 }
8369
8370 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8371 {
8372 if (vec->parent_instr->type != nir_instr_type_alu)
8373 return;
8374 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8375 if (vec_instr->op != nir_op_vec(vec->num_components))
8376 return;
8377
8378 for (unsigned i = 0; i < vec->num_components; i++) {
8379 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8380 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8381 }
8382 }
8383
8384 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8385 {
8386 Builder bld(ctx->program, ctx->block);
8387 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8388 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8389 has_clamped_lod = false;
8390 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8391 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8392 clamped_lod = Temp();
8393 std::vector<Temp> coords;
8394 std::vector<Temp> derivs;
8395 nir_const_value *sample_index_cv = NULL;
8396 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8397 enum glsl_base_type stype;
8398 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8399
8400 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8401 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8402 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8403 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8404
8405 for (unsigned i = 0; i < instr->num_srcs; i++) {
8406 switch (instr->src[i].src_type) {
8407 case nir_tex_src_coord: {
8408 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8409 for (unsigned i = 0; i < coord.size(); i++)
8410 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8411 break;
8412 }
8413 case nir_tex_src_bias:
8414 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8415 has_bias = true;
8416 break;
8417 case nir_tex_src_lod: {
8418 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8419
8420 if (val && val->f32 <= 0.0) {
8421 level_zero = true;
8422 } else {
8423 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8424 has_lod = true;
8425 }
8426 break;
8427 }
8428 case nir_tex_src_min_lod:
8429 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8430 has_clamped_lod = true;
8431 break;
8432 case nir_tex_src_comparator:
8433 if (instr->is_shadow) {
8434 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8435 has_compare = true;
8436 }
8437 break;
8438 case nir_tex_src_offset:
8439 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8440 get_const_vec(instr->src[i].src.ssa, const_offset);
8441 has_offset = true;
8442 break;
8443 case nir_tex_src_ddx:
8444 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8445 has_ddx = true;
8446 break;
8447 case nir_tex_src_ddy:
8448 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8449 has_ddy = true;
8450 break;
8451 case nir_tex_src_ms_index:
8452 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8453 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8454 has_sample_index = true;
8455 break;
8456 case nir_tex_src_texture_offset:
8457 case nir_tex_src_sampler_offset:
8458 default:
8459 break;
8460 }
8461 }
8462
8463 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8464 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8465
8466 if (instr->op == nir_texop_texture_samples) {
8467 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8468
8469 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8470 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8471 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 */));
8472
8473 Operand default_sample = Operand(1u);
8474 if (ctx->options->robust_buffer_access) {
8475 /* Extract the second dword of the descriptor, if it's
8476 * all zero, then it's a null descriptor.
8477 */
8478 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8479 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8480 default_sample = Operand(is_non_null_descriptor);
8481 }
8482
8483 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8484 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8485 samples, default_sample, bld.scc(is_msaa));
8486 return;
8487 }
8488
8489 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8490 aco_ptr<Instruction> tmp_instr;
8491 Temp acc, pack = Temp();
8492
8493 uint32_t pack_const = 0;
8494 for (unsigned i = 0; i < offset.size(); i++) {
8495 if (!const_offset[i])
8496 continue;
8497 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8498 }
8499
8500 if (offset.type() == RegType::sgpr) {
8501 for (unsigned i = 0; i < offset.size(); i++) {
8502 if (const_offset[i])
8503 continue;
8504
8505 acc = emit_extract_vector(ctx, offset, i, s1);
8506 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8507
8508 if (i) {
8509 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8510 }
8511
8512 if (pack == Temp()) {
8513 pack = acc;
8514 } else {
8515 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8516 }
8517 }
8518
8519 if (pack_const && pack != Temp())
8520 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8521 } else {
8522 for (unsigned i = 0; i < offset.size(); i++) {
8523 if (const_offset[i])
8524 continue;
8525
8526 acc = emit_extract_vector(ctx, offset, i, v1);
8527 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8528
8529 if (i) {
8530 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8531 }
8532
8533 if (pack == Temp()) {
8534 pack = acc;
8535 } else {
8536 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8537 }
8538 }
8539
8540 if (pack_const && pack != Temp())
8541 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8542 }
8543 if (pack_const && pack == Temp())
8544 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8545 else if (pack == Temp())
8546 has_offset = false;
8547 else
8548 offset = pack;
8549 }
8550
8551 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8552 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8553
8554 /* pack derivatives */
8555 if (has_ddx || has_ddy) {
8556 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8557 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8558 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8559 derivs = {ddx, zero, ddy, zero};
8560 } else {
8561 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8562 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8563 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8564 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8565 }
8566 has_derivs = true;
8567 }
8568
8569 if (instr->coord_components > 1 &&
8570 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8571 instr->is_array &&
8572 instr->op != nir_texop_txf)
8573 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8574
8575 if (instr->coord_components > 2 &&
8576 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8577 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8578 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8579 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8580 instr->is_array &&
8581 instr->op != nir_texop_txf &&
8582 instr->op != nir_texop_txf_ms &&
8583 instr->op != nir_texop_fragment_fetch &&
8584 instr->op != nir_texop_fragment_mask_fetch)
8585 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8586
8587 if (ctx->options->chip_class == GFX9 &&
8588 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8589 instr->op != nir_texop_lod && instr->coord_components) {
8590 assert(coords.size() > 0 && coords.size() < 3);
8591
8592 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8593 Operand((uint32_t) 0) :
8594 Operand((uint32_t) 0x3f000000)));
8595 }
8596
8597 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8598
8599 if (instr->op == nir_texop_samples_identical)
8600 resource = fmask_ptr;
8601
8602 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8603 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8604 instr->op != nir_texop_txs &&
8605 instr->op != nir_texop_fragment_fetch &&
8606 instr->op != nir_texop_fragment_mask_fetch) {
8607 assert(has_sample_index);
8608 Operand op(sample_index);
8609 if (sample_index_cv)
8610 op = Operand(sample_index_cv->u32);
8611 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8612 }
8613
8614 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8615 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8616 Temp off = emit_extract_vector(ctx, offset, i, v1);
8617 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8618 }
8619 has_offset = false;
8620 }
8621
8622 /* Build tex instruction */
8623 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8624 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8625 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8626 : 0;
8627 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8628 Temp tmp_dst = dst;
8629
8630 /* gather4 selects the component by dmask and always returns vec4 */
8631 if (instr->op == nir_texop_tg4) {
8632 assert(instr->dest.ssa.num_components == 4);
8633 if (instr->is_shadow)
8634 dmask = 1;
8635 else
8636 dmask = 1 << instr->component;
8637 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8638 tmp_dst = bld.tmp(v4);
8639 } else if (instr->op == nir_texop_samples_identical) {
8640 tmp_dst = bld.tmp(v1);
8641 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8642 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8643 }
8644
8645 aco_ptr<MIMG_instruction> tex;
8646 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8647 if (!has_lod)
8648 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8649
8650 bool div_by_6 = instr->op == nir_texop_txs &&
8651 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8652 instr->is_array &&
8653 (dmask & (1 << 2));
8654 if (tmp_dst.id() == dst.id() && div_by_6)
8655 tmp_dst = bld.tmp(tmp_dst.regClass());
8656
8657 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8658 tex->operands[0] = Operand(resource);
8659 tex->operands[1] = Operand(s4); /* no sampler */
8660 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8661 if (ctx->options->chip_class == GFX9 &&
8662 instr->op == nir_texop_txs &&
8663 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8664 instr->is_array) {
8665 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8666 } else if (instr->op == nir_texop_query_levels) {
8667 tex->dmask = 1 << 3;
8668 } else {
8669 tex->dmask = dmask;
8670 }
8671 tex->da = da;
8672 tex->definitions[0] = Definition(tmp_dst);
8673 tex->dim = dim;
8674 tex->can_reorder = true;
8675 ctx->block->instructions.emplace_back(std::move(tex));
8676
8677 if (div_by_6) {
8678 /* divide 3rd value by 6 by multiplying with magic number */
8679 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8680 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8681 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8682 assert(instr->dest.ssa.num_components == 3);
8683 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8684 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8685 emit_extract_vector(ctx, tmp_dst, 0, v1),
8686 emit_extract_vector(ctx, tmp_dst, 1, v1),
8687 by_6);
8688
8689 }
8690
8691 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8692 return;
8693 }
8694
8695 Temp tg4_compare_cube_wa64 = Temp();
8696
8697 if (tg4_integer_workarounds) {
8698 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8699 tex->operands[0] = Operand(resource);
8700 tex->operands[1] = Operand(s4); /* no sampler */
8701 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8702 tex->dim = dim;
8703 tex->dmask = 0x3;
8704 tex->da = da;
8705 Temp size = bld.tmp(v2);
8706 tex->definitions[0] = Definition(size);
8707 tex->can_reorder = true;
8708 ctx->block->instructions.emplace_back(std::move(tex));
8709 emit_split_vector(ctx, size, size.size());
8710
8711 Temp half_texel[2];
8712 for (unsigned i = 0; i < 2; i++) {
8713 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8714 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8715 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8716 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8717 }
8718
8719 Temp new_coords[2] = {
8720 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8721 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8722 };
8723
8724 if (tg4_integer_cube_workaround) {
8725 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8726 Temp desc[resource.size()];
8727 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8728 Format::PSEUDO, 1, resource.size())};
8729 split->operands[0] = Operand(resource);
8730 for (unsigned i = 0; i < resource.size(); i++) {
8731 desc[i] = bld.tmp(s1);
8732 split->definitions[i] = Definition(desc[i]);
8733 }
8734 ctx->block->instructions.emplace_back(std::move(split));
8735
8736 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8737 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8738 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8739
8740 Temp nfmt;
8741 if (stype == GLSL_TYPE_UINT) {
8742 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8743 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8744 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8745 bld.scc(compare_cube_wa));
8746 } else {
8747 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8748 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8749 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8750 bld.scc(compare_cube_wa));
8751 }
8752 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8753 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8754
8755 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8756
8757 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8758 Operand((uint32_t)C_008F14_NUM_FORMAT));
8759 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8760
8761 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8762 Format::PSEUDO, resource.size(), 1)};
8763 for (unsigned i = 0; i < resource.size(); i++)
8764 vec->operands[i] = Operand(desc[i]);
8765 resource = bld.tmp(resource.regClass());
8766 vec->definitions[0] = Definition(resource);
8767 ctx->block->instructions.emplace_back(std::move(vec));
8768
8769 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8770 new_coords[0], coords[0], tg4_compare_cube_wa64);
8771 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8772 new_coords[1], coords[1], tg4_compare_cube_wa64);
8773 }
8774 coords[0] = new_coords[0];
8775 coords[1] = new_coords[1];
8776 }
8777
8778 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8779 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8780
8781 assert(coords.size() == 1);
8782 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8783 aco_opcode op;
8784 switch (last_bit) {
8785 case 1:
8786 op = aco_opcode::buffer_load_format_x; break;
8787 case 2:
8788 op = aco_opcode::buffer_load_format_xy; break;
8789 case 3:
8790 op = aco_opcode::buffer_load_format_xyz; break;
8791 case 4:
8792 op = aco_opcode::buffer_load_format_xyzw; break;
8793 default:
8794 unreachable("Tex instruction loads more than 4 components.");
8795 }
8796
8797 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8798 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8799 tmp_dst = dst;
8800 else
8801 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8802
8803 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8804 mubuf->operands[0] = Operand(resource);
8805 mubuf->operands[1] = Operand(coords[0]);
8806 mubuf->operands[2] = Operand((uint32_t) 0);
8807 mubuf->definitions[0] = Definition(tmp_dst);
8808 mubuf->idxen = true;
8809 mubuf->can_reorder = true;
8810 ctx->block->instructions.emplace_back(std::move(mubuf));
8811
8812 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8813 return;
8814 }
8815
8816 /* gather MIMG address components */
8817 std::vector<Temp> args;
8818 if (has_offset)
8819 args.emplace_back(offset);
8820 if (has_bias)
8821 args.emplace_back(bias);
8822 if (has_compare)
8823 args.emplace_back(compare);
8824 if (has_derivs)
8825 args.insert(args.end(), derivs.begin(), derivs.end());
8826
8827 args.insert(args.end(), coords.begin(), coords.end());
8828 if (has_sample_index)
8829 args.emplace_back(sample_index);
8830 if (has_lod)
8831 args.emplace_back(lod);
8832 if (has_clamped_lod)
8833 args.emplace_back(clamped_lod);
8834
8835 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8836 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8837 vec->definitions[0] = Definition(arg);
8838 for (unsigned i = 0; i < args.size(); i++)
8839 vec->operands[i] = Operand(args[i]);
8840 ctx->block->instructions.emplace_back(std::move(vec));
8841
8842
8843 if (instr->op == nir_texop_txf ||
8844 instr->op == nir_texop_txf_ms ||
8845 instr->op == nir_texop_samples_identical ||
8846 instr->op == nir_texop_fragment_fetch ||
8847 instr->op == nir_texop_fragment_mask_fetch) {
8848 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;
8849 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8850 tex->operands[0] = Operand(resource);
8851 tex->operands[1] = Operand(s4); /* no sampler */
8852 tex->operands[2] = Operand(arg);
8853 tex->dim = dim;
8854 tex->dmask = dmask;
8855 tex->unrm = true;
8856 tex->da = da;
8857 tex->definitions[0] = Definition(tmp_dst);
8858 tex->can_reorder = true;
8859 ctx->block->instructions.emplace_back(std::move(tex));
8860
8861 if (instr->op == nir_texop_samples_identical) {
8862 assert(dmask == 1 && dst.regClass() == v1);
8863 assert(dst.id() != tmp_dst.id());
8864
8865 Temp tmp = bld.tmp(bld.lm);
8866 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8867 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8868
8869 } else {
8870 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8871 }
8872 return;
8873 }
8874
8875 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8876 aco_opcode opcode = aco_opcode::image_sample;
8877 if (has_offset) { /* image_sample_*_o */
8878 if (has_clamped_lod) {
8879 if (has_compare) {
8880 opcode = aco_opcode::image_sample_c_cl_o;
8881 if (has_derivs)
8882 opcode = aco_opcode::image_sample_c_d_cl_o;
8883 if (has_bias)
8884 opcode = aco_opcode::image_sample_c_b_cl_o;
8885 } else {
8886 opcode = aco_opcode::image_sample_cl_o;
8887 if (has_derivs)
8888 opcode = aco_opcode::image_sample_d_cl_o;
8889 if (has_bias)
8890 opcode = aco_opcode::image_sample_b_cl_o;
8891 }
8892 } else if (has_compare) {
8893 opcode = aco_opcode::image_sample_c_o;
8894 if (has_derivs)
8895 opcode = aco_opcode::image_sample_c_d_o;
8896 if (has_bias)
8897 opcode = aco_opcode::image_sample_c_b_o;
8898 if (level_zero)
8899 opcode = aco_opcode::image_sample_c_lz_o;
8900 if (has_lod)
8901 opcode = aco_opcode::image_sample_c_l_o;
8902 } else {
8903 opcode = aco_opcode::image_sample_o;
8904 if (has_derivs)
8905 opcode = aco_opcode::image_sample_d_o;
8906 if (has_bias)
8907 opcode = aco_opcode::image_sample_b_o;
8908 if (level_zero)
8909 opcode = aco_opcode::image_sample_lz_o;
8910 if (has_lod)
8911 opcode = aco_opcode::image_sample_l_o;
8912 }
8913 } else if (has_clamped_lod) { /* image_sample_*_cl */
8914 if (has_compare) {
8915 opcode = aco_opcode::image_sample_c_cl;
8916 if (has_derivs)
8917 opcode = aco_opcode::image_sample_c_d_cl;
8918 if (has_bias)
8919 opcode = aco_opcode::image_sample_c_b_cl;
8920 } else {
8921 opcode = aco_opcode::image_sample_cl;
8922 if (has_derivs)
8923 opcode = aco_opcode::image_sample_d_cl;
8924 if (has_bias)
8925 opcode = aco_opcode::image_sample_b_cl;
8926 }
8927 } else { /* no offset */
8928 if (has_compare) {
8929 opcode = aco_opcode::image_sample_c;
8930 if (has_derivs)
8931 opcode = aco_opcode::image_sample_c_d;
8932 if (has_bias)
8933 opcode = aco_opcode::image_sample_c_b;
8934 if (level_zero)
8935 opcode = aco_opcode::image_sample_c_lz;
8936 if (has_lod)
8937 opcode = aco_opcode::image_sample_c_l;
8938 } else {
8939 opcode = aco_opcode::image_sample;
8940 if (has_derivs)
8941 opcode = aco_opcode::image_sample_d;
8942 if (has_bias)
8943 opcode = aco_opcode::image_sample_b;
8944 if (level_zero)
8945 opcode = aco_opcode::image_sample_lz;
8946 if (has_lod)
8947 opcode = aco_opcode::image_sample_l;
8948 }
8949 }
8950
8951 if (instr->op == nir_texop_tg4) {
8952 if (has_offset) { /* image_gather4_*_o */
8953 if (has_compare) {
8954 opcode = aco_opcode::image_gather4_c_lz_o;
8955 if (has_lod)
8956 opcode = aco_opcode::image_gather4_c_l_o;
8957 if (has_bias)
8958 opcode = aco_opcode::image_gather4_c_b_o;
8959 } else {
8960 opcode = aco_opcode::image_gather4_lz_o;
8961 if (has_lod)
8962 opcode = aco_opcode::image_gather4_l_o;
8963 if (has_bias)
8964 opcode = aco_opcode::image_gather4_b_o;
8965 }
8966 } else {
8967 if (has_compare) {
8968 opcode = aco_opcode::image_gather4_c_lz;
8969 if (has_lod)
8970 opcode = aco_opcode::image_gather4_c_l;
8971 if (has_bias)
8972 opcode = aco_opcode::image_gather4_c_b;
8973 } else {
8974 opcode = aco_opcode::image_gather4_lz;
8975 if (has_lod)
8976 opcode = aco_opcode::image_gather4_l;
8977 if (has_bias)
8978 opcode = aco_opcode::image_gather4_b;
8979 }
8980 }
8981 } else if (instr->op == nir_texop_lod) {
8982 opcode = aco_opcode::image_get_lod;
8983 }
8984
8985 /* we don't need the bias, sample index, compare value or offset to be
8986 * computed in WQM but if the p_create_vector copies the coordinates, then it
8987 * needs to be in WQM */
8988 if (ctx->stage == fragment_fs &&
8989 !has_derivs && !has_lod && !level_zero &&
8990 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8991 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8992 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8993
8994 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8995 tex->operands[0] = Operand(resource);
8996 tex->operands[1] = Operand(sampler);
8997 tex->operands[2] = Operand(arg);
8998 tex->dim = dim;
8999 tex->dmask = dmask;
9000 tex->da = da;
9001 tex->definitions[0] = Definition(tmp_dst);
9002 tex->can_reorder = true;
9003 ctx->block->instructions.emplace_back(std::move(tex));
9004
9005 if (tg4_integer_cube_workaround) {
9006 assert(tmp_dst.id() != dst.id());
9007 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
9008
9009 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
9010 Temp val[4];
9011 for (unsigned i = 0; i < dst.size(); i++) {
9012 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
9013 Temp cvt_val;
9014 if (stype == GLSL_TYPE_UINT)
9015 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
9016 else
9017 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
9018 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
9019 }
9020 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
9021 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
9022 val[0], val[1], val[2], val[3]);
9023 }
9024 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
9025 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
9026
9027 }
9028
9029
9030 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa, RegClass rc, bool logical)
9031 {
9032 Temp tmp = get_ssa_temp(ctx, ssa);
9033 if (ssa->parent_instr->type == nir_instr_type_ssa_undef) {
9034 return Operand(rc);
9035 } else if (logical && ssa->bit_size == 1 && ssa->parent_instr->type == nir_instr_type_load_const) {
9036 if (ctx->program->wave_size == 64)
9037 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT64_MAX : 0u);
9038 else
9039 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT32_MAX : 0u);
9040 } else {
9041 return Operand(tmp);
9042 }
9043 }
9044
9045 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
9046 {
9047 aco_ptr<Pseudo_instruction> phi;
9048 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
9049 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
9050
9051 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
9052 logical |= ctx->block->kind & block_kind_merge;
9053 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
9054
9055 /* we want a sorted list of sources, since the predecessor list is also sorted */
9056 std::map<unsigned, nir_ssa_def*> phi_src;
9057 nir_foreach_phi_src(src, instr)
9058 phi_src[src->pred->index] = src->src.ssa;
9059
9060 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
9061 unsigned num_operands = 0;
9062 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
9063 unsigned num_defined = 0;
9064 unsigned cur_pred_idx = 0;
9065 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
9066 if (cur_pred_idx < preds.size()) {
9067 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
9068 unsigned block = ctx->cf_info.nir_to_aco[src.first];
9069 unsigned skipped = 0;
9070 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
9071 skipped++;
9072 if (cur_pred_idx + skipped < preds.size()) {
9073 for (unsigned i = 0; i < skipped; i++)
9074 operands[num_operands++] = Operand(dst.regClass());
9075 cur_pred_idx += skipped;
9076 } else {
9077 continue;
9078 }
9079 }
9080 /* Handle missing predecessors at the end. This shouldn't happen with loop
9081 * headers and we can't ignore these sources for loop header phis. */
9082 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
9083 continue;
9084 cur_pred_idx++;
9085 Operand op = get_phi_operand(ctx, src.second, dst.regClass(), logical);
9086 operands[num_operands++] = op;
9087 num_defined += !op.isUndefined();
9088 }
9089 /* handle block_kind_continue_or_break at loop exit blocks */
9090 while (cur_pred_idx++ < preds.size())
9091 operands[num_operands++] = Operand(dst.regClass());
9092
9093 /* If the loop ends with a break, still add a linear continue edge in case
9094 * that break is divergent or continue_or_break is used. We'll either remove
9095 * this operand later in visit_loop() if it's not necessary or replace the
9096 * undef with something correct. */
9097 if (!logical && ctx->block->kind & block_kind_loop_header) {
9098 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
9099 nir_block *last = nir_loop_last_block(loop);
9100 if (last->successors[0] != instr->instr.block)
9101 operands[num_operands++] = Operand(RegClass());
9102 }
9103
9104 if (num_defined == 0) {
9105 Builder bld(ctx->program, ctx->block);
9106 if (dst.regClass() == s1) {
9107 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
9108 } else if (dst.regClass() == v1) {
9109 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
9110 } else {
9111 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9112 for (unsigned i = 0; i < dst.size(); i++)
9113 vec->operands[i] = Operand(0u);
9114 vec->definitions[0] = Definition(dst);
9115 ctx->block->instructions.emplace_back(std::move(vec));
9116 }
9117 return;
9118 }
9119
9120 /* we can use a linear phi in some cases if one src is undef */
9121 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9122 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9123
9124 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9125 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9126 assert(invert->kind & block_kind_invert);
9127
9128 unsigned then_block = invert->linear_preds[0];
9129
9130 Block* insert_block = NULL;
9131 for (unsigned i = 0; i < num_operands; i++) {
9132 Operand op = operands[i];
9133 if (op.isUndefined())
9134 continue;
9135 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9136 phi->operands[0] = op;
9137 break;
9138 }
9139 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9140 phi->operands[1] = Operand(dst.regClass());
9141 phi->definitions[0] = Definition(dst);
9142 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9143 return;
9144 }
9145
9146 /* try to scalarize vector phis */
9147 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9148 // TODO: scalarize linear phis on divergent ifs
9149 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9150 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9151 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9152 Operand src = operands[i];
9153 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9154 can_scalarize = false;
9155 }
9156 if (can_scalarize) {
9157 unsigned num_components = instr->dest.ssa.num_components;
9158 assert(dst.size() % num_components == 0);
9159 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9160
9161 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9162 for (unsigned k = 0; k < num_components; k++) {
9163 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9164 for (unsigned i = 0; i < num_operands; i++) {
9165 Operand src = operands[i];
9166 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9167 }
9168 Temp phi_dst = {ctx->program->allocateId(), rc};
9169 phi->definitions[0] = Definition(phi_dst);
9170 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9171 new_vec[k] = phi_dst;
9172 vec->operands[k] = Operand(phi_dst);
9173 }
9174 vec->definitions[0] = Definition(dst);
9175 ctx->block->instructions.emplace_back(std::move(vec));
9176 ctx->allocated_vec.emplace(dst.id(), new_vec);
9177 return;
9178 }
9179 }
9180
9181 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9182 for (unsigned i = 0; i < num_operands; i++)
9183 phi->operands[i] = operands[i];
9184 phi->definitions[0] = Definition(dst);
9185 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9186 }
9187
9188
9189 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9190 {
9191 Temp dst = get_ssa_temp(ctx, &instr->def);
9192
9193 assert(dst.type() == RegType::sgpr);
9194
9195 if (dst.size() == 1) {
9196 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9197 } else {
9198 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9199 for (unsigned i = 0; i < dst.size(); i++)
9200 vec->operands[i] = Operand(0u);
9201 vec->definitions[0] = Definition(dst);
9202 ctx->block->instructions.emplace_back(std::move(vec));
9203 }
9204 }
9205
9206 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9207 {
9208 Builder bld(ctx->program, ctx->block);
9209 Block *logical_target;
9210 append_logical_end(ctx->block);
9211 unsigned idx = ctx->block->index;
9212
9213 switch (instr->type) {
9214 case nir_jump_break:
9215 logical_target = ctx->cf_info.parent_loop.exit;
9216 add_logical_edge(idx, logical_target);
9217 ctx->block->kind |= block_kind_break;
9218
9219 if (!ctx->cf_info.parent_if.is_divergent &&
9220 !ctx->cf_info.parent_loop.has_divergent_continue) {
9221 /* uniform break - directly jump out of the loop */
9222 ctx->block->kind |= block_kind_uniform;
9223 ctx->cf_info.has_branch = true;
9224 bld.branch(aco_opcode::p_branch);
9225 add_linear_edge(idx, logical_target);
9226 return;
9227 }
9228 ctx->cf_info.parent_loop.has_divergent_branch = true;
9229 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9230 break;
9231 case nir_jump_continue:
9232 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9233 add_logical_edge(idx, logical_target);
9234 ctx->block->kind |= block_kind_continue;
9235
9236 if (ctx->cf_info.parent_if.is_divergent) {
9237 /* for potential uniform breaks after this continue,
9238 we must ensure that they are handled correctly */
9239 ctx->cf_info.parent_loop.has_divergent_continue = true;
9240 ctx->cf_info.parent_loop.has_divergent_branch = true;
9241 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9242 } else {
9243 /* uniform continue - directly jump to the loop header */
9244 ctx->block->kind |= block_kind_uniform;
9245 ctx->cf_info.has_branch = true;
9246 bld.branch(aco_opcode::p_branch);
9247 add_linear_edge(idx, logical_target);
9248 return;
9249 }
9250 break;
9251 default:
9252 fprintf(stderr, "Unknown NIR jump instr: ");
9253 nir_print_instr(&instr->instr, stderr);
9254 fprintf(stderr, "\n");
9255 abort();
9256 }
9257
9258 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9259 ctx->cf_info.exec_potentially_empty_break = true;
9260 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9261 }
9262
9263 /* remove critical edges from linear CFG */
9264 bld.branch(aco_opcode::p_branch);
9265 Block* break_block = ctx->program->create_and_insert_block();
9266 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9267 break_block->kind |= block_kind_uniform;
9268 add_linear_edge(idx, break_block);
9269 /* the loop_header pointer might be invalidated by this point */
9270 if (instr->type == nir_jump_continue)
9271 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9272 add_linear_edge(break_block->index, logical_target);
9273 bld.reset(break_block);
9274 bld.branch(aco_opcode::p_branch);
9275
9276 Block* continue_block = ctx->program->create_and_insert_block();
9277 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9278 add_linear_edge(idx, continue_block);
9279 append_logical_start(continue_block);
9280 ctx->block = continue_block;
9281 return;
9282 }
9283
9284 void visit_block(isel_context *ctx, nir_block *block)
9285 {
9286 nir_foreach_instr(instr, block) {
9287 switch (instr->type) {
9288 case nir_instr_type_alu:
9289 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9290 break;
9291 case nir_instr_type_load_const:
9292 visit_load_const(ctx, nir_instr_as_load_const(instr));
9293 break;
9294 case nir_instr_type_intrinsic:
9295 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9296 break;
9297 case nir_instr_type_tex:
9298 visit_tex(ctx, nir_instr_as_tex(instr));
9299 break;
9300 case nir_instr_type_phi:
9301 visit_phi(ctx, nir_instr_as_phi(instr));
9302 break;
9303 case nir_instr_type_ssa_undef:
9304 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9305 break;
9306 case nir_instr_type_deref:
9307 break;
9308 case nir_instr_type_jump:
9309 visit_jump(ctx, nir_instr_as_jump(instr));
9310 break;
9311 default:
9312 fprintf(stderr, "Unknown NIR instr type: ");
9313 nir_print_instr(instr, stderr);
9314 fprintf(stderr, "\n");
9315 //abort();
9316 }
9317 }
9318
9319 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9320 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9321 }
9322
9323
9324
9325 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9326 aco_ptr<Instruction>& header_phi, Operand *vals)
9327 {
9328 vals[0] = Operand(header_phi->definitions[0].getTemp());
9329 RegClass rc = vals[0].regClass();
9330
9331 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9332
9333 unsigned next_pred = 1;
9334
9335 for (unsigned idx = first + 1; idx <= last; idx++) {
9336 Block& block = ctx->program->blocks[idx];
9337 if (block.loop_nest_depth != loop_nest_depth) {
9338 vals[idx - first] = vals[idx - 1 - first];
9339 continue;
9340 }
9341
9342 if (block.kind & block_kind_continue) {
9343 vals[idx - first] = header_phi->operands[next_pred];
9344 next_pred++;
9345 continue;
9346 }
9347
9348 bool all_same = true;
9349 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9350 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9351
9352 Operand val;
9353 if (all_same) {
9354 val = vals[block.linear_preds[0] - first];
9355 } else {
9356 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9357 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9358 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9359 phi->operands[i] = vals[block.linear_preds[i] - first];
9360 val = Operand(Temp(ctx->program->allocateId(), rc));
9361 phi->definitions[0] = Definition(val.getTemp());
9362 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9363 }
9364 vals[idx - first] = val;
9365 }
9366
9367 return vals[last - first];
9368 }
9369
9370 static void visit_loop(isel_context *ctx, nir_loop *loop)
9371 {
9372 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9373 append_logical_end(ctx->block);
9374 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9375 Builder bld(ctx->program, ctx->block);
9376 bld.branch(aco_opcode::p_branch);
9377 unsigned loop_preheader_idx = ctx->block->index;
9378
9379 Block loop_exit = Block();
9380 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9381 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9382
9383 Block* loop_header = ctx->program->create_and_insert_block();
9384 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9385 loop_header->kind |= block_kind_loop_header;
9386 add_edge(loop_preheader_idx, loop_header);
9387 ctx->block = loop_header;
9388
9389 /* emit loop body */
9390 unsigned loop_header_idx = loop_header->index;
9391 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9392 append_logical_start(ctx->block);
9393 bool unreachable = visit_cf_list(ctx, &loop->body);
9394
9395 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9396 if (!ctx->cf_info.has_branch) {
9397 append_logical_end(ctx->block);
9398 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9399 /* Discards can result in code running with an empty exec mask.
9400 * This would result in divergent breaks not ever being taken. As a
9401 * workaround, break the loop when the loop mask is empty instead of
9402 * always continuing. */
9403 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9404 unsigned block_idx = ctx->block->index;
9405
9406 /* create helper blocks to avoid critical edges */
9407 Block *break_block = ctx->program->create_and_insert_block();
9408 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9409 break_block->kind = block_kind_uniform;
9410 bld.reset(break_block);
9411 bld.branch(aco_opcode::p_branch);
9412 add_linear_edge(block_idx, break_block);
9413 add_linear_edge(break_block->index, &loop_exit);
9414
9415 Block *continue_block = ctx->program->create_and_insert_block();
9416 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9417 continue_block->kind = block_kind_uniform;
9418 bld.reset(continue_block);
9419 bld.branch(aco_opcode::p_branch);
9420 add_linear_edge(block_idx, continue_block);
9421 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9422
9423 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9424 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9425 ctx->block = &ctx->program->blocks[block_idx];
9426 } else {
9427 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9428 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9429 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9430 else
9431 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9432 }
9433
9434 bld.reset(ctx->block);
9435 bld.branch(aco_opcode::p_branch);
9436 }
9437
9438 /* Fixup phis in loop header from unreachable blocks.
9439 * has_branch/has_divergent_branch also indicates if the loop ends with a
9440 * break/continue instruction, but we don't emit those if unreachable=true */
9441 if (unreachable) {
9442 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9443 bool linear = ctx->cf_info.has_branch;
9444 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9445 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9446 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9447 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9448 /* the last operand should be the one that needs to be removed */
9449 instr->operands.pop_back();
9450 } else if (!is_phi(instr)) {
9451 break;
9452 }
9453 }
9454 }
9455
9456 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9457 * and the previous one shouldn't both happen at once because a break in the
9458 * merge block would get CSE'd */
9459 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9460 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9461 Operand vals[num_vals];
9462 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9463 if (instr->opcode == aco_opcode::p_linear_phi) {
9464 if (ctx->cf_info.has_branch)
9465 instr->operands.pop_back();
9466 else
9467 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9468 } else if (!is_phi(instr)) {
9469 break;
9470 }
9471 }
9472 }
9473
9474 ctx->cf_info.has_branch = false;
9475
9476 // TODO: if the loop has not a single exit, we must add one °°
9477 /* emit loop successor block */
9478 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9479 append_logical_start(ctx->block);
9480
9481 #if 0
9482 // TODO: check if it is beneficial to not branch on continues
9483 /* trim linear phis in loop header */
9484 for (auto&& instr : loop_entry->instructions) {
9485 if (instr->opcode == aco_opcode::p_linear_phi) {
9486 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9487 new_phi->definitions[0] = instr->definitions[0];
9488 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9489 new_phi->operands[i] = instr->operands[i];
9490 /* check that the remaining operands are all the same */
9491 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9492 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9493 instr.swap(new_phi);
9494 } else if (instr->opcode == aco_opcode::p_phi) {
9495 continue;
9496 } else {
9497 break;
9498 }
9499 }
9500 #endif
9501 }
9502
9503 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9504 {
9505 ic->cond = cond;
9506
9507 append_logical_end(ctx->block);
9508 ctx->block->kind |= block_kind_branch;
9509
9510 /* branch to linear then block */
9511 assert(cond.regClass() == ctx->program->lane_mask);
9512 aco_ptr<Pseudo_branch_instruction> branch;
9513 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9514 branch->operands[0] = Operand(cond);
9515 ctx->block->instructions.push_back(std::move(branch));
9516
9517 ic->BB_if_idx = ctx->block->index;
9518 ic->BB_invert = Block();
9519 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9520 /* Invert blocks are intentionally not marked as top level because they
9521 * are not part of the logical cfg. */
9522 ic->BB_invert.kind |= block_kind_invert;
9523 ic->BB_endif = Block();
9524 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9525 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9526
9527 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9528 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9529 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9530 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9531 ctx->cf_info.parent_if.is_divergent = true;
9532
9533 /* divergent branches use cbranch_execz */
9534 ctx->cf_info.exec_potentially_empty_discard = false;
9535 ctx->cf_info.exec_potentially_empty_break = false;
9536 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9537
9538 /** emit logical then block */
9539 Block* BB_then_logical = ctx->program->create_and_insert_block();
9540 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9541 add_edge(ic->BB_if_idx, BB_then_logical);
9542 ctx->block = BB_then_logical;
9543 append_logical_start(BB_then_logical);
9544 }
9545
9546 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9547 {
9548 Block *BB_then_logical = ctx->block;
9549 append_logical_end(BB_then_logical);
9550 /* branch from logical then block to invert block */
9551 aco_ptr<Pseudo_branch_instruction> branch;
9552 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9553 BB_then_logical->instructions.emplace_back(std::move(branch));
9554 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9555 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9556 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9557 BB_then_logical->kind |= block_kind_uniform;
9558 assert(!ctx->cf_info.has_branch);
9559 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9560 ctx->cf_info.parent_loop.has_divergent_branch = false;
9561
9562 /** emit linear then block */
9563 Block* BB_then_linear = ctx->program->create_and_insert_block();
9564 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9565 BB_then_linear->kind |= block_kind_uniform;
9566 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9567 /* branch from linear then block to invert block */
9568 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9569 BB_then_linear->instructions.emplace_back(std::move(branch));
9570 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9571
9572 /** emit invert merge block */
9573 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9574 ic->invert_idx = ctx->block->index;
9575
9576 /* branch to linear else block (skip else) */
9577 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9578 branch->operands[0] = Operand(ic->cond);
9579 ctx->block->instructions.push_back(std::move(branch));
9580
9581 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9582 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9583 ic->exec_potentially_empty_break_depth_old =
9584 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9585 /* divergent branches use cbranch_execz */
9586 ctx->cf_info.exec_potentially_empty_discard = false;
9587 ctx->cf_info.exec_potentially_empty_break = false;
9588 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9589
9590 /** emit logical else block */
9591 Block* BB_else_logical = ctx->program->create_and_insert_block();
9592 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9593 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9594 add_linear_edge(ic->invert_idx, BB_else_logical);
9595 ctx->block = BB_else_logical;
9596 append_logical_start(BB_else_logical);
9597 }
9598
9599 static void end_divergent_if(isel_context *ctx, if_context *ic)
9600 {
9601 Block *BB_else_logical = ctx->block;
9602 append_logical_end(BB_else_logical);
9603
9604 /* branch from logical else block to endif block */
9605 aco_ptr<Pseudo_branch_instruction> branch;
9606 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9607 BB_else_logical->instructions.emplace_back(std::move(branch));
9608 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9609 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9610 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9611 BB_else_logical->kind |= block_kind_uniform;
9612
9613 assert(!ctx->cf_info.has_branch);
9614 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9615
9616
9617 /** emit linear else block */
9618 Block* BB_else_linear = ctx->program->create_and_insert_block();
9619 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9620 BB_else_linear->kind |= block_kind_uniform;
9621 add_linear_edge(ic->invert_idx, BB_else_linear);
9622
9623 /* branch from linear else block to endif block */
9624 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9625 BB_else_linear->instructions.emplace_back(std::move(branch));
9626 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9627
9628
9629 /** emit endif merge block */
9630 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9631 append_logical_start(ctx->block);
9632
9633
9634 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9635 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9636 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9637 ctx->cf_info.exec_potentially_empty_break_depth =
9638 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9639 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9640 !ctx->cf_info.parent_if.is_divergent) {
9641 ctx->cf_info.exec_potentially_empty_break = false;
9642 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9643 }
9644 /* uniform control flow never has an empty exec-mask */
9645 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9646 ctx->cf_info.exec_potentially_empty_discard = false;
9647 ctx->cf_info.exec_potentially_empty_break = false;
9648 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9649 }
9650 }
9651
9652 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9653 {
9654 assert(cond.regClass() == s1);
9655
9656 append_logical_end(ctx->block);
9657 ctx->block->kind |= block_kind_uniform;
9658
9659 aco_ptr<Pseudo_branch_instruction> branch;
9660 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9661 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9662 branch->operands[0] = Operand(cond);
9663 branch->operands[0].setFixed(scc);
9664 ctx->block->instructions.emplace_back(std::move(branch));
9665
9666 ic->BB_if_idx = ctx->block->index;
9667 ic->BB_endif = Block();
9668 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9669 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9670
9671 ctx->cf_info.has_branch = false;
9672 ctx->cf_info.parent_loop.has_divergent_branch = false;
9673
9674 /** emit then block */
9675 Block* BB_then = ctx->program->create_and_insert_block();
9676 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9677 add_edge(ic->BB_if_idx, BB_then);
9678 append_logical_start(BB_then);
9679 ctx->block = BB_then;
9680 }
9681
9682 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9683 {
9684 Block *BB_then = ctx->block;
9685
9686 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9687 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9688
9689 if (!ic->uniform_has_then_branch) {
9690 append_logical_end(BB_then);
9691 /* branch from then block to endif block */
9692 aco_ptr<Pseudo_branch_instruction> branch;
9693 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9694 BB_then->instructions.emplace_back(std::move(branch));
9695 add_linear_edge(BB_then->index, &ic->BB_endif);
9696 if (!ic->then_branch_divergent)
9697 add_logical_edge(BB_then->index, &ic->BB_endif);
9698 BB_then->kind |= block_kind_uniform;
9699 }
9700
9701 ctx->cf_info.has_branch = false;
9702 ctx->cf_info.parent_loop.has_divergent_branch = false;
9703
9704 /** emit else block */
9705 Block* BB_else = ctx->program->create_and_insert_block();
9706 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9707 add_edge(ic->BB_if_idx, BB_else);
9708 append_logical_start(BB_else);
9709 ctx->block = BB_else;
9710 }
9711
9712 static void end_uniform_if(isel_context *ctx, if_context *ic)
9713 {
9714 Block *BB_else = ctx->block;
9715
9716 if (!ctx->cf_info.has_branch) {
9717 append_logical_end(BB_else);
9718 /* branch from then block to endif block */
9719 aco_ptr<Pseudo_branch_instruction> branch;
9720 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9721 BB_else->instructions.emplace_back(std::move(branch));
9722 add_linear_edge(BB_else->index, &ic->BB_endif);
9723 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9724 add_logical_edge(BB_else->index, &ic->BB_endif);
9725 BB_else->kind |= block_kind_uniform;
9726 }
9727
9728 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9729 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9730
9731 /** emit endif merge block */
9732 if (!ctx->cf_info.has_branch) {
9733 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9734 append_logical_start(ctx->block);
9735 }
9736 }
9737
9738 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9739 {
9740 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9741 Builder bld(ctx->program, ctx->block);
9742 aco_ptr<Pseudo_branch_instruction> branch;
9743 if_context ic;
9744
9745 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9746 /**
9747 * Uniform conditionals are represented in the following way*) :
9748 *
9749 * The linear and logical CFG:
9750 * BB_IF
9751 * / \
9752 * BB_THEN (logical) BB_ELSE (logical)
9753 * \ /
9754 * BB_ENDIF
9755 *
9756 * *) Exceptions may be due to break and continue statements within loops
9757 * If a break/continue happens within uniform control flow, it branches
9758 * to the loop exit/entry block. Otherwise, it branches to the next
9759 * merge block.
9760 **/
9761
9762 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9763 assert(cond.regClass() == ctx->program->lane_mask);
9764 cond = bool_to_scalar_condition(ctx, cond);
9765
9766 begin_uniform_if_then(ctx, &ic, cond);
9767 visit_cf_list(ctx, &if_stmt->then_list);
9768
9769 begin_uniform_if_else(ctx, &ic);
9770 visit_cf_list(ctx, &if_stmt->else_list);
9771
9772 end_uniform_if(ctx, &ic);
9773 } else { /* non-uniform condition */
9774 /**
9775 * To maintain a logical and linear CFG without critical edges,
9776 * non-uniform conditionals are represented in the following way*) :
9777 *
9778 * The linear CFG:
9779 * BB_IF
9780 * / \
9781 * BB_THEN (logical) BB_THEN (linear)
9782 * \ /
9783 * BB_INVERT (linear)
9784 * / \
9785 * BB_ELSE (logical) BB_ELSE (linear)
9786 * \ /
9787 * BB_ENDIF
9788 *
9789 * The logical CFG:
9790 * BB_IF
9791 * / \
9792 * BB_THEN (logical) BB_ELSE (logical)
9793 * \ /
9794 * BB_ENDIF
9795 *
9796 * *) Exceptions may be due to break and continue statements within loops
9797 **/
9798
9799 begin_divergent_if_then(ctx, &ic, cond);
9800 visit_cf_list(ctx, &if_stmt->then_list);
9801
9802 begin_divergent_if_else(ctx, &ic);
9803 visit_cf_list(ctx, &if_stmt->else_list);
9804
9805 end_divergent_if(ctx, &ic);
9806 }
9807
9808 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9809 }
9810
9811 static bool visit_cf_list(isel_context *ctx,
9812 struct exec_list *list)
9813 {
9814 foreach_list_typed(nir_cf_node, node, node, list) {
9815 switch (node->type) {
9816 case nir_cf_node_block:
9817 visit_block(ctx, nir_cf_node_as_block(node));
9818 break;
9819 case nir_cf_node_if:
9820 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9821 return true;
9822 break;
9823 case nir_cf_node_loop:
9824 visit_loop(ctx, nir_cf_node_as_loop(node));
9825 break;
9826 default:
9827 unreachable("unimplemented cf list type");
9828 }
9829 }
9830 return false;
9831 }
9832
9833 static void create_null_export(isel_context *ctx)
9834 {
9835 /* Some shader stages always need to have exports.
9836 * So when there is none, we need to add a null export.
9837 */
9838
9839 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9840 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9841 Builder bld(ctx->program, ctx->block);
9842 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9843 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9844 }
9845
9846 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9847 {
9848 assert(ctx->stage == vertex_vs ||
9849 ctx->stage == tess_eval_vs ||
9850 ctx->stage == gs_copy_vs ||
9851 ctx->stage == ngg_vertex_gs ||
9852 ctx->stage == ngg_tess_eval_gs);
9853
9854 int offset = (ctx->stage & sw_tes)
9855 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9856 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9857 uint64_t mask = ctx->outputs.mask[slot];
9858 if (!is_pos && !mask)
9859 return false;
9860 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9861 return false;
9862 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9863 exp->enabled_mask = mask;
9864 for (unsigned i = 0; i < 4; ++i) {
9865 if (mask & (1 << i))
9866 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9867 else
9868 exp->operands[i] = Operand(v1);
9869 }
9870 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9871 * Setting valid_mask=1 prevents it and has no other effect.
9872 */
9873 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9874 exp->done = false;
9875 exp->compressed = false;
9876 if (is_pos)
9877 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9878 else
9879 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9880 ctx->block->instructions.emplace_back(std::move(exp));
9881
9882 return true;
9883 }
9884
9885 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9886 {
9887 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9888 exp->enabled_mask = 0;
9889 for (unsigned i = 0; i < 4; ++i)
9890 exp->operands[i] = Operand(v1);
9891 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9892 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9893 exp->enabled_mask |= 0x1;
9894 }
9895 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9896 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9897 exp->enabled_mask |= 0x4;
9898 }
9899 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9900 if (ctx->options->chip_class < GFX9) {
9901 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9902 exp->enabled_mask |= 0x8;
9903 } else {
9904 Builder bld(ctx->program, ctx->block);
9905
9906 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9907 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9908 if (exp->operands[2].isTemp())
9909 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9910
9911 exp->operands[2] = Operand(out);
9912 exp->enabled_mask |= 0x4;
9913 }
9914 }
9915 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9916 exp->done = false;
9917 exp->compressed = false;
9918 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9919 ctx->block->instructions.emplace_back(std::move(exp));
9920 }
9921
9922 static void create_export_phis(isel_context *ctx)
9923 {
9924 /* Used when exports are needed, but the output temps are defined in a preceding block.
9925 * This function will set up phis in order to access the outputs in the next block.
9926 */
9927
9928 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9929 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9930 ctx->block->instructions.pop_back();
9931
9932 Builder bld(ctx->program, ctx->block);
9933
9934 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9935 uint64_t mask = ctx->outputs.mask[slot];
9936 for (unsigned i = 0; i < 4; ++i) {
9937 if (!(mask & (1 << i)))
9938 continue;
9939
9940 Temp old = ctx->outputs.temps[slot * 4 + i];
9941 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9942 ctx->outputs.temps[slot * 4 + i] = phi;
9943 }
9944 }
9945
9946 bld.insert(std::move(logical_start));
9947 }
9948
9949 static void create_vs_exports(isel_context *ctx)
9950 {
9951 assert(ctx->stage == vertex_vs ||
9952 ctx->stage == tess_eval_vs ||
9953 ctx->stage == gs_copy_vs ||
9954 ctx->stage == ngg_vertex_gs ||
9955 ctx->stage == ngg_tess_eval_gs);
9956
9957 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9958 ? &ctx->program->info->tes.outinfo
9959 : &ctx->program->info->vs.outinfo;
9960
9961 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9962 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9963 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9964 }
9965
9966 if (ctx->options->key.has_multiview_view_index) {
9967 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9968 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9969 }
9970
9971 /* the order these position exports are created is important */
9972 int next_pos = 0;
9973 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9974 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9975 export_vs_psiz_layer_viewport(ctx, &next_pos);
9976 exported_pos = true;
9977 }
9978 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9979 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9980 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9981 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9982
9983 if (ctx->export_clip_dists) {
9984 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9985 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9986 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9987 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9988 }
9989
9990 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9991 if (i < VARYING_SLOT_VAR0 &&
9992 i != VARYING_SLOT_LAYER &&
9993 i != VARYING_SLOT_PRIMITIVE_ID &&
9994 i != VARYING_SLOT_VIEWPORT)
9995 continue;
9996
9997 export_vs_varying(ctx, i, false, NULL);
9998 }
9999
10000 if (!exported_pos)
10001 create_null_export(ctx);
10002 }
10003
10004 static bool export_fs_mrt_z(isel_context *ctx)
10005 {
10006 Builder bld(ctx->program, ctx->block);
10007 unsigned enabled_channels = 0;
10008 bool compr = false;
10009 Operand values[4];
10010
10011 for (unsigned i = 0; i < 4; ++i) {
10012 values[i] = Operand(v1);
10013 }
10014
10015 /* Both stencil and sample mask only need 16-bits. */
10016 if (!ctx->program->info->ps.writes_z &&
10017 (ctx->program->info->ps.writes_stencil ||
10018 ctx->program->info->ps.writes_sample_mask)) {
10019 compr = true; /* COMPR flag */
10020
10021 if (ctx->program->info->ps.writes_stencil) {
10022 /* Stencil should be in X[23:16]. */
10023 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10024 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
10025 enabled_channels |= 0x3;
10026 }
10027
10028 if (ctx->program->info->ps.writes_sample_mask) {
10029 /* SampleMask should be in Y[15:0]. */
10030 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10031 enabled_channels |= 0xc;
10032 }
10033 } else {
10034 if (ctx->program->info->ps.writes_z) {
10035 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
10036 enabled_channels |= 0x1;
10037 }
10038
10039 if (ctx->program->info->ps.writes_stencil) {
10040 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10041 enabled_channels |= 0x2;
10042 }
10043
10044 if (ctx->program->info->ps.writes_sample_mask) {
10045 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10046 enabled_channels |= 0x4;
10047 }
10048 }
10049
10050 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
10051 * writemask component.
10052 */
10053 if (ctx->options->chip_class == GFX6 &&
10054 ctx->options->family != CHIP_OLAND &&
10055 ctx->options->family != CHIP_HAINAN) {
10056 enabled_channels |= 0x1;
10057 }
10058
10059 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10060 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
10061
10062 return true;
10063 }
10064
10065 static bool export_fs_mrt_color(isel_context *ctx, int slot)
10066 {
10067 Builder bld(ctx->program, ctx->block);
10068 unsigned write_mask = ctx->outputs.mask[slot];
10069 Operand values[4];
10070
10071 for (unsigned i = 0; i < 4; ++i) {
10072 if (write_mask & (1 << i)) {
10073 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
10074 } else {
10075 values[i] = Operand(v1);
10076 }
10077 }
10078
10079 unsigned target, col_format;
10080 unsigned enabled_channels = 0;
10081 aco_opcode compr_op = (aco_opcode)0;
10082
10083 slot -= FRAG_RESULT_DATA0;
10084 target = V_008DFC_SQ_EXP_MRT + slot;
10085 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
10086
10087 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
10088 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
10089 bool is_16bit = values[0].regClass() == v2b;
10090
10091 switch (col_format)
10092 {
10093 case V_028714_SPI_SHADER_ZERO:
10094 enabled_channels = 0; /* writemask */
10095 target = V_008DFC_SQ_EXP_NULL;
10096 break;
10097
10098 case V_028714_SPI_SHADER_32_R:
10099 enabled_channels = 1;
10100 break;
10101
10102 case V_028714_SPI_SHADER_32_GR:
10103 enabled_channels = 0x3;
10104 break;
10105
10106 case V_028714_SPI_SHADER_32_AR:
10107 if (ctx->options->chip_class >= GFX10) {
10108 /* Special case: on GFX10, the outputs are different for 32_AR */
10109 enabled_channels = 0x3;
10110 values[1] = values[3];
10111 values[3] = Operand(v1);
10112 } else {
10113 enabled_channels = 0x9;
10114 }
10115 break;
10116
10117 case V_028714_SPI_SHADER_FP16_ABGR:
10118 enabled_channels = 0x5;
10119 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
10120 if (is_16bit) {
10121 if (ctx->options->chip_class >= GFX9) {
10122 /* Pack the FP16 values together instead of converting them to
10123 * FP32 and back to FP16.
10124 * TODO: use p_create_vector and let the compiler optimizes.
10125 */
10126 compr_op = aco_opcode::v_pack_b32_f16;
10127 } else {
10128 for (unsigned i = 0; i < 4; i++) {
10129 if ((write_mask >> i) & 1)
10130 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
10131 }
10132 }
10133 }
10134 break;
10135
10136 case V_028714_SPI_SHADER_UNORM16_ABGR:
10137 enabled_channels = 0x5;
10138 if (is_16bit && ctx->options->chip_class >= GFX9) {
10139 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
10140 } else {
10141 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10142 }
10143 break;
10144
10145 case V_028714_SPI_SHADER_SNORM16_ABGR:
10146 enabled_channels = 0x5;
10147 if (is_16bit && ctx->options->chip_class >= GFX9) {
10148 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
10149 } else {
10150 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10151 }
10152 break;
10153
10154 case V_028714_SPI_SHADER_UINT16_ABGR: {
10155 enabled_channels = 0x5;
10156 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10157 if (is_int8 || is_int10) {
10158 /* clamp */
10159 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10160 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10161
10162 for (unsigned i = 0; i < 4; i++) {
10163 if ((write_mask >> i) & 1) {
10164 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10165 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10166 values[i]);
10167 }
10168 }
10169 } else if (is_16bit) {
10170 for (unsigned i = 0; i < 4; i++) {
10171 if ((write_mask >> i) & 1) {
10172 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, false);
10173 values[i] = Operand(tmp);
10174 }
10175 }
10176 }
10177 break;
10178 }
10179
10180 case V_028714_SPI_SHADER_SINT16_ABGR:
10181 enabled_channels = 0x5;
10182 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10183 if (is_int8 || is_int10) {
10184 /* clamp */
10185 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10186 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10187 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10188 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10189
10190 for (unsigned i = 0; i < 4; i++) {
10191 if ((write_mask >> i) & 1) {
10192 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10193 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10194 values[i]);
10195 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10196 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10197 values[i]);
10198 }
10199 }
10200 } else if (is_16bit) {
10201 for (unsigned i = 0; i < 4; i++) {
10202 if ((write_mask >> i) & 1) {
10203 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, true);
10204 values[i] = Operand(tmp);
10205 }
10206 }
10207 }
10208 break;
10209
10210 case V_028714_SPI_SHADER_32_ABGR:
10211 enabled_channels = 0xF;
10212 break;
10213
10214 default:
10215 break;
10216 }
10217
10218 if (target == V_008DFC_SQ_EXP_NULL)
10219 return false;
10220
10221 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10222 if (ctx->options->enable_mrt_output_nan_fixup &&
10223 !is_16bit &&
10224 (col_format == V_028714_SPI_SHADER_32_R ||
10225 col_format == V_028714_SPI_SHADER_32_GR ||
10226 col_format == V_028714_SPI_SHADER_32_AR ||
10227 col_format == V_028714_SPI_SHADER_32_ABGR ||
10228 col_format == V_028714_SPI_SHADER_FP16_ABGR)) {
10229 for (int i = 0; i < 4; i++) {
10230 if (!(write_mask & (1 << i)))
10231 continue;
10232
10233 Temp isnan = bld.vopc(aco_opcode::v_cmp_class_f32,
10234 bld.hint_vcc(bld.def(bld.lm)), values[i],
10235 bld.copy(bld.def(v1), Operand(3u)));
10236 values[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), values[i],
10237 bld.copy(bld.def(v1), Operand(0u)), isnan);
10238 }
10239 }
10240
10241 if ((bool) compr_op) {
10242 for (int i = 0; i < 2; i++) {
10243 /* check if at least one of the values to be compressed is enabled */
10244 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10245 if (enabled) {
10246 enabled_channels |= enabled << (i*2);
10247 values[i] = bld.vop3(compr_op, bld.def(v1),
10248 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10249 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10250 } else {
10251 values[i] = Operand(v1);
10252 }
10253 }
10254 values[2] = Operand(v1);
10255 values[3] = Operand(v1);
10256 } else {
10257 for (int i = 0; i < 4; i++)
10258 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10259 }
10260
10261 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10262 enabled_channels, target, (bool) compr_op);
10263 return true;
10264 }
10265
10266 static void create_fs_exports(isel_context *ctx)
10267 {
10268 bool exported = false;
10269
10270 /* Export depth, stencil and sample mask. */
10271 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10272 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10273 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10274 exported |= export_fs_mrt_z(ctx);
10275
10276 /* Export all color render targets. */
10277 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10278 if (ctx->outputs.mask[i])
10279 exported |= export_fs_mrt_color(ctx, i);
10280
10281 if (!exported)
10282 create_null_export(ctx);
10283 }
10284
10285 static void write_tcs_tess_factors(isel_context *ctx)
10286 {
10287 unsigned outer_comps;
10288 unsigned inner_comps;
10289
10290 switch (ctx->args->options->key.tcs.primitive_mode) {
10291 case GL_ISOLINES:
10292 outer_comps = 2;
10293 inner_comps = 0;
10294 break;
10295 case GL_TRIANGLES:
10296 outer_comps = 3;
10297 inner_comps = 1;
10298 break;
10299 case GL_QUADS:
10300 outer_comps = 4;
10301 inner_comps = 2;
10302 break;
10303 default:
10304 return;
10305 }
10306
10307 Builder bld(ctx->program, ctx->block);
10308
10309 bld.barrier(aco_opcode::p_memory_barrier_shared);
10310 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10311 bld.sopp(aco_opcode::s_barrier);
10312
10313 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10314 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10315
10316 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10317 if_context ic_invocation_id_is_zero;
10318 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10319 bld.reset(ctx->block);
10320
10321 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));
10322
10323 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10324 unsigned stride = inner_comps + outer_comps;
10325 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10326 Temp tf_inner_vec;
10327 Temp tf_outer_vec;
10328 Temp out[6];
10329 assert(stride <= (sizeof(out) / sizeof(Temp)));
10330
10331 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10332 // LINES reversal
10333 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10334 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10335 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10336 } else {
10337 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);
10338 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);
10339
10340 for (unsigned i = 0; i < outer_comps; ++i)
10341 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10342 for (unsigned i = 0; i < inner_comps; ++i)
10343 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10344 }
10345
10346 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10347 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10348 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10349 unsigned tf_const_offset = 0;
10350
10351 if (ctx->program->chip_class <= GFX8) {
10352 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);
10353 if_context ic_rel_patch_id_is_zero;
10354 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10355 bld.reset(ctx->block);
10356
10357 /* Store the dynamic HS control word. */
10358 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10359 bld.mubuf(aco_opcode::buffer_store_dword,
10360 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10361 /* immediate OFFSET */ 0, /* OFFEN */ false, /* swizzled */ false, /* idxen*/ false,
10362 /* addr64 */ false, /* disable_wqm */ false, /* glc */ true);
10363 tf_const_offset += 4;
10364
10365 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10366 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10367 bld.reset(ctx->block);
10368 }
10369
10370 assert(stride == 2 || stride == 4 || stride == 6);
10371 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10372 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10373
10374 /* Store to offchip for TES to read - only if TES reads them */
10375 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10376 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));
10377 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10378
10379 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10380 store_vmem_mubuf(ctx, tf_outer_vec, hs_ring_tess_offchip, vmem_offs_outer.first, oc_lds, vmem_offs_outer.second, 4, (1 << outer_comps) - 1, true, false);
10381
10382 if (likely(inner_comps)) {
10383 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10384 store_vmem_mubuf(ctx, tf_inner_vec, hs_ring_tess_offchip, vmem_offs_inner.first, oc_lds, vmem_offs_inner.second, 4, (1 << inner_comps) - 1, true, false);
10385 }
10386 }
10387
10388 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10389 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10390 }
10391
10392 static void emit_stream_output(isel_context *ctx,
10393 Temp const *so_buffers,
10394 Temp const *so_write_offset,
10395 const struct radv_stream_output *output)
10396 {
10397 unsigned num_comps = util_bitcount(output->component_mask);
10398 unsigned writemask = (1 << num_comps) - 1;
10399 unsigned loc = output->location;
10400 unsigned buf = output->buffer;
10401
10402 assert(num_comps && num_comps <= 4);
10403 if (!num_comps || num_comps > 4)
10404 return;
10405
10406 unsigned start = ffs(output->component_mask) - 1;
10407
10408 Temp out[4];
10409 bool all_undef = true;
10410 assert(ctx->stage & hw_vs);
10411 for (unsigned i = 0; i < num_comps; i++) {
10412 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10413 all_undef = all_undef && !out[i].id();
10414 }
10415 if (all_undef)
10416 return;
10417
10418 while (writemask) {
10419 int start, count;
10420 u_bit_scan_consecutive_range(&writemask, &start, &count);
10421 if (count == 3 && ctx->options->chip_class == GFX6) {
10422 /* GFX6 doesn't support storing vec3, split it. */
10423 writemask |= 1u << (start + 2);
10424 count = 2;
10425 }
10426
10427 unsigned offset = output->offset + start * 4;
10428
10429 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10430 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10431 for (int i = 0; i < count; ++i)
10432 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10433 vec->definitions[0] = Definition(write_data);
10434 ctx->block->instructions.emplace_back(std::move(vec));
10435
10436 aco_opcode opcode;
10437 switch (count) {
10438 case 1:
10439 opcode = aco_opcode::buffer_store_dword;
10440 break;
10441 case 2:
10442 opcode = aco_opcode::buffer_store_dwordx2;
10443 break;
10444 case 3:
10445 opcode = aco_opcode::buffer_store_dwordx3;
10446 break;
10447 case 4:
10448 opcode = aco_opcode::buffer_store_dwordx4;
10449 break;
10450 default:
10451 unreachable("Unsupported dword count.");
10452 }
10453
10454 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10455 store->operands[0] = Operand(so_buffers[buf]);
10456 store->operands[1] = Operand(so_write_offset[buf]);
10457 store->operands[2] = Operand((uint32_t) 0);
10458 store->operands[3] = Operand(write_data);
10459 if (offset > 4095) {
10460 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10461 Builder bld(ctx->program, ctx->block);
10462 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10463 } else {
10464 store->offset = offset;
10465 }
10466 store->offen = true;
10467 store->glc = true;
10468 store->dlc = false;
10469 store->slc = true;
10470 store->can_reorder = true;
10471 ctx->block->instructions.emplace_back(std::move(store));
10472 }
10473 }
10474
10475 static void emit_streamout(isel_context *ctx, unsigned stream)
10476 {
10477 Builder bld(ctx->program, ctx->block);
10478
10479 Temp so_buffers[4];
10480 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10481 for (unsigned i = 0; i < 4; i++) {
10482 unsigned stride = ctx->program->info->so.strides[i];
10483 if (!stride)
10484 continue;
10485
10486 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10487 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10488 }
10489
10490 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10491 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10492
10493 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10494
10495 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10496
10497 if_context ic;
10498 begin_divergent_if_then(ctx, &ic, can_emit);
10499
10500 bld.reset(ctx->block);
10501
10502 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10503
10504 Temp so_write_offset[4];
10505
10506 for (unsigned i = 0; i < 4; i++) {
10507 unsigned stride = ctx->program->info->so.strides[i];
10508 if (!stride)
10509 continue;
10510
10511 if (stride == 1) {
10512 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10513 get_arg(ctx, ctx->args->streamout_write_idx),
10514 get_arg(ctx, ctx->args->streamout_offset[i]));
10515 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10516
10517 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10518 } else {
10519 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10520 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10521 get_arg(ctx, ctx->args->streamout_offset[i]));
10522 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10523 }
10524 }
10525
10526 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10527 struct radv_stream_output *output =
10528 &ctx->program->info->so.outputs[i];
10529 if (stream != output->stream)
10530 continue;
10531
10532 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10533 }
10534
10535 begin_divergent_if_else(ctx, &ic);
10536 end_divergent_if(ctx, &ic);
10537 }
10538
10539 } /* end namespace */
10540
10541 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10542 {
10543 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10544 Builder bld(ctx->program, ctx->block);
10545 constexpr unsigned hs_idx = 1u;
10546 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10547 get_arg(ctx, ctx->args->merged_wave_info),
10548 Operand((8u << 16) | (hs_idx * 8u)));
10549 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10550
10551 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10552
10553 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10554 get_arg(ctx, ctx->args->rel_auto_id),
10555 get_arg(ctx, ctx->args->ac.instance_id),
10556 ls_has_nonzero_hs_threads);
10557 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10558 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10559 get_arg(ctx, ctx->args->rel_auto_id),
10560 ls_has_nonzero_hs_threads);
10561 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10562 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10563 get_arg(ctx, ctx->args->ac.vertex_id),
10564 ls_has_nonzero_hs_threads);
10565
10566 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10567 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10568 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10569 }
10570
10571 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10572 {
10573 /* Split all arguments except for the first (ring_offsets) and the last
10574 * (exec) so that the dead channels don't stay live throughout the program.
10575 */
10576 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10577 if (startpgm->definitions[i].regClass().size() > 1) {
10578 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10579 startpgm->definitions[i].regClass().size());
10580 }
10581 }
10582 }
10583
10584 void handle_bc_optimize(isel_context *ctx)
10585 {
10586 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10587 Builder bld(ctx->program, ctx->block);
10588 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10589 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10590 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10591 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10592 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10593 if (uses_center && uses_centroid) {
10594 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10595 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10596
10597 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10598 Temp new_coord[2];
10599 for (unsigned i = 0; i < 2; i++) {
10600 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10601 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10602 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10603 persp_centroid, persp_center, sel);
10604 }
10605 ctx->persp_centroid = bld.tmp(v2);
10606 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10607 Operand(new_coord[0]), Operand(new_coord[1]));
10608 emit_split_vector(ctx, ctx->persp_centroid, 2);
10609 }
10610
10611 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10612 Temp new_coord[2];
10613 for (unsigned i = 0; i < 2; i++) {
10614 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10615 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10616 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10617 linear_centroid, linear_center, sel);
10618 }
10619 ctx->linear_centroid = bld.tmp(v2);
10620 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10621 Operand(new_coord[0]), Operand(new_coord[1]));
10622 emit_split_vector(ctx, ctx->linear_centroid, 2);
10623 }
10624 }
10625 }
10626
10627 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10628 {
10629 Program *program = ctx->program;
10630
10631 unsigned float_controls = shader->info.float_controls_execution_mode;
10632
10633 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10634 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10635 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10636 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10637 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10638
10639 program->next_fp_mode.must_flush_denorms32 =
10640 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10641 program->next_fp_mode.must_flush_denorms16_64 =
10642 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10643 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10644
10645 program->next_fp_mode.care_about_round32 =
10646 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10647
10648 program->next_fp_mode.care_about_round16_64 =
10649 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10650 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10651
10652 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10653 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10654 if (program->next_fp_mode.must_flush_denorms16_64)
10655 program->next_fp_mode.denorm16_64 = 0;
10656 else
10657 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10658
10659 /* preserving fp32 denorms is expensive, so only do it if asked */
10660 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10661 program->next_fp_mode.denorm32 = fp_denorm_keep;
10662 else
10663 program->next_fp_mode.denorm32 = 0;
10664
10665 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10666 program->next_fp_mode.round32 = fp_round_tz;
10667 else
10668 program->next_fp_mode.round32 = fp_round_ne;
10669
10670 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10671 program->next_fp_mode.round16_64 = fp_round_tz;
10672 else
10673 program->next_fp_mode.round16_64 = fp_round_ne;
10674
10675 ctx->block->fp_mode = program->next_fp_mode;
10676 }
10677
10678 void cleanup_cfg(Program *program)
10679 {
10680 /* create linear_succs/logical_succs */
10681 for (Block& BB : program->blocks) {
10682 for (unsigned idx : BB.linear_preds)
10683 program->blocks[idx].linear_succs.emplace_back(BB.index);
10684 for (unsigned idx : BB.logical_preds)
10685 program->blocks[idx].logical_succs.emplace_back(BB.index);
10686 }
10687 }
10688
10689 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10690 {
10691 Builder bld(ctx->program, ctx->block);
10692
10693 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10694 Temp count = i == 0
10695 ? get_arg(ctx, ctx->args->merged_wave_info)
10696 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10697 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10698
10699 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10700 Temp cond;
10701
10702 if (ctx->program->wave_size == 64) {
10703 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10704 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10705 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10706 } else {
10707 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10708 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10709 }
10710
10711 return cond;
10712 }
10713
10714 bool ngg_early_prim_export(isel_context *ctx)
10715 {
10716 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10717 return true;
10718 }
10719
10720 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10721 {
10722 Builder bld(ctx->program, ctx->block);
10723
10724 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10725 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10726
10727 /* Get the id of the current wave within the threadgroup (workgroup) */
10728 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10729 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10730
10731 /* Execute the following code only on the first wave (wave id 0),
10732 * use the SCC def to tell if the wave id is zero or not.
10733 */
10734 Temp cond = wave_id_in_tg.def(1).getTemp();
10735 if_context ic;
10736 begin_uniform_if_then(ctx, &ic, cond);
10737 begin_uniform_if_else(ctx, &ic);
10738 bld.reset(ctx->block);
10739
10740 /* Number of vertices output by VS/TES */
10741 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10742 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10743 /* Number of primitives output by VS/TES */
10744 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10745 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10746
10747 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10748 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10749 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10750
10751 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10752 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10753
10754 end_uniform_if(ctx, &ic);
10755
10756 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10757 bld.reset(ctx->block);
10758 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10759 }
10760
10761 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10762 {
10763 Builder bld(ctx->program, ctx->block);
10764
10765 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10766 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10767 }
10768
10769 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10770 Temp tmp;
10771
10772 for (unsigned i = 0; i < num_vertices; ++i) {
10773 assert(vtxindex[i].id());
10774
10775 if (i)
10776 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10777 else
10778 tmp = vtxindex[i];
10779
10780 /* The initial edge flag is always false in tess eval shaders. */
10781 if (ctx->stage == ngg_vertex_gs) {
10782 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10783 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10784 }
10785 }
10786
10787 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10788
10789 return tmp;
10790 }
10791
10792 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10793 {
10794 Builder bld(ctx->program, ctx->block);
10795 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10796
10797 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10798 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10799 false /* compressed */, true/* done */, false /* valid mask */);
10800 }
10801
10802 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10803 {
10804 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10805 * These must always come before VS exports.
10806 *
10807 * It is recommended to do these as early as possible. They can be at the beginning when
10808 * there is no SW GS and the shader doesn't write edge flags.
10809 */
10810
10811 if_context ic;
10812 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10813 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10814
10815 Builder bld(ctx->program, ctx->block);
10816 constexpr unsigned max_vertices_per_primitive = 3;
10817 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10818
10819 if (ctx->stage == ngg_vertex_gs) {
10820 /* TODO: optimize for points & lines */
10821 } else if (ctx->stage == ngg_tess_eval_gs) {
10822 if (ctx->shader->info.tess.point_mode)
10823 num_vertices_per_primitive = 1;
10824 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10825 num_vertices_per_primitive = 2;
10826 } else {
10827 unreachable("Unsupported NGG shader stage");
10828 }
10829
10830 Temp vtxindex[max_vertices_per_primitive];
10831 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10832 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10833 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10834 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10835 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10836 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10837 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10838 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10839
10840 /* Export primitive data to the index buffer. */
10841 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10842
10843 /* Export primitive ID. */
10844 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10845 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10846 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10847 Temp provoking_vtx_index = vtxindex[0];
10848 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10849
10850 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10851 }
10852
10853 begin_divergent_if_else(ctx, &ic);
10854 end_divergent_if(ctx, &ic);
10855 }
10856
10857 void ngg_emit_nogs_output(isel_context *ctx)
10858 {
10859 /* Emits NGG GS output, for stages that don't have SW GS. */
10860
10861 if_context ic;
10862 Builder bld(ctx->program, ctx->block);
10863 bool late_prim_export = !ngg_early_prim_export(ctx);
10864
10865 /* NGG streamout is currently disabled by default. */
10866 assert(!ctx->args->shader_info->so.num_outputs);
10867
10868 if (late_prim_export) {
10869 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10870 create_export_phis(ctx);
10871 /* Do what we need to do in the GS threads. */
10872 ngg_emit_nogs_gsthreads(ctx);
10873
10874 /* What comes next should be executed on ES threads. */
10875 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10876 begin_divergent_if_then(ctx, &ic, is_es_thread);
10877 bld.reset(ctx->block);
10878 }
10879
10880 /* Export VS outputs */
10881 ctx->block->kind |= block_kind_export_end;
10882 create_vs_exports(ctx);
10883
10884 /* Export primitive ID */
10885 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10886 Temp prim_id;
10887
10888 if (ctx->stage == ngg_vertex_gs) {
10889 /* Wait for GS threads to store primitive ID in LDS. */
10890 bld.barrier(aco_opcode::p_memory_barrier_shared);
10891 bld.sopp(aco_opcode::s_barrier);
10892
10893 /* Calculate LDS address where the GS threads stored the primitive ID. */
10894 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10895 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10896 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10897 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10898 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10899 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10900
10901 /* Load primitive ID from LDS. */
10902 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10903 } else if (ctx->stage == ngg_tess_eval_gs) {
10904 /* TES: Just use the patch ID as the primitive ID. */
10905 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10906 } else {
10907 unreachable("unsupported NGG shader stage.");
10908 }
10909
10910 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10911 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10912
10913 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10914 }
10915
10916 if (late_prim_export) {
10917 begin_divergent_if_else(ctx, &ic);
10918 end_divergent_if(ctx, &ic);
10919 bld.reset(ctx->block);
10920 }
10921 }
10922
10923 void select_program(Program *program,
10924 unsigned shader_count,
10925 struct nir_shader *const *shaders,
10926 ac_shader_config* config,
10927 struct radv_shader_args *args)
10928 {
10929 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10930 if_context ic_merged_wave_info;
10931 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10932
10933 for (unsigned i = 0; i < shader_count; i++) {
10934 nir_shader *nir = shaders[i];
10935 init_context(&ctx, nir);
10936
10937 setup_fp_mode(&ctx, nir);
10938
10939 if (!i) {
10940 /* needs to be after init_context() for FS */
10941 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10942 append_logical_start(ctx.block);
10943
10944 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10945 fix_ls_vgpr_init_bug(&ctx, startpgm);
10946
10947 split_arguments(&ctx, startpgm);
10948 }
10949
10950 if (ngg_no_gs) {
10951 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10952
10953 if (ngg_early_prim_export(&ctx))
10954 ngg_emit_nogs_gsthreads(&ctx);
10955 }
10956
10957 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10958 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10959 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10960 ((nir->info.stage == MESA_SHADER_VERTEX &&
10961 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10962 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10963 ctx.stage == tess_eval_geometry_gs));
10964
10965 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10966 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10967 if (check_merged_wave_info) {
10968 Temp cond = merged_wave_info_to_mask(&ctx, i);
10969 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10970 }
10971
10972 if (i) {
10973 Builder bld(ctx.program, ctx.block);
10974
10975 bld.barrier(aco_opcode::p_memory_barrier_shared);
10976 bld.sopp(aco_opcode::s_barrier);
10977
10978 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10979 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));
10980 }
10981 } else if (ctx.stage == geometry_gs)
10982 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10983
10984 if (ctx.stage == fragment_fs)
10985 handle_bc_optimize(&ctx);
10986
10987 visit_cf_list(&ctx, &func->body);
10988
10989 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10990 emit_streamout(&ctx, 0);
10991
10992 if (ctx.stage & hw_vs) {
10993 create_vs_exports(&ctx);
10994 ctx.block->kind |= block_kind_export_end;
10995 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10996 ngg_emit_nogs_output(&ctx);
10997 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10998 Builder bld(ctx.program, ctx.block);
10999 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
11000 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
11001 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
11002 write_tcs_tess_factors(&ctx);
11003 }
11004
11005 if (ctx.stage == fragment_fs) {
11006 create_fs_exports(&ctx);
11007 ctx.block->kind |= block_kind_export_end;
11008 }
11009
11010 if (endif_merged_wave_info) {
11011 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
11012 end_divergent_if(&ctx, &ic_merged_wave_info);
11013 }
11014
11015 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
11016 ngg_emit_nogs_output(&ctx);
11017
11018 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
11019 /* Outputs of the previous stage are inputs to the next stage */
11020 ctx.inputs = ctx.outputs;
11021 ctx.outputs = shader_io_state();
11022 }
11023 }
11024
11025 program->config->float_mode = program->blocks[0].fp_mode.val;
11026
11027 append_logical_end(ctx.block);
11028 ctx.block->kind |= block_kind_uniform;
11029 Builder bld(ctx.program, ctx.block);
11030 if (ctx.program->wb_smem_l1_on_end)
11031 bld.smem(aco_opcode::s_dcache_wb, false);
11032 bld.sopp(aco_opcode::s_endpgm);
11033
11034 cleanup_cfg(program);
11035 }
11036
11037 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
11038 ac_shader_config* config,
11039 struct radv_shader_args *args)
11040 {
11041 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
11042
11043 ctx.block->fp_mode = program->next_fp_mode;
11044
11045 add_startpgm(&ctx);
11046 append_logical_start(ctx.block);
11047
11048 Builder bld(ctx.program, ctx.block);
11049
11050 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
11051
11052 Operand stream_id(0u);
11053 if (args->shader_info->so.num_outputs)
11054 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
11055 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
11056
11057 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
11058
11059 std::stack<Block> endif_blocks;
11060
11061 for (unsigned stream = 0; stream < 4; stream++) {
11062 if (stream_id.isConstant() && stream != stream_id.constantValue())
11063 continue;
11064
11065 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
11066 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
11067 continue;
11068
11069 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
11070
11071 unsigned BB_if_idx = ctx.block->index;
11072 Block BB_endif = Block();
11073 if (!stream_id.isConstant()) {
11074 /* begin IF */
11075 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
11076 append_logical_end(ctx.block);
11077 ctx.block->kind |= block_kind_uniform;
11078 bld.branch(aco_opcode::p_cbranch_z, cond);
11079
11080 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
11081
11082 ctx.block = ctx.program->create_and_insert_block();
11083 add_edge(BB_if_idx, ctx.block);
11084 bld.reset(ctx.block);
11085 append_logical_start(ctx.block);
11086 }
11087
11088 unsigned offset = 0;
11089 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
11090 if (args->shader_info->gs.output_streams[i] != stream)
11091 continue;
11092
11093 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
11094 unsigned length = util_last_bit(output_usage_mask);
11095 for (unsigned j = 0; j < length; ++j) {
11096 if (!(output_usage_mask & (1 << j)))
11097 continue;
11098
11099 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
11100 Temp voffset = vtx_offset;
11101 if (const_offset >= 4096u) {
11102 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
11103 const_offset %= 4096u;
11104 }
11105
11106 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
11107 mubuf->definitions[0] = bld.def(v1);
11108 mubuf->operands[0] = Operand(gsvs_ring);
11109 mubuf->operands[1] = Operand(voffset);
11110 mubuf->operands[2] = Operand(0u);
11111 mubuf->offen = true;
11112 mubuf->offset = const_offset;
11113 mubuf->glc = true;
11114 mubuf->slc = true;
11115 mubuf->dlc = args->options->chip_class >= GFX10;
11116 mubuf->barrier = barrier_none;
11117 mubuf->can_reorder = true;
11118
11119 ctx.outputs.mask[i] |= 1 << j;
11120 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
11121
11122 bld.insert(std::move(mubuf));
11123
11124 offset++;
11125 }
11126 }
11127
11128 if (args->shader_info->so.num_outputs) {
11129 emit_streamout(&ctx, stream);
11130 bld.reset(ctx.block);
11131 }
11132
11133 if (stream == 0) {
11134 create_vs_exports(&ctx);
11135 ctx.block->kind |= block_kind_export_end;
11136 }
11137
11138 if (!stream_id.isConstant()) {
11139 append_logical_end(ctx.block);
11140
11141 /* branch from then block to endif block */
11142 bld.branch(aco_opcode::p_branch);
11143 add_edge(ctx.block->index, &BB_endif);
11144 ctx.block->kind |= block_kind_uniform;
11145
11146 /* emit else block */
11147 ctx.block = ctx.program->create_and_insert_block();
11148 add_edge(BB_if_idx, ctx.block);
11149 bld.reset(ctx.block);
11150 append_logical_start(ctx.block);
11151
11152 endif_blocks.push(std::move(BB_endif));
11153 }
11154 }
11155
11156 while (!endif_blocks.empty()) {
11157 Block BB_endif = std::move(endif_blocks.top());
11158 endif_blocks.pop();
11159
11160 Block *BB_else = ctx.block;
11161
11162 append_logical_end(BB_else);
11163 /* branch from else block to endif block */
11164 bld.branch(aco_opcode::p_branch);
11165 add_edge(BB_else->index, &BB_endif);
11166 BB_else->kind |= block_kind_uniform;
11167
11168 /** emit endif merge block */
11169 ctx.block = program->insert_block(std::move(BB_endif));
11170 bld.reset(ctx.block);
11171 append_logical_start(ctx.block);
11172 }
11173
11174 program->config->float_mode = program->blocks[0].fp_mode.val;
11175
11176 append_logical_end(ctx.block);
11177 ctx.block->kind |= block_kind_uniform;
11178 bld.sopp(aco_opcode::s_endpgm);
11179
11180 cleanup_cfg(program);
11181 }
11182 }