aco: rewrite graph coloring in spiller
[mesa.git] / src / amd / compiler / aco_instruction_selection.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <algorithm>
27 #include <array>
28 #include <stack>
29 #include <map>
30
31 #include "ac_shader_util.h"
32 #include "aco_ir.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
37
38 namespace aco {
39 namespace {
40
41 class loop_info_RAII {
42 isel_context* ctx;
43 unsigned header_idx_old;
44 Block* exit_old;
45 bool divergent_cont_old;
46 bool divergent_branch_old;
47 bool divergent_if_old;
48
49 public:
50 loop_info_RAII(isel_context* ctx, unsigned loop_header_idx, Block* loop_exit)
51 : ctx(ctx),
52 header_idx_old(ctx->cf_info.parent_loop.header_idx), exit_old(ctx->cf_info.parent_loop.exit),
53 divergent_cont_old(ctx->cf_info.parent_loop.has_divergent_continue),
54 divergent_branch_old(ctx->cf_info.parent_loop.has_divergent_branch),
55 divergent_if_old(ctx->cf_info.parent_if.is_divergent)
56 {
57 ctx->cf_info.parent_loop.header_idx = loop_header_idx;
58 ctx->cf_info.parent_loop.exit = loop_exit;
59 ctx->cf_info.parent_loop.has_divergent_continue = false;
60 ctx->cf_info.parent_loop.has_divergent_branch = false;
61 ctx->cf_info.parent_if.is_divergent = false;
62 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
63 }
64
65 ~loop_info_RAII()
66 {
67 ctx->cf_info.parent_loop.header_idx = header_idx_old;
68 ctx->cf_info.parent_loop.exit = exit_old;
69 ctx->cf_info.parent_loop.has_divergent_continue = divergent_cont_old;
70 ctx->cf_info.parent_loop.has_divergent_branch = divergent_branch_old;
71 ctx->cf_info.parent_if.is_divergent = divergent_if_old;
72 ctx->cf_info.loop_nest_depth = ctx->cf_info.loop_nest_depth - 1;
73 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent)
74 ctx->cf_info.exec_potentially_empty_discard = false;
75 }
76 };
77
78 struct if_context {
79 Temp cond;
80
81 bool divergent_old;
82 bool exec_potentially_empty_discard_old;
83 bool exec_potentially_empty_break_old;
84 uint16_t exec_potentially_empty_break_depth_old;
85
86 unsigned BB_if_idx;
87 unsigned invert_idx;
88 bool uniform_has_then_branch;
89 bool then_branch_divergent;
90 Block BB_invert;
91 Block BB_endif;
92 };
93
94 static bool visit_cf_list(struct isel_context *ctx,
95 struct exec_list *list);
96
97 static void add_logical_edge(unsigned pred_idx, Block *succ)
98 {
99 succ->logical_preds.emplace_back(pred_idx);
100 }
101
102
103 static void add_linear_edge(unsigned pred_idx, Block *succ)
104 {
105 succ->linear_preds.emplace_back(pred_idx);
106 }
107
108 static void add_edge(unsigned pred_idx, Block *succ)
109 {
110 add_logical_edge(pred_idx, succ);
111 add_linear_edge(pred_idx, succ);
112 }
113
114 static void append_logical_start(Block *b)
115 {
116 Builder(NULL, b).pseudo(aco_opcode::p_logical_start);
117 }
118
119 static void append_logical_end(Block *b)
120 {
121 Builder(NULL, b).pseudo(aco_opcode::p_logical_end);
122 }
123
124 Temp get_ssa_temp(struct isel_context *ctx, nir_ssa_def *def)
125 {
126 assert(ctx->allocated[def->index].id());
127 return ctx->allocated[def->index];
128 }
129
130 Temp emit_mbcnt(isel_context *ctx, Definition dst,
131 Operand mask_lo = Operand((uint32_t) -1), Operand mask_hi = Operand((uint32_t) -1))
132 {
133 Builder bld(ctx->program, ctx->block);
134 Definition lo_def = ctx->program->wave_size == 32 ? dst : bld.def(v1);
135 Temp thread_id_lo = bld.vop3(aco_opcode::v_mbcnt_lo_u32_b32, lo_def, mask_lo, Operand(0u));
136
137 if (ctx->program->wave_size == 32) {
138 return thread_id_lo;
139 } else if (ctx->program->chip_class <= GFX7) {
140 Temp thread_id_hi = bld.vop2(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 } else {
143 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32_e64, dst, mask_hi, thread_id_lo);
144 return thread_id_hi;
145 }
146 }
147
148 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
149 {
150 Builder bld(ctx->program, ctx->block);
151
152 if (!dst.id())
153 dst = bld.tmp(src.regClass());
154
155 assert(src.size() == dst.size());
156
157 if (ctx->stage != fragment_fs) {
158 if (!dst.id())
159 return src;
160
161 bld.copy(Definition(dst), src);
162 return dst;
163 }
164
165 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
166 ctx->program->needs_wqm |= program_needs_wqm;
167 return dst;
168 }
169
170 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
171 {
172 if (index.regClass() == s1)
173 return bld.readlane(bld.def(s1), data, index);
174
175 if (ctx->options->chip_class <= GFX7) {
176 /* GFX6-7: there is no bpermute instruction */
177 Operand index_op(index);
178 Operand input_data(data);
179 index_op.setLateKill(true);
180 input_data.setLateKill(true);
181
182 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(bld.lm), bld.def(bld.lm, vcc), index_op, input_data);
183 } else if (ctx->options->chip_class >= GFX10 && ctx->program->wave_size == 64) {
184 /* GFX10 wave64 mode: emulate full-wave bpermute */
185 if (!ctx->has_gfx10_wave64_bpermute) {
186 ctx->has_gfx10_wave64_bpermute = true;
187 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
188 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
189 }
190
191 Temp index_is_lo = bld.vopc(aco_opcode::v_cmp_ge_u32, bld.def(bld.lm), Operand(31u), index);
192 Builder::Result index_is_lo_split = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), index_is_lo);
193 Temp index_is_lo_n1 = bld.sop1(aco_opcode::s_not_b32, bld.def(s1), bld.def(s1, scc), index_is_lo_split.def(1).getTemp());
194 Operand same_half = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), index_is_lo_split.def(0).getTemp(), index_is_lo_n1);
195 Operand index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
196 Operand input_data(data);
197
198 index_x4.setLateKill(true);
199 input_data.setLateKill(true);
200 same_half.setLateKill(true);
201
202 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc), index_x4, input_data, same_half);
203 } else {
204 /* GFX8-9 or GFX10 wave32: bpermute works normally */
205 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
206 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
207 }
208 }
209
210 static Temp emit_masked_swizzle(isel_context *ctx, Builder &bld, Temp src, unsigned mask)
211 {
212 if (ctx->options->chip_class >= GFX8) {
213 unsigned and_mask = mask & 0x1f;
214 unsigned or_mask = (mask >> 5) & 0x1f;
215 unsigned xor_mask = (mask >> 10) & 0x1f;
216
217 uint16_t dpp_ctrl = 0xffff;
218
219 // TODO: we could use DPP8 for some swizzles
220 if (and_mask == 0x1f && or_mask < 4 && xor_mask < 4) {
221 unsigned res[4] = {0, 1, 2, 3};
222 for (unsigned i = 0; i < 4; i++)
223 res[i] = ((res[i] | or_mask) ^ xor_mask) & 0x3;
224 dpp_ctrl = dpp_quad_perm(res[0], res[1], res[2], res[3]);
225 } else if (and_mask == 0x1f && !or_mask && xor_mask == 8) {
226 dpp_ctrl = dpp_row_rr(8);
227 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0xf) {
228 dpp_ctrl = dpp_row_mirror;
229 } else if (and_mask == 0x1f && !or_mask && xor_mask == 0x7) {
230 dpp_ctrl = dpp_row_half_mirror;
231 }
232
233 if (dpp_ctrl != 0xffff)
234 return bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
235 }
236
237 return bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false);
238 }
239
240 Temp as_vgpr(isel_context *ctx, Temp val)
241 {
242 if (val.type() == RegType::sgpr) {
243 Builder bld(ctx->program, ctx->block);
244 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
245 }
246 assert(val.type() == RegType::vgpr);
247 return val;
248 }
249
250 //assumes a != 0xffffffff
251 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
252 {
253 assert(b != 0);
254 Builder bld(ctx->program, ctx->block);
255
256 if (util_is_power_of_two_or_zero(b)) {
257 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
258 return;
259 }
260
261 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
262
263 assert(info.multiplier <= 0xffffffff);
264
265 bool pre_shift = info.pre_shift != 0;
266 bool increment = info.increment != 0;
267 bool multiply = true;
268 bool post_shift = info.post_shift != 0;
269
270 if (!pre_shift && !increment && !multiply && !post_shift) {
271 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
272 return;
273 }
274
275 Temp pre_shift_dst = a;
276 if (pre_shift) {
277 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
278 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
279 }
280
281 Temp increment_dst = pre_shift_dst;
282 if (increment) {
283 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
284 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
285 }
286
287 Temp multiply_dst = increment_dst;
288 if (multiply) {
289 multiply_dst = post_shift ? bld.tmp(v1) : dst;
290 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
291 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
292 }
293
294 if (post_shift) {
295 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
296 }
297 }
298
299 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
300 {
301 Builder bld(ctx->program, ctx->block);
302 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
303 }
304
305
306 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
307 {
308 /* no need to extract the whole vector */
309 if (src.regClass() == dst_rc) {
310 assert(idx == 0);
311 return src;
312 }
313
314 assert(src.bytes() > (idx * dst_rc.bytes()));
315 Builder bld(ctx->program, ctx->block);
316 auto it = ctx->allocated_vec.find(src.id());
317 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
318 if (it->second[idx].regClass() == dst_rc) {
319 return it->second[idx];
320 } else {
321 assert(!dst_rc.is_subdword());
322 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
323 return bld.copy(bld.def(dst_rc), it->second[idx]);
324 }
325 }
326
327 if (dst_rc.is_subdword())
328 src = as_vgpr(ctx, src);
329
330 if (src.bytes() == dst_rc.bytes()) {
331 assert(idx == 0);
332 return bld.copy(bld.def(dst_rc), src);
333 } else {
334 Temp dst = bld.tmp(dst_rc);
335 emit_extract_vector(ctx, src, idx, dst);
336 return dst;
337 }
338 }
339
340 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
341 {
342 if (num_components == 1)
343 return;
344 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
345 return;
346 RegClass rc;
347 if (num_components > vec_src.size()) {
348 if (vec_src.type() == RegType::sgpr) {
349 /* should still help get_alu_src() */
350 emit_split_vector(ctx, vec_src, vec_src.size());
351 return;
352 }
353 /* sub-dword split */
354 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
355 } else {
356 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
357 }
358 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
359 split->operands[0] = Operand(vec_src);
360 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
361 for (unsigned i = 0; i < num_components; i++) {
362 elems[i] = {ctx->program->allocateId(), rc};
363 split->definitions[i] = Definition(elems[i]);
364 }
365 ctx->block->instructions.emplace_back(std::move(split));
366 ctx->allocated_vec.emplace(vec_src.id(), elems);
367 }
368
369 /* This vector expansion uses a mask to determine which elements in the new vector
370 * come from the original vector. The other elements are undefined. */
371 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
372 {
373 emit_split_vector(ctx, vec_src, util_bitcount(mask));
374
375 if (vec_src == dst)
376 return;
377
378 Builder bld(ctx->program, ctx->block);
379 if (num_components == 1) {
380 if (dst.type() == RegType::sgpr)
381 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
382 else
383 bld.copy(Definition(dst), vec_src);
384 return;
385 }
386
387 unsigned component_size = dst.size() / num_components;
388 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
389
390 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
391 vec->definitions[0] = Definition(dst);
392 unsigned k = 0;
393 for (unsigned i = 0; i < num_components; i++) {
394 if (mask & (1 << i)) {
395 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
396 if (dst.type() == RegType::sgpr)
397 src = bld.as_uniform(src);
398 vec->operands[i] = Operand(src);
399 } else {
400 vec->operands[i] = Operand(0u);
401 }
402 elems[i] = vec->operands[i].getTemp();
403 }
404 ctx->block->instructions.emplace_back(std::move(vec));
405 ctx->allocated_vec.emplace(dst.id(), elems);
406 }
407
408 /* adjust misaligned small bit size loads */
409 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
410 {
411 Builder bld(ctx->program, ctx->block);
412 Operand shift;
413 Temp select = Temp();
414 if (offset.isConstant()) {
415 assert(offset.constantValue() && offset.constantValue() < 4);
416 shift = Operand(offset.constantValue() * 8);
417 } else {
418 /* bit_offset = 8 * (offset & 0x3) */
419 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
420 select = bld.tmp(s1);
421 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
422 }
423
424 if (vec.size() == 1) {
425 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
426 } else if (vec.size() == 2) {
427 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
428 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
429 if (tmp == dst)
430 emit_split_vector(ctx, dst, 2);
431 else
432 emit_extract_vector(ctx, tmp, 0, dst);
433 } else if (vec.size() == 4) {
434 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
435 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
436 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
437 if (select != Temp())
438 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), bld.scc(select));
439 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
440 Temp mid = bld.tmp(s1);
441 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
442 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
443 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
444 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
445 emit_split_vector(ctx, dst, 2);
446 }
447 }
448
449 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst, unsigned component_size)
450 {
451 Builder bld(ctx->program, ctx->block);
452 if (offset.isTemp()) {
453 Temp tmp[4] = {vec, vec, vec, vec};
454
455 if (vec.size() == 4) {
456 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1), tmp[3] = bld.tmp(v1);
457 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), Definition(tmp[3]), vec);
458 } else if (vec.size() == 3) {
459 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
460 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
461 } else if (vec.size() == 2) {
462 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
463 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
464 }
465 for (unsigned i = 0; i < dst.size(); i++)
466 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
467
468 vec = tmp[0];
469 if (dst.size() == 2)
470 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
471
472 offset = Operand(0u);
473 }
474
475 unsigned num_components = dst.bytes() / component_size;
476 if (vec.regClass() == dst.regClass()) {
477 assert(offset.constantValue() == 0);
478 bld.copy(Definition(dst), vec);
479 emit_split_vector(ctx, dst, num_components);
480 return;
481 }
482
483 emit_split_vector(ctx, vec, vec.bytes() / component_size);
484 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
485 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
486
487 assert(offset.constantValue() % component_size == 0);
488 unsigned skip = offset.constantValue() / component_size;
489 for (unsigned i = 0; i < num_components; i++)
490 elems[i] = emit_extract_vector(ctx, vec, i + skip, rc);
491
492 /* if dst is vgpr - split the src and create a shrunk version according to the mask. */
493 if (dst.type() == RegType::vgpr) {
494 aco_ptr<Pseudo_instruction> create_vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
495 for (unsigned i = 0; i < num_components; i++)
496 create_vec->operands[i] = Operand(elems[i]);
497 create_vec->definitions[0] = Definition(dst);
498 bld.insert(std::move(create_vec));
499
500 /* if dst is sgpr - split the src, but move the original to sgpr. */
501 } else if (skip) {
502 vec = bld.pseudo(aco_opcode::p_as_uniform, bld.def(RegClass(RegType::sgpr, vec.size())), vec);
503 byte_align_scalar(ctx, vec, offset, dst);
504 } else {
505 assert(dst.size() == vec.size());
506 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
507 }
508
509 ctx->allocated_vec.emplace(dst.id(), elems);
510 }
511
512 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
513 {
514 Builder bld(ctx->program, ctx->block);
515 if (!dst.id())
516 dst = bld.tmp(bld.lm);
517
518 assert(val.regClass() == s1);
519 assert(dst.regClass() == bld.lm);
520
521 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
522 }
523
524 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
525 {
526 Builder bld(ctx->program, ctx->block);
527 if (!dst.id())
528 dst = bld.tmp(s1);
529
530 assert(val.regClass() == bld.lm);
531 assert(dst.regClass() == s1);
532
533 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
534 Temp tmp = bld.tmp(s1);
535 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
536 return emit_wqm(ctx, tmp, dst);
537 }
538
539 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
540 {
541 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
542 return get_ssa_temp(ctx, src.src.ssa);
543
544 if (src.src.ssa->num_components == size) {
545 bool identity_swizzle = true;
546 for (unsigned i = 0; identity_swizzle && i < size; i++) {
547 if (src.swizzle[i] != i)
548 identity_swizzle = false;
549 }
550 if (identity_swizzle)
551 return get_ssa_temp(ctx, src.src.ssa);
552 }
553
554 Temp vec = get_ssa_temp(ctx, src.src.ssa);
555 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
556 assert(elem_size > 0);
557 assert(vec.bytes() % elem_size == 0);
558
559 if (elem_size < 4 && vec.type() == RegType::sgpr) {
560 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
561 assert(size == 1);
562 unsigned swizzle = src.swizzle[0];
563 if (vec.size() > 1) {
564 assert(src.src.ssa->bit_size == 16);
565 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
566 swizzle = swizzle & 1;
567 }
568 if (swizzle == 0)
569 return vec;
570
571 Temp dst{ctx->program->allocateId(), s1};
572 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
573 bfe->operands[0] = Operand(vec);
574 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
575 bfe->definitions[0] = Definition(dst);
576 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
577 ctx->block->instructions.emplace_back(std::move(bfe));
578 return dst;
579 }
580
581 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
582 if (size == 1) {
583 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
584 } else {
585 assert(size <= 4);
586 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
587 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
588 for (unsigned i = 0; i < size; ++i) {
589 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
590 vec_instr->operands[i] = Operand{elems[i]};
591 }
592 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
593 vec_instr->definitions[0] = Definition(dst);
594 ctx->block->instructions.emplace_back(std::move(vec_instr));
595 ctx->allocated_vec.emplace(dst.id(), elems);
596 return dst;
597 }
598 }
599
600 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
601 {
602 if (ptr.size() == 2)
603 return ptr;
604 Builder bld(ctx->program, ctx->block);
605 if (ptr.type() == RegType::vgpr)
606 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
607 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
608 ptr, Operand((unsigned)ctx->options->address32_hi));
609 }
610
611 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
612 {
613 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
614 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
615 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
616 sop2->definitions[0] = Definition(dst);
617 if (writes_scc)
618 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
619 ctx->block->instructions.emplace_back(std::move(sop2));
620 }
621
622 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
623 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
624 {
625 Builder bld(ctx->program, ctx->block);
626 bld.is_precise = instr->exact;
627
628 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
629 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
630 if (src1.type() == RegType::sgpr) {
631 if (commutative && src0.type() == RegType::vgpr) {
632 Temp t = src0;
633 src0 = src1;
634 src1 = t;
635 } else {
636 src1 = as_vgpr(ctx, src1);
637 }
638 }
639
640 if (flush_denorms && ctx->program->chip_class < GFX9) {
641 assert(dst.size() == 1);
642 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
643 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
644 } else {
645 bld.vop2(op, Definition(dst), src0, src1);
646 }
647 }
648
649 void emit_vop2_instruction_logic64(isel_context *ctx, nir_alu_instr *instr,
650 aco_opcode op, Temp dst)
651 {
652 Builder bld(ctx->program, ctx->block);
653 bld.is_precise = instr->exact;
654
655 Temp src0 = get_alu_src(ctx, instr->src[0]);
656 Temp src1 = get_alu_src(ctx, instr->src[1]);
657
658 if (src1.type() == RegType::sgpr) {
659 assert(src0.type() == RegType::vgpr);
660 std::swap(src0, src1);
661 }
662
663 Temp src00 = bld.tmp(src0.type(), 1);
664 Temp src01 = bld.tmp(src0.type(), 1);
665 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
666 Temp src10 = bld.tmp(v1);
667 Temp src11 = bld.tmp(v1);
668 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
669 Temp lo = bld.vop2(op, bld.def(v1), src00, src10);
670 Temp hi = bld.vop2(op, bld.def(v1), src01, src11);
671 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
672 }
673
674 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
675 bool flush_denorms = false)
676 {
677 Temp src0 = get_alu_src(ctx, instr->src[0]);
678 Temp src1 = get_alu_src(ctx, instr->src[1]);
679 Temp src2 = get_alu_src(ctx, instr->src[2]);
680
681 /* ensure that the instruction has at most 1 sgpr operand
682 * The optimizer will inline constants for us */
683 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
684 src0 = as_vgpr(ctx, src0);
685 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
686 src1 = as_vgpr(ctx, src1);
687 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
688 src2 = as_vgpr(ctx, src2);
689
690 Builder bld(ctx->program, ctx->block);
691 bld.is_precise = instr->exact;
692 if (flush_denorms && ctx->program->chip_class < GFX9) {
693 assert(dst.size() == 1);
694 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
695 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
696 } else {
697 bld.vop3(op, Definition(dst), src0, src1, src2);
698 }
699 }
700
701 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
702 {
703 Builder bld(ctx->program, ctx->block);
704 bld.is_precise = instr->exact;
705 if (dst.type() == RegType::sgpr)
706 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
707 bld.vop1(op, bld.def(RegType::vgpr, dst.size()), get_alu_src(ctx, instr->src[0])));
708 else
709 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
710 }
711
712 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
713 {
714 Temp src0 = get_alu_src(ctx, instr->src[0]);
715 Temp src1 = get_alu_src(ctx, instr->src[1]);
716 assert(src0.size() == src1.size());
717
718 aco_ptr<Instruction> vopc;
719 if (src1.type() == RegType::sgpr) {
720 if (src0.type() == RegType::vgpr) {
721 /* to swap the operands, we might also have to change the opcode */
722 switch (op) {
723 case aco_opcode::v_cmp_lt_f16:
724 op = aco_opcode::v_cmp_gt_f16;
725 break;
726 case aco_opcode::v_cmp_ge_f16:
727 op = aco_opcode::v_cmp_le_f16;
728 break;
729 case aco_opcode::v_cmp_lt_i16:
730 op = aco_opcode::v_cmp_gt_i16;
731 break;
732 case aco_opcode::v_cmp_ge_i16:
733 op = aco_opcode::v_cmp_le_i16;
734 break;
735 case aco_opcode::v_cmp_lt_u16:
736 op = aco_opcode::v_cmp_gt_u16;
737 break;
738 case aco_opcode::v_cmp_ge_u16:
739 op = aco_opcode::v_cmp_le_u16;
740 break;
741 case aco_opcode::v_cmp_lt_f32:
742 op = aco_opcode::v_cmp_gt_f32;
743 break;
744 case aco_opcode::v_cmp_ge_f32:
745 op = aco_opcode::v_cmp_le_f32;
746 break;
747 case aco_opcode::v_cmp_lt_i32:
748 op = aco_opcode::v_cmp_gt_i32;
749 break;
750 case aco_opcode::v_cmp_ge_i32:
751 op = aco_opcode::v_cmp_le_i32;
752 break;
753 case aco_opcode::v_cmp_lt_u32:
754 op = aco_opcode::v_cmp_gt_u32;
755 break;
756 case aco_opcode::v_cmp_ge_u32:
757 op = aco_opcode::v_cmp_le_u32;
758 break;
759 case aco_opcode::v_cmp_lt_f64:
760 op = aco_opcode::v_cmp_gt_f64;
761 break;
762 case aco_opcode::v_cmp_ge_f64:
763 op = aco_opcode::v_cmp_le_f64;
764 break;
765 case aco_opcode::v_cmp_lt_i64:
766 op = aco_opcode::v_cmp_gt_i64;
767 break;
768 case aco_opcode::v_cmp_ge_i64:
769 op = aco_opcode::v_cmp_le_i64;
770 break;
771 case aco_opcode::v_cmp_lt_u64:
772 op = aco_opcode::v_cmp_gt_u64;
773 break;
774 case aco_opcode::v_cmp_ge_u64:
775 op = aco_opcode::v_cmp_le_u64;
776 break;
777 default: /* eq and ne are commutative */
778 break;
779 }
780 Temp t = src0;
781 src0 = src1;
782 src1 = t;
783 } else {
784 src1 = as_vgpr(ctx, src1);
785 }
786 }
787
788 Builder bld(ctx->program, ctx->block);
789 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
790 }
791
792 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
793 {
794 Temp src0 = get_alu_src(ctx, instr->src[0]);
795 Temp src1 = get_alu_src(ctx, instr->src[1]);
796 Builder bld(ctx->program, ctx->block);
797
798 assert(dst.regClass() == bld.lm);
799 assert(src0.type() == RegType::sgpr);
800 assert(src1.type() == RegType::sgpr);
801 assert(src0.regClass() == src1.regClass());
802
803 /* Emit the SALU comparison instruction */
804 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
805 /* Turn the result into a per-lane bool */
806 bool_to_vector_condition(ctx, cmp, dst);
807 }
808
809 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
810 aco_opcode v16_op, aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
811 {
812 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : instr->src[0].src.ssa->bit_size == 32 ? s32_op : aco_opcode::num_opcodes;
813 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : instr->src[0].src.ssa->bit_size == 32 ? v32_op : v16_op;
814 bool use_valu = s_op == aco_opcode::num_opcodes ||
815 nir_dest_is_divergent(instr->dest.dest) ||
816 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
817 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
818 aco_opcode op = use_valu ? v_op : s_op;
819 assert(op != aco_opcode::num_opcodes);
820 assert(dst.regClass() == ctx->program->lane_mask);
821
822 if (use_valu)
823 emit_vopc_instruction(ctx, instr, op, dst);
824 else
825 emit_sopc_instruction(ctx, instr, op, dst);
826 }
827
828 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
829 {
830 Builder bld(ctx->program, ctx->block);
831 Temp src0 = get_alu_src(ctx, instr->src[0]);
832 Temp src1 = get_alu_src(ctx, instr->src[1]);
833
834 assert(dst.regClass() == bld.lm);
835 assert(src0.regClass() == bld.lm);
836 assert(src1.regClass() == bld.lm);
837
838 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
839 }
840
841 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
842 {
843 Builder bld(ctx->program, ctx->block);
844 Temp cond = get_alu_src(ctx, instr->src[0]);
845 Temp then = get_alu_src(ctx, instr->src[1]);
846 Temp els = get_alu_src(ctx, instr->src[2]);
847
848 assert(cond.regClass() == bld.lm);
849
850 if (dst.type() == RegType::vgpr) {
851 aco_ptr<Instruction> bcsel;
852 if (dst.size() == 1) {
853 then = as_vgpr(ctx, then);
854 els = as_vgpr(ctx, els);
855
856 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
857 } else if (dst.size() == 2) {
858 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
859 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
860 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
861 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
862
863 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
864 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
865
866 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
867 } else {
868 fprintf(stderr, "Unimplemented NIR instr bit size: ");
869 nir_print_instr(&instr->instr, stderr);
870 fprintf(stderr, "\n");
871 }
872 return;
873 }
874
875 if (instr->dest.dest.ssa.bit_size == 1) {
876 assert(dst.regClass() == bld.lm);
877 assert(then.regClass() == bld.lm);
878 assert(els.regClass() == bld.lm);
879 }
880
881 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
882 if (dst.regClass() == s1 || dst.regClass() == s2) {
883 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
884 assert(dst.size() == then.size());
885 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
886 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
887 } else {
888 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
889 nir_print_instr(&instr->instr, stderr);
890 fprintf(stderr, "\n");
891 }
892 return;
893 }
894
895 /* divergent boolean bcsel
896 * this implements bcsel on bools: dst = s0 ? s1 : s2
897 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
898 assert(instr->dest.dest.ssa.bit_size == 1);
899
900 if (cond.id() != then.id())
901 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
902
903 if (cond.id() == els.id())
904 bld.sop1(Builder::s_mov, Definition(dst), then);
905 else
906 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
907 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
908 }
909
910 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
911 aco_opcode op, uint32_t undo)
912 {
913 /* multiply by 16777216 to handle denormals */
914 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
915 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
916 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
917 scaled = bld.vop1(op, bld.def(v1), scaled);
918 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
919
920 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
921
922 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
923 }
924
925 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
926 {
927 if (ctx->block->fp_mode.denorm32 == 0) {
928 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
929 return;
930 }
931
932 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
933 }
934
935 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
936 {
937 if (ctx->block->fp_mode.denorm32 == 0) {
938 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
939 return;
940 }
941
942 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
943 }
944
945 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
946 {
947 if (ctx->block->fp_mode.denorm32 == 0) {
948 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
949 return;
950 }
951
952 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
953 }
954
955 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
956 {
957 if (ctx->block->fp_mode.denorm32 == 0) {
958 bld.vop1(aco_opcode::v_log_f32, dst, val);
959 return;
960 }
961
962 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
963 }
964
965 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
966 {
967 if (ctx->options->chip_class >= GFX7)
968 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
969
970 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
971 /* TODO: create more efficient code! */
972 if (val.type() == RegType::sgpr)
973 val = as_vgpr(ctx, val);
974
975 /* Split the input value. */
976 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
977 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
978
979 /* Extract the exponent and compute the unbiased value. */
980 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
981 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
982
983 /* Extract the fractional part. */
984 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
985 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
986
987 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
988 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
989
990 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
991 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
992 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
993 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
994 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
995
996 /* Get the sign bit. */
997 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
998
999 /* Decide the operation to apply depending on the unbiased exponent. */
1000 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
1001 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
1002 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
1003 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
1004 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
1005 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
1006
1007 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
1008 }
1009
1010 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
1011 {
1012 if (ctx->options->chip_class >= GFX7)
1013 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
1014
1015 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1016 * lowered at NIR level for precision reasons). */
1017 Temp src0 = as_vgpr(ctx, val);
1018
1019 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
1020 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
1021
1022 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
1023 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
1024 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
1025
1026 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
1027 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
1028 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
1029 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
1030
1031 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
1032 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
1033
1034 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
1035
1036 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
1037 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
1038
1039 return add->definitions[0].getTemp();
1040 }
1041
1042 Temp convert_int(isel_context *ctx, Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
1043 if (!dst.id()) {
1044 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
1045 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
1046 else
1047 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
1048 }
1049
1050 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
1051 return bld.copy(Definition(dst), src);
1052 else if (dst.bytes() < src.bytes())
1053 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
1054
1055 Temp tmp = dst;
1056 if (dst_bits == 64)
1057 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
1058
1059 if (tmp == src) {
1060 } else if (src.regClass() == s1) {
1061 if (is_signed)
1062 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
1063 else
1064 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
1065 } else if (ctx->options->chip_class >= GFX8) {
1066 assert(src_bits != 8 || src.regClass() == v1b);
1067 assert(src_bits != 16 || src.regClass() == v2b);
1068 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
1069 sdwa->operands[0] = Operand(src);
1070 sdwa->definitions[0] = Definition(tmp);
1071 if (is_signed)
1072 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
1073 else
1074 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
1075 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
1076 bld.insert(std::move(sdwa));
1077 } else {
1078 assert(ctx->options->chip_class == GFX6 || ctx->options->chip_class == GFX7);
1079 aco_opcode opcode = is_signed ? aco_opcode::v_bfe_i32 : aco_opcode::v_bfe_u32;
1080 bld.vop3(opcode, Definition(tmp), src, Operand(0u), Operand(src_bits == 8 ? 8u : 16u));
1081 }
1082
1083 if (dst_bits == 64) {
1084 if (is_signed && dst.regClass() == s2) {
1085 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
1086 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1087 } else if (is_signed && dst.regClass() == v2) {
1088 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
1089 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
1090 } else {
1091 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
1092 }
1093 }
1094
1095 return dst;
1096 }
1097
1098 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1099 {
1100 if (!instr->dest.dest.is_ssa) {
1101 fprintf(stderr, "nir alu dst not in ssa: ");
1102 nir_print_instr(&instr->instr, stderr);
1103 fprintf(stderr, "\n");
1104 abort();
1105 }
1106 Builder bld(ctx->program, ctx->block);
1107 bld.is_precise = instr->exact;
1108 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1109 switch(instr->op) {
1110 case nir_op_vec2:
1111 case nir_op_vec3:
1112 case nir_op_vec4: {
1113 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1114 unsigned num = instr->dest.dest.ssa.num_components;
1115 for (unsigned i = 0; i < num; ++i)
1116 elems[i] = get_alu_src(ctx, instr->src[i]);
1117
1118 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1119 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1120 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1121 for (unsigned i = 0; i < num; ++i) {
1122 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1123 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1124 else
1125 vec->operands[i] = Operand{elems[i]};
1126 }
1127 vec->definitions[0] = Definition(dst);
1128 ctx->block->instructions.emplace_back(std::move(vec));
1129 ctx->allocated_vec.emplace(dst.id(), elems);
1130 } else {
1131 // TODO: that is a bit suboptimal..
1132 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1133 for (unsigned i = 0; i < num - 1; ++i)
1134 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1135 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1136 for (unsigned i = 0; i < num; ++i) {
1137 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1138 if (bit % 32 == 0) {
1139 elems[bit / 32] = elems[i];
1140 } else {
1141 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1142 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1143 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1144 }
1145 }
1146 if (dst.size() == 1)
1147 bld.copy(Definition(dst), elems[0]);
1148 else
1149 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1150 }
1151 break;
1152 }
1153 case nir_op_mov: {
1154 Temp src = get_alu_src(ctx, instr->src[0]);
1155 aco_ptr<Instruction> mov;
1156 if (dst.type() == RegType::sgpr) {
1157 if (src.type() == RegType::vgpr)
1158 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1159 else if (src.regClass() == s1)
1160 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1161 else if (src.regClass() == s2)
1162 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1163 else
1164 unreachable("wrong src register class for nir_op_imov");
1165 } else {
1166 if (dst.regClass() == v1)
1167 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1168 else if (dst.regClass() == v1b ||
1169 dst.regClass() == v2b ||
1170 dst.regClass() == v2)
1171 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1172 else
1173 unreachable("wrong src register class for nir_op_imov");
1174 }
1175 break;
1176 }
1177 case nir_op_inot: {
1178 Temp src = get_alu_src(ctx, instr->src[0]);
1179 if (instr->dest.dest.ssa.bit_size == 1) {
1180 assert(src.regClass() == bld.lm);
1181 assert(dst.regClass() == bld.lm);
1182 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1183 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1184 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1185 } else if (dst.regClass() == v1) {
1186 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1187 } else if (dst.regClass() == v2) {
1188 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
1189 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
1190 lo = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), lo);
1191 hi = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), hi);
1192 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
1193 } else if (dst.type() == RegType::sgpr) {
1194 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1195 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1196 } else {
1197 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1198 nir_print_instr(&instr->instr, stderr);
1199 fprintf(stderr, "\n");
1200 }
1201 break;
1202 }
1203 case nir_op_ineg: {
1204 Temp src = get_alu_src(ctx, instr->src[0]);
1205 if (dst.regClass() == v1) {
1206 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1207 } else if (dst.regClass() == s1) {
1208 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1209 } else if (dst.size() == 2) {
1210 Temp src0 = bld.tmp(dst.type(), 1);
1211 Temp src1 = bld.tmp(dst.type(), 1);
1212 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1213
1214 if (dst.regClass() == s2) {
1215 Temp carry = bld.tmp(s1);
1216 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1217 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1218 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1219 } else {
1220 Temp lower = bld.tmp(v1);
1221 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1222 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1223 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1224 }
1225 } else {
1226 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1227 nir_print_instr(&instr->instr, stderr);
1228 fprintf(stderr, "\n");
1229 }
1230 break;
1231 }
1232 case nir_op_iabs: {
1233 if (dst.regClass() == s1) {
1234 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1235 } else if (dst.regClass() == v1) {
1236 Temp src = get_alu_src(ctx, instr->src[0]);
1237 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1238 } else {
1239 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1240 nir_print_instr(&instr->instr, stderr);
1241 fprintf(stderr, "\n");
1242 }
1243 break;
1244 }
1245 case nir_op_isign: {
1246 Temp src = get_alu_src(ctx, instr->src[0]);
1247 if (dst.regClass() == s1) {
1248 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1249 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1250 } else if (dst.regClass() == s2) {
1251 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1252 Temp neqz;
1253 if (ctx->program->chip_class >= GFX8)
1254 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1255 else
1256 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1257 /* SCC gets zero-extended to 64 bit */
1258 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1259 } else if (dst.regClass() == v1) {
1260 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1261 } else if (dst.regClass() == v2) {
1262 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1263 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1264 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1265 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1266 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1267 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1268 } else {
1269 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1270 nir_print_instr(&instr->instr, stderr);
1271 fprintf(stderr, "\n");
1272 }
1273 break;
1274 }
1275 case nir_op_imax: {
1276 if (dst.regClass() == v1) {
1277 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1278 } else if (dst.regClass() == s1) {
1279 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1280 } else {
1281 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1282 nir_print_instr(&instr->instr, stderr);
1283 fprintf(stderr, "\n");
1284 }
1285 break;
1286 }
1287 case nir_op_umax: {
1288 if (dst.regClass() == v1) {
1289 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1290 } else if (dst.regClass() == s1) {
1291 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1292 } else {
1293 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1294 nir_print_instr(&instr->instr, stderr);
1295 fprintf(stderr, "\n");
1296 }
1297 break;
1298 }
1299 case nir_op_imin: {
1300 if (dst.regClass() == v1) {
1301 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1302 } else if (dst.regClass() == s1) {
1303 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1304 } else {
1305 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1306 nir_print_instr(&instr->instr, stderr);
1307 fprintf(stderr, "\n");
1308 }
1309 break;
1310 }
1311 case nir_op_umin: {
1312 if (dst.regClass() == v1) {
1313 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1314 } else if (dst.regClass() == s1) {
1315 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1316 } else {
1317 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1318 nir_print_instr(&instr->instr, stderr);
1319 fprintf(stderr, "\n");
1320 }
1321 break;
1322 }
1323 case nir_op_ior: {
1324 if (instr->dest.dest.ssa.bit_size == 1) {
1325 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1326 } else if (dst.regClass() == v1) {
1327 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1328 } else if (dst.regClass() == v2) {
1329 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_or_b32, dst);
1330 } else if (dst.regClass() == s1) {
1331 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1332 } else if (dst.regClass() == s2) {
1333 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1334 } else {
1335 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1336 nir_print_instr(&instr->instr, stderr);
1337 fprintf(stderr, "\n");
1338 }
1339 break;
1340 }
1341 case nir_op_iand: {
1342 if (instr->dest.dest.ssa.bit_size == 1) {
1343 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1344 } else if (dst.regClass() == v1) {
1345 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1346 } else if (dst.regClass() == v2) {
1347 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_and_b32, dst);
1348 } else if (dst.regClass() == s1) {
1349 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1350 } else if (dst.regClass() == s2) {
1351 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1352 } else {
1353 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1354 nir_print_instr(&instr->instr, stderr);
1355 fprintf(stderr, "\n");
1356 }
1357 break;
1358 }
1359 case nir_op_ixor: {
1360 if (instr->dest.dest.ssa.bit_size == 1) {
1361 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1362 } else if (dst.regClass() == v1) {
1363 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1364 } else if (dst.regClass() == v2) {
1365 emit_vop2_instruction_logic64(ctx, instr, aco_opcode::v_xor_b32, dst);
1366 } else if (dst.regClass() == s1) {
1367 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1368 } else if (dst.regClass() == s2) {
1369 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1370 } else {
1371 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1372 nir_print_instr(&instr->instr, stderr);
1373 fprintf(stderr, "\n");
1374 }
1375 break;
1376 }
1377 case nir_op_ushr: {
1378 if (dst.regClass() == v1) {
1379 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1380 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1381 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1382 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1383 } else if (dst.regClass() == v2) {
1384 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1385 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1386 } else if (dst.regClass() == s2) {
1387 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1388 } else if (dst.regClass() == s1) {
1389 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1390 } else {
1391 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1392 nir_print_instr(&instr->instr, stderr);
1393 fprintf(stderr, "\n");
1394 }
1395 break;
1396 }
1397 case nir_op_ishl: {
1398 if (dst.regClass() == v1) {
1399 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1400 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1401 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1402 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1403 } else if (dst.regClass() == v2) {
1404 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1405 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1406 } else if (dst.regClass() == s1) {
1407 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1408 } else if (dst.regClass() == s2) {
1409 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1410 } else {
1411 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1412 nir_print_instr(&instr->instr, stderr);
1413 fprintf(stderr, "\n");
1414 }
1415 break;
1416 }
1417 case nir_op_ishr: {
1418 if (dst.regClass() == v1) {
1419 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1420 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1421 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1422 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1423 } else if (dst.regClass() == v2) {
1424 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1425 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1426 } else if (dst.regClass() == s1) {
1427 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1428 } else if (dst.regClass() == s2) {
1429 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1430 } else {
1431 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1432 nir_print_instr(&instr->instr, stderr);
1433 fprintf(stderr, "\n");
1434 }
1435 break;
1436 }
1437 case nir_op_find_lsb: {
1438 Temp src = get_alu_src(ctx, instr->src[0]);
1439 if (src.regClass() == s1) {
1440 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1441 } else if (src.regClass() == v1) {
1442 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1443 } else if (src.regClass() == s2) {
1444 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1445 } else {
1446 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1447 nir_print_instr(&instr->instr, stderr);
1448 fprintf(stderr, "\n");
1449 }
1450 break;
1451 }
1452 case nir_op_ufind_msb:
1453 case nir_op_ifind_msb: {
1454 Temp src = get_alu_src(ctx, instr->src[0]);
1455 if (src.regClass() == s1 || src.regClass() == s2) {
1456 aco_opcode op = src.regClass() == s2 ?
1457 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1458 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1459 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1460
1461 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1462 Operand(src.size() * 32u - 1u), msb_rev);
1463 Temp msb = sub.def(0).getTemp();
1464 Temp carry = sub.def(1).getTemp();
1465
1466 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1467 } else if (src.regClass() == v1) {
1468 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1469 Temp msb_rev = bld.tmp(v1);
1470 emit_vop1_instruction(ctx, instr, op, msb_rev);
1471 Temp msb = bld.tmp(v1);
1472 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1473 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1474 } else {
1475 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1476 nir_print_instr(&instr->instr, stderr);
1477 fprintf(stderr, "\n");
1478 }
1479 break;
1480 }
1481 case nir_op_bitfield_reverse: {
1482 if (dst.regClass() == s1) {
1483 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1484 } else if (dst.regClass() == v1) {
1485 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1486 } else {
1487 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1488 nir_print_instr(&instr->instr, stderr);
1489 fprintf(stderr, "\n");
1490 }
1491 break;
1492 }
1493 case nir_op_iadd: {
1494 if (dst.regClass() == s1) {
1495 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1496 break;
1497 }
1498
1499 Temp src0 = get_alu_src(ctx, instr->src[0]);
1500 Temp src1 = get_alu_src(ctx, instr->src[1]);
1501 if (dst.regClass() == v1) {
1502 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1503 break;
1504 }
1505
1506 assert(src0.size() == 2 && src1.size() == 2);
1507 Temp src00 = bld.tmp(src0.type(), 1);
1508 Temp src01 = bld.tmp(dst.type(), 1);
1509 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1510 Temp src10 = bld.tmp(src1.type(), 1);
1511 Temp src11 = bld.tmp(dst.type(), 1);
1512 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1513
1514 if (dst.regClass() == s2) {
1515 Temp carry = bld.tmp(s1);
1516 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1517 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1518 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1519 } else if (dst.regClass() == v2) {
1520 Temp dst0 = bld.tmp(v1);
1521 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1522 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1523 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1524 } else {
1525 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1526 nir_print_instr(&instr->instr, stderr);
1527 fprintf(stderr, "\n");
1528 }
1529 break;
1530 }
1531 case nir_op_uadd_sat: {
1532 Temp src0 = get_alu_src(ctx, instr->src[0]);
1533 Temp src1 = get_alu_src(ctx, instr->src[1]);
1534 if (dst.regClass() == s1) {
1535 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1536 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1537 src0, src1);
1538 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1539 } else if (dst.regClass() == v1) {
1540 if (ctx->options->chip_class >= GFX9) {
1541 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1542 add->operands[0] = Operand(src0);
1543 add->operands[1] = Operand(src1);
1544 add->definitions[0] = Definition(dst);
1545 add->clamp = 1;
1546 ctx->block->instructions.emplace_back(std::move(add));
1547 } else {
1548 if (src1.regClass() != v1)
1549 std::swap(src0, src1);
1550 assert(src1.regClass() == v1);
1551 Temp tmp = bld.tmp(v1);
1552 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1553 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1554 }
1555 } else {
1556 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1557 nir_print_instr(&instr->instr, stderr);
1558 fprintf(stderr, "\n");
1559 }
1560 break;
1561 }
1562 case nir_op_uadd_carry: {
1563 Temp src0 = get_alu_src(ctx, instr->src[0]);
1564 Temp src1 = get_alu_src(ctx, instr->src[1]);
1565 if (dst.regClass() == s1) {
1566 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1567 break;
1568 }
1569 if (dst.regClass() == v1) {
1570 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1571 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1572 break;
1573 }
1574
1575 Temp src00 = bld.tmp(src0.type(), 1);
1576 Temp src01 = bld.tmp(dst.type(), 1);
1577 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1578 Temp src10 = bld.tmp(src1.type(), 1);
1579 Temp src11 = bld.tmp(dst.type(), 1);
1580 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1581 if (dst.regClass() == s2) {
1582 Temp carry = bld.tmp(s1);
1583 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1584 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1585 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1586 } else if (dst.regClass() == v2) {
1587 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1588 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1589 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1590 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1591 } else {
1592 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1593 nir_print_instr(&instr->instr, stderr);
1594 fprintf(stderr, "\n");
1595 }
1596 break;
1597 }
1598 case nir_op_isub: {
1599 if (dst.regClass() == s1) {
1600 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1601 break;
1602 }
1603
1604 Temp src0 = get_alu_src(ctx, instr->src[0]);
1605 Temp src1 = get_alu_src(ctx, instr->src[1]);
1606 if (dst.regClass() == v1) {
1607 bld.vsub32(Definition(dst), src0, src1);
1608 break;
1609 }
1610
1611 Temp src00 = bld.tmp(src0.type(), 1);
1612 Temp src01 = bld.tmp(dst.type(), 1);
1613 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1614 Temp src10 = bld.tmp(src1.type(), 1);
1615 Temp src11 = bld.tmp(dst.type(), 1);
1616 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1617 if (dst.regClass() == s2) {
1618 Temp carry = bld.tmp(s1);
1619 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1620 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1621 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1622 } else if (dst.regClass() == v2) {
1623 Temp lower = bld.tmp(v1);
1624 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1625 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1626 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1627 } else {
1628 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1629 nir_print_instr(&instr->instr, stderr);
1630 fprintf(stderr, "\n");
1631 }
1632 break;
1633 }
1634 case nir_op_usub_borrow: {
1635 Temp src0 = get_alu_src(ctx, instr->src[0]);
1636 Temp src1 = get_alu_src(ctx, instr->src[1]);
1637 if (dst.regClass() == s1) {
1638 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1639 break;
1640 } else if (dst.regClass() == v1) {
1641 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1642 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1643 break;
1644 }
1645
1646 Temp src00 = bld.tmp(src0.type(), 1);
1647 Temp src01 = bld.tmp(dst.type(), 1);
1648 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1649 Temp src10 = bld.tmp(src1.type(), 1);
1650 Temp src11 = bld.tmp(dst.type(), 1);
1651 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1652 if (dst.regClass() == s2) {
1653 Temp borrow = bld.tmp(s1);
1654 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1655 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1656 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1657 } else if (dst.regClass() == v2) {
1658 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1659 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1660 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1661 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1662 } else {
1663 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1664 nir_print_instr(&instr->instr, stderr);
1665 fprintf(stderr, "\n");
1666 }
1667 break;
1668 }
1669 case nir_op_imul: {
1670 if (dst.regClass() == v1) {
1671 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1672 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1673 } else if (dst.regClass() == s1) {
1674 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1675 } else {
1676 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1677 nir_print_instr(&instr->instr, stderr);
1678 fprintf(stderr, "\n");
1679 }
1680 break;
1681 }
1682 case nir_op_umul_high: {
1683 if (dst.regClass() == v1) {
1684 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1685 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1686 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1687 } else if (dst.regClass() == s1) {
1688 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1689 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1690 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1691 } else {
1692 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1693 nir_print_instr(&instr->instr, stderr);
1694 fprintf(stderr, "\n");
1695 }
1696 break;
1697 }
1698 case nir_op_imul_high: {
1699 if (dst.regClass() == v1) {
1700 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1701 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1702 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1703 } else if (dst.regClass() == s1) {
1704 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1705 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1706 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1707 } else {
1708 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1709 nir_print_instr(&instr->instr, stderr);
1710 fprintf(stderr, "\n");
1711 }
1712 break;
1713 }
1714 case nir_op_fmul: {
1715 Temp src0 = get_alu_src(ctx, instr->src[0]);
1716 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1717 if (dst.regClass() == v2b) {
1718 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1719 } else if (dst.regClass() == v1) {
1720 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1721 } else if (dst.regClass() == v2) {
1722 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1723 } else {
1724 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1725 nir_print_instr(&instr->instr, stderr);
1726 fprintf(stderr, "\n");
1727 }
1728 break;
1729 }
1730 case nir_op_fadd: {
1731 Temp src0 = get_alu_src(ctx, instr->src[0]);
1732 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1733 if (dst.regClass() == v2b) {
1734 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1735 } else if (dst.regClass() == v1) {
1736 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1737 } else if (dst.regClass() == v2) {
1738 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
1739 } else {
1740 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1741 nir_print_instr(&instr->instr, stderr);
1742 fprintf(stderr, "\n");
1743 }
1744 break;
1745 }
1746 case nir_op_fsub: {
1747 Temp src0 = get_alu_src(ctx, instr->src[0]);
1748 Temp src1 = get_alu_src(ctx, instr->src[1]);
1749 if (dst.regClass() == v2b) {
1750 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1751 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1752 else
1753 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1754 } else if (dst.regClass() == v1) {
1755 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1756 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1757 else
1758 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1759 } else if (dst.regClass() == v2) {
1760 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1761 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1762 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1763 sub->neg[1] = true;
1764 } else {
1765 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1766 nir_print_instr(&instr->instr, stderr);
1767 fprintf(stderr, "\n");
1768 }
1769 break;
1770 }
1771 case nir_op_fmax: {
1772 Temp src0 = get_alu_src(ctx, instr->src[0]);
1773 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1774 if (dst.regClass() == v2b) {
1775 // TODO: check fp_mode.must_flush_denorms16_64
1776 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1777 } else if (dst.regClass() == v1) {
1778 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1779 } else if (dst.regClass() == v2) {
1780 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1781 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1782 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1783 } else {
1784 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1785 }
1786 } else {
1787 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1788 nir_print_instr(&instr->instr, stderr);
1789 fprintf(stderr, "\n");
1790 }
1791 break;
1792 }
1793 case nir_op_fmin: {
1794 Temp src0 = get_alu_src(ctx, instr->src[0]);
1795 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1796 if (dst.regClass() == v2b) {
1797 // TODO: check fp_mode.must_flush_denorms16_64
1798 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1799 } else if (dst.regClass() == v1) {
1800 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1801 } else if (dst.regClass() == v2) {
1802 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1803 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1804 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1805 } else {
1806 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1807 }
1808 } else {
1809 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1810 nir_print_instr(&instr->instr, stderr);
1811 fprintf(stderr, "\n");
1812 }
1813 break;
1814 }
1815 case nir_op_fmax3: {
1816 if (dst.regClass() == v2b) {
1817 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1818 } else if (dst.regClass() == v1) {
1819 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1820 } else {
1821 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1822 nir_print_instr(&instr->instr, stderr);
1823 fprintf(stderr, "\n");
1824 }
1825 break;
1826 }
1827 case nir_op_fmin3: {
1828 if (dst.regClass() == v2b) {
1829 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1830 } else if (dst.regClass() == v1) {
1831 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1832 } else {
1833 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1834 nir_print_instr(&instr->instr, stderr);
1835 fprintf(stderr, "\n");
1836 }
1837 break;
1838 }
1839 case nir_op_fmed3: {
1840 if (dst.regClass() == v2b) {
1841 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1842 } else if (dst.regClass() == v1) {
1843 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1844 } else {
1845 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1846 nir_print_instr(&instr->instr, stderr);
1847 fprintf(stderr, "\n");
1848 }
1849 break;
1850 }
1851 case nir_op_umax3: {
1852 if (dst.size() == 1) {
1853 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1854 } else {
1855 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1856 nir_print_instr(&instr->instr, stderr);
1857 fprintf(stderr, "\n");
1858 }
1859 break;
1860 }
1861 case nir_op_umin3: {
1862 if (dst.size() == 1) {
1863 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1864 } else {
1865 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1866 nir_print_instr(&instr->instr, stderr);
1867 fprintf(stderr, "\n");
1868 }
1869 break;
1870 }
1871 case nir_op_umed3: {
1872 if (dst.size() == 1) {
1873 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1874 } else {
1875 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1876 nir_print_instr(&instr->instr, stderr);
1877 fprintf(stderr, "\n");
1878 }
1879 break;
1880 }
1881 case nir_op_imax3: {
1882 if (dst.size() == 1) {
1883 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1884 } else {
1885 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1886 nir_print_instr(&instr->instr, stderr);
1887 fprintf(stderr, "\n");
1888 }
1889 break;
1890 }
1891 case nir_op_imin3: {
1892 if (dst.size() == 1) {
1893 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1894 } else {
1895 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1896 nir_print_instr(&instr->instr, stderr);
1897 fprintf(stderr, "\n");
1898 }
1899 break;
1900 }
1901 case nir_op_imed3: {
1902 if (dst.size() == 1) {
1903 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1904 } else {
1905 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1906 nir_print_instr(&instr->instr, stderr);
1907 fprintf(stderr, "\n");
1908 }
1909 break;
1910 }
1911 case nir_op_cube_face_coord: {
1912 Temp in = get_alu_src(ctx, instr->src[0], 3);
1913 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1914 emit_extract_vector(ctx, in, 1, v1),
1915 emit_extract_vector(ctx, in, 2, v1) };
1916 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1917 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1918 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1919 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1920 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1921 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1922 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1923 break;
1924 }
1925 case nir_op_cube_face_index: {
1926 Temp in = get_alu_src(ctx, instr->src[0], 3);
1927 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1928 emit_extract_vector(ctx, in, 1, v1),
1929 emit_extract_vector(ctx, in, 2, v1) };
1930 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1931 break;
1932 }
1933 case nir_op_bcsel: {
1934 emit_bcsel(ctx, instr, dst);
1935 break;
1936 }
1937 case nir_op_frsq: {
1938 Temp src = get_alu_src(ctx, instr->src[0]);
1939 if (dst.regClass() == v2b) {
1940 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1941 } else if (dst.regClass() == v1) {
1942 emit_rsq(ctx, bld, Definition(dst), src);
1943 } else if (dst.regClass() == v2) {
1944 /* Lowered at NIR level for precision reasons. */
1945 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1946 } else {
1947 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1948 nir_print_instr(&instr->instr, stderr);
1949 fprintf(stderr, "\n");
1950 }
1951 break;
1952 }
1953 case nir_op_fneg: {
1954 Temp src = get_alu_src(ctx, instr->src[0]);
1955 if (dst.regClass() == v2b) {
1956 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1957 } else if (dst.regClass() == v1) {
1958 if (ctx->block->fp_mode.must_flush_denorms32)
1959 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1960 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1961 } else if (dst.regClass() == v2) {
1962 if (ctx->block->fp_mode.must_flush_denorms16_64)
1963 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1964 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1965 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1966 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1967 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1968 } else {
1969 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1970 nir_print_instr(&instr->instr, stderr);
1971 fprintf(stderr, "\n");
1972 }
1973 break;
1974 }
1975 case nir_op_fabs: {
1976 Temp src = get_alu_src(ctx, instr->src[0]);
1977 if (dst.regClass() == v2b) {
1978 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1979 } else if (dst.regClass() == v1) {
1980 if (ctx->block->fp_mode.must_flush_denorms32)
1981 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1982 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1983 } else if (dst.regClass() == v2) {
1984 if (ctx->block->fp_mode.must_flush_denorms16_64)
1985 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1986 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1987 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1988 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1989 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1990 } else {
1991 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1992 nir_print_instr(&instr->instr, stderr);
1993 fprintf(stderr, "\n");
1994 }
1995 break;
1996 }
1997 case nir_op_fsat: {
1998 Temp src = get_alu_src(ctx, instr->src[0]);
1999 if (dst.regClass() == v2b) {
2000 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src);
2001 } else if (dst.regClass() == v1) {
2002 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2003 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
2004 // TODO: confirm that this holds under any circumstances
2005 } else if (dst.regClass() == v2) {
2006 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
2007 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
2008 vop3->clamp = true;
2009 } else {
2010 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2011 nir_print_instr(&instr->instr, stderr);
2012 fprintf(stderr, "\n");
2013 }
2014 break;
2015 }
2016 case nir_op_flog2: {
2017 Temp src = get_alu_src(ctx, instr->src[0]);
2018 if (dst.regClass() == v2b) {
2019 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
2020 } else if (dst.regClass() == v1) {
2021 emit_log2(ctx, bld, Definition(dst), src);
2022 } else {
2023 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2024 nir_print_instr(&instr->instr, stderr);
2025 fprintf(stderr, "\n");
2026 }
2027 break;
2028 }
2029 case nir_op_frcp: {
2030 Temp src = get_alu_src(ctx, instr->src[0]);
2031 if (dst.regClass() == v2b) {
2032 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
2033 } else if (dst.regClass() == v1) {
2034 emit_rcp(ctx, bld, Definition(dst), src);
2035 } else if (dst.regClass() == v2) {
2036 /* Lowered at NIR level for precision reasons. */
2037 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
2038 } else {
2039 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2040 nir_print_instr(&instr->instr, stderr);
2041 fprintf(stderr, "\n");
2042 }
2043 break;
2044 }
2045 case nir_op_fexp2: {
2046 if (dst.regClass() == v2b) {
2047 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
2048 } else if (dst.regClass() == v1) {
2049 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
2050 } else {
2051 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2052 nir_print_instr(&instr->instr, stderr);
2053 fprintf(stderr, "\n");
2054 }
2055 break;
2056 }
2057 case nir_op_fsqrt: {
2058 Temp src = get_alu_src(ctx, instr->src[0]);
2059 if (dst.regClass() == v2b) {
2060 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
2061 } else if (dst.regClass() == v1) {
2062 emit_sqrt(ctx, bld, Definition(dst), src);
2063 } else if (dst.regClass() == v2) {
2064 /* Lowered at NIR level for precision reasons. */
2065 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
2066 } else {
2067 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2068 nir_print_instr(&instr->instr, stderr);
2069 fprintf(stderr, "\n");
2070 }
2071 break;
2072 }
2073 case nir_op_ffract: {
2074 if (dst.regClass() == v2b) {
2075 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
2076 } else if (dst.regClass() == v1) {
2077 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
2078 } else if (dst.regClass() == v2) {
2079 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
2080 } else {
2081 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2082 nir_print_instr(&instr->instr, stderr);
2083 fprintf(stderr, "\n");
2084 }
2085 break;
2086 }
2087 case nir_op_ffloor: {
2088 Temp src = get_alu_src(ctx, instr->src[0]);
2089 if (dst.regClass() == v2b) {
2090 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
2091 } else if (dst.regClass() == v1) {
2092 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
2093 } else if (dst.regClass() == v2) {
2094 emit_floor_f64(ctx, bld, Definition(dst), src);
2095 } else {
2096 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2097 nir_print_instr(&instr->instr, stderr);
2098 fprintf(stderr, "\n");
2099 }
2100 break;
2101 }
2102 case nir_op_fceil: {
2103 Temp src0 = get_alu_src(ctx, instr->src[0]);
2104 if (dst.regClass() == v2b) {
2105 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
2106 } else if (dst.regClass() == v1) {
2107 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
2108 } else if (dst.regClass() == v2) {
2109 if (ctx->options->chip_class >= GFX7) {
2110 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
2111 } else {
2112 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2113 /* trunc = trunc(src0)
2114 * if (src0 > 0.0 && src0 != trunc)
2115 * trunc += 1.0
2116 */
2117 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2118 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2119 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2120 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2121 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);
2122 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2123 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2124 }
2125 } else {
2126 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2127 nir_print_instr(&instr->instr, stderr);
2128 fprintf(stderr, "\n");
2129 }
2130 break;
2131 }
2132 case nir_op_ftrunc: {
2133 Temp src = get_alu_src(ctx, instr->src[0]);
2134 if (dst.regClass() == v2b) {
2135 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2136 } else if (dst.regClass() == v1) {
2137 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2138 } else if (dst.regClass() == v2) {
2139 emit_trunc_f64(ctx, bld, Definition(dst), src);
2140 } else {
2141 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2142 nir_print_instr(&instr->instr, stderr);
2143 fprintf(stderr, "\n");
2144 }
2145 break;
2146 }
2147 case nir_op_fround_even: {
2148 Temp src0 = get_alu_src(ctx, instr->src[0]);
2149 if (dst.regClass() == v2b) {
2150 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2151 } else if (dst.regClass() == v1) {
2152 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2153 } else if (dst.regClass() == v2) {
2154 if (ctx->options->chip_class >= GFX7) {
2155 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2156 } else {
2157 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2158 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2159 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2160
2161 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2162 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));
2163 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));
2164 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));
2165 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2166 tmp = sub->definitions[0].getTemp();
2167
2168 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2169 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2170 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2171 Temp cond = vop3->definitions[0].getTemp();
2172
2173 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2174 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2175 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2176 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2177
2178 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2179 }
2180 } else {
2181 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2182 nir_print_instr(&instr->instr, stderr);
2183 fprintf(stderr, "\n");
2184 }
2185 break;
2186 }
2187 case nir_op_fsin:
2188 case nir_op_fcos: {
2189 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2190 aco_ptr<Instruction> norm;
2191 if (dst.regClass() == v2b) {
2192 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3118u));
2193 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2194 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2195 bld.vop1(opcode, Definition(dst), tmp);
2196 } else if (dst.regClass() == v1) {
2197 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2198 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2199
2200 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2201 if (ctx->options->chip_class < GFX9)
2202 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2203
2204 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2205 bld.vop1(opcode, Definition(dst), tmp);
2206 } else {
2207 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2208 nir_print_instr(&instr->instr, stderr);
2209 fprintf(stderr, "\n");
2210 }
2211 break;
2212 }
2213 case nir_op_ldexp: {
2214 Temp src0 = get_alu_src(ctx, instr->src[0]);
2215 Temp src1 = get_alu_src(ctx, instr->src[1]);
2216 if (dst.regClass() == v2b) {
2217 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2218 } else if (dst.regClass() == v1) {
2219 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2220 } else if (dst.regClass() == v2) {
2221 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2222 } else {
2223 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2224 nir_print_instr(&instr->instr, stderr);
2225 fprintf(stderr, "\n");
2226 }
2227 break;
2228 }
2229 case nir_op_frexp_sig: {
2230 Temp src = get_alu_src(ctx, instr->src[0]);
2231 if (dst.regClass() == v2b) {
2232 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2233 } else if (dst.regClass() == v1) {
2234 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2235 } else if (dst.regClass() == v2) {
2236 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2237 } else {
2238 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2239 nir_print_instr(&instr->instr, stderr);
2240 fprintf(stderr, "\n");
2241 }
2242 break;
2243 }
2244 case nir_op_frexp_exp: {
2245 Temp src = get_alu_src(ctx, instr->src[0]);
2246 if (instr->src[0].src.ssa->bit_size == 16) {
2247 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2248 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2249 convert_int(ctx, bld, tmp, 8, 32, true, dst);
2250 } else if (instr->src[0].src.ssa->bit_size == 32) {
2251 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2252 } else if (instr->src[0].src.ssa->bit_size == 64) {
2253 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2254 } else {
2255 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2256 nir_print_instr(&instr->instr, stderr);
2257 fprintf(stderr, "\n");
2258 }
2259 break;
2260 }
2261 case nir_op_fsign: {
2262 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2263 if (dst.regClass() == v2b) {
2264 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2265 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2266 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2267 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2268 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2269 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2270 } else if (dst.regClass() == v1) {
2271 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2272 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2273 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2274 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2275 } else if (dst.regClass() == v2) {
2276 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2277 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2278 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2279
2280 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2281 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2282 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2283
2284 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2285 } else {
2286 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2287 nir_print_instr(&instr->instr, stderr);
2288 fprintf(stderr, "\n");
2289 }
2290 break;
2291 }
2292 case nir_op_f2f16:
2293 case nir_op_f2f16_rtne: {
2294 Temp src = get_alu_src(ctx, instr->src[0]);
2295 if (instr->src[0].src.ssa->bit_size == 64)
2296 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2297 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2298 break;
2299 }
2300 case nir_op_f2f16_rtz: {
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 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2305 break;
2306 }
2307 case nir_op_f2f32: {
2308 if (instr->src[0].src.ssa->bit_size == 16) {
2309 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2310 } else if (instr->src[0].src.ssa->bit_size == 64) {
2311 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2312 } else {
2313 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2314 nir_print_instr(&instr->instr, stderr);
2315 fprintf(stderr, "\n");
2316 }
2317 break;
2318 }
2319 case nir_op_f2f64: {
2320 Temp src = get_alu_src(ctx, instr->src[0]);
2321 if (instr->src[0].src.ssa->bit_size == 16)
2322 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2323 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2324 break;
2325 }
2326 case nir_op_i2f16: {
2327 assert(dst.regClass() == v2b);
2328 Temp src = get_alu_src(ctx, instr->src[0]);
2329 if (instr->src[0].src.ssa->bit_size == 8)
2330 src = convert_int(ctx, bld, src, 8, 16, true);
2331 else if (instr->src[0].src.ssa->bit_size == 64)
2332 src = convert_int(ctx, bld, src, 64, 32, false);
2333 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2334 break;
2335 }
2336 case nir_op_i2f32: {
2337 assert(dst.size() == 1);
2338 Temp src = get_alu_src(ctx, instr->src[0]);
2339 if (instr->src[0].src.ssa->bit_size <= 16)
2340 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2341 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2342 break;
2343 }
2344 case nir_op_i2f64: {
2345 if (instr->src[0].src.ssa->bit_size <= 32) {
2346 Temp src = get_alu_src(ctx, instr->src[0]);
2347 if (instr->src[0].src.ssa->bit_size <= 16)
2348 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2349 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2350 } else if (instr->src[0].src.ssa->bit_size == 64) {
2351 Temp src = get_alu_src(ctx, instr->src[0]);
2352 RegClass rc = RegClass(src.type(), 1);
2353 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2354 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2355 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2356 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2357 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2358 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2359
2360 } else {
2361 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2362 nir_print_instr(&instr->instr, stderr);
2363 fprintf(stderr, "\n");
2364 }
2365 break;
2366 }
2367 case nir_op_u2f16: {
2368 assert(dst.regClass() == v2b);
2369 Temp src = get_alu_src(ctx, instr->src[0]);
2370 if (instr->src[0].src.ssa->bit_size == 8)
2371 src = convert_int(ctx, bld, src, 8, 16, false);
2372 else if (instr->src[0].src.ssa->bit_size == 64)
2373 src = convert_int(ctx, bld, src, 64, 32, false);
2374 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2375 break;
2376 }
2377 case nir_op_u2f32: {
2378 assert(dst.size() == 1);
2379 Temp src = get_alu_src(ctx, instr->src[0]);
2380 if (instr->src[0].src.ssa->bit_size == 8) {
2381 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2382 } else {
2383 if (instr->src[0].src.ssa->bit_size == 16)
2384 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2385 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2386 }
2387 break;
2388 }
2389 case nir_op_u2f64: {
2390 if (instr->src[0].src.ssa->bit_size <= 32) {
2391 Temp src = get_alu_src(ctx, instr->src[0]);
2392 if (instr->src[0].src.ssa->bit_size <= 16)
2393 src = convert_int(ctx, bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2394 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2395 } else if (instr->src[0].src.ssa->bit_size == 64) {
2396 Temp src = get_alu_src(ctx, instr->src[0]);
2397 RegClass rc = RegClass(src.type(), 1);
2398 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2399 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2400 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2401 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2402 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2403 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2404 } else {
2405 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2406 nir_print_instr(&instr->instr, stderr);
2407 fprintf(stderr, "\n");
2408 }
2409 break;
2410 }
2411 case nir_op_f2i8:
2412 case nir_op_f2i16: {
2413 if (instr->src[0].src.ssa->bit_size == 16)
2414 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i16_f16, dst);
2415 else if (instr->src[0].src.ssa->bit_size == 32)
2416 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2417 else
2418 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2419 break;
2420 }
2421 case nir_op_f2u8:
2422 case nir_op_f2u16: {
2423 if (instr->src[0].src.ssa->bit_size == 16)
2424 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u16_f16, dst);
2425 else if (instr->src[0].src.ssa->bit_size == 32)
2426 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2427 else
2428 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2429 break;
2430 }
2431 case nir_op_f2i32: {
2432 Temp src = get_alu_src(ctx, instr->src[0]);
2433 if (instr->src[0].src.ssa->bit_size == 16) {
2434 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2435 if (dst.type() == RegType::vgpr) {
2436 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2437 } else {
2438 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2439 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2440 }
2441 } else if (instr->src[0].src.ssa->bit_size == 32) {
2442 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f32, dst);
2443 } else if (instr->src[0].src.ssa->bit_size == 64) {
2444 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_i32_f64, dst);
2445 } else {
2446 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2447 nir_print_instr(&instr->instr, stderr);
2448 fprintf(stderr, "\n");
2449 }
2450 break;
2451 }
2452 case nir_op_f2u32: {
2453 Temp src = get_alu_src(ctx, instr->src[0]);
2454 if (instr->src[0].src.ssa->bit_size == 16) {
2455 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2456 if (dst.type() == RegType::vgpr) {
2457 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2458 } else {
2459 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2460 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2461 }
2462 } else if (instr->src[0].src.ssa->bit_size == 32) {
2463 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f32, dst);
2464 } else if (instr->src[0].src.ssa->bit_size == 64) {
2465 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_u32_f64, dst);
2466 } else {
2467 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2468 nir_print_instr(&instr->instr, stderr);
2469 fprintf(stderr, "\n");
2470 }
2471 break;
2472 }
2473 case nir_op_f2i64: {
2474 Temp src = get_alu_src(ctx, instr->src[0]);
2475 if (instr->src[0].src.ssa->bit_size == 16)
2476 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2477
2478 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2479 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2480 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2481 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2482 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2483 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2484 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2485 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2486 Temp new_exponent = bld.tmp(v1);
2487 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2488 if (ctx->program->chip_class >= GFX8)
2489 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2490 else
2491 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2492 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2493 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2494 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2495 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2496 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2497 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2498 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2499 Temp new_lower = bld.tmp(v1);
2500 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2501 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2502 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2503
2504 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2505 if (src.type() == RegType::vgpr)
2506 src = bld.as_uniform(src);
2507 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2508 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2509 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2510 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2511 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2512 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2513 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2514 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2515 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2516 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2517 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2518 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2519 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2520 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2521 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2522 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2523 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2524 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2525 Temp borrow = bld.tmp(s1);
2526 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2527 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2528 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2529
2530 } else if (instr->src[0].src.ssa->bit_size == 64) {
2531 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2532 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2533 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2534 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2535 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2536 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2537 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2538 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2539 if (dst.type() == RegType::sgpr) {
2540 lower = bld.as_uniform(lower);
2541 upper = bld.as_uniform(upper);
2542 }
2543 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2544
2545 } else {
2546 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2547 nir_print_instr(&instr->instr, stderr);
2548 fprintf(stderr, "\n");
2549 }
2550 break;
2551 }
2552 case nir_op_f2u64: {
2553 Temp src = get_alu_src(ctx, instr->src[0]);
2554 if (instr->src[0].src.ssa->bit_size == 16)
2555 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2556
2557 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2558 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2559 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2560 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2561 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2562 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2563 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2564 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2565 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2566 Temp new_exponent = bld.tmp(v1);
2567 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2568 if (ctx->program->chip_class >= GFX8)
2569 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2570 else
2571 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2572 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2573 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2574 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2575 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2576 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2577 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2578 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2579
2580 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2581 if (src.type() == RegType::vgpr)
2582 src = bld.as_uniform(src);
2583 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2584 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2585 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2586 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2587 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2588 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2589 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2590 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2591 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2592 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2593 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2594 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2595 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2596 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2597 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2598 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2599 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2600 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2601
2602 } else if (instr->src[0].src.ssa->bit_size == 64) {
2603 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2604 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2605 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2606 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2607 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2608 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2609 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2610 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2611 if (dst.type() == RegType::sgpr) {
2612 lower = bld.as_uniform(lower);
2613 upper = bld.as_uniform(upper);
2614 }
2615 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2616
2617 } else {
2618 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2619 nir_print_instr(&instr->instr, stderr);
2620 fprintf(stderr, "\n");
2621 }
2622 break;
2623 }
2624 case nir_op_b2f16: {
2625 Temp src = get_alu_src(ctx, instr->src[0]);
2626 assert(src.regClass() == bld.lm);
2627
2628 if (dst.regClass() == s1) {
2629 src = bool_to_scalar_condition(ctx, src);
2630 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2631 } else if (dst.regClass() == v2b) {
2632 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2633 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2634 } else {
2635 unreachable("Wrong destination register class for nir_op_b2f16.");
2636 }
2637 break;
2638 }
2639 case nir_op_b2f32: {
2640 Temp src = get_alu_src(ctx, instr->src[0]);
2641 assert(src.regClass() == bld.lm);
2642
2643 if (dst.regClass() == s1) {
2644 src = bool_to_scalar_condition(ctx, src);
2645 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2646 } else if (dst.regClass() == v1) {
2647 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2648 } else {
2649 unreachable("Wrong destination register class for nir_op_b2f32.");
2650 }
2651 break;
2652 }
2653 case nir_op_b2f64: {
2654 Temp src = get_alu_src(ctx, instr->src[0]);
2655 assert(src.regClass() == bld.lm);
2656
2657 if (dst.regClass() == s2) {
2658 src = bool_to_scalar_condition(ctx, src);
2659 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2660 } else if (dst.regClass() == v2) {
2661 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2662 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2663 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2664 } else {
2665 unreachable("Wrong destination register class for nir_op_b2f64.");
2666 }
2667 break;
2668 }
2669 case nir_op_i2i8:
2670 case nir_op_i2i16:
2671 case nir_op_i2i32:
2672 case nir_op_i2i64: {
2673 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2674 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2675 break;
2676 }
2677 case nir_op_u2u8:
2678 case nir_op_u2u16:
2679 case nir_op_u2u32:
2680 case nir_op_u2u64: {
2681 convert_int(ctx, bld, get_alu_src(ctx, instr->src[0]),
2682 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2683 break;
2684 }
2685 case nir_op_b2b32:
2686 case nir_op_b2i32: {
2687 Temp src = get_alu_src(ctx, instr->src[0]);
2688 assert(src.regClass() == bld.lm);
2689
2690 if (dst.regClass() == s1) {
2691 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2692 bool_to_scalar_condition(ctx, src, dst);
2693 } else if (dst.regClass() == v1) {
2694 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2695 } else {
2696 unreachable("Invalid register class for b2i32");
2697 }
2698 break;
2699 }
2700 case nir_op_b2b1:
2701 case nir_op_i2b1: {
2702 Temp src = get_alu_src(ctx, instr->src[0]);
2703 assert(dst.regClass() == bld.lm);
2704
2705 if (src.type() == RegType::vgpr) {
2706 assert(src.regClass() == v1 || src.regClass() == v2);
2707 assert(dst.regClass() == bld.lm);
2708 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2709 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2710 } else {
2711 assert(src.regClass() == s1 || src.regClass() == s2);
2712 Temp tmp;
2713 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2714 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2715 } else {
2716 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2717 bld.scc(bld.def(s1)), Operand(0u), src);
2718 }
2719 bool_to_vector_condition(ctx, tmp, dst);
2720 }
2721 break;
2722 }
2723 case nir_op_pack_64_2x32_split: {
2724 Temp src0 = get_alu_src(ctx, instr->src[0]);
2725 Temp src1 = get_alu_src(ctx, instr->src[1]);
2726
2727 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2728 break;
2729 }
2730 case nir_op_unpack_64_2x32_split_x:
2731 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2732 break;
2733 case nir_op_unpack_64_2x32_split_y:
2734 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2735 break;
2736 case nir_op_unpack_32_2x16_split_x:
2737 if (dst.type() == RegType::vgpr) {
2738 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2739 } else {
2740 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2741 }
2742 break;
2743 case nir_op_unpack_32_2x16_split_y:
2744 if (dst.type() == RegType::vgpr) {
2745 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2746 } else {
2747 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)));
2748 }
2749 break;
2750 case nir_op_pack_32_2x16_split: {
2751 Temp src0 = get_alu_src(ctx, instr->src[0]);
2752 Temp src1 = get_alu_src(ctx, instr->src[1]);
2753 if (dst.regClass() == v1) {
2754 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2755 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2756 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2757 } else {
2758 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2759 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2760 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2761 }
2762 break;
2763 }
2764 case nir_op_pack_half_2x16: {
2765 Temp src = get_alu_src(ctx, instr->src[0], 2);
2766
2767 if (dst.regClass() == v1) {
2768 Temp src0 = bld.tmp(v1);
2769 Temp src1 = bld.tmp(v1);
2770 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2771 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2772 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2773 else
2774 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2775 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2776 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2777 } else {
2778 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2779 nir_print_instr(&instr->instr, stderr);
2780 fprintf(stderr, "\n");
2781 }
2782 break;
2783 }
2784 case nir_op_unpack_half_2x16_split_x: {
2785 if (dst.regClass() == v1) {
2786 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2787 } else {
2788 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2789 nir_print_instr(&instr->instr, stderr);
2790 fprintf(stderr, "\n");
2791 }
2792 break;
2793 }
2794 case nir_op_unpack_half_2x16_split_y: {
2795 if (dst.regClass() == v1) {
2796 /* TODO: use SDWA here */
2797 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2798 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2799 } else {
2800 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2801 nir_print_instr(&instr->instr, stderr);
2802 fprintf(stderr, "\n");
2803 }
2804 break;
2805 }
2806 case nir_op_fquantize2f16: {
2807 Temp src = get_alu_src(ctx, instr->src[0]);
2808 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2809 Temp f32, cmp_res;
2810
2811 if (ctx->program->chip_class >= GFX8) {
2812 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2813 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2814 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2815 } else {
2816 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2817 * so compare the result and flush to 0 if it's smaller.
2818 */
2819 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2820 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2821 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2822 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2823 cmp_res = vop3->definitions[0].getTemp();
2824 }
2825
2826 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2827 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2828 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2829 } else {
2830 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2831 }
2832 break;
2833 }
2834 case nir_op_bfm: {
2835 Temp bits = get_alu_src(ctx, instr->src[0]);
2836 Temp offset = get_alu_src(ctx, instr->src[1]);
2837
2838 if (dst.regClass() == s1) {
2839 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2840 } else if (dst.regClass() == v1) {
2841 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2842 } else {
2843 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2844 nir_print_instr(&instr->instr, stderr);
2845 fprintf(stderr, "\n");
2846 }
2847 break;
2848 }
2849 case nir_op_bitfield_select: {
2850 /* (mask & insert) | (~mask & base) */
2851 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2852 Temp insert = get_alu_src(ctx, instr->src[1]);
2853 Temp base = get_alu_src(ctx, instr->src[2]);
2854
2855 /* dst = (insert & bitmask) | (base & ~bitmask) */
2856 if (dst.regClass() == s1) {
2857 aco_ptr<Instruction> sop2;
2858 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2859 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2860 Operand lhs;
2861 if (const_insert && const_bitmask) {
2862 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2863 } else {
2864 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2865 lhs = Operand(insert);
2866 }
2867
2868 Operand rhs;
2869 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2870 if (const_base && const_bitmask) {
2871 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2872 } else {
2873 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2874 rhs = Operand(base);
2875 }
2876
2877 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2878
2879 } else if (dst.regClass() == v1) {
2880 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2881 base = as_vgpr(ctx, base);
2882 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2883 insert = as_vgpr(ctx, insert);
2884
2885 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2886
2887 } else {
2888 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2889 nir_print_instr(&instr->instr, stderr);
2890 fprintf(stderr, "\n");
2891 }
2892 break;
2893 }
2894 case nir_op_ubfe:
2895 case nir_op_ibfe: {
2896 Temp base = get_alu_src(ctx, instr->src[0]);
2897 Temp offset = get_alu_src(ctx, instr->src[1]);
2898 Temp bits = get_alu_src(ctx, instr->src[2]);
2899
2900 if (dst.type() == RegType::sgpr) {
2901 Operand extract;
2902 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2903 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2904 if (const_offset && const_bits) {
2905 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2906 extract = Operand(const_extract);
2907 } else {
2908 Operand width;
2909 if (const_bits) {
2910 width = Operand(const_bits->u32 << 16);
2911 } else {
2912 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2913 }
2914 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2915 }
2916
2917 aco_opcode opcode;
2918 if (dst.regClass() == s1) {
2919 if (instr->op == nir_op_ubfe)
2920 opcode = aco_opcode::s_bfe_u32;
2921 else
2922 opcode = aco_opcode::s_bfe_i32;
2923 } else if (dst.regClass() == s2) {
2924 if (instr->op == nir_op_ubfe)
2925 opcode = aco_opcode::s_bfe_u64;
2926 else
2927 opcode = aco_opcode::s_bfe_i64;
2928 } else {
2929 unreachable("Unsupported BFE bit size");
2930 }
2931
2932 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2933
2934 } else {
2935 aco_opcode opcode;
2936 if (dst.regClass() == v1) {
2937 if (instr->op == nir_op_ubfe)
2938 opcode = aco_opcode::v_bfe_u32;
2939 else
2940 opcode = aco_opcode::v_bfe_i32;
2941 } else {
2942 unreachable("Unsupported BFE bit size");
2943 }
2944
2945 emit_vop3a_instruction(ctx, instr, opcode, dst);
2946 }
2947 break;
2948 }
2949 case nir_op_bit_count: {
2950 Temp src = get_alu_src(ctx, instr->src[0]);
2951 if (src.regClass() == s1) {
2952 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2953 } else if (src.regClass() == v1) {
2954 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2955 } else if (src.regClass() == v2) {
2956 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2957 emit_extract_vector(ctx, src, 1, v1),
2958 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2959 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2960 } else if (src.regClass() == s2) {
2961 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2962 } else {
2963 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2964 nir_print_instr(&instr->instr, stderr);
2965 fprintf(stderr, "\n");
2966 }
2967 break;
2968 }
2969 case nir_op_flt: {
2970 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2971 break;
2972 }
2973 case nir_op_fge: {
2974 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2975 break;
2976 }
2977 case nir_op_feq: {
2978 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2979 break;
2980 }
2981 case nir_op_fne: {
2982 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2983 break;
2984 }
2985 case nir_op_ilt: {
2986 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);
2987 break;
2988 }
2989 case nir_op_ige: {
2990 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);
2991 break;
2992 }
2993 case nir_op_ieq: {
2994 if (instr->src[0].src.ssa->bit_size == 1)
2995 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2996 else
2997 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,
2998 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2999 break;
3000 }
3001 case nir_op_ine: {
3002 if (instr->src[0].src.ssa->bit_size == 1)
3003 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
3004 else
3005 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,
3006 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
3007 break;
3008 }
3009 case nir_op_ult: {
3010 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);
3011 break;
3012 }
3013 case nir_op_uge: {
3014 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);
3015 break;
3016 }
3017 case nir_op_fddx:
3018 case nir_op_fddy:
3019 case nir_op_fddx_fine:
3020 case nir_op_fddy_fine:
3021 case nir_op_fddx_coarse:
3022 case nir_op_fddy_coarse: {
3023 Temp src = get_alu_src(ctx, instr->src[0]);
3024 uint16_t dpp_ctrl1, dpp_ctrl2;
3025 if (instr->op == nir_op_fddx_fine) {
3026 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
3027 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
3028 } else if (instr->op == nir_op_fddy_fine) {
3029 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
3030 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
3031 } else {
3032 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
3033 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
3034 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
3035 else
3036 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
3037 }
3038
3039 Temp tmp;
3040 if (ctx->program->chip_class >= GFX8) {
3041 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
3042 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
3043 } else {
3044 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
3045 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
3046 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
3047 }
3048 emit_wqm(ctx, tmp, dst, true);
3049 break;
3050 }
3051 default:
3052 fprintf(stderr, "Unknown NIR ALU instr: ");
3053 nir_print_instr(&instr->instr, stderr);
3054 fprintf(stderr, "\n");
3055 }
3056 }
3057
3058 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
3059 {
3060 Temp dst = get_ssa_temp(ctx, &instr->def);
3061
3062 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3063 // which get truncated the lsb if double and msb if int
3064 // for now, we only use s_mov_b64 with 64bit inline constants
3065 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
3066 assert(dst.type() == RegType::sgpr);
3067
3068 Builder bld(ctx->program, ctx->block);
3069
3070 if (instr->def.bit_size == 1) {
3071 assert(dst.regClass() == bld.lm);
3072 int val = instr->value[0].b ? -1 : 0;
3073 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
3074 bld.sop1(Builder::s_mov, Definition(dst), op);
3075 } else if (instr->def.bit_size == 8) {
3076 /* ensure that the value is correctly represented in the low byte of the register */
3077 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
3078 } else if (instr->def.bit_size == 16) {
3079 /* ensure that the value is correctly represented in the low half of the register */
3080 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
3081 } else if (dst.size() == 1) {
3082 bld.copy(Definition(dst), Operand(instr->value[0].u32));
3083 } else {
3084 assert(dst.size() != 1);
3085 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3086 if (instr->def.bit_size == 64)
3087 for (unsigned i = 0; i < dst.size(); i++)
3088 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3089 else {
3090 for (unsigned i = 0; i < dst.size(); i++)
3091 vec->operands[i] = Operand{instr->value[i].u32};
3092 }
3093 vec->definitions[0] = Definition(dst);
3094 ctx->block->instructions.emplace_back(std::move(vec));
3095 }
3096 }
3097
3098 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3099 {
3100 uint32_t new_mask = 0;
3101 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3102 if (mask & (1u << i))
3103 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3104 return new_mask;
3105 }
3106
3107 struct LoadEmitInfo {
3108 Operand offset;
3109 Temp dst;
3110 unsigned num_components;
3111 unsigned component_size;
3112 Temp resource = Temp(0, s1);
3113 unsigned component_stride = 0;
3114 unsigned const_offset = 0;
3115 unsigned align_mul = 0;
3116 unsigned align_offset = 0;
3117
3118 bool glc = false;
3119 unsigned swizzle_component_size = 0;
3120 barrier_interaction barrier = barrier_none;
3121 bool can_reorder = true;
3122 Temp soffset = Temp(0, s1);
3123 };
3124
3125 using LoadCallback = Temp(*)(
3126 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3127 unsigned align, unsigned const_offset, Temp dst_hint);
3128
3129 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3130 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3131 {
3132 unsigned load_size = info->num_components * info->component_size;
3133 unsigned component_size = info->component_size;
3134
3135 unsigned num_vals = 0;
3136 Temp vals[info->dst.bytes()];
3137
3138 unsigned const_offset = info->const_offset;
3139
3140 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3141 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3142
3143 unsigned bytes_read = 0;
3144 while (bytes_read < load_size) {
3145 unsigned bytes_needed = load_size - bytes_read;
3146
3147 /* add buffer for unaligned loads */
3148 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3149
3150 if (byte_align) {
3151 if ((bytes_needed > 2 ||
3152 (bytes_needed == 2 && (align_mul % 2 || align_offset % 2)) ||
3153 !supports_8bit_16bit_loads) && byte_align_loads) {
3154 if (info->component_stride) {
3155 assert(supports_8bit_16bit_loads && "unimplemented");
3156 bytes_needed = 2;
3157 byte_align = 0;
3158 } else {
3159 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3160 bytes_needed = align(bytes_needed, 4);
3161 }
3162 } else {
3163 byte_align = 0;
3164 }
3165 }
3166
3167 if (info->swizzle_component_size)
3168 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3169 if (info->component_stride)
3170 bytes_needed = MIN2(bytes_needed, info->component_size);
3171
3172 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3173
3174 /* reduce constant offset */
3175 Operand offset = info->offset;
3176 unsigned reduced_const_offset = const_offset;
3177 bool remove_const_offset_completely = need_to_align_offset;
3178 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3179 unsigned to_add = const_offset;
3180 if (remove_const_offset_completely) {
3181 reduced_const_offset = 0;
3182 } else {
3183 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3184 reduced_const_offset %= max_const_offset_plus_one;
3185 }
3186 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3187 if (offset.isConstant()) {
3188 offset = Operand(offset.constantValue() + to_add);
3189 } else if (offset_tmp.regClass() == s1) {
3190 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3191 offset_tmp, Operand(to_add));
3192 } else if (offset_tmp.regClass() == v1) {
3193 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3194 } else {
3195 Temp lo = bld.tmp(offset_tmp.type(), 1);
3196 Temp hi = bld.tmp(offset_tmp.type(), 1);
3197 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3198
3199 if (offset_tmp.regClass() == s2) {
3200 Temp carry = bld.tmp(s1);
3201 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3202 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3203 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3204 } else {
3205 Temp new_lo = bld.tmp(v1);
3206 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3207 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3208 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3209 }
3210 }
3211 }
3212
3213 /* align offset down if needed */
3214 Operand aligned_offset = offset;
3215 if (need_to_align_offset) {
3216 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3217 if (offset.isConstant()) {
3218 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3219 } else if (offset_tmp.regClass() == s1) {
3220 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3221 } else if (offset_tmp.regClass() == s2) {
3222 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3223 } else if (offset_tmp.regClass() == v1) {
3224 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3225 } else if (offset_tmp.regClass() == v2) {
3226 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3227 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3228 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3229 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3230 }
3231 }
3232 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3233 bld.copy(bld.def(s1), aligned_offset);
3234
3235 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3236 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3237 reduced_const_offset, byte_align ? Temp() : info->dst);
3238
3239 /* the callback wrote directly to dst */
3240 if (val == info->dst) {
3241 assert(num_vals == 0);
3242 emit_split_vector(ctx, info->dst, info->num_components);
3243 return;
3244 }
3245
3246 /* shift result right if needed */
3247 if (info->component_size < 4 && byte_align_loads) {
3248 Operand align((uint32_t)byte_align);
3249 if (byte_align == -1) {
3250 if (offset.isConstant())
3251 align = Operand(offset.constantValue() % 4u);
3252 else if (offset.size() == 2)
3253 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3254 else
3255 align = offset;
3256 }
3257
3258 assert(val.bytes() >= load_size && "unimplemented");
3259 if (val.type() == RegType::sgpr)
3260 byte_align_scalar(ctx, val, align, info->dst);
3261 else
3262 byte_align_vector(ctx, val, align, info->dst, component_size);
3263 return;
3264 }
3265
3266 /* add result to list and advance */
3267 if (info->component_stride) {
3268 assert(val.bytes() == info->component_size && "unimplemented");
3269 const_offset += info->component_stride;
3270 align_offset = (align_offset + info->component_stride) % align_mul;
3271 } else {
3272 const_offset += val.bytes();
3273 align_offset = (align_offset + val.bytes()) % align_mul;
3274 }
3275 bytes_read += val.bytes();
3276 vals[num_vals++] = val;
3277 }
3278
3279 /* create array of components */
3280 unsigned components_split = 0;
3281 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3282 bool has_vgprs = false;
3283 for (unsigned i = 0; i < num_vals;) {
3284 Temp tmp[num_vals];
3285 unsigned num_tmps = 0;
3286 unsigned tmp_size = 0;
3287 RegType reg_type = RegType::sgpr;
3288 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3289 if (vals[i].type() == RegType::vgpr)
3290 reg_type = RegType::vgpr;
3291 tmp_size += vals[i].bytes();
3292 tmp[num_tmps++] = vals[i++];
3293 }
3294 if (num_tmps > 1) {
3295 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3296 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3297 for (unsigned i = 0; i < num_vals; i++)
3298 vec->operands[i] = Operand(tmp[i]);
3299 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3300 vec->definitions[0] = Definition(tmp[0]);
3301 bld.insert(std::move(vec));
3302 }
3303
3304 if (tmp[0].bytes() % component_size) {
3305 /* trim tmp[0] */
3306 assert(i == num_vals);
3307 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3308 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3309 }
3310
3311 RegClass elem_rc = RegClass::get(reg_type, component_size);
3312
3313 unsigned start = components_split;
3314
3315 if (tmp_size == elem_rc.bytes()) {
3316 allocated_vec[components_split++] = tmp[0];
3317 } else {
3318 assert(tmp_size % elem_rc.bytes() == 0);
3319 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3320 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3321 for (unsigned i = 0; i < split->definitions.size(); i++) {
3322 Temp component = bld.tmp(elem_rc);
3323 allocated_vec[components_split++] = component;
3324 split->definitions[i] = Definition(component);
3325 }
3326 split->operands[0] = Operand(tmp[0]);
3327 bld.insert(std::move(split));
3328 }
3329
3330 /* try to p_as_uniform early so we can create more optimizable code and
3331 * also update allocated_vec */
3332 for (unsigned j = start; j < components_split; j++) {
3333 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3334 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3335 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3336 }
3337 }
3338
3339 /* concatenate components and p_as_uniform() result if needed */
3340 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3341 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3342
3343 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3344
3345 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3346 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3347 for (unsigned i = 0; i < info->num_components; i++)
3348 vec->operands[i] = Operand(allocated_vec[i]);
3349 if (padding_bytes)
3350 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3351 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3352 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3353 vec->definitions[0] = Definition(tmp);
3354 bld.insert(std::move(vec));
3355 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3356 } else {
3357 vec->definitions[0] = Definition(info->dst);
3358 bld.insert(std::move(vec));
3359 }
3360 }
3361
3362 Operand load_lds_size_m0(Builder& bld)
3363 {
3364 /* TODO: m0 does not need to be initialized on GFX9+ */
3365 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3366 }
3367
3368 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3369 Temp offset, unsigned bytes_needed,
3370 unsigned align, unsigned const_offset,
3371 Temp dst_hint)
3372 {
3373 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3374
3375 Operand m = load_lds_size_m0(bld);
3376
3377 bool large_ds_read = bld.program->chip_class >= GFX7;
3378 bool usable_read2 = bld.program->chip_class >= GFX7;
3379
3380 bool read2 = false;
3381 unsigned size = 0;
3382 aco_opcode op;
3383 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3384 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3385 size = 16;
3386 op = aco_opcode::ds_read_b128;
3387 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3388 size = 16;
3389 read2 = true;
3390 op = aco_opcode::ds_read2_b64;
3391 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3392 size = 12;
3393 op = aco_opcode::ds_read_b96;
3394 } else if (bytes_needed >= 8 && align % 8 == 0) {
3395 size = 8;
3396 op = aco_opcode::ds_read_b64;
3397 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3398 size = 8;
3399 read2 = true;
3400 op = aco_opcode::ds_read2_b32;
3401 } else if (bytes_needed >= 4 && align % 4 == 0) {
3402 size = 4;
3403 op = aco_opcode::ds_read_b32;
3404 } else if (bytes_needed >= 2 && align % 2 == 0) {
3405 size = 2;
3406 op = aco_opcode::ds_read_u16;
3407 } else {
3408 size = 1;
3409 op = aco_opcode::ds_read_u8;
3410 }
3411
3412 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3413 if (const_offset >= max_offset_plus_one) {
3414 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3415 const_offset %= max_offset_plus_one;
3416 }
3417
3418 if (read2)
3419 const_offset /= (size / 2u);
3420
3421 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3422 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3423 if (read2)
3424 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3425 else
3426 bld.ds(op, Definition(val), offset, m, const_offset);
3427
3428 if (size < 4)
3429 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3430
3431 return val;
3432 }
3433
3434 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3435
3436 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3437 Temp offset, unsigned bytes_needed,
3438 unsigned align, unsigned const_offset,
3439 Temp dst_hint)
3440 {
3441 unsigned size = 0;
3442 aco_opcode op;
3443 if (bytes_needed <= 4) {
3444 size = 1;
3445 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3446 } else if (bytes_needed <= 8) {
3447 size = 2;
3448 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3449 } else if (bytes_needed <= 16) {
3450 size = 4;
3451 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3452 } else if (bytes_needed <= 32) {
3453 size = 8;
3454 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3455 } else {
3456 size = 16;
3457 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3458 }
3459 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3460 if (info->resource.id()) {
3461 load->operands[0] = Operand(info->resource);
3462 load->operands[1] = Operand(offset);
3463 } else {
3464 load->operands[0] = Operand(offset);
3465 load->operands[1] = Operand(0u);
3466 }
3467 RegClass rc(RegType::sgpr, size);
3468 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3469 load->definitions[0] = Definition(val);
3470 load->glc = info->glc;
3471 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3472 load->barrier = info->barrier;
3473 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3474 bld.insert(std::move(load));
3475 return val;
3476 }
3477
3478 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3479
3480 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3481 Temp offset, unsigned bytes_needed,
3482 unsigned align_, unsigned const_offset,
3483 Temp dst_hint)
3484 {
3485 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3486 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3487
3488 if (info->soffset.id()) {
3489 if (soffset.isTemp())
3490 vaddr = bld.copy(bld.def(v1), soffset);
3491 soffset = Operand(info->soffset);
3492 }
3493
3494 unsigned bytes_size = 0;
3495 aco_opcode op;
3496 if (bytes_needed == 1) {
3497 bytes_size = 1;
3498 op = aco_opcode::buffer_load_ubyte;
3499 } else if (bytes_needed == 2) {
3500 bytes_size = 2;
3501 op = aco_opcode::buffer_load_ushort;
3502 } else if (bytes_needed <= 4) {
3503 bytes_size = 4;
3504 op = aco_opcode::buffer_load_dword;
3505 } else if (bytes_needed <= 8) {
3506 bytes_size = 8;
3507 op = aco_opcode::buffer_load_dwordx2;
3508 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3509 bytes_size = 12;
3510 op = aco_opcode::buffer_load_dwordx3;
3511 } else {
3512 bytes_size = 16;
3513 op = aco_opcode::buffer_load_dwordx4;
3514 }
3515 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3516 mubuf->operands[0] = Operand(info->resource);
3517 mubuf->operands[1] = vaddr;
3518 mubuf->operands[2] = soffset;
3519 mubuf->offen = (offset.type() == RegType::vgpr);
3520 mubuf->glc = info->glc;
3521 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3522 mubuf->barrier = info->barrier;
3523 mubuf->can_reorder = info->can_reorder;
3524 mubuf->offset = const_offset;
3525 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3526 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3527 mubuf->definitions[0] = Definition(val);
3528 bld.insert(std::move(mubuf));
3529
3530 return val;
3531 }
3532
3533 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3534
3535 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3536 {
3537 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3538 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3539
3540 if (addr.type() == RegType::vgpr)
3541 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3542 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3543 }
3544
3545 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3546 Temp offset, unsigned bytes_needed,
3547 unsigned align_, unsigned const_offset,
3548 Temp dst_hint)
3549 {
3550 unsigned bytes_size = 0;
3551 bool mubuf = bld.program->chip_class == GFX6;
3552 bool global = bld.program->chip_class >= GFX9;
3553 aco_opcode op;
3554 if (bytes_needed == 1) {
3555 bytes_size = 1;
3556 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3557 } else if (bytes_needed == 2) {
3558 bytes_size = 2;
3559 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3560 } else if (bytes_needed <= 4) {
3561 bytes_size = 4;
3562 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3563 } else if (bytes_needed <= 8) {
3564 bytes_size = 8;
3565 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3566 } else if (bytes_needed <= 12 && !mubuf) {
3567 bytes_size = 12;
3568 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3569 } else {
3570 bytes_size = 16;
3571 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3572 }
3573 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3574 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3575 if (mubuf) {
3576 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3577 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3578 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3579 mubuf->operands[2] = Operand(0u);
3580 mubuf->glc = info->glc;
3581 mubuf->dlc = false;
3582 mubuf->offset = 0;
3583 mubuf->addr64 = offset.type() == RegType::vgpr;
3584 mubuf->disable_wqm = false;
3585 mubuf->barrier = info->barrier;
3586 mubuf->definitions[0] = Definition(val);
3587 bld.insert(std::move(mubuf));
3588 } else {
3589 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3590
3591 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3592 flat->operands[0] = Operand(offset);
3593 flat->operands[1] = Operand(s1);
3594 flat->glc = info->glc;
3595 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3596 flat->barrier = info->barrier;
3597 flat->offset = 0u;
3598 flat->definitions[0] = Definition(val);
3599 bld.insert(std::move(flat));
3600 }
3601
3602 return val;
3603 }
3604
3605 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3606
3607 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3608 Temp address, unsigned base_offset, unsigned align)
3609 {
3610 assert(util_is_power_of_two_nonzero(align));
3611
3612 Builder bld(ctx->program, ctx->block);
3613
3614 unsigned num_components = dst.bytes() / elem_size_bytes;
3615 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3616 info.align_mul = align;
3617 info.align_offset = 0;
3618 info.barrier = barrier_shared;
3619 info.can_reorder = false;
3620 info.const_offset = base_offset;
3621 emit_lds_load(ctx, bld, &info);
3622
3623 return dst;
3624 }
3625
3626 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3627 {
3628 if (!count)
3629 return;
3630
3631 Builder bld(ctx->program, ctx->block);
3632
3633 ASSERTED bool is_subdword = false;
3634 for (unsigned i = 0; i < count; i++)
3635 is_subdword |= offsets[i] % 4;
3636 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3637 assert(!is_subdword || dst_type == RegType::vgpr);
3638
3639 /* count == 1 fast path */
3640 if (count == 1) {
3641 if (dst_type == RegType::sgpr)
3642 dst[0] = bld.as_uniform(src);
3643 else
3644 dst[0] = as_vgpr(ctx, src);
3645 return;
3646 }
3647
3648 for (unsigned i = 0; i < count - 1; i++)
3649 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3650 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3651
3652 if (is_subdword && src.type() == RegType::sgpr) {
3653 src = as_vgpr(ctx, src);
3654 } else {
3655 /* use allocated_vec if possible */
3656 auto it = ctx->allocated_vec.find(src.id());
3657 if (it != ctx->allocated_vec.end()) {
3658 unsigned total_size = 0;
3659 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3660 total_size += it->second[i].bytes();
3661 if (total_size != src.bytes())
3662 goto split;
3663
3664 unsigned elem_size = it->second[0].bytes();
3665
3666 for (unsigned i = 0; i < count; i++) {
3667 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3668 goto split;
3669 }
3670
3671 for (unsigned i = 0; i < count; i++) {
3672 unsigned start_idx = offsets[i] / elem_size;
3673 unsigned op_count = dst[i].bytes() / elem_size;
3674 if (op_count == 1) {
3675 if (dst_type == RegType::sgpr)
3676 dst[i] = bld.as_uniform(it->second[start_idx]);
3677 else
3678 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3679 continue;
3680 }
3681
3682 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3683 for (unsigned j = 0; j < op_count; j++) {
3684 Temp tmp = it->second[start_idx + j];
3685 if (dst_type == RegType::sgpr)
3686 tmp = bld.as_uniform(tmp);
3687 vec->operands[j] = Operand(tmp);
3688 }
3689 vec->definitions[0] = Definition(dst[i]);
3690 bld.insert(std::move(vec));
3691 }
3692 return;
3693 }
3694 }
3695
3696 if (dst_type == RegType::sgpr)
3697 src = bld.as_uniform(src);
3698
3699 split:
3700 /* just split it */
3701 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3702 split->operands[0] = Operand(src);
3703 for (unsigned i = 0; i < count; i++)
3704 split->definitions[i] = Definition(dst[i]);
3705 bld.insert(std::move(split));
3706 }
3707
3708 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3709 int *start, int *count)
3710 {
3711 unsigned start_elem = ffs(todo_mask) - 1;
3712 bool skip = !(mask & (1 << start_elem));
3713 if (skip)
3714 mask = ~mask & todo_mask;
3715
3716 mask &= todo_mask;
3717
3718 u_bit_scan_consecutive_range(&mask, start, count);
3719
3720 return !skip;
3721 }
3722
3723 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3724 {
3725 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3726 }
3727
3728 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3729 Temp address, unsigned base_offset, unsigned align)
3730 {
3731 assert(util_is_power_of_two_nonzero(align));
3732 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3733
3734 Builder bld(ctx->program, ctx->block);
3735 bool large_ds_write = ctx->options->chip_class >= GFX7;
3736 bool usable_write2 = ctx->options->chip_class >= GFX7;
3737
3738 unsigned write_count = 0;
3739 Temp write_datas[32];
3740 unsigned offsets[32];
3741 aco_opcode opcodes[32];
3742
3743 wrmask = widen_mask(wrmask, elem_size_bytes);
3744
3745 uint32_t todo = u_bit_consecutive(0, data.bytes());
3746 while (todo) {
3747 int offset, bytes;
3748 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3749 offsets[write_count] = offset;
3750 opcodes[write_count] = aco_opcode::num_opcodes;
3751 write_count++;
3752 advance_write_mask(&todo, offset, bytes);
3753 continue;
3754 }
3755
3756 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3757 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3758 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3759 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3760
3761 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3762 aco_opcode op = aco_opcode::num_opcodes;
3763 if (bytes >= 16 && aligned16 && large_ds_write) {
3764 op = aco_opcode::ds_write_b128;
3765 bytes = 16;
3766 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3767 op = aco_opcode::ds_write_b96;
3768 bytes = 12;
3769 } else if (bytes >= 8 && aligned8) {
3770 op = aco_opcode::ds_write_b64;
3771 bytes = 8;
3772 } else if (bytes >= 4 && aligned4) {
3773 op = aco_opcode::ds_write_b32;
3774 bytes = 4;
3775 } else if (bytes >= 2 && aligned2) {
3776 op = aco_opcode::ds_write_b16;
3777 bytes = 2;
3778 } else if (bytes >= 1) {
3779 op = aco_opcode::ds_write_b8;
3780 bytes = 1;
3781 } else {
3782 assert(false);
3783 }
3784
3785 offsets[write_count] = offset;
3786 opcodes[write_count] = op;
3787 write_count++;
3788 advance_write_mask(&todo, offset, bytes);
3789 }
3790
3791 Operand m = load_lds_size_m0(bld);
3792
3793 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3794
3795 for (unsigned i = 0; i < write_count; i++) {
3796 aco_opcode op = opcodes[i];
3797 if (op == aco_opcode::num_opcodes)
3798 continue;
3799
3800 Temp data = write_datas[i];
3801
3802 unsigned second = write_count;
3803 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3804 for (second = i + 1; second < write_count; second++) {
3805 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3806 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3807 opcodes[second] = aco_opcode::num_opcodes;
3808 break;
3809 }
3810 }
3811 }
3812
3813 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3814 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3815
3816 unsigned inline_offset = base_offset + offsets[i];
3817 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3818 Temp address_offset = address;
3819 if (inline_offset > max_offset) {
3820 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3821 inline_offset = offsets[i];
3822 }
3823 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3824
3825 if (write2) {
3826 Temp second_data = write_datas[second];
3827 inline_offset /= data.bytes();
3828 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3829 } else {
3830 bld.ds(op, address_offset, data, m, inline_offset);
3831 }
3832 }
3833 }
3834
3835 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3836 {
3837 unsigned align = 16;
3838 if (const_offset)
3839 align = std::min(align, 1u << (ffs(const_offset) - 1));
3840
3841 return align;
3842 }
3843
3844
3845 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3846 {
3847 switch (bytes) {
3848 case 1:
3849 assert(!smem);
3850 return aco_opcode::buffer_store_byte;
3851 case 2:
3852 assert(!smem);
3853 return aco_opcode::buffer_store_short;
3854 case 4:
3855 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3856 case 8:
3857 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3858 case 12:
3859 assert(!smem);
3860 return aco_opcode::buffer_store_dwordx3;
3861 case 16:
3862 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3863 }
3864 unreachable("Unexpected store size");
3865 return aco_opcode::num_opcodes;
3866 }
3867
3868 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3869 Temp data, unsigned writemask, int swizzle_element_size,
3870 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3871 {
3872 unsigned write_count_with_skips = 0;
3873 bool skips[16];
3874
3875 /* determine how to split the data */
3876 unsigned todo = u_bit_consecutive(0, data.bytes());
3877 while (todo) {
3878 int offset, bytes;
3879 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3880 offsets[write_count_with_skips] = offset;
3881 if (skips[write_count_with_skips]) {
3882 advance_write_mask(&todo, offset, bytes);
3883 write_count_with_skips++;
3884 continue;
3885 }
3886
3887 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3888 * larger than swizzle_element_size */
3889 bytes = MIN2(bytes, swizzle_element_size);
3890 if (bytes % 4)
3891 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3892
3893 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3894 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3895 bytes = 8;
3896
3897 /* dword or larger stores have to be dword-aligned */
3898 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3899 unsigned align_offset = (instr ? nir_intrinsic_align_offset(instr) : 0) + offset;
3900 bool dword_aligned = align_offset % 4 == 0 && align_mul % 4 == 0;
3901 if (!dword_aligned)
3902 bytes = MIN2(bytes, (align_offset % 2 == 0 && align_mul % 2 == 0) ? 2 : 1);
3903
3904 advance_write_mask(&todo, offset, bytes);
3905 write_count_with_skips++;
3906 }
3907
3908 /* actually split data */
3909 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3910
3911 /* remove skips */
3912 for (unsigned i = 0; i < write_count_with_skips; i++) {
3913 if (skips[i])
3914 continue;
3915 write_datas[*write_count] = write_datas[i];
3916 offsets[*write_count] = offsets[i];
3917 (*write_count)++;
3918 }
3919 }
3920
3921 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3922 unsigned split_cnt = 0u, Temp dst = Temp())
3923 {
3924 Builder bld(ctx->program, ctx->block);
3925 unsigned dword_size = elem_size_bytes / 4;
3926
3927 if (!dst.id())
3928 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3929
3930 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3931 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3932 instr->definitions[0] = Definition(dst);
3933
3934 for (unsigned i = 0; i < cnt; ++i) {
3935 if (arr[i].id()) {
3936 assert(arr[i].size() == dword_size);
3937 allocated_vec[i] = arr[i];
3938 instr->operands[i] = Operand(arr[i]);
3939 } else {
3940 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3941 allocated_vec[i] = zero;
3942 instr->operands[i] = Operand(zero);
3943 }
3944 }
3945
3946 bld.insert(std::move(instr));
3947
3948 if (split_cnt)
3949 emit_split_vector(ctx, dst, split_cnt);
3950 else
3951 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3952
3953 return dst;
3954 }
3955
3956 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3957 {
3958 if (const_offset >= 4096) {
3959 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3960 const_offset %= 4096u;
3961
3962 if (!voffset.id())
3963 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3964 else if (unlikely(voffset.regClass() == s1))
3965 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3966 else if (likely(voffset.regClass() == v1))
3967 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3968 else
3969 unreachable("Unsupported register class of voffset");
3970 }
3971
3972 return const_offset;
3973 }
3974
3975 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3976 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3977 {
3978 assert(vdata.id());
3979 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3980 assert(vdata.size() >= 1 && vdata.size() <= 4);
3981
3982 Builder bld(ctx->program, ctx->block);
3983 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
3984 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3985
3986 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3987 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3988 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3989 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3990 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3991
3992 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3993 }
3994
3995 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3996 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3997 bool allow_combining = true, bool reorder = true, bool slc = false)
3998 {
3999 Builder bld(ctx->program, ctx->block);
4000 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4001 assert(write_mask);
4002 write_mask = widen_mask(write_mask, elem_size_bytes);
4003
4004 unsigned write_count = 0;
4005 Temp write_datas[32];
4006 unsigned offsets[32];
4007 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
4008 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
4009
4010 for (unsigned i = 0; i < write_count; i++) {
4011 unsigned const_offset = offsets[i] + base_const_offset;
4012 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc);
4013 }
4014 }
4015
4016 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
4017 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
4018 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
4019 {
4020 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
4021 assert((num_components * elem_size_bytes) == dst.bytes());
4022 assert(!!stride != allow_combining);
4023
4024 Builder bld(ctx->program, ctx->block);
4025
4026 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
4027 info.component_stride = allow_combining ? 0 : stride;
4028 info.glc = true;
4029 info.swizzle_component_size = allow_combining ? 0 : 4;
4030 info.align_mul = MIN2(elem_size_bytes, 4);
4031 info.align_offset = 0;
4032 info.soffset = soffset;
4033 info.const_offset = base_const_offset;
4034 emit_mubuf_load(ctx, bld, &info);
4035 }
4036
4037 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)
4038 {
4039 Builder bld(ctx->program, ctx->block);
4040 Temp offset = base_offset.first;
4041 unsigned const_offset = base_offset.second;
4042
4043 if (!nir_src_is_const(*off_src)) {
4044 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
4045 Temp with_stride;
4046
4047 /* Calculate indirect offset with stride */
4048 if (likely(indirect_offset_arg.regClass() == v1))
4049 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4050 else if (indirect_offset_arg.regClass() == s1)
4051 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4052 else
4053 unreachable("Unsupported register class of indirect offset");
4054
4055 /* Add to the supplied base offset */
4056 if (offset.id() == 0)
4057 offset = with_stride;
4058 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4059 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4060 else if (offset.size() == 1 && with_stride.size() == 1)
4061 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4062 else
4063 unreachable("Unsupported register class of indirect offset");
4064 } else {
4065 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4066 const_offset += const_offset_arg * stride;
4067 }
4068
4069 return std::make_pair(offset, const_offset);
4070 }
4071
4072 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4073 {
4074 Builder bld(ctx->program, ctx->block);
4075 Temp offset;
4076
4077 if (off1.first.id() && off2.first.id()) {
4078 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4079 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4080 else if (off1.first.size() == 1 && off2.first.size() == 1)
4081 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4082 else
4083 unreachable("Unsupported register class of indirect offset");
4084 } else {
4085 offset = off1.first.id() ? off1.first : off2.first;
4086 }
4087
4088 return std::make_pair(offset, off1.second + off2.second);
4089 }
4090
4091 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4092 {
4093 Builder bld(ctx->program, ctx->block);
4094 unsigned const_offset = offs.second * multiplier;
4095
4096 if (!offs.first.id())
4097 return std::make_pair(offs.first, const_offset);
4098
4099 Temp offset = unlikely(offs.first.regClass() == s1)
4100 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4101 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4102
4103 return std::make_pair(offset, const_offset);
4104 }
4105
4106 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4107 {
4108 Builder bld(ctx->program, ctx->block);
4109
4110 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4111 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4112 /* component is in bytes */
4113 const_offset += nir_intrinsic_component(instr) * component_stride;
4114
4115 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4116 nir_src *off_src = nir_get_io_offset_src(instr);
4117 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4118 }
4119
4120 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4121 {
4122 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4123 }
4124
4125 Temp get_tess_rel_patch_id(isel_context *ctx)
4126 {
4127 Builder bld(ctx->program, ctx->block);
4128
4129 switch (ctx->shader->info.stage) {
4130 case MESA_SHADER_TESS_CTRL:
4131 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4132 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4133 case MESA_SHADER_TESS_EVAL:
4134 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4135 default:
4136 unreachable("Unsupported stage in get_tess_rel_patch_id");
4137 }
4138 }
4139
4140 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4141 {
4142 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4143 Builder bld(ctx->program, ctx->block);
4144
4145 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4146 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4147
4148 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4149
4150 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4151 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4152
4153 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4154 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4155 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4156
4157 return offset_mul(ctx, offs, 4u);
4158 }
4159
4160 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4161 {
4162 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4163 Builder bld(ctx->program, ctx->block);
4164
4165 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4166 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4167 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4168 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4169
4170 std::pair<Temp, unsigned> offs = instr
4171 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4172 : std::make_pair(Temp(), 0u);
4173
4174 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4175 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4176
4177 if (per_vertex) {
4178 assert(instr);
4179
4180 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4181 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4182
4183 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4184 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4185 } else {
4186 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4187 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4188 }
4189
4190 return offs;
4191 }
4192
4193 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4194 {
4195 Builder bld(ctx->program, ctx->block);
4196
4197 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4198 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4199
4200 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4201
4202 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4203 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4204 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4205
4206 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4207 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4208
4209 return offs;
4210 }
4211
4212 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4213 {
4214 Builder bld(ctx->program, ctx->block);
4215
4216 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4217 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4218 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4219 unsigned attr_stride = ctx->tcs_num_patches;
4220
4221 std::pair<Temp, unsigned> offs = instr
4222 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4223 : std::make_pair(Temp(), 0u);
4224
4225 if (const_base_offset)
4226 offs.second += const_base_offset * attr_stride;
4227
4228 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4229 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4230 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4231
4232 return offs;
4233 }
4234
4235 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4236 {
4237 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4238
4239 if (mask == 0)
4240 return false;
4241
4242 unsigned drv_loc = nir_intrinsic_base(instr);
4243 nir_src *off_src = nir_get_io_offset_src(instr);
4244
4245 if (!nir_src_is_const(*off_src)) {
4246 *indirect = true;
4247 return false;
4248 }
4249
4250 *indirect = false;
4251 uint64_t slot = per_vertex
4252 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4253 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4254 return (((uint64_t) 1) << slot) & mask;
4255 }
4256
4257 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4258 {
4259 unsigned write_mask = nir_intrinsic_write_mask(instr);
4260 unsigned component = nir_intrinsic_component(instr);
4261 unsigned idx = nir_intrinsic_base(instr) + component;
4262
4263 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4264 if (off_instr->type != nir_instr_type_load_const)
4265 return false;
4266
4267 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4268 idx += nir_src_as_uint(instr->src[1]) * 4u;
4269
4270 if (instr->src[0].ssa->bit_size == 64)
4271 write_mask = widen_mask(write_mask, 2);
4272
4273 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4274
4275 for (unsigned i = 0; i < 8; ++i) {
4276 if (write_mask & (1 << i)) {
4277 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4278 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4279 }
4280 idx++;
4281 }
4282
4283 return true;
4284 }
4285
4286 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4287 {
4288 /* Only TCS per-vertex inputs are supported by this function.
4289 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4290 */
4291 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4292 return false;
4293
4294 nir_src *off_src = nir_get_io_offset_src(instr);
4295 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4296 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4297 bool can_use_temps = nir_src_is_const(*off_src) &&
4298 vertex_index_instr->type == nir_instr_type_intrinsic &&
4299 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4300
4301 if (!can_use_temps)
4302 return false;
4303
4304 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4305 Temp *src = &ctx->inputs.temps[idx];
4306 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4307
4308 return true;
4309 }
4310
4311 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4312 {
4313 Builder bld(ctx->program, ctx->block);
4314
4315 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4316 /* 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. */
4317 bool indirect_write;
4318 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4319 if (temp_only_input && !indirect_write)
4320 return;
4321 }
4322
4323 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4324 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4325 unsigned write_mask = nir_intrinsic_write_mask(instr);
4326 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4327
4328 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4329 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4330 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4331 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4332 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4333 } else {
4334 Temp lds_base;
4335
4336 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4337 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4338 unsigned itemsize = ctx->stage == vertex_geometry_gs
4339 ? ctx->program->info->vs.es_info.esgs_itemsize
4340 : ctx->program->info->tes.es_info.esgs_itemsize;
4341 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4342 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));
4343 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4344 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4345 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4346 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4347 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4348 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4349 */
4350 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4351 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4352 } else {
4353 unreachable("Invalid LS or ES stage");
4354 }
4355
4356 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4357 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4358 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4359 }
4360 }
4361
4362 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4363 {
4364 if (per_vertex)
4365 return false;
4366
4367 unsigned off = nir_intrinsic_base(instr) * 4u;
4368 return off == ctx->tcs_tess_lvl_out_loc ||
4369 off == ctx->tcs_tess_lvl_in_loc;
4370
4371 }
4372
4373 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4374 {
4375 uint64_t mask = per_vertex
4376 ? ctx->program->info->tcs.tes_inputs_read
4377 : ctx->program->info->tcs.tes_patch_inputs_read;
4378
4379 bool indirect_write = false;
4380 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4381 return indirect_write || output_read_by_tes;
4382 }
4383
4384 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4385 {
4386 uint64_t mask = per_vertex
4387 ? ctx->shader->info.outputs_read
4388 : ctx->shader->info.patch_outputs_read;
4389
4390 bool indirect_write = false;
4391 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4392 return indirect_write || output_read;
4393 }
4394
4395 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4396 {
4397 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4398 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4399
4400 Builder bld(ctx->program, ctx->block);
4401
4402 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4403 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4404 unsigned write_mask = nir_intrinsic_write_mask(instr);
4405
4406 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4407 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4408 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4409
4410 if (write_to_vmem) {
4411 std::pair<Temp, unsigned> vmem_offs = per_vertex
4412 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4413 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4414
4415 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));
4416 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4417 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);
4418 }
4419
4420 if (write_to_lds) {
4421 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4422 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4423 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4424 }
4425 }
4426
4427 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4428 {
4429 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4430 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4431
4432 Builder bld(ctx->program, ctx->block);
4433
4434 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4435 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4436 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4437 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4438
4439 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4440 }
4441
4442 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4443 {
4444 if (ctx->stage == vertex_vs ||
4445 ctx->stage == tess_eval_vs ||
4446 ctx->stage == fragment_fs ||
4447 ctx->stage == ngg_vertex_gs ||
4448 ctx->stage == ngg_tess_eval_gs ||
4449 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4450 bool stored_to_temps = store_output_to_temps(ctx, instr);
4451 if (!stored_to_temps) {
4452 fprintf(stderr, "Unimplemented output offset instruction:\n");
4453 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4454 fprintf(stderr, "\n");
4455 abort();
4456 }
4457 } else if (ctx->stage == vertex_es ||
4458 ctx->stage == vertex_ls ||
4459 ctx->stage == tess_eval_es ||
4460 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4461 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4462 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4463 visit_store_ls_or_es_output(ctx, instr);
4464 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4465 visit_store_tcs_output(ctx, instr, false);
4466 } else {
4467 unreachable("Shader stage not implemented");
4468 }
4469 }
4470
4471 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4472 {
4473 visit_load_tcs_output(ctx, instr, false);
4474 }
4475
4476 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4477 {
4478 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4479 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4480
4481 Builder bld(ctx->program, ctx->block);
4482
4483 if (dst.regClass() == v2b) {
4484 if (ctx->program->has_16bank_lds) {
4485 assert(ctx->options->chip_class <= GFX8);
4486 Builder::Result interp_p1 =
4487 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4488 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4489 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4490 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4491 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4492 bld.m0(prim_mask), interp_p1, idx, component);
4493 } else {
4494 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4495
4496 if (ctx->options->chip_class == GFX8)
4497 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4498
4499 Builder::Result interp_p1 =
4500 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4501 coord1, bld.m0(prim_mask), idx, component);
4502 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4503 interp_p1, idx, component);
4504 }
4505 } else {
4506 Builder::Result interp_p1 =
4507 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4508 bld.m0(prim_mask), idx, component);
4509
4510 if (ctx->program->has_16bank_lds)
4511 interp_p1.instr->operands[0].setLateKill(true);
4512
4513 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4514 bld.m0(prim_mask), interp_p1, idx, component);
4515 }
4516 }
4517
4518 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4519 {
4520 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4521 for (unsigned i = 0; i < num_components; i++)
4522 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4523 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4524 assert(num_components == 4);
4525 Builder bld(ctx->program, ctx->block);
4526 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4527 }
4528
4529 for (Operand& op : vec->operands)
4530 op = op.isUndefined() ? Operand(0u) : op;
4531
4532 vec->definitions[0] = Definition(dst);
4533 ctx->block->instructions.emplace_back(std::move(vec));
4534 emit_split_vector(ctx, dst, num_components);
4535 return;
4536 }
4537
4538 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4539 {
4540 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4541 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4542 unsigned idx = nir_intrinsic_base(instr);
4543 unsigned component = nir_intrinsic_component(instr);
4544 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4545
4546 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4547 if (offset) {
4548 assert(offset->u32 == 0);
4549 } else {
4550 /* the lower 15bit of the prim_mask contain the offset into LDS
4551 * while the upper bits contain the number of prims */
4552 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4553 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4554 Builder bld(ctx->program, ctx->block);
4555 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4556 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4557 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4558 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4559 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4560 }
4561
4562 if (instr->dest.ssa.num_components == 1) {
4563 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4564 } else {
4565 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4566 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4567 {
4568 Temp tmp = {ctx->program->allocateId(), v1};
4569 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4570 vec->operands[i] = Operand(tmp);
4571 }
4572 vec->definitions[0] = Definition(dst);
4573 ctx->block->instructions.emplace_back(std::move(vec));
4574 }
4575 }
4576
4577 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4578 unsigned offset, unsigned stride, unsigned channels)
4579 {
4580 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4581 if (vtx_info->chan_byte_size != 4 && channels == 3)
4582 return false;
4583 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4584 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4585 }
4586
4587 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4588 unsigned offset, unsigned stride, unsigned *channels)
4589 {
4590 if (!vtx_info->chan_byte_size) {
4591 *channels = vtx_info->num_channels;
4592 return vtx_info->chan_format;
4593 }
4594
4595 unsigned num_channels = *channels;
4596 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4597 unsigned new_channels = num_channels + 1;
4598 /* first, assume more loads is worse and try using a larger data format */
4599 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4600 new_channels++;
4601 /* don't make the attribute potentially out-of-bounds */
4602 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4603 new_channels = 5;
4604 }
4605
4606 if (new_channels == 5) {
4607 /* then try decreasing load size (at the cost of more loads) */
4608 new_channels = *channels;
4609 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4610 new_channels--;
4611 }
4612
4613 if (new_channels < *channels)
4614 *channels = new_channels;
4615 num_channels = new_channels;
4616 }
4617
4618 switch (vtx_info->chan_format) {
4619 case V_008F0C_BUF_DATA_FORMAT_8:
4620 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4621 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4622 case V_008F0C_BUF_DATA_FORMAT_16:
4623 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4624 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4625 case V_008F0C_BUF_DATA_FORMAT_32:
4626 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4627 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4628 }
4629 unreachable("shouldn't reach here");
4630 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4631 }
4632
4633 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4634 * so we may need to fix it up. */
4635 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4636 {
4637 Builder bld(ctx->program, ctx->block);
4638
4639 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4640 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4641
4642 /* For the integer-like cases, do a natural sign extension.
4643 *
4644 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4645 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4646 * exponent.
4647 */
4648 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4649 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4650
4651 /* Convert back to the right type. */
4652 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4653 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4654 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4655 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4656 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4657 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4658 }
4659
4660 return alpha;
4661 }
4662
4663 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4664 {
4665 Builder bld(ctx->program, ctx->block);
4666 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4667 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4668
4669 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4670 if (off_instr->type != nir_instr_type_load_const) {
4671 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4672 nir_print_instr(off_instr, stderr);
4673 fprintf(stderr, "\n");
4674 }
4675 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4676
4677 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4678
4679 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4680 unsigned component = nir_intrinsic_component(instr);
4681 unsigned bitsize = instr->dest.ssa.bit_size;
4682 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4683 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4684 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4685 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4686
4687 unsigned dfmt = attrib_format & 0xf;
4688 unsigned nfmt = (attrib_format >> 4) & 0x7;
4689 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4690
4691 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4692 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4693 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4694 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4695 if (post_shuffle)
4696 num_channels = MAX2(num_channels, 3);
4697
4698 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4699 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4700
4701 Temp index;
4702 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4703 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4704 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4705 if (divisor) {
4706 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4707 if (divisor != 1) {
4708 Temp divided = bld.tmp(v1);
4709 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4710 index = bld.vadd32(bld.def(v1), start_instance, divided);
4711 } else {
4712 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4713 }
4714 } else {
4715 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4716 }
4717 } else {
4718 index = bld.vadd32(bld.def(v1),
4719 get_arg(ctx, ctx->args->ac.base_vertex),
4720 get_arg(ctx, ctx->args->ac.vertex_id));
4721 }
4722
4723 Temp channels[num_channels];
4724 unsigned channel_start = 0;
4725 bool direct_fetch = false;
4726
4727 /* skip unused channels at the start */
4728 if (vtx_info->chan_byte_size && !post_shuffle) {
4729 channel_start = ffs(mask) - 1;
4730 for (unsigned i = 0; i < channel_start; i++)
4731 channels[i] = Temp(0, s1);
4732 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4733 num_channels = 3 - (ffs(mask) - 1);
4734 }
4735
4736 /* load channels */
4737 while (channel_start < num_channels) {
4738 unsigned fetch_component = num_channels - channel_start;
4739 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4740 bool expanded = false;
4741
4742 /* use MUBUF when possible to avoid possible alignment issues */
4743 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4744 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4745 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4746 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4747 vtx_info->chan_byte_size == 4;
4748 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4749 if (!use_mubuf) {
4750 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4751 } else {
4752 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4753 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4754 fetch_component = 4;
4755 expanded = true;
4756 }
4757 }
4758
4759 unsigned fetch_bytes = fetch_component * bitsize / 8;
4760
4761 Temp fetch_index = index;
4762 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4763 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4764 fetch_offset = fetch_offset % attrib_stride;
4765 }
4766
4767 Operand soffset(0u);
4768 if (fetch_offset >= 4096) {
4769 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4770 fetch_offset %= 4096;
4771 }
4772
4773 aco_opcode opcode;
4774 switch (fetch_bytes) {
4775 case 2:
4776 assert(!use_mubuf && bitsize == 16);
4777 opcode = aco_opcode::tbuffer_load_format_d16_x;
4778 break;
4779 case 4:
4780 if (bitsize == 16) {
4781 assert(!use_mubuf);
4782 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4783 } else {
4784 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4785 }
4786 break;
4787 case 6:
4788 assert(!use_mubuf && bitsize == 16);
4789 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4790 break;
4791 case 8:
4792 if (bitsize == 16) {
4793 assert(!use_mubuf);
4794 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4795 } else {
4796 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4797 }
4798 break;
4799 case 12:
4800 assert(ctx->options->chip_class >= GFX7 ||
4801 (!use_mubuf && ctx->options->chip_class == GFX6));
4802 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4803 break;
4804 case 16:
4805 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4806 break;
4807 default:
4808 unreachable("Unimplemented load_input vector size");
4809 }
4810
4811 Temp fetch_dst;
4812 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4813 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4814 num_channels <= 3)) {
4815 direct_fetch = true;
4816 fetch_dst = dst;
4817 } else {
4818 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4819 }
4820
4821 if (use_mubuf) {
4822 Instruction *mubuf = bld.mubuf(opcode,
4823 Definition(fetch_dst), list, fetch_index, soffset,
4824 fetch_offset, false, true).instr;
4825 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4826 } else {
4827 Instruction *mtbuf = bld.mtbuf(opcode,
4828 Definition(fetch_dst), list, fetch_index, soffset,
4829 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4830 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4831 }
4832
4833 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4834
4835 if (fetch_component == 1) {
4836 channels[channel_start] = fetch_dst;
4837 } else {
4838 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4839 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4840 bitsize == 16 ? v2b : v1);
4841 }
4842
4843 channel_start += fetch_component;
4844 }
4845
4846 if (!direct_fetch) {
4847 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4848 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4849
4850 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4851 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4852 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4853
4854 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4855 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4856 unsigned num_temp = 0;
4857 for (unsigned i = 0; i < dst.size(); i++) {
4858 unsigned idx = i + component;
4859 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4860 Temp channel = channels[swizzle[idx]];
4861 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4862 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4863 vec->operands[i] = Operand(channel);
4864
4865 num_temp++;
4866 elems[i] = channel;
4867 } else if (is_float && idx == 3) {
4868 vec->operands[i] = Operand(0x3f800000u);
4869 } else if (!is_float && idx == 3) {
4870 vec->operands[i] = Operand(1u);
4871 } else {
4872 vec->operands[i] = Operand(0u);
4873 }
4874 }
4875 vec->definitions[0] = Definition(dst);
4876 ctx->block->instructions.emplace_back(std::move(vec));
4877 emit_split_vector(ctx, dst, dst.size());
4878
4879 if (num_temp == dst.size())
4880 ctx->allocated_vec.emplace(dst.id(), elems);
4881 }
4882 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4883 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4884 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4885 if (off_instr->type != nir_instr_type_load_const ||
4886 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4887 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4888 nir_print_instr(off_instr, stderr);
4889 fprintf(stderr, "\n");
4890 }
4891
4892 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4893 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4894 if (offset) {
4895 assert(offset->u32 == 0);
4896 } else {
4897 /* the lower 15bit of the prim_mask contain the offset into LDS
4898 * while the upper bits contain the number of prims */
4899 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4900 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4901 Builder bld(ctx->program, ctx->block);
4902 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4903 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4904 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4905 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4906 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4907 }
4908
4909 unsigned idx = nir_intrinsic_base(instr);
4910 unsigned component = nir_intrinsic_component(instr);
4911 unsigned vertex_id = 2; /* P0 */
4912
4913 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4914 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4915 switch (src0->u32) {
4916 case 0:
4917 vertex_id = 2; /* P0 */
4918 break;
4919 case 1:
4920 vertex_id = 0; /* P10 */
4921 break;
4922 case 2:
4923 vertex_id = 1; /* P20 */
4924 break;
4925 default:
4926 unreachable("invalid vertex index");
4927 }
4928 }
4929
4930 if (dst.size() == 1) {
4931 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4932 } else {
4933 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4934 for (unsigned i = 0; i < dst.size(); i++)
4935 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4936 vec->definitions[0] = Definition(dst);
4937 bld.insert(std::move(vec));
4938 }
4939
4940 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4941 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4942 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4943 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4944 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4945
4946 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4947 } else {
4948 unreachable("Shader stage not implemented");
4949 }
4950 }
4951
4952 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4953 {
4954 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4955
4956 Builder bld(ctx->program, ctx->block);
4957 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4958 Temp vertex_offset;
4959
4960 if (!nir_src_is_const(*vertex_src)) {
4961 /* better code could be created, but this case probably doesn't happen
4962 * much in practice */
4963 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4964 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4965 Temp elem;
4966
4967 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4968 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4969 if (i % 2u)
4970 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4971 } else {
4972 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4973 }
4974
4975 if (vertex_offset.id()) {
4976 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4977 Operand(i), indirect_vertex);
4978 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4979 } else {
4980 vertex_offset = elem;
4981 }
4982 }
4983
4984 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4985 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4986 } else {
4987 unsigned vertex = nir_src_as_uint(*vertex_src);
4988 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4989 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4990 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4991 Operand((vertex % 2u) * 16u), Operand(16u));
4992 else
4993 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4994 }
4995
4996 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4997 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4998 return offset_mul(ctx, offs, 4u);
4999 }
5000
5001 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5002 {
5003 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
5004
5005 Builder bld(ctx->program, ctx->block);
5006 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5007 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5008
5009 if (ctx->stage == geometry_gs) {
5010 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
5011 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
5012 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);
5013 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
5014 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
5015 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5016 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5017 } else {
5018 unreachable("Unsupported GS stage.");
5019 }
5020 }
5021
5022 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5023 {
5024 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5025
5026 Builder bld(ctx->program, ctx->block);
5027 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5028
5029 if (load_input_from_temps(ctx, instr, dst))
5030 return;
5031
5032 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
5033 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5034 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
5035
5036 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
5037 }
5038
5039 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5040 {
5041 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5042
5043 Builder bld(ctx->program, ctx->block);
5044
5045 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
5046 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
5047 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5048
5049 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5050 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5051
5052 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5053 }
5054
5055 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5056 {
5057 switch (ctx->shader->info.stage) {
5058 case MESA_SHADER_GEOMETRY:
5059 visit_load_gs_per_vertex_input(ctx, instr);
5060 break;
5061 case MESA_SHADER_TESS_CTRL:
5062 visit_load_tcs_per_vertex_input(ctx, instr);
5063 break;
5064 case MESA_SHADER_TESS_EVAL:
5065 visit_load_tes_per_vertex_input(ctx, instr);
5066 break;
5067 default:
5068 unreachable("Unimplemented shader stage");
5069 }
5070 }
5071
5072 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5073 {
5074 visit_load_tcs_output(ctx, instr, true);
5075 }
5076
5077 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5078 {
5079 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5080 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5081
5082 visit_store_tcs_output(ctx, instr, true);
5083 }
5084
5085 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5086 {
5087 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5088
5089 Builder bld(ctx->program, ctx->block);
5090 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5091
5092 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5093 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5094 Operand tes_w(0u);
5095
5096 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5097 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5098 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5099 tes_w = Operand(tmp);
5100 }
5101
5102 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5103 emit_split_vector(ctx, tess_coord, 3);
5104 }
5105
5106 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5107 {
5108 if (ctx->program->info->need_indirect_descriptor_sets) {
5109 Builder bld(ctx->program, ctx->block);
5110 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5111 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5112 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5113 }
5114
5115 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5116 }
5117
5118
5119 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5120 {
5121 Builder bld(ctx->program, ctx->block);
5122 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5123 if (!nir_dest_is_divergent(instr->dest))
5124 index = bld.as_uniform(index);
5125 unsigned desc_set = nir_intrinsic_desc_set(instr);
5126 unsigned binding = nir_intrinsic_binding(instr);
5127
5128 Temp desc_ptr;
5129 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5130 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5131 unsigned offset = layout->binding[binding].offset;
5132 unsigned stride;
5133 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5134 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5135 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5136 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5137 offset = pipeline_layout->push_constant_size + 16 * idx;
5138 stride = 16;
5139 } else {
5140 desc_ptr = load_desc_ptr(ctx, desc_set);
5141 stride = layout->binding[binding].size;
5142 }
5143
5144 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5145 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5146 if (stride != 1) {
5147 if (nir_const_index) {
5148 const_index = const_index * stride;
5149 } else if (index.type() == RegType::vgpr) {
5150 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5151 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5152 } else {
5153 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5154 }
5155 }
5156 if (offset) {
5157 if (nir_const_index) {
5158 const_index = const_index + offset;
5159 } else if (index.type() == RegType::vgpr) {
5160 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5161 } else {
5162 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5163 }
5164 }
5165
5166 if (nir_const_index && const_index == 0) {
5167 index = desc_ptr;
5168 } else if (index.type() == RegType::vgpr) {
5169 index = bld.vadd32(bld.def(v1),
5170 nir_const_index ? Operand(const_index) : Operand(index),
5171 Operand(desc_ptr));
5172 } else {
5173 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5174 nir_const_index ? Operand(const_index) : Operand(index),
5175 Operand(desc_ptr));
5176 }
5177
5178 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5179 }
5180
5181 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5182 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5183 bool glc=false, bool readonly=true, bool allow_smem=true)
5184 {
5185 Builder bld(ctx->program, ctx->block);
5186
5187 bool use_smem = dst.type() != RegType::vgpr && (!glc || ctx->options->chip_class >= GFX8) && allow_smem;
5188 if (use_smem)
5189 offset = bld.as_uniform(offset);
5190
5191 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5192 info.glc = glc;
5193 info.barrier = readonly ? barrier_none : barrier_buffer;
5194 info.can_reorder = readonly;
5195 info.align_mul = align_mul;
5196 info.align_offset = align_offset;
5197 if (use_smem)
5198 emit_smem_load(ctx, bld, &info);
5199 else
5200 emit_mubuf_load(ctx, bld, &info);
5201 }
5202
5203 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5204 {
5205 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5206 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5207
5208 Builder bld(ctx->program, ctx->block);
5209
5210 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5211 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5212 unsigned binding = nir_intrinsic_binding(idx_instr);
5213 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5214
5215 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5216 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5217 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5218 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5219 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5220 if (ctx->options->chip_class >= GFX10) {
5221 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5222 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5223 S_008F0C_RESOURCE_LEVEL(1);
5224 } else {
5225 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5226 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5227 }
5228 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5229 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5230 Operand(0xFFFFFFFFu),
5231 Operand(desc_type));
5232 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5233 rsrc, upper_dwords);
5234 } else {
5235 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5236 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5237 }
5238 unsigned size = instr->dest.ssa.bit_size / 8;
5239 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5240 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5241 }
5242
5243 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5244 {
5245 Builder bld(ctx->program, ctx->block);
5246 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5247 unsigned offset = nir_intrinsic_base(instr);
5248 unsigned count = instr->dest.ssa.num_components;
5249 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5250
5251 if (index_cv && instr->dest.ssa.bit_size == 32) {
5252 unsigned start = (offset + index_cv->u32) / 4u;
5253 start -= ctx->args->ac.base_inline_push_consts;
5254 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5255 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5256 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5257 for (unsigned i = 0; i < count; ++i) {
5258 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5259 vec->operands[i] = Operand{elems[i]};
5260 }
5261 vec->definitions[0] = Definition(dst);
5262 ctx->block->instructions.emplace_back(std::move(vec));
5263 ctx->allocated_vec.emplace(dst.id(), elems);
5264 return;
5265 }
5266 }
5267
5268 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5269 if (offset != 0) // TODO check if index != 0 as well
5270 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5271 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5272 Temp vec = dst;
5273 bool trim = false;
5274 bool aligned = true;
5275
5276 if (instr->dest.ssa.bit_size == 8) {
5277 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5278 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5279 if (!aligned)
5280 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5281 } else if (instr->dest.ssa.bit_size == 16) {
5282 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5283 if (!aligned)
5284 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5285 }
5286
5287 aco_opcode op;
5288
5289 switch (vec.size()) {
5290 case 1:
5291 op = aco_opcode::s_load_dword;
5292 break;
5293 case 2:
5294 op = aco_opcode::s_load_dwordx2;
5295 break;
5296 case 3:
5297 vec = bld.tmp(s4);
5298 trim = true;
5299 case 4:
5300 op = aco_opcode::s_load_dwordx4;
5301 break;
5302 case 6:
5303 vec = bld.tmp(s8);
5304 trim = true;
5305 case 8:
5306 op = aco_opcode::s_load_dwordx8;
5307 break;
5308 default:
5309 unreachable("unimplemented or forbidden load_push_constant.");
5310 }
5311
5312 bld.smem(op, Definition(vec), ptr, index);
5313
5314 if (!aligned) {
5315 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5316 byte_align_scalar(ctx, vec, byte_offset, dst);
5317 return;
5318 }
5319
5320 if (trim) {
5321 emit_split_vector(ctx, vec, 4);
5322 RegClass rc = dst.size() == 3 ? s1 : s2;
5323 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5324 emit_extract_vector(ctx, vec, 0, rc),
5325 emit_extract_vector(ctx, vec, 1, rc),
5326 emit_extract_vector(ctx, vec, 2, rc));
5327
5328 }
5329 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5330 }
5331
5332 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5333 {
5334 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5335
5336 Builder bld(ctx->program, ctx->block);
5337
5338 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5339 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5340 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5341 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5342 if (ctx->options->chip_class >= GFX10) {
5343 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5344 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5345 S_008F0C_RESOURCE_LEVEL(1);
5346 } else {
5347 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5348 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5349 }
5350
5351 unsigned base = nir_intrinsic_base(instr);
5352 unsigned range = nir_intrinsic_range(instr);
5353
5354 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5355 if (base && offset.type() == RegType::sgpr)
5356 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5357 else if (base && offset.type() == RegType::vgpr)
5358 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5359
5360 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5361 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5362 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5363 Operand(desc_type));
5364 unsigned size = instr->dest.ssa.bit_size / 8;
5365 // TODO: get alignment information for subdword constants
5366 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5367 }
5368
5369 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5370 {
5371 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5372 ctx->cf_info.exec_potentially_empty_discard = true;
5373
5374 ctx->program->needs_exact = true;
5375
5376 // TODO: optimize uniform conditions
5377 Builder bld(ctx->program, ctx->block);
5378 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5379 assert(src.regClass() == bld.lm);
5380 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5381 bld.pseudo(aco_opcode::p_discard_if, src);
5382 ctx->block->kind |= block_kind_uses_discard_if;
5383 return;
5384 }
5385
5386 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5387 {
5388 Builder bld(ctx->program, ctx->block);
5389
5390 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5391 ctx->cf_info.exec_potentially_empty_discard = true;
5392
5393 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5394 ctx->cf_info.parent_loop.has_divergent_continue;
5395
5396 if (ctx->block->loop_nest_depth &&
5397 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5398 /* we handle discards the same way as jump instructions */
5399 append_logical_end(ctx->block);
5400
5401 /* in loops, discard behaves like break */
5402 Block *linear_target = ctx->cf_info.parent_loop.exit;
5403 ctx->block->kind |= block_kind_discard;
5404
5405 if (!divergent) {
5406 /* uniform discard - loop ends here */
5407 assert(nir_instr_is_last(&instr->instr));
5408 ctx->block->kind |= block_kind_uniform;
5409 ctx->cf_info.has_branch = true;
5410 bld.branch(aco_opcode::p_branch);
5411 add_linear_edge(ctx->block->index, linear_target);
5412 return;
5413 }
5414
5415 /* we add a break right behind the discard() instructions */
5416 ctx->block->kind |= block_kind_break;
5417 unsigned idx = ctx->block->index;
5418
5419 ctx->cf_info.parent_loop.has_divergent_branch = true;
5420 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5421
5422 /* remove critical edges from linear CFG */
5423 bld.branch(aco_opcode::p_branch);
5424 Block* break_block = ctx->program->create_and_insert_block();
5425 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5426 break_block->kind |= block_kind_uniform;
5427 add_linear_edge(idx, break_block);
5428 add_linear_edge(break_block->index, linear_target);
5429 bld.reset(break_block);
5430 bld.branch(aco_opcode::p_branch);
5431
5432 Block* continue_block = ctx->program->create_and_insert_block();
5433 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5434 add_linear_edge(idx, continue_block);
5435 append_logical_start(continue_block);
5436 ctx->block = continue_block;
5437
5438 return;
5439 }
5440
5441 /* it can currently happen that NIR doesn't remove the unreachable code */
5442 if (!nir_instr_is_last(&instr->instr)) {
5443 ctx->program->needs_exact = true;
5444 /* save exec somewhere temporarily so that it doesn't get
5445 * overwritten before the discard from outer exec masks */
5446 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5447 bld.pseudo(aco_opcode::p_discard_if, cond);
5448 ctx->block->kind |= block_kind_uses_discard_if;
5449 return;
5450 }
5451
5452 /* This condition is incorrect for uniformly branched discards in a loop
5453 * predicated by a divergent condition, but the above code catches that case
5454 * and the discard would end up turning into a discard_if.
5455 * For example:
5456 * if (divergent) {
5457 * while (...) {
5458 * if (uniform) {
5459 * discard;
5460 * }
5461 * }
5462 * }
5463 */
5464 if (!ctx->cf_info.parent_if.is_divergent) {
5465 /* program just ends here */
5466 ctx->block->kind |= block_kind_uniform;
5467 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5468 0 /* enabled mask */, 9 /* dest */,
5469 false /* compressed */, true/* done */, true /* valid mask */);
5470 bld.sopp(aco_opcode::s_endpgm);
5471 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5472 } else {
5473 ctx->block->kind |= block_kind_discard;
5474 /* branch and linear edge is added by visit_if() */
5475 }
5476 }
5477
5478 enum aco_descriptor_type {
5479 ACO_DESC_IMAGE,
5480 ACO_DESC_FMASK,
5481 ACO_DESC_SAMPLER,
5482 ACO_DESC_BUFFER,
5483 ACO_DESC_PLANE_0,
5484 ACO_DESC_PLANE_1,
5485 ACO_DESC_PLANE_2,
5486 };
5487
5488 static bool
5489 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5490 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5491 return false;
5492 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5493 return dim == ac_image_cube ||
5494 dim == ac_image_1darray ||
5495 dim == ac_image_2darray ||
5496 dim == ac_image_2darraymsaa;
5497 }
5498
5499 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5500 enum aco_descriptor_type desc_type,
5501 const nir_tex_instr *tex_instr, bool image, bool write)
5502 {
5503 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5504 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5505 if (it != ctx->tex_desc.end())
5506 return it->second;
5507 */
5508 Temp index = Temp();
5509 bool index_set = false;
5510 unsigned constant_index = 0;
5511 unsigned descriptor_set;
5512 unsigned base_index;
5513 Builder bld(ctx->program, ctx->block);
5514
5515 if (!deref_instr) {
5516 assert(tex_instr && !image);
5517 descriptor_set = 0;
5518 base_index = tex_instr->sampler_index;
5519 } else {
5520 while(deref_instr->deref_type != nir_deref_type_var) {
5521 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5522 if (!array_size)
5523 array_size = 1;
5524
5525 assert(deref_instr->deref_type == nir_deref_type_array);
5526 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5527 if (const_value) {
5528 constant_index += array_size * const_value->u32;
5529 } else {
5530 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5531 if (indirect.type() == RegType::vgpr)
5532 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5533
5534 if (array_size != 1)
5535 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5536
5537 if (!index_set) {
5538 index = indirect;
5539 index_set = true;
5540 } else {
5541 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5542 }
5543 }
5544
5545 deref_instr = nir_src_as_deref(deref_instr->parent);
5546 }
5547 descriptor_set = deref_instr->var->data.descriptor_set;
5548 base_index = deref_instr->var->data.binding;
5549 }
5550
5551 Temp list = load_desc_ptr(ctx, descriptor_set);
5552 list = convert_pointer_to_64_bit(ctx, list);
5553
5554 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5555 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5556 unsigned offset = binding->offset;
5557 unsigned stride = binding->size;
5558 aco_opcode opcode;
5559 RegClass type;
5560
5561 assert(base_index < layout->binding_count);
5562
5563 switch (desc_type) {
5564 case ACO_DESC_IMAGE:
5565 type = s8;
5566 opcode = aco_opcode::s_load_dwordx8;
5567 break;
5568 case ACO_DESC_FMASK:
5569 type = s8;
5570 opcode = aco_opcode::s_load_dwordx8;
5571 offset += 32;
5572 break;
5573 case ACO_DESC_SAMPLER:
5574 type = s4;
5575 opcode = aco_opcode::s_load_dwordx4;
5576 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5577 offset += radv_combined_image_descriptor_sampler_offset(binding);
5578 break;
5579 case ACO_DESC_BUFFER:
5580 type = s4;
5581 opcode = aco_opcode::s_load_dwordx4;
5582 break;
5583 case ACO_DESC_PLANE_0:
5584 case ACO_DESC_PLANE_1:
5585 type = s8;
5586 opcode = aco_opcode::s_load_dwordx8;
5587 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5588 break;
5589 case ACO_DESC_PLANE_2:
5590 type = s4;
5591 opcode = aco_opcode::s_load_dwordx4;
5592 offset += 64;
5593 break;
5594 default:
5595 unreachable("invalid desc_type\n");
5596 }
5597
5598 offset += constant_index * stride;
5599
5600 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5601 (!index_set || binding->immutable_samplers_equal)) {
5602 if (binding->immutable_samplers_equal)
5603 constant_index = 0;
5604
5605 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5606 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5607 Operand(samplers[constant_index * 4 + 0]),
5608 Operand(samplers[constant_index * 4 + 1]),
5609 Operand(samplers[constant_index * 4 + 2]),
5610 Operand(samplers[constant_index * 4 + 3]));
5611 }
5612
5613 Operand off;
5614 if (!index_set) {
5615 off = bld.copy(bld.def(s1), Operand(offset));
5616 } else {
5617 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5618 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5619 }
5620
5621 Temp res = bld.smem(opcode, bld.def(type), list, off);
5622
5623 if (desc_type == ACO_DESC_PLANE_2) {
5624 Temp components[8];
5625 for (unsigned i = 0; i < 8; i++)
5626 components[i] = bld.tmp(s1);
5627 bld.pseudo(aco_opcode::p_split_vector,
5628 Definition(components[0]),
5629 Definition(components[1]),
5630 Definition(components[2]),
5631 Definition(components[3]),
5632 res);
5633
5634 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5635 bld.pseudo(aco_opcode::p_split_vector,
5636 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5637 Definition(components[4]),
5638 Definition(components[5]),
5639 Definition(components[6]),
5640 Definition(components[7]),
5641 desc2);
5642
5643 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5644 components[0], components[1], components[2], components[3],
5645 components[4], components[5], components[6], components[7]);
5646 }
5647
5648 return res;
5649 }
5650
5651 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5652 {
5653 switch (dim) {
5654 case GLSL_SAMPLER_DIM_BUF:
5655 return 1;
5656 case GLSL_SAMPLER_DIM_1D:
5657 return array ? 2 : 1;
5658 case GLSL_SAMPLER_DIM_2D:
5659 return array ? 3 : 2;
5660 case GLSL_SAMPLER_DIM_MS:
5661 return array ? 4 : 3;
5662 case GLSL_SAMPLER_DIM_3D:
5663 case GLSL_SAMPLER_DIM_CUBE:
5664 return 3;
5665 case GLSL_SAMPLER_DIM_RECT:
5666 case GLSL_SAMPLER_DIM_SUBPASS:
5667 return 2;
5668 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5669 return 3;
5670 default:
5671 break;
5672 }
5673 return 0;
5674 }
5675
5676
5677 /* Adjust the sample index according to FMASK.
5678 *
5679 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5680 * which is the identity mapping. Each nibble says which physical sample
5681 * should be fetched to get that sample.
5682 *
5683 * For example, 0x11111100 means there are only 2 samples stored and
5684 * the second sample covers 3/4 of the pixel. When reading samples 0
5685 * and 1, return physical sample 0 (determined by the first two 0s
5686 * in FMASK), otherwise return physical sample 1.
5687 *
5688 * The sample index should be adjusted as follows:
5689 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5690 */
5691 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5692 {
5693 Builder bld(ctx->program, ctx->block);
5694 Temp fmask = bld.tmp(v1);
5695 unsigned dim = ctx->options->chip_class >= GFX10
5696 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5697 : 0;
5698
5699 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5700 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5701 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5702 load->operands[0] = Operand(fmask_desc_ptr);
5703 load->operands[1] = Operand(s4); /* no sampler */
5704 load->operands[2] = Operand(coord);
5705 load->definitions[0] = Definition(fmask);
5706 load->glc = false;
5707 load->dlc = false;
5708 load->dmask = 0x1;
5709 load->unrm = true;
5710 load->da = da;
5711 load->dim = dim;
5712 load->can_reorder = true; /* fmask images shouldn't be modified */
5713 ctx->block->instructions.emplace_back(std::move(load));
5714
5715 Operand sample_index4;
5716 if (sample_index.isConstant()) {
5717 if (sample_index.constantValue() < 16) {
5718 sample_index4 = Operand(sample_index.constantValue() << 2);
5719 } else {
5720 sample_index4 = Operand(0u);
5721 }
5722 } else if (sample_index.regClass() == s1) {
5723 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5724 } else {
5725 assert(sample_index.regClass() == v1);
5726 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5727 }
5728
5729 Temp final_sample;
5730 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5731 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5732 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5733 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5734 else
5735 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5736
5737 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5738 * resource descriptor is 0 (invalid),
5739 */
5740 Temp compare = bld.tmp(bld.lm);
5741 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5742 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5743
5744 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5745
5746 /* Replace the MSAA sample index. */
5747 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5748 }
5749
5750 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5751 {
5752
5753 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5754 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5755 bool is_array = glsl_sampler_type_is_array(type);
5756 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5757 assert(!add_frag_pos && "Input attachments should be lowered.");
5758 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5759 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5760 int count = image_type_to_components_count(dim, is_array);
5761 std::vector<Temp> coords(count);
5762 Builder bld(ctx->program, ctx->block);
5763
5764 if (is_ms) {
5765 count--;
5766 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5767 /* get sample index */
5768 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5769 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5770 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5771 std::vector<Temp> fmask_load_address;
5772 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5773 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5774
5775 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5776 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5777 } else {
5778 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5779 }
5780 }
5781
5782 if (gfx9_1d) {
5783 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5784 coords.resize(coords.size() + 1);
5785 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5786 if (is_array)
5787 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5788 } else {
5789 for (int i = 0; i < count; i++)
5790 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5791 }
5792
5793 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5794 instr->intrinsic == nir_intrinsic_image_deref_store) {
5795 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5796 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5797
5798 if (!level_zero)
5799 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5800 }
5801
5802 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5803 for (unsigned i = 0; i < coords.size(); i++)
5804 vec->operands[i] = Operand(coords[i]);
5805 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5806 vec->definitions[0] = Definition(res);
5807 ctx->block->instructions.emplace_back(std::move(vec));
5808 return res;
5809 }
5810
5811
5812 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5813 {
5814 Builder bld(ctx->program, ctx->block);
5815 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5816 const struct glsl_type *type = glsl_without_array(var->type);
5817 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5818 bool is_array = glsl_sampler_type_is_array(type);
5819 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5820
5821 if (dim == GLSL_SAMPLER_DIM_BUF) {
5822 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5823 unsigned num_channels = util_last_bit(mask);
5824 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5825 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5826
5827 aco_opcode opcode;
5828 switch (num_channels) {
5829 case 1:
5830 opcode = aco_opcode::buffer_load_format_x;
5831 break;
5832 case 2:
5833 opcode = aco_opcode::buffer_load_format_xy;
5834 break;
5835 case 3:
5836 opcode = aco_opcode::buffer_load_format_xyz;
5837 break;
5838 case 4:
5839 opcode = aco_opcode::buffer_load_format_xyzw;
5840 break;
5841 default:
5842 unreachable(">4 channel buffer image load");
5843 }
5844 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5845 load->operands[0] = Operand(rsrc);
5846 load->operands[1] = Operand(vindex);
5847 load->operands[2] = Operand((uint32_t) 0);
5848 Temp tmp;
5849 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5850 tmp = dst;
5851 else
5852 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5853 load->definitions[0] = Definition(tmp);
5854 load->idxen = true;
5855 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5856 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5857 load->barrier = barrier_image;
5858 ctx->block->instructions.emplace_back(std::move(load));
5859
5860 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5861 return;
5862 }
5863
5864 Temp coords = get_image_coords(ctx, instr, type);
5865 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5866
5867 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5868 unsigned num_components = util_bitcount(dmask);
5869 Temp tmp;
5870 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5871 tmp = dst;
5872 else
5873 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5874
5875 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5876 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5877
5878 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5879 load->operands[0] = Operand(resource);
5880 load->operands[1] = Operand(s4); /* no sampler */
5881 load->operands[2] = Operand(coords);
5882 load->definitions[0] = Definition(tmp);
5883 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5884 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5885 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5886 load->dmask = dmask;
5887 load->unrm = true;
5888 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5889 load->barrier = barrier_image;
5890 ctx->block->instructions.emplace_back(std::move(load));
5891
5892 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5893 return;
5894 }
5895
5896 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5897 {
5898 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5899 const struct glsl_type *type = glsl_without_array(var->type);
5900 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5901 bool is_array = glsl_sampler_type_is_array(type);
5902 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5903
5904 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5905
5906 if (dim == GLSL_SAMPLER_DIM_BUF) {
5907 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5908 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5909 aco_opcode opcode;
5910 switch (data.size()) {
5911 case 1:
5912 opcode = aco_opcode::buffer_store_format_x;
5913 break;
5914 case 2:
5915 opcode = aco_opcode::buffer_store_format_xy;
5916 break;
5917 case 3:
5918 opcode = aco_opcode::buffer_store_format_xyz;
5919 break;
5920 case 4:
5921 opcode = aco_opcode::buffer_store_format_xyzw;
5922 break;
5923 default:
5924 unreachable(">4 channel buffer image store");
5925 }
5926 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5927 store->operands[0] = Operand(rsrc);
5928 store->operands[1] = Operand(vindex);
5929 store->operands[2] = Operand((uint32_t) 0);
5930 store->operands[3] = Operand(data);
5931 store->idxen = true;
5932 store->glc = glc;
5933 store->dlc = false;
5934 store->disable_wqm = true;
5935 store->barrier = barrier_image;
5936 ctx->program->needs_exact = true;
5937 ctx->block->instructions.emplace_back(std::move(store));
5938 return;
5939 }
5940
5941 assert(data.type() == RegType::vgpr);
5942 Temp coords = get_image_coords(ctx, instr, type);
5943 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5944
5945 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5946 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5947
5948 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5949 store->operands[0] = Operand(resource);
5950 store->operands[1] = Operand(data);
5951 store->operands[2] = Operand(coords);
5952 store->glc = glc;
5953 store->dlc = false;
5954 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5955 store->dmask = (1 << data.size()) - 1;
5956 store->unrm = true;
5957 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5958 store->disable_wqm = true;
5959 store->barrier = barrier_image;
5960 ctx->program->needs_exact = true;
5961 ctx->block->instructions.emplace_back(std::move(store));
5962 return;
5963 }
5964
5965 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5966 {
5967 /* return the previous value if dest is ever used */
5968 bool return_previous = false;
5969 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5970 return_previous = true;
5971 break;
5972 }
5973 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5974 return_previous = true;
5975 break;
5976 }
5977
5978 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5979 const struct glsl_type *type = glsl_without_array(var->type);
5980 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5981 bool is_array = glsl_sampler_type_is_array(type);
5982 Builder bld(ctx->program, ctx->block);
5983
5984 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5985 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5986
5987 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5988 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5989
5990 aco_opcode buf_op, image_op;
5991 switch (instr->intrinsic) {
5992 case nir_intrinsic_image_deref_atomic_add:
5993 buf_op = aco_opcode::buffer_atomic_add;
5994 image_op = aco_opcode::image_atomic_add;
5995 break;
5996 case nir_intrinsic_image_deref_atomic_umin:
5997 buf_op = aco_opcode::buffer_atomic_umin;
5998 image_op = aco_opcode::image_atomic_umin;
5999 break;
6000 case nir_intrinsic_image_deref_atomic_imin:
6001 buf_op = aco_opcode::buffer_atomic_smin;
6002 image_op = aco_opcode::image_atomic_smin;
6003 break;
6004 case nir_intrinsic_image_deref_atomic_umax:
6005 buf_op = aco_opcode::buffer_atomic_umax;
6006 image_op = aco_opcode::image_atomic_umax;
6007 break;
6008 case nir_intrinsic_image_deref_atomic_imax:
6009 buf_op = aco_opcode::buffer_atomic_smax;
6010 image_op = aco_opcode::image_atomic_smax;
6011 break;
6012 case nir_intrinsic_image_deref_atomic_and:
6013 buf_op = aco_opcode::buffer_atomic_and;
6014 image_op = aco_opcode::image_atomic_and;
6015 break;
6016 case nir_intrinsic_image_deref_atomic_or:
6017 buf_op = aco_opcode::buffer_atomic_or;
6018 image_op = aco_opcode::image_atomic_or;
6019 break;
6020 case nir_intrinsic_image_deref_atomic_xor:
6021 buf_op = aco_opcode::buffer_atomic_xor;
6022 image_op = aco_opcode::image_atomic_xor;
6023 break;
6024 case nir_intrinsic_image_deref_atomic_exchange:
6025 buf_op = aco_opcode::buffer_atomic_swap;
6026 image_op = aco_opcode::image_atomic_swap;
6027 break;
6028 case nir_intrinsic_image_deref_atomic_comp_swap:
6029 buf_op = aco_opcode::buffer_atomic_cmpswap;
6030 image_op = aco_opcode::image_atomic_cmpswap;
6031 break;
6032 default:
6033 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
6034 }
6035
6036 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6037
6038 if (dim == GLSL_SAMPLER_DIM_BUF) {
6039 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
6040 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
6041 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6042 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6043 mubuf->operands[0] = Operand(resource);
6044 mubuf->operands[1] = Operand(vindex);
6045 mubuf->operands[2] = Operand((uint32_t)0);
6046 mubuf->operands[3] = Operand(data);
6047 if (return_previous)
6048 mubuf->definitions[0] = Definition(dst);
6049 mubuf->offset = 0;
6050 mubuf->idxen = true;
6051 mubuf->glc = return_previous;
6052 mubuf->dlc = false; /* Not needed for atomics */
6053 mubuf->disable_wqm = true;
6054 mubuf->barrier = barrier_image;
6055 ctx->program->needs_exact = true;
6056 ctx->block->instructions.emplace_back(std::move(mubuf));
6057 return;
6058 }
6059
6060 Temp coords = get_image_coords(ctx, instr, type);
6061 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6062 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6063 mimg->operands[0] = Operand(resource);
6064 mimg->operands[1] = Operand(data);
6065 mimg->operands[2] = Operand(coords);
6066 if (return_previous)
6067 mimg->definitions[0] = Definition(dst);
6068 mimg->glc = return_previous;
6069 mimg->dlc = false; /* Not needed for atomics */
6070 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6071 mimg->dmask = (1 << data.size()) - 1;
6072 mimg->unrm = true;
6073 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6074 mimg->disable_wqm = true;
6075 mimg->barrier = barrier_image;
6076 ctx->program->needs_exact = true;
6077 ctx->block->instructions.emplace_back(std::move(mimg));
6078 return;
6079 }
6080
6081 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6082 {
6083 if (in_elements && ctx->options->chip_class == GFX8) {
6084 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6085 Builder bld(ctx->program, ctx->block);
6086
6087 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6088
6089 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6090 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6091
6092 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6093 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6094
6095 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6096 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6097
6098 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6099 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6100 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6101 if (dst.type() == RegType::vgpr)
6102 bld.copy(Definition(dst), shr_dst);
6103
6104 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6105 } else {
6106 emit_extract_vector(ctx, desc, 2, dst);
6107 }
6108 }
6109
6110 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6111 {
6112 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6113 const struct glsl_type *type = glsl_without_array(var->type);
6114 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6115 bool is_array = glsl_sampler_type_is_array(type);
6116 Builder bld(ctx->program, ctx->block);
6117
6118 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6119 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6120 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6121 }
6122
6123 /* LOD */
6124 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6125
6126 /* Resource */
6127 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6128
6129 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6130
6131 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6132 mimg->operands[0] = Operand(resource);
6133 mimg->operands[1] = Operand(s4); /* no sampler */
6134 mimg->operands[2] = Operand(lod);
6135 uint8_t& dmask = mimg->dmask;
6136 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6137 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6138 mimg->da = glsl_sampler_type_is_array(type);
6139 mimg->can_reorder = true;
6140 Definition& def = mimg->definitions[0];
6141 ctx->block->instructions.emplace_back(std::move(mimg));
6142
6143 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6144 glsl_sampler_type_is_array(type)) {
6145
6146 assert(instr->dest.ssa.num_components == 3);
6147 Temp tmp = {ctx->program->allocateId(), v3};
6148 def = Definition(tmp);
6149 emit_split_vector(ctx, tmp, 3);
6150
6151 /* divide 3rd value by 6 by multiplying with magic number */
6152 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6153 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6154
6155 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6156 emit_extract_vector(ctx, tmp, 0, v1),
6157 emit_extract_vector(ctx, tmp, 1, v1),
6158 by_6);
6159
6160 } else if (ctx->options->chip_class == GFX9 &&
6161 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6162 glsl_sampler_type_is_array(type)) {
6163 assert(instr->dest.ssa.num_components == 2);
6164 def = Definition(dst);
6165 dmask = 0x5;
6166 } else {
6167 def = Definition(dst);
6168 }
6169
6170 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6171 }
6172
6173 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6174 {
6175 Builder bld(ctx->program, ctx->block);
6176 unsigned num_components = instr->num_components;
6177
6178 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6179 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6180 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6181
6182 unsigned access = nir_intrinsic_access(instr);
6183 bool glc = access & (ACCESS_VOLATILE | ACCESS_COHERENT);
6184 unsigned size = instr->dest.ssa.bit_size / 8;
6185
6186 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[0].ssa, access);
6187 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6188 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6189 */
6190 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_store : has_vmem_store));
6191 allow_smem |= ((access & ACCESS_RESTRICT) && (access & ACCESS_NON_WRITEABLE)) || (access & ACCESS_CAN_REORDER);
6192
6193 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6194 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false, allow_smem);
6195 }
6196
6197 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6198 {
6199 Builder bld(ctx->program, ctx->block);
6200 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6201 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6202 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6203 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6204
6205 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6206 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6207
6208 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6209 uint32_t flags = get_all_buffer_resource_flags(ctx, instr->src[1].ssa, nir_intrinsic_access(instr));
6210 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6211 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6212 */
6213 bool allow_smem = !(flags & (0 && glc ? has_nonglc_vmem_loadstore : has_vmem_loadstore));
6214
6215 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6216 ctx->options->chip_class >= GFX8 &&
6217 (elem_size_bytes >= 4 || can_subdword_ssbo_store_use_smem(instr)) &&
6218 allow_smem;
6219 if (smem)
6220 offset = bld.as_uniform(offset);
6221 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6222
6223 unsigned write_count = 0;
6224 Temp write_datas[32];
6225 unsigned offsets[32];
6226 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6227 data, writemask, 16, &write_count, write_datas, offsets);
6228
6229 for (unsigned i = 0; i < write_count; i++) {
6230 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6231 if (smem && ctx->stage == fragment_fs)
6232 op = aco_opcode::p_fs_buffer_store_smem;
6233
6234 if (smem) {
6235 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6236 store->operands[0] = Operand(rsrc);
6237 if (offsets[i]) {
6238 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6239 offset, Operand(offsets[i]));
6240 store->operands[1] = Operand(off);
6241 } else {
6242 store->operands[1] = Operand(offset);
6243 }
6244 if (op != aco_opcode::p_fs_buffer_store_smem)
6245 store->operands[1].setFixed(m0);
6246 store->operands[2] = Operand(write_datas[i]);
6247 store->glc = glc;
6248 store->dlc = false;
6249 store->disable_wqm = true;
6250 store->barrier = barrier_buffer;
6251 ctx->block->instructions.emplace_back(std::move(store));
6252 ctx->program->wb_smem_l1_on_end = true;
6253 if (op == aco_opcode::p_fs_buffer_store_smem) {
6254 ctx->block->kind |= block_kind_needs_lowering;
6255 ctx->program->needs_exact = true;
6256 }
6257 } else {
6258 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6259 store->operands[0] = Operand(rsrc);
6260 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6261 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6262 store->operands[3] = Operand(write_datas[i]);
6263 store->offset = offsets[i];
6264 store->offen = (offset.type() == RegType::vgpr);
6265 store->glc = glc;
6266 store->dlc = false;
6267 store->disable_wqm = true;
6268 store->barrier = barrier_buffer;
6269 ctx->program->needs_exact = true;
6270 ctx->block->instructions.emplace_back(std::move(store));
6271 }
6272 }
6273 }
6274
6275 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6276 {
6277 /* return the previous value if dest is ever used */
6278 bool return_previous = false;
6279 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6280 return_previous = true;
6281 break;
6282 }
6283 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6284 return_previous = true;
6285 break;
6286 }
6287
6288 Builder bld(ctx->program, ctx->block);
6289 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6290
6291 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6292 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6293 get_ssa_temp(ctx, instr->src[3].ssa), data);
6294
6295 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6296 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6297 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6298
6299 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6300
6301 aco_opcode op32, op64;
6302 switch (instr->intrinsic) {
6303 case nir_intrinsic_ssbo_atomic_add:
6304 op32 = aco_opcode::buffer_atomic_add;
6305 op64 = aco_opcode::buffer_atomic_add_x2;
6306 break;
6307 case nir_intrinsic_ssbo_atomic_imin:
6308 op32 = aco_opcode::buffer_atomic_smin;
6309 op64 = aco_opcode::buffer_atomic_smin_x2;
6310 break;
6311 case nir_intrinsic_ssbo_atomic_umin:
6312 op32 = aco_opcode::buffer_atomic_umin;
6313 op64 = aco_opcode::buffer_atomic_umin_x2;
6314 break;
6315 case nir_intrinsic_ssbo_atomic_imax:
6316 op32 = aco_opcode::buffer_atomic_smax;
6317 op64 = aco_opcode::buffer_atomic_smax_x2;
6318 break;
6319 case nir_intrinsic_ssbo_atomic_umax:
6320 op32 = aco_opcode::buffer_atomic_umax;
6321 op64 = aco_opcode::buffer_atomic_umax_x2;
6322 break;
6323 case nir_intrinsic_ssbo_atomic_and:
6324 op32 = aco_opcode::buffer_atomic_and;
6325 op64 = aco_opcode::buffer_atomic_and_x2;
6326 break;
6327 case nir_intrinsic_ssbo_atomic_or:
6328 op32 = aco_opcode::buffer_atomic_or;
6329 op64 = aco_opcode::buffer_atomic_or_x2;
6330 break;
6331 case nir_intrinsic_ssbo_atomic_xor:
6332 op32 = aco_opcode::buffer_atomic_xor;
6333 op64 = aco_opcode::buffer_atomic_xor_x2;
6334 break;
6335 case nir_intrinsic_ssbo_atomic_exchange:
6336 op32 = aco_opcode::buffer_atomic_swap;
6337 op64 = aco_opcode::buffer_atomic_swap_x2;
6338 break;
6339 case nir_intrinsic_ssbo_atomic_comp_swap:
6340 op32 = aco_opcode::buffer_atomic_cmpswap;
6341 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6342 break;
6343 default:
6344 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6345 }
6346 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6347 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6348 mubuf->operands[0] = Operand(rsrc);
6349 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6350 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6351 mubuf->operands[3] = Operand(data);
6352 if (return_previous)
6353 mubuf->definitions[0] = Definition(dst);
6354 mubuf->offset = 0;
6355 mubuf->offen = (offset.type() == RegType::vgpr);
6356 mubuf->glc = return_previous;
6357 mubuf->dlc = false; /* Not needed for atomics */
6358 mubuf->disable_wqm = true;
6359 mubuf->barrier = barrier_buffer;
6360 ctx->program->needs_exact = true;
6361 ctx->block->instructions.emplace_back(std::move(mubuf));
6362 }
6363
6364 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6365
6366 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6367 Builder bld(ctx->program, ctx->block);
6368 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6369 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6370 }
6371
6372 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6373 {
6374 Builder bld(ctx->program, ctx->block);
6375 unsigned num_components = instr->num_components;
6376 unsigned component_size = instr->dest.ssa.bit_size / 8;
6377
6378 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6379 get_ssa_temp(ctx, &instr->dest.ssa),
6380 num_components, component_size};
6381 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6382 info.align_mul = nir_intrinsic_align_mul(instr);
6383 info.align_offset = nir_intrinsic_align_offset(instr);
6384 info.barrier = barrier_buffer;
6385 info.can_reorder = false;
6386 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6387 * it's safe to use SMEM */
6388 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6389 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6390 emit_global_load(ctx, bld, &info);
6391 } else {
6392 info.offset = Operand(bld.as_uniform(info.offset));
6393 emit_smem_load(ctx, bld, &info);
6394 }
6395 }
6396
6397 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6398 {
6399 Builder bld(ctx->program, ctx->block);
6400 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6401 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6402
6403 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6404 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6405 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6406
6407 if (ctx->options->chip_class >= GFX7)
6408 addr = as_vgpr(ctx, addr);
6409
6410 unsigned write_count = 0;
6411 Temp write_datas[32];
6412 unsigned offsets[32];
6413 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6414 16, &write_count, write_datas, offsets);
6415
6416 for (unsigned i = 0; i < write_count; i++) {
6417 if (ctx->options->chip_class >= GFX7) {
6418 unsigned offset = offsets[i];
6419 Temp store_addr = addr;
6420 if (offset > 0 && ctx->options->chip_class < GFX9) {
6421 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6422 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6423 Temp carry = bld.tmp(bld.lm);
6424 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6425
6426 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6427 Operand(offset), addr0);
6428 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6429 Operand(0u), addr1,
6430 carry).def(1).setHint(vcc);
6431
6432 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6433
6434 offset = 0;
6435 }
6436
6437 bool global = ctx->options->chip_class >= GFX9;
6438 aco_opcode op;
6439 switch (write_datas[i].bytes()) {
6440 case 1:
6441 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6442 break;
6443 case 2:
6444 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6445 break;
6446 case 4:
6447 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6448 break;
6449 case 8:
6450 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6451 break;
6452 case 12:
6453 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6454 break;
6455 case 16:
6456 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6457 break;
6458 default:
6459 unreachable("store_global not implemented for this size.");
6460 }
6461
6462 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6463 flat->operands[0] = Operand(store_addr);
6464 flat->operands[1] = Operand(s1);
6465 flat->operands[2] = Operand(write_datas[i]);
6466 flat->glc = glc;
6467 flat->dlc = false;
6468 flat->offset = offset;
6469 flat->disable_wqm = true;
6470 flat->barrier = barrier_buffer;
6471 ctx->program->needs_exact = true;
6472 ctx->block->instructions.emplace_back(std::move(flat));
6473 } else {
6474 assert(ctx->options->chip_class == GFX6);
6475
6476 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6477
6478 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6479
6480 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6481 mubuf->operands[0] = Operand(rsrc);
6482 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6483 mubuf->operands[2] = Operand(0u);
6484 mubuf->operands[3] = Operand(write_datas[i]);
6485 mubuf->glc = glc;
6486 mubuf->dlc = false;
6487 mubuf->offset = offsets[i];
6488 mubuf->addr64 = addr.type() == RegType::vgpr;
6489 mubuf->disable_wqm = true;
6490 mubuf->barrier = barrier_buffer;
6491 ctx->program->needs_exact = true;
6492 ctx->block->instructions.emplace_back(std::move(mubuf));
6493 }
6494 }
6495 }
6496
6497 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6498 {
6499 /* return the previous value if dest is ever used */
6500 bool return_previous = false;
6501 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6502 return_previous = true;
6503 break;
6504 }
6505 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6506 return_previous = true;
6507 break;
6508 }
6509
6510 Builder bld(ctx->program, ctx->block);
6511 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6512 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6513
6514 if (ctx->options->chip_class >= GFX7)
6515 addr = as_vgpr(ctx, addr);
6516
6517 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6518 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6519 get_ssa_temp(ctx, instr->src[2].ssa), data);
6520
6521 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6522
6523 aco_opcode op32, op64;
6524
6525 if (ctx->options->chip_class >= GFX7) {
6526 bool global = ctx->options->chip_class >= GFX9;
6527 switch (instr->intrinsic) {
6528 case nir_intrinsic_global_atomic_add:
6529 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6530 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6531 break;
6532 case nir_intrinsic_global_atomic_imin:
6533 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6534 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6535 break;
6536 case nir_intrinsic_global_atomic_umin:
6537 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6538 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6539 break;
6540 case nir_intrinsic_global_atomic_imax:
6541 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6542 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6543 break;
6544 case nir_intrinsic_global_atomic_umax:
6545 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6546 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6547 break;
6548 case nir_intrinsic_global_atomic_and:
6549 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6550 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6551 break;
6552 case nir_intrinsic_global_atomic_or:
6553 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6554 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6555 break;
6556 case nir_intrinsic_global_atomic_xor:
6557 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6558 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6559 break;
6560 case nir_intrinsic_global_atomic_exchange:
6561 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6562 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6563 break;
6564 case nir_intrinsic_global_atomic_comp_swap:
6565 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6566 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6567 break;
6568 default:
6569 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6570 }
6571
6572 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6573 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6574 flat->operands[0] = Operand(addr);
6575 flat->operands[1] = Operand(s1);
6576 flat->operands[2] = Operand(data);
6577 if (return_previous)
6578 flat->definitions[0] = Definition(dst);
6579 flat->glc = return_previous;
6580 flat->dlc = false; /* Not needed for atomics */
6581 flat->offset = 0;
6582 flat->disable_wqm = true;
6583 flat->barrier = barrier_buffer;
6584 ctx->program->needs_exact = true;
6585 ctx->block->instructions.emplace_back(std::move(flat));
6586 } else {
6587 assert(ctx->options->chip_class == GFX6);
6588
6589 switch (instr->intrinsic) {
6590 case nir_intrinsic_global_atomic_add:
6591 op32 = aco_opcode::buffer_atomic_add;
6592 op64 = aco_opcode::buffer_atomic_add_x2;
6593 break;
6594 case nir_intrinsic_global_atomic_imin:
6595 op32 = aco_opcode::buffer_atomic_smin;
6596 op64 = aco_opcode::buffer_atomic_smin_x2;
6597 break;
6598 case nir_intrinsic_global_atomic_umin:
6599 op32 = aco_opcode::buffer_atomic_umin;
6600 op64 = aco_opcode::buffer_atomic_umin_x2;
6601 break;
6602 case nir_intrinsic_global_atomic_imax:
6603 op32 = aco_opcode::buffer_atomic_smax;
6604 op64 = aco_opcode::buffer_atomic_smax_x2;
6605 break;
6606 case nir_intrinsic_global_atomic_umax:
6607 op32 = aco_opcode::buffer_atomic_umax;
6608 op64 = aco_opcode::buffer_atomic_umax_x2;
6609 break;
6610 case nir_intrinsic_global_atomic_and:
6611 op32 = aco_opcode::buffer_atomic_and;
6612 op64 = aco_opcode::buffer_atomic_and_x2;
6613 break;
6614 case nir_intrinsic_global_atomic_or:
6615 op32 = aco_opcode::buffer_atomic_or;
6616 op64 = aco_opcode::buffer_atomic_or_x2;
6617 break;
6618 case nir_intrinsic_global_atomic_xor:
6619 op32 = aco_opcode::buffer_atomic_xor;
6620 op64 = aco_opcode::buffer_atomic_xor_x2;
6621 break;
6622 case nir_intrinsic_global_atomic_exchange:
6623 op32 = aco_opcode::buffer_atomic_swap;
6624 op64 = aco_opcode::buffer_atomic_swap_x2;
6625 break;
6626 case nir_intrinsic_global_atomic_comp_swap:
6627 op32 = aco_opcode::buffer_atomic_cmpswap;
6628 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6629 break;
6630 default:
6631 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6632 }
6633
6634 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6635
6636 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6637
6638 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6639 mubuf->operands[0] = Operand(rsrc);
6640 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6641 mubuf->operands[2] = Operand(0u);
6642 mubuf->operands[3] = Operand(data);
6643 if (return_previous)
6644 mubuf->definitions[0] = Definition(dst);
6645 mubuf->glc = return_previous;
6646 mubuf->dlc = false;
6647 mubuf->offset = 0;
6648 mubuf->addr64 = addr.type() == RegType::vgpr;
6649 mubuf->disable_wqm = true;
6650 mubuf->barrier = barrier_buffer;
6651 ctx->program->needs_exact = true;
6652 ctx->block->instructions.emplace_back(std::move(mubuf));
6653 }
6654 }
6655
6656 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6657 Builder bld(ctx->program, ctx->block);
6658 switch(instr->intrinsic) {
6659 case nir_intrinsic_group_memory_barrier:
6660 case nir_intrinsic_memory_barrier:
6661 bld.barrier(aco_opcode::p_memory_barrier_common);
6662 break;
6663 case nir_intrinsic_memory_barrier_buffer:
6664 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6665 break;
6666 case nir_intrinsic_memory_barrier_image:
6667 bld.barrier(aco_opcode::p_memory_barrier_image);
6668 break;
6669 case nir_intrinsic_memory_barrier_tcs_patch:
6670 case nir_intrinsic_memory_barrier_shared:
6671 bld.barrier(aco_opcode::p_memory_barrier_shared);
6672 break;
6673 default:
6674 unreachable("Unimplemented memory barrier intrinsic");
6675 break;
6676 }
6677 }
6678
6679 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6680 {
6681 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6682 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6683 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6684 Builder bld(ctx->program, ctx->block);
6685
6686 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6687 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6688 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6689 }
6690
6691 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6692 {
6693 unsigned writemask = nir_intrinsic_write_mask(instr);
6694 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6695 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6696 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6697
6698 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6699 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6700 }
6701
6702 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6703 {
6704 unsigned offset = nir_intrinsic_base(instr);
6705 Builder bld(ctx->program, ctx->block);
6706 Operand m = load_lds_size_m0(bld);
6707 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6708 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6709
6710 unsigned num_operands = 3;
6711 aco_opcode op32, op64, op32_rtn, op64_rtn;
6712 switch(instr->intrinsic) {
6713 case nir_intrinsic_shared_atomic_add:
6714 op32 = aco_opcode::ds_add_u32;
6715 op64 = aco_opcode::ds_add_u64;
6716 op32_rtn = aco_opcode::ds_add_rtn_u32;
6717 op64_rtn = aco_opcode::ds_add_rtn_u64;
6718 break;
6719 case nir_intrinsic_shared_atomic_imin:
6720 op32 = aco_opcode::ds_min_i32;
6721 op64 = aco_opcode::ds_min_i64;
6722 op32_rtn = aco_opcode::ds_min_rtn_i32;
6723 op64_rtn = aco_opcode::ds_min_rtn_i64;
6724 break;
6725 case nir_intrinsic_shared_atomic_umin:
6726 op32 = aco_opcode::ds_min_u32;
6727 op64 = aco_opcode::ds_min_u64;
6728 op32_rtn = aco_opcode::ds_min_rtn_u32;
6729 op64_rtn = aco_opcode::ds_min_rtn_u64;
6730 break;
6731 case nir_intrinsic_shared_atomic_imax:
6732 op32 = aco_opcode::ds_max_i32;
6733 op64 = aco_opcode::ds_max_i64;
6734 op32_rtn = aco_opcode::ds_max_rtn_i32;
6735 op64_rtn = aco_opcode::ds_max_rtn_i64;
6736 break;
6737 case nir_intrinsic_shared_atomic_umax:
6738 op32 = aco_opcode::ds_max_u32;
6739 op64 = aco_opcode::ds_max_u64;
6740 op32_rtn = aco_opcode::ds_max_rtn_u32;
6741 op64_rtn = aco_opcode::ds_max_rtn_u64;
6742 break;
6743 case nir_intrinsic_shared_atomic_and:
6744 op32 = aco_opcode::ds_and_b32;
6745 op64 = aco_opcode::ds_and_b64;
6746 op32_rtn = aco_opcode::ds_and_rtn_b32;
6747 op64_rtn = aco_opcode::ds_and_rtn_b64;
6748 break;
6749 case nir_intrinsic_shared_atomic_or:
6750 op32 = aco_opcode::ds_or_b32;
6751 op64 = aco_opcode::ds_or_b64;
6752 op32_rtn = aco_opcode::ds_or_rtn_b32;
6753 op64_rtn = aco_opcode::ds_or_rtn_b64;
6754 break;
6755 case nir_intrinsic_shared_atomic_xor:
6756 op32 = aco_opcode::ds_xor_b32;
6757 op64 = aco_opcode::ds_xor_b64;
6758 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6759 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6760 break;
6761 case nir_intrinsic_shared_atomic_exchange:
6762 op32 = aco_opcode::ds_write_b32;
6763 op64 = aco_opcode::ds_write_b64;
6764 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6765 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6766 break;
6767 case nir_intrinsic_shared_atomic_comp_swap:
6768 op32 = aco_opcode::ds_cmpst_b32;
6769 op64 = aco_opcode::ds_cmpst_b64;
6770 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6771 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6772 num_operands = 4;
6773 break;
6774 default:
6775 unreachable("Unhandled shared atomic intrinsic");
6776 }
6777
6778 /* return the previous value if dest is ever used */
6779 bool return_previous = false;
6780 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6781 return_previous = true;
6782 break;
6783 }
6784 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6785 return_previous = true;
6786 break;
6787 }
6788
6789 aco_opcode op;
6790 if (data.size() == 1) {
6791 assert(instr->dest.ssa.bit_size == 32);
6792 op = return_previous ? op32_rtn : op32;
6793 } else {
6794 assert(instr->dest.ssa.bit_size == 64);
6795 op = return_previous ? op64_rtn : op64;
6796 }
6797
6798 if (offset > 65535) {
6799 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6800 offset = 0;
6801 }
6802
6803 aco_ptr<DS_instruction> ds;
6804 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6805 ds->operands[0] = Operand(address);
6806 ds->operands[1] = Operand(data);
6807 if (num_operands == 4)
6808 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6809 ds->operands[num_operands - 1] = m;
6810 ds->offset0 = offset;
6811 if (return_previous)
6812 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6813 ctx->block->instructions.emplace_back(std::move(ds));
6814 }
6815
6816 Temp get_scratch_resource(isel_context *ctx)
6817 {
6818 Builder bld(ctx->program, ctx->block);
6819 Temp scratch_addr = ctx->program->private_segment_buffer;
6820 if (ctx->stage != compute_cs)
6821 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6822
6823 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6824 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6825
6826 if (ctx->program->chip_class >= GFX10) {
6827 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6828 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6829 S_008F0C_RESOURCE_LEVEL(1);
6830 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6831 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6832 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6833 }
6834
6835 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6836 if (ctx->program->chip_class <= GFX8)
6837 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6838
6839 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6840 }
6841
6842 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6843 Builder bld(ctx->program, ctx->block);
6844 Temp rsrc = get_scratch_resource(ctx);
6845 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6846 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6847
6848 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6849 instr->dest.ssa.bit_size / 8u, rsrc};
6850 info.align_mul = nir_intrinsic_align_mul(instr);
6851 info.align_offset = nir_intrinsic_align_offset(instr);
6852 info.swizzle_component_size = 16;
6853 info.can_reorder = false;
6854 info.soffset = ctx->program->scratch_offset;
6855 emit_mubuf_load(ctx, bld, &info);
6856 }
6857
6858 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6859 Builder bld(ctx->program, ctx->block);
6860 Temp rsrc = get_scratch_resource(ctx);
6861 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6862 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6863
6864 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6865 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6866
6867 unsigned write_count = 0;
6868 Temp write_datas[32];
6869 unsigned offsets[32];
6870 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6871 16, &write_count, write_datas, offsets);
6872
6873 for (unsigned i = 0; i < write_count; i++) {
6874 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6875 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true);
6876 }
6877 }
6878
6879 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6880 uint8_t log2_ps_iter_samples;
6881 if (ctx->program->info->ps.force_persample) {
6882 log2_ps_iter_samples =
6883 util_logbase2(ctx->options->key.fs.num_samples);
6884 } else {
6885 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6886 }
6887
6888 /* The bit pattern matches that used by fixed function fragment
6889 * processing. */
6890 static const unsigned ps_iter_masks[] = {
6891 0xffff, /* not used */
6892 0x5555,
6893 0x1111,
6894 0x0101,
6895 0x0001,
6896 };
6897 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6898
6899 Builder bld(ctx->program, ctx->block);
6900
6901 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6902 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6903 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6904 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6905 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6906 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6907 }
6908
6909 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6910 Builder bld(ctx->program, ctx->block);
6911
6912 unsigned stream = nir_intrinsic_stream_id(instr);
6913 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6914 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6915 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6916
6917 /* get GSVS ring */
6918 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6919
6920 unsigned num_components =
6921 ctx->program->info->gs.num_stream_output_components[stream];
6922 assert(num_components);
6923
6924 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6925 unsigned stream_offset = 0;
6926 for (unsigned i = 0; i < stream; i++) {
6927 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6928 stream_offset += prev_stride * ctx->program->wave_size;
6929 }
6930
6931 /* Limit on the stride field for <= GFX7. */
6932 assert(stride < (1 << 14));
6933
6934 Temp gsvs_dwords[4];
6935 for (unsigned i = 0; i < 4; i++)
6936 gsvs_dwords[i] = bld.tmp(s1);
6937 bld.pseudo(aco_opcode::p_split_vector,
6938 Definition(gsvs_dwords[0]),
6939 Definition(gsvs_dwords[1]),
6940 Definition(gsvs_dwords[2]),
6941 Definition(gsvs_dwords[3]),
6942 gsvs_ring);
6943
6944 if (stream_offset) {
6945 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6946
6947 Temp carry = bld.tmp(s1);
6948 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6949 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));
6950 }
6951
6952 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)));
6953 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6954
6955 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6956 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6957
6958 unsigned offset = 0;
6959 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6960 if (ctx->program->info->gs.output_streams[i] != stream)
6961 continue;
6962
6963 for (unsigned j = 0; j < 4; j++) {
6964 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6965 continue;
6966
6967 if (ctx->outputs.mask[i] & (1 << j)) {
6968 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6969 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6970 if (const_offset >= 4096u) {
6971 if (vaddr_offset.isUndefined())
6972 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6973 else
6974 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6975 const_offset %= 4096u;
6976 }
6977
6978 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6979 mtbuf->operands[0] = Operand(gsvs_ring);
6980 mtbuf->operands[1] = vaddr_offset;
6981 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6982 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6983 mtbuf->offen = !vaddr_offset.isUndefined();
6984 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6985 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6986 mtbuf->offset = const_offset;
6987 mtbuf->glc = true;
6988 mtbuf->slc = true;
6989 mtbuf->barrier = barrier_gs_data;
6990 mtbuf->can_reorder = true;
6991 bld.insert(std::move(mtbuf));
6992 }
6993
6994 offset += ctx->shader->info.gs.vertices_out;
6995 }
6996
6997 /* outputs for the next vertex are undefined and keeping them around can
6998 * create invalid IR with control flow */
6999 ctx->outputs.mask[i] = 0;
7000 }
7001
7002 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
7003 }
7004
7005 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
7006 {
7007 Builder bld(ctx->program, ctx->block);
7008
7009 if (cluster_size == 1) {
7010 return src;
7011 } if (op == nir_op_iand && cluster_size == 4) {
7012 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7013 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7014 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
7015 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
7016 } else if (op == nir_op_ior && cluster_size == 4) {
7017 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7018 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
7019 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
7020 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
7021 //subgroupAnd(val) -> (exec & ~val) == 0
7022 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7023 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7024 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
7025 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
7026 //subgroupOr(val) -> (val & exec) != 0
7027 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
7028 return bool_to_vector_condition(ctx, tmp);
7029 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
7030 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7031 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7032 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
7033 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
7034 return bool_to_vector_condition(ctx, tmp);
7035 } else {
7036 //subgroupClustered{And,Or,Xor}(val, n) ->
7037 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7038 //cluster_offset = ~(n - 1) & lane_id
7039 //cluster_mask = ((1 << n) - 1)
7040 //subgroupClusteredAnd():
7041 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7042 //subgroupClusteredOr():
7043 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7044 //subgroupClusteredXor():
7045 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7046 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
7047 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
7048
7049 Temp tmp;
7050 if (op == nir_op_iand)
7051 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7052 else
7053 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7054
7055 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
7056
7057 if (ctx->program->chip_class <= GFX7)
7058 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
7059 else if (ctx->program->wave_size == 64)
7060 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
7061 else
7062 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
7063 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7064 if (cluster_mask != 0xffffffff)
7065 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7066
7067 Definition cmp_def = Definition();
7068 if (op == nir_op_iand) {
7069 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7070 } else if (op == nir_op_ior) {
7071 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7072 } else if (op == nir_op_ixor) {
7073 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7074 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7075 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7076 }
7077 cmp_def.setHint(vcc);
7078 return cmp_def.getTemp();
7079 }
7080 }
7081
7082 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7083 {
7084 Builder bld(ctx->program, ctx->block);
7085
7086 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7087 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7088 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7089 Temp tmp;
7090 if (op == nir_op_iand)
7091 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7092 else
7093 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7094
7095 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7096 Temp lo = lohi.def(0).getTemp();
7097 Temp hi = lohi.def(1).getTemp();
7098 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7099
7100 Definition cmp_def = Definition();
7101 if (op == nir_op_iand)
7102 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7103 else if (op == nir_op_ior)
7104 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7105 else if (op == nir_op_ixor)
7106 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7107 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7108 cmp_def.setHint(vcc);
7109 return cmp_def.getTemp();
7110 }
7111
7112 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7113 {
7114 Builder bld(ctx->program, ctx->block);
7115
7116 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7117 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7118 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7119 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7120 if (op == nir_op_iand)
7121 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7122 else if (op == nir_op_ior)
7123 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7124 else if (op == nir_op_ixor)
7125 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7126
7127 assert(false);
7128 return Temp();
7129 }
7130
7131 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7132 {
7133 Builder bld(ctx->program, ctx->block);
7134 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7135 if (src.regClass().type() == RegType::vgpr) {
7136 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7137 } else if (src.regClass() == s1) {
7138 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7139 } else if (src.regClass() == s2) {
7140 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7141 } else {
7142 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7143 nir_print_instr(&instr->instr, stderr);
7144 fprintf(stderr, "\n");
7145 }
7146 }
7147
7148 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7149 {
7150 Builder bld(ctx->program, ctx->block);
7151 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7152 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7153 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7154
7155 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7156 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7157 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7158 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7159
7160 /* Build DD X/Y */
7161 if (ctx->program->chip_class >= GFX8) {
7162 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7163 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7164 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7165 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7166 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7167 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7168 } else {
7169 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7170 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7171 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7172 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7173 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7174 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7175 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7176 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7177 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7178 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7179 }
7180
7181 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7182 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7183 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7184 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7185 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7186 Temp wqm1 = bld.tmp(v1);
7187 emit_wqm(ctx, tmp1, wqm1, true);
7188 Temp wqm2 = bld.tmp(v1);
7189 emit_wqm(ctx, tmp2, wqm2, true);
7190 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7191 return;
7192 }
7193
7194 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7195 {
7196 Builder bld(ctx->program, ctx->block);
7197 switch(instr->intrinsic) {
7198 case nir_intrinsic_load_barycentric_sample:
7199 case nir_intrinsic_load_barycentric_pixel:
7200 case nir_intrinsic_load_barycentric_centroid: {
7201 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7202 Temp bary = Temp(0, s2);
7203 switch (mode) {
7204 case INTERP_MODE_SMOOTH:
7205 case INTERP_MODE_NONE:
7206 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7207 bary = get_arg(ctx, ctx->args->ac.persp_center);
7208 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7209 bary = ctx->persp_centroid;
7210 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7211 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7212 break;
7213 case INTERP_MODE_NOPERSPECTIVE:
7214 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7215 bary = get_arg(ctx, ctx->args->ac.linear_center);
7216 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7217 bary = ctx->linear_centroid;
7218 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7219 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7220 break;
7221 default:
7222 break;
7223 }
7224 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7225 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7226 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7227 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7228 Operand(p1), Operand(p2));
7229 emit_split_vector(ctx, dst, 2);
7230 break;
7231 }
7232 case nir_intrinsic_load_barycentric_model: {
7233 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7234
7235 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7236 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7237 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7238 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7239 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7240 Operand(p1), Operand(p2), Operand(p3));
7241 emit_split_vector(ctx, dst, 3);
7242 break;
7243 }
7244 case nir_intrinsic_load_barycentric_at_sample: {
7245 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7246 switch (ctx->options->key.fs.num_samples) {
7247 case 2: sample_pos_offset += 1 << 3; break;
7248 case 4: sample_pos_offset += 3 << 3; break;
7249 case 8: sample_pos_offset += 7 << 3; break;
7250 default: break;
7251 }
7252 Temp sample_pos;
7253 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7254 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7255 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7256 if (addr.type() == RegType::sgpr) {
7257 Operand offset;
7258 if (const_addr) {
7259 sample_pos_offset += const_addr->u32 << 3;
7260 offset = Operand(sample_pos_offset);
7261 } else if (ctx->options->chip_class >= GFX9) {
7262 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7263 } else {
7264 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7265 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7266 }
7267
7268 Operand off = bld.copy(bld.def(s1), Operand(offset));
7269 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7270
7271 } else if (ctx->options->chip_class >= GFX9) {
7272 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7273 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7274 } else if (ctx->options->chip_class >= GFX7) {
7275 /* addr += private_segment_buffer + sample_pos_offset */
7276 Temp tmp0 = bld.tmp(s1);
7277 Temp tmp1 = bld.tmp(s1);
7278 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7279 Definition scc_tmp = bld.def(s1, scc);
7280 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7281 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7282 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7283 Temp pck0 = bld.tmp(v1);
7284 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7285 tmp1 = as_vgpr(ctx, tmp1);
7286 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);
7287 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7288
7289 /* sample_pos = flat_load_dwordx2 addr */
7290 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7291 } else {
7292 assert(ctx->options->chip_class == GFX6);
7293
7294 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7295 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7296 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7297
7298 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7299 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7300
7301 sample_pos = bld.tmp(v2);
7302
7303 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7304 load->definitions[0] = Definition(sample_pos);
7305 load->operands[0] = Operand(rsrc);
7306 load->operands[1] = Operand(addr);
7307 load->operands[2] = Operand(0u);
7308 load->offset = sample_pos_offset;
7309 load->offen = 0;
7310 load->addr64 = true;
7311 load->glc = false;
7312 load->dlc = false;
7313 load->disable_wqm = false;
7314 load->barrier = barrier_none;
7315 load->can_reorder = true;
7316 ctx->block->instructions.emplace_back(std::move(load));
7317 }
7318
7319 /* sample_pos -= 0.5 */
7320 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7321 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7322 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7323 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7324 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7325
7326 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7327 break;
7328 }
7329 case nir_intrinsic_load_barycentric_at_offset: {
7330 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7331 RegClass rc = RegClass(offset.type(), 1);
7332 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7333 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7334 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7335 break;
7336 }
7337 case nir_intrinsic_load_front_face: {
7338 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7339 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7340 break;
7341 }
7342 case nir_intrinsic_load_view_index: {
7343 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7344 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7345 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7346 break;
7347 }
7348
7349 /* fallthrough */
7350 }
7351 case nir_intrinsic_load_layer_id: {
7352 unsigned idx = nir_intrinsic_base(instr);
7353 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7354 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7355 break;
7356 }
7357 case nir_intrinsic_load_frag_coord: {
7358 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7359 break;
7360 }
7361 case nir_intrinsic_load_sample_pos: {
7362 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7363 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7364 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7365 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7366 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7367 break;
7368 }
7369 case nir_intrinsic_load_tess_coord:
7370 visit_load_tess_coord(ctx, instr);
7371 break;
7372 case nir_intrinsic_load_interpolated_input:
7373 visit_load_interpolated_input(ctx, instr);
7374 break;
7375 case nir_intrinsic_store_output:
7376 visit_store_output(ctx, instr);
7377 break;
7378 case nir_intrinsic_load_input:
7379 case nir_intrinsic_load_input_vertex:
7380 visit_load_input(ctx, instr);
7381 break;
7382 case nir_intrinsic_load_output:
7383 visit_load_output(ctx, instr);
7384 break;
7385 case nir_intrinsic_load_per_vertex_input:
7386 visit_load_per_vertex_input(ctx, instr);
7387 break;
7388 case nir_intrinsic_load_per_vertex_output:
7389 visit_load_per_vertex_output(ctx, instr);
7390 break;
7391 case nir_intrinsic_store_per_vertex_output:
7392 visit_store_per_vertex_output(ctx, instr);
7393 break;
7394 case nir_intrinsic_load_ubo:
7395 visit_load_ubo(ctx, instr);
7396 break;
7397 case nir_intrinsic_load_push_constant:
7398 visit_load_push_constant(ctx, instr);
7399 break;
7400 case nir_intrinsic_load_constant:
7401 visit_load_constant(ctx, instr);
7402 break;
7403 case nir_intrinsic_vulkan_resource_index:
7404 visit_load_resource(ctx, instr);
7405 break;
7406 case nir_intrinsic_discard:
7407 visit_discard(ctx, instr);
7408 break;
7409 case nir_intrinsic_discard_if:
7410 visit_discard_if(ctx, instr);
7411 break;
7412 case nir_intrinsic_load_shared:
7413 visit_load_shared(ctx, instr);
7414 break;
7415 case nir_intrinsic_store_shared:
7416 visit_store_shared(ctx, instr);
7417 break;
7418 case nir_intrinsic_shared_atomic_add:
7419 case nir_intrinsic_shared_atomic_imin:
7420 case nir_intrinsic_shared_atomic_umin:
7421 case nir_intrinsic_shared_atomic_imax:
7422 case nir_intrinsic_shared_atomic_umax:
7423 case nir_intrinsic_shared_atomic_and:
7424 case nir_intrinsic_shared_atomic_or:
7425 case nir_intrinsic_shared_atomic_xor:
7426 case nir_intrinsic_shared_atomic_exchange:
7427 case nir_intrinsic_shared_atomic_comp_swap:
7428 visit_shared_atomic(ctx, instr);
7429 break;
7430 case nir_intrinsic_image_deref_load:
7431 visit_image_load(ctx, instr);
7432 break;
7433 case nir_intrinsic_image_deref_store:
7434 visit_image_store(ctx, instr);
7435 break;
7436 case nir_intrinsic_image_deref_atomic_add:
7437 case nir_intrinsic_image_deref_atomic_umin:
7438 case nir_intrinsic_image_deref_atomic_imin:
7439 case nir_intrinsic_image_deref_atomic_umax:
7440 case nir_intrinsic_image_deref_atomic_imax:
7441 case nir_intrinsic_image_deref_atomic_and:
7442 case nir_intrinsic_image_deref_atomic_or:
7443 case nir_intrinsic_image_deref_atomic_xor:
7444 case nir_intrinsic_image_deref_atomic_exchange:
7445 case nir_intrinsic_image_deref_atomic_comp_swap:
7446 visit_image_atomic(ctx, instr);
7447 break;
7448 case nir_intrinsic_image_deref_size:
7449 visit_image_size(ctx, instr);
7450 break;
7451 case nir_intrinsic_load_ssbo:
7452 visit_load_ssbo(ctx, instr);
7453 break;
7454 case nir_intrinsic_store_ssbo:
7455 visit_store_ssbo(ctx, instr);
7456 break;
7457 case nir_intrinsic_load_global:
7458 visit_load_global(ctx, instr);
7459 break;
7460 case nir_intrinsic_store_global:
7461 visit_store_global(ctx, instr);
7462 break;
7463 case nir_intrinsic_global_atomic_add:
7464 case nir_intrinsic_global_atomic_imin:
7465 case nir_intrinsic_global_atomic_umin:
7466 case nir_intrinsic_global_atomic_imax:
7467 case nir_intrinsic_global_atomic_umax:
7468 case nir_intrinsic_global_atomic_and:
7469 case nir_intrinsic_global_atomic_or:
7470 case nir_intrinsic_global_atomic_xor:
7471 case nir_intrinsic_global_atomic_exchange:
7472 case nir_intrinsic_global_atomic_comp_swap:
7473 visit_global_atomic(ctx, instr);
7474 break;
7475 case nir_intrinsic_ssbo_atomic_add:
7476 case nir_intrinsic_ssbo_atomic_imin:
7477 case nir_intrinsic_ssbo_atomic_umin:
7478 case nir_intrinsic_ssbo_atomic_imax:
7479 case nir_intrinsic_ssbo_atomic_umax:
7480 case nir_intrinsic_ssbo_atomic_and:
7481 case nir_intrinsic_ssbo_atomic_or:
7482 case nir_intrinsic_ssbo_atomic_xor:
7483 case nir_intrinsic_ssbo_atomic_exchange:
7484 case nir_intrinsic_ssbo_atomic_comp_swap:
7485 visit_atomic_ssbo(ctx, instr);
7486 break;
7487 case nir_intrinsic_load_scratch:
7488 visit_load_scratch(ctx, instr);
7489 break;
7490 case nir_intrinsic_store_scratch:
7491 visit_store_scratch(ctx, instr);
7492 break;
7493 case nir_intrinsic_get_buffer_size:
7494 visit_get_buffer_size(ctx, instr);
7495 break;
7496 case nir_intrinsic_control_barrier: {
7497 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7498 /* GFX6 only (thanks to a hw bug workaround):
7499 * The real barrier instruction isn’t needed, because an entire patch
7500 * always fits into a single wave.
7501 */
7502 break;
7503 }
7504
7505 if (ctx->program->workgroup_size > ctx->program->wave_size)
7506 bld.sopp(aco_opcode::s_barrier);
7507
7508 break;
7509 }
7510 case nir_intrinsic_memory_barrier_tcs_patch:
7511 case nir_intrinsic_group_memory_barrier:
7512 case nir_intrinsic_memory_barrier:
7513 case nir_intrinsic_memory_barrier_buffer:
7514 case nir_intrinsic_memory_barrier_image:
7515 case nir_intrinsic_memory_barrier_shared:
7516 emit_memory_barrier(ctx, instr);
7517 break;
7518 case nir_intrinsic_load_num_work_groups: {
7519 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7520 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7521 emit_split_vector(ctx, dst, 3);
7522 break;
7523 }
7524 case nir_intrinsic_load_local_invocation_id: {
7525 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7526 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7527 emit_split_vector(ctx, dst, 3);
7528 break;
7529 }
7530 case nir_intrinsic_load_work_group_id: {
7531 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7532 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7533 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7534 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7535 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7536 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7537 emit_split_vector(ctx, dst, 3);
7538 break;
7539 }
7540 case nir_intrinsic_load_local_invocation_index: {
7541 Temp id = emit_mbcnt(ctx, bld.def(v1));
7542
7543 /* The tg_size bits [6:11] contain the subgroup id,
7544 * we need this multiplied by the wave size, and then OR the thread id to it.
7545 */
7546 if (ctx->program->wave_size == 64) {
7547 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7548 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7549 get_arg(ctx, ctx->args->ac.tg_size));
7550 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7551 } else {
7552 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7553 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7554 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7555 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7556 }
7557 break;
7558 }
7559 case nir_intrinsic_load_subgroup_id: {
7560 if (ctx->stage == compute_cs) {
7561 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7562 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7563 } else {
7564 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7565 }
7566 break;
7567 }
7568 case nir_intrinsic_load_subgroup_invocation: {
7569 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7570 break;
7571 }
7572 case nir_intrinsic_load_num_subgroups: {
7573 if (ctx->stage == compute_cs)
7574 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7575 get_arg(ctx, ctx->args->ac.tg_size));
7576 else
7577 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7578 break;
7579 }
7580 case nir_intrinsic_ballot: {
7581 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7582 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7583 Definition tmp = bld.def(dst.regClass());
7584 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7585 if (instr->src[0].ssa->bit_size == 1) {
7586 assert(src.regClass() == bld.lm);
7587 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7588 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7589 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7590 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7591 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7592 } else {
7593 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7594 nir_print_instr(&instr->instr, stderr);
7595 fprintf(stderr, "\n");
7596 }
7597 if (dst.size() != bld.lm.size()) {
7598 /* Wave32 with ballot size set to 64 */
7599 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7600 }
7601 emit_wqm(ctx, tmp.getTemp(), dst);
7602 break;
7603 }
7604 case nir_intrinsic_shuffle:
7605 case nir_intrinsic_read_invocation: {
7606 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7607 if (!nir_src_is_divergent(instr->src[0])) {
7608 emit_uniform_subgroup(ctx, instr, src);
7609 } else {
7610 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7611 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7612 tid = bld.as_uniform(tid);
7613 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7614 if (src.regClass() == v1b || src.regClass() == v2b) {
7615 Temp tmp = bld.tmp(v1);
7616 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7617 if (dst.type() == RegType::vgpr)
7618 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7619 else
7620 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7621 } else if (src.regClass() == v1) {
7622 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7623 } else if (src.regClass() == v2) {
7624 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7625 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7626 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7627 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7628 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7629 emit_split_vector(ctx, dst, 2);
7630 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7631 assert(src.regClass() == bld.lm);
7632 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7633 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7634 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7635 assert(src.regClass() == bld.lm);
7636 Temp tmp;
7637 if (ctx->program->chip_class <= GFX7)
7638 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7639 else if (ctx->program->wave_size == 64)
7640 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7641 else
7642 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7643 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7644 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7645 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7646 } else {
7647 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7648 nir_print_instr(&instr->instr, stderr);
7649 fprintf(stderr, "\n");
7650 }
7651 }
7652 break;
7653 }
7654 case nir_intrinsic_load_sample_id: {
7655 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7656 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7657 break;
7658 }
7659 case nir_intrinsic_load_sample_mask_in: {
7660 visit_load_sample_mask_in(ctx, instr);
7661 break;
7662 }
7663 case nir_intrinsic_read_first_invocation: {
7664 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7665 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7666 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7667 emit_wqm(ctx,
7668 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7669 dst);
7670 } else if (src.regClass() == v2) {
7671 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7672 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7673 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7674 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7675 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7676 emit_split_vector(ctx, dst, 2);
7677 } else if (instr->dest.ssa.bit_size == 1) {
7678 assert(src.regClass() == bld.lm);
7679 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7680 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7681 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7682 } else if (src.regClass() == s1) {
7683 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7684 } else if (src.regClass() == s2) {
7685 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7686 } else {
7687 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7688 nir_print_instr(&instr->instr, stderr);
7689 fprintf(stderr, "\n");
7690 }
7691 break;
7692 }
7693 case nir_intrinsic_vote_all: {
7694 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7695 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7696 assert(src.regClass() == bld.lm);
7697 assert(dst.regClass() == bld.lm);
7698
7699 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7700 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7701 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7702 break;
7703 }
7704 case nir_intrinsic_vote_any: {
7705 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7706 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7707 assert(src.regClass() == bld.lm);
7708 assert(dst.regClass() == bld.lm);
7709
7710 Temp tmp = bool_to_scalar_condition(ctx, src);
7711 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7712 break;
7713 }
7714 case nir_intrinsic_reduce:
7715 case nir_intrinsic_inclusive_scan:
7716 case nir_intrinsic_exclusive_scan: {
7717 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7718 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7719 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7720 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7721 nir_intrinsic_cluster_size(instr) : 0;
7722 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7723
7724 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7725 emit_uniform_subgroup(ctx, instr, src);
7726 } else if (instr->dest.ssa.bit_size == 1) {
7727 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7728 op = nir_op_iand;
7729 else if (op == nir_op_iadd)
7730 op = nir_op_ixor;
7731 else if (op == nir_op_umax || op == nir_op_imax)
7732 op = nir_op_ior;
7733 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7734
7735 switch (instr->intrinsic) {
7736 case nir_intrinsic_reduce:
7737 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7738 break;
7739 case nir_intrinsic_exclusive_scan:
7740 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7741 break;
7742 case nir_intrinsic_inclusive_scan:
7743 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7744 break;
7745 default:
7746 assert(false);
7747 }
7748 } else if (cluster_size == 1) {
7749 bld.copy(Definition(dst), src);
7750 } else {
7751 unsigned bit_size = instr->src[0].ssa->bit_size;
7752
7753 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7754
7755 ReduceOp reduce_op;
7756 switch (op) {
7757 #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;
7758 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7759 CASEI(iadd)
7760 CASEI(imul)
7761 CASEI(imin)
7762 CASEI(umin)
7763 CASEI(imax)
7764 CASEI(umax)
7765 CASEI(iand)
7766 CASEI(ior)
7767 CASEI(ixor)
7768 CASEF(fadd)
7769 CASEF(fmul)
7770 CASEF(fmin)
7771 CASEF(fmax)
7772 default:
7773 unreachable("unknown reduction op");
7774 #undef CASEI
7775 #undef CASEF
7776 }
7777
7778 aco_opcode aco_op;
7779 switch (instr->intrinsic) {
7780 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7781 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7782 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7783 default:
7784 unreachable("unknown reduce intrinsic");
7785 }
7786
7787 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7788 reduce->operands[0] = Operand(src);
7789 // filled in by aco_reduce_assign.cpp, used internally as part of the
7790 // reduce sequence
7791 assert(dst.size() == 1 || dst.size() == 2);
7792 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7793 reduce->operands[2] = Operand(v1.as_linear());
7794
7795 Temp tmp_dst = bld.tmp(dst.regClass());
7796 reduce->definitions[0] = Definition(tmp_dst);
7797 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7798 reduce->definitions[2] = Definition();
7799 reduce->definitions[3] = Definition(scc, s1);
7800 reduce->definitions[4] = Definition();
7801 reduce->reduce_op = reduce_op;
7802 reduce->cluster_size = cluster_size;
7803 ctx->block->instructions.emplace_back(std::move(reduce));
7804
7805 emit_wqm(ctx, tmp_dst, dst);
7806 }
7807 break;
7808 }
7809 case nir_intrinsic_quad_broadcast: {
7810 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7811 if (!nir_dest_is_divergent(instr->dest)) {
7812 emit_uniform_subgroup(ctx, instr, src);
7813 } else {
7814 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7815 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7816 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7817
7818 if (instr->dest.ssa.bit_size == 1) {
7819 assert(src.regClass() == bld.lm);
7820 assert(dst.regClass() == bld.lm);
7821 uint32_t half_mask = 0x11111111u << lane;
7822 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7823 Temp tmp = bld.tmp(bld.lm);
7824 bld.sop1(Builder::s_wqm, Definition(tmp),
7825 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7826 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7827 emit_wqm(ctx, tmp, dst);
7828 } else if (instr->dest.ssa.bit_size == 8) {
7829 Temp tmp = bld.tmp(v1);
7830 if (ctx->program->chip_class >= GFX8)
7831 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7832 else
7833 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7834 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7835 } else if (instr->dest.ssa.bit_size == 16) {
7836 Temp tmp = bld.tmp(v1);
7837 if (ctx->program->chip_class >= GFX8)
7838 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7839 else
7840 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7841 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7842 } else if (instr->dest.ssa.bit_size == 32) {
7843 if (ctx->program->chip_class >= GFX8)
7844 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7845 else
7846 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7847 } else if (instr->dest.ssa.bit_size == 64) {
7848 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7849 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7850 if (ctx->program->chip_class >= GFX8) {
7851 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7852 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7853 } else {
7854 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7855 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7856 }
7857 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7858 emit_split_vector(ctx, dst, 2);
7859 } else {
7860 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7861 nir_print_instr(&instr->instr, stderr);
7862 fprintf(stderr, "\n");
7863 }
7864 }
7865 break;
7866 }
7867 case nir_intrinsic_quad_swap_horizontal:
7868 case nir_intrinsic_quad_swap_vertical:
7869 case nir_intrinsic_quad_swap_diagonal:
7870 case nir_intrinsic_quad_swizzle_amd: {
7871 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7872 if (!nir_dest_is_divergent(instr->dest)) {
7873 emit_uniform_subgroup(ctx, instr, src);
7874 break;
7875 }
7876 uint16_t dpp_ctrl = 0;
7877 switch (instr->intrinsic) {
7878 case nir_intrinsic_quad_swap_horizontal:
7879 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7880 break;
7881 case nir_intrinsic_quad_swap_vertical:
7882 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7883 break;
7884 case nir_intrinsic_quad_swap_diagonal:
7885 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7886 break;
7887 case nir_intrinsic_quad_swizzle_amd:
7888 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7889 break;
7890 default:
7891 break;
7892 }
7893 if (ctx->program->chip_class < GFX8)
7894 dpp_ctrl |= (1 << 15);
7895
7896 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7897 if (instr->dest.ssa.bit_size == 1) {
7898 assert(src.regClass() == bld.lm);
7899 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7900 if (ctx->program->chip_class >= GFX8)
7901 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7902 else
7903 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7904 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7905 emit_wqm(ctx, tmp, dst);
7906 } else if (instr->dest.ssa.bit_size == 8) {
7907 Temp tmp = bld.tmp(v1);
7908 if (ctx->program->chip_class >= GFX8)
7909 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7910 else
7911 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7912 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7913 } else if (instr->dest.ssa.bit_size == 16) {
7914 Temp tmp = bld.tmp(v1);
7915 if (ctx->program->chip_class >= GFX8)
7916 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7917 else
7918 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7919 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7920 } else if (instr->dest.ssa.bit_size == 32) {
7921 Temp tmp;
7922 if (ctx->program->chip_class >= GFX8)
7923 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7924 else
7925 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7926 emit_wqm(ctx, tmp, dst);
7927 } else if (instr->dest.ssa.bit_size == 64) {
7928 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7929 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7930 if (ctx->program->chip_class >= GFX8) {
7931 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7932 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7933 } else {
7934 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7935 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7936 }
7937 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7938 emit_split_vector(ctx, dst, 2);
7939 } else {
7940 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7941 nir_print_instr(&instr->instr, stderr);
7942 fprintf(stderr, "\n");
7943 }
7944 break;
7945 }
7946 case nir_intrinsic_masked_swizzle_amd: {
7947 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7948 if (!nir_dest_is_divergent(instr->dest)) {
7949 emit_uniform_subgroup(ctx, instr, src);
7950 break;
7951 }
7952 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7953 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7954 if (instr->dest.ssa.bit_size == 1) {
7955 assert(src.regClass() == bld.lm);
7956 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7957 src = emit_masked_swizzle(ctx, bld, src, mask);
7958 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7959 emit_wqm(ctx, tmp, dst);
7960 } else if (dst.regClass() == v1b) {
7961 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7962 emit_extract_vector(ctx, tmp, 0, dst);
7963 } else if (dst.regClass() == v2b) {
7964 Temp tmp = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask));
7965 emit_extract_vector(ctx, tmp, 0, dst);
7966 } else if (dst.regClass() == v1) {
7967 emit_wqm(ctx, emit_masked_swizzle(ctx, bld, src, mask), dst);
7968 } else if (dst.regClass() == v2) {
7969 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7970 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7971 lo = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, lo, mask));
7972 hi = emit_wqm(ctx, emit_masked_swizzle(ctx, bld, hi, mask));
7973 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7974 emit_split_vector(ctx, dst, 2);
7975 } else {
7976 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7977 nir_print_instr(&instr->instr, stderr);
7978 fprintf(stderr, "\n");
7979 }
7980 break;
7981 }
7982 case nir_intrinsic_write_invocation_amd: {
7983 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7984 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7985 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7986 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7987 if (dst.regClass() == v1) {
7988 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7989 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7990 } else if (dst.regClass() == v2) {
7991 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7992 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7993 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7994 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7995 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7996 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7997 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7998 emit_split_vector(ctx, dst, 2);
7999 } else {
8000 fprintf(stderr, "Unimplemented NIR instr bit size: ");
8001 nir_print_instr(&instr->instr, stderr);
8002 fprintf(stderr, "\n");
8003 }
8004 break;
8005 }
8006 case nir_intrinsic_mbcnt_amd: {
8007 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8008 RegClass rc = RegClass(src.type(), 1);
8009 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
8010 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
8011 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8012 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
8013 emit_wqm(ctx, wqm_tmp, dst);
8014 break;
8015 }
8016 case nir_intrinsic_load_helper_invocation: {
8017 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8018 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
8019 ctx->block->kind |= block_kind_needs_lowering;
8020 ctx->program->needs_exact = true;
8021 break;
8022 }
8023 case nir_intrinsic_is_helper_invocation: {
8024 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8025 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
8026 ctx->block->kind |= block_kind_needs_lowering;
8027 ctx->program->needs_exact = true;
8028 break;
8029 }
8030 case nir_intrinsic_demote:
8031 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
8032
8033 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8034 ctx->cf_info.exec_potentially_empty_discard = true;
8035 ctx->block->kind |= block_kind_uses_demote;
8036 ctx->program->needs_exact = true;
8037 break;
8038 case nir_intrinsic_demote_if: {
8039 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
8040 assert(src.regClass() == bld.lm);
8041 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
8042 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
8043
8044 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
8045 ctx->cf_info.exec_potentially_empty_discard = true;
8046 ctx->block->kind |= block_kind_uses_demote;
8047 ctx->program->needs_exact = true;
8048 break;
8049 }
8050 case nir_intrinsic_first_invocation: {
8051 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
8052 get_ssa_temp(ctx, &instr->dest.ssa));
8053 break;
8054 }
8055 case nir_intrinsic_shader_clock: {
8056 aco_opcode opcode =
8057 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
8058 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
8059 bld.smem(opcode, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
8060 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
8061 break;
8062 }
8063 case nir_intrinsic_load_vertex_id_zero_base: {
8064 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8065 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
8066 break;
8067 }
8068 case nir_intrinsic_load_first_vertex: {
8069 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8070 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
8071 break;
8072 }
8073 case nir_intrinsic_load_base_instance: {
8074 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8075 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
8076 break;
8077 }
8078 case nir_intrinsic_load_instance_id: {
8079 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8080 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
8081 break;
8082 }
8083 case nir_intrinsic_load_draw_id: {
8084 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8085 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8086 break;
8087 }
8088 case nir_intrinsic_load_invocation_id: {
8089 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8090
8091 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8092 if (ctx->options->chip_class >= GFX10)
8093 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8094 else
8095 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8096 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8097 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8098 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8099 } else {
8100 unreachable("Unsupported stage for load_invocation_id");
8101 }
8102
8103 break;
8104 }
8105 case nir_intrinsic_load_primitive_id: {
8106 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8107
8108 switch (ctx->shader->info.stage) {
8109 case MESA_SHADER_GEOMETRY:
8110 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8111 break;
8112 case MESA_SHADER_TESS_CTRL:
8113 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8114 break;
8115 case MESA_SHADER_TESS_EVAL:
8116 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8117 break;
8118 default:
8119 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8120 }
8121
8122 break;
8123 }
8124 case nir_intrinsic_load_patch_vertices_in: {
8125 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8126 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8127
8128 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8129 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8130 break;
8131 }
8132 case nir_intrinsic_emit_vertex_with_counter: {
8133 visit_emit_vertex_with_counter(ctx, instr);
8134 break;
8135 }
8136 case nir_intrinsic_end_primitive_with_counter: {
8137 unsigned stream = nir_intrinsic_stream_id(instr);
8138 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8139 break;
8140 }
8141 case nir_intrinsic_set_vertex_count: {
8142 /* unused, the HW keeps track of this for us */
8143 break;
8144 }
8145 default:
8146 fprintf(stderr, "Unimplemented intrinsic instr: ");
8147 nir_print_instr(&instr->instr, stderr);
8148 fprintf(stderr, "\n");
8149 abort();
8150
8151 break;
8152 }
8153 }
8154
8155
8156 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8157 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8158 enum glsl_base_type *stype)
8159 {
8160 nir_deref_instr *texture_deref_instr = NULL;
8161 nir_deref_instr *sampler_deref_instr = NULL;
8162 int plane = -1;
8163
8164 for (unsigned i = 0; i < instr->num_srcs; i++) {
8165 switch (instr->src[i].src_type) {
8166 case nir_tex_src_texture_deref:
8167 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8168 break;
8169 case nir_tex_src_sampler_deref:
8170 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8171 break;
8172 case nir_tex_src_plane:
8173 plane = nir_src_as_int(instr->src[i].src);
8174 break;
8175 default:
8176 break;
8177 }
8178 }
8179
8180 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8181
8182 if (!sampler_deref_instr)
8183 sampler_deref_instr = texture_deref_instr;
8184
8185 if (plane >= 0) {
8186 assert(instr->op != nir_texop_txf_ms &&
8187 instr->op != nir_texop_samples_identical);
8188 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8189 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8190 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8191 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8192 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8193 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8194 } else {
8195 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8196 }
8197 if (samp_ptr) {
8198 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8199
8200 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8201 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8202 Builder bld(ctx->program, ctx->block);
8203
8204 /* to avoid unnecessary moves, we split and recombine sampler and image */
8205 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8206 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8207 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8208 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8209 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8210 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8211 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8212 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8213
8214 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8215 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8216 img[0], img[1], img[2], img[3],
8217 img[4], img[5], img[6], img[7]);
8218 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8219 samp[0], samp[1], samp[2], samp[3]);
8220 }
8221 }
8222 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8223 instr->op == nir_texop_samples_identical))
8224 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8225 }
8226
8227 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8228 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8229 {
8230 Builder bld(ctx->program, ctx->block);
8231
8232 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8233 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8234 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8235
8236 Operand neg_one(0xbf800000u);
8237 Operand one(0x3f800000u);
8238 Operand two(0x40000000u);
8239 Operand four(0x40800000u);
8240
8241 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8242 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8243 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8244
8245 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8246 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8247 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8248 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);
8249
8250 // select sc
8251 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8252 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8253 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8254 one, is_ma_y);
8255 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8256
8257 // select tc
8258 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8259 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8260 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8261
8262 // select ma
8263 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8264 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8265 deriv_z, is_ma_z);
8266 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8267 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8268 }
8269
8270 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8271 {
8272 Builder bld(ctx->program, ctx->block);
8273 Temp ma, tc, sc, id;
8274
8275 if (is_array) {
8276 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8277
8278 // see comment in ac_prepare_cube_coords()
8279 if (ctx->options->chip_class <= GFX8)
8280 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8281 }
8282
8283 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8284
8285 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8286 vop3a->operands[0] = Operand(ma);
8287 vop3a->abs[0] = true;
8288 Temp invma = bld.tmp(v1);
8289 vop3a->definitions[0] = Definition(invma);
8290 ctx->block->instructions.emplace_back(std::move(vop3a));
8291
8292 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8293 if (!is_deriv)
8294 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8295
8296 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8297 if (!is_deriv)
8298 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8299
8300 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8301
8302 if (is_deriv) {
8303 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8304 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8305
8306 for (unsigned i = 0; i < 2; i++) {
8307 // see comment in ac_prepare_cube_coords()
8308 Temp deriv_ma;
8309 Temp deriv_sc, deriv_tc;
8310 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8311 &deriv_ma, &deriv_sc, &deriv_tc);
8312
8313 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8314
8315 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8316 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8317 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8318 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8319 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8320 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8321 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8322 }
8323
8324 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8325 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8326 }
8327
8328 if (is_array)
8329 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8330 coords.resize(3);
8331 coords[0] = sc;
8332 coords[1] = tc;
8333 coords[2] = id;
8334 }
8335
8336 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8337 {
8338 if (vec->parent_instr->type != nir_instr_type_alu)
8339 return;
8340 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8341 if (vec_instr->op != nir_op_vec(vec->num_components))
8342 return;
8343
8344 for (unsigned i = 0; i < vec->num_components; i++) {
8345 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8346 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8347 }
8348 }
8349
8350 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8351 {
8352 Builder bld(ctx->program, ctx->block);
8353 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8354 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8355 has_clamped_lod = false;
8356 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8357 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8358 clamped_lod = Temp();
8359 std::vector<Temp> coords;
8360 std::vector<Temp> derivs;
8361 nir_const_value *sample_index_cv = NULL;
8362 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8363 enum glsl_base_type stype;
8364 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8365
8366 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8367 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8368 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8369 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8370
8371 for (unsigned i = 0; i < instr->num_srcs; i++) {
8372 switch (instr->src[i].src_type) {
8373 case nir_tex_src_coord: {
8374 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8375 for (unsigned i = 0; i < coord.size(); i++)
8376 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8377 break;
8378 }
8379 case nir_tex_src_bias:
8380 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8381 has_bias = true;
8382 break;
8383 case nir_tex_src_lod: {
8384 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8385
8386 if (val && val->f32 <= 0.0) {
8387 level_zero = true;
8388 } else {
8389 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8390 has_lod = true;
8391 }
8392 break;
8393 }
8394 case nir_tex_src_min_lod:
8395 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8396 has_clamped_lod = true;
8397 break;
8398 case nir_tex_src_comparator:
8399 if (instr->is_shadow) {
8400 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8401 has_compare = true;
8402 }
8403 break;
8404 case nir_tex_src_offset:
8405 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8406 get_const_vec(instr->src[i].src.ssa, const_offset);
8407 has_offset = true;
8408 break;
8409 case nir_tex_src_ddx:
8410 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8411 has_ddx = true;
8412 break;
8413 case nir_tex_src_ddy:
8414 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8415 has_ddy = true;
8416 break;
8417 case nir_tex_src_ms_index:
8418 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8419 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8420 has_sample_index = true;
8421 break;
8422 case nir_tex_src_texture_offset:
8423 case nir_tex_src_sampler_offset:
8424 default:
8425 break;
8426 }
8427 }
8428
8429 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8430 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8431
8432 if (instr->op == nir_texop_texture_samples) {
8433 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8434
8435 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8436 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8437 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 */));
8438
8439 Operand default_sample = Operand(1u);
8440 if (ctx->options->robust_buffer_access) {
8441 /* Extract the second dword of the descriptor, if it's
8442 * all zero, then it's a null descriptor.
8443 */
8444 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8445 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8446 default_sample = Operand(is_non_null_descriptor);
8447 }
8448
8449 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8450 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8451 samples, default_sample, bld.scc(is_msaa));
8452 return;
8453 }
8454
8455 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8456 aco_ptr<Instruction> tmp_instr;
8457 Temp acc, pack = Temp();
8458
8459 uint32_t pack_const = 0;
8460 for (unsigned i = 0; i < offset.size(); i++) {
8461 if (!const_offset[i])
8462 continue;
8463 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8464 }
8465
8466 if (offset.type() == RegType::sgpr) {
8467 for (unsigned i = 0; i < offset.size(); i++) {
8468 if (const_offset[i])
8469 continue;
8470
8471 acc = emit_extract_vector(ctx, offset, i, s1);
8472 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8473
8474 if (i) {
8475 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8476 }
8477
8478 if (pack == Temp()) {
8479 pack = acc;
8480 } else {
8481 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8482 }
8483 }
8484
8485 if (pack_const && pack != Temp())
8486 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8487 } else {
8488 for (unsigned i = 0; i < offset.size(); i++) {
8489 if (const_offset[i])
8490 continue;
8491
8492 acc = emit_extract_vector(ctx, offset, i, v1);
8493 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8494
8495 if (i) {
8496 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8497 }
8498
8499 if (pack == Temp()) {
8500 pack = acc;
8501 } else {
8502 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8503 }
8504 }
8505
8506 if (pack_const && pack != Temp())
8507 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8508 }
8509 if (pack_const && pack == Temp())
8510 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8511 else if (pack == Temp())
8512 has_offset = false;
8513 else
8514 offset = pack;
8515 }
8516
8517 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8518 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8519
8520 /* pack derivatives */
8521 if (has_ddx || has_ddy) {
8522 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8523 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8524 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8525 derivs = {ddx, zero, ddy, zero};
8526 } else {
8527 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8528 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8529 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8530 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8531 }
8532 has_derivs = true;
8533 }
8534
8535 if (instr->coord_components > 1 &&
8536 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8537 instr->is_array &&
8538 instr->op != nir_texop_txf)
8539 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8540
8541 if (instr->coord_components > 2 &&
8542 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8543 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8544 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8545 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8546 instr->is_array &&
8547 instr->op != nir_texop_txf &&
8548 instr->op != nir_texop_txf_ms &&
8549 instr->op != nir_texop_fragment_fetch &&
8550 instr->op != nir_texop_fragment_mask_fetch)
8551 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8552
8553 if (ctx->options->chip_class == GFX9 &&
8554 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8555 instr->op != nir_texop_lod && instr->coord_components) {
8556 assert(coords.size() > 0 && coords.size() < 3);
8557
8558 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8559 Operand((uint32_t) 0) :
8560 Operand((uint32_t) 0x3f000000)));
8561 }
8562
8563 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8564
8565 if (instr->op == nir_texop_samples_identical)
8566 resource = fmask_ptr;
8567
8568 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8569 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8570 instr->op != nir_texop_txs &&
8571 instr->op != nir_texop_fragment_fetch &&
8572 instr->op != nir_texop_fragment_mask_fetch) {
8573 assert(has_sample_index);
8574 Operand op(sample_index);
8575 if (sample_index_cv)
8576 op = Operand(sample_index_cv->u32);
8577 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8578 }
8579
8580 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8581 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8582 Temp off = emit_extract_vector(ctx, offset, i, v1);
8583 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8584 }
8585 has_offset = false;
8586 }
8587
8588 /* Build tex instruction */
8589 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8590 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8591 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8592 : 0;
8593 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8594 Temp tmp_dst = dst;
8595
8596 /* gather4 selects the component by dmask and always returns vec4 */
8597 if (instr->op == nir_texop_tg4) {
8598 assert(instr->dest.ssa.num_components == 4);
8599 if (instr->is_shadow)
8600 dmask = 1;
8601 else
8602 dmask = 1 << instr->component;
8603 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8604 tmp_dst = bld.tmp(v4);
8605 } else if (instr->op == nir_texop_samples_identical) {
8606 tmp_dst = bld.tmp(v1);
8607 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8608 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8609 }
8610
8611 aco_ptr<MIMG_instruction> tex;
8612 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8613 if (!has_lod)
8614 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8615
8616 bool div_by_6 = instr->op == nir_texop_txs &&
8617 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8618 instr->is_array &&
8619 (dmask & (1 << 2));
8620 if (tmp_dst.id() == dst.id() && div_by_6)
8621 tmp_dst = bld.tmp(tmp_dst.regClass());
8622
8623 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8624 tex->operands[0] = Operand(resource);
8625 tex->operands[1] = Operand(s4); /* no sampler */
8626 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8627 if (ctx->options->chip_class == GFX9 &&
8628 instr->op == nir_texop_txs &&
8629 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8630 instr->is_array) {
8631 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8632 } else if (instr->op == nir_texop_query_levels) {
8633 tex->dmask = 1 << 3;
8634 } else {
8635 tex->dmask = dmask;
8636 }
8637 tex->da = da;
8638 tex->definitions[0] = Definition(tmp_dst);
8639 tex->dim = dim;
8640 tex->can_reorder = true;
8641 ctx->block->instructions.emplace_back(std::move(tex));
8642
8643 if (div_by_6) {
8644 /* divide 3rd value by 6 by multiplying with magic number */
8645 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8646 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8647 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8648 assert(instr->dest.ssa.num_components == 3);
8649 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8650 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8651 emit_extract_vector(ctx, tmp_dst, 0, v1),
8652 emit_extract_vector(ctx, tmp_dst, 1, v1),
8653 by_6);
8654
8655 }
8656
8657 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8658 return;
8659 }
8660
8661 Temp tg4_compare_cube_wa64 = Temp();
8662
8663 if (tg4_integer_workarounds) {
8664 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8665 tex->operands[0] = Operand(resource);
8666 tex->operands[1] = Operand(s4); /* no sampler */
8667 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8668 tex->dim = dim;
8669 tex->dmask = 0x3;
8670 tex->da = da;
8671 Temp size = bld.tmp(v2);
8672 tex->definitions[0] = Definition(size);
8673 tex->can_reorder = true;
8674 ctx->block->instructions.emplace_back(std::move(tex));
8675 emit_split_vector(ctx, size, size.size());
8676
8677 Temp half_texel[2];
8678 for (unsigned i = 0; i < 2; i++) {
8679 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8680 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8681 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8682 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8683 }
8684
8685 Temp new_coords[2] = {
8686 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8687 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8688 };
8689
8690 if (tg4_integer_cube_workaround) {
8691 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8692 Temp desc[resource.size()];
8693 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8694 Format::PSEUDO, 1, resource.size())};
8695 split->operands[0] = Operand(resource);
8696 for (unsigned i = 0; i < resource.size(); i++) {
8697 desc[i] = bld.tmp(s1);
8698 split->definitions[i] = Definition(desc[i]);
8699 }
8700 ctx->block->instructions.emplace_back(std::move(split));
8701
8702 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8703 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8704 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8705
8706 Temp nfmt;
8707 if (stype == GLSL_TYPE_UINT) {
8708 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8709 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8710 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8711 bld.scc(compare_cube_wa));
8712 } else {
8713 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8714 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8715 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8716 bld.scc(compare_cube_wa));
8717 }
8718 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8719 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8720
8721 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8722
8723 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8724 Operand((uint32_t)C_008F14_NUM_FORMAT));
8725 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8726
8727 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8728 Format::PSEUDO, resource.size(), 1)};
8729 for (unsigned i = 0; i < resource.size(); i++)
8730 vec->operands[i] = Operand(desc[i]);
8731 resource = bld.tmp(resource.regClass());
8732 vec->definitions[0] = Definition(resource);
8733 ctx->block->instructions.emplace_back(std::move(vec));
8734
8735 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8736 new_coords[0], coords[0], tg4_compare_cube_wa64);
8737 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8738 new_coords[1], coords[1], tg4_compare_cube_wa64);
8739 }
8740 coords[0] = new_coords[0];
8741 coords[1] = new_coords[1];
8742 }
8743
8744 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8745 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8746
8747 assert(coords.size() == 1);
8748 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8749 aco_opcode op;
8750 switch (last_bit) {
8751 case 1:
8752 op = aco_opcode::buffer_load_format_x; break;
8753 case 2:
8754 op = aco_opcode::buffer_load_format_xy; break;
8755 case 3:
8756 op = aco_opcode::buffer_load_format_xyz; break;
8757 case 4:
8758 op = aco_opcode::buffer_load_format_xyzw; break;
8759 default:
8760 unreachable("Tex instruction loads more than 4 components.");
8761 }
8762
8763 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8764 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8765 tmp_dst = dst;
8766 else
8767 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8768
8769 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8770 mubuf->operands[0] = Operand(resource);
8771 mubuf->operands[1] = Operand(coords[0]);
8772 mubuf->operands[2] = Operand((uint32_t) 0);
8773 mubuf->definitions[0] = Definition(tmp_dst);
8774 mubuf->idxen = true;
8775 mubuf->can_reorder = true;
8776 ctx->block->instructions.emplace_back(std::move(mubuf));
8777
8778 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8779 return;
8780 }
8781
8782 /* gather MIMG address components */
8783 std::vector<Temp> args;
8784 if (has_offset)
8785 args.emplace_back(offset);
8786 if (has_bias)
8787 args.emplace_back(bias);
8788 if (has_compare)
8789 args.emplace_back(compare);
8790 if (has_derivs)
8791 args.insert(args.end(), derivs.begin(), derivs.end());
8792
8793 args.insert(args.end(), coords.begin(), coords.end());
8794 if (has_sample_index)
8795 args.emplace_back(sample_index);
8796 if (has_lod)
8797 args.emplace_back(lod);
8798 if (has_clamped_lod)
8799 args.emplace_back(clamped_lod);
8800
8801 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8802 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8803 vec->definitions[0] = Definition(arg);
8804 for (unsigned i = 0; i < args.size(); i++)
8805 vec->operands[i] = Operand(args[i]);
8806 ctx->block->instructions.emplace_back(std::move(vec));
8807
8808
8809 if (instr->op == nir_texop_txf ||
8810 instr->op == nir_texop_txf_ms ||
8811 instr->op == nir_texop_samples_identical ||
8812 instr->op == nir_texop_fragment_fetch ||
8813 instr->op == nir_texop_fragment_mask_fetch) {
8814 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;
8815 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8816 tex->operands[0] = Operand(resource);
8817 tex->operands[1] = Operand(s4); /* no sampler */
8818 tex->operands[2] = Operand(arg);
8819 tex->dim = dim;
8820 tex->dmask = dmask;
8821 tex->unrm = true;
8822 tex->da = da;
8823 tex->definitions[0] = Definition(tmp_dst);
8824 tex->can_reorder = true;
8825 ctx->block->instructions.emplace_back(std::move(tex));
8826
8827 if (instr->op == nir_texop_samples_identical) {
8828 assert(dmask == 1 && dst.regClass() == v1);
8829 assert(dst.id() != tmp_dst.id());
8830
8831 Temp tmp = bld.tmp(bld.lm);
8832 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8833 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8834
8835 } else {
8836 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8837 }
8838 return;
8839 }
8840
8841 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8842 aco_opcode opcode = aco_opcode::image_sample;
8843 if (has_offset) { /* image_sample_*_o */
8844 if (has_clamped_lod) {
8845 if (has_compare) {
8846 opcode = aco_opcode::image_sample_c_cl_o;
8847 if (has_derivs)
8848 opcode = aco_opcode::image_sample_c_d_cl_o;
8849 if (has_bias)
8850 opcode = aco_opcode::image_sample_c_b_cl_o;
8851 } else {
8852 opcode = aco_opcode::image_sample_cl_o;
8853 if (has_derivs)
8854 opcode = aco_opcode::image_sample_d_cl_o;
8855 if (has_bias)
8856 opcode = aco_opcode::image_sample_b_cl_o;
8857 }
8858 } else if (has_compare) {
8859 opcode = aco_opcode::image_sample_c_o;
8860 if (has_derivs)
8861 opcode = aco_opcode::image_sample_c_d_o;
8862 if (has_bias)
8863 opcode = aco_opcode::image_sample_c_b_o;
8864 if (level_zero)
8865 opcode = aco_opcode::image_sample_c_lz_o;
8866 if (has_lod)
8867 opcode = aco_opcode::image_sample_c_l_o;
8868 } else {
8869 opcode = aco_opcode::image_sample_o;
8870 if (has_derivs)
8871 opcode = aco_opcode::image_sample_d_o;
8872 if (has_bias)
8873 opcode = aco_opcode::image_sample_b_o;
8874 if (level_zero)
8875 opcode = aco_opcode::image_sample_lz_o;
8876 if (has_lod)
8877 opcode = aco_opcode::image_sample_l_o;
8878 }
8879 } else if (has_clamped_lod) { /* image_sample_*_cl */
8880 if (has_compare) {
8881 opcode = aco_opcode::image_sample_c_cl;
8882 if (has_derivs)
8883 opcode = aco_opcode::image_sample_c_d_cl;
8884 if (has_bias)
8885 opcode = aco_opcode::image_sample_c_b_cl;
8886 } else {
8887 opcode = aco_opcode::image_sample_cl;
8888 if (has_derivs)
8889 opcode = aco_opcode::image_sample_d_cl;
8890 if (has_bias)
8891 opcode = aco_opcode::image_sample_b_cl;
8892 }
8893 } else { /* no offset */
8894 if (has_compare) {
8895 opcode = aco_opcode::image_sample_c;
8896 if (has_derivs)
8897 opcode = aco_opcode::image_sample_c_d;
8898 if (has_bias)
8899 opcode = aco_opcode::image_sample_c_b;
8900 if (level_zero)
8901 opcode = aco_opcode::image_sample_c_lz;
8902 if (has_lod)
8903 opcode = aco_opcode::image_sample_c_l;
8904 } else {
8905 opcode = aco_opcode::image_sample;
8906 if (has_derivs)
8907 opcode = aco_opcode::image_sample_d;
8908 if (has_bias)
8909 opcode = aco_opcode::image_sample_b;
8910 if (level_zero)
8911 opcode = aco_opcode::image_sample_lz;
8912 if (has_lod)
8913 opcode = aco_opcode::image_sample_l;
8914 }
8915 }
8916
8917 if (instr->op == nir_texop_tg4) {
8918 if (has_offset) { /* image_gather4_*_o */
8919 if (has_compare) {
8920 opcode = aco_opcode::image_gather4_c_lz_o;
8921 if (has_lod)
8922 opcode = aco_opcode::image_gather4_c_l_o;
8923 if (has_bias)
8924 opcode = aco_opcode::image_gather4_c_b_o;
8925 } else {
8926 opcode = aco_opcode::image_gather4_lz_o;
8927 if (has_lod)
8928 opcode = aco_opcode::image_gather4_l_o;
8929 if (has_bias)
8930 opcode = aco_opcode::image_gather4_b_o;
8931 }
8932 } else {
8933 if (has_compare) {
8934 opcode = aco_opcode::image_gather4_c_lz;
8935 if (has_lod)
8936 opcode = aco_opcode::image_gather4_c_l;
8937 if (has_bias)
8938 opcode = aco_opcode::image_gather4_c_b;
8939 } else {
8940 opcode = aco_opcode::image_gather4_lz;
8941 if (has_lod)
8942 opcode = aco_opcode::image_gather4_l;
8943 if (has_bias)
8944 opcode = aco_opcode::image_gather4_b;
8945 }
8946 }
8947 } else if (instr->op == nir_texop_lod) {
8948 opcode = aco_opcode::image_get_lod;
8949 }
8950
8951 /* we don't need the bias, sample index, compare value or offset to be
8952 * computed in WQM but if the p_create_vector copies the coordinates, then it
8953 * needs to be in WQM */
8954 if (ctx->stage == fragment_fs &&
8955 !has_derivs && !has_lod && !level_zero &&
8956 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8957 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8958 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8959
8960 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8961 tex->operands[0] = Operand(resource);
8962 tex->operands[1] = Operand(sampler);
8963 tex->operands[2] = Operand(arg);
8964 tex->dim = dim;
8965 tex->dmask = dmask;
8966 tex->da = da;
8967 tex->definitions[0] = Definition(tmp_dst);
8968 tex->can_reorder = true;
8969 ctx->block->instructions.emplace_back(std::move(tex));
8970
8971 if (tg4_integer_cube_workaround) {
8972 assert(tmp_dst.id() != dst.id());
8973 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8974
8975 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8976 Temp val[4];
8977 for (unsigned i = 0; i < dst.size(); i++) {
8978 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8979 Temp cvt_val;
8980 if (stype == GLSL_TYPE_UINT)
8981 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8982 else
8983 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8984 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8985 }
8986 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8987 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8988 val[0], val[1], val[2], val[3]);
8989 }
8990 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8991 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8992
8993 }
8994
8995
8996 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa, RegClass rc, bool logical)
8997 {
8998 Temp tmp = get_ssa_temp(ctx, ssa);
8999 if (ssa->parent_instr->type == nir_instr_type_ssa_undef) {
9000 return Operand(rc);
9001 } else if (logical && ssa->bit_size == 1 && ssa->parent_instr->type == nir_instr_type_load_const) {
9002 if (ctx->program->wave_size == 64)
9003 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT64_MAX : 0u);
9004 else
9005 return Operand(nir_instr_as_load_const(ssa->parent_instr)->value[0].b ? UINT32_MAX : 0u);
9006 } else {
9007 return Operand(tmp);
9008 }
9009 }
9010
9011 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
9012 {
9013 aco_ptr<Pseudo_instruction> phi;
9014 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
9015 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
9016
9017 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
9018 logical |= ctx->block->kind & block_kind_merge;
9019 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
9020
9021 /* we want a sorted list of sources, since the predecessor list is also sorted */
9022 std::map<unsigned, nir_ssa_def*> phi_src;
9023 nir_foreach_phi_src(src, instr)
9024 phi_src[src->pred->index] = src->src.ssa;
9025
9026 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
9027 unsigned num_operands = 0;
9028 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
9029 unsigned num_defined = 0;
9030 unsigned cur_pred_idx = 0;
9031 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
9032 if (cur_pred_idx < preds.size()) {
9033 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
9034 unsigned block = ctx->cf_info.nir_to_aco[src.first];
9035 unsigned skipped = 0;
9036 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
9037 skipped++;
9038 if (cur_pred_idx + skipped < preds.size()) {
9039 for (unsigned i = 0; i < skipped; i++)
9040 operands[num_operands++] = Operand(dst.regClass());
9041 cur_pred_idx += skipped;
9042 } else {
9043 continue;
9044 }
9045 }
9046 /* Handle missing predecessors at the end. This shouldn't happen with loop
9047 * headers and we can't ignore these sources for loop header phis. */
9048 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
9049 continue;
9050 cur_pred_idx++;
9051 Operand op = get_phi_operand(ctx, src.second, dst.regClass(), logical);
9052 operands[num_operands++] = op;
9053 num_defined += !op.isUndefined();
9054 }
9055 /* handle block_kind_continue_or_break at loop exit blocks */
9056 while (cur_pred_idx++ < preds.size())
9057 operands[num_operands++] = Operand(dst.regClass());
9058
9059 /* If the loop ends with a break, still add a linear continue edge in case
9060 * that break is divergent or continue_or_break is used. We'll either remove
9061 * this operand later in visit_loop() if it's not necessary or replace the
9062 * undef with something correct. */
9063 if (!logical && ctx->block->kind & block_kind_loop_header) {
9064 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
9065 nir_block *last = nir_loop_last_block(loop);
9066 if (last->successors[0] != instr->instr.block)
9067 operands[num_operands++] = Operand(RegClass());
9068 }
9069
9070 if (num_defined == 0) {
9071 Builder bld(ctx->program, ctx->block);
9072 if (dst.regClass() == s1) {
9073 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
9074 } else if (dst.regClass() == v1) {
9075 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
9076 } else {
9077 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9078 for (unsigned i = 0; i < dst.size(); i++)
9079 vec->operands[i] = Operand(0u);
9080 vec->definitions[0] = Definition(dst);
9081 ctx->block->instructions.emplace_back(std::move(vec));
9082 }
9083 return;
9084 }
9085
9086 /* we can use a linear phi in some cases if one src is undef */
9087 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9088 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9089
9090 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9091 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9092 assert(invert->kind & block_kind_invert);
9093
9094 unsigned then_block = invert->linear_preds[0];
9095
9096 Block* insert_block = NULL;
9097 for (unsigned i = 0; i < num_operands; i++) {
9098 Operand op = operands[i];
9099 if (op.isUndefined())
9100 continue;
9101 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9102 phi->operands[0] = op;
9103 break;
9104 }
9105 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9106 phi->operands[1] = Operand(dst.regClass());
9107 phi->definitions[0] = Definition(dst);
9108 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9109 return;
9110 }
9111
9112 /* try to scalarize vector phis */
9113 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9114 // TODO: scalarize linear phis on divergent ifs
9115 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9116 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9117 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9118 Operand src = operands[i];
9119 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9120 can_scalarize = false;
9121 }
9122 if (can_scalarize) {
9123 unsigned num_components = instr->dest.ssa.num_components;
9124 assert(dst.size() % num_components == 0);
9125 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9126
9127 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9128 for (unsigned k = 0; k < num_components; k++) {
9129 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9130 for (unsigned i = 0; i < num_operands; i++) {
9131 Operand src = operands[i];
9132 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9133 }
9134 Temp phi_dst = {ctx->program->allocateId(), rc};
9135 phi->definitions[0] = Definition(phi_dst);
9136 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9137 new_vec[k] = phi_dst;
9138 vec->operands[k] = Operand(phi_dst);
9139 }
9140 vec->definitions[0] = Definition(dst);
9141 ctx->block->instructions.emplace_back(std::move(vec));
9142 ctx->allocated_vec.emplace(dst.id(), new_vec);
9143 return;
9144 }
9145 }
9146
9147 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9148 for (unsigned i = 0; i < num_operands; i++)
9149 phi->operands[i] = operands[i];
9150 phi->definitions[0] = Definition(dst);
9151 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9152 }
9153
9154
9155 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9156 {
9157 Temp dst = get_ssa_temp(ctx, &instr->def);
9158
9159 assert(dst.type() == RegType::sgpr);
9160
9161 if (dst.size() == 1) {
9162 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9163 } else {
9164 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9165 for (unsigned i = 0; i < dst.size(); i++)
9166 vec->operands[i] = Operand(0u);
9167 vec->definitions[0] = Definition(dst);
9168 ctx->block->instructions.emplace_back(std::move(vec));
9169 }
9170 }
9171
9172 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9173 {
9174 Builder bld(ctx->program, ctx->block);
9175 Block *logical_target;
9176 append_logical_end(ctx->block);
9177 unsigned idx = ctx->block->index;
9178
9179 switch (instr->type) {
9180 case nir_jump_break:
9181 logical_target = ctx->cf_info.parent_loop.exit;
9182 add_logical_edge(idx, logical_target);
9183 ctx->block->kind |= block_kind_break;
9184
9185 if (!ctx->cf_info.parent_if.is_divergent &&
9186 !ctx->cf_info.parent_loop.has_divergent_continue) {
9187 /* uniform break - directly jump out of the loop */
9188 ctx->block->kind |= block_kind_uniform;
9189 ctx->cf_info.has_branch = true;
9190 bld.branch(aco_opcode::p_branch);
9191 add_linear_edge(idx, logical_target);
9192 return;
9193 }
9194 ctx->cf_info.parent_loop.has_divergent_branch = true;
9195 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9196 break;
9197 case nir_jump_continue:
9198 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9199 add_logical_edge(idx, logical_target);
9200 ctx->block->kind |= block_kind_continue;
9201
9202 if (ctx->cf_info.parent_if.is_divergent) {
9203 /* for potential uniform breaks after this continue,
9204 we must ensure that they are handled correctly */
9205 ctx->cf_info.parent_loop.has_divergent_continue = true;
9206 ctx->cf_info.parent_loop.has_divergent_branch = true;
9207 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9208 } else {
9209 /* uniform continue - directly jump to the loop header */
9210 ctx->block->kind |= block_kind_uniform;
9211 ctx->cf_info.has_branch = true;
9212 bld.branch(aco_opcode::p_branch);
9213 add_linear_edge(idx, logical_target);
9214 return;
9215 }
9216 break;
9217 default:
9218 fprintf(stderr, "Unknown NIR jump instr: ");
9219 nir_print_instr(&instr->instr, stderr);
9220 fprintf(stderr, "\n");
9221 abort();
9222 }
9223
9224 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9225 ctx->cf_info.exec_potentially_empty_break = true;
9226 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9227 }
9228
9229 /* remove critical edges from linear CFG */
9230 bld.branch(aco_opcode::p_branch);
9231 Block* break_block = ctx->program->create_and_insert_block();
9232 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9233 break_block->kind |= block_kind_uniform;
9234 add_linear_edge(idx, break_block);
9235 /* the loop_header pointer might be invalidated by this point */
9236 if (instr->type == nir_jump_continue)
9237 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9238 add_linear_edge(break_block->index, logical_target);
9239 bld.reset(break_block);
9240 bld.branch(aco_opcode::p_branch);
9241
9242 Block* continue_block = ctx->program->create_and_insert_block();
9243 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9244 add_linear_edge(idx, continue_block);
9245 append_logical_start(continue_block);
9246 ctx->block = continue_block;
9247 return;
9248 }
9249
9250 void visit_block(isel_context *ctx, nir_block *block)
9251 {
9252 nir_foreach_instr(instr, block) {
9253 switch (instr->type) {
9254 case nir_instr_type_alu:
9255 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9256 break;
9257 case nir_instr_type_load_const:
9258 visit_load_const(ctx, nir_instr_as_load_const(instr));
9259 break;
9260 case nir_instr_type_intrinsic:
9261 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9262 break;
9263 case nir_instr_type_tex:
9264 visit_tex(ctx, nir_instr_as_tex(instr));
9265 break;
9266 case nir_instr_type_phi:
9267 visit_phi(ctx, nir_instr_as_phi(instr));
9268 break;
9269 case nir_instr_type_ssa_undef:
9270 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9271 break;
9272 case nir_instr_type_deref:
9273 break;
9274 case nir_instr_type_jump:
9275 visit_jump(ctx, nir_instr_as_jump(instr));
9276 break;
9277 default:
9278 fprintf(stderr, "Unknown NIR instr type: ");
9279 nir_print_instr(instr, stderr);
9280 fprintf(stderr, "\n");
9281 //abort();
9282 }
9283 }
9284
9285 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9286 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9287 }
9288
9289
9290
9291 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9292 aco_ptr<Instruction>& header_phi, Operand *vals)
9293 {
9294 vals[0] = Operand(header_phi->definitions[0].getTemp());
9295 RegClass rc = vals[0].regClass();
9296
9297 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9298
9299 unsigned next_pred = 1;
9300
9301 for (unsigned idx = first + 1; idx <= last; idx++) {
9302 Block& block = ctx->program->blocks[idx];
9303 if (block.loop_nest_depth != loop_nest_depth) {
9304 vals[idx - first] = vals[idx - 1 - first];
9305 continue;
9306 }
9307
9308 if (block.kind & block_kind_continue) {
9309 vals[idx - first] = header_phi->operands[next_pred];
9310 next_pred++;
9311 continue;
9312 }
9313
9314 bool all_same = true;
9315 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9316 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9317
9318 Operand val;
9319 if (all_same) {
9320 val = vals[block.linear_preds[0] - first];
9321 } else {
9322 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9323 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9324 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9325 phi->operands[i] = vals[block.linear_preds[i] - first];
9326 val = Operand(Temp(ctx->program->allocateId(), rc));
9327 phi->definitions[0] = Definition(val.getTemp());
9328 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9329 }
9330 vals[idx - first] = val;
9331 }
9332
9333 return vals[last - first];
9334 }
9335
9336 static void visit_loop(isel_context *ctx, nir_loop *loop)
9337 {
9338 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9339 append_logical_end(ctx->block);
9340 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9341 Builder bld(ctx->program, ctx->block);
9342 bld.branch(aco_opcode::p_branch);
9343 unsigned loop_preheader_idx = ctx->block->index;
9344
9345 Block loop_exit = Block();
9346 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9347 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9348
9349 Block* loop_header = ctx->program->create_and_insert_block();
9350 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9351 loop_header->kind |= block_kind_loop_header;
9352 add_edge(loop_preheader_idx, loop_header);
9353 ctx->block = loop_header;
9354
9355 /* emit loop body */
9356 unsigned loop_header_idx = loop_header->index;
9357 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9358 append_logical_start(ctx->block);
9359 bool unreachable = visit_cf_list(ctx, &loop->body);
9360
9361 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9362 if (!ctx->cf_info.has_branch) {
9363 append_logical_end(ctx->block);
9364 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9365 /* Discards can result in code running with an empty exec mask.
9366 * This would result in divergent breaks not ever being taken. As a
9367 * workaround, break the loop when the loop mask is empty instead of
9368 * always continuing. */
9369 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9370 unsigned block_idx = ctx->block->index;
9371
9372 /* create helper blocks to avoid critical edges */
9373 Block *break_block = ctx->program->create_and_insert_block();
9374 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9375 break_block->kind = block_kind_uniform;
9376 bld.reset(break_block);
9377 bld.branch(aco_opcode::p_branch);
9378 add_linear_edge(block_idx, break_block);
9379 add_linear_edge(break_block->index, &loop_exit);
9380
9381 Block *continue_block = ctx->program->create_and_insert_block();
9382 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9383 continue_block->kind = block_kind_uniform;
9384 bld.reset(continue_block);
9385 bld.branch(aco_opcode::p_branch);
9386 add_linear_edge(block_idx, continue_block);
9387 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9388
9389 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9390 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9391 ctx->block = &ctx->program->blocks[block_idx];
9392 } else {
9393 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9394 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9395 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9396 else
9397 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9398 }
9399
9400 bld.reset(ctx->block);
9401 bld.branch(aco_opcode::p_branch);
9402 }
9403
9404 /* Fixup phis in loop header from unreachable blocks.
9405 * has_branch/has_divergent_branch also indicates if the loop ends with a
9406 * break/continue instruction, but we don't emit those if unreachable=true */
9407 if (unreachable) {
9408 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9409 bool linear = ctx->cf_info.has_branch;
9410 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9411 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9412 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9413 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9414 /* the last operand should be the one that needs to be removed */
9415 instr->operands.pop_back();
9416 } else if (!is_phi(instr)) {
9417 break;
9418 }
9419 }
9420 }
9421
9422 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9423 * and the previous one shouldn't both happen at once because a break in the
9424 * merge block would get CSE'd */
9425 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9426 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9427 Operand vals[num_vals];
9428 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9429 if (instr->opcode == aco_opcode::p_linear_phi) {
9430 if (ctx->cf_info.has_branch)
9431 instr->operands.pop_back();
9432 else
9433 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9434 } else if (!is_phi(instr)) {
9435 break;
9436 }
9437 }
9438 }
9439
9440 ctx->cf_info.has_branch = false;
9441
9442 // TODO: if the loop has not a single exit, we must add one °°
9443 /* emit loop successor block */
9444 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9445 append_logical_start(ctx->block);
9446
9447 #if 0
9448 // TODO: check if it is beneficial to not branch on continues
9449 /* trim linear phis in loop header */
9450 for (auto&& instr : loop_entry->instructions) {
9451 if (instr->opcode == aco_opcode::p_linear_phi) {
9452 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9453 new_phi->definitions[0] = instr->definitions[0];
9454 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9455 new_phi->operands[i] = instr->operands[i];
9456 /* check that the remaining operands are all the same */
9457 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9458 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9459 instr.swap(new_phi);
9460 } else if (instr->opcode == aco_opcode::p_phi) {
9461 continue;
9462 } else {
9463 break;
9464 }
9465 }
9466 #endif
9467 }
9468
9469 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9470 {
9471 ic->cond = cond;
9472
9473 append_logical_end(ctx->block);
9474 ctx->block->kind |= block_kind_branch;
9475
9476 /* branch to linear then block */
9477 assert(cond.regClass() == ctx->program->lane_mask);
9478 aco_ptr<Pseudo_branch_instruction> branch;
9479 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9480 branch->operands[0] = Operand(cond);
9481 ctx->block->instructions.push_back(std::move(branch));
9482
9483 ic->BB_if_idx = ctx->block->index;
9484 ic->BB_invert = Block();
9485 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9486 /* Invert blocks are intentionally not marked as top level because they
9487 * are not part of the logical cfg. */
9488 ic->BB_invert.kind |= block_kind_invert;
9489 ic->BB_endif = Block();
9490 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9491 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9492
9493 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9494 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9495 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9496 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9497 ctx->cf_info.parent_if.is_divergent = true;
9498
9499 /* divergent branches use cbranch_execz */
9500 ctx->cf_info.exec_potentially_empty_discard = false;
9501 ctx->cf_info.exec_potentially_empty_break = false;
9502 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9503
9504 /** emit logical then block */
9505 Block* BB_then_logical = ctx->program->create_and_insert_block();
9506 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9507 add_edge(ic->BB_if_idx, BB_then_logical);
9508 ctx->block = BB_then_logical;
9509 append_logical_start(BB_then_logical);
9510 }
9511
9512 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9513 {
9514 Block *BB_then_logical = ctx->block;
9515 append_logical_end(BB_then_logical);
9516 /* branch from logical then block to invert block */
9517 aco_ptr<Pseudo_branch_instruction> branch;
9518 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9519 BB_then_logical->instructions.emplace_back(std::move(branch));
9520 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9521 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9522 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9523 BB_then_logical->kind |= block_kind_uniform;
9524 assert(!ctx->cf_info.has_branch);
9525 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9526 ctx->cf_info.parent_loop.has_divergent_branch = false;
9527
9528 /** emit linear then block */
9529 Block* BB_then_linear = ctx->program->create_and_insert_block();
9530 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9531 BB_then_linear->kind |= block_kind_uniform;
9532 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9533 /* branch from linear then block to invert block */
9534 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9535 BB_then_linear->instructions.emplace_back(std::move(branch));
9536 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9537
9538 /** emit invert merge block */
9539 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9540 ic->invert_idx = ctx->block->index;
9541
9542 /* branch to linear else block (skip else) */
9543 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9544 branch->operands[0] = Operand(ic->cond);
9545 ctx->block->instructions.push_back(std::move(branch));
9546
9547 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9548 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9549 ic->exec_potentially_empty_break_depth_old =
9550 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9551 /* divergent branches use cbranch_execz */
9552 ctx->cf_info.exec_potentially_empty_discard = false;
9553 ctx->cf_info.exec_potentially_empty_break = false;
9554 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9555
9556 /** emit logical else block */
9557 Block* BB_else_logical = ctx->program->create_and_insert_block();
9558 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9559 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9560 add_linear_edge(ic->invert_idx, BB_else_logical);
9561 ctx->block = BB_else_logical;
9562 append_logical_start(BB_else_logical);
9563 }
9564
9565 static void end_divergent_if(isel_context *ctx, if_context *ic)
9566 {
9567 Block *BB_else_logical = ctx->block;
9568 append_logical_end(BB_else_logical);
9569
9570 /* branch from logical else block to endif block */
9571 aco_ptr<Pseudo_branch_instruction> branch;
9572 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9573 BB_else_logical->instructions.emplace_back(std::move(branch));
9574 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9575 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9576 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9577 BB_else_logical->kind |= block_kind_uniform;
9578
9579 assert(!ctx->cf_info.has_branch);
9580 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9581
9582
9583 /** emit linear else block */
9584 Block* BB_else_linear = ctx->program->create_and_insert_block();
9585 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9586 BB_else_linear->kind |= block_kind_uniform;
9587 add_linear_edge(ic->invert_idx, BB_else_linear);
9588
9589 /* branch from linear else block to endif block */
9590 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9591 BB_else_linear->instructions.emplace_back(std::move(branch));
9592 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9593
9594
9595 /** emit endif merge block */
9596 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9597 append_logical_start(ctx->block);
9598
9599
9600 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9601 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9602 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9603 ctx->cf_info.exec_potentially_empty_break_depth =
9604 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9605 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9606 !ctx->cf_info.parent_if.is_divergent) {
9607 ctx->cf_info.exec_potentially_empty_break = false;
9608 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9609 }
9610 /* uniform control flow never has an empty exec-mask */
9611 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9612 ctx->cf_info.exec_potentially_empty_discard = false;
9613 ctx->cf_info.exec_potentially_empty_break = false;
9614 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9615 }
9616 }
9617
9618 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9619 {
9620 assert(cond.regClass() == s1);
9621
9622 append_logical_end(ctx->block);
9623 ctx->block->kind |= block_kind_uniform;
9624
9625 aco_ptr<Pseudo_branch_instruction> branch;
9626 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9627 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9628 branch->operands[0] = Operand(cond);
9629 branch->operands[0].setFixed(scc);
9630 ctx->block->instructions.emplace_back(std::move(branch));
9631
9632 ic->BB_if_idx = ctx->block->index;
9633 ic->BB_endif = Block();
9634 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9635 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9636
9637 ctx->cf_info.has_branch = false;
9638 ctx->cf_info.parent_loop.has_divergent_branch = false;
9639
9640 /** emit then block */
9641 Block* BB_then = ctx->program->create_and_insert_block();
9642 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9643 add_edge(ic->BB_if_idx, BB_then);
9644 append_logical_start(BB_then);
9645 ctx->block = BB_then;
9646 }
9647
9648 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9649 {
9650 Block *BB_then = ctx->block;
9651
9652 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9653 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9654
9655 if (!ic->uniform_has_then_branch) {
9656 append_logical_end(BB_then);
9657 /* branch from then block to endif block */
9658 aco_ptr<Pseudo_branch_instruction> branch;
9659 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9660 BB_then->instructions.emplace_back(std::move(branch));
9661 add_linear_edge(BB_then->index, &ic->BB_endif);
9662 if (!ic->then_branch_divergent)
9663 add_logical_edge(BB_then->index, &ic->BB_endif);
9664 BB_then->kind |= block_kind_uniform;
9665 }
9666
9667 ctx->cf_info.has_branch = false;
9668 ctx->cf_info.parent_loop.has_divergent_branch = false;
9669
9670 /** emit else block */
9671 Block* BB_else = ctx->program->create_and_insert_block();
9672 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9673 add_edge(ic->BB_if_idx, BB_else);
9674 append_logical_start(BB_else);
9675 ctx->block = BB_else;
9676 }
9677
9678 static void end_uniform_if(isel_context *ctx, if_context *ic)
9679 {
9680 Block *BB_else = ctx->block;
9681
9682 if (!ctx->cf_info.has_branch) {
9683 append_logical_end(BB_else);
9684 /* branch from then block to endif block */
9685 aco_ptr<Pseudo_branch_instruction> branch;
9686 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9687 BB_else->instructions.emplace_back(std::move(branch));
9688 add_linear_edge(BB_else->index, &ic->BB_endif);
9689 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9690 add_logical_edge(BB_else->index, &ic->BB_endif);
9691 BB_else->kind |= block_kind_uniform;
9692 }
9693
9694 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9695 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9696
9697 /** emit endif merge block */
9698 if (!ctx->cf_info.has_branch) {
9699 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9700 append_logical_start(ctx->block);
9701 }
9702 }
9703
9704 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9705 {
9706 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9707 Builder bld(ctx->program, ctx->block);
9708 aco_ptr<Pseudo_branch_instruction> branch;
9709 if_context ic;
9710
9711 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9712 /**
9713 * Uniform conditionals are represented in the following way*) :
9714 *
9715 * The linear and logical CFG:
9716 * BB_IF
9717 * / \
9718 * BB_THEN (logical) BB_ELSE (logical)
9719 * \ /
9720 * BB_ENDIF
9721 *
9722 * *) Exceptions may be due to break and continue statements within loops
9723 * If a break/continue happens within uniform control flow, it branches
9724 * to the loop exit/entry block. Otherwise, it branches to the next
9725 * merge block.
9726 **/
9727
9728 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9729 assert(cond.regClass() == ctx->program->lane_mask);
9730 cond = bool_to_scalar_condition(ctx, cond);
9731
9732 begin_uniform_if_then(ctx, &ic, cond);
9733 visit_cf_list(ctx, &if_stmt->then_list);
9734
9735 begin_uniform_if_else(ctx, &ic);
9736 visit_cf_list(ctx, &if_stmt->else_list);
9737
9738 end_uniform_if(ctx, &ic);
9739 } else { /* non-uniform condition */
9740 /**
9741 * To maintain a logical and linear CFG without critical edges,
9742 * non-uniform conditionals are represented in the following way*) :
9743 *
9744 * The linear CFG:
9745 * BB_IF
9746 * / \
9747 * BB_THEN (logical) BB_THEN (linear)
9748 * \ /
9749 * BB_INVERT (linear)
9750 * / \
9751 * BB_ELSE (logical) BB_ELSE (linear)
9752 * \ /
9753 * BB_ENDIF
9754 *
9755 * The logical CFG:
9756 * BB_IF
9757 * / \
9758 * BB_THEN (logical) BB_ELSE (logical)
9759 * \ /
9760 * BB_ENDIF
9761 *
9762 * *) Exceptions may be due to break and continue statements within loops
9763 **/
9764
9765 begin_divergent_if_then(ctx, &ic, cond);
9766 visit_cf_list(ctx, &if_stmt->then_list);
9767
9768 begin_divergent_if_else(ctx, &ic);
9769 visit_cf_list(ctx, &if_stmt->else_list);
9770
9771 end_divergent_if(ctx, &ic);
9772 }
9773
9774 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9775 }
9776
9777 static bool visit_cf_list(isel_context *ctx,
9778 struct exec_list *list)
9779 {
9780 foreach_list_typed(nir_cf_node, node, node, list) {
9781 switch (node->type) {
9782 case nir_cf_node_block:
9783 visit_block(ctx, nir_cf_node_as_block(node));
9784 break;
9785 case nir_cf_node_if:
9786 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9787 return true;
9788 break;
9789 case nir_cf_node_loop:
9790 visit_loop(ctx, nir_cf_node_as_loop(node));
9791 break;
9792 default:
9793 unreachable("unimplemented cf list type");
9794 }
9795 }
9796 return false;
9797 }
9798
9799 static void create_null_export(isel_context *ctx)
9800 {
9801 /* Some shader stages always need to have exports.
9802 * So when there is none, we need to add a null export.
9803 */
9804
9805 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9806 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9807 Builder bld(ctx->program, ctx->block);
9808 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9809 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9810 }
9811
9812 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9813 {
9814 assert(ctx->stage == vertex_vs ||
9815 ctx->stage == tess_eval_vs ||
9816 ctx->stage == gs_copy_vs ||
9817 ctx->stage == ngg_vertex_gs ||
9818 ctx->stage == ngg_tess_eval_gs);
9819
9820 int offset = (ctx->stage & sw_tes)
9821 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9822 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9823 uint64_t mask = ctx->outputs.mask[slot];
9824 if (!is_pos && !mask)
9825 return false;
9826 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9827 return false;
9828 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9829 exp->enabled_mask = mask;
9830 for (unsigned i = 0; i < 4; ++i) {
9831 if (mask & (1 << i))
9832 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9833 else
9834 exp->operands[i] = Operand(v1);
9835 }
9836 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9837 * Setting valid_mask=1 prevents it and has no other effect.
9838 */
9839 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9840 exp->done = false;
9841 exp->compressed = false;
9842 if (is_pos)
9843 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9844 else
9845 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9846 ctx->block->instructions.emplace_back(std::move(exp));
9847
9848 return true;
9849 }
9850
9851 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9852 {
9853 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9854 exp->enabled_mask = 0;
9855 for (unsigned i = 0; i < 4; ++i)
9856 exp->operands[i] = Operand(v1);
9857 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9858 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9859 exp->enabled_mask |= 0x1;
9860 }
9861 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9862 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9863 exp->enabled_mask |= 0x4;
9864 }
9865 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9866 if (ctx->options->chip_class < GFX9) {
9867 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9868 exp->enabled_mask |= 0x8;
9869 } else {
9870 Builder bld(ctx->program, ctx->block);
9871
9872 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9873 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9874 if (exp->operands[2].isTemp())
9875 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9876
9877 exp->operands[2] = Operand(out);
9878 exp->enabled_mask |= 0x4;
9879 }
9880 }
9881 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9882 exp->done = false;
9883 exp->compressed = false;
9884 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9885 ctx->block->instructions.emplace_back(std::move(exp));
9886 }
9887
9888 static void create_export_phis(isel_context *ctx)
9889 {
9890 /* Used when exports are needed, but the output temps are defined in a preceding block.
9891 * This function will set up phis in order to access the outputs in the next block.
9892 */
9893
9894 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9895 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9896 ctx->block->instructions.pop_back();
9897
9898 Builder bld(ctx->program, ctx->block);
9899
9900 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9901 uint64_t mask = ctx->outputs.mask[slot];
9902 for (unsigned i = 0; i < 4; ++i) {
9903 if (!(mask & (1 << i)))
9904 continue;
9905
9906 Temp old = ctx->outputs.temps[slot * 4 + i];
9907 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9908 ctx->outputs.temps[slot * 4 + i] = phi;
9909 }
9910 }
9911
9912 bld.insert(std::move(logical_start));
9913 }
9914
9915 static void create_vs_exports(isel_context *ctx)
9916 {
9917 assert(ctx->stage == vertex_vs ||
9918 ctx->stage == tess_eval_vs ||
9919 ctx->stage == gs_copy_vs ||
9920 ctx->stage == ngg_vertex_gs ||
9921 ctx->stage == ngg_tess_eval_gs);
9922
9923 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9924 ? &ctx->program->info->tes.outinfo
9925 : &ctx->program->info->vs.outinfo;
9926
9927 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9928 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9929 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9930 }
9931
9932 if (ctx->options->key.has_multiview_view_index) {
9933 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9934 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9935 }
9936
9937 /* the order these position exports are created is important */
9938 int next_pos = 0;
9939 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9940 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9941 export_vs_psiz_layer_viewport(ctx, &next_pos);
9942 exported_pos = true;
9943 }
9944 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9945 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9946 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9947 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9948
9949 if (ctx->export_clip_dists) {
9950 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9951 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9952 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9953 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9954 }
9955
9956 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9957 if (i < VARYING_SLOT_VAR0 &&
9958 i != VARYING_SLOT_LAYER &&
9959 i != VARYING_SLOT_PRIMITIVE_ID &&
9960 i != VARYING_SLOT_VIEWPORT)
9961 continue;
9962
9963 export_vs_varying(ctx, i, false, NULL);
9964 }
9965
9966 if (!exported_pos)
9967 create_null_export(ctx);
9968 }
9969
9970 static bool export_fs_mrt_z(isel_context *ctx)
9971 {
9972 Builder bld(ctx->program, ctx->block);
9973 unsigned enabled_channels = 0;
9974 bool compr = false;
9975 Operand values[4];
9976
9977 for (unsigned i = 0; i < 4; ++i) {
9978 values[i] = Operand(v1);
9979 }
9980
9981 /* Both stencil and sample mask only need 16-bits. */
9982 if (!ctx->program->info->ps.writes_z &&
9983 (ctx->program->info->ps.writes_stencil ||
9984 ctx->program->info->ps.writes_sample_mask)) {
9985 compr = true; /* COMPR flag */
9986
9987 if (ctx->program->info->ps.writes_stencil) {
9988 /* Stencil should be in X[23:16]. */
9989 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9990 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9991 enabled_channels |= 0x3;
9992 }
9993
9994 if (ctx->program->info->ps.writes_sample_mask) {
9995 /* SampleMask should be in Y[15:0]. */
9996 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9997 enabled_channels |= 0xc;
9998 }
9999 } else {
10000 if (ctx->program->info->ps.writes_z) {
10001 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
10002 enabled_channels |= 0x1;
10003 }
10004
10005 if (ctx->program->info->ps.writes_stencil) {
10006 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
10007 enabled_channels |= 0x2;
10008 }
10009
10010 if (ctx->program->info->ps.writes_sample_mask) {
10011 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
10012 enabled_channels |= 0x4;
10013 }
10014 }
10015
10016 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
10017 * writemask component.
10018 */
10019 if (ctx->options->chip_class == GFX6 &&
10020 ctx->options->family != CHIP_OLAND &&
10021 ctx->options->family != CHIP_HAINAN) {
10022 enabled_channels |= 0x1;
10023 }
10024
10025 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10026 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
10027
10028 return true;
10029 }
10030
10031 static bool export_fs_mrt_color(isel_context *ctx, int slot)
10032 {
10033 Builder bld(ctx->program, ctx->block);
10034 unsigned write_mask = ctx->outputs.mask[slot];
10035 Operand values[4];
10036
10037 for (unsigned i = 0; i < 4; ++i) {
10038 if (write_mask & (1 << i)) {
10039 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
10040 } else {
10041 values[i] = Operand(v1);
10042 }
10043 }
10044
10045 unsigned target, col_format;
10046 unsigned enabled_channels = 0;
10047 aco_opcode compr_op = (aco_opcode)0;
10048
10049 slot -= FRAG_RESULT_DATA0;
10050 target = V_008DFC_SQ_EXP_MRT + slot;
10051 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
10052
10053 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
10054 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
10055 bool is_16bit = values[0].regClass() == v2b;
10056
10057 switch (col_format)
10058 {
10059 case V_028714_SPI_SHADER_ZERO:
10060 enabled_channels = 0; /* writemask */
10061 target = V_008DFC_SQ_EXP_NULL;
10062 break;
10063
10064 case V_028714_SPI_SHADER_32_R:
10065 enabled_channels = 1;
10066 break;
10067
10068 case V_028714_SPI_SHADER_32_GR:
10069 enabled_channels = 0x3;
10070 break;
10071
10072 case V_028714_SPI_SHADER_32_AR:
10073 if (ctx->options->chip_class >= GFX10) {
10074 /* Special case: on GFX10, the outputs are different for 32_AR */
10075 enabled_channels = 0x3;
10076 values[1] = values[3];
10077 values[3] = Operand(v1);
10078 } else {
10079 enabled_channels = 0x9;
10080 }
10081 break;
10082
10083 case V_028714_SPI_SHADER_FP16_ABGR:
10084 enabled_channels = 0x5;
10085 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
10086 if (is_16bit) {
10087 if (ctx->options->chip_class >= GFX9) {
10088 /* Pack the FP16 values together instead of converting them to
10089 * FP32 and back to FP16.
10090 * TODO: use p_create_vector and let the compiler optimizes.
10091 */
10092 compr_op = aco_opcode::v_pack_b32_f16;
10093 } else {
10094 for (unsigned i = 0; i < 4; i++) {
10095 if ((write_mask >> i) & 1)
10096 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
10097 }
10098 }
10099 }
10100 break;
10101
10102 case V_028714_SPI_SHADER_UNORM16_ABGR:
10103 enabled_channels = 0x5;
10104 if (is_16bit && ctx->options->chip_class >= GFX9) {
10105 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
10106 } else {
10107 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10108 }
10109 break;
10110
10111 case V_028714_SPI_SHADER_SNORM16_ABGR:
10112 enabled_channels = 0x5;
10113 if (is_16bit && ctx->options->chip_class >= GFX9) {
10114 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
10115 } else {
10116 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10117 }
10118 break;
10119
10120 case V_028714_SPI_SHADER_UINT16_ABGR: {
10121 enabled_channels = 0x5;
10122 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10123 if (is_int8 || is_int10) {
10124 /* clamp */
10125 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10126 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10127
10128 for (unsigned i = 0; i < 4; i++) {
10129 if ((write_mask >> i) & 1) {
10130 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10131 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10132 values[i]);
10133 }
10134 }
10135 } else if (is_16bit) {
10136 for (unsigned i = 0; i < 4; i++) {
10137 if ((write_mask >> i) & 1) {
10138 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, false);
10139 values[i] = Operand(tmp);
10140 }
10141 }
10142 }
10143 break;
10144 }
10145
10146 case V_028714_SPI_SHADER_SINT16_ABGR:
10147 enabled_channels = 0x5;
10148 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10149 if (is_int8 || is_int10) {
10150 /* clamp */
10151 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10152 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10153 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10154 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10155
10156 for (unsigned i = 0; i < 4; i++) {
10157 if ((write_mask >> i) & 1) {
10158 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10159 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10160 values[i]);
10161 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10162 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10163 values[i]);
10164 }
10165 }
10166 } else if (is_16bit) {
10167 for (unsigned i = 0; i < 4; i++) {
10168 if ((write_mask >> i) & 1) {
10169 Temp tmp = convert_int(ctx, bld, values[i].getTemp(), 16, 32, true);
10170 values[i] = Operand(tmp);
10171 }
10172 }
10173 }
10174 break;
10175
10176 case V_028714_SPI_SHADER_32_ABGR:
10177 enabled_channels = 0xF;
10178 break;
10179
10180 default:
10181 break;
10182 }
10183
10184 if (target == V_008DFC_SQ_EXP_NULL)
10185 return false;
10186
10187 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10188 if (ctx->options->enable_mrt_output_nan_fixup &&
10189 !is_16bit &&
10190 (col_format == V_028714_SPI_SHADER_32_R ||
10191 col_format == V_028714_SPI_SHADER_32_GR ||
10192 col_format == V_028714_SPI_SHADER_32_AR ||
10193 col_format == V_028714_SPI_SHADER_32_ABGR ||
10194 col_format == V_028714_SPI_SHADER_FP16_ABGR)) {
10195 for (int i = 0; i < 4; i++) {
10196 if (!(write_mask & (1 << i)))
10197 continue;
10198
10199 Temp isnan = bld.vopc(aco_opcode::v_cmp_class_f32,
10200 bld.hint_vcc(bld.def(bld.lm)), values[i],
10201 bld.copy(bld.def(v1), Operand(3u)));
10202 values[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), values[i],
10203 bld.copy(bld.def(v1), Operand(0u)), isnan);
10204 }
10205 }
10206
10207 if ((bool) compr_op) {
10208 for (int i = 0; i < 2; i++) {
10209 /* check if at least one of the values to be compressed is enabled */
10210 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10211 if (enabled) {
10212 enabled_channels |= enabled << (i*2);
10213 values[i] = bld.vop3(compr_op, bld.def(v1),
10214 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10215 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10216 } else {
10217 values[i] = Operand(v1);
10218 }
10219 }
10220 values[2] = Operand(v1);
10221 values[3] = Operand(v1);
10222 } else {
10223 for (int i = 0; i < 4; i++)
10224 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10225 }
10226
10227 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10228 enabled_channels, target, (bool) compr_op);
10229 return true;
10230 }
10231
10232 static void create_fs_exports(isel_context *ctx)
10233 {
10234 bool exported = false;
10235
10236 /* Export depth, stencil and sample mask. */
10237 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10238 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10239 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10240 exported |= export_fs_mrt_z(ctx);
10241
10242 /* Export all color render targets. */
10243 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10244 if (ctx->outputs.mask[i])
10245 exported |= export_fs_mrt_color(ctx, i);
10246
10247 if (!exported)
10248 create_null_export(ctx);
10249 }
10250
10251 static void write_tcs_tess_factors(isel_context *ctx)
10252 {
10253 unsigned outer_comps;
10254 unsigned inner_comps;
10255
10256 switch (ctx->args->options->key.tcs.primitive_mode) {
10257 case GL_ISOLINES:
10258 outer_comps = 2;
10259 inner_comps = 0;
10260 break;
10261 case GL_TRIANGLES:
10262 outer_comps = 3;
10263 inner_comps = 1;
10264 break;
10265 case GL_QUADS:
10266 outer_comps = 4;
10267 inner_comps = 2;
10268 break;
10269 default:
10270 return;
10271 }
10272
10273 Builder bld(ctx->program, ctx->block);
10274
10275 bld.barrier(aco_opcode::p_memory_barrier_shared);
10276 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10277 bld.sopp(aco_opcode::s_barrier);
10278
10279 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10280 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10281
10282 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10283 if_context ic_invocation_id_is_zero;
10284 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10285 bld.reset(ctx->block);
10286
10287 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));
10288
10289 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10290 unsigned stride = inner_comps + outer_comps;
10291 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10292 Temp tf_inner_vec;
10293 Temp tf_outer_vec;
10294 Temp out[6];
10295 assert(stride <= (sizeof(out) / sizeof(Temp)));
10296
10297 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10298 // LINES reversal
10299 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10300 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10301 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10302 } else {
10303 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);
10304 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);
10305
10306 for (unsigned i = 0; i < outer_comps; ++i)
10307 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10308 for (unsigned i = 0; i < inner_comps; ++i)
10309 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10310 }
10311
10312 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10313 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10314 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10315 unsigned tf_const_offset = 0;
10316
10317 if (ctx->program->chip_class <= GFX8) {
10318 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);
10319 if_context ic_rel_patch_id_is_zero;
10320 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10321 bld.reset(ctx->block);
10322
10323 /* Store the dynamic HS control word. */
10324 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10325 bld.mubuf(aco_opcode::buffer_store_dword,
10326 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10327 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10328 /* disable_wqm */ false, /* glc */ true);
10329 tf_const_offset += 4;
10330
10331 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10332 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10333 bld.reset(ctx->block);
10334 }
10335
10336 assert(stride == 2 || stride == 4 || stride == 6);
10337 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10338 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10339
10340 /* Store to offchip for TES to read - only if TES reads them */
10341 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10342 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));
10343 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10344
10345 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10346 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);
10347
10348 if (likely(inner_comps)) {
10349 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10350 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);
10351 }
10352 }
10353
10354 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10355 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10356 }
10357
10358 static void emit_stream_output(isel_context *ctx,
10359 Temp const *so_buffers,
10360 Temp const *so_write_offset,
10361 const struct radv_stream_output *output)
10362 {
10363 unsigned num_comps = util_bitcount(output->component_mask);
10364 unsigned writemask = (1 << num_comps) - 1;
10365 unsigned loc = output->location;
10366 unsigned buf = output->buffer;
10367
10368 assert(num_comps && num_comps <= 4);
10369 if (!num_comps || num_comps > 4)
10370 return;
10371
10372 unsigned start = ffs(output->component_mask) - 1;
10373
10374 Temp out[4];
10375 bool all_undef = true;
10376 assert(ctx->stage & hw_vs);
10377 for (unsigned i = 0; i < num_comps; i++) {
10378 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10379 all_undef = all_undef && !out[i].id();
10380 }
10381 if (all_undef)
10382 return;
10383
10384 while (writemask) {
10385 int start, count;
10386 u_bit_scan_consecutive_range(&writemask, &start, &count);
10387 if (count == 3 && ctx->options->chip_class == GFX6) {
10388 /* GFX6 doesn't support storing vec3, split it. */
10389 writemask |= 1u << (start + 2);
10390 count = 2;
10391 }
10392
10393 unsigned offset = output->offset + start * 4;
10394
10395 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10396 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10397 for (int i = 0; i < count; ++i)
10398 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10399 vec->definitions[0] = Definition(write_data);
10400 ctx->block->instructions.emplace_back(std::move(vec));
10401
10402 aco_opcode opcode;
10403 switch (count) {
10404 case 1:
10405 opcode = aco_opcode::buffer_store_dword;
10406 break;
10407 case 2:
10408 opcode = aco_opcode::buffer_store_dwordx2;
10409 break;
10410 case 3:
10411 opcode = aco_opcode::buffer_store_dwordx3;
10412 break;
10413 case 4:
10414 opcode = aco_opcode::buffer_store_dwordx4;
10415 break;
10416 default:
10417 unreachable("Unsupported dword count.");
10418 }
10419
10420 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10421 store->operands[0] = Operand(so_buffers[buf]);
10422 store->operands[1] = Operand(so_write_offset[buf]);
10423 store->operands[2] = Operand((uint32_t) 0);
10424 store->operands[3] = Operand(write_data);
10425 if (offset > 4095) {
10426 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10427 Builder bld(ctx->program, ctx->block);
10428 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10429 } else {
10430 store->offset = offset;
10431 }
10432 store->offen = true;
10433 store->glc = true;
10434 store->dlc = false;
10435 store->slc = true;
10436 store->can_reorder = true;
10437 ctx->block->instructions.emplace_back(std::move(store));
10438 }
10439 }
10440
10441 static void emit_streamout(isel_context *ctx, unsigned stream)
10442 {
10443 Builder bld(ctx->program, ctx->block);
10444
10445 Temp so_buffers[4];
10446 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10447 for (unsigned i = 0; i < 4; i++) {
10448 unsigned stride = ctx->program->info->so.strides[i];
10449 if (!stride)
10450 continue;
10451
10452 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10453 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10454 }
10455
10456 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10457 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10458
10459 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10460
10461 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10462
10463 if_context ic;
10464 begin_divergent_if_then(ctx, &ic, can_emit);
10465
10466 bld.reset(ctx->block);
10467
10468 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10469
10470 Temp so_write_offset[4];
10471
10472 for (unsigned i = 0; i < 4; i++) {
10473 unsigned stride = ctx->program->info->so.strides[i];
10474 if (!stride)
10475 continue;
10476
10477 if (stride == 1) {
10478 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10479 get_arg(ctx, ctx->args->streamout_write_idx),
10480 get_arg(ctx, ctx->args->streamout_offset[i]));
10481 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10482
10483 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10484 } else {
10485 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10486 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10487 get_arg(ctx, ctx->args->streamout_offset[i]));
10488 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10489 }
10490 }
10491
10492 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10493 struct radv_stream_output *output =
10494 &ctx->program->info->so.outputs[i];
10495 if (stream != output->stream)
10496 continue;
10497
10498 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10499 }
10500
10501 begin_divergent_if_else(ctx, &ic);
10502 end_divergent_if(ctx, &ic);
10503 }
10504
10505 } /* end namespace */
10506
10507 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10508 {
10509 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10510 Builder bld(ctx->program, ctx->block);
10511 constexpr unsigned hs_idx = 1u;
10512 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10513 get_arg(ctx, ctx->args->merged_wave_info),
10514 Operand((8u << 16) | (hs_idx * 8u)));
10515 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10516
10517 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10518
10519 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10520 get_arg(ctx, ctx->args->rel_auto_id),
10521 get_arg(ctx, ctx->args->ac.instance_id),
10522 ls_has_nonzero_hs_threads);
10523 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10524 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10525 get_arg(ctx, ctx->args->rel_auto_id),
10526 ls_has_nonzero_hs_threads);
10527 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10528 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10529 get_arg(ctx, ctx->args->ac.vertex_id),
10530 ls_has_nonzero_hs_threads);
10531
10532 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10533 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10534 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10535 }
10536
10537 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10538 {
10539 /* Split all arguments except for the first (ring_offsets) and the last
10540 * (exec) so that the dead channels don't stay live throughout the program.
10541 */
10542 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10543 if (startpgm->definitions[i].regClass().size() > 1) {
10544 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10545 startpgm->definitions[i].regClass().size());
10546 }
10547 }
10548 }
10549
10550 void handle_bc_optimize(isel_context *ctx)
10551 {
10552 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10553 Builder bld(ctx->program, ctx->block);
10554 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10555 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10556 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10557 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10558 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10559 if (uses_center && uses_centroid) {
10560 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10561 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10562
10563 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10564 Temp new_coord[2];
10565 for (unsigned i = 0; i < 2; i++) {
10566 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10567 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10568 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10569 persp_centroid, persp_center, sel);
10570 }
10571 ctx->persp_centroid = bld.tmp(v2);
10572 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10573 Operand(new_coord[0]), Operand(new_coord[1]));
10574 emit_split_vector(ctx, ctx->persp_centroid, 2);
10575 }
10576
10577 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10578 Temp new_coord[2];
10579 for (unsigned i = 0; i < 2; i++) {
10580 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10581 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10582 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10583 linear_centroid, linear_center, sel);
10584 }
10585 ctx->linear_centroid = bld.tmp(v2);
10586 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10587 Operand(new_coord[0]), Operand(new_coord[1]));
10588 emit_split_vector(ctx, ctx->linear_centroid, 2);
10589 }
10590 }
10591 }
10592
10593 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10594 {
10595 Program *program = ctx->program;
10596
10597 unsigned float_controls = shader->info.float_controls_execution_mode;
10598
10599 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10600 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10601 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10602 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10603 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10604
10605 program->next_fp_mode.must_flush_denorms32 =
10606 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10607 program->next_fp_mode.must_flush_denorms16_64 =
10608 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10609 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10610
10611 program->next_fp_mode.care_about_round32 =
10612 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10613
10614 program->next_fp_mode.care_about_round16_64 =
10615 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10616 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10617
10618 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10619 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10620 if (program->next_fp_mode.must_flush_denorms16_64)
10621 program->next_fp_mode.denorm16_64 = 0;
10622 else
10623 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10624
10625 /* preserving fp32 denorms is expensive, so only do it if asked */
10626 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10627 program->next_fp_mode.denorm32 = fp_denorm_keep;
10628 else
10629 program->next_fp_mode.denorm32 = 0;
10630
10631 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10632 program->next_fp_mode.round32 = fp_round_tz;
10633 else
10634 program->next_fp_mode.round32 = fp_round_ne;
10635
10636 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10637 program->next_fp_mode.round16_64 = fp_round_tz;
10638 else
10639 program->next_fp_mode.round16_64 = fp_round_ne;
10640
10641 ctx->block->fp_mode = program->next_fp_mode;
10642 }
10643
10644 void cleanup_cfg(Program *program)
10645 {
10646 /* create linear_succs/logical_succs */
10647 for (Block& BB : program->blocks) {
10648 for (unsigned idx : BB.linear_preds)
10649 program->blocks[idx].linear_succs.emplace_back(BB.index);
10650 for (unsigned idx : BB.logical_preds)
10651 program->blocks[idx].logical_succs.emplace_back(BB.index);
10652 }
10653 }
10654
10655 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10656 {
10657 Builder bld(ctx->program, ctx->block);
10658
10659 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10660 Temp count = i == 0
10661 ? get_arg(ctx, ctx->args->merged_wave_info)
10662 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10663 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10664
10665 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10666 Temp cond;
10667
10668 if (ctx->program->wave_size == 64) {
10669 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10670 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10671 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10672 } else {
10673 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10674 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10675 }
10676
10677 return cond;
10678 }
10679
10680 bool ngg_early_prim_export(isel_context *ctx)
10681 {
10682 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10683 return true;
10684 }
10685
10686 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10687 {
10688 Builder bld(ctx->program, ctx->block);
10689
10690 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10691 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10692
10693 /* Get the id of the current wave within the threadgroup (workgroup) */
10694 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10695 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10696
10697 /* Execute the following code only on the first wave (wave id 0),
10698 * use the SCC def to tell if the wave id is zero or not.
10699 */
10700 Temp cond = wave_id_in_tg.def(1).getTemp();
10701 if_context ic;
10702 begin_uniform_if_then(ctx, &ic, cond);
10703 begin_uniform_if_else(ctx, &ic);
10704 bld.reset(ctx->block);
10705
10706 /* Number of vertices output by VS/TES */
10707 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10708 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10709 /* Number of primitives output by VS/TES */
10710 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10711 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10712
10713 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10714 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10715 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10716
10717 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10718 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10719
10720 end_uniform_if(ctx, &ic);
10721
10722 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10723 bld.reset(ctx->block);
10724 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10725 }
10726
10727 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10728 {
10729 Builder bld(ctx->program, ctx->block);
10730
10731 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10732 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10733 }
10734
10735 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10736 Temp tmp;
10737
10738 for (unsigned i = 0; i < num_vertices; ++i) {
10739 assert(vtxindex[i].id());
10740
10741 if (i)
10742 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10743 else
10744 tmp = vtxindex[i];
10745
10746 /* The initial edge flag is always false in tess eval shaders. */
10747 if (ctx->stage == ngg_vertex_gs) {
10748 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10749 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10750 }
10751 }
10752
10753 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10754
10755 return tmp;
10756 }
10757
10758 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10759 {
10760 Builder bld(ctx->program, ctx->block);
10761 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10762
10763 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10764 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10765 false /* compressed */, true/* done */, false /* valid mask */);
10766 }
10767
10768 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10769 {
10770 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10771 * These must always come before VS exports.
10772 *
10773 * It is recommended to do these as early as possible. They can be at the beginning when
10774 * there is no SW GS and the shader doesn't write edge flags.
10775 */
10776
10777 if_context ic;
10778 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10779 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10780
10781 Builder bld(ctx->program, ctx->block);
10782 constexpr unsigned max_vertices_per_primitive = 3;
10783 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10784
10785 if (ctx->stage == ngg_vertex_gs) {
10786 /* TODO: optimize for points & lines */
10787 } else if (ctx->stage == ngg_tess_eval_gs) {
10788 if (ctx->shader->info.tess.point_mode)
10789 num_vertices_per_primitive = 1;
10790 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10791 num_vertices_per_primitive = 2;
10792 } else {
10793 unreachable("Unsupported NGG shader stage");
10794 }
10795
10796 Temp vtxindex[max_vertices_per_primitive];
10797 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10798 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10799 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10800 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10801 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10802 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10803 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10804 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10805
10806 /* Export primitive data to the index buffer. */
10807 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10808
10809 /* Export primitive ID. */
10810 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10811 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10812 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10813 Temp provoking_vtx_index = vtxindex[0];
10814 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10815
10816 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10817 }
10818
10819 begin_divergent_if_else(ctx, &ic);
10820 end_divergent_if(ctx, &ic);
10821 }
10822
10823 void ngg_emit_nogs_output(isel_context *ctx)
10824 {
10825 /* Emits NGG GS output, for stages that don't have SW GS. */
10826
10827 if_context ic;
10828 Builder bld(ctx->program, ctx->block);
10829 bool late_prim_export = !ngg_early_prim_export(ctx);
10830
10831 /* NGG streamout is currently disabled by default. */
10832 assert(!ctx->args->shader_info->so.num_outputs);
10833
10834 if (late_prim_export) {
10835 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10836 create_export_phis(ctx);
10837 /* Do what we need to do in the GS threads. */
10838 ngg_emit_nogs_gsthreads(ctx);
10839
10840 /* What comes next should be executed on ES threads. */
10841 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10842 begin_divergent_if_then(ctx, &ic, is_es_thread);
10843 bld.reset(ctx->block);
10844 }
10845
10846 /* Export VS outputs */
10847 ctx->block->kind |= block_kind_export_end;
10848 create_vs_exports(ctx);
10849
10850 /* Export primitive ID */
10851 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10852 Temp prim_id;
10853
10854 if (ctx->stage == ngg_vertex_gs) {
10855 /* Wait for GS threads to store primitive ID in LDS. */
10856 bld.barrier(aco_opcode::p_memory_barrier_shared);
10857 bld.sopp(aco_opcode::s_barrier);
10858
10859 /* Calculate LDS address where the GS threads stored the primitive ID. */
10860 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10861 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10862 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10863 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10864 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10865 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10866
10867 /* Load primitive ID from LDS. */
10868 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10869 } else if (ctx->stage == ngg_tess_eval_gs) {
10870 /* TES: Just use the patch ID as the primitive ID. */
10871 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10872 } else {
10873 unreachable("unsupported NGG shader stage.");
10874 }
10875
10876 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10877 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10878
10879 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10880 }
10881
10882 if (late_prim_export) {
10883 begin_divergent_if_else(ctx, &ic);
10884 end_divergent_if(ctx, &ic);
10885 bld.reset(ctx->block);
10886 }
10887 }
10888
10889 void select_program(Program *program,
10890 unsigned shader_count,
10891 struct nir_shader *const *shaders,
10892 ac_shader_config* config,
10893 struct radv_shader_args *args)
10894 {
10895 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10896 if_context ic_merged_wave_info;
10897 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10898
10899 for (unsigned i = 0; i < shader_count; i++) {
10900 nir_shader *nir = shaders[i];
10901 init_context(&ctx, nir);
10902
10903 setup_fp_mode(&ctx, nir);
10904
10905 if (!i) {
10906 /* needs to be after init_context() for FS */
10907 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10908 append_logical_start(ctx.block);
10909
10910 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10911 fix_ls_vgpr_init_bug(&ctx, startpgm);
10912
10913 split_arguments(&ctx, startpgm);
10914 }
10915
10916 if (ngg_no_gs) {
10917 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10918
10919 if (ngg_early_prim_export(&ctx))
10920 ngg_emit_nogs_gsthreads(&ctx);
10921 }
10922
10923 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10924 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10925 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10926 ((nir->info.stage == MESA_SHADER_VERTEX &&
10927 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10928 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10929 ctx.stage == tess_eval_geometry_gs));
10930
10931 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10932 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10933 if (check_merged_wave_info) {
10934 Temp cond = merged_wave_info_to_mask(&ctx, i);
10935 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10936 }
10937
10938 if (i) {
10939 Builder bld(ctx.program, ctx.block);
10940
10941 bld.barrier(aco_opcode::p_memory_barrier_shared);
10942 bld.sopp(aco_opcode::s_barrier);
10943
10944 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10945 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));
10946 }
10947 } else if (ctx.stage == geometry_gs)
10948 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10949
10950 if (ctx.stage == fragment_fs)
10951 handle_bc_optimize(&ctx);
10952
10953 visit_cf_list(&ctx, &func->body);
10954
10955 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10956 emit_streamout(&ctx, 0);
10957
10958 if (ctx.stage & hw_vs) {
10959 create_vs_exports(&ctx);
10960 ctx.block->kind |= block_kind_export_end;
10961 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10962 ngg_emit_nogs_output(&ctx);
10963 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10964 Builder bld(ctx.program, ctx.block);
10965 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10966 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10967 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10968 write_tcs_tess_factors(&ctx);
10969 }
10970
10971 if (ctx.stage == fragment_fs) {
10972 create_fs_exports(&ctx);
10973 ctx.block->kind |= block_kind_export_end;
10974 }
10975
10976 if (endif_merged_wave_info) {
10977 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10978 end_divergent_if(&ctx, &ic_merged_wave_info);
10979 }
10980
10981 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10982 ngg_emit_nogs_output(&ctx);
10983
10984 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10985 /* Outputs of the previous stage are inputs to the next stage */
10986 ctx.inputs = ctx.outputs;
10987 ctx.outputs = shader_io_state();
10988 }
10989 }
10990
10991 program->config->float_mode = program->blocks[0].fp_mode.val;
10992
10993 append_logical_end(ctx.block);
10994 ctx.block->kind |= block_kind_uniform;
10995 Builder bld(ctx.program, ctx.block);
10996 if (ctx.program->wb_smem_l1_on_end)
10997 bld.smem(aco_opcode::s_dcache_wb, false);
10998 bld.sopp(aco_opcode::s_endpgm);
10999
11000 cleanup_cfg(program);
11001 }
11002
11003 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
11004 ac_shader_config* config,
11005 struct radv_shader_args *args)
11006 {
11007 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
11008
11009 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
11010 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
11011 program->next_fp_mode.must_flush_denorms32 = false;
11012 program->next_fp_mode.must_flush_denorms16_64 = false;
11013 program->next_fp_mode.care_about_round32 = false;
11014 program->next_fp_mode.care_about_round16_64 = false;
11015 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
11016 program->next_fp_mode.denorm32 = 0;
11017 program->next_fp_mode.round32 = fp_round_ne;
11018 program->next_fp_mode.round16_64 = fp_round_ne;
11019 ctx.block->fp_mode = program->next_fp_mode;
11020
11021 add_startpgm(&ctx);
11022 append_logical_start(ctx.block);
11023
11024 Builder bld(ctx.program, ctx.block);
11025
11026 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
11027
11028 Operand stream_id(0u);
11029 if (args->shader_info->so.num_outputs)
11030 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
11031 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
11032
11033 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
11034
11035 std::stack<Block> endif_blocks;
11036
11037 for (unsigned stream = 0; stream < 4; stream++) {
11038 if (stream_id.isConstant() && stream != stream_id.constantValue())
11039 continue;
11040
11041 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
11042 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
11043 continue;
11044
11045 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
11046
11047 unsigned BB_if_idx = ctx.block->index;
11048 Block BB_endif = Block();
11049 if (!stream_id.isConstant()) {
11050 /* begin IF */
11051 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
11052 append_logical_end(ctx.block);
11053 ctx.block->kind |= block_kind_uniform;
11054 bld.branch(aco_opcode::p_cbranch_z, cond);
11055
11056 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
11057
11058 ctx.block = ctx.program->create_and_insert_block();
11059 add_edge(BB_if_idx, ctx.block);
11060 bld.reset(ctx.block);
11061 append_logical_start(ctx.block);
11062 }
11063
11064 unsigned offset = 0;
11065 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
11066 if (args->shader_info->gs.output_streams[i] != stream)
11067 continue;
11068
11069 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
11070 unsigned length = util_last_bit(output_usage_mask);
11071 for (unsigned j = 0; j < length; ++j) {
11072 if (!(output_usage_mask & (1 << j)))
11073 continue;
11074
11075 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
11076 Temp voffset = vtx_offset;
11077 if (const_offset >= 4096u) {
11078 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
11079 const_offset %= 4096u;
11080 }
11081
11082 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
11083 mubuf->definitions[0] = bld.def(v1);
11084 mubuf->operands[0] = Operand(gsvs_ring);
11085 mubuf->operands[1] = Operand(voffset);
11086 mubuf->operands[2] = Operand(0u);
11087 mubuf->offen = true;
11088 mubuf->offset = const_offset;
11089 mubuf->glc = true;
11090 mubuf->slc = true;
11091 mubuf->dlc = args->options->chip_class >= GFX10;
11092 mubuf->barrier = barrier_none;
11093 mubuf->can_reorder = true;
11094
11095 ctx.outputs.mask[i] |= 1 << j;
11096 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
11097
11098 bld.insert(std::move(mubuf));
11099
11100 offset++;
11101 }
11102 }
11103
11104 if (args->shader_info->so.num_outputs) {
11105 emit_streamout(&ctx, stream);
11106 bld.reset(ctx.block);
11107 }
11108
11109 if (stream == 0) {
11110 create_vs_exports(&ctx);
11111 ctx.block->kind |= block_kind_export_end;
11112 }
11113
11114 if (!stream_id.isConstant()) {
11115 append_logical_end(ctx.block);
11116
11117 /* branch from then block to endif block */
11118 bld.branch(aco_opcode::p_branch);
11119 add_edge(ctx.block->index, &BB_endif);
11120 ctx.block->kind |= block_kind_uniform;
11121
11122 /* emit else block */
11123 ctx.block = ctx.program->create_and_insert_block();
11124 add_edge(BB_if_idx, ctx.block);
11125 bld.reset(ctx.block);
11126 append_logical_start(ctx.block);
11127
11128 endif_blocks.push(std::move(BB_endif));
11129 }
11130 }
11131
11132 while (!endif_blocks.empty()) {
11133 Block BB_endif = std::move(endif_blocks.top());
11134 endif_blocks.pop();
11135
11136 Block *BB_else = ctx.block;
11137
11138 append_logical_end(BB_else);
11139 /* branch from else block to endif block */
11140 bld.branch(aco_opcode::p_branch);
11141 add_edge(BB_else->index, &BB_endif);
11142 BB_else->kind |= block_kind_uniform;
11143
11144 /** emit endif merge block */
11145 ctx.block = program->insert_block(std::move(BB_endif));
11146 bld.reset(ctx.block);
11147 append_logical_start(ctx.block);
11148 }
11149
11150 program->config->float_mode = program->blocks[0].fp_mode.val;
11151
11152 append_logical_end(ctx.block);
11153 ctx.block->kind |= block_kind_uniform;
11154 bld.sopp(aco_opcode::s_endpgm);
11155
11156 cleanup_cfg(program);
11157 }
11158 }