aco: implement 16-bit nir_intrinsic_quad_* on GFX6-GFX7
[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 {
140 Temp thread_id_hi = bld.vop3(aco_opcode::v_mbcnt_hi_u32_b32, dst, mask_hi, thread_id_lo);
141 return thread_id_hi;
142 }
143 }
144
145 Temp emit_wqm(isel_context *ctx, Temp src, Temp dst=Temp(0, s1), bool program_needs_wqm = false)
146 {
147 Builder bld(ctx->program, ctx->block);
148
149 if (!dst.id())
150 dst = bld.tmp(src.regClass());
151
152 assert(src.size() == dst.size());
153
154 if (ctx->stage != fragment_fs) {
155 if (!dst.id())
156 return src;
157
158 bld.copy(Definition(dst), src);
159 return dst;
160 }
161
162 bld.pseudo(aco_opcode::p_wqm, Definition(dst), src);
163 ctx->program->needs_wqm |= program_needs_wqm;
164 return dst;
165 }
166
167 static Temp emit_bpermute(isel_context *ctx, Builder &bld, Temp index, Temp data)
168 {
169 if (index.regClass() == s1)
170 return bld.readlane(bld.def(s1), data, index);
171
172 if (ctx->options->chip_class <= GFX7) {
173 /* GFX6-7: there is no bpermute instruction */
174 Operand index_op(index);
175 Operand input_data(data);
176 index_op.setLateKill(true);
177 input_data.setLateKill(true);
178
179 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(bld.lm), bld.def(bld.lm, vcc), index_op, input_data);
180 } else if (ctx->options->chip_class >= GFX10 && ctx->program->wave_size == 64) {
181 /* GFX10 wave64 mode: emulate full-wave bpermute */
182 if (!ctx->has_gfx10_wave64_bpermute) {
183 ctx->has_gfx10_wave64_bpermute = true;
184 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
185 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
186 }
187
188 Temp index_is_lo = bld.vopc(aco_opcode::v_cmp_ge_u32, bld.def(bld.lm), Operand(31u), index);
189 Builder::Result index_is_lo_split = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), index_is_lo);
190 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());
191 Operand same_half = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), index_is_lo_split.def(0).getTemp(), index_is_lo_n1);
192 Operand index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
193 Operand input_data(data);
194
195 index_x4.setLateKill(true);
196 input_data.setLateKill(true);
197 same_half.setLateKill(true);
198
199 return bld.pseudo(aco_opcode::p_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc), index_x4, input_data, same_half);
200 } else {
201 /* GFX8-9 or GFX10 wave32: bpermute works normally */
202 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
203 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
204 }
205 }
206
207 Temp as_vgpr(isel_context *ctx, Temp val)
208 {
209 if (val.type() == RegType::sgpr) {
210 Builder bld(ctx->program, ctx->block);
211 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
212 }
213 assert(val.type() == RegType::vgpr);
214 return val;
215 }
216
217 //assumes a != 0xffffffff
218 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
219 {
220 assert(b != 0);
221 Builder bld(ctx->program, ctx->block);
222
223 if (util_is_power_of_two_or_zero(b)) {
224 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
225 return;
226 }
227
228 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
229
230 assert(info.multiplier <= 0xffffffff);
231
232 bool pre_shift = info.pre_shift != 0;
233 bool increment = info.increment != 0;
234 bool multiply = true;
235 bool post_shift = info.post_shift != 0;
236
237 if (!pre_shift && !increment && !multiply && !post_shift) {
238 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
239 return;
240 }
241
242 Temp pre_shift_dst = a;
243 if (pre_shift) {
244 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
245 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
246 }
247
248 Temp increment_dst = pre_shift_dst;
249 if (increment) {
250 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
251 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
252 }
253
254 Temp multiply_dst = increment_dst;
255 if (multiply) {
256 multiply_dst = post_shift ? bld.tmp(v1) : dst;
257 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
258 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
259 }
260
261 if (post_shift) {
262 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
263 }
264 }
265
266 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
267 {
268 Builder bld(ctx->program, ctx->block);
269 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
270 }
271
272
273 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
274 {
275 /* no need to extract the whole vector */
276 if (src.regClass() == dst_rc) {
277 assert(idx == 0);
278 return src;
279 }
280
281 assert(src.bytes() > (idx * dst_rc.bytes()));
282 Builder bld(ctx->program, ctx->block);
283 auto it = ctx->allocated_vec.find(src.id());
284 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
285 if (it->second[idx].regClass() == dst_rc) {
286 return it->second[idx];
287 } else {
288 assert(!dst_rc.is_subdword());
289 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
290 return bld.copy(bld.def(dst_rc), it->second[idx]);
291 }
292 }
293
294 if (dst_rc.is_subdword())
295 src = as_vgpr(ctx, src);
296
297 if (src.bytes() == dst_rc.bytes()) {
298 assert(idx == 0);
299 return bld.copy(bld.def(dst_rc), src);
300 } else {
301 Temp dst = bld.tmp(dst_rc);
302 emit_extract_vector(ctx, src, idx, dst);
303 return dst;
304 }
305 }
306
307 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
308 {
309 if (num_components == 1)
310 return;
311 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
312 return;
313 RegClass rc;
314 if (num_components > vec_src.size()) {
315 if (vec_src.type() == RegType::sgpr) {
316 /* should still help get_alu_src() */
317 emit_split_vector(ctx, vec_src, vec_src.size());
318 return;
319 }
320 /* sub-dword split */
321 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
322 } else {
323 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
324 }
325 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
326 split->operands[0] = Operand(vec_src);
327 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
328 for (unsigned i = 0; i < num_components; i++) {
329 elems[i] = {ctx->program->allocateId(), rc};
330 split->definitions[i] = Definition(elems[i]);
331 }
332 ctx->block->instructions.emplace_back(std::move(split));
333 ctx->allocated_vec.emplace(vec_src.id(), elems);
334 }
335
336 /* This vector expansion uses a mask to determine which elements in the new vector
337 * come from the original vector. The other elements are undefined. */
338 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
339 {
340 emit_split_vector(ctx, vec_src, util_bitcount(mask));
341
342 if (vec_src == dst)
343 return;
344
345 Builder bld(ctx->program, ctx->block);
346 if (num_components == 1) {
347 if (dst.type() == RegType::sgpr)
348 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
349 else
350 bld.copy(Definition(dst), vec_src);
351 return;
352 }
353
354 unsigned component_size = dst.size() / num_components;
355 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
356
357 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
358 vec->definitions[0] = Definition(dst);
359 unsigned k = 0;
360 for (unsigned i = 0; i < num_components; i++) {
361 if (mask & (1 << i)) {
362 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
363 if (dst.type() == RegType::sgpr)
364 src = bld.as_uniform(src);
365 vec->operands[i] = Operand(src);
366 } else {
367 vec->operands[i] = Operand(0u);
368 }
369 elems[i] = vec->operands[i].getTemp();
370 }
371 ctx->block->instructions.emplace_back(std::move(vec));
372 ctx->allocated_vec.emplace(dst.id(), elems);
373 }
374
375 /* adjust misaligned small bit size loads */
376 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
377 {
378 Builder bld(ctx->program, ctx->block);
379 Operand shift;
380 Temp select = Temp();
381 if (offset.isConstant()) {
382 assert(offset.constantValue() && offset.constantValue() < 4);
383 shift = Operand(offset.constantValue() * 8);
384 } else {
385 /* bit_offset = 8 * (offset & 0x3) */
386 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
387 select = bld.tmp(s1);
388 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
389 }
390
391 if (vec.size() == 1) {
392 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
393 } else if (vec.size() == 2) {
394 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
395 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
396 if (tmp == dst)
397 emit_split_vector(ctx, dst, 2);
398 else
399 emit_extract_vector(ctx, tmp, 0, dst);
400 } else if (vec.size() == 4) {
401 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
402 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
403 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
404 if (select != Temp())
405 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), select);
406 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
407 Temp mid = bld.tmp(s1);
408 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
409 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
410 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
411 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
412 emit_split_vector(ctx, dst, 2);
413 }
414 }
415
416 /* this function trims subdword vectors:
417 * if dst is vgpr - split the src and create a shrunk version according to the mask.
418 * if dst is sgpr - split the src, but move the original to sgpr. */
419 void trim_subdword_vector(isel_context *ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
420 {
421 assert(vec_src.type() == RegType::vgpr);
422 emit_split_vector(ctx, vec_src, num_components);
423
424 Builder bld(ctx->program, ctx->block);
425 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
426 unsigned component_size = vec_src.bytes() / num_components;
427 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
428
429 unsigned k = 0;
430 for (unsigned i = 0; i < num_components; i++) {
431 if (mask & (1 << i))
432 elems[k++] = emit_extract_vector(ctx, vec_src, i, rc);
433 }
434
435 if (dst.type() == RegType::vgpr) {
436 assert(dst.bytes() == k * component_size);
437 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, k, 1)};
438 for (unsigned i = 0; i < k; i++)
439 vec->operands[i] = Operand(elems[i]);
440 vec->definitions[0] = Definition(dst);
441 bld.insert(std::move(vec));
442 } else {
443 // TODO: alignbyte if mask doesn't start with 1?
444 assert(mask & 1);
445 assert(dst.size() == vec_src.size());
446 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
447 }
448 ctx->allocated_vec.emplace(dst.id(), elems);
449 }
450
451 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
452 {
453 Builder bld(ctx->program, ctx->block);
454 if (!dst.id())
455 dst = bld.tmp(bld.lm);
456
457 assert(val.regClass() == s1);
458 assert(dst.regClass() == bld.lm);
459
460 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
461 }
462
463 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
464 {
465 Builder bld(ctx->program, ctx->block);
466 if (!dst.id())
467 dst = bld.tmp(s1);
468
469 assert(val.regClass() == bld.lm);
470 assert(dst.regClass() == s1);
471
472 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
473 Temp tmp = bld.tmp(s1);
474 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
475 return emit_wqm(ctx, tmp, dst);
476 }
477
478 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
479 {
480 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
481 return get_ssa_temp(ctx, src.src.ssa);
482
483 if (src.src.ssa->num_components == size) {
484 bool identity_swizzle = true;
485 for (unsigned i = 0; identity_swizzle && i < size; i++) {
486 if (src.swizzle[i] != i)
487 identity_swizzle = false;
488 }
489 if (identity_swizzle)
490 return get_ssa_temp(ctx, src.src.ssa);
491 }
492
493 Temp vec = get_ssa_temp(ctx, src.src.ssa);
494 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
495 assert(elem_size > 0);
496 assert(vec.bytes() % elem_size == 0);
497
498 if (elem_size < 4 && vec.type() == RegType::sgpr) {
499 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
500 assert(size == 1);
501 unsigned swizzle = src.swizzle[0];
502 if (vec.size() > 1) {
503 assert(src.src.ssa->bit_size == 16);
504 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
505 swizzle = swizzle & 1;
506 }
507 if (swizzle == 0)
508 return vec;
509
510 Temp dst{ctx->program->allocateId(), s1};
511 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 2)};
512 bfe->operands[0] = Operand(vec);
513 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
514 bfe->definitions[0] = Definition(dst);
515 bfe->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
516 ctx->block->instructions.emplace_back(std::move(bfe));
517 return dst;
518 }
519
520 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
521 if (size == 1) {
522 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
523 } else {
524 assert(size <= 4);
525 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
526 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
527 for (unsigned i = 0; i < size; ++i) {
528 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
529 vec_instr->operands[i] = Operand{elems[i]};
530 }
531 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
532 vec_instr->definitions[0] = Definition(dst);
533 ctx->block->instructions.emplace_back(std::move(vec_instr));
534 ctx->allocated_vec.emplace(dst.id(), elems);
535 return dst;
536 }
537 }
538
539 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
540 {
541 if (ptr.size() == 2)
542 return ptr;
543 Builder bld(ctx->program, ctx->block);
544 if (ptr.type() == RegType::vgpr)
545 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
546 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
547 ptr, Operand((unsigned)ctx->options->address32_hi));
548 }
549
550 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
551 {
552 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
553 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
554 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
555 sop2->definitions[0] = Definition(dst);
556 if (writes_scc)
557 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
558 ctx->block->instructions.emplace_back(std::move(sop2));
559 }
560
561 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
562 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
563 {
564 Builder bld(ctx->program, ctx->block);
565 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
566 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
567 if (src1.type() == RegType::sgpr) {
568 if (commutative && src0.type() == RegType::vgpr) {
569 Temp t = src0;
570 src0 = src1;
571 src1 = t;
572 } else {
573 src1 = as_vgpr(ctx, src1);
574 }
575 }
576
577 if (flush_denorms && ctx->program->chip_class < GFX9) {
578 assert(dst.size() == 1);
579 Temp tmp = bld.vop2(op, bld.def(v1), src0, src1);
580 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
581 } else {
582 bld.vop2(op, Definition(dst), src0, src1);
583 }
584 }
585
586 void emit_vop3a_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
587 bool flush_denorms = false)
588 {
589 Temp src0 = get_alu_src(ctx, instr->src[0]);
590 Temp src1 = get_alu_src(ctx, instr->src[1]);
591 Temp src2 = get_alu_src(ctx, instr->src[2]);
592
593 /* ensure that the instruction has at most 1 sgpr operand
594 * The optimizer will inline constants for us */
595 if (src0.type() == RegType::sgpr && src1.type() == RegType::sgpr)
596 src0 = as_vgpr(ctx, src0);
597 if (src1.type() == RegType::sgpr && src2.type() == RegType::sgpr)
598 src1 = as_vgpr(ctx, src1);
599 if (src2.type() == RegType::sgpr && src0.type() == RegType::sgpr)
600 src2 = as_vgpr(ctx, src2);
601
602 Builder bld(ctx->program, ctx->block);
603 if (flush_denorms && ctx->program->chip_class < GFX9) {
604 assert(dst.size() == 1);
605 Temp tmp = bld.vop3(op, Definition(dst), src0, src1, src2);
606 bld.vop2(aco_opcode::v_mul_f32, Definition(dst), Operand(0x3f800000u), tmp);
607 } else {
608 bld.vop3(op, Definition(dst), src0, src1, src2);
609 }
610 }
611
612 void emit_vop1_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
613 {
614 Builder bld(ctx->program, ctx->block);
615 bld.vop1(op, Definition(dst), get_alu_src(ctx, instr->src[0]));
616 }
617
618 void emit_vopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
619 {
620 Temp src0 = get_alu_src(ctx, instr->src[0]);
621 Temp src1 = get_alu_src(ctx, instr->src[1]);
622 assert(src0.size() == src1.size());
623
624 aco_ptr<Instruction> vopc;
625 if (src1.type() == RegType::sgpr) {
626 if (src0.type() == RegType::vgpr) {
627 /* to swap the operands, we might also have to change the opcode */
628 switch (op) {
629 case aco_opcode::v_cmp_lt_f16:
630 op = aco_opcode::v_cmp_gt_f16;
631 break;
632 case aco_opcode::v_cmp_ge_f16:
633 op = aco_opcode::v_cmp_le_f16;
634 break;
635 case aco_opcode::v_cmp_lt_i16:
636 op = aco_opcode::v_cmp_gt_i16;
637 break;
638 case aco_opcode::v_cmp_ge_i16:
639 op = aco_opcode::v_cmp_le_i16;
640 break;
641 case aco_opcode::v_cmp_lt_u16:
642 op = aco_opcode::v_cmp_gt_u16;
643 break;
644 case aco_opcode::v_cmp_ge_u16:
645 op = aco_opcode::v_cmp_le_u16;
646 break;
647 case aco_opcode::v_cmp_lt_f32:
648 op = aco_opcode::v_cmp_gt_f32;
649 break;
650 case aco_opcode::v_cmp_ge_f32:
651 op = aco_opcode::v_cmp_le_f32;
652 break;
653 case aco_opcode::v_cmp_lt_i32:
654 op = aco_opcode::v_cmp_gt_i32;
655 break;
656 case aco_opcode::v_cmp_ge_i32:
657 op = aco_opcode::v_cmp_le_i32;
658 break;
659 case aco_opcode::v_cmp_lt_u32:
660 op = aco_opcode::v_cmp_gt_u32;
661 break;
662 case aco_opcode::v_cmp_ge_u32:
663 op = aco_opcode::v_cmp_le_u32;
664 break;
665 case aco_opcode::v_cmp_lt_f64:
666 op = aco_opcode::v_cmp_gt_f64;
667 break;
668 case aco_opcode::v_cmp_ge_f64:
669 op = aco_opcode::v_cmp_le_f64;
670 break;
671 case aco_opcode::v_cmp_lt_i64:
672 op = aco_opcode::v_cmp_gt_i64;
673 break;
674 case aco_opcode::v_cmp_ge_i64:
675 op = aco_opcode::v_cmp_le_i64;
676 break;
677 case aco_opcode::v_cmp_lt_u64:
678 op = aco_opcode::v_cmp_gt_u64;
679 break;
680 case aco_opcode::v_cmp_ge_u64:
681 op = aco_opcode::v_cmp_le_u64;
682 break;
683 default: /* eq and ne are commutative */
684 break;
685 }
686 Temp t = src0;
687 src0 = src1;
688 src1 = t;
689 } else {
690 src1 = as_vgpr(ctx, src1);
691 }
692 }
693
694 Builder bld(ctx->program, ctx->block);
695 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
696 }
697
698 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
699 {
700 Temp src0 = get_alu_src(ctx, instr->src[0]);
701 Temp src1 = get_alu_src(ctx, instr->src[1]);
702 Builder bld(ctx->program, ctx->block);
703
704 assert(dst.regClass() == bld.lm);
705 assert(src0.type() == RegType::sgpr);
706 assert(src1.type() == RegType::sgpr);
707 assert(src0.regClass() == src1.regClass());
708
709 /* Emit the SALU comparison instruction */
710 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
711 /* Turn the result into a per-lane bool */
712 bool_to_vector_condition(ctx, cmp, dst);
713 }
714
715 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
716 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)
717 {
718 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;
719 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;
720 bool use_valu = s_op == aco_opcode::num_opcodes ||
721 nir_dest_is_divergent(instr->dest.dest) ||
722 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
723 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
724 aco_opcode op = use_valu ? v_op : s_op;
725 assert(op != aco_opcode::num_opcodes);
726 assert(dst.regClass() == ctx->program->lane_mask);
727
728 if (use_valu)
729 emit_vopc_instruction(ctx, instr, op, dst);
730 else
731 emit_sopc_instruction(ctx, instr, op, dst);
732 }
733
734 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
735 {
736 Builder bld(ctx->program, ctx->block);
737 Temp src0 = get_alu_src(ctx, instr->src[0]);
738 Temp src1 = get_alu_src(ctx, instr->src[1]);
739
740 assert(dst.regClass() == bld.lm);
741 assert(src0.regClass() == bld.lm);
742 assert(src1.regClass() == bld.lm);
743
744 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
745 }
746
747 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
748 {
749 Builder bld(ctx->program, ctx->block);
750 Temp cond = get_alu_src(ctx, instr->src[0]);
751 Temp then = get_alu_src(ctx, instr->src[1]);
752 Temp els = get_alu_src(ctx, instr->src[2]);
753
754 assert(cond.regClass() == bld.lm);
755
756 if (dst.type() == RegType::vgpr) {
757 aco_ptr<Instruction> bcsel;
758 if (dst.size() == 1) {
759 then = as_vgpr(ctx, then);
760 els = as_vgpr(ctx, els);
761
762 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
763 } else if (dst.size() == 2) {
764 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
765 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
766 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
767 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
768
769 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
770 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
771
772 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
773 } else {
774 fprintf(stderr, "Unimplemented NIR instr bit size: ");
775 nir_print_instr(&instr->instr, stderr);
776 fprintf(stderr, "\n");
777 }
778 return;
779 }
780
781 if (instr->dest.dest.ssa.bit_size == 1) {
782 assert(dst.regClass() == bld.lm);
783 assert(then.regClass() == bld.lm);
784 assert(els.regClass() == bld.lm);
785 }
786
787 if (!nir_src_is_divergent(instr->src[0].src)) { /* uniform condition and values in sgpr */
788 if (dst.regClass() == s1 || dst.regClass() == s2) {
789 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
790 assert(dst.size() == then.size());
791 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
792 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
793 } else {
794 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
795 nir_print_instr(&instr->instr, stderr);
796 fprintf(stderr, "\n");
797 }
798 return;
799 }
800
801 /* divergent boolean bcsel
802 * this implements bcsel on bools: dst = s0 ? s1 : s2
803 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
804 assert(instr->dest.dest.ssa.bit_size == 1);
805
806 if (cond.id() != then.id())
807 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
808
809 if (cond.id() == els.id())
810 bld.sop1(Builder::s_mov, Definition(dst), then);
811 else
812 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
813 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
814 }
815
816 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
817 aco_opcode op, uint32_t undo)
818 {
819 /* multiply by 16777216 to handle denormals */
820 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
821 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
822 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
823 scaled = bld.vop1(op, bld.def(v1), scaled);
824 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
825
826 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
827
828 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
829 }
830
831 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
832 {
833 if (ctx->block->fp_mode.denorm32 == 0) {
834 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
835 return;
836 }
837
838 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
839 }
840
841 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
842 {
843 if (ctx->block->fp_mode.denorm32 == 0) {
844 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
845 return;
846 }
847
848 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
849 }
850
851 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
852 {
853 if (ctx->block->fp_mode.denorm32 == 0) {
854 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
855 return;
856 }
857
858 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
859 }
860
861 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
862 {
863 if (ctx->block->fp_mode.denorm32 == 0) {
864 bld.vop1(aco_opcode::v_log_f32, dst, val);
865 return;
866 }
867
868 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
869 }
870
871 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
872 {
873 if (ctx->options->chip_class >= GFX7)
874 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
875
876 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
877 /* TODO: create more efficient code! */
878 if (val.type() == RegType::sgpr)
879 val = as_vgpr(ctx, val);
880
881 /* Split the input value. */
882 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
883 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
884
885 /* Extract the exponent and compute the unbiased value. */
886 Temp exponent = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), val_hi, Operand(20u), Operand(11u));
887 exponent = bld.vsub32(bld.def(v1), exponent, Operand(1023u));
888
889 /* Extract the fractional part. */
890 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
891 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
892
893 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
894 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
895
896 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
897 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
898 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
899 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
900 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
901
902 /* Get the sign bit. */
903 Temp sign = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x80000000u), val_hi);
904
905 /* Decide the operation to apply depending on the unbiased exponent. */
906 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
907 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
908 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
909 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
910 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
911 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
912
913 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
914 }
915
916 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
917 {
918 if (ctx->options->chip_class >= GFX7)
919 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
920
921 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
922 Temp src0 = as_vgpr(ctx, val);
923
924 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
925 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
926
927 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
928 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
929 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
930
931 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
932 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
933 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
934 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
935
936 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
937 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
938
939 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
940
941 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
942 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
943
944 return add->definitions[0].getTemp();
945 }
946
947 Temp convert_int(Builder& bld, Temp src, unsigned src_bits, unsigned dst_bits, bool is_signed, Temp dst=Temp()) {
948 if (!dst.id()) {
949 if (dst_bits % 32 == 0 || src.type() == RegType::sgpr)
950 dst = bld.tmp(src.type(), DIV_ROUND_UP(dst_bits, 32u));
951 else
952 dst = bld.tmp(RegClass(RegType::vgpr, dst_bits / 8u).as_subdword());
953 }
954
955 if (dst.bytes() == src.bytes() && dst_bits < src_bits)
956 return bld.copy(Definition(dst), src);
957 else if (dst.bytes() < src.bytes())
958 return bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
959
960 Temp tmp = dst;
961 if (dst_bits == 64)
962 tmp = src_bits == 32 ? src : bld.tmp(src.type(), 1);
963
964 if (tmp == src) {
965 } else if (src.regClass() == s1) {
966 if (is_signed)
967 bld.sop1(src_bits == 8 ? aco_opcode::s_sext_i32_i8 : aco_opcode::s_sext_i32_i16, Definition(tmp), src);
968 else
969 bld.sop2(aco_opcode::s_and_b32, Definition(tmp), bld.def(s1, scc), Operand(src_bits == 8 ? 0xFFu : 0xFFFFu), src);
970 } else {
971 assert(src_bits != 8 || src.regClass() == v1b);
972 assert(src_bits != 16 || src.regClass() == v2b);
973 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
974 sdwa->operands[0] = Operand(src);
975 sdwa->definitions[0] = Definition(tmp);
976 if (is_signed)
977 sdwa->sel[0] = src_bits == 8 ? sdwa_sbyte : sdwa_sword;
978 else
979 sdwa->sel[0] = src_bits == 8 ? sdwa_ubyte : sdwa_uword;
980 sdwa->dst_sel = tmp.bytes() == 2 ? sdwa_uword : sdwa_udword;
981 bld.insert(std::move(sdwa));
982 }
983
984 if (dst_bits == 64) {
985 if (is_signed && dst.regClass() == s2) {
986 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), tmp, Operand(31u));
987 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
988 } else if (is_signed && dst.regClass() == v2) {
989 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), tmp);
990 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, high);
991 } else {
992 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tmp, Operand(0u));
993 }
994 }
995
996 return dst;
997 }
998
999 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
1000 {
1001 if (!instr->dest.dest.is_ssa) {
1002 fprintf(stderr, "nir alu dst not in ssa: ");
1003 nir_print_instr(&instr->instr, stderr);
1004 fprintf(stderr, "\n");
1005 abort();
1006 }
1007 Builder bld(ctx->program, ctx->block);
1008 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
1009 switch(instr->op) {
1010 case nir_op_vec2:
1011 case nir_op_vec3:
1012 case nir_op_vec4: {
1013 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
1014 unsigned num = instr->dest.dest.ssa.num_components;
1015 for (unsigned i = 0; i < num; ++i)
1016 elems[i] = get_alu_src(ctx, instr->src[i]);
1017
1018 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
1019 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
1020 RegClass elem_rc = RegClass::get(RegType::vgpr, instr->dest.dest.ssa.bit_size / 8u);
1021 for (unsigned i = 0; i < num; ++i) {
1022 if (elems[i].type() == RegType::sgpr && elem_rc.is_subdword())
1023 vec->operands[i] = Operand(emit_extract_vector(ctx, elems[i], 0, elem_rc));
1024 else
1025 vec->operands[i] = Operand{elems[i]};
1026 }
1027 vec->definitions[0] = Definition(dst);
1028 ctx->block->instructions.emplace_back(std::move(vec));
1029 ctx->allocated_vec.emplace(dst.id(), elems);
1030 } else {
1031 // TODO: that is a bit suboptimal..
1032 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
1033 for (unsigned i = 0; i < num - 1; ++i)
1034 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
1035 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
1036 for (unsigned i = 0; i < num; ++i) {
1037 unsigned bit = i * instr->dest.dest.ssa.bit_size;
1038 if (bit % 32 == 0) {
1039 elems[bit / 32] = elems[i];
1040 } else {
1041 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
1042 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
1043 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
1044 }
1045 }
1046 if (dst.size() == 1)
1047 bld.copy(Definition(dst), elems[0]);
1048 else
1049 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
1050 }
1051 break;
1052 }
1053 case nir_op_mov: {
1054 Temp src = get_alu_src(ctx, instr->src[0]);
1055 aco_ptr<Instruction> mov;
1056 if (dst.type() == RegType::sgpr) {
1057 if (src.type() == RegType::vgpr)
1058 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
1059 else if (src.regClass() == s1)
1060 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
1061 else if (src.regClass() == s2)
1062 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
1063 else
1064 unreachable("wrong src register class for nir_op_imov");
1065 } else {
1066 if (dst.regClass() == v1)
1067 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
1068 else if (dst.regClass() == v1b ||
1069 dst.regClass() == v2b ||
1070 dst.regClass() == v2)
1071 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
1072 else
1073 unreachable("wrong src register class for nir_op_imov");
1074 }
1075 break;
1076 }
1077 case nir_op_inot: {
1078 Temp src = get_alu_src(ctx, instr->src[0]);
1079 if (instr->dest.dest.ssa.bit_size == 1) {
1080 assert(src.regClass() == bld.lm);
1081 assert(dst.regClass() == bld.lm);
1082 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1083 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1084 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1085 } else if (dst.regClass() == v1) {
1086 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1087 } else if (dst.type() == RegType::sgpr) {
1088 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1089 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1090 } else {
1091 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1092 nir_print_instr(&instr->instr, stderr);
1093 fprintf(stderr, "\n");
1094 }
1095 break;
1096 }
1097 case nir_op_ineg: {
1098 Temp src = get_alu_src(ctx, instr->src[0]);
1099 if (dst.regClass() == v1) {
1100 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1101 } else if (dst.regClass() == s1) {
1102 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1103 } else if (dst.size() == 2) {
1104 Temp src0 = bld.tmp(dst.type(), 1);
1105 Temp src1 = bld.tmp(dst.type(), 1);
1106 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1107
1108 if (dst.regClass() == s2) {
1109 Temp carry = bld.tmp(s1);
1110 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1111 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1112 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1113 } else {
1114 Temp lower = bld.tmp(v1);
1115 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1116 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1117 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1118 }
1119 } else {
1120 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1121 nir_print_instr(&instr->instr, stderr);
1122 fprintf(stderr, "\n");
1123 }
1124 break;
1125 }
1126 case nir_op_iabs: {
1127 if (dst.regClass() == s1) {
1128 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1129 } else if (dst.regClass() == v1) {
1130 Temp src = get_alu_src(ctx, instr->src[0]);
1131 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1132 } else {
1133 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1134 nir_print_instr(&instr->instr, stderr);
1135 fprintf(stderr, "\n");
1136 }
1137 break;
1138 }
1139 case nir_op_isign: {
1140 Temp src = get_alu_src(ctx, instr->src[0]);
1141 if (dst.regClass() == s1) {
1142 Temp tmp = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), src, Operand((uint32_t)-1));
1143 bld.sop2(aco_opcode::s_min_i32, Definition(dst), bld.def(s1, scc), tmp, Operand(1u));
1144 } else if (dst.regClass() == s2) {
1145 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1146 Temp neqz;
1147 if (ctx->program->chip_class >= GFX8)
1148 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1149 else
1150 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1151 /* SCC gets zero-extended to 64 bit */
1152 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1153 } else if (dst.regClass() == v1) {
1154 bld.vop3(aco_opcode::v_med3_i32, Definition(dst), Operand((uint32_t)-1), src, Operand(1u));
1155 } else if (dst.regClass() == v2) {
1156 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1157 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1158 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1159 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1160 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1161 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1162 } else {
1163 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1164 nir_print_instr(&instr->instr, stderr);
1165 fprintf(stderr, "\n");
1166 }
1167 break;
1168 }
1169 case nir_op_imax: {
1170 if (dst.regClass() == v1) {
1171 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1172 } else if (dst.regClass() == s1) {
1173 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1174 } else {
1175 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1176 nir_print_instr(&instr->instr, stderr);
1177 fprintf(stderr, "\n");
1178 }
1179 break;
1180 }
1181 case nir_op_umax: {
1182 if (dst.regClass() == v1) {
1183 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1184 } else if (dst.regClass() == s1) {
1185 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1186 } else {
1187 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1188 nir_print_instr(&instr->instr, stderr);
1189 fprintf(stderr, "\n");
1190 }
1191 break;
1192 }
1193 case nir_op_imin: {
1194 if (dst.regClass() == v1) {
1195 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1196 } else if (dst.regClass() == s1) {
1197 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1198 } else {
1199 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1200 nir_print_instr(&instr->instr, stderr);
1201 fprintf(stderr, "\n");
1202 }
1203 break;
1204 }
1205 case nir_op_umin: {
1206 if (dst.regClass() == v1) {
1207 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1208 } else if (dst.regClass() == s1) {
1209 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1210 } else {
1211 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1212 nir_print_instr(&instr->instr, stderr);
1213 fprintf(stderr, "\n");
1214 }
1215 break;
1216 }
1217 case nir_op_ior: {
1218 if (instr->dest.dest.ssa.bit_size == 1) {
1219 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1220 } else if (dst.regClass() == v1) {
1221 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1222 } else if (dst.regClass() == s1) {
1223 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1224 } else if (dst.regClass() == s2) {
1225 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1226 } else {
1227 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1228 nir_print_instr(&instr->instr, stderr);
1229 fprintf(stderr, "\n");
1230 }
1231 break;
1232 }
1233 case nir_op_iand: {
1234 if (instr->dest.dest.ssa.bit_size == 1) {
1235 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1236 } else if (dst.regClass() == v1) {
1237 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1238 } else if (dst.regClass() == s1) {
1239 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1240 } else if (dst.regClass() == s2) {
1241 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1242 } else {
1243 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1244 nir_print_instr(&instr->instr, stderr);
1245 fprintf(stderr, "\n");
1246 }
1247 break;
1248 }
1249 case nir_op_ixor: {
1250 if (instr->dest.dest.ssa.bit_size == 1) {
1251 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1252 } else if (dst.regClass() == v1) {
1253 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1254 } else if (dst.regClass() == s1) {
1255 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1256 } else if (dst.regClass() == s2) {
1257 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1258 } else {
1259 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1260 nir_print_instr(&instr->instr, stderr);
1261 fprintf(stderr, "\n");
1262 }
1263 break;
1264 }
1265 case nir_op_ushr: {
1266 if (dst.regClass() == v1) {
1267 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1268 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1269 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1270 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1271 } else if (dst.regClass() == v2) {
1272 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1273 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1274 } else if (dst.regClass() == s2) {
1275 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1276 } else if (dst.regClass() == s1) {
1277 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1278 } else {
1279 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1280 nir_print_instr(&instr->instr, stderr);
1281 fprintf(stderr, "\n");
1282 }
1283 break;
1284 }
1285 case nir_op_ishl: {
1286 if (dst.regClass() == v1) {
1287 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1288 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1289 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1290 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1291 } else if (dst.regClass() == v2) {
1292 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1293 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1294 } else if (dst.regClass() == s1) {
1295 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1296 } else if (dst.regClass() == s2) {
1297 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1298 } else {
1299 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1300 nir_print_instr(&instr->instr, stderr);
1301 fprintf(stderr, "\n");
1302 }
1303 break;
1304 }
1305 case nir_op_ishr: {
1306 if (dst.regClass() == v1) {
1307 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1308 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1309 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1310 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1311 } else if (dst.regClass() == v2) {
1312 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1313 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1314 } else if (dst.regClass() == s1) {
1315 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1316 } else if (dst.regClass() == s2) {
1317 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1318 } else {
1319 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1320 nir_print_instr(&instr->instr, stderr);
1321 fprintf(stderr, "\n");
1322 }
1323 break;
1324 }
1325 case nir_op_find_lsb: {
1326 Temp src = get_alu_src(ctx, instr->src[0]);
1327 if (src.regClass() == s1) {
1328 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1329 } else if (src.regClass() == v1) {
1330 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1331 } else if (src.regClass() == s2) {
1332 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1333 } else {
1334 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1335 nir_print_instr(&instr->instr, stderr);
1336 fprintf(stderr, "\n");
1337 }
1338 break;
1339 }
1340 case nir_op_ufind_msb:
1341 case nir_op_ifind_msb: {
1342 Temp src = get_alu_src(ctx, instr->src[0]);
1343 if (src.regClass() == s1 || src.regClass() == s2) {
1344 aco_opcode op = src.regClass() == s2 ?
1345 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1346 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1347 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1348
1349 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1350 Operand(src.size() * 32u - 1u), msb_rev);
1351 Temp msb = sub.def(0).getTemp();
1352 Temp carry = sub.def(1).getTemp();
1353
1354 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1355 } else if (src.regClass() == v1) {
1356 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1357 Temp msb_rev = bld.tmp(v1);
1358 emit_vop1_instruction(ctx, instr, op, msb_rev);
1359 Temp msb = bld.tmp(v1);
1360 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1361 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1362 } else {
1363 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1364 nir_print_instr(&instr->instr, stderr);
1365 fprintf(stderr, "\n");
1366 }
1367 break;
1368 }
1369 case nir_op_bitfield_reverse: {
1370 if (dst.regClass() == s1) {
1371 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1372 } else if (dst.regClass() == v1) {
1373 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1374 } else {
1375 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1376 nir_print_instr(&instr->instr, stderr);
1377 fprintf(stderr, "\n");
1378 }
1379 break;
1380 }
1381 case nir_op_iadd: {
1382 if (dst.regClass() == s1) {
1383 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1384 break;
1385 }
1386
1387 Temp src0 = get_alu_src(ctx, instr->src[0]);
1388 Temp src1 = get_alu_src(ctx, instr->src[1]);
1389 if (dst.regClass() == v1) {
1390 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1391 break;
1392 }
1393
1394 assert(src0.size() == 2 && src1.size() == 2);
1395 Temp src00 = bld.tmp(src0.type(), 1);
1396 Temp src01 = bld.tmp(dst.type(), 1);
1397 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1398 Temp src10 = bld.tmp(src1.type(), 1);
1399 Temp src11 = bld.tmp(dst.type(), 1);
1400 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1401
1402 if (dst.regClass() == s2) {
1403 Temp carry = bld.tmp(s1);
1404 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1405 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1406 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1407 } else if (dst.regClass() == v2) {
1408 Temp dst0 = bld.tmp(v1);
1409 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1410 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1411 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1412 } else {
1413 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1414 nir_print_instr(&instr->instr, stderr);
1415 fprintf(stderr, "\n");
1416 }
1417 break;
1418 }
1419 case nir_op_uadd_sat: {
1420 Temp src0 = get_alu_src(ctx, instr->src[0]);
1421 Temp src1 = get_alu_src(ctx, instr->src[1]);
1422 if (dst.regClass() == s1) {
1423 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1424 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1425 src0, src1);
1426 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1427 } else if (dst.regClass() == v1) {
1428 if (ctx->options->chip_class >= GFX9) {
1429 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1430 add->operands[0] = Operand(src0);
1431 add->operands[1] = Operand(src1);
1432 add->definitions[0] = Definition(dst);
1433 add->clamp = 1;
1434 ctx->block->instructions.emplace_back(std::move(add));
1435 } else {
1436 if (src1.regClass() != v1)
1437 std::swap(src0, src1);
1438 assert(src1.regClass() == v1);
1439 Temp tmp = bld.tmp(v1);
1440 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1441 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1442 }
1443 } else {
1444 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1445 nir_print_instr(&instr->instr, stderr);
1446 fprintf(stderr, "\n");
1447 }
1448 break;
1449 }
1450 case nir_op_uadd_carry: {
1451 Temp src0 = get_alu_src(ctx, instr->src[0]);
1452 Temp src1 = get_alu_src(ctx, instr->src[1]);
1453 if (dst.regClass() == s1) {
1454 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1455 break;
1456 }
1457 if (dst.regClass() == v1) {
1458 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1459 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1460 break;
1461 }
1462
1463 Temp src00 = bld.tmp(src0.type(), 1);
1464 Temp src01 = bld.tmp(dst.type(), 1);
1465 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1466 Temp src10 = bld.tmp(src1.type(), 1);
1467 Temp src11 = bld.tmp(dst.type(), 1);
1468 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1469 if (dst.regClass() == s2) {
1470 Temp carry = bld.tmp(s1);
1471 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1472 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1473 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1474 } else if (dst.regClass() == v2) {
1475 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1476 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1477 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1478 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1479 } else {
1480 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1481 nir_print_instr(&instr->instr, stderr);
1482 fprintf(stderr, "\n");
1483 }
1484 break;
1485 }
1486 case nir_op_isub: {
1487 if (dst.regClass() == s1) {
1488 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1489 break;
1490 }
1491
1492 Temp src0 = get_alu_src(ctx, instr->src[0]);
1493 Temp src1 = get_alu_src(ctx, instr->src[1]);
1494 if (dst.regClass() == v1) {
1495 bld.vsub32(Definition(dst), src0, src1);
1496 break;
1497 }
1498
1499 Temp src00 = bld.tmp(src0.type(), 1);
1500 Temp src01 = bld.tmp(dst.type(), 1);
1501 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1502 Temp src10 = bld.tmp(src1.type(), 1);
1503 Temp src11 = bld.tmp(dst.type(), 1);
1504 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1505 if (dst.regClass() == s2) {
1506 Temp carry = bld.tmp(s1);
1507 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1508 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1509 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1510 } else if (dst.regClass() == v2) {
1511 Temp lower = bld.tmp(v1);
1512 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1513 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1514 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1515 } else {
1516 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1517 nir_print_instr(&instr->instr, stderr);
1518 fprintf(stderr, "\n");
1519 }
1520 break;
1521 }
1522 case nir_op_usub_borrow: {
1523 Temp src0 = get_alu_src(ctx, instr->src[0]);
1524 Temp src1 = get_alu_src(ctx, instr->src[1]);
1525 if (dst.regClass() == s1) {
1526 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1527 break;
1528 } else if (dst.regClass() == v1) {
1529 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1530 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1531 break;
1532 }
1533
1534 Temp src00 = bld.tmp(src0.type(), 1);
1535 Temp src01 = bld.tmp(dst.type(), 1);
1536 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1537 Temp src10 = bld.tmp(src1.type(), 1);
1538 Temp src11 = bld.tmp(dst.type(), 1);
1539 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1540 if (dst.regClass() == s2) {
1541 Temp borrow = bld.tmp(s1);
1542 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1543 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1544 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1545 } else if (dst.regClass() == v2) {
1546 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1547 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1548 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1549 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1550 } else {
1551 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1552 nir_print_instr(&instr->instr, stderr);
1553 fprintf(stderr, "\n");
1554 }
1555 break;
1556 }
1557 case nir_op_imul: {
1558 if (dst.regClass() == v1) {
1559 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1560 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1561 } else if (dst.regClass() == s1) {
1562 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1563 } else {
1564 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1565 nir_print_instr(&instr->instr, stderr);
1566 fprintf(stderr, "\n");
1567 }
1568 break;
1569 }
1570 case nir_op_umul_high: {
1571 if (dst.regClass() == v1) {
1572 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1573 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1574 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1575 } else if (dst.regClass() == s1) {
1576 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1577 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1578 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1579 } else {
1580 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1581 nir_print_instr(&instr->instr, stderr);
1582 fprintf(stderr, "\n");
1583 }
1584 break;
1585 }
1586 case nir_op_imul_high: {
1587 if (dst.regClass() == v1) {
1588 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1589 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1590 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1591 } else if (dst.regClass() == s1) {
1592 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1593 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1594 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1595 } else {
1596 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1597 nir_print_instr(&instr->instr, stderr);
1598 fprintf(stderr, "\n");
1599 }
1600 break;
1601 }
1602 case nir_op_fmul: {
1603 Temp src0 = get_alu_src(ctx, instr->src[0]);
1604 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1605 if (dst.regClass() == v2b) {
1606 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f16, dst, true);
1607 } else if (dst.regClass() == v1) {
1608 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1609 } else if (dst.regClass() == v2) {
1610 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), src0, src1);
1611 } else {
1612 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1613 nir_print_instr(&instr->instr, stderr);
1614 fprintf(stderr, "\n");
1615 }
1616 break;
1617 }
1618 case nir_op_fadd: {
1619 Temp src0 = get_alu_src(ctx, instr->src[0]);
1620 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1621 if (dst.regClass() == v2b) {
1622 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f16, dst, true);
1623 } else if (dst.regClass() == v1) {
1624 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1625 } else if (dst.regClass() == v2) {
1626 bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, src1);
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_fsub: {
1635 Temp src0 = get_alu_src(ctx, instr->src[0]);
1636 Temp src1 = get_alu_src(ctx, instr->src[1]);
1637 if (dst.regClass() == v2b) {
1638 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1639 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f16, dst, false);
1640 else
1641 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f16, dst, true);
1642 } else if (dst.regClass() == v1) {
1643 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1644 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1645 else
1646 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1647 } else if (dst.regClass() == v2) {
1648 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1649 as_vgpr(ctx, src0), as_vgpr(ctx, src1));
1650 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1651 sub->neg[1] = true;
1652 } else {
1653 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1654 nir_print_instr(&instr->instr, stderr);
1655 fprintf(stderr, "\n");
1656 }
1657 break;
1658 }
1659 case nir_op_fmax: {
1660 Temp src0 = get_alu_src(ctx, instr->src[0]);
1661 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1662 if (dst.regClass() == v2b) {
1663 // TODO: check fp_mode.must_flush_denorms16_64
1664 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, dst, true);
1665 } else if (dst.regClass() == v1) {
1666 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1667 } else if (dst.regClass() == v2) {
1668 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1669 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1670 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1671 } else {
1672 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1673 }
1674 } else {
1675 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1676 nir_print_instr(&instr->instr, stderr);
1677 fprintf(stderr, "\n");
1678 }
1679 break;
1680 }
1681 case nir_op_fmin: {
1682 Temp src0 = get_alu_src(ctx, instr->src[0]);
1683 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1684 if (dst.regClass() == v2b) {
1685 // TODO: check fp_mode.must_flush_denorms16_64
1686 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, dst, true);
1687 } else if (dst.regClass() == v1) {
1688 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1689 } else if (dst.regClass() == v2) {
1690 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1691 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1692 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1693 } else {
1694 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1695 }
1696 } else {
1697 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1698 nir_print_instr(&instr->instr, stderr);
1699 fprintf(stderr, "\n");
1700 }
1701 break;
1702 }
1703 case nir_op_fmax3: {
1704 if (dst.regClass() == v2b) {
1705 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f16, dst, false);
1706 } else if (dst.regClass() == v1) {
1707 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1708 } else {
1709 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1710 nir_print_instr(&instr->instr, stderr);
1711 fprintf(stderr, "\n");
1712 }
1713 break;
1714 }
1715 case nir_op_fmin3: {
1716 if (dst.regClass() == v2b) {
1717 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f16, dst, false);
1718 } else if (dst.regClass() == v1) {
1719 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1720 } else {
1721 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1722 nir_print_instr(&instr->instr, stderr);
1723 fprintf(stderr, "\n");
1724 }
1725 break;
1726 }
1727 case nir_op_fmed3: {
1728 if (dst.regClass() == v2b) {
1729 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f16, dst, false);
1730 } else if (dst.regClass() == v1) {
1731 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1732 } else {
1733 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1734 nir_print_instr(&instr->instr, stderr);
1735 fprintf(stderr, "\n");
1736 }
1737 break;
1738 }
1739 case nir_op_umax3: {
1740 if (dst.size() == 1) {
1741 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1742 } else {
1743 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1744 nir_print_instr(&instr->instr, stderr);
1745 fprintf(stderr, "\n");
1746 }
1747 break;
1748 }
1749 case nir_op_umin3: {
1750 if (dst.size() == 1) {
1751 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1752 } else {
1753 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1754 nir_print_instr(&instr->instr, stderr);
1755 fprintf(stderr, "\n");
1756 }
1757 break;
1758 }
1759 case nir_op_umed3: {
1760 if (dst.size() == 1) {
1761 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1762 } else {
1763 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1764 nir_print_instr(&instr->instr, stderr);
1765 fprintf(stderr, "\n");
1766 }
1767 break;
1768 }
1769 case nir_op_imax3: {
1770 if (dst.size() == 1) {
1771 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1772 } else {
1773 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1774 nir_print_instr(&instr->instr, stderr);
1775 fprintf(stderr, "\n");
1776 }
1777 break;
1778 }
1779 case nir_op_imin3: {
1780 if (dst.size() == 1) {
1781 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
1782 } else {
1783 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1784 nir_print_instr(&instr->instr, stderr);
1785 fprintf(stderr, "\n");
1786 }
1787 break;
1788 }
1789 case nir_op_imed3: {
1790 if (dst.size() == 1) {
1791 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1792 } else {
1793 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1794 nir_print_instr(&instr->instr, stderr);
1795 fprintf(stderr, "\n");
1796 }
1797 break;
1798 }
1799 case nir_op_cube_face_coord: {
1800 Temp in = get_alu_src(ctx, instr->src[0], 3);
1801 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1802 emit_extract_vector(ctx, in, 1, v1),
1803 emit_extract_vector(ctx, in, 2, v1) };
1804 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1805 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1806 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1807 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1808 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1809 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1810 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1811 break;
1812 }
1813 case nir_op_cube_face_index: {
1814 Temp in = get_alu_src(ctx, instr->src[0], 3);
1815 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1816 emit_extract_vector(ctx, in, 1, v1),
1817 emit_extract_vector(ctx, in, 2, v1) };
1818 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1819 break;
1820 }
1821 case nir_op_bcsel: {
1822 emit_bcsel(ctx, instr, dst);
1823 break;
1824 }
1825 case nir_op_frsq: {
1826 Temp src = get_alu_src(ctx, instr->src[0]);
1827 if (dst.regClass() == v2b) {
1828 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f16, dst);
1829 } else if (dst.regClass() == v1) {
1830 emit_rsq(ctx, bld, Definition(dst), src);
1831 } else if (dst.regClass() == v2) {
1832 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1833 } else {
1834 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1835 nir_print_instr(&instr->instr, stderr);
1836 fprintf(stderr, "\n");
1837 }
1838 break;
1839 }
1840 case nir_op_fneg: {
1841 Temp src = get_alu_src(ctx, instr->src[0]);
1842 if (dst.regClass() == v2b) {
1843 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x8000u), as_vgpr(ctx, src));
1844 } else if (dst.regClass() == v1) {
1845 if (ctx->block->fp_mode.must_flush_denorms32)
1846 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1847 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1848 } else if (dst.regClass() == v2) {
1849 if (ctx->block->fp_mode.must_flush_denorms16_64)
1850 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1851 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1852 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1853 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1854 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1855 } else {
1856 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1857 nir_print_instr(&instr->instr, stderr);
1858 fprintf(stderr, "\n");
1859 }
1860 break;
1861 }
1862 case nir_op_fabs: {
1863 Temp src = get_alu_src(ctx, instr->src[0]);
1864 if (dst.regClass() == v2b) {
1865 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFu), as_vgpr(ctx, src));
1866 } else if (dst.regClass() == v1) {
1867 if (ctx->block->fp_mode.must_flush_denorms32)
1868 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1869 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1870 } else if (dst.regClass() == v2) {
1871 if (ctx->block->fp_mode.must_flush_denorms16_64)
1872 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1873 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1874 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1875 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1876 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1877 } else {
1878 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1879 nir_print_instr(&instr->instr, stderr);
1880 fprintf(stderr, "\n");
1881 }
1882 break;
1883 }
1884 case nir_op_fsat: {
1885 Temp src = get_alu_src(ctx, instr->src[0]);
1886 if (dst.regClass() == v2b) {
1887 bld.vop3(aco_opcode::v_med3_f16, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1888 } else if (dst.regClass() == v1) {
1889 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1890 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1891 // TODO: confirm that this holds under any circumstances
1892 } else if (dst.regClass() == v2) {
1893 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1894 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1895 vop3->clamp = true;
1896 } else {
1897 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1898 nir_print_instr(&instr->instr, stderr);
1899 fprintf(stderr, "\n");
1900 }
1901 break;
1902 }
1903 case nir_op_flog2: {
1904 Temp src = get_alu_src(ctx, instr->src[0]);
1905 if (dst.regClass() == v2b) {
1906 emit_vop1_instruction(ctx, instr, aco_opcode::v_log_f16, dst);
1907 } else if (dst.regClass() == v1) {
1908 emit_log2(ctx, bld, Definition(dst), src);
1909 } else {
1910 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1911 nir_print_instr(&instr->instr, stderr);
1912 fprintf(stderr, "\n");
1913 }
1914 break;
1915 }
1916 case nir_op_frcp: {
1917 Temp src = get_alu_src(ctx, instr->src[0]);
1918 if (dst.regClass() == v2b) {
1919 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f16, dst);
1920 } else if (dst.regClass() == v1) {
1921 emit_rcp(ctx, bld, Definition(dst), src);
1922 } else if (dst.regClass() == v2) {
1923 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1924 } else {
1925 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1926 nir_print_instr(&instr->instr, stderr);
1927 fprintf(stderr, "\n");
1928 }
1929 break;
1930 }
1931 case nir_op_fexp2: {
1932 if (dst.regClass() == v2b) {
1933 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f16, dst);
1934 } else if (dst.regClass() == v1) {
1935 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1936 } else {
1937 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1938 nir_print_instr(&instr->instr, stderr);
1939 fprintf(stderr, "\n");
1940 }
1941 break;
1942 }
1943 case nir_op_fsqrt: {
1944 Temp src = get_alu_src(ctx, instr->src[0]);
1945 if (dst.regClass() == v2b) {
1946 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f16, dst);
1947 } else if (dst.regClass() == v1) {
1948 emit_sqrt(ctx, bld, Definition(dst), src);
1949 } else if (dst.regClass() == v2) {
1950 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1951 } else {
1952 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1953 nir_print_instr(&instr->instr, stderr);
1954 fprintf(stderr, "\n");
1955 }
1956 break;
1957 }
1958 case nir_op_ffract: {
1959 if (dst.regClass() == v2b) {
1960 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f16, dst);
1961 } else if (dst.regClass() == v1) {
1962 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1963 } else if (dst.regClass() == v2) {
1964 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1965 } else {
1966 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1967 nir_print_instr(&instr->instr, stderr);
1968 fprintf(stderr, "\n");
1969 }
1970 break;
1971 }
1972 case nir_op_ffloor: {
1973 Temp src = get_alu_src(ctx, instr->src[0]);
1974 if (dst.regClass() == v2b) {
1975 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f16, dst);
1976 } else if (dst.regClass() == v1) {
1977 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1978 } else if (dst.regClass() == v2) {
1979 emit_floor_f64(ctx, bld, Definition(dst), src);
1980 } else {
1981 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1982 nir_print_instr(&instr->instr, stderr);
1983 fprintf(stderr, "\n");
1984 }
1985 break;
1986 }
1987 case nir_op_fceil: {
1988 Temp src0 = get_alu_src(ctx, instr->src[0]);
1989 if (dst.regClass() == v2b) {
1990 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f16, dst);
1991 } else if (dst.regClass() == v1) {
1992 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1993 } else if (dst.regClass() == v2) {
1994 if (ctx->options->chip_class >= GFX7) {
1995 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1996 } else {
1997 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1998 /* trunc = trunc(src0)
1999 * if (src0 > 0.0 && src0 != trunc)
2000 * trunc += 1.0
2001 */
2002 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
2003 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
2004 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
2005 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
2006 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);
2007 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
2008 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
2009 }
2010 } else {
2011 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2012 nir_print_instr(&instr->instr, stderr);
2013 fprintf(stderr, "\n");
2014 }
2015 break;
2016 }
2017 case nir_op_ftrunc: {
2018 Temp src = get_alu_src(ctx, instr->src[0]);
2019 if (dst.regClass() == v2b) {
2020 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f16, dst);
2021 } else if (dst.regClass() == v1) {
2022 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
2023 } else if (dst.regClass() == v2) {
2024 emit_trunc_f64(ctx, bld, Definition(dst), src);
2025 } else {
2026 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2027 nir_print_instr(&instr->instr, stderr);
2028 fprintf(stderr, "\n");
2029 }
2030 break;
2031 }
2032 case nir_op_fround_even: {
2033 Temp src0 = get_alu_src(ctx, instr->src[0]);
2034 if (dst.regClass() == v2b) {
2035 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f16, dst);
2036 } else if (dst.regClass() == v1) {
2037 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
2038 } else if (dst.regClass() == v2) {
2039 if (ctx->options->chip_class >= GFX7) {
2040 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
2041 } else {
2042 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2043 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
2044 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
2045
2046 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
2047 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));
2048 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));
2049 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));
2050 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
2051 tmp = sub->definitions[0].getTemp();
2052
2053 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
2054 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
2055 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2056 Temp cond = vop3->definitions[0].getTemp();
2057
2058 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
2059 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
2060 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
2061 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
2062
2063 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
2064 }
2065 } else {
2066 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2067 nir_print_instr(&instr->instr, stderr);
2068 fprintf(stderr, "\n");
2069 }
2070 break;
2071 }
2072 case nir_op_fsin:
2073 case nir_op_fcos: {
2074 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2075 aco_ptr<Instruction> norm;
2076 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
2077 if (dst.regClass() == v2b) {
2078 Temp tmp = bld.vop2(aco_opcode::v_mul_f16, bld.def(v1), half_pi, src);
2079 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f16 : aco_opcode::v_cos_f16;
2080 bld.vop1(opcode, Definition(dst), tmp);
2081 } else if (dst.regClass() == v1) {
2082 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, src);
2083
2084 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2085 if (ctx->options->chip_class < GFX9)
2086 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2087
2088 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2089 bld.vop1(opcode, Definition(dst), tmp);
2090 } else {
2091 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2092 nir_print_instr(&instr->instr, stderr);
2093 fprintf(stderr, "\n");
2094 }
2095 break;
2096 }
2097 case nir_op_ldexp: {
2098 Temp src0 = get_alu_src(ctx, instr->src[0]);
2099 Temp src1 = get_alu_src(ctx, instr->src[1]);
2100 if (dst.regClass() == v2b) {
2101 emit_vop2_instruction(ctx, instr, aco_opcode::v_ldexp_f16, dst, false);
2102 } else if (dst.regClass() == v1) {
2103 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst), as_vgpr(ctx, src0), src1);
2104 } else if (dst.regClass() == v2) {
2105 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst), as_vgpr(ctx, src0), src1);
2106 } else {
2107 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2108 nir_print_instr(&instr->instr, stderr);
2109 fprintf(stderr, "\n");
2110 }
2111 break;
2112 }
2113 case nir_op_frexp_sig: {
2114 Temp src = get_alu_src(ctx, instr->src[0]);
2115 if (dst.regClass() == v2b) {
2116 bld.vop1(aco_opcode::v_frexp_mant_f16, Definition(dst), src);
2117 } else if (dst.regClass() == v1) {
2118 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2119 } else if (dst.regClass() == v2) {
2120 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2121 } else {
2122 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2123 nir_print_instr(&instr->instr, stderr);
2124 fprintf(stderr, "\n");
2125 }
2126 break;
2127 }
2128 case nir_op_frexp_exp: {
2129 Temp src = get_alu_src(ctx, instr->src[0]);
2130 if (instr->src[0].src.ssa->bit_size == 16) {
2131 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2132 tmp = bld.pseudo(aco_opcode::p_extract_vector, bld.def(v1b), tmp, Operand(0u));
2133 convert_int(bld, tmp, 8, 32, true, dst);
2134 } else if (instr->src[0].src.ssa->bit_size == 32) {
2135 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2136 } else if (instr->src[0].src.ssa->bit_size == 64) {
2137 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2138 } else {
2139 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2140 nir_print_instr(&instr->instr, stderr);
2141 fprintf(stderr, "\n");
2142 }
2143 break;
2144 }
2145 case nir_op_fsign: {
2146 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2147 if (dst.regClass() == v2b) {
2148 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2149 Temp minus_one = bld.copy(bld.def(v1), Operand(0xbc00u));
2150 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2151 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), one, src, cond);
2152 cond = bld.vopc(aco_opcode::v_cmp_le_f16, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2153 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), minus_one, src, cond);
2154 } else if (dst.regClass() == v1) {
2155 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2156 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2157 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2158 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2159 } else if (dst.regClass() == v2) {
2160 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2161 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2162 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2163
2164 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2165 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2166 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2167
2168 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2169 } else {
2170 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2171 nir_print_instr(&instr->instr, stderr);
2172 fprintf(stderr, "\n");
2173 }
2174 break;
2175 }
2176 case nir_op_f2f16:
2177 case nir_op_f2f16_rtne: {
2178 Temp src = get_alu_src(ctx, instr->src[0]);
2179 if (instr->src[0].src.ssa->bit_size == 64)
2180 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2181 bld.vop1(aco_opcode::v_cvt_f16_f32, Definition(dst), src);
2182 break;
2183 }
2184 case nir_op_f2f16_rtz: {
2185 Temp src = get_alu_src(ctx, instr->src[0]);
2186 if (instr->src[0].src.ssa->bit_size == 64)
2187 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2188 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src, Operand(0u));
2189 break;
2190 }
2191 case nir_op_f2f32: {
2192 if (instr->src[0].src.ssa->bit_size == 16) {
2193 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2194 } else if (instr->src[0].src.ssa->bit_size == 64) {
2195 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2196 } else {
2197 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2198 nir_print_instr(&instr->instr, stderr);
2199 fprintf(stderr, "\n");
2200 }
2201 break;
2202 }
2203 case nir_op_f2f64: {
2204 Temp src = get_alu_src(ctx, instr->src[0]);
2205 if (instr->src[0].src.ssa->bit_size == 16)
2206 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2207 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2208 break;
2209 }
2210 case nir_op_i2f16: {
2211 assert(dst.regClass() == v2b);
2212 Temp src = get_alu_src(ctx, instr->src[0]);
2213 if (instr->src[0].src.ssa->bit_size == 8)
2214 src = convert_int(bld, src, 8, 16, true);
2215 bld.vop1(aco_opcode::v_cvt_f16_i16, Definition(dst), src);
2216 break;
2217 }
2218 case nir_op_i2f32: {
2219 assert(dst.size() == 1);
2220 Temp src = get_alu_src(ctx, instr->src[0]);
2221 if (instr->src[0].src.ssa->bit_size <= 16)
2222 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2223 bld.vop1(aco_opcode::v_cvt_f32_i32, Definition(dst), src);
2224 break;
2225 }
2226 case nir_op_i2f64: {
2227 if (instr->src[0].src.ssa->bit_size <= 32) {
2228 Temp src = get_alu_src(ctx, instr->src[0]);
2229 if (instr->src[0].src.ssa->bit_size <= 16)
2230 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2231 bld.vop1(aco_opcode::v_cvt_f64_i32, Definition(dst), src);
2232 } else if (instr->src[0].src.ssa->bit_size == 64) {
2233 Temp src = get_alu_src(ctx, instr->src[0]);
2234 RegClass rc = RegClass(src.type(), 1);
2235 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2236 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2237 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2238 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2239 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2240 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2241
2242 } else {
2243 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2244 nir_print_instr(&instr->instr, stderr);
2245 fprintf(stderr, "\n");
2246 }
2247 break;
2248 }
2249 case nir_op_u2f16: {
2250 assert(dst.regClass() == v2b);
2251 Temp src = get_alu_src(ctx, instr->src[0]);
2252 if (instr->src[0].src.ssa->bit_size == 8)
2253 src = convert_int(bld, src, 8, 16, false);
2254 bld.vop1(aco_opcode::v_cvt_f16_u16, Definition(dst), src);
2255 break;
2256 }
2257 case nir_op_u2f32: {
2258 assert(dst.size() == 1);
2259 Temp src = get_alu_src(ctx, instr->src[0]);
2260 if (instr->src[0].src.ssa->bit_size == 8) {
2261 //TODO: we should use v_cvt_f32_ubyte1/v_cvt_f32_ubyte2/etc depending on the register assignment
2262 bld.vop1(aco_opcode::v_cvt_f32_ubyte0, Definition(dst), src);
2263 } else {
2264 if (instr->src[0].src.ssa->bit_size == 16)
2265 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, true);
2266 bld.vop1(aco_opcode::v_cvt_f32_u32, Definition(dst), src);
2267 }
2268 break;
2269 }
2270 case nir_op_u2f64: {
2271 if (instr->src[0].src.ssa->bit_size <= 32) {
2272 Temp src = get_alu_src(ctx, instr->src[0]);
2273 if (instr->src[0].src.ssa->bit_size <= 16)
2274 src = convert_int(bld, src, instr->src[0].src.ssa->bit_size, 32, false);
2275 bld.vop1(aco_opcode::v_cvt_f64_u32, Definition(dst), src);
2276 } else if (instr->src[0].src.ssa->bit_size == 64) {
2277 Temp src = get_alu_src(ctx, instr->src[0]);
2278 RegClass rc = RegClass(src.type(), 1);
2279 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2280 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2281 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2282 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2283 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2284 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, 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_f2i8:
2293 case nir_op_f2i16: {
2294 Temp src = get_alu_src(ctx, instr->src[0]);
2295 if (instr->src[0].src.ssa->bit_size == 16)
2296 src = bld.vop1(aco_opcode::v_cvt_i16_f16, bld.def(v1), src);
2297 else if (instr->src[0].src.ssa->bit_size == 32)
2298 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2299 else
2300 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2301
2302 if (dst.type() == RegType::vgpr)
2303 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2304 else
2305 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2306 break;
2307 }
2308 case nir_op_f2u8:
2309 case nir_op_f2u16: {
2310 Temp src = get_alu_src(ctx, instr->src[0]);
2311 if (instr->src[0].src.ssa->bit_size == 16)
2312 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2313 else if (instr->src[0].src.ssa->bit_size == 32)
2314 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2315 else
2316 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2317
2318 if (dst.type() == RegType::vgpr)
2319 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(0u));
2320 else
2321 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2322 break;
2323 }
2324 case nir_op_f2i32: {
2325 Temp src = get_alu_src(ctx, instr->src[0]);
2326 if (instr->src[0].src.ssa->bit_size == 16) {
2327 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2328 if (dst.type() == RegType::vgpr) {
2329 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), tmp);
2330 } else {
2331 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2332 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), tmp));
2333 }
2334 } else if (instr->src[0].src.ssa->bit_size == 32) {
2335 if (dst.type() == RegType::vgpr)
2336 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2337 else
2338 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2339 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2340
2341 } else if (instr->src[0].src.ssa->bit_size == 64) {
2342 if (dst.type() == RegType::vgpr)
2343 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2344 else
2345 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2346 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2347
2348 } else {
2349 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2350 nir_print_instr(&instr->instr, stderr);
2351 fprintf(stderr, "\n");
2352 }
2353 break;
2354 }
2355 case nir_op_f2u32: {
2356 Temp src = get_alu_src(ctx, instr->src[0]);
2357 if (instr->src[0].src.ssa->bit_size == 16) {
2358 Temp tmp = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2359 if (dst.type() == RegType::vgpr) {
2360 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), tmp);
2361 } else {
2362 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2363 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), tmp));
2364 }
2365 } else if (instr->src[0].src.ssa->bit_size == 32) {
2366 if (dst.type() == RegType::vgpr)
2367 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2368 else
2369 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2370 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2371
2372 } else if (instr->src[0].src.ssa->bit_size == 64) {
2373 if (dst.type() == RegType::vgpr)
2374 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2375 else
2376 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2377 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2378
2379 } else {
2380 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2381 nir_print_instr(&instr->instr, stderr);
2382 fprintf(stderr, "\n");
2383 }
2384 break;
2385 }
2386 case nir_op_f2i64: {
2387 Temp src = get_alu_src(ctx, instr->src[0]);
2388 if (instr->src[0].src.ssa->bit_size == 16)
2389 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2390
2391 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2392 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2393 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2394 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2395 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2396 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2397 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2398 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2399 Temp new_exponent = bld.tmp(v1);
2400 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2401 if (ctx->program->chip_class >= GFX8)
2402 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2403 else
2404 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2405 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2406 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2407 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2408 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2409 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2410 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2411 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2412 Temp new_lower = bld.tmp(v1);
2413 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2414 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2415 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2416
2417 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2418 if (src.type() == RegType::vgpr)
2419 src = bld.as_uniform(src);
2420 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2421 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2422 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2423 exponent = bld.sop2(aco_opcode::s_min_i32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2424 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2425 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2426 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2427 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2428 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2429 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2430 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2431 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2432 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2433 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2434 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2435 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2436 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2437 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2438 Temp borrow = bld.tmp(s1);
2439 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2440 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2441 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2442
2443 } else if (instr->src[0].src.ssa->bit_size == 64) {
2444 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2445 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2446 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2447 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2448 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2449 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2450 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2451 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2452 if (dst.type() == RegType::sgpr) {
2453 lower = bld.as_uniform(lower);
2454 upper = bld.as_uniform(upper);
2455 }
2456 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2457
2458 } else {
2459 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2460 nir_print_instr(&instr->instr, stderr);
2461 fprintf(stderr, "\n");
2462 }
2463 break;
2464 }
2465 case nir_op_f2u64: {
2466 Temp src = get_alu_src(ctx, instr->src[0]);
2467 if (instr->src[0].src.ssa->bit_size == 16)
2468 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2469
2470 if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::vgpr) {
2471 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2472 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2473 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2474 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2475 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2476 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2477 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2478 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2479 Temp new_exponent = bld.tmp(v1);
2480 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2481 if (ctx->program->chip_class >= GFX8)
2482 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2483 else
2484 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2485 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2486 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2487 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2488 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2489 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2490 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2491 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2492
2493 } else if (instr->src[0].src.ssa->bit_size <= 32 && dst.type() == RegType::sgpr) {
2494 if (src.type() == RegType::vgpr)
2495 src = bld.as_uniform(src);
2496 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2497 exponent = bld.sop2(aco_opcode::s_sub_i32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2498 exponent = bld.sop2(aco_opcode::s_max_i32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2499 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2500 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2501 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2502 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2503 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2504 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2505 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2506 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2507 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2508 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2509 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2510 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2511 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2512 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2513 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2514
2515 } else if (instr->src[0].src.ssa->bit_size == 64) {
2516 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2517 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2518 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2519 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2520 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2521 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2522 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2523 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2524 if (dst.type() == RegType::sgpr) {
2525 lower = bld.as_uniform(lower);
2526 upper = bld.as_uniform(upper);
2527 }
2528 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2529
2530 } else {
2531 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2532 nir_print_instr(&instr->instr, stderr);
2533 fprintf(stderr, "\n");
2534 }
2535 break;
2536 }
2537 case nir_op_b2f16: {
2538 Temp src = get_alu_src(ctx, instr->src[0]);
2539 assert(src.regClass() == bld.lm);
2540
2541 if (dst.regClass() == s1) {
2542 src = bool_to_scalar_condition(ctx, src);
2543 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3c00u), src);
2544 } else if (dst.regClass() == v2b) {
2545 Temp one = bld.copy(bld.def(v1), Operand(0x3c00u));
2546 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), one, src);
2547 } else {
2548 unreachable("Wrong destination register class for nir_op_b2f16.");
2549 }
2550 break;
2551 }
2552 case nir_op_b2f32: {
2553 Temp src = get_alu_src(ctx, instr->src[0]);
2554 assert(src.regClass() == bld.lm);
2555
2556 if (dst.regClass() == s1) {
2557 src = bool_to_scalar_condition(ctx, src);
2558 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2559 } else if (dst.regClass() == v1) {
2560 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2561 } else {
2562 unreachable("Wrong destination register class for nir_op_b2f32.");
2563 }
2564 break;
2565 }
2566 case nir_op_b2f64: {
2567 Temp src = get_alu_src(ctx, instr->src[0]);
2568 assert(src.regClass() == bld.lm);
2569
2570 if (dst.regClass() == s2) {
2571 src = bool_to_scalar_condition(ctx, src);
2572 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2573 } else if (dst.regClass() == v2) {
2574 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2575 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2576 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2577 } else {
2578 unreachable("Wrong destination register class for nir_op_b2f64.");
2579 }
2580 break;
2581 }
2582 case nir_op_i2i8:
2583 case nir_op_i2i16:
2584 case nir_op_i2i32:
2585 case nir_op_i2i64: {
2586 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2587 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, true, dst);
2588 break;
2589 }
2590 case nir_op_u2u8:
2591 case nir_op_u2u16:
2592 case nir_op_u2u32:
2593 case nir_op_u2u64: {
2594 convert_int(bld, get_alu_src(ctx, instr->src[0]),
2595 instr->src[0].src.ssa->bit_size, instr->dest.dest.ssa.bit_size, false, dst);
2596 break;
2597 }
2598 case nir_op_b2b32:
2599 case nir_op_b2i32: {
2600 Temp src = get_alu_src(ctx, instr->src[0]);
2601 assert(src.regClass() == bld.lm);
2602
2603 if (dst.regClass() == s1) {
2604 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2605 bool_to_scalar_condition(ctx, src, dst);
2606 } else if (dst.regClass() == v1) {
2607 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2608 } else {
2609 unreachable("Invalid register class for b2i32");
2610 }
2611 break;
2612 }
2613 case nir_op_b2b1:
2614 case nir_op_i2b1: {
2615 Temp src = get_alu_src(ctx, instr->src[0]);
2616 assert(dst.regClass() == bld.lm);
2617
2618 if (src.type() == RegType::vgpr) {
2619 assert(src.regClass() == v1 || src.regClass() == v2);
2620 assert(dst.regClass() == bld.lm);
2621 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2622 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2623 } else {
2624 assert(src.regClass() == s1 || src.regClass() == s2);
2625 Temp tmp;
2626 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2627 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2628 } else {
2629 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2630 bld.scc(bld.def(s1)), Operand(0u), src);
2631 }
2632 bool_to_vector_condition(ctx, tmp, dst);
2633 }
2634 break;
2635 }
2636 case nir_op_pack_64_2x32_split: {
2637 Temp src0 = get_alu_src(ctx, instr->src[0]);
2638 Temp src1 = get_alu_src(ctx, instr->src[1]);
2639
2640 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2641 break;
2642 }
2643 case nir_op_unpack_64_2x32_split_x:
2644 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2645 break;
2646 case nir_op_unpack_64_2x32_split_y:
2647 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2648 break;
2649 case nir_op_unpack_32_2x16_split_x:
2650 if (dst.type() == RegType::vgpr) {
2651 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2652 } else {
2653 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2654 }
2655 break;
2656 case nir_op_unpack_32_2x16_split_y:
2657 if (dst.type() == RegType::vgpr) {
2658 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2659 } else {
2660 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)));
2661 }
2662 break;
2663 case nir_op_pack_32_2x16_split: {
2664 Temp src0 = get_alu_src(ctx, instr->src[0]);
2665 Temp src1 = get_alu_src(ctx, instr->src[1]);
2666 if (dst.regClass() == v1) {
2667 src0 = emit_extract_vector(ctx, src0, 0, v2b);
2668 src1 = emit_extract_vector(ctx, src1, 0, v2b);
2669 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2670 } else {
2671 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2672 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2673 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2674 }
2675 break;
2676 }
2677 case nir_op_pack_half_2x16: {
2678 Temp src = get_alu_src(ctx, instr->src[0], 2);
2679
2680 if (dst.regClass() == v1) {
2681 Temp src0 = bld.tmp(v1);
2682 Temp src1 = bld.tmp(v1);
2683 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2684 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2685 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2686 else
2687 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2688 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2689 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2690 } else {
2691 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2692 nir_print_instr(&instr->instr, stderr);
2693 fprintf(stderr, "\n");
2694 }
2695 break;
2696 }
2697 case nir_op_unpack_half_2x16_split_x: {
2698 if (dst.regClass() == v1) {
2699 Builder bld(ctx->program, ctx->block);
2700 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2701 } else {
2702 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2703 nir_print_instr(&instr->instr, stderr);
2704 fprintf(stderr, "\n");
2705 }
2706 break;
2707 }
2708 case nir_op_unpack_half_2x16_split_y: {
2709 if (dst.regClass() == v1) {
2710 Builder bld(ctx->program, ctx->block);
2711 /* TODO: use SDWA here */
2712 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2713 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2714 } else {
2715 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2716 nir_print_instr(&instr->instr, stderr);
2717 fprintf(stderr, "\n");
2718 }
2719 break;
2720 }
2721 case nir_op_fquantize2f16: {
2722 Temp src = get_alu_src(ctx, instr->src[0]);
2723 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2724 Temp f32, cmp_res;
2725
2726 if (ctx->program->chip_class >= GFX8) {
2727 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2728 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2729 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2730 } else {
2731 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2732 * so compare the result and flush to 0 if it's smaller.
2733 */
2734 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2735 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2736 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2737 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2738 cmp_res = vop3->definitions[0].getTemp();
2739 }
2740
2741 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2742 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2743 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2744 } else {
2745 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2746 }
2747 break;
2748 }
2749 case nir_op_bfm: {
2750 Temp bits = get_alu_src(ctx, instr->src[0]);
2751 Temp offset = get_alu_src(ctx, instr->src[1]);
2752
2753 if (dst.regClass() == s1) {
2754 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2755 } else if (dst.regClass() == v1) {
2756 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2757 } else {
2758 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2759 nir_print_instr(&instr->instr, stderr);
2760 fprintf(stderr, "\n");
2761 }
2762 break;
2763 }
2764 case nir_op_bitfield_select: {
2765 /* (mask & insert) | (~mask & base) */
2766 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2767 Temp insert = get_alu_src(ctx, instr->src[1]);
2768 Temp base = get_alu_src(ctx, instr->src[2]);
2769
2770 /* dst = (insert & bitmask) | (base & ~bitmask) */
2771 if (dst.regClass() == s1) {
2772 aco_ptr<Instruction> sop2;
2773 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2774 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2775 Operand lhs;
2776 if (const_insert && const_bitmask) {
2777 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2778 } else {
2779 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2780 lhs = Operand(insert);
2781 }
2782
2783 Operand rhs;
2784 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2785 if (const_base && const_bitmask) {
2786 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2787 } else {
2788 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2789 rhs = Operand(base);
2790 }
2791
2792 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2793
2794 } else if (dst.regClass() == v1) {
2795 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2796 base = as_vgpr(ctx, base);
2797 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2798 insert = as_vgpr(ctx, insert);
2799
2800 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2801
2802 } else {
2803 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2804 nir_print_instr(&instr->instr, stderr);
2805 fprintf(stderr, "\n");
2806 }
2807 break;
2808 }
2809 case nir_op_ubfe:
2810 case nir_op_ibfe: {
2811 Temp base = get_alu_src(ctx, instr->src[0]);
2812 Temp offset = get_alu_src(ctx, instr->src[1]);
2813 Temp bits = get_alu_src(ctx, instr->src[2]);
2814
2815 if (dst.type() == RegType::sgpr) {
2816 Operand extract;
2817 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2818 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2819 if (const_offset && const_bits) {
2820 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2821 extract = Operand(const_extract);
2822 } else {
2823 Operand width;
2824 if (const_bits) {
2825 width = Operand(const_bits->u32 << 16);
2826 } else {
2827 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2828 }
2829 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2830 }
2831
2832 aco_opcode opcode;
2833 if (dst.regClass() == s1) {
2834 if (instr->op == nir_op_ubfe)
2835 opcode = aco_opcode::s_bfe_u32;
2836 else
2837 opcode = aco_opcode::s_bfe_i32;
2838 } else if (dst.regClass() == s2) {
2839 if (instr->op == nir_op_ubfe)
2840 opcode = aco_opcode::s_bfe_u64;
2841 else
2842 opcode = aco_opcode::s_bfe_i64;
2843 } else {
2844 unreachable("Unsupported BFE bit size");
2845 }
2846
2847 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2848
2849 } else {
2850 aco_opcode opcode;
2851 if (dst.regClass() == v1) {
2852 if (instr->op == nir_op_ubfe)
2853 opcode = aco_opcode::v_bfe_u32;
2854 else
2855 opcode = aco_opcode::v_bfe_i32;
2856 } else {
2857 unreachable("Unsupported BFE bit size");
2858 }
2859
2860 emit_vop3a_instruction(ctx, instr, opcode, dst);
2861 }
2862 break;
2863 }
2864 case nir_op_bit_count: {
2865 Temp src = get_alu_src(ctx, instr->src[0]);
2866 if (src.regClass() == s1) {
2867 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2868 } else if (src.regClass() == v1) {
2869 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2870 } else if (src.regClass() == v2) {
2871 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2872 emit_extract_vector(ctx, src, 1, v1),
2873 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2874 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2875 } else if (src.regClass() == s2) {
2876 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2877 } else {
2878 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2879 nir_print_instr(&instr->instr, stderr);
2880 fprintf(stderr, "\n");
2881 }
2882 break;
2883 }
2884 case nir_op_flt: {
2885 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f16, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2886 break;
2887 }
2888 case nir_op_fge: {
2889 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f16, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2890 break;
2891 }
2892 case nir_op_feq: {
2893 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f16, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2894 break;
2895 }
2896 case nir_op_fne: {
2897 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f16, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2898 break;
2899 }
2900 case nir_op_ilt: {
2901 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);
2902 break;
2903 }
2904 case nir_op_ige: {
2905 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);
2906 break;
2907 }
2908 case nir_op_ieq: {
2909 if (instr->src[0].src.ssa->bit_size == 1)
2910 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2911 else
2912 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,
2913 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2914 break;
2915 }
2916 case nir_op_ine: {
2917 if (instr->src[0].src.ssa->bit_size == 1)
2918 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2919 else
2920 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,
2921 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2922 break;
2923 }
2924 case nir_op_ult: {
2925 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);
2926 break;
2927 }
2928 case nir_op_uge: {
2929 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);
2930 break;
2931 }
2932 case nir_op_fddx:
2933 case nir_op_fddy:
2934 case nir_op_fddx_fine:
2935 case nir_op_fddy_fine:
2936 case nir_op_fddx_coarse:
2937 case nir_op_fddy_coarse: {
2938 Temp src = get_alu_src(ctx, instr->src[0]);
2939 uint16_t dpp_ctrl1, dpp_ctrl2;
2940 if (instr->op == nir_op_fddx_fine) {
2941 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2942 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2943 } else if (instr->op == nir_op_fddy_fine) {
2944 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2945 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2946 } else {
2947 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2948 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2949 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2950 else
2951 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2952 }
2953
2954 Temp tmp;
2955 if (ctx->program->chip_class >= GFX8) {
2956 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2957 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2958 } else {
2959 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2960 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2961 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2962 }
2963 emit_wqm(ctx, tmp, dst, true);
2964 break;
2965 }
2966 default:
2967 fprintf(stderr, "Unknown NIR ALU instr: ");
2968 nir_print_instr(&instr->instr, stderr);
2969 fprintf(stderr, "\n");
2970 }
2971 }
2972
2973 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
2974 {
2975 Temp dst = get_ssa_temp(ctx, &instr->def);
2976
2977 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2978 // which get truncated the lsb if double and msb if int
2979 // for now, we only use s_mov_b64 with 64bit inline constants
2980 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
2981 assert(dst.type() == RegType::sgpr);
2982
2983 Builder bld(ctx->program, ctx->block);
2984
2985 if (instr->def.bit_size == 1) {
2986 assert(dst.regClass() == bld.lm);
2987 int val = instr->value[0].b ? -1 : 0;
2988 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
2989 bld.sop1(Builder::s_mov, Definition(dst), op);
2990 } else if (instr->def.bit_size == 8) {
2991 /* ensure that the value is correctly represented in the low byte of the register */
2992 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u8);
2993 } else if (instr->def.bit_size == 16) {
2994 /* ensure that the value is correctly represented in the low half of the register */
2995 bld.sopk(aco_opcode::s_movk_i32, Definition(dst), instr->value[0].u16);
2996 } else if (dst.size() == 1) {
2997 bld.copy(Definition(dst), Operand(instr->value[0].u32));
2998 } else {
2999 assert(dst.size() != 1);
3000 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
3001 if (instr->def.bit_size == 64)
3002 for (unsigned i = 0; i < dst.size(); i++)
3003 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
3004 else {
3005 for (unsigned i = 0; i < dst.size(); i++)
3006 vec->operands[i] = Operand{instr->value[i].u32};
3007 }
3008 vec->definitions[0] = Definition(dst);
3009 ctx->block->instructions.emplace_back(std::move(vec));
3010 }
3011 }
3012
3013 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
3014 {
3015 uint32_t new_mask = 0;
3016 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
3017 if (mask & (1u << i))
3018 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
3019 return new_mask;
3020 }
3021
3022 void byte_align_vector(isel_context *ctx, Temp vec, Operand offset, Temp dst)
3023 {
3024 Builder bld(ctx->program, ctx->block);
3025 if (offset.isTemp()) {
3026 Temp tmp[3] = {vec, vec, vec};
3027
3028 if (vec.size() == 3) {
3029 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
3030 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
3031 } else if (vec.size() == 2) {
3032 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
3033 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
3034 }
3035 for (unsigned i = 0; i < dst.size(); i++)
3036 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], offset);
3037
3038 vec = tmp[0];
3039 if (dst.size() == 2)
3040 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
3041
3042 offset = Operand(0u);
3043 }
3044
3045 if (vec.bytes() == dst.bytes() && offset.constantValue() == 0)
3046 bld.copy(Definition(dst), vec);
3047 else
3048 trim_subdword_vector(ctx, vec, dst, vec.bytes(), ((1 << dst.bytes()) - 1) << offset.constantValue());
3049 }
3050
3051 struct LoadEmitInfo {
3052 Operand offset;
3053 Temp dst;
3054 unsigned num_components;
3055 unsigned component_size;
3056 Temp resource = Temp(0, s1);
3057 unsigned component_stride = 0;
3058 unsigned const_offset = 0;
3059 unsigned align_mul = 0;
3060 unsigned align_offset = 0;
3061
3062 bool glc = false;
3063 unsigned swizzle_component_size = 0;
3064 barrier_interaction barrier = barrier_none;
3065 bool can_reorder = true;
3066 Temp soffset = Temp(0, s1);
3067 };
3068
3069 using LoadCallback = Temp(*)(
3070 Builder& bld, const LoadEmitInfo* info, Temp offset, unsigned bytes_needed,
3071 unsigned align, unsigned const_offset, Temp dst_hint);
3072
3073 template <LoadCallback callback, bool byte_align_loads, bool supports_8bit_16bit_loads, unsigned max_const_offset_plus_one>
3074 void emit_load(isel_context *ctx, Builder& bld, const LoadEmitInfo *info)
3075 {
3076 unsigned load_size = info->num_components * info->component_size;
3077 unsigned component_size = info->component_size;
3078
3079 unsigned num_vals = 0;
3080 Temp vals[info->dst.bytes()];
3081
3082 unsigned const_offset = info->const_offset;
3083
3084 unsigned align_mul = info->align_mul ? info->align_mul : component_size;
3085 unsigned align_offset = (info->align_offset + const_offset) % align_mul;
3086
3087 unsigned bytes_read = 0;
3088 while (bytes_read < load_size) {
3089 unsigned bytes_needed = load_size - bytes_read;
3090
3091 /* add buffer for unaligned loads */
3092 int byte_align = align_mul % 4 == 0 ? align_offset % 4 : -1;
3093
3094 if (byte_align) {
3095 if ((bytes_needed > 2 || !supports_8bit_16bit_loads) && byte_align_loads) {
3096 if (info->component_stride) {
3097 assert(supports_8bit_16bit_loads && "unimplemented");
3098 bytes_needed = 2;
3099 byte_align = 0;
3100 } else {
3101 bytes_needed += byte_align == -1 ? 4 - info->align_mul : byte_align;
3102 bytes_needed = align(bytes_needed, 4);
3103 }
3104 } else {
3105 byte_align = 0;
3106 }
3107 }
3108
3109 if (info->swizzle_component_size)
3110 bytes_needed = MIN2(bytes_needed, info->swizzle_component_size);
3111 if (info->component_stride)
3112 bytes_needed = MIN2(bytes_needed, info->component_size);
3113
3114 bool need_to_align_offset = byte_align && (align_mul % 4 || align_offset % 4);
3115
3116 /* reduce constant offset */
3117 Operand offset = info->offset;
3118 unsigned reduced_const_offset = const_offset;
3119 bool remove_const_offset_completely = need_to_align_offset;
3120 if (const_offset && (remove_const_offset_completely || const_offset >= max_const_offset_plus_one)) {
3121 unsigned to_add = const_offset;
3122 if (remove_const_offset_completely) {
3123 reduced_const_offset = 0;
3124 } else {
3125 to_add = const_offset / max_const_offset_plus_one * max_const_offset_plus_one;
3126 reduced_const_offset %= max_const_offset_plus_one;
3127 }
3128 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3129 if (offset.isConstant()) {
3130 offset = Operand(offset.constantValue() + to_add);
3131 } else if (offset_tmp.regClass() == s1) {
3132 offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
3133 offset_tmp, Operand(to_add));
3134 } else if (offset_tmp.regClass() == v1) {
3135 offset = bld.vadd32(bld.def(v1), offset_tmp, Operand(to_add));
3136 } else {
3137 Temp lo = bld.tmp(offset_tmp.type(), 1);
3138 Temp hi = bld.tmp(offset_tmp.type(), 1);
3139 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3140
3141 if (offset_tmp.regClass() == s2) {
3142 Temp carry = bld.tmp(s1);
3143 lo = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), lo, Operand(to_add));
3144 hi = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), hi, carry);
3145 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), lo, hi);
3146 } else {
3147 Temp new_lo = bld.tmp(v1);
3148 Temp carry = bld.vadd32(Definition(new_lo), lo, Operand(to_add), true).def(1).getTemp();
3149 hi = bld.vadd32(bld.def(v1), hi, Operand(0u), false, carry);
3150 offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_lo, hi);
3151 }
3152 }
3153 }
3154
3155 /* align offset down if needed */
3156 Operand aligned_offset = offset;
3157 if (need_to_align_offset) {
3158 Temp offset_tmp = offset.isTemp() ? offset.getTemp() : Temp();
3159 if (offset.isConstant()) {
3160 aligned_offset = Operand(offset.constantValue() & 0xfffffffcu);
3161 } else if (offset_tmp.regClass() == s1) {
3162 aligned_offset = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfffffffcu), offset_tmp);
3163 } else if (offset_tmp.regClass() == s2) {
3164 aligned_offset = bld.sop2(aco_opcode::s_and_b64, bld.def(s2), bld.def(s1, scc), Operand((uint64_t)0xfffffffffffffffcllu), offset_tmp);
3165 } else if (offset_tmp.regClass() == v1) {
3166 aligned_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), offset_tmp);
3167 } else if (offset_tmp.regClass() == v2) {
3168 Temp hi = bld.tmp(v1), lo = bld.tmp(v1);
3169 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), offset_tmp);
3170 lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xfffffffcu), lo);
3171 aligned_offset = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), lo, hi);
3172 }
3173 }
3174 Temp aligned_offset_tmp = aligned_offset.isTemp() ? aligned_offset.getTemp() :
3175 bld.copy(bld.def(s1), aligned_offset);
3176
3177 unsigned align = align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
3178 Temp val = callback(bld, info, aligned_offset_tmp, bytes_needed, align,
3179 reduced_const_offset, byte_align ? Temp() : info->dst);
3180
3181 /* shift result right if needed */
3182 if (byte_align) {
3183 Operand align((uint32_t)byte_align);
3184 if (byte_align == -1) {
3185 if (offset.isConstant())
3186 align = Operand(offset.constantValue() % 4u);
3187 else if (offset.size() == 2)
3188 align = Operand(emit_extract_vector(ctx, offset.getTemp(), 0, RegClass(offset.getTemp().type(), 1)));
3189 else
3190 align = offset;
3191 }
3192
3193 if (align.isTemp() || align.constantValue()) {
3194 assert(val.bytes() >= load_size && "unimplemented");
3195 Temp new_val = bld.tmp(RegClass::get(val.type(), load_size));
3196 if (val.type() == RegType::sgpr)
3197 byte_align_scalar(ctx, val, align, new_val);
3198 else
3199 byte_align_vector(ctx, val, align, new_val);
3200 val = new_val;
3201 }
3202 }
3203
3204 /* add result to list and advance */
3205 if (info->component_stride) {
3206 assert(val.bytes() == info->component_size && "unimplemented");
3207 const_offset += info->component_stride;
3208 align_offset = (align_offset + info->component_stride) % align_mul;
3209 } else {
3210 const_offset += val.bytes();
3211 align_offset = (align_offset + val.bytes()) % align_mul;
3212 }
3213 bytes_read += val.bytes();
3214 vals[num_vals++] = val;
3215 }
3216
3217 /* the callback wrote directly to dst */
3218 if (vals[0] == info->dst) {
3219 assert(num_vals == 1);
3220 emit_split_vector(ctx, info->dst, info->num_components);
3221 return;
3222 }
3223
3224 /* create array of components */
3225 unsigned components_split = 0;
3226 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3227 bool has_vgprs = false;
3228 for (unsigned i = 0; i < num_vals;) {
3229 Temp tmp[num_vals];
3230 unsigned num_tmps = 0;
3231 unsigned tmp_size = 0;
3232 RegType reg_type = RegType::sgpr;
3233 while ((!tmp_size || (tmp_size % component_size)) && i < num_vals) {
3234 if (vals[i].type() == RegType::vgpr)
3235 reg_type = RegType::vgpr;
3236 tmp_size += vals[i].bytes();
3237 tmp[num_tmps++] = vals[i++];
3238 }
3239 if (num_tmps > 1) {
3240 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3241 aco_opcode::p_create_vector, Format::PSEUDO, num_tmps, 1)};
3242 for (unsigned i = 0; i < num_vals; i++)
3243 vec->operands[i] = Operand(tmp[i]);
3244 tmp[0] = bld.tmp(RegClass::get(reg_type, tmp_size));
3245 vec->definitions[0] = Definition(tmp[0]);
3246 bld.insert(std::move(vec));
3247 }
3248
3249 if (tmp[0].bytes() % component_size) {
3250 /* trim tmp[0] */
3251 assert(i == num_vals);
3252 RegClass new_rc = RegClass::get(reg_type, tmp[0].bytes() / component_size * component_size);
3253 tmp[0] = bld.pseudo(aco_opcode::p_extract_vector, bld.def(new_rc), tmp[0], Operand(0u));
3254 }
3255
3256 RegClass elem_rc = RegClass::get(reg_type, component_size);
3257
3258 unsigned start = components_split;
3259
3260 if (tmp_size == elem_rc.bytes()) {
3261 allocated_vec[components_split++] = tmp[0];
3262 } else {
3263 assert(tmp_size % elem_rc.bytes() == 0);
3264 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(
3265 aco_opcode::p_split_vector, Format::PSEUDO, 1, tmp_size / elem_rc.bytes())};
3266 for (unsigned i = 0; i < split->definitions.size(); i++) {
3267 Temp component = bld.tmp(elem_rc);
3268 allocated_vec[components_split++] = component;
3269 split->definitions[i] = Definition(component);
3270 }
3271 split->operands[0] = Operand(tmp[0]);
3272 bld.insert(std::move(split));
3273 }
3274
3275 /* try to p_as_uniform early so we can create more optimizable code and
3276 * also update allocated_vec */
3277 for (unsigned j = start; j < components_split; j++) {
3278 if (allocated_vec[j].bytes() % 4 == 0 && info->dst.type() == RegType::sgpr)
3279 allocated_vec[j] = bld.as_uniform(allocated_vec[j]);
3280 has_vgprs |= allocated_vec[j].type() == RegType::vgpr;
3281 }
3282 }
3283
3284 /* concatenate components and p_as_uniform() result if needed */
3285 if (info->dst.type() == RegType::vgpr || !has_vgprs)
3286 ctx->allocated_vec.emplace(info->dst.id(), allocated_vec);
3287
3288 int padding_bytes = MAX2((int)info->dst.bytes() - int(allocated_vec[0].bytes() * info->num_components), 0);
3289
3290 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(
3291 aco_opcode::p_create_vector, Format::PSEUDO, info->num_components + !!padding_bytes, 1)};
3292 for (unsigned i = 0; i < info->num_components; i++)
3293 vec->operands[i] = Operand(allocated_vec[i]);
3294 if (padding_bytes)
3295 vec->operands[info->num_components] = Operand(RegClass::get(RegType::vgpr, padding_bytes));
3296 if (info->dst.type() == RegType::sgpr && has_vgprs) {
3297 Temp tmp = bld.tmp(RegType::vgpr, info->dst.size());
3298 vec->definitions[0] = Definition(tmp);
3299 bld.insert(std::move(vec));
3300 bld.pseudo(aco_opcode::p_as_uniform, Definition(info->dst), tmp);
3301 } else {
3302 vec->definitions[0] = Definition(info->dst);
3303 bld.insert(std::move(vec));
3304 }
3305 }
3306
3307 Operand load_lds_size_m0(Builder& bld)
3308 {
3309 /* TODO: m0 does not need to be initialized on GFX9+ */
3310 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
3311 }
3312
3313 Temp lds_load_callback(Builder& bld, const LoadEmitInfo *info,
3314 Temp offset, unsigned bytes_needed,
3315 unsigned align, unsigned const_offset,
3316 Temp dst_hint)
3317 {
3318 offset = offset.regClass() == s1 ? bld.copy(bld.def(v1), offset) : offset;
3319
3320 Operand m = load_lds_size_m0(bld);
3321
3322 bool large_ds_read = bld.program->chip_class >= GFX7;
3323 bool usable_read2 = bld.program->chip_class >= GFX7;
3324
3325 bool read2 = false;
3326 unsigned size = 0;
3327 aco_opcode op;
3328 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3329 if (bytes_needed >= 16 && align % 16 == 0 && large_ds_read) {
3330 size = 16;
3331 op = aco_opcode::ds_read_b128;
3332 } else if (bytes_needed >= 16 && align % 8 == 0 && const_offset % 8 == 0 && usable_read2) {
3333 size = 16;
3334 read2 = true;
3335 op = aco_opcode::ds_read2_b64;
3336 } else if (bytes_needed >= 12 && align % 16 == 0 && large_ds_read) {
3337 size = 12;
3338 op = aco_opcode::ds_read_b96;
3339 } else if (bytes_needed >= 8 && align % 8 == 0) {
3340 size = 8;
3341 op = aco_opcode::ds_read_b64;
3342 } else if (bytes_needed >= 8 && align % 4 == 0 && const_offset % 4 == 0) {
3343 size = 8;
3344 read2 = true;
3345 op = aco_opcode::ds_read2_b32;
3346 } else if (bytes_needed >= 4 && align % 4 == 0) {
3347 size = 4;
3348 op = aco_opcode::ds_read_b32;
3349 } else if (bytes_needed >= 2 && align % 2 == 0) {
3350 size = 2;
3351 op = aco_opcode::ds_read_u16;
3352 } else {
3353 size = 1;
3354 op = aco_opcode::ds_read_u8;
3355 }
3356
3357 unsigned max_offset_plus_one = read2 ? 254 * (size / 2u) + 1 : 65536;
3358 if (const_offset >= max_offset_plus_one) {
3359 offset = bld.vadd32(bld.def(v1), offset, Operand(const_offset / max_offset_plus_one));
3360 const_offset %= max_offset_plus_one;
3361 }
3362
3363 if (read2)
3364 const_offset /= (size / 2u);
3365
3366 RegClass rc = RegClass(RegType::vgpr, DIV_ROUND_UP(size, 4));
3367 Temp val = rc == info->dst.regClass() && dst_hint.id() ? dst_hint : bld.tmp(rc);
3368 if (read2)
3369 bld.ds(op, Definition(val), offset, m, const_offset, const_offset + 1);
3370 else
3371 bld.ds(op, Definition(val), offset, m, const_offset);
3372
3373 if (size < 4)
3374 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, size)), val, Operand(0u));
3375
3376 return val;
3377 }
3378
3379 static auto emit_lds_load = emit_load<lds_load_callback, false, true, UINT32_MAX>;
3380
3381 Temp smem_load_callback(Builder& bld, const LoadEmitInfo *info,
3382 Temp offset, unsigned bytes_needed,
3383 unsigned align, unsigned const_offset,
3384 Temp dst_hint)
3385 {
3386 unsigned size = 0;
3387 aco_opcode op;
3388 if (bytes_needed <= 4) {
3389 size = 1;
3390 op = info->resource.id() ? aco_opcode::s_buffer_load_dword : aco_opcode::s_load_dword;
3391 } else if (bytes_needed <= 8) {
3392 size = 2;
3393 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx2 : aco_opcode::s_load_dwordx2;
3394 } else if (bytes_needed <= 16) {
3395 size = 4;
3396 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx4 : aco_opcode::s_load_dwordx4;
3397 } else if (bytes_needed <= 32) {
3398 size = 8;
3399 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx8 : aco_opcode::s_load_dwordx8;
3400 } else {
3401 size = 16;
3402 op = info->resource.id() ? aco_opcode::s_buffer_load_dwordx16 : aco_opcode::s_load_dwordx16;
3403 }
3404 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
3405 if (info->resource.id()) {
3406 load->operands[0] = Operand(info->resource);
3407 load->operands[1] = Operand(offset);
3408 } else {
3409 load->operands[0] = Operand(offset);
3410 load->operands[1] = Operand(0u);
3411 }
3412 RegClass rc(RegType::sgpr, size);
3413 Temp val = dst_hint.id() && dst_hint.regClass() == rc ? dst_hint : bld.tmp(rc);
3414 load->definitions[0] = Definition(val);
3415 load->glc = info->glc;
3416 load->dlc = info->glc && bld.program->chip_class >= GFX10;
3417 load->barrier = info->barrier;
3418 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3419 bld.insert(std::move(load));
3420 return val;
3421 }
3422
3423 static auto emit_smem_load = emit_load<smem_load_callback, true, false, 1024>;
3424
3425 Temp mubuf_load_callback(Builder& bld, const LoadEmitInfo *info,
3426 Temp offset, unsigned bytes_needed,
3427 unsigned align_, unsigned const_offset,
3428 Temp dst_hint)
3429 {
3430 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3431 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
3432
3433 if (info->soffset.id()) {
3434 if (soffset.isTemp())
3435 vaddr = bld.copy(bld.def(v1), soffset);
3436 soffset = Operand(info->soffset);
3437 }
3438
3439 unsigned bytes_size = 0;
3440 aco_opcode op;
3441 if (bytes_needed == 1) {
3442 bytes_size = 1;
3443 op = aco_opcode::buffer_load_ubyte;
3444 } else if (bytes_needed == 2) {
3445 bytes_size = 2;
3446 op = aco_opcode::buffer_load_ushort;
3447 } else if (bytes_needed <= 4) {
3448 bytes_size = 4;
3449 op = aco_opcode::buffer_load_dword;
3450 } else if (bytes_needed <= 8) {
3451 bytes_size = 8;
3452 op = aco_opcode::buffer_load_dwordx2;
3453 } else if (bytes_needed <= 12 && bld.program->chip_class > GFX6) {
3454 bytes_size = 12;
3455 op = aco_opcode::buffer_load_dwordx3;
3456 } else {
3457 bytes_size = 16;
3458 op = aco_opcode::buffer_load_dwordx4;
3459 }
3460 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3461 mubuf->operands[0] = Operand(info->resource);
3462 mubuf->operands[1] = vaddr;
3463 mubuf->operands[2] = soffset;
3464 mubuf->offen = (offset.type() == RegType::vgpr);
3465 mubuf->glc = info->glc;
3466 mubuf->dlc = info->glc && bld.program->chip_class >= GFX10;
3467 mubuf->barrier = info->barrier;
3468 mubuf->can_reorder = info->can_reorder;
3469 mubuf->offset = const_offset;
3470 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3471 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3472 mubuf->definitions[0] = Definition(val);
3473 bld.insert(std::move(mubuf));
3474
3475 if (bytes_size < 4)
3476 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3477
3478 return val;
3479 }
3480
3481 static auto emit_mubuf_load = emit_load<mubuf_load_callback, true, true, 4096>;
3482
3483 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
3484 {
3485 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
3486 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
3487
3488 if (addr.type() == RegType::vgpr)
3489 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
3490 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
3491 }
3492
3493 Temp global_load_callback(Builder& bld, const LoadEmitInfo *info,
3494 Temp offset, unsigned bytes_needed,
3495 unsigned align_, unsigned const_offset,
3496 Temp dst_hint)
3497 {
3498 unsigned bytes_size = 0;
3499 bool mubuf = bld.program->chip_class == GFX6;
3500 bool global = bld.program->chip_class >= GFX9;
3501 aco_opcode op;
3502 if (bytes_needed == 1) {
3503 bytes_size = 1;
3504 op = mubuf ? aco_opcode::buffer_load_ubyte : global ? aco_opcode::global_load_ubyte : aco_opcode::flat_load_ubyte;
3505 } else if (bytes_needed == 2) {
3506 bytes_size = 2;
3507 op = mubuf ? aco_opcode::buffer_load_ushort : global ? aco_opcode::global_load_ushort : aco_opcode::flat_load_ushort;
3508 } else if (bytes_needed <= 4) {
3509 bytes_size = 4;
3510 op = mubuf ? aco_opcode::buffer_load_dword : global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
3511 } else if (bytes_needed <= 8) {
3512 bytes_size = 8;
3513 op = mubuf ? aco_opcode::buffer_load_dwordx2 : global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
3514 } else if (bytes_needed <= 12 && !mubuf) {
3515 bytes_size = 12;
3516 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
3517 } else {
3518 bytes_size = 16;
3519 op = mubuf ? aco_opcode::buffer_load_dwordx4 : global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
3520 }
3521 RegClass rc = RegClass::get(RegType::vgpr, align(bytes_size, 4));
3522 Temp val = dst_hint.id() && rc == dst_hint.regClass() ? dst_hint : bld.tmp(rc);
3523 if (mubuf) {
3524 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
3525 mubuf->operands[0] = Operand(get_gfx6_global_rsrc(bld, offset));
3526 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
3527 mubuf->operands[2] = Operand(0u);
3528 mubuf->glc = info->glc;
3529 mubuf->dlc = false;
3530 mubuf->offset = 0;
3531 mubuf->addr64 = offset.type() == RegType::vgpr;
3532 mubuf->disable_wqm = false;
3533 mubuf->barrier = info->barrier;
3534 mubuf->definitions[0] = Definition(val);
3535 bld.insert(std::move(mubuf));
3536 } else {
3537 offset = offset.regClass() == s2 ? bld.copy(bld.def(v2), offset) : offset;
3538
3539 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
3540 flat->operands[0] = Operand(offset);
3541 flat->operands[1] = Operand(s1);
3542 flat->glc = info->glc;
3543 flat->dlc = info->glc && bld.program->chip_class >= GFX10;
3544 flat->barrier = info->barrier;
3545 flat->offset = 0u;
3546 flat->definitions[0] = Definition(val);
3547 bld.insert(std::move(flat));
3548 }
3549
3550 if (bytes_size < 4)
3551 val = bld.pseudo(aco_opcode::p_extract_vector, bld.def(RegClass::get(RegType::vgpr, bytes_size)), val, Operand(0u));
3552
3553 return val;
3554 }
3555
3556 static auto emit_global_load = emit_load<global_load_callback, true, true, 1>;
3557
3558 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3559 Temp address, unsigned base_offset, unsigned align)
3560 {
3561 assert(util_is_power_of_two_nonzero(align));
3562
3563 Builder bld(ctx->program, ctx->block);
3564
3565 unsigned num_components = dst.bytes() / elem_size_bytes;
3566 LoadEmitInfo info = {Operand(as_vgpr(ctx, address)), dst, num_components, elem_size_bytes};
3567 info.align_mul = align;
3568 info.align_offset = 0;
3569 info.barrier = barrier_shared;
3570 info.can_reorder = false;
3571 info.const_offset = base_offset;
3572 emit_lds_load(ctx, bld, &info);
3573
3574 return dst;
3575 }
3576
3577 void split_store_data(isel_context *ctx, RegType dst_type, unsigned count, Temp *dst, unsigned *offsets, Temp src)
3578 {
3579 if (!count)
3580 return;
3581
3582 Builder bld(ctx->program, ctx->block);
3583
3584 ASSERTED bool is_subdword = false;
3585 for (unsigned i = 0; i < count; i++)
3586 is_subdword |= offsets[i] % 4;
3587 is_subdword |= (src.bytes() - offsets[count - 1]) % 4;
3588 assert(!is_subdword || dst_type == RegType::vgpr);
3589
3590 /* count == 1 fast path */
3591 if (count == 1) {
3592 if (dst_type == RegType::sgpr)
3593 dst[0] = bld.as_uniform(src);
3594 else
3595 dst[0] = as_vgpr(ctx, src);
3596 return;
3597 }
3598
3599 for (unsigned i = 0; i < count - 1; i++)
3600 dst[i] = bld.tmp(RegClass::get(dst_type, offsets[i + 1] - offsets[i]));
3601 dst[count - 1] = bld.tmp(RegClass::get(dst_type, src.bytes() - offsets[count - 1]));
3602
3603 if (is_subdword && src.type() == RegType::sgpr) {
3604 src = as_vgpr(ctx, src);
3605 } else {
3606 /* use allocated_vec if possible */
3607 auto it = ctx->allocated_vec.find(src.id());
3608 if (it != ctx->allocated_vec.end()) {
3609 unsigned total_size = 0;
3610 for (unsigned i = 0; it->second[i].bytes() && (i < NIR_MAX_VEC_COMPONENTS); i++)
3611 total_size += it->second[i].bytes();
3612 if (total_size != src.bytes())
3613 goto split;
3614
3615 unsigned elem_size = it->second[0].bytes();
3616
3617 for (unsigned i = 0; i < count; i++) {
3618 if (offsets[i] % elem_size || dst[i].bytes() % elem_size)
3619 goto split;
3620 }
3621
3622 for (unsigned i = 0; i < count; i++) {
3623 unsigned start_idx = offsets[i] / elem_size;
3624 unsigned op_count = dst[i].bytes() / elem_size;
3625 if (op_count == 1) {
3626 if (dst_type == RegType::sgpr)
3627 dst[i] = bld.as_uniform(it->second[start_idx]);
3628 else
3629 dst[i] = as_vgpr(ctx, it->second[start_idx]);
3630 continue;
3631 }
3632
3633 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, op_count, 1)};
3634 for (unsigned j = 0; j < op_count; j++) {
3635 Temp tmp = it->second[start_idx + j];
3636 if (dst_type == RegType::sgpr)
3637 tmp = bld.as_uniform(tmp);
3638 vec->operands[j] = Operand(tmp);
3639 }
3640 vec->definitions[0] = Definition(dst[i]);
3641 bld.insert(std::move(vec));
3642 }
3643 return;
3644 }
3645 }
3646
3647 if (dst_type == RegType::sgpr)
3648 src = bld.as_uniform(src);
3649
3650 split:
3651 /* just split it */
3652 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, count)};
3653 split->operands[0] = Operand(src);
3654 for (unsigned i = 0; i < count; i++)
3655 split->definitions[i] = Definition(dst[i]);
3656 bld.insert(std::move(split));
3657 }
3658
3659 bool scan_write_mask(uint32_t mask, uint32_t todo_mask,
3660 int *start, int *count)
3661 {
3662 unsigned start_elem = ffs(todo_mask) - 1;
3663 bool skip = !(mask & (1 << start_elem));
3664 if (skip)
3665 mask = ~mask & todo_mask;
3666
3667 mask &= todo_mask;
3668
3669 u_bit_scan_consecutive_range(&mask, start, count);
3670
3671 return !skip;
3672 }
3673
3674 void advance_write_mask(uint32_t *todo_mask, int start, int count)
3675 {
3676 *todo_mask &= ~u_bit_consecutive(0, count) << start;
3677 }
3678
3679 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3680 Temp address, unsigned base_offset, unsigned align)
3681 {
3682 assert(util_is_power_of_two_nonzero(align));
3683 assert(util_is_power_of_two_nonzero(elem_size_bytes) && elem_size_bytes <= 8);
3684
3685 Builder bld(ctx->program, ctx->block);
3686 bool large_ds_write = ctx->options->chip_class >= GFX7;
3687 bool usable_write2 = ctx->options->chip_class >= GFX7;
3688
3689 unsigned write_count = 0;
3690 Temp write_datas[32];
3691 unsigned offsets[32];
3692 aco_opcode opcodes[32];
3693
3694 wrmask = widen_mask(wrmask, elem_size_bytes);
3695
3696 uint32_t todo = u_bit_consecutive(0, data.bytes());
3697 while (todo) {
3698 int offset, bytes;
3699 if (!scan_write_mask(wrmask, todo, &offset, &bytes)) {
3700 offsets[write_count] = offset;
3701 opcodes[write_count] = aco_opcode::num_opcodes;
3702 write_count++;
3703 advance_write_mask(&todo, offset, bytes);
3704 continue;
3705 }
3706
3707 bool aligned2 = offset % 2 == 0 && align % 2 == 0;
3708 bool aligned4 = offset % 4 == 0 && align % 4 == 0;
3709 bool aligned8 = offset % 8 == 0 && align % 8 == 0;
3710 bool aligned16 = offset % 16 == 0 && align % 16 == 0;
3711
3712 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3713 aco_opcode op = aco_opcode::num_opcodes;
3714 if (bytes >= 16 && aligned16 && large_ds_write) {
3715 op = aco_opcode::ds_write_b128;
3716 bytes = 16;
3717 } else if (bytes >= 12 && aligned16 && large_ds_write) {
3718 op = aco_opcode::ds_write_b96;
3719 bytes = 12;
3720 } else if (bytes >= 8 && aligned8) {
3721 op = aco_opcode::ds_write_b64;
3722 bytes = 8;
3723 } else if (bytes >= 4 && aligned4) {
3724 op = aco_opcode::ds_write_b32;
3725 bytes = 4;
3726 } else if (bytes >= 2 && aligned2) {
3727 op = aco_opcode::ds_write_b16;
3728 bytes = 2;
3729 } else if (bytes >= 1) {
3730 op = aco_opcode::ds_write_b8;
3731 bytes = 1;
3732 } else {
3733 assert(false);
3734 }
3735
3736 offsets[write_count] = offset;
3737 opcodes[write_count] = op;
3738 write_count++;
3739 advance_write_mask(&todo, offset, bytes);
3740 }
3741
3742 Operand m = load_lds_size_m0(bld);
3743
3744 split_store_data(ctx, RegType::vgpr, write_count, write_datas, offsets, data);
3745
3746 for (unsigned i = 0; i < write_count; i++) {
3747 aco_opcode op = opcodes[i];
3748 if (op == aco_opcode::num_opcodes)
3749 continue;
3750
3751 Temp data = write_datas[i];
3752
3753 unsigned second = write_count;
3754 if (usable_write2 && (op == aco_opcode::ds_write_b32 || op == aco_opcode::ds_write_b64)) {
3755 for (second = i + 1; second < write_count; second++) {
3756 if (opcodes[second] == op && (offsets[second] - offsets[i]) % data.bytes() == 0) {
3757 op = data.bytes() == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3758 opcodes[second] = aco_opcode::num_opcodes;
3759 break;
3760 }
3761 }
3762 }
3763
3764 bool write2 = op == aco_opcode::ds_write2_b32 || op == aco_opcode::ds_write2_b64;
3765 unsigned write2_off = (offsets[second] - offsets[i]) / data.bytes();
3766
3767 unsigned inline_offset = base_offset + offsets[i];
3768 unsigned max_offset = write2 ? (255 - write2_off) * data.bytes() : 65535;
3769 Temp address_offset = address;
3770 if (inline_offset > max_offset) {
3771 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3772 inline_offset = offsets[i];
3773 }
3774 assert(inline_offset <= max_offset); /* offsets[i] shouldn't be large enough for this to happen */
3775
3776 if (write2) {
3777 Temp second_data = write_datas[second];
3778 inline_offset /= data.bytes();
3779 bld.ds(op, address_offset, data, second_data, m, inline_offset, inline_offset + write2_off);
3780 } else {
3781 bld.ds(op, address_offset, data, m, inline_offset);
3782 }
3783 }
3784 }
3785
3786 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3787 {
3788 unsigned align = 16;
3789 if (const_offset)
3790 align = std::min(align, 1u << (ffs(const_offset) - 1));
3791
3792 return align;
3793 }
3794
3795
3796 aco_opcode get_buffer_store_op(bool smem, unsigned bytes)
3797 {
3798 switch (bytes) {
3799 case 1:
3800 assert(!smem);
3801 return aco_opcode::buffer_store_byte;
3802 case 2:
3803 assert(!smem);
3804 return aco_opcode::buffer_store_short;
3805 case 4:
3806 return smem ? aco_opcode::s_buffer_store_dword : aco_opcode::buffer_store_dword;
3807 case 8:
3808 return smem ? aco_opcode::s_buffer_store_dwordx2 : aco_opcode::buffer_store_dwordx2;
3809 case 12:
3810 assert(!smem);
3811 return aco_opcode::buffer_store_dwordx3;
3812 case 16:
3813 return smem ? aco_opcode::s_buffer_store_dwordx4 : aco_opcode::buffer_store_dwordx4;
3814 }
3815 unreachable("Unexpected store size");
3816 return aco_opcode::num_opcodes;
3817 }
3818
3819 void split_buffer_store(isel_context *ctx, nir_intrinsic_instr *instr, bool smem, RegType dst_type,
3820 Temp data, unsigned writemask, int swizzle_element_size,
3821 unsigned *write_count, Temp *write_datas, unsigned *offsets)
3822 {
3823 unsigned write_count_with_skips = 0;
3824 bool skips[16];
3825
3826 /* determine how to split the data */
3827 unsigned todo = u_bit_consecutive(0, data.bytes());
3828 while (todo) {
3829 int offset, bytes;
3830 skips[write_count_with_skips] = !scan_write_mask(writemask, todo, &offset, &bytes);
3831 offsets[write_count_with_skips] = offset;
3832 if (skips[write_count_with_skips]) {
3833 advance_write_mask(&todo, offset, bytes);
3834 write_count_with_skips++;
3835 continue;
3836 }
3837
3838 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3839 * larger than swizzle_element_size */
3840 bytes = MIN2(bytes, swizzle_element_size);
3841 if (bytes % 4)
3842 bytes = bytes > 4 ? bytes & ~0x3 : MIN2(bytes, 2);
3843
3844 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3845 if ((ctx->program->chip_class == GFX6 || smem) && bytes == 12)
3846 bytes = 8;
3847
3848 /* dword or larger stores have to be dword-aligned */
3849 unsigned align_mul = instr ? nir_intrinsic_align_mul(instr) : 4;
3850 unsigned align_offset = instr ? nir_intrinsic_align_mul(instr) : 0;
3851 bool dword_aligned = (align_offset + offset) % 4 == 0 && align_mul % 4 == 0;
3852 if (bytes >= 4 && !dword_aligned)
3853 bytes = MIN2(bytes, 2);
3854
3855 advance_write_mask(&todo, offset, bytes);
3856 write_count_with_skips++;
3857 }
3858
3859 /* actually split data */
3860 split_store_data(ctx, dst_type, write_count_with_skips, write_datas, offsets, data);
3861
3862 /* remove skips */
3863 for (unsigned i = 0; i < write_count_with_skips; i++) {
3864 if (skips[i])
3865 continue;
3866 write_datas[*write_count] = write_datas[i];
3867 offsets[*write_count] = offsets[i];
3868 (*write_count)++;
3869 }
3870 }
3871
3872 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3873 unsigned split_cnt = 0u, Temp dst = Temp())
3874 {
3875 Builder bld(ctx->program, ctx->block);
3876 unsigned dword_size = elem_size_bytes / 4;
3877
3878 if (!dst.id())
3879 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3880
3881 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3882 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3883 instr->definitions[0] = Definition(dst);
3884
3885 for (unsigned i = 0; i < cnt; ++i) {
3886 if (arr[i].id()) {
3887 assert(arr[i].size() == dword_size);
3888 allocated_vec[i] = arr[i];
3889 instr->operands[i] = Operand(arr[i]);
3890 } else {
3891 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3892 allocated_vec[i] = zero;
3893 instr->operands[i] = Operand(zero);
3894 }
3895 }
3896
3897 bld.insert(std::move(instr));
3898
3899 if (split_cnt)
3900 emit_split_vector(ctx, dst, split_cnt);
3901 else
3902 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3903
3904 return dst;
3905 }
3906
3907 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3908 {
3909 if (const_offset >= 4096) {
3910 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3911 const_offset %= 4096u;
3912
3913 if (!voffset.id())
3914 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3915 else if (unlikely(voffset.regClass() == s1))
3916 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3917 else if (likely(voffset.regClass() == v1))
3918 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3919 else
3920 unreachable("Unsupported register class of voffset");
3921 }
3922
3923 return const_offset;
3924 }
3925
3926 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3927 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3928 {
3929 assert(vdata.id());
3930 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3931 assert(vdata.size() >= 1 && vdata.size() <= 4);
3932
3933 Builder bld(ctx->program, ctx->block);
3934 aco_opcode op = get_buffer_store_op(false, vdata.bytes());
3935 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3936
3937 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3938 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3939 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3940 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3941 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3942
3943 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3944 }
3945
3946 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3947 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3948 bool allow_combining = true, bool reorder = true, bool slc = false)
3949 {
3950 Builder bld(ctx->program, ctx->block);
3951 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
3952 assert(write_mask);
3953 write_mask = widen_mask(write_mask, elem_size_bytes);
3954
3955 unsigned write_count = 0;
3956 Temp write_datas[32];
3957 unsigned offsets[32];
3958 split_buffer_store(ctx, NULL, false, RegType::vgpr, src, write_mask,
3959 allow_combining ? 16 : 4, &write_count, write_datas, offsets);
3960
3961 for (unsigned i = 0; i < write_count; i++) {
3962 unsigned const_offset = offsets[i] + base_const_offset;
3963 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, write_datas[i], const_offset, reorder, slc);
3964 }
3965 }
3966
3967 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3968 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3969 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3970 {
3971 assert(elem_size_bytes == 2 || elem_size_bytes == 4 || elem_size_bytes == 8);
3972 assert((num_components * elem_size_bytes) == dst.bytes());
3973 assert(!!stride != allow_combining);
3974
3975 Builder bld(ctx->program, ctx->block);
3976
3977 LoadEmitInfo info = {Operand(voffset), dst, num_components, elem_size_bytes, descriptor};
3978 info.component_stride = allow_combining ? 0 : stride;
3979 info.glc = true;
3980 info.swizzle_component_size = allow_combining ? 0 : 4;
3981 info.align_mul = MIN2(elem_size_bytes, 4);
3982 info.align_offset = 0;
3983 info.soffset = soffset;
3984 info.const_offset = base_const_offset;
3985 emit_mubuf_load(ctx, bld, &info);
3986 }
3987
3988 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)
3989 {
3990 Builder bld(ctx->program, ctx->block);
3991 Temp offset = base_offset.first;
3992 unsigned const_offset = base_offset.second;
3993
3994 if (!nir_src_is_const(*off_src)) {
3995 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3996 Temp with_stride;
3997
3998 /* Calculate indirect offset with stride */
3999 if (likely(indirect_offset_arg.regClass() == v1))
4000 with_stride = bld.v_mul24_imm(bld.def(v1), indirect_offset_arg, stride);
4001 else if (indirect_offset_arg.regClass() == s1)
4002 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
4003 else
4004 unreachable("Unsupported register class of indirect offset");
4005
4006 /* Add to the supplied base offset */
4007 if (offset.id() == 0)
4008 offset = with_stride;
4009 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
4010 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
4011 else if (offset.size() == 1 && with_stride.size() == 1)
4012 offset = bld.vadd32(bld.def(v1), with_stride, offset);
4013 else
4014 unreachable("Unsupported register class of indirect offset");
4015 } else {
4016 unsigned const_offset_arg = nir_src_as_uint(*off_src);
4017 const_offset += const_offset_arg * stride;
4018 }
4019
4020 return std::make_pair(offset, const_offset);
4021 }
4022
4023 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
4024 {
4025 Builder bld(ctx->program, ctx->block);
4026 Temp offset;
4027
4028 if (off1.first.id() && off2.first.id()) {
4029 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
4030 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
4031 else if (off1.first.size() == 1 && off2.first.size() == 1)
4032 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
4033 else
4034 unreachable("Unsupported register class of indirect offset");
4035 } else {
4036 offset = off1.first.id() ? off1.first : off2.first;
4037 }
4038
4039 return std::make_pair(offset, off1.second + off2.second);
4040 }
4041
4042 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
4043 {
4044 Builder bld(ctx->program, ctx->block);
4045 unsigned const_offset = offs.second * multiplier;
4046
4047 if (!offs.first.id())
4048 return std::make_pair(offs.first, const_offset);
4049
4050 Temp offset = unlikely(offs.first.regClass() == s1)
4051 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
4052 : bld.v_mul24_imm(bld.def(v1), offs.first, multiplier);
4053
4054 return std::make_pair(offset, const_offset);
4055 }
4056
4057 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
4058 {
4059 Builder bld(ctx->program, ctx->block);
4060
4061 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4062 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
4063 /* component is in bytes */
4064 const_offset += nir_intrinsic_component(instr) * component_stride;
4065
4066 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4067 nir_src *off_src = nir_get_io_offset_src(instr);
4068 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
4069 }
4070
4071 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
4072 {
4073 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
4074 }
4075
4076 Temp get_tess_rel_patch_id(isel_context *ctx)
4077 {
4078 Builder bld(ctx->program, ctx->block);
4079
4080 switch (ctx->shader->info.stage) {
4081 case MESA_SHADER_TESS_CTRL:
4082 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
4083 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
4084 case MESA_SHADER_TESS_EVAL:
4085 return get_arg(ctx, ctx->args->tes_rel_patch_id);
4086 default:
4087 unreachable("Unsupported stage in get_tess_rel_patch_id");
4088 }
4089 }
4090
4091 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4092 {
4093 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4094 Builder bld(ctx->program, ctx->block);
4095
4096 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
4097 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
4098
4099 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
4100
4101 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4102 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
4103
4104 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4105 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
4106 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
4107
4108 return offset_mul(ctx, offs, 4u);
4109 }
4110
4111 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
4112 {
4113 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4114 Builder bld(ctx->program, ctx->block);
4115
4116 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
4117 uint32_t output_vertex_size = ctx->tcs_num_outputs * 16;
4118 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4119 uint32_t output_patch_stride = pervertex_output_patch_size + ctx->tcs_num_patch_outputs * 16;
4120
4121 std::pair<Temp, unsigned> offs = instr
4122 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
4123 : std::make_pair(Temp(), 0u);
4124
4125 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4126 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
4127
4128 if (per_vertex) {
4129 assert(instr);
4130
4131 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4132 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
4133
4134 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
4135 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
4136 } else {
4137 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
4138 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
4139 }
4140
4141 return offs;
4142 }
4143
4144 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
4145 {
4146 Builder bld(ctx->program, ctx->block);
4147
4148 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
4149 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
4150
4151 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
4152
4153 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4154 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
4155 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
4156
4157 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4158 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
4159
4160 return offs;
4161 }
4162
4163 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
4164 {
4165 Builder bld(ctx->program, ctx->block);
4166
4167 unsigned output_vertex_size = ctx->tcs_num_outputs * 16;
4168 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
4169 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
4170 unsigned attr_stride = ctx->tcs_num_patches;
4171
4172 std::pair<Temp, unsigned> offs = instr
4173 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
4174 : std::make_pair(Temp(), 0u);
4175
4176 if (const_base_offset)
4177 offs.second += const_base_offset * attr_stride;
4178
4179 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
4180 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, 16u);
4181 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
4182
4183 return offs;
4184 }
4185
4186 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
4187 {
4188 assert(per_vertex || ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4189
4190 if (mask == 0)
4191 return false;
4192
4193 unsigned drv_loc = nir_intrinsic_base(instr);
4194 nir_src *off_src = nir_get_io_offset_src(instr);
4195
4196 if (!nir_src_is_const(*off_src)) {
4197 *indirect = true;
4198 return false;
4199 }
4200
4201 *indirect = false;
4202 uint64_t slot = per_vertex
4203 ? ctx->output_drv_loc_to_var_slot[ctx->shader->info.stage][drv_loc / 4]
4204 : (ctx->output_tcs_patch_drv_loc_to_var_slot[drv_loc / 4] - VARYING_SLOT_PATCH0);
4205 return (((uint64_t) 1) << slot) & mask;
4206 }
4207
4208 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
4209 {
4210 unsigned write_mask = nir_intrinsic_write_mask(instr);
4211 unsigned component = nir_intrinsic_component(instr);
4212 unsigned idx = nir_intrinsic_base(instr) + component;
4213
4214 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
4215 if (off_instr->type != nir_instr_type_load_const)
4216 return false;
4217
4218 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4219 idx += nir_src_as_uint(instr->src[1]) * 4u;
4220
4221 if (instr->src[0].ssa->bit_size == 64)
4222 write_mask = widen_mask(write_mask, 2);
4223
4224 RegClass rc = instr->src[0].ssa->bit_size == 16 ? v2b : v1;
4225
4226 for (unsigned i = 0; i < 8; ++i) {
4227 if (write_mask & (1 << i)) {
4228 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
4229 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, rc);
4230 }
4231 idx++;
4232 }
4233
4234 return true;
4235 }
4236
4237 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
4238 {
4239 /* Only TCS per-vertex inputs are supported by this function.
4240 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4241 */
4242 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
4243 return false;
4244
4245 nir_src *off_src = nir_get_io_offset_src(instr);
4246 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
4247 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
4248 bool can_use_temps = nir_src_is_const(*off_src) &&
4249 vertex_index_instr->type == nir_instr_type_intrinsic &&
4250 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
4251
4252 if (!can_use_temps)
4253 return false;
4254
4255 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
4256 Temp *src = &ctx->inputs.temps[idx];
4257 create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u, 0, dst);
4258
4259 return true;
4260 }
4261
4262 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
4263 {
4264 Builder bld(ctx->program, ctx->block);
4265
4266 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
4267 /* 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. */
4268 bool indirect_write;
4269 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
4270 if (temp_only_input && !indirect_write)
4271 return;
4272 }
4273
4274 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
4275 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4276 unsigned write_mask = nir_intrinsic_write_mask(instr);
4277 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
4278
4279 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
4280 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4281 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
4282 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
4283 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
4284 } else {
4285 Temp lds_base;
4286
4287 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4288 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4289 unsigned itemsize = ctx->stage == vertex_geometry_gs
4290 ? ctx->program->info->vs.es_info.esgs_itemsize
4291 : ctx->program->info->tes.es_info.esgs_itemsize;
4292 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
4293 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));
4294 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
4295 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
4296 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
4297 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
4298 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4299 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4300 */
4301 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
4302 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, ctx->tcs_num_inputs * 16u);
4303 } else {
4304 unreachable("Invalid LS or ES stage");
4305 }
4306
4307 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
4308 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4309 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
4310 }
4311 }
4312
4313 bool tcs_output_is_tess_factor(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4314 {
4315 if (per_vertex)
4316 return false;
4317
4318 unsigned off = nir_intrinsic_base(instr) * 4u;
4319 return off == ctx->tcs_tess_lvl_out_loc ||
4320 off == ctx->tcs_tess_lvl_in_loc;
4321
4322 }
4323
4324 bool tcs_output_is_read_by_tes(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4325 {
4326 uint64_t mask = per_vertex
4327 ? ctx->program->info->tcs.tes_inputs_read
4328 : ctx->program->info->tcs.tes_patch_inputs_read;
4329
4330 bool indirect_write = false;
4331 bool output_read_by_tes = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4332 return indirect_write || output_read_by_tes;
4333 }
4334
4335 bool tcs_output_is_read_by_tcs(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4336 {
4337 uint64_t mask = per_vertex
4338 ? ctx->shader->info.outputs_read
4339 : ctx->shader->info.patch_outputs_read;
4340
4341 bool indirect_write = false;
4342 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
4343 return indirect_write || output_read;
4344 }
4345
4346 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4347 {
4348 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4349 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4350
4351 Builder bld(ctx->program, ctx->block);
4352
4353 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
4354 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4355 unsigned write_mask = nir_intrinsic_write_mask(instr);
4356
4357 bool is_tess_factor = tcs_output_is_tess_factor(ctx, instr, per_vertex);
4358 bool write_to_vmem = !is_tess_factor && tcs_output_is_read_by_tes(ctx, instr, per_vertex);
4359 bool write_to_lds = is_tess_factor || tcs_output_is_read_by_tcs(ctx, instr, per_vertex);
4360
4361 if (write_to_vmem) {
4362 std::pair<Temp, unsigned> vmem_offs = per_vertex
4363 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
4364 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
4365
4366 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));
4367 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4368 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);
4369 }
4370
4371 if (write_to_lds) {
4372 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4373 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4374 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
4375 }
4376 }
4377
4378 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
4379 {
4380 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4381 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4382
4383 Builder bld(ctx->program, ctx->block);
4384
4385 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4386 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
4387 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
4388 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
4389
4390 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
4391 }
4392
4393 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
4394 {
4395 if (ctx->stage == vertex_vs ||
4396 ctx->stage == tess_eval_vs ||
4397 ctx->stage == fragment_fs ||
4398 ctx->stage == ngg_vertex_gs ||
4399 ctx->stage == ngg_tess_eval_gs ||
4400 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
4401 bool stored_to_temps = store_output_to_temps(ctx, instr);
4402 if (!stored_to_temps) {
4403 fprintf(stderr, "Unimplemented output offset instruction:\n");
4404 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
4405 fprintf(stderr, "\n");
4406 abort();
4407 }
4408 } else if (ctx->stage == vertex_es ||
4409 ctx->stage == vertex_ls ||
4410 ctx->stage == tess_eval_es ||
4411 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4412 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
4413 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
4414 visit_store_ls_or_es_output(ctx, instr);
4415 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
4416 visit_store_tcs_output(ctx, instr, false);
4417 } else {
4418 unreachable("Shader stage not implemented");
4419 }
4420 }
4421
4422 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
4423 {
4424 visit_load_tcs_output(ctx, instr, false);
4425 }
4426
4427 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
4428 {
4429 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
4430 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
4431
4432 Builder bld(ctx->program, ctx->block);
4433
4434 if (dst.regClass() == v2b) {
4435 if (ctx->program->has_16bank_lds) {
4436 assert(ctx->options->chip_class <= GFX8);
4437 Builder::Result interp_p1 =
4438 bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1),
4439 Operand(2u) /* P0 */, bld.m0(prim_mask), idx, component);
4440 interp_p1 = bld.vintrp(aco_opcode::v_interp_p1lv_f16, bld.def(v2b),
4441 coord1, bld.m0(prim_mask), interp_p1, idx, component);
4442 bld.vintrp(aco_opcode::v_interp_p2_legacy_f16, Definition(dst), coord2,
4443 bld.m0(prim_mask), interp_p1, idx, component);
4444 } else {
4445 aco_opcode interp_p2_op = aco_opcode::v_interp_p2_f16;
4446
4447 if (ctx->options->chip_class == GFX8)
4448 interp_p2_op = aco_opcode::v_interp_p2_legacy_f16;
4449
4450 Builder::Result interp_p1 =
4451 bld.vintrp(aco_opcode::v_interp_p1ll_f16, bld.def(v1),
4452 coord1, bld.m0(prim_mask), idx, component);
4453 bld.vintrp(interp_p2_op, Definition(dst), coord2, bld.m0(prim_mask),
4454 interp_p1, idx, component);
4455 }
4456 } else {
4457 Builder::Result interp_p1 =
4458 bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1,
4459 bld.m0(prim_mask), idx, component);
4460
4461 if (ctx->program->has_16bank_lds)
4462 interp_p1.instr->operands[0].setLateKill(true);
4463
4464 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2,
4465 bld.m0(prim_mask), interp_p1, idx, component);
4466 }
4467 }
4468
4469 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
4470 {
4471 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
4472 for (unsigned i = 0; i < num_components; i++)
4473 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
4474 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
4475 assert(num_components == 4);
4476 Builder bld(ctx->program, ctx->block);
4477 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
4478 }
4479
4480 for (Operand& op : vec->operands)
4481 op = op.isUndefined() ? Operand(0u) : op;
4482
4483 vec->definitions[0] = Definition(dst);
4484 ctx->block->instructions.emplace_back(std::move(vec));
4485 emit_split_vector(ctx, dst, num_components);
4486 return;
4487 }
4488
4489 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
4490 {
4491 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4492 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
4493 unsigned idx = nir_intrinsic_base(instr);
4494 unsigned component = nir_intrinsic_component(instr);
4495 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4496
4497 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
4498 if (offset) {
4499 assert(offset->u32 == 0);
4500 } else {
4501 /* the lower 15bit of the prim_mask contain the offset into LDS
4502 * while the upper bits contain the number of prims */
4503 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
4504 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4505 Builder bld(ctx->program, ctx->block);
4506 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4507 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4508 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4509 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4510 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4511 }
4512
4513 if (instr->dest.ssa.num_components == 1) {
4514 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
4515 } else {
4516 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
4517 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
4518 {
4519 Temp tmp = {ctx->program->allocateId(), v1};
4520 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
4521 vec->operands[i] = Operand(tmp);
4522 }
4523 vec->definitions[0] = Definition(dst);
4524 ctx->block->instructions.emplace_back(std::move(vec));
4525 }
4526 }
4527
4528 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
4529 unsigned offset, unsigned stride, unsigned channels)
4530 {
4531 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
4532 if (vtx_info->chan_byte_size != 4 && channels == 3)
4533 return false;
4534 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
4535 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
4536 }
4537
4538 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
4539 unsigned offset, unsigned stride, unsigned *channels)
4540 {
4541 if (!vtx_info->chan_byte_size) {
4542 *channels = vtx_info->num_channels;
4543 return vtx_info->chan_format;
4544 }
4545
4546 unsigned num_channels = *channels;
4547 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
4548 unsigned new_channels = num_channels + 1;
4549 /* first, assume more loads is worse and try using a larger data format */
4550 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
4551 new_channels++;
4552 /* don't make the attribute potentially out-of-bounds */
4553 if (offset + new_channels * vtx_info->chan_byte_size > stride)
4554 new_channels = 5;
4555 }
4556
4557 if (new_channels == 5) {
4558 /* then try decreasing load size (at the cost of more loads) */
4559 new_channels = *channels;
4560 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
4561 new_channels--;
4562 }
4563
4564 if (new_channels < *channels)
4565 *channels = new_channels;
4566 num_channels = new_channels;
4567 }
4568
4569 switch (vtx_info->chan_format) {
4570 case V_008F0C_BUF_DATA_FORMAT_8:
4571 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
4572 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
4573 case V_008F0C_BUF_DATA_FORMAT_16:
4574 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
4575 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
4576 case V_008F0C_BUF_DATA_FORMAT_32:
4577 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
4578 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
4579 }
4580 unreachable("shouldn't reach here");
4581 return V_008F0C_BUF_DATA_FORMAT_INVALID;
4582 }
4583
4584 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4585 * so we may need to fix it up. */
4586 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4587 {
4588 Builder bld(ctx->program, ctx->block);
4589
4590 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4591 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4592
4593 /* For the integer-like cases, do a natural sign extension.
4594 *
4595 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4596 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4597 * exponent.
4598 */
4599 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4600 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4601
4602 /* Convert back to the right type. */
4603 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4604 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4605 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4606 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4607 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4608 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4609 }
4610
4611 return alpha;
4612 }
4613
4614 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4615 {
4616 Builder bld(ctx->program, ctx->block);
4617 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4618 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4619
4620 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4621 if (off_instr->type != nir_instr_type_load_const) {
4622 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4623 nir_print_instr(off_instr, stderr);
4624 fprintf(stderr, "\n");
4625 }
4626 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4627
4628 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4629
4630 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4631 unsigned component = nir_intrinsic_component(instr);
4632 unsigned bitsize = instr->dest.ssa.bit_size;
4633 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4634 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4635 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4636 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4637
4638 unsigned dfmt = attrib_format & 0xf;
4639 unsigned nfmt = (attrib_format >> 4) & 0x7;
4640 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4641
4642 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4643 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4644 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4645 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4646 if (post_shuffle)
4647 num_channels = MAX2(num_channels, 3);
4648
4649 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4650 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4651
4652 Temp index;
4653 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4654 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4655 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4656 if (divisor) {
4657 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4658 if (divisor != 1) {
4659 Temp divided = bld.tmp(v1);
4660 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4661 index = bld.vadd32(bld.def(v1), start_instance, divided);
4662 } else {
4663 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4664 }
4665 } else {
4666 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4667 }
4668 } else {
4669 index = bld.vadd32(bld.def(v1),
4670 get_arg(ctx, ctx->args->ac.base_vertex),
4671 get_arg(ctx, ctx->args->ac.vertex_id));
4672 }
4673
4674 Temp channels[num_channels];
4675 unsigned channel_start = 0;
4676 bool direct_fetch = false;
4677
4678 /* skip unused channels at the start */
4679 if (vtx_info->chan_byte_size && !post_shuffle) {
4680 channel_start = ffs(mask) - 1;
4681 for (unsigned i = 0; i < channel_start; i++)
4682 channels[i] = Temp(0, s1);
4683 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4684 num_channels = 3 - (ffs(mask) - 1);
4685 }
4686
4687 /* load channels */
4688 while (channel_start < num_channels) {
4689 unsigned fetch_component = num_channels - channel_start;
4690 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4691 bool expanded = false;
4692
4693 /* use MUBUF when possible to avoid possible alignment issues */
4694 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4695 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4696 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4697 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4698 vtx_info->chan_byte_size == 4;
4699 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4700 if (!use_mubuf) {
4701 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_component);
4702 } else {
4703 if (fetch_component == 3 && ctx->options->chip_class == GFX6) {
4704 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4705 fetch_component = 4;
4706 expanded = true;
4707 }
4708 }
4709
4710 unsigned fetch_bytes = fetch_component * bitsize / 8;
4711
4712 Temp fetch_index = index;
4713 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4714 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4715 fetch_offset = fetch_offset % attrib_stride;
4716 }
4717
4718 Operand soffset(0u);
4719 if (fetch_offset >= 4096) {
4720 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4721 fetch_offset %= 4096;
4722 }
4723
4724 aco_opcode opcode;
4725 switch (fetch_bytes) {
4726 case 2:
4727 assert(!use_mubuf && bitsize == 16);
4728 opcode = aco_opcode::tbuffer_load_format_d16_x;
4729 break;
4730 case 4:
4731 if (bitsize == 16) {
4732 assert(!use_mubuf);
4733 opcode = aco_opcode::tbuffer_load_format_d16_xy;
4734 } else {
4735 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4736 }
4737 break;
4738 case 6:
4739 assert(!use_mubuf && bitsize == 16);
4740 opcode = aco_opcode::tbuffer_load_format_d16_xyz;
4741 break;
4742 case 8:
4743 if (bitsize == 16) {
4744 assert(!use_mubuf);
4745 opcode = aco_opcode::tbuffer_load_format_d16_xyzw;
4746 } else {
4747 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4748 }
4749 break;
4750 case 12:
4751 assert(ctx->options->chip_class >= GFX7 ||
4752 (!use_mubuf && ctx->options->chip_class == GFX6));
4753 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4754 break;
4755 case 16:
4756 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4757 break;
4758 default:
4759 unreachable("Unimplemented load_input vector size");
4760 }
4761
4762 Temp fetch_dst;
4763 if (channel_start == 0 && fetch_bytes == dst.bytes() && !post_shuffle &&
4764 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4765 num_channels <= 3)) {
4766 direct_fetch = true;
4767 fetch_dst = dst;
4768 } else {
4769 fetch_dst = bld.tmp(RegClass::get(RegType::vgpr, fetch_bytes));
4770 }
4771
4772 if (use_mubuf) {
4773 Instruction *mubuf = bld.mubuf(opcode,
4774 Definition(fetch_dst), list, fetch_index, soffset,
4775 fetch_offset, false, true).instr;
4776 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4777 } else {
4778 Instruction *mtbuf = bld.mtbuf(opcode,
4779 Definition(fetch_dst), list, fetch_index, soffset,
4780 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4781 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4782 }
4783
4784 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4785
4786 if (fetch_component == 1) {
4787 channels[channel_start] = fetch_dst;
4788 } else {
4789 for (unsigned i = 0; i < MIN2(fetch_component, num_channels - channel_start); i++)
4790 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i,
4791 bitsize == 16 ? v2b : v1);
4792 }
4793
4794 channel_start += fetch_component;
4795 }
4796
4797 if (!direct_fetch) {
4798 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4799 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4800
4801 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4802 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4803 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4804
4805 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4806 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4807 unsigned num_temp = 0;
4808 for (unsigned i = 0; i < dst.size(); i++) {
4809 unsigned idx = i + component;
4810 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4811 Temp channel = channels[swizzle[idx]];
4812 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4813 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4814 vec->operands[i] = Operand(channel);
4815
4816 num_temp++;
4817 elems[i] = channel;
4818 } else if (is_float && idx == 3) {
4819 vec->operands[i] = Operand(0x3f800000u);
4820 } else if (!is_float && idx == 3) {
4821 vec->operands[i] = Operand(1u);
4822 } else {
4823 vec->operands[i] = Operand(0u);
4824 }
4825 }
4826 vec->definitions[0] = Definition(dst);
4827 ctx->block->instructions.emplace_back(std::move(vec));
4828 emit_split_vector(ctx, dst, dst.size());
4829
4830 if (num_temp == dst.size())
4831 ctx->allocated_vec.emplace(dst.id(), elems);
4832 }
4833 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4834 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4835 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4836 if (off_instr->type != nir_instr_type_load_const ||
4837 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4838 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4839 nir_print_instr(off_instr, stderr);
4840 fprintf(stderr, "\n");
4841 }
4842
4843 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4844 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4845 if (offset) {
4846 assert(offset->u32 == 0);
4847 } else {
4848 /* the lower 15bit of the prim_mask contain the offset into LDS
4849 * while the upper bits contain the number of prims */
4850 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4851 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4852 Builder bld(ctx->program, ctx->block);
4853 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4854 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4855 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4856 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4857 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4858 }
4859
4860 unsigned idx = nir_intrinsic_base(instr);
4861 unsigned component = nir_intrinsic_component(instr);
4862 unsigned vertex_id = 2; /* P0 */
4863
4864 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4865 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4866 switch (src0->u32) {
4867 case 0:
4868 vertex_id = 2; /* P0 */
4869 break;
4870 case 1:
4871 vertex_id = 0; /* P10 */
4872 break;
4873 case 2:
4874 vertex_id = 1; /* P20 */
4875 break;
4876 default:
4877 unreachable("invalid vertex index");
4878 }
4879 }
4880
4881 if (dst.size() == 1) {
4882 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4883 } else {
4884 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4885 for (unsigned i = 0; i < dst.size(); i++)
4886 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4887 vec->definitions[0] = Definition(dst);
4888 bld.insert(std::move(vec));
4889 }
4890
4891 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4892 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4893 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4894 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4895 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4896
4897 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4898 } else {
4899 unreachable("Shader stage not implemented");
4900 }
4901 }
4902
4903 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4904 {
4905 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4906
4907 Builder bld(ctx->program, ctx->block);
4908 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4909 Temp vertex_offset;
4910
4911 if (!nir_src_is_const(*vertex_src)) {
4912 /* better code could be created, but this case probably doesn't happen
4913 * much in practice */
4914 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4915 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4916 Temp elem;
4917
4918 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4919 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4920 if (i % 2u)
4921 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4922 } else {
4923 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4924 }
4925
4926 if (vertex_offset.id()) {
4927 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4928 Operand(i), indirect_vertex);
4929 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4930 } else {
4931 vertex_offset = elem;
4932 }
4933 }
4934
4935 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4936 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4937 } else {
4938 unsigned vertex = nir_src_as_uint(*vertex_src);
4939 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4940 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4941 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4942 Operand((vertex % 2u) * 16u), Operand(16u));
4943 else
4944 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4945 }
4946
4947 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4948 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4949 return offset_mul(ctx, offs, 4u);
4950 }
4951
4952 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4953 {
4954 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4955
4956 Builder bld(ctx->program, ctx->block);
4957 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4958 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4959
4960 if (ctx->stage == geometry_gs) {
4961 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4962 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4963 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);
4964 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4965 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4966 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4967 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4968 } else {
4969 unreachable("Unsupported GS stage.");
4970 }
4971 }
4972
4973 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4974 {
4975 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4976
4977 Builder bld(ctx->program, ctx->block);
4978 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4979
4980 if (load_input_from_temps(ctx, instr, dst))
4981 return;
4982
4983 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4984 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4985 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4986
4987 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4988 }
4989
4990 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4991 {
4992 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4993
4994 Builder bld(ctx->program, ctx->block);
4995
4996 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4997 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4998 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4999
5000 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
5001 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
5002
5003 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
5004 }
5005
5006 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
5007 {
5008 switch (ctx->shader->info.stage) {
5009 case MESA_SHADER_GEOMETRY:
5010 visit_load_gs_per_vertex_input(ctx, instr);
5011 break;
5012 case MESA_SHADER_TESS_CTRL:
5013 visit_load_tcs_per_vertex_input(ctx, instr);
5014 break;
5015 case MESA_SHADER_TESS_EVAL:
5016 visit_load_tes_per_vertex_input(ctx, instr);
5017 break;
5018 default:
5019 unreachable("Unimplemented shader stage");
5020 }
5021 }
5022
5023 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5024 {
5025 visit_load_tcs_output(ctx, instr, true);
5026 }
5027
5028 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
5029 {
5030 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
5031 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
5032
5033 visit_store_tcs_output(ctx, instr, true);
5034 }
5035
5036 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
5037 {
5038 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
5039
5040 Builder bld(ctx->program, ctx->block);
5041 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5042
5043 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
5044 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
5045 Operand tes_w(0u);
5046
5047 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
5048 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
5049 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
5050 tes_w = Operand(tmp);
5051 }
5052
5053 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
5054 emit_split_vector(ctx, tess_coord, 3);
5055 }
5056
5057 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
5058 {
5059 if (ctx->program->info->need_indirect_descriptor_sets) {
5060 Builder bld(ctx->program, ctx->block);
5061 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
5062 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
5063 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
5064 }
5065
5066 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
5067 }
5068
5069
5070 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
5071 {
5072 Builder bld(ctx->program, ctx->block);
5073 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
5074 if (!nir_dest_is_divergent(instr->dest))
5075 index = bld.as_uniform(index);
5076 unsigned desc_set = nir_intrinsic_desc_set(instr);
5077 unsigned binding = nir_intrinsic_binding(instr);
5078
5079 Temp desc_ptr;
5080 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
5081 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
5082 unsigned offset = layout->binding[binding].offset;
5083 unsigned stride;
5084 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
5085 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
5086 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
5087 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
5088 offset = pipeline_layout->push_constant_size + 16 * idx;
5089 stride = 16;
5090 } else {
5091 desc_ptr = load_desc_ptr(ctx, desc_set);
5092 stride = layout->binding[binding].size;
5093 }
5094
5095 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
5096 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
5097 if (stride != 1) {
5098 if (nir_const_index) {
5099 const_index = const_index * stride;
5100 } else if (index.type() == RegType::vgpr) {
5101 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
5102 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
5103 } else {
5104 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
5105 }
5106 }
5107 if (offset) {
5108 if (nir_const_index) {
5109 const_index = const_index + offset;
5110 } else if (index.type() == RegType::vgpr) {
5111 index = bld.vadd32(bld.def(v1), Operand(offset), index);
5112 } else {
5113 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
5114 }
5115 }
5116
5117 if (nir_const_index && const_index == 0) {
5118 index = desc_ptr;
5119 } else if (index.type() == RegType::vgpr) {
5120 index = bld.vadd32(bld.def(v1),
5121 nir_const_index ? Operand(const_index) : Operand(index),
5122 Operand(desc_ptr));
5123 } else {
5124 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5125 nir_const_index ? Operand(const_index) : Operand(index),
5126 Operand(desc_ptr));
5127 }
5128
5129 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
5130 }
5131
5132 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
5133 Temp dst, Temp rsrc, Temp offset, unsigned align_mul, unsigned align_offset,
5134 bool glc=false, bool readonly=true)
5135 {
5136 Builder bld(ctx->program, ctx->block);
5137
5138 bool use_smem = dst.type() != RegType::vgpr && ((ctx->options->chip_class >= GFX8 && component_size >= 4) || readonly);
5139 if (use_smem)
5140 offset = bld.as_uniform(offset);
5141
5142 LoadEmitInfo info = {Operand(offset), dst, num_components, component_size, rsrc};
5143 info.glc = glc;
5144 info.barrier = readonly ? barrier_none : barrier_buffer;
5145 info.can_reorder = readonly;
5146 info.align_mul = align_mul;
5147 info.align_offset = align_offset;
5148 if (use_smem)
5149 emit_smem_load(ctx, bld, &info);
5150 else
5151 emit_mubuf_load(ctx, bld, &info);
5152 }
5153
5154 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
5155 {
5156 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5157 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
5158
5159 Builder bld(ctx->program, ctx->block);
5160
5161 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
5162 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
5163 unsigned binding = nir_intrinsic_binding(idx_instr);
5164 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
5165
5166 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
5167 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5168 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5169 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5170 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5171 if (ctx->options->chip_class >= GFX10) {
5172 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5173 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5174 S_008F0C_RESOURCE_LEVEL(1);
5175 } else {
5176 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5177 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5178 }
5179 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
5180 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
5181 Operand(0xFFFFFFFFu),
5182 Operand(desc_type));
5183 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5184 rsrc, upper_dwords);
5185 } else {
5186 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
5187 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5188 }
5189 unsigned size = instr->dest.ssa.bit_size / 8;
5190 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
5191 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
5192 }
5193
5194 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5195 {
5196 Builder bld(ctx->program, ctx->block);
5197 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5198 unsigned offset = nir_intrinsic_base(instr);
5199 unsigned count = instr->dest.ssa.num_components;
5200 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
5201
5202 if (index_cv && instr->dest.ssa.bit_size == 32) {
5203 unsigned start = (offset + index_cv->u32) / 4u;
5204 start -= ctx->args->ac.base_inline_push_consts;
5205 if (start + count <= ctx->args->ac.num_inline_push_consts) {
5206 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
5207 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5208 for (unsigned i = 0; i < count; ++i) {
5209 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
5210 vec->operands[i] = Operand{elems[i]};
5211 }
5212 vec->definitions[0] = Definition(dst);
5213 ctx->block->instructions.emplace_back(std::move(vec));
5214 ctx->allocated_vec.emplace(dst.id(), elems);
5215 return;
5216 }
5217 }
5218
5219 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
5220 if (offset != 0) // TODO check if index != 0 as well
5221 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
5222 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
5223 Temp vec = dst;
5224 bool trim = false;
5225 bool aligned = true;
5226
5227 if (instr->dest.ssa.bit_size == 8) {
5228 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5229 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
5230 if (!aligned)
5231 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
5232 } else if (instr->dest.ssa.bit_size == 16) {
5233 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
5234 if (!aligned)
5235 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
5236 }
5237
5238 aco_opcode op;
5239
5240 switch (vec.size()) {
5241 case 1:
5242 op = aco_opcode::s_load_dword;
5243 break;
5244 case 2:
5245 op = aco_opcode::s_load_dwordx2;
5246 break;
5247 case 3:
5248 vec = bld.tmp(s4);
5249 trim = true;
5250 case 4:
5251 op = aco_opcode::s_load_dwordx4;
5252 break;
5253 case 6:
5254 vec = bld.tmp(s8);
5255 trim = true;
5256 case 8:
5257 op = aco_opcode::s_load_dwordx8;
5258 break;
5259 default:
5260 unreachable("unimplemented or forbidden load_push_constant.");
5261 }
5262
5263 bld.smem(op, Definition(vec), ptr, index);
5264
5265 if (!aligned) {
5266 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
5267 byte_align_scalar(ctx, vec, byte_offset, dst);
5268 return;
5269 }
5270
5271 if (trim) {
5272 emit_split_vector(ctx, vec, 4);
5273 RegClass rc = dst.size() == 3 ? s1 : s2;
5274 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5275 emit_extract_vector(ctx, vec, 0, rc),
5276 emit_extract_vector(ctx, vec, 1, rc),
5277 emit_extract_vector(ctx, vec, 2, rc));
5278
5279 }
5280 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5281 }
5282
5283 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
5284 {
5285 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5286
5287 Builder bld(ctx->program, ctx->block);
5288
5289 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5290 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5291 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5292 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
5293 if (ctx->options->chip_class >= GFX10) {
5294 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5295 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
5296 S_008F0C_RESOURCE_LEVEL(1);
5297 } else {
5298 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5299 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5300 }
5301
5302 unsigned base = nir_intrinsic_base(instr);
5303 unsigned range = nir_intrinsic_range(instr);
5304
5305 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
5306 if (base && offset.type() == RegType::sgpr)
5307 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
5308 else if (base && offset.type() == RegType::vgpr)
5309 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
5310
5311 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5312 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
5313 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
5314 Operand(desc_type));
5315 unsigned size = instr->dest.ssa.bit_size / 8;
5316 // TODO: get alignment information for subdword constants
5317 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, size, 0);
5318 }
5319
5320 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
5321 {
5322 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5323 ctx->cf_info.exec_potentially_empty_discard = true;
5324
5325 ctx->program->needs_exact = true;
5326
5327 // TODO: optimize uniform conditions
5328 Builder bld(ctx->program, ctx->block);
5329 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
5330 assert(src.regClass() == bld.lm);
5331 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
5332 bld.pseudo(aco_opcode::p_discard_if, src);
5333 ctx->block->kind |= block_kind_uses_discard_if;
5334 return;
5335 }
5336
5337 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
5338 {
5339 Builder bld(ctx->program, ctx->block);
5340
5341 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
5342 ctx->cf_info.exec_potentially_empty_discard = true;
5343
5344 bool divergent = ctx->cf_info.parent_if.is_divergent ||
5345 ctx->cf_info.parent_loop.has_divergent_continue;
5346
5347 if (ctx->block->loop_nest_depth &&
5348 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
5349 /* we handle discards the same way as jump instructions */
5350 append_logical_end(ctx->block);
5351
5352 /* in loops, discard behaves like break */
5353 Block *linear_target = ctx->cf_info.parent_loop.exit;
5354 ctx->block->kind |= block_kind_discard;
5355
5356 if (!divergent) {
5357 /* uniform discard - loop ends here */
5358 assert(nir_instr_is_last(&instr->instr));
5359 ctx->block->kind |= block_kind_uniform;
5360 ctx->cf_info.has_branch = true;
5361 bld.branch(aco_opcode::p_branch);
5362 add_linear_edge(ctx->block->index, linear_target);
5363 return;
5364 }
5365
5366 /* we add a break right behind the discard() instructions */
5367 ctx->block->kind |= block_kind_break;
5368 unsigned idx = ctx->block->index;
5369
5370 ctx->cf_info.parent_loop.has_divergent_branch = true;
5371 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
5372
5373 /* remove critical edges from linear CFG */
5374 bld.branch(aco_opcode::p_branch);
5375 Block* break_block = ctx->program->create_and_insert_block();
5376 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5377 break_block->kind |= block_kind_uniform;
5378 add_linear_edge(idx, break_block);
5379 add_linear_edge(break_block->index, linear_target);
5380 bld.reset(break_block);
5381 bld.branch(aco_opcode::p_branch);
5382
5383 Block* continue_block = ctx->program->create_and_insert_block();
5384 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
5385 add_linear_edge(idx, continue_block);
5386 append_logical_start(continue_block);
5387 ctx->block = continue_block;
5388
5389 return;
5390 }
5391
5392 /* it can currently happen that NIR doesn't remove the unreachable code */
5393 if (!nir_instr_is_last(&instr->instr)) {
5394 ctx->program->needs_exact = true;
5395 /* save exec somewhere temporarily so that it doesn't get
5396 * overwritten before the discard from outer exec masks */
5397 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5398 bld.pseudo(aco_opcode::p_discard_if, cond);
5399 ctx->block->kind |= block_kind_uses_discard_if;
5400 return;
5401 }
5402
5403 /* This condition is incorrect for uniformly branched discards in a loop
5404 * predicated by a divergent condition, but the above code catches that case
5405 * and the discard would end up turning into a discard_if.
5406 * For example:
5407 * if (divergent) {
5408 * while (...) {
5409 * if (uniform) {
5410 * discard;
5411 * }
5412 * }
5413 * }
5414 */
5415 if (!ctx->cf_info.parent_if.is_divergent) {
5416 /* program just ends here */
5417 ctx->block->kind |= block_kind_uniform;
5418 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5419 0 /* enabled mask */, 9 /* dest */,
5420 false /* compressed */, true/* done */, true /* valid mask */);
5421 bld.sopp(aco_opcode::s_endpgm);
5422 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5423 } else {
5424 ctx->block->kind |= block_kind_discard;
5425 /* branch and linear edge is added by visit_if() */
5426 }
5427 }
5428
5429 enum aco_descriptor_type {
5430 ACO_DESC_IMAGE,
5431 ACO_DESC_FMASK,
5432 ACO_DESC_SAMPLER,
5433 ACO_DESC_BUFFER,
5434 ACO_DESC_PLANE_0,
5435 ACO_DESC_PLANE_1,
5436 ACO_DESC_PLANE_2,
5437 };
5438
5439 static bool
5440 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5441 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5442 return false;
5443 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5444 return dim == ac_image_cube ||
5445 dim == ac_image_1darray ||
5446 dim == ac_image_2darray ||
5447 dim == ac_image_2darraymsaa;
5448 }
5449
5450 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5451 enum aco_descriptor_type desc_type,
5452 const nir_tex_instr *tex_instr, bool image, bool write)
5453 {
5454 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5455 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5456 if (it != ctx->tex_desc.end())
5457 return it->second;
5458 */
5459 Temp index = Temp();
5460 bool index_set = false;
5461 unsigned constant_index = 0;
5462 unsigned descriptor_set;
5463 unsigned base_index;
5464 Builder bld(ctx->program, ctx->block);
5465
5466 if (!deref_instr) {
5467 assert(tex_instr && !image);
5468 descriptor_set = 0;
5469 base_index = tex_instr->sampler_index;
5470 } else {
5471 while(deref_instr->deref_type != nir_deref_type_var) {
5472 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5473 if (!array_size)
5474 array_size = 1;
5475
5476 assert(deref_instr->deref_type == nir_deref_type_array);
5477 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5478 if (const_value) {
5479 constant_index += array_size * const_value->u32;
5480 } else {
5481 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5482 if (indirect.type() == RegType::vgpr)
5483 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5484
5485 if (array_size != 1)
5486 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5487
5488 if (!index_set) {
5489 index = indirect;
5490 index_set = true;
5491 } else {
5492 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5493 }
5494 }
5495
5496 deref_instr = nir_src_as_deref(deref_instr->parent);
5497 }
5498 descriptor_set = deref_instr->var->data.descriptor_set;
5499 base_index = deref_instr->var->data.binding;
5500 }
5501
5502 Temp list = load_desc_ptr(ctx, descriptor_set);
5503 list = convert_pointer_to_64_bit(ctx, list);
5504
5505 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5506 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5507 unsigned offset = binding->offset;
5508 unsigned stride = binding->size;
5509 aco_opcode opcode;
5510 RegClass type;
5511
5512 assert(base_index < layout->binding_count);
5513
5514 switch (desc_type) {
5515 case ACO_DESC_IMAGE:
5516 type = s8;
5517 opcode = aco_opcode::s_load_dwordx8;
5518 break;
5519 case ACO_DESC_FMASK:
5520 type = s8;
5521 opcode = aco_opcode::s_load_dwordx8;
5522 offset += 32;
5523 break;
5524 case ACO_DESC_SAMPLER:
5525 type = s4;
5526 opcode = aco_opcode::s_load_dwordx4;
5527 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5528 offset += radv_combined_image_descriptor_sampler_offset(binding);
5529 break;
5530 case ACO_DESC_BUFFER:
5531 type = s4;
5532 opcode = aco_opcode::s_load_dwordx4;
5533 break;
5534 case ACO_DESC_PLANE_0:
5535 case ACO_DESC_PLANE_1:
5536 type = s8;
5537 opcode = aco_opcode::s_load_dwordx8;
5538 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5539 break;
5540 case ACO_DESC_PLANE_2:
5541 type = s4;
5542 opcode = aco_opcode::s_load_dwordx4;
5543 offset += 64;
5544 break;
5545 default:
5546 unreachable("invalid desc_type\n");
5547 }
5548
5549 offset += constant_index * stride;
5550
5551 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5552 (!index_set || binding->immutable_samplers_equal)) {
5553 if (binding->immutable_samplers_equal)
5554 constant_index = 0;
5555
5556 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5557 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5558 Operand(samplers[constant_index * 4 + 0]),
5559 Operand(samplers[constant_index * 4 + 1]),
5560 Operand(samplers[constant_index * 4 + 2]),
5561 Operand(samplers[constant_index * 4 + 3]));
5562 }
5563
5564 Operand off;
5565 if (!index_set) {
5566 off = bld.copy(bld.def(s1), Operand(offset));
5567 } else {
5568 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5569 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5570 }
5571
5572 Temp res = bld.smem(opcode, bld.def(type), list, off);
5573
5574 if (desc_type == ACO_DESC_PLANE_2) {
5575 Temp components[8];
5576 for (unsigned i = 0; i < 8; i++)
5577 components[i] = bld.tmp(s1);
5578 bld.pseudo(aco_opcode::p_split_vector,
5579 Definition(components[0]),
5580 Definition(components[1]),
5581 Definition(components[2]),
5582 Definition(components[3]),
5583 res);
5584
5585 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5586 bld.pseudo(aco_opcode::p_split_vector,
5587 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5588 Definition(components[4]),
5589 Definition(components[5]),
5590 Definition(components[6]),
5591 Definition(components[7]),
5592 desc2);
5593
5594 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5595 components[0], components[1], components[2], components[3],
5596 components[4], components[5], components[6], components[7]);
5597 }
5598
5599 return res;
5600 }
5601
5602 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5603 {
5604 switch (dim) {
5605 case GLSL_SAMPLER_DIM_BUF:
5606 return 1;
5607 case GLSL_SAMPLER_DIM_1D:
5608 return array ? 2 : 1;
5609 case GLSL_SAMPLER_DIM_2D:
5610 return array ? 3 : 2;
5611 case GLSL_SAMPLER_DIM_MS:
5612 return array ? 4 : 3;
5613 case GLSL_SAMPLER_DIM_3D:
5614 case GLSL_SAMPLER_DIM_CUBE:
5615 return 3;
5616 case GLSL_SAMPLER_DIM_RECT:
5617 case GLSL_SAMPLER_DIM_SUBPASS:
5618 return 2;
5619 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5620 return 3;
5621 default:
5622 break;
5623 }
5624 return 0;
5625 }
5626
5627
5628 /* Adjust the sample index according to FMASK.
5629 *
5630 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5631 * which is the identity mapping. Each nibble says which physical sample
5632 * should be fetched to get that sample.
5633 *
5634 * For example, 0x11111100 means there are only 2 samples stored and
5635 * the second sample covers 3/4 of the pixel. When reading samples 0
5636 * and 1, return physical sample 0 (determined by the first two 0s
5637 * in FMASK), otherwise return physical sample 1.
5638 *
5639 * The sample index should be adjusted as follows:
5640 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5641 */
5642 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5643 {
5644 Builder bld(ctx->program, ctx->block);
5645 Temp fmask = bld.tmp(v1);
5646 unsigned dim = ctx->options->chip_class >= GFX10
5647 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5648 : 0;
5649
5650 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5651 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5652 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5653 load->operands[0] = Operand(fmask_desc_ptr);
5654 load->operands[1] = Operand(s4); /* no sampler */
5655 load->operands[2] = Operand(coord);
5656 load->definitions[0] = Definition(fmask);
5657 load->glc = false;
5658 load->dlc = false;
5659 load->dmask = 0x1;
5660 load->unrm = true;
5661 load->da = da;
5662 load->dim = dim;
5663 load->can_reorder = true; /* fmask images shouldn't be modified */
5664 ctx->block->instructions.emplace_back(std::move(load));
5665
5666 Operand sample_index4;
5667 if (sample_index.isConstant()) {
5668 if (sample_index.constantValue() < 16) {
5669 sample_index4 = Operand(sample_index.constantValue() << 2);
5670 } else {
5671 sample_index4 = Operand(0u);
5672 }
5673 } else if (sample_index.regClass() == s1) {
5674 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5675 } else {
5676 assert(sample_index.regClass() == v1);
5677 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5678 }
5679
5680 Temp final_sample;
5681 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5682 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5683 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5684 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5685 else
5686 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5687
5688 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5689 * resource descriptor is 0 (invalid),
5690 */
5691 Temp compare = bld.tmp(bld.lm);
5692 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5693 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5694
5695 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5696
5697 /* Replace the MSAA sample index. */
5698 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5699 }
5700
5701 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5702 {
5703
5704 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5705 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5706 bool is_array = glsl_sampler_type_is_array(type);
5707 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5708 assert(!add_frag_pos && "Input attachments should be lowered.");
5709 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5710 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5711 int count = image_type_to_components_count(dim, is_array);
5712 std::vector<Temp> coords(count);
5713 Builder bld(ctx->program, ctx->block);
5714
5715 if (is_ms) {
5716 count--;
5717 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5718 /* get sample index */
5719 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5720 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5721 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5722 std::vector<Temp> fmask_load_address;
5723 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5724 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5725
5726 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5727 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5728 } else {
5729 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5730 }
5731 }
5732
5733 if (gfx9_1d) {
5734 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5735 coords.resize(coords.size() + 1);
5736 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5737 if (is_array)
5738 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5739 } else {
5740 for (int i = 0; i < count; i++)
5741 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5742 }
5743
5744 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5745 instr->intrinsic == nir_intrinsic_image_deref_store) {
5746 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5747 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5748
5749 if (!level_zero)
5750 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5751 }
5752
5753 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5754 for (unsigned i = 0; i < coords.size(); i++)
5755 vec->operands[i] = Operand(coords[i]);
5756 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5757 vec->definitions[0] = Definition(res);
5758 ctx->block->instructions.emplace_back(std::move(vec));
5759 return res;
5760 }
5761
5762
5763 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5764 {
5765 Builder bld(ctx->program, ctx->block);
5766 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5767 const struct glsl_type *type = glsl_without_array(var->type);
5768 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5769 bool is_array = glsl_sampler_type_is_array(type);
5770 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5771
5772 if (dim == GLSL_SAMPLER_DIM_BUF) {
5773 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5774 unsigned num_channels = util_last_bit(mask);
5775 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5776 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5777
5778 aco_opcode opcode;
5779 switch (num_channels) {
5780 case 1:
5781 opcode = aco_opcode::buffer_load_format_x;
5782 break;
5783 case 2:
5784 opcode = aco_opcode::buffer_load_format_xy;
5785 break;
5786 case 3:
5787 opcode = aco_opcode::buffer_load_format_xyz;
5788 break;
5789 case 4:
5790 opcode = aco_opcode::buffer_load_format_xyzw;
5791 break;
5792 default:
5793 unreachable(">4 channel buffer image load");
5794 }
5795 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5796 load->operands[0] = Operand(rsrc);
5797 load->operands[1] = Operand(vindex);
5798 load->operands[2] = Operand((uint32_t) 0);
5799 Temp tmp;
5800 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5801 tmp = dst;
5802 else
5803 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5804 load->definitions[0] = Definition(tmp);
5805 load->idxen = true;
5806 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5807 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5808 load->barrier = barrier_image;
5809 ctx->block->instructions.emplace_back(std::move(load));
5810
5811 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5812 return;
5813 }
5814
5815 Temp coords = get_image_coords(ctx, instr, type);
5816 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5817
5818 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5819 unsigned num_components = util_bitcount(dmask);
5820 Temp tmp;
5821 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5822 tmp = dst;
5823 else
5824 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5825
5826 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5827 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5828
5829 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5830 load->operands[0] = Operand(resource);
5831 load->operands[1] = Operand(s4); /* no sampler */
5832 load->operands[2] = Operand(coords);
5833 load->definitions[0] = Definition(tmp);
5834 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5835 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5836 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5837 load->dmask = dmask;
5838 load->unrm = true;
5839 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5840 load->barrier = barrier_image;
5841 ctx->block->instructions.emplace_back(std::move(load));
5842
5843 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5844 return;
5845 }
5846
5847 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5848 {
5849 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5850 const struct glsl_type *type = glsl_without_array(var->type);
5851 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5852 bool is_array = glsl_sampler_type_is_array(type);
5853 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5854
5855 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5856
5857 if (dim == GLSL_SAMPLER_DIM_BUF) {
5858 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5859 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5860 aco_opcode opcode;
5861 switch (data.size()) {
5862 case 1:
5863 opcode = aco_opcode::buffer_store_format_x;
5864 break;
5865 case 2:
5866 opcode = aco_opcode::buffer_store_format_xy;
5867 break;
5868 case 3:
5869 opcode = aco_opcode::buffer_store_format_xyz;
5870 break;
5871 case 4:
5872 opcode = aco_opcode::buffer_store_format_xyzw;
5873 break;
5874 default:
5875 unreachable(">4 channel buffer image store");
5876 }
5877 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5878 store->operands[0] = Operand(rsrc);
5879 store->operands[1] = Operand(vindex);
5880 store->operands[2] = Operand((uint32_t) 0);
5881 store->operands[3] = Operand(data);
5882 store->idxen = true;
5883 store->glc = glc;
5884 store->dlc = false;
5885 store->disable_wqm = true;
5886 store->barrier = barrier_image;
5887 ctx->program->needs_exact = true;
5888 ctx->block->instructions.emplace_back(std::move(store));
5889 return;
5890 }
5891
5892 assert(data.type() == RegType::vgpr);
5893 Temp coords = get_image_coords(ctx, instr, type);
5894 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5895
5896 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5897 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5898
5899 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5900 store->operands[0] = Operand(resource);
5901 store->operands[1] = Operand(data);
5902 store->operands[2] = Operand(coords);
5903 store->glc = glc;
5904 store->dlc = false;
5905 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5906 store->dmask = (1 << data.size()) - 1;
5907 store->unrm = true;
5908 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5909 store->disable_wqm = true;
5910 store->barrier = barrier_image;
5911 ctx->program->needs_exact = true;
5912 ctx->block->instructions.emplace_back(std::move(store));
5913 return;
5914 }
5915
5916 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5917 {
5918 /* return the previous value if dest is ever used */
5919 bool return_previous = false;
5920 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5921 return_previous = true;
5922 break;
5923 }
5924 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5925 return_previous = true;
5926 break;
5927 }
5928
5929 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5930 const struct glsl_type *type = glsl_without_array(var->type);
5931 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5932 bool is_array = glsl_sampler_type_is_array(type);
5933 Builder bld(ctx->program, ctx->block);
5934
5935 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5936 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5937
5938 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5939 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5940
5941 aco_opcode buf_op, image_op;
5942 switch (instr->intrinsic) {
5943 case nir_intrinsic_image_deref_atomic_add:
5944 buf_op = aco_opcode::buffer_atomic_add;
5945 image_op = aco_opcode::image_atomic_add;
5946 break;
5947 case nir_intrinsic_image_deref_atomic_umin:
5948 buf_op = aco_opcode::buffer_atomic_umin;
5949 image_op = aco_opcode::image_atomic_umin;
5950 break;
5951 case nir_intrinsic_image_deref_atomic_imin:
5952 buf_op = aco_opcode::buffer_atomic_smin;
5953 image_op = aco_opcode::image_atomic_smin;
5954 break;
5955 case nir_intrinsic_image_deref_atomic_umax:
5956 buf_op = aco_opcode::buffer_atomic_umax;
5957 image_op = aco_opcode::image_atomic_umax;
5958 break;
5959 case nir_intrinsic_image_deref_atomic_imax:
5960 buf_op = aco_opcode::buffer_atomic_smax;
5961 image_op = aco_opcode::image_atomic_smax;
5962 break;
5963 case nir_intrinsic_image_deref_atomic_and:
5964 buf_op = aco_opcode::buffer_atomic_and;
5965 image_op = aco_opcode::image_atomic_and;
5966 break;
5967 case nir_intrinsic_image_deref_atomic_or:
5968 buf_op = aco_opcode::buffer_atomic_or;
5969 image_op = aco_opcode::image_atomic_or;
5970 break;
5971 case nir_intrinsic_image_deref_atomic_xor:
5972 buf_op = aco_opcode::buffer_atomic_xor;
5973 image_op = aco_opcode::image_atomic_xor;
5974 break;
5975 case nir_intrinsic_image_deref_atomic_exchange:
5976 buf_op = aco_opcode::buffer_atomic_swap;
5977 image_op = aco_opcode::image_atomic_swap;
5978 break;
5979 case nir_intrinsic_image_deref_atomic_comp_swap:
5980 buf_op = aco_opcode::buffer_atomic_cmpswap;
5981 image_op = aco_opcode::image_atomic_cmpswap;
5982 break;
5983 default:
5984 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5985 }
5986
5987 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5988
5989 if (dim == GLSL_SAMPLER_DIM_BUF) {
5990 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5991 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5992 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5993 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5994 mubuf->operands[0] = Operand(resource);
5995 mubuf->operands[1] = Operand(vindex);
5996 mubuf->operands[2] = Operand((uint32_t)0);
5997 mubuf->operands[3] = Operand(data);
5998 if (return_previous)
5999 mubuf->definitions[0] = Definition(dst);
6000 mubuf->offset = 0;
6001 mubuf->idxen = true;
6002 mubuf->glc = return_previous;
6003 mubuf->dlc = false; /* Not needed for atomics */
6004 mubuf->disable_wqm = true;
6005 mubuf->barrier = barrier_image;
6006 ctx->program->needs_exact = true;
6007 ctx->block->instructions.emplace_back(std::move(mubuf));
6008 return;
6009 }
6010
6011 Temp coords = get_image_coords(ctx, instr, type);
6012 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
6013 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
6014 mimg->operands[0] = Operand(resource);
6015 mimg->operands[1] = Operand(data);
6016 mimg->operands[2] = Operand(coords);
6017 if (return_previous)
6018 mimg->definitions[0] = Definition(dst);
6019 mimg->glc = return_previous;
6020 mimg->dlc = false; /* Not needed for atomics */
6021 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6022 mimg->dmask = (1 << data.size()) - 1;
6023 mimg->unrm = true;
6024 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
6025 mimg->disable_wqm = true;
6026 mimg->barrier = barrier_image;
6027 ctx->program->needs_exact = true;
6028 ctx->block->instructions.emplace_back(std::move(mimg));
6029 return;
6030 }
6031
6032 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
6033 {
6034 if (in_elements && ctx->options->chip_class == GFX8) {
6035 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6036 Builder bld(ctx->program, ctx->block);
6037
6038 Temp size = emit_extract_vector(ctx, desc, 2, s1);
6039
6040 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
6041 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
6042
6043 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
6044 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
6045
6046 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
6047 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
6048
6049 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
6050 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
6051 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
6052 if (dst.type() == RegType::vgpr)
6053 bld.copy(Definition(dst), shr_dst);
6054
6055 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6056 } else {
6057 emit_extract_vector(ctx, desc, 2, dst);
6058 }
6059 }
6060
6061 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
6062 {
6063 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
6064 const struct glsl_type *type = glsl_without_array(var->type);
6065 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
6066 bool is_array = glsl_sampler_type_is_array(type);
6067 Builder bld(ctx->program, ctx->block);
6068
6069 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
6070 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
6071 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
6072 }
6073
6074 /* LOD */
6075 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
6076
6077 /* Resource */
6078 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
6079
6080 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6081
6082 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
6083 mimg->operands[0] = Operand(resource);
6084 mimg->operands[1] = Operand(s4); /* no sampler */
6085 mimg->operands[2] = Operand(lod);
6086 uint8_t& dmask = mimg->dmask;
6087 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
6088 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
6089 mimg->da = glsl_sampler_type_is_array(type);
6090 mimg->can_reorder = true;
6091 Definition& def = mimg->definitions[0];
6092 ctx->block->instructions.emplace_back(std::move(mimg));
6093
6094 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
6095 glsl_sampler_type_is_array(type)) {
6096
6097 assert(instr->dest.ssa.num_components == 3);
6098 Temp tmp = {ctx->program->allocateId(), v3};
6099 def = Definition(tmp);
6100 emit_split_vector(ctx, tmp, 3);
6101
6102 /* divide 3rd value by 6 by multiplying with magic number */
6103 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
6104 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
6105
6106 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6107 emit_extract_vector(ctx, tmp, 0, v1),
6108 emit_extract_vector(ctx, tmp, 1, v1),
6109 by_6);
6110
6111 } else if (ctx->options->chip_class == GFX9 &&
6112 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
6113 glsl_sampler_type_is_array(type)) {
6114 assert(instr->dest.ssa.num_components == 2);
6115 def = Definition(dst);
6116 dmask = 0x5;
6117 } else {
6118 def = Definition(dst);
6119 }
6120
6121 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
6122 }
6123
6124 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6125 {
6126 Builder bld(ctx->program, ctx->block);
6127 unsigned num_components = instr->num_components;
6128
6129 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6130 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6131 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6132
6133 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6134 unsigned size = instr->dest.ssa.bit_size / 8;
6135 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa),
6136 nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr), glc, false);
6137 }
6138
6139 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6140 {
6141 Builder bld(ctx->program, ctx->block);
6142 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6143 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6144 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6145 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
6146
6147 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6148 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6149
6150 bool smem = !nir_src_is_divergent(instr->src[2]) &&
6151 ctx->options->chip_class >= GFX8 &&
6152 elem_size_bytes >= 4;
6153 if (smem)
6154 offset = bld.as_uniform(offset);
6155 bool smem_nonfs = smem && ctx->stage != fragment_fs;
6156
6157 unsigned write_count = 0;
6158 Temp write_datas[32];
6159 unsigned offsets[32];
6160 split_buffer_store(ctx, instr, smem, smem_nonfs ? RegType::sgpr : (smem ? data.type() : RegType::vgpr),
6161 data, writemask, 16, &write_count, write_datas, offsets);
6162
6163 for (unsigned i = 0; i < write_count; i++) {
6164 aco_opcode op = get_buffer_store_op(smem, write_datas[i].bytes());
6165 if (smem && ctx->stage == fragment_fs)
6166 op = aco_opcode::p_fs_buffer_store_smem;
6167
6168 if (smem) {
6169 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(op, Format::SMEM, 3, 0)};
6170 store->operands[0] = Operand(rsrc);
6171 if (offsets[i]) {
6172 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
6173 offset, Operand(offsets[i]));
6174 store->operands[1] = Operand(off);
6175 } else {
6176 store->operands[1] = Operand(offset);
6177 }
6178 if (op != aco_opcode::p_fs_buffer_store_smem)
6179 store->operands[1].setFixed(m0);
6180 store->operands[2] = Operand(write_datas[i]);
6181 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6182 store->dlc = false;
6183 store->disable_wqm = true;
6184 store->barrier = barrier_buffer;
6185 ctx->block->instructions.emplace_back(std::move(store));
6186 ctx->program->wb_smem_l1_on_end = true;
6187 if (op == aco_opcode::p_fs_buffer_store_smem) {
6188 ctx->block->kind |= block_kind_needs_lowering;
6189 ctx->program->needs_exact = true;
6190 }
6191 } else {
6192 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6193 store->operands[0] = Operand(rsrc);
6194 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6195 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6196 store->operands[3] = Operand(write_datas[i]);
6197 store->offset = offsets[i];
6198 store->offen = (offset.type() == RegType::vgpr);
6199 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6200 store->dlc = false;
6201 store->disable_wqm = true;
6202 store->barrier = barrier_buffer;
6203 ctx->program->needs_exact = true;
6204 ctx->block->instructions.emplace_back(std::move(store));
6205 }
6206 }
6207 }
6208
6209 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
6210 {
6211 /* return the previous value if dest is ever used */
6212 bool return_previous = false;
6213 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6214 return_previous = true;
6215 break;
6216 }
6217 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6218 return_previous = true;
6219 break;
6220 }
6221
6222 Builder bld(ctx->program, ctx->block);
6223 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
6224
6225 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
6226 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6227 get_ssa_temp(ctx, instr->src[3].ssa), data);
6228
6229 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
6230 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6231 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
6232
6233 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6234
6235 aco_opcode op32, op64;
6236 switch (instr->intrinsic) {
6237 case nir_intrinsic_ssbo_atomic_add:
6238 op32 = aco_opcode::buffer_atomic_add;
6239 op64 = aco_opcode::buffer_atomic_add_x2;
6240 break;
6241 case nir_intrinsic_ssbo_atomic_imin:
6242 op32 = aco_opcode::buffer_atomic_smin;
6243 op64 = aco_opcode::buffer_atomic_smin_x2;
6244 break;
6245 case nir_intrinsic_ssbo_atomic_umin:
6246 op32 = aco_opcode::buffer_atomic_umin;
6247 op64 = aco_opcode::buffer_atomic_umin_x2;
6248 break;
6249 case nir_intrinsic_ssbo_atomic_imax:
6250 op32 = aco_opcode::buffer_atomic_smax;
6251 op64 = aco_opcode::buffer_atomic_smax_x2;
6252 break;
6253 case nir_intrinsic_ssbo_atomic_umax:
6254 op32 = aco_opcode::buffer_atomic_umax;
6255 op64 = aco_opcode::buffer_atomic_umax_x2;
6256 break;
6257 case nir_intrinsic_ssbo_atomic_and:
6258 op32 = aco_opcode::buffer_atomic_and;
6259 op64 = aco_opcode::buffer_atomic_and_x2;
6260 break;
6261 case nir_intrinsic_ssbo_atomic_or:
6262 op32 = aco_opcode::buffer_atomic_or;
6263 op64 = aco_opcode::buffer_atomic_or_x2;
6264 break;
6265 case nir_intrinsic_ssbo_atomic_xor:
6266 op32 = aco_opcode::buffer_atomic_xor;
6267 op64 = aco_opcode::buffer_atomic_xor_x2;
6268 break;
6269 case nir_intrinsic_ssbo_atomic_exchange:
6270 op32 = aco_opcode::buffer_atomic_swap;
6271 op64 = aco_opcode::buffer_atomic_swap_x2;
6272 break;
6273 case nir_intrinsic_ssbo_atomic_comp_swap:
6274 op32 = aco_opcode::buffer_atomic_cmpswap;
6275 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6276 break;
6277 default:
6278 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6279 }
6280 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6281 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6282 mubuf->operands[0] = Operand(rsrc);
6283 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
6284 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
6285 mubuf->operands[3] = Operand(data);
6286 if (return_previous)
6287 mubuf->definitions[0] = Definition(dst);
6288 mubuf->offset = 0;
6289 mubuf->offen = (offset.type() == RegType::vgpr);
6290 mubuf->glc = return_previous;
6291 mubuf->dlc = false; /* Not needed for atomics */
6292 mubuf->disable_wqm = true;
6293 mubuf->barrier = barrier_buffer;
6294 ctx->program->needs_exact = true;
6295 ctx->block->instructions.emplace_back(std::move(mubuf));
6296 }
6297
6298 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
6299
6300 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6301 Builder bld(ctx->program, ctx->block);
6302 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
6303 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
6304 }
6305
6306 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6307 {
6308 Builder bld(ctx->program, ctx->block);
6309 unsigned num_components = instr->num_components;
6310 unsigned component_size = instr->dest.ssa.bit_size / 8;
6311
6312 LoadEmitInfo info = {Operand(get_ssa_temp(ctx, instr->src[0].ssa)),
6313 get_ssa_temp(ctx, &instr->dest.ssa),
6314 num_components, component_size};
6315 info.glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6316 info.align_mul = nir_intrinsic_align_mul(instr);
6317 info.align_offset = nir_intrinsic_align_offset(instr);
6318 info.barrier = barrier_buffer;
6319 info.can_reorder = false;
6320 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6321 * it's safe to use SMEM */
6322 bool can_use_smem = nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE;
6323 if (info.dst.type() == RegType::vgpr || (info.glc && ctx->options->chip_class < GFX8) || !can_use_smem) {
6324 emit_global_load(ctx, bld, &info);
6325 } else {
6326 info.offset = Operand(bld.as_uniform(info.offset));
6327 emit_smem_load(ctx, bld, &info);
6328 }
6329 }
6330
6331 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6332 {
6333 Builder bld(ctx->program, ctx->block);
6334 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6335 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6336
6337 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6338 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6339 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6340
6341 if (ctx->options->chip_class >= GFX7)
6342 addr = as_vgpr(ctx, addr);
6343
6344 unsigned write_count = 0;
6345 Temp write_datas[32];
6346 unsigned offsets[32];
6347 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6348 16, &write_count, write_datas, offsets);
6349
6350 for (unsigned i = 0; i < write_count; i++) {
6351 if (ctx->options->chip_class >= GFX7) {
6352 unsigned offset = offsets[i];
6353 Temp store_addr = addr;
6354 if (offset > 0 && ctx->options->chip_class < GFX9) {
6355 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6356 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6357 Temp carry = bld.tmp(bld.lm);
6358 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6359
6360 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6361 Operand(offset), addr0);
6362 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6363 Operand(0u), addr1,
6364 carry).def(1).setHint(vcc);
6365
6366 store_addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6367
6368 offset = 0;
6369 }
6370
6371 bool global = ctx->options->chip_class >= GFX9;
6372 aco_opcode op;
6373 switch (write_datas[i].bytes()) {
6374 case 1:
6375 op = global ? aco_opcode::global_store_byte : aco_opcode::flat_store_byte;
6376 break;
6377 case 2:
6378 op = global ? aco_opcode::global_store_short : aco_opcode::flat_store_short;
6379 break;
6380 case 4:
6381 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6382 break;
6383 case 8:
6384 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6385 break;
6386 case 12:
6387 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6388 break;
6389 case 16:
6390 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6391 break;
6392 default:
6393 unreachable("store_global not implemented for this size.");
6394 }
6395
6396 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6397 flat->operands[0] = Operand(store_addr);
6398 flat->operands[1] = Operand(s1);
6399 flat->operands[2] = Operand(write_datas[i]);
6400 flat->glc = glc;
6401 flat->dlc = false;
6402 flat->offset = offset;
6403 flat->disable_wqm = true;
6404 flat->barrier = barrier_buffer;
6405 ctx->program->needs_exact = true;
6406 ctx->block->instructions.emplace_back(std::move(flat));
6407 } else {
6408 assert(ctx->options->chip_class == GFX6);
6409
6410 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6411
6412 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6413
6414 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6415 mubuf->operands[0] = Operand(rsrc);
6416 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6417 mubuf->operands[2] = Operand(0u);
6418 mubuf->operands[3] = Operand(write_datas[i]);
6419 mubuf->glc = glc;
6420 mubuf->dlc = false;
6421 mubuf->offset = offsets[i];
6422 mubuf->addr64 = addr.type() == RegType::vgpr;
6423 mubuf->disable_wqm = true;
6424 mubuf->barrier = barrier_buffer;
6425 ctx->program->needs_exact = true;
6426 ctx->block->instructions.emplace_back(std::move(mubuf));
6427 }
6428 }
6429 }
6430
6431 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6432 {
6433 /* return the previous value if dest is ever used */
6434 bool return_previous = false;
6435 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6436 return_previous = true;
6437 break;
6438 }
6439 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6440 return_previous = true;
6441 break;
6442 }
6443
6444 Builder bld(ctx->program, ctx->block);
6445 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6446 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6447
6448 if (ctx->options->chip_class >= GFX7)
6449 addr = as_vgpr(ctx, addr);
6450
6451 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6452 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6453 get_ssa_temp(ctx, instr->src[2].ssa), data);
6454
6455 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6456
6457 aco_opcode op32, op64;
6458
6459 if (ctx->options->chip_class >= GFX7) {
6460 bool global = ctx->options->chip_class >= GFX9;
6461 switch (instr->intrinsic) {
6462 case nir_intrinsic_global_atomic_add:
6463 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6464 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6465 break;
6466 case nir_intrinsic_global_atomic_imin:
6467 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6468 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6469 break;
6470 case nir_intrinsic_global_atomic_umin:
6471 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6472 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6473 break;
6474 case nir_intrinsic_global_atomic_imax:
6475 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6476 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6477 break;
6478 case nir_intrinsic_global_atomic_umax:
6479 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6480 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6481 break;
6482 case nir_intrinsic_global_atomic_and:
6483 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6484 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6485 break;
6486 case nir_intrinsic_global_atomic_or:
6487 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6488 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6489 break;
6490 case nir_intrinsic_global_atomic_xor:
6491 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6492 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6493 break;
6494 case nir_intrinsic_global_atomic_exchange:
6495 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6496 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6497 break;
6498 case nir_intrinsic_global_atomic_comp_swap:
6499 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6500 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6501 break;
6502 default:
6503 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6504 }
6505
6506 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6507 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6508 flat->operands[0] = Operand(addr);
6509 flat->operands[1] = Operand(s1);
6510 flat->operands[2] = Operand(data);
6511 if (return_previous)
6512 flat->definitions[0] = Definition(dst);
6513 flat->glc = return_previous;
6514 flat->dlc = false; /* Not needed for atomics */
6515 flat->offset = 0;
6516 flat->disable_wqm = true;
6517 flat->barrier = barrier_buffer;
6518 ctx->program->needs_exact = true;
6519 ctx->block->instructions.emplace_back(std::move(flat));
6520 } else {
6521 assert(ctx->options->chip_class == GFX6);
6522
6523 switch (instr->intrinsic) {
6524 case nir_intrinsic_global_atomic_add:
6525 op32 = aco_opcode::buffer_atomic_add;
6526 op64 = aco_opcode::buffer_atomic_add_x2;
6527 break;
6528 case nir_intrinsic_global_atomic_imin:
6529 op32 = aco_opcode::buffer_atomic_smin;
6530 op64 = aco_opcode::buffer_atomic_smin_x2;
6531 break;
6532 case nir_intrinsic_global_atomic_umin:
6533 op32 = aco_opcode::buffer_atomic_umin;
6534 op64 = aco_opcode::buffer_atomic_umin_x2;
6535 break;
6536 case nir_intrinsic_global_atomic_imax:
6537 op32 = aco_opcode::buffer_atomic_smax;
6538 op64 = aco_opcode::buffer_atomic_smax_x2;
6539 break;
6540 case nir_intrinsic_global_atomic_umax:
6541 op32 = aco_opcode::buffer_atomic_umax;
6542 op64 = aco_opcode::buffer_atomic_umax_x2;
6543 break;
6544 case nir_intrinsic_global_atomic_and:
6545 op32 = aco_opcode::buffer_atomic_and;
6546 op64 = aco_opcode::buffer_atomic_and_x2;
6547 break;
6548 case nir_intrinsic_global_atomic_or:
6549 op32 = aco_opcode::buffer_atomic_or;
6550 op64 = aco_opcode::buffer_atomic_or_x2;
6551 break;
6552 case nir_intrinsic_global_atomic_xor:
6553 op32 = aco_opcode::buffer_atomic_xor;
6554 op64 = aco_opcode::buffer_atomic_xor_x2;
6555 break;
6556 case nir_intrinsic_global_atomic_exchange:
6557 op32 = aco_opcode::buffer_atomic_swap;
6558 op64 = aco_opcode::buffer_atomic_swap_x2;
6559 break;
6560 case nir_intrinsic_global_atomic_comp_swap:
6561 op32 = aco_opcode::buffer_atomic_cmpswap;
6562 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6563 break;
6564 default:
6565 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6566 }
6567
6568 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6569
6570 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6571
6572 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6573 mubuf->operands[0] = Operand(rsrc);
6574 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6575 mubuf->operands[2] = Operand(0u);
6576 mubuf->operands[3] = Operand(data);
6577 if (return_previous)
6578 mubuf->definitions[0] = Definition(dst);
6579 mubuf->glc = return_previous;
6580 mubuf->dlc = false;
6581 mubuf->offset = 0;
6582 mubuf->addr64 = addr.type() == RegType::vgpr;
6583 mubuf->disable_wqm = true;
6584 mubuf->barrier = barrier_buffer;
6585 ctx->program->needs_exact = true;
6586 ctx->block->instructions.emplace_back(std::move(mubuf));
6587 }
6588 }
6589
6590 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6591 Builder bld(ctx->program, ctx->block);
6592 switch(instr->intrinsic) {
6593 case nir_intrinsic_group_memory_barrier:
6594 case nir_intrinsic_memory_barrier:
6595 bld.barrier(aco_opcode::p_memory_barrier_common);
6596 break;
6597 case nir_intrinsic_memory_barrier_buffer:
6598 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6599 break;
6600 case nir_intrinsic_memory_barrier_image:
6601 bld.barrier(aco_opcode::p_memory_barrier_image);
6602 break;
6603 case nir_intrinsic_memory_barrier_tcs_patch:
6604 case nir_intrinsic_memory_barrier_shared:
6605 bld.barrier(aco_opcode::p_memory_barrier_shared);
6606 break;
6607 default:
6608 unreachable("Unimplemented memory barrier intrinsic");
6609 break;
6610 }
6611 }
6612
6613 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6614 {
6615 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6616 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6617 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6618 Builder bld(ctx->program, ctx->block);
6619
6620 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6621 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6622 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6623 }
6624
6625 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6626 {
6627 unsigned writemask = nir_intrinsic_write_mask(instr);
6628 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6629 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6630 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6631
6632 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6633 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6634 }
6635
6636 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6637 {
6638 unsigned offset = nir_intrinsic_base(instr);
6639 Builder bld(ctx->program, ctx->block);
6640 Operand m = load_lds_size_m0(bld);
6641 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6642 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6643
6644 unsigned num_operands = 3;
6645 aco_opcode op32, op64, op32_rtn, op64_rtn;
6646 switch(instr->intrinsic) {
6647 case nir_intrinsic_shared_atomic_add:
6648 op32 = aco_opcode::ds_add_u32;
6649 op64 = aco_opcode::ds_add_u64;
6650 op32_rtn = aco_opcode::ds_add_rtn_u32;
6651 op64_rtn = aco_opcode::ds_add_rtn_u64;
6652 break;
6653 case nir_intrinsic_shared_atomic_imin:
6654 op32 = aco_opcode::ds_min_i32;
6655 op64 = aco_opcode::ds_min_i64;
6656 op32_rtn = aco_opcode::ds_min_rtn_i32;
6657 op64_rtn = aco_opcode::ds_min_rtn_i64;
6658 break;
6659 case nir_intrinsic_shared_atomic_umin:
6660 op32 = aco_opcode::ds_min_u32;
6661 op64 = aco_opcode::ds_min_u64;
6662 op32_rtn = aco_opcode::ds_min_rtn_u32;
6663 op64_rtn = aco_opcode::ds_min_rtn_u64;
6664 break;
6665 case nir_intrinsic_shared_atomic_imax:
6666 op32 = aco_opcode::ds_max_i32;
6667 op64 = aco_opcode::ds_max_i64;
6668 op32_rtn = aco_opcode::ds_max_rtn_i32;
6669 op64_rtn = aco_opcode::ds_max_rtn_i64;
6670 break;
6671 case nir_intrinsic_shared_atomic_umax:
6672 op32 = aco_opcode::ds_max_u32;
6673 op64 = aco_opcode::ds_max_u64;
6674 op32_rtn = aco_opcode::ds_max_rtn_u32;
6675 op64_rtn = aco_opcode::ds_max_rtn_u64;
6676 break;
6677 case nir_intrinsic_shared_atomic_and:
6678 op32 = aco_opcode::ds_and_b32;
6679 op64 = aco_opcode::ds_and_b64;
6680 op32_rtn = aco_opcode::ds_and_rtn_b32;
6681 op64_rtn = aco_opcode::ds_and_rtn_b64;
6682 break;
6683 case nir_intrinsic_shared_atomic_or:
6684 op32 = aco_opcode::ds_or_b32;
6685 op64 = aco_opcode::ds_or_b64;
6686 op32_rtn = aco_opcode::ds_or_rtn_b32;
6687 op64_rtn = aco_opcode::ds_or_rtn_b64;
6688 break;
6689 case nir_intrinsic_shared_atomic_xor:
6690 op32 = aco_opcode::ds_xor_b32;
6691 op64 = aco_opcode::ds_xor_b64;
6692 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6693 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6694 break;
6695 case nir_intrinsic_shared_atomic_exchange:
6696 op32 = aco_opcode::ds_write_b32;
6697 op64 = aco_opcode::ds_write_b64;
6698 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6699 op64_rtn = aco_opcode::ds_wrxchg_rtn_b64;
6700 break;
6701 case nir_intrinsic_shared_atomic_comp_swap:
6702 op32 = aco_opcode::ds_cmpst_b32;
6703 op64 = aco_opcode::ds_cmpst_b64;
6704 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6705 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6706 num_operands = 4;
6707 break;
6708 default:
6709 unreachable("Unhandled shared atomic intrinsic");
6710 }
6711
6712 /* return the previous value if dest is ever used */
6713 bool return_previous = false;
6714 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6715 return_previous = true;
6716 break;
6717 }
6718 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6719 return_previous = true;
6720 break;
6721 }
6722
6723 aco_opcode op;
6724 if (data.size() == 1) {
6725 assert(instr->dest.ssa.bit_size == 32);
6726 op = return_previous ? op32_rtn : op32;
6727 } else {
6728 assert(instr->dest.ssa.bit_size == 64);
6729 op = return_previous ? op64_rtn : op64;
6730 }
6731
6732 if (offset > 65535) {
6733 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6734 offset = 0;
6735 }
6736
6737 aco_ptr<DS_instruction> ds;
6738 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6739 ds->operands[0] = Operand(address);
6740 ds->operands[1] = Operand(data);
6741 if (num_operands == 4)
6742 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6743 ds->operands[num_operands - 1] = m;
6744 ds->offset0 = offset;
6745 if (return_previous)
6746 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6747 ctx->block->instructions.emplace_back(std::move(ds));
6748 }
6749
6750 Temp get_scratch_resource(isel_context *ctx)
6751 {
6752 Builder bld(ctx->program, ctx->block);
6753 Temp scratch_addr = ctx->program->private_segment_buffer;
6754 if (ctx->stage != compute_cs)
6755 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6756
6757 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6758 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6759
6760 if (ctx->program->chip_class >= GFX10) {
6761 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6762 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6763 S_008F0C_RESOURCE_LEVEL(1);
6764 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6765 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6766 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6767 }
6768
6769 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6770 if (ctx->program->chip_class <= GFX8)
6771 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6772
6773 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6774 }
6775
6776 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6777 Builder bld(ctx->program, ctx->block);
6778 Temp rsrc = get_scratch_resource(ctx);
6779 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6780 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6781
6782 LoadEmitInfo info = {Operand(offset), dst, instr->dest.ssa.num_components,
6783 instr->dest.ssa.bit_size / 8u, rsrc};
6784 info.align_mul = nir_intrinsic_align_mul(instr);
6785 info.align_offset = nir_intrinsic_align_offset(instr);
6786 info.swizzle_component_size = 16;
6787 info.can_reorder = false;
6788 info.soffset = ctx->program->scratch_offset;
6789 emit_mubuf_load(ctx, bld, &info);
6790 }
6791
6792 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6793 Builder bld(ctx->program, ctx->block);
6794 Temp rsrc = get_scratch_resource(ctx);
6795 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6796 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6797
6798 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6799 unsigned writemask = widen_mask(nir_intrinsic_write_mask(instr), elem_size_bytes);
6800
6801 unsigned write_count = 0;
6802 Temp write_datas[32];
6803 unsigned offsets[32];
6804 split_buffer_store(ctx, instr, false, RegType::vgpr, data, writemask,
6805 16, &write_count, write_datas, offsets);
6806
6807 for (unsigned i = 0; i < write_count; i++) {
6808 aco_opcode op = get_buffer_store_op(false, write_datas[i].bytes());
6809 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_datas[i], offsets[i], true);
6810 }
6811 }
6812
6813 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6814 uint8_t log2_ps_iter_samples;
6815 if (ctx->program->info->ps.force_persample) {
6816 log2_ps_iter_samples =
6817 util_logbase2(ctx->options->key.fs.num_samples);
6818 } else {
6819 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6820 }
6821
6822 /* The bit pattern matches that used by fixed function fragment
6823 * processing. */
6824 static const unsigned ps_iter_masks[] = {
6825 0xffff, /* not used */
6826 0x5555,
6827 0x1111,
6828 0x0101,
6829 0x0001,
6830 };
6831 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6832
6833 Builder bld(ctx->program, ctx->block);
6834
6835 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6836 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6837 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6838 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6839 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6840 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6841 }
6842
6843 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6844 Builder bld(ctx->program, ctx->block);
6845
6846 unsigned stream = nir_intrinsic_stream_id(instr);
6847 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6848 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6849 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6850
6851 /* get GSVS ring */
6852 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6853
6854 unsigned num_components =
6855 ctx->program->info->gs.num_stream_output_components[stream];
6856 assert(num_components);
6857
6858 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6859 unsigned stream_offset = 0;
6860 for (unsigned i = 0; i < stream; i++) {
6861 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6862 stream_offset += prev_stride * ctx->program->wave_size;
6863 }
6864
6865 /* Limit on the stride field for <= GFX7. */
6866 assert(stride < (1 << 14));
6867
6868 Temp gsvs_dwords[4];
6869 for (unsigned i = 0; i < 4; i++)
6870 gsvs_dwords[i] = bld.tmp(s1);
6871 bld.pseudo(aco_opcode::p_split_vector,
6872 Definition(gsvs_dwords[0]),
6873 Definition(gsvs_dwords[1]),
6874 Definition(gsvs_dwords[2]),
6875 Definition(gsvs_dwords[3]),
6876 gsvs_ring);
6877
6878 if (stream_offset) {
6879 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6880
6881 Temp carry = bld.tmp(s1);
6882 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6883 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));
6884 }
6885
6886 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)));
6887 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6888
6889 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6890 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6891
6892 unsigned offset = 0;
6893 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6894 if (ctx->program->info->gs.output_streams[i] != stream)
6895 continue;
6896
6897 for (unsigned j = 0; j < 4; j++) {
6898 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6899 continue;
6900
6901 if (ctx->outputs.mask[i] & (1 << j)) {
6902 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6903 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6904 if (const_offset >= 4096u) {
6905 if (vaddr_offset.isUndefined())
6906 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6907 else
6908 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6909 const_offset %= 4096u;
6910 }
6911
6912 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6913 mtbuf->operands[0] = Operand(gsvs_ring);
6914 mtbuf->operands[1] = vaddr_offset;
6915 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6916 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6917 mtbuf->offen = !vaddr_offset.isUndefined();
6918 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6919 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6920 mtbuf->offset = const_offset;
6921 mtbuf->glc = true;
6922 mtbuf->slc = true;
6923 mtbuf->barrier = barrier_gs_data;
6924 mtbuf->can_reorder = true;
6925 bld.insert(std::move(mtbuf));
6926 }
6927
6928 offset += ctx->shader->info.gs.vertices_out;
6929 }
6930
6931 /* outputs for the next vertex are undefined and keeping them around can
6932 * create invalid IR with control flow */
6933 ctx->outputs.mask[i] = 0;
6934 }
6935
6936 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6937 }
6938
6939 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6940 {
6941 Builder bld(ctx->program, ctx->block);
6942
6943 if (cluster_size == 1) {
6944 return src;
6945 } if (op == nir_op_iand && cluster_size == 4) {
6946 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6947 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6948 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6949 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6950 } else if (op == nir_op_ior && cluster_size == 4) {
6951 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6952 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6953 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6954 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6955 //subgroupAnd(val) -> (exec & ~val) == 0
6956 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6957 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6958 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6959 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6960 //subgroupOr(val) -> (val & exec) != 0
6961 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6962 return bool_to_vector_condition(ctx, tmp);
6963 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6964 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6965 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6966 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6967 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6968 return bool_to_vector_condition(ctx, tmp);
6969 } else {
6970 //subgroupClustered{And,Or,Xor}(val, n) ->
6971 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6972 //cluster_offset = ~(n - 1) & lane_id
6973 //cluster_mask = ((1 << n) - 1)
6974 //subgroupClusteredAnd():
6975 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6976 //subgroupClusteredOr():
6977 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6978 //subgroupClusteredXor():
6979 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6980 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6981 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6982
6983 Temp tmp;
6984 if (op == nir_op_iand)
6985 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6986 else
6987 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6988
6989 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6990
6991 if (ctx->program->chip_class <= GFX7)
6992 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6993 else if (ctx->program->wave_size == 64)
6994 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6995 else
6996 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6997 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6998 if (cluster_mask != 0xffffffff)
6999 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
7000
7001 Definition cmp_def = Definition();
7002 if (op == nir_op_iand) {
7003 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
7004 } else if (op == nir_op_ior) {
7005 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7006 } else if (op == nir_op_ixor) {
7007 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
7008 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
7009 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
7010 }
7011 cmp_def.setHint(vcc);
7012 return cmp_def.getTemp();
7013 }
7014 }
7015
7016 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
7017 {
7018 Builder bld(ctx->program, ctx->block);
7019
7020 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7021 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7022 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7023 Temp tmp;
7024 if (op == nir_op_iand)
7025 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
7026 else
7027 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
7028
7029 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
7030 Temp lo = lohi.def(0).getTemp();
7031 Temp hi = lohi.def(1).getTemp();
7032 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
7033
7034 Definition cmp_def = Definition();
7035 if (op == nir_op_iand)
7036 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7037 else if (op == nir_op_ior)
7038 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
7039 else if (op == nir_op_ixor)
7040 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
7041 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
7042 cmp_def.setHint(vcc);
7043 return cmp_def.getTemp();
7044 }
7045
7046 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
7047 {
7048 Builder bld(ctx->program, ctx->block);
7049
7050 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7051 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7052 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7053 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
7054 if (op == nir_op_iand)
7055 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7056 else if (op == nir_op_ior)
7057 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7058 else if (op == nir_op_ixor)
7059 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
7060
7061 assert(false);
7062 return Temp();
7063 }
7064
7065 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
7066 {
7067 Builder bld(ctx->program, ctx->block);
7068 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
7069 if (src.regClass().type() == RegType::vgpr) {
7070 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
7071 } else if (src.regClass() == s1) {
7072 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7073 } else if (src.regClass() == s2) {
7074 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7075 } else {
7076 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7077 nir_print_instr(&instr->instr, stderr);
7078 fprintf(stderr, "\n");
7079 }
7080 }
7081
7082 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7083 {
7084 Builder bld(ctx->program, ctx->block);
7085 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7086 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7087 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7088
7089 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7090 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7091 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7092 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7093
7094 /* Build DD X/Y */
7095 if (ctx->program->chip_class >= GFX8) {
7096 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7097 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7098 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7099 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7100 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7101 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7102 } else {
7103 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7104 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7105 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7106 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7107 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7108 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7109 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7110 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7111 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7112 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7113 }
7114
7115 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7116 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7117 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7118 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7119 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7120 Temp wqm1 = bld.tmp(v1);
7121 emit_wqm(ctx, tmp1, wqm1, true);
7122 Temp wqm2 = bld.tmp(v1);
7123 emit_wqm(ctx, tmp2, wqm2, true);
7124 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7125 return;
7126 }
7127
7128 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7129 {
7130 Builder bld(ctx->program, ctx->block);
7131 switch(instr->intrinsic) {
7132 case nir_intrinsic_load_barycentric_sample:
7133 case nir_intrinsic_load_barycentric_pixel:
7134 case nir_intrinsic_load_barycentric_centroid: {
7135 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7136 Temp bary = Temp(0, s2);
7137 switch (mode) {
7138 case INTERP_MODE_SMOOTH:
7139 case INTERP_MODE_NONE:
7140 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7141 bary = get_arg(ctx, ctx->args->ac.persp_center);
7142 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7143 bary = ctx->persp_centroid;
7144 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7145 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7146 break;
7147 case INTERP_MODE_NOPERSPECTIVE:
7148 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7149 bary = get_arg(ctx, ctx->args->ac.linear_center);
7150 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7151 bary = ctx->linear_centroid;
7152 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7153 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7154 break;
7155 default:
7156 break;
7157 }
7158 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7159 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7160 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7161 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7162 Operand(p1), Operand(p2));
7163 emit_split_vector(ctx, dst, 2);
7164 break;
7165 }
7166 case nir_intrinsic_load_barycentric_model: {
7167 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7168
7169 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7170 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7171 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7172 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7173 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7174 Operand(p1), Operand(p2), Operand(p3));
7175 emit_split_vector(ctx, dst, 3);
7176 break;
7177 }
7178 case nir_intrinsic_load_barycentric_at_sample: {
7179 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7180 switch (ctx->options->key.fs.num_samples) {
7181 case 2: sample_pos_offset += 1 << 3; break;
7182 case 4: sample_pos_offset += 3 << 3; break;
7183 case 8: sample_pos_offset += 7 << 3; break;
7184 default: break;
7185 }
7186 Temp sample_pos;
7187 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7188 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7189 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7190 if (addr.type() == RegType::sgpr) {
7191 Operand offset;
7192 if (const_addr) {
7193 sample_pos_offset += const_addr->u32 << 3;
7194 offset = Operand(sample_pos_offset);
7195 } else if (ctx->options->chip_class >= GFX9) {
7196 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7197 } else {
7198 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7199 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7200 }
7201
7202 Operand off = bld.copy(bld.def(s1), Operand(offset));
7203 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7204
7205 } else if (ctx->options->chip_class >= GFX9) {
7206 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7207 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7208 } else if (ctx->options->chip_class >= GFX7) {
7209 /* addr += private_segment_buffer + sample_pos_offset */
7210 Temp tmp0 = bld.tmp(s1);
7211 Temp tmp1 = bld.tmp(s1);
7212 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7213 Definition scc_tmp = bld.def(s1, scc);
7214 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7215 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7216 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7217 Temp pck0 = bld.tmp(v1);
7218 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7219 tmp1 = as_vgpr(ctx, tmp1);
7220 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);
7221 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7222
7223 /* sample_pos = flat_load_dwordx2 addr */
7224 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7225 } else {
7226 assert(ctx->options->chip_class == GFX6);
7227
7228 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7229 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7230 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7231
7232 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7233 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7234
7235 sample_pos = bld.tmp(v2);
7236
7237 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7238 load->definitions[0] = Definition(sample_pos);
7239 load->operands[0] = Operand(rsrc);
7240 load->operands[1] = Operand(addr);
7241 load->operands[2] = Operand(0u);
7242 load->offset = sample_pos_offset;
7243 load->offen = 0;
7244 load->addr64 = true;
7245 load->glc = false;
7246 load->dlc = false;
7247 load->disable_wqm = false;
7248 load->barrier = barrier_none;
7249 load->can_reorder = true;
7250 ctx->block->instructions.emplace_back(std::move(load));
7251 }
7252
7253 /* sample_pos -= 0.5 */
7254 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7255 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7256 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7257 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7258 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7259
7260 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7261 break;
7262 }
7263 case nir_intrinsic_load_barycentric_at_offset: {
7264 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7265 RegClass rc = RegClass(offset.type(), 1);
7266 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7267 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7268 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7269 break;
7270 }
7271 case nir_intrinsic_load_front_face: {
7272 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7273 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7274 break;
7275 }
7276 case nir_intrinsic_load_view_index: {
7277 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7278 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7279 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7280 break;
7281 }
7282
7283 /* fallthrough */
7284 }
7285 case nir_intrinsic_load_layer_id: {
7286 unsigned idx = nir_intrinsic_base(instr);
7287 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7288 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7289 break;
7290 }
7291 case nir_intrinsic_load_frag_coord: {
7292 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7293 break;
7294 }
7295 case nir_intrinsic_load_sample_pos: {
7296 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7297 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7298 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7299 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7300 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7301 break;
7302 }
7303 case nir_intrinsic_load_tess_coord:
7304 visit_load_tess_coord(ctx, instr);
7305 break;
7306 case nir_intrinsic_load_interpolated_input:
7307 visit_load_interpolated_input(ctx, instr);
7308 break;
7309 case nir_intrinsic_store_output:
7310 visit_store_output(ctx, instr);
7311 break;
7312 case nir_intrinsic_load_input:
7313 case nir_intrinsic_load_input_vertex:
7314 visit_load_input(ctx, instr);
7315 break;
7316 case nir_intrinsic_load_output:
7317 visit_load_output(ctx, instr);
7318 break;
7319 case nir_intrinsic_load_per_vertex_input:
7320 visit_load_per_vertex_input(ctx, instr);
7321 break;
7322 case nir_intrinsic_load_per_vertex_output:
7323 visit_load_per_vertex_output(ctx, instr);
7324 break;
7325 case nir_intrinsic_store_per_vertex_output:
7326 visit_store_per_vertex_output(ctx, instr);
7327 break;
7328 case nir_intrinsic_load_ubo:
7329 visit_load_ubo(ctx, instr);
7330 break;
7331 case nir_intrinsic_load_push_constant:
7332 visit_load_push_constant(ctx, instr);
7333 break;
7334 case nir_intrinsic_load_constant:
7335 visit_load_constant(ctx, instr);
7336 break;
7337 case nir_intrinsic_vulkan_resource_index:
7338 visit_load_resource(ctx, instr);
7339 break;
7340 case nir_intrinsic_discard:
7341 visit_discard(ctx, instr);
7342 break;
7343 case nir_intrinsic_discard_if:
7344 visit_discard_if(ctx, instr);
7345 break;
7346 case nir_intrinsic_load_shared:
7347 visit_load_shared(ctx, instr);
7348 break;
7349 case nir_intrinsic_store_shared:
7350 visit_store_shared(ctx, instr);
7351 break;
7352 case nir_intrinsic_shared_atomic_add:
7353 case nir_intrinsic_shared_atomic_imin:
7354 case nir_intrinsic_shared_atomic_umin:
7355 case nir_intrinsic_shared_atomic_imax:
7356 case nir_intrinsic_shared_atomic_umax:
7357 case nir_intrinsic_shared_atomic_and:
7358 case nir_intrinsic_shared_atomic_or:
7359 case nir_intrinsic_shared_atomic_xor:
7360 case nir_intrinsic_shared_atomic_exchange:
7361 case nir_intrinsic_shared_atomic_comp_swap:
7362 visit_shared_atomic(ctx, instr);
7363 break;
7364 case nir_intrinsic_image_deref_load:
7365 visit_image_load(ctx, instr);
7366 break;
7367 case nir_intrinsic_image_deref_store:
7368 visit_image_store(ctx, instr);
7369 break;
7370 case nir_intrinsic_image_deref_atomic_add:
7371 case nir_intrinsic_image_deref_atomic_umin:
7372 case nir_intrinsic_image_deref_atomic_imin:
7373 case nir_intrinsic_image_deref_atomic_umax:
7374 case nir_intrinsic_image_deref_atomic_imax:
7375 case nir_intrinsic_image_deref_atomic_and:
7376 case nir_intrinsic_image_deref_atomic_or:
7377 case nir_intrinsic_image_deref_atomic_xor:
7378 case nir_intrinsic_image_deref_atomic_exchange:
7379 case nir_intrinsic_image_deref_atomic_comp_swap:
7380 visit_image_atomic(ctx, instr);
7381 break;
7382 case nir_intrinsic_image_deref_size:
7383 visit_image_size(ctx, instr);
7384 break;
7385 case nir_intrinsic_load_ssbo:
7386 visit_load_ssbo(ctx, instr);
7387 break;
7388 case nir_intrinsic_store_ssbo:
7389 visit_store_ssbo(ctx, instr);
7390 break;
7391 case nir_intrinsic_load_global:
7392 visit_load_global(ctx, instr);
7393 break;
7394 case nir_intrinsic_store_global:
7395 visit_store_global(ctx, instr);
7396 break;
7397 case nir_intrinsic_global_atomic_add:
7398 case nir_intrinsic_global_atomic_imin:
7399 case nir_intrinsic_global_atomic_umin:
7400 case nir_intrinsic_global_atomic_imax:
7401 case nir_intrinsic_global_atomic_umax:
7402 case nir_intrinsic_global_atomic_and:
7403 case nir_intrinsic_global_atomic_or:
7404 case nir_intrinsic_global_atomic_xor:
7405 case nir_intrinsic_global_atomic_exchange:
7406 case nir_intrinsic_global_atomic_comp_swap:
7407 visit_global_atomic(ctx, instr);
7408 break;
7409 case nir_intrinsic_ssbo_atomic_add:
7410 case nir_intrinsic_ssbo_atomic_imin:
7411 case nir_intrinsic_ssbo_atomic_umin:
7412 case nir_intrinsic_ssbo_atomic_imax:
7413 case nir_intrinsic_ssbo_atomic_umax:
7414 case nir_intrinsic_ssbo_atomic_and:
7415 case nir_intrinsic_ssbo_atomic_or:
7416 case nir_intrinsic_ssbo_atomic_xor:
7417 case nir_intrinsic_ssbo_atomic_exchange:
7418 case nir_intrinsic_ssbo_atomic_comp_swap:
7419 visit_atomic_ssbo(ctx, instr);
7420 break;
7421 case nir_intrinsic_load_scratch:
7422 visit_load_scratch(ctx, instr);
7423 break;
7424 case nir_intrinsic_store_scratch:
7425 visit_store_scratch(ctx, instr);
7426 break;
7427 case nir_intrinsic_get_buffer_size:
7428 visit_get_buffer_size(ctx, instr);
7429 break;
7430 case nir_intrinsic_control_barrier: {
7431 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7432 /* GFX6 only (thanks to a hw bug workaround):
7433 * The real barrier instruction isn’t needed, because an entire patch
7434 * always fits into a single wave.
7435 */
7436 break;
7437 }
7438
7439 if (ctx->program->workgroup_size > ctx->program->wave_size)
7440 bld.sopp(aco_opcode::s_barrier);
7441
7442 break;
7443 }
7444 case nir_intrinsic_memory_barrier_tcs_patch:
7445 case nir_intrinsic_group_memory_barrier:
7446 case nir_intrinsic_memory_barrier:
7447 case nir_intrinsic_memory_barrier_buffer:
7448 case nir_intrinsic_memory_barrier_image:
7449 case nir_intrinsic_memory_barrier_shared:
7450 emit_memory_barrier(ctx, instr);
7451 break;
7452 case nir_intrinsic_load_num_work_groups: {
7453 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7454 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7455 emit_split_vector(ctx, dst, 3);
7456 break;
7457 }
7458 case nir_intrinsic_load_local_invocation_id: {
7459 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7460 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7461 emit_split_vector(ctx, dst, 3);
7462 break;
7463 }
7464 case nir_intrinsic_load_work_group_id: {
7465 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7466 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7467 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7468 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7469 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7470 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7471 emit_split_vector(ctx, dst, 3);
7472 break;
7473 }
7474 case nir_intrinsic_load_local_invocation_index: {
7475 Temp id = emit_mbcnt(ctx, bld.def(v1));
7476
7477 /* The tg_size bits [6:11] contain the subgroup id,
7478 * we need this multiplied by the wave size, and then OR the thread id to it.
7479 */
7480 if (ctx->program->wave_size == 64) {
7481 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7482 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7483 get_arg(ctx, ctx->args->ac.tg_size));
7484 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7485 } else {
7486 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7487 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7488 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7489 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7490 }
7491 break;
7492 }
7493 case nir_intrinsic_load_subgroup_id: {
7494 if (ctx->stage == compute_cs) {
7495 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7496 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7497 } else {
7498 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7499 }
7500 break;
7501 }
7502 case nir_intrinsic_load_subgroup_invocation: {
7503 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7504 break;
7505 }
7506 case nir_intrinsic_load_num_subgroups: {
7507 if (ctx->stage == compute_cs)
7508 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7509 get_arg(ctx, ctx->args->ac.tg_size));
7510 else
7511 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7512 break;
7513 }
7514 case nir_intrinsic_ballot: {
7515 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7516 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7517 Definition tmp = bld.def(dst.regClass());
7518 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7519 if (instr->src[0].ssa->bit_size == 1) {
7520 assert(src.regClass() == bld.lm);
7521 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7522 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7523 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7524 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7525 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7526 } else {
7527 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7528 nir_print_instr(&instr->instr, stderr);
7529 fprintf(stderr, "\n");
7530 }
7531 if (dst.size() != bld.lm.size()) {
7532 /* Wave32 with ballot size set to 64 */
7533 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7534 }
7535 emit_wqm(ctx, tmp.getTemp(), dst);
7536 break;
7537 }
7538 case nir_intrinsic_shuffle:
7539 case nir_intrinsic_read_invocation: {
7540 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7541 if (!nir_src_is_divergent(instr->src[0])) {
7542 emit_uniform_subgroup(ctx, instr, src);
7543 } else {
7544 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7545 if (instr->intrinsic == nir_intrinsic_read_invocation || !nir_src_is_divergent(instr->src[1]))
7546 tid = bld.as_uniform(tid);
7547 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7548 if (src.regClass() == v1b || src.regClass() == v2b) {
7549 Temp tmp = bld.tmp(v1);
7550 tmp = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), tmp);
7551 if (dst.type() == RegType::vgpr)
7552 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(src.regClass() == v1b ? v3b : v2b), tmp);
7553 else
7554 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
7555 } else if (src.regClass() == v1) {
7556 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7557 } else if (src.regClass() == v2) {
7558 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7559 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7560 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7561 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7562 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7563 emit_split_vector(ctx, dst, 2);
7564 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7565 assert(src.regClass() == bld.lm);
7566 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7567 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7568 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7569 assert(src.regClass() == bld.lm);
7570 Temp tmp;
7571 if (ctx->program->chip_class <= GFX7)
7572 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7573 else if (ctx->program->wave_size == 64)
7574 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7575 else
7576 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7577 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7578 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7579 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7580 } else {
7581 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7582 nir_print_instr(&instr->instr, stderr);
7583 fprintf(stderr, "\n");
7584 }
7585 }
7586 break;
7587 }
7588 case nir_intrinsic_load_sample_id: {
7589 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7590 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7591 break;
7592 }
7593 case nir_intrinsic_load_sample_mask_in: {
7594 visit_load_sample_mask_in(ctx, instr);
7595 break;
7596 }
7597 case nir_intrinsic_read_first_invocation: {
7598 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7599 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7600 if (src.regClass() == v1b || src.regClass() == v2b || src.regClass() == v1) {
7601 emit_wqm(ctx,
7602 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7603 dst);
7604 } else if (src.regClass() == v2) {
7605 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7606 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7607 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7608 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7609 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7610 emit_split_vector(ctx, dst, 2);
7611 } else if (instr->dest.ssa.bit_size == 1) {
7612 assert(src.regClass() == bld.lm);
7613 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7614 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7615 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7616 } else if (src.regClass() == s1) {
7617 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7618 } else if (src.regClass() == s2) {
7619 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7620 } else {
7621 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7622 nir_print_instr(&instr->instr, stderr);
7623 fprintf(stderr, "\n");
7624 }
7625 break;
7626 }
7627 case nir_intrinsic_vote_all: {
7628 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7629 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7630 assert(src.regClass() == bld.lm);
7631 assert(dst.regClass() == bld.lm);
7632
7633 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7634 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7635 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7636 break;
7637 }
7638 case nir_intrinsic_vote_any: {
7639 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7640 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7641 assert(src.regClass() == bld.lm);
7642 assert(dst.regClass() == bld.lm);
7643
7644 Temp tmp = bool_to_scalar_condition(ctx, src);
7645 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7646 break;
7647 }
7648 case nir_intrinsic_reduce:
7649 case nir_intrinsic_inclusive_scan:
7650 case nir_intrinsic_exclusive_scan: {
7651 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7652 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7653 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7654 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7655 nir_intrinsic_cluster_size(instr) : 0;
7656 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7657
7658 if (!nir_src_is_divergent(instr->src[0]) && (op == nir_op_ior || op == nir_op_iand)) {
7659 emit_uniform_subgroup(ctx, instr, src);
7660 } else if (instr->dest.ssa.bit_size == 1) {
7661 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7662 op = nir_op_iand;
7663 else if (op == nir_op_iadd)
7664 op = nir_op_ixor;
7665 else if (op == nir_op_umax || op == nir_op_imax)
7666 op = nir_op_ior;
7667 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7668
7669 switch (instr->intrinsic) {
7670 case nir_intrinsic_reduce:
7671 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7672 break;
7673 case nir_intrinsic_exclusive_scan:
7674 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7675 break;
7676 case nir_intrinsic_inclusive_scan:
7677 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7678 break;
7679 default:
7680 assert(false);
7681 }
7682 } else if (cluster_size == 1) {
7683 bld.copy(Definition(dst), src);
7684 } else {
7685 unsigned bit_size = instr->src[0].ssa->bit_size;
7686
7687 src = emit_extract_vector(ctx, src, 0, RegClass::get(RegType::vgpr, bit_size / 8));
7688
7689 ReduceOp reduce_op;
7690 switch (op) {
7691 #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;
7692 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7693 CASEI(iadd)
7694 CASEI(imul)
7695 CASEI(imin)
7696 CASEI(umin)
7697 CASEI(imax)
7698 CASEI(umax)
7699 CASEI(iand)
7700 CASEI(ior)
7701 CASEI(ixor)
7702 CASEF(fadd)
7703 CASEF(fmul)
7704 CASEF(fmin)
7705 CASEF(fmax)
7706 default:
7707 unreachable("unknown reduction op");
7708 #undef CASEI
7709 #undef CASEF
7710 }
7711
7712 aco_opcode aco_op;
7713 switch (instr->intrinsic) {
7714 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7715 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7716 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7717 default:
7718 unreachable("unknown reduce intrinsic");
7719 }
7720
7721 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7722 reduce->operands[0] = Operand(src);
7723 // filled in by aco_reduce_assign.cpp, used internally as part of the
7724 // reduce sequence
7725 assert(dst.size() == 1 || dst.size() == 2);
7726 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7727 reduce->operands[2] = Operand(v1.as_linear());
7728
7729 Temp tmp_dst = bld.tmp(dst.regClass());
7730 reduce->definitions[0] = Definition(tmp_dst);
7731 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7732 reduce->definitions[2] = Definition();
7733 reduce->definitions[3] = Definition(scc, s1);
7734 reduce->definitions[4] = Definition();
7735 reduce->reduce_op = reduce_op;
7736 reduce->cluster_size = cluster_size;
7737 ctx->block->instructions.emplace_back(std::move(reduce));
7738
7739 emit_wqm(ctx, tmp_dst, dst);
7740 }
7741 break;
7742 }
7743 case nir_intrinsic_quad_broadcast: {
7744 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7745 if (!nir_dest_is_divergent(instr->dest)) {
7746 emit_uniform_subgroup(ctx, instr, src);
7747 } else {
7748 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7749 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7750 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7751
7752 if (instr->dest.ssa.bit_size == 1) {
7753 assert(src.regClass() == bld.lm);
7754 assert(dst.regClass() == bld.lm);
7755 uint32_t half_mask = 0x11111111u << lane;
7756 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7757 Temp tmp = bld.tmp(bld.lm);
7758 bld.sop1(Builder::s_wqm, Definition(tmp),
7759 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7760 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7761 emit_wqm(ctx, tmp, dst);
7762 } else if (instr->dest.ssa.bit_size == 8) {
7763 Temp tmp = bld.tmp(v1);
7764 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7765 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7766 } else if (instr->dest.ssa.bit_size == 16) {
7767 Temp tmp = bld.tmp(v1);
7768 if (ctx->program->chip_class >= GFX8)
7769 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7770 else
7771 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), tmp);
7772 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7773 } else if (instr->dest.ssa.bit_size == 32) {
7774 if (ctx->program->chip_class >= GFX8)
7775 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7776 else
7777 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7778 } else if (instr->dest.ssa.bit_size == 64) {
7779 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7780 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7781 if (ctx->program->chip_class >= GFX8) {
7782 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7783 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7784 } else {
7785 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7786 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7787 }
7788 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7789 emit_split_vector(ctx, dst, 2);
7790 } else {
7791 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7792 nir_print_instr(&instr->instr, stderr);
7793 fprintf(stderr, "\n");
7794 }
7795 }
7796 break;
7797 }
7798 case nir_intrinsic_quad_swap_horizontal:
7799 case nir_intrinsic_quad_swap_vertical:
7800 case nir_intrinsic_quad_swap_diagonal:
7801 case nir_intrinsic_quad_swizzle_amd: {
7802 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7803 if (!nir_dest_is_divergent(instr->dest)) {
7804 emit_uniform_subgroup(ctx, instr, src);
7805 break;
7806 }
7807 uint16_t dpp_ctrl = 0;
7808 switch (instr->intrinsic) {
7809 case nir_intrinsic_quad_swap_horizontal:
7810 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7811 break;
7812 case nir_intrinsic_quad_swap_vertical:
7813 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7814 break;
7815 case nir_intrinsic_quad_swap_diagonal:
7816 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7817 break;
7818 case nir_intrinsic_quad_swizzle_amd:
7819 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7820 break;
7821 default:
7822 break;
7823 }
7824 if (ctx->program->chip_class < GFX8)
7825 dpp_ctrl |= (1 << 15);
7826
7827 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7828 if (instr->dest.ssa.bit_size == 1) {
7829 assert(src.regClass() == bld.lm);
7830 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7831 if (ctx->program->chip_class >= GFX8)
7832 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7833 else
7834 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7835 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7836 emit_wqm(ctx, tmp, dst);
7837 } else if (instr->dest.ssa.bit_size == 8) {
7838 Temp tmp = bld.tmp(v1);
7839 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), tmp);
7840 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v3b), tmp);
7841 } else if (instr->dest.ssa.bit_size == 16) {
7842 Temp tmp = bld.tmp(v1);
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), tmp);
7845 else
7846 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl), tmp);
7847 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
7848 } else if (instr->dest.ssa.bit_size == 32) {
7849 Temp tmp;
7850 if (ctx->program->chip_class >= GFX8)
7851 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7852 else
7853 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7854 emit_wqm(ctx, tmp, dst);
7855 } else if (instr->dest.ssa.bit_size == 64) {
7856 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7857 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7858 if (ctx->program->chip_class >= GFX8) {
7859 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7860 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7861 } else {
7862 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7863 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7864 }
7865 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7866 emit_split_vector(ctx, dst, 2);
7867 } else {
7868 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7869 nir_print_instr(&instr->instr, stderr);
7870 fprintf(stderr, "\n");
7871 }
7872 break;
7873 }
7874 case nir_intrinsic_masked_swizzle_amd: {
7875 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7876 if (!nir_dest_is_divergent(instr->dest)) {
7877 emit_uniform_subgroup(ctx, instr, src);
7878 break;
7879 }
7880 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7881 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7882 if (dst.regClass() == v1) {
7883 emit_wqm(ctx,
7884 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7885 dst);
7886 } else if (dst.regClass() == v2) {
7887 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7888 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7889 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7890 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7891 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7892 emit_split_vector(ctx, dst, 2);
7893 } else {
7894 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7895 nir_print_instr(&instr->instr, stderr);
7896 fprintf(stderr, "\n");
7897 }
7898 break;
7899 }
7900 case nir_intrinsic_write_invocation_amd: {
7901 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7902 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7903 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7904 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7905 if (dst.regClass() == v1) {
7906 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7907 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7908 } else if (dst.regClass() == v2) {
7909 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7910 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7911 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7912 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7913 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7914 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7915 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7916 emit_split_vector(ctx, dst, 2);
7917 } else {
7918 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7919 nir_print_instr(&instr->instr, stderr);
7920 fprintf(stderr, "\n");
7921 }
7922 break;
7923 }
7924 case nir_intrinsic_mbcnt_amd: {
7925 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7926 RegClass rc = RegClass(src.type(), 1);
7927 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7928 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7929 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7930 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7931 emit_wqm(ctx, wqm_tmp, dst);
7932 break;
7933 }
7934 case nir_intrinsic_load_helper_invocation: {
7935 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7936 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7937 ctx->block->kind |= block_kind_needs_lowering;
7938 ctx->program->needs_exact = true;
7939 break;
7940 }
7941 case nir_intrinsic_is_helper_invocation: {
7942 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7943 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7944 ctx->block->kind |= block_kind_needs_lowering;
7945 ctx->program->needs_exact = true;
7946 break;
7947 }
7948 case nir_intrinsic_demote:
7949 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7950
7951 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7952 ctx->cf_info.exec_potentially_empty_discard = true;
7953 ctx->block->kind |= block_kind_uses_demote;
7954 ctx->program->needs_exact = true;
7955 break;
7956 case nir_intrinsic_demote_if: {
7957 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7958 assert(src.regClass() == bld.lm);
7959 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7960 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7961
7962 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7963 ctx->cf_info.exec_potentially_empty_discard = true;
7964 ctx->block->kind |= block_kind_uses_demote;
7965 ctx->program->needs_exact = true;
7966 break;
7967 }
7968 case nir_intrinsic_first_invocation: {
7969 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7970 get_ssa_temp(ctx, &instr->dest.ssa));
7971 break;
7972 }
7973 case nir_intrinsic_shader_clock: {
7974 aco_opcode opcode =
7975 nir_intrinsic_memory_scope(instr) == NIR_SCOPE_DEVICE ?
7976 aco_opcode::s_memrealtime : aco_opcode::s_memtime;
7977 bld.smem(opcode, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7978 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7979 break;
7980 }
7981 case nir_intrinsic_load_vertex_id_zero_base: {
7982 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7983 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7984 break;
7985 }
7986 case nir_intrinsic_load_first_vertex: {
7987 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7988 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7989 break;
7990 }
7991 case nir_intrinsic_load_base_instance: {
7992 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7993 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7994 break;
7995 }
7996 case nir_intrinsic_load_instance_id: {
7997 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7998 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7999 break;
8000 }
8001 case nir_intrinsic_load_draw_id: {
8002 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8003 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
8004 break;
8005 }
8006 case nir_intrinsic_load_invocation_id: {
8007 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8008
8009 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
8010 if (ctx->options->chip_class >= GFX10)
8011 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8012 else
8013 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
8014 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
8015 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
8016 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
8017 } else {
8018 unreachable("Unsupported stage for load_invocation_id");
8019 }
8020
8021 break;
8022 }
8023 case nir_intrinsic_load_primitive_id: {
8024 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8025
8026 switch (ctx->shader->info.stage) {
8027 case MESA_SHADER_GEOMETRY:
8028 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
8029 break;
8030 case MESA_SHADER_TESS_CTRL:
8031 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
8032 break;
8033 case MESA_SHADER_TESS_EVAL:
8034 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
8035 break;
8036 default:
8037 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8038 }
8039
8040 break;
8041 }
8042 case nir_intrinsic_load_patch_vertices_in: {
8043 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
8044 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
8045
8046 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8047 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
8048 break;
8049 }
8050 case nir_intrinsic_emit_vertex_with_counter: {
8051 visit_emit_vertex_with_counter(ctx, instr);
8052 break;
8053 }
8054 case nir_intrinsic_end_primitive_with_counter: {
8055 unsigned stream = nir_intrinsic_stream_id(instr);
8056 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
8057 break;
8058 }
8059 case nir_intrinsic_set_vertex_count: {
8060 /* unused, the HW keeps track of this for us */
8061 break;
8062 }
8063 default:
8064 fprintf(stderr, "Unimplemented intrinsic instr: ");
8065 nir_print_instr(&instr->instr, stderr);
8066 fprintf(stderr, "\n");
8067 abort();
8068
8069 break;
8070 }
8071 }
8072
8073
8074 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
8075 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
8076 enum glsl_base_type *stype)
8077 {
8078 nir_deref_instr *texture_deref_instr = NULL;
8079 nir_deref_instr *sampler_deref_instr = NULL;
8080 int plane = -1;
8081
8082 for (unsigned i = 0; i < instr->num_srcs; i++) {
8083 switch (instr->src[i].src_type) {
8084 case nir_tex_src_texture_deref:
8085 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
8086 break;
8087 case nir_tex_src_sampler_deref:
8088 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
8089 break;
8090 case nir_tex_src_plane:
8091 plane = nir_src_as_int(instr->src[i].src);
8092 break;
8093 default:
8094 break;
8095 }
8096 }
8097
8098 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
8099
8100 if (!sampler_deref_instr)
8101 sampler_deref_instr = texture_deref_instr;
8102
8103 if (plane >= 0) {
8104 assert(instr->op != nir_texop_txf_ms &&
8105 instr->op != nir_texop_samples_identical);
8106 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
8107 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
8108 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8109 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8110 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8111 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8112 } else {
8113 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8114 }
8115 if (samp_ptr) {
8116 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8117
8118 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8119 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8120 Builder bld(ctx->program, ctx->block);
8121
8122 /* to avoid unnecessary moves, we split and recombine sampler and image */
8123 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8124 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8125 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8126 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8127 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8128 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8129 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8130 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8131
8132 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8133 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8134 img[0], img[1], img[2], img[3],
8135 img[4], img[5], img[6], img[7]);
8136 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8137 samp[0], samp[1], samp[2], samp[3]);
8138 }
8139 }
8140 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8141 instr->op == nir_texop_samples_identical))
8142 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8143 }
8144
8145 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8146 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8147 {
8148 Builder bld(ctx->program, ctx->block);
8149
8150 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8151 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8152 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8153
8154 Operand neg_one(0xbf800000u);
8155 Operand one(0x3f800000u);
8156 Operand two(0x40000000u);
8157 Operand four(0x40800000u);
8158
8159 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8160 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8161 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8162
8163 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8164 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8165 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8166 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);
8167
8168 // select sc
8169 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8170 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8171 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8172 one, is_ma_y);
8173 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8174
8175 // select tc
8176 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8177 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8178 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8179
8180 // select ma
8181 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8182 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8183 deriv_z, is_ma_z);
8184 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8185 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8186 }
8187
8188 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8189 {
8190 Builder bld(ctx->program, ctx->block);
8191 Temp ma, tc, sc, id;
8192
8193 if (is_array) {
8194 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8195
8196 // see comment in ac_prepare_cube_coords()
8197 if (ctx->options->chip_class <= GFX8)
8198 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8199 }
8200
8201 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8202
8203 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8204 vop3a->operands[0] = Operand(ma);
8205 vop3a->abs[0] = true;
8206 Temp invma = bld.tmp(v1);
8207 vop3a->definitions[0] = Definition(invma);
8208 ctx->block->instructions.emplace_back(std::move(vop3a));
8209
8210 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8211 if (!is_deriv)
8212 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8213
8214 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8215 if (!is_deriv)
8216 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8217
8218 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8219
8220 if (is_deriv) {
8221 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8222 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8223
8224 for (unsigned i = 0; i < 2; i++) {
8225 // see comment in ac_prepare_cube_coords()
8226 Temp deriv_ma;
8227 Temp deriv_sc, deriv_tc;
8228 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8229 &deriv_ma, &deriv_sc, &deriv_tc);
8230
8231 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8232
8233 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8234 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8235 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8236 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8237 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8238 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8239 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8240 }
8241
8242 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8243 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8244 }
8245
8246 if (is_array)
8247 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8248 coords.resize(3);
8249 coords[0] = sc;
8250 coords[1] = tc;
8251 coords[2] = id;
8252 }
8253
8254 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8255 {
8256 if (vec->parent_instr->type != nir_instr_type_alu)
8257 return;
8258 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8259 if (vec_instr->op != nir_op_vec(vec->num_components))
8260 return;
8261
8262 for (unsigned i = 0; i < vec->num_components; i++) {
8263 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8264 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8265 }
8266 }
8267
8268 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8269 {
8270 Builder bld(ctx->program, ctx->block);
8271 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8272 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false,
8273 has_clamped_lod = false;
8274 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8275 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp(),
8276 clamped_lod = Temp();
8277 std::vector<Temp> coords;
8278 std::vector<Temp> derivs;
8279 nir_const_value *sample_index_cv = NULL;
8280 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8281 enum glsl_base_type stype;
8282 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8283
8284 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8285 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8286 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8287 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8288
8289 for (unsigned i = 0; i < instr->num_srcs; i++) {
8290 switch (instr->src[i].src_type) {
8291 case nir_tex_src_coord: {
8292 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8293 for (unsigned i = 0; i < coord.size(); i++)
8294 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8295 break;
8296 }
8297 case nir_tex_src_bias:
8298 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8299 has_bias = true;
8300 break;
8301 case nir_tex_src_lod: {
8302 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8303
8304 if (val && val->f32 <= 0.0) {
8305 level_zero = true;
8306 } else {
8307 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8308 has_lod = true;
8309 }
8310 break;
8311 }
8312 case nir_tex_src_min_lod:
8313 clamped_lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8314 has_clamped_lod = true;
8315 break;
8316 case nir_tex_src_comparator:
8317 if (instr->is_shadow) {
8318 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8319 has_compare = true;
8320 }
8321 break;
8322 case nir_tex_src_offset:
8323 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8324 get_const_vec(instr->src[i].src.ssa, const_offset);
8325 has_offset = true;
8326 break;
8327 case nir_tex_src_ddx:
8328 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8329 has_ddx = true;
8330 break;
8331 case nir_tex_src_ddy:
8332 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8333 has_ddy = true;
8334 break;
8335 case nir_tex_src_ms_index:
8336 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8337 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8338 has_sample_index = true;
8339 break;
8340 case nir_tex_src_texture_offset:
8341 case nir_tex_src_sampler_offset:
8342 default:
8343 break;
8344 }
8345 }
8346
8347 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8348 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8349
8350 if (instr->op == nir_texop_texture_samples) {
8351 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8352
8353 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8354 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8355 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 */));
8356
8357 Operand default_sample = Operand(1u);
8358 if (ctx->options->robust_buffer_access) {
8359 /* Extract the second dword of the descriptor, if it's
8360 * all zero, then it's a null descriptor.
8361 */
8362 Temp dword1 = emit_extract_vector(ctx, resource, 1, s1);
8363 Temp is_non_null_descriptor = bld.sopc(aco_opcode::s_cmp_gt_u32, bld.def(s1, scc), dword1, Operand(0u));
8364 default_sample = Operand(is_non_null_descriptor);
8365 }
8366
8367 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8368 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8369 samples, default_sample, bld.scc(is_msaa));
8370 return;
8371 }
8372
8373 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8374 aco_ptr<Instruction> tmp_instr;
8375 Temp acc, pack = Temp();
8376
8377 uint32_t pack_const = 0;
8378 for (unsigned i = 0; i < offset.size(); i++) {
8379 if (!const_offset[i])
8380 continue;
8381 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8382 }
8383
8384 if (offset.type() == RegType::sgpr) {
8385 for (unsigned i = 0; i < offset.size(); i++) {
8386 if (const_offset[i])
8387 continue;
8388
8389 acc = emit_extract_vector(ctx, offset, i, s1);
8390 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8391
8392 if (i) {
8393 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8394 }
8395
8396 if (pack == Temp()) {
8397 pack = acc;
8398 } else {
8399 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8400 }
8401 }
8402
8403 if (pack_const && pack != Temp())
8404 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8405 } else {
8406 for (unsigned i = 0; i < offset.size(); i++) {
8407 if (const_offset[i])
8408 continue;
8409
8410 acc = emit_extract_vector(ctx, offset, i, v1);
8411 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8412
8413 if (i) {
8414 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8415 }
8416
8417 if (pack == Temp()) {
8418 pack = acc;
8419 } else {
8420 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8421 }
8422 }
8423
8424 if (pack_const && pack != Temp())
8425 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8426 }
8427 if (pack_const && pack == Temp())
8428 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8429 else if (pack == Temp())
8430 has_offset = false;
8431 else
8432 offset = pack;
8433 }
8434
8435 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8436 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8437
8438 /* pack derivatives */
8439 if (has_ddx || has_ddy) {
8440 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8441 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8442 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8443 derivs = {ddx, zero, ddy, zero};
8444 } else {
8445 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8446 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8447 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8448 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8449 }
8450 has_derivs = true;
8451 }
8452
8453 if (instr->coord_components > 1 &&
8454 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8455 instr->is_array &&
8456 instr->op != nir_texop_txf)
8457 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8458
8459 if (instr->coord_components > 2 &&
8460 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8461 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8462 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8463 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8464 instr->is_array &&
8465 instr->op != nir_texop_txf &&
8466 instr->op != nir_texop_txf_ms &&
8467 instr->op != nir_texop_fragment_fetch &&
8468 instr->op != nir_texop_fragment_mask_fetch)
8469 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8470
8471 if (ctx->options->chip_class == GFX9 &&
8472 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8473 instr->op != nir_texop_lod && instr->coord_components) {
8474 assert(coords.size() > 0 && coords.size() < 3);
8475
8476 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8477 Operand((uint32_t) 0) :
8478 Operand((uint32_t) 0x3f000000)));
8479 }
8480
8481 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8482
8483 if (instr->op == nir_texop_samples_identical)
8484 resource = fmask_ptr;
8485
8486 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8487 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8488 instr->op != nir_texop_txs &&
8489 instr->op != nir_texop_fragment_fetch &&
8490 instr->op != nir_texop_fragment_mask_fetch) {
8491 assert(has_sample_index);
8492 Operand op(sample_index);
8493 if (sample_index_cv)
8494 op = Operand(sample_index_cv->u32);
8495 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8496 }
8497
8498 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8499 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8500 Temp off = emit_extract_vector(ctx, offset, i, v1);
8501 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8502 }
8503 has_offset = false;
8504 }
8505
8506 /* Build tex instruction */
8507 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8508 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8509 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8510 : 0;
8511 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8512 Temp tmp_dst = dst;
8513
8514 /* gather4 selects the component by dmask and always returns vec4 */
8515 if (instr->op == nir_texop_tg4) {
8516 assert(instr->dest.ssa.num_components == 4);
8517 if (instr->is_shadow)
8518 dmask = 1;
8519 else
8520 dmask = 1 << instr->component;
8521 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8522 tmp_dst = bld.tmp(v4);
8523 } else if (instr->op == nir_texop_samples_identical) {
8524 tmp_dst = bld.tmp(v1);
8525 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8526 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8527 }
8528
8529 aco_ptr<MIMG_instruction> tex;
8530 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8531 if (!has_lod)
8532 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8533
8534 bool div_by_6 = instr->op == nir_texop_txs &&
8535 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8536 instr->is_array &&
8537 (dmask & (1 << 2));
8538 if (tmp_dst.id() == dst.id() && div_by_6)
8539 tmp_dst = bld.tmp(tmp_dst.regClass());
8540
8541 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8542 tex->operands[0] = Operand(resource);
8543 tex->operands[1] = Operand(s4); /* no sampler */
8544 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8545 if (ctx->options->chip_class == GFX9 &&
8546 instr->op == nir_texop_txs &&
8547 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8548 instr->is_array) {
8549 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8550 } else if (instr->op == nir_texop_query_levels) {
8551 tex->dmask = 1 << 3;
8552 } else {
8553 tex->dmask = dmask;
8554 }
8555 tex->da = da;
8556 tex->definitions[0] = Definition(tmp_dst);
8557 tex->dim = dim;
8558 tex->can_reorder = true;
8559 ctx->block->instructions.emplace_back(std::move(tex));
8560
8561 if (div_by_6) {
8562 /* divide 3rd value by 6 by multiplying with magic number */
8563 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8564 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8565 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8566 assert(instr->dest.ssa.num_components == 3);
8567 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8568 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8569 emit_extract_vector(ctx, tmp_dst, 0, v1),
8570 emit_extract_vector(ctx, tmp_dst, 1, v1),
8571 by_6);
8572
8573 }
8574
8575 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8576 return;
8577 }
8578
8579 Temp tg4_compare_cube_wa64 = Temp();
8580
8581 if (tg4_integer_workarounds) {
8582 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8583 tex->operands[0] = Operand(resource);
8584 tex->operands[1] = Operand(s4); /* no sampler */
8585 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8586 tex->dim = dim;
8587 tex->dmask = 0x3;
8588 tex->da = da;
8589 Temp size = bld.tmp(v2);
8590 tex->definitions[0] = Definition(size);
8591 tex->can_reorder = true;
8592 ctx->block->instructions.emplace_back(std::move(tex));
8593 emit_split_vector(ctx, size, size.size());
8594
8595 Temp half_texel[2];
8596 for (unsigned i = 0; i < 2; i++) {
8597 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8598 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8599 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8600 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8601 }
8602
8603 Temp new_coords[2] = {
8604 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8605 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8606 };
8607
8608 if (tg4_integer_cube_workaround) {
8609 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8610 Temp desc[resource.size()];
8611 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8612 Format::PSEUDO, 1, resource.size())};
8613 split->operands[0] = Operand(resource);
8614 for (unsigned i = 0; i < resource.size(); i++) {
8615 desc[i] = bld.tmp(s1);
8616 split->definitions[i] = Definition(desc[i]);
8617 }
8618 ctx->block->instructions.emplace_back(std::move(split));
8619
8620 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8621 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8622 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8623
8624 Temp nfmt;
8625 if (stype == GLSL_TYPE_UINT) {
8626 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8627 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8628 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8629 bld.scc(compare_cube_wa));
8630 } else {
8631 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8632 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8633 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8634 bld.scc(compare_cube_wa));
8635 }
8636 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8637 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8638
8639 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8640
8641 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8642 Operand((uint32_t)C_008F14_NUM_FORMAT));
8643 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8644
8645 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8646 Format::PSEUDO, resource.size(), 1)};
8647 for (unsigned i = 0; i < resource.size(); i++)
8648 vec->operands[i] = Operand(desc[i]);
8649 resource = bld.tmp(resource.regClass());
8650 vec->definitions[0] = Definition(resource);
8651 ctx->block->instructions.emplace_back(std::move(vec));
8652
8653 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8654 new_coords[0], coords[0], tg4_compare_cube_wa64);
8655 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8656 new_coords[1], coords[1], tg4_compare_cube_wa64);
8657 }
8658 coords[0] = new_coords[0];
8659 coords[1] = new_coords[1];
8660 }
8661
8662 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8663 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8664
8665 assert(coords.size() == 1);
8666 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8667 aco_opcode op;
8668 switch (last_bit) {
8669 case 1:
8670 op = aco_opcode::buffer_load_format_x; break;
8671 case 2:
8672 op = aco_opcode::buffer_load_format_xy; break;
8673 case 3:
8674 op = aco_opcode::buffer_load_format_xyz; break;
8675 case 4:
8676 op = aco_opcode::buffer_load_format_xyzw; break;
8677 default:
8678 unreachable("Tex instruction loads more than 4 components.");
8679 }
8680
8681 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8682 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8683 tmp_dst = dst;
8684 else
8685 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8686
8687 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8688 mubuf->operands[0] = Operand(resource);
8689 mubuf->operands[1] = Operand(coords[0]);
8690 mubuf->operands[2] = Operand((uint32_t) 0);
8691 mubuf->definitions[0] = Definition(tmp_dst);
8692 mubuf->idxen = true;
8693 mubuf->can_reorder = true;
8694 ctx->block->instructions.emplace_back(std::move(mubuf));
8695
8696 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8697 return;
8698 }
8699
8700 /* gather MIMG address components */
8701 std::vector<Temp> args;
8702 if (has_offset)
8703 args.emplace_back(offset);
8704 if (has_bias)
8705 args.emplace_back(bias);
8706 if (has_compare)
8707 args.emplace_back(compare);
8708 if (has_derivs)
8709 args.insert(args.end(), derivs.begin(), derivs.end());
8710
8711 args.insert(args.end(), coords.begin(), coords.end());
8712 if (has_sample_index)
8713 args.emplace_back(sample_index);
8714 if (has_lod)
8715 args.emplace_back(lod);
8716 if (has_clamped_lod)
8717 args.emplace_back(clamped_lod);
8718
8719 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8720 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8721 vec->definitions[0] = Definition(arg);
8722 for (unsigned i = 0; i < args.size(); i++)
8723 vec->operands[i] = Operand(args[i]);
8724 ctx->block->instructions.emplace_back(std::move(vec));
8725
8726
8727 if (instr->op == nir_texop_txf ||
8728 instr->op == nir_texop_txf_ms ||
8729 instr->op == nir_texop_samples_identical ||
8730 instr->op == nir_texop_fragment_fetch ||
8731 instr->op == nir_texop_fragment_mask_fetch) {
8732 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;
8733 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8734 tex->operands[0] = Operand(resource);
8735 tex->operands[1] = Operand(s4); /* no sampler */
8736 tex->operands[2] = Operand(arg);
8737 tex->dim = dim;
8738 tex->dmask = dmask;
8739 tex->unrm = true;
8740 tex->da = da;
8741 tex->definitions[0] = Definition(tmp_dst);
8742 tex->can_reorder = true;
8743 ctx->block->instructions.emplace_back(std::move(tex));
8744
8745 if (instr->op == nir_texop_samples_identical) {
8746 assert(dmask == 1 && dst.regClass() == v1);
8747 assert(dst.id() != tmp_dst.id());
8748
8749 Temp tmp = bld.tmp(bld.lm);
8750 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8751 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8752
8753 } else {
8754 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8755 }
8756 return;
8757 }
8758
8759 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8760 aco_opcode opcode = aco_opcode::image_sample;
8761 if (has_offset) { /* image_sample_*_o */
8762 if (has_clamped_lod) {
8763 if (has_compare) {
8764 opcode = aco_opcode::image_sample_c_cl_o;
8765 if (has_derivs)
8766 opcode = aco_opcode::image_sample_c_d_cl_o;
8767 if (has_bias)
8768 opcode = aco_opcode::image_sample_c_b_cl_o;
8769 } else {
8770 opcode = aco_opcode::image_sample_cl_o;
8771 if (has_derivs)
8772 opcode = aco_opcode::image_sample_d_cl_o;
8773 if (has_bias)
8774 opcode = aco_opcode::image_sample_b_cl_o;
8775 }
8776 } else if (has_compare) {
8777 opcode = aco_opcode::image_sample_c_o;
8778 if (has_derivs)
8779 opcode = aco_opcode::image_sample_c_d_o;
8780 if (has_bias)
8781 opcode = aco_opcode::image_sample_c_b_o;
8782 if (level_zero)
8783 opcode = aco_opcode::image_sample_c_lz_o;
8784 if (has_lod)
8785 opcode = aco_opcode::image_sample_c_l_o;
8786 } else {
8787 opcode = aco_opcode::image_sample_o;
8788 if (has_derivs)
8789 opcode = aco_opcode::image_sample_d_o;
8790 if (has_bias)
8791 opcode = aco_opcode::image_sample_b_o;
8792 if (level_zero)
8793 opcode = aco_opcode::image_sample_lz_o;
8794 if (has_lod)
8795 opcode = aco_opcode::image_sample_l_o;
8796 }
8797 } else if (has_clamped_lod) { /* image_sample_*_cl */
8798 if (has_compare) {
8799 opcode = aco_opcode::image_sample_c_cl;
8800 if (has_derivs)
8801 opcode = aco_opcode::image_sample_c_d_cl;
8802 if (has_bias)
8803 opcode = aco_opcode::image_sample_c_b_cl;
8804 } else {
8805 opcode = aco_opcode::image_sample_cl;
8806 if (has_derivs)
8807 opcode = aco_opcode::image_sample_d_cl;
8808 if (has_bias)
8809 opcode = aco_opcode::image_sample_b_cl;
8810 }
8811 } else { /* no offset */
8812 if (has_compare) {
8813 opcode = aco_opcode::image_sample_c;
8814 if (has_derivs)
8815 opcode = aco_opcode::image_sample_c_d;
8816 if (has_bias)
8817 opcode = aco_opcode::image_sample_c_b;
8818 if (level_zero)
8819 opcode = aco_opcode::image_sample_c_lz;
8820 if (has_lod)
8821 opcode = aco_opcode::image_sample_c_l;
8822 } else {
8823 opcode = aco_opcode::image_sample;
8824 if (has_derivs)
8825 opcode = aco_opcode::image_sample_d;
8826 if (has_bias)
8827 opcode = aco_opcode::image_sample_b;
8828 if (level_zero)
8829 opcode = aco_opcode::image_sample_lz;
8830 if (has_lod)
8831 opcode = aco_opcode::image_sample_l;
8832 }
8833 }
8834
8835 if (instr->op == nir_texop_tg4) {
8836 if (has_offset) { /* image_gather4_*_o */
8837 if (has_compare) {
8838 opcode = aco_opcode::image_gather4_c_lz_o;
8839 if (has_lod)
8840 opcode = aco_opcode::image_gather4_c_l_o;
8841 if (has_bias)
8842 opcode = aco_opcode::image_gather4_c_b_o;
8843 } else {
8844 opcode = aco_opcode::image_gather4_lz_o;
8845 if (has_lod)
8846 opcode = aco_opcode::image_gather4_l_o;
8847 if (has_bias)
8848 opcode = aco_opcode::image_gather4_b_o;
8849 }
8850 } else {
8851 if (has_compare) {
8852 opcode = aco_opcode::image_gather4_c_lz;
8853 if (has_lod)
8854 opcode = aco_opcode::image_gather4_c_l;
8855 if (has_bias)
8856 opcode = aco_opcode::image_gather4_c_b;
8857 } else {
8858 opcode = aco_opcode::image_gather4_lz;
8859 if (has_lod)
8860 opcode = aco_opcode::image_gather4_l;
8861 if (has_bias)
8862 opcode = aco_opcode::image_gather4_b;
8863 }
8864 }
8865 } else if (instr->op == nir_texop_lod) {
8866 opcode = aco_opcode::image_get_lod;
8867 }
8868
8869 /* we don't need the bias, sample index, compare value or offset to be
8870 * computed in WQM but if the p_create_vector copies the coordinates, then it
8871 * needs to be in WQM */
8872 if (ctx->stage == fragment_fs &&
8873 !has_derivs && !has_lod && !level_zero &&
8874 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8875 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8876 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8877
8878 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8879 tex->operands[0] = Operand(resource);
8880 tex->operands[1] = Operand(sampler);
8881 tex->operands[2] = Operand(arg);
8882 tex->dim = dim;
8883 tex->dmask = dmask;
8884 tex->da = da;
8885 tex->definitions[0] = Definition(tmp_dst);
8886 tex->can_reorder = true;
8887 ctx->block->instructions.emplace_back(std::move(tex));
8888
8889 if (tg4_integer_cube_workaround) {
8890 assert(tmp_dst.id() != dst.id());
8891 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8892
8893 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8894 Temp val[4];
8895 for (unsigned i = 0; i < dst.size(); i++) {
8896 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8897 Temp cvt_val;
8898 if (stype == GLSL_TYPE_UINT)
8899 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8900 else
8901 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8902 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8903 }
8904 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8905 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8906 val[0], val[1], val[2], val[3]);
8907 }
8908 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8909 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8910
8911 }
8912
8913
8914 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8915 {
8916 Temp tmp = get_ssa_temp(ctx, ssa);
8917 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8918 return Operand(tmp.regClass());
8919 else
8920 return Operand(tmp);
8921 }
8922
8923 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8924 {
8925 aco_ptr<Pseudo_instruction> phi;
8926 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8927 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8928
8929 bool logical = !dst.is_linear() || nir_dest_is_divergent(instr->dest);
8930 logical |= ctx->block->kind & block_kind_merge;
8931 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8932
8933 /* we want a sorted list of sources, since the predecessor list is also sorted */
8934 std::map<unsigned, nir_ssa_def*> phi_src;
8935 nir_foreach_phi_src(src, instr)
8936 phi_src[src->pred->index] = src->src.ssa;
8937
8938 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8939 unsigned num_operands = 0;
8940 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8941 unsigned num_defined = 0;
8942 unsigned cur_pred_idx = 0;
8943 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8944 if (cur_pred_idx < preds.size()) {
8945 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8946 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8947 unsigned skipped = 0;
8948 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8949 skipped++;
8950 if (cur_pred_idx + skipped < preds.size()) {
8951 for (unsigned i = 0; i < skipped; i++)
8952 operands[num_operands++] = Operand(dst.regClass());
8953 cur_pred_idx += skipped;
8954 } else {
8955 continue;
8956 }
8957 }
8958 /* Handle missing predecessors at the end. This shouldn't happen with loop
8959 * headers and we can't ignore these sources for loop header phis. */
8960 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8961 continue;
8962 cur_pred_idx++;
8963 Operand op = get_phi_operand(ctx, src.second);
8964 operands[num_operands++] = op;
8965 num_defined += !op.isUndefined();
8966 }
8967 /* handle block_kind_continue_or_break at loop exit blocks */
8968 while (cur_pred_idx++ < preds.size())
8969 operands[num_operands++] = Operand(dst.regClass());
8970
8971 /* If the loop ends with a break, still add a linear continue edge in case
8972 * that break is divergent or continue_or_break is used. We'll either remove
8973 * this operand later in visit_loop() if it's not necessary or replace the
8974 * undef with something correct. */
8975 if (!logical && ctx->block->kind & block_kind_loop_header) {
8976 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8977 nir_block *last = nir_loop_last_block(loop);
8978 if (last->successors[0] != instr->instr.block)
8979 operands[num_operands++] = Operand(RegClass());
8980 }
8981
8982 if (num_defined == 0) {
8983 Builder bld(ctx->program, ctx->block);
8984 if (dst.regClass() == s1) {
8985 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8986 } else if (dst.regClass() == v1) {
8987 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8988 } else {
8989 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8990 for (unsigned i = 0; i < dst.size(); i++)
8991 vec->operands[i] = Operand(0u);
8992 vec->definitions[0] = Definition(dst);
8993 ctx->block->instructions.emplace_back(std::move(vec));
8994 }
8995 return;
8996 }
8997
8998 /* we can use a linear phi in some cases if one src is undef */
8999 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
9000 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
9001
9002 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
9003 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
9004 assert(invert->kind & block_kind_invert);
9005
9006 unsigned then_block = invert->linear_preds[0];
9007
9008 Block* insert_block = NULL;
9009 for (unsigned i = 0; i < num_operands; i++) {
9010 Operand op = operands[i];
9011 if (op.isUndefined())
9012 continue;
9013 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
9014 phi->operands[0] = op;
9015 break;
9016 }
9017 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
9018 phi->operands[1] = Operand(dst.regClass());
9019 phi->definitions[0] = Definition(dst);
9020 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
9021 return;
9022 }
9023
9024 /* try to scalarize vector phis */
9025 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
9026 // TODO: scalarize linear phis on divergent ifs
9027 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
9028 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
9029 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
9030 Operand src = operands[i];
9031 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
9032 can_scalarize = false;
9033 }
9034 if (can_scalarize) {
9035 unsigned num_components = instr->dest.ssa.num_components;
9036 assert(dst.size() % num_components == 0);
9037 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
9038
9039 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
9040 for (unsigned k = 0; k < num_components; k++) {
9041 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9042 for (unsigned i = 0; i < num_operands; i++) {
9043 Operand src = operands[i];
9044 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
9045 }
9046 Temp phi_dst = {ctx->program->allocateId(), rc};
9047 phi->definitions[0] = Definition(phi_dst);
9048 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9049 new_vec[k] = phi_dst;
9050 vec->operands[k] = Operand(phi_dst);
9051 }
9052 vec->definitions[0] = Definition(dst);
9053 ctx->block->instructions.emplace_back(std::move(vec));
9054 ctx->allocated_vec.emplace(dst.id(), new_vec);
9055 return;
9056 }
9057 }
9058
9059 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
9060 for (unsigned i = 0; i < num_operands; i++)
9061 phi->operands[i] = operands[i];
9062 phi->definitions[0] = Definition(dst);
9063 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
9064 }
9065
9066
9067 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
9068 {
9069 Temp dst = get_ssa_temp(ctx, &instr->def);
9070
9071 assert(dst.type() == RegType::sgpr);
9072
9073 if (dst.size() == 1) {
9074 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
9075 } else {
9076 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
9077 for (unsigned i = 0; i < dst.size(); i++)
9078 vec->operands[i] = Operand(0u);
9079 vec->definitions[0] = Definition(dst);
9080 ctx->block->instructions.emplace_back(std::move(vec));
9081 }
9082 }
9083
9084 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
9085 {
9086 Builder bld(ctx->program, ctx->block);
9087 Block *logical_target;
9088 append_logical_end(ctx->block);
9089 unsigned idx = ctx->block->index;
9090
9091 switch (instr->type) {
9092 case nir_jump_break:
9093 logical_target = ctx->cf_info.parent_loop.exit;
9094 add_logical_edge(idx, logical_target);
9095 ctx->block->kind |= block_kind_break;
9096
9097 if (!ctx->cf_info.parent_if.is_divergent &&
9098 !ctx->cf_info.parent_loop.has_divergent_continue) {
9099 /* uniform break - directly jump out of the loop */
9100 ctx->block->kind |= block_kind_uniform;
9101 ctx->cf_info.has_branch = true;
9102 bld.branch(aco_opcode::p_branch);
9103 add_linear_edge(idx, logical_target);
9104 return;
9105 }
9106 ctx->cf_info.parent_loop.has_divergent_branch = true;
9107 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9108 break;
9109 case nir_jump_continue:
9110 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9111 add_logical_edge(idx, logical_target);
9112 ctx->block->kind |= block_kind_continue;
9113
9114 if (ctx->cf_info.parent_if.is_divergent) {
9115 /* for potential uniform breaks after this continue,
9116 we must ensure that they are handled correctly */
9117 ctx->cf_info.parent_loop.has_divergent_continue = true;
9118 ctx->cf_info.parent_loop.has_divergent_branch = true;
9119 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
9120 } else {
9121 /* uniform continue - directly jump to the loop header */
9122 ctx->block->kind |= block_kind_uniform;
9123 ctx->cf_info.has_branch = true;
9124 bld.branch(aco_opcode::p_branch);
9125 add_linear_edge(idx, logical_target);
9126 return;
9127 }
9128 break;
9129 default:
9130 fprintf(stderr, "Unknown NIR jump instr: ");
9131 nir_print_instr(&instr->instr, stderr);
9132 fprintf(stderr, "\n");
9133 abort();
9134 }
9135
9136 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
9137 ctx->cf_info.exec_potentially_empty_break = true;
9138 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
9139 }
9140
9141 /* remove critical edges from linear CFG */
9142 bld.branch(aco_opcode::p_branch);
9143 Block* break_block = ctx->program->create_and_insert_block();
9144 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9145 break_block->kind |= block_kind_uniform;
9146 add_linear_edge(idx, break_block);
9147 /* the loop_header pointer might be invalidated by this point */
9148 if (instr->type == nir_jump_continue)
9149 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
9150 add_linear_edge(break_block->index, logical_target);
9151 bld.reset(break_block);
9152 bld.branch(aco_opcode::p_branch);
9153
9154 Block* continue_block = ctx->program->create_and_insert_block();
9155 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9156 add_linear_edge(idx, continue_block);
9157 append_logical_start(continue_block);
9158 ctx->block = continue_block;
9159 return;
9160 }
9161
9162 void visit_block(isel_context *ctx, nir_block *block)
9163 {
9164 nir_foreach_instr(instr, block) {
9165 switch (instr->type) {
9166 case nir_instr_type_alu:
9167 visit_alu_instr(ctx, nir_instr_as_alu(instr));
9168 break;
9169 case nir_instr_type_load_const:
9170 visit_load_const(ctx, nir_instr_as_load_const(instr));
9171 break;
9172 case nir_instr_type_intrinsic:
9173 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9174 break;
9175 case nir_instr_type_tex:
9176 visit_tex(ctx, nir_instr_as_tex(instr));
9177 break;
9178 case nir_instr_type_phi:
9179 visit_phi(ctx, nir_instr_as_phi(instr));
9180 break;
9181 case nir_instr_type_ssa_undef:
9182 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9183 break;
9184 case nir_instr_type_deref:
9185 break;
9186 case nir_instr_type_jump:
9187 visit_jump(ctx, nir_instr_as_jump(instr));
9188 break;
9189 default:
9190 fprintf(stderr, "Unknown NIR instr type: ");
9191 nir_print_instr(instr, stderr);
9192 fprintf(stderr, "\n");
9193 //abort();
9194 }
9195 }
9196
9197 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9198 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9199 }
9200
9201
9202
9203 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9204 aco_ptr<Instruction>& header_phi, Operand *vals)
9205 {
9206 vals[0] = Operand(header_phi->definitions[0].getTemp());
9207 RegClass rc = vals[0].regClass();
9208
9209 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9210
9211 unsigned next_pred = 1;
9212
9213 for (unsigned idx = first + 1; idx <= last; idx++) {
9214 Block& block = ctx->program->blocks[idx];
9215 if (block.loop_nest_depth != loop_nest_depth) {
9216 vals[idx - first] = vals[idx - 1 - first];
9217 continue;
9218 }
9219
9220 if (block.kind & block_kind_continue) {
9221 vals[idx - first] = header_phi->operands[next_pred];
9222 next_pred++;
9223 continue;
9224 }
9225
9226 bool all_same = true;
9227 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9228 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9229
9230 Operand val;
9231 if (all_same) {
9232 val = vals[block.linear_preds[0] - first];
9233 } else {
9234 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9235 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9236 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9237 phi->operands[i] = vals[block.linear_preds[i] - first];
9238 val = Operand(Temp(ctx->program->allocateId(), rc));
9239 phi->definitions[0] = Definition(val.getTemp());
9240 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9241 }
9242 vals[idx - first] = val;
9243 }
9244
9245 return vals[last - first];
9246 }
9247
9248 static void visit_loop(isel_context *ctx, nir_loop *loop)
9249 {
9250 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9251 append_logical_end(ctx->block);
9252 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9253 Builder bld(ctx->program, ctx->block);
9254 bld.branch(aco_opcode::p_branch);
9255 unsigned loop_preheader_idx = ctx->block->index;
9256
9257 Block loop_exit = Block();
9258 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9259 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9260
9261 Block* loop_header = ctx->program->create_and_insert_block();
9262 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9263 loop_header->kind |= block_kind_loop_header;
9264 add_edge(loop_preheader_idx, loop_header);
9265 ctx->block = loop_header;
9266
9267 /* emit loop body */
9268 unsigned loop_header_idx = loop_header->index;
9269 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9270 append_logical_start(ctx->block);
9271 bool unreachable = visit_cf_list(ctx, &loop->body);
9272
9273 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9274 if (!ctx->cf_info.has_branch) {
9275 append_logical_end(ctx->block);
9276 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9277 /* Discards can result in code running with an empty exec mask.
9278 * This would result in divergent breaks not ever being taken. As a
9279 * workaround, break the loop when the loop mask is empty instead of
9280 * always continuing. */
9281 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9282 unsigned block_idx = ctx->block->index;
9283
9284 /* create helper blocks to avoid critical edges */
9285 Block *break_block = ctx->program->create_and_insert_block();
9286 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9287 break_block->kind = block_kind_uniform;
9288 bld.reset(break_block);
9289 bld.branch(aco_opcode::p_branch);
9290 add_linear_edge(block_idx, break_block);
9291 add_linear_edge(break_block->index, &loop_exit);
9292
9293 Block *continue_block = ctx->program->create_and_insert_block();
9294 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9295 continue_block->kind = block_kind_uniform;
9296 bld.reset(continue_block);
9297 bld.branch(aco_opcode::p_branch);
9298 add_linear_edge(block_idx, continue_block);
9299 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9300
9301 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9302 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9303 ctx->block = &ctx->program->blocks[block_idx];
9304 } else {
9305 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9306 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9307 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9308 else
9309 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9310 }
9311
9312 bld.reset(ctx->block);
9313 bld.branch(aco_opcode::p_branch);
9314 }
9315
9316 /* Fixup phis in loop header from unreachable blocks.
9317 * has_branch/has_divergent_branch also indicates if the loop ends with a
9318 * break/continue instruction, but we don't emit those if unreachable=true */
9319 if (unreachable) {
9320 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9321 bool linear = ctx->cf_info.has_branch;
9322 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9323 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9324 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9325 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9326 /* the last operand should be the one that needs to be removed */
9327 instr->operands.pop_back();
9328 } else if (!is_phi(instr)) {
9329 break;
9330 }
9331 }
9332 }
9333
9334 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9335 * and the previous one shouldn't both happen at once because a break in the
9336 * merge block would get CSE'd */
9337 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9338 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9339 Operand vals[num_vals];
9340 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9341 if (instr->opcode == aco_opcode::p_linear_phi) {
9342 if (ctx->cf_info.has_branch)
9343 instr->operands.pop_back();
9344 else
9345 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9346 } else if (!is_phi(instr)) {
9347 break;
9348 }
9349 }
9350 }
9351
9352 ctx->cf_info.has_branch = false;
9353
9354 // TODO: if the loop has not a single exit, we must add one °°
9355 /* emit loop successor block */
9356 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9357 append_logical_start(ctx->block);
9358
9359 #if 0
9360 // TODO: check if it is beneficial to not branch on continues
9361 /* trim linear phis in loop header */
9362 for (auto&& instr : loop_entry->instructions) {
9363 if (instr->opcode == aco_opcode::p_linear_phi) {
9364 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9365 new_phi->definitions[0] = instr->definitions[0];
9366 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9367 new_phi->operands[i] = instr->operands[i];
9368 /* check that the remaining operands are all the same */
9369 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9370 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9371 instr.swap(new_phi);
9372 } else if (instr->opcode == aco_opcode::p_phi) {
9373 continue;
9374 } else {
9375 break;
9376 }
9377 }
9378 #endif
9379 }
9380
9381 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9382 {
9383 ic->cond = cond;
9384
9385 append_logical_end(ctx->block);
9386 ctx->block->kind |= block_kind_branch;
9387
9388 /* branch to linear then block */
9389 assert(cond.regClass() == ctx->program->lane_mask);
9390 aco_ptr<Pseudo_branch_instruction> branch;
9391 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9392 branch->operands[0] = Operand(cond);
9393 ctx->block->instructions.push_back(std::move(branch));
9394
9395 ic->BB_if_idx = ctx->block->index;
9396 ic->BB_invert = Block();
9397 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9398 /* Invert blocks are intentionally not marked as top level because they
9399 * are not part of the logical cfg. */
9400 ic->BB_invert.kind |= block_kind_invert;
9401 ic->BB_endif = Block();
9402 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9403 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9404
9405 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9406 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9407 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9408 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9409 ctx->cf_info.parent_if.is_divergent = true;
9410
9411 /* divergent branches use cbranch_execz */
9412 ctx->cf_info.exec_potentially_empty_discard = false;
9413 ctx->cf_info.exec_potentially_empty_break = false;
9414 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9415
9416 /** emit logical then block */
9417 Block* BB_then_logical = ctx->program->create_and_insert_block();
9418 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9419 add_edge(ic->BB_if_idx, BB_then_logical);
9420 ctx->block = BB_then_logical;
9421 append_logical_start(BB_then_logical);
9422 }
9423
9424 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9425 {
9426 Block *BB_then_logical = ctx->block;
9427 append_logical_end(BB_then_logical);
9428 /* branch from logical then block to invert block */
9429 aco_ptr<Pseudo_branch_instruction> branch;
9430 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9431 BB_then_logical->instructions.emplace_back(std::move(branch));
9432 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9433 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9434 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9435 BB_then_logical->kind |= block_kind_uniform;
9436 assert(!ctx->cf_info.has_branch);
9437 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9438 ctx->cf_info.parent_loop.has_divergent_branch = false;
9439
9440 /** emit linear then block */
9441 Block* BB_then_linear = ctx->program->create_and_insert_block();
9442 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9443 BB_then_linear->kind |= block_kind_uniform;
9444 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9445 /* branch from linear then block to invert block */
9446 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9447 BB_then_linear->instructions.emplace_back(std::move(branch));
9448 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9449
9450 /** emit invert merge block */
9451 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9452 ic->invert_idx = ctx->block->index;
9453
9454 /* branch to linear else block (skip else) */
9455 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9456 branch->operands[0] = Operand(ic->cond);
9457 ctx->block->instructions.push_back(std::move(branch));
9458
9459 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9460 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9461 ic->exec_potentially_empty_break_depth_old =
9462 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9463 /* divergent branches use cbranch_execz */
9464 ctx->cf_info.exec_potentially_empty_discard = false;
9465 ctx->cf_info.exec_potentially_empty_break = false;
9466 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9467
9468 /** emit logical else block */
9469 Block* BB_else_logical = ctx->program->create_and_insert_block();
9470 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9471 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9472 add_linear_edge(ic->invert_idx, BB_else_logical);
9473 ctx->block = BB_else_logical;
9474 append_logical_start(BB_else_logical);
9475 }
9476
9477 static void end_divergent_if(isel_context *ctx, if_context *ic)
9478 {
9479 Block *BB_else_logical = ctx->block;
9480 append_logical_end(BB_else_logical);
9481
9482 /* branch from logical else block to endif block */
9483 aco_ptr<Pseudo_branch_instruction> branch;
9484 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9485 BB_else_logical->instructions.emplace_back(std::move(branch));
9486 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9487 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9488 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9489 BB_else_logical->kind |= block_kind_uniform;
9490
9491 assert(!ctx->cf_info.has_branch);
9492 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9493
9494
9495 /** emit linear else block */
9496 Block* BB_else_linear = ctx->program->create_and_insert_block();
9497 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9498 BB_else_linear->kind |= block_kind_uniform;
9499 add_linear_edge(ic->invert_idx, BB_else_linear);
9500
9501 /* branch from linear else block to endif block */
9502 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9503 BB_else_linear->instructions.emplace_back(std::move(branch));
9504 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9505
9506
9507 /** emit endif merge block */
9508 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9509 append_logical_start(ctx->block);
9510
9511
9512 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9513 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9514 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9515 ctx->cf_info.exec_potentially_empty_break_depth =
9516 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9517 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9518 !ctx->cf_info.parent_if.is_divergent) {
9519 ctx->cf_info.exec_potentially_empty_break = false;
9520 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9521 }
9522 /* uniform control flow never has an empty exec-mask */
9523 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9524 ctx->cf_info.exec_potentially_empty_discard = false;
9525 ctx->cf_info.exec_potentially_empty_break = false;
9526 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9527 }
9528 }
9529
9530 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9531 {
9532 assert(cond.regClass() == s1);
9533
9534 append_logical_end(ctx->block);
9535 ctx->block->kind |= block_kind_uniform;
9536
9537 aco_ptr<Pseudo_branch_instruction> branch;
9538 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9539 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9540 branch->operands[0] = Operand(cond);
9541 branch->operands[0].setFixed(scc);
9542 ctx->block->instructions.emplace_back(std::move(branch));
9543
9544 ic->BB_if_idx = ctx->block->index;
9545 ic->BB_endif = Block();
9546 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9547 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9548
9549 ctx->cf_info.has_branch = false;
9550 ctx->cf_info.parent_loop.has_divergent_branch = false;
9551
9552 /** emit then block */
9553 Block* BB_then = ctx->program->create_and_insert_block();
9554 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9555 add_edge(ic->BB_if_idx, BB_then);
9556 append_logical_start(BB_then);
9557 ctx->block = BB_then;
9558 }
9559
9560 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9561 {
9562 Block *BB_then = ctx->block;
9563
9564 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9565 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9566
9567 if (!ic->uniform_has_then_branch) {
9568 append_logical_end(BB_then);
9569 /* branch from then block to endif block */
9570 aco_ptr<Pseudo_branch_instruction> branch;
9571 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9572 BB_then->instructions.emplace_back(std::move(branch));
9573 add_linear_edge(BB_then->index, &ic->BB_endif);
9574 if (!ic->then_branch_divergent)
9575 add_logical_edge(BB_then->index, &ic->BB_endif);
9576 BB_then->kind |= block_kind_uniform;
9577 }
9578
9579 ctx->cf_info.has_branch = false;
9580 ctx->cf_info.parent_loop.has_divergent_branch = false;
9581
9582 /** emit else block */
9583 Block* BB_else = ctx->program->create_and_insert_block();
9584 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9585 add_edge(ic->BB_if_idx, BB_else);
9586 append_logical_start(BB_else);
9587 ctx->block = BB_else;
9588 }
9589
9590 static void end_uniform_if(isel_context *ctx, if_context *ic)
9591 {
9592 Block *BB_else = ctx->block;
9593
9594 if (!ctx->cf_info.has_branch) {
9595 append_logical_end(BB_else);
9596 /* branch from then block to endif block */
9597 aco_ptr<Pseudo_branch_instruction> branch;
9598 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9599 BB_else->instructions.emplace_back(std::move(branch));
9600 add_linear_edge(BB_else->index, &ic->BB_endif);
9601 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9602 add_logical_edge(BB_else->index, &ic->BB_endif);
9603 BB_else->kind |= block_kind_uniform;
9604 }
9605
9606 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9607 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9608
9609 /** emit endif merge block */
9610 if (!ctx->cf_info.has_branch) {
9611 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9612 append_logical_start(ctx->block);
9613 }
9614 }
9615
9616 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9617 {
9618 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9619 Builder bld(ctx->program, ctx->block);
9620 aco_ptr<Pseudo_branch_instruction> branch;
9621 if_context ic;
9622
9623 if (!nir_src_is_divergent(if_stmt->condition)) { /* uniform condition */
9624 /**
9625 * Uniform conditionals are represented in the following way*) :
9626 *
9627 * The linear and logical CFG:
9628 * BB_IF
9629 * / \
9630 * BB_THEN (logical) BB_ELSE (logical)
9631 * \ /
9632 * BB_ENDIF
9633 *
9634 * *) Exceptions may be due to break and continue statements within loops
9635 * If a break/continue happens within uniform control flow, it branches
9636 * to the loop exit/entry block. Otherwise, it branches to the next
9637 * merge block.
9638 **/
9639
9640 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9641 assert(cond.regClass() == ctx->program->lane_mask);
9642 cond = bool_to_scalar_condition(ctx, cond);
9643
9644 begin_uniform_if_then(ctx, &ic, cond);
9645 visit_cf_list(ctx, &if_stmt->then_list);
9646
9647 begin_uniform_if_else(ctx, &ic);
9648 visit_cf_list(ctx, &if_stmt->else_list);
9649
9650 end_uniform_if(ctx, &ic);
9651 } else { /* non-uniform condition */
9652 /**
9653 * To maintain a logical and linear CFG without critical edges,
9654 * non-uniform conditionals are represented in the following way*) :
9655 *
9656 * The linear CFG:
9657 * BB_IF
9658 * / \
9659 * BB_THEN (logical) BB_THEN (linear)
9660 * \ /
9661 * BB_INVERT (linear)
9662 * / \
9663 * BB_ELSE (logical) BB_ELSE (linear)
9664 * \ /
9665 * BB_ENDIF
9666 *
9667 * The logical CFG:
9668 * BB_IF
9669 * / \
9670 * BB_THEN (logical) BB_ELSE (logical)
9671 * \ /
9672 * BB_ENDIF
9673 *
9674 * *) Exceptions may be due to break and continue statements within loops
9675 **/
9676
9677 begin_divergent_if_then(ctx, &ic, cond);
9678 visit_cf_list(ctx, &if_stmt->then_list);
9679
9680 begin_divergent_if_else(ctx, &ic);
9681 visit_cf_list(ctx, &if_stmt->else_list);
9682
9683 end_divergent_if(ctx, &ic);
9684 }
9685
9686 return !ctx->cf_info.has_branch && !ctx->block->logical_preds.empty();
9687 }
9688
9689 static bool visit_cf_list(isel_context *ctx,
9690 struct exec_list *list)
9691 {
9692 foreach_list_typed(nir_cf_node, node, node, list) {
9693 switch (node->type) {
9694 case nir_cf_node_block:
9695 visit_block(ctx, nir_cf_node_as_block(node));
9696 break;
9697 case nir_cf_node_if:
9698 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9699 return true;
9700 break;
9701 case nir_cf_node_loop:
9702 visit_loop(ctx, nir_cf_node_as_loop(node));
9703 break;
9704 default:
9705 unreachable("unimplemented cf list type");
9706 }
9707 }
9708 return false;
9709 }
9710
9711 static void create_null_export(isel_context *ctx)
9712 {
9713 /* Some shader stages always need to have exports.
9714 * So when there is none, we need to add a null export.
9715 */
9716
9717 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9718 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9719 Builder bld(ctx->program, ctx->block);
9720 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9721 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9722 }
9723
9724 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9725 {
9726 assert(ctx->stage == vertex_vs ||
9727 ctx->stage == tess_eval_vs ||
9728 ctx->stage == gs_copy_vs ||
9729 ctx->stage == ngg_vertex_gs ||
9730 ctx->stage == ngg_tess_eval_gs);
9731
9732 int offset = (ctx->stage & sw_tes)
9733 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9734 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9735 uint64_t mask = ctx->outputs.mask[slot];
9736 if (!is_pos && !mask)
9737 return false;
9738 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9739 return false;
9740 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9741 exp->enabled_mask = mask;
9742 for (unsigned i = 0; i < 4; ++i) {
9743 if (mask & (1 << i))
9744 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9745 else
9746 exp->operands[i] = Operand(v1);
9747 }
9748 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9749 * Setting valid_mask=1 prevents it and has no other effect.
9750 */
9751 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9752 exp->done = false;
9753 exp->compressed = false;
9754 if (is_pos)
9755 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9756 else
9757 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9758 ctx->block->instructions.emplace_back(std::move(exp));
9759
9760 return true;
9761 }
9762
9763 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9764 {
9765 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9766 exp->enabled_mask = 0;
9767 for (unsigned i = 0; i < 4; ++i)
9768 exp->operands[i] = Operand(v1);
9769 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9770 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9771 exp->enabled_mask |= 0x1;
9772 }
9773 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9774 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9775 exp->enabled_mask |= 0x4;
9776 }
9777 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9778 if (ctx->options->chip_class < GFX9) {
9779 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9780 exp->enabled_mask |= 0x8;
9781 } else {
9782 Builder bld(ctx->program, ctx->block);
9783
9784 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9785 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9786 if (exp->operands[2].isTemp())
9787 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9788
9789 exp->operands[2] = Operand(out);
9790 exp->enabled_mask |= 0x4;
9791 }
9792 }
9793 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9794 exp->done = false;
9795 exp->compressed = false;
9796 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9797 ctx->block->instructions.emplace_back(std::move(exp));
9798 }
9799
9800 static void create_export_phis(isel_context *ctx)
9801 {
9802 /* Used when exports are needed, but the output temps are defined in a preceding block.
9803 * This function will set up phis in order to access the outputs in the next block.
9804 */
9805
9806 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9807 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9808 ctx->block->instructions.pop_back();
9809
9810 Builder bld(ctx->program, ctx->block);
9811
9812 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9813 uint64_t mask = ctx->outputs.mask[slot];
9814 for (unsigned i = 0; i < 4; ++i) {
9815 if (!(mask & (1 << i)))
9816 continue;
9817
9818 Temp old = ctx->outputs.temps[slot * 4 + i];
9819 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9820 ctx->outputs.temps[slot * 4 + i] = phi;
9821 }
9822 }
9823
9824 bld.insert(std::move(logical_start));
9825 }
9826
9827 static void create_vs_exports(isel_context *ctx)
9828 {
9829 assert(ctx->stage == vertex_vs ||
9830 ctx->stage == tess_eval_vs ||
9831 ctx->stage == gs_copy_vs ||
9832 ctx->stage == ngg_vertex_gs ||
9833 ctx->stage == ngg_tess_eval_gs);
9834
9835 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9836 ? &ctx->program->info->tes.outinfo
9837 : &ctx->program->info->vs.outinfo;
9838
9839 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9840 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9841 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9842 }
9843
9844 if (ctx->options->key.has_multiview_view_index) {
9845 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9846 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9847 }
9848
9849 /* the order these position exports are created is important */
9850 int next_pos = 0;
9851 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9852 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9853 export_vs_psiz_layer_viewport(ctx, &next_pos);
9854 exported_pos = true;
9855 }
9856 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9857 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9858 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9859 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9860
9861 if (ctx->export_clip_dists) {
9862 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9863 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9864 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9865 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9866 }
9867
9868 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9869 if (i < VARYING_SLOT_VAR0 &&
9870 i != VARYING_SLOT_LAYER &&
9871 i != VARYING_SLOT_PRIMITIVE_ID &&
9872 i != VARYING_SLOT_VIEWPORT)
9873 continue;
9874
9875 export_vs_varying(ctx, i, false, NULL);
9876 }
9877
9878 if (!exported_pos)
9879 create_null_export(ctx);
9880 }
9881
9882 static bool export_fs_mrt_z(isel_context *ctx)
9883 {
9884 Builder bld(ctx->program, ctx->block);
9885 unsigned enabled_channels = 0;
9886 bool compr = false;
9887 Operand values[4];
9888
9889 for (unsigned i = 0; i < 4; ++i) {
9890 values[i] = Operand(v1);
9891 }
9892
9893 /* Both stencil and sample mask only need 16-bits. */
9894 if (!ctx->program->info->ps.writes_z &&
9895 (ctx->program->info->ps.writes_stencil ||
9896 ctx->program->info->ps.writes_sample_mask)) {
9897 compr = true; /* COMPR flag */
9898
9899 if (ctx->program->info->ps.writes_stencil) {
9900 /* Stencil should be in X[23:16]. */
9901 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9902 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9903 enabled_channels |= 0x3;
9904 }
9905
9906 if (ctx->program->info->ps.writes_sample_mask) {
9907 /* SampleMask should be in Y[15:0]. */
9908 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9909 enabled_channels |= 0xc;
9910 }
9911 } else {
9912 if (ctx->program->info->ps.writes_z) {
9913 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9914 enabled_channels |= 0x1;
9915 }
9916
9917 if (ctx->program->info->ps.writes_stencil) {
9918 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9919 enabled_channels |= 0x2;
9920 }
9921
9922 if (ctx->program->info->ps.writes_sample_mask) {
9923 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9924 enabled_channels |= 0x4;
9925 }
9926 }
9927
9928 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9929 * writemask component.
9930 */
9931 if (ctx->options->chip_class == GFX6 &&
9932 ctx->options->family != CHIP_OLAND &&
9933 ctx->options->family != CHIP_HAINAN) {
9934 enabled_channels |= 0x1;
9935 }
9936
9937 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9938 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9939
9940 return true;
9941 }
9942
9943 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9944 {
9945 Builder bld(ctx->program, ctx->block);
9946 unsigned write_mask = ctx->outputs.mask[slot];
9947 Operand values[4];
9948
9949 for (unsigned i = 0; i < 4; ++i) {
9950 if (write_mask & (1 << i)) {
9951 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9952 } else {
9953 values[i] = Operand(v1);
9954 }
9955 }
9956
9957 unsigned target, col_format;
9958 unsigned enabled_channels = 0;
9959 aco_opcode compr_op = (aco_opcode)0;
9960
9961 slot -= FRAG_RESULT_DATA0;
9962 target = V_008DFC_SQ_EXP_MRT + slot;
9963 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9964
9965 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9966 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9967 bool is_16bit = values[0].regClass() == v2b;
9968
9969 switch (col_format)
9970 {
9971 case V_028714_SPI_SHADER_ZERO:
9972 enabled_channels = 0; /* writemask */
9973 target = V_008DFC_SQ_EXP_NULL;
9974 break;
9975
9976 case V_028714_SPI_SHADER_32_R:
9977 enabled_channels = 1;
9978 break;
9979
9980 case V_028714_SPI_SHADER_32_GR:
9981 enabled_channels = 0x3;
9982 break;
9983
9984 case V_028714_SPI_SHADER_32_AR:
9985 if (ctx->options->chip_class >= GFX10) {
9986 /* Special case: on GFX10, the outputs are different for 32_AR */
9987 enabled_channels = 0x3;
9988 values[1] = values[3];
9989 values[3] = Operand(v1);
9990 } else {
9991 enabled_channels = 0x9;
9992 }
9993 break;
9994
9995 case V_028714_SPI_SHADER_FP16_ABGR:
9996 enabled_channels = 0x5;
9997 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9998 if (is_16bit) {
9999 if (ctx->options->chip_class >= GFX9) {
10000 /* Pack the FP16 values together instead of converting them to
10001 * FP32 and back to FP16.
10002 * TODO: use p_create_vector and let the compiler optimizes.
10003 */
10004 compr_op = aco_opcode::v_pack_b32_f16;
10005 } else {
10006 for (unsigned i = 0; i < 4; i++) {
10007 if ((write_mask >> i) & 1)
10008 values[i] = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), values[i]);
10009 }
10010 }
10011 }
10012 break;
10013
10014 case V_028714_SPI_SHADER_UNORM16_ABGR:
10015 enabled_channels = 0x5;
10016 if (is_16bit && ctx->options->chip_class >= GFX9) {
10017 compr_op = aco_opcode::v_cvt_pknorm_u16_f16;
10018 } else {
10019 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
10020 }
10021 break;
10022
10023 case V_028714_SPI_SHADER_SNORM16_ABGR:
10024 enabled_channels = 0x5;
10025 if (is_16bit && ctx->options->chip_class >= GFX9) {
10026 compr_op = aco_opcode::v_cvt_pknorm_i16_f16;
10027 } else {
10028 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
10029 }
10030 break;
10031
10032 case V_028714_SPI_SHADER_UINT16_ABGR: {
10033 enabled_channels = 0x5;
10034 compr_op = aco_opcode::v_cvt_pk_u16_u32;
10035 if (is_int8 || is_int10) {
10036 /* clamp */
10037 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
10038 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10039
10040 for (unsigned i = 0; i < 4; i++) {
10041 if ((write_mask >> i) & 1) {
10042 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
10043 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
10044 values[i]);
10045 }
10046 }
10047 } else if (is_16bit) {
10048 for (unsigned i = 0; i < 4; i++) {
10049 if ((write_mask >> i) & 1) {
10050 Temp tmp = convert_int(bld, values[i].getTemp(), 16, 32, false);
10051 values[i] = Operand(tmp);
10052 }
10053 }
10054 }
10055 break;
10056 }
10057
10058 case V_028714_SPI_SHADER_SINT16_ABGR:
10059 enabled_channels = 0x5;
10060 compr_op = aco_opcode::v_cvt_pk_i16_i32;
10061 if (is_int8 || is_int10) {
10062 /* clamp */
10063 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
10064 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
10065 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
10066 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
10067
10068 for (unsigned i = 0; i < 4; i++) {
10069 if ((write_mask >> i) & 1) {
10070 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
10071 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
10072 values[i]);
10073 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
10074 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
10075 values[i]);
10076 }
10077 }
10078 } else if (is_16bit) {
10079 for (unsigned i = 0; i < 4; i++) {
10080 if ((write_mask >> i) & 1) {
10081 Temp tmp = convert_int(bld, values[i].getTemp(), 16, 32, true);
10082 values[i] = Operand(tmp);
10083 }
10084 }
10085 }
10086 break;
10087
10088 case V_028714_SPI_SHADER_32_ABGR:
10089 enabled_channels = 0xF;
10090 break;
10091
10092 default:
10093 break;
10094 }
10095
10096 if (target == V_008DFC_SQ_EXP_NULL)
10097 return false;
10098
10099 if ((bool) compr_op) {
10100 for (int i = 0; i < 2; i++) {
10101 /* check if at least one of the values to be compressed is enabled */
10102 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
10103 if (enabled) {
10104 enabled_channels |= enabled << (i*2);
10105 values[i] = bld.vop3(compr_op, bld.def(v1),
10106 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
10107 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
10108 } else {
10109 values[i] = Operand(v1);
10110 }
10111 }
10112 values[2] = Operand(v1);
10113 values[3] = Operand(v1);
10114 } else {
10115 for (int i = 0; i < 4; i++)
10116 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
10117 }
10118
10119 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
10120 enabled_channels, target, (bool) compr_op);
10121 return true;
10122 }
10123
10124 static void create_fs_exports(isel_context *ctx)
10125 {
10126 bool exported = false;
10127
10128 /* Export depth, stencil and sample mask. */
10129 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
10130 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
10131 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
10132 exported |= export_fs_mrt_z(ctx);
10133
10134 /* Export all color render targets. */
10135 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
10136 if (ctx->outputs.mask[i])
10137 exported |= export_fs_mrt_color(ctx, i);
10138
10139 if (!exported)
10140 create_null_export(ctx);
10141 }
10142
10143 static void write_tcs_tess_factors(isel_context *ctx)
10144 {
10145 unsigned outer_comps;
10146 unsigned inner_comps;
10147
10148 switch (ctx->args->options->key.tcs.primitive_mode) {
10149 case GL_ISOLINES:
10150 outer_comps = 2;
10151 inner_comps = 0;
10152 break;
10153 case GL_TRIANGLES:
10154 outer_comps = 3;
10155 inner_comps = 1;
10156 break;
10157 case GL_QUADS:
10158 outer_comps = 4;
10159 inner_comps = 2;
10160 break;
10161 default:
10162 return;
10163 }
10164
10165 Builder bld(ctx->program, ctx->block);
10166
10167 bld.barrier(aco_opcode::p_memory_barrier_shared);
10168 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
10169 bld.sopp(aco_opcode::s_barrier);
10170
10171 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
10172 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
10173
10174 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
10175 if_context ic_invocation_id_is_zero;
10176 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
10177 bld.reset(ctx->block);
10178
10179 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));
10180
10181 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
10182 unsigned stride = inner_comps + outer_comps;
10183 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
10184 Temp tf_inner_vec;
10185 Temp tf_outer_vec;
10186 Temp out[6];
10187 assert(stride <= (sizeof(out) / sizeof(Temp)));
10188
10189 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
10190 // LINES reversal
10191 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
10192 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
10193 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
10194 } else {
10195 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);
10196 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);
10197
10198 for (unsigned i = 0; i < outer_comps; ++i)
10199 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
10200 for (unsigned i = 0; i < inner_comps; ++i)
10201 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
10202 }
10203
10204 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
10205 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
10206 Temp byte_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, stride * 4u);
10207 unsigned tf_const_offset = 0;
10208
10209 if (ctx->program->chip_class <= GFX8) {
10210 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);
10211 if_context ic_rel_patch_id_is_zero;
10212 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10213 bld.reset(ctx->block);
10214
10215 /* Store the dynamic HS control word. */
10216 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10217 bld.mubuf(aco_opcode::buffer_store_dword,
10218 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10219 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10220 /* disable_wqm */ false, /* glc */ true);
10221 tf_const_offset += 4;
10222
10223 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10224 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10225 bld.reset(ctx->block);
10226 }
10227
10228 assert(stride == 2 || stride == 4 || stride == 6);
10229 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10230 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10231
10232 /* Store to offchip for TES to read - only if TES reads them */
10233 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10234 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));
10235 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10236
10237 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10238 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);
10239
10240 if (likely(inner_comps)) {
10241 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10242 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);
10243 }
10244 }
10245
10246 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10247 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10248 }
10249
10250 static void emit_stream_output(isel_context *ctx,
10251 Temp const *so_buffers,
10252 Temp const *so_write_offset,
10253 const struct radv_stream_output *output)
10254 {
10255 unsigned num_comps = util_bitcount(output->component_mask);
10256 unsigned writemask = (1 << num_comps) - 1;
10257 unsigned loc = output->location;
10258 unsigned buf = output->buffer;
10259
10260 assert(num_comps && num_comps <= 4);
10261 if (!num_comps || num_comps > 4)
10262 return;
10263
10264 unsigned start = ffs(output->component_mask) - 1;
10265
10266 Temp out[4];
10267 bool all_undef = true;
10268 assert(ctx->stage & hw_vs);
10269 for (unsigned i = 0; i < num_comps; i++) {
10270 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10271 all_undef = all_undef && !out[i].id();
10272 }
10273 if (all_undef)
10274 return;
10275
10276 while (writemask) {
10277 int start, count;
10278 u_bit_scan_consecutive_range(&writemask, &start, &count);
10279 if (count == 3 && ctx->options->chip_class == GFX6) {
10280 /* GFX6 doesn't support storing vec3, split it. */
10281 writemask |= 1u << (start + 2);
10282 count = 2;
10283 }
10284
10285 unsigned offset = output->offset + start * 4;
10286
10287 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10288 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10289 for (int i = 0; i < count; ++i)
10290 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10291 vec->definitions[0] = Definition(write_data);
10292 ctx->block->instructions.emplace_back(std::move(vec));
10293
10294 aco_opcode opcode;
10295 switch (count) {
10296 case 1:
10297 opcode = aco_opcode::buffer_store_dword;
10298 break;
10299 case 2:
10300 opcode = aco_opcode::buffer_store_dwordx2;
10301 break;
10302 case 3:
10303 opcode = aco_opcode::buffer_store_dwordx3;
10304 break;
10305 case 4:
10306 opcode = aco_opcode::buffer_store_dwordx4;
10307 break;
10308 default:
10309 unreachable("Unsupported dword count.");
10310 }
10311
10312 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10313 store->operands[0] = Operand(so_buffers[buf]);
10314 store->operands[1] = Operand(so_write_offset[buf]);
10315 store->operands[2] = Operand((uint32_t) 0);
10316 store->operands[3] = Operand(write_data);
10317 if (offset > 4095) {
10318 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10319 Builder bld(ctx->program, ctx->block);
10320 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10321 } else {
10322 store->offset = offset;
10323 }
10324 store->offen = true;
10325 store->glc = true;
10326 store->dlc = false;
10327 store->slc = true;
10328 store->can_reorder = true;
10329 ctx->block->instructions.emplace_back(std::move(store));
10330 }
10331 }
10332
10333 static void emit_streamout(isel_context *ctx, unsigned stream)
10334 {
10335 Builder bld(ctx->program, ctx->block);
10336
10337 Temp so_buffers[4];
10338 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10339 for (unsigned i = 0; i < 4; i++) {
10340 unsigned stride = ctx->program->info->so.strides[i];
10341 if (!stride)
10342 continue;
10343
10344 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10345 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10346 }
10347
10348 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10349 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10350
10351 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10352
10353 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10354
10355 if_context ic;
10356 begin_divergent_if_then(ctx, &ic, can_emit);
10357
10358 bld.reset(ctx->block);
10359
10360 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10361
10362 Temp so_write_offset[4];
10363
10364 for (unsigned i = 0; i < 4; i++) {
10365 unsigned stride = ctx->program->info->so.strides[i];
10366 if (!stride)
10367 continue;
10368
10369 if (stride == 1) {
10370 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10371 get_arg(ctx, ctx->args->streamout_write_idx),
10372 get_arg(ctx, ctx->args->streamout_offset[i]));
10373 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10374
10375 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10376 } else {
10377 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10378 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10379 get_arg(ctx, ctx->args->streamout_offset[i]));
10380 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10381 }
10382 }
10383
10384 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10385 struct radv_stream_output *output =
10386 &ctx->program->info->so.outputs[i];
10387 if (stream != output->stream)
10388 continue;
10389
10390 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10391 }
10392
10393 begin_divergent_if_else(ctx, &ic);
10394 end_divergent_if(ctx, &ic);
10395 }
10396
10397 } /* end namespace */
10398
10399 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10400 {
10401 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10402 Builder bld(ctx->program, ctx->block);
10403 constexpr unsigned hs_idx = 1u;
10404 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10405 get_arg(ctx, ctx->args->merged_wave_info),
10406 Operand((8u << 16) | (hs_idx * 8u)));
10407 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10408
10409 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10410
10411 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10412 get_arg(ctx, ctx->args->rel_auto_id),
10413 get_arg(ctx, ctx->args->ac.instance_id),
10414 ls_has_nonzero_hs_threads);
10415 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10416 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10417 get_arg(ctx, ctx->args->rel_auto_id),
10418 ls_has_nonzero_hs_threads);
10419 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10420 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10421 get_arg(ctx, ctx->args->ac.vertex_id),
10422 ls_has_nonzero_hs_threads);
10423
10424 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10425 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10426 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10427 }
10428
10429 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10430 {
10431 /* Split all arguments except for the first (ring_offsets) and the last
10432 * (exec) so that the dead channels don't stay live throughout the program.
10433 */
10434 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10435 if (startpgm->definitions[i].regClass().size() > 1) {
10436 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10437 startpgm->definitions[i].regClass().size());
10438 }
10439 }
10440 }
10441
10442 void handle_bc_optimize(isel_context *ctx)
10443 {
10444 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10445 Builder bld(ctx->program, ctx->block);
10446 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10447 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10448 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10449 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10450 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10451 if (uses_center && uses_centroid) {
10452 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10453 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10454
10455 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10456 Temp new_coord[2];
10457 for (unsigned i = 0; i < 2; i++) {
10458 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10459 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10460 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10461 persp_centroid, persp_center, sel);
10462 }
10463 ctx->persp_centroid = bld.tmp(v2);
10464 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10465 Operand(new_coord[0]), Operand(new_coord[1]));
10466 emit_split_vector(ctx, ctx->persp_centroid, 2);
10467 }
10468
10469 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10470 Temp new_coord[2];
10471 for (unsigned i = 0; i < 2; i++) {
10472 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10473 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10474 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10475 linear_centroid, linear_center, sel);
10476 }
10477 ctx->linear_centroid = bld.tmp(v2);
10478 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10479 Operand(new_coord[0]), Operand(new_coord[1]));
10480 emit_split_vector(ctx, ctx->linear_centroid, 2);
10481 }
10482 }
10483 }
10484
10485 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10486 {
10487 Program *program = ctx->program;
10488
10489 unsigned float_controls = shader->info.float_controls_execution_mode;
10490
10491 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10492 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10493 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10494 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10495 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10496
10497 program->next_fp_mode.must_flush_denorms32 =
10498 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10499 program->next_fp_mode.must_flush_denorms16_64 =
10500 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10501 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10502
10503 program->next_fp_mode.care_about_round32 =
10504 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10505
10506 program->next_fp_mode.care_about_round16_64 =
10507 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10508 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10509
10510 /* default to preserving fp16 and fp64 denorms, since it's free */
10511 if (program->next_fp_mode.must_flush_denorms16_64)
10512 program->next_fp_mode.denorm16_64 = 0;
10513 else
10514 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10515
10516 /* preserving fp32 denorms is expensive, so only do it if asked */
10517 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10518 program->next_fp_mode.denorm32 = fp_denorm_keep;
10519 else
10520 program->next_fp_mode.denorm32 = 0;
10521
10522 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10523 program->next_fp_mode.round32 = fp_round_tz;
10524 else
10525 program->next_fp_mode.round32 = fp_round_ne;
10526
10527 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10528 program->next_fp_mode.round16_64 = fp_round_tz;
10529 else
10530 program->next_fp_mode.round16_64 = fp_round_ne;
10531
10532 ctx->block->fp_mode = program->next_fp_mode;
10533 }
10534
10535 void cleanup_cfg(Program *program)
10536 {
10537 /* create linear_succs/logical_succs */
10538 for (Block& BB : program->blocks) {
10539 for (unsigned idx : BB.linear_preds)
10540 program->blocks[idx].linear_succs.emplace_back(BB.index);
10541 for (unsigned idx : BB.logical_preds)
10542 program->blocks[idx].logical_succs.emplace_back(BB.index);
10543 }
10544 }
10545
10546 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10547 {
10548 Builder bld(ctx->program, ctx->block);
10549
10550 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10551 Temp count = i == 0
10552 ? get_arg(ctx, ctx->args->merged_wave_info)
10553 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10554 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10555
10556 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10557 Temp cond;
10558
10559 if (ctx->program->wave_size == 64) {
10560 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10561 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10562 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10563 } else {
10564 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10565 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10566 }
10567
10568 return cond;
10569 }
10570
10571 bool ngg_early_prim_export(isel_context *ctx)
10572 {
10573 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10574 return true;
10575 }
10576
10577 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10578 {
10579 Builder bld(ctx->program, ctx->block);
10580
10581 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10582 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10583
10584 /* Get the id of the current wave within the threadgroup (workgroup) */
10585 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10586 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10587
10588 /* Execute the following code only on the first wave (wave id 0),
10589 * use the SCC def to tell if the wave id is zero or not.
10590 */
10591 Temp cond = wave_id_in_tg.def(1).getTemp();
10592 if_context ic;
10593 begin_uniform_if_then(ctx, &ic, cond);
10594 begin_uniform_if_else(ctx, &ic);
10595 bld.reset(ctx->block);
10596
10597 /* Number of vertices output by VS/TES */
10598 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10599 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10600 /* Number of primitives output by VS/TES */
10601 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10602 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10603
10604 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10605 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10606 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10607
10608 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10609 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10610
10611 end_uniform_if(ctx, &ic);
10612
10613 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10614 bld.reset(ctx->block);
10615 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10616 }
10617
10618 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10619 {
10620 Builder bld(ctx->program, ctx->block);
10621
10622 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10623 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10624 }
10625
10626 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10627 Temp tmp;
10628
10629 for (unsigned i = 0; i < num_vertices; ++i) {
10630 assert(vtxindex[i].id());
10631
10632 if (i)
10633 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10634 else
10635 tmp = vtxindex[i];
10636
10637 /* The initial edge flag is always false in tess eval shaders. */
10638 if (ctx->stage == ngg_vertex_gs) {
10639 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10640 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10641 }
10642 }
10643
10644 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10645
10646 return tmp;
10647 }
10648
10649 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10650 {
10651 Builder bld(ctx->program, ctx->block);
10652 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10653
10654 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10655 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10656 false /* compressed */, true/* done */, false /* valid mask */);
10657 }
10658
10659 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10660 {
10661 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10662 * These must always come before VS exports.
10663 *
10664 * It is recommended to do these as early as possible. They can be at the beginning when
10665 * there is no SW GS and the shader doesn't write edge flags.
10666 */
10667
10668 if_context ic;
10669 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10670 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10671
10672 Builder bld(ctx->program, ctx->block);
10673 constexpr unsigned max_vertices_per_primitive = 3;
10674 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10675
10676 if (ctx->stage == ngg_vertex_gs) {
10677 /* TODO: optimize for points & lines */
10678 } else if (ctx->stage == ngg_tess_eval_gs) {
10679 if (ctx->shader->info.tess.point_mode)
10680 num_vertices_per_primitive = 1;
10681 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10682 num_vertices_per_primitive = 2;
10683 } else {
10684 unreachable("Unsupported NGG shader stage");
10685 }
10686
10687 Temp vtxindex[max_vertices_per_primitive];
10688 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10689 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10690 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10691 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10692 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10693 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10694 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10695 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10696
10697 /* Export primitive data to the index buffer. */
10698 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10699
10700 /* Export primitive ID. */
10701 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10702 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10703 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10704 Temp provoking_vtx_index = vtxindex[0];
10705 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10706
10707 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10708 }
10709
10710 begin_divergent_if_else(ctx, &ic);
10711 end_divergent_if(ctx, &ic);
10712 }
10713
10714 void ngg_emit_nogs_output(isel_context *ctx)
10715 {
10716 /* Emits NGG GS output, for stages that don't have SW GS. */
10717
10718 if_context ic;
10719 Builder bld(ctx->program, ctx->block);
10720 bool late_prim_export = !ngg_early_prim_export(ctx);
10721
10722 /* NGG streamout is currently disabled by default. */
10723 assert(!ctx->args->shader_info->so.num_outputs);
10724
10725 if (late_prim_export) {
10726 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10727 create_export_phis(ctx);
10728 /* Do what we need to do in the GS threads. */
10729 ngg_emit_nogs_gsthreads(ctx);
10730
10731 /* What comes next should be executed on ES threads. */
10732 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10733 begin_divergent_if_then(ctx, &ic, is_es_thread);
10734 bld.reset(ctx->block);
10735 }
10736
10737 /* Export VS outputs */
10738 ctx->block->kind |= block_kind_export_end;
10739 create_vs_exports(ctx);
10740
10741 /* Export primitive ID */
10742 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10743 Temp prim_id;
10744
10745 if (ctx->stage == ngg_vertex_gs) {
10746 /* Wait for GS threads to store primitive ID in LDS. */
10747 bld.barrier(aco_opcode::p_memory_barrier_shared);
10748 bld.sopp(aco_opcode::s_barrier);
10749
10750 /* Calculate LDS address where the GS threads stored the primitive ID. */
10751 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10752 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10753 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10754 Temp wave_id_mul = bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10755 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10756 Temp addr = bld.v_mul24_imm(bld.def(v1), thread_id_in_tg, 4u);
10757
10758 /* Load primitive ID from LDS. */
10759 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10760 } else if (ctx->stage == ngg_tess_eval_gs) {
10761 /* TES: Just use the patch ID as the primitive ID. */
10762 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10763 } else {
10764 unreachable("unsupported NGG shader stage.");
10765 }
10766
10767 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10768 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10769
10770 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10771 }
10772
10773 if (late_prim_export) {
10774 begin_divergent_if_else(ctx, &ic);
10775 end_divergent_if(ctx, &ic);
10776 bld.reset(ctx->block);
10777 }
10778 }
10779
10780 void select_program(Program *program,
10781 unsigned shader_count,
10782 struct nir_shader *const *shaders,
10783 ac_shader_config* config,
10784 struct radv_shader_args *args)
10785 {
10786 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10787 if_context ic_merged_wave_info;
10788 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10789
10790 for (unsigned i = 0; i < shader_count; i++) {
10791 nir_shader *nir = shaders[i];
10792 init_context(&ctx, nir);
10793
10794 setup_fp_mode(&ctx, nir);
10795
10796 if (!i) {
10797 /* needs to be after init_context() for FS */
10798 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10799 append_logical_start(ctx.block);
10800
10801 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10802 fix_ls_vgpr_init_bug(&ctx, startpgm);
10803
10804 split_arguments(&ctx, startpgm);
10805 }
10806
10807 if (ngg_no_gs) {
10808 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10809
10810 if (ngg_early_prim_export(&ctx))
10811 ngg_emit_nogs_gsthreads(&ctx);
10812 }
10813
10814 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10815 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10816 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10817 ((nir->info.stage == MESA_SHADER_VERTEX &&
10818 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10819 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10820 ctx.stage == tess_eval_geometry_gs));
10821
10822 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10823 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10824 if (check_merged_wave_info) {
10825 Temp cond = merged_wave_info_to_mask(&ctx, i);
10826 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10827 }
10828
10829 if (i) {
10830 Builder bld(ctx.program, ctx.block);
10831
10832 bld.barrier(aco_opcode::p_memory_barrier_shared);
10833 bld.sopp(aco_opcode::s_barrier);
10834
10835 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10836 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));
10837 }
10838 } else if (ctx.stage == geometry_gs)
10839 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10840
10841 if (ctx.stage == fragment_fs)
10842 handle_bc_optimize(&ctx);
10843
10844 visit_cf_list(&ctx, &func->body);
10845
10846 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10847 emit_streamout(&ctx, 0);
10848
10849 if (ctx.stage & hw_vs) {
10850 create_vs_exports(&ctx);
10851 ctx.block->kind |= block_kind_export_end;
10852 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10853 ngg_emit_nogs_output(&ctx);
10854 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10855 Builder bld(ctx.program, ctx.block);
10856 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10857 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10858 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10859 write_tcs_tess_factors(&ctx);
10860 }
10861
10862 if (ctx.stage == fragment_fs) {
10863 create_fs_exports(&ctx);
10864 ctx.block->kind |= block_kind_export_end;
10865 }
10866
10867 if (endif_merged_wave_info) {
10868 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10869 end_divergent_if(&ctx, &ic_merged_wave_info);
10870 }
10871
10872 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10873 ngg_emit_nogs_output(&ctx);
10874
10875 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10876 /* Outputs of the previous stage are inputs to the next stage */
10877 ctx.inputs = ctx.outputs;
10878 ctx.outputs = shader_io_state();
10879 }
10880 }
10881
10882 program->config->float_mode = program->blocks[0].fp_mode.val;
10883
10884 append_logical_end(ctx.block);
10885 ctx.block->kind |= block_kind_uniform;
10886 Builder bld(ctx.program, ctx.block);
10887 if (ctx.program->wb_smem_l1_on_end)
10888 bld.smem(aco_opcode::s_dcache_wb, false);
10889 bld.sopp(aco_opcode::s_endpgm);
10890
10891 cleanup_cfg(program);
10892 }
10893
10894 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10895 ac_shader_config* config,
10896 struct radv_shader_args *args)
10897 {
10898 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10899
10900 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10901 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10902 program->next_fp_mode.must_flush_denorms32 = false;
10903 program->next_fp_mode.must_flush_denorms16_64 = false;
10904 program->next_fp_mode.care_about_round32 = false;
10905 program->next_fp_mode.care_about_round16_64 = false;
10906 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10907 program->next_fp_mode.denorm32 = 0;
10908 program->next_fp_mode.round32 = fp_round_ne;
10909 program->next_fp_mode.round16_64 = fp_round_ne;
10910 ctx.block->fp_mode = program->next_fp_mode;
10911
10912 add_startpgm(&ctx);
10913 append_logical_start(ctx.block);
10914
10915 Builder bld(ctx.program, ctx.block);
10916
10917 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10918
10919 Operand stream_id(0u);
10920 if (args->shader_info->so.num_outputs)
10921 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10922 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10923
10924 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10925
10926 std::stack<Block> endif_blocks;
10927
10928 for (unsigned stream = 0; stream < 4; stream++) {
10929 if (stream_id.isConstant() && stream != stream_id.constantValue())
10930 continue;
10931
10932 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10933 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10934 continue;
10935
10936 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10937
10938 unsigned BB_if_idx = ctx.block->index;
10939 Block BB_endif = Block();
10940 if (!stream_id.isConstant()) {
10941 /* begin IF */
10942 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10943 append_logical_end(ctx.block);
10944 ctx.block->kind |= block_kind_uniform;
10945 bld.branch(aco_opcode::p_cbranch_z, cond);
10946
10947 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10948
10949 ctx.block = ctx.program->create_and_insert_block();
10950 add_edge(BB_if_idx, ctx.block);
10951 bld.reset(ctx.block);
10952 append_logical_start(ctx.block);
10953 }
10954
10955 unsigned offset = 0;
10956 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10957 if (args->shader_info->gs.output_streams[i] != stream)
10958 continue;
10959
10960 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10961 unsigned length = util_last_bit(output_usage_mask);
10962 for (unsigned j = 0; j < length; ++j) {
10963 if (!(output_usage_mask & (1 << j)))
10964 continue;
10965
10966 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10967 Temp voffset = vtx_offset;
10968 if (const_offset >= 4096u) {
10969 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10970 const_offset %= 4096u;
10971 }
10972
10973 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10974 mubuf->definitions[0] = bld.def(v1);
10975 mubuf->operands[0] = Operand(gsvs_ring);
10976 mubuf->operands[1] = Operand(voffset);
10977 mubuf->operands[2] = Operand(0u);
10978 mubuf->offen = true;
10979 mubuf->offset = const_offset;
10980 mubuf->glc = true;
10981 mubuf->slc = true;
10982 mubuf->dlc = args->options->chip_class >= GFX10;
10983 mubuf->barrier = barrier_none;
10984 mubuf->can_reorder = true;
10985
10986 ctx.outputs.mask[i] |= 1 << j;
10987 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10988
10989 bld.insert(std::move(mubuf));
10990
10991 offset++;
10992 }
10993 }
10994
10995 if (args->shader_info->so.num_outputs) {
10996 emit_streamout(&ctx, stream);
10997 bld.reset(ctx.block);
10998 }
10999
11000 if (stream == 0) {
11001 create_vs_exports(&ctx);
11002 ctx.block->kind |= block_kind_export_end;
11003 }
11004
11005 if (!stream_id.isConstant()) {
11006 append_logical_end(ctx.block);
11007
11008 /* branch from then block to endif block */
11009 bld.branch(aco_opcode::p_branch);
11010 add_edge(ctx.block->index, &BB_endif);
11011 ctx.block->kind |= block_kind_uniform;
11012
11013 /* emit else block */
11014 ctx.block = ctx.program->create_and_insert_block();
11015 add_edge(BB_if_idx, ctx.block);
11016 bld.reset(ctx.block);
11017 append_logical_start(ctx.block);
11018
11019 endif_blocks.push(std::move(BB_endif));
11020 }
11021 }
11022
11023 while (!endif_blocks.empty()) {
11024 Block BB_endif = std::move(endif_blocks.top());
11025 endif_blocks.pop();
11026
11027 Block *BB_else = ctx.block;
11028
11029 append_logical_end(BB_else);
11030 /* branch from else block to endif block */
11031 bld.branch(aco_opcode::p_branch);
11032 add_edge(BB_else->index, &BB_endif);
11033 BB_else->kind |= block_kind_uniform;
11034
11035 /** emit endif merge block */
11036 ctx.block = program->insert_block(std::move(BB_endif));
11037 bld.reset(ctx.block);
11038 append_logical_start(ctx.block);
11039 }
11040
11041 program->config->float_mode = program->blocks[0].fp_mode.val;
11042
11043 append_logical_end(ctx.block);
11044 ctx.block->kind |= block_kind_uniform;
11045 bld.sopp(aco_opcode::s_endpgm);
11046
11047 cleanup_cfg(program);
11048 }
11049 }