11dda97e2c71af1fa90e76cef8b0540592c99c91
[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 Temp index_x4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), index);
173
174 /* Currently not implemented on GFX6-7 */
175 assert(ctx->options->chip_class >= GFX8);
176
177 if (ctx->options->chip_class <= GFX9 || ctx->program->wave_size == 32) {
178 return bld.ds(aco_opcode::ds_bpermute_b32, bld.def(v1), index_x4, data);
179 }
180
181 /* GFX10, wave64 mode:
182 * The bpermute instruction is limited to half-wave operation, which means that it can't
183 * properly support subgroup shuffle like older generations (or wave32 mode), so we
184 * emulate it here.
185 */
186 if (!ctx->has_gfx10_wave64_bpermute) {
187 ctx->has_gfx10_wave64_bpermute = true;
188 ctx->program->config->num_shared_vgprs = 8; /* Shared VGPRs are allocated in groups of 8 */
189 ctx->program->vgpr_limit -= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
190 }
191
192 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
193 Temp lane_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), lane_id);
194 Temp index_is_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x20u), index);
195 Temp cmp = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm, vcc), lane_is_hi, index_is_hi);
196
197 return bld.reduction(aco_opcode::p_wave64_bpermute, bld.def(v1), bld.def(s2), bld.def(s1, scc),
198 bld.vcc(cmp), Operand(v2.as_linear()), index_x4, data, gfx10_wave64_bpermute);
199 }
200
201 Temp as_vgpr(isel_context *ctx, Temp val)
202 {
203 if (val.type() == RegType::sgpr) {
204 Builder bld(ctx->program, ctx->block);
205 return bld.copy(bld.def(RegType::vgpr, val.size()), val);
206 }
207 assert(val.type() == RegType::vgpr);
208 return val;
209 }
210
211 //assumes a != 0xffffffff
212 void emit_v_div_u32(isel_context *ctx, Temp dst, Temp a, uint32_t b)
213 {
214 assert(b != 0);
215 Builder bld(ctx->program, ctx->block);
216
217 if (util_is_power_of_two_or_zero(b)) {
218 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)util_logbase2(b)), a);
219 return;
220 }
221
222 util_fast_udiv_info info = util_compute_fast_udiv_info(b, 32, 32);
223
224 assert(info.multiplier <= 0xffffffff);
225
226 bool pre_shift = info.pre_shift != 0;
227 bool increment = info.increment != 0;
228 bool multiply = true;
229 bool post_shift = info.post_shift != 0;
230
231 if (!pre_shift && !increment && !multiply && !post_shift) {
232 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), a);
233 return;
234 }
235
236 Temp pre_shift_dst = a;
237 if (pre_shift) {
238 pre_shift_dst = (increment || multiply || post_shift) ? bld.tmp(v1) : dst;
239 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(pre_shift_dst), Operand((uint32_t)info.pre_shift), a);
240 }
241
242 Temp increment_dst = pre_shift_dst;
243 if (increment) {
244 increment_dst = (post_shift || multiply) ? bld.tmp(v1) : dst;
245 bld.vadd32(Definition(increment_dst), Operand((uint32_t) info.increment), pre_shift_dst);
246 }
247
248 Temp multiply_dst = increment_dst;
249 if (multiply) {
250 multiply_dst = post_shift ? bld.tmp(v1) : dst;
251 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(multiply_dst), increment_dst,
252 bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand((uint32_t)info.multiplier)));
253 }
254
255 if (post_shift) {
256 bld.vop2(aco_opcode::v_lshrrev_b32, Definition(dst), Operand((uint32_t)info.post_shift), multiply_dst);
257 }
258 }
259
260 void emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, Temp dst)
261 {
262 Builder bld(ctx->program, ctx->block);
263 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), src, Operand(idx));
264 }
265
266
267 Temp emit_extract_vector(isel_context* ctx, Temp src, uint32_t idx, RegClass dst_rc)
268 {
269 /* no need to extract the whole vector */
270 if (src.regClass() == dst_rc) {
271 assert(idx == 0);
272 return src;
273 }
274
275 assert(src.bytes() > (idx * dst_rc.bytes()));
276 Builder bld(ctx->program, ctx->block);
277 auto it = ctx->allocated_vec.find(src.id());
278 if (it != ctx->allocated_vec.end() && dst_rc.bytes() == it->second[idx].regClass().bytes()) {
279 if (it->second[idx].regClass() == dst_rc) {
280 return it->second[idx];
281 } else {
282 assert(!dst_rc.is_subdword());
283 assert(dst_rc.type() == RegType::vgpr && it->second[idx].type() == RegType::sgpr);
284 return bld.copy(bld.def(dst_rc), it->second[idx]);
285 }
286 }
287
288 if (dst_rc.is_subdword())
289 src = as_vgpr(ctx, src);
290
291 if (src.bytes() == dst_rc.bytes()) {
292 assert(idx == 0);
293 return bld.copy(bld.def(dst_rc), src);
294 } else {
295 Temp dst = bld.tmp(dst_rc);
296 emit_extract_vector(ctx, src, idx, dst);
297 return dst;
298 }
299 }
300
301 void emit_split_vector(isel_context* ctx, Temp vec_src, unsigned num_components)
302 {
303 if (num_components == 1)
304 return;
305 if (ctx->allocated_vec.find(vec_src.id()) != ctx->allocated_vec.end())
306 return;
307 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_components)};
308 split->operands[0] = Operand(vec_src);
309 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
310 RegClass rc;
311 if (num_components > vec_src.size()) {
312 if (vec_src.type() == RegType::sgpr)
313 return;
314
315 /* sub-dword split */
316 assert(vec_src.type() == RegType::vgpr);
317 rc = RegClass(RegType::vgpr, vec_src.bytes() / num_components).as_subdword();
318 } else {
319 rc = RegClass(vec_src.type(), vec_src.size() / num_components);
320 }
321 for (unsigned i = 0; i < num_components; i++) {
322 elems[i] = {ctx->program->allocateId(), rc};
323 split->definitions[i] = Definition(elems[i]);
324 }
325 ctx->block->instructions.emplace_back(std::move(split));
326 ctx->allocated_vec.emplace(vec_src.id(), elems);
327 }
328
329 /* This vector expansion uses a mask to determine which elements in the new vector
330 * come from the original vector. The other elements are undefined. */
331 void expand_vector(isel_context* ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
332 {
333 emit_split_vector(ctx, vec_src, util_bitcount(mask));
334
335 if (vec_src == dst)
336 return;
337
338 Builder bld(ctx->program, ctx->block);
339 if (num_components == 1) {
340 if (dst.type() == RegType::sgpr)
341 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
342 else
343 bld.copy(Definition(dst), vec_src);
344 return;
345 }
346
347 unsigned component_size = dst.size() / num_components;
348 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
349
350 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
351 vec->definitions[0] = Definition(dst);
352 unsigned k = 0;
353 for (unsigned i = 0; i < num_components; i++) {
354 if (mask & (1 << i)) {
355 Temp src = emit_extract_vector(ctx, vec_src, k++, RegClass(vec_src.type(), component_size));
356 if (dst.type() == RegType::sgpr)
357 src = bld.as_uniform(src);
358 vec->operands[i] = Operand(src);
359 } else {
360 vec->operands[i] = Operand(0u);
361 }
362 elems[i] = vec->operands[i].getTemp();
363 }
364 ctx->block->instructions.emplace_back(std::move(vec));
365 ctx->allocated_vec.emplace(dst.id(), elems);
366 }
367
368 /* adjust misaligned small bit size loads */
369 void byte_align_scalar(isel_context *ctx, Temp vec, Operand offset, Temp dst)
370 {
371 Builder bld(ctx->program, ctx->block);
372 Operand shift;
373 Temp select = Temp();
374 if (offset.isConstant()) {
375 assert(offset.constantValue() && offset.constantValue() < 4);
376 shift = Operand(offset.constantValue() * 8);
377 } else {
378 /* bit_offset = 8 * (offset & 0x3) */
379 Temp tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), offset, Operand(3u));
380 select = bld.tmp(s1);
381 shift = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.scc(Definition(select)), tmp, Operand(3u));
382 }
383
384 if (vec.size() == 1) {
385 bld.sop2(aco_opcode::s_lshr_b32, Definition(dst), bld.def(s1, scc), vec, shift);
386 } else if (vec.size() == 2) {
387 Temp tmp = dst.size() == 2 ? dst : bld.tmp(s2);
388 bld.sop2(aco_opcode::s_lshr_b64, Definition(tmp), bld.def(s1, scc), vec, shift);
389 if (tmp == dst)
390 emit_split_vector(ctx, dst, 2);
391 else
392 emit_extract_vector(ctx, tmp, 0, dst);
393 } else if (vec.size() == 4) {
394 Temp lo = bld.tmp(s2), hi = bld.tmp(s2);
395 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), vec);
396 hi = bld.pseudo(aco_opcode::p_extract_vector, bld.def(s1), hi, Operand(0u));
397 if (select != Temp())
398 hi = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), hi, Operand(0u), select);
399 lo = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), lo, shift);
400 Temp mid = bld.tmp(s1);
401 lo = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), Definition(mid), lo);
402 hi = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), hi, shift);
403 mid = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), hi, mid);
404 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, mid);
405 emit_split_vector(ctx, dst, 2);
406 }
407 }
408
409 /* this function trims subdword vectors:
410 * if dst is vgpr - split the src and create a shrunk version according to the mask.
411 * if dst is sgpr - split the src, but move the original to sgpr. */
412 void trim_subdword_vector(isel_context *ctx, Temp vec_src, Temp dst, unsigned num_components, unsigned mask)
413 {
414 assert(vec_src.type() == RegType::vgpr);
415 emit_split_vector(ctx, vec_src, num_components);
416
417 Builder bld(ctx->program, ctx->block);
418 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
419 unsigned component_size = vec_src.bytes() / num_components;
420 RegClass rc = RegClass(RegType::vgpr, component_size).as_subdword();
421
422 unsigned k = 0;
423 for (unsigned i = 0; i < num_components; i++) {
424 if (mask & (1 << i))
425 elems[k++] = emit_extract_vector(ctx, vec_src, i, rc);
426 }
427
428 if (dst.type() == RegType::vgpr) {
429 assert(dst.bytes() == k * component_size);
430 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, k, 1)};
431 for (unsigned i = 0; i < k; i++)
432 vec->operands[i] = Operand(elems[i]);
433 vec->definitions[0] = Definition(dst);
434 bld.insert(std::move(vec));
435 } else {
436 // TODO: alignbyte if mask doesn't start with 1?
437 assert(mask & 1);
438 assert(dst.size() == vec_src.size());
439 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec_src);
440 }
441 ctx->allocated_vec.emplace(dst.id(), elems);
442 }
443
444 Temp bool_to_vector_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s2))
445 {
446 Builder bld(ctx->program, ctx->block);
447 if (!dst.id())
448 dst = bld.tmp(bld.lm);
449
450 assert(val.regClass() == s1);
451 assert(dst.regClass() == bld.lm);
452
453 return bld.sop2(Builder::s_cselect, Definition(dst), Operand((uint32_t) -1), Operand(0u), bld.scc(val));
454 }
455
456 Temp bool_to_scalar_condition(isel_context *ctx, Temp val, Temp dst = Temp(0, s1))
457 {
458 Builder bld(ctx->program, ctx->block);
459 if (!dst.id())
460 dst = bld.tmp(s1);
461
462 assert(val.regClass() == bld.lm);
463 assert(dst.regClass() == s1);
464
465 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
466 Temp tmp = bld.tmp(s1);
467 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.scc(Definition(tmp)), val, Operand(exec, bld.lm));
468 return emit_wqm(ctx, tmp, dst);
469 }
470
471 Temp get_alu_src(struct isel_context *ctx, nir_alu_src src, unsigned size=1)
472 {
473 if (src.src.ssa->num_components == 1 && src.swizzle[0] == 0 && size == 1)
474 return get_ssa_temp(ctx, src.src.ssa);
475
476 if (src.src.ssa->num_components == size) {
477 bool identity_swizzle = true;
478 for (unsigned i = 0; identity_swizzle && i < size; i++) {
479 if (src.swizzle[i] != i)
480 identity_swizzle = false;
481 }
482 if (identity_swizzle)
483 return get_ssa_temp(ctx, src.src.ssa);
484 }
485
486 Temp vec = get_ssa_temp(ctx, src.src.ssa);
487 unsigned elem_size = vec.bytes() / src.src.ssa->num_components;
488 assert(elem_size > 0);
489 assert(vec.bytes() % elem_size == 0);
490
491 if (elem_size < 4 && vec.type() == RegType::sgpr) {
492 assert(src.src.ssa->bit_size == 8 || src.src.ssa->bit_size == 16);
493 assert(size == 1);
494 unsigned swizzle = src.swizzle[0];
495 if (vec.size() > 1) {
496 assert(src.src.ssa->bit_size == 16);
497 vec = emit_extract_vector(ctx, vec, swizzle / 2, s1);
498 swizzle = swizzle & 1;
499 }
500 if (swizzle == 0)
501 return vec;
502
503 Temp dst{ctx->program->allocateId(), s1};
504 aco_ptr<SOP2_instruction> bfe{create_instruction<SOP2_instruction>(aco_opcode::s_bfe_u32, Format::SOP2, 2, 1)};
505 bfe->operands[0] = Operand(vec);
506 bfe->operands[1] = Operand(uint32_t((src.src.ssa->bit_size << 16) | (src.src.ssa->bit_size * swizzle)));
507 bfe->definitions[0] = Definition(dst);
508 ctx->block->instructions.emplace_back(std::move(bfe));
509 return dst;
510 }
511
512 RegClass elem_rc = elem_size < 4 ? RegClass(vec.type(), elem_size).as_subdword() : RegClass(vec.type(), elem_size / 4);
513 if (size == 1) {
514 return emit_extract_vector(ctx, vec, src.swizzle[0], elem_rc);
515 } else {
516 assert(size <= 4);
517 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
518 aco_ptr<Pseudo_instruction> vec_instr{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
519 for (unsigned i = 0; i < size; ++i) {
520 elems[i] = emit_extract_vector(ctx, vec, src.swizzle[i], elem_rc);
521 vec_instr->operands[i] = Operand{elems[i]};
522 }
523 Temp dst{ctx->program->allocateId(), RegClass(vec.type(), elem_size * size / 4)};
524 vec_instr->definitions[0] = Definition(dst);
525 ctx->block->instructions.emplace_back(std::move(vec_instr));
526 ctx->allocated_vec.emplace(dst.id(), elems);
527 return dst;
528 }
529 }
530
531 Temp convert_pointer_to_64_bit(isel_context *ctx, Temp ptr)
532 {
533 if (ptr.size() == 2)
534 return ptr;
535 Builder bld(ctx->program, ctx->block);
536 if (ptr.type() == RegType::vgpr)
537 ptr = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), ptr);
538 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s2),
539 ptr, Operand((unsigned)ctx->options->address32_hi));
540 }
541
542 void emit_sop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst, bool writes_scc)
543 {
544 aco_ptr<SOP2_instruction> sop2{create_instruction<SOP2_instruction>(op, Format::SOP2, 2, writes_scc ? 2 : 1)};
545 sop2->operands[0] = Operand(get_alu_src(ctx, instr->src[0]));
546 sop2->operands[1] = Operand(get_alu_src(ctx, instr->src[1]));
547 sop2->definitions[0] = Definition(dst);
548 if (writes_scc)
549 sop2->definitions[1] = Definition(ctx->program->allocateId(), scc, s1);
550 ctx->block->instructions.emplace_back(std::move(sop2));
551 }
552
553 void emit_vop2_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst,
554 bool commutative, bool swap_srcs=false, bool flush_denorms = false)
555 {
556 Builder bld(ctx->program, ctx->block);
557 Temp src0 = get_alu_src(ctx, instr->src[swap_srcs ? 1 : 0]);
558 Temp src1 = get_alu_src(ctx, instr->src[swap_srcs ? 0 : 1]);
559 if (src1.type() == RegType::sgpr) {
560 if (commutative && src0.type() == RegType::vgpr) {
561 Temp t = src0;
562 src0 = src1;
563 src1 = t;
564 } else if (src0.type() == RegType::vgpr &&
565 op != aco_opcode::v_madmk_f32 &&
566 op != aco_opcode::v_madak_f32 &&
567 op != aco_opcode::v_madmk_f16 &&
568 op != aco_opcode::v_madak_f16) {
569 /* If the instruction is not commutative, we emit a VOP3A instruction */
570 bld.vop2_e64(op, Definition(dst), src0, src1);
571 return;
572 } else {
573 src1 = bld.copy(bld.def(RegType::vgpr, src1.size()), src1); //TODO: as_vgpr
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_f32:
630 op = aco_opcode::v_cmp_gt_f32;
631 break;
632 case aco_opcode::v_cmp_ge_f32:
633 op = aco_opcode::v_cmp_le_f32;
634 break;
635 case aco_opcode::v_cmp_lt_i32:
636 op = aco_opcode::v_cmp_gt_i32;
637 break;
638 case aco_opcode::v_cmp_ge_i32:
639 op = aco_opcode::v_cmp_le_i32;
640 break;
641 case aco_opcode::v_cmp_lt_u32:
642 op = aco_opcode::v_cmp_gt_u32;
643 break;
644 case aco_opcode::v_cmp_ge_u32:
645 op = aco_opcode::v_cmp_le_u32;
646 break;
647 case aco_opcode::v_cmp_lt_f64:
648 op = aco_opcode::v_cmp_gt_f64;
649 break;
650 case aco_opcode::v_cmp_ge_f64:
651 op = aco_opcode::v_cmp_le_f64;
652 break;
653 case aco_opcode::v_cmp_lt_i64:
654 op = aco_opcode::v_cmp_gt_i64;
655 break;
656 case aco_opcode::v_cmp_ge_i64:
657 op = aco_opcode::v_cmp_le_i64;
658 break;
659 case aco_opcode::v_cmp_lt_u64:
660 op = aco_opcode::v_cmp_gt_u64;
661 break;
662 case aco_opcode::v_cmp_ge_u64:
663 op = aco_opcode::v_cmp_le_u64;
664 break;
665 default: /* eq and ne are commutative */
666 break;
667 }
668 Temp t = src0;
669 src0 = src1;
670 src1 = t;
671 } else {
672 src1 = as_vgpr(ctx, src1);
673 }
674 }
675
676 Builder bld(ctx->program, ctx->block);
677 bld.vopc(op, bld.hint_vcc(Definition(dst)), src0, src1);
678 }
679
680 void emit_sopc_instruction(isel_context *ctx, nir_alu_instr *instr, aco_opcode op, Temp dst)
681 {
682 Temp src0 = get_alu_src(ctx, instr->src[0]);
683 Temp src1 = get_alu_src(ctx, instr->src[1]);
684 Builder bld(ctx->program, ctx->block);
685
686 assert(dst.regClass() == bld.lm);
687 assert(src0.type() == RegType::sgpr);
688 assert(src1.type() == RegType::sgpr);
689 assert(src0.regClass() == src1.regClass());
690
691 /* Emit the SALU comparison instruction */
692 Temp cmp = bld.sopc(op, bld.scc(bld.def(s1)), src0, src1);
693 /* Turn the result into a per-lane bool */
694 bool_to_vector_condition(ctx, cmp, dst);
695 }
696
697 void emit_comparison(isel_context *ctx, nir_alu_instr *instr, Temp dst,
698 aco_opcode v32_op, aco_opcode v64_op, aco_opcode s32_op = aco_opcode::num_opcodes, aco_opcode s64_op = aco_opcode::num_opcodes)
699 {
700 aco_opcode s_op = instr->src[0].src.ssa->bit_size == 64 ? s64_op : s32_op;
701 aco_opcode v_op = instr->src[0].src.ssa->bit_size == 64 ? v64_op : v32_op;
702 bool divergent_vals = ctx->divergent_vals[instr->dest.dest.ssa.index];
703 bool use_valu = s_op == aco_opcode::num_opcodes ||
704 divergent_vals ||
705 ctx->allocated[instr->src[0].src.ssa->index].type() == RegType::vgpr ||
706 ctx->allocated[instr->src[1].src.ssa->index].type() == RegType::vgpr;
707 aco_opcode op = use_valu ? v_op : s_op;
708 assert(op != aco_opcode::num_opcodes);
709 assert(dst.regClass() == ctx->program->lane_mask);
710
711 if (use_valu)
712 emit_vopc_instruction(ctx, instr, op, dst);
713 else
714 emit_sopc_instruction(ctx, instr, op, dst);
715 }
716
717 void emit_boolean_logic(isel_context *ctx, nir_alu_instr *instr, Builder::WaveSpecificOpcode op, Temp dst)
718 {
719 Builder bld(ctx->program, ctx->block);
720 Temp src0 = get_alu_src(ctx, instr->src[0]);
721 Temp src1 = get_alu_src(ctx, instr->src[1]);
722
723 assert(dst.regClass() == bld.lm);
724 assert(src0.regClass() == bld.lm);
725 assert(src1.regClass() == bld.lm);
726
727 bld.sop2(op, Definition(dst), bld.def(s1, scc), src0, src1);
728 }
729
730 void emit_bcsel(isel_context *ctx, nir_alu_instr *instr, Temp dst)
731 {
732 Builder bld(ctx->program, ctx->block);
733 Temp cond = get_alu_src(ctx, instr->src[0]);
734 Temp then = get_alu_src(ctx, instr->src[1]);
735 Temp els = get_alu_src(ctx, instr->src[2]);
736
737 assert(cond.regClass() == bld.lm);
738
739 if (dst.type() == RegType::vgpr) {
740 aco_ptr<Instruction> bcsel;
741 if (dst.size() == 1) {
742 then = as_vgpr(ctx, then);
743 els = as_vgpr(ctx, els);
744
745 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), els, then, cond);
746 } else if (dst.size() == 2) {
747 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
748 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), then);
749 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
750 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), els);
751
752 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, cond);
753 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, cond);
754
755 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
756 } else {
757 fprintf(stderr, "Unimplemented NIR instr bit size: ");
758 nir_print_instr(&instr->instr, stderr);
759 fprintf(stderr, "\n");
760 }
761 return;
762 }
763
764 if (instr->dest.dest.ssa.bit_size == 1) {
765 assert(dst.regClass() == bld.lm);
766 assert(then.regClass() == bld.lm);
767 assert(els.regClass() == bld.lm);
768 }
769
770 if (!ctx->divergent_vals[instr->src[0].src.ssa->index]) { /* uniform condition and values in sgpr */
771 if (dst.regClass() == s1 || dst.regClass() == s2) {
772 assert((then.regClass() == s1 || then.regClass() == s2) && els.regClass() == then.regClass());
773 assert(dst.size() == then.size());
774 aco_opcode op = dst.regClass() == s1 ? aco_opcode::s_cselect_b32 : aco_opcode::s_cselect_b64;
775 bld.sop2(op, Definition(dst), then, els, bld.scc(bool_to_scalar_condition(ctx, cond)));
776 } else {
777 fprintf(stderr, "Unimplemented uniform bcsel bit size: ");
778 nir_print_instr(&instr->instr, stderr);
779 fprintf(stderr, "\n");
780 }
781 return;
782 }
783
784 /* divergent boolean bcsel
785 * this implements bcsel on bools: dst = s0 ? s1 : s2
786 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
787 assert(instr->dest.dest.ssa.bit_size == 1);
788
789 if (cond.id() != then.id())
790 then = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cond, then);
791
792 if (cond.id() == els.id())
793 bld.sop1(Builder::s_mov, Definition(dst), then);
794 else
795 bld.sop2(Builder::s_or, Definition(dst), bld.def(s1, scc), then,
796 bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), els, cond));
797 }
798
799 void emit_scaled_op(isel_context *ctx, Builder& bld, Definition dst, Temp val,
800 aco_opcode op, uint32_t undo)
801 {
802 /* multiply by 16777216 to handle denormals */
803 Temp is_denormal = bld.vopc(aco_opcode::v_cmp_class_f32, bld.hint_vcc(bld.def(bld.lm)),
804 as_vgpr(ctx, val), bld.copy(bld.def(v1), Operand((1u << 7) | (1u << 4))));
805 Temp scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x4b800000u), val);
806 scaled = bld.vop1(op, bld.def(v1), scaled);
807 scaled = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(undo), scaled);
808
809 Temp not_scaled = bld.vop1(op, bld.def(v1), val);
810
811 bld.vop2(aco_opcode::v_cndmask_b32, dst, not_scaled, scaled, is_denormal);
812 }
813
814 void emit_rcp(isel_context *ctx, Builder& bld, Definition dst, Temp val)
815 {
816 if (ctx->block->fp_mode.denorm32 == 0) {
817 bld.vop1(aco_opcode::v_rcp_f32, dst, val);
818 return;
819 }
820
821 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rcp_f32, 0x4b800000u);
822 }
823
824 void emit_rsq(isel_context *ctx, Builder& bld, Definition dst, Temp val)
825 {
826 if (ctx->block->fp_mode.denorm32 == 0) {
827 bld.vop1(aco_opcode::v_rsq_f32, dst, val);
828 return;
829 }
830
831 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_rsq_f32, 0x45800000u);
832 }
833
834 void emit_sqrt(isel_context *ctx, Builder& bld, Definition dst, Temp val)
835 {
836 if (ctx->block->fp_mode.denorm32 == 0) {
837 bld.vop1(aco_opcode::v_sqrt_f32, dst, val);
838 return;
839 }
840
841 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_sqrt_f32, 0x39800000u);
842 }
843
844 void emit_log2(isel_context *ctx, Builder& bld, Definition dst, Temp val)
845 {
846 if (ctx->block->fp_mode.denorm32 == 0) {
847 bld.vop1(aco_opcode::v_log_f32, dst, val);
848 return;
849 }
850
851 emit_scaled_op(ctx, bld, dst, val, aco_opcode::v_log_f32, 0xc1c00000u);
852 }
853
854 Temp emit_trunc_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
855 {
856 if (ctx->options->chip_class >= GFX7)
857 return bld.vop1(aco_opcode::v_trunc_f64, Definition(dst), val);
858
859 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
860 /* TODO: create more efficient code! */
861 if (val.type() == RegType::sgpr)
862 val = as_vgpr(ctx, val);
863
864 /* Split the input value. */
865 Temp val_lo = bld.tmp(v1), val_hi = bld.tmp(v1);
866 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
867
868 /* Extract the exponent and compute the unbiased value. */
869 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f64, bld.def(v1), val);
870
871 /* Extract the fractional part. */
872 Temp fract_mask = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x000fffffu));
873 fract_mask = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), fract_mask, exponent);
874
875 Temp fract_mask_lo = bld.tmp(v1), fract_mask_hi = bld.tmp(v1);
876 bld.pseudo(aco_opcode::p_split_vector, Definition(fract_mask_lo), Definition(fract_mask_hi), fract_mask);
877
878 Temp fract_lo = bld.tmp(v1), fract_hi = bld.tmp(v1);
879 Temp tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_lo);
880 fract_lo = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_lo, tmp);
881 tmp = bld.vop1(aco_opcode::v_not_b32, bld.def(v1), fract_mask_hi);
882 fract_hi = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), val_hi, tmp);
883
884 /* Get the sign bit. */
885 Temp sign = bld.vop2(aco_opcode::v_ashr_i32, bld.def(v1), Operand(31u), val_hi);
886
887 /* Decide the operation to apply depending on the unbiased exponent. */
888 Temp exp_lt0 = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)), exponent, Operand(0u));
889 Temp dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_lo, bld.copy(bld.def(v1), Operand(0u)), exp_lt0);
890 Temp dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), fract_hi, sign, exp_lt0);
891 Temp exp_gt51 = bld.vopc_e64(aco_opcode::v_cmp_gt_i32, bld.def(s2), exponent, Operand(51u));
892 dst_lo = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_lo, val_lo, exp_gt51);
893 dst_hi = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), dst_hi, val_hi, exp_gt51);
894
895 return bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst_lo, dst_hi);
896 }
897
898 Temp emit_floor_f64(isel_context *ctx, Builder& bld, Definition dst, Temp val)
899 {
900 if (ctx->options->chip_class >= GFX7)
901 return bld.vop1(aco_opcode::v_floor_f64, Definition(dst), val);
902
903 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
904 Temp src0 = as_vgpr(ctx, val);
905
906 Temp mask = bld.copy(bld.def(s1), Operand(3u)); /* isnan */
907 Temp min_val = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(-1u), Operand(0x3fefffffu));
908
909 Temp isnan = bld.vopc_e64(aco_opcode::v_cmp_class_f64, bld.hint_vcc(bld.def(bld.lm)), src0, mask);
910 Temp fract = bld.vop1(aco_opcode::v_fract_f64, bld.def(v2), src0);
911 Temp min = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), fract, min_val);
912
913 Temp then_lo = bld.tmp(v1), then_hi = bld.tmp(v1);
914 bld.pseudo(aco_opcode::p_split_vector, Definition(then_lo), Definition(then_hi), src0);
915 Temp else_lo = bld.tmp(v1), else_hi = bld.tmp(v1);
916 bld.pseudo(aco_opcode::p_split_vector, Definition(else_lo), Definition(else_hi), min);
917
918 Temp dst0 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_lo, then_lo, isnan);
919 Temp dst1 = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), else_hi, then_hi, isnan);
920
921 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), dst0, dst1);
922
923 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src0, v);
924 static_cast<VOP3A_instruction*>(add)->neg[1] = true;
925
926 return add->definitions[0].getTemp();
927 }
928
929 void visit_alu_instr(isel_context *ctx, nir_alu_instr *instr)
930 {
931 if (!instr->dest.dest.is_ssa) {
932 fprintf(stderr, "nir alu dst not in ssa: ");
933 nir_print_instr(&instr->instr, stderr);
934 fprintf(stderr, "\n");
935 abort();
936 }
937 Builder bld(ctx->program, ctx->block);
938 Temp dst = get_ssa_temp(ctx, &instr->dest.dest.ssa);
939 switch(instr->op) {
940 case nir_op_vec2:
941 case nir_op_vec3:
942 case nir_op_vec4: {
943 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
944 unsigned num = instr->dest.dest.ssa.num_components;
945 for (unsigned i = 0; i < num; ++i)
946 elems[i] = get_alu_src(ctx, instr->src[i]);
947
948 if (instr->dest.dest.ssa.bit_size >= 32 || dst.type() == RegType::vgpr) {
949 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.dest.ssa.num_components, 1)};
950 for (unsigned i = 0; i < num; ++i)
951 vec->operands[i] = Operand{elems[i]};
952 vec->definitions[0] = Definition(dst);
953 ctx->block->instructions.emplace_back(std::move(vec));
954 ctx->allocated_vec.emplace(dst.id(), elems);
955 } else {
956 // TODO: that is a bit suboptimal..
957 Temp mask = bld.copy(bld.def(s1), Operand((1u << instr->dest.dest.ssa.bit_size) - 1));
958 for (unsigned i = 0; i < num - 1; ++i)
959 if (((i+1) * instr->dest.dest.ssa.bit_size) % 32)
960 elems[i] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), elems[i], mask);
961 for (unsigned i = 0; i < num; ++i) {
962 unsigned bit = i * instr->dest.dest.ssa.bit_size;
963 if (bit % 32 == 0) {
964 elems[bit / 32] = elems[i];
965 } else {
966 elems[i] = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc),
967 elems[i], Operand((i * instr->dest.dest.ssa.bit_size) % 32));
968 elems[bit / 32] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), elems[bit / 32], elems[i]);
969 }
970 }
971 if (dst.size() == 1)
972 bld.copy(Definition(dst), elems[0]);
973 else
974 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), elems[0], elems[1]);
975 }
976 break;
977 }
978 case nir_op_mov: {
979 Temp src = get_alu_src(ctx, instr->src[0]);
980 aco_ptr<Instruction> mov;
981 if (dst.type() == RegType::sgpr) {
982 if (src.type() == RegType::vgpr)
983 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
984 else if (src.regClass() == s1)
985 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
986 else if (src.regClass() == s2)
987 bld.sop1(aco_opcode::s_mov_b64, Definition(dst), src);
988 else
989 unreachable("wrong src register class for nir_op_imov");
990 } else if (dst.regClass() == v1) {
991 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), src);
992 } else if (dst.regClass() == v2) {
993 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
994 } else {
995 nir_print_instr(&instr->instr, stderr);
996 unreachable("Should have been lowered to scalar.");
997 }
998 break;
999 }
1000 case nir_op_inot: {
1001 Temp src = get_alu_src(ctx, instr->src[0]);
1002 if (instr->dest.dest.ssa.bit_size == 1) {
1003 assert(src.regClass() == bld.lm);
1004 assert(dst.regClass() == bld.lm);
1005 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1006 Temp tmp = bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), src);
1007 bld.sop2(Builder::s_and, Definition(dst), bld.def(s1, scc), tmp, Operand(exec, bld.lm));
1008 } else if (dst.regClass() == v1) {
1009 emit_vop1_instruction(ctx, instr, aco_opcode::v_not_b32, dst);
1010 } else if (dst.type() == RegType::sgpr) {
1011 aco_opcode opcode = dst.size() == 1 ? aco_opcode::s_not_b32 : aco_opcode::s_not_b64;
1012 bld.sop1(opcode, Definition(dst), bld.def(s1, scc), src);
1013 } else {
1014 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1015 nir_print_instr(&instr->instr, stderr);
1016 fprintf(stderr, "\n");
1017 }
1018 break;
1019 }
1020 case nir_op_ineg: {
1021 Temp src = get_alu_src(ctx, instr->src[0]);
1022 if (dst.regClass() == v1) {
1023 bld.vsub32(Definition(dst), Operand(0u), Operand(src));
1024 } else if (dst.regClass() == s1) {
1025 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand((uint32_t) -1), src);
1026 } else if (dst.size() == 2) {
1027 Temp src0 = bld.tmp(dst.type(), 1);
1028 Temp src1 = bld.tmp(dst.type(), 1);
1029 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
1030
1031 if (dst.regClass() == s2) {
1032 Temp carry = bld.tmp(s1);
1033 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), Operand(0u), src0);
1034 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), src1, carry);
1035 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1036 } else {
1037 Temp lower = bld.tmp(v1);
1038 Temp borrow = bld.vsub32(Definition(lower), Operand(0u), src0, true).def(1).getTemp();
1039 Temp upper = bld.vsub32(bld.def(v1), Operand(0u), src1, false, borrow);
1040 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1041 }
1042 } else {
1043 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1044 nir_print_instr(&instr->instr, stderr);
1045 fprintf(stderr, "\n");
1046 }
1047 break;
1048 }
1049 case nir_op_iabs: {
1050 if (dst.regClass() == s1) {
1051 bld.sop1(aco_opcode::s_abs_i32, Definition(dst), bld.def(s1, scc), get_alu_src(ctx, instr->src[0]));
1052 } else if (dst.regClass() == v1) {
1053 Temp src = get_alu_src(ctx, instr->src[0]);
1054 bld.vop2(aco_opcode::v_max_i32, Definition(dst), src, bld.vsub32(bld.def(v1), Operand(0u), src));
1055 } else {
1056 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1057 nir_print_instr(&instr->instr, stderr);
1058 fprintf(stderr, "\n");
1059 }
1060 break;
1061 }
1062 case nir_op_isign: {
1063 Temp src = get_alu_src(ctx, instr->src[0]);
1064 if (dst.regClass() == s1) {
1065 Temp tmp = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
1066 Temp gtz = bld.sopc(aco_opcode::s_cmp_gt_i32, bld.def(s1, scc), src, Operand(0u));
1067 bld.sop2(aco_opcode::s_add_i32, Definition(dst), bld.def(s1, scc), gtz, tmp);
1068 } else if (dst.regClass() == s2) {
1069 Temp neg = bld.sop2(aco_opcode::s_ashr_i64, bld.def(s2), bld.def(s1, scc), src, Operand(63u));
1070 Temp neqz;
1071 if (ctx->program->chip_class >= GFX8)
1072 neqz = bld.sopc(aco_opcode::s_cmp_lg_u64, bld.def(s1, scc), src, Operand(0u));
1073 else
1074 neqz = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), src, Operand(0u)).def(1).getTemp();
1075 /* SCC gets zero-extended to 64 bit */
1076 bld.sop2(aco_opcode::s_or_b64, Definition(dst), bld.def(s1, scc), neg, bld.scc(neqz));
1077 } else if (dst.regClass() == v1) {
1078 Temp tmp = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
1079 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1080 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(1u), tmp, gtz);
1081 } else if (dst.regClass() == v2) {
1082 Temp upper = emit_extract_vector(ctx, src, 1, v1);
1083 Temp neg = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), upper);
1084 Temp gtz = bld.vopc(aco_opcode::v_cmp_ge_i64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
1085 Temp lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(1u), neg, gtz);
1086 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), neg, gtz);
1087 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1088 } else {
1089 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1090 nir_print_instr(&instr->instr, stderr);
1091 fprintf(stderr, "\n");
1092 }
1093 break;
1094 }
1095 case nir_op_imax: {
1096 if (dst.regClass() == v1) {
1097 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_i32, dst, true);
1098 } else if (dst.regClass() == s1) {
1099 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_i32, dst, true);
1100 } else {
1101 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1102 nir_print_instr(&instr->instr, stderr);
1103 fprintf(stderr, "\n");
1104 }
1105 break;
1106 }
1107 case nir_op_umax: {
1108 if (dst.regClass() == v1) {
1109 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_u32, dst, true);
1110 } else if (dst.regClass() == s1) {
1111 emit_sop2_instruction(ctx, instr, aco_opcode::s_max_u32, dst, true);
1112 } else {
1113 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1114 nir_print_instr(&instr->instr, stderr);
1115 fprintf(stderr, "\n");
1116 }
1117 break;
1118 }
1119 case nir_op_imin: {
1120 if (dst.regClass() == v1) {
1121 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_i32, dst, true);
1122 } else if (dst.regClass() == s1) {
1123 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_i32, dst, true);
1124 } else {
1125 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1126 nir_print_instr(&instr->instr, stderr);
1127 fprintf(stderr, "\n");
1128 }
1129 break;
1130 }
1131 case nir_op_umin: {
1132 if (dst.regClass() == v1) {
1133 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_u32, dst, true);
1134 } else if (dst.regClass() == s1) {
1135 emit_sop2_instruction(ctx, instr, aco_opcode::s_min_u32, dst, true);
1136 } else {
1137 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1138 nir_print_instr(&instr->instr, stderr);
1139 fprintf(stderr, "\n");
1140 }
1141 break;
1142 }
1143 case nir_op_ior: {
1144 if (instr->dest.dest.ssa.bit_size == 1) {
1145 emit_boolean_logic(ctx, instr, Builder::s_or, dst);
1146 } else if (dst.regClass() == v1) {
1147 emit_vop2_instruction(ctx, instr, aco_opcode::v_or_b32, dst, true);
1148 } else if (dst.regClass() == s1) {
1149 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b32, dst, true);
1150 } else if (dst.regClass() == s2) {
1151 emit_sop2_instruction(ctx, instr, aco_opcode::s_or_b64, dst, true);
1152 } else {
1153 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1154 nir_print_instr(&instr->instr, stderr);
1155 fprintf(stderr, "\n");
1156 }
1157 break;
1158 }
1159 case nir_op_iand: {
1160 if (instr->dest.dest.ssa.bit_size == 1) {
1161 emit_boolean_logic(ctx, instr, Builder::s_and, dst);
1162 } else if (dst.regClass() == v1) {
1163 emit_vop2_instruction(ctx, instr, aco_opcode::v_and_b32, dst, true);
1164 } else if (dst.regClass() == s1) {
1165 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b32, dst, true);
1166 } else if (dst.regClass() == s2) {
1167 emit_sop2_instruction(ctx, instr, aco_opcode::s_and_b64, dst, true);
1168 } else {
1169 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1170 nir_print_instr(&instr->instr, stderr);
1171 fprintf(stderr, "\n");
1172 }
1173 break;
1174 }
1175 case nir_op_ixor: {
1176 if (instr->dest.dest.ssa.bit_size == 1) {
1177 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
1178 } else if (dst.regClass() == v1) {
1179 emit_vop2_instruction(ctx, instr, aco_opcode::v_xor_b32, dst, true);
1180 } else if (dst.regClass() == s1) {
1181 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b32, dst, true);
1182 } else if (dst.regClass() == s2) {
1183 emit_sop2_instruction(ctx, instr, aco_opcode::s_xor_b64, dst, true);
1184 } else {
1185 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1186 nir_print_instr(&instr->instr, stderr);
1187 fprintf(stderr, "\n");
1188 }
1189 break;
1190 }
1191 case nir_op_ushr: {
1192 if (dst.regClass() == v1) {
1193 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshrrev_b32, dst, false, true);
1194 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1195 bld.vop3(aco_opcode::v_lshrrev_b64, Definition(dst),
1196 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1197 } else if (dst.regClass() == v2) {
1198 bld.vop3(aco_opcode::v_lshr_b64, Definition(dst),
1199 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1200 } else if (dst.regClass() == s2) {
1201 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b64, dst, true);
1202 } else if (dst.regClass() == s1) {
1203 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshr_b32, dst, true);
1204 } else {
1205 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1206 nir_print_instr(&instr->instr, stderr);
1207 fprintf(stderr, "\n");
1208 }
1209 break;
1210 }
1211 case nir_op_ishl: {
1212 if (dst.regClass() == v1) {
1213 emit_vop2_instruction(ctx, instr, aco_opcode::v_lshlrev_b32, dst, false, true);
1214 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1215 bld.vop3(aco_opcode::v_lshlrev_b64, Definition(dst),
1216 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1217 } else if (dst.regClass() == v2) {
1218 bld.vop3(aco_opcode::v_lshl_b64, Definition(dst),
1219 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1220 } else if (dst.regClass() == s1) {
1221 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b32, dst, true);
1222 } else if (dst.regClass() == s2) {
1223 emit_sop2_instruction(ctx, instr, aco_opcode::s_lshl_b64, dst, true);
1224 } else {
1225 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1226 nir_print_instr(&instr->instr, stderr);
1227 fprintf(stderr, "\n");
1228 }
1229 break;
1230 }
1231 case nir_op_ishr: {
1232 if (dst.regClass() == v1) {
1233 emit_vop2_instruction(ctx, instr, aco_opcode::v_ashrrev_i32, dst, false, true);
1234 } else if (dst.regClass() == v2 && ctx->program->chip_class >= GFX8) {
1235 bld.vop3(aco_opcode::v_ashrrev_i64, Definition(dst),
1236 get_alu_src(ctx, instr->src[1]), get_alu_src(ctx, instr->src[0]));
1237 } else if (dst.regClass() == v2) {
1238 bld.vop3(aco_opcode::v_ashr_i64, Definition(dst),
1239 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1240 } else if (dst.regClass() == s1) {
1241 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i32, dst, true);
1242 } else if (dst.regClass() == s2) {
1243 emit_sop2_instruction(ctx, instr, aco_opcode::s_ashr_i64, dst, true);
1244 } else {
1245 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1246 nir_print_instr(&instr->instr, stderr);
1247 fprintf(stderr, "\n");
1248 }
1249 break;
1250 }
1251 case nir_op_find_lsb: {
1252 Temp src = get_alu_src(ctx, instr->src[0]);
1253 if (src.regClass() == s1) {
1254 bld.sop1(aco_opcode::s_ff1_i32_b32, Definition(dst), src);
1255 } else if (src.regClass() == v1) {
1256 emit_vop1_instruction(ctx, instr, aco_opcode::v_ffbl_b32, dst);
1257 } else if (src.regClass() == s2) {
1258 bld.sop1(aco_opcode::s_ff1_i32_b64, Definition(dst), src);
1259 } else {
1260 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1261 nir_print_instr(&instr->instr, stderr);
1262 fprintf(stderr, "\n");
1263 }
1264 break;
1265 }
1266 case nir_op_ufind_msb:
1267 case nir_op_ifind_msb: {
1268 Temp src = get_alu_src(ctx, instr->src[0]);
1269 if (src.regClass() == s1 || src.regClass() == s2) {
1270 aco_opcode op = src.regClass() == s2 ?
1271 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b64 : aco_opcode::s_flbit_i32_i64) :
1272 (instr->op == nir_op_ufind_msb ? aco_opcode::s_flbit_i32_b32 : aco_opcode::s_flbit_i32);
1273 Temp msb_rev = bld.sop1(op, bld.def(s1), src);
1274
1275 Builder::Result sub = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc),
1276 Operand(src.size() * 32u - 1u), msb_rev);
1277 Temp msb = sub.def(0).getTemp();
1278 Temp carry = sub.def(1).getTemp();
1279
1280 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t)-1), msb, bld.scc(carry));
1281 } else if (src.regClass() == v1) {
1282 aco_opcode op = instr->op == nir_op_ufind_msb ? aco_opcode::v_ffbh_u32 : aco_opcode::v_ffbh_i32;
1283 Temp msb_rev = bld.tmp(v1);
1284 emit_vop1_instruction(ctx, instr, op, msb_rev);
1285 Temp msb = bld.tmp(v1);
1286 Temp carry = bld.vsub32(Definition(msb), Operand(31u), Operand(msb_rev), true).def(1).getTemp();
1287 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), msb, Operand((uint32_t)-1), carry);
1288 } else {
1289 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1290 nir_print_instr(&instr->instr, stderr);
1291 fprintf(stderr, "\n");
1292 }
1293 break;
1294 }
1295 case nir_op_bitfield_reverse: {
1296 if (dst.regClass() == s1) {
1297 bld.sop1(aco_opcode::s_brev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1298 } else if (dst.regClass() == v1) {
1299 bld.vop1(aco_opcode::v_bfrev_b32, Definition(dst), get_alu_src(ctx, instr->src[0]));
1300 } else {
1301 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1302 nir_print_instr(&instr->instr, stderr);
1303 fprintf(stderr, "\n");
1304 }
1305 break;
1306 }
1307 case nir_op_iadd: {
1308 if (dst.regClass() == s1) {
1309 emit_sop2_instruction(ctx, instr, aco_opcode::s_add_u32, dst, true);
1310 break;
1311 }
1312
1313 Temp src0 = get_alu_src(ctx, instr->src[0]);
1314 Temp src1 = get_alu_src(ctx, instr->src[1]);
1315 if (dst.regClass() == v1) {
1316 bld.vadd32(Definition(dst), Operand(src0), Operand(src1));
1317 break;
1318 }
1319
1320 assert(src0.size() == 2 && src1.size() == 2);
1321 Temp src00 = bld.tmp(src0.type(), 1);
1322 Temp src01 = bld.tmp(dst.type(), 1);
1323 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1324 Temp src10 = bld.tmp(src1.type(), 1);
1325 Temp src11 = bld.tmp(dst.type(), 1);
1326 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1327
1328 if (dst.regClass() == s2) {
1329 Temp carry = bld.tmp(s1);
1330 Temp dst0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1331 Temp dst1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), src01, src11, bld.scc(carry));
1332 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1333 } else if (dst.regClass() == v2) {
1334 Temp dst0 = bld.tmp(v1);
1335 Temp carry = bld.vadd32(Definition(dst0), src00, src10, true).def(1).getTemp();
1336 Temp dst1 = bld.vadd32(bld.def(v1), src01, src11, false, carry);
1337 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1338 } else {
1339 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1340 nir_print_instr(&instr->instr, stderr);
1341 fprintf(stderr, "\n");
1342 }
1343 break;
1344 }
1345 case nir_op_uadd_sat: {
1346 Temp src0 = get_alu_src(ctx, instr->src[0]);
1347 Temp src1 = get_alu_src(ctx, instr->src[1]);
1348 if (dst.regClass() == s1) {
1349 Temp tmp = bld.tmp(s1), carry = bld.tmp(s1);
1350 bld.sop2(aco_opcode::s_add_u32, Definition(tmp), bld.scc(Definition(carry)),
1351 src0, src1);
1352 bld.sop2(aco_opcode::s_cselect_b32, Definition(dst), Operand((uint32_t) -1), tmp, bld.scc(carry));
1353 } else if (dst.regClass() == v1) {
1354 if (ctx->options->chip_class >= GFX9) {
1355 aco_ptr<VOP3A_instruction> add{create_instruction<VOP3A_instruction>(aco_opcode::v_add_u32, asVOP3(Format::VOP2), 2, 1)};
1356 add->operands[0] = Operand(src0);
1357 add->operands[1] = Operand(src1);
1358 add->definitions[0] = Definition(dst);
1359 add->clamp = 1;
1360 ctx->block->instructions.emplace_back(std::move(add));
1361 } else {
1362 if (src1.regClass() != v1)
1363 std::swap(src0, src1);
1364 assert(src1.regClass() == v1);
1365 Temp tmp = bld.tmp(v1);
1366 Temp carry = bld.vadd32(Definition(tmp), src0, src1, true).def(1).getTemp();
1367 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), tmp, Operand((uint32_t) -1), carry);
1368 }
1369 } else {
1370 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1371 nir_print_instr(&instr->instr, stderr);
1372 fprintf(stderr, "\n");
1373 }
1374 break;
1375 }
1376 case nir_op_uadd_carry: {
1377 Temp src0 = get_alu_src(ctx, instr->src[0]);
1378 Temp src1 = get_alu_src(ctx, instr->src[1]);
1379 if (dst.regClass() == s1) {
1380 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1381 break;
1382 }
1383 if (dst.regClass() == v1) {
1384 Temp carry = bld.vadd32(bld.def(v1), src0, src1, true).def(1).getTemp();
1385 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), carry);
1386 break;
1387 }
1388
1389 Temp src00 = bld.tmp(src0.type(), 1);
1390 Temp src01 = bld.tmp(dst.type(), 1);
1391 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1392 Temp src10 = bld.tmp(src1.type(), 1);
1393 Temp src11 = bld.tmp(dst.type(), 1);
1394 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1395 if (dst.regClass() == s2) {
1396 Temp carry = bld.tmp(s1);
1397 bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1398 carry = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(carry)).def(1).getTemp();
1399 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1400 } else if (dst.regClass() == v2) {
1401 Temp carry = bld.vadd32(bld.def(v1), src00, src10, true).def(1).getTemp();
1402 carry = bld.vadd32(bld.def(v1), src01, src11, true, carry).def(1).getTemp();
1403 carry = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), carry);
1404 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), carry, Operand(0u));
1405 } else {
1406 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1407 nir_print_instr(&instr->instr, stderr);
1408 fprintf(stderr, "\n");
1409 }
1410 break;
1411 }
1412 case nir_op_isub: {
1413 if (dst.regClass() == s1) {
1414 emit_sop2_instruction(ctx, instr, aco_opcode::s_sub_i32, dst, true);
1415 break;
1416 }
1417
1418 Temp src0 = get_alu_src(ctx, instr->src[0]);
1419 Temp src1 = get_alu_src(ctx, instr->src[1]);
1420 if (dst.regClass() == v1) {
1421 bld.vsub32(Definition(dst), src0, src1);
1422 break;
1423 }
1424
1425 Temp src00 = bld.tmp(src0.type(), 1);
1426 Temp src01 = bld.tmp(dst.type(), 1);
1427 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1428 Temp src10 = bld.tmp(src1.type(), 1);
1429 Temp src11 = bld.tmp(dst.type(), 1);
1430 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1431 if (dst.regClass() == s2) {
1432 Temp carry = bld.tmp(s1);
1433 Temp dst0 = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(carry)), src00, src10);
1434 Temp dst1 = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), src01, src11, carry);
1435 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1436 } else if (dst.regClass() == v2) {
1437 Temp lower = bld.tmp(v1);
1438 Temp borrow = bld.vsub32(Definition(lower), src00, src10, true).def(1).getTemp();
1439 Temp upper = bld.vsub32(bld.def(v1), src01, src11, false, borrow);
1440 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1441 } else {
1442 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1443 nir_print_instr(&instr->instr, stderr);
1444 fprintf(stderr, "\n");
1445 }
1446 break;
1447 }
1448 case nir_op_usub_borrow: {
1449 Temp src0 = get_alu_src(ctx, instr->src[0]);
1450 Temp src1 = get_alu_src(ctx, instr->src[1]);
1451 if (dst.regClass() == s1) {
1452 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(dst)), src0, src1);
1453 break;
1454 } else if (dst.regClass() == v1) {
1455 Temp borrow = bld.vsub32(bld.def(v1), src0, src1, true).def(1).getTemp();
1456 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), borrow);
1457 break;
1458 }
1459
1460 Temp src00 = bld.tmp(src0.type(), 1);
1461 Temp src01 = bld.tmp(dst.type(), 1);
1462 bld.pseudo(aco_opcode::p_split_vector, Definition(src00), Definition(src01), src0);
1463 Temp src10 = bld.tmp(src1.type(), 1);
1464 Temp src11 = bld.tmp(dst.type(), 1);
1465 bld.pseudo(aco_opcode::p_split_vector, Definition(src10), Definition(src11), src1);
1466 if (dst.regClass() == s2) {
1467 Temp borrow = bld.tmp(s1);
1468 bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), src00, src10);
1469 borrow = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.scc(bld.def(s1)), src01, src11, bld.scc(borrow)).def(1).getTemp();
1470 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1471 } else if (dst.regClass() == v2) {
1472 Temp borrow = bld.vsub32(bld.def(v1), src00, src10, true).def(1).getTemp();
1473 borrow = bld.vsub32(bld.def(v1), src01, src11, true, Operand(borrow)).def(1).getTemp();
1474 borrow = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand(1u), borrow);
1475 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), borrow, Operand(0u));
1476 } else {
1477 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1478 nir_print_instr(&instr->instr, stderr);
1479 fprintf(stderr, "\n");
1480 }
1481 break;
1482 }
1483 case nir_op_imul: {
1484 if (dst.regClass() == v1) {
1485 bld.vop3(aco_opcode::v_mul_lo_u32, Definition(dst),
1486 get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1487 } else if (dst.regClass() == s1) {
1488 emit_sop2_instruction(ctx, instr, aco_opcode::s_mul_i32, dst, false);
1489 } else {
1490 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1491 nir_print_instr(&instr->instr, stderr);
1492 fprintf(stderr, "\n");
1493 }
1494 break;
1495 }
1496 case nir_op_umul_high: {
1497 if (dst.regClass() == v1) {
1498 bld.vop3(aco_opcode::v_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1499 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1500 bld.sop2(aco_opcode::s_mul_hi_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1501 } else if (dst.regClass() == s1) {
1502 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1503 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1504 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1505 } else {
1506 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1507 nir_print_instr(&instr->instr, stderr);
1508 fprintf(stderr, "\n");
1509 }
1510 break;
1511 }
1512 case nir_op_imul_high: {
1513 if (dst.regClass() == v1) {
1514 bld.vop3(aco_opcode::v_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1515 } else if (dst.regClass() == s1 && ctx->options->chip_class >= GFX9) {
1516 bld.sop2(aco_opcode::s_mul_hi_i32, Definition(dst), get_alu_src(ctx, instr->src[0]), get_alu_src(ctx, instr->src[1]));
1517 } else if (dst.regClass() == s1) {
1518 Temp tmp = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), get_alu_src(ctx, instr->src[0]),
1519 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1520 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), tmp);
1521 } else {
1522 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1523 nir_print_instr(&instr->instr, stderr);
1524 fprintf(stderr, "\n");
1525 }
1526 break;
1527 }
1528 case nir_op_fmul: {
1529 if (dst.size() == 1) {
1530 emit_vop2_instruction(ctx, instr, aco_opcode::v_mul_f32, dst, true);
1531 } else if (dst.size() == 2) {
1532 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), get_alu_src(ctx, instr->src[0]),
1533 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1534 } else {
1535 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1536 nir_print_instr(&instr->instr, stderr);
1537 fprintf(stderr, "\n");
1538 }
1539 break;
1540 }
1541 case nir_op_fadd: {
1542 if (dst.size() == 1) {
1543 emit_vop2_instruction(ctx, instr, aco_opcode::v_add_f32, dst, true);
1544 } else if (dst.size() == 2) {
1545 bld.vop3(aco_opcode::v_add_f64, Definition(dst), get_alu_src(ctx, instr->src[0]),
1546 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1547 } else {
1548 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1549 nir_print_instr(&instr->instr, stderr);
1550 fprintf(stderr, "\n");
1551 }
1552 break;
1553 }
1554 case nir_op_fsub: {
1555 Temp src0 = get_alu_src(ctx, instr->src[0]);
1556 Temp src1 = get_alu_src(ctx, instr->src[1]);
1557 if (dst.size() == 1) {
1558 if (src1.type() == RegType::vgpr || src0.type() != RegType::vgpr)
1559 emit_vop2_instruction(ctx, instr, aco_opcode::v_sub_f32, dst, false);
1560 else
1561 emit_vop2_instruction(ctx, instr, aco_opcode::v_subrev_f32, dst, true);
1562 } else if (dst.size() == 2) {
1563 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst),
1564 get_alu_src(ctx, instr->src[0]),
1565 as_vgpr(ctx, get_alu_src(ctx, instr->src[1])));
1566 VOP3A_instruction* sub = static_cast<VOP3A_instruction*>(add);
1567 sub->neg[1] = true;
1568 } else {
1569 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1570 nir_print_instr(&instr->instr, stderr);
1571 fprintf(stderr, "\n");
1572 }
1573 break;
1574 }
1575 case nir_op_fmax: {
1576 Temp src0 = get_alu_src(ctx, instr->src[0]);
1577 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1578 if (dst.regClass() == v2b) {
1579 // TODO: check fp_mode.must_flush_denorms16_64
1580 Temp tmp = bld.tmp(v1);
1581 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f16, tmp, true);
1582 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1583 } else if (dst.regClass() == v1) {
1584 emit_vop2_instruction(ctx, instr, aco_opcode::v_max_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1585 } else if (dst.regClass() == v2) {
1586 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1587 Temp tmp = bld.vop3(aco_opcode::v_max_f64, bld.def(v2), src0, src1);
1588 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1589 } else {
1590 bld.vop3(aco_opcode::v_max_f64, Definition(dst), src0, src1);
1591 }
1592 } else {
1593 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1594 nir_print_instr(&instr->instr, stderr);
1595 fprintf(stderr, "\n");
1596 }
1597 break;
1598 }
1599 case nir_op_fmin: {
1600 Temp src0 = get_alu_src(ctx, instr->src[0]);
1601 Temp src1 = as_vgpr(ctx, get_alu_src(ctx, instr->src[1]));
1602 if (dst.regClass() == v2b) {
1603 // TODO: check fp_mode.must_flush_denorms16_64
1604 Temp tmp = bld.tmp(v1);
1605 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f16, tmp, true);
1606 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1607 } else if (dst.regClass() == v1) {
1608 emit_vop2_instruction(ctx, instr, aco_opcode::v_min_f32, dst, true, false, ctx->block->fp_mode.must_flush_denorms32);
1609 } else if (dst.regClass() == v2) {
1610 if (ctx->block->fp_mode.must_flush_denorms16_64 && ctx->program->chip_class < GFX9) {
1611 Temp tmp = bld.vop3(aco_opcode::v_min_f64, bld.def(v2), src0, src1);
1612 bld.vop3(aco_opcode::v_mul_f64, Definition(dst), Operand(0x3FF0000000000000lu), tmp);
1613 } else {
1614 bld.vop3(aco_opcode::v_min_f64, Definition(dst), src0, src1);
1615 }
1616 } else {
1617 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1618 nir_print_instr(&instr->instr, stderr);
1619 fprintf(stderr, "\n");
1620 }
1621 break;
1622 }
1623 case nir_op_fmax3: {
1624 if (dst.size() == 1) {
1625 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1626 } else {
1627 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1628 nir_print_instr(&instr->instr, stderr);
1629 fprintf(stderr, "\n");
1630 }
1631 break;
1632 }
1633 case nir_op_fmin3: {
1634 if (dst.size() == 1) {
1635 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1636 } else {
1637 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1638 nir_print_instr(&instr->instr, stderr);
1639 fprintf(stderr, "\n");
1640 }
1641 break;
1642 }
1643 case nir_op_fmed3: {
1644 if (dst.size() == 1) {
1645 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_f32, dst, ctx->block->fp_mode.must_flush_denorms32);
1646 } else {
1647 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1648 nir_print_instr(&instr->instr, stderr);
1649 fprintf(stderr, "\n");
1650 }
1651 break;
1652 }
1653 case nir_op_umax3: {
1654 if (dst.size() == 1) {
1655 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_u32, dst);
1656 } else {
1657 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1658 nir_print_instr(&instr->instr, stderr);
1659 fprintf(stderr, "\n");
1660 }
1661 break;
1662 }
1663 case nir_op_umin3: {
1664 if (dst.size() == 1) {
1665 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_u32, dst);
1666 } else {
1667 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1668 nir_print_instr(&instr->instr, stderr);
1669 fprintf(stderr, "\n");
1670 }
1671 break;
1672 }
1673 case nir_op_umed3: {
1674 if (dst.size() == 1) {
1675 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_u32, dst);
1676 } else {
1677 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1678 nir_print_instr(&instr->instr, stderr);
1679 fprintf(stderr, "\n");
1680 }
1681 break;
1682 }
1683 case nir_op_imax3: {
1684 if (dst.size() == 1) {
1685 emit_vop3a_instruction(ctx, instr, aco_opcode::v_max3_i32, dst);
1686 } else {
1687 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1688 nir_print_instr(&instr->instr, stderr);
1689 fprintf(stderr, "\n");
1690 }
1691 break;
1692 }
1693 case nir_op_imin3: {
1694 if (dst.size() == 1) {
1695 emit_vop3a_instruction(ctx, instr, aco_opcode::v_min3_i32, dst);
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_imed3: {
1704 if (dst.size() == 1) {
1705 emit_vop3a_instruction(ctx, instr, aco_opcode::v_med3_i32, dst);
1706 } else {
1707 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1708 nir_print_instr(&instr->instr, stderr);
1709 fprintf(stderr, "\n");
1710 }
1711 break;
1712 }
1713 case nir_op_cube_face_coord: {
1714 Temp in = get_alu_src(ctx, instr->src[0], 3);
1715 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1716 emit_extract_vector(ctx, in, 1, v1),
1717 emit_extract_vector(ctx, in, 2, v1) };
1718 Temp ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), src[0], src[1], src[2]);
1719 ma = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), ma);
1720 Temp sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), src[0], src[1], src[2]);
1721 Temp tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), src[0], src[1], src[2]);
1722 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, ma, Operand(0x3f000000u/*0.5*/));
1723 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, ma, Operand(0x3f000000u/*0.5*/));
1724 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), sc, tc);
1725 break;
1726 }
1727 case nir_op_cube_face_index: {
1728 Temp in = get_alu_src(ctx, instr->src[0], 3);
1729 Temp src[3] = { emit_extract_vector(ctx, in, 0, v1),
1730 emit_extract_vector(ctx, in, 1, v1),
1731 emit_extract_vector(ctx, in, 2, v1) };
1732 bld.vop3(aco_opcode::v_cubeid_f32, Definition(dst), src[0], src[1], src[2]);
1733 break;
1734 }
1735 case nir_op_bcsel: {
1736 emit_bcsel(ctx, instr, dst);
1737 break;
1738 }
1739 case nir_op_frsq: {
1740 Temp src = get_alu_src(ctx, instr->src[0]);
1741 if (dst.regClass() == v2b) {
1742 Temp tmp = bld.vop1(aco_opcode::v_rsq_f16, bld.def(v1), src);
1743 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1744 } else if (dst.regClass() == v1) {
1745 emit_rsq(ctx, bld, Definition(dst), src);
1746 } else if (dst.regClass() == v2) {
1747 emit_vop1_instruction(ctx, instr, aco_opcode::v_rsq_f64, dst);
1748 } else {
1749 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1750 nir_print_instr(&instr->instr, stderr);
1751 fprintf(stderr, "\n");
1752 }
1753 break;
1754 }
1755 case nir_op_fneg: {
1756 Temp src = get_alu_src(ctx, instr->src[0]);
1757 if (dst.size() == 1) {
1758 if (ctx->block->fp_mode.must_flush_denorms32)
1759 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1760 bld.vop2(aco_opcode::v_xor_b32, Definition(dst), Operand(0x80000000u), as_vgpr(ctx, src));
1761 } else if (dst.size() == 2) {
1762 if (ctx->block->fp_mode.must_flush_denorms16_64)
1763 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1764 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1765 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1766 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), Operand(0x80000000u), upper);
1767 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1768 } else {
1769 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1770 nir_print_instr(&instr->instr, stderr);
1771 fprintf(stderr, "\n");
1772 }
1773 break;
1774 }
1775 case nir_op_fabs: {
1776 Temp src = get_alu_src(ctx, instr->src[0]);
1777 if (dst.size() == 1) {
1778 if (ctx->block->fp_mode.must_flush_denorms32)
1779 src = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0x3f800000u), as_vgpr(ctx, src));
1780 bld.vop2(aco_opcode::v_and_b32, Definition(dst), Operand(0x7FFFFFFFu), as_vgpr(ctx, src));
1781 } else if (dst.size() == 2) {
1782 if (ctx->block->fp_mode.must_flush_denorms16_64)
1783 src = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), Operand(0x3FF0000000000000lu), as_vgpr(ctx, src));
1784 Temp upper = bld.tmp(v1), lower = bld.tmp(v1);
1785 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
1786 upper = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7FFFFFFFu), upper);
1787 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
1788 } else {
1789 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1790 nir_print_instr(&instr->instr, stderr);
1791 fprintf(stderr, "\n");
1792 }
1793 break;
1794 }
1795 case nir_op_fsat: {
1796 Temp src = get_alu_src(ctx, instr->src[0]);
1797 if (dst.size() == 1) {
1798 bld.vop3(aco_opcode::v_med3_f32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
1799 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1800 // TODO: confirm that this holds under any circumstances
1801 } else if (dst.size() == 2) {
1802 Instruction* add = bld.vop3(aco_opcode::v_add_f64, Definition(dst), src, Operand(0u));
1803 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(add);
1804 vop3->clamp = true;
1805 } else {
1806 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1807 nir_print_instr(&instr->instr, stderr);
1808 fprintf(stderr, "\n");
1809 }
1810 break;
1811 }
1812 case nir_op_flog2: {
1813 Temp src = get_alu_src(ctx, instr->src[0]);
1814 if (dst.regClass() == v2b) {
1815 Temp tmp = bld.vop1(aco_opcode::v_log_f16, bld.def(v1), src);
1816 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1817 } else if (dst.regClass() == v1) {
1818 emit_log2(ctx, bld, Definition(dst), src);
1819 } else {
1820 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1821 nir_print_instr(&instr->instr, stderr);
1822 fprintf(stderr, "\n");
1823 }
1824 break;
1825 }
1826 case nir_op_frcp: {
1827 Temp src = get_alu_src(ctx, instr->src[0]);
1828 if (dst.regClass() == v2b) {
1829 Temp tmp = bld.vop1(aco_opcode::v_rcp_f16, bld.def(v1), src);
1830 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1831 } else if (dst.regClass() == v1) {
1832 emit_rcp(ctx, bld, Definition(dst), src);
1833 } else if (dst.regClass() == v2) {
1834 emit_vop1_instruction(ctx, instr, aco_opcode::v_rcp_f64, dst);
1835 } else {
1836 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1837 nir_print_instr(&instr->instr, stderr);
1838 fprintf(stderr, "\n");
1839 }
1840 break;
1841 }
1842 case nir_op_fexp2: {
1843 if (dst.regClass() == v2b) {
1844 Temp src = get_alu_src(ctx, instr->src[0]);
1845 Temp tmp = bld.vop1(aco_opcode::v_exp_f16, bld.def(v1), src);
1846 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1847 } else if (dst.regClass() == v1) {
1848 emit_vop1_instruction(ctx, instr, aco_opcode::v_exp_f32, dst);
1849 } else {
1850 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1851 nir_print_instr(&instr->instr, stderr);
1852 fprintf(stderr, "\n");
1853 }
1854 break;
1855 }
1856 case nir_op_fsqrt: {
1857 Temp src = get_alu_src(ctx, instr->src[0]);
1858 if (dst.regClass() == v2b) {
1859 Temp tmp = bld.vop1(aco_opcode::v_sqrt_f16, bld.def(v1), src);
1860 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1861 } else if (dst.regClass() == v1) {
1862 emit_sqrt(ctx, bld, Definition(dst), src);
1863 } else if (dst.regClass() == v2) {
1864 emit_vop1_instruction(ctx, instr, aco_opcode::v_sqrt_f64, dst);
1865 } else {
1866 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1867 nir_print_instr(&instr->instr, stderr);
1868 fprintf(stderr, "\n");
1869 }
1870 break;
1871 }
1872 case nir_op_ffract: {
1873 if (dst.regClass() == v2b) {
1874 Temp src = get_alu_src(ctx, instr->src[0]);
1875 Temp tmp = bld.vop1(aco_opcode::v_fract_f16, bld.def(v1), src);
1876 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1877 } else if (dst.regClass() == v1) {
1878 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f32, dst);
1879 } else if (dst.regClass() == v2) {
1880 emit_vop1_instruction(ctx, instr, aco_opcode::v_fract_f64, dst);
1881 } else {
1882 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1883 nir_print_instr(&instr->instr, stderr);
1884 fprintf(stderr, "\n");
1885 }
1886 break;
1887 }
1888 case nir_op_ffloor: {
1889 Temp src = get_alu_src(ctx, instr->src[0]);
1890 if (dst.regClass() == v2b) {
1891 Temp tmp = bld.vop1(aco_opcode::v_floor_f16, bld.def(v1), src);
1892 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1893 } else if (dst.regClass() == v1) {
1894 emit_vop1_instruction(ctx, instr, aco_opcode::v_floor_f32, dst);
1895 } else if (dst.regClass() == v2) {
1896 emit_floor_f64(ctx, bld, Definition(dst), src);
1897 } else {
1898 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1899 nir_print_instr(&instr->instr, stderr);
1900 fprintf(stderr, "\n");
1901 }
1902 break;
1903 }
1904 case nir_op_fceil: {
1905 Temp src0 = get_alu_src(ctx, instr->src[0]);
1906 if (dst.regClass() == v2b) {
1907 Temp tmp = bld.vop1(aco_opcode::v_ceil_f16, bld.def(v1), src0);
1908 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1909 } else if (dst.regClass() == v1) {
1910 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f32, dst);
1911 } else if (dst.regClass() == v2) {
1912 if (ctx->options->chip_class >= GFX7) {
1913 emit_vop1_instruction(ctx, instr, aco_opcode::v_ceil_f64, dst);
1914 } else {
1915 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1916 /* trunc = trunc(src0)
1917 * if (src0 > 0.0 && src0 != trunc)
1918 * trunc += 1.0
1919 */
1920 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src0);
1921 Temp tmp0 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.def(bld.lm), src0, Operand(0u));
1922 Temp tmp1 = bld.vopc(aco_opcode::v_cmp_lg_f64, bld.hint_vcc(bld.def(bld.lm)), src0, trunc);
1923 Temp cond = bld.sop2(aco_opcode::s_and_b64, bld.hint_vcc(bld.def(s2)), bld.def(s1, scc), tmp0, tmp1);
1924 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);
1925 add = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), bld.copy(bld.def(v1), Operand(0u)), add);
1926 bld.vop3(aco_opcode::v_add_f64, Definition(dst), trunc, add);
1927 }
1928 } else {
1929 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1930 nir_print_instr(&instr->instr, stderr);
1931 fprintf(stderr, "\n");
1932 }
1933 break;
1934 }
1935 case nir_op_ftrunc: {
1936 Temp src = get_alu_src(ctx, instr->src[0]);
1937 if (dst.regClass() == v2b) {
1938 Temp tmp = bld.vop1(aco_opcode::v_trunc_f16, bld.def(v1), src);
1939 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1940 } else if (dst.regClass() == v1) {
1941 emit_vop1_instruction(ctx, instr, aco_opcode::v_trunc_f32, dst);
1942 } else if (dst.regClass() == v2) {
1943 emit_trunc_f64(ctx, bld, Definition(dst), src);
1944 } else {
1945 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1946 nir_print_instr(&instr->instr, stderr);
1947 fprintf(stderr, "\n");
1948 }
1949 break;
1950 }
1951 case nir_op_fround_even: {
1952 Temp src0 = get_alu_src(ctx, instr->src[0]);
1953 if (dst.regClass() == v2b) {
1954 Temp tmp = bld.vop1(aco_opcode::v_rndne_f16, bld.def(v1), src0);
1955 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
1956 } else if (dst.regClass() == v1) {
1957 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f32, dst);
1958 } else if (dst.regClass() == v2) {
1959 if (ctx->options->chip_class >= GFX7) {
1960 emit_vop1_instruction(ctx, instr, aco_opcode::v_rndne_f64, dst);
1961 } else {
1962 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
1963 Temp src0_lo = bld.tmp(v1), src0_hi = bld.tmp(v1);
1964 bld.pseudo(aco_opcode::p_split_vector, Definition(src0_lo), Definition(src0_hi), src0);
1965
1966 Temp bitmask = bld.sop1(aco_opcode::s_brev_b32, bld.def(s1), bld.copy(bld.def(s1), Operand(-2u)));
1967 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));
1968 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));
1969 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));
1970 static_cast<VOP3A_instruction*>(sub)->neg[1] = true;
1971 tmp = sub->definitions[0].getTemp();
1972
1973 Temp v = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(-1u), Operand(0x432fffffu));
1974 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_gt_f64, bld.hint_vcc(bld.def(bld.lm)), src0, v);
1975 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
1976 Temp cond = vop3->definitions[0].getTemp();
1977
1978 Temp tmp_lo = bld.tmp(v1), tmp_hi = bld.tmp(v1);
1979 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp_lo), Definition(tmp_hi), tmp);
1980 Temp dst0 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_lo, as_vgpr(ctx, src0_lo), cond);
1981 Temp dst1 = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp_hi, as_vgpr(ctx, src0_hi), cond);
1982
1983 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), dst0, dst1);
1984 }
1985 } else {
1986 fprintf(stderr, "Unimplemented NIR instr bit size: ");
1987 nir_print_instr(&instr->instr, stderr);
1988 fprintf(stderr, "\n");
1989 }
1990 break;
1991 }
1992 case nir_op_fsin:
1993 case nir_op_fcos: {
1994 Temp src = get_alu_src(ctx, instr->src[0]);
1995 aco_ptr<Instruction> norm;
1996 if (dst.size() == 1) {
1997 Temp half_pi = bld.copy(bld.def(s1), Operand(0x3e22f983u));
1998 Temp tmp = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), half_pi, as_vgpr(ctx, src));
1999
2000 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2001 if (ctx->options->chip_class < GFX9)
2002 tmp = bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), tmp);
2003
2004 aco_opcode opcode = instr->op == nir_op_fsin ? aco_opcode::v_sin_f32 : aco_opcode::v_cos_f32;
2005 bld.vop1(opcode, Definition(dst), tmp);
2006 } else {
2007 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2008 nir_print_instr(&instr->instr, stderr);
2009 fprintf(stderr, "\n");
2010 }
2011 break;
2012 }
2013 case nir_op_ldexp: {
2014 if (dst.size() == 1) {
2015 bld.vop3(aco_opcode::v_ldexp_f32, Definition(dst),
2016 as_vgpr(ctx, get_alu_src(ctx, instr->src[0])),
2017 get_alu_src(ctx, instr->src[1]));
2018 } else if (dst.size() == 2) {
2019 bld.vop3(aco_opcode::v_ldexp_f64, Definition(dst),
2020 as_vgpr(ctx, get_alu_src(ctx, instr->src[0])),
2021 get_alu_src(ctx, instr->src[1]));
2022 } else {
2023 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2024 nir_print_instr(&instr->instr, stderr);
2025 fprintf(stderr, "\n");
2026 }
2027 break;
2028 }
2029 case nir_op_frexp_sig: {
2030 Temp src = get_alu_src(ctx, instr->src[0]);
2031 if (dst.regClass() == v2b) {
2032 Temp tmp = bld.vop1(aco_opcode::v_frexp_mant_f16, bld.def(v1), src);
2033 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), tmp);
2034 } else if (dst.regClass() == v1) {
2035 bld.vop1(aco_opcode::v_frexp_mant_f32, Definition(dst), src);
2036 } else if (dst.regClass() == v2) {
2037 bld.vop1(aco_opcode::v_frexp_mant_f64, Definition(dst), src);
2038 } else {
2039 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2040 nir_print_instr(&instr->instr, stderr);
2041 fprintf(stderr, "\n");
2042 }
2043 break;
2044 }
2045 case nir_op_frexp_exp: {
2046 Temp src = get_alu_src(ctx, instr->src[0]);
2047 if (instr->src[0].src.ssa->bit_size == 16) {
2048 Temp tmp = bld.vop1(aco_opcode::v_frexp_exp_i16_f16, bld.def(v1), src);
2049 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), tmp, Operand(0u));
2050 } else if (instr->src[0].src.ssa->bit_size == 32) {
2051 bld.vop1(aco_opcode::v_frexp_exp_i32_f32, Definition(dst), src);
2052 } else if (instr->src[0].src.ssa->bit_size == 64) {
2053 bld.vop1(aco_opcode::v_frexp_exp_i32_f64, Definition(dst), src);
2054 } else {
2055 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2056 nir_print_instr(&instr->instr, stderr);
2057 fprintf(stderr, "\n");
2058 }
2059 break;
2060 }
2061 case nir_op_fsign: {
2062 Temp src = as_vgpr(ctx, get_alu_src(ctx, instr->src[0]));
2063 if (dst.size() == 1) {
2064 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2065 src = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0x3f800000u), src, cond);
2066 cond = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2067 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0xbf800000u), src, cond);
2068 } else if (dst.size() == 2) {
2069 Temp cond = bld.vopc(aco_opcode::v_cmp_nlt_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2070 Temp tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0x3FF00000u));
2071 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, emit_extract_vector(ctx, src, 1, v1), cond);
2072
2073 cond = bld.vopc(aco_opcode::v_cmp_le_f64, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), src);
2074 tmp = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0xBFF00000u));
2075 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), tmp, upper, cond);
2076
2077 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2078 } else {
2079 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2080 nir_print_instr(&instr->instr, stderr);
2081 fprintf(stderr, "\n");
2082 }
2083 break;
2084 }
2085 case nir_op_f2f16:
2086 case nir_op_f2f16_rtne: {
2087 Temp src = get_alu_src(ctx, instr->src[0]);
2088 if (instr->src[0].src.ssa->bit_size == 64)
2089 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2090 src = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2091 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2092 break;
2093 }
2094 case nir_op_f2f16_rtz: {
2095 Temp src = get_alu_src(ctx, instr->src[0]);
2096 if (instr->src[0].src.ssa->bit_size == 64)
2097 src = bld.vop1(aco_opcode::v_cvt_f32_f64, bld.def(v1), src);
2098 src = bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, bld.def(v1), src, Operand(0u));
2099 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2100 break;
2101 }
2102 case nir_op_f2f32: {
2103 if (instr->src[0].src.ssa->bit_size == 16) {
2104 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f16, dst);
2105 } else if (instr->src[0].src.ssa->bit_size == 64) {
2106 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_f64, dst);
2107 } else {
2108 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2109 nir_print_instr(&instr->instr, stderr);
2110 fprintf(stderr, "\n");
2111 }
2112 break;
2113 }
2114 case nir_op_f2f64: {
2115 Temp src = get_alu_src(ctx, instr->src[0]);
2116 if (instr->src[0].src.ssa->bit_size == 16)
2117 src = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src);
2118 bld.vop1(aco_opcode::v_cvt_f64_f32, Definition(dst), src);
2119 break;
2120 }
2121 case nir_op_i2f32: {
2122 assert(dst.size() == 1);
2123 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_i32, dst);
2124 break;
2125 }
2126 case nir_op_i2f64: {
2127 if (instr->src[0].src.ssa->bit_size == 32) {
2128 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_i32, dst);
2129 } else if (instr->src[0].src.ssa->bit_size == 64) {
2130 Temp src = get_alu_src(ctx, instr->src[0]);
2131 RegClass rc = RegClass(src.type(), 1);
2132 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2133 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2134 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2135 upper = bld.vop1(aco_opcode::v_cvt_f64_i32, bld.def(v2), upper);
2136 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2137 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2138
2139 } else {
2140 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2141 nir_print_instr(&instr->instr, stderr);
2142 fprintf(stderr, "\n");
2143 }
2144 break;
2145 }
2146 case nir_op_u2f32: {
2147 assert(dst.size() == 1);
2148 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f32_u32, dst);
2149 break;
2150 }
2151 case nir_op_u2f64: {
2152 if (instr->src[0].src.ssa->bit_size == 32) {
2153 emit_vop1_instruction(ctx, instr, aco_opcode::v_cvt_f64_u32, dst);
2154 } else if (instr->src[0].src.ssa->bit_size == 64) {
2155 Temp src = get_alu_src(ctx, instr->src[0]);
2156 RegClass rc = RegClass(src.type(), 1);
2157 Temp lower = bld.tmp(rc), upper = bld.tmp(rc);
2158 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), src);
2159 lower = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), lower);
2160 upper = bld.vop1(aco_opcode::v_cvt_f64_u32, bld.def(v2), upper);
2161 upper = bld.vop3(aco_opcode::v_ldexp_f64, bld.def(v2), upper, Operand(32u));
2162 bld.vop3(aco_opcode::v_add_f64, Definition(dst), lower, upper);
2163 } else {
2164 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2165 nir_print_instr(&instr->instr, stderr);
2166 fprintf(stderr, "\n");
2167 }
2168 break;
2169 }
2170 case nir_op_f2i16: {
2171 Temp src = get_alu_src(ctx, instr->src[0]);
2172 if (instr->src[0].src.ssa->bit_size == 16)
2173 src = bld.vop1(aco_opcode::v_cvt_i16_f16, bld.def(v1), src);
2174 else if (instr->src[0].src.ssa->bit_size == 32)
2175 src = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src);
2176 else
2177 src = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src);
2178
2179 if (dst.type() == RegType::vgpr)
2180 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2181 else
2182 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2183 break;
2184 }
2185 case nir_op_f2u16: {
2186 Temp src = get_alu_src(ctx, instr->src[0]);
2187 if (instr->src[0].src.ssa->bit_size == 16)
2188 src = bld.vop1(aco_opcode::v_cvt_u16_f16, bld.def(v1), src);
2189 else if (instr->src[0].src.ssa->bit_size == 32)
2190 src = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src);
2191 else
2192 src = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src);
2193
2194 if (dst.type() == RegType::vgpr)
2195 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(v2b), src);
2196 else
2197 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), src);
2198 break;
2199 }
2200 case nir_op_f2i32: {
2201 Temp src = get_alu_src(ctx, instr->src[0]);
2202 if (instr->src[0].src.ssa->bit_size == 32) {
2203 if (dst.type() == RegType::vgpr)
2204 bld.vop1(aco_opcode::v_cvt_i32_f32, Definition(dst), src);
2205 else
2206 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2207 bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), src));
2208
2209 } else if (instr->src[0].src.ssa->bit_size == 64) {
2210 if (dst.type() == RegType::vgpr)
2211 bld.vop1(aco_opcode::v_cvt_i32_f64, Definition(dst), src);
2212 else
2213 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2214 bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), src));
2215
2216 } else {
2217 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2218 nir_print_instr(&instr->instr, stderr);
2219 fprintf(stderr, "\n");
2220 }
2221 break;
2222 }
2223 case nir_op_f2u32: {
2224 Temp src = get_alu_src(ctx, instr->src[0]);
2225 if (instr->src[0].src.ssa->bit_size == 32) {
2226 if (dst.type() == RegType::vgpr)
2227 bld.vop1(aco_opcode::v_cvt_u32_f32, Definition(dst), src);
2228 else
2229 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2230 bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), src));
2231
2232 } else if (instr->src[0].src.ssa->bit_size == 64) {
2233 if (dst.type() == RegType::vgpr)
2234 bld.vop1(aco_opcode::v_cvt_u32_f64, Definition(dst), src);
2235 else
2236 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst),
2237 bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), src));
2238
2239 } else {
2240 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2241 nir_print_instr(&instr->instr, stderr);
2242 fprintf(stderr, "\n");
2243 }
2244 break;
2245 }
2246 case nir_op_f2i64: {
2247 Temp src = get_alu_src(ctx, instr->src[0]);
2248 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
2249 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2250 exponent = bld.vop3(aco_opcode::v_med3_i32, bld.def(v1), Operand(0x0u), exponent, Operand(64u));
2251 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2252 Temp sign = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2253 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2254 mantissa = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(7u), mantissa);
2255 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2256 Temp new_exponent = bld.tmp(v1);
2257 Temp borrow = bld.vsub32(Definition(new_exponent), Operand(63u), exponent, true).def(1).getTemp();
2258 if (ctx->program->chip_class >= GFX8)
2259 mantissa = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), new_exponent, mantissa);
2260 else
2261 mantissa = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), mantissa, new_exponent);
2262 Temp saturate = bld.vop1(aco_opcode::v_bfrev_b32, bld.def(v1), Operand(0xfffffffeu));
2263 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2264 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2265 lower = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), lower, Operand(0xffffffffu), borrow);
2266 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), upper, saturate, borrow);
2267 lower = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, lower);
2268 upper = bld.vop2(aco_opcode::v_xor_b32, bld.def(v1), sign, upper);
2269 Temp new_lower = bld.tmp(v1);
2270 borrow = bld.vsub32(Definition(new_lower), lower, sign, true).def(1).getTemp();
2271 Temp new_upper = bld.vsub32(bld.def(v1), upper, sign, false, borrow);
2272 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), new_lower, new_upper);
2273
2274 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
2275 if (src.type() == RegType::vgpr)
2276 src = bld.as_uniform(src);
2277 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2278 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2279 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2280 exponent = bld.sop2(aco_opcode::s_min_u32, bld.def(s1), bld.def(s1, scc), Operand(64u), exponent);
2281 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2282 Temp sign = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2283 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2284 mantissa = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), mantissa, Operand(7u));
2285 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2286 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(63u), exponent);
2287 mantissa = bld.sop2(aco_opcode::s_lshr_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent);
2288 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), exponent, Operand(0xffffffffu)); // exp >= 64
2289 Temp saturate = bld.sop1(aco_opcode::s_brev_b64, bld.def(s2), Operand(0xfffffffeu));
2290 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), saturate, mantissa, cond);
2291 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2292 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2293 lower = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, lower);
2294 upper = bld.sop2(aco_opcode::s_xor_b32, bld.def(s1), bld.def(s1, scc), sign, upper);
2295 Temp borrow = bld.tmp(s1);
2296 lower = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.scc(Definition(borrow)), lower, sign);
2297 upper = bld.sop2(aco_opcode::s_subb_u32, bld.def(s1), bld.def(s1, scc), upper, sign, borrow);
2298 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2299
2300 } else if (instr->src[0].src.ssa->bit_size == 64) {
2301 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2302 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2303 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2304 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2305 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2306 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2307 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2308 Temp upper = bld.vop1(aco_opcode::v_cvt_i32_f64, bld.def(v1), floor);
2309 if (dst.type() == RegType::sgpr) {
2310 lower = bld.as_uniform(lower);
2311 upper = bld.as_uniform(upper);
2312 }
2313 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2314
2315 } else {
2316 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2317 nir_print_instr(&instr->instr, stderr);
2318 fprintf(stderr, "\n");
2319 }
2320 break;
2321 }
2322 case nir_op_f2u64: {
2323 Temp src = get_alu_src(ctx, instr->src[0]);
2324 if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::vgpr) {
2325 Temp exponent = bld.vop1(aco_opcode::v_frexp_exp_i32_f32, bld.def(v1), src);
2326 Temp exponent_in_range = bld.vopc(aco_opcode::v_cmp_ge_i32, bld.hint_vcc(bld.def(bld.lm)), Operand(64u), exponent);
2327 exponent = bld.vop2(aco_opcode::v_max_i32, bld.def(v1), Operand(0x0u), exponent);
2328 Temp mantissa = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffu), src);
2329 mantissa = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(0x800000u), mantissa);
2330 Temp exponent_small = bld.vsub32(bld.def(v1), Operand(24u), exponent);
2331 Temp small = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), exponent_small, mantissa);
2332 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), Operand(0u), mantissa);
2333 Temp new_exponent = bld.tmp(v1);
2334 Temp cond_small = bld.vsub32(Definition(new_exponent), exponent, Operand(24u), true).def(1).getTemp();
2335 if (ctx->program->chip_class >= GFX8)
2336 mantissa = bld.vop3(aco_opcode::v_lshlrev_b64, bld.def(v2), new_exponent, mantissa);
2337 else
2338 mantissa = bld.vop3(aco_opcode::v_lshl_b64, bld.def(v2), mantissa, new_exponent);
2339 Temp lower = bld.tmp(v1), upper = bld.tmp(v1);
2340 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2341 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), lower, small, cond_small);
2342 upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), upper, Operand(0u), cond_small);
2343 lower = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), lower, exponent_in_range);
2344 upper = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xffffffffu), upper, exponent_in_range);
2345 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2346
2347 } else if (instr->src[0].src.ssa->bit_size == 32 && dst.type() == RegType::sgpr) {
2348 if (src.type() == RegType::vgpr)
2349 src = bld.as_uniform(src);
2350 Temp exponent = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), src, Operand(0x80017u));
2351 exponent = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(126u));
2352 exponent = bld.sop2(aco_opcode::s_max_u32, bld.def(s1), bld.def(s1, scc), Operand(0u), exponent);
2353 Temp mantissa = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0x7fffffu), src);
2354 mantissa = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(0x800000u), mantissa);
2355 Temp exponent_small = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), Operand(24u), exponent);
2356 Temp small = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), mantissa, exponent_small);
2357 mantissa = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), mantissa);
2358 Temp exponent_large = bld.sop2(aco_opcode::s_sub_u32, bld.def(s1), bld.def(s1, scc), exponent, Operand(24u));
2359 mantissa = bld.sop2(aco_opcode::s_lshl_b64, bld.def(s2), bld.def(s1, scc), mantissa, exponent_large);
2360 Temp cond = bld.sopc(aco_opcode::s_cmp_ge_i32, bld.def(s1, scc), Operand(64u), exponent);
2361 mantissa = bld.sop2(aco_opcode::s_cselect_b64, bld.def(s2), mantissa, Operand(0xffffffffu), cond);
2362 Temp lower = bld.tmp(s1), upper = bld.tmp(s1);
2363 bld.pseudo(aco_opcode::p_split_vector, Definition(lower), Definition(upper), mantissa);
2364 Temp cond_small = bld.sopc(aco_opcode::s_cmp_le_i32, bld.def(s1, scc), exponent, Operand(24u));
2365 lower = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), small, lower, cond_small);
2366 upper = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), Operand(0u), upper, cond_small);
2367 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2368
2369 } else if (instr->src[0].src.ssa->bit_size == 64) {
2370 Temp vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0x3df00000u));
2371 Temp trunc = emit_trunc_f64(ctx, bld, bld.def(v2), src);
2372 Temp mul = bld.vop3(aco_opcode::v_mul_f64, bld.def(v2), trunc, vec);
2373 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(0u), Operand(0xc1f00000u));
2374 Temp floor = emit_floor_f64(ctx, bld, bld.def(v2), mul);
2375 Temp fma = bld.vop3(aco_opcode::v_fma_f64, bld.def(v2), floor, vec, trunc);
2376 Temp lower = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), fma);
2377 Temp upper = bld.vop1(aco_opcode::v_cvt_u32_f64, bld.def(v1), floor);
2378 if (dst.type() == RegType::sgpr) {
2379 lower = bld.as_uniform(lower);
2380 upper = bld.as_uniform(upper);
2381 }
2382 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lower, upper);
2383
2384 } else {
2385 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2386 nir_print_instr(&instr->instr, stderr);
2387 fprintf(stderr, "\n");
2388 }
2389 break;
2390 }
2391 case nir_op_b2f32: {
2392 Temp src = get_alu_src(ctx, instr->src[0]);
2393 assert(src.regClass() == bld.lm);
2394
2395 if (dst.regClass() == s1) {
2396 src = bool_to_scalar_condition(ctx, src);
2397 bld.sop2(aco_opcode::s_mul_i32, Definition(dst), Operand(0x3f800000u), src);
2398 } else if (dst.regClass() == v1) {
2399 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(0x3f800000u), src);
2400 } else {
2401 unreachable("Wrong destination register class for nir_op_b2f32.");
2402 }
2403 break;
2404 }
2405 case nir_op_b2f64: {
2406 Temp src = get_alu_src(ctx, instr->src[0]);
2407 assert(src.regClass() == bld.lm);
2408
2409 if (dst.regClass() == s2) {
2410 src = bool_to_scalar_condition(ctx, src);
2411 bld.sop2(aco_opcode::s_cselect_b64, Definition(dst), Operand(0x3f800000u), Operand(0u), bld.scc(src));
2412 } else if (dst.regClass() == v2) {
2413 Temp one = bld.vop1(aco_opcode::v_mov_b32, bld.def(v2), Operand(0x3FF00000u));
2414 Temp upper = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), one, src);
2415 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), Operand(0u), upper);
2416 } else {
2417 unreachable("Wrong destination register class for nir_op_b2f64.");
2418 }
2419 break;
2420 }
2421 case nir_op_i2i8:
2422 case nir_op_u2u8: {
2423 Temp src = get_alu_src(ctx, instr->src[0]);
2424 /* we can actually just say dst = src */
2425 if (src.regClass() == s1)
2426 bld.copy(Definition(dst), src);
2427 else
2428 emit_extract_vector(ctx, src, 0, dst);
2429 break;
2430 }
2431 case nir_op_i2i16: {
2432 Temp src = get_alu_src(ctx, instr->src[0]);
2433 if (instr->src[0].src.ssa->bit_size == 8) {
2434 if (dst.regClass() == s1) {
2435 bld.sop1(aco_opcode::s_sext_i32_i8, Definition(dst), Operand(src));
2436 } else {
2437 assert(src.regClass() == v1b);
2438 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2439 sdwa->operands[0] = Operand(src);
2440 sdwa->definitions[0] = Definition(dst);
2441 sdwa->sel[0] = sdwa_sbyte;
2442 sdwa->dst_sel = sdwa_sword;
2443 ctx->block->instructions.emplace_back(std::move(sdwa));
2444 }
2445 } else {
2446 Temp src = get_alu_src(ctx, instr->src[0]);
2447 /* we can actually just say dst = src */
2448 if (src.regClass() == s1)
2449 bld.copy(Definition(dst), src);
2450 else
2451 emit_extract_vector(ctx, src, 0, dst);
2452 }
2453 break;
2454 }
2455 case nir_op_u2u16: {
2456 Temp src = get_alu_src(ctx, instr->src[0]);
2457 if (instr->src[0].src.ssa->bit_size == 8) {
2458 if (dst.regClass() == s1)
2459 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFu), src);
2460 else {
2461 assert(src.regClass() == v1b);
2462 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2463 sdwa->operands[0] = Operand(src);
2464 sdwa->definitions[0] = Definition(dst);
2465 sdwa->sel[0] = sdwa_ubyte;
2466 sdwa->dst_sel = sdwa_uword;
2467 ctx->block->instructions.emplace_back(std::move(sdwa));
2468 }
2469 } else {
2470 Temp src = get_alu_src(ctx, instr->src[0]);
2471 /* we can actually just say dst = src */
2472 if (src.regClass() == s1)
2473 bld.copy(Definition(dst), src);
2474 else
2475 emit_extract_vector(ctx, src, 0, dst);
2476 }
2477 break;
2478 }
2479 case nir_op_i2i32: {
2480 Temp src = get_alu_src(ctx, instr->src[0]);
2481 if (instr->src[0].src.ssa->bit_size == 8) {
2482 if (dst.regClass() == s1) {
2483 bld.sop1(aco_opcode::s_sext_i32_i8, Definition(dst), Operand(src));
2484 } else {
2485 assert(src.regClass() == v1b);
2486 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2487 sdwa->operands[0] = Operand(src);
2488 sdwa->definitions[0] = Definition(dst);
2489 sdwa->sel[0] = sdwa_sbyte;
2490 sdwa->dst_sel = sdwa_sdword;
2491 ctx->block->instructions.emplace_back(std::move(sdwa));
2492 }
2493 } else if (instr->src[0].src.ssa->bit_size == 16) {
2494 if (dst.regClass() == s1) {
2495 bld.sop1(aco_opcode::s_sext_i32_i16, Definition(dst), Operand(src));
2496 } else {
2497 assert(src.regClass() == v2b);
2498 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2499 sdwa->operands[0] = Operand(src);
2500 sdwa->definitions[0] = Definition(dst);
2501 sdwa->sel[0] = sdwa_sword;
2502 sdwa->dst_sel = sdwa_udword;
2503 ctx->block->instructions.emplace_back(std::move(sdwa));
2504 }
2505 } else if (instr->src[0].src.ssa->bit_size == 64) {
2506 /* we can actually just say dst = src, as it would map the lower register */
2507 emit_extract_vector(ctx, src, 0, dst);
2508 } else {
2509 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2510 nir_print_instr(&instr->instr, stderr);
2511 fprintf(stderr, "\n");
2512 }
2513 break;
2514 }
2515 case nir_op_u2u32: {
2516 Temp src = get_alu_src(ctx, instr->src[0]);
2517 if (instr->src[0].src.ssa->bit_size == 8) {
2518 if (dst.regClass() == s1)
2519 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFu), src);
2520 else {
2521 assert(src.regClass() == v1b);
2522 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2523 sdwa->operands[0] = Operand(src);
2524 sdwa->definitions[0] = Definition(dst);
2525 sdwa->sel[0] = sdwa_ubyte;
2526 sdwa->dst_sel = sdwa_udword;
2527 ctx->block->instructions.emplace_back(std::move(sdwa));
2528 }
2529 } else if (instr->src[0].src.ssa->bit_size == 16) {
2530 if (dst.regClass() == s1) {
2531 bld.sop2(aco_opcode::s_and_b32, Definition(dst), bld.def(s1, scc), Operand(0xFFFFu), src);
2532 } else {
2533 assert(src.regClass() == v2b);
2534 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
2535 sdwa->operands[0] = Operand(src);
2536 sdwa->definitions[0] = Definition(dst);
2537 sdwa->sel[0] = sdwa_uword;
2538 sdwa->dst_sel = sdwa_udword;
2539 ctx->block->instructions.emplace_back(std::move(sdwa));
2540 }
2541 } else if (instr->src[0].src.ssa->bit_size == 64) {
2542 /* we can actually just say dst = src, as it would map the lower register */
2543 emit_extract_vector(ctx, src, 0, dst);
2544 } else {
2545 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2546 nir_print_instr(&instr->instr, stderr);
2547 fprintf(stderr, "\n");
2548 }
2549 break;
2550 }
2551 case nir_op_i2i64: {
2552 Temp src = get_alu_src(ctx, instr->src[0]);
2553 if (src.regClass() == s1) {
2554 Temp high = bld.sop2(aco_opcode::s_ashr_i32, bld.def(s1), bld.def(s1, scc), src, Operand(31u));
2555 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2556 } else if (src.regClass() == v1) {
2557 Temp high = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(31u), src);
2558 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, high);
2559 } else {
2560 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2561 nir_print_instr(&instr->instr, stderr);
2562 fprintf(stderr, "\n");
2563 }
2564 break;
2565 }
2566 case nir_op_u2u64: {
2567 Temp src = get_alu_src(ctx, instr->src[0]);
2568 if (instr->src[0].src.ssa->bit_size == 32) {
2569 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src, Operand(0u));
2570 } else {
2571 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2572 nir_print_instr(&instr->instr, stderr);
2573 fprintf(stderr, "\n");
2574 }
2575 break;
2576 }
2577 case nir_op_b2b32:
2578 case nir_op_b2i32: {
2579 Temp src = get_alu_src(ctx, instr->src[0]);
2580 assert(src.regClass() == bld.lm);
2581
2582 if (dst.regClass() == s1) {
2583 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2584 bool_to_scalar_condition(ctx, src, dst);
2585 } else if (dst.regClass() == v1) {
2586 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand(1u), src);
2587 } else {
2588 unreachable("Invalid register class for b2i32");
2589 }
2590 break;
2591 }
2592 case nir_op_b2b1:
2593 case nir_op_i2b1: {
2594 Temp src = get_alu_src(ctx, instr->src[0]);
2595 assert(dst.regClass() == bld.lm);
2596
2597 if (src.type() == RegType::vgpr) {
2598 assert(src.regClass() == v1 || src.regClass() == v2);
2599 assert(dst.regClass() == bld.lm);
2600 bld.vopc(src.size() == 2 ? aco_opcode::v_cmp_lg_u64 : aco_opcode::v_cmp_lg_u32,
2601 Definition(dst), Operand(0u), src).def(0).setHint(vcc);
2602 } else {
2603 assert(src.regClass() == s1 || src.regClass() == s2);
2604 Temp tmp;
2605 if (src.regClass() == s2 && ctx->program->chip_class <= GFX7) {
2606 tmp = bld.sop2(aco_opcode::s_or_b64, bld.def(s2), bld.def(s1, scc), Operand(0u), src).def(1).getTemp();
2607 } else {
2608 tmp = bld.sopc(src.size() == 2 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::s_cmp_lg_u32,
2609 bld.scc(bld.def(s1)), Operand(0u), src);
2610 }
2611 bool_to_vector_condition(ctx, tmp, dst);
2612 }
2613 break;
2614 }
2615 case nir_op_pack_64_2x32_split: {
2616 Temp src0 = get_alu_src(ctx, instr->src[0]);
2617 Temp src1 = get_alu_src(ctx, instr->src[1]);
2618
2619 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2620 break;
2621 }
2622 case nir_op_unpack_64_2x32_split_x:
2623 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2624 break;
2625 case nir_op_unpack_64_2x32_split_y:
2626 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2627 break;
2628 case nir_op_unpack_32_2x16_split_x:
2629 if (dst.type() == RegType::vgpr) {
2630 bld.pseudo(aco_opcode::p_split_vector, Definition(dst), bld.def(dst.regClass()), get_alu_src(ctx, instr->src[0]));
2631 } else {
2632 bld.copy(Definition(dst), get_alu_src(ctx, instr->src[0]));
2633 }
2634 break;
2635 case nir_op_unpack_32_2x16_split_y:
2636 if (dst.type() == RegType::vgpr) {
2637 bld.pseudo(aco_opcode::p_split_vector, bld.def(dst.regClass()), Definition(dst), get_alu_src(ctx, instr->src[0]));
2638 } else {
2639 bld.sop2(aco_opcode::s_bfe_u32, Definition(dst), get_alu_src(ctx, instr->src[0]), Operand(uint32_t(16 << 16 | 16)));
2640 }
2641 break;
2642 case nir_op_pack_32_2x16_split: {
2643 Temp src0 = get_alu_src(ctx, instr->src[0]);
2644 Temp src1 = get_alu_src(ctx, instr->src[1]);
2645 if (dst.regClass() == v1) {
2646 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src0, src1);
2647 } else {
2648 src0 = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), src0, Operand(0xFFFFu));
2649 src1 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), src1, Operand(16u));
2650 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), src0, src1);
2651 }
2652 break;
2653 }
2654 case nir_op_pack_half_2x16: {
2655 Temp src = get_alu_src(ctx, instr->src[0], 2);
2656
2657 if (dst.regClass() == v1) {
2658 Temp src0 = bld.tmp(v1);
2659 Temp src1 = bld.tmp(v1);
2660 bld.pseudo(aco_opcode::p_split_vector, Definition(src0), Definition(src1), src);
2661 if (!ctx->block->fp_mode.care_about_round32 || ctx->block->fp_mode.round32 == fp_round_tz)
2662 bld.vop3(aco_opcode::v_cvt_pkrtz_f16_f32, Definition(dst), src0, src1);
2663 else
2664 bld.vop3(aco_opcode::v_cvt_pk_u16_u32, Definition(dst),
2665 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src0),
2666 bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), src1));
2667 } else {
2668 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2669 nir_print_instr(&instr->instr, stderr);
2670 fprintf(stderr, "\n");
2671 }
2672 break;
2673 }
2674 case nir_op_unpack_half_2x16_split_x: {
2675 if (dst.regClass() == v1) {
2676 Builder bld(ctx->program, ctx->block);
2677 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst), get_alu_src(ctx, instr->src[0]));
2678 } else {
2679 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2680 nir_print_instr(&instr->instr, stderr);
2681 fprintf(stderr, "\n");
2682 }
2683 break;
2684 }
2685 case nir_op_unpack_half_2x16_split_y: {
2686 if (dst.regClass() == v1) {
2687 Builder bld(ctx->program, ctx->block);
2688 /* TODO: use SDWA here */
2689 bld.vop1(aco_opcode::v_cvt_f32_f16, Definition(dst),
2690 bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), as_vgpr(ctx, get_alu_src(ctx, instr->src[0]))));
2691 } else {
2692 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2693 nir_print_instr(&instr->instr, stderr);
2694 fprintf(stderr, "\n");
2695 }
2696 break;
2697 }
2698 case nir_op_fquantize2f16: {
2699 Temp src = get_alu_src(ctx, instr->src[0]);
2700 Temp f16 = bld.vop1(aco_opcode::v_cvt_f16_f32, bld.def(v1), src);
2701 Temp f32, cmp_res;
2702
2703 if (ctx->program->chip_class >= GFX8) {
2704 Temp mask = bld.copy(bld.def(s1), Operand(0x36Fu)); /* value is NOT negative/positive denormal value */
2705 cmp_res = bld.vopc_e64(aco_opcode::v_cmp_class_f16, bld.hint_vcc(bld.def(bld.lm)), f16, mask);
2706 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2707 } else {
2708 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2709 * so compare the result and flush to 0 if it's smaller.
2710 */
2711 f32 = bld.vop1(aco_opcode::v_cvt_f32_f16, bld.def(v1), f16);
2712 Temp smallest = bld.copy(bld.def(s1), Operand(0x38800000u));
2713 Instruction* vop3 = bld.vopc_e64(aco_opcode::v_cmp_nlt_f32, bld.hint_vcc(bld.def(bld.lm)), f32, smallest);
2714 static_cast<VOP3A_instruction*>(vop3)->abs[0] = true;
2715 cmp_res = vop3->definitions[0].getTemp();
2716 }
2717
2718 if (ctx->block->fp_mode.preserve_signed_zero_inf_nan32 || ctx->program->chip_class < GFX8) {
2719 Temp copysign_0 = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0u), as_vgpr(ctx, src));
2720 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), copysign_0, f32, cmp_res);
2721 } else {
2722 bld.vop2(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), f32, cmp_res);
2723 }
2724 break;
2725 }
2726 case nir_op_bfm: {
2727 Temp bits = get_alu_src(ctx, instr->src[0]);
2728 Temp offset = get_alu_src(ctx, instr->src[1]);
2729
2730 if (dst.regClass() == s1) {
2731 bld.sop2(aco_opcode::s_bfm_b32, Definition(dst), bits, offset);
2732 } else if (dst.regClass() == v1) {
2733 bld.vop3(aco_opcode::v_bfm_b32, Definition(dst), bits, offset);
2734 } else {
2735 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2736 nir_print_instr(&instr->instr, stderr);
2737 fprintf(stderr, "\n");
2738 }
2739 break;
2740 }
2741 case nir_op_bitfield_select: {
2742 /* (mask & insert) | (~mask & base) */
2743 Temp bitmask = get_alu_src(ctx, instr->src[0]);
2744 Temp insert = get_alu_src(ctx, instr->src[1]);
2745 Temp base = get_alu_src(ctx, instr->src[2]);
2746
2747 /* dst = (insert & bitmask) | (base & ~bitmask) */
2748 if (dst.regClass() == s1) {
2749 aco_ptr<Instruction> sop2;
2750 nir_const_value* const_bitmask = nir_src_as_const_value(instr->src[0].src);
2751 nir_const_value* const_insert = nir_src_as_const_value(instr->src[1].src);
2752 Operand lhs;
2753 if (const_insert && const_bitmask) {
2754 lhs = Operand(const_insert->u32 & const_bitmask->u32);
2755 } else {
2756 insert = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), insert, bitmask);
2757 lhs = Operand(insert);
2758 }
2759
2760 Operand rhs;
2761 nir_const_value* const_base = nir_src_as_const_value(instr->src[2].src);
2762 if (const_base && const_bitmask) {
2763 rhs = Operand(const_base->u32 & ~const_bitmask->u32);
2764 } else {
2765 base = bld.sop2(aco_opcode::s_andn2_b32, bld.def(s1), bld.def(s1, scc), base, bitmask);
2766 rhs = Operand(base);
2767 }
2768
2769 bld.sop2(aco_opcode::s_or_b32, Definition(dst), bld.def(s1, scc), rhs, lhs);
2770
2771 } else if (dst.regClass() == v1) {
2772 if (base.type() == RegType::sgpr && (bitmask.type() == RegType::sgpr || (insert.type() == RegType::sgpr)))
2773 base = as_vgpr(ctx, base);
2774 if (insert.type() == RegType::sgpr && bitmask.type() == RegType::sgpr)
2775 insert = as_vgpr(ctx, insert);
2776
2777 bld.vop3(aco_opcode::v_bfi_b32, Definition(dst), bitmask, insert, base);
2778
2779 } else {
2780 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2781 nir_print_instr(&instr->instr, stderr);
2782 fprintf(stderr, "\n");
2783 }
2784 break;
2785 }
2786 case nir_op_ubfe:
2787 case nir_op_ibfe: {
2788 Temp base = get_alu_src(ctx, instr->src[0]);
2789 Temp offset = get_alu_src(ctx, instr->src[1]);
2790 Temp bits = get_alu_src(ctx, instr->src[2]);
2791
2792 if (dst.type() == RegType::sgpr) {
2793 Operand extract;
2794 nir_const_value* const_offset = nir_src_as_const_value(instr->src[1].src);
2795 nir_const_value* const_bits = nir_src_as_const_value(instr->src[2].src);
2796 if (const_offset && const_bits) {
2797 uint32_t const_extract = (const_bits->u32 << 16) | const_offset->u32;
2798 extract = Operand(const_extract);
2799 } else {
2800 Operand width;
2801 if (const_bits) {
2802 width = Operand(const_bits->u32 << 16);
2803 } else {
2804 width = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), bits, Operand(16u));
2805 }
2806 extract = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), offset, width);
2807 }
2808
2809 aco_opcode opcode;
2810 if (dst.regClass() == s1) {
2811 if (instr->op == nir_op_ubfe)
2812 opcode = aco_opcode::s_bfe_u32;
2813 else
2814 opcode = aco_opcode::s_bfe_i32;
2815 } else if (dst.regClass() == s2) {
2816 if (instr->op == nir_op_ubfe)
2817 opcode = aco_opcode::s_bfe_u64;
2818 else
2819 opcode = aco_opcode::s_bfe_i64;
2820 } else {
2821 unreachable("Unsupported BFE bit size");
2822 }
2823
2824 bld.sop2(opcode, Definition(dst), bld.def(s1, scc), base, extract);
2825
2826 } else {
2827 aco_opcode opcode;
2828 if (dst.regClass() == v1) {
2829 if (instr->op == nir_op_ubfe)
2830 opcode = aco_opcode::v_bfe_u32;
2831 else
2832 opcode = aco_opcode::v_bfe_i32;
2833 } else {
2834 unreachable("Unsupported BFE bit size");
2835 }
2836
2837 emit_vop3a_instruction(ctx, instr, opcode, dst);
2838 }
2839 break;
2840 }
2841 case nir_op_bit_count: {
2842 Temp src = get_alu_src(ctx, instr->src[0]);
2843 if (src.regClass() == s1) {
2844 bld.sop1(aco_opcode::s_bcnt1_i32_b32, Definition(dst), bld.def(s1, scc), src);
2845 } else if (src.regClass() == v1) {
2846 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst), src, Operand(0u));
2847 } else if (src.regClass() == v2) {
2848 bld.vop3(aco_opcode::v_bcnt_u32_b32, Definition(dst),
2849 emit_extract_vector(ctx, src, 1, v1),
2850 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1),
2851 emit_extract_vector(ctx, src, 0, v1), Operand(0u)));
2852 } else if (src.regClass() == s2) {
2853 bld.sop1(aco_opcode::s_bcnt1_i32_b64, Definition(dst), bld.def(s1, scc), src);
2854 } else {
2855 fprintf(stderr, "Unimplemented NIR instr bit size: ");
2856 nir_print_instr(&instr->instr, stderr);
2857 fprintf(stderr, "\n");
2858 }
2859 break;
2860 }
2861 case nir_op_flt: {
2862 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_f32, aco_opcode::v_cmp_lt_f64);
2863 break;
2864 }
2865 case nir_op_fge: {
2866 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_f32, aco_opcode::v_cmp_ge_f64);
2867 break;
2868 }
2869 case nir_op_feq: {
2870 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_f32, aco_opcode::v_cmp_eq_f64);
2871 break;
2872 }
2873 case nir_op_fne: {
2874 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_neq_f32, aco_opcode::v_cmp_neq_f64);
2875 break;
2876 }
2877 case nir_op_ilt: {
2878 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_i32, aco_opcode::v_cmp_lt_i64, aco_opcode::s_cmp_lt_i32);
2879 break;
2880 }
2881 case nir_op_ige: {
2882 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_i32, aco_opcode::v_cmp_ge_i64, aco_opcode::s_cmp_ge_i32);
2883 break;
2884 }
2885 case nir_op_ieq: {
2886 if (instr->src[0].src.ssa->bit_size == 1)
2887 emit_boolean_logic(ctx, instr, Builder::s_xnor, dst);
2888 else
2889 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_eq_i32, aco_opcode::v_cmp_eq_i64, aco_opcode::s_cmp_eq_i32,
2890 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_eq_u64 : aco_opcode::num_opcodes);
2891 break;
2892 }
2893 case nir_op_ine: {
2894 if (instr->src[0].src.ssa->bit_size == 1)
2895 emit_boolean_logic(ctx, instr, Builder::s_xor, dst);
2896 else
2897 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lg_i32, aco_opcode::v_cmp_lg_i64, aco_opcode::s_cmp_lg_i32,
2898 ctx->program->chip_class >= GFX8 ? aco_opcode::s_cmp_lg_u64 : aco_opcode::num_opcodes);
2899 break;
2900 }
2901 case nir_op_ult: {
2902 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_lt_u32, aco_opcode::v_cmp_lt_u64, aco_opcode::s_cmp_lt_u32);
2903 break;
2904 }
2905 case nir_op_uge: {
2906 emit_comparison(ctx, instr, dst, aco_opcode::v_cmp_ge_u32, aco_opcode::v_cmp_ge_u64, aco_opcode::s_cmp_ge_u32);
2907 break;
2908 }
2909 case nir_op_fddx:
2910 case nir_op_fddy:
2911 case nir_op_fddx_fine:
2912 case nir_op_fddy_fine:
2913 case nir_op_fddx_coarse:
2914 case nir_op_fddy_coarse: {
2915 Temp src = get_alu_src(ctx, instr->src[0]);
2916 uint16_t dpp_ctrl1, dpp_ctrl2;
2917 if (instr->op == nir_op_fddx_fine) {
2918 dpp_ctrl1 = dpp_quad_perm(0, 0, 2, 2);
2919 dpp_ctrl2 = dpp_quad_perm(1, 1, 3, 3);
2920 } else if (instr->op == nir_op_fddy_fine) {
2921 dpp_ctrl1 = dpp_quad_perm(0, 1, 0, 1);
2922 dpp_ctrl2 = dpp_quad_perm(2, 3, 2, 3);
2923 } else {
2924 dpp_ctrl1 = dpp_quad_perm(0, 0, 0, 0);
2925 if (instr->op == nir_op_fddx || instr->op == nir_op_fddx_coarse)
2926 dpp_ctrl2 = dpp_quad_perm(1, 1, 1, 1);
2927 else
2928 dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
2929 }
2930
2931 Temp tmp;
2932 if (ctx->program->chip_class >= GFX8) {
2933 Temp tl = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl1);
2934 tmp = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), src, tl, dpp_ctrl2);
2935 } else {
2936 Temp tl = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl1);
2937 Temp tr = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl2);
2938 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), tr, tl);
2939 }
2940 emit_wqm(ctx, tmp, dst, true);
2941 break;
2942 }
2943 default:
2944 fprintf(stderr, "Unknown NIR ALU instr: ");
2945 nir_print_instr(&instr->instr, stderr);
2946 fprintf(stderr, "\n");
2947 }
2948 }
2949
2950 void visit_load_const(isel_context *ctx, nir_load_const_instr *instr)
2951 {
2952 Temp dst = get_ssa_temp(ctx, &instr->def);
2953
2954 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2955 // which get truncated the lsb if double and msb if int
2956 // for now, we only use s_mov_b64 with 64bit inline constants
2957 assert(instr->def.num_components == 1 && "Vector load_const should be lowered to scalar.");
2958 assert(dst.type() == RegType::sgpr);
2959
2960 Builder bld(ctx->program, ctx->block);
2961
2962 if (instr->def.bit_size == 1) {
2963 assert(dst.regClass() == bld.lm);
2964 int val = instr->value[0].b ? -1 : 0;
2965 Operand op = bld.lm.size() == 1 ? Operand((uint32_t) val) : Operand((uint64_t) val);
2966 bld.sop1(Builder::s_mov, Definition(dst), op);
2967 } else if (dst.size() == 1) {
2968 bld.copy(Definition(dst), Operand(instr->value[0].u32));
2969 } else {
2970 assert(dst.size() != 1);
2971 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
2972 if (instr->def.bit_size == 64)
2973 for (unsigned i = 0; i < dst.size(); i++)
2974 vec->operands[i] = Operand{(uint32_t)(instr->value[0].u64 >> i * 32)};
2975 else {
2976 for (unsigned i = 0; i < dst.size(); i++)
2977 vec->operands[i] = Operand{instr->value[i].u32};
2978 }
2979 vec->definitions[0] = Definition(dst);
2980 ctx->block->instructions.emplace_back(std::move(vec));
2981 }
2982 }
2983
2984 uint32_t widen_mask(uint32_t mask, unsigned multiplier)
2985 {
2986 uint32_t new_mask = 0;
2987 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
2988 if (mask & (1u << i))
2989 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
2990 return new_mask;
2991 }
2992
2993 Operand load_lds_size_m0(isel_context *ctx)
2994 {
2995 /* TODO: m0 does not need to be initialized on GFX9+ */
2996 Builder bld(ctx->program, ctx->block);
2997 return bld.m0((Temp)bld.sopk(aco_opcode::s_movk_i32, bld.def(s1, m0), 0xffff));
2998 }
2999
3000 Temp load_lds(isel_context *ctx, unsigned elem_size_bytes, Temp dst,
3001 Temp address, unsigned base_offset, unsigned align)
3002 {
3003 assert(util_is_power_of_two_nonzero(align) && align >= 4);
3004
3005 Builder bld(ctx->program, ctx->block);
3006
3007 Operand m = load_lds_size_m0(ctx);
3008
3009 unsigned num_components = dst.size() * 4u / elem_size_bytes;
3010 unsigned bytes_read = 0;
3011 unsigned result_size = 0;
3012 unsigned total_bytes = num_components * elem_size_bytes;
3013 std::array<Temp, NIR_MAX_VEC_COMPONENTS> result;
3014 bool large_ds_read = ctx->options->chip_class >= GFX7;
3015 bool usable_read2 = ctx->options->chip_class >= GFX7;
3016
3017 while (bytes_read < total_bytes) {
3018 unsigned todo = total_bytes - bytes_read;
3019 bool aligned8 = bytes_read % 8 == 0 && align % 8 == 0;
3020 bool aligned16 = bytes_read % 16 == 0 && align % 16 == 0;
3021
3022 aco_opcode op = aco_opcode::last_opcode;
3023 bool read2 = false;
3024 if (todo >= 16 && aligned16 && large_ds_read) {
3025 op = aco_opcode::ds_read_b128;
3026 todo = 16;
3027 } else if (todo >= 16 && aligned8 && usable_read2) {
3028 op = aco_opcode::ds_read2_b64;
3029 read2 = true;
3030 todo = 16;
3031 } else if (todo >= 12 && aligned16 && large_ds_read) {
3032 op = aco_opcode::ds_read_b96;
3033 todo = 12;
3034 } else if (todo >= 8 && aligned8) {
3035 op = aco_opcode::ds_read_b64;
3036 todo = 8;
3037 } else if (todo >= 8 && usable_read2) {
3038 op = aco_opcode::ds_read2_b32;
3039 read2 = true;
3040 todo = 8;
3041 } else if (todo >= 4) {
3042 op = aco_opcode::ds_read_b32;
3043 todo = 4;
3044 } else {
3045 assert(false);
3046 }
3047 assert(todo % elem_size_bytes == 0);
3048 unsigned num_elements = todo / elem_size_bytes;
3049 unsigned offset = base_offset + bytes_read;
3050 unsigned max_offset = read2 ? 1019 : 65535;
3051
3052 Temp address_offset = address;
3053 if (offset > max_offset) {
3054 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3055 offset = bytes_read;
3056 }
3057 assert(offset <= max_offset); /* bytes_read shouldn't be large enough for this to happen */
3058
3059 Temp res;
3060 if (num_components == 1 && dst.type() == RegType::vgpr)
3061 res = dst;
3062 else
3063 res = bld.tmp(RegClass(RegType::vgpr, todo / 4));
3064
3065 if (read2)
3066 res = bld.ds(op, Definition(res), address_offset, m, offset / (todo / 2), (offset / (todo / 2)) + 1);
3067 else
3068 res = bld.ds(op, Definition(res), address_offset, m, offset);
3069
3070 if (num_components == 1) {
3071 assert(todo == total_bytes);
3072 if (dst.type() == RegType::sgpr)
3073 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), res);
3074 return dst;
3075 }
3076
3077 if (dst.type() == RegType::sgpr) {
3078 Temp new_res = bld.tmp(RegType::sgpr, res.size());
3079 expand_vector(ctx, res, new_res, res.size(), (1 << res.size()) - 1);
3080 res = new_res;
3081 }
3082
3083 if (num_elements == 1) {
3084 result[result_size++] = res;
3085 } else {
3086 assert(res != dst && res.size() % num_elements == 0);
3087 aco_ptr<Pseudo_instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector, Format::PSEUDO, 1, num_elements)};
3088 split->operands[0] = Operand(res);
3089 for (unsigned i = 0; i < num_elements; i++)
3090 split->definitions[i] = Definition(result[result_size++] = bld.tmp(res.type(), elem_size_bytes / 4));
3091 ctx->block->instructions.emplace_back(std::move(split));
3092 }
3093
3094 bytes_read += todo;
3095 }
3096
3097 assert(result_size == num_components && result_size > 1);
3098 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, result_size, 1)};
3099 for (unsigned i = 0; i < result_size; i++)
3100 vec->operands[i] = Operand(result[i]);
3101 vec->definitions[0] = Definition(dst);
3102 ctx->block->instructions.emplace_back(std::move(vec));
3103 ctx->allocated_vec.emplace(dst.id(), result);
3104
3105 return dst;
3106 }
3107
3108 Temp extract_subvector(isel_context *ctx, Temp data, unsigned start, unsigned size, RegType type)
3109 {
3110 if (start == 0 && size == data.size())
3111 return type == RegType::vgpr ? as_vgpr(ctx, data) : data;
3112
3113 unsigned size_hint = 1;
3114 auto it = ctx->allocated_vec.find(data.id());
3115 if (it != ctx->allocated_vec.end())
3116 size_hint = it->second[0].size();
3117 if (size % size_hint || start % size_hint)
3118 size_hint = 1;
3119
3120 start /= size_hint;
3121 size /= size_hint;
3122
3123 Temp elems[size];
3124 for (unsigned i = 0; i < size; i++)
3125 elems[i] = emit_extract_vector(ctx, data, start + i, RegClass(type, size_hint));
3126
3127 if (size == 1)
3128 return type == RegType::vgpr ? as_vgpr(ctx, elems[0]) : elems[0];
3129
3130 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, size, 1)};
3131 for (unsigned i = 0; i < size; i++)
3132 vec->operands[i] = Operand(elems[i]);
3133 Temp res = {ctx->program->allocateId(), RegClass(type, size * size_hint)};
3134 vec->definitions[0] = Definition(res);
3135 ctx->block->instructions.emplace_back(std::move(vec));
3136 return res;
3137 }
3138
3139 void ds_write_helper(isel_context *ctx, Operand m, Temp address, Temp data, unsigned data_start, unsigned total_size, unsigned offset0, unsigned offset1, unsigned align)
3140 {
3141 Builder bld(ctx->program, ctx->block);
3142 unsigned bytes_written = 0;
3143 bool large_ds_write = ctx->options->chip_class >= GFX7;
3144 bool usable_write2 = ctx->options->chip_class >= GFX7;
3145
3146 while (bytes_written < total_size * 4) {
3147 unsigned todo = total_size * 4 - bytes_written;
3148 bool aligned8 = bytes_written % 8 == 0 && align % 8 == 0;
3149 bool aligned16 = bytes_written % 16 == 0 && align % 16 == 0;
3150
3151 aco_opcode op = aco_opcode::last_opcode;
3152 bool write2 = false;
3153 unsigned size = 0;
3154 if (todo >= 16 && aligned16 && large_ds_write) {
3155 op = aco_opcode::ds_write_b128;
3156 size = 4;
3157 } else if (todo >= 16 && aligned8 && usable_write2) {
3158 op = aco_opcode::ds_write2_b64;
3159 write2 = true;
3160 size = 4;
3161 } else if (todo >= 12 && aligned16 && large_ds_write) {
3162 op = aco_opcode::ds_write_b96;
3163 size = 3;
3164 } else if (todo >= 8 && aligned8) {
3165 op = aco_opcode::ds_write_b64;
3166 size = 2;
3167 } else if (todo >= 8 && usable_write2) {
3168 op = aco_opcode::ds_write2_b32;
3169 write2 = true;
3170 size = 2;
3171 } else if (todo >= 4) {
3172 op = aco_opcode::ds_write_b32;
3173 size = 1;
3174 } else {
3175 assert(false);
3176 }
3177
3178 unsigned offset = offset0 + offset1 + bytes_written;
3179 unsigned max_offset = write2 ? 1020 : 65535;
3180 Temp address_offset = address;
3181 if (offset > max_offset) {
3182 address_offset = bld.vadd32(bld.def(v1), Operand(offset0), address_offset);
3183 offset = offset1 + bytes_written;
3184 }
3185 assert(offset <= max_offset); /* offset1 shouldn't be large enough for this to happen */
3186
3187 if (write2) {
3188 Temp val0 = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size / 2, RegType::vgpr);
3189 Temp val1 = extract_subvector(ctx, data, data_start + (bytes_written >> 2) + 1, size / 2, RegType::vgpr);
3190 bld.ds(op, address_offset, val0, val1, m, offset / size / 2, (offset / size / 2) + 1);
3191 } else {
3192 Temp val = extract_subvector(ctx, data, data_start + (bytes_written >> 2), size, RegType::vgpr);
3193 bld.ds(op, address_offset, val, m, offset);
3194 }
3195
3196 bytes_written += size * 4;
3197 }
3198 }
3199
3200 void store_lds(isel_context *ctx, unsigned elem_size_bytes, Temp data, uint32_t wrmask,
3201 Temp address, unsigned base_offset, unsigned align)
3202 {
3203 assert(util_is_power_of_two_nonzero(align) && align >= 4);
3204 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3205
3206 Operand m = load_lds_size_m0(ctx);
3207
3208 /* we need at most two stores, assuming that the writemask is at most 4 bits wide */
3209 assert(wrmask <= 0x0f);
3210 int start[2], count[2];
3211 u_bit_scan_consecutive_range(&wrmask, &start[0], &count[0]);
3212 u_bit_scan_consecutive_range(&wrmask, &start[1], &count[1]);
3213 assert(wrmask == 0);
3214
3215 /* one combined store is sufficient */
3216 if (count[0] == count[1] && (align % elem_size_bytes) == 0 && (base_offset % elem_size_bytes) == 0) {
3217 Builder bld(ctx->program, ctx->block);
3218
3219 Temp address_offset = address;
3220 if ((base_offset / elem_size_bytes) + start[1] > 255) {
3221 address_offset = bld.vadd32(bld.def(v1), Operand(base_offset), address_offset);
3222 base_offset = 0;
3223 }
3224
3225 assert(count[0] == 1);
3226 RegClass xtract_rc(RegType::vgpr, elem_size_bytes / 4);
3227
3228 Temp val0 = emit_extract_vector(ctx, data, start[0], xtract_rc);
3229 Temp val1 = emit_extract_vector(ctx, data, start[1], xtract_rc);
3230 aco_opcode op = elem_size_bytes == 4 ? aco_opcode::ds_write2_b32 : aco_opcode::ds_write2_b64;
3231 base_offset = base_offset / elem_size_bytes;
3232 bld.ds(op, address_offset, val0, val1, m,
3233 base_offset + start[0], base_offset + start[1]);
3234 return;
3235 }
3236
3237 for (unsigned i = 0; i < 2; i++) {
3238 if (count[i] == 0)
3239 continue;
3240
3241 unsigned elem_size_words = elem_size_bytes / 4;
3242 ds_write_helper(ctx, m, address, data, start[i] * elem_size_words, count[i] * elem_size_words,
3243 base_offset, start[i] * elem_size_bytes, align);
3244 }
3245 return;
3246 }
3247
3248 unsigned calculate_lds_alignment(isel_context *ctx, unsigned const_offset)
3249 {
3250 unsigned align = 16;
3251 if (const_offset)
3252 align = std::min(align, 1u << (ffs(const_offset) - 1));
3253
3254 return align;
3255 }
3256
3257
3258 Temp create_vec_from_array(isel_context *ctx, Temp arr[], unsigned cnt, RegType reg_type, unsigned elem_size_bytes,
3259 unsigned split_cnt = 0u, Temp dst = Temp())
3260 {
3261 Builder bld(ctx->program, ctx->block);
3262 unsigned dword_size = elem_size_bytes / 4;
3263
3264 if (!dst.id())
3265 dst = bld.tmp(RegClass(reg_type, cnt * dword_size));
3266
3267 std::array<Temp, NIR_MAX_VEC_COMPONENTS> allocated_vec;
3268 aco_ptr<Pseudo_instruction> instr {create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, cnt, 1)};
3269 instr->definitions[0] = Definition(dst);
3270
3271 for (unsigned i = 0; i < cnt; ++i) {
3272 if (arr[i].id()) {
3273 assert(arr[i].size() == dword_size);
3274 allocated_vec[i] = arr[i];
3275 instr->operands[i] = Operand(arr[i]);
3276 } else {
3277 Temp zero = bld.copy(bld.def(RegClass(reg_type, dword_size)), Operand(0u, dword_size == 2));
3278 allocated_vec[i] = zero;
3279 instr->operands[i] = Operand(zero);
3280 }
3281 }
3282
3283 bld.insert(std::move(instr));
3284
3285 if (split_cnt)
3286 emit_split_vector(ctx, dst, split_cnt);
3287 else
3288 ctx->allocated_vec.emplace(dst.id(), allocated_vec); /* emit_split_vector already does this */
3289
3290 return dst;
3291 }
3292
3293 inline unsigned resolve_excess_vmem_const_offset(Builder &bld, Temp &voffset, unsigned const_offset)
3294 {
3295 if (const_offset >= 4096) {
3296 unsigned excess_const_offset = const_offset / 4096u * 4096u;
3297 const_offset %= 4096u;
3298
3299 if (!voffset.id())
3300 voffset = bld.copy(bld.def(v1), Operand(excess_const_offset));
3301 else if (unlikely(voffset.regClass() == s1))
3302 voffset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), Operand(excess_const_offset), Operand(voffset));
3303 else if (likely(voffset.regClass() == v1))
3304 voffset = bld.vadd32(bld.def(v1), Operand(voffset), Operand(excess_const_offset));
3305 else
3306 unreachable("Unsupported register class of voffset");
3307 }
3308
3309 return const_offset;
3310 }
3311
3312 void emit_single_mubuf_store(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset, Temp vdata,
3313 unsigned const_offset = 0u, bool allow_reorder = true, bool slc = false)
3314 {
3315 assert(vdata.id());
3316 assert(vdata.size() != 3 || ctx->program->chip_class != GFX6);
3317 assert(vdata.size() >= 1 && vdata.size() <= 4);
3318
3319 Builder bld(ctx->program, ctx->block);
3320 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_store_dword + vdata.size() - 1);
3321 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3322
3323 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3324 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3325 Builder::Result r = bld.mubuf(op, Operand(descriptor), voffset_op, soffset_op, Operand(vdata), const_offset,
3326 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3327 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc);
3328
3329 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3330 }
3331
3332 void store_vmem_mubuf(isel_context *ctx, Temp src, Temp descriptor, Temp voffset, Temp soffset,
3333 unsigned base_const_offset, unsigned elem_size_bytes, unsigned write_mask,
3334 bool allow_combining = true, bool reorder = true, bool slc = false)
3335 {
3336 Builder bld(ctx->program, ctx->block);
3337 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3338 assert(write_mask);
3339
3340 if (elem_size_bytes == 8) {
3341 elem_size_bytes = 4;
3342 write_mask = widen_mask(write_mask, 2);
3343 }
3344
3345 while (write_mask) {
3346 int start = 0;
3347 int count = 0;
3348 u_bit_scan_consecutive_range(&write_mask, &start, &count);
3349 assert(count > 0);
3350 assert(start >= 0);
3351
3352 while (count > 0) {
3353 unsigned sub_count = allow_combining ? MIN2(count, 4) : 1;
3354 unsigned const_offset = (unsigned) start * elem_size_bytes + base_const_offset;
3355
3356 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
3357 if (unlikely(ctx->program->chip_class == GFX6 && sub_count == 3))
3358 sub_count = 2;
3359
3360 Temp elem = extract_subvector(ctx, src, start, sub_count, RegType::vgpr);
3361 emit_single_mubuf_store(ctx, descriptor, voffset, soffset, elem, const_offset, reorder, slc);
3362
3363 count -= sub_count;
3364 start += sub_count;
3365 }
3366
3367 assert(count == 0);
3368 }
3369 }
3370
3371 Temp emit_single_mubuf_load(isel_context *ctx, Temp descriptor, Temp voffset, Temp soffset,
3372 unsigned const_offset, unsigned size_dwords, bool allow_reorder = true)
3373 {
3374 assert(size_dwords != 3 || ctx->program->chip_class != GFX6);
3375 assert(size_dwords >= 1 && size_dwords <= 4);
3376
3377 Builder bld(ctx->program, ctx->block);
3378 Temp vdata = bld.tmp(RegClass(RegType::vgpr, size_dwords));
3379 aco_opcode op = (aco_opcode) ((unsigned) aco_opcode::buffer_load_dword + size_dwords - 1);
3380 const_offset = resolve_excess_vmem_const_offset(bld, voffset, const_offset);
3381
3382 Operand voffset_op = voffset.id() ? Operand(as_vgpr(ctx, voffset)) : Operand(v1);
3383 Operand soffset_op = soffset.id() ? Operand(soffset) : Operand(0u);
3384 Builder::Result r = bld.mubuf(op, Definition(vdata), Operand(descriptor), voffset_op, soffset_op, const_offset,
3385 /* offen */ !voffset_op.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3386 /* disable_wqm */ false, /* glc */ true,
3387 /* dlc*/ ctx->program->chip_class >= GFX10, /* slc */ false);
3388
3389 static_cast<MUBUF_instruction *>(r.instr)->can_reorder = allow_reorder;
3390
3391 return vdata;
3392 }
3393
3394 void load_vmem_mubuf(isel_context *ctx, Temp dst, Temp descriptor, Temp voffset, Temp soffset,
3395 unsigned base_const_offset, unsigned elem_size_bytes, unsigned num_components,
3396 unsigned stride = 0u, bool allow_combining = true, bool allow_reorder = true)
3397 {
3398 assert(elem_size_bytes == 4 || elem_size_bytes == 8);
3399 assert((num_components * elem_size_bytes / 4) == dst.size());
3400 assert(!!stride != allow_combining);
3401
3402 Builder bld(ctx->program, ctx->block);
3403 unsigned split_cnt = num_components;
3404
3405 if (elem_size_bytes == 8) {
3406 elem_size_bytes = 4;
3407 num_components *= 2;
3408 }
3409
3410 if (!stride)
3411 stride = elem_size_bytes;
3412
3413 unsigned load_size = 1;
3414 if (allow_combining) {
3415 if ((num_components % 4) == 0)
3416 load_size = 4;
3417 else if ((num_components % 3) == 0 && ctx->program->chip_class != GFX6)
3418 load_size = 3;
3419 else if ((num_components % 2) == 0)
3420 load_size = 2;
3421 }
3422
3423 unsigned num_loads = num_components / load_size;
3424 std::array<Temp, NIR_MAX_VEC_COMPONENTS> elems;
3425
3426 for (unsigned i = 0; i < num_loads; ++i) {
3427 unsigned const_offset = i * stride * load_size + base_const_offset;
3428 elems[i] = emit_single_mubuf_load(ctx, descriptor, voffset, soffset, const_offset, load_size, allow_reorder);
3429 }
3430
3431 create_vec_from_array(ctx, elems.data(), num_loads, RegType::vgpr, load_size * 4u, split_cnt, dst);
3432 }
3433
3434 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)
3435 {
3436 Builder bld(ctx->program, ctx->block);
3437 Temp offset = base_offset.first;
3438 unsigned const_offset = base_offset.second;
3439
3440 if (!nir_src_is_const(*off_src)) {
3441 Temp indirect_offset_arg = get_ssa_temp(ctx, off_src->ssa);
3442 Temp with_stride;
3443
3444 /* Calculate indirect offset with stride */
3445 if (likely(indirect_offset_arg.regClass() == v1))
3446 with_stride = bld.v_mul_imm(bld.def(v1), indirect_offset_arg, stride);
3447 else if (indirect_offset_arg.regClass() == s1)
3448 with_stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), indirect_offset_arg);
3449 else
3450 unreachable("Unsupported register class of indirect offset");
3451
3452 /* Add to the supplied base offset */
3453 if (offset.id() == 0)
3454 offset = with_stride;
3455 else if (unlikely(offset.regClass() == s1 && with_stride.regClass() == s1))
3456 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), with_stride, offset);
3457 else if (offset.size() == 1 && with_stride.size() == 1)
3458 offset = bld.vadd32(bld.def(v1), with_stride, offset);
3459 else
3460 unreachable("Unsupported register class of indirect offset");
3461 } else {
3462 unsigned const_offset_arg = nir_src_as_uint(*off_src);
3463 const_offset += const_offset_arg * stride;
3464 }
3465
3466 return std::make_pair(offset, const_offset);
3467 }
3468
3469 std::pair<Temp, unsigned> offset_add(isel_context *ctx, const std::pair<Temp, unsigned> &off1, const std::pair<Temp, unsigned> &off2)
3470 {
3471 Builder bld(ctx->program, ctx->block);
3472 Temp offset;
3473
3474 if (off1.first.id() && off2.first.id()) {
3475 if (unlikely(off1.first.regClass() == s1 && off2.first.regClass() == s1))
3476 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), off1.first, off2.first);
3477 else if (off1.first.size() == 1 && off2.first.size() == 1)
3478 offset = bld.vadd32(bld.def(v1), off1.first, off2.first);
3479 else
3480 unreachable("Unsupported register class of indirect offset");
3481 } else {
3482 offset = off1.first.id() ? off1.first : off2.first;
3483 }
3484
3485 return std::make_pair(offset, off1.second + off2.second);
3486 }
3487
3488 std::pair<Temp, unsigned> offset_mul(isel_context *ctx, const std::pair<Temp, unsigned> &offs, unsigned multiplier)
3489 {
3490 Builder bld(ctx->program, ctx->block);
3491 unsigned const_offset = offs.second * multiplier;
3492
3493 if (!offs.first.id())
3494 return std::make_pair(offs.first, const_offset);
3495
3496 Temp offset = unlikely(offs.first.regClass() == s1)
3497 ? bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(multiplier), offs.first)
3498 : bld.v_mul_imm(bld.def(v1), offs.first, multiplier);
3499
3500 return std::make_pair(offset, const_offset);
3501 }
3502
3503 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride, unsigned component_stride)
3504 {
3505 Builder bld(ctx->program, ctx->block);
3506
3507 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3508 unsigned const_offset = nir_intrinsic_base(instr) * base_stride;
3509 /* component is in bytes */
3510 const_offset += nir_intrinsic_component(instr) * component_stride;
3511
3512 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3513 nir_src *off_src = nir_get_io_offset_src(instr);
3514 return offset_add_from_nir(ctx, std::make_pair(Temp(), const_offset), off_src, 4u * base_stride);
3515 }
3516
3517 std::pair<Temp, unsigned> get_intrinsic_io_basic_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned stride = 1u)
3518 {
3519 return get_intrinsic_io_basic_offset(ctx, instr, stride, stride);
3520 }
3521
3522 Temp get_tess_rel_patch_id(isel_context *ctx)
3523 {
3524 Builder bld(ctx->program, ctx->block);
3525
3526 switch (ctx->shader->info.stage) {
3527 case MESA_SHADER_TESS_CTRL:
3528 return bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffu),
3529 get_arg(ctx, ctx->args->ac.tcs_rel_ids));
3530 case MESA_SHADER_TESS_EVAL:
3531 return get_arg(ctx, ctx->args->tes_rel_patch_id);
3532 default:
3533 unreachable("Unsupported stage in get_tess_rel_patch_id");
3534 }
3535 }
3536
3537 std::pair<Temp, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3538 {
3539 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3540 Builder bld(ctx->program, ctx->block);
3541
3542 uint32_t tcs_in_patch_stride = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 4;
3543 uint32_t tcs_in_vertex_stride = ctx->tcs_num_inputs * 4;
3544
3545 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr);
3546
3547 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3548 offs = offset_add_from_nir(ctx, offs, vertex_index_src, tcs_in_vertex_stride);
3549
3550 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3551 Temp tcs_in_current_patch_offset = bld.v_mul24_imm(bld.def(v1), rel_patch_id, tcs_in_patch_stride);
3552 offs = offset_add(ctx, offs, std::make_pair(tcs_in_current_patch_offset, 0));
3553
3554 return offset_mul(ctx, offs, 4u);
3555 }
3556
3557 std::pair<Temp, unsigned> get_tcs_output_lds_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, bool per_vertex = false)
3558 {
3559 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3560 Builder bld(ctx->program, ctx->block);
3561
3562 uint32_t input_patch_size = ctx->args->options->key.tcs.input_vertices * ctx->tcs_num_inputs * 16;
3563 uint32_t num_tcs_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
3564 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
3565 uint32_t output_vertex_size = num_tcs_outputs * 16;
3566 uint32_t pervertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3567 uint32_t output_patch_stride = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
3568
3569 std::pair<Temp, unsigned> offs = instr
3570 ? get_intrinsic_io_basic_offset(ctx, instr, 4u)
3571 : std::make_pair(Temp(), 0u);
3572
3573 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3574 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, output_patch_stride);
3575
3576 if (per_vertex) {
3577 assert(instr);
3578
3579 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3580 offs = offset_add_from_nir(ctx, offs, vertex_index_src, output_vertex_size);
3581
3582 uint32_t output_patch0_offset = (input_patch_size * ctx->tcs_num_patches);
3583 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_offset));
3584 } else {
3585 uint32_t output_patch0_patch_data_offset = (input_patch_size * ctx->tcs_num_patches + pervertex_output_patch_size);
3586 offs = offset_add(ctx, offs, std::make_pair(patch_off, output_patch0_patch_data_offset));
3587 }
3588
3589 return offs;
3590 }
3591
3592 std::pair<Temp, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr)
3593 {
3594 Builder bld(ctx->program, ctx->block);
3595
3596 unsigned vertices_per_patch = ctx->shader->info.tess.tcs_vertices_out;
3597 unsigned attr_stride = vertices_per_patch * ctx->tcs_num_patches;
3598
3599 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u);
3600
3601 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3602 Temp patch_off = bld.v_mul24_imm(bld.def(v1), rel_patch_id, vertices_per_patch * 16u);
3603 offs = offset_add(ctx, offs, std::make_pair(patch_off, 0u));
3604
3605 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3606 offs = offset_add_from_nir(ctx, offs, vertex_index_src, 16u);
3607
3608 return offs;
3609 }
3610
3611 std::pair<Temp, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context *ctx, nir_intrinsic_instr *instr = nullptr, unsigned const_base_offset = 0u)
3612 {
3613 Builder bld(ctx->program, ctx->block);
3614
3615 unsigned num_tcs_outputs = ctx->shader->info.stage == MESA_SHADER_TESS_CTRL
3616 ? util_last_bit64(ctx->args->shader_info->tcs.outputs_written)
3617 : ctx->args->options->key.tes.tcs_num_outputs;
3618
3619 unsigned output_vertex_size = num_tcs_outputs * 16;
3620 unsigned per_vertex_output_patch_size = ctx->shader->info.tess.tcs_vertices_out * output_vertex_size;
3621 unsigned per_patch_data_offset = per_vertex_output_patch_size * ctx->tcs_num_patches;
3622 unsigned attr_stride = ctx->tcs_num_patches;
3623
3624 std::pair<Temp, unsigned> offs = instr
3625 ? get_intrinsic_io_basic_offset(ctx, instr, attr_stride * 4u, 4u)
3626 : std::make_pair(Temp(), 0u);
3627
3628 if (const_base_offset)
3629 offs.second += const_base_offset * attr_stride;
3630
3631 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
3632 Temp patch_off = bld.v_mul_imm(bld.def(v1), rel_patch_id, 16u);
3633 offs = offset_add(ctx, offs, std::make_pair(patch_off, per_patch_data_offset));
3634
3635 return offs;
3636 }
3637
3638 bool tcs_driver_location_matches_api_mask(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex, uint64_t mask, bool *indirect)
3639 {
3640 unsigned off = nir_intrinsic_base(instr) * 4u;
3641 nir_src *off_src = nir_get_io_offset_src(instr);
3642
3643 if (!nir_src_is_const(*off_src)) {
3644 *indirect = true;
3645 return false;
3646 }
3647
3648 *indirect = false;
3649 off += nir_src_as_uint(*off_src) * 16u;
3650
3651 while (mask) {
3652 unsigned slot = u_bit_scan64(&mask) + (per_vertex ? 0 : VARYING_SLOT_PATCH0);
3653 if (off == shader_io_get_unique_index((gl_varying_slot) slot) * 16u)
3654 return true;
3655 }
3656
3657 return false;
3658 }
3659
3660 bool store_output_to_temps(isel_context *ctx, nir_intrinsic_instr *instr)
3661 {
3662 unsigned write_mask = nir_intrinsic_write_mask(instr);
3663 unsigned component = nir_intrinsic_component(instr);
3664 unsigned idx = nir_intrinsic_base(instr) + component;
3665
3666 nir_instr *off_instr = instr->src[1].ssa->parent_instr;
3667 if (off_instr->type != nir_instr_type_load_const)
3668 return false;
3669
3670 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3671 idx += nir_src_as_uint(instr->src[1]) * 4u;
3672
3673 if (instr->src[0].ssa->bit_size == 64)
3674 write_mask = widen_mask(write_mask, 2);
3675
3676 for (unsigned i = 0; i < 8; ++i) {
3677 if (write_mask & (1 << i)) {
3678 ctx->outputs.mask[idx / 4u] |= 1 << (idx % 4u);
3679 ctx->outputs.temps[idx] = emit_extract_vector(ctx, src, i, v1);
3680 }
3681 idx++;
3682 }
3683
3684 return true;
3685 }
3686
3687 bool load_input_from_temps(isel_context *ctx, nir_intrinsic_instr *instr, Temp dst)
3688 {
3689 /* Only TCS per-vertex inputs are supported by this function.
3690 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
3691 */
3692 if (ctx->shader->info.stage != MESA_SHADER_TESS_CTRL || !ctx->tcs_in_out_eq)
3693 return false;
3694
3695 nir_src *off_src = nir_get_io_offset_src(instr);
3696 nir_src *vertex_index_src = nir_get_io_vertex_index_src(instr);
3697 nir_instr *vertex_index_instr = vertex_index_src->ssa->parent_instr;
3698 bool can_use_temps = nir_src_is_const(*off_src) &&
3699 vertex_index_instr->type == nir_instr_type_intrinsic &&
3700 nir_instr_as_intrinsic(vertex_index_instr)->intrinsic == nir_intrinsic_load_invocation_id;
3701
3702 if (!can_use_temps)
3703 return false;
3704
3705 unsigned idx = nir_intrinsic_base(instr) + nir_intrinsic_component(instr) + 4 * nir_src_as_uint(*off_src);
3706 Temp *src = &ctx->inputs.temps[idx];
3707 Temp vec = create_vec_from_array(ctx, src, dst.size(), dst.regClass().type(), 4u);
3708 assert(vec.size() == dst.size());
3709
3710 Builder bld(ctx->program, ctx->block);
3711 bld.copy(Definition(dst), vec);
3712 return true;
3713 }
3714
3715 void visit_store_ls_or_es_output(isel_context *ctx, nir_intrinsic_instr *instr)
3716 {
3717 Builder bld(ctx->program, ctx->block);
3718
3719 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, 4u);
3720 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
3721 unsigned write_mask = nir_intrinsic_write_mask(instr);
3722 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8u;
3723
3724 if (ctx->tcs_in_out_eq && store_output_to_temps(ctx, instr)) {
3725 /* 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. */
3726 bool indirect_write;
3727 bool temp_only_input = tcs_driver_location_matches_api_mask(ctx, instr, true, ctx->tcs_temp_only_inputs, &indirect_write);
3728 if (temp_only_input && !indirect_write)
3729 return;
3730 }
3731
3732 if (ctx->stage == vertex_es || ctx->stage == tess_eval_es) {
3733 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
3734 Temp esgs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_VS * 16u));
3735 Temp es2gs_offset = get_arg(ctx, ctx->args->es2gs_offset);
3736 store_vmem_mubuf(ctx, src, esgs_ring, offs.first, es2gs_offset, offs.second, elem_size_bytes, write_mask, false, true, true);
3737 } else {
3738 Temp lds_base;
3739
3740 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
3741 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
3742 unsigned itemsize = ctx->stage == vertex_geometry_gs
3743 ? ctx->program->info->vs.es_info.esgs_itemsize
3744 : ctx->program->info->tes.es_info.esgs_itemsize;
3745 Temp thread_id = emit_mbcnt(ctx, bld.def(v1));
3746 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));
3747 Temp vertex_idx = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), thread_id,
3748 bld.v_mul24_imm(bld.def(v1), as_vgpr(ctx, wave_idx), ctx->program->wave_size));
3749 lds_base = bld.v_mul24_imm(bld.def(v1), vertex_idx, itemsize);
3750 } else if (ctx->stage == vertex_ls || ctx->stage == vertex_tess_control_hs) {
3751 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
3752 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
3753 */
3754 unsigned num_tcs_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
3755 Temp vertex_idx = get_arg(ctx, ctx->args->rel_auto_id);
3756 lds_base = bld.v_mul_imm(bld.def(v1), vertex_idx, num_tcs_inputs * 16u);
3757 } else {
3758 unreachable("Invalid LS or ES stage");
3759 }
3760
3761 offs = offset_add(ctx, offs, std::make_pair(lds_base, 0u));
3762 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
3763 store_lds(ctx, elem_size_bytes, src, write_mask, offs.first, offs.second, lds_align);
3764 }
3765 }
3766
3767 bool should_write_tcs_patch_output_to_vmem(isel_context *ctx, nir_intrinsic_instr *instr)
3768 {
3769 unsigned off = nir_intrinsic_base(instr) * 4u;
3770 return off != ctx->tcs_tess_lvl_out_loc &&
3771 off != ctx->tcs_tess_lvl_in_loc;
3772 }
3773
3774 bool should_write_tcs_output_to_lds(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3775 {
3776 /* When none of the appropriate outputs are read, we are OK to never write to LDS */
3777 if (per_vertex ? ctx->shader->info.outputs_read == 0U : ctx->shader->info.patch_outputs_read == 0u)
3778 return false;
3779
3780 uint64_t mask = per_vertex
3781 ? ctx->shader->info.outputs_read
3782 : ctx->shader->info.patch_outputs_read;
3783 bool indirect_write;
3784 bool output_read = tcs_driver_location_matches_api_mask(ctx, instr, per_vertex, mask, &indirect_write);
3785 return indirect_write || output_read;
3786 }
3787
3788 void visit_store_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3789 {
3790 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3791 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3792
3793 Builder bld(ctx->program, ctx->block);
3794
3795 Temp store_val = get_ssa_temp(ctx, instr->src[0].ssa);
3796 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3797 unsigned write_mask = nir_intrinsic_write_mask(instr);
3798
3799 /* Only write to VMEM if the output is per-vertex or it's per-patch non tess factor */
3800 bool write_to_vmem = per_vertex || should_write_tcs_patch_output_to_vmem(ctx, instr);
3801 /* Only write to LDS if the output is read by the shader, or it's per-patch tess factor */
3802 bool write_to_lds = !write_to_vmem || should_write_tcs_output_to_lds(ctx, instr, per_vertex);
3803
3804 if (write_to_vmem) {
3805 std::pair<Temp, unsigned> vmem_offs = per_vertex
3806 ? get_tcs_per_vertex_output_vmem_offset(ctx, instr)
3807 : get_tcs_per_patch_output_vmem_offset(ctx, instr);
3808
3809 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));
3810 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
3811 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);
3812 }
3813
3814 if (write_to_lds) {
3815 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3816 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3817 store_lds(ctx, elem_size_bytes, store_val, write_mask, lds_offs.first, lds_offs.second, lds_align);
3818 }
3819 }
3820
3821 void visit_load_tcs_output(isel_context *ctx, nir_intrinsic_instr *instr, bool per_vertex)
3822 {
3823 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
3824 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
3825
3826 Builder bld(ctx->program, ctx->block);
3827
3828 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3829 std::pair<Temp, unsigned> lds_offs = get_tcs_output_lds_offset(ctx, instr, per_vertex);
3830 unsigned lds_align = calculate_lds_alignment(ctx, lds_offs.second);
3831 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
3832
3833 load_lds(ctx, elem_size_bytes, dst, lds_offs.first, lds_offs.second, lds_align);
3834 }
3835
3836 void visit_store_output(isel_context *ctx, nir_intrinsic_instr *instr)
3837 {
3838 if (ctx->stage == vertex_vs ||
3839 ctx->stage == tess_eval_vs ||
3840 ctx->stage == fragment_fs ||
3841 ctx->stage == ngg_vertex_gs ||
3842 ctx->stage == ngg_tess_eval_gs ||
3843 ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
3844 bool stored_to_temps = store_output_to_temps(ctx, instr);
3845 if (!stored_to_temps) {
3846 fprintf(stderr, "Unimplemented output offset instruction:\n");
3847 nir_print_instr(instr->src[1].ssa->parent_instr, stderr);
3848 fprintf(stderr, "\n");
3849 abort();
3850 }
3851 } else if (ctx->stage == vertex_es ||
3852 ctx->stage == vertex_ls ||
3853 ctx->stage == tess_eval_es ||
3854 (ctx->stage == vertex_tess_control_hs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3855 (ctx->stage == vertex_geometry_gs && ctx->shader->info.stage == MESA_SHADER_VERTEX) ||
3856 (ctx->stage == tess_eval_geometry_gs && ctx->shader->info.stage == MESA_SHADER_TESS_EVAL)) {
3857 visit_store_ls_or_es_output(ctx, instr);
3858 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
3859 visit_store_tcs_output(ctx, instr, false);
3860 } else {
3861 unreachable("Shader stage not implemented");
3862 }
3863 }
3864
3865 void visit_load_output(isel_context *ctx, nir_intrinsic_instr *instr)
3866 {
3867 visit_load_tcs_output(ctx, instr, false);
3868 }
3869
3870 void emit_interp_instr(isel_context *ctx, unsigned idx, unsigned component, Temp src, Temp dst, Temp prim_mask)
3871 {
3872 Temp coord1 = emit_extract_vector(ctx, src, 0, v1);
3873 Temp coord2 = emit_extract_vector(ctx, src, 1, v1);
3874
3875 Builder bld(ctx->program, ctx->block);
3876 Builder::Result interp_p1 = bld.vintrp(aco_opcode::v_interp_p1_f32, bld.def(v1), coord1, bld.m0(prim_mask), idx, component);
3877 if (ctx->program->has_16bank_lds)
3878 interp_p1.instr->operands[0].setLateKill(true);
3879 bld.vintrp(aco_opcode::v_interp_p2_f32, Definition(dst), coord2, bld.m0(prim_mask), interp_p1, idx, component);
3880 }
3881
3882 void emit_load_frag_coord(isel_context *ctx, Temp dst, unsigned num_components)
3883 {
3884 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1));
3885 for (unsigned i = 0; i < num_components; i++)
3886 vec->operands[i] = Operand(get_arg(ctx, ctx->args->ac.frag_pos[i]));
3887 if (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_ena)) {
3888 assert(num_components == 4);
3889 Builder bld(ctx->program, ctx->block);
3890 vec->operands[3] = bld.vop1(aco_opcode::v_rcp_f32, bld.def(v1), get_arg(ctx, ctx->args->ac.frag_pos[3]));
3891 }
3892
3893 for (Operand& op : vec->operands)
3894 op = op.isUndefined() ? Operand(0u) : op;
3895
3896 vec->definitions[0] = Definition(dst);
3897 ctx->block->instructions.emplace_back(std::move(vec));
3898 emit_split_vector(ctx, dst, num_components);
3899 return;
3900 }
3901
3902 void visit_load_interpolated_input(isel_context *ctx, nir_intrinsic_instr *instr)
3903 {
3904 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
3905 Temp coords = get_ssa_temp(ctx, instr->src[0].ssa);
3906 unsigned idx = nir_intrinsic_base(instr);
3907 unsigned component = nir_intrinsic_component(instr);
3908 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
3909
3910 nir_const_value* offset = nir_src_as_const_value(instr->src[1]);
3911 if (offset) {
3912 assert(offset->u32 == 0);
3913 } else {
3914 /* the lower 15bit of the prim_mask contain the offset into LDS
3915 * while the upper bits contain the number of prims */
3916 Temp offset_src = get_ssa_temp(ctx, instr->src[1].ssa);
3917 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
3918 Builder bld(ctx->program, ctx->block);
3919 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
3920 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
3921 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
3922 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
3923 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
3924 }
3925
3926 if (instr->dest.ssa.num_components == 1) {
3927 emit_interp_instr(ctx, idx, component, coords, dst, prim_mask);
3928 } else {
3929 aco_ptr<Pseudo_instruction> vec(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, instr->dest.ssa.num_components, 1));
3930 for (unsigned i = 0; i < instr->dest.ssa.num_components; i++)
3931 {
3932 Temp tmp = {ctx->program->allocateId(), v1};
3933 emit_interp_instr(ctx, idx, component+i, coords, tmp, prim_mask);
3934 vec->operands[i] = Operand(tmp);
3935 }
3936 vec->definitions[0] = Definition(dst);
3937 ctx->block->instructions.emplace_back(std::move(vec));
3938 }
3939 }
3940
3941 bool check_vertex_fetch_size(isel_context *ctx, const ac_data_format_info *vtx_info,
3942 unsigned offset, unsigned stride, unsigned channels)
3943 {
3944 unsigned vertex_byte_size = vtx_info->chan_byte_size * channels;
3945 if (vtx_info->chan_byte_size != 4 && channels == 3)
3946 return false;
3947 return (ctx->options->chip_class != GFX6 && ctx->options->chip_class != GFX10) ||
3948 (offset % vertex_byte_size == 0 && stride % vertex_byte_size == 0);
3949 }
3950
3951 uint8_t get_fetch_data_format(isel_context *ctx, const ac_data_format_info *vtx_info,
3952 unsigned offset, unsigned stride, unsigned *channels)
3953 {
3954 if (!vtx_info->chan_byte_size) {
3955 *channels = vtx_info->num_channels;
3956 return vtx_info->chan_format;
3957 }
3958
3959 unsigned num_channels = *channels;
3960 if (!check_vertex_fetch_size(ctx, vtx_info, offset, stride, *channels)) {
3961 unsigned new_channels = num_channels + 1;
3962 /* first, assume more loads is worse and try using a larger data format */
3963 while (new_channels <= 4 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels)) {
3964 new_channels++;
3965 /* don't make the attribute potentially out-of-bounds */
3966 if (offset + new_channels * vtx_info->chan_byte_size > stride)
3967 new_channels = 5;
3968 }
3969
3970 if (new_channels == 5) {
3971 /* then try decreasing load size (at the cost of more loads) */
3972 new_channels = *channels;
3973 while (new_channels > 1 && !check_vertex_fetch_size(ctx, vtx_info, offset, stride, new_channels))
3974 new_channels--;
3975 }
3976
3977 if (new_channels < *channels)
3978 *channels = new_channels;
3979 num_channels = new_channels;
3980 }
3981
3982 switch (vtx_info->chan_format) {
3983 case V_008F0C_BUF_DATA_FORMAT_8:
3984 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8, V_008F0C_BUF_DATA_FORMAT_8_8,
3985 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_8_8_8_8}[num_channels - 1];
3986 case V_008F0C_BUF_DATA_FORMAT_16:
3987 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16, V_008F0C_BUF_DATA_FORMAT_16_16,
3988 V_008F0C_BUF_DATA_FORMAT_INVALID, V_008F0C_BUF_DATA_FORMAT_16_16_16_16}[num_channels - 1];
3989 case V_008F0C_BUF_DATA_FORMAT_32:
3990 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_DATA_FORMAT_32_32,
3991 V_008F0C_BUF_DATA_FORMAT_32_32_32, V_008F0C_BUF_DATA_FORMAT_32_32_32_32}[num_channels - 1];
3992 }
3993 unreachable("shouldn't reach here");
3994 return V_008F0C_BUF_DATA_FORMAT_INVALID;
3995 }
3996
3997 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3998 * so we may need to fix it up. */
3999 Temp adjust_vertex_fetch_alpha(isel_context *ctx, unsigned adjustment, Temp alpha)
4000 {
4001 Builder bld(ctx->program, ctx->block);
4002
4003 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
4004 alpha = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), alpha);
4005
4006 /* For the integer-like cases, do a natural sign extension.
4007 *
4008 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4009 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4010 * exponent.
4011 */
4012 alpha = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(adjustment == RADV_ALPHA_ADJUST_SNORM ? 7u : 30u), alpha);
4013 alpha = bld.vop2(aco_opcode::v_ashrrev_i32, bld.def(v1), Operand(30u), alpha);
4014
4015 /* Convert back to the right type. */
4016 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
4017 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4018 Temp clamp = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0xbf800000u), alpha);
4019 alpha = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0xbf800000u), alpha, clamp);
4020 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
4021 alpha = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), alpha);
4022 }
4023
4024 return alpha;
4025 }
4026
4027 void visit_load_input(isel_context *ctx, nir_intrinsic_instr *instr)
4028 {
4029 Builder bld(ctx->program, ctx->block);
4030 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4031 if (ctx->shader->info.stage == MESA_SHADER_VERTEX) {
4032
4033 nir_instr *off_instr = instr->src[0].ssa->parent_instr;
4034 if (off_instr->type != nir_instr_type_load_const) {
4035 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4036 nir_print_instr(off_instr, stderr);
4037 fprintf(stderr, "\n");
4038 }
4039 uint32_t offset = nir_instr_as_load_const(off_instr)->value[0].u32;
4040
4041 Temp vertex_buffers = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->vertex_buffers));
4042
4043 unsigned location = nir_intrinsic_base(instr) / 4 - VERT_ATTRIB_GENERIC0 + offset;
4044 unsigned component = nir_intrinsic_component(instr);
4045 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[location];
4046 uint32_t attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[location];
4047 uint32_t attrib_stride = ctx->options->key.vs.vertex_attribute_strides[location];
4048 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[location];
4049
4050 unsigned dfmt = attrib_format & 0xf;
4051 unsigned nfmt = (attrib_format >> 4) & 0x7;
4052 const struct ac_data_format_info *vtx_info = ac_get_data_format_info(dfmt);
4053
4054 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa) << component;
4055 unsigned num_channels = MIN2(util_last_bit(mask), vtx_info->num_channels);
4056 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (location * 2)) & 3;
4057 bool post_shuffle = ctx->options->key.vs.post_shuffle & (1 << location);
4058 if (post_shuffle)
4059 num_channels = MAX2(num_channels, 3);
4060
4061 Operand off = bld.copy(bld.def(s1), Operand(attrib_binding * 16u));
4062 Temp list = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), vertex_buffers, off);
4063
4064 Temp index;
4065 if (ctx->options->key.vs.instance_rate_inputs & (1u << location)) {
4066 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[location];
4067 Temp start_instance = get_arg(ctx, ctx->args->ac.start_instance);
4068 if (divisor) {
4069 Temp instance_id = get_arg(ctx, ctx->args->ac.instance_id);
4070 if (divisor != 1) {
4071 Temp divided = bld.tmp(v1);
4072 emit_v_div_u32(ctx, divided, as_vgpr(ctx, instance_id), divisor);
4073 index = bld.vadd32(bld.def(v1), start_instance, divided);
4074 } else {
4075 index = bld.vadd32(bld.def(v1), start_instance, instance_id);
4076 }
4077 } else {
4078 index = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), start_instance);
4079 }
4080 } else {
4081 index = bld.vadd32(bld.def(v1),
4082 get_arg(ctx, ctx->args->ac.base_vertex),
4083 get_arg(ctx, ctx->args->ac.vertex_id));
4084 }
4085
4086 Temp channels[num_channels];
4087 unsigned channel_start = 0;
4088 bool direct_fetch = false;
4089
4090 /* skip unused channels at the start */
4091 if (vtx_info->chan_byte_size && !post_shuffle) {
4092 channel_start = ffs(mask) - 1;
4093 for (unsigned i = 0; i < channel_start; i++)
4094 channels[i] = Temp(0, s1);
4095 } else if (vtx_info->chan_byte_size && post_shuffle && !(mask & 0x8)) {
4096 num_channels = 3 - (ffs(mask) - 1);
4097 }
4098
4099 /* load channels */
4100 while (channel_start < num_channels) {
4101 unsigned fetch_size = num_channels - channel_start;
4102 unsigned fetch_offset = attrib_offset + channel_start * vtx_info->chan_byte_size;
4103 bool expanded = false;
4104
4105 /* use MUBUF when possible to avoid possible alignment issues */
4106 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4107 bool use_mubuf = (nfmt == V_008F0C_BUF_NUM_FORMAT_FLOAT ||
4108 nfmt == V_008F0C_BUF_NUM_FORMAT_UINT ||
4109 nfmt == V_008F0C_BUF_NUM_FORMAT_SINT) &&
4110 vtx_info->chan_byte_size == 4;
4111 unsigned fetch_dfmt = V_008F0C_BUF_DATA_FORMAT_INVALID;
4112 if (!use_mubuf) {
4113 fetch_dfmt = get_fetch_data_format(ctx, vtx_info, fetch_offset, attrib_stride, &fetch_size);
4114 } else {
4115 if (fetch_size == 3 && ctx->options->chip_class == GFX6) {
4116 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4117 fetch_size = 4;
4118 expanded = true;
4119 }
4120 }
4121
4122 Temp fetch_index = index;
4123 if (attrib_stride != 0 && fetch_offset > attrib_stride) {
4124 fetch_index = bld.vadd32(bld.def(v1), Operand(fetch_offset / attrib_stride), fetch_index);
4125 fetch_offset = fetch_offset % attrib_stride;
4126 }
4127
4128 Operand soffset(0u);
4129 if (fetch_offset >= 4096) {
4130 soffset = bld.copy(bld.def(s1), Operand(fetch_offset / 4096 * 4096));
4131 fetch_offset %= 4096;
4132 }
4133
4134 aco_opcode opcode;
4135 switch (fetch_size) {
4136 case 1:
4137 opcode = use_mubuf ? aco_opcode::buffer_load_dword : aco_opcode::tbuffer_load_format_x;
4138 break;
4139 case 2:
4140 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx2 : aco_opcode::tbuffer_load_format_xy;
4141 break;
4142 case 3:
4143 assert(ctx->options->chip_class >= GFX7 ||
4144 (!use_mubuf && ctx->options->chip_class == GFX6));
4145 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx3 : aco_opcode::tbuffer_load_format_xyz;
4146 break;
4147 case 4:
4148 opcode = use_mubuf ? aco_opcode::buffer_load_dwordx4 : aco_opcode::tbuffer_load_format_xyzw;
4149 break;
4150 default:
4151 unreachable("Unimplemented load_input vector size");
4152 }
4153
4154 Temp fetch_dst;
4155 if (channel_start == 0 && fetch_size == dst.size() && !post_shuffle &&
4156 !expanded && (alpha_adjust == RADV_ALPHA_ADJUST_NONE ||
4157 num_channels <= 3)) {
4158 direct_fetch = true;
4159 fetch_dst = dst;
4160 } else {
4161 fetch_dst = bld.tmp(RegType::vgpr, fetch_size);
4162 }
4163
4164 if (use_mubuf) {
4165 Instruction *mubuf = bld.mubuf(opcode,
4166 Definition(fetch_dst), list, fetch_index, soffset,
4167 fetch_offset, false, true).instr;
4168 static_cast<MUBUF_instruction*>(mubuf)->can_reorder = true;
4169 } else {
4170 Instruction *mtbuf = bld.mtbuf(opcode,
4171 Definition(fetch_dst), list, fetch_index, soffset,
4172 fetch_dfmt, nfmt, fetch_offset, false, true).instr;
4173 static_cast<MTBUF_instruction*>(mtbuf)->can_reorder = true;
4174 }
4175
4176 emit_split_vector(ctx, fetch_dst, fetch_dst.size());
4177
4178 if (fetch_size == 1) {
4179 channels[channel_start] = fetch_dst;
4180 } else {
4181 for (unsigned i = 0; i < MIN2(fetch_size, num_channels - channel_start); i++)
4182 channels[channel_start + i] = emit_extract_vector(ctx, fetch_dst, i, v1);
4183 }
4184
4185 channel_start += fetch_size;
4186 }
4187
4188 if (!direct_fetch) {
4189 bool is_float = nfmt != V_008F0C_BUF_NUM_FORMAT_UINT &&
4190 nfmt != V_008F0C_BUF_NUM_FORMAT_SINT;
4191
4192 static const unsigned swizzle_normal[4] = {0, 1, 2, 3};
4193 static const unsigned swizzle_post_shuffle[4] = {2, 1, 0, 3};
4194 const unsigned *swizzle = post_shuffle ? swizzle_post_shuffle : swizzle_normal;
4195
4196 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4197 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4198 unsigned num_temp = 0;
4199 for (unsigned i = 0; i < dst.size(); i++) {
4200 unsigned idx = i + component;
4201 if (swizzle[idx] < num_channels && channels[swizzle[idx]].id()) {
4202 Temp channel = channels[swizzle[idx]];
4203 if (idx == 3 && alpha_adjust != RADV_ALPHA_ADJUST_NONE)
4204 channel = adjust_vertex_fetch_alpha(ctx, alpha_adjust, channel);
4205 vec->operands[i] = Operand(channel);
4206
4207 num_temp++;
4208 elems[i] = channel;
4209 } else if (is_float && idx == 3) {
4210 vec->operands[i] = Operand(0x3f800000u);
4211 } else if (!is_float && idx == 3) {
4212 vec->operands[i] = Operand(1u);
4213 } else {
4214 vec->operands[i] = Operand(0u);
4215 }
4216 }
4217 vec->definitions[0] = Definition(dst);
4218 ctx->block->instructions.emplace_back(std::move(vec));
4219 emit_split_vector(ctx, dst, dst.size());
4220
4221 if (num_temp == dst.size())
4222 ctx->allocated_vec.emplace(dst.id(), elems);
4223 }
4224 } else if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) {
4225 unsigned offset_idx = instr->intrinsic == nir_intrinsic_load_input ? 0 : 1;
4226 nir_instr *off_instr = instr->src[offset_idx].ssa->parent_instr;
4227 if (off_instr->type != nir_instr_type_load_const ||
4228 nir_instr_as_load_const(off_instr)->value[0].u32 != 0) {
4229 fprintf(stderr, "Unimplemented nir_intrinsic_load_input offset\n");
4230 nir_print_instr(off_instr, stderr);
4231 fprintf(stderr, "\n");
4232 }
4233
4234 Temp prim_mask = get_arg(ctx, ctx->args->ac.prim_mask);
4235 nir_const_value* offset = nir_src_as_const_value(instr->src[offset_idx]);
4236 if (offset) {
4237 assert(offset->u32 == 0);
4238 } else {
4239 /* the lower 15bit of the prim_mask contain the offset into LDS
4240 * while the upper bits contain the number of prims */
4241 Temp offset_src = get_ssa_temp(ctx, instr->src[offset_idx].ssa);
4242 assert(offset_src.regClass() == s1 && "TODO: divergent offsets...");
4243 Builder bld(ctx->program, ctx->block);
4244 Temp stride = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc), prim_mask, Operand(16u));
4245 stride = bld.sop1(aco_opcode::s_bcnt1_i32_b32, bld.def(s1), bld.def(s1, scc), stride);
4246 stride = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, Operand(48u));
4247 offset_src = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), stride, offset_src);
4248 prim_mask = bld.sop2(aco_opcode::s_add_i32, bld.def(s1, m0), bld.def(s1, scc), offset_src, prim_mask);
4249 }
4250
4251 unsigned idx = nir_intrinsic_base(instr);
4252 unsigned component = nir_intrinsic_component(instr);
4253 unsigned vertex_id = 2; /* P0 */
4254
4255 if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
4256 nir_const_value* src0 = nir_src_as_const_value(instr->src[0]);
4257 switch (src0->u32) {
4258 case 0:
4259 vertex_id = 2; /* P0 */
4260 break;
4261 case 1:
4262 vertex_id = 0; /* P10 */
4263 break;
4264 case 2:
4265 vertex_id = 1; /* P20 */
4266 break;
4267 default:
4268 unreachable("invalid vertex index");
4269 }
4270 }
4271
4272 if (dst.size() == 1) {
4273 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(dst), Operand(vertex_id), bld.m0(prim_mask), idx, component);
4274 } else {
4275 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
4276 for (unsigned i = 0; i < dst.size(); i++)
4277 vec->operands[i] = bld.vintrp(aco_opcode::v_interp_mov_f32, bld.def(v1), Operand(vertex_id), bld.m0(prim_mask), idx, component + i);
4278 vec->definitions[0] = Definition(dst);
4279 bld.insert(std::move(vec));
4280 }
4281
4282 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4283 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4284 Temp soffset = get_arg(ctx, ctx->args->oc_lds);
4285 std::pair<Temp, unsigned> offs = get_tcs_per_patch_output_vmem_offset(ctx, instr);
4286 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8u;
4287
4288 load_vmem_mubuf(ctx, dst, ring, offs.first, soffset, offs.second, elem_size_bytes, instr->dest.ssa.num_components);
4289 } else {
4290 unreachable("Shader stage not implemented");
4291 }
4292 }
4293
4294 std::pair<Temp, unsigned> get_gs_per_vertex_input_offset(isel_context *ctx, nir_intrinsic_instr *instr, unsigned base_stride = 1u)
4295 {
4296 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4297
4298 Builder bld(ctx->program, ctx->block);
4299 nir_src *vertex_src = nir_get_io_vertex_index_src(instr);
4300 Temp vertex_offset;
4301
4302 if (!nir_src_is_const(*vertex_src)) {
4303 /* better code could be created, but this case probably doesn't happen
4304 * much in practice */
4305 Temp indirect_vertex = as_vgpr(ctx, get_ssa_temp(ctx, vertex_src->ssa));
4306 for (unsigned i = 0; i < ctx->shader->info.gs.vertices_in; i++) {
4307 Temp elem;
4308
4309 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4310 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i / 2u * 2u]);
4311 if (i % 2u)
4312 elem = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(16u), elem);
4313 } else {
4314 elem = get_arg(ctx, ctx->args->gs_vtx_offset[i]);
4315 }
4316
4317 if (vertex_offset.id()) {
4318 Temp cond = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)),
4319 Operand(i), indirect_vertex);
4320 vertex_offset = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), vertex_offset, elem, cond);
4321 } else {
4322 vertex_offset = elem;
4323 }
4324 }
4325
4326 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4327 vertex_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu), vertex_offset);
4328 } else {
4329 unsigned vertex = nir_src_as_uint(*vertex_src);
4330 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs)
4331 vertex_offset = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
4332 get_arg(ctx, ctx->args->gs_vtx_offset[vertex / 2u * 2u]),
4333 Operand((vertex % 2u) * 16u), Operand(16u));
4334 else
4335 vertex_offset = get_arg(ctx, ctx->args->gs_vtx_offset[vertex]);
4336 }
4337
4338 std::pair<Temp, unsigned> offs = get_intrinsic_io_basic_offset(ctx, instr, base_stride);
4339 offs = offset_add(ctx, offs, std::make_pair(vertex_offset, 0u));
4340 return offset_mul(ctx, offs, 4u);
4341 }
4342
4343 void visit_load_gs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4344 {
4345 assert(ctx->shader->info.stage == MESA_SHADER_GEOMETRY);
4346
4347 Builder bld(ctx->program, ctx->block);
4348 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4349 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4350
4351 if (ctx->stage == geometry_gs) {
4352 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr, ctx->program->wave_size);
4353 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_ESGS_GS * 16u));
4354 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);
4355 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
4356 std::pair<Temp, unsigned> offs = get_gs_per_vertex_input_offset(ctx, instr);
4357 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4358 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4359 } else {
4360 unreachable("Unsupported GS stage.");
4361 }
4362 }
4363
4364 void visit_load_tcs_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4365 {
4366 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4367
4368 Builder bld(ctx->program, ctx->block);
4369 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4370
4371 if (load_input_from_temps(ctx, instr, dst))
4372 return;
4373
4374 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_input_lds_offset(ctx, instr);
4375 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4376 unsigned lds_align = calculate_lds_alignment(ctx, offs.second);
4377
4378 load_lds(ctx, elem_size_bytes, dst, offs.first, offs.second, lds_align);
4379 }
4380
4381 void visit_load_tes_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4382 {
4383 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4384
4385 Builder bld(ctx->program, ctx->block);
4386
4387 Temp ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_HS_TESS_OFFCHIP * 16u));
4388 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
4389 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4390
4391 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
4392 std::pair<Temp, unsigned> offs = get_tcs_per_vertex_output_vmem_offset(ctx, instr);
4393
4394 load_vmem_mubuf(ctx, dst, ring, offs.first, oc_lds, offs.second, elem_size_bytes, instr->dest.ssa.num_components, 0u, true, true);
4395 }
4396
4397 void visit_load_per_vertex_input(isel_context *ctx, nir_intrinsic_instr *instr)
4398 {
4399 switch (ctx->shader->info.stage) {
4400 case MESA_SHADER_GEOMETRY:
4401 visit_load_gs_per_vertex_input(ctx, instr);
4402 break;
4403 case MESA_SHADER_TESS_CTRL:
4404 visit_load_tcs_per_vertex_input(ctx, instr);
4405 break;
4406 case MESA_SHADER_TESS_EVAL:
4407 visit_load_tes_per_vertex_input(ctx, instr);
4408 break;
4409 default:
4410 unreachable("Unimplemented shader stage");
4411 }
4412 }
4413
4414 void visit_load_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4415 {
4416 visit_load_tcs_output(ctx, instr, true);
4417 }
4418
4419 void visit_store_per_vertex_output(isel_context *ctx, nir_intrinsic_instr *instr)
4420 {
4421 assert(ctx->stage == tess_control_hs || ctx->stage == vertex_tess_control_hs);
4422 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL);
4423
4424 visit_store_tcs_output(ctx, instr, true);
4425 }
4426
4427 void visit_load_tess_coord(isel_context *ctx, nir_intrinsic_instr *instr)
4428 {
4429 assert(ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
4430
4431 Builder bld(ctx->program, ctx->block);
4432 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4433
4434 Operand tes_u(get_arg(ctx, ctx->args->tes_u));
4435 Operand tes_v(get_arg(ctx, ctx->args->tes_v));
4436 Operand tes_w(0u);
4437
4438 if (ctx->shader->info.tess.primitive_mode == GL_TRIANGLES) {
4439 Temp tmp = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), tes_u, tes_v);
4440 tmp = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0x3f800000u /* 1.0f */), tmp);
4441 tes_w = Operand(tmp);
4442 }
4443
4444 Temp tess_coord = bld.pseudo(aco_opcode::p_create_vector, Definition(dst), tes_u, tes_v, tes_w);
4445 emit_split_vector(ctx, tess_coord, 3);
4446 }
4447
4448 Temp load_desc_ptr(isel_context *ctx, unsigned desc_set)
4449 {
4450 if (ctx->program->info->need_indirect_descriptor_sets) {
4451 Builder bld(ctx->program, ctx->block);
4452 Temp ptr64 = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->descriptor_sets[0]));
4453 Operand off = bld.copy(bld.def(s1), Operand(desc_set << 2));
4454 return bld.smem(aco_opcode::s_load_dword, bld.def(s1), ptr64, off);//, false, false, false);
4455 }
4456
4457 return get_arg(ctx, ctx->args->descriptor_sets[desc_set]);
4458 }
4459
4460
4461 void visit_load_resource(isel_context *ctx, nir_intrinsic_instr *instr)
4462 {
4463 Builder bld(ctx->program, ctx->block);
4464 Temp index = get_ssa_temp(ctx, instr->src[0].ssa);
4465 if (!ctx->divergent_vals[instr->dest.ssa.index])
4466 index = bld.as_uniform(index);
4467 unsigned desc_set = nir_intrinsic_desc_set(instr);
4468 unsigned binding = nir_intrinsic_binding(instr);
4469
4470 Temp desc_ptr;
4471 radv_pipeline_layout *pipeline_layout = ctx->options->layout;
4472 radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
4473 unsigned offset = layout->binding[binding].offset;
4474 unsigned stride;
4475 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
4476 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
4477 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start + layout->binding[binding].dynamic_offset_offset;
4478 desc_ptr = get_arg(ctx, ctx->args->ac.push_constants);
4479 offset = pipeline_layout->push_constant_size + 16 * idx;
4480 stride = 16;
4481 } else {
4482 desc_ptr = load_desc_ptr(ctx, desc_set);
4483 stride = layout->binding[binding].size;
4484 }
4485
4486 nir_const_value* nir_const_index = nir_src_as_const_value(instr->src[0]);
4487 unsigned const_index = nir_const_index ? nir_const_index->u32 : 0;
4488 if (stride != 1) {
4489 if (nir_const_index) {
4490 const_index = const_index * stride;
4491 } else if (index.type() == RegType::vgpr) {
4492 bool index24bit = layout->binding[binding].array_size <= 0x1000000;
4493 index = bld.v_mul_imm(bld.def(v1), index, stride, index24bit);
4494 } else {
4495 index = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), Operand(index));
4496 }
4497 }
4498 if (offset) {
4499 if (nir_const_index) {
4500 const_index = const_index + offset;
4501 } else if (index.type() == RegType::vgpr) {
4502 index = bld.vadd32(bld.def(v1), Operand(offset), index);
4503 } else {
4504 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), Operand(index));
4505 }
4506 }
4507
4508 if (nir_const_index && const_index == 0) {
4509 index = desc_ptr;
4510 } else if (index.type() == RegType::vgpr) {
4511 index = bld.vadd32(bld.def(v1),
4512 nir_const_index ? Operand(const_index) : Operand(index),
4513 Operand(desc_ptr));
4514 } else {
4515 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
4516 nir_const_index ? Operand(const_index) : Operand(index),
4517 Operand(desc_ptr));
4518 }
4519
4520 bld.copy(Definition(get_ssa_temp(ctx, &instr->dest.ssa)), index);
4521 }
4522
4523 void load_buffer(isel_context *ctx, unsigned num_components, unsigned component_size,
4524 Temp dst, Temp rsrc, Temp offset, int byte_align,
4525 bool glc=false, bool readonly=true)
4526 {
4527 Builder bld(ctx->program, ctx->block);
4528 bool dlc = glc && ctx->options->chip_class >= GFX10;
4529 unsigned num_bytes = num_components * component_size;
4530
4531 aco_opcode op;
4532 if (dst.type() == RegType::vgpr || ((ctx->options->chip_class < GFX8 || component_size < 4) && !readonly)) {
4533 Operand vaddr = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
4534 Operand soffset = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
4535 unsigned const_offset = 0;
4536
4537 /* for small bit sizes add buffer for unaligned loads */
4538 if (byte_align) {
4539 if (num_bytes > 2)
4540 num_bytes += byte_align == -1 ? 4 - component_size : byte_align;
4541 else
4542 byte_align = 0;
4543 }
4544
4545 Temp lower = Temp();
4546 if (num_bytes > 16) {
4547 assert(num_components == 3 || num_components == 4);
4548 op = aco_opcode::buffer_load_dwordx4;
4549 lower = bld.tmp(v4);
4550 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4551 mubuf->definitions[0] = Definition(lower);
4552 mubuf->operands[0] = Operand(rsrc);
4553 mubuf->operands[1] = vaddr;
4554 mubuf->operands[2] = soffset;
4555 mubuf->offen = (offset.type() == RegType::vgpr);
4556 mubuf->glc = glc;
4557 mubuf->dlc = dlc;
4558 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4559 mubuf->can_reorder = readonly;
4560 bld.insert(std::move(mubuf));
4561 emit_split_vector(ctx, lower, 2);
4562 num_bytes -= 16;
4563 const_offset = 16;
4564 } else if (num_bytes == 12 && ctx->options->chip_class == GFX6) {
4565 /* GFX6 doesn't support loading vec3, expand to vec4. */
4566 num_bytes = 16;
4567 }
4568
4569 switch (num_bytes) {
4570 case 1:
4571 op = aco_opcode::buffer_load_ubyte;
4572 break;
4573 case 2:
4574 op = aco_opcode::buffer_load_ushort;
4575 break;
4576 case 3:
4577 case 4:
4578 op = aco_opcode::buffer_load_dword;
4579 break;
4580 case 5:
4581 case 6:
4582 case 7:
4583 case 8:
4584 op = aco_opcode::buffer_load_dwordx2;
4585 break;
4586 case 10:
4587 case 12:
4588 assert(ctx->options->chip_class > GFX6);
4589 op = aco_opcode::buffer_load_dwordx3;
4590 break;
4591 case 16:
4592 op = aco_opcode::buffer_load_dwordx4;
4593 break;
4594 default:
4595 unreachable("Load SSBO not implemented for this size.");
4596 }
4597 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
4598 mubuf->operands[0] = Operand(rsrc);
4599 mubuf->operands[1] = vaddr;
4600 mubuf->operands[2] = soffset;
4601 mubuf->offen = (offset.type() == RegType::vgpr);
4602 mubuf->glc = glc;
4603 mubuf->dlc = dlc;
4604 mubuf->barrier = readonly ? barrier_none : barrier_buffer;
4605 mubuf->can_reorder = readonly;
4606 mubuf->offset = const_offset;
4607 aco_ptr<Instruction> instr = std::move(mubuf);
4608
4609 if (component_size < 4) {
4610 Temp vec = num_bytes <= 4 ? bld.tmp(v1) : num_bytes <= 8 ? bld.tmp(v2) : bld.tmp(v3);
4611 instr->definitions[0] = Definition(vec);
4612 bld.insert(std::move(instr));
4613
4614 if (byte_align == -1 || (byte_align && dst.type() == RegType::sgpr)) {
4615 Operand align = byte_align == -1 ? Operand(offset) : Operand((uint32_t)byte_align);
4616 Temp tmp[3] = {vec, vec, vec};
4617
4618 if (vec.size() == 3) {
4619 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = bld.tmp(v1);
4620 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), Definition(tmp[2]), vec);
4621 } else if (vec.size() == 2) {
4622 tmp[0] = bld.tmp(v1), tmp[1] = bld.tmp(v1), tmp[2] = tmp[1];
4623 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp[0]), Definition(tmp[1]), vec);
4624 }
4625 for (unsigned i = 0; i < dst.size(); i++)
4626 tmp[i] = bld.vop3(aco_opcode::v_alignbyte_b32, bld.def(v1), tmp[i + 1], tmp[i], align);
4627
4628 vec = tmp[0];
4629 if (dst.size() == 2)
4630 vec = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), tmp[0], tmp[1]);
4631
4632 byte_align = 0;
4633 }
4634
4635 if (dst.type() == RegType::vgpr && num_components == 1) {
4636 bld.pseudo(aco_opcode::p_extract_vector, Definition(dst), vec, Operand(byte_align / component_size));
4637 } else {
4638 trim_subdword_vector(ctx, vec, dst, 4 * vec.size() / component_size, ((1 << num_components) - 1) << byte_align / component_size);
4639 }
4640
4641 return;
4642
4643 } else if (dst.size() > 4) {
4644 assert(lower != Temp());
4645 Temp upper = bld.tmp(RegType::vgpr, dst.size() - lower.size());
4646 instr->definitions[0] = Definition(upper);
4647 bld.insert(std::move(instr));
4648 if (dst.size() == 8)
4649 emit_split_vector(ctx, upper, 2);
4650 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size() / 2, 1));
4651 instr->operands[0] = Operand(emit_extract_vector(ctx, lower, 0, v2));
4652 instr->operands[1] = Operand(emit_extract_vector(ctx, lower, 1, v2));
4653 instr->operands[2] = Operand(emit_extract_vector(ctx, upper, 0, v2));
4654 if (dst.size() == 8)
4655 instr->operands[3] = Operand(emit_extract_vector(ctx, upper, 1, v2));
4656 } else if (dst.size() == 3 && ctx->options->chip_class == GFX6) {
4657 Temp vec = bld.tmp(v4);
4658 instr->definitions[0] = Definition(vec);
4659 bld.insert(std::move(instr));
4660 emit_split_vector(ctx, vec, 4);
4661
4662 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
4663 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
4664 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
4665 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
4666 }
4667
4668 if (dst.type() == RegType::sgpr) {
4669 Temp vec = bld.tmp(RegType::vgpr, dst.size());
4670 instr->definitions[0] = Definition(vec);
4671 bld.insert(std::move(instr));
4672 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
4673 } else {
4674 instr->definitions[0] = Definition(dst);
4675 bld.insert(std::move(instr));
4676 emit_split_vector(ctx, dst, num_components);
4677 }
4678 } else {
4679 /* for small bit sizes add buffer for unaligned loads */
4680 if (byte_align)
4681 num_bytes += byte_align == -1 ? 4 - component_size : byte_align;
4682
4683 switch (num_bytes) {
4684 case 1:
4685 case 2:
4686 case 3:
4687 case 4:
4688 op = aco_opcode::s_buffer_load_dword;
4689 break;
4690 case 5:
4691 case 6:
4692 case 7:
4693 case 8:
4694 op = aco_opcode::s_buffer_load_dwordx2;
4695 break;
4696 case 10:
4697 case 12:
4698 case 16:
4699 op = aco_opcode::s_buffer_load_dwordx4;
4700 break;
4701 case 24:
4702 case 32:
4703 op = aco_opcode::s_buffer_load_dwordx8;
4704 break;
4705 default:
4706 unreachable("Load SSBO not implemented for this size.");
4707 }
4708 offset = bld.as_uniform(offset);
4709 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
4710 load->operands[0] = Operand(rsrc);
4711 load->operands[1] = Operand(offset);
4712 assert(load->operands[1].getTemp().type() == RegType::sgpr);
4713 load->definitions[0] = Definition(dst);
4714 load->glc = glc;
4715 load->dlc = dlc;
4716 load->barrier = readonly ? barrier_none : barrier_buffer;
4717 load->can_reorder = false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
4718 assert(ctx->options->chip_class >= GFX8 || !glc);
4719
4720 /* adjust misaligned small bit size loads */
4721 if (byte_align) {
4722 Temp vec = num_bytes <= 4 ? bld.tmp(s1) : num_bytes <= 8 ? bld.tmp(s2) : bld.tmp(s4);
4723 load->definitions[0] = Definition(vec);
4724 bld.insert(std::move(load));
4725 Operand byte_offset = byte_align > 0 ? Operand(uint32_t(byte_align)) : Operand(offset);
4726 byte_align_scalar(ctx, vec, byte_offset, dst);
4727
4728 /* trim vector */
4729 } else if (dst.size() == 3) {
4730 Temp vec = bld.tmp(s4);
4731 load->definitions[0] = Definition(vec);
4732 bld.insert(std::move(load));
4733 emit_split_vector(ctx, vec, 4);
4734
4735 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4736 emit_extract_vector(ctx, vec, 0, s1),
4737 emit_extract_vector(ctx, vec, 1, s1),
4738 emit_extract_vector(ctx, vec, 2, s1));
4739 } else if (dst.size() == 6) {
4740 Temp vec = bld.tmp(s8);
4741 load->definitions[0] = Definition(vec);
4742 bld.insert(std::move(load));
4743 emit_split_vector(ctx, vec, 4);
4744
4745 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4746 emit_extract_vector(ctx, vec, 0, s2),
4747 emit_extract_vector(ctx, vec, 1, s2),
4748 emit_extract_vector(ctx, vec, 2, s2));
4749 } else {
4750 bld.insert(std::move(load));
4751 }
4752 emit_split_vector(ctx, dst, num_components);
4753 }
4754 }
4755
4756 void visit_load_ubo(isel_context *ctx, nir_intrinsic_instr *instr)
4757 {
4758 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4759 Temp rsrc = get_ssa_temp(ctx, instr->src[0].ssa);
4760
4761 Builder bld(ctx->program, ctx->block);
4762
4763 nir_intrinsic_instr* idx_instr = nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
4764 unsigned desc_set = nir_intrinsic_desc_set(idx_instr);
4765 unsigned binding = nir_intrinsic_binding(idx_instr);
4766 radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
4767
4768 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
4769 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4770 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4771 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4772 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4773 if (ctx->options->chip_class >= GFX10) {
4774 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4775 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4776 S_008F0C_RESOURCE_LEVEL(1);
4777 } else {
4778 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4779 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4780 }
4781 Temp upper_dwords = bld.pseudo(aco_opcode::p_create_vector, bld.def(s3),
4782 Operand(S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi)),
4783 Operand(0xFFFFFFFFu),
4784 Operand(desc_type));
4785 rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4786 rsrc, upper_dwords);
4787 } else {
4788 rsrc = convert_pointer_to_64_bit(ctx, rsrc);
4789 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
4790 }
4791 unsigned size = instr->dest.ssa.bit_size / 8;
4792 int byte_align = 0;
4793 if (size < 4) {
4794 unsigned align_mul = nir_intrinsic_align_mul(instr);
4795 unsigned align_offset = nir_intrinsic_align_offset(instr);
4796 byte_align = align_mul % 4 == 0 ? align_offset : -1;
4797 }
4798 load_buffer(ctx, instr->num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), byte_align);
4799 }
4800
4801 void visit_load_push_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4802 {
4803 Builder bld(ctx->program, ctx->block);
4804 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4805 unsigned offset = nir_intrinsic_base(instr);
4806 unsigned count = instr->dest.ssa.num_components;
4807 nir_const_value *index_cv = nir_src_as_const_value(instr->src[0]);
4808
4809 if (index_cv && instr->dest.ssa.bit_size == 32) {
4810 unsigned start = (offset + index_cv->u32) / 4u;
4811 start -= ctx->args->ac.base_inline_push_consts;
4812 if (start + count <= ctx->args->ac.num_inline_push_consts) {
4813 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
4814 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
4815 for (unsigned i = 0; i < count; ++i) {
4816 elems[i] = get_arg(ctx, ctx->args->ac.inline_push_consts[start + i]);
4817 vec->operands[i] = Operand{elems[i]};
4818 }
4819 vec->definitions[0] = Definition(dst);
4820 ctx->block->instructions.emplace_back(std::move(vec));
4821 ctx->allocated_vec.emplace(dst.id(), elems);
4822 return;
4823 }
4824 }
4825
4826 Temp index = bld.as_uniform(get_ssa_temp(ctx, instr->src[0].ssa));
4827 if (offset != 0) // TODO check if index != 0 as well
4828 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset), index);
4829 Temp ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->ac.push_constants));
4830 Temp vec = dst;
4831 bool trim = false;
4832 bool aligned = true;
4833
4834 if (instr->dest.ssa.bit_size == 8) {
4835 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
4836 bool fits_in_dword = count == 1 || (index_cv && ((offset + index_cv->u32) % 4 + count) <= 4);
4837 if (!aligned)
4838 vec = fits_in_dword ? bld.tmp(s1) : bld.tmp(s2);
4839 } else if (instr->dest.ssa.bit_size == 16) {
4840 aligned = index_cv && (offset + index_cv->u32) % 4 == 0;
4841 if (!aligned)
4842 vec = count == 4 ? bld.tmp(s4) : count > 1 ? bld.tmp(s2) : bld.tmp(s1);
4843 }
4844
4845 aco_opcode op;
4846
4847 switch (vec.size()) {
4848 case 1:
4849 op = aco_opcode::s_load_dword;
4850 break;
4851 case 2:
4852 op = aco_opcode::s_load_dwordx2;
4853 break;
4854 case 3:
4855 vec = bld.tmp(s4);
4856 trim = true;
4857 case 4:
4858 op = aco_opcode::s_load_dwordx4;
4859 break;
4860 case 6:
4861 vec = bld.tmp(s8);
4862 trim = true;
4863 case 8:
4864 op = aco_opcode::s_load_dwordx8;
4865 break;
4866 default:
4867 unreachable("unimplemented or forbidden load_push_constant.");
4868 }
4869
4870 bld.smem(op, Definition(vec), ptr, index);
4871
4872 if (!aligned) {
4873 Operand byte_offset = index_cv ? Operand((offset + index_cv->u32) % 4) : Operand(index);
4874 byte_align_scalar(ctx, vec, byte_offset, dst);
4875 return;
4876 }
4877
4878 if (trim) {
4879 emit_split_vector(ctx, vec, 4);
4880 RegClass rc = dst.size() == 3 ? s1 : s2;
4881 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
4882 emit_extract_vector(ctx, vec, 0, rc),
4883 emit_extract_vector(ctx, vec, 1, rc),
4884 emit_extract_vector(ctx, vec, 2, rc));
4885
4886 }
4887 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
4888 }
4889
4890 void visit_load_constant(isel_context *ctx, nir_intrinsic_instr *instr)
4891 {
4892 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
4893
4894 Builder bld(ctx->program, ctx->block);
4895
4896 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
4897 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
4898 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
4899 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
4900 if (ctx->options->chip_class >= GFX10) {
4901 desc_type |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
4902 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
4903 S_008F0C_RESOURCE_LEVEL(1);
4904 } else {
4905 desc_type |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
4906 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
4907 }
4908
4909 unsigned base = nir_intrinsic_base(instr);
4910 unsigned range = nir_intrinsic_range(instr);
4911
4912 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
4913 if (base && offset.type() == RegType::sgpr)
4914 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), offset, Operand(base));
4915 else if (base && offset.type() == RegType::vgpr)
4916 offset = bld.vadd32(bld.def(v1), Operand(base), offset);
4917
4918 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
4919 bld.sop1(aco_opcode::p_constaddr, bld.def(s2), bld.def(s1, scc), Operand(ctx->constant_data_offset)),
4920 Operand(MIN2(base + range, ctx->shader->constant_data_size)),
4921 Operand(desc_type));
4922 unsigned size = instr->dest.ssa.bit_size / 8;
4923 // TODO: get alignment information for subdword constants
4924 unsigned byte_align = size < 4 ? -1 : 0;
4925 load_buffer(ctx, instr->num_components, size, dst, rsrc, offset, byte_align);
4926 }
4927
4928 void visit_discard_if(isel_context *ctx, nir_intrinsic_instr *instr)
4929 {
4930 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
4931 ctx->cf_info.exec_potentially_empty_discard = true;
4932
4933 ctx->program->needs_exact = true;
4934
4935 // TODO: optimize uniform conditions
4936 Builder bld(ctx->program, ctx->block);
4937 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
4938 assert(src.regClass() == bld.lm);
4939 src = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
4940 bld.pseudo(aco_opcode::p_discard_if, src);
4941 ctx->block->kind |= block_kind_uses_discard_if;
4942 return;
4943 }
4944
4945 void visit_discard(isel_context* ctx, nir_intrinsic_instr *instr)
4946 {
4947 Builder bld(ctx->program, ctx->block);
4948
4949 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
4950 ctx->cf_info.exec_potentially_empty_discard = true;
4951
4952 bool divergent = ctx->cf_info.parent_if.is_divergent ||
4953 ctx->cf_info.parent_loop.has_divergent_continue;
4954
4955 if (ctx->block->loop_nest_depth &&
4956 ((nir_instr_is_last(&instr->instr) && !divergent) || divergent)) {
4957 /* we handle discards the same way as jump instructions */
4958 append_logical_end(ctx->block);
4959
4960 /* in loops, discard behaves like break */
4961 Block *linear_target = ctx->cf_info.parent_loop.exit;
4962 ctx->block->kind |= block_kind_discard;
4963
4964 if (!divergent) {
4965 /* uniform discard - loop ends here */
4966 assert(nir_instr_is_last(&instr->instr));
4967 ctx->block->kind |= block_kind_uniform;
4968 ctx->cf_info.has_branch = true;
4969 bld.branch(aco_opcode::p_branch);
4970 add_linear_edge(ctx->block->index, linear_target);
4971 return;
4972 }
4973
4974 /* we add a break right behind the discard() instructions */
4975 ctx->block->kind |= block_kind_break;
4976 unsigned idx = ctx->block->index;
4977
4978 ctx->cf_info.parent_loop.has_divergent_branch = true;
4979 ctx->cf_info.nir_to_aco[instr->instr.block->index] = idx;
4980
4981 /* remove critical edges from linear CFG */
4982 bld.branch(aco_opcode::p_branch);
4983 Block* break_block = ctx->program->create_and_insert_block();
4984 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
4985 break_block->kind |= block_kind_uniform;
4986 add_linear_edge(idx, break_block);
4987 add_linear_edge(break_block->index, linear_target);
4988 bld.reset(break_block);
4989 bld.branch(aco_opcode::p_branch);
4990
4991 Block* continue_block = ctx->program->create_and_insert_block();
4992 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
4993 add_linear_edge(idx, continue_block);
4994 append_logical_start(continue_block);
4995 ctx->block = continue_block;
4996
4997 return;
4998 }
4999
5000 /* it can currently happen that NIR doesn't remove the unreachable code */
5001 if (!nir_instr_is_last(&instr->instr)) {
5002 ctx->program->needs_exact = true;
5003 /* save exec somewhere temporarily so that it doesn't get
5004 * overwritten before the discard from outer exec masks */
5005 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), Operand(0xFFFFFFFF), Operand(exec, bld.lm));
5006 bld.pseudo(aco_opcode::p_discard_if, cond);
5007 ctx->block->kind |= block_kind_uses_discard_if;
5008 return;
5009 }
5010
5011 /* This condition is incorrect for uniformly branched discards in a loop
5012 * predicated by a divergent condition, but the above code catches that case
5013 * and the discard would end up turning into a discard_if.
5014 * For example:
5015 * if (divergent) {
5016 * while (...) {
5017 * if (uniform) {
5018 * discard;
5019 * }
5020 * }
5021 * }
5022 */
5023 if (!ctx->cf_info.parent_if.is_divergent) {
5024 /* program just ends here */
5025 ctx->block->kind |= block_kind_uniform;
5026 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
5027 0 /* enabled mask */, 9 /* dest */,
5028 false /* compressed */, true/* done */, true /* valid mask */);
5029 bld.sopp(aco_opcode::s_endpgm);
5030 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5031 } else {
5032 ctx->block->kind |= block_kind_discard;
5033 /* branch and linear edge is added by visit_if() */
5034 }
5035 }
5036
5037 enum aco_descriptor_type {
5038 ACO_DESC_IMAGE,
5039 ACO_DESC_FMASK,
5040 ACO_DESC_SAMPLER,
5041 ACO_DESC_BUFFER,
5042 ACO_DESC_PLANE_0,
5043 ACO_DESC_PLANE_1,
5044 ACO_DESC_PLANE_2,
5045 };
5046
5047 static bool
5048 should_declare_array(isel_context *ctx, enum glsl_sampler_dim sampler_dim, bool is_array) {
5049 if (sampler_dim == GLSL_SAMPLER_DIM_BUF)
5050 return false;
5051 ac_image_dim dim = ac_get_sampler_dim(ctx->options->chip_class, sampler_dim, is_array);
5052 return dim == ac_image_cube ||
5053 dim == ac_image_1darray ||
5054 dim == ac_image_2darray ||
5055 dim == ac_image_2darraymsaa;
5056 }
5057
5058 Temp get_sampler_desc(isel_context *ctx, nir_deref_instr *deref_instr,
5059 enum aco_descriptor_type desc_type,
5060 const nir_tex_instr *tex_instr, bool image, bool write)
5061 {
5062 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5063 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5064 if (it != ctx->tex_desc.end())
5065 return it->second;
5066 */
5067 Temp index = Temp();
5068 bool index_set = false;
5069 unsigned constant_index = 0;
5070 unsigned descriptor_set;
5071 unsigned base_index;
5072 Builder bld(ctx->program, ctx->block);
5073
5074 if (!deref_instr) {
5075 assert(tex_instr && !image);
5076 descriptor_set = 0;
5077 base_index = tex_instr->sampler_index;
5078 } else {
5079 while(deref_instr->deref_type != nir_deref_type_var) {
5080 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
5081 if (!array_size)
5082 array_size = 1;
5083
5084 assert(deref_instr->deref_type == nir_deref_type_array);
5085 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
5086 if (const_value) {
5087 constant_index += array_size * const_value->u32;
5088 } else {
5089 Temp indirect = get_ssa_temp(ctx, deref_instr->arr.index.ssa);
5090 if (indirect.type() == RegType::vgpr)
5091 indirect = bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), indirect);
5092
5093 if (array_size != 1)
5094 indirect = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(array_size), indirect);
5095
5096 if (!index_set) {
5097 index = indirect;
5098 index_set = true;
5099 } else {
5100 index = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), index, indirect);
5101 }
5102 }
5103
5104 deref_instr = nir_src_as_deref(deref_instr->parent);
5105 }
5106 descriptor_set = deref_instr->var->data.descriptor_set;
5107 base_index = deref_instr->var->data.binding;
5108 }
5109
5110 Temp list = load_desc_ptr(ctx, descriptor_set);
5111 list = convert_pointer_to_64_bit(ctx, list);
5112
5113 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
5114 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
5115 unsigned offset = binding->offset;
5116 unsigned stride = binding->size;
5117 aco_opcode opcode;
5118 RegClass type;
5119
5120 assert(base_index < layout->binding_count);
5121
5122 switch (desc_type) {
5123 case ACO_DESC_IMAGE:
5124 type = s8;
5125 opcode = aco_opcode::s_load_dwordx8;
5126 break;
5127 case ACO_DESC_FMASK:
5128 type = s8;
5129 opcode = aco_opcode::s_load_dwordx8;
5130 offset += 32;
5131 break;
5132 case ACO_DESC_SAMPLER:
5133 type = s4;
5134 opcode = aco_opcode::s_load_dwordx4;
5135 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
5136 offset += radv_combined_image_descriptor_sampler_offset(binding);
5137 break;
5138 case ACO_DESC_BUFFER:
5139 type = s4;
5140 opcode = aco_opcode::s_load_dwordx4;
5141 break;
5142 case ACO_DESC_PLANE_0:
5143 case ACO_DESC_PLANE_1:
5144 type = s8;
5145 opcode = aco_opcode::s_load_dwordx8;
5146 offset += 32 * (desc_type - ACO_DESC_PLANE_0);
5147 break;
5148 case ACO_DESC_PLANE_2:
5149 type = s4;
5150 opcode = aco_opcode::s_load_dwordx4;
5151 offset += 64;
5152 break;
5153 default:
5154 unreachable("invalid desc_type\n");
5155 }
5156
5157 offset += constant_index * stride;
5158
5159 if (desc_type == ACO_DESC_SAMPLER && binding->immutable_samplers_offset &&
5160 (!index_set || binding->immutable_samplers_equal)) {
5161 if (binding->immutable_samplers_equal)
5162 constant_index = 0;
5163
5164 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
5165 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
5166 Operand(samplers[constant_index * 4 + 0]),
5167 Operand(samplers[constant_index * 4 + 1]),
5168 Operand(samplers[constant_index * 4 + 2]),
5169 Operand(samplers[constant_index * 4 + 3]));
5170 }
5171
5172 Operand off;
5173 if (!index_set) {
5174 off = bld.copy(bld.def(s1), Operand(offset));
5175 } else {
5176 off = Operand((Temp)bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc), Operand(offset),
5177 bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(stride), index)));
5178 }
5179
5180 Temp res = bld.smem(opcode, bld.def(type), list, off);
5181
5182 if (desc_type == ACO_DESC_PLANE_2) {
5183 Temp components[8];
5184 for (unsigned i = 0; i < 8; i++)
5185 components[i] = bld.tmp(s1);
5186 bld.pseudo(aco_opcode::p_split_vector,
5187 Definition(components[0]),
5188 Definition(components[1]),
5189 Definition(components[2]),
5190 Definition(components[3]),
5191 res);
5192
5193 Temp desc2 = get_sampler_desc(ctx, deref_instr, ACO_DESC_PLANE_1, tex_instr, image, write);
5194 bld.pseudo(aco_opcode::p_split_vector,
5195 bld.def(s1), bld.def(s1), bld.def(s1), bld.def(s1),
5196 Definition(components[4]),
5197 Definition(components[5]),
5198 Definition(components[6]),
5199 Definition(components[7]),
5200 desc2);
5201
5202 res = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
5203 components[0], components[1], components[2], components[3],
5204 components[4], components[5], components[6], components[7]);
5205 }
5206
5207 return res;
5208 }
5209
5210 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
5211 {
5212 switch (dim) {
5213 case GLSL_SAMPLER_DIM_BUF:
5214 return 1;
5215 case GLSL_SAMPLER_DIM_1D:
5216 return array ? 2 : 1;
5217 case GLSL_SAMPLER_DIM_2D:
5218 return array ? 3 : 2;
5219 case GLSL_SAMPLER_DIM_MS:
5220 return array ? 4 : 3;
5221 case GLSL_SAMPLER_DIM_3D:
5222 case GLSL_SAMPLER_DIM_CUBE:
5223 return 3;
5224 case GLSL_SAMPLER_DIM_RECT:
5225 case GLSL_SAMPLER_DIM_SUBPASS:
5226 return 2;
5227 case GLSL_SAMPLER_DIM_SUBPASS_MS:
5228 return 3;
5229 default:
5230 break;
5231 }
5232 return 0;
5233 }
5234
5235
5236 /* Adjust the sample index according to FMASK.
5237 *
5238 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5239 * which is the identity mapping. Each nibble says which physical sample
5240 * should be fetched to get that sample.
5241 *
5242 * For example, 0x11111100 means there are only 2 samples stored and
5243 * the second sample covers 3/4 of the pixel. When reading samples 0
5244 * and 1, return physical sample 0 (determined by the first two 0s
5245 * in FMASK), otherwise return physical sample 1.
5246 *
5247 * The sample index should be adjusted as follows:
5248 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5249 */
5250 static Temp adjust_sample_index_using_fmask(isel_context *ctx, bool da, std::vector<Temp>& coords, Operand sample_index, Temp fmask_desc_ptr)
5251 {
5252 Builder bld(ctx->program, ctx->block);
5253 Temp fmask = bld.tmp(v1);
5254 unsigned dim = ctx->options->chip_class >= GFX10
5255 ? ac_get_sampler_dim(ctx->options->chip_class, GLSL_SAMPLER_DIM_2D, da)
5256 : 0;
5257
5258 Temp coord = da ? bld.pseudo(aco_opcode::p_create_vector, bld.def(v3), coords[0], coords[1], coords[2]) :
5259 bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), coords[0], coords[1]);
5260 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(aco_opcode::image_load, Format::MIMG, 3, 1)};
5261 load->operands[0] = Operand(fmask_desc_ptr);
5262 load->operands[1] = Operand(s4); /* no sampler */
5263 load->operands[2] = Operand(coord);
5264 load->definitions[0] = Definition(fmask);
5265 load->glc = false;
5266 load->dlc = false;
5267 load->dmask = 0x1;
5268 load->unrm = true;
5269 load->da = da;
5270 load->dim = dim;
5271 load->can_reorder = true; /* fmask images shouldn't be modified */
5272 ctx->block->instructions.emplace_back(std::move(load));
5273
5274 Operand sample_index4;
5275 if (sample_index.isConstant() && sample_index.constantValue() < 16) {
5276 sample_index4 = Operand(sample_index.constantValue() << 2);
5277 } else if (sample_index.regClass() == s1) {
5278 sample_index4 = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), sample_index, Operand(2u));
5279 } else {
5280 assert(sample_index.regClass() == v1);
5281 sample_index4 = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), sample_index);
5282 }
5283
5284 Temp final_sample;
5285 if (sample_index4.isConstant() && sample_index4.constantValue() == 0)
5286 final_sample = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(15u), fmask);
5287 else if (sample_index4.isConstant() && sample_index4.constantValue() == 28)
5288 final_sample = bld.vop2(aco_opcode::v_lshrrev_b32, bld.def(v1), Operand(28u), fmask);
5289 else
5290 final_sample = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), fmask, sample_index4, Operand(4u));
5291
5292 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5293 * resource descriptor is 0 (invalid),
5294 */
5295 Temp compare = bld.tmp(bld.lm);
5296 bld.vopc_e64(aco_opcode::v_cmp_lg_u32, Definition(compare),
5297 Operand(0u), emit_extract_vector(ctx, fmask_desc_ptr, 1, s1)).def(0).setHint(vcc);
5298
5299 Temp sample_index_v = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), sample_index);
5300
5301 /* Replace the MSAA sample index. */
5302 return bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), sample_index_v, final_sample, compare);
5303 }
5304
5305 static Temp get_image_coords(isel_context *ctx, const nir_intrinsic_instr *instr, const struct glsl_type *type)
5306 {
5307
5308 Temp src0 = get_ssa_temp(ctx, instr->src[1].ssa);
5309 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5310 bool is_array = glsl_sampler_type_is_array(type);
5311 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5312 assert(!add_frag_pos && "Input attachments should be lowered.");
5313 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
5314 bool gfx9_1d = ctx->options->chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
5315 int count = image_type_to_components_count(dim, is_array);
5316 std::vector<Temp> coords(count);
5317 Builder bld(ctx->program, ctx->block);
5318
5319 if (is_ms) {
5320 count--;
5321 Temp src2 = get_ssa_temp(ctx, instr->src[2].ssa);
5322 /* get sample index */
5323 if (instr->intrinsic == nir_intrinsic_image_deref_load) {
5324 nir_const_value *sample_cv = nir_src_as_const_value(instr->src[2]);
5325 Operand sample_index = sample_cv ? Operand(sample_cv->u32) : Operand(emit_extract_vector(ctx, src2, 0, v1));
5326 std::vector<Temp> fmask_load_address;
5327 for (unsigned i = 0; i < (is_array ? 3 : 2); i++)
5328 fmask_load_address.emplace_back(emit_extract_vector(ctx, src0, i, v1));
5329
5330 Temp fmask_desc_ptr = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_FMASK, nullptr, false, false);
5331 coords[count] = adjust_sample_index_using_fmask(ctx, is_array, fmask_load_address, sample_index, fmask_desc_ptr);
5332 } else {
5333 coords[count] = emit_extract_vector(ctx, src2, 0, v1);
5334 }
5335 }
5336
5337 if (gfx9_1d) {
5338 coords[0] = emit_extract_vector(ctx, src0, 0, v1);
5339 coords.resize(coords.size() + 1);
5340 coords[1] = bld.copy(bld.def(v1), Operand(0u));
5341 if (is_array)
5342 coords[2] = emit_extract_vector(ctx, src0, 1, v1);
5343 } else {
5344 for (int i = 0; i < count; i++)
5345 coords[i] = emit_extract_vector(ctx, src0, i, v1);
5346 }
5347
5348 if (instr->intrinsic == nir_intrinsic_image_deref_load ||
5349 instr->intrinsic == nir_intrinsic_image_deref_store) {
5350 int lod_index = instr->intrinsic == nir_intrinsic_image_deref_load ? 3 : 4;
5351 bool level_zero = nir_src_is_const(instr->src[lod_index]) && nir_src_as_uint(instr->src[lod_index]) == 0;
5352
5353 if (!level_zero)
5354 coords.emplace_back(get_ssa_temp(ctx, instr->src[lod_index].ssa));
5355 }
5356
5357 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, coords.size(), 1)};
5358 for (unsigned i = 0; i < coords.size(); i++)
5359 vec->operands[i] = Operand(coords[i]);
5360 Temp res = {ctx->program->allocateId(), RegClass(RegType::vgpr, coords.size())};
5361 vec->definitions[0] = Definition(res);
5362 ctx->block->instructions.emplace_back(std::move(vec));
5363 return res;
5364 }
5365
5366
5367 void visit_image_load(isel_context *ctx, nir_intrinsic_instr *instr)
5368 {
5369 Builder bld(ctx->program, ctx->block);
5370 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5371 const struct glsl_type *type = glsl_without_array(var->type);
5372 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5373 bool is_array = glsl_sampler_type_is_array(type);
5374 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5375
5376 if (dim == GLSL_SAMPLER_DIM_BUF) {
5377 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
5378 unsigned num_channels = util_last_bit(mask);
5379 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5380 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5381
5382 aco_opcode opcode;
5383 switch (num_channels) {
5384 case 1:
5385 opcode = aco_opcode::buffer_load_format_x;
5386 break;
5387 case 2:
5388 opcode = aco_opcode::buffer_load_format_xy;
5389 break;
5390 case 3:
5391 opcode = aco_opcode::buffer_load_format_xyz;
5392 break;
5393 case 4:
5394 opcode = aco_opcode::buffer_load_format_xyzw;
5395 break;
5396 default:
5397 unreachable(">4 channel buffer image load");
5398 }
5399 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 3, 1)};
5400 load->operands[0] = Operand(rsrc);
5401 load->operands[1] = Operand(vindex);
5402 load->operands[2] = Operand((uint32_t) 0);
5403 Temp tmp;
5404 if (num_channels == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5405 tmp = dst;
5406 else
5407 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_channels)};
5408 load->definitions[0] = Definition(tmp);
5409 load->idxen = true;
5410 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT);
5411 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5412 load->barrier = barrier_image;
5413 ctx->block->instructions.emplace_back(std::move(load));
5414
5415 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, (1 << num_channels) - 1);
5416 return;
5417 }
5418
5419 Temp coords = get_image_coords(ctx, instr, type);
5420 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5421
5422 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
5423 unsigned num_components = util_bitcount(dmask);
5424 Temp tmp;
5425 if (num_components == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
5426 tmp = dst;
5427 else
5428 tmp = {ctx->program->allocateId(), RegClass(RegType::vgpr, num_components)};
5429
5430 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
5431 aco_opcode opcode = level_zero ? aco_opcode::image_load : aco_opcode::image_load_mip;
5432
5433 aco_ptr<MIMG_instruction> load{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1)};
5434 load->operands[0] = Operand(resource);
5435 load->operands[1] = Operand(s4); /* no sampler */
5436 load->operands[2] = Operand(coords);
5437 load->definitions[0] = Definition(tmp);
5438 load->glc = var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT) ? 1 : 0;
5439 load->dlc = load->glc && ctx->options->chip_class >= GFX10;
5440 load->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5441 load->dmask = dmask;
5442 load->unrm = true;
5443 load->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5444 load->barrier = barrier_image;
5445 ctx->block->instructions.emplace_back(std::move(load));
5446
5447 expand_vector(ctx, tmp, dst, instr->dest.ssa.num_components, dmask);
5448 return;
5449 }
5450
5451 void visit_image_store(isel_context *ctx, nir_intrinsic_instr *instr)
5452 {
5453 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5454 const struct glsl_type *type = glsl_without_array(var->type);
5455 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5456 bool is_array = glsl_sampler_type_is_array(type);
5457 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5458
5459 bool glc = ctx->options->chip_class == GFX6 || var->data.access & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE) ? 1 : 0;
5460
5461 if (dim == GLSL_SAMPLER_DIM_BUF) {
5462 Temp rsrc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5463 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5464 aco_opcode opcode;
5465 switch (data.size()) {
5466 case 1:
5467 opcode = aco_opcode::buffer_store_format_x;
5468 break;
5469 case 2:
5470 opcode = aco_opcode::buffer_store_format_xy;
5471 break;
5472 case 3:
5473 opcode = aco_opcode::buffer_store_format_xyz;
5474 break;
5475 case 4:
5476 opcode = aco_opcode::buffer_store_format_xyzw;
5477 break;
5478 default:
5479 unreachable(">4 channel buffer image store");
5480 }
5481 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
5482 store->operands[0] = Operand(rsrc);
5483 store->operands[1] = Operand(vindex);
5484 store->operands[2] = Operand((uint32_t) 0);
5485 store->operands[3] = Operand(data);
5486 store->idxen = true;
5487 store->glc = glc;
5488 store->dlc = false;
5489 store->disable_wqm = true;
5490 store->barrier = barrier_image;
5491 ctx->program->needs_exact = true;
5492 ctx->block->instructions.emplace_back(std::move(store));
5493 return;
5494 }
5495
5496 assert(data.type() == RegType::vgpr);
5497 Temp coords = get_image_coords(ctx, instr, type);
5498 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5499
5500 bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
5501 aco_opcode opcode = level_zero ? aco_opcode::image_store : aco_opcode::image_store_mip;
5502
5503 aco_ptr<MIMG_instruction> store{create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 0)};
5504 store->operands[0] = Operand(resource);
5505 store->operands[1] = Operand(data);
5506 store->operands[2] = Operand(coords);
5507 store->glc = glc;
5508 store->dlc = false;
5509 store->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5510 store->dmask = (1 << data.size()) - 1;
5511 store->unrm = true;
5512 store->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5513 store->disable_wqm = true;
5514 store->barrier = barrier_image;
5515 ctx->program->needs_exact = true;
5516 ctx->block->instructions.emplace_back(std::move(store));
5517 return;
5518 }
5519
5520 void visit_image_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
5521 {
5522 /* return the previous value if dest is ever used */
5523 bool return_previous = false;
5524 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5525 return_previous = true;
5526 break;
5527 }
5528 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5529 return_previous = true;
5530 break;
5531 }
5532
5533 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5534 const struct glsl_type *type = glsl_without_array(var->type);
5535 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5536 bool is_array = glsl_sampler_type_is_array(type);
5537 Builder bld(ctx->program, ctx->block);
5538
5539 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[3].ssa));
5540 assert(data.size() == 1 && "64bit ssbo atomics not yet implemented.");
5541
5542 if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
5543 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), get_ssa_temp(ctx, instr->src[4].ssa), data);
5544
5545 aco_opcode buf_op, image_op;
5546 switch (instr->intrinsic) {
5547 case nir_intrinsic_image_deref_atomic_add:
5548 buf_op = aco_opcode::buffer_atomic_add;
5549 image_op = aco_opcode::image_atomic_add;
5550 break;
5551 case nir_intrinsic_image_deref_atomic_umin:
5552 buf_op = aco_opcode::buffer_atomic_umin;
5553 image_op = aco_opcode::image_atomic_umin;
5554 break;
5555 case nir_intrinsic_image_deref_atomic_imin:
5556 buf_op = aco_opcode::buffer_atomic_smin;
5557 image_op = aco_opcode::image_atomic_smin;
5558 break;
5559 case nir_intrinsic_image_deref_atomic_umax:
5560 buf_op = aco_opcode::buffer_atomic_umax;
5561 image_op = aco_opcode::image_atomic_umax;
5562 break;
5563 case nir_intrinsic_image_deref_atomic_imax:
5564 buf_op = aco_opcode::buffer_atomic_smax;
5565 image_op = aco_opcode::image_atomic_smax;
5566 break;
5567 case nir_intrinsic_image_deref_atomic_and:
5568 buf_op = aco_opcode::buffer_atomic_and;
5569 image_op = aco_opcode::image_atomic_and;
5570 break;
5571 case nir_intrinsic_image_deref_atomic_or:
5572 buf_op = aco_opcode::buffer_atomic_or;
5573 image_op = aco_opcode::image_atomic_or;
5574 break;
5575 case nir_intrinsic_image_deref_atomic_xor:
5576 buf_op = aco_opcode::buffer_atomic_xor;
5577 image_op = aco_opcode::image_atomic_xor;
5578 break;
5579 case nir_intrinsic_image_deref_atomic_exchange:
5580 buf_op = aco_opcode::buffer_atomic_swap;
5581 image_op = aco_opcode::image_atomic_swap;
5582 break;
5583 case nir_intrinsic_image_deref_atomic_comp_swap:
5584 buf_op = aco_opcode::buffer_atomic_cmpswap;
5585 image_op = aco_opcode::image_atomic_cmpswap;
5586 break;
5587 default:
5588 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5589 }
5590
5591 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5592
5593 if (dim == GLSL_SAMPLER_DIM_BUF) {
5594 Temp vindex = emit_extract_vector(ctx, get_ssa_temp(ctx, instr->src[1].ssa), 0, v1);
5595 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, nullptr, true, true);
5596 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
5597 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(buf_op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5598 mubuf->operands[0] = Operand(resource);
5599 mubuf->operands[1] = Operand(vindex);
5600 mubuf->operands[2] = Operand((uint32_t)0);
5601 mubuf->operands[3] = Operand(data);
5602 if (return_previous)
5603 mubuf->definitions[0] = Definition(dst);
5604 mubuf->offset = 0;
5605 mubuf->idxen = true;
5606 mubuf->glc = return_previous;
5607 mubuf->dlc = false; /* Not needed for atomics */
5608 mubuf->disable_wqm = true;
5609 mubuf->barrier = barrier_image;
5610 ctx->program->needs_exact = true;
5611 ctx->block->instructions.emplace_back(std::move(mubuf));
5612 return;
5613 }
5614
5615 Temp coords = get_image_coords(ctx, instr, type);
5616 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, nullptr, true, true);
5617 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(image_op, Format::MIMG, 3, return_previous ? 1 : 0)};
5618 mimg->operands[0] = Operand(resource);
5619 mimg->operands[1] = Operand(data);
5620 mimg->operands[2] = Operand(coords);
5621 if (return_previous)
5622 mimg->definitions[0] = Definition(dst);
5623 mimg->glc = return_previous;
5624 mimg->dlc = false; /* Not needed for atomics */
5625 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5626 mimg->dmask = (1 << data.size()) - 1;
5627 mimg->unrm = true;
5628 mimg->da = should_declare_array(ctx, dim, glsl_sampler_type_is_array(type));
5629 mimg->disable_wqm = true;
5630 mimg->barrier = barrier_image;
5631 ctx->program->needs_exact = true;
5632 ctx->block->instructions.emplace_back(std::move(mimg));
5633 return;
5634 }
5635
5636 void get_buffer_size(isel_context *ctx, Temp desc, Temp dst, bool in_elements)
5637 {
5638 if (in_elements && ctx->options->chip_class == GFX8) {
5639 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5640 Builder bld(ctx->program, ctx->block);
5641
5642 Temp size = emit_extract_vector(ctx, desc, 2, s1);
5643
5644 Temp size_div3 = bld.vop3(aco_opcode::v_mul_hi_u32, bld.def(v1), bld.copy(bld.def(v1), Operand(0xaaaaaaabu)), size);
5645 size_div3 = bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.as_uniform(size_div3), Operand(1u));
5646
5647 Temp stride = emit_extract_vector(ctx, desc, 1, s1);
5648 stride = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), stride, Operand((5u << 16) | 16u));
5649
5650 Temp is12 = bld.sopc(aco_opcode::s_cmp_eq_i32, bld.def(s1, scc), stride, Operand(12u));
5651 size = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1), size_div3, size, bld.scc(is12));
5652
5653 Temp shr_dst = dst.type() == RegType::vgpr ? bld.tmp(s1) : dst;
5654 bld.sop2(aco_opcode::s_lshr_b32, Definition(shr_dst), bld.def(s1, scc),
5655 size, bld.sop1(aco_opcode::s_ff1_i32_b32, bld.def(s1), stride));
5656 if (dst.type() == RegType::vgpr)
5657 bld.copy(Definition(dst), shr_dst);
5658
5659 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5660 } else {
5661 emit_extract_vector(ctx, desc, 2, dst);
5662 }
5663 }
5664
5665 void visit_image_size(isel_context *ctx, nir_intrinsic_instr *instr)
5666 {
5667 const nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
5668 const struct glsl_type *type = glsl_without_array(var->type);
5669 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
5670 bool is_array = glsl_sampler_type_is_array(type);
5671 Builder bld(ctx->program, ctx->block);
5672
5673 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
5674 Temp desc = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_BUFFER, NULL, true, false);
5675 return get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), true);
5676 }
5677
5678 /* LOD */
5679 Temp lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
5680
5681 /* Resource */
5682 Temp resource = get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), ACO_DESC_IMAGE, NULL, true, false);
5683
5684 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5685
5686 aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1)};
5687 mimg->operands[0] = Operand(resource);
5688 mimg->operands[1] = Operand(s4); /* no sampler */
5689 mimg->operands[2] = Operand(lod);
5690 uint8_t& dmask = mimg->dmask;
5691 mimg->dim = ac_get_image_dim(ctx->options->chip_class, dim, is_array);
5692 mimg->dmask = (1 << instr->dest.ssa.num_components) - 1;
5693 mimg->da = glsl_sampler_type_is_array(type);
5694 mimg->can_reorder = true;
5695 Definition& def = mimg->definitions[0];
5696 ctx->block->instructions.emplace_back(std::move(mimg));
5697
5698 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
5699 glsl_sampler_type_is_array(type)) {
5700
5701 assert(instr->dest.ssa.num_components == 3);
5702 Temp tmp = {ctx->program->allocateId(), v3};
5703 def = Definition(tmp);
5704 emit_split_vector(ctx, tmp, 3);
5705
5706 /* divide 3rd value by 6 by multiplying with magic number */
5707 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
5708 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp, 2, v1), c);
5709
5710 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
5711 emit_extract_vector(ctx, tmp, 0, v1),
5712 emit_extract_vector(ctx, tmp, 1, v1),
5713 by_6);
5714
5715 } else if (ctx->options->chip_class == GFX9 &&
5716 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
5717 glsl_sampler_type_is_array(type)) {
5718 assert(instr->dest.ssa.num_components == 2);
5719 def = Definition(dst);
5720 dmask = 0x5;
5721 } else {
5722 def = Definition(dst);
5723 }
5724
5725 emit_split_vector(ctx, dst, instr->dest.ssa.num_components);
5726 }
5727
5728 void visit_load_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5729 {
5730 Builder bld(ctx->program, ctx->block);
5731 unsigned num_components = instr->num_components;
5732
5733 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5734 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5735 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5736
5737 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
5738 unsigned size = instr->dest.ssa.bit_size / 8;
5739 int byte_align = 0;
5740 if (size < 4) {
5741 unsigned align_mul = nir_intrinsic_align_mul(instr);
5742 unsigned align_offset = nir_intrinsic_align_offset(instr);
5743 byte_align = align_mul % 4 == 0 ? align_offset : -1;
5744 }
5745 load_buffer(ctx, num_components, size, dst, rsrc, get_ssa_temp(ctx, instr->src[1].ssa), byte_align, glc, false);
5746 }
5747
5748 void visit_store_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5749 {
5750 Builder bld(ctx->program, ctx->block);
5751 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
5752 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
5753 unsigned writemask = nir_intrinsic_write_mask(instr);
5754 Temp offset = get_ssa_temp(ctx, instr->src[2].ssa);
5755
5756 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
5757 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5758
5759 bool smem = !ctx->divergent_vals[instr->src[2].ssa->index] &&
5760 ctx->options->chip_class >= GFX8 &&
5761 elem_size_bytes >= 4;
5762 if (smem)
5763 offset = bld.as_uniform(offset);
5764 bool smem_nonfs = smem && ctx->stage != fragment_fs;
5765
5766 while (writemask) {
5767 int start, count;
5768 u_bit_scan_consecutive_range(&writemask, &start, &count);
5769 if (count == 3 && (smem || ctx->options->chip_class == GFX6)) {
5770 /* GFX6 doesn't support storing vec3, split it. */
5771 writemask |= 1u << (start + 2);
5772 count = 2;
5773 }
5774 int num_bytes = count * elem_size_bytes;
5775
5776 /* dword or larger stores have to be dword-aligned */
5777 if (elem_size_bytes < 4 && num_bytes > 2) {
5778 // TODO: improve alignment check of sub-dword stores
5779 unsigned count_new = 2 / elem_size_bytes;
5780 writemask |= ((1 << (count - count_new)) - 1) << (start + count_new);
5781 count = count_new;
5782 num_bytes = 2;
5783 }
5784
5785 if (num_bytes > 16) {
5786 assert(elem_size_bytes == 8);
5787 writemask |= (((count - 2) << 1) - 1) << (start + 2);
5788 count = 2;
5789 num_bytes = 16;
5790 }
5791
5792 Temp write_data;
5793 if (elem_size_bytes < 4) {
5794 if (data.type() == RegType::sgpr) {
5795 data = as_vgpr(ctx, data);
5796 emit_split_vector(ctx, data, 4 * data.size() / elem_size_bytes);
5797 }
5798 RegClass rc = RegClass(RegType::vgpr, elem_size_bytes).as_subdword();
5799 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5800 for (int i = 0; i < count; i++)
5801 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, rc));
5802 write_data = bld.tmp(RegClass(RegType::vgpr, num_bytes).as_subdword());
5803 vec->definitions[0] = Definition(write_data);
5804 bld.insert(std::move(vec));
5805 } else if (count != instr->num_components) {
5806 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
5807 for (int i = 0; i < count; i++) {
5808 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(data.type(), elem_size_bytes / 4));
5809 vec->operands[i] = Operand(smem_nonfs ? bld.as_uniform(elem) : elem);
5810 }
5811 write_data = bld.tmp(!smem ? RegType::vgpr : smem_nonfs ? RegType::sgpr : data.type(), count * elem_size_bytes / 4);
5812 vec->definitions[0] = Definition(write_data);
5813 ctx->block->instructions.emplace_back(std::move(vec));
5814 } else if (!smem && data.type() != RegType::vgpr) {
5815 assert(num_bytes % 4 == 0);
5816 write_data = bld.copy(bld.def(RegType::vgpr, num_bytes / 4), data);
5817 } else if (smem_nonfs && data.type() == RegType::vgpr) {
5818 assert(num_bytes % 4 == 0);
5819 write_data = bld.as_uniform(data);
5820 } else {
5821 write_data = data;
5822 }
5823
5824 aco_opcode vmem_op, smem_op = aco_opcode::last_opcode;
5825 switch (num_bytes) {
5826 case 1:
5827 vmem_op = aco_opcode::buffer_store_byte;
5828 break;
5829 case 2:
5830 vmem_op = aco_opcode::buffer_store_short;
5831 break;
5832 case 4:
5833 vmem_op = aco_opcode::buffer_store_dword;
5834 smem_op = aco_opcode::s_buffer_store_dword;
5835 break;
5836 case 8:
5837 vmem_op = aco_opcode::buffer_store_dwordx2;
5838 smem_op = aco_opcode::s_buffer_store_dwordx2;
5839 break;
5840 case 12:
5841 vmem_op = aco_opcode::buffer_store_dwordx3;
5842 assert(!smem && ctx->options->chip_class > GFX6);
5843 break;
5844 case 16:
5845 vmem_op = aco_opcode::buffer_store_dwordx4;
5846 smem_op = aco_opcode::s_buffer_store_dwordx4;
5847 break;
5848 default:
5849 unreachable("Store SSBO not implemented for this size.");
5850 }
5851 if (ctx->stage == fragment_fs)
5852 smem_op = aco_opcode::p_fs_buffer_store_smem;
5853
5854 if (smem) {
5855 aco_ptr<SMEM_instruction> store{create_instruction<SMEM_instruction>(smem_op, Format::SMEM, 3, 0)};
5856 store->operands[0] = Operand(rsrc);
5857 if (start) {
5858 Temp off = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
5859 offset, Operand(start * elem_size_bytes));
5860 store->operands[1] = Operand(off);
5861 } else {
5862 store->operands[1] = Operand(offset);
5863 }
5864 if (smem_op != aco_opcode::p_fs_buffer_store_smem)
5865 store->operands[1].setFixed(m0);
5866 store->operands[2] = Operand(write_data);
5867 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5868 store->dlc = false;
5869 store->disable_wqm = true;
5870 store->barrier = barrier_buffer;
5871 ctx->block->instructions.emplace_back(std::move(store));
5872 ctx->program->wb_smem_l1_on_end = true;
5873 if (smem_op == aco_opcode::p_fs_buffer_store_smem) {
5874 ctx->block->kind |= block_kind_needs_lowering;
5875 ctx->program->needs_exact = true;
5876 }
5877 } else {
5878 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(vmem_op, Format::MUBUF, 4, 0)};
5879 store->operands[0] = Operand(rsrc);
5880 store->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5881 store->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5882 store->operands[3] = Operand(write_data);
5883 store->offset = start * elem_size_bytes;
5884 store->offen = (offset.type() == RegType::vgpr);
5885 store->glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
5886 store->dlc = false;
5887 store->disable_wqm = true;
5888 store->barrier = barrier_buffer;
5889 ctx->program->needs_exact = true;
5890 ctx->block->instructions.emplace_back(std::move(store));
5891 }
5892 }
5893 }
5894
5895 void visit_atomic_ssbo(isel_context *ctx, nir_intrinsic_instr *instr)
5896 {
5897 /* return the previous value if dest is ever used */
5898 bool return_previous = false;
5899 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
5900 return_previous = true;
5901 break;
5902 }
5903 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
5904 return_previous = true;
5905 break;
5906 }
5907
5908 Builder bld(ctx->program, ctx->block);
5909 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[2].ssa));
5910
5911 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
5912 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
5913 get_ssa_temp(ctx, instr->src[3].ssa), data);
5914
5915 Temp offset = get_ssa_temp(ctx, instr->src[1].ssa);
5916 Temp rsrc = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5917 rsrc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), rsrc, Operand(0u));
5918
5919 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
5920
5921 aco_opcode op32, op64;
5922 switch (instr->intrinsic) {
5923 case nir_intrinsic_ssbo_atomic_add:
5924 op32 = aco_opcode::buffer_atomic_add;
5925 op64 = aco_opcode::buffer_atomic_add_x2;
5926 break;
5927 case nir_intrinsic_ssbo_atomic_imin:
5928 op32 = aco_opcode::buffer_atomic_smin;
5929 op64 = aco_opcode::buffer_atomic_smin_x2;
5930 break;
5931 case nir_intrinsic_ssbo_atomic_umin:
5932 op32 = aco_opcode::buffer_atomic_umin;
5933 op64 = aco_opcode::buffer_atomic_umin_x2;
5934 break;
5935 case nir_intrinsic_ssbo_atomic_imax:
5936 op32 = aco_opcode::buffer_atomic_smax;
5937 op64 = aco_opcode::buffer_atomic_smax_x2;
5938 break;
5939 case nir_intrinsic_ssbo_atomic_umax:
5940 op32 = aco_opcode::buffer_atomic_umax;
5941 op64 = aco_opcode::buffer_atomic_umax_x2;
5942 break;
5943 case nir_intrinsic_ssbo_atomic_and:
5944 op32 = aco_opcode::buffer_atomic_and;
5945 op64 = aco_opcode::buffer_atomic_and_x2;
5946 break;
5947 case nir_intrinsic_ssbo_atomic_or:
5948 op32 = aco_opcode::buffer_atomic_or;
5949 op64 = aco_opcode::buffer_atomic_or_x2;
5950 break;
5951 case nir_intrinsic_ssbo_atomic_xor:
5952 op32 = aco_opcode::buffer_atomic_xor;
5953 op64 = aco_opcode::buffer_atomic_xor_x2;
5954 break;
5955 case nir_intrinsic_ssbo_atomic_exchange:
5956 op32 = aco_opcode::buffer_atomic_swap;
5957 op64 = aco_opcode::buffer_atomic_swap_x2;
5958 break;
5959 case nir_intrinsic_ssbo_atomic_comp_swap:
5960 op32 = aco_opcode::buffer_atomic_cmpswap;
5961 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
5962 break;
5963 default:
5964 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
5965 }
5966 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
5967 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
5968 mubuf->operands[0] = Operand(rsrc);
5969 mubuf->operands[1] = offset.type() == RegType::vgpr ? Operand(offset) : Operand(v1);
5970 mubuf->operands[2] = offset.type() == RegType::sgpr ? Operand(offset) : Operand((uint32_t) 0);
5971 mubuf->operands[3] = Operand(data);
5972 if (return_previous)
5973 mubuf->definitions[0] = Definition(dst);
5974 mubuf->offset = 0;
5975 mubuf->offen = (offset.type() == RegType::vgpr);
5976 mubuf->glc = return_previous;
5977 mubuf->dlc = false; /* Not needed for atomics */
5978 mubuf->disable_wqm = true;
5979 mubuf->barrier = barrier_buffer;
5980 ctx->program->needs_exact = true;
5981 ctx->block->instructions.emplace_back(std::move(mubuf));
5982 }
5983
5984 void visit_get_buffer_size(isel_context *ctx, nir_intrinsic_instr *instr) {
5985
5986 Temp index = convert_pointer_to_64_bit(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
5987 Builder bld(ctx->program, ctx->block);
5988 Temp desc = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), index, Operand(0u));
5989 get_buffer_size(ctx, desc, get_ssa_temp(ctx, &instr->dest.ssa), false);
5990 }
5991
5992 Temp get_gfx6_global_rsrc(Builder& bld, Temp addr)
5993 {
5994 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5995 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
5996
5997 if (addr.type() == RegType::vgpr)
5998 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf));
5999 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), addr, Operand(-1u), Operand(rsrc_conf));
6000 }
6001
6002 void visit_load_global(isel_context *ctx, nir_intrinsic_instr *instr)
6003 {
6004 Builder bld(ctx->program, ctx->block);
6005 unsigned num_components = instr->num_components;
6006 unsigned num_bytes = num_components * instr->dest.ssa.bit_size / 8;
6007
6008 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6009 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6010
6011 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT);
6012 bool dlc = glc && ctx->options->chip_class >= GFX10;
6013 aco_opcode op;
6014 if (dst.type() == RegType::vgpr || (glc && ctx->options->chip_class < GFX8)) {
6015 bool global = ctx->options->chip_class >= GFX9;
6016
6017 if (ctx->options->chip_class >= GFX7) {
6018 aco_opcode op;
6019 switch (num_bytes) {
6020 case 4:
6021 op = global ? aco_opcode::global_load_dword : aco_opcode::flat_load_dword;
6022 break;
6023 case 8:
6024 op = global ? aco_opcode::global_load_dwordx2 : aco_opcode::flat_load_dwordx2;
6025 break;
6026 case 12:
6027 op = global ? aco_opcode::global_load_dwordx3 : aco_opcode::flat_load_dwordx3;
6028 break;
6029 case 16:
6030 op = global ? aco_opcode::global_load_dwordx4 : aco_opcode::flat_load_dwordx4;
6031 break;
6032 default:
6033 unreachable("load_global not implemented for this size.");
6034 }
6035
6036 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 2, 1)};
6037 flat->operands[0] = Operand(addr);
6038 flat->operands[1] = Operand(s1);
6039 flat->glc = glc;
6040 flat->dlc = dlc;
6041 flat->barrier = barrier_buffer;
6042
6043 if (dst.type() == RegType::sgpr) {
6044 Temp vec = bld.tmp(RegType::vgpr, dst.size());
6045 flat->definitions[0] = Definition(vec);
6046 ctx->block->instructions.emplace_back(std::move(flat));
6047 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
6048 } else {
6049 flat->definitions[0] = Definition(dst);
6050 ctx->block->instructions.emplace_back(std::move(flat));
6051 }
6052 emit_split_vector(ctx, dst, num_components);
6053 } else {
6054 assert(ctx->options->chip_class == GFX6);
6055
6056 /* GFX6 doesn't support loading vec3, expand to vec4. */
6057 num_bytes = num_bytes == 12 ? 16 : num_bytes;
6058
6059 aco_opcode op;
6060 switch (num_bytes) {
6061 case 4:
6062 op = aco_opcode::buffer_load_dword;
6063 break;
6064 case 8:
6065 op = aco_opcode::buffer_load_dwordx2;
6066 break;
6067 case 16:
6068 op = aco_opcode::buffer_load_dwordx4;
6069 break;
6070 default:
6071 unreachable("load_global not implemented for this size.");
6072 }
6073
6074 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6075
6076 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
6077 mubuf->operands[0] = Operand(rsrc);
6078 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6079 mubuf->operands[2] = Operand(0u);
6080 mubuf->glc = glc;
6081 mubuf->dlc = false;
6082 mubuf->offset = 0;
6083 mubuf->addr64 = addr.type() == RegType::vgpr;
6084 mubuf->disable_wqm = false;
6085 mubuf->barrier = barrier_buffer;
6086 aco_ptr<Instruction> instr = std::move(mubuf);
6087
6088 /* expand vector */
6089 if (dst.size() == 3) {
6090 Temp vec = bld.tmp(v4);
6091 instr->definitions[0] = Definition(vec);
6092 bld.insert(std::move(instr));
6093 emit_split_vector(ctx, vec, 4);
6094
6095 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 3, 1));
6096 instr->operands[0] = Operand(emit_extract_vector(ctx, vec, 0, v1));
6097 instr->operands[1] = Operand(emit_extract_vector(ctx, vec, 1, v1));
6098 instr->operands[2] = Operand(emit_extract_vector(ctx, vec, 2, v1));
6099 }
6100
6101 if (dst.type() == RegType::sgpr) {
6102 Temp vec = bld.tmp(RegType::vgpr, dst.size());
6103 instr->definitions[0] = Definition(vec);
6104 bld.insert(std::move(instr));
6105 expand_vector(ctx, vec, dst, num_components, (1 << num_components) - 1);
6106 bld.pseudo(aco_opcode::p_as_uniform, Definition(dst), vec);
6107 } else {
6108 instr->definitions[0] = Definition(dst);
6109 bld.insert(std::move(instr));
6110 emit_split_vector(ctx, dst, num_components);
6111 }
6112 }
6113 } else {
6114 switch (num_bytes) {
6115 case 4:
6116 op = aco_opcode::s_load_dword;
6117 break;
6118 case 8:
6119 op = aco_opcode::s_load_dwordx2;
6120 break;
6121 case 12:
6122 case 16:
6123 op = aco_opcode::s_load_dwordx4;
6124 break;
6125 default:
6126 unreachable("load_global not implemented for this size.");
6127 }
6128 aco_ptr<SMEM_instruction> load{create_instruction<SMEM_instruction>(op, Format::SMEM, 2, 1)};
6129 load->operands[0] = Operand(addr);
6130 load->operands[1] = Operand(0u);
6131 load->definitions[0] = Definition(dst);
6132 load->glc = glc;
6133 load->dlc = dlc;
6134 load->barrier = barrier_buffer;
6135 assert(ctx->options->chip_class >= GFX8 || !glc);
6136
6137 if (dst.size() == 3) {
6138 /* trim vector */
6139 Temp vec = bld.tmp(s4);
6140 load->definitions[0] = Definition(vec);
6141 ctx->block->instructions.emplace_back(std::move(load));
6142 emit_split_vector(ctx, vec, 4);
6143
6144 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
6145 emit_extract_vector(ctx, vec, 0, s1),
6146 emit_extract_vector(ctx, vec, 1, s1),
6147 emit_extract_vector(ctx, vec, 2, s1));
6148 } else {
6149 ctx->block->instructions.emplace_back(std::move(load));
6150 }
6151 }
6152 }
6153
6154 void visit_store_global(isel_context *ctx, nir_intrinsic_instr *instr)
6155 {
6156 Builder bld(ctx->program, ctx->block);
6157 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6158
6159 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6160 Temp addr = get_ssa_temp(ctx, instr->src[1].ssa);
6161
6162 if (ctx->options->chip_class >= GFX7)
6163 addr = as_vgpr(ctx, addr);
6164
6165 unsigned writemask = nir_intrinsic_write_mask(instr);
6166 while (writemask) {
6167 int start, count;
6168 u_bit_scan_consecutive_range(&writemask, &start, &count);
6169 if (count == 3 && ctx->options->chip_class == GFX6) {
6170 /* GFX6 doesn't support storing vec3, split it. */
6171 writemask |= 1u << (start + 2);
6172 count = 2;
6173 }
6174 unsigned num_bytes = count * elem_size_bytes;
6175
6176 Temp write_data = data;
6177 if (count != instr->num_components) {
6178 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6179 for (int i = 0; i < count; i++)
6180 vec->operands[i] = Operand(emit_extract_vector(ctx, data, start + i, v1));
6181 write_data = bld.tmp(RegType::vgpr, count);
6182 vec->definitions[0] = Definition(write_data);
6183 ctx->block->instructions.emplace_back(std::move(vec));
6184 }
6185
6186 bool glc = nir_intrinsic_access(instr) & (ACCESS_VOLATILE | ACCESS_COHERENT | ACCESS_NON_READABLE);
6187 unsigned offset = start * elem_size_bytes;
6188
6189 if (ctx->options->chip_class >= GFX7) {
6190 if (offset > 0 && ctx->options->chip_class < GFX9) {
6191 Temp addr0 = bld.tmp(v1), addr1 = bld.tmp(v1);
6192 Temp new_addr0 = bld.tmp(v1), new_addr1 = bld.tmp(v1);
6193 Temp carry = bld.tmp(bld.lm);
6194 bld.pseudo(aco_opcode::p_split_vector, Definition(addr0), Definition(addr1), addr);
6195
6196 bld.vop2(aco_opcode::v_add_co_u32, Definition(new_addr0), bld.hint_vcc(Definition(carry)),
6197 Operand(offset), addr0);
6198 bld.vop2(aco_opcode::v_addc_co_u32, Definition(new_addr1), bld.def(bld.lm),
6199 Operand(0u), addr1,
6200 carry).def(1).setHint(vcc);
6201
6202 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), new_addr0, new_addr1);
6203
6204 offset = 0;
6205 }
6206
6207 bool global = ctx->options->chip_class >= GFX9;
6208 aco_opcode op;
6209 switch (num_bytes) {
6210 case 4:
6211 op = global ? aco_opcode::global_store_dword : aco_opcode::flat_store_dword;
6212 break;
6213 case 8:
6214 op = global ? aco_opcode::global_store_dwordx2 : aco_opcode::flat_store_dwordx2;
6215 break;
6216 case 12:
6217 op = global ? aco_opcode::global_store_dwordx3 : aco_opcode::flat_store_dwordx3;
6218 break;
6219 case 16:
6220 op = global ? aco_opcode::global_store_dwordx4 : aco_opcode::flat_store_dwordx4;
6221 break;
6222 default:
6223 unreachable("store_global not implemented for this size.");
6224 }
6225
6226 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, 0)};
6227 flat->operands[0] = Operand(addr);
6228 flat->operands[1] = Operand(s1);
6229 flat->operands[2] = Operand(data);
6230 flat->glc = glc;
6231 flat->dlc = false;
6232 flat->offset = offset;
6233 flat->disable_wqm = true;
6234 flat->barrier = barrier_buffer;
6235 ctx->program->needs_exact = true;
6236 ctx->block->instructions.emplace_back(std::move(flat));
6237 } else {
6238 assert(ctx->options->chip_class == GFX6);
6239
6240 aco_opcode op;
6241 switch (num_bytes) {
6242 case 4:
6243 op = aco_opcode::buffer_store_dword;
6244 break;
6245 case 8:
6246 op = aco_opcode::buffer_store_dwordx2;
6247 break;
6248 case 16:
6249 op = aco_opcode::buffer_store_dwordx4;
6250 break;
6251 default:
6252 unreachable("store_global not implemented for this size.");
6253 }
6254
6255 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6256
6257 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, 0)};
6258 mubuf->operands[0] = Operand(rsrc);
6259 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6260 mubuf->operands[2] = Operand(0u);
6261 mubuf->operands[3] = Operand(write_data);
6262 mubuf->glc = glc;
6263 mubuf->dlc = false;
6264 mubuf->offset = offset;
6265 mubuf->addr64 = addr.type() == RegType::vgpr;
6266 mubuf->disable_wqm = true;
6267 mubuf->barrier = barrier_buffer;
6268 ctx->program->needs_exact = true;
6269 ctx->block->instructions.emplace_back(std::move(mubuf));
6270 }
6271 }
6272 }
6273
6274 void visit_global_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6275 {
6276 /* return the previous value if dest is ever used */
6277 bool return_previous = false;
6278 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6279 return_previous = true;
6280 break;
6281 }
6282 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6283 return_previous = true;
6284 break;
6285 }
6286
6287 Builder bld(ctx->program, ctx->block);
6288 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
6289 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6290
6291 if (ctx->options->chip_class >= GFX7)
6292 addr = as_vgpr(ctx, addr);
6293
6294 if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
6295 data = bld.pseudo(aco_opcode::p_create_vector, bld.def(RegType::vgpr, data.size() * 2),
6296 get_ssa_temp(ctx, instr->src[2].ssa), data);
6297
6298 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6299
6300 aco_opcode op32, op64;
6301
6302 if (ctx->options->chip_class >= GFX7) {
6303 bool global = ctx->options->chip_class >= GFX9;
6304 switch (instr->intrinsic) {
6305 case nir_intrinsic_global_atomic_add:
6306 op32 = global ? aco_opcode::global_atomic_add : aco_opcode::flat_atomic_add;
6307 op64 = global ? aco_opcode::global_atomic_add_x2 : aco_opcode::flat_atomic_add_x2;
6308 break;
6309 case nir_intrinsic_global_atomic_imin:
6310 op32 = global ? aco_opcode::global_atomic_smin : aco_opcode::flat_atomic_smin;
6311 op64 = global ? aco_opcode::global_atomic_smin_x2 : aco_opcode::flat_atomic_smin_x2;
6312 break;
6313 case nir_intrinsic_global_atomic_umin:
6314 op32 = global ? aco_opcode::global_atomic_umin : aco_opcode::flat_atomic_umin;
6315 op64 = global ? aco_opcode::global_atomic_umin_x2 : aco_opcode::flat_atomic_umin_x2;
6316 break;
6317 case nir_intrinsic_global_atomic_imax:
6318 op32 = global ? aco_opcode::global_atomic_smax : aco_opcode::flat_atomic_smax;
6319 op64 = global ? aco_opcode::global_atomic_smax_x2 : aco_opcode::flat_atomic_smax_x2;
6320 break;
6321 case nir_intrinsic_global_atomic_umax:
6322 op32 = global ? aco_opcode::global_atomic_umax : aco_opcode::flat_atomic_umax;
6323 op64 = global ? aco_opcode::global_atomic_umax_x2 : aco_opcode::flat_atomic_umax_x2;
6324 break;
6325 case nir_intrinsic_global_atomic_and:
6326 op32 = global ? aco_opcode::global_atomic_and : aco_opcode::flat_atomic_and;
6327 op64 = global ? aco_opcode::global_atomic_and_x2 : aco_opcode::flat_atomic_and_x2;
6328 break;
6329 case nir_intrinsic_global_atomic_or:
6330 op32 = global ? aco_opcode::global_atomic_or : aco_opcode::flat_atomic_or;
6331 op64 = global ? aco_opcode::global_atomic_or_x2 : aco_opcode::flat_atomic_or_x2;
6332 break;
6333 case nir_intrinsic_global_atomic_xor:
6334 op32 = global ? aco_opcode::global_atomic_xor : aco_opcode::flat_atomic_xor;
6335 op64 = global ? aco_opcode::global_atomic_xor_x2 : aco_opcode::flat_atomic_xor_x2;
6336 break;
6337 case nir_intrinsic_global_atomic_exchange:
6338 op32 = global ? aco_opcode::global_atomic_swap : aco_opcode::flat_atomic_swap;
6339 op64 = global ? aco_opcode::global_atomic_swap_x2 : aco_opcode::flat_atomic_swap_x2;
6340 break;
6341 case nir_intrinsic_global_atomic_comp_swap:
6342 op32 = global ? aco_opcode::global_atomic_cmpswap : aco_opcode::flat_atomic_cmpswap;
6343 op64 = global ? aco_opcode::global_atomic_cmpswap_x2 : aco_opcode::flat_atomic_cmpswap_x2;
6344 break;
6345 default:
6346 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6347 }
6348
6349 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6350 aco_ptr<FLAT_instruction> flat{create_instruction<FLAT_instruction>(op, global ? Format::GLOBAL : Format::FLAT, 3, return_previous ? 1 : 0)};
6351 flat->operands[0] = Operand(addr);
6352 flat->operands[1] = Operand(s1);
6353 flat->operands[2] = Operand(data);
6354 if (return_previous)
6355 flat->definitions[0] = Definition(dst);
6356 flat->glc = return_previous;
6357 flat->dlc = false; /* Not needed for atomics */
6358 flat->offset = 0;
6359 flat->disable_wqm = true;
6360 flat->barrier = barrier_buffer;
6361 ctx->program->needs_exact = true;
6362 ctx->block->instructions.emplace_back(std::move(flat));
6363 } else {
6364 assert(ctx->options->chip_class == GFX6);
6365
6366 switch (instr->intrinsic) {
6367 case nir_intrinsic_global_atomic_add:
6368 op32 = aco_opcode::buffer_atomic_add;
6369 op64 = aco_opcode::buffer_atomic_add_x2;
6370 break;
6371 case nir_intrinsic_global_atomic_imin:
6372 op32 = aco_opcode::buffer_atomic_smin;
6373 op64 = aco_opcode::buffer_atomic_smin_x2;
6374 break;
6375 case nir_intrinsic_global_atomic_umin:
6376 op32 = aco_opcode::buffer_atomic_umin;
6377 op64 = aco_opcode::buffer_atomic_umin_x2;
6378 break;
6379 case nir_intrinsic_global_atomic_imax:
6380 op32 = aco_opcode::buffer_atomic_smax;
6381 op64 = aco_opcode::buffer_atomic_smax_x2;
6382 break;
6383 case nir_intrinsic_global_atomic_umax:
6384 op32 = aco_opcode::buffer_atomic_umax;
6385 op64 = aco_opcode::buffer_atomic_umax_x2;
6386 break;
6387 case nir_intrinsic_global_atomic_and:
6388 op32 = aco_opcode::buffer_atomic_and;
6389 op64 = aco_opcode::buffer_atomic_and_x2;
6390 break;
6391 case nir_intrinsic_global_atomic_or:
6392 op32 = aco_opcode::buffer_atomic_or;
6393 op64 = aco_opcode::buffer_atomic_or_x2;
6394 break;
6395 case nir_intrinsic_global_atomic_xor:
6396 op32 = aco_opcode::buffer_atomic_xor;
6397 op64 = aco_opcode::buffer_atomic_xor_x2;
6398 break;
6399 case nir_intrinsic_global_atomic_exchange:
6400 op32 = aco_opcode::buffer_atomic_swap;
6401 op64 = aco_opcode::buffer_atomic_swap_x2;
6402 break;
6403 case nir_intrinsic_global_atomic_comp_swap:
6404 op32 = aco_opcode::buffer_atomic_cmpswap;
6405 op64 = aco_opcode::buffer_atomic_cmpswap_x2;
6406 break;
6407 default:
6408 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6409 }
6410
6411 Temp rsrc = get_gfx6_global_rsrc(bld, addr);
6412
6413 aco_opcode op = instr->dest.ssa.bit_size == 32 ? op32 : op64;
6414
6415 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 4, return_previous ? 1 : 0)};
6416 mubuf->operands[0] = Operand(rsrc);
6417 mubuf->operands[1] = addr.type() == RegType::vgpr ? Operand(addr) : Operand(v1);
6418 mubuf->operands[2] = Operand(0u);
6419 mubuf->operands[3] = Operand(data);
6420 if (return_previous)
6421 mubuf->definitions[0] = Definition(dst);
6422 mubuf->glc = return_previous;
6423 mubuf->dlc = false;
6424 mubuf->offset = 0;
6425 mubuf->addr64 = addr.type() == RegType::vgpr;
6426 mubuf->disable_wqm = true;
6427 mubuf->barrier = barrier_buffer;
6428 ctx->program->needs_exact = true;
6429 ctx->block->instructions.emplace_back(std::move(mubuf));
6430 }
6431 }
6432
6433 void emit_memory_barrier(isel_context *ctx, nir_intrinsic_instr *instr) {
6434 Builder bld(ctx->program, ctx->block);
6435 switch(instr->intrinsic) {
6436 case nir_intrinsic_group_memory_barrier:
6437 case nir_intrinsic_memory_barrier:
6438 bld.barrier(aco_opcode::p_memory_barrier_common);
6439 break;
6440 case nir_intrinsic_memory_barrier_buffer:
6441 bld.barrier(aco_opcode::p_memory_barrier_buffer);
6442 break;
6443 case nir_intrinsic_memory_barrier_image:
6444 bld.barrier(aco_opcode::p_memory_barrier_image);
6445 break;
6446 case nir_intrinsic_memory_barrier_tcs_patch:
6447 case nir_intrinsic_memory_barrier_shared:
6448 bld.barrier(aco_opcode::p_memory_barrier_shared);
6449 break;
6450 default:
6451 unreachable("Unimplemented memory barrier intrinsic");
6452 break;
6453 }
6454 }
6455
6456 void visit_load_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6457 {
6458 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6459 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6460 assert(instr->dest.ssa.bit_size >= 32 && "Bitsize not supported in load_shared.");
6461 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6462 Builder bld(ctx->program, ctx->block);
6463
6464 unsigned elem_size_bytes = instr->dest.ssa.bit_size / 8;
6465 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6466 load_lds(ctx, elem_size_bytes, dst, address, nir_intrinsic_base(instr), align);
6467 }
6468
6469 void visit_store_shared(isel_context *ctx, nir_intrinsic_instr *instr)
6470 {
6471 unsigned writemask = nir_intrinsic_write_mask(instr);
6472 Temp data = get_ssa_temp(ctx, instr->src[0].ssa);
6473 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6474 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6475 assert(elem_size_bytes >= 4 && "Only 32bit & 64bit store_shared currently supported.");
6476
6477 unsigned align = nir_intrinsic_align_mul(instr) ? nir_intrinsic_align(instr) : elem_size_bytes;
6478 store_lds(ctx, elem_size_bytes, data, writemask, address, nir_intrinsic_base(instr), align);
6479 }
6480
6481 void visit_shared_atomic(isel_context *ctx, nir_intrinsic_instr *instr)
6482 {
6483 unsigned offset = nir_intrinsic_base(instr);
6484 Operand m = load_lds_size_m0(ctx);
6485 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6486 Temp address = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6487
6488 unsigned num_operands = 3;
6489 aco_opcode op32, op64, op32_rtn, op64_rtn;
6490 switch(instr->intrinsic) {
6491 case nir_intrinsic_shared_atomic_add:
6492 op32 = aco_opcode::ds_add_u32;
6493 op64 = aco_opcode::ds_add_u64;
6494 op32_rtn = aco_opcode::ds_add_rtn_u32;
6495 op64_rtn = aco_opcode::ds_add_rtn_u64;
6496 break;
6497 case nir_intrinsic_shared_atomic_imin:
6498 op32 = aco_opcode::ds_min_i32;
6499 op64 = aco_opcode::ds_min_i64;
6500 op32_rtn = aco_opcode::ds_min_rtn_i32;
6501 op64_rtn = aco_opcode::ds_min_rtn_i64;
6502 break;
6503 case nir_intrinsic_shared_atomic_umin:
6504 op32 = aco_opcode::ds_min_u32;
6505 op64 = aco_opcode::ds_min_u64;
6506 op32_rtn = aco_opcode::ds_min_rtn_u32;
6507 op64_rtn = aco_opcode::ds_min_rtn_u64;
6508 break;
6509 case nir_intrinsic_shared_atomic_imax:
6510 op32 = aco_opcode::ds_max_i32;
6511 op64 = aco_opcode::ds_max_i64;
6512 op32_rtn = aco_opcode::ds_max_rtn_i32;
6513 op64_rtn = aco_opcode::ds_max_rtn_i64;
6514 break;
6515 case nir_intrinsic_shared_atomic_umax:
6516 op32 = aco_opcode::ds_max_u32;
6517 op64 = aco_opcode::ds_max_u64;
6518 op32_rtn = aco_opcode::ds_max_rtn_u32;
6519 op64_rtn = aco_opcode::ds_max_rtn_u64;
6520 break;
6521 case nir_intrinsic_shared_atomic_and:
6522 op32 = aco_opcode::ds_and_b32;
6523 op64 = aco_opcode::ds_and_b64;
6524 op32_rtn = aco_opcode::ds_and_rtn_b32;
6525 op64_rtn = aco_opcode::ds_and_rtn_b64;
6526 break;
6527 case nir_intrinsic_shared_atomic_or:
6528 op32 = aco_opcode::ds_or_b32;
6529 op64 = aco_opcode::ds_or_b64;
6530 op32_rtn = aco_opcode::ds_or_rtn_b32;
6531 op64_rtn = aco_opcode::ds_or_rtn_b64;
6532 break;
6533 case nir_intrinsic_shared_atomic_xor:
6534 op32 = aco_opcode::ds_xor_b32;
6535 op64 = aco_opcode::ds_xor_b64;
6536 op32_rtn = aco_opcode::ds_xor_rtn_b32;
6537 op64_rtn = aco_opcode::ds_xor_rtn_b64;
6538 break;
6539 case nir_intrinsic_shared_atomic_exchange:
6540 op32 = aco_opcode::ds_write_b32;
6541 op64 = aco_opcode::ds_write_b64;
6542 op32_rtn = aco_opcode::ds_wrxchg_rtn_b32;
6543 op64_rtn = aco_opcode::ds_wrxchg2_rtn_b64;
6544 break;
6545 case nir_intrinsic_shared_atomic_comp_swap:
6546 op32 = aco_opcode::ds_cmpst_b32;
6547 op64 = aco_opcode::ds_cmpst_b64;
6548 op32_rtn = aco_opcode::ds_cmpst_rtn_b32;
6549 op64_rtn = aco_opcode::ds_cmpst_rtn_b64;
6550 num_operands = 4;
6551 break;
6552 default:
6553 unreachable("Unhandled shared atomic intrinsic");
6554 }
6555
6556 /* return the previous value if dest is ever used */
6557 bool return_previous = false;
6558 nir_foreach_use_safe(use_src, &instr->dest.ssa) {
6559 return_previous = true;
6560 break;
6561 }
6562 nir_foreach_if_use_safe(use_src, &instr->dest.ssa) {
6563 return_previous = true;
6564 break;
6565 }
6566
6567 aco_opcode op;
6568 if (data.size() == 1) {
6569 assert(instr->dest.ssa.bit_size == 32);
6570 op = return_previous ? op32_rtn : op32;
6571 } else {
6572 assert(instr->dest.ssa.bit_size == 64);
6573 op = return_previous ? op64_rtn : op64;
6574 }
6575
6576 if (offset > 65535) {
6577 Builder bld(ctx->program, ctx->block);
6578 address = bld.vadd32(bld.def(v1), Operand(offset), address);
6579 offset = 0;
6580 }
6581
6582 aco_ptr<DS_instruction> ds;
6583 ds.reset(create_instruction<DS_instruction>(op, Format::DS, num_operands, return_previous ? 1 : 0));
6584 ds->operands[0] = Operand(address);
6585 ds->operands[1] = Operand(data);
6586 if (num_operands == 4)
6587 ds->operands[2] = Operand(get_ssa_temp(ctx, instr->src[2].ssa));
6588 ds->operands[num_operands - 1] = m;
6589 ds->offset0 = offset;
6590 if (return_previous)
6591 ds->definitions[0] = Definition(get_ssa_temp(ctx, &instr->dest.ssa));
6592 ctx->block->instructions.emplace_back(std::move(ds));
6593 }
6594
6595 Temp get_scratch_resource(isel_context *ctx)
6596 {
6597 Builder bld(ctx->program, ctx->block);
6598 Temp scratch_addr = ctx->program->private_segment_buffer;
6599 if (ctx->stage != compute_cs)
6600 scratch_addr = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), scratch_addr, Operand(0u));
6601
6602 uint32_t rsrc_conf = S_008F0C_ADD_TID_ENABLE(1) |
6603 S_008F0C_INDEX_STRIDE(ctx->program->wave_size == 64 ? 3 : 2);;
6604
6605 if (ctx->program->chip_class >= GFX10) {
6606 rsrc_conf |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
6607 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
6608 S_008F0C_RESOURCE_LEVEL(1);
6609 } else if (ctx->program->chip_class <= GFX7) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6610 rsrc_conf |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
6611 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
6612 }
6613
6614 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6615 if (ctx->program->chip_class <= GFX8)
6616 rsrc_conf |= S_008F0C_ELEMENT_SIZE(3);
6617
6618 return bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), scratch_addr, Operand(-1u), Operand(rsrc_conf));
6619 }
6620
6621 void visit_load_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6622 assert(instr->dest.ssa.bit_size == 32 || instr->dest.ssa.bit_size == 64);
6623 Builder bld(ctx->program, ctx->block);
6624 Temp rsrc = get_scratch_resource(ctx);
6625 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6626 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6627
6628 aco_opcode op;
6629 switch (dst.size()) {
6630 case 1:
6631 op = aco_opcode::buffer_load_dword;
6632 break;
6633 case 2:
6634 op = aco_opcode::buffer_load_dwordx2;
6635 break;
6636 case 3:
6637 op = aco_opcode::buffer_load_dwordx3;
6638 break;
6639 case 4:
6640 op = aco_opcode::buffer_load_dwordx4;
6641 break;
6642 case 6:
6643 case 8: {
6644 std::array<Temp,NIR_MAX_VEC_COMPONENTS> elems;
6645 Temp lower = bld.mubuf(aco_opcode::buffer_load_dwordx4,
6646 bld.def(v4), rsrc, offset,
6647 ctx->program->scratch_offset, 0, true);
6648 Temp upper = bld.mubuf(dst.size() == 6 ? aco_opcode::buffer_load_dwordx2 :
6649 aco_opcode::buffer_load_dwordx4,
6650 dst.size() == 6 ? bld.def(v2) : bld.def(v4),
6651 rsrc, offset, ctx->program->scratch_offset, 16, true);
6652 emit_split_vector(ctx, lower, 2);
6653 elems[0] = emit_extract_vector(ctx, lower, 0, v2);
6654 elems[1] = emit_extract_vector(ctx, lower, 1, v2);
6655 if (dst.size() == 8) {
6656 emit_split_vector(ctx, upper, 2);
6657 elems[2] = emit_extract_vector(ctx, upper, 0, v2);
6658 elems[3] = emit_extract_vector(ctx, upper, 1, v2);
6659 } else {
6660 elems[2] = upper;
6661 }
6662
6663 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
6664 Format::PSEUDO, dst.size() / 2, 1)};
6665 for (unsigned i = 0; i < dst.size() / 2; i++)
6666 vec->operands[i] = Operand(elems[i]);
6667 vec->definitions[0] = Definition(dst);
6668 bld.insert(std::move(vec));
6669 ctx->allocated_vec.emplace(dst.id(), elems);
6670 return;
6671 }
6672 default:
6673 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
6674 }
6675
6676 bld.mubuf(op, Definition(dst), rsrc, offset, ctx->program->scratch_offset, 0, true);
6677 emit_split_vector(ctx, dst, instr->num_components);
6678 }
6679
6680 void visit_store_scratch(isel_context *ctx, nir_intrinsic_instr *instr) {
6681 assert(instr->src[0].ssa->bit_size == 32 || instr->src[0].ssa->bit_size == 64);
6682 Builder bld(ctx->program, ctx->block);
6683 Temp rsrc = get_scratch_resource(ctx);
6684 Temp data = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6685 Temp offset = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[1].ssa));
6686
6687 unsigned elem_size_bytes = instr->src[0].ssa->bit_size / 8;
6688 unsigned writemask = nir_intrinsic_write_mask(instr);
6689
6690 while (writemask) {
6691 int start, count;
6692 u_bit_scan_consecutive_range(&writemask, &start, &count);
6693 int num_bytes = count * elem_size_bytes;
6694
6695 if (num_bytes > 16) {
6696 assert(elem_size_bytes == 8);
6697 writemask |= (((count - 2) << 1) - 1) << (start + 2);
6698 count = 2;
6699 num_bytes = 16;
6700 }
6701
6702 // TODO: check alignment of sub-dword stores
6703 // TODO: split 3 bytes. there is no store instruction for that
6704
6705 Temp write_data;
6706 if (count != instr->num_components) {
6707 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
6708 for (int i = 0; i < count; i++) {
6709 Temp elem = emit_extract_vector(ctx, data, start + i, RegClass(RegType::vgpr, elem_size_bytes / 4));
6710 vec->operands[i] = Operand(elem);
6711 }
6712 write_data = bld.tmp(RegClass(RegType::vgpr, count * elem_size_bytes / 4));
6713 vec->definitions[0] = Definition(write_data);
6714 ctx->block->instructions.emplace_back(std::move(vec));
6715 } else {
6716 write_data = data;
6717 }
6718
6719 aco_opcode op;
6720 switch (num_bytes) {
6721 case 4:
6722 op = aco_opcode::buffer_store_dword;
6723 break;
6724 case 8:
6725 op = aco_opcode::buffer_store_dwordx2;
6726 break;
6727 case 12:
6728 op = aco_opcode::buffer_store_dwordx3;
6729 break;
6730 case 16:
6731 op = aco_opcode::buffer_store_dwordx4;
6732 break;
6733 default:
6734 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
6735 }
6736
6737 bld.mubuf(op, rsrc, offset, ctx->program->scratch_offset, write_data, start * elem_size_bytes, true);
6738 }
6739 }
6740
6741 void visit_load_sample_mask_in(isel_context *ctx, nir_intrinsic_instr *instr) {
6742 uint8_t log2_ps_iter_samples;
6743 if (ctx->program->info->ps.force_persample) {
6744 log2_ps_iter_samples =
6745 util_logbase2(ctx->options->key.fs.num_samples);
6746 } else {
6747 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
6748 }
6749
6750 /* The bit pattern matches that used by fixed function fragment
6751 * processing. */
6752 static const unsigned ps_iter_masks[] = {
6753 0xffff, /* not used */
6754 0x5555,
6755 0x1111,
6756 0x0101,
6757 0x0001,
6758 };
6759 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
6760
6761 Builder bld(ctx->program, ctx->block);
6762
6763 Temp sample_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
6764 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
6765 Temp ps_iter_mask = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(ps_iter_masks[log2_ps_iter_samples]));
6766 Temp mask = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), sample_id, ps_iter_mask);
6767 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
6768 bld.vop2(aco_opcode::v_and_b32, Definition(dst), mask, get_arg(ctx, ctx->args->ac.sample_coverage));
6769 }
6770
6771 void visit_emit_vertex_with_counter(isel_context *ctx, nir_intrinsic_instr *instr) {
6772 Builder bld(ctx->program, ctx->block);
6773
6774 unsigned stream = nir_intrinsic_stream_id(instr);
6775 Temp next_vertex = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
6776 next_vertex = bld.v_mul_imm(bld.def(v1), next_vertex, 4u);
6777 nir_const_value *next_vertex_cv = nir_src_as_const_value(instr->src[0]);
6778
6779 /* get GSVS ring */
6780 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), ctx->program->private_segment_buffer, Operand(RING_GSVS_GS * 16u));
6781
6782 unsigned num_components =
6783 ctx->program->info->gs.num_stream_output_components[stream];
6784 assert(num_components);
6785
6786 unsigned stride = 4u * num_components * ctx->shader->info.gs.vertices_out;
6787 unsigned stream_offset = 0;
6788 for (unsigned i = 0; i < stream; i++) {
6789 unsigned prev_stride = 4u * ctx->program->info->gs.num_stream_output_components[i] * ctx->shader->info.gs.vertices_out;
6790 stream_offset += prev_stride * ctx->program->wave_size;
6791 }
6792
6793 /* Limit on the stride field for <= GFX7. */
6794 assert(stride < (1 << 14));
6795
6796 Temp gsvs_dwords[4];
6797 for (unsigned i = 0; i < 4; i++)
6798 gsvs_dwords[i] = bld.tmp(s1);
6799 bld.pseudo(aco_opcode::p_split_vector,
6800 Definition(gsvs_dwords[0]),
6801 Definition(gsvs_dwords[1]),
6802 Definition(gsvs_dwords[2]),
6803 Definition(gsvs_dwords[3]),
6804 gsvs_ring);
6805
6806 if (stream_offset) {
6807 Temp stream_offset_tmp = bld.copy(bld.def(s1), Operand(stream_offset));
6808
6809 Temp carry = bld.tmp(s1);
6810 gsvs_dwords[0] = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.scc(Definition(carry)), gsvs_dwords[0], stream_offset_tmp);
6811 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));
6812 }
6813
6814 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)));
6815 gsvs_dwords[2] = bld.copy(bld.def(s1), Operand((uint32_t)ctx->program->wave_size));
6816
6817 gsvs_ring = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
6818 gsvs_dwords[0], gsvs_dwords[1], gsvs_dwords[2], gsvs_dwords[3]);
6819
6820 unsigned offset = 0;
6821 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; i++) {
6822 if (ctx->program->info->gs.output_streams[i] != stream)
6823 continue;
6824
6825 for (unsigned j = 0; j < 4; j++) {
6826 if (!(ctx->program->info->gs.output_usage_mask[i] & (1 << j)))
6827 continue;
6828
6829 if (ctx->outputs.mask[i] & (1 << j)) {
6830 Operand vaddr_offset = next_vertex_cv ? Operand(v1) : Operand(next_vertex);
6831 unsigned const_offset = (offset + (next_vertex_cv ? next_vertex_cv->u32 : 0u)) * 4u;
6832 if (const_offset >= 4096u) {
6833 if (vaddr_offset.isUndefined())
6834 vaddr_offset = bld.copy(bld.def(v1), Operand(const_offset / 4096u * 4096u));
6835 else
6836 vaddr_offset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), vaddr_offset);
6837 const_offset %= 4096u;
6838 }
6839
6840 aco_ptr<MTBUF_instruction> mtbuf{create_instruction<MTBUF_instruction>(aco_opcode::tbuffer_store_format_x, Format::MTBUF, 4, 0)};
6841 mtbuf->operands[0] = Operand(gsvs_ring);
6842 mtbuf->operands[1] = vaddr_offset;
6843 mtbuf->operands[2] = Operand(get_arg(ctx, ctx->args->gs2vs_offset));
6844 mtbuf->operands[3] = Operand(ctx->outputs.temps[i * 4u + j]);
6845 mtbuf->offen = !vaddr_offset.isUndefined();
6846 mtbuf->dfmt = V_008F0C_BUF_DATA_FORMAT_32;
6847 mtbuf->nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
6848 mtbuf->offset = const_offset;
6849 mtbuf->glc = true;
6850 mtbuf->slc = true;
6851 mtbuf->barrier = barrier_gs_data;
6852 mtbuf->can_reorder = true;
6853 bld.insert(std::move(mtbuf));
6854 }
6855
6856 offset += ctx->shader->info.gs.vertices_out;
6857 }
6858
6859 /* outputs for the next vertex are undefined and keeping them around can
6860 * create invalid IR with control flow */
6861 ctx->outputs.mask[i] = 0;
6862 }
6863
6864 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(false, true, stream));
6865 }
6866
6867 Temp emit_boolean_reduce(isel_context *ctx, nir_op op, unsigned cluster_size, Temp src)
6868 {
6869 Builder bld(ctx->program, ctx->block);
6870
6871 if (cluster_size == 1) {
6872 return src;
6873 } if (op == nir_op_iand && cluster_size == 4) {
6874 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6875 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6876 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc),
6877 bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc), tmp));
6878 } else if (op == nir_op_ior && cluster_size == 4) {
6879 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6880 return bld.sop1(Builder::s_wqm, bld.def(bld.lm), bld.def(s1, scc),
6881 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)));
6882 } else if (op == nir_op_iand && cluster_size == ctx->program->wave_size) {
6883 //subgroupAnd(val) -> (exec & ~val) == 0
6884 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
6885 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
6886 return bld.sop1(Builder::s_not, bld.def(bld.lm), bld.def(s1, scc), cond);
6887 } else if (op == nir_op_ior && cluster_size == ctx->program->wave_size) {
6888 //subgroupOr(val) -> (val & exec) != 0
6889 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm)).def(1).getTemp();
6890 return bool_to_vector_condition(ctx, tmp);
6891 } else if (op == nir_op_ixor && cluster_size == ctx->program->wave_size) {
6892 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6893 Temp tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6894 tmp = bld.sop1(Builder::s_bcnt1_i32, bld.def(s1), bld.def(s1, scc), tmp);
6895 tmp = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), tmp, Operand(1u)).def(1).getTemp();
6896 return bool_to_vector_condition(ctx, tmp);
6897 } else {
6898 //subgroupClustered{And,Or,Xor}(val, n) ->
6899 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6900 //cluster_offset = ~(n - 1) & lane_id
6901 //cluster_mask = ((1 << n) - 1)
6902 //subgroupClusteredAnd():
6903 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6904 //subgroupClusteredOr():
6905 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6906 //subgroupClusteredXor():
6907 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6908 Temp lane_id = emit_mbcnt(ctx, bld.def(v1));
6909 Temp cluster_offset = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(~uint32_t(cluster_size - 1)), lane_id);
6910
6911 Temp tmp;
6912 if (op == nir_op_iand)
6913 tmp = bld.sop2(Builder::s_orn2, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6914 else
6915 tmp = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
6916
6917 uint32_t cluster_mask = cluster_size == 32 ? -1 : (1u << cluster_size) - 1u;
6918
6919 if (ctx->program->chip_class <= GFX7)
6920 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), tmp, cluster_offset);
6921 else if (ctx->program->wave_size == 64)
6922 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), cluster_offset, tmp);
6923 else
6924 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), cluster_offset, tmp);
6925 tmp = emit_extract_vector(ctx, tmp, 0, v1);
6926 if (cluster_mask != 0xffffffff)
6927 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(cluster_mask), tmp);
6928
6929 Definition cmp_def = Definition();
6930 if (op == nir_op_iand) {
6931 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(cluster_mask), tmp).def(0);
6932 } else if (op == nir_op_ior) {
6933 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6934 } else if (op == nir_op_ixor) {
6935 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u),
6936 bld.vop3(aco_opcode::v_bcnt_u32_b32, bld.def(v1), tmp, Operand(0u)));
6937 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp).def(0);
6938 }
6939 cmp_def.setHint(vcc);
6940 return cmp_def.getTemp();
6941 }
6942 }
6943
6944 Temp emit_boolean_exclusive_scan(isel_context *ctx, nir_op op, Temp src)
6945 {
6946 Builder bld(ctx->program, ctx->block);
6947
6948 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
6949 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
6950 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
6951 Temp tmp;
6952 if (op == nir_op_iand)
6953 tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src);
6954 else
6955 tmp = bld.sop2(Builder::s_and, bld.def(s2), bld.def(s1, scc), src, Operand(exec, bld.lm));
6956
6957 Builder::Result lohi = bld.pseudo(aco_opcode::p_split_vector, bld.def(s1), bld.def(s1), tmp);
6958 Temp lo = lohi.def(0).getTemp();
6959 Temp hi = lohi.def(1).getTemp();
6960 Temp mbcnt = emit_mbcnt(ctx, bld.def(v1), Operand(lo), Operand(hi));
6961
6962 Definition cmp_def = Definition();
6963 if (op == nir_op_iand)
6964 cmp_def = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6965 else if (op == nir_op_ior)
6966 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), mbcnt).def(0);
6967 else if (op == nir_op_ixor)
6968 cmp_def = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u),
6969 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), mbcnt)).def(0);
6970 cmp_def.setHint(vcc);
6971 return cmp_def.getTemp();
6972 }
6973
6974 Temp emit_boolean_inclusive_scan(isel_context *ctx, nir_op op, Temp src)
6975 {
6976 Builder bld(ctx->program, ctx->block);
6977
6978 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
6979 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
6980 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
6981 Temp tmp = emit_boolean_exclusive_scan(ctx, op, src);
6982 if (op == nir_op_iand)
6983 return bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6984 else if (op == nir_op_ior)
6985 return bld.sop2(Builder::s_or, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6986 else if (op == nir_op_ixor)
6987 return bld.sop2(Builder::s_xor, bld.def(bld.lm), bld.def(s1, scc), tmp, src);
6988
6989 assert(false);
6990 return Temp();
6991 }
6992
6993 void emit_uniform_subgroup(isel_context *ctx, nir_intrinsic_instr *instr, Temp src)
6994 {
6995 Builder bld(ctx->program, ctx->block);
6996 Definition dst(get_ssa_temp(ctx, &instr->dest.ssa));
6997 if (src.regClass().type() == RegType::vgpr) {
6998 bld.pseudo(aco_opcode::p_as_uniform, dst, src);
6999 } else if (src.regClass() == s1) {
7000 bld.sop1(aco_opcode::s_mov_b32, dst, src);
7001 } else if (src.regClass() == s2) {
7002 bld.sop1(aco_opcode::s_mov_b64, dst, src);
7003 } else {
7004 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7005 nir_print_instr(&instr->instr, stderr);
7006 fprintf(stderr, "\n");
7007 }
7008 }
7009
7010 void emit_interp_center(isel_context *ctx, Temp dst, Temp pos1, Temp pos2)
7011 {
7012 Builder bld(ctx->program, ctx->block);
7013 Temp persp_center = get_arg(ctx, ctx->args->ac.persp_center);
7014 Temp p1 = emit_extract_vector(ctx, persp_center, 0, v1);
7015 Temp p2 = emit_extract_vector(ctx, persp_center, 1, v1);
7016
7017 Temp ddx_1, ddx_2, ddy_1, ddy_2;
7018 uint32_t dpp_ctrl0 = dpp_quad_perm(0, 0, 0, 0);
7019 uint32_t dpp_ctrl1 = dpp_quad_perm(1, 1, 1, 1);
7020 uint32_t dpp_ctrl2 = dpp_quad_perm(2, 2, 2, 2);
7021
7022 /* Build DD X/Y */
7023 if (ctx->program->chip_class >= GFX8) {
7024 Temp tl_1 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p1, dpp_ctrl0);
7025 ddx_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl1);
7026 ddy_1 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p1, tl_1, dpp_ctrl2);
7027 Temp tl_2 = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), p2, dpp_ctrl0);
7028 ddx_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl1);
7029 ddy_2 = bld.vop2_dpp(aco_opcode::v_sub_f32, bld.def(v1), p2, tl_2, dpp_ctrl2);
7030 } else {
7031 Temp tl_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl0);
7032 ddx_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl1);
7033 ddx_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_1, tl_1);
7034 ddx_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p1, (1 << 15) | dpp_ctrl2);
7035 ddx_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddx_2, tl_1);
7036 Temp tl_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl0);
7037 ddy_1 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl1);
7038 ddy_1 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_1, tl_2);
7039 ddy_2 = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), p2, (1 << 15) | dpp_ctrl2);
7040 ddy_2 = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), ddy_2, tl_2);
7041 }
7042
7043 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7044 Temp tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_1, pos1, p1);
7045 Temp tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddx_2, pos1, p2);
7046 tmp1 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_1, pos2, tmp1);
7047 tmp2 = bld.vop3(aco_opcode::v_mad_f32, bld.def(v1), ddy_2, pos2, tmp2);
7048 Temp wqm1 = bld.tmp(v1);
7049 emit_wqm(ctx, tmp1, wqm1, true);
7050 Temp wqm2 = bld.tmp(v1);
7051 emit_wqm(ctx, tmp2, wqm2, true);
7052 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), wqm1, wqm2);
7053 return;
7054 }
7055
7056 void visit_intrinsic(isel_context *ctx, nir_intrinsic_instr *instr)
7057 {
7058 Builder bld(ctx->program, ctx->block);
7059 switch(instr->intrinsic) {
7060 case nir_intrinsic_load_barycentric_sample:
7061 case nir_intrinsic_load_barycentric_pixel:
7062 case nir_intrinsic_load_barycentric_centroid: {
7063 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(instr);
7064 Temp bary = Temp(0, s2);
7065 switch (mode) {
7066 case INTERP_MODE_SMOOTH:
7067 case INTERP_MODE_NONE:
7068 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7069 bary = get_arg(ctx, ctx->args->ac.persp_center);
7070 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7071 bary = ctx->persp_centroid;
7072 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7073 bary = get_arg(ctx, ctx->args->ac.persp_sample);
7074 break;
7075 case INTERP_MODE_NOPERSPECTIVE:
7076 if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel)
7077 bary = get_arg(ctx, ctx->args->ac.linear_center);
7078 else if (instr->intrinsic == nir_intrinsic_load_barycentric_centroid)
7079 bary = ctx->linear_centroid;
7080 else if (instr->intrinsic == nir_intrinsic_load_barycentric_sample)
7081 bary = get_arg(ctx, ctx->args->ac.linear_sample);
7082 break;
7083 default:
7084 break;
7085 }
7086 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7087 Temp p1 = emit_extract_vector(ctx, bary, 0, v1);
7088 Temp p2 = emit_extract_vector(ctx, bary, 1, v1);
7089 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7090 Operand(p1), Operand(p2));
7091 emit_split_vector(ctx, dst, 2);
7092 break;
7093 }
7094 case nir_intrinsic_load_barycentric_model: {
7095 Temp model = get_arg(ctx, ctx->args->ac.pull_model);
7096
7097 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7098 Temp p1 = emit_extract_vector(ctx, model, 0, v1);
7099 Temp p2 = emit_extract_vector(ctx, model, 1, v1);
7100 Temp p3 = emit_extract_vector(ctx, model, 2, v1);
7101 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7102 Operand(p1), Operand(p2), Operand(p3));
7103 emit_split_vector(ctx, dst, 3);
7104 break;
7105 }
7106 case nir_intrinsic_load_barycentric_at_sample: {
7107 uint32_t sample_pos_offset = RING_PS_SAMPLE_POSITIONS * 16;
7108 switch (ctx->options->key.fs.num_samples) {
7109 case 2: sample_pos_offset += 1 << 3; break;
7110 case 4: sample_pos_offset += 3 << 3; break;
7111 case 8: sample_pos_offset += 7 << 3; break;
7112 default: break;
7113 }
7114 Temp sample_pos;
7115 Temp addr = get_ssa_temp(ctx, instr->src[0].ssa);
7116 nir_const_value* const_addr = nir_src_as_const_value(instr->src[0]);
7117 Temp private_segment_buffer = ctx->program->private_segment_buffer;
7118 if (addr.type() == RegType::sgpr) {
7119 Operand offset;
7120 if (const_addr) {
7121 sample_pos_offset += const_addr->u32 << 3;
7122 offset = Operand(sample_pos_offset);
7123 } else if (ctx->options->chip_class >= GFX9) {
7124 offset = bld.sop2(aco_opcode::s_lshl3_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7125 } else {
7126 offset = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), addr, Operand(3u));
7127 offset = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), bld.def(s1, scc), addr, Operand(sample_pos_offset));
7128 }
7129
7130 Operand off = bld.copy(bld.def(s1), Operand(offset));
7131 sample_pos = bld.smem(aco_opcode::s_load_dwordx2, bld.def(s2), private_segment_buffer, off);
7132
7133 } else if (ctx->options->chip_class >= GFX9) {
7134 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7135 sample_pos = bld.global(aco_opcode::global_load_dwordx2, bld.def(v2), addr, private_segment_buffer, sample_pos_offset);
7136 } else if (ctx->options->chip_class >= GFX7) {
7137 /* addr += private_segment_buffer + sample_pos_offset */
7138 Temp tmp0 = bld.tmp(s1);
7139 Temp tmp1 = bld.tmp(s1);
7140 bld.pseudo(aco_opcode::p_split_vector, Definition(tmp0), Definition(tmp1), private_segment_buffer);
7141 Definition scc_tmp = bld.def(s1, scc);
7142 tmp0 = bld.sop2(aco_opcode::s_add_u32, bld.def(s1), scc_tmp, tmp0, Operand(sample_pos_offset));
7143 tmp1 = bld.sop2(aco_opcode::s_addc_u32, bld.def(s1), bld.def(s1, scc), tmp1, Operand(0u), bld.scc(scc_tmp.getTemp()));
7144 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7145 Temp pck0 = bld.tmp(v1);
7146 Temp carry = bld.vadd32(Definition(pck0), tmp0, addr, true).def(1).getTemp();
7147 tmp1 = as_vgpr(ctx, tmp1);
7148 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);
7149 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), pck0, pck1);
7150
7151 /* sample_pos = flat_load_dwordx2 addr */
7152 sample_pos = bld.flat(aco_opcode::flat_load_dwordx2, bld.def(v2), addr, Operand(s1));
7153 } else {
7154 assert(ctx->options->chip_class == GFX6);
7155
7156 uint32_t rsrc_conf = S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
7157 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
7158 Temp rsrc = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4), private_segment_buffer, Operand(0u), Operand(rsrc_conf));
7159
7160 addr = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(3u), addr);
7161 addr = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), addr, Operand(0u));
7162
7163 sample_pos = bld.tmp(v2);
7164
7165 aco_ptr<MUBUF_instruction> load{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dwordx2, Format::MUBUF, 3, 1)};
7166 load->definitions[0] = Definition(sample_pos);
7167 load->operands[0] = Operand(rsrc);
7168 load->operands[1] = Operand(addr);
7169 load->operands[2] = Operand(0u);
7170 load->offset = sample_pos_offset;
7171 load->offen = 0;
7172 load->addr64 = true;
7173 load->glc = false;
7174 load->dlc = false;
7175 load->disable_wqm = false;
7176 load->barrier = barrier_none;
7177 load->can_reorder = true;
7178 ctx->block->instructions.emplace_back(std::move(load));
7179 }
7180
7181 /* sample_pos -= 0.5 */
7182 Temp pos1 = bld.tmp(RegClass(sample_pos.type(), 1));
7183 Temp pos2 = bld.tmp(RegClass(sample_pos.type(), 1));
7184 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), sample_pos);
7185 pos1 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos1, Operand(0x3f000000u));
7186 pos2 = bld.vop2_e64(aco_opcode::v_sub_f32, bld.def(v1), pos2, Operand(0x3f000000u));
7187
7188 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7189 break;
7190 }
7191 case nir_intrinsic_load_barycentric_at_offset: {
7192 Temp offset = get_ssa_temp(ctx, instr->src[0].ssa);
7193 RegClass rc = RegClass(offset.type(), 1);
7194 Temp pos1 = bld.tmp(rc), pos2 = bld.tmp(rc);
7195 bld.pseudo(aco_opcode::p_split_vector, Definition(pos1), Definition(pos2), offset);
7196 emit_interp_center(ctx, get_ssa_temp(ctx, &instr->dest.ssa), pos1, pos2);
7197 break;
7198 }
7199 case nir_intrinsic_load_front_face: {
7200 bld.vopc(aco_opcode::v_cmp_lg_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7201 Operand(0u), get_arg(ctx, ctx->args->ac.front_face)).def(0).setHint(vcc);
7202 break;
7203 }
7204 case nir_intrinsic_load_view_index: {
7205 if (ctx->stage & (sw_vs | sw_gs | sw_tcs | sw_tes)) {
7206 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7207 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.view_index)));
7208 break;
7209 }
7210
7211 /* fallthrough */
7212 }
7213 case nir_intrinsic_load_layer_id: {
7214 unsigned idx = nir_intrinsic_base(instr);
7215 bld.vintrp(aco_opcode::v_interp_mov_f32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7216 Operand(2u), bld.m0(get_arg(ctx, ctx->args->ac.prim_mask)), idx, 0);
7217 break;
7218 }
7219 case nir_intrinsic_load_frag_coord: {
7220 emit_load_frag_coord(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 4);
7221 break;
7222 }
7223 case nir_intrinsic_load_sample_pos: {
7224 Temp posx = get_arg(ctx, ctx->args->ac.frag_pos[0]);
7225 Temp posy = get_arg(ctx, ctx->args->ac.frag_pos[1]);
7226 bld.pseudo(aco_opcode::p_create_vector, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7227 posx.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posx) : Operand(0u),
7228 posy.id() ? bld.vop1(aco_opcode::v_fract_f32, bld.def(v1), posy) : Operand(0u));
7229 break;
7230 }
7231 case nir_intrinsic_load_tess_coord:
7232 visit_load_tess_coord(ctx, instr);
7233 break;
7234 case nir_intrinsic_load_interpolated_input:
7235 visit_load_interpolated_input(ctx, instr);
7236 break;
7237 case nir_intrinsic_store_output:
7238 visit_store_output(ctx, instr);
7239 break;
7240 case nir_intrinsic_load_input:
7241 case nir_intrinsic_load_input_vertex:
7242 visit_load_input(ctx, instr);
7243 break;
7244 case nir_intrinsic_load_output:
7245 visit_load_output(ctx, instr);
7246 break;
7247 case nir_intrinsic_load_per_vertex_input:
7248 visit_load_per_vertex_input(ctx, instr);
7249 break;
7250 case nir_intrinsic_load_per_vertex_output:
7251 visit_load_per_vertex_output(ctx, instr);
7252 break;
7253 case nir_intrinsic_store_per_vertex_output:
7254 visit_store_per_vertex_output(ctx, instr);
7255 break;
7256 case nir_intrinsic_load_ubo:
7257 visit_load_ubo(ctx, instr);
7258 break;
7259 case nir_intrinsic_load_push_constant:
7260 visit_load_push_constant(ctx, instr);
7261 break;
7262 case nir_intrinsic_load_constant:
7263 visit_load_constant(ctx, instr);
7264 break;
7265 case nir_intrinsic_vulkan_resource_index:
7266 visit_load_resource(ctx, instr);
7267 break;
7268 case nir_intrinsic_discard:
7269 visit_discard(ctx, instr);
7270 break;
7271 case nir_intrinsic_discard_if:
7272 visit_discard_if(ctx, instr);
7273 break;
7274 case nir_intrinsic_load_shared:
7275 visit_load_shared(ctx, instr);
7276 break;
7277 case nir_intrinsic_store_shared:
7278 visit_store_shared(ctx, instr);
7279 break;
7280 case nir_intrinsic_shared_atomic_add:
7281 case nir_intrinsic_shared_atomic_imin:
7282 case nir_intrinsic_shared_atomic_umin:
7283 case nir_intrinsic_shared_atomic_imax:
7284 case nir_intrinsic_shared_atomic_umax:
7285 case nir_intrinsic_shared_atomic_and:
7286 case nir_intrinsic_shared_atomic_or:
7287 case nir_intrinsic_shared_atomic_xor:
7288 case nir_intrinsic_shared_atomic_exchange:
7289 case nir_intrinsic_shared_atomic_comp_swap:
7290 visit_shared_atomic(ctx, instr);
7291 break;
7292 case nir_intrinsic_image_deref_load:
7293 visit_image_load(ctx, instr);
7294 break;
7295 case nir_intrinsic_image_deref_store:
7296 visit_image_store(ctx, instr);
7297 break;
7298 case nir_intrinsic_image_deref_atomic_add:
7299 case nir_intrinsic_image_deref_atomic_umin:
7300 case nir_intrinsic_image_deref_atomic_imin:
7301 case nir_intrinsic_image_deref_atomic_umax:
7302 case nir_intrinsic_image_deref_atomic_imax:
7303 case nir_intrinsic_image_deref_atomic_and:
7304 case nir_intrinsic_image_deref_atomic_or:
7305 case nir_intrinsic_image_deref_atomic_xor:
7306 case nir_intrinsic_image_deref_atomic_exchange:
7307 case nir_intrinsic_image_deref_atomic_comp_swap:
7308 visit_image_atomic(ctx, instr);
7309 break;
7310 case nir_intrinsic_image_deref_size:
7311 visit_image_size(ctx, instr);
7312 break;
7313 case nir_intrinsic_load_ssbo:
7314 visit_load_ssbo(ctx, instr);
7315 break;
7316 case nir_intrinsic_store_ssbo:
7317 visit_store_ssbo(ctx, instr);
7318 break;
7319 case nir_intrinsic_load_global:
7320 visit_load_global(ctx, instr);
7321 break;
7322 case nir_intrinsic_store_global:
7323 visit_store_global(ctx, instr);
7324 break;
7325 case nir_intrinsic_global_atomic_add:
7326 case nir_intrinsic_global_atomic_imin:
7327 case nir_intrinsic_global_atomic_umin:
7328 case nir_intrinsic_global_atomic_imax:
7329 case nir_intrinsic_global_atomic_umax:
7330 case nir_intrinsic_global_atomic_and:
7331 case nir_intrinsic_global_atomic_or:
7332 case nir_intrinsic_global_atomic_xor:
7333 case nir_intrinsic_global_atomic_exchange:
7334 case nir_intrinsic_global_atomic_comp_swap:
7335 visit_global_atomic(ctx, instr);
7336 break;
7337 case nir_intrinsic_ssbo_atomic_add:
7338 case nir_intrinsic_ssbo_atomic_imin:
7339 case nir_intrinsic_ssbo_atomic_umin:
7340 case nir_intrinsic_ssbo_atomic_imax:
7341 case nir_intrinsic_ssbo_atomic_umax:
7342 case nir_intrinsic_ssbo_atomic_and:
7343 case nir_intrinsic_ssbo_atomic_or:
7344 case nir_intrinsic_ssbo_atomic_xor:
7345 case nir_intrinsic_ssbo_atomic_exchange:
7346 case nir_intrinsic_ssbo_atomic_comp_swap:
7347 visit_atomic_ssbo(ctx, instr);
7348 break;
7349 case nir_intrinsic_load_scratch:
7350 visit_load_scratch(ctx, instr);
7351 break;
7352 case nir_intrinsic_store_scratch:
7353 visit_store_scratch(ctx, instr);
7354 break;
7355 case nir_intrinsic_get_buffer_size:
7356 visit_get_buffer_size(ctx, instr);
7357 break;
7358 case nir_intrinsic_control_barrier: {
7359 if (ctx->program->chip_class == GFX6 && ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7360 /* GFX6 only (thanks to a hw bug workaround):
7361 * The real barrier instruction isn’t needed, because an entire patch
7362 * always fits into a single wave.
7363 */
7364 break;
7365 }
7366
7367 if (ctx->program->workgroup_size > ctx->program->wave_size)
7368 bld.sopp(aco_opcode::s_barrier);
7369
7370 break;
7371 }
7372 case nir_intrinsic_memory_barrier_tcs_patch:
7373 case nir_intrinsic_group_memory_barrier:
7374 case nir_intrinsic_memory_barrier:
7375 case nir_intrinsic_memory_barrier_buffer:
7376 case nir_intrinsic_memory_barrier_image:
7377 case nir_intrinsic_memory_barrier_shared:
7378 emit_memory_barrier(ctx, instr);
7379 break;
7380 case nir_intrinsic_load_num_work_groups: {
7381 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7382 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.num_work_groups)));
7383 emit_split_vector(ctx, dst, 3);
7384 break;
7385 }
7386 case nir_intrinsic_load_local_invocation_id: {
7387 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7388 bld.copy(Definition(dst), Operand(get_arg(ctx, ctx->args->ac.local_invocation_ids)));
7389 emit_split_vector(ctx, dst, 3);
7390 break;
7391 }
7392 case nir_intrinsic_load_work_group_id: {
7393 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7394 struct ac_arg *args = ctx->args->ac.workgroup_ids;
7395 bld.pseudo(aco_opcode::p_create_vector, Definition(dst),
7396 args[0].used ? Operand(get_arg(ctx, args[0])) : Operand(0u),
7397 args[1].used ? Operand(get_arg(ctx, args[1])) : Operand(0u),
7398 args[2].used ? Operand(get_arg(ctx, args[2])) : Operand(0u));
7399 emit_split_vector(ctx, dst, 3);
7400 break;
7401 }
7402 case nir_intrinsic_load_local_invocation_index: {
7403 Temp id = emit_mbcnt(ctx, bld.def(v1));
7404
7405 /* The tg_size bits [6:11] contain the subgroup id,
7406 * we need this multiplied by the wave size, and then OR the thread id to it.
7407 */
7408 if (ctx->program->wave_size == 64) {
7409 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7410 Temp tg_num = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), Operand(0xfc0u),
7411 get_arg(ctx, ctx->args->ac.tg_size));
7412 bld.vop2(aco_opcode::v_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, id);
7413 } else {
7414 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7415 Temp tg_num = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
7416 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7417 bld.vop3(aco_opcode::v_lshl_or_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), tg_num, Operand(0x5u), id);
7418 }
7419 break;
7420 }
7421 case nir_intrinsic_load_subgroup_id: {
7422 if (ctx->stage == compute_cs) {
7423 bld.sop2(aco_opcode::s_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc),
7424 get_arg(ctx, ctx->args->ac.tg_size), Operand(0x6u | (0x6u << 16)));
7425 } else {
7426 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x0u));
7427 }
7428 break;
7429 }
7430 case nir_intrinsic_load_subgroup_invocation: {
7431 emit_mbcnt(ctx, Definition(get_ssa_temp(ctx, &instr->dest.ssa)));
7432 break;
7433 }
7434 case nir_intrinsic_load_num_subgroups: {
7435 if (ctx->stage == compute_cs)
7436 bld.sop2(aco_opcode::s_and_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), bld.def(s1, scc), Operand(0x3fu),
7437 get_arg(ctx, ctx->args->ac.tg_size));
7438 else
7439 bld.sop1(aco_opcode::s_mov_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), Operand(0x1u));
7440 break;
7441 }
7442 case nir_intrinsic_ballot: {
7443 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7444 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7445 Definition tmp = bld.def(dst.regClass());
7446 Definition lanemask_tmp = dst.size() == bld.lm.size() ? tmp : bld.def(src.regClass());
7447 if (instr->src[0].ssa->bit_size == 1) {
7448 assert(src.regClass() == bld.lm);
7449 bld.sop2(Builder::s_and, lanemask_tmp, bld.def(s1, scc), Operand(exec, bld.lm), src);
7450 } else if (instr->src[0].ssa->bit_size == 32 && src.regClass() == v1) {
7451 bld.vopc(aco_opcode::v_cmp_lg_u32, lanemask_tmp, Operand(0u), src);
7452 } else if (instr->src[0].ssa->bit_size == 64 && src.regClass() == v2) {
7453 bld.vopc(aco_opcode::v_cmp_lg_u64, lanemask_tmp, Operand(0u), src);
7454 } else {
7455 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7456 nir_print_instr(&instr->instr, stderr);
7457 fprintf(stderr, "\n");
7458 }
7459 if (dst.size() != bld.lm.size()) {
7460 /* Wave32 with ballot size set to 64 */
7461 bld.pseudo(aco_opcode::p_create_vector, Definition(tmp), lanemask_tmp.getTemp(), Operand(0u));
7462 }
7463 emit_wqm(ctx, tmp.getTemp(), dst);
7464 break;
7465 }
7466 case nir_intrinsic_shuffle:
7467 case nir_intrinsic_read_invocation: {
7468 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7469 if (!ctx->divergent_vals[instr->src[0].ssa->index]) {
7470 emit_uniform_subgroup(ctx, instr, src);
7471 } else {
7472 Temp tid = get_ssa_temp(ctx, instr->src[1].ssa);
7473 if (instr->intrinsic == nir_intrinsic_read_invocation || !ctx->divergent_vals[instr->src[1].ssa->index])
7474 tid = bld.as_uniform(tid);
7475 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7476 if (src.regClass() == v1) {
7477 emit_wqm(ctx, emit_bpermute(ctx, bld, tid, src), dst);
7478 } else if (src.regClass() == v2) {
7479 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7480 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7481 lo = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, lo));
7482 hi = emit_wqm(ctx, emit_bpermute(ctx, bld, tid, hi));
7483 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7484 emit_split_vector(ctx, dst, 2);
7485 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == s1) {
7486 assert(src.regClass() == bld.lm);
7487 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src, tid);
7488 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7489 } else if (instr->dest.ssa.bit_size == 1 && tid.regClass() == v1) {
7490 assert(src.regClass() == bld.lm);
7491 Temp tmp;
7492 if (ctx->program->chip_class <= GFX7)
7493 tmp = bld.vop3(aco_opcode::v_lshr_b64, bld.def(v2), src, tid);
7494 else if (ctx->program->wave_size == 64)
7495 tmp = bld.vop3(aco_opcode::v_lshrrev_b64, bld.def(v2), tid, src);
7496 else
7497 tmp = bld.vop2_e64(aco_opcode::v_lshrrev_b32, bld.def(v1), tid, src);
7498 tmp = emit_extract_vector(ctx, tmp, 0, v1);
7499 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(1u), tmp);
7500 emit_wqm(ctx, bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), tmp), dst);
7501 } else {
7502 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7503 nir_print_instr(&instr->instr, stderr);
7504 fprintf(stderr, "\n");
7505 }
7506 }
7507 break;
7508 }
7509 case nir_intrinsic_load_sample_id: {
7510 bld.vop3(aco_opcode::v_bfe_u32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
7511 get_arg(ctx, ctx->args->ac.ancillary), Operand(8u), Operand(4u));
7512 break;
7513 }
7514 case nir_intrinsic_load_sample_mask_in: {
7515 visit_load_sample_mask_in(ctx, instr);
7516 break;
7517 }
7518 case nir_intrinsic_read_first_invocation: {
7519 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7520 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7521 if (src.regClass() == v1) {
7522 emit_wqm(ctx,
7523 bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), src),
7524 dst);
7525 } else if (src.regClass() == v2) {
7526 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7527 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7528 lo = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), lo));
7529 hi = emit_wqm(ctx, bld.vop1(aco_opcode::v_readfirstlane_b32, bld.def(s1), hi));
7530 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7531 emit_split_vector(ctx, dst, 2);
7532 } else if (instr->dest.ssa.bit_size == 1) {
7533 assert(src.regClass() == bld.lm);
7534 Temp tmp = bld.sopc(Builder::s_bitcmp1, bld.def(s1, scc), src,
7535 bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)));
7536 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7537 } else if (src.regClass() == s1) {
7538 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), src);
7539 } else if (src.regClass() == s2) {
7540 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), src);
7541 } else {
7542 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7543 nir_print_instr(&instr->instr, stderr);
7544 fprintf(stderr, "\n");
7545 }
7546 break;
7547 }
7548 case nir_intrinsic_vote_all: {
7549 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7550 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7551 assert(src.regClass() == bld.lm);
7552 assert(dst.regClass() == bld.lm);
7553
7554 Temp tmp = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), Operand(exec, bld.lm), src).def(1).getTemp();
7555 Temp cond = bool_to_vector_condition(ctx, emit_wqm(ctx, tmp));
7556 bld.sop1(Builder::s_not, Definition(dst), bld.def(s1, scc), cond);
7557 break;
7558 }
7559 case nir_intrinsic_vote_any: {
7560 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7561 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7562 assert(src.regClass() == bld.lm);
7563 assert(dst.regClass() == bld.lm);
7564
7565 Temp tmp = bool_to_scalar_condition(ctx, src);
7566 bool_to_vector_condition(ctx, emit_wqm(ctx, tmp), dst);
7567 break;
7568 }
7569 case nir_intrinsic_reduce:
7570 case nir_intrinsic_inclusive_scan:
7571 case nir_intrinsic_exclusive_scan: {
7572 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7573 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7574 nir_op op = (nir_op) nir_intrinsic_reduction_op(instr);
7575 unsigned cluster_size = instr->intrinsic == nir_intrinsic_reduce ?
7576 nir_intrinsic_cluster_size(instr) : 0;
7577 cluster_size = util_next_power_of_two(MIN2(cluster_size ? cluster_size : ctx->program->wave_size, ctx->program->wave_size));
7578
7579 if (!ctx->divergent_vals[instr->src[0].ssa->index] && (op == nir_op_ior || op == nir_op_iand)) {
7580 emit_uniform_subgroup(ctx, instr, src);
7581 } else if (instr->dest.ssa.bit_size == 1) {
7582 if (op == nir_op_imul || op == nir_op_umin || op == nir_op_imin)
7583 op = nir_op_iand;
7584 else if (op == nir_op_iadd)
7585 op = nir_op_ixor;
7586 else if (op == nir_op_umax || op == nir_op_imax)
7587 op = nir_op_ior;
7588 assert(op == nir_op_iand || op == nir_op_ior || op == nir_op_ixor);
7589
7590 switch (instr->intrinsic) {
7591 case nir_intrinsic_reduce:
7592 emit_wqm(ctx, emit_boolean_reduce(ctx, op, cluster_size, src), dst);
7593 break;
7594 case nir_intrinsic_exclusive_scan:
7595 emit_wqm(ctx, emit_boolean_exclusive_scan(ctx, op, src), dst);
7596 break;
7597 case nir_intrinsic_inclusive_scan:
7598 emit_wqm(ctx, emit_boolean_inclusive_scan(ctx, op, src), dst);
7599 break;
7600 default:
7601 assert(false);
7602 }
7603 } else if (cluster_size == 1) {
7604 bld.copy(Definition(dst), src);
7605 } else {
7606 src = as_vgpr(ctx, src);
7607
7608 ReduceOp reduce_op;
7609 switch (op) {
7610 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7611 CASE(iadd)
7612 CASE(imul)
7613 CASE(fadd)
7614 CASE(fmul)
7615 CASE(imin)
7616 CASE(umin)
7617 CASE(fmin)
7618 CASE(imax)
7619 CASE(umax)
7620 CASE(fmax)
7621 CASE(iand)
7622 CASE(ior)
7623 CASE(ixor)
7624 default:
7625 unreachable("unknown reduction op");
7626 #undef CASE
7627 }
7628
7629 aco_opcode aco_op;
7630 switch (instr->intrinsic) {
7631 case nir_intrinsic_reduce: aco_op = aco_opcode::p_reduce; break;
7632 case nir_intrinsic_inclusive_scan: aco_op = aco_opcode::p_inclusive_scan; break;
7633 case nir_intrinsic_exclusive_scan: aco_op = aco_opcode::p_exclusive_scan; break;
7634 default:
7635 unreachable("unknown reduce intrinsic");
7636 }
7637
7638 aco_ptr<Pseudo_reduction_instruction> reduce{create_instruction<Pseudo_reduction_instruction>(aco_op, Format::PSEUDO_REDUCTION, 3, 5)};
7639 reduce->operands[0] = Operand(src);
7640 // filled in by aco_reduce_assign.cpp, used internally as part of the
7641 // reduce sequence
7642 assert(dst.size() == 1 || dst.size() == 2);
7643 reduce->operands[1] = Operand(RegClass(RegType::vgpr, dst.size()).as_linear());
7644 reduce->operands[2] = Operand(v1.as_linear());
7645
7646 Temp tmp_dst = bld.tmp(dst.regClass());
7647 reduce->definitions[0] = Definition(tmp_dst);
7648 reduce->definitions[1] = bld.def(ctx->program->lane_mask); // used internally
7649 reduce->definitions[2] = Definition();
7650 reduce->definitions[3] = Definition(scc, s1);
7651 reduce->definitions[4] = Definition();
7652 reduce->reduce_op = reduce_op;
7653 reduce->cluster_size = cluster_size;
7654 ctx->block->instructions.emplace_back(std::move(reduce));
7655
7656 emit_wqm(ctx, tmp_dst, dst);
7657 }
7658 break;
7659 }
7660 case nir_intrinsic_quad_broadcast: {
7661 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7662 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7663 emit_uniform_subgroup(ctx, instr, src);
7664 } else {
7665 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7666 unsigned lane = nir_src_as_const_value(instr->src[1])->u32;
7667 uint32_t dpp_ctrl = dpp_quad_perm(lane, lane, lane, lane);
7668
7669 if (instr->dest.ssa.bit_size == 1) {
7670 assert(src.regClass() == bld.lm);
7671 assert(dst.regClass() == bld.lm);
7672 uint32_t half_mask = 0x11111111u << lane;
7673 Temp mask_tmp = bld.pseudo(aco_opcode::p_create_vector, bld.def(s2), Operand(half_mask), Operand(half_mask));
7674 Temp tmp = bld.tmp(bld.lm);
7675 bld.sop1(Builder::s_wqm, Definition(tmp),
7676 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), mask_tmp,
7677 bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm))));
7678 emit_wqm(ctx, tmp, dst);
7679 } else if (instr->dest.ssa.bit_size == 32) {
7680 if (ctx->program->chip_class >= GFX8)
7681 emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl), dst);
7682 else
7683 emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, (1 << 15) | dpp_ctrl), dst);
7684 } else if (instr->dest.ssa.bit_size == 64) {
7685 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7686 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7687 if (ctx->program->chip_class >= GFX8) {
7688 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7689 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7690 } else {
7691 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, (1 << 15) | dpp_ctrl));
7692 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, (1 << 15) | dpp_ctrl));
7693 }
7694 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7695 emit_split_vector(ctx, dst, 2);
7696 } else {
7697 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7698 nir_print_instr(&instr->instr, stderr);
7699 fprintf(stderr, "\n");
7700 }
7701 }
7702 break;
7703 }
7704 case nir_intrinsic_quad_swap_horizontal:
7705 case nir_intrinsic_quad_swap_vertical:
7706 case nir_intrinsic_quad_swap_diagonal:
7707 case nir_intrinsic_quad_swizzle_amd: {
7708 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7709 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7710 emit_uniform_subgroup(ctx, instr, src);
7711 break;
7712 }
7713 uint16_t dpp_ctrl = 0;
7714 switch (instr->intrinsic) {
7715 case nir_intrinsic_quad_swap_horizontal:
7716 dpp_ctrl = dpp_quad_perm(1, 0, 3, 2);
7717 break;
7718 case nir_intrinsic_quad_swap_vertical:
7719 dpp_ctrl = dpp_quad_perm(2, 3, 0, 1);
7720 break;
7721 case nir_intrinsic_quad_swap_diagonal:
7722 dpp_ctrl = dpp_quad_perm(3, 2, 1, 0);
7723 break;
7724 case nir_intrinsic_quad_swizzle_amd:
7725 dpp_ctrl = nir_intrinsic_swizzle_mask(instr);
7726 break;
7727 default:
7728 break;
7729 }
7730 if (ctx->program->chip_class < GFX8)
7731 dpp_ctrl |= (1 << 15);
7732
7733 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7734 if (instr->dest.ssa.bit_size == 1) {
7735 assert(src.regClass() == bld.lm);
7736 src = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), Operand(0u), Operand((uint32_t)-1), src);
7737 if (ctx->program->chip_class >= GFX8)
7738 src = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7739 else
7740 src = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7741 Temp tmp = bld.vopc(aco_opcode::v_cmp_lg_u32, bld.def(bld.lm), Operand(0u), src);
7742 emit_wqm(ctx, tmp, dst);
7743 } else if (instr->dest.ssa.bit_size == 32) {
7744 Temp tmp;
7745 if (ctx->program->chip_class >= GFX8)
7746 tmp = bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), src, dpp_ctrl);
7747 else
7748 tmp = bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, dpp_ctrl);
7749 emit_wqm(ctx, tmp, dst);
7750 } else if (instr->dest.ssa.bit_size == 64) {
7751 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7752 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7753 if (ctx->program->chip_class >= GFX8) {
7754 lo = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), lo, dpp_ctrl));
7755 hi = emit_wqm(ctx, bld.vop1_dpp(aco_opcode::v_mov_b32, bld.def(v1), hi, dpp_ctrl));
7756 } else {
7757 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, dpp_ctrl));
7758 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, dpp_ctrl));
7759 }
7760 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7761 emit_split_vector(ctx, dst, 2);
7762 } else {
7763 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7764 nir_print_instr(&instr->instr, stderr);
7765 fprintf(stderr, "\n");
7766 }
7767 break;
7768 }
7769 case nir_intrinsic_masked_swizzle_amd: {
7770 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7771 if (!ctx->divergent_vals[instr->dest.ssa.index]) {
7772 emit_uniform_subgroup(ctx, instr, src);
7773 break;
7774 }
7775 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7776 uint32_t mask = nir_intrinsic_swizzle_mask(instr);
7777 if (dst.regClass() == v1) {
7778 emit_wqm(ctx,
7779 bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), src, mask, 0, false),
7780 dst);
7781 } else if (dst.regClass() == v2) {
7782 Temp lo = bld.tmp(v1), hi = bld.tmp(v1);
7783 bld.pseudo(aco_opcode::p_split_vector, Definition(lo), Definition(hi), src);
7784 lo = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), lo, mask, 0, false));
7785 hi = emit_wqm(ctx, bld.ds(aco_opcode::ds_swizzle_b32, bld.def(v1), hi, mask, 0, false));
7786 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7787 emit_split_vector(ctx, dst, 2);
7788 } else {
7789 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7790 nir_print_instr(&instr->instr, stderr);
7791 fprintf(stderr, "\n");
7792 }
7793 break;
7794 }
7795 case nir_intrinsic_write_invocation_amd: {
7796 Temp src = as_vgpr(ctx, get_ssa_temp(ctx, instr->src[0].ssa));
7797 Temp val = bld.as_uniform(get_ssa_temp(ctx, instr->src[1].ssa));
7798 Temp lane = bld.as_uniform(get_ssa_temp(ctx, instr->src[2].ssa));
7799 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7800 if (dst.regClass() == v1) {
7801 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7802 emit_wqm(ctx, bld.writelane(bld.def(v1), val, lane, src), dst);
7803 } else if (dst.regClass() == v2) {
7804 Temp src_lo = bld.tmp(v1), src_hi = bld.tmp(v1);
7805 Temp val_lo = bld.tmp(s1), val_hi = bld.tmp(s1);
7806 bld.pseudo(aco_opcode::p_split_vector, Definition(src_lo), Definition(src_hi), src);
7807 bld.pseudo(aco_opcode::p_split_vector, Definition(val_lo), Definition(val_hi), val);
7808 Temp lo = emit_wqm(ctx, bld.writelane(bld.def(v1), val_lo, lane, src_hi));
7809 Temp hi = emit_wqm(ctx, bld.writelane(bld.def(v1), val_hi, lane, src_hi));
7810 bld.pseudo(aco_opcode::p_create_vector, Definition(dst), lo, hi);
7811 emit_split_vector(ctx, dst, 2);
7812 } else {
7813 fprintf(stderr, "Unimplemented NIR instr bit size: ");
7814 nir_print_instr(&instr->instr, stderr);
7815 fprintf(stderr, "\n");
7816 }
7817 break;
7818 }
7819 case nir_intrinsic_mbcnt_amd: {
7820 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7821 RegClass rc = RegClass(src.type(), 1);
7822 Temp mask_lo = bld.tmp(rc), mask_hi = bld.tmp(rc);
7823 bld.pseudo(aco_opcode::p_split_vector, Definition(mask_lo), Definition(mask_hi), src);
7824 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7825 Temp wqm_tmp = emit_mbcnt(ctx, bld.def(v1), Operand(mask_lo), Operand(mask_hi));
7826 emit_wqm(ctx, wqm_tmp, dst);
7827 break;
7828 }
7829 case nir_intrinsic_load_helper_invocation: {
7830 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7831 bld.pseudo(aco_opcode::p_load_helper, Definition(dst));
7832 ctx->block->kind |= block_kind_needs_lowering;
7833 ctx->program->needs_exact = true;
7834 break;
7835 }
7836 case nir_intrinsic_is_helper_invocation: {
7837 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7838 bld.pseudo(aco_opcode::p_is_helper, Definition(dst));
7839 ctx->block->kind |= block_kind_needs_lowering;
7840 ctx->program->needs_exact = true;
7841 break;
7842 }
7843 case nir_intrinsic_demote:
7844 bld.pseudo(aco_opcode::p_demote_to_helper, Operand(-1u));
7845
7846 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7847 ctx->cf_info.exec_potentially_empty_discard = true;
7848 ctx->block->kind |= block_kind_uses_demote;
7849 ctx->program->needs_exact = true;
7850 break;
7851 case nir_intrinsic_demote_if: {
7852 Temp src = get_ssa_temp(ctx, instr->src[0].ssa);
7853 assert(src.regClass() == bld.lm);
7854 Temp cond = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), src, Operand(exec, bld.lm));
7855 bld.pseudo(aco_opcode::p_demote_to_helper, cond);
7856
7857 if (ctx->cf_info.loop_nest_depth || ctx->cf_info.parent_if.is_divergent)
7858 ctx->cf_info.exec_potentially_empty_discard = true;
7859 ctx->block->kind |= block_kind_uses_demote;
7860 ctx->program->needs_exact = true;
7861 break;
7862 }
7863 case nir_intrinsic_first_invocation: {
7864 emit_wqm(ctx, bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm)),
7865 get_ssa_temp(ctx, &instr->dest.ssa));
7866 break;
7867 }
7868 case nir_intrinsic_shader_clock:
7869 bld.smem(aco_opcode::s_memtime, Definition(get_ssa_temp(ctx, &instr->dest.ssa)), false);
7870 emit_split_vector(ctx, get_ssa_temp(ctx, &instr->dest.ssa), 2);
7871 break;
7872 case nir_intrinsic_load_vertex_id_zero_base: {
7873 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7874 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.vertex_id));
7875 break;
7876 }
7877 case nir_intrinsic_load_first_vertex: {
7878 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7879 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.base_vertex));
7880 break;
7881 }
7882 case nir_intrinsic_load_base_instance: {
7883 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7884 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.start_instance));
7885 break;
7886 }
7887 case nir_intrinsic_load_instance_id: {
7888 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7889 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.instance_id));
7890 break;
7891 }
7892 case nir_intrinsic_load_draw_id: {
7893 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7894 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.draw_id));
7895 break;
7896 }
7897 case nir_intrinsic_load_invocation_id: {
7898 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7899
7900 if (ctx->shader->info.stage == MESA_SHADER_GEOMETRY) {
7901 if (ctx->options->chip_class >= GFX10)
7902 bld.vop2_e64(aco_opcode::v_and_b32, Definition(dst), Operand(127u), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7903 else
7904 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_invocation_id));
7905 } else if (ctx->shader->info.stage == MESA_SHADER_TESS_CTRL) {
7906 bld.vop3(aco_opcode::v_bfe_u32, Definition(dst),
7907 get_arg(ctx, ctx->args->ac.tcs_rel_ids), Operand(8u), Operand(5u));
7908 } else {
7909 unreachable("Unsupported stage for load_invocation_id");
7910 }
7911
7912 break;
7913 }
7914 case nir_intrinsic_load_primitive_id: {
7915 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7916
7917 switch (ctx->shader->info.stage) {
7918 case MESA_SHADER_GEOMETRY:
7919 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.gs_prim_id));
7920 break;
7921 case MESA_SHADER_TESS_CTRL:
7922 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tcs_patch_id));
7923 break;
7924 case MESA_SHADER_TESS_EVAL:
7925 bld.copy(Definition(dst), get_arg(ctx, ctx->args->ac.tes_patch_id));
7926 break;
7927 default:
7928 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7929 }
7930
7931 break;
7932 }
7933 case nir_intrinsic_load_patch_vertices_in: {
7934 assert(ctx->shader->info.stage == MESA_SHADER_TESS_CTRL ||
7935 ctx->shader->info.stage == MESA_SHADER_TESS_EVAL);
7936
7937 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
7938 bld.copy(Definition(dst), Operand(ctx->args->options->key.tcs.input_vertices));
7939 break;
7940 }
7941 case nir_intrinsic_emit_vertex_with_counter: {
7942 visit_emit_vertex_with_counter(ctx, instr);
7943 break;
7944 }
7945 case nir_intrinsic_end_primitive_with_counter: {
7946 unsigned stream = nir_intrinsic_stream_id(instr);
7947 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx->gs_wave_id), -1, sendmsg_gs(true, false, stream));
7948 break;
7949 }
7950 case nir_intrinsic_set_vertex_count: {
7951 /* unused, the HW keeps track of this for us */
7952 break;
7953 }
7954 default:
7955 fprintf(stderr, "Unimplemented intrinsic instr: ");
7956 nir_print_instr(&instr->instr, stderr);
7957 fprintf(stderr, "\n");
7958 abort();
7959
7960 break;
7961 }
7962 }
7963
7964
7965 void tex_fetch_ptrs(isel_context *ctx, nir_tex_instr *instr,
7966 Temp *res_ptr, Temp *samp_ptr, Temp *fmask_ptr,
7967 enum glsl_base_type *stype)
7968 {
7969 nir_deref_instr *texture_deref_instr = NULL;
7970 nir_deref_instr *sampler_deref_instr = NULL;
7971 int plane = -1;
7972
7973 for (unsigned i = 0; i < instr->num_srcs; i++) {
7974 switch (instr->src[i].src_type) {
7975 case nir_tex_src_texture_deref:
7976 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
7977 break;
7978 case nir_tex_src_sampler_deref:
7979 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
7980 break;
7981 case nir_tex_src_plane:
7982 plane = nir_src_as_int(instr->src[i].src);
7983 break;
7984 default:
7985 break;
7986 }
7987 }
7988
7989 *stype = glsl_get_sampler_result_type(texture_deref_instr->type);
7990
7991 if (!sampler_deref_instr)
7992 sampler_deref_instr = texture_deref_instr;
7993
7994 if (plane >= 0) {
7995 assert(instr->op != nir_texop_txf_ms &&
7996 instr->op != nir_texop_samples_identical);
7997 assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
7998 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, (aco_descriptor_type)(ACO_DESC_PLANE_0 + plane), instr, false, false);
7999 } else if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8000 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_BUFFER, instr, false, false);
8001 } else if (instr->op == nir_texop_fragment_mask_fetch) {
8002 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8003 } else {
8004 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_IMAGE, instr, false, false);
8005 }
8006 if (samp_ptr) {
8007 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, ACO_DESC_SAMPLER, instr, false, false);
8008
8009 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT && ctx->options->chip_class < GFX8) {
8010 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8011 Builder bld(ctx->program, ctx->block);
8012
8013 /* to avoid unnecessary moves, we split and recombine sampler and image */
8014 Temp img[8] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1),
8015 bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8016 Temp samp[4] = {bld.tmp(s1), bld.tmp(s1), bld.tmp(s1), bld.tmp(s1)};
8017 bld.pseudo(aco_opcode::p_split_vector, Definition(img[0]), Definition(img[1]),
8018 Definition(img[2]), Definition(img[3]), Definition(img[4]),
8019 Definition(img[5]), Definition(img[6]), Definition(img[7]), *res_ptr);
8020 bld.pseudo(aco_opcode::p_split_vector, Definition(samp[0]), Definition(samp[1]),
8021 Definition(samp[2]), Definition(samp[3]), *samp_ptr);
8022
8023 samp[0] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), samp[0], img[7]);
8024 *res_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s8),
8025 img[0], img[1], img[2], img[3],
8026 img[4], img[5], img[6], img[7]);
8027 *samp_ptr = bld.pseudo(aco_opcode::p_create_vector, bld.def(s4),
8028 samp[0], samp[1], samp[2], samp[3]);
8029 }
8030 }
8031 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
8032 instr->op == nir_texop_samples_identical))
8033 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, ACO_DESC_FMASK, instr, false, false);
8034 }
8035
8036 void build_cube_select(isel_context *ctx, Temp ma, Temp id, Temp deriv,
8037 Temp *out_ma, Temp *out_sc, Temp *out_tc)
8038 {
8039 Builder bld(ctx->program, ctx->block);
8040
8041 Temp deriv_x = emit_extract_vector(ctx, deriv, 0, v1);
8042 Temp deriv_y = emit_extract_vector(ctx, deriv, 1, v1);
8043 Temp deriv_z = emit_extract_vector(ctx, deriv, 2, v1);
8044
8045 Operand neg_one(0xbf800000u);
8046 Operand one(0x3f800000u);
8047 Operand two(0x40000000u);
8048 Operand four(0x40800000u);
8049
8050 Temp is_ma_positive = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), ma);
8051 Temp sgn_ma = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, one, is_ma_positive);
8052 Temp neg_sgn_ma = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1), Operand(0u), sgn_ma);
8053
8054 Temp is_ma_z = bld.vopc(aco_opcode::v_cmp_le_f32, bld.hint_vcc(bld.def(bld.lm)), four, id);
8055 Temp is_ma_y = bld.vopc(aco_opcode::v_cmp_le_f32, bld.def(bld.lm), two, id);
8056 is_ma_y = bld.sop2(Builder::s_andn2, bld.hint_vcc(bld.def(bld.lm)), is_ma_y, is_ma_z);
8057 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);
8058
8059 // select sc
8060 Temp tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_z, deriv_x, is_not_ma_x);
8061 Temp sgn = bld.vop2_e64(aco_opcode::v_cndmask_b32, bld.def(v1),
8062 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_sgn_ma, sgn_ma, is_ma_z),
8063 one, is_ma_y);
8064 *out_sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8065
8066 // select tc
8067 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_y, deriv_z, is_ma_y);
8068 sgn = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), neg_one, sgn_ma, is_ma_y);
8069 *out_tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tmp, sgn);
8070
8071 // select ma
8072 tmp = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8073 bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), deriv_x, deriv_y, is_ma_y),
8074 deriv_z, is_ma_z);
8075 tmp = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x7fffffffu), tmp);
8076 *out_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), two, tmp);
8077 }
8078
8079 void prepare_cube_coords(isel_context *ctx, std::vector<Temp>& coords, Temp* ddx, Temp* ddy, bool is_deriv, bool is_array)
8080 {
8081 Builder bld(ctx->program, ctx->block);
8082 Temp ma, tc, sc, id;
8083
8084 if (is_array) {
8085 coords[3] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[3]);
8086
8087 // see comment in ac_prepare_cube_coords()
8088 if (ctx->options->chip_class <= GFX8)
8089 coords[3] = bld.vop2(aco_opcode::v_max_f32, bld.def(v1), Operand(0u), coords[3]);
8090 }
8091
8092 ma = bld.vop3(aco_opcode::v_cubema_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8093
8094 aco_ptr<VOP3A_instruction> vop3a{create_instruction<VOP3A_instruction>(aco_opcode::v_rcp_f32, asVOP3(Format::VOP1), 1, 1)};
8095 vop3a->operands[0] = Operand(ma);
8096 vop3a->abs[0] = true;
8097 Temp invma = bld.tmp(v1);
8098 vop3a->definitions[0] = Definition(invma);
8099 ctx->block->instructions.emplace_back(std::move(vop3a));
8100
8101 sc = bld.vop3(aco_opcode::v_cubesc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8102 if (!is_deriv)
8103 sc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), sc, invma, Operand(0x3fc00000u/*1.5*/));
8104
8105 tc = bld.vop3(aco_opcode::v_cubetc_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8106 if (!is_deriv)
8107 tc = bld.vop2(aco_opcode::v_madak_f32, bld.def(v1), tc, invma, Operand(0x3fc00000u/*1.5*/));
8108
8109 id = bld.vop3(aco_opcode::v_cubeid_f32, bld.def(v1), coords[0], coords[1], coords[2]);
8110
8111 if (is_deriv) {
8112 sc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), sc, invma);
8113 tc = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), tc, invma);
8114
8115 for (unsigned i = 0; i < 2; i++) {
8116 // see comment in ac_prepare_cube_coords()
8117 Temp deriv_ma;
8118 Temp deriv_sc, deriv_tc;
8119 build_cube_select(ctx, ma, id, i ? *ddy : *ddx,
8120 &deriv_ma, &deriv_sc, &deriv_tc);
8121
8122 deriv_ma = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, invma);
8123
8124 Temp x = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8125 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_sc, invma),
8126 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, sc));
8127 Temp y = bld.vop2(aco_opcode::v_sub_f32, bld.def(v1),
8128 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_tc, invma),
8129 bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), deriv_ma, tc));
8130 *(i ? ddy : ddx) = bld.pseudo(aco_opcode::p_create_vector, bld.def(v2), x, y);
8131 }
8132
8133 sc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), sc);
8134 tc = bld.vop2(aco_opcode::v_add_f32, bld.def(v1), Operand(0x3fc00000u/*1.5*/), tc);
8135 }
8136
8137 if (is_array)
8138 id = bld.vop2(aco_opcode::v_madmk_f32, bld.def(v1), coords[3], id, Operand(0x41000000u/*8.0*/));
8139 coords.resize(3);
8140 coords[0] = sc;
8141 coords[1] = tc;
8142 coords[2] = id;
8143 }
8144
8145 void get_const_vec(nir_ssa_def *vec, nir_const_value *cv[4])
8146 {
8147 if (vec->parent_instr->type != nir_instr_type_alu)
8148 return;
8149 nir_alu_instr *vec_instr = nir_instr_as_alu(vec->parent_instr);
8150 if (vec_instr->op != nir_op_vec(vec->num_components))
8151 return;
8152
8153 for (unsigned i = 0; i < vec->num_components; i++) {
8154 cv[i] = vec_instr->src[i].swizzle[0] == 0 ?
8155 nir_src_as_const_value(vec_instr->src[i].src) : NULL;
8156 }
8157 }
8158
8159 void visit_tex(isel_context *ctx, nir_tex_instr *instr)
8160 {
8161 Builder bld(ctx->program, ctx->block);
8162 bool has_bias = false, has_lod = false, level_zero = false, has_compare = false,
8163 has_offset = false, has_ddx = false, has_ddy = false, has_derivs = false, has_sample_index = false;
8164 Temp resource, sampler, fmask_ptr, bias = Temp(), compare = Temp(), sample_index = Temp(),
8165 lod = Temp(), offset = Temp(), ddx = Temp(), ddy = Temp();
8166 std::vector<Temp> coords;
8167 std::vector<Temp> derivs;
8168 nir_const_value *sample_index_cv = NULL;
8169 nir_const_value *const_offset[4] = {NULL, NULL, NULL, NULL};
8170 enum glsl_base_type stype;
8171 tex_fetch_ptrs(ctx, instr, &resource, &sampler, &fmask_ptr, &stype);
8172
8173 bool tg4_integer_workarounds = ctx->options->chip_class <= GFX8 && instr->op == nir_texop_tg4 &&
8174 (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT);
8175 bool tg4_integer_cube_workaround = tg4_integer_workarounds &&
8176 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
8177
8178 for (unsigned i = 0; i < instr->num_srcs; i++) {
8179 switch (instr->src[i].src_type) {
8180 case nir_tex_src_coord: {
8181 Temp coord = get_ssa_temp(ctx, instr->src[i].src.ssa);
8182 for (unsigned i = 0; i < coord.size(); i++)
8183 coords.emplace_back(emit_extract_vector(ctx, coord, i, v1));
8184 break;
8185 }
8186 case nir_tex_src_bias:
8187 if (instr->op == nir_texop_txb) {
8188 bias = get_ssa_temp(ctx, instr->src[i].src.ssa);
8189 has_bias = true;
8190 }
8191 break;
8192 case nir_tex_src_lod: {
8193 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
8194
8195 if (val && val->f32 <= 0.0) {
8196 level_zero = true;
8197 } else {
8198 lod = get_ssa_temp(ctx, instr->src[i].src.ssa);
8199 has_lod = true;
8200 }
8201 break;
8202 }
8203 case nir_tex_src_comparator:
8204 if (instr->is_shadow) {
8205 compare = get_ssa_temp(ctx, instr->src[i].src.ssa);
8206 has_compare = true;
8207 }
8208 break;
8209 case nir_tex_src_offset:
8210 offset = get_ssa_temp(ctx, instr->src[i].src.ssa);
8211 get_const_vec(instr->src[i].src.ssa, const_offset);
8212 has_offset = true;
8213 break;
8214 case nir_tex_src_ddx:
8215 ddx = get_ssa_temp(ctx, instr->src[i].src.ssa);
8216 has_ddx = true;
8217 break;
8218 case nir_tex_src_ddy:
8219 ddy = get_ssa_temp(ctx, instr->src[i].src.ssa);
8220 has_ddy = true;
8221 break;
8222 case nir_tex_src_ms_index:
8223 sample_index = get_ssa_temp(ctx, instr->src[i].src.ssa);
8224 sample_index_cv = nir_src_as_const_value(instr->src[i].src);
8225 has_sample_index = true;
8226 break;
8227 case nir_tex_src_texture_offset:
8228 case nir_tex_src_sampler_offset:
8229 default:
8230 break;
8231 }
8232 }
8233
8234 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
8235 return get_buffer_size(ctx, resource, get_ssa_temp(ctx, &instr->dest.ssa), true);
8236
8237 if (instr->op == nir_texop_texture_samples) {
8238 Temp dword3 = emit_extract_vector(ctx, resource, 3, s1);
8239
8240 Temp samples_log2 = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), dword3, Operand(16u | 4u<<16));
8241 Temp samples = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), Operand(1u), samples_log2);
8242 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 */));
8243 Temp is_msaa = bld.sopc(aco_opcode::s_cmp_ge_u32, bld.def(s1, scc), type, Operand(14u));
8244
8245 bld.sop2(aco_opcode::s_cselect_b32, Definition(get_ssa_temp(ctx, &instr->dest.ssa)),
8246 samples, Operand(1u), bld.scc(is_msaa));
8247 return;
8248 }
8249
8250 if (has_offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
8251 aco_ptr<Instruction> tmp_instr;
8252 Temp acc, pack = Temp();
8253
8254 uint32_t pack_const = 0;
8255 for (unsigned i = 0; i < offset.size(); i++) {
8256 if (!const_offset[i])
8257 continue;
8258 pack_const |= (const_offset[i]->u32 & 0x3Fu) << (8u * i);
8259 }
8260
8261 if (offset.type() == RegType::sgpr) {
8262 for (unsigned i = 0; i < offset.size(); i++) {
8263 if (const_offset[i])
8264 continue;
8265
8266 acc = emit_extract_vector(ctx, offset, i, s1);
8267 acc = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(0x3Fu));
8268
8269 if (i) {
8270 acc = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), acc, Operand(8u * i));
8271 }
8272
8273 if (pack == Temp()) {
8274 pack = acc;
8275 } else {
8276 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), pack, acc);
8277 }
8278 }
8279
8280 if (pack_const && pack != Temp())
8281 pack = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), Operand(pack_const), pack);
8282 } else {
8283 for (unsigned i = 0; i < offset.size(); i++) {
8284 if (const_offset[i])
8285 continue;
8286
8287 acc = emit_extract_vector(ctx, offset, i, v1);
8288 acc = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0x3Fu), acc);
8289
8290 if (i) {
8291 acc = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(8u * i), acc);
8292 }
8293
8294 if (pack == Temp()) {
8295 pack = acc;
8296 } else {
8297 pack = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), pack, acc);
8298 }
8299 }
8300
8301 if (pack_const && pack != Temp())
8302 pack = bld.sop2(aco_opcode::v_or_b32, bld.def(v1), Operand(pack_const), pack);
8303 }
8304 if (pack_const && pack == Temp())
8305 offset = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(pack_const));
8306 else if (pack == Temp())
8307 has_offset = false;
8308 else
8309 offset = pack;
8310 }
8311
8312 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && instr->coord_components)
8313 prepare_cube_coords(ctx, coords, &ddx, &ddy, instr->op == nir_texop_txd, instr->is_array && instr->op != nir_texop_lod);
8314
8315 /* pack derivatives */
8316 if (has_ddx || has_ddy) {
8317 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && ctx->options->chip_class == GFX9) {
8318 assert(has_ddx && has_ddy && ddx.size() == 1 && ddy.size() == 1);
8319 Temp zero = bld.copy(bld.def(v1), Operand(0u));
8320 derivs = {ddy, zero, ddy, zero};
8321 } else {
8322 for (unsigned i = 0; has_ddx && i < ddx.size(); i++)
8323 derivs.emplace_back(emit_extract_vector(ctx, ddx, i, v1));
8324 for (unsigned i = 0; has_ddy && i < ddy.size(); i++)
8325 derivs.emplace_back(emit_extract_vector(ctx, ddy, i, v1));
8326 }
8327 has_derivs = true;
8328 }
8329
8330 if (instr->coord_components > 1 &&
8331 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8332 instr->is_array &&
8333 instr->op != nir_texop_txf)
8334 coords[1] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[1]);
8335
8336 if (instr->coord_components > 2 &&
8337 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
8338 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8339 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
8340 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8341 instr->is_array &&
8342 instr->op != nir_texop_txf &&
8343 instr->op != nir_texop_txf_ms &&
8344 instr->op != nir_texop_fragment_fetch &&
8345 instr->op != nir_texop_fragment_mask_fetch)
8346 coords[2] = bld.vop1(aco_opcode::v_rndne_f32, bld.def(v1), coords[2]);
8347
8348 if (ctx->options->chip_class == GFX9 &&
8349 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8350 instr->op != nir_texop_lod && instr->coord_components) {
8351 assert(coords.size() > 0 && coords.size() < 3);
8352
8353 coords.insert(std::next(coords.begin()), bld.copy(bld.def(v1), instr->op == nir_texop_txf ?
8354 Operand((uint32_t) 0) :
8355 Operand((uint32_t) 0x3f000000)));
8356 }
8357
8358 bool da = should_declare_array(ctx, instr->sampler_dim, instr->is_array);
8359
8360 if (instr->op == nir_texop_samples_identical)
8361 resource = fmask_ptr;
8362
8363 else if ((instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
8364 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
8365 instr->op != nir_texop_txs &&
8366 instr->op != nir_texop_fragment_fetch &&
8367 instr->op != nir_texop_fragment_mask_fetch) {
8368 assert(has_sample_index);
8369 Operand op(sample_index);
8370 if (sample_index_cv)
8371 op = Operand(sample_index_cv->u32);
8372 sample_index = adjust_sample_index_using_fmask(ctx, da, coords, op, fmask_ptr);
8373 }
8374
8375 if (has_offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
8376 for (unsigned i = 0; i < std::min(offset.size(), instr->coord_components); i++) {
8377 Temp off = emit_extract_vector(ctx, offset, i, v1);
8378 coords[i] = bld.vadd32(bld.def(v1), coords[i], off);
8379 }
8380 has_offset = false;
8381 }
8382
8383 /* Build tex instruction */
8384 unsigned dmask = nir_ssa_def_components_read(&instr->dest.ssa);
8385 unsigned dim = ctx->options->chip_class >= GFX10 && instr->sampler_dim != GLSL_SAMPLER_DIM_BUF
8386 ? ac_get_sampler_dim(ctx->options->chip_class, instr->sampler_dim, instr->is_array)
8387 : 0;
8388 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8389 Temp tmp_dst = dst;
8390
8391 /* gather4 selects the component by dmask and always returns vec4 */
8392 if (instr->op == nir_texop_tg4) {
8393 assert(instr->dest.ssa.num_components == 4);
8394 if (instr->is_shadow)
8395 dmask = 1;
8396 else
8397 dmask = 1 << instr->component;
8398 if (tg4_integer_cube_workaround || dst.type() == RegType::sgpr)
8399 tmp_dst = bld.tmp(v4);
8400 } else if (instr->op == nir_texop_samples_identical) {
8401 tmp_dst = bld.tmp(v1);
8402 } else if (util_bitcount(dmask) != instr->dest.ssa.num_components || dst.type() == RegType::sgpr) {
8403 tmp_dst = bld.tmp(RegClass(RegType::vgpr, util_bitcount(dmask)));
8404 }
8405
8406 aco_ptr<MIMG_instruction> tex;
8407 if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels) {
8408 if (!has_lod)
8409 lod = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8410
8411 bool div_by_6 = instr->op == nir_texop_txs &&
8412 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
8413 instr->is_array &&
8414 (dmask & (1 << 2));
8415 if (tmp_dst.id() == dst.id() && div_by_6)
8416 tmp_dst = bld.tmp(tmp_dst.regClass());
8417
8418 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8419 tex->operands[0] = Operand(resource);
8420 tex->operands[1] = Operand(s4); /* no sampler */
8421 tex->operands[2] = Operand(as_vgpr(ctx,lod));
8422 if (ctx->options->chip_class == GFX9 &&
8423 instr->op == nir_texop_txs &&
8424 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
8425 instr->is_array) {
8426 tex->dmask = (dmask & 0x1) | ((dmask & 0x2) << 1);
8427 } else if (instr->op == nir_texop_query_levels) {
8428 tex->dmask = 1 << 3;
8429 } else {
8430 tex->dmask = dmask;
8431 }
8432 tex->da = da;
8433 tex->definitions[0] = Definition(tmp_dst);
8434 tex->dim = dim;
8435 tex->can_reorder = true;
8436 ctx->block->instructions.emplace_back(std::move(tex));
8437
8438 if (div_by_6) {
8439 /* divide 3rd value by 6 by multiplying with magic number */
8440 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8441 Temp c = bld.copy(bld.def(s1), Operand((uint32_t) 0x2AAAAAAB));
8442 Temp by_6 = bld.vop3(aco_opcode::v_mul_hi_i32, bld.def(v1), emit_extract_vector(ctx, tmp_dst, 2, v1), c);
8443 assert(instr->dest.ssa.num_components == 3);
8444 Temp tmp = dst.type() == RegType::vgpr ? dst : bld.tmp(v3);
8445 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8446 emit_extract_vector(ctx, tmp_dst, 0, v1),
8447 emit_extract_vector(ctx, tmp_dst, 1, v1),
8448 by_6);
8449
8450 }
8451
8452 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8453 return;
8454 }
8455
8456 Temp tg4_compare_cube_wa64 = Temp();
8457
8458 if (tg4_integer_workarounds) {
8459 tex.reset(create_instruction<MIMG_instruction>(aco_opcode::image_get_resinfo, Format::MIMG, 3, 1));
8460 tex->operands[0] = Operand(resource);
8461 tex->operands[1] = Operand(s4); /* no sampler */
8462 tex->operands[2] = bld.vop1(aco_opcode::v_mov_b32, bld.def(v1), Operand(0u));
8463 tex->dim = dim;
8464 tex->dmask = 0x3;
8465 tex->da = da;
8466 Temp size = bld.tmp(v2);
8467 tex->definitions[0] = Definition(size);
8468 tex->can_reorder = true;
8469 ctx->block->instructions.emplace_back(std::move(tex));
8470 emit_split_vector(ctx, size, size.size());
8471
8472 Temp half_texel[2];
8473 for (unsigned i = 0; i < 2; i++) {
8474 half_texel[i] = emit_extract_vector(ctx, size, i, v1);
8475 half_texel[i] = bld.vop1(aco_opcode::v_cvt_f32_i32, bld.def(v1), half_texel[i]);
8476 half_texel[i] = bld.vop1(aco_opcode::v_rcp_iflag_f32, bld.def(v1), half_texel[i]);
8477 half_texel[i] = bld.vop2(aco_opcode::v_mul_f32, bld.def(v1), Operand(0xbf000000/*-0.5*/), half_texel[i]);
8478 }
8479
8480 Temp new_coords[2] = {
8481 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[0], half_texel[0]),
8482 bld.vop2(aco_opcode::v_add_f32, bld.def(v1), coords[1], half_texel[1])
8483 };
8484
8485 if (tg4_integer_cube_workaround) {
8486 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8487 Temp desc[resource.size()];
8488 aco_ptr<Instruction> split{create_instruction<Pseudo_instruction>(aco_opcode::p_split_vector,
8489 Format::PSEUDO, 1, resource.size())};
8490 split->operands[0] = Operand(resource);
8491 for (unsigned i = 0; i < resource.size(); i++) {
8492 desc[i] = bld.tmp(s1);
8493 split->definitions[i] = Definition(desc[i]);
8494 }
8495 ctx->block->instructions.emplace_back(std::move(split));
8496
8497 Temp dfmt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc), desc[1], Operand(20u | (6u << 16)));
8498 Temp compare_cube_wa = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), dfmt,
8499 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8));
8500
8501 Temp nfmt;
8502 if (stype == GLSL_TYPE_UINT) {
8503 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8504 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED),
8505 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT),
8506 bld.scc(compare_cube_wa));
8507 } else {
8508 nfmt = bld.sop2(aco_opcode::s_cselect_b32, bld.def(s1),
8509 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED),
8510 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT),
8511 bld.scc(compare_cube_wa));
8512 }
8513 tg4_compare_cube_wa64 = bld.tmp(bld.lm);
8514 bool_to_vector_condition(ctx, compare_cube_wa, tg4_compare_cube_wa64);
8515
8516 nfmt = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), nfmt, Operand(26u));
8517
8518 desc[1] = bld.sop2(aco_opcode::s_and_b32, bld.def(s1), bld.def(s1, scc), desc[1],
8519 Operand((uint32_t)C_008F14_NUM_FORMAT));
8520 desc[1] = bld.sop2(aco_opcode::s_or_b32, bld.def(s1), bld.def(s1, scc), desc[1], nfmt);
8521
8522 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector,
8523 Format::PSEUDO, resource.size(), 1)};
8524 for (unsigned i = 0; i < resource.size(); i++)
8525 vec->operands[i] = Operand(desc[i]);
8526 resource = bld.tmp(resource.regClass());
8527 vec->definitions[0] = Definition(resource);
8528 ctx->block->instructions.emplace_back(std::move(vec));
8529
8530 new_coords[0] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8531 new_coords[0], coords[0], tg4_compare_cube_wa64);
8532 new_coords[1] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
8533 new_coords[1], coords[1], tg4_compare_cube_wa64);
8534 }
8535 coords[0] = new_coords[0];
8536 coords[1] = new_coords[1];
8537 }
8538
8539 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
8540 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8541
8542 assert(coords.size() == 1);
8543 unsigned last_bit = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
8544 aco_opcode op;
8545 switch (last_bit) {
8546 case 1:
8547 op = aco_opcode::buffer_load_format_x; break;
8548 case 2:
8549 op = aco_opcode::buffer_load_format_xy; break;
8550 case 3:
8551 op = aco_opcode::buffer_load_format_xyz; break;
8552 case 4:
8553 op = aco_opcode::buffer_load_format_xyzw; break;
8554 default:
8555 unreachable("Tex instruction loads more than 4 components.");
8556 }
8557
8558 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8559 if (last_bit == instr->dest.ssa.num_components && dst.type() == RegType::vgpr)
8560 tmp_dst = dst;
8561 else
8562 tmp_dst = bld.tmp(RegType::vgpr, last_bit);
8563
8564 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(op, Format::MUBUF, 3, 1)};
8565 mubuf->operands[0] = Operand(resource);
8566 mubuf->operands[1] = Operand(coords[0]);
8567 mubuf->operands[2] = Operand((uint32_t) 0);
8568 mubuf->definitions[0] = Definition(tmp_dst);
8569 mubuf->idxen = true;
8570 mubuf->can_reorder = true;
8571 ctx->block->instructions.emplace_back(std::move(mubuf));
8572
8573 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, (1 << last_bit) - 1);
8574 return;
8575 }
8576
8577 /* gather MIMG address components */
8578 std::vector<Temp> args;
8579 if (has_offset)
8580 args.emplace_back(offset);
8581 if (has_bias)
8582 args.emplace_back(bias);
8583 if (has_compare)
8584 args.emplace_back(compare);
8585 if (has_derivs)
8586 args.insert(args.end(), derivs.begin(), derivs.end());
8587
8588 args.insert(args.end(), coords.begin(), coords.end());
8589 if (has_sample_index)
8590 args.emplace_back(sample_index);
8591 if (has_lod)
8592 args.emplace_back(lod);
8593
8594 Temp arg = bld.tmp(RegClass(RegType::vgpr, args.size()));
8595 aco_ptr<Instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, args.size(), 1)};
8596 vec->definitions[0] = Definition(arg);
8597 for (unsigned i = 0; i < args.size(); i++)
8598 vec->operands[i] = Operand(args[i]);
8599 ctx->block->instructions.emplace_back(std::move(vec));
8600
8601
8602 if (instr->op == nir_texop_txf ||
8603 instr->op == nir_texop_txf_ms ||
8604 instr->op == nir_texop_samples_identical ||
8605 instr->op == nir_texop_fragment_fetch ||
8606 instr->op == nir_texop_fragment_mask_fetch) {
8607 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;
8608 tex.reset(create_instruction<MIMG_instruction>(op, Format::MIMG, 3, 1));
8609 tex->operands[0] = Operand(resource);
8610 tex->operands[1] = Operand(s4); /* no sampler */
8611 tex->operands[2] = Operand(arg);
8612 tex->dim = dim;
8613 tex->dmask = dmask;
8614 tex->unrm = true;
8615 tex->da = da;
8616 tex->definitions[0] = Definition(tmp_dst);
8617 tex->can_reorder = true;
8618 ctx->block->instructions.emplace_back(std::move(tex));
8619
8620 if (instr->op == nir_texop_samples_identical) {
8621 assert(dmask == 1 && dst.regClass() == v1);
8622 assert(dst.id() != tmp_dst.id());
8623
8624 Temp tmp = bld.tmp(bld.lm);
8625 bld.vopc(aco_opcode::v_cmp_eq_u32, Definition(tmp), Operand(0u), tmp_dst).def(0).setHint(vcc);
8626 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(dst), Operand(0u), Operand((uint32_t)-1), tmp);
8627
8628 } else {
8629 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, dmask);
8630 }
8631 return;
8632 }
8633
8634 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8635 aco_opcode opcode = aco_opcode::image_sample;
8636 if (has_offset) { /* image_sample_*_o */
8637 if (has_compare) {
8638 opcode = aco_opcode::image_sample_c_o;
8639 if (has_derivs)
8640 opcode = aco_opcode::image_sample_c_d_o;
8641 if (has_bias)
8642 opcode = aco_opcode::image_sample_c_b_o;
8643 if (level_zero)
8644 opcode = aco_opcode::image_sample_c_lz_o;
8645 if (has_lod)
8646 opcode = aco_opcode::image_sample_c_l_o;
8647 } else {
8648 opcode = aco_opcode::image_sample_o;
8649 if (has_derivs)
8650 opcode = aco_opcode::image_sample_d_o;
8651 if (has_bias)
8652 opcode = aco_opcode::image_sample_b_o;
8653 if (level_zero)
8654 opcode = aco_opcode::image_sample_lz_o;
8655 if (has_lod)
8656 opcode = aco_opcode::image_sample_l_o;
8657 }
8658 } else { /* no offset */
8659 if (has_compare) {
8660 opcode = aco_opcode::image_sample_c;
8661 if (has_derivs)
8662 opcode = aco_opcode::image_sample_c_d;
8663 if (has_bias)
8664 opcode = aco_opcode::image_sample_c_b;
8665 if (level_zero)
8666 opcode = aco_opcode::image_sample_c_lz;
8667 if (has_lod)
8668 opcode = aco_opcode::image_sample_c_l;
8669 } else {
8670 opcode = aco_opcode::image_sample;
8671 if (has_derivs)
8672 opcode = aco_opcode::image_sample_d;
8673 if (has_bias)
8674 opcode = aco_opcode::image_sample_b;
8675 if (level_zero)
8676 opcode = aco_opcode::image_sample_lz;
8677 if (has_lod)
8678 opcode = aco_opcode::image_sample_l;
8679 }
8680 }
8681
8682 if (instr->op == nir_texop_tg4) {
8683 if (has_offset) {
8684 opcode = aco_opcode::image_gather4_lz_o;
8685 if (has_compare)
8686 opcode = aco_opcode::image_gather4_c_lz_o;
8687 } else {
8688 opcode = aco_opcode::image_gather4_lz;
8689 if (has_compare)
8690 opcode = aco_opcode::image_gather4_c_lz;
8691 }
8692 } else if (instr->op == nir_texop_lod) {
8693 opcode = aco_opcode::image_get_lod;
8694 }
8695
8696 /* we don't need the bias, sample index, compare value or offset to be
8697 * computed in WQM but if the p_create_vector copies the coordinates, then it
8698 * needs to be in WQM */
8699 if (ctx->stage == fragment_fs &&
8700 !has_derivs && !has_lod && !level_zero &&
8701 instr->sampler_dim != GLSL_SAMPLER_DIM_MS &&
8702 instr->sampler_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
8703 arg = emit_wqm(ctx, arg, bld.tmp(arg.regClass()), true);
8704
8705 tex.reset(create_instruction<MIMG_instruction>(opcode, Format::MIMG, 3, 1));
8706 tex->operands[0] = Operand(resource);
8707 tex->operands[1] = Operand(sampler);
8708 tex->operands[2] = Operand(arg);
8709 tex->dim = dim;
8710 tex->dmask = dmask;
8711 tex->da = da;
8712 tex->definitions[0] = Definition(tmp_dst);
8713 tex->can_reorder = true;
8714 ctx->block->instructions.emplace_back(std::move(tex));
8715
8716 if (tg4_integer_cube_workaround) {
8717 assert(tmp_dst.id() != dst.id());
8718 assert(tmp_dst.size() == dst.size() && dst.size() == 4);
8719
8720 emit_split_vector(ctx, tmp_dst, tmp_dst.size());
8721 Temp val[4];
8722 for (unsigned i = 0; i < dst.size(); i++) {
8723 val[i] = emit_extract_vector(ctx, tmp_dst, i, v1);
8724 Temp cvt_val;
8725 if (stype == GLSL_TYPE_UINT)
8726 cvt_val = bld.vop1(aco_opcode::v_cvt_u32_f32, bld.def(v1), val[i]);
8727 else
8728 cvt_val = bld.vop1(aco_opcode::v_cvt_i32_f32, bld.def(v1), val[i]);
8729 val[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1), val[i], cvt_val, tg4_compare_cube_wa64);
8730 }
8731 Temp tmp = dst.regClass() == v4 ? dst : bld.tmp(v4);
8732 tmp_dst = bld.pseudo(aco_opcode::p_create_vector, Definition(tmp),
8733 val[0], val[1], val[2], val[3]);
8734 }
8735 unsigned mask = instr->op == nir_texop_tg4 ? 0xF : dmask;
8736 expand_vector(ctx, tmp_dst, dst, instr->dest.ssa.num_components, mask);
8737
8738 }
8739
8740
8741 Operand get_phi_operand(isel_context *ctx, nir_ssa_def *ssa)
8742 {
8743 Temp tmp = get_ssa_temp(ctx, ssa);
8744 if (ssa->parent_instr->type == nir_instr_type_ssa_undef)
8745 return Operand(tmp.regClass());
8746 else
8747 return Operand(tmp);
8748 }
8749
8750 void visit_phi(isel_context *ctx, nir_phi_instr *instr)
8751 {
8752 aco_ptr<Pseudo_instruction> phi;
8753 Temp dst = get_ssa_temp(ctx, &instr->dest.ssa);
8754 assert(instr->dest.ssa.bit_size != 1 || dst.regClass() == ctx->program->lane_mask);
8755
8756 bool logical = !dst.is_linear() || ctx->divergent_vals[instr->dest.ssa.index];
8757 logical |= ctx->block->kind & block_kind_merge;
8758 aco_opcode opcode = logical ? aco_opcode::p_phi : aco_opcode::p_linear_phi;
8759
8760 /* we want a sorted list of sources, since the predecessor list is also sorted */
8761 std::map<unsigned, nir_ssa_def*> phi_src;
8762 nir_foreach_phi_src(src, instr)
8763 phi_src[src->pred->index] = src->src.ssa;
8764
8765 std::vector<unsigned>& preds = logical ? ctx->block->logical_preds : ctx->block->linear_preds;
8766 unsigned num_operands = 0;
8767 Operand operands[std::max(exec_list_length(&instr->srcs), (unsigned)preds.size()) + 1];
8768 unsigned num_defined = 0;
8769 unsigned cur_pred_idx = 0;
8770 for (std::pair<unsigned, nir_ssa_def *> src : phi_src) {
8771 if (cur_pred_idx < preds.size()) {
8772 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8773 unsigned block = ctx->cf_info.nir_to_aco[src.first];
8774 unsigned skipped = 0;
8775 while (cur_pred_idx + skipped < preds.size() && preds[cur_pred_idx + skipped] != block)
8776 skipped++;
8777 if (cur_pred_idx + skipped < preds.size()) {
8778 for (unsigned i = 0; i < skipped; i++)
8779 operands[num_operands++] = Operand(dst.regClass());
8780 cur_pred_idx += skipped;
8781 } else {
8782 continue;
8783 }
8784 }
8785 /* Handle missing predecessors at the end. This shouldn't happen with loop
8786 * headers and we can't ignore these sources for loop header phis. */
8787 if (!(ctx->block->kind & block_kind_loop_header) && cur_pred_idx >= preds.size())
8788 continue;
8789 cur_pred_idx++;
8790 Operand op = get_phi_operand(ctx, src.second);
8791 operands[num_operands++] = op;
8792 num_defined += !op.isUndefined();
8793 }
8794 /* handle block_kind_continue_or_break at loop exit blocks */
8795 while (cur_pred_idx++ < preds.size())
8796 operands[num_operands++] = Operand(dst.regClass());
8797
8798 /* If the loop ends with a break, still add a linear continue edge in case
8799 * that break is divergent or continue_or_break is used. We'll either remove
8800 * this operand later in visit_loop() if it's not necessary or replace the
8801 * undef with something correct. */
8802 if (!logical && ctx->block->kind & block_kind_loop_header) {
8803 nir_loop *loop = nir_cf_node_as_loop(instr->instr.block->cf_node.parent);
8804 nir_block *last = nir_loop_last_block(loop);
8805 if (last->successors[0] != instr->instr.block)
8806 operands[num_operands++] = Operand(RegClass());
8807 }
8808
8809 if (num_defined == 0) {
8810 Builder bld(ctx->program, ctx->block);
8811 if (dst.regClass() == s1) {
8812 bld.sop1(aco_opcode::s_mov_b32, Definition(dst), Operand(0u));
8813 } else if (dst.regClass() == v1) {
8814 bld.vop1(aco_opcode::v_mov_b32, Definition(dst), Operand(0u));
8815 } else {
8816 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8817 for (unsigned i = 0; i < dst.size(); i++)
8818 vec->operands[i] = Operand(0u);
8819 vec->definitions[0] = Definition(dst);
8820 ctx->block->instructions.emplace_back(std::move(vec));
8821 }
8822 return;
8823 }
8824
8825 /* we can use a linear phi in some cases if one src is undef */
8826 if (dst.is_linear() && ctx->block->kind & block_kind_merge && num_defined == 1) {
8827 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_operands, 1));
8828
8829 Block *linear_else = &ctx->program->blocks[ctx->block->linear_preds[1]];
8830 Block *invert = &ctx->program->blocks[linear_else->linear_preds[0]];
8831 assert(invert->kind & block_kind_invert);
8832
8833 unsigned then_block = invert->linear_preds[0];
8834
8835 Block* insert_block = NULL;
8836 for (unsigned i = 0; i < num_operands; i++) {
8837 Operand op = operands[i];
8838 if (op.isUndefined())
8839 continue;
8840 insert_block = ctx->block->logical_preds[i] == then_block ? invert : ctx->block;
8841 phi->operands[0] = op;
8842 break;
8843 }
8844 assert(insert_block); /* should be handled by the "num_defined == 0" case above */
8845 phi->operands[1] = Operand(dst.regClass());
8846 phi->definitions[0] = Definition(dst);
8847 insert_block->instructions.emplace(insert_block->instructions.begin(), std::move(phi));
8848 return;
8849 }
8850
8851 /* try to scalarize vector phis */
8852 if (instr->dest.ssa.bit_size != 1 && dst.size() > 1) {
8853 // TODO: scalarize linear phis on divergent ifs
8854 bool can_scalarize = (opcode == aco_opcode::p_phi || !(ctx->block->kind & block_kind_merge));
8855 std::array<Temp, NIR_MAX_VEC_COMPONENTS> new_vec;
8856 for (unsigned i = 0; can_scalarize && (i < num_operands); i++) {
8857 Operand src = operands[i];
8858 if (src.isTemp() && ctx->allocated_vec.find(src.tempId()) == ctx->allocated_vec.end())
8859 can_scalarize = false;
8860 }
8861 if (can_scalarize) {
8862 unsigned num_components = instr->dest.ssa.num_components;
8863 assert(dst.size() % num_components == 0);
8864 RegClass rc = RegClass(dst.type(), dst.size() / num_components);
8865
8866 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_components, 1)};
8867 for (unsigned k = 0; k < num_components; k++) {
8868 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8869 for (unsigned i = 0; i < num_operands; i++) {
8870 Operand src = operands[i];
8871 phi->operands[i] = src.isTemp() ? Operand(ctx->allocated_vec[src.tempId()][k]) : Operand(rc);
8872 }
8873 Temp phi_dst = {ctx->program->allocateId(), rc};
8874 phi->definitions[0] = Definition(phi_dst);
8875 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8876 new_vec[k] = phi_dst;
8877 vec->operands[k] = Operand(phi_dst);
8878 }
8879 vec->definitions[0] = Definition(dst);
8880 ctx->block->instructions.emplace_back(std::move(vec));
8881 ctx->allocated_vec.emplace(dst.id(), new_vec);
8882 return;
8883 }
8884 }
8885
8886 phi.reset(create_instruction<Pseudo_instruction>(opcode, Format::PSEUDO, num_operands, 1));
8887 for (unsigned i = 0; i < num_operands; i++)
8888 phi->operands[i] = operands[i];
8889 phi->definitions[0] = Definition(dst);
8890 ctx->block->instructions.emplace(ctx->block->instructions.begin(), std::move(phi));
8891 }
8892
8893
8894 void visit_undef(isel_context *ctx, nir_ssa_undef_instr *instr)
8895 {
8896 Temp dst = get_ssa_temp(ctx, &instr->def);
8897
8898 assert(dst.type() == RegType::sgpr);
8899
8900 if (dst.size() == 1) {
8901 Builder(ctx->program, ctx->block).copy(Definition(dst), Operand(0u));
8902 } else {
8903 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, dst.size(), 1)};
8904 for (unsigned i = 0; i < dst.size(); i++)
8905 vec->operands[i] = Operand(0u);
8906 vec->definitions[0] = Definition(dst);
8907 ctx->block->instructions.emplace_back(std::move(vec));
8908 }
8909 }
8910
8911 void visit_jump(isel_context *ctx, nir_jump_instr *instr)
8912 {
8913 Builder bld(ctx->program, ctx->block);
8914 Block *logical_target;
8915 append_logical_end(ctx->block);
8916 unsigned idx = ctx->block->index;
8917
8918 switch (instr->type) {
8919 case nir_jump_break:
8920 logical_target = ctx->cf_info.parent_loop.exit;
8921 add_logical_edge(idx, logical_target);
8922 ctx->block->kind |= block_kind_break;
8923
8924 if (!ctx->cf_info.parent_if.is_divergent &&
8925 !ctx->cf_info.parent_loop.has_divergent_continue) {
8926 /* uniform break - directly jump out of the loop */
8927 ctx->block->kind |= block_kind_uniform;
8928 ctx->cf_info.has_branch = true;
8929 bld.branch(aco_opcode::p_branch);
8930 add_linear_edge(idx, logical_target);
8931 return;
8932 }
8933 ctx->cf_info.parent_loop.has_divergent_branch = true;
8934 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8935 break;
8936 case nir_jump_continue:
8937 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8938 add_logical_edge(idx, logical_target);
8939 ctx->block->kind |= block_kind_continue;
8940
8941 if (ctx->cf_info.parent_if.is_divergent) {
8942 /* for potential uniform breaks after this continue,
8943 we must ensure that they are handled correctly */
8944 ctx->cf_info.parent_loop.has_divergent_continue = true;
8945 ctx->cf_info.parent_loop.has_divergent_branch = true;
8946 ctx->cf_info.nir_to_aco[instr->instr.block->index] = ctx->block->index;
8947 } else {
8948 /* uniform continue - directly jump to the loop header */
8949 ctx->block->kind |= block_kind_uniform;
8950 ctx->cf_info.has_branch = true;
8951 bld.branch(aco_opcode::p_branch);
8952 add_linear_edge(idx, logical_target);
8953 return;
8954 }
8955 break;
8956 default:
8957 fprintf(stderr, "Unknown NIR jump instr: ");
8958 nir_print_instr(&instr->instr, stderr);
8959 fprintf(stderr, "\n");
8960 abort();
8961 }
8962
8963 if (ctx->cf_info.parent_if.is_divergent && !ctx->cf_info.exec_potentially_empty_break) {
8964 ctx->cf_info.exec_potentially_empty_break = true;
8965 ctx->cf_info.exec_potentially_empty_break_depth = ctx->cf_info.loop_nest_depth;
8966 }
8967
8968 /* remove critical edges from linear CFG */
8969 bld.branch(aco_opcode::p_branch);
8970 Block* break_block = ctx->program->create_and_insert_block();
8971 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8972 break_block->kind |= block_kind_uniform;
8973 add_linear_edge(idx, break_block);
8974 /* the loop_header pointer might be invalidated by this point */
8975 if (instr->type == nir_jump_continue)
8976 logical_target = &ctx->program->blocks[ctx->cf_info.parent_loop.header_idx];
8977 add_linear_edge(break_block->index, logical_target);
8978 bld.reset(break_block);
8979 bld.branch(aco_opcode::p_branch);
8980
8981 Block* continue_block = ctx->program->create_and_insert_block();
8982 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
8983 add_linear_edge(idx, continue_block);
8984 append_logical_start(continue_block);
8985 ctx->block = continue_block;
8986 return;
8987 }
8988
8989 void visit_block(isel_context *ctx, nir_block *block)
8990 {
8991 nir_foreach_instr(instr, block) {
8992 switch (instr->type) {
8993 case nir_instr_type_alu:
8994 visit_alu_instr(ctx, nir_instr_as_alu(instr));
8995 break;
8996 case nir_instr_type_load_const:
8997 visit_load_const(ctx, nir_instr_as_load_const(instr));
8998 break;
8999 case nir_instr_type_intrinsic:
9000 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
9001 break;
9002 case nir_instr_type_tex:
9003 visit_tex(ctx, nir_instr_as_tex(instr));
9004 break;
9005 case nir_instr_type_phi:
9006 visit_phi(ctx, nir_instr_as_phi(instr));
9007 break;
9008 case nir_instr_type_ssa_undef:
9009 visit_undef(ctx, nir_instr_as_ssa_undef(instr));
9010 break;
9011 case nir_instr_type_deref:
9012 break;
9013 case nir_instr_type_jump:
9014 visit_jump(ctx, nir_instr_as_jump(instr));
9015 break;
9016 default:
9017 fprintf(stderr, "Unknown NIR instr type: ");
9018 nir_print_instr(instr, stderr);
9019 fprintf(stderr, "\n");
9020 //abort();
9021 }
9022 }
9023
9024 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9025 ctx->cf_info.nir_to_aco[block->index] = ctx->block->index;
9026 }
9027
9028
9029
9030 static Operand create_continue_phis(isel_context *ctx, unsigned first, unsigned last,
9031 aco_ptr<Instruction>& header_phi, Operand *vals)
9032 {
9033 vals[0] = Operand(header_phi->definitions[0].getTemp());
9034 RegClass rc = vals[0].regClass();
9035
9036 unsigned loop_nest_depth = ctx->program->blocks[first].loop_nest_depth;
9037
9038 unsigned next_pred = 1;
9039
9040 for (unsigned idx = first + 1; idx <= last; idx++) {
9041 Block& block = ctx->program->blocks[idx];
9042 if (block.loop_nest_depth != loop_nest_depth) {
9043 vals[idx - first] = vals[idx - 1 - first];
9044 continue;
9045 }
9046
9047 if (block.kind & block_kind_continue) {
9048 vals[idx - first] = header_phi->operands[next_pred];
9049 next_pred++;
9050 continue;
9051 }
9052
9053 bool all_same = true;
9054 for (unsigned i = 1; all_same && (i < block.linear_preds.size()); i++)
9055 all_same = vals[block.linear_preds[i] - first] == vals[block.linear_preds[0] - first];
9056
9057 Operand val;
9058 if (all_same) {
9059 val = vals[block.linear_preds[0] - first];
9060 } else {
9061 aco_ptr<Instruction> phi(create_instruction<Pseudo_instruction>(
9062 aco_opcode::p_linear_phi, Format::PSEUDO, block.linear_preds.size(), 1));
9063 for (unsigned i = 0; i < block.linear_preds.size(); i++)
9064 phi->operands[i] = vals[block.linear_preds[i] - first];
9065 val = Operand(Temp(ctx->program->allocateId(), rc));
9066 phi->definitions[0] = Definition(val.getTemp());
9067 block.instructions.emplace(block.instructions.begin(), std::move(phi));
9068 }
9069 vals[idx - first] = val;
9070 }
9071
9072 return vals[last - first];
9073 }
9074
9075 static void visit_loop(isel_context *ctx, nir_loop *loop)
9076 {
9077 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9078 append_logical_end(ctx->block);
9079 ctx->block->kind |= block_kind_loop_preheader | block_kind_uniform;
9080 Builder bld(ctx->program, ctx->block);
9081 bld.branch(aco_opcode::p_branch);
9082 unsigned loop_preheader_idx = ctx->block->index;
9083
9084 Block loop_exit = Block();
9085 loop_exit.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9086 loop_exit.kind |= (block_kind_loop_exit | (ctx->block->kind & block_kind_top_level));
9087
9088 Block* loop_header = ctx->program->create_and_insert_block();
9089 loop_header->loop_nest_depth = ctx->cf_info.loop_nest_depth + 1;
9090 loop_header->kind |= block_kind_loop_header;
9091 add_edge(loop_preheader_idx, loop_header);
9092 ctx->block = loop_header;
9093
9094 /* emit loop body */
9095 unsigned loop_header_idx = loop_header->index;
9096 loop_info_RAII loop_raii(ctx, loop_header_idx, &loop_exit);
9097 append_logical_start(ctx->block);
9098 bool unreachable = visit_cf_list(ctx, &loop->body);
9099
9100 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9101 if (!ctx->cf_info.has_branch) {
9102 append_logical_end(ctx->block);
9103 if (ctx->cf_info.exec_potentially_empty_discard || ctx->cf_info.exec_potentially_empty_break) {
9104 /* Discards can result in code running with an empty exec mask.
9105 * This would result in divergent breaks not ever being taken. As a
9106 * workaround, break the loop when the loop mask is empty instead of
9107 * always continuing. */
9108 ctx->block->kind |= (block_kind_continue_or_break | block_kind_uniform);
9109 unsigned block_idx = ctx->block->index;
9110
9111 /* create helper blocks to avoid critical edges */
9112 Block *break_block = ctx->program->create_and_insert_block();
9113 break_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9114 break_block->kind = block_kind_uniform;
9115 bld.reset(break_block);
9116 bld.branch(aco_opcode::p_branch);
9117 add_linear_edge(block_idx, break_block);
9118 add_linear_edge(break_block->index, &loop_exit);
9119
9120 Block *continue_block = ctx->program->create_and_insert_block();
9121 continue_block->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9122 continue_block->kind = block_kind_uniform;
9123 bld.reset(continue_block);
9124 bld.branch(aco_opcode::p_branch);
9125 add_linear_edge(block_idx, continue_block);
9126 add_linear_edge(continue_block->index, &ctx->program->blocks[loop_header_idx]);
9127
9128 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9129 add_logical_edge(block_idx, &ctx->program->blocks[loop_header_idx]);
9130 ctx->block = &ctx->program->blocks[block_idx];
9131 } else {
9132 ctx->block->kind |= (block_kind_continue | block_kind_uniform);
9133 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9134 add_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9135 else
9136 add_linear_edge(ctx->block->index, &ctx->program->blocks[loop_header_idx]);
9137 }
9138
9139 bld.reset(ctx->block);
9140 bld.branch(aco_opcode::p_branch);
9141 }
9142
9143 /* Fixup phis in loop header from unreachable blocks.
9144 * has_branch/has_divergent_branch also indicates if the loop ends with a
9145 * break/continue instruction, but we don't emit those if unreachable=true */
9146 if (unreachable) {
9147 assert(ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch);
9148 bool linear = ctx->cf_info.has_branch;
9149 bool logical = ctx->cf_info.has_branch || ctx->cf_info.parent_loop.has_divergent_branch;
9150 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9151 if ((logical && instr->opcode == aco_opcode::p_phi) ||
9152 (linear && instr->opcode == aco_opcode::p_linear_phi)) {
9153 /* the last operand should be the one that needs to be removed */
9154 instr->operands.pop_back();
9155 } else if (!is_phi(instr)) {
9156 break;
9157 }
9158 }
9159 }
9160
9161 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9162 * and the previous one shouldn't both happen at once because a break in the
9163 * merge block would get CSE'd */
9164 if (nir_loop_last_block(loop)->successors[0] != nir_loop_first_block(loop)) {
9165 unsigned num_vals = ctx->cf_info.has_branch ? 1 : (ctx->block->index - loop_header_idx + 1);
9166 Operand vals[num_vals];
9167 for (aco_ptr<Instruction>& instr : ctx->program->blocks[loop_header_idx].instructions) {
9168 if (instr->opcode == aco_opcode::p_linear_phi) {
9169 if (ctx->cf_info.has_branch)
9170 instr->operands.pop_back();
9171 else
9172 instr->operands.back() = create_continue_phis(ctx, loop_header_idx, ctx->block->index, instr, vals);
9173 } else if (!is_phi(instr)) {
9174 break;
9175 }
9176 }
9177 }
9178
9179 ctx->cf_info.has_branch = false;
9180
9181 // TODO: if the loop has not a single exit, we must add one °°
9182 /* emit loop successor block */
9183 ctx->block = ctx->program->insert_block(std::move(loop_exit));
9184 append_logical_start(ctx->block);
9185
9186 #if 0
9187 // TODO: check if it is beneficial to not branch on continues
9188 /* trim linear phis in loop header */
9189 for (auto&& instr : loop_entry->instructions) {
9190 if (instr->opcode == aco_opcode::p_linear_phi) {
9191 aco_ptr<Pseudo_instruction> new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, loop_entry->linear_predecessors.size(), 1)};
9192 new_phi->definitions[0] = instr->definitions[0];
9193 for (unsigned i = 0; i < new_phi->operands.size(); i++)
9194 new_phi->operands[i] = instr->operands[i];
9195 /* check that the remaining operands are all the same */
9196 for (unsigned i = new_phi->operands.size(); i < instr->operands.size(); i++)
9197 assert(instr->operands[i].tempId() == instr->operands.back().tempId());
9198 instr.swap(new_phi);
9199 } else if (instr->opcode == aco_opcode::p_phi) {
9200 continue;
9201 } else {
9202 break;
9203 }
9204 }
9205 #endif
9206 }
9207
9208 static void begin_divergent_if_then(isel_context *ctx, if_context *ic, Temp cond)
9209 {
9210 ic->cond = cond;
9211
9212 append_logical_end(ctx->block);
9213 ctx->block->kind |= block_kind_branch;
9214
9215 /* branch to linear then block */
9216 assert(cond.regClass() == ctx->program->lane_mask);
9217 aco_ptr<Pseudo_branch_instruction> branch;
9218 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_z, Format::PSEUDO_BRANCH, 1, 0));
9219 branch->operands[0] = Operand(cond);
9220 ctx->block->instructions.push_back(std::move(branch));
9221
9222 ic->BB_if_idx = ctx->block->index;
9223 ic->BB_invert = Block();
9224 ic->BB_invert.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9225 /* Invert blocks are intentionally not marked as top level because they
9226 * are not part of the logical cfg. */
9227 ic->BB_invert.kind |= block_kind_invert;
9228 ic->BB_endif = Block();
9229 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9230 ic->BB_endif.kind |= (block_kind_merge | (ctx->block->kind & block_kind_top_level));
9231
9232 ic->exec_potentially_empty_discard_old = ctx->cf_info.exec_potentially_empty_discard;
9233 ic->exec_potentially_empty_break_old = ctx->cf_info.exec_potentially_empty_break;
9234 ic->exec_potentially_empty_break_depth_old = ctx->cf_info.exec_potentially_empty_break_depth;
9235 ic->divergent_old = ctx->cf_info.parent_if.is_divergent;
9236 ctx->cf_info.parent_if.is_divergent = true;
9237
9238 /* divergent branches use cbranch_execz */
9239 ctx->cf_info.exec_potentially_empty_discard = false;
9240 ctx->cf_info.exec_potentially_empty_break = false;
9241 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9242
9243 /** emit logical then block */
9244 Block* BB_then_logical = ctx->program->create_and_insert_block();
9245 BB_then_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9246 add_edge(ic->BB_if_idx, BB_then_logical);
9247 ctx->block = BB_then_logical;
9248 append_logical_start(BB_then_logical);
9249 }
9250
9251 static void begin_divergent_if_else(isel_context *ctx, if_context *ic)
9252 {
9253 Block *BB_then_logical = ctx->block;
9254 append_logical_end(BB_then_logical);
9255 /* branch from logical then block to invert block */
9256 aco_ptr<Pseudo_branch_instruction> branch;
9257 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9258 BB_then_logical->instructions.emplace_back(std::move(branch));
9259 add_linear_edge(BB_then_logical->index, &ic->BB_invert);
9260 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9261 add_logical_edge(BB_then_logical->index, &ic->BB_endif);
9262 BB_then_logical->kind |= block_kind_uniform;
9263 assert(!ctx->cf_info.has_branch);
9264 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9265 ctx->cf_info.parent_loop.has_divergent_branch = false;
9266
9267 /** emit linear then block */
9268 Block* BB_then_linear = ctx->program->create_and_insert_block();
9269 BB_then_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9270 BB_then_linear->kind |= block_kind_uniform;
9271 add_linear_edge(ic->BB_if_idx, BB_then_linear);
9272 /* branch from linear then block to invert block */
9273 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9274 BB_then_linear->instructions.emplace_back(std::move(branch));
9275 add_linear_edge(BB_then_linear->index, &ic->BB_invert);
9276
9277 /** emit invert merge block */
9278 ctx->block = ctx->program->insert_block(std::move(ic->BB_invert));
9279 ic->invert_idx = ctx->block->index;
9280
9281 /* branch to linear else block (skip else) */
9282 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_cbranch_nz, Format::PSEUDO_BRANCH, 1, 0));
9283 branch->operands[0] = Operand(ic->cond);
9284 ctx->block->instructions.push_back(std::move(branch));
9285
9286 ic->exec_potentially_empty_discard_old |= ctx->cf_info.exec_potentially_empty_discard;
9287 ic->exec_potentially_empty_break_old |= ctx->cf_info.exec_potentially_empty_break;
9288 ic->exec_potentially_empty_break_depth_old =
9289 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9290 /* divergent branches use cbranch_execz */
9291 ctx->cf_info.exec_potentially_empty_discard = false;
9292 ctx->cf_info.exec_potentially_empty_break = false;
9293 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9294
9295 /** emit logical else block */
9296 Block* BB_else_logical = ctx->program->create_and_insert_block();
9297 BB_else_logical->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9298 add_logical_edge(ic->BB_if_idx, BB_else_logical);
9299 add_linear_edge(ic->invert_idx, BB_else_logical);
9300 ctx->block = BB_else_logical;
9301 append_logical_start(BB_else_logical);
9302 }
9303
9304 static void end_divergent_if(isel_context *ctx, if_context *ic)
9305 {
9306 Block *BB_else_logical = ctx->block;
9307 append_logical_end(BB_else_logical);
9308
9309 /* branch from logical else block to endif block */
9310 aco_ptr<Pseudo_branch_instruction> branch;
9311 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9312 BB_else_logical->instructions.emplace_back(std::move(branch));
9313 add_linear_edge(BB_else_logical->index, &ic->BB_endif);
9314 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9315 add_logical_edge(BB_else_logical->index, &ic->BB_endif);
9316 BB_else_logical->kind |= block_kind_uniform;
9317
9318 assert(!ctx->cf_info.has_branch);
9319 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9320
9321
9322 /** emit linear else block */
9323 Block* BB_else_linear = ctx->program->create_and_insert_block();
9324 BB_else_linear->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9325 BB_else_linear->kind |= block_kind_uniform;
9326 add_linear_edge(ic->invert_idx, BB_else_linear);
9327
9328 /* branch from linear else block to endif block */
9329 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9330 BB_else_linear->instructions.emplace_back(std::move(branch));
9331 add_linear_edge(BB_else_linear->index, &ic->BB_endif);
9332
9333
9334 /** emit endif merge block */
9335 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9336 append_logical_start(ctx->block);
9337
9338
9339 ctx->cf_info.parent_if.is_divergent = ic->divergent_old;
9340 ctx->cf_info.exec_potentially_empty_discard |= ic->exec_potentially_empty_discard_old;
9341 ctx->cf_info.exec_potentially_empty_break |= ic->exec_potentially_empty_break_old;
9342 ctx->cf_info.exec_potentially_empty_break_depth =
9343 std::min(ic->exec_potentially_empty_break_depth_old, ctx->cf_info.exec_potentially_empty_break_depth);
9344 if (ctx->cf_info.loop_nest_depth == ctx->cf_info.exec_potentially_empty_break_depth &&
9345 !ctx->cf_info.parent_if.is_divergent) {
9346 ctx->cf_info.exec_potentially_empty_break = false;
9347 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9348 }
9349 /* uniform control flow never has an empty exec-mask */
9350 if (!ctx->cf_info.loop_nest_depth && !ctx->cf_info.parent_if.is_divergent) {
9351 ctx->cf_info.exec_potentially_empty_discard = false;
9352 ctx->cf_info.exec_potentially_empty_break = false;
9353 ctx->cf_info.exec_potentially_empty_break_depth = UINT16_MAX;
9354 }
9355 }
9356
9357 static void begin_uniform_if_then(isel_context *ctx, if_context *ic, Temp cond)
9358 {
9359 assert(cond.regClass() == s1);
9360
9361 append_logical_end(ctx->block);
9362 ctx->block->kind |= block_kind_uniform;
9363
9364 aco_ptr<Pseudo_branch_instruction> branch;
9365 aco_opcode branch_opcode = aco_opcode::p_cbranch_z;
9366 branch.reset(create_instruction<Pseudo_branch_instruction>(branch_opcode, Format::PSEUDO_BRANCH, 1, 0));
9367 branch->operands[0] = Operand(cond);
9368 branch->operands[0].setFixed(scc);
9369 ctx->block->instructions.emplace_back(std::move(branch));
9370
9371 ic->BB_if_idx = ctx->block->index;
9372 ic->BB_endif = Block();
9373 ic->BB_endif.loop_nest_depth = ctx->cf_info.loop_nest_depth;
9374 ic->BB_endif.kind |= ctx->block->kind & block_kind_top_level;
9375
9376 ctx->cf_info.has_branch = false;
9377 ctx->cf_info.parent_loop.has_divergent_branch = false;
9378
9379 /** emit then block */
9380 Block* BB_then = ctx->program->create_and_insert_block();
9381 BB_then->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9382 add_edge(ic->BB_if_idx, BB_then);
9383 append_logical_start(BB_then);
9384 ctx->block = BB_then;
9385 }
9386
9387 static void begin_uniform_if_else(isel_context *ctx, if_context *ic)
9388 {
9389 Block *BB_then = ctx->block;
9390
9391 ic->uniform_has_then_branch = ctx->cf_info.has_branch;
9392 ic->then_branch_divergent = ctx->cf_info.parent_loop.has_divergent_branch;
9393
9394 if (!ic->uniform_has_then_branch) {
9395 append_logical_end(BB_then);
9396 /* branch from then block to endif block */
9397 aco_ptr<Pseudo_branch_instruction> branch;
9398 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9399 BB_then->instructions.emplace_back(std::move(branch));
9400 add_linear_edge(BB_then->index, &ic->BB_endif);
9401 if (!ic->then_branch_divergent)
9402 add_logical_edge(BB_then->index, &ic->BB_endif);
9403 BB_then->kind |= block_kind_uniform;
9404 }
9405
9406 ctx->cf_info.has_branch = false;
9407 ctx->cf_info.parent_loop.has_divergent_branch = false;
9408
9409 /** emit else block */
9410 Block* BB_else = ctx->program->create_and_insert_block();
9411 BB_else->loop_nest_depth = ctx->cf_info.loop_nest_depth;
9412 add_edge(ic->BB_if_idx, BB_else);
9413 append_logical_start(BB_else);
9414 ctx->block = BB_else;
9415 }
9416
9417 static void end_uniform_if(isel_context *ctx, if_context *ic)
9418 {
9419 Block *BB_else = ctx->block;
9420
9421 if (!ctx->cf_info.has_branch) {
9422 append_logical_end(BB_else);
9423 /* branch from then block to endif block */
9424 aco_ptr<Pseudo_branch_instruction> branch;
9425 branch.reset(create_instruction<Pseudo_branch_instruction>(aco_opcode::p_branch, Format::PSEUDO_BRANCH, 0, 0));
9426 BB_else->instructions.emplace_back(std::move(branch));
9427 add_linear_edge(BB_else->index, &ic->BB_endif);
9428 if (!ctx->cf_info.parent_loop.has_divergent_branch)
9429 add_logical_edge(BB_else->index, &ic->BB_endif);
9430 BB_else->kind |= block_kind_uniform;
9431 }
9432
9433 ctx->cf_info.has_branch &= ic->uniform_has_then_branch;
9434 ctx->cf_info.parent_loop.has_divergent_branch &= ic->then_branch_divergent;
9435
9436 /** emit endif merge block */
9437 if (!ctx->cf_info.has_branch) {
9438 ctx->block = ctx->program->insert_block(std::move(ic->BB_endif));
9439 append_logical_start(ctx->block);
9440 }
9441 }
9442
9443 static bool visit_if(isel_context *ctx, nir_if *if_stmt)
9444 {
9445 Temp cond = get_ssa_temp(ctx, if_stmt->condition.ssa);
9446 Builder bld(ctx->program, ctx->block);
9447 aco_ptr<Pseudo_branch_instruction> branch;
9448 if_context ic;
9449
9450 if (!ctx->divergent_vals[if_stmt->condition.ssa->index]) { /* uniform condition */
9451 /**
9452 * Uniform conditionals are represented in the following way*) :
9453 *
9454 * The linear and logical CFG:
9455 * BB_IF
9456 * / \
9457 * BB_THEN (logical) BB_ELSE (logical)
9458 * \ /
9459 * BB_ENDIF
9460 *
9461 * *) Exceptions may be due to break and continue statements within loops
9462 * If a break/continue happens within uniform control flow, it branches
9463 * to the loop exit/entry block. Otherwise, it branches to the next
9464 * merge block.
9465 **/
9466
9467 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9468 assert(cond.regClass() == ctx->program->lane_mask);
9469 cond = bool_to_scalar_condition(ctx, cond);
9470
9471 begin_uniform_if_then(ctx, &ic, cond);
9472 visit_cf_list(ctx, &if_stmt->then_list);
9473
9474 begin_uniform_if_else(ctx, &ic);
9475 visit_cf_list(ctx, &if_stmt->else_list);
9476
9477 end_uniform_if(ctx, &ic);
9478
9479 return !ctx->cf_info.has_branch;
9480 } else { /* non-uniform condition */
9481 /**
9482 * To maintain a logical and linear CFG without critical edges,
9483 * non-uniform conditionals are represented in the following way*) :
9484 *
9485 * The linear CFG:
9486 * BB_IF
9487 * / \
9488 * BB_THEN (logical) BB_THEN (linear)
9489 * \ /
9490 * BB_INVERT (linear)
9491 * / \
9492 * BB_ELSE (logical) BB_ELSE (linear)
9493 * \ /
9494 * BB_ENDIF
9495 *
9496 * The logical CFG:
9497 * BB_IF
9498 * / \
9499 * BB_THEN (logical) BB_ELSE (logical)
9500 * \ /
9501 * BB_ENDIF
9502 *
9503 * *) Exceptions may be due to break and continue statements within loops
9504 **/
9505
9506 begin_divergent_if_then(ctx, &ic, cond);
9507 visit_cf_list(ctx, &if_stmt->then_list);
9508
9509 begin_divergent_if_else(ctx, &ic);
9510 visit_cf_list(ctx, &if_stmt->else_list);
9511
9512 end_divergent_if(ctx, &ic);
9513
9514 return true;
9515 }
9516 }
9517
9518 static bool visit_cf_list(isel_context *ctx,
9519 struct exec_list *list)
9520 {
9521 foreach_list_typed(nir_cf_node, node, node, list) {
9522 switch (node->type) {
9523 case nir_cf_node_block:
9524 visit_block(ctx, nir_cf_node_as_block(node));
9525 break;
9526 case nir_cf_node_if:
9527 if (!visit_if(ctx, nir_cf_node_as_if(node)))
9528 return true;
9529 break;
9530 case nir_cf_node_loop:
9531 visit_loop(ctx, nir_cf_node_as_loop(node));
9532 break;
9533 default:
9534 unreachable("unimplemented cf list type");
9535 }
9536 }
9537 return false;
9538 }
9539
9540 static void create_null_export(isel_context *ctx)
9541 {
9542 /* Some shader stages always need to have exports.
9543 * So when there is none, we need to add a null export.
9544 */
9545
9546 unsigned dest = (ctx->program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
9547 bool vm = (ctx->program->stage & hw_fs) || ctx->program->chip_class >= GFX10;
9548 Builder bld(ctx->program, ctx->block);
9549 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
9550 /* enabled_mask */ 0, dest, /* compr */ false, /* done */ true, vm);
9551 }
9552
9553 static bool export_vs_varying(isel_context *ctx, int slot, bool is_pos, int *next_pos)
9554 {
9555 assert(ctx->stage == vertex_vs ||
9556 ctx->stage == tess_eval_vs ||
9557 ctx->stage == gs_copy_vs ||
9558 ctx->stage == ngg_vertex_gs ||
9559 ctx->stage == ngg_tess_eval_gs);
9560
9561 int offset = (ctx->stage & sw_tes)
9562 ? ctx->program->info->tes.outinfo.vs_output_param_offset[slot]
9563 : ctx->program->info->vs.outinfo.vs_output_param_offset[slot];
9564 uint64_t mask = ctx->outputs.mask[slot];
9565 if (!is_pos && !mask)
9566 return false;
9567 if (!is_pos && offset == AC_EXP_PARAM_UNDEFINED)
9568 return false;
9569 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9570 exp->enabled_mask = mask;
9571 for (unsigned i = 0; i < 4; ++i) {
9572 if (mask & (1 << i))
9573 exp->operands[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9574 else
9575 exp->operands[i] = Operand(v1);
9576 }
9577 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9578 * Setting valid_mask=1 prevents it and has no other effect.
9579 */
9580 exp->valid_mask = ctx->options->chip_class >= GFX10 && is_pos && *next_pos == 0;
9581 exp->done = false;
9582 exp->compressed = false;
9583 if (is_pos)
9584 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9585 else
9586 exp->dest = V_008DFC_SQ_EXP_PARAM + offset;
9587 ctx->block->instructions.emplace_back(std::move(exp));
9588
9589 return true;
9590 }
9591
9592 static void export_vs_psiz_layer_viewport(isel_context *ctx, int *next_pos)
9593 {
9594 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
9595 exp->enabled_mask = 0;
9596 for (unsigned i = 0; i < 4; ++i)
9597 exp->operands[i] = Operand(v1);
9598 if (ctx->outputs.mask[VARYING_SLOT_PSIZ]) {
9599 exp->operands[0] = Operand(ctx->outputs.temps[VARYING_SLOT_PSIZ * 4u]);
9600 exp->enabled_mask |= 0x1;
9601 }
9602 if (ctx->outputs.mask[VARYING_SLOT_LAYER]) {
9603 exp->operands[2] = Operand(ctx->outputs.temps[VARYING_SLOT_LAYER * 4u]);
9604 exp->enabled_mask |= 0x4;
9605 }
9606 if (ctx->outputs.mask[VARYING_SLOT_VIEWPORT]) {
9607 if (ctx->options->chip_class < GFX9) {
9608 exp->operands[3] = Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]);
9609 exp->enabled_mask |= 0x8;
9610 } else {
9611 Builder bld(ctx->program, ctx->block);
9612
9613 Temp out = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u),
9614 Operand(ctx->outputs.temps[VARYING_SLOT_VIEWPORT * 4u]));
9615 if (exp->operands[2].isTemp())
9616 out = bld.vop2(aco_opcode::v_or_b32, bld.def(v1), Operand(out), exp->operands[2]);
9617
9618 exp->operands[2] = Operand(out);
9619 exp->enabled_mask |= 0x4;
9620 }
9621 }
9622 exp->valid_mask = ctx->options->chip_class >= GFX10 && *next_pos == 0;
9623 exp->done = false;
9624 exp->compressed = false;
9625 exp->dest = V_008DFC_SQ_EXP_POS + (*next_pos)++;
9626 ctx->block->instructions.emplace_back(std::move(exp));
9627 }
9628
9629 static void create_export_phis(isel_context *ctx)
9630 {
9631 /* Used when exports are needed, but the output temps are defined in a preceding block.
9632 * This function will set up phis in order to access the outputs in the next block.
9633 */
9634
9635 assert(ctx->block->instructions.back()->opcode == aco_opcode::p_logical_start);
9636 aco_ptr<Instruction> logical_start = aco_ptr<Instruction>(ctx->block->instructions.back().release());
9637 ctx->block->instructions.pop_back();
9638
9639 Builder bld(ctx->program, ctx->block);
9640
9641 for (unsigned slot = 0; slot <= VARYING_SLOT_VAR31; ++slot) {
9642 uint64_t mask = ctx->outputs.mask[slot];
9643 for (unsigned i = 0; i < 4; ++i) {
9644 if (!(mask & (1 << i)))
9645 continue;
9646
9647 Temp old = ctx->outputs.temps[slot * 4 + i];
9648 Temp phi = bld.pseudo(aco_opcode::p_phi, bld.def(v1), old, Operand(v1));
9649 ctx->outputs.temps[slot * 4 + i] = phi;
9650 }
9651 }
9652
9653 bld.insert(std::move(logical_start));
9654 }
9655
9656 static void create_vs_exports(isel_context *ctx)
9657 {
9658 assert(ctx->stage == vertex_vs ||
9659 ctx->stage == tess_eval_vs ||
9660 ctx->stage == gs_copy_vs ||
9661 ctx->stage == ngg_vertex_gs ||
9662 ctx->stage == ngg_tess_eval_gs);
9663
9664 radv_vs_output_info *outinfo = (ctx->stage & sw_tes)
9665 ? &ctx->program->info->tes.outinfo
9666 : &ctx->program->info->vs.outinfo;
9667
9668 if (outinfo->export_prim_id && !(ctx->stage & hw_ngg_gs)) {
9669 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
9670 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = get_arg(ctx, ctx->args->vs_prim_id);
9671 }
9672
9673 if (ctx->options->key.has_multiview_view_index) {
9674 ctx->outputs.mask[VARYING_SLOT_LAYER] |= 0x1;
9675 ctx->outputs.temps[VARYING_SLOT_LAYER * 4u] = as_vgpr(ctx, get_arg(ctx, ctx->args->ac.view_index));
9676 }
9677
9678 /* the order these position exports are created is important */
9679 int next_pos = 0;
9680 bool exported_pos = export_vs_varying(ctx, VARYING_SLOT_POS, true, &next_pos);
9681 if (outinfo->writes_pointsize || outinfo->writes_layer || outinfo->writes_viewport_index) {
9682 export_vs_psiz_layer_viewport(ctx, &next_pos);
9683 exported_pos = true;
9684 }
9685 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9686 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, true, &next_pos);
9687 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9688 exported_pos |= export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, true, &next_pos);
9689
9690 if (ctx->export_clip_dists) {
9691 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
9692 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST0, false, &next_pos);
9693 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
9694 export_vs_varying(ctx, VARYING_SLOT_CLIP_DIST1, false, &next_pos);
9695 }
9696
9697 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
9698 if (i < VARYING_SLOT_VAR0 &&
9699 i != VARYING_SLOT_LAYER &&
9700 i != VARYING_SLOT_PRIMITIVE_ID)
9701 continue;
9702
9703 export_vs_varying(ctx, i, false, NULL);
9704 }
9705
9706 if (!exported_pos)
9707 create_null_export(ctx);
9708 }
9709
9710 static bool export_fs_mrt_z(isel_context *ctx)
9711 {
9712 Builder bld(ctx->program, ctx->block);
9713 unsigned enabled_channels = 0;
9714 bool compr = false;
9715 Operand values[4];
9716
9717 for (unsigned i = 0; i < 4; ++i) {
9718 values[i] = Operand(v1);
9719 }
9720
9721 /* Both stencil and sample mask only need 16-bits. */
9722 if (!ctx->program->info->ps.writes_z &&
9723 (ctx->program->info->ps.writes_stencil ||
9724 ctx->program->info->ps.writes_sample_mask)) {
9725 compr = true; /* COMPR flag */
9726
9727 if (ctx->program->info->ps.writes_stencil) {
9728 /* Stencil should be in X[23:16]. */
9729 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9730 values[0] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(16u), values[0]);
9731 enabled_channels |= 0x3;
9732 }
9733
9734 if (ctx->program->info->ps.writes_sample_mask) {
9735 /* SampleMask should be in Y[15:0]. */
9736 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9737 enabled_channels |= 0xc;
9738 }
9739 } else {
9740 if (ctx->program->info->ps.writes_z) {
9741 values[0] = Operand(ctx->outputs.temps[FRAG_RESULT_DEPTH * 4u]);
9742 enabled_channels |= 0x1;
9743 }
9744
9745 if (ctx->program->info->ps.writes_stencil) {
9746 values[1] = Operand(ctx->outputs.temps[FRAG_RESULT_STENCIL * 4u]);
9747 enabled_channels |= 0x2;
9748 }
9749
9750 if (ctx->program->info->ps.writes_sample_mask) {
9751 values[2] = Operand(ctx->outputs.temps[FRAG_RESULT_SAMPLE_MASK * 4u]);
9752 enabled_channels |= 0x4;
9753 }
9754 }
9755
9756 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9757 * writemask component.
9758 */
9759 if (ctx->options->chip_class == GFX6 &&
9760 ctx->options->family != CHIP_OLAND &&
9761 ctx->options->family != CHIP_HAINAN) {
9762 enabled_channels |= 0x1;
9763 }
9764
9765 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9766 enabled_channels, V_008DFC_SQ_EXP_MRTZ, compr);
9767
9768 return true;
9769 }
9770
9771 static bool export_fs_mrt_color(isel_context *ctx, int slot)
9772 {
9773 Builder bld(ctx->program, ctx->block);
9774 unsigned write_mask = ctx->outputs.mask[slot];
9775 Operand values[4];
9776
9777 for (unsigned i = 0; i < 4; ++i) {
9778 if (write_mask & (1 << i)) {
9779 values[i] = Operand(ctx->outputs.temps[slot * 4u + i]);
9780 } else {
9781 values[i] = Operand(v1);
9782 }
9783 }
9784
9785 unsigned target, col_format;
9786 unsigned enabled_channels = 0;
9787 aco_opcode compr_op = (aco_opcode)0;
9788
9789 slot -= FRAG_RESULT_DATA0;
9790 target = V_008DFC_SQ_EXP_MRT + slot;
9791 col_format = (ctx->options->key.fs.col_format >> (4 * slot)) & 0xf;
9792
9793 bool is_int8 = (ctx->options->key.fs.is_int8 >> slot) & 1;
9794 bool is_int10 = (ctx->options->key.fs.is_int10 >> slot) & 1;
9795
9796 switch (col_format)
9797 {
9798 case V_028714_SPI_SHADER_ZERO:
9799 enabled_channels = 0; /* writemask */
9800 target = V_008DFC_SQ_EXP_NULL;
9801 break;
9802
9803 case V_028714_SPI_SHADER_32_R:
9804 enabled_channels = 1;
9805 break;
9806
9807 case V_028714_SPI_SHADER_32_GR:
9808 enabled_channels = 0x3;
9809 break;
9810
9811 case V_028714_SPI_SHADER_32_AR:
9812 if (ctx->options->chip_class >= GFX10) {
9813 /* Special case: on GFX10, the outputs are different for 32_AR */
9814 enabled_channels = 0x3;
9815 values[1] = values[3];
9816 values[3] = Operand(v1);
9817 } else {
9818 enabled_channels = 0x9;
9819 }
9820 break;
9821
9822 case V_028714_SPI_SHADER_FP16_ABGR:
9823 enabled_channels = 0x5;
9824 compr_op = aco_opcode::v_cvt_pkrtz_f16_f32;
9825 break;
9826
9827 case V_028714_SPI_SHADER_UNORM16_ABGR:
9828 enabled_channels = 0x5;
9829 compr_op = aco_opcode::v_cvt_pknorm_u16_f32;
9830 break;
9831
9832 case V_028714_SPI_SHADER_SNORM16_ABGR:
9833 enabled_channels = 0x5;
9834 compr_op = aco_opcode::v_cvt_pknorm_i16_f32;
9835 break;
9836
9837 case V_028714_SPI_SHADER_UINT16_ABGR: {
9838 enabled_channels = 0x5;
9839 compr_op = aco_opcode::v_cvt_pk_u16_u32;
9840 if (is_int8 || is_int10) {
9841 /* clamp */
9842 uint32_t max_rgb = is_int8 ? 255 : is_int10 ? 1023 : 0;
9843 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9844
9845 for (unsigned i = 0; i < 4; i++) {
9846 if ((write_mask >> i) & 1) {
9847 values[i] = bld.vop2(aco_opcode::v_min_u32, bld.def(v1),
9848 i == 3 && is_int10 ? Operand(3u) : Operand(max_rgb_val),
9849 values[i]);
9850 }
9851 }
9852 }
9853 break;
9854 }
9855
9856 case V_028714_SPI_SHADER_SINT16_ABGR:
9857 enabled_channels = 0x5;
9858 compr_op = aco_opcode::v_cvt_pk_i16_i32;
9859 if (is_int8 || is_int10) {
9860 /* clamp */
9861 uint32_t max_rgb = is_int8 ? 127 : is_int10 ? 511 : 0;
9862 uint32_t min_rgb = is_int8 ? -128 :is_int10 ? -512 : 0;
9863 Temp max_rgb_val = bld.copy(bld.def(s1), Operand(max_rgb));
9864 Temp min_rgb_val = bld.copy(bld.def(s1), Operand(min_rgb));
9865
9866 for (unsigned i = 0; i < 4; i++) {
9867 if ((write_mask >> i) & 1) {
9868 values[i] = bld.vop2(aco_opcode::v_min_i32, bld.def(v1),
9869 i == 3 && is_int10 ? Operand(1u) : Operand(max_rgb_val),
9870 values[i]);
9871 values[i] = bld.vop2(aco_opcode::v_max_i32, bld.def(v1),
9872 i == 3 && is_int10 ? Operand(-2u) : Operand(min_rgb_val),
9873 values[i]);
9874 }
9875 }
9876 }
9877 break;
9878
9879 case V_028714_SPI_SHADER_32_ABGR:
9880 enabled_channels = 0xF;
9881 break;
9882
9883 default:
9884 break;
9885 }
9886
9887 if (target == V_008DFC_SQ_EXP_NULL)
9888 return false;
9889
9890 if ((bool) compr_op) {
9891 for (int i = 0; i < 2; i++) {
9892 /* check if at least one of the values to be compressed is enabled */
9893 unsigned enabled = (write_mask >> (i*2) | write_mask >> (i*2+1)) & 0x1;
9894 if (enabled) {
9895 enabled_channels |= enabled << (i*2);
9896 values[i] = bld.vop3(compr_op, bld.def(v1),
9897 values[i*2].isUndefined() ? Operand(0u) : values[i*2],
9898 values[i*2+1].isUndefined() ? Operand(0u): values[i*2+1]);
9899 } else {
9900 values[i] = Operand(v1);
9901 }
9902 }
9903 values[2] = Operand(v1);
9904 values[3] = Operand(v1);
9905 } else {
9906 for (int i = 0; i < 4; i++)
9907 values[i] = enabled_channels & (1 << i) ? values[i] : Operand(v1);
9908 }
9909
9910 bld.exp(aco_opcode::exp, values[0], values[1], values[2], values[3],
9911 enabled_channels, target, (bool) compr_op);
9912 return true;
9913 }
9914
9915 static void create_fs_exports(isel_context *ctx)
9916 {
9917 bool exported = false;
9918
9919 /* Export depth, stencil and sample mask. */
9920 if (ctx->outputs.mask[FRAG_RESULT_DEPTH] ||
9921 ctx->outputs.mask[FRAG_RESULT_STENCIL] ||
9922 ctx->outputs.mask[FRAG_RESULT_SAMPLE_MASK])
9923 exported |= export_fs_mrt_z(ctx);
9924
9925 /* Export all color render targets. */
9926 for (unsigned i = FRAG_RESULT_DATA0; i < FRAG_RESULT_DATA7 + 1; ++i)
9927 if (ctx->outputs.mask[i])
9928 exported |= export_fs_mrt_color(ctx, i);
9929
9930 if (!exported)
9931 create_null_export(ctx);
9932 }
9933
9934 static void write_tcs_tess_factors(isel_context *ctx)
9935 {
9936 unsigned outer_comps;
9937 unsigned inner_comps;
9938
9939 switch (ctx->args->options->key.tcs.primitive_mode) {
9940 case GL_ISOLINES:
9941 outer_comps = 2;
9942 inner_comps = 0;
9943 break;
9944 case GL_TRIANGLES:
9945 outer_comps = 3;
9946 inner_comps = 1;
9947 break;
9948 case GL_QUADS:
9949 outer_comps = 4;
9950 inner_comps = 2;
9951 break;
9952 default:
9953 return;
9954 }
9955
9956 Builder bld(ctx->program, ctx->block);
9957
9958 bld.barrier(aco_opcode::p_memory_barrier_shared);
9959 if (unlikely(ctx->program->chip_class != GFX6 && ctx->program->workgroup_size > ctx->program->wave_size))
9960 bld.sopp(aco_opcode::s_barrier);
9961
9962 Temp tcs_rel_ids = get_arg(ctx, ctx->args->ac.tcs_rel_ids);
9963 Temp invocation_id = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), tcs_rel_ids, Operand(8u), Operand(5u));
9964
9965 Temp invocation_id_is_zero = bld.vopc(aco_opcode::v_cmp_eq_u32, bld.hint_vcc(bld.def(bld.lm)), Operand(0u), invocation_id);
9966 if_context ic_invocation_id_is_zero;
9967 begin_divergent_if_then(ctx, &ic_invocation_id_is_zero, invocation_id_is_zero);
9968 bld.reset(ctx->block);
9969
9970 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));
9971
9972 std::pair<Temp, unsigned> lds_base = get_tcs_output_lds_offset(ctx);
9973 unsigned stride = inner_comps + outer_comps;
9974 unsigned lds_align = calculate_lds_alignment(ctx, lds_base.second);
9975 Temp tf_inner_vec;
9976 Temp tf_outer_vec;
9977 Temp out[6];
9978 assert(stride <= (sizeof(out) / sizeof(Temp)));
9979
9980 if (ctx->args->options->key.tcs.primitive_mode == GL_ISOLINES) {
9981 // LINES reversal
9982 tf_outer_vec = load_lds(ctx, 4, bld.tmp(v2), lds_base.first, lds_base.second + ctx->tcs_tess_lvl_out_loc, lds_align);
9983 out[1] = emit_extract_vector(ctx, tf_outer_vec, 0, v1);
9984 out[0] = emit_extract_vector(ctx, tf_outer_vec, 1, v1);
9985 } else {
9986 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);
9987 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);
9988
9989 for (unsigned i = 0; i < outer_comps; ++i)
9990 out[i] = emit_extract_vector(ctx, tf_outer_vec, i, v1);
9991 for (unsigned i = 0; i < inner_comps; ++i)
9992 out[outer_comps + i] = emit_extract_vector(ctx, tf_inner_vec, i, v1);
9993 }
9994
9995 Temp rel_patch_id = get_tess_rel_patch_id(ctx);
9996 Temp tf_base = get_arg(ctx, ctx->args->tess_factor_offset);
9997 Temp byte_offset = bld.v_mul_imm(bld.def(v1), rel_patch_id, stride * 4u);
9998 unsigned tf_const_offset = 0;
9999
10000 if (ctx->program->chip_class <= GFX8) {
10001 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);
10002 if_context ic_rel_patch_id_is_zero;
10003 begin_divergent_if_then(ctx, &ic_rel_patch_id_is_zero, rel_patch_id_is_zero);
10004 bld.reset(ctx->block);
10005
10006 /* Store the dynamic HS control word. */
10007 Temp control_word = bld.copy(bld.def(v1), Operand(0x80000000u));
10008 bld.mubuf(aco_opcode::buffer_store_dword,
10009 /* SRSRC */ hs_ring_tess_factor, /* VADDR */ Operand(v1), /* SOFFSET */ tf_base, /* VDATA */ control_word,
10010 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10011 /* disable_wqm */ false, /* glc */ true);
10012 tf_const_offset += 4;
10013
10014 begin_divergent_if_else(ctx, &ic_rel_patch_id_is_zero);
10015 end_divergent_if(ctx, &ic_rel_patch_id_is_zero);
10016 bld.reset(ctx->block);
10017 }
10018
10019 assert(stride == 2 || stride == 4 || stride == 6);
10020 Temp tf_vec = create_vec_from_array(ctx, out, stride, RegType::vgpr, 4u);
10021 store_vmem_mubuf(ctx, tf_vec, hs_ring_tess_factor, byte_offset, tf_base, tf_const_offset, 4, (1 << stride) - 1, true, false);
10022
10023 /* Store to offchip for TES to read - only if TES reads them */
10024 if (ctx->args->options->key.tcs.tes_reads_tess_factors) {
10025 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));
10026 Temp oc_lds = get_arg(ctx, ctx->args->oc_lds);
10027
10028 std::pair<Temp, unsigned> vmem_offs_outer = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_out_loc);
10029 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);
10030
10031 if (likely(inner_comps)) {
10032 std::pair<Temp, unsigned> vmem_offs_inner = get_tcs_per_patch_output_vmem_offset(ctx, nullptr, ctx->tcs_tess_lvl_in_loc);
10033 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);
10034 }
10035 }
10036
10037 begin_divergent_if_else(ctx, &ic_invocation_id_is_zero);
10038 end_divergent_if(ctx, &ic_invocation_id_is_zero);
10039 }
10040
10041 static void emit_stream_output(isel_context *ctx,
10042 Temp const *so_buffers,
10043 Temp const *so_write_offset,
10044 const struct radv_stream_output *output)
10045 {
10046 unsigned num_comps = util_bitcount(output->component_mask);
10047 unsigned writemask = (1 << num_comps) - 1;
10048 unsigned loc = output->location;
10049 unsigned buf = output->buffer;
10050
10051 assert(num_comps && num_comps <= 4);
10052 if (!num_comps || num_comps > 4)
10053 return;
10054
10055 unsigned start = ffs(output->component_mask) - 1;
10056
10057 Temp out[4];
10058 bool all_undef = true;
10059 assert(ctx->stage == vertex_vs || ctx->stage == gs_copy_vs);
10060 for (unsigned i = 0; i < num_comps; i++) {
10061 out[i] = ctx->outputs.temps[loc * 4 + start + i];
10062 all_undef = all_undef && !out[i].id();
10063 }
10064 if (all_undef)
10065 return;
10066
10067 while (writemask) {
10068 int start, count;
10069 u_bit_scan_consecutive_range(&writemask, &start, &count);
10070 if (count == 3 && ctx->options->chip_class == GFX6) {
10071 /* GFX6 doesn't support storing vec3, split it. */
10072 writemask |= 1u << (start + 2);
10073 count = 2;
10074 }
10075
10076 unsigned offset = output->offset + start * 4;
10077
10078 Temp write_data = {ctx->program->allocateId(), RegClass(RegType::vgpr, count)};
10079 aco_ptr<Pseudo_instruction> vec{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, count, 1)};
10080 for (int i = 0; i < count; ++i)
10081 vec->operands[i] = (ctx->outputs.mask[loc] & 1 << (start + i)) ? Operand(out[start + i]) : Operand(0u);
10082 vec->definitions[0] = Definition(write_data);
10083 ctx->block->instructions.emplace_back(std::move(vec));
10084
10085 aco_opcode opcode;
10086 switch (count) {
10087 case 1:
10088 opcode = aco_opcode::buffer_store_dword;
10089 break;
10090 case 2:
10091 opcode = aco_opcode::buffer_store_dwordx2;
10092 break;
10093 case 3:
10094 opcode = aco_opcode::buffer_store_dwordx3;
10095 break;
10096 case 4:
10097 opcode = aco_opcode::buffer_store_dwordx4;
10098 break;
10099 default:
10100 unreachable("Unsupported dword count.");
10101 }
10102
10103 aco_ptr<MUBUF_instruction> store{create_instruction<MUBUF_instruction>(opcode, Format::MUBUF, 4, 0)};
10104 store->operands[0] = Operand(so_buffers[buf]);
10105 store->operands[1] = Operand(so_write_offset[buf]);
10106 store->operands[2] = Operand((uint32_t) 0);
10107 store->operands[3] = Operand(write_data);
10108 if (offset > 4095) {
10109 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10110 Builder bld(ctx->program, ctx->block);
10111 store->operands[0] = bld.vadd32(bld.def(v1), Operand(offset), Operand(so_write_offset[buf]));
10112 } else {
10113 store->offset = offset;
10114 }
10115 store->offen = true;
10116 store->glc = true;
10117 store->dlc = false;
10118 store->slc = true;
10119 store->can_reorder = true;
10120 ctx->block->instructions.emplace_back(std::move(store));
10121 }
10122 }
10123
10124 static void emit_streamout(isel_context *ctx, unsigned stream)
10125 {
10126 Builder bld(ctx->program, ctx->block);
10127
10128 Temp so_buffers[4];
10129 Temp buf_ptr = convert_pointer_to_64_bit(ctx, get_arg(ctx, ctx->args->streamout_buffers));
10130 for (unsigned i = 0; i < 4; i++) {
10131 unsigned stride = ctx->program->info->so.strides[i];
10132 if (!stride)
10133 continue;
10134
10135 Operand off = bld.copy(bld.def(s1), Operand(i * 16u));
10136 so_buffers[i] = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), buf_ptr, off);
10137 }
10138
10139 Temp so_vtx_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10140 get_arg(ctx, ctx->args->streamout_config), Operand(0x70010u));
10141
10142 Temp tid = emit_mbcnt(ctx, bld.def(v1));
10143
10144 Temp can_emit = bld.vopc(aco_opcode::v_cmp_gt_i32, bld.def(bld.lm), so_vtx_count, tid);
10145
10146 if_context ic;
10147 begin_divergent_if_then(ctx, &ic, can_emit);
10148
10149 bld.reset(ctx->block);
10150
10151 Temp so_write_index = bld.vadd32(bld.def(v1), get_arg(ctx, ctx->args->streamout_write_idx), tid);
10152
10153 Temp so_write_offset[4];
10154
10155 for (unsigned i = 0; i < 4; i++) {
10156 unsigned stride = ctx->program->info->so.strides[i];
10157 if (!stride)
10158 continue;
10159
10160 if (stride == 1) {
10161 Temp offset = bld.sop2(aco_opcode::s_add_i32, bld.def(s1), bld.def(s1, scc),
10162 get_arg(ctx, ctx->args->streamout_write_idx),
10163 get_arg(ctx, ctx->args->streamout_offset[i]));
10164 Temp new_offset = bld.vadd32(bld.def(v1), offset, tid);
10165
10166 so_write_offset[i] = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), new_offset);
10167 } else {
10168 Temp offset = bld.v_mul_imm(bld.def(v1), so_write_index, stride * 4u);
10169 Temp offset2 = bld.sop2(aco_opcode::s_mul_i32, bld.def(s1), Operand(4u),
10170 get_arg(ctx, ctx->args->streamout_offset[i]));
10171 so_write_offset[i] = bld.vadd32(bld.def(v1), offset, offset2);
10172 }
10173 }
10174
10175 for (unsigned i = 0; i < ctx->program->info->so.num_outputs; i++) {
10176 struct radv_stream_output *output =
10177 &ctx->program->info->so.outputs[i];
10178 if (stream != output->stream)
10179 continue;
10180
10181 emit_stream_output(ctx, so_buffers, so_write_offset, output);
10182 }
10183
10184 begin_divergent_if_else(ctx, &ic);
10185 end_divergent_if(ctx, &ic);
10186 }
10187
10188 } /* end namespace */
10189
10190 void fix_ls_vgpr_init_bug(isel_context *ctx, Pseudo_instruction *startpgm)
10191 {
10192 assert(ctx->shader->info.stage == MESA_SHADER_VERTEX);
10193 Builder bld(ctx->program, ctx->block);
10194 constexpr unsigned hs_idx = 1u;
10195 Builder::Result hs_thread_count = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10196 get_arg(ctx, ctx->args->merged_wave_info),
10197 Operand((8u << 16) | (hs_idx * 8u)));
10198 Temp ls_has_nonzero_hs_threads = bool_to_vector_condition(ctx, hs_thread_count.def(1).getTemp());
10199
10200 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10201
10202 Temp instance_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10203 get_arg(ctx, ctx->args->rel_auto_id),
10204 get_arg(ctx, ctx->args->ac.instance_id),
10205 ls_has_nonzero_hs_threads);
10206 Temp rel_auto_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10207 get_arg(ctx, ctx->args->ac.tcs_rel_ids),
10208 get_arg(ctx, ctx->args->rel_auto_id),
10209 ls_has_nonzero_hs_threads);
10210 Temp vertex_id = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10211 get_arg(ctx, ctx->args->ac.tcs_patch_id),
10212 get_arg(ctx, ctx->args->ac.vertex_id),
10213 ls_has_nonzero_hs_threads);
10214
10215 ctx->arg_temps[ctx->args->ac.instance_id.arg_index] = instance_id;
10216 ctx->arg_temps[ctx->args->rel_auto_id.arg_index] = rel_auto_id;
10217 ctx->arg_temps[ctx->args->ac.vertex_id.arg_index] = vertex_id;
10218 }
10219
10220 void split_arguments(isel_context *ctx, Pseudo_instruction *startpgm)
10221 {
10222 /* Split all arguments except for the first (ring_offsets) and the last
10223 * (exec) so that the dead channels don't stay live throughout the program.
10224 */
10225 for (int i = 1; i < startpgm->definitions.size() - 1; i++) {
10226 if (startpgm->definitions[i].regClass().size() > 1) {
10227 emit_split_vector(ctx, startpgm->definitions[i].getTemp(),
10228 startpgm->definitions[i].regClass().size());
10229 }
10230 }
10231 }
10232
10233 void handle_bc_optimize(isel_context *ctx)
10234 {
10235 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10236 Builder bld(ctx->program, ctx->block);
10237 uint32_t spi_ps_input_ena = ctx->program->config->spi_ps_input_ena;
10238 bool uses_center = G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena);
10239 bool uses_centroid = G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena);
10240 ctx->persp_centroid = get_arg(ctx, ctx->args->ac.persp_centroid);
10241 ctx->linear_centroid = get_arg(ctx, ctx->args->ac.linear_centroid);
10242 if (uses_center && uses_centroid) {
10243 Temp sel = bld.vopc_e64(aco_opcode::v_cmp_lt_i32, bld.hint_vcc(bld.def(bld.lm)),
10244 get_arg(ctx, ctx->args->ac.prim_mask), Operand(0u));
10245
10246 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena)) {
10247 Temp new_coord[2];
10248 for (unsigned i = 0; i < 2; i++) {
10249 Temp persp_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_centroid), i, v1);
10250 Temp persp_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.persp_center), i, v1);
10251 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10252 persp_centroid, persp_center, sel);
10253 }
10254 ctx->persp_centroid = bld.tmp(v2);
10255 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->persp_centroid),
10256 Operand(new_coord[0]), Operand(new_coord[1]));
10257 emit_split_vector(ctx, ctx->persp_centroid, 2);
10258 }
10259
10260 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena)) {
10261 Temp new_coord[2];
10262 for (unsigned i = 0; i < 2; i++) {
10263 Temp linear_centroid = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_centroid), i, v1);
10264 Temp linear_center = emit_extract_vector(ctx, get_arg(ctx, ctx->args->ac.linear_center), i, v1);
10265 new_coord[i] = bld.vop2(aco_opcode::v_cndmask_b32, bld.def(v1),
10266 linear_centroid, linear_center, sel);
10267 }
10268 ctx->linear_centroid = bld.tmp(v2);
10269 bld.pseudo(aco_opcode::p_create_vector, Definition(ctx->linear_centroid),
10270 Operand(new_coord[0]), Operand(new_coord[1]));
10271 emit_split_vector(ctx, ctx->linear_centroid, 2);
10272 }
10273 }
10274 }
10275
10276 void setup_fp_mode(isel_context *ctx, nir_shader *shader)
10277 {
10278 Program *program = ctx->program;
10279
10280 unsigned float_controls = shader->info.float_controls_execution_mode;
10281
10282 program->next_fp_mode.preserve_signed_zero_inf_nan32 =
10283 float_controls & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32;
10284 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 =
10285 float_controls & (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16 |
10286 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
10287
10288 program->next_fp_mode.must_flush_denorms32 =
10289 float_controls & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32;
10290 program->next_fp_mode.must_flush_denorms16_64 =
10291 float_controls & (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 |
10292 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
10293
10294 program->next_fp_mode.care_about_round32 =
10295 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32);
10296
10297 program->next_fp_mode.care_about_round16_64 =
10298 float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64 |
10299 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
10300
10301 /* default to preserving fp16 and fp64 denorms, since it's free */
10302 if (program->next_fp_mode.must_flush_denorms16_64)
10303 program->next_fp_mode.denorm16_64 = 0;
10304 else
10305 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10306
10307 /* preserving fp32 denorms is expensive, so only do it if asked */
10308 if (float_controls & FLOAT_CONTROLS_DENORM_PRESERVE_FP32)
10309 program->next_fp_mode.denorm32 = fp_denorm_keep;
10310 else
10311 program->next_fp_mode.denorm32 = 0;
10312
10313 if (float_controls & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32)
10314 program->next_fp_mode.round32 = fp_round_tz;
10315 else
10316 program->next_fp_mode.round32 = fp_round_ne;
10317
10318 if (float_controls & (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 | FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64))
10319 program->next_fp_mode.round16_64 = fp_round_tz;
10320 else
10321 program->next_fp_mode.round16_64 = fp_round_ne;
10322
10323 ctx->block->fp_mode = program->next_fp_mode;
10324 }
10325
10326 void cleanup_cfg(Program *program)
10327 {
10328 /* create linear_succs/logical_succs */
10329 for (Block& BB : program->blocks) {
10330 for (unsigned idx : BB.linear_preds)
10331 program->blocks[idx].linear_succs.emplace_back(BB.index);
10332 for (unsigned idx : BB.logical_preds)
10333 program->blocks[idx].logical_succs.emplace_back(BB.index);
10334 }
10335 }
10336
10337 Temp merged_wave_info_to_mask(isel_context *ctx, unsigned i)
10338 {
10339 Builder bld(ctx->program, ctx->block);
10340
10341 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10342 Temp count = i == 0
10343 ? get_arg(ctx, ctx->args->merged_wave_info)
10344 : bld.sop2(aco_opcode::s_lshr_b32, bld.def(s1), bld.def(s1, scc),
10345 get_arg(ctx, ctx->args->merged_wave_info), Operand(i * 8u));
10346
10347 Temp mask = bld.sop2(aco_opcode::s_bfm_b64, bld.def(s2), count, Operand(0u));
10348 Temp cond;
10349
10350 if (ctx->program->wave_size == 64) {
10351 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10352 Temp active_64 = bld.sopc(aco_opcode::s_bitcmp1_b32, bld.def(s1, scc), count, Operand(6u /* log2(64) */));
10353 cond = bld.sop2(Builder::s_cselect, bld.def(bld.lm), Operand(-1u), mask, bld.scc(active_64));
10354 } else {
10355 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10356 cond = emit_extract_vector(ctx, mask, 0, bld.lm);
10357 }
10358
10359 return cond;
10360 }
10361
10362 bool ngg_early_prim_export(isel_context *ctx)
10363 {
10364 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10365 return true;
10366 }
10367
10368 void ngg_emit_sendmsg_gs_alloc_req(isel_context *ctx)
10369 {
10370 Builder bld(ctx->program, ctx->block);
10371
10372 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10373 bld.sopp(aco_opcode::s_setprio, -1u, 0x3u);
10374
10375 /* Get the id of the current wave within the threadgroup (workgroup) */
10376 Builder::Result wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10377 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10378
10379 /* Execute the following code only on the first wave (wave id 0),
10380 * use the SCC def to tell if the wave id is zero or not.
10381 */
10382 Temp cond = wave_id_in_tg.def(1).getTemp();
10383 if_context ic;
10384 begin_uniform_if_then(ctx, &ic, cond);
10385 begin_uniform_if_else(ctx, &ic);
10386 bld.reset(ctx->block);
10387
10388 /* Number of vertices output by VS/TES */
10389 Temp vtx_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10390 get_arg(ctx, ctx->args->gs_tg_info), Operand(12u | (9u << 16u)));
10391 /* Number of primitives output by VS/TES */
10392 Temp prm_cnt = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10393 get_arg(ctx, ctx->args->gs_tg_info), Operand(22u | (9u << 16u)));
10394
10395 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10396 Temp tmp = bld.sop2(aco_opcode::s_lshl_b32, bld.def(s1), bld.def(s1, scc), prm_cnt, Operand(12u));
10397 tmp = bld.sop2(aco_opcode::s_or_b32, bld.m0(bld.def(s1)), bld.def(s1, scc), tmp, vtx_cnt);
10398
10399 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10400 bld.sopp(aco_opcode::s_sendmsg, bld.m0(tmp), -1, sendmsg_gs_alloc_req);
10401
10402 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10403 bld.sopp(aco_opcode::s_setprio, -1u, 0x0u);
10404
10405 end_uniform_if(ctx, &ic);
10406 }
10407
10408 Temp ngg_get_prim_exp_arg(isel_context *ctx, unsigned num_vertices, const Temp vtxindex[])
10409 {
10410 Builder bld(ctx->program, ctx->block);
10411
10412 if (ctx->args->options->key.vs_common_out.as_ngg_passthrough) {
10413 return get_arg(ctx, ctx->args->gs_vtx_offset[0]);
10414 }
10415
10416 Temp gs_invocation_id = get_arg(ctx, ctx->args->ac.gs_invocation_id);
10417 Temp tmp;
10418
10419 for (unsigned i = 0; i < num_vertices; ++i) {
10420 assert(vtxindex[i].id());
10421
10422 if (i)
10423 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), vtxindex[i], Operand(10u * i), tmp);
10424 else
10425 tmp = vtxindex[i];
10426
10427 /* The initial edge flag is always false in tess eval shaders. */
10428 if (ctx->stage == ngg_vertex_gs) {
10429 Temp edgeflag = bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1), gs_invocation_id, Operand(8 + i), Operand(1u));
10430 tmp = bld.vop3(aco_opcode::v_lshl_add_u32, bld.def(v1), edgeflag, Operand(10u * i + 9u), tmp);
10431 }
10432 }
10433
10434 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10435
10436 return tmp;
10437 }
10438
10439 void ngg_emit_prim_export(isel_context *ctx, unsigned num_vertices_per_primitive, const Temp vtxindex[])
10440 {
10441 Builder bld(ctx->program, ctx->block);
10442 Temp prim_exp_arg = ngg_get_prim_exp_arg(ctx, num_vertices_per_primitive, vtxindex);
10443
10444 bld.exp(aco_opcode::exp, prim_exp_arg, Operand(v1), Operand(v1), Operand(v1),
10445 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM /* dest */,
10446 false /* compressed */, true/* done */, false /* valid mask */);
10447 }
10448
10449 void ngg_emit_nogs_gsthreads(isel_context *ctx)
10450 {
10451 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10452 * These must always come before VS exports.
10453 *
10454 * It is recommended to do these as early as possible. They can be at the beginning when
10455 * there is no SW GS and the shader doesn't write edge flags.
10456 */
10457
10458 if_context ic;
10459 Temp is_gs_thread = merged_wave_info_to_mask(ctx, 1);
10460 begin_divergent_if_then(ctx, &ic, is_gs_thread);
10461
10462 Builder bld(ctx->program, ctx->block);
10463 constexpr unsigned max_vertices_per_primitive = 3;
10464 unsigned num_vertices_per_primitive = max_vertices_per_primitive;
10465
10466 if (ctx->stage == ngg_vertex_gs) {
10467 /* TODO: optimize for points & lines */
10468 } else if (ctx->stage == ngg_tess_eval_gs) {
10469 if (ctx->shader->info.tess.point_mode)
10470 num_vertices_per_primitive = 1;
10471 else if (ctx->shader->info.tess.primitive_mode == GL_ISOLINES)
10472 num_vertices_per_primitive = 2;
10473 } else {
10474 unreachable("Unsupported NGG shader stage");
10475 }
10476
10477 Temp vtxindex[max_vertices_per_primitive];
10478 vtxindex[0] = bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10479 get_arg(ctx, ctx->args->gs_vtx_offset[0]));
10480 vtxindex[1] = num_vertices_per_primitive < 2 ? Temp(0, v1) :
10481 bld.vop3(aco_opcode::v_bfe_u32, bld.def(v1),
10482 get_arg(ctx, ctx->args->gs_vtx_offset[0]), Operand(16u), Operand(16u));
10483 vtxindex[2] = num_vertices_per_primitive < 3 ? Temp(0, v1) :
10484 bld.vop2(aco_opcode::v_and_b32, bld.def(v1), Operand(0xffffu),
10485 get_arg(ctx, ctx->args->gs_vtx_offset[2]));
10486
10487 /* Export primitive data to the index buffer. */
10488 ngg_emit_prim_export(ctx, num_vertices_per_primitive, vtxindex);
10489
10490 /* Export primitive ID. */
10491 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
10492 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10493 Temp prim_id = get_arg(ctx, ctx->args->ac.gs_prim_id);
10494 Temp provoking_vtx_index = vtxindex[0];
10495 Temp addr = bld.v_mul_imm(bld.def(v1), provoking_vtx_index, 4u);
10496
10497 store_lds(ctx, 4, prim_id, 0x1u, addr, 0u, 4u);
10498 }
10499
10500 begin_divergent_if_else(ctx, &ic);
10501 end_divergent_if(ctx, &ic);
10502 }
10503
10504 void ngg_emit_nogs_output(isel_context *ctx)
10505 {
10506 /* Emits NGG GS output, for stages that don't have SW GS. */
10507
10508 if_context ic;
10509 Builder bld(ctx->program, ctx->block);
10510 bool late_prim_export = !ngg_early_prim_export(ctx);
10511
10512 /* NGG streamout is currently disabled by default. */
10513 assert(!ctx->args->shader_info->so.num_outputs);
10514
10515 if (late_prim_export) {
10516 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10517 create_export_phis(ctx);
10518 /* Do what we need to do in the GS threads. */
10519 ngg_emit_nogs_gsthreads(ctx);
10520
10521 /* What comes next should be executed on ES threads. */
10522 Temp is_es_thread = merged_wave_info_to_mask(ctx, 0);
10523 begin_divergent_if_then(ctx, &ic, is_es_thread);
10524 bld.reset(ctx->block);
10525 }
10526
10527 /* Export VS outputs */
10528 ctx->block->kind |= block_kind_export_end;
10529 create_vs_exports(ctx);
10530
10531 /* Export primitive ID */
10532 if (ctx->args->options->key.vs_common_out.export_prim_id) {
10533 Temp prim_id;
10534
10535 if (ctx->stage == ngg_vertex_gs) {
10536 /* Wait for GS threads to store primitive ID in LDS. */
10537 bld.barrier(aco_opcode::p_memory_barrier_shared);
10538 bld.sopp(aco_opcode::s_barrier);
10539
10540 /* Calculate LDS address where the GS threads stored the primitive ID. */
10541 Temp wave_id_in_tg = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10542 get_arg(ctx, ctx->args->merged_wave_info), Operand(24u | (4u << 16)));
10543 Temp thread_id_in_wave = emit_mbcnt(ctx, bld.def(v1));
10544 Temp wave_id_mul = bld.v_mul_imm(bld.def(v1), as_vgpr(ctx, wave_id_in_tg), ctx->program->wave_size);
10545 Temp thread_id_in_tg = bld.vadd32(bld.def(v1), Operand(wave_id_mul), Operand(thread_id_in_wave));
10546 Temp addr = bld.v_mul_imm(bld.def(v1), thread_id_in_tg, 4u);
10547
10548 /* Load primitive ID from LDS. */
10549 prim_id = load_lds(ctx, 4, bld.tmp(v1), addr, 0u, 4u);
10550 } else if (ctx->stage == ngg_tess_eval_gs) {
10551 /* TES: Just use the patch ID as the primitive ID. */
10552 prim_id = get_arg(ctx, ctx->args->ac.tes_patch_id);
10553 } else {
10554 unreachable("unsupported NGG shader stage.");
10555 }
10556
10557 ctx->outputs.mask[VARYING_SLOT_PRIMITIVE_ID] |= 0x1;
10558 ctx->outputs.temps[VARYING_SLOT_PRIMITIVE_ID * 4u] = prim_id;
10559
10560 export_vs_varying(ctx, VARYING_SLOT_PRIMITIVE_ID, false, nullptr);
10561 }
10562
10563 if (late_prim_export) {
10564 begin_divergent_if_else(ctx, &ic);
10565 end_divergent_if(ctx, &ic);
10566 bld.reset(ctx->block);
10567 }
10568 }
10569
10570 void select_program(Program *program,
10571 unsigned shader_count,
10572 struct nir_shader *const *shaders,
10573 ac_shader_config* config,
10574 struct radv_shader_args *args)
10575 {
10576 isel_context ctx = setup_isel_context(program, shader_count, shaders, config, args, false);
10577 if_context ic_merged_wave_info;
10578 bool ngg_no_gs = ctx.stage == ngg_vertex_gs || ctx.stage == ngg_tess_eval_gs;
10579
10580 for (unsigned i = 0; i < shader_count; i++) {
10581 nir_shader *nir = shaders[i];
10582 init_context(&ctx, nir);
10583
10584 setup_fp_mode(&ctx, nir);
10585
10586 if (!i) {
10587 /* needs to be after init_context() for FS */
10588 Pseudo_instruction *startpgm = add_startpgm(&ctx);
10589 append_logical_start(ctx.block);
10590
10591 if (unlikely(args->options->has_ls_vgpr_init_bug && ctx.stage == vertex_tess_control_hs))
10592 fix_ls_vgpr_init_bug(&ctx, startpgm);
10593
10594 split_arguments(&ctx, startpgm);
10595 }
10596
10597 if (ngg_no_gs) {
10598 ngg_emit_sendmsg_gs_alloc_req(&ctx);
10599
10600 if (ngg_early_prim_export(&ctx))
10601 ngg_emit_nogs_gsthreads(&ctx);
10602 }
10603
10604 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10605 nir_function_impl *func = nir_shader_get_entrypoint(nir);
10606 bool empty_shader = nir_cf_list_is_empty_block(&func->body) &&
10607 ((nir->info.stage == MESA_SHADER_VERTEX &&
10608 (ctx.stage == vertex_tess_control_hs || ctx.stage == vertex_geometry_gs)) ||
10609 (nir->info.stage == MESA_SHADER_TESS_EVAL &&
10610 ctx.stage == tess_eval_geometry_gs));
10611
10612 bool check_merged_wave_info = ctx.tcs_in_out_eq ? i == 0 : ((shader_count >= 2 && !empty_shader) || ngg_no_gs);
10613 bool endif_merged_wave_info = ctx.tcs_in_out_eq ? i == 1 : check_merged_wave_info;
10614 if (check_merged_wave_info) {
10615 Temp cond = merged_wave_info_to_mask(&ctx, i);
10616 begin_divergent_if_then(&ctx, &ic_merged_wave_info, cond);
10617 }
10618
10619 if (i) {
10620 Builder bld(ctx.program, ctx.block);
10621
10622 bld.barrier(aco_opcode::p_memory_barrier_shared);
10623 bld.sopp(aco_opcode::s_barrier);
10624
10625 if (ctx.stage == vertex_geometry_gs || ctx.stage == tess_eval_geometry_gs) {
10626 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));
10627 }
10628 } else if (ctx.stage == geometry_gs)
10629 ctx.gs_wave_id = get_arg(&ctx, args->gs_wave_id);
10630
10631 if (ctx.stage == fragment_fs)
10632 handle_bc_optimize(&ctx);
10633
10634 visit_cf_list(&ctx, &func->body);
10635
10636 if (ctx.program->info->so.num_outputs && (ctx.stage & hw_vs))
10637 emit_streamout(&ctx, 0);
10638
10639 if (ctx.stage & hw_vs) {
10640 create_vs_exports(&ctx);
10641 ctx.block->kind |= block_kind_export_end;
10642 } else if (ngg_no_gs && ngg_early_prim_export(&ctx)) {
10643 ngg_emit_nogs_output(&ctx);
10644 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
10645 Builder bld(ctx.program, ctx.block);
10646 bld.barrier(aco_opcode::p_memory_barrier_gs_data);
10647 bld.sopp(aco_opcode::s_sendmsg, bld.m0(ctx.gs_wave_id), -1, sendmsg_gs_done(false, false, 0));
10648 } else if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
10649 write_tcs_tess_factors(&ctx);
10650 }
10651
10652 if (ctx.stage == fragment_fs) {
10653 create_fs_exports(&ctx);
10654 ctx.block->kind |= block_kind_export_end;
10655 }
10656
10657 if (endif_merged_wave_info) {
10658 begin_divergent_if_else(&ctx, &ic_merged_wave_info);
10659 end_divergent_if(&ctx, &ic_merged_wave_info);
10660 }
10661
10662 if (ngg_no_gs && !ngg_early_prim_export(&ctx))
10663 ngg_emit_nogs_output(&ctx);
10664
10665 ralloc_free(ctx.divergent_vals);
10666
10667 if (i == 0 && ctx.stage == vertex_tess_control_hs && ctx.tcs_in_out_eq) {
10668 /* Outputs of the previous stage are inputs to the next stage */
10669 ctx.inputs = ctx.outputs;
10670 ctx.outputs = shader_io_state();
10671 }
10672 }
10673
10674 program->config->float_mode = program->blocks[0].fp_mode.val;
10675
10676 append_logical_end(ctx.block);
10677 ctx.block->kind |= block_kind_uniform;
10678 Builder bld(ctx.program, ctx.block);
10679 if (ctx.program->wb_smem_l1_on_end)
10680 bld.smem(aco_opcode::s_dcache_wb, false);
10681 bld.sopp(aco_opcode::s_endpgm);
10682
10683 cleanup_cfg(program);
10684 }
10685
10686 void select_gs_copy_shader(Program *program, struct nir_shader *gs_shader,
10687 ac_shader_config* config,
10688 struct radv_shader_args *args)
10689 {
10690 isel_context ctx = setup_isel_context(program, 1, &gs_shader, config, args, true);
10691
10692 program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
10693 program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
10694 program->next_fp_mode.must_flush_denorms32 = false;
10695 program->next_fp_mode.must_flush_denorms16_64 = false;
10696 program->next_fp_mode.care_about_round32 = false;
10697 program->next_fp_mode.care_about_round16_64 = false;
10698 program->next_fp_mode.denorm16_64 = fp_denorm_keep;
10699 program->next_fp_mode.denorm32 = 0;
10700 program->next_fp_mode.round32 = fp_round_ne;
10701 program->next_fp_mode.round16_64 = fp_round_ne;
10702 ctx.block->fp_mode = program->next_fp_mode;
10703
10704 add_startpgm(&ctx);
10705 append_logical_start(ctx.block);
10706
10707 Builder bld(ctx.program, ctx.block);
10708
10709 Temp gsvs_ring = bld.smem(aco_opcode::s_load_dwordx4, bld.def(s4), program->private_segment_buffer, Operand(RING_GSVS_VS * 16u));
10710
10711 Operand stream_id(0u);
10712 if (args->shader_info->so.num_outputs)
10713 stream_id = bld.sop2(aco_opcode::s_bfe_u32, bld.def(s1), bld.def(s1, scc),
10714 get_arg(&ctx, ctx.args->streamout_config), Operand(0x20018u));
10715
10716 Temp vtx_offset = bld.vop2(aco_opcode::v_lshlrev_b32, bld.def(v1), Operand(2u), get_arg(&ctx, ctx.args->ac.vertex_id));
10717
10718 std::stack<Block> endif_blocks;
10719
10720 for (unsigned stream = 0; stream < 4; stream++) {
10721 if (stream_id.isConstant() && stream != stream_id.constantValue())
10722 continue;
10723
10724 unsigned num_components = args->shader_info->gs.num_stream_output_components[stream];
10725 if (stream > 0 && (!num_components || !args->shader_info->so.num_outputs))
10726 continue;
10727
10728 memset(ctx.outputs.mask, 0, sizeof(ctx.outputs.mask));
10729
10730 unsigned BB_if_idx = ctx.block->index;
10731 Block BB_endif = Block();
10732 if (!stream_id.isConstant()) {
10733 /* begin IF */
10734 Temp cond = bld.sopc(aco_opcode::s_cmp_eq_u32, bld.def(s1, scc), stream_id, Operand(stream));
10735 append_logical_end(ctx.block);
10736 ctx.block->kind |= block_kind_uniform;
10737 bld.branch(aco_opcode::p_cbranch_z, cond);
10738
10739 BB_endif.kind |= ctx.block->kind & block_kind_top_level;
10740
10741 ctx.block = ctx.program->create_and_insert_block();
10742 add_edge(BB_if_idx, ctx.block);
10743 bld.reset(ctx.block);
10744 append_logical_start(ctx.block);
10745 }
10746
10747 unsigned offset = 0;
10748 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
10749 if (args->shader_info->gs.output_streams[i] != stream)
10750 continue;
10751
10752 unsigned output_usage_mask = args->shader_info->gs.output_usage_mask[i];
10753 unsigned length = util_last_bit(output_usage_mask);
10754 for (unsigned j = 0; j < length; ++j) {
10755 if (!(output_usage_mask & (1 << j)))
10756 continue;
10757
10758 unsigned const_offset = offset * args->shader_info->gs.vertices_out * 16 * 4;
10759 Temp voffset = vtx_offset;
10760 if (const_offset >= 4096u) {
10761 voffset = bld.vadd32(bld.def(v1), Operand(const_offset / 4096u * 4096u), voffset);
10762 const_offset %= 4096u;
10763 }
10764
10765 aco_ptr<MUBUF_instruction> mubuf{create_instruction<MUBUF_instruction>(aco_opcode::buffer_load_dword, Format::MUBUF, 3, 1)};
10766 mubuf->definitions[0] = bld.def(v1);
10767 mubuf->operands[0] = Operand(gsvs_ring);
10768 mubuf->operands[1] = Operand(voffset);
10769 mubuf->operands[2] = Operand(0u);
10770 mubuf->offen = true;
10771 mubuf->offset = const_offset;
10772 mubuf->glc = true;
10773 mubuf->slc = true;
10774 mubuf->dlc = args->options->chip_class >= GFX10;
10775 mubuf->barrier = barrier_none;
10776 mubuf->can_reorder = true;
10777
10778 ctx.outputs.mask[i] |= 1 << j;
10779 ctx.outputs.temps[i * 4u + j] = mubuf->definitions[0].getTemp();
10780
10781 bld.insert(std::move(mubuf));
10782
10783 offset++;
10784 }
10785 }
10786
10787 if (args->shader_info->so.num_outputs) {
10788 emit_streamout(&ctx, stream);
10789 bld.reset(ctx.block);
10790 }
10791
10792 if (stream == 0) {
10793 create_vs_exports(&ctx);
10794 ctx.block->kind |= block_kind_export_end;
10795 }
10796
10797 if (!stream_id.isConstant()) {
10798 append_logical_end(ctx.block);
10799
10800 /* branch from then block to endif block */
10801 bld.branch(aco_opcode::p_branch);
10802 add_edge(ctx.block->index, &BB_endif);
10803 ctx.block->kind |= block_kind_uniform;
10804
10805 /* emit else block */
10806 ctx.block = ctx.program->create_and_insert_block();
10807 add_edge(BB_if_idx, ctx.block);
10808 bld.reset(ctx.block);
10809 append_logical_start(ctx.block);
10810
10811 endif_blocks.push(std::move(BB_endif));
10812 }
10813 }
10814
10815 while (!endif_blocks.empty()) {
10816 Block BB_endif = std::move(endif_blocks.top());
10817 endif_blocks.pop();
10818
10819 Block *BB_else = ctx.block;
10820
10821 append_logical_end(BB_else);
10822 /* branch from else block to endif block */
10823 bld.branch(aco_opcode::p_branch);
10824 add_edge(BB_else->index, &BB_endif);
10825 BB_else->kind |= block_kind_uniform;
10826
10827 /** emit endif merge block */
10828 ctx.block = program->insert_block(std::move(BB_endif));
10829 bld.reset(ctx.block);
10830 append_logical_start(ctx.block);
10831 }
10832
10833 program->config->float_mode = program->blocks[0].fp_mode.val;
10834
10835 append_logical_end(ctx.block);
10836 ctx.block->kind |= block_kind_uniform;
10837 bld.sopp(aco_opcode::s_endpgm);
10838
10839 cleanup_cfg(program);
10840 }
10841 }