2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
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:
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
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
30 #include "ac_shader_util.h"
32 #include "aco_builder.h"
33 #include "aco_interface.h"
34 #include "aco_instruction_selection_setup.cpp"
35 #include "util/fast_idiv_by_const.h"
40 class loop_info_RAII
{
42 unsigned header_idx_old
;
44 bool divergent_cont_old
;
45 bool divergent_branch_old
;
46 bool divergent_if_old
;
49 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
51 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
52 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
53 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
54 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
56 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
57 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
58 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
59 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
60 ctx
->cf_info
.parent_if
.is_divergent
= false;
61 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
66 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
67 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
68 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
69 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
70 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
71 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
72 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
73 ctx
->cf_info
.exec_potentially_empty
= false;
81 bool exec_potentially_empty_old
;
85 bool then_branch_divergent
;
90 static void visit_cf_list(struct isel_context
*ctx
,
91 struct exec_list
*list
);
93 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
95 succ
->logical_preds
.emplace_back(pred_idx
);
99 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
101 succ
->linear_preds
.emplace_back(pred_idx
);
104 static void add_edge(unsigned pred_idx
, Block
*succ
)
106 add_logical_edge(pred_idx
, succ
);
107 add_linear_edge(pred_idx
, succ
);
110 static void append_logical_start(Block
*b
)
112 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
115 static void append_logical_end(Block
*b
)
117 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
120 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
122 assert(ctx
->allocated
[def
->index
].id());
123 return ctx
->allocated
[def
->index
];
126 Temp
emit_mbcnt(isel_context
*ctx
, Definition dst
,
127 Operand mask_lo
= Operand((uint32_t) -1), Operand mask_hi
= Operand((uint32_t) -1))
129 Builder
bld(ctx
->program
, ctx
->block
);
130 Definition lo_def
= ctx
->program
->wave_size
== 32 ? dst
: bld
.def(v1
);
131 Temp thread_id_lo
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, lo_def
, mask_lo
, Operand(0u));
133 if (ctx
->program
->wave_size
== 32) {
136 Temp thread_id_hi
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, dst
, mask_hi
, thread_id_lo
);
141 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
143 Builder
bld(ctx
->program
, ctx
->block
);
146 dst
= bld
.tmp(src
.regClass());
148 assert(src
.size() == dst
.size());
150 if (ctx
->stage
!= fragment_fs
) {
154 bld
.copy(Definition(dst
), src
);
158 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
159 ctx
->program
->needs_wqm
|= program_needs_wqm
;
163 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
165 if (index
.regClass() == s1
)
166 return bld
.readlane(bld
.def(s1
), data
, index
);
168 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
170 /* Currently not implemented on GFX6-7 */
171 assert(ctx
->options
->chip_class
>= GFX8
);
173 if (ctx
->options
->chip_class
<= GFX9
|| ctx
->program
->wave_size
== 32) {
174 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
177 /* GFX10, wave64 mode:
178 * The bpermute instruction is limited to half-wave operation, which means that it can't
179 * properly support subgroup shuffle like older generations (or wave32 mode), so we
182 if (!ctx
->has_gfx10_wave64_bpermute
) {
183 ctx
->has_gfx10_wave64_bpermute
= true;
184 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
185 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
188 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
189 Temp lane_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), lane_id
);
190 Temp index_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), index
);
191 Temp cmp
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
, vcc
), lane_is_hi
, index_is_hi
);
193 return bld
.reduction(aco_opcode::p_wave64_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
),
194 bld
.vcc(cmp
), Operand(v2
.as_linear()), index_x4
, data
, gfx10_wave64_bpermute
);
197 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
199 if (val
.type() == RegType::sgpr
) {
200 Builder
bld(ctx
->program
, ctx
->block
);
201 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
203 assert(val
.type() == RegType::vgpr
);
207 //assumes a != 0xffffffff
208 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
211 Builder
bld(ctx
->program
, ctx
->block
);
213 if (util_is_power_of_two_or_zero(b
)) {
214 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
218 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
220 assert(info
.multiplier
<= 0xffffffff);
222 bool pre_shift
= info
.pre_shift
!= 0;
223 bool increment
= info
.increment
!= 0;
224 bool multiply
= true;
225 bool post_shift
= info
.post_shift
!= 0;
227 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
228 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
232 Temp pre_shift_dst
= a
;
234 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
235 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
238 Temp increment_dst
= pre_shift_dst
;
240 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
241 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
244 Temp multiply_dst
= increment_dst
;
246 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
247 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
248 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
252 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
256 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
258 Builder
bld(ctx
->program
, ctx
->block
);
259 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
263 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
265 /* no need to extract the whole vector */
266 if (src
.regClass() == dst_rc
) {
270 assert(src
.size() > idx
);
271 Builder
bld(ctx
->program
, ctx
->block
);
272 auto it
= ctx
->allocated_vec
.find(src
.id());
273 /* the size check needs to be early because elements other than 0 may be garbage */
274 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
275 if (it
->second
[idx
].regClass() == dst_rc
) {
276 return it
->second
[idx
];
278 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
279 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
280 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
284 if (src
.size() == dst_rc
.size()) {
286 return bld
.copy(bld
.def(dst_rc
), src
);
288 Temp dst
= bld
.tmp(dst_rc
);
289 emit_extract_vector(ctx
, src
, idx
, dst
);
294 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
296 if (num_components
== 1)
298 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
300 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
301 split
->operands
[0] = Operand(vec_src
);
302 std::array
<Temp
,4> elems
;
303 for (unsigned i
= 0; i
< num_components
; i
++) {
304 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
305 split
->definitions
[i
] = Definition(elems
[i
]);
307 ctx
->block
->instructions
.emplace_back(std::move(split
));
308 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
311 /* This vector expansion uses a mask to determine which elements in the new vector
312 * come from the original vector. The other elements are undefined. */
313 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
315 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
320 Builder
bld(ctx
->program
, ctx
->block
);
321 if (num_components
== 1) {
322 if (dst
.type() == RegType::sgpr
)
323 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
325 bld
.copy(Definition(dst
), vec_src
);
329 unsigned component_size
= dst
.size() / num_components
;
330 std::array
<Temp
,4> elems
;
332 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
333 vec
->definitions
[0] = Definition(dst
);
335 for (unsigned i
= 0; i
< num_components
; i
++) {
336 if (mask
& (1 << i
)) {
337 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
338 if (dst
.type() == RegType::sgpr
)
339 src
= bld
.as_uniform(src
);
340 vec
->operands
[i
] = Operand(src
);
342 vec
->operands
[i
] = Operand(0u);
344 elems
[i
] = vec
->operands
[i
].getTemp();
346 ctx
->block
->instructions
.emplace_back(std::move(vec
));
347 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
350 Temp
bool_to_vector_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s2
))
352 Builder
bld(ctx
->program
, ctx
->block
);
354 dst
= bld
.tmp(bld
.lm
);
356 assert(val
.regClass() == s1
);
357 assert(dst
.regClass() == bld
.lm
);
359 return bld
.sop2(Builder::s_cselect
, bld
.hint_vcc(Definition(dst
)), Operand((uint32_t) -1), Operand(0u), bld
.scc(val
));
362 Temp
bool_to_scalar_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s1
))
364 Builder
bld(ctx
->program
, ctx
->block
);
368 assert(val
.regClass() == bld
.lm
);
369 assert(dst
.regClass() == s1
);
371 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
372 Temp tmp
= bld
.tmp(s1
);
373 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, bld
.lm
));
374 return emit_wqm(ctx
, tmp
, dst
);
377 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
379 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
380 return get_ssa_temp(ctx
, src
.src
.ssa
);
382 if (src
.src
.ssa
->num_components
== size
) {
383 bool identity_swizzle
= true;
384 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
385 if (src
.swizzle
[i
] != i
)
386 identity_swizzle
= false;
388 if (identity_swizzle
)
389 return get_ssa_temp(ctx
, src
.src
.ssa
);
392 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
393 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
394 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
395 assert(vec
.size() % elem_size
== 0);
397 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
399 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
402 std::array
<Temp
,4> elems
;
403 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
404 for (unsigned i
= 0; i
< size
; ++i
) {
405 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
406 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
408 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
409 vec_instr
->definitions
[0] = Definition(dst
);
410 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
411 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
416 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
420 Builder
bld(ctx
->program
, ctx
->block
);
421 if (ptr
.type() == RegType::vgpr
)
422 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
423 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
424 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
427 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
429 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
430 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
431 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
432 sop2
->definitions
[0] = Definition(dst
);
434 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
435 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
438 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool commutative
, bool swap_srcs
=false)
440 Builder
bld(ctx
->program
, ctx
->block
);
441 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
442 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
443 if (src1
.type() == RegType::sgpr
) {
444 if (commutative
&& src0
.type() == RegType::vgpr
) {
448 } else if (src0
.type() == RegType::vgpr
&&
449 op
!= aco_opcode::v_madmk_f32
&&
450 op
!= aco_opcode::v_madak_f32
&&
451 op
!= aco_opcode::v_madmk_f16
&&
452 op
!= aco_opcode::v_madak_f16
) {
453 /* If the instruction is not commutative, we emit a VOP3A instruction */
454 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
457 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
460 bld
.vop2(op
, Definition(dst
), src0
, src1
);
463 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
465 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
466 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
467 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
469 /* ensure that the instruction has at most 1 sgpr operand
470 * The optimizer will inline constants for us */
471 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
472 src0
= as_vgpr(ctx
, src0
);
473 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
474 src1
= as_vgpr(ctx
, src1
);
475 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
476 src2
= as_vgpr(ctx
, src2
);
478 Builder
bld(ctx
->program
, ctx
->block
);
479 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
482 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
484 Builder
bld(ctx
->program
, ctx
->block
);
485 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
488 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
490 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
491 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
492 assert(src0
.size() == src1
.size());
494 aco_ptr
<Instruction
> vopc
;
495 if (src1
.type() == RegType::sgpr
) {
496 if (src0
.type() == RegType::vgpr
) {
497 /* to swap the operands, we might also have to change the opcode */
499 case aco_opcode::v_cmp_lt_f32
:
500 op
= aco_opcode::v_cmp_gt_f32
;
502 case aco_opcode::v_cmp_ge_f32
:
503 op
= aco_opcode::v_cmp_le_f32
;
505 case aco_opcode::v_cmp_lt_i32
:
506 op
= aco_opcode::v_cmp_gt_i32
;
508 case aco_opcode::v_cmp_ge_i32
:
509 op
= aco_opcode::v_cmp_le_i32
;
511 case aco_opcode::v_cmp_lt_u32
:
512 op
= aco_opcode::v_cmp_gt_u32
;
514 case aco_opcode::v_cmp_ge_u32
:
515 op
= aco_opcode::v_cmp_le_u32
;
517 case aco_opcode::v_cmp_lt_f64
:
518 op
= aco_opcode::v_cmp_gt_f64
;
520 case aco_opcode::v_cmp_ge_f64
:
521 op
= aco_opcode::v_cmp_le_f64
;
523 case aco_opcode::v_cmp_lt_i64
:
524 op
= aco_opcode::v_cmp_gt_i64
;
526 case aco_opcode::v_cmp_ge_i64
:
527 op
= aco_opcode::v_cmp_le_i64
;
529 case aco_opcode::v_cmp_lt_u64
:
530 op
= aco_opcode::v_cmp_gt_u64
;
532 case aco_opcode::v_cmp_ge_u64
:
533 op
= aco_opcode::v_cmp_le_u64
;
535 default: /* eq and ne are commutative */
542 src1
= as_vgpr(ctx
, src1
);
546 Builder
bld(ctx
->program
, ctx
->block
);
547 bld
.vopc(op
, bld
.hint_vcc(Definition(dst
)), src0
, src1
);
550 void emit_sopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
552 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
553 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
554 Builder
bld(ctx
->program
, ctx
->block
);
556 assert(dst
.regClass() == bld
.lm
);
557 assert(src0
.type() == RegType::sgpr
);
558 assert(src1
.type() == RegType::sgpr
);
559 assert(src0
.regClass() == src1
.regClass());
561 /* Emit the SALU comparison instruction */
562 Temp cmp
= bld
.sopc(op
, bld
.scc(bld
.def(s1
)), src0
, src1
);
563 /* Turn the result into a per-lane bool */
564 bool_to_vector_condition(ctx
, cmp
, dst
);
567 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
,
568 aco_opcode v32_op
, aco_opcode v64_op
, aco_opcode s32_op
= aco_opcode::last_opcode
, aco_opcode s64_op
= aco_opcode::last_opcode
)
570 aco_opcode s_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? s64_op
: s32_op
;
571 aco_opcode v_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? v64_op
: v32_op
;
572 bool divergent_vals
= ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
];
573 bool use_valu
= s_op
== aco_opcode::last_opcode
||
575 ctx
->allocated
[instr
->src
[0].src
.ssa
->index
].type() == RegType::vgpr
||
576 ctx
->allocated
[instr
->src
[1].src
.ssa
->index
].type() == RegType::vgpr
;
577 aco_opcode op
= use_valu
? v_op
: s_op
;
578 assert(op
!= aco_opcode::last_opcode
);
581 emit_vopc_instruction(ctx
, instr
, op
, dst
);
583 emit_sopc_instruction(ctx
, instr
, op
, dst
);
586 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, Builder::WaveSpecificOpcode op
, Temp dst
)
588 Builder
bld(ctx
->program
, ctx
->block
);
589 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
590 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
592 assert(dst
.regClass() == bld
.lm
);
593 assert(src0
.regClass() == bld
.lm
);
594 assert(src1
.regClass() == bld
.lm
);
596 bld
.sop2(op
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
599 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
601 Builder
bld(ctx
->program
, ctx
->block
);
602 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
603 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
604 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
606 assert(cond
.regClass() == bld
.lm
);
608 if (dst
.type() == RegType::vgpr
) {
609 aco_ptr
<Instruction
> bcsel
;
610 if (dst
.size() == 1) {
611 then
= as_vgpr(ctx
, then
);
612 els
= as_vgpr(ctx
, els
);
614 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
615 } else if (dst
.size() == 2) {
616 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
617 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
618 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
619 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
621 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
622 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
624 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
626 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
627 nir_print_instr(&instr
->instr
, stderr
);
628 fprintf(stderr
, "\n");
633 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
634 assert(dst
.regClass() == bld
.lm
);
635 assert(then
.regClass() == bld
.lm
);
636 assert(els
.regClass() == bld
.lm
);
639 if (!ctx
->divergent_vals
[instr
->src
[0].src
.ssa
->index
]) { /* uniform condition and values in sgpr */
640 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
641 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
642 assert(dst
.size() == then
.size());
643 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
644 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(bool_to_scalar_condition(ctx
, cond
)));
646 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
647 nir_print_instr(&instr
->instr
, stderr
);
648 fprintf(stderr
, "\n");
653 /* divergent boolean bcsel
654 * this implements bcsel on bools: dst = s0 ? s1 : s2
655 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
656 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
658 if (cond
.id() != then
.id())
659 then
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
, then
);
661 if (cond
.id() == els
.id())
662 bld
.sop1(Builder::s_mov
, Definition(dst
), then
);
664 bld
.sop2(Builder::s_or
, Definition(dst
), bld
.def(s1
, scc
), then
,
665 bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), els
, cond
));
668 void emit_scaled_op(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
,
669 aco_opcode op
, uint32_t undo
)
671 /* multiply by 16777216 to handle denormals */
672 Temp is_denormal
= bld
.vopc(aco_opcode::v_cmp_class_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
673 as_vgpr(ctx
, val
), bld
.copy(bld
.def(v1
), Operand((1u << 7) | (1u << 4))));
674 Temp scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x4b800000u
), val
);
675 scaled
= bld
.vop1(op
, bld
.def(v1
), scaled
);
676 scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(undo
), scaled
);
678 Temp not_scaled
= bld
.vop1(op
, bld
.def(v1
), val
);
680 bld
.vop2(aco_opcode::v_cndmask_b32
, dst
, not_scaled
, scaled
, is_denormal
);
683 void emit_rcp(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
685 if (ctx
->block
->fp_mode
.denorm32
== 0) {
686 bld
.vop1(aco_opcode::v_rcp_f32
, dst
, val
);
690 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rcp_f32
, 0x4b800000u
);
693 void emit_rsq(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
695 if (ctx
->block
->fp_mode
.denorm32
== 0) {
696 bld
.vop1(aco_opcode::v_rsq_f32
, dst
, val
);
700 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rsq_f32
, 0x45800000u
);
703 void emit_sqrt(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
705 if (ctx
->block
->fp_mode
.denorm32
== 0) {
706 bld
.vop1(aco_opcode::v_sqrt_f32
, dst
, val
);
710 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_sqrt_f32
, 0x39800000u
);
713 void emit_log2(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
715 if (ctx
->block
->fp_mode
.denorm32
== 0) {
716 bld
.vop1(aco_opcode::v_log_f32
, dst
, val
);
720 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_log_f32
, 0xc1c00000u
);
723 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
725 if (!instr
->dest
.dest
.is_ssa
) {
726 fprintf(stderr
, "nir alu dst not in ssa: ");
727 nir_print_instr(&instr
->instr
, stderr
);
728 fprintf(stderr
, "\n");
731 Builder
bld(ctx
->program
, ctx
->block
);
732 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
737 std::array
<Temp
,4> elems
;
738 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
739 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
740 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
741 vec
->operands
[i
] = Operand
{elems
[i
]};
743 vec
->definitions
[0] = Definition(dst
);
744 ctx
->block
->instructions
.emplace_back(std::move(vec
));
745 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
749 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
750 aco_ptr
<Instruction
> mov
;
751 if (dst
.type() == RegType::sgpr
) {
752 if (src
.type() == RegType::vgpr
)
753 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
754 else if (src
.regClass() == s1
)
755 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
756 else if (src
.regClass() == s2
)
757 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
759 unreachable("wrong src register class for nir_op_imov");
760 } else if (dst
.regClass() == v1
) {
761 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
762 } else if (dst
.regClass() == v2
) {
763 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
765 nir_print_instr(&instr
->instr
, stderr
);
766 unreachable("Should have been lowered to scalar.");
771 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
772 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
773 assert(src
.regClass() == bld
.lm
);
774 assert(dst
.regClass() == bld
.lm
);
775 bld
.sop2(Builder::s_andn2
, Definition(dst
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
776 } else if (dst
.regClass() == v1
) {
777 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
778 } else if (dst
.type() == RegType::sgpr
) {
779 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
780 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
782 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
783 nir_print_instr(&instr
->instr
, stderr
);
784 fprintf(stderr
, "\n");
789 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
790 if (dst
.regClass() == v1
) {
791 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
792 } else if (dst
.regClass() == s1
) {
793 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
794 } else if (dst
.size() == 2) {
795 Temp src0
= bld
.tmp(dst
.type(), 1);
796 Temp src1
= bld
.tmp(dst
.type(), 1);
797 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
799 if (dst
.regClass() == s2
) {
800 Temp carry
= bld
.tmp(s1
);
801 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
802 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
803 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
805 Temp lower
= bld
.tmp(v1
);
806 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
807 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
808 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
811 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
812 nir_print_instr(&instr
->instr
, stderr
);
813 fprintf(stderr
, "\n");
818 if (dst
.regClass() == s1
) {
819 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
820 } else if (dst
.regClass() == v1
) {
821 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
822 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
824 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
825 nir_print_instr(&instr
->instr
, stderr
);
826 fprintf(stderr
, "\n");
831 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
832 if (dst
.regClass() == s1
) {
833 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
834 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
835 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
836 } else if (dst
.regClass() == s2
) {
837 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
839 if (ctx
->program
->chip_class
>= GFX8
)
840 neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
842 neqz
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(0u)).def(1).getTemp();
843 /* SCC gets zero-extended to 64 bit */
844 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, bld
.scc(neqz
));
845 } else if (dst
.regClass() == v1
) {
846 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
847 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
848 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
849 } else if (dst
.regClass() == v2
) {
850 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
851 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
852 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
853 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
854 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
855 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
857 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
858 nir_print_instr(&instr
->instr
, stderr
);
859 fprintf(stderr
, "\n");
864 if (dst
.regClass() == v1
) {
865 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
866 } else if (dst
.regClass() == s1
) {
867 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
869 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
870 nir_print_instr(&instr
->instr
, stderr
);
871 fprintf(stderr
, "\n");
876 if (dst
.regClass() == v1
) {
877 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
878 } else if (dst
.regClass() == s1
) {
879 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
881 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
882 nir_print_instr(&instr
->instr
, stderr
);
883 fprintf(stderr
, "\n");
888 if (dst
.regClass() == v1
) {
889 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
890 } else if (dst
.regClass() == s1
) {
891 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
893 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
894 nir_print_instr(&instr
->instr
, stderr
);
895 fprintf(stderr
, "\n");
900 if (dst
.regClass() == v1
) {
901 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
902 } else if (dst
.regClass() == s1
) {
903 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
905 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
906 nir_print_instr(&instr
->instr
, stderr
);
907 fprintf(stderr
, "\n");
912 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
913 emit_boolean_logic(ctx
, instr
, Builder::s_or
, dst
);
914 } else if (dst
.regClass() == v1
) {
915 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
916 } else if (dst
.regClass() == s1
) {
917 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
918 } else if (dst
.regClass() == s2
) {
919 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
921 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
922 nir_print_instr(&instr
->instr
, stderr
);
923 fprintf(stderr
, "\n");
928 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
929 emit_boolean_logic(ctx
, instr
, Builder::s_and
, dst
);
930 } else if (dst
.regClass() == v1
) {
931 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
932 } else if (dst
.regClass() == s1
) {
933 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
934 } else if (dst
.regClass() == s2
) {
935 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
937 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
938 nir_print_instr(&instr
->instr
, stderr
);
939 fprintf(stderr
, "\n");
944 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
945 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
946 } else if (dst
.regClass() == v1
) {
947 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
948 } else if (dst
.regClass() == s1
) {
949 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
950 } else if (dst
.regClass() == s2
) {
951 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
953 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
954 nir_print_instr(&instr
->instr
, stderr
);
955 fprintf(stderr
, "\n");
960 if (dst
.regClass() == v1
) {
961 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
962 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
963 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
964 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
965 } else if (dst
.regClass() == v2
) {
966 bld
.vop3(aco_opcode::v_lshr_b64
, Definition(dst
),
967 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
968 } else if (dst
.regClass() == s2
) {
969 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
970 } else if (dst
.regClass() == s1
) {
971 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
973 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
974 nir_print_instr(&instr
->instr
, stderr
);
975 fprintf(stderr
, "\n");
980 if (dst
.regClass() == v1
) {
981 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
982 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
983 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
984 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
985 } else if (dst
.regClass() == v2
) {
986 bld
.vop3(aco_opcode::v_lshl_b64
, Definition(dst
),
987 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
988 } else if (dst
.regClass() == s1
) {
989 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
990 } else if (dst
.regClass() == s2
) {
991 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
993 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
994 nir_print_instr(&instr
->instr
, stderr
);
995 fprintf(stderr
, "\n");
1000 if (dst
.regClass() == v1
) {
1001 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
1002 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1003 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
1004 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1005 } else if (dst
.regClass() == v2
) {
1006 bld
.vop3(aco_opcode::v_ashr_i64
, Definition(dst
),
1007 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1008 } else if (dst
.regClass() == s1
) {
1009 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
1010 } else if (dst
.regClass() == s2
) {
1011 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
1013 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1014 nir_print_instr(&instr
->instr
, stderr
);
1015 fprintf(stderr
, "\n");
1019 case nir_op_find_lsb
: {
1020 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1021 if (src
.regClass() == s1
) {
1022 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
1023 } else if (src
.regClass() == v1
) {
1024 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
1025 } else if (src
.regClass() == s2
) {
1026 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
1028 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1029 nir_print_instr(&instr
->instr
, stderr
);
1030 fprintf(stderr
, "\n");
1034 case nir_op_ufind_msb
:
1035 case nir_op_ifind_msb
: {
1036 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1037 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
1038 aco_opcode op
= src
.regClass() == s2
?
1039 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
1040 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
1041 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
1043 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
1044 Operand(src
.size() * 32u - 1u), msb_rev
);
1045 Temp msb
= sub
.def(0).getTemp();
1046 Temp carry
= sub
.def(1).getTemp();
1048 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
1049 } else if (src
.regClass() == v1
) {
1050 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
1051 Temp msb_rev
= bld
.tmp(v1
);
1052 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
1053 Temp msb
= bld
.tmp(v1
);
1054 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
1055 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
1057 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1058 nir_print_instr(&instr
->instr
, stderr
);
1059 fprintf(stderr
, "\n");
1063 case nir_op_bitfield_reverse
: {
1064 if (dst
.regClass() == s1
) {
1065 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1066 } else if (dst
.regClass() == v1
) {
1067 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1069 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1070 nir_print_instr(&instr
->instr
, stderr
);
1071 fprintf(stderr
, "\n");
1076 if (dst
.regClass() == s1
) {
1077 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1081 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1082 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1083 if (dst
.regClass() == v1
) {
1084 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1088 assert(src0
.size() == 2 && src1
.size() == 2);
1089 Temp src00
= bld
.tmp(src0
.type(), 1);
1090 Temp src01
= bld
.tmp(dst
.type(), 1);
1091 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1092 Temp src10
= bld
.tmp(src1
.type(), 1);
1093 Temp src11
= bld
.tmp(dst
.type(), 1);
1094 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1096 if (dst
.regClass() == s2
) {
1097 Temp carry
= bld
.tmp(s1
);
1098 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1099 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1100 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1101 } else if (dst
.regClass() == v2
) {
1102 Temp dst0
= bld
.tmp(v1
);
1103 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1104 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1105 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1107 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1108 nir_print_instr(&instr
->instr
, stderr
);
1109 fprintf(stderr
, "\n");
1113 case nir_op_uadd_sat
: {
1114 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1115 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1116 if (dst
.regClass() == s1
) {
1117 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1118 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1120 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1121 } else if (dst
.regClass() == v1
) {
1122 if (ctx
->options
->chip_class
>= GFX9
) {
1123 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1124 add
->operands
[0] = Operand(src0
);
1125 add
->operands
[1] = Operand(src1
);
1126 add
->definitions
[0] = Definition(dst
);
1128 ctx
->block
->instructions
.emplace_back(std::move(add
));
1130 if (src1
.regClass() != v1
)
1131 std::swap(src0
, src1
);
1132 assert(src1
.regClass() == v1
);
1133 Temp tmp
= bld
.tmp(v1
);
1134 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1135 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1138 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1139 nir_print_instr(&instr
->instr
, stderr
);
1140 fprintf(stderr
, "\n");
1144 case nir_op_uadd_carry
: {
1145 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1146 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1147 if (dst
.regClass() == s1
) {
1148 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1151 if (dst
.regClass() == v1
) {
1152 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1153 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1157 Temp src00
= bld
.tmp(src0
.type(), 1);
1158 Temp src01
= bld
.tmp(dst
.type(), 1);
1159 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1160 Temp src10
= bld
.tmp(src1
.type(), 1);
1161 Temp src11
= bld
.tmp(dst
.type(), 1);
1162 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1163 if (dst
.regClass() == s2
) {
1164 Temp carry
= bld
.tmp(s1
);
1165 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1166 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1167 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1168 } else if (dst
.regClass() == v2
) {
1169 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1170 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1171 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1172 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1174 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1175 nir_print_instr(&instr
->instr
, stderr
);
1176 fprintf(stderr
, "\n");
1181 if (dst
.regClass() == s1
) {
1182 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1186 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1187 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1188 if (dst
.regClass() == v1
) {
1189 bld
.vsub32(Definition(dst
), src0
, src1
);
1193 Temp src00
= bld
.tmp(src0
.type(), 1);
1194 Temp src01
= bld
.tmp(dst
.type(), 1);
1195 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1196 Temp src10
= bld
.tmp(src1
.type(), 1);
1197 Temp src11
= bld
.tmp(dst
.type(), 1);
1198 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1199 if (dst
.regClass() == s2
) {
1200 Temp carry
= bld
.tmp(s1
);
1201 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1202 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1203 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1204 } else if (dst
.regClass() == v2
) {
1205 Temp lower
= bld
.tmp(v1
);
1206 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1207 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1208 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1210 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1211 nir_print_instr(&instr
->instr
, stderr
);
1212 fprintf(stderr
, "\n");
1216 case nir_op_usub_borrow
: {
1217 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1218 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1219 if (dst
.regClass() == s1
) {
1220 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1222 } else if (dst
.regClass() == v1
) {
1223 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1224 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1228 Temp src00
= bld
.tmp(src0
.type(), 1);
1229 Temp src01
= bld
.tmp(dst
.type(), 1);
1230 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1231 Temp src10
= bld
.tmp(src1
.type(), 1);
1232 Temp src11
= bld
.tmp(dst
.type(), 1);
1233 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1234 if (dst
.regClass() == s2
) {
1235 Temp borrow
= bld
.tmp(s1
);
1236 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1237 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1238 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1239 } else if (dst
.regClass() == v2
) {
1240 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1241 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1242 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1243 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1245 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1246 nir_print_instr(&instr
->instr
, stderr
);
1247 fprintf(stderr
, "\n");
1252 if (dst
.regClass() == v1
) {
1253 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1254 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1255 } else if (dst
.regClass() == s1
) {
1256 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1258 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1259 nir_print_instr(&instr
->instr
, stderr
);
1260 fprintf(stderr
, "\n");
1264 case nir_op_umul_high
: {
1265 if (dst
.regClass() == v1
) {
1266 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1267 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1268 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1269 } else if (dst
.regClass() == s1
) {
1270 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1271 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1272 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1274 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1275 nir_print_instr(&instr
->instr
, stderr
);
1276 fprintf(stderr
, "\n");
1280 case nir_op_imul_high
: {
1281 if (dst
.regClass() == v1
) {
1282 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1283 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1284 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1285 } else if (dst
.regClass() == s1
) {
1286 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1287 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1288 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1290 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1291 nir_print_instr(&instr
->instr
, stderr
);
1292 fprintf(stderr
, "\n");
1297 if (dst
.size() == 1) {
1298 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1299 } else if (dst
.size() == 2) {
1300 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1301 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1303 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1304 nir_print_instr(&instr
->instr
, stderr
);
1305 fprintf(stderr
, "\n");
1310 if (dst
.size() == 1) {
1311 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1312 } else if (dst
.size() == 2) {
1313 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1314 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1316 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1317 nir_print_instr(&instr
->instr
, stderr
);
1318 fprintf(stderr
, "\n");
1323 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1324 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1325 if (dst
.size() == 1) {
1326 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1327 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1329 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1330 } else if (dst
.size() == 2) {
1331 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1332 get_alu_src(ctx
, instr
->src
[0]),
1333 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1334 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1337 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1338 nir_print_instr(&instr
->instr
, stderr
);
1339 fprintf(stderr
, "\n");
1344 if (dst
.size() == 1) {
1345 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true);
1346 } else if (dst
.size() == 2) {
1347 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1348 get_alu_src(ctx
, instr
->src
[0]),
1349 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1351 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1352 nir_print_instr(&instr
->instr
, stderr
);
1353 fprintf(stderr
, "\n");
1358 if (dst
.size() == 1) {
1359 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1360 } else if (dst
.size() == 2) {
1361 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1362 get_alu_src(ctx
, instr
->src
[0]),
1363 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1365 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1366 nir_print_instr(&instr
->instr
, stderr
);
1367 fprintf(stderr
, "\n");
1371 case nir_op_fmax3
: {
1372 if (dst
.size() == 1) {
1373 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1375 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1376 nir_print_instr(&instr
->instr
, stderr
);
1377 fprintf(stderr
, "\n");
1381 case nir_op_fmin3
: {
1382 if (dst
.size() == 1) {
1383 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1385 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1386 nir_print_instr(&instr
->instr
, stderr
);
1387 fprintf(stderr
, "\n");
1391 case nir_op_fmed3
: {
1392 if (dst
.size() == 1) {
1393 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1395 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1396 nir_print_instr(&instr
->instr
, stderr
);
1397 fprintf(stderr
, "\n");
1401 case nir_op_umax3
: {
1402 if (dst
.size() == 1) {
1403 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1405 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1406 nir_print_instr(&instr
->instr
, stderr
);
1407 fprintf(stderr
, "\n");
1411 case nir_op_umin3
: {
1412 if (dst
.size() == 1) {
1413 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1415 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1416 nir_print_instr(&instr
->instr
, stderr
);
1417 fprintf(stderr
, "\n");
1421 case nir_op_umed3
: {
1422 if (dst
.size() == 1) {
1423 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1425 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1426 nir_print_instr(&instr
->instr
, stderr
);
1427 fprintf(stderr
, "\n");
1431 case nir_op_imax3
: {
1432 if (dst
.size() == 1) {
1433 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1435 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1436 nir_print_instr(&instr
->instr
, stderr
);
1437 fprintf(stderr
, "\n");
1441 case nir_op_imin3
: {
1442 if (dst
.size() == 1) {
1443 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1445 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1446 nir_print_instr(&instr
->instr
, stderr
);
1447 fprintf(stderr
, "\n");
1451 case nir_op_imed3
: {
1452 if (dst
.size() == 1) {
1453 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1455 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1456 nir_print_instr(&instr
->instr
, stderr
);
1457 fprintf(stderr
, "\n");
1461 case nir_op_cube_face_coord
: {
1462 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1463 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1464 emit_extract_vector(ctx
, in
, 1, v1
),
1465 emit_extract_vector(ctx
, in
, 2, v1
) };
1466 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1467 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1468 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1469 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1470 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1471 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1472 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1475 case nir_op_cube_face_index
: {
1476 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1477 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1478 emit_extract_vector(ctx
, in
, 1, v1
),
1479 emit_extract_vector(ctx
, in
, 2, v1
) };
1480 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1483 case nir_op_bcsel
: {
1484 emit_bcsel(ctx
, instr
, dst
);
1488 if (dst
.size() == 1) {
1489 emit_rsq(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1490 } else if (dst
.size() == 2) {
1491 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1493 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1494 nir_print_instr(&instr
->instr
, stderr
);
1495 fprintf(stderr
, "\n");
1500 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1501 if (dst
.size() == 1) {
1502 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1503 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1504 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1505 } else if (dst
.size() == 2) {
1506 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1507 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1508 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1509 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1510 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1511 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1513 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1514 nir_print_instr(&instr
->instr
, stderr
);
1515 fprintf(stderr
, "\n");
1520 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1521 if (dst
.size() == 1) {
1522 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1523 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1524 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1525 } else if (dst
.size() == 2) {
1526 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1527 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1528 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1529 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1530 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1531 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1533 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1534 nir_print_instr(&instr
->instr
, stderr
);
1535 fprintf(stderr
, "\n");
1540 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1541 if (dst
.size() == 1) {
1542 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1543 } else if (dst
.size() == 2) {
1544 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1545 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1548 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1549 nir_print_instr(&instr
->instr
, stderr
);
1550 fprintf(stderr
, "\n");
1554 case nir_op_flog2
: {
1555 if (dst
.size() == 1) {
1556 emit_log2(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1558 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1559 nir_print_instr(&instr
->instr
, stderr
);
1560 fprintf(stderr
, "\n");
1565 if (dst
.size() == 1) {
1566 emit_rcp(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1567 } else if (dst
.size() == 2) {
1568 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1570 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1571 nir_print_instr(&instr
->instr
, stderr
);
1572 fprintf(stderr
, "\n");
1576 case nir_op_fexp2
: {
1577 if (dst
.size() == 1) {
1578 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1580 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1581 nir_print_instr(&instr
->instr
, stderr
);
1582 fprintf(stderr
, "\n");
1586 case nir_op_fsqrt
: {
1587 if (dst
.size() == 1) {
1588 emit_sqrt(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1589 } else if (dst
.size() == 2) {
1590 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1592 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1593 nir_print_instr(&instr
->instr
, stderr
);
1594 fprintf(stderr
, "\n");
1598 case nir_op_ffract
: {
1599 if (dst
.size() == 1) {
1600 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1601 } else if (dst
.size() == 2) {
1602 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1604 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1605 nir_print_instr(&instr
->instr
, stderr
);
1606 fprintf(stderr
, "\n");
1610 case nir_op_ffloor
: {
1611 if (dst
.size() == 1) {
1612 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1613 } else if (dst
.size() == 2) {
1614 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1616 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1617 nir_print_instr(&instr
->instr
, stderr
);
1618 fprintf(stderr
, "\n");
1622 case nir_op_fceil
: {
1623 if (dst
.size() == 1) {
1624 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1625 } else if (dst
.size() == 2) {
1626 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1628 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1629 nir_print_instr(&instr
->instr
, stderr
);
1630 fprintf(stderr
, "\n");
1634 case nir_op_ftrunc
: {
1635 if (dst
.size() == 1) {
1636 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1637 } else if (dst
.size() == 2) {
1638 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1640 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1641 nir_print_instr(&instr
->instr
, stderr
);
1642 fprintf(stderr
, "\n");
1646 case nir_op_fround_even
: {
1647 if (dst
.size() == 1) {
1648 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1649 } else if (dst
.size() == 2) {
1650 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1652 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1653 nir_print_instr(&instr
->instr
, stderr
);
1654 fprintf(stderr
, "\n");
1660 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1661 aco_ptr
<Instruction
> norm
;
1662 if (dst
.size() == 1) {
1663 Temp half_pi
= bld
.copy(bld
.def(s1
), Operand(0x3e22f983u
));
1664 Temp tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, as_vgpr(ctx
, src
));
1666 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1667 if (ctx
->options
->chip_class
< GFX9
)
1668 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1670 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1671 bld
.vop1(opcode
, Definition(dst
), tmp
);
1673 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1674 nir_print_instr(&instr
->instr
, stderr
);
1675 fprintf(stderr
, "\n");
1679 case nir_op_ldexp
: {
1680 if (dst
.size() == 1) {
1681 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1682 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1683 get_alu_src(ctx
, instr
->src
[1]));
1684 } else if (dst
.size() == 2) {
1685 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1686 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1687 get_alu_src(ctx
, instr
->src
[1]));
1689 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1690 nir_print_instr(&instr
->instr
, stderr
);
1691 fprintf(stderr
, "\n");
1695 case nir_op_frexp_sig
: {
1696 if (dst
.size() == 1) {
1697 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1698 get_alu_src(ctx
, instr
->src
[0]));
1699 } else if (dst
.size() == 2) {
1700 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1701 get_alu_src(ctx
, instr
->src
[0]));
1703 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1704 nir_print_instr(&instr
->instr
, stderr
);
1705 fprintf(stderr
, "\n");
1709 case nir_op_frexp_exp
: {
1710 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1711 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1712 get_alu_src(ctx
, instr
->src
[0]));
1713 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1714 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1715 get_alu_src(ctx
, instr
->src
[0]));
1717 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1718 nir_print_instr(&instr
->instr
, stderr
);
1719 fprintf(stderr
, "\n");
1723 case nir_op_fsign
: {
1724 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1725 if (dst
.size() == 1) {
1726 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1727 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1728 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1729 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1730 } else if (dst
.size() == 2) {
1731 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1732 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1733 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, emit_extract_vector(ctx
, src
, 1, v1
), cond
);
1735 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1736 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1737 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1739 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1741 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1742 nir_print_instr(&instr
->instr
, stderr
);
1743 fprintf(stderr
, "\n");
1747 case nir_op_f2f32
: {
1748 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1749 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1751 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1752 nir_print_instr(&instr
->instr
, stderr
);
1753 fprintf(stderr
, "\n");
1757 case nir_op_f2f64
: {
1758 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1759 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1761 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1762 nir_print_instr(&instr
->instr
, stderr
);
1763 fprintf(stderr
, "\n");
1767 case nir_op_i2f32
: {
1768 assert(dst
.size() == 1);
1769 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1772 case nir_op_i2f64
: {
1773 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1774 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1775 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1776 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1777 RegClass rc
= RegClass(src
.type(), 1);
1778 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1779 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1780 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1781 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1782 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1783 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1786 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1787 nir_print_instr(&instr
->instr
, stderr
);
1788 fprintf(stderr
, "\n");
1792 case nir_op_u2f32
: {
1793 assert(dst
.size() == 1);
1794 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1797 case nir_op_u2f64
: {
1798 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1799 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1800 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1801 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1802 RegClass rc
= RegClass(src
.type(), 1);
1803 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1804 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1805 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1806 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1807 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1808 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1810 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1811 nir_print_instr(&instr
->instr
, stderr
);
1812 fprintf(stderr
, "\n");
1816 case nir_op_f2i32
: {
1817 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1818 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1819 if (dst
.type() == RegType::vgpr
)
1820 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1822 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1823 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1825 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1826 if (dst
.type() == RegType::vgpr
)
1827 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1829 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1830 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1833 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1834 nir_print_instr(&instr
->instr
, stderr
);
1835 fprintf(stderr
, "\n");
1839 case nir_op_f2u32
: {
1840 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1841 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1842 if (dst
.type() == RegType::vgpr
)
1843 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1845 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1846 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1848 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1849 if (dst
.type() == RegType::vgpr
)
1850 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1852 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1853 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1856 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1857 nir_print_instr(&instr
->instr
, stderr
);
1858 fprintf(stderr
, "\n");
1862 case nir_op_f2i64
: {
1863 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1864 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1865 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1866 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1867 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1868 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1869 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1870 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1871 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1872 Temp new_exponent
= bld
.tmp(v1
);
1873 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1874 if (ctx
->program
->chip_class
>= GFX8
)
1875 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1877 mantissa
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), mantissa
, new_exponent
);
1878 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1879 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1880 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1881 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1882 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1883 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1884 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1885 Temp new_lower
= bld
.tmp(v1
);
1886 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1887 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1888 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1890 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1891 if (src
.type() == RegType::vgpr
)
1892 src
= bld
.as_uniform(src
);
1893 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1894 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1895 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1896 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1897 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1898 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1899 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1900 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1901 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1902 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1903 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1904 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1905 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1906 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1907 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1908 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1909 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1910 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1911 Temp borrow
= bld
.tmp(s1
);
1912 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1913 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1914 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1916 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1917 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1918 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1919 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1920 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1921 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1922 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1923 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1924 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1925 if (dst
.type() == RegType::sgpr
) {
1926 lower
= bld
.as_uniform(lower
);
1927 upper
= bld
.as_uniform(upper
);
1929 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1932 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1933 nir_print_instr(&instr
->instr
, stderr
);
1934 fprintf(stderr
, "\n");
1938 case nir_op_f2u64
: {
1939 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1940 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1941 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1942 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(64u), exponent
);
1943 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1944 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1945 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1946 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1947 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1948 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1949 Temp new_exponent
= bld
.tmp(v1
);
1950 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1951 if (ctx
->program
->chip_class
>= GFX8
)
1952 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1954 mantissa
= bld
.vop3(aco_opcode::v_lshl_b64
, bld
.def(v2
), mantissa
, new_exponent
);
1955 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1956 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1957 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1958 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1959 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1960 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1961 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1963 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1964 if (src
.type() == RegType::vgpr
)
1965 src
= bld
.as_uniform(src
);
1966 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1967 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1968 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1969 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1970 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1971 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1972 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1973 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1974 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1975 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1976 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1977 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1978 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1979 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1980 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1981 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1982 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1983 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1985 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1986 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1987 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1988 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1989 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1990 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1991 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1992 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1993 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1994 if (dst
.type() == RegType::sgpr
) {
1995 lower
= bld
.as_uniform(lower
);
1996 upper
= bld
.as_uniform(upper
);
1998 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2001 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2002 nir_print_instr(&instr
->instr
, stderr
);
2003 fprintf(stderr
, "\n");
2007 case nir_op_b2f32
: {
2008 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2009 assert(src
.regClass() == bld
.lm
);
2011 if (dst
.regClass() == s1
) {
2012 src
= bool_to_scalar_condition(ctx
, src
);
2013 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
2014 } else if (dst
.regClass() == v1
) {
2015 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
2017 unreachable("Wrong destination register class for nir_op_b2f32.");
2021 case nir_op_b2f64
: {
2022 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2023 assert(src
.regClass() == bld
.lm
);
2025 if (dst
.regClass() == s2
) {
2026 src
= bool_to_scalar_condition(ctx
, src
);
2027 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
2028 } else if (dst
.regClass() == v2
) {
2029 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
2030 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
, src
);
2031 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
2033 unreachable("Wrong destination register class for nir_op_b2f64.");
2037 case nir_op_i2i32
: {
2038 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2039 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2040 /* we can actually just say dst = src, as it would map the lower register */
2041 emit_extract_vector(ctx
, src
, 0, dst
);
2043 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2044 nir_print_instr(&instr
->instr
, stderr
);
2045 fprintf(stderr
, "\n");
2049 case nir_op_u2u32
: {
2050 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2051 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2052 if (dst
.regClass() == s1
) {
2053 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
2055 // TODO: do better with SDWA
2056 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
2058 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2059 /* we can actually just say dst = src, as it would map the lower register */
2060 emit_extract_vector(ctx
, src
, 0, dst
);
2062 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2063 nir_print_instr(&instr
->instr
, stderr
);
2064 fprintf(stderr
, "\n");
2068 case nir_op_i2i64
: {
2069 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2070 if (src
.regClass() == s1
) {
2071 Temp high
= bld
.sopc(aco_opcode::s_ashr_i32
, bld
.def(s1
, scc
), src
, Operand(31u));
2072 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, high
);
2073 } else if (src
.regClass() == v1
) {
2074 Temp high
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
2075 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, high
);
2077 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2078 nir_print_instr(&instr
->instr
, stderr
);
2079 fprintf(stderr
, "\n");
2083 case nir_op_u2u64
: {
2084 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2085 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2086 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
2088 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr
->instr
, stderr
);
2090 fprintf(stderr
, "\n");
2094 case nir_op_b2i32
: {
2095 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2096 assert(src
.regClass() == bld
.lm
);
2098 if (dst
.regClass() == s1
) {
2099 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2100 bool_to_scalar_condition(ctx
, src
, dst
);
2101 } else if (dst
.regClass() == v1
) {
2102 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2104 unreachable("Invalid register class for b2i32");
2109 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2110 assert(dst
.regClass() == bld
.lm
);
2112 if (src
.type() == RegType::vgpr
) {
2113 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2114 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2115 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2117 assert(src
.regClass() == s1
|| src
.regClass() == s2
);
2119 if (src
.regClass() == s2
&& ctx
->program
->chip_class
<= GFX7
) {
2120 tmp
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0u), src
).def(1).getTemp();
2122 tmp
= bld
.sopc(src
.size() == 2 ? aco_opcode::s_cmp_lg_u64
: aco_opcode::s_cmp_lg_u32
,
2123 bld
.scc(bld
.def(s1
)), Operand(0u), src
);
2125 bool_to_vector_condition(ctx
, tmp
, dst
);
2129 case nir_op_pack_64_2x32_split
: {
2130 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2131 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2133 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2136 case nir_op_unpack_64_2x32_split_x
:
2137 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2139 case nir_op_unpack_64_2x32_split_y
:
2140 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2142 case nir_op_pack_half_2x16
: {
2143 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2145 if (dst
.regClass() == v1
) {
2146 Temp src0
= bld
.tmp(v1
);
2147 Temp src1
= bld
.tmp(v1
);
2148 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2149 if (!ctx
->block
->fp_mode
.care_about_round32
|| ctx
->block
->fp_mode
.round32
== fp_round_tz
)
2150 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2152 bld
.vop3(aco_opcode::v_cvt_pk_u16_u32
, Definition(dst
),
2153 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src0
),
2154 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src1
));
2156 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2157 nir_print_instr(&instr
->instr
, stderr
);
2158 fprintf(stderr
, "\n");
2162 case nir_op_unpack_half_2x16_split_x
: {
2163 if (dst
.regClass() == v1
) {
2164 Builder
bld(ctx
->program
, ctx
->block
);
2165 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2167 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2168 nir_print_instr(&instr
->instr
, stderr
);
2169 fprintf(stderr
, "\n");
2173 case nir_op_unpack_half_2x16_split_y
: {
2174 if (dst
.regClass() == v1
) {
2175 Builder
bld(ctx
->program
, ctx
->block
);
2176 /* TODO: use SDWA here */
2177 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2178 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2180 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2181 nir_print_instr(&instr
->instr
, stderr
);
2182 fprintf(stderr
, "\n");
2186 case nir_op_fquantize2f16
: {
2187 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2188 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), src
);
2191 if (ctx
->program
->chip_class
>= GFX8
) {
2192 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2193 cmp_res
= bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), f16
, mask
);
2194 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2196 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2197 * so compare the result and flush to 0 if it's smaller.
2199 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2200 Temp smallest
= bld
.copy(bld
.def(s1
), Operand(0x38800000u
));
2201 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), f32
, smallest
);
2202 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
2203 cmp_res
= vop3
->definitions
[0].getTemp();
2206 if (ctx
->block
->fp_mode
.preserve_signed_zero_inf_nan32
|| ctx
->program
->chip_class
< GFX8
) {
2207 Temp copysign_0
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0u), as_vgpr(ctx
, src
));
2208 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), copysign_0
, f32
, cmp_res
);
2210 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2215 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2216 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2218 if (dst
.regClass() == s1
) {
2219 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2220 } else if (dst
.regClass() == v1
) {
2221 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2223 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2224 nir_print_instr(&instr
->instr
, stderr
);
2225 fprintf(stderr
, "\n");
2229 case nir_op_bitfield_select
: {
2230 /* (mask & insert) | (~mask & base) */
2231 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2232 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2233 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2235 /* dst = (insert & bitmask) | (base & ~bitmask) */
2236 if (dst
.regClass() == s1
) {
2237 aco_ptr
<Instruction
> sop2
;
2238 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2239 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2241 if (const_insert
&& const_bitmask
) {
2242 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2244 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2245 lhs
= Operand(insert
);
2249 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2250 if (const_base
&& const_bitmask
) {
2251 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2253 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2254 rhs
= Operand(base
);
2257 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2259 } else if (dst
.regClass() == v1
) {
2260 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2261 base
= as_vgpr(ctx
, base
);
2262 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2263 insert
= as_vgpr(ctx
, insert
);
2265 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2268 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2269 nir_print_instr(&instr
->instr
, stderr
);
2270 fprintf(stderr
, "\n");
2276 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2277 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2278 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2280 if (dst
.type() == RegType::sgpr
) {
2282 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2283 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2284 if (const_offset
&& const_bits
) {
2285 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2286 extract
= Operand(const_extract
);
2290 width
= Operand(const_bits
->u32
<< 16);
2292 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2294 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2298 if (dst
.regClass() == s1
) {
2299 if (instr
->op
== nir_op_ubfe
)
2300 opcode
= aco_opcode::s_bfe_u32
;
2302 opcode
= aco_opcode::s_bfe_i32
;
2303 } else if (dst
.regClass() == s2
) {
2304 if (instr
->op
== nir_op_ubfe
)
2305 opcode
= aco_opcode::s_bfe_u64
;
2307 opcode
= aco_opcode::s_bfe_i64
;
2309 unreachable("Unsupported BFE bit size");
2312 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2316 if (dst
.regClass() == v1
) {
2317 if (instr
->op
== nir_op_ubfe
)
2318 opcode
= aco_opcode::v_bfe_u32
;
2320 opcode
= aco_opcode::v_bfe_i32
;
2322 unreachable("Unsupported BFE bit size");
2325 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2329 case nir_op_bit_count
: {
2330 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2331 if (src
.regClass() == s1
) {
2332 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2333 } else if (src
.regClass() == v1
) {
2334 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2335 } else if (src
.regClass() == v2
) {
2336 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2337 emit_extract_vector(ctx
, src
, 1, v1
),
2338 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2339 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2340 } else if (src
.regClass() == s2
) {
2341 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2343 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2344 nir_print_instr(&instr
->instr
, stderr
);
2345 fprintf(stderr
, "\n");
2350 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_f32
, aco_opcode::v_cmp_lt_f64
);
2354 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_f32
, aco_opcode::v_cmp_ge_f64
);
2358 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_f32
, aco_opcode::v_cmp_eq_f64
);
2362 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_neq_f32
, aco_opcode::v_cmp_neq_f64
);
2366 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_i32
, aco_opcode::v_cmp_lt_i64
, aco_opcode::s_cmp_lt_i32
);
2370 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_i32
, aco_opcode::v_cmp_ge_i64
, aco_opcode::s_cmp_ge_i32
);
2374 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2375 emit_boolean_logic(ctx
, instr
, Builder::s_xnor
, dst
);
2377 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_i32
, aco_opcode::v_cmp_eq_i64
, aco_opcode::s_cmp_eq_i32
, aco_opcode::s_cmp_eq_u64
);
2381 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2382 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
2384 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lg_i32
, aco_opcode::v_cmp_lg_i64
, aco_opcode::s_cmp_lg_i32
, aco_opcode::s_cmp_lg_u64
);
2388 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_u32
, aco_opcode::v_cmp_lt_u64
, aco_opcode::s_cmp_lt_u32
);
2392 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_u32
, aco_opcode::v_cmp_ge_u64
, aco_opcode::s_cmp_ge_u32
);
2397 case nir_op_fddx_fine
:
2398 case nir_op_fddy_fine
:
2399 case nir_op_fddx_coarse
:
2400 case nir_op_fddy_coarse
: {
2401 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2402 uint16_t dpp_ctrl1
, dpp_ctrl2
;
2403 if (instr
->op
== nir_op_fddx_fine
) {
2404 dpp_ctrl1
= dpp_quad_perm(0, 0, 2, 2);
2405 dpp_ctrl2
= dpp_quad_perm(1, 1, 3, 3);
2406 } else if (instr
->op
== nir_op_fddy_fine
) {
2407 dpp_ctrl1
= dpp_quad_perm(0, 1, 0, 1);
2408 dpp_ctrl2
= dpp_quad_perm(2, 3, 2, 3);
2410 dpp_ctrl1
= dpp_quad_perm(0, 0, 0, 0);
2411 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2412 dpp_ctrl2
= dpp_quad_perm(1, 1, 1, 1);
2414 dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
2418 if (ctx
->program
->chip_class
>= GFX8
) {
2419 Temp tl
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl1
);
2420 tmp
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), src
, tl
, dpp_ctrl2
);
2422 Temp tl
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl1
);
2423 Temp tr
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl2
);
2424 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), tr
, tl
);
2426 emit_wqm(ctx
, tmp
, dst
, true);
2430 fprintf(stderr
, "Unknown NIR ALU instr: ");
2431 nir_print_instr(&instr
->instr
, stderr
);
2432 fprintf(stderr
, "\n");
2436 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2438 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2440 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2441 // which get truncated the lsb if double and msb if int
2442 // for now, we only use s_mov_b64 with 64bit inline constants
2443 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2444 assert(dst
.type() == RegType::sgpr
);
2446 Builder
bld(ctx
->program
, ctx
->block
);
2448 if (instr
->def
.bit_size
== 1) {
2449 assert(dst
.regClass() == bld
.lm
);
2450 int val
= instr
->value
[0].b
? -1 : 0;
2451 Operand op
= bld
.lm
.size() == 1 ? Operand((uint32_t) val
) : Operand((uint64_t) val
);
2452 bld
.sop1(Builder::s_mov
, Definition(dst
), op
);
2453 } else if (dst
.size() == 1) {
2454 bld
.copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2456 assert(dst
.size() != 1);
2457 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2458 if (instr
->def
.bit_size
== 64)
2459 for (unsigned i
= 0; i
< dst
.size(); i
++)
2460 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2462 for (unsigned i
= 0; i
< dst
.size(); i
++)
2463 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2465 vec
->definitions
[0] = Definition(dst
);
2466 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2470 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2472 uint32_t new_mask
= 0;
2473 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2474 if (mask
& (1u << i
))
2475 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2479 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2481 /* This wouldn't work inside control flow or with indirect offsets but
2482 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2484 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2485 unsigned component
= nir_intrinsic_component(instr
);
2486 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2487 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2489 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2490 if (off_instr
->type
!= nir_instr_type_load_const
) {
2491 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2492 nir_print_instr(off_instr
, stderr
);
2493 fprintf(stderr
, "\n");
2495 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2497 if (instr
->src
[0].ssa
->bit_size
== 64)
2498 write_mask
= widen_mask(write_mask
, 2);
2500 for (unsigned i
= 0; i
< 8; ++i
) {
2501 if (write_mask
& (1 << i
)) {
2502 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2503 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2509 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2511 Builder
bld(ctx
->program
, ctx
->block
);
2512 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2514 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2515 for (unsigned i
= 0; i
< 4; ++i
) {
2516 if (write_mask
& (1 << i
)) {
2517 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2518 values
[i
] = Operand(tmp
);
2520 values
[i
] = Operand(v1
);
2524 unsigned index
= nir_intrinsic_base(instr
) / 4;
2525 unsigned target
, col_format
;
2526 unsigned enabled_channels
= 0xF;
2527 aco_opcode compr_op
= (aco_opcode
)0;
2529 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2530 assert(offset
&& "Non-const offsets on exports not yet supported");
2531 index
+= offset
->u32
;
2533 assert(index
!= FRAG_RESULT_COLOR
);
2535 /* Unlike vertex shader exports, it's fine to use multiple exports to
2536 * export separate channels of one target. So shaders which export both
2537 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2538 * TODO: combine the exports in those cases and create better code
2541 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2543 if (ctx
->program
->info
->ps
.writes_z
) {
2544 target
= V_008DFC_SQ_EXP_MRTZ
;
2545 enabled_channels
= 0x4;
2546 col_format
= (unsigned) -1;
2548 values
[2] = values
[0];
2549 values
[0] = Operand(v1
);
2551 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(values
[0]), Operand(v1
), Operand(v1
),
2552 0xc, V_008DFC_SQ_EXP_MRTZ
, true);
2556 } else if (index
== FRAG_RESULT_DEPTH
) {
2558 target
= V_008DFC_SQ_EXP_MRTZ
;
2559 enabled_channels
= 0x1;
2560 col_format
= (unsigned) -1;
2562 } else if (index
== FRAG_RESULT_STENCIL
) {
2564 if (ctx
->program
->info
->ps
.writes_z
) {
2565 target
= V_008DFC_SQ_EXP_MRTZ
;
2566 enabled_channels
= 0x2;
2567 col_format
= (unsigned) -1;
2569 values
[1] = values
[0];
2570 values
[0] = Operand(v1
);
2572 values
[0] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u), values
[0]);
2573 bld
.exp(aco_opcode::exp
, values
[0], Operand(v1
), Operand(v1
), Operand(v1
),
2574 0x3, V_008DFC_SQ_EXP_MRTZ
, true);
2579 index
-= FRAG_RESULT_DATA0
;
2580 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2581 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2583 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2584 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2588 case V_028714_SPI_SHADER_ZERO
:
2589 enabled_channels
= 0; /* writemask */
2590 target
= V_008DFC_SQ_EXP_NULL
;
2593 case V_028714_SPI_SHADER_32_R
:
2594 enabled_channels
= 1;
2597 case V_028714_SPI_SHADER_32_GR
:
2598 enabled_channels
= 0x3;
2601 case V_028714_SPI_SHADER_32_AR
:
2602 if (ctx
->options
->chip_class
>= GFX10
) {
2603 /* Special case: on GFX10, the outputs are different for 32_AR */
2604 enabled_channels
= 0x3;
2605 values
[1] = values
[3];
2606 values
[3] = Operand(v1
);
2608 enabled_channels
= 0x9;
2612 case V_028714_SPI_SHADER_FP16_ABGR
:
2613 enabled_channels
= 0x5;
2614 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2617 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2618 enabled_channels
= 0x5;
2619 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2622 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2623 enabled_channels
= 0x5;
2624 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2627 case V_028714_SPI_SHADER_UINT16_ABGR
: {
2628 enabled_channels
= 0x5;
2629 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2630 if (is_int8
|| is_int10
) {
2632 uint32_t max_rgb
= is_int8
? 255 : is_int10
? 1023 : 0;
2633 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
2635 for (unsigned i
= 0; i
< 4; i
++) {
2636 if ((write_mask
>> i
) & 1) {
2637 values
[i
] = bld
.vop2(aco_opcode::v_min_u32
, bld
.def(v1
),
2638 i
== 3 && is_int10
? Operand(3u) : Operand(max_rgb_val
),
2646 case V_028714_SPI_SHADER_SINT16_ABGR
:
2647 enabled_channels
= 0x5;
2648 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2649 if (is_int8
|| is_int10
) {
2651 uint32_t max_rgb
= is_int8
? 127 : is_int10
? 511 : 0;
2652 uint32_t min_rgb
= is_int8
? -128 :is_int10
? -512 : 0;
2653 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
2654 Temp min_rgb_val
= bld
.copy(bld
.def(s1
), Operand(min_rgb
));
2656 for (unsigned i
= 0; i
< 4; i
++) {
2657 if ((write_mask
>> i
) & 1) {
2658 values
[i
] = bld
.vop2(aco_opcode::v_min_i32
, bld
.def(v1
),
2659 i
== 3 && is_int10
? Operand(1u) : Operand(max_rgb_val
),
2661 values
[i
] = bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
),
2662 i
== 3 && is_int10
? Operand(-2u) : Operand(min_rgb_val
),
2669 case V_028714_SPI_SHADER_32_ABGR
:
2670 enabled_channels
= 0xF;
2677 if (target
== V_008DFC_SQ_EXP_NULL
)
2680 if ((bool) compr_op
) {
2681 for (int i
= 0; i
< 2; i
++) {
2682 /* check if at least one of the values to be compressed is enabled */
2683 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2685 enabled_channels
|= enabled
<< (i
*2);
2686 values
[i
] = bld
.vop3(compr_op
, bld
.def(v1
),
2687 values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2],
2688 values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1]);
2690 values
[i
] = Operand(v1
);
2693 values
[2] = Operand(v1
);
2694 values
[3] = Operand(v1
);
2696 for (int i
= 0; i
< 4; i
++)
2697 values
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2700 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
2701 enabled_channels
, target
, (bool) compr_op
);
2704 Operand
load_lds_size_m0(isel_context
*ctx
)
2706 /* TODO: m0 does not need to be initialized on GFX9+ */
2707 Builder
bld(ctx
->program
, ctx
->block
);
2708 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
2711 void load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
2712 Temp address
, unsigned base_offset
, unsigned align
)
2714 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2716 Builder
bld(ctx
->program
, ctx
->block
);
2718 Operand m
= load_lds_size_m0(ctx
);
2720 unsigned num_components
= dst
.size() * 4u / elem_size_bytes
;
2721 unsigned bytes_read
= 0;
2722 unsigned result_size
= 0;
2723 unsigned total_bytes
= num_components
* elem_size_bytes
;
2724 std::array
<Temp
, 4> result
;
2726 while (bytes_read
< total_bytes
) {
2727 unsigned todo
= total_bytes
- bytes_read
;
2728 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2729 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2731 aco_opcode op
= aco_opcode::last_opcode
;
2733 if (todo
>= 16 && aligned16
) {
2734 op
= aco_opcode::ds_read_b128
;
2736 } else if (todo
>= 16 && aligned8
) {
2737 op
= aco_opcode::ds_read2_b64
;
2740 } else if (todo
>= 12 && aligned16
) {
2741 op
= aco_opcode::ds_read_b96
;
2743 } else if (todo
>= 8 && aligned8
) {
2744 op
= aco_opcode::ds_read_b64
;
2746 } else if (todo
>= 8) {
2747 op
= aco_opcode::ds_read2_b32
;
2750 } else if (todo
>= 4) {
2751 op
= aco_opcode::ds_read_b32
;
2756 assert(todo
% elem_size_bytes
== 0);
2757 unsigned num_elements
= todo
/ elem_size_bytes
;
2758 unsigned offset
= base_offset
+ bytes_read
;
2759 unsigned max_offset
= read2
? 1019 : 65535;
2761 Temp address_offset
= address
;
2762 if (offset
> max_offset
) {
2763 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2764 offset
= bytes_read
;
2766 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2769 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2772 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2775 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
2777 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2779 if (num_components
== 1) {
2780 assert(todo
== total_bytes
);
2781 if (dst
.type() == RegType::sgpr
)
2782 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2786 if (dst
.type() == RegType::sgpr
)
2787 res
= bld
.as_uniform(res
);
2789 if (num_elements
== 1) {
2790 result
[result_size
++] = res
;
2792 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2793 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2794 split
->operands
[0] = Operand(res
);
2795 for (unsigned i
= 0; i
< num_elements
; i
++)
2796 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2797 ctx
->block
->instructions
.emplace_back(std::move(split
));
2803 assert(result_size
== num_components
&& result_size
> 1);
2804 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2805 for (unsigned i
= 0; i
< result_size
; i
++)
2806 vec
->operands
[i
] = Operand(result
[i
]);
2807 vec
->definitions
[0] = Definition(dst
);
2808 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2809 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2812 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2814 if (start
== 0 && size
== data
.size())
2815 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2817 unsigned size_hint
= 1;
2818 auto it
= ctx
->allocated_vec
.find(data
.id());
2819 if (it
!= ctx
->allocated_vec
.end())
2820 size_hint
= it
->second
[0].size();
2821 if (size
% size_hint
|| start
% size_hint
)
2828 for (unsigned i
= 0; i
< size
; i
++)
2829 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2832 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2834 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2835 for (unsigned i
= 0; i
< size
; i
++)
2836 vec
->operands
[i
] = Operand(elems
[i
]);
2837 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2838 vec
->definitions
[0] = Definition(res
);
2839 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2843 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
)
2845 Builder
bld(ctx
->program
, ctx
->block
);
2846 unsigned bytes_written
= 0;
2847 while (bytes_written
< total_size
* 4) {
2848 unsigned todo
= total_size
* 4 - bytes_written
;
2849 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
2850 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
2852 aco_opcode op
= aco_opcode::last_opcode
;
2853 bool write2
= false;
2855 if (todo
>= 16 && aligned16
) {
2856 op
= aco_opcode::ds_write_b128
;
2858 } else if (todo
>= 16 && aligned8
) {
2859 op
= aco_opcode::ds_write2_b64
;
2862 } else if (todo
>= 12 && aligned16
) {
2863 op
= aco_opcode::ds_write_b96
;
2865 } else if (todo
>= 8 && aligned8
) {
2866 op
= aco_opcode::ds_write_b64
;
2868 } else if (todo
>= 8) {
2869 op
= aco_opcode::ds_write2_b32
;
2872 } else if (todo
>= 4) {
2873 op
= aco_opcode::ds_write_b32
;
2879 unsigned offset
= offset0
+ offset1
+ bytes_written
;
2880 unsigned max_offset
= write2
? 1020 : 65535;
2881 Temp address_offset
= address
;
2882 if (offset
> max_offset
) {
2883 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
2884 offset
= offset1
+ bytes_written
;
2886 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
2889 Temp val0
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
/ 2, RegType::vgpr
);
2890 Temp val1
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2) + 1, size
/ 2, RegType::vgpr
);
2891 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
2893 Temp val
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
, RegType::vgpr
);
2894 bld
.ds(op
, address_offset
, val
, m
, offset
);
2897 bytes_written
+= size
* 4;
2901 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
2902 Temp address
, unsigned base_offset
, unsigned align
)
2904 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2906 Operand m
= load_lds_size_m0(ctx
);
2908 /* we need at most two stores for 32bit variables */
2909 int start
[2], count
[2];
2910 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2911 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2912 assert(wrmask
== 0);
2914 /* one combined store is sufficient */
2915 if (count
[0] == count
[1]) {
2916 Builder
bld(ctx
->program
, ctx
->block
);
2918 Temp address_offset
= address
;
2919 if ((base_offset
>> 2) + start
[1] > 255) {
2920 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2924 assert(count
[0] == 1);
2925 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
2926 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
2927 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2928 base_offset
= base_offset
/ elem_size_bytes
;
2929 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2930 base_offset
+ start
[0], base_offset
+ start
[1]);
2934 for (unsigned i
= 0; i
< 2; i
++) {
2938 unsigned elem_size_words
= elem_size_bytes
/ 4;
2939 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2940 base_offset
, start
[i
] * elem_size_bytes
, align
);
2945 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2947 if (ctx
->stage
== vertex_vs
) {
2948 visit_store_vs_output(ctx
, instr
);
2949 } else if (ctx
->stage
== fragment_fs
) {
2950 visit_store_fs_output(ctx
, instr
);
2952 unreachable("Shader stage not implemented");
2956 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2958 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2959 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2961 Builder
bld(ctx
->program
, ctx
->block
);
2962 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2963 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2966 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2968 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2969 for (unsigned i
= 0; i
< num_components
; i
++)
2970 vec
->operands
[i
] = Operand(get_arg(ctx
, ctx
->args
->ac
.frag_pos
[i
]));
2971 if (G_0286CC_POS_W_FLOAT_ENA(ctx
->program
->config
->spi_ps_input_ena
)) {
2972 assert(num_components
== 4);
2973 Builder
bld(ctx
->program
, ctx
->block
);
2974 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->ac
.frag_pos
[3]));
2977 for (Operand
& op
: vec
->operands
)
2978 op
= op
.isUndefined() ? Operand(0u) : op
;
2980 vec
->definitions
[0] = Definition(dst
);
2981 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2982 emit_split_vector(ctx
, dst
, num_components
);
2986 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2988 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2989 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2990 unsigned idx
= nir_intrinsic_base(instr
);
2991 unsigned component
= nir_intrinsic_component(instr
);
2992 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
2994 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2996 assert(offset
->u32
== 0);
2998 /* the lower 15bit of the prim_mask contain the offset into LDS
2999 * while the upper bits contain the number of prims */
3000 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3001 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3002 Builder
bld(ctx
->program
, ctx
->block
);
3003 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3004 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3005 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3006 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3007 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3010 if (instr
->dest
.ssa
.num_components
== 1) {
3011 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
3013 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
3014 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
3016 Temp tmp
= {ctx
->program
->allocateId(), v1
};
3017 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
3018 vec
->operands
[i
] = Operand(tmp
);
3020 vec
->definitions
[0] = Definition(dst
);
3021 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3025 unsigned get_num_channels_from_data_format(unsigned data_format
)
3027 switch (data_format
) {
3028 case V_008F0C_BUF_DATA_FORMAT_8
:
3029 case V_008F0C_BUF_DATA_FORMAT_16
:
3030 case V_008F0C_BUF_DATA_FORMAT_32
:
3032 case V_008F0C_BUF_DATA_FORMAT_8_8
:
3033 case V_008F0C_BUF_DATA_FORMAT_16_16
:
3034 case V_008F0C_BUF_DATA_FORMAT_32_32
:
3036 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
3037 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
3038 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
3040 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
3041 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
3042 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
3043 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
3044 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
3053 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3054 * so we may need to fix it up. */
3055 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
3057 Builder
bld(ctx
->program
, ctx
->block
);
3059 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3060 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3062 /* For the integer-like cases, do a natural sign extension.
3064 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3065 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3068 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3069 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3071 /* Convert back to the right type. */
3072 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3073 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3074 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0xbf800000u
), alpha
);
3075 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3076 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3077 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3083 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3085 Builder
bld(ctx
->program
, ctx
->block
);
3086 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3087 if (ctx
->stage
& sw_vs
) {
3089 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3090 if (off_instr
->type
!= nir_instr_type_load_const
) {
3091 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3092 nir_print_instr(off_instr
, stderr
);
3093 fprintf(stderr
, "\n");
3095 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3097 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->vertex_buffers
));
3099 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3100 unsigned component
= nir_intrinsic_component(instr
);
3101 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3102 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3103 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3104 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3106 unsigned dfmt
= attrib_format
& 0xf;
3108 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3109 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
3110 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3111 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
3112 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3113 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3115 num_channels
= MAX2(num_channels
, 3);
3117 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
3120 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3121 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3122 Temp start_instance
= get_arg(ctx
, ctx
->args
->ac
.start_instance
);
3124 ctx
->needs_instance_id
= true;
3125 Temp instance_id
= get_arg(ctx
, ctx
->args
->ac
.instance_id
);
3127 Temp divided
= bld
.tmp(v1
);
3128 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, instance_id
), divisor
);
3129 index
= bld
.vadd32(bld
.def(v1
), start_instance
, divided
);
3131 index
= bld
.vadd32(bld
.def(v1
), start_instance
, instance_id
);
3134 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), start_instance
);
3137 index
= bld
.vadd32(bld
.def(v1
),
3138 get_arg(ctx
, ctx
->args
->ac
.base_vertex
),
3139 get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
3142 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
3143 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
3144 attrib_offset
= attrib_offset
% attrib_stride
;
3147 Operand
soffset(0u);
3148 if (attrib_offset
>= 4096) {
3149 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
3154 switch (num_channels
) {
3156 opcode
= aco_opcode::tbuffer_load_format_x
;
3159 opcode
= aco_opcode::tbuffer_load_format_xy
;
3162 opcode
= aco_opcode::tbuffer_load_format_xyz
;
3165 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
3168 unreachable("Unimplemented load_input vector size");
3171 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
3173 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
3174 mubuf
->operands
[0] = Operand(index
);
3175 mubuf
->operands
[1] = Operand(list
);
3176 mubuf
->operands
[2] = soffset
;
3177 mubuf
->definitions
[0] = Definition(tmp
);
3178 mubuf
->idxen
= true;
3179 mubuf
->can_reorder
= true;
3182 assert(attrib_offset
< 4096);
3183 mubuf
->offset
= attrib_offset
;
3184 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3186 emit_split_vector(ctx
, tmp
, tmp
.size());
3188 if (tmp
.id() != dst
.id()) {
3189 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3190 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3192 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3193 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3194 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3196 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3197 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3198 unsigned idx
= i
+ component
;
3199 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
3200 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
3201 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
3202 } else if (idx
< num_channels
) {
3203 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
3204 } else if (is_float
&& idx
== 3) {
3205 vec
->operands
[i
] = Operand(0x3f800000u
);
3206 } else if (!is_float
&& idx
== 3) {
3207 vec
->operands
[i
] = Operand(1u);
3209 vec
->operands
[i
] = Operand(0u);
3212 vec
->definitions
[0] = Definition(dst
);
3213 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3214 emit_split_vector(ctx
, dst
, dst
.size());
3217 } else if (ctx
->stage
== fragment_fs
) {
3218 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3219 if (off_instr
->type
!= nir_instr_type_load_const
||
3220 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3221 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3222 nir_print_instr(off_instr
, stderr
);
3223 fprintf(stderr
, "\n");
3226 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
3227 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
3229 assert(offset
->u32
== 0);
3231 /* the lower 15bit of the prim_mask contain the offset into LDS
3232 * while the upper bits contain the number of prims */
3233 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3234 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3235 Builder
bld(ctx
->program
, ctx
->block
);
3236 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3237 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3238 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3239 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3240 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3243 unsigned idx
= nir_intrinsic_base(instr
);
3244 unsigned component
= nir_intrinsic_component(instr
);
3246 if (dst
.size() == 1) {
3247 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
3249 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3250 for (unsigned i
= 0; i
< dst
.size(); i
++)
3251 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
3252 vec
->definitions
[0] = Definition(dst
);
3253 bld
.insert(std::move(vec
));
3257 unreachable("Shader stage not implemented");
3261 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3263 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3264 Builder
bld(ctx
->program
, ctx
->block
);
3265 Temp ptr64
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->descriptor_sets
[0]));
3266 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
3269 return get_arg(ctx
, ctx
->args
->descriptor_sets
[desc_set
]);
3273 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3275 Builder
bld(ctx
->program
, ctx
->block
);
3276 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3277 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3278 index
= bld
.as_uniform(index
);
3279 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3280 unsigned binding
= nir_intrinsic_binding(instr
);
3283 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3284 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3285 unsigned offset
= layout
->binding
[binding
].offset
;
3287 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3288 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3289 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3290 desc_ptr
= get_arg(ctx
, ctx
->args
->ac
.push_constants
);
3291 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3294 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3295 stride
= layout
->binding
[binding
].size
;
3298 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3299 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3301 if (nir_const_index
) {
3302 const_index
= const_index
* stride
;
3303 } else if (index
.type() == RegType::vgpr
) {
3304 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3305 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3307 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3311 if (nir_const_index
) {
3312 const_index
= const_index
+ offset
;
3313 } else if (index
.type() == RegType::vgpr
) {
3314 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
3316 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
3320 if (nir_const_index
&& const_index
== 0) {
3322 } else if (index
.type() == RegType::vgpr
) {
3323 index
= bld
.vadd32(bld
.def(v1
),
3324 nir_const_index
? Operand(const_index
) : Operand(index
),
3327 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3328 nir_const_index
? Operand(const_index
) : Operand(index
),
3332 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
3335 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
,
3336 Temp rsrc
, Temp offset
, bool glc
=false, bool readonly
=true)
3338 Builder
bld(ctx
->program
, ctx
->block
);
3340 unsigned num_bytes
= dst
.size() * 4;
3341 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
3344 if (dst
.type() == RegType::vgpr
|| (ctx
->options
->chip_class
< GFX8
&& !readonly
)) {
3345 if (ctx
->options
->chip_class
< GFX8
)
3346 offset
= as_vgpr(ctx
, offset
);
3348 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3349 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3350 unsigned const_offset
= 0;
3352 Temp lower
= Temp();
3353 if (num_bytes
> 16) {
3354 assert(num_components
== 3 || num_components
== 4);
3355 op
= aco_opcode::buffer_load_dwordx4
;
3356 lower
= bld
.tmp(v4
);
3357 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3358 mubuf
->definitions
[0] = Definition(lower
);
3359 mubuf
->operands
[0] = vaddr
;
3360 mubuf
->operands
[1] = Operand(rsrc
);
3361 mubuf
->operands
[2] = soffset
;
3362 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3365 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3366 mubuf
->can_reorder
= readonly
;
3367 bld
.insert(std::move(mubuf
));
3368 emit_split_vector(ctx
, lower
, 2);
3373 switch (num_bytes
) {
3375 op
= aco_opcode::buffer_load_dword
;
3378 op
= aco_opcode::buffer_load_dwordx2
;
3381 op
= aco_opcode::buffer_load_dwordx3
;
3384 op
= aco_opcode::buffer_load_dwordx4
;
3387 unreachable("Load SSBO not implemented for this size.");
3389 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3390 mubuf
->operands
[0] = vaddr
;
3391 mubuf
->operands
[1] = Operand(rsrc
);
3392 mubuf
->operands
[2] = soffset
;
3393 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3396 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3397 mubuf
->can_reorder
= readonly
;
3398 mubuf
->offset
= const_offset
;
3399 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3401 if (dst
.size() > 4) {
3402 assert(lower
!= Temp());
3403 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3404 instr
->definitions
[0] = Definition(upper
);
3405 bld
.insert(std::move(instr
));
3406 if (dst
.size() == 8)
3407 emit_split_vector(ctx
, upper
, 2);
3408 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3409 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3410 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3411 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3412 if (dst
.size() == 8)
3413 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3416 if (dst
.type() == RegType::sgpr
) {
3417 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3418 instr
->definitions
[0] = Definition(vec
);
3419 bld
.insert(std::move(instr
));
3420 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3422 instr
->definitions
[0] = Definition(dst
);
3423 bld
.insert(std::move(instr
));
3426 switch (num_bytes
) {
3428 op
= aco_opcode::s_buffer_load_dword
;
3431 op
= aco_opcode::s_buffer_load_dwordx2
;
3435 op
= aco_opcode::s_buffer_load_dwordx4
;
3439 op
= aco_opcode::s_buffer_load_dwordx8
;
3442 unreachable("Load SSBO not implemented for this size.");
3444 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3445 load
->operands
[0] = Operand(rsrc
);
3446 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3447 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3448 load
->definitions
[0] = Definition(dst
);
3451 load
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3452 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3453 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3456 if (dst
.size() == 3) {
3457 Temp vec
= bld
.tmp(s4
);
3458 load
->definitions
[0] = Definition(vec
);
3459 bld
.insert(std::move(load
));
3460 emit_split_vector(ctx
, vec
, 4);
3462 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3463 emit_extract_vector(ctx
, vec
, 0, s1
),
3464 emit_extract_vector(ctx
, vec
, 1, s1
),
3465 emit_extract_vector(ctx
, vec
, 2, s1
));
3466 } else if (dst
.size() == 6) {
3467 Temp vec
= bld
.tmp(s8
);
3468 load
->definitions
[0] = Definition(vec
);
3469 bld
.insert(std::move(load
));
3470 emit_split_vector(ctx
, vec
, 4);
3472 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3473 emit_extract_vector(ctx
, vec
, 0, s2
),
3474 emit_extract_vector(ctx
, vec
, 1, s2
),
3475 emit_extract_vector(ctx
, vec
, 2, s2
));
3477 bld
.insert(std::move(load
));
3481 emit_split_vector(ctx
, dst
, num_components
);
3484 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3486 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3487 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3489 Builder
bld(ctx
->program
, ctx
->block
);
3491 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3492 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3493 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3494 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3496 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3497 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3498 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3499 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3500 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3501 if (ctx
->options
->chip_class
>= GFX10
) {
3502 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3503 S_008F0C_OOB_SELECT(3) |
3504 S_008F0C_RESOURCE_LEVEL(1);
3506 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3507 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3509 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3510 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3511 Operand(0xFFFFFFFFu
),
3512 Operand(desc_type
));
3513 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3514 rsrc
, upper_dwords
);
3516 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3517 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3520 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3523 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3525 Builder
bld(ctx
->program
, ctx
->block
);
3526 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3528 unsigned offset
= nir_intrinsic_base(instr
);
3529 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3530 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3532 unsigned count
= instr
->dest
.ssa
.num_components
;
3533 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3534 start
-= ctx
->args
->ac
.base_inline_push_consts
;
3535 if (start
+ count
<= ctx
->args
->ac
.num_inline_push_consts
) {
3536 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3537 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3538 for (unsigned i
= 0; i
< count
; ++i
) {
3539 elems
[i
] = get_arg(ctx
, ctx
->args
->ac
.inline_push_consts
[start
+ i
]);
3540 vec
->operands
[i
] = Operand
{elems
[i
]};
3542 vec
->definitions
[0] = Definition(dst
);
3543 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3544 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3549 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3550 if (offset
!= 0) // TODO check if index != 0 as well
3551 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3552 Temp ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->ac
.push_constants
));
3557 switch (dst
.size()) {
3559 op
= aco_opcode::s_load_dword
;
3562 op
= aco_opcode::s_load_dwordx2
;
3568 op
= aco_opcode::s_load_dwordx4
;
3574 op
= aco_opcode::s_load_dwordx8
;
3577 unreachable("unimplemented or forbidden load_push_constant.");
3580 bld
.smem(op
, Definition(vec
), ptr
, index
);
3583 emit_split_vector(ctx
, vec
, 4);
3584 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3585 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3586 emit_extract_vector(ctx
, vec
, 0, rc
),
3587 emit_extract_vector(ctx
, vec
, 1, rc
),
3588 emit_extract_vector(ctx
, vec
, 2, rc
));
3591 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3594 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3596 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3598 Builder
bld(ctx
->program
, ctx
->block
);
3600 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3601 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3602 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3603 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3604 if (ctx
->options
->chip_class
>= GFX10
) {
3605 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3606 S_008F0C_OOB_SELECT(3) |
3607 S_008F0C_RESOURCE_LEVEL(1);
3609 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3610 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3613 unsigned base
= nir_intrinsic_base(instr
);
3614 unsigned range
= nir_intrinsic_range(instr
);
3616 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3617 if (base
&& offset
.type() == RegType::sgpr
)
3618 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3619 else if (base
&& offset
.type() == RegType::vgpr
)
3620 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3622 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3623 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
3624 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
3625 Operand(desc_type
));
3627 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3630 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3632 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3633 ctx
->cf_info
.exec_potentially_empty
= true;
3635 ctx
->program
->needs_exact
= true;
3637 // TODO: optimize uniform conditions
3638 Builder
bld(ctx
->program
, ctx
->block
);
3639 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3640 assert(src
.regClass() == bld
.lm
);
3641 src
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
3642 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3643 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3647 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3649 Builder
bld(ctx
->program
, ctx
->block
);
3651 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3652 ctx
->cf_info
.exec_potentially_empty
= true;
3654 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3655 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3657 if (ctx
->block
->loop_nest_depth
&&
3658 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3659 /* we handle discards the same way as jump instructions */
3660 append_logical_end(ctx
->block
);
3662 /* in loops, discard behaves like break */
3663 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3664 ctx
->block
->kind
|= block_kind_discard
;
3667 /* uniform discard - loop ends here */
3668 assert(nir_instr_is_last(&instr
->instr
));
3669 ctx
->block
->kind
|= block_kind_uniform
;
3670 ctx
->cf_info
.has_branch
= true;
3671 bld
.branch(aco_opcode::p_branch
);
3672 add_linear_edge(ctx
->block
->index
, linear_target
);
3676 /* we add a break right behind the discard() instructions */
3677 ctx
->block
->kind
|= block_kind_break
;
3678 unsigned idx
= ctx
->block
->index
;
3680 /* remove critical edges from linear CFG */
3681 bld
.branch(aco_opcode::p_branch
);
3682 Block
* break_block
= ctx
->program
->create_and_insert_block();
3683 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3684 break_block
->kind
|= block_kind_uniform
;
3685 add_linear_edge(idx
, break_block
);
3686 add_linear_edge(break_block
->index
, linear_target
);
3687 bld
.reset(break_block
);
3688 bld
.branch(aco_opcode::p_branch
);
3690 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3691 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3692 add_linear_edge(idx
, continue_block
);
3693 append_logical_start(continue_block
);
3694 ctx
->block
= continue_block
;
3699 /* it can currently happen that NIR doesn't remove the unreachable code */
3700 if (!nir_instr_is_last(&instr
->instr
)) {
3701 ctx
->program
->needs_exact
= true;
3702 /* save exec somewhere temporarily so that it doesn't get
3703 * overwritten before the discard from outer exec masks */
3704 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, bld
.lm
));
3705 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3706 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3710 /* This condition is incorrect for uniformly branched discards in a loop
3711 * predicated by a divergent condition, but the above code catches that case
3712 * and the discard would end up turning into a discard_if.
3722 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3723 /* program just ends here */
3724 ctx
->block
->kind
|= block_kind_uniform
;
3725 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3726 0 /* enabled mask */, 9 /* dest */,
3727 false /* compressed */, true/* done */, true /* valid mask */);
3728 bld
.sopp(aco_opcode::s_endpgm
);
3729 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3731 ctx
->block
->kind
|= block_kind_discard
;
3732 /* branch and linear edge is added by visit_if() */
3736 enum aco_descriptor_type
{
3747 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3748 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3750 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
3751 return dim
== ac_image_cube
||
3752 dim
== ac_image_1darray
||
3753 dim
== ac_image_2darray
||
3754 dim
== ac_image_2darraymsaa
;
3757 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3758 enum aco_descriptor_type desc_type
,
3759 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3761 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3762 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3763 if (it != ctx->tex_desc.end())
3766 Temp index
= Temp();
3767 bool index_set
= false;
3768 unsigned constant_index
= 0;
3769 unsigned descriptor_set
;
3770 unsigned base_index
;
3771 Builder
bld(ctx
->program
, ctx
->block
);
3774 assert(tex_instr
&& !image
);
3776 base_index
= tex_instr
->sampler_index
;
3778 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3779 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3783 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3784 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3786 constant_index
+= array_size
* const_value
->u32
;
3788 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
3789 if (indirect
.type() == RegType::vgpr
)
3790 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
3792 if (array_size
!= 1)
3793 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3799 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3803 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3805 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3806 base_index
= deref_instr
->var
->data
.binding
;
3809 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3810 list
= convert_pointer_to_64_bit(ctx
, list
);
3812 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3813 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3814 unsigned offset
= binding
->offset
;
3815 unsigned stride
= binding
->size
;
3819 assert(base_index
< layout
->binding_count
);
3821 switch (desc_type
) {
3822 case ACO_DESC_IMAGE
:
3824 opcode
= aco_opcode::s_load_dwordx8
;
3826 case ACO_DESC_FMASK
:
3828 opcode
= aco_opcode::s_load_dwordx8
;
3831 case ACO_DESC_SAMPLER
:
3833 opcode
= aco_opcode::s_load_dwordx4
;
3834 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3835 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3837 case ACO_DESC_BUFFER
:
3839 opcode
= aco_opcode::s_load_dwordx4
;
3841 case ACO_DESC_PLANE_0
:
3842 case ACO_DESC_PLANE_1
:
3844 opcode
= aco_opcode::s_load_dwordx8
;
3845 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3847 case ACO_DESC_PLANE_2
:
3849 opcode
= aco_opcode::s_load_dwordx4
;
3853 unreachable("invalid desc_type\n");
3856 offset
+= constant_index
* stride
;
3858 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3859 (!index_set
|| binding
->immutable_samplers_equal
)) {
3860 if (binding
->immutable_samplers_equal
)
3863 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3864 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3865 Operand(samplers
[constant_index
* 4 + 0]),
3866 Operand(samplers
[constant_index
* 4 + 1]),
3867 Operand(samplers
[constant_index
* 4 + 2]),
3868 Operand(samplers
[constant_index
* 4 + 3]));
3873 off
= Operand(offset
);
3875 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3876 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3879 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3881 if (desc_type
== ACO_DESC_PLANE_2
) {
3883 for (unsigned i
= 0; i
< 8; i
++)
3884 components
[i
] = bld
.tmp(s1
);
3885 bld
.pseudo(aco_opcode::p_split_vector
,
3886 Definition(components
[0]),
3887 Definition(components
[1]),
3888 Definition(components
[2]),
3889 Definition(components
[3]),
3892 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3893 bld
.pseudo(aco_opcode::p_split_vector
,
3894 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3895 Definition(components
[4]),
3896 Definition(components
[5]),
3897 Definition(components
[6]),
3898 Definition(components
[7]),
3901 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3902 components
[0], components
[1], components
[2], components
[3],
3903 components
[4], components
[5], components
[6], components
[7]);
3909 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3912 case GLSL_SAMPLER_DIM_BUF
:
3914 case GLSL_SAMPLER_DIM_1D
:
3915 return array
? 2 : 1;
3916 case GLSL_SAMPLER_DIM_2D
:
3917 return array
? 3 : 2;
3918 case GLSL_SAMPLER_DIM_MS
:
3919 return array
? 4 : 3;
3920 case GLSL_SAMPLER_DIM_3D
:
3921 case GLSL_SAMPLER_DIM_CUBE
:
3923 case GLSL_SAMPLER_DIM_RECT
:
3924 case GLSL_SAMPLER_DIM_SUBPASS
:
3926 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3935 /* Adjust the sample index according to FMASK.
3937 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3938 * which is the identity mapping. Each nibble says which physical sample
3939 * should be fetched to get that sample.
3941 * For example, 0x11111100 means there are only 2 samples stored and
3942 * the second sample covers 3/4 of the pixel. When reading samples 0
3943 * and 1, return physical sample 0 (determined by the first two 0s
3944 * in FMASK), otherwise return physical sample 1.
3946 * The sample index should be adjusted as follows:
3947 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3949 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3951 Builder
bld(ctx
->program
, ctx
->block
);
3952 Temp fmask
= bld
.tmp(v1
);
3953 unsigned dim
= ctx
->options
->chip_class
>= GFX10
3954 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
3957 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3958 load
->operands
[0] = Operand(coords
);
3959 load
->operands
[1] = Operand(fmask_desc_ptr
);
3960 load
->definitions
[0] = Definition(fmask
);
3967 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3968 ctx
->block
->instructions
.emplace_back(std::move(load
));
3970 Operand sample_index4
;
3971 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3972 sample_index4
= Operand(sample_index
.constantValue() << 2);
3973 } else if (sample_index
.regClass() == s1
) {
3974 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3976 assert(sample_index
.regClass() == v1
);
3977 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3981 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3982 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3983 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3984 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3986 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3988 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3989 * resource descriptor is 0 (invalid),
3991 Temp compare
= bld
.tmp(bld
.lm
);
3992 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3993 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3995 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3997 /* Replace the MSAA sample index. */
3998 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
4001 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
4004 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4005 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4006 bool is_array
= glsl_sampler_type_is_array(type
);
4007 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
4008 assert(!add_frag_pos
&& "Input attachments should be lowered.");
4009 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
4010 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
4011 int count
= image_type_to_components_count(dim
, is_array
);
4012 std::vector
<Operand
> coords(count
);
4015 Operand sample_index
;
4016 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
4018 sample_index
= Operand(sample_cv
->u32
);
4020 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
4022 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
4023 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
4024 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
4025 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
4026 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
4027 vec
->definitions
[0] = Definition(fmask_load_address
);
4028 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4030 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
4031 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
4034 coords
[count
] = sample_index
;
4037 if (count
== 1 && !gfx9_1d
)
4038 return emit_extract_vector(ctx
, src0
, 0, v1
);
4041 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
4042 coords
.resize(coords
.size() + 1);
4043 coords
[1] = Operand((uint32_t) 0);
4045 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
4047 for (int i
= 0; i
< count
; i
++)
4048 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
4051 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
4052 for (unsigned i
= 0; i
< coords
.size(); i
++)
4053 vec
->operands
[i
] = coords
[i
];
4054 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
4055 vec
->definitions
[0] = Definition(res
);
4056 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4061 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4063 Builder
bld(ctx
->program
, ctx
->block
);
4064 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4065 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4066 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4067 bool is_array
= glsl_sampler_type_is_array(type
);
4068 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4070 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4071 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4072 unsigned num_channels
= util_last_bit(mask
);
4073 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4074 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4077 switch (num_channels
) {
4079 opcode
= aco_opcode::buffer_load_format_x
;
4082 opcode
= aco_opcode::buffer_load_format_xy
;
4085 opcode
= aco_opcode::buffer_load_format_xyz
;
4088 opcode
= aco_opcode::buffer_load_format_xyzw
;
4091 unreachable(">4 channel buffer image load");
4093 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4094 load
->operands
[0] = Operand(vindex
);
4095 load
->operands
[1] = Operand(rsrc
);
4096 load
->operands
[2] = Operand((uint32_t) 0);
4098 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4101 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4102 load
->definitions
[0] = Definition(tmp
);
4104 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4105 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
4106 load
->barrier
= barrier_image
;
4107 ctx
->block
->instructions
.emplace_back(std::move(load
));
4109 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4113 Temp coords
= get_image_coords(ctx
, instr
, type
);
4114 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4116 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4117 unsigned num_components
= util_bitcount(dmask
);
4119 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4122 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4124 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
4125 load
->operands
[0] = Operand(coords
);
4126 load
->operands
[1] = Operand(resource
);
4127 load
->definitions
[0] = Definition(tmp
);
4128 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4129 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
4130 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4131 load
->dmask
= dmask
;
4133 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4134 load
->barrier
= barrier_image
;
4135 ctx
->block
->instructions
.emplace_back(std::move(load
));
4137 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4141 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4143 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4144 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4145 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4146 bool is_array
= glsl_sampler_type_is_array(type
);
4147 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4149 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4151 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4152 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4153 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4155 switch (data
.size()) {
4157 opcode
= aco_opcode::buffer_store_format_x
;
4160 opcode
= aco_opcode::buffer_store_format_xy
;
4163 opcode
= aco_opcode::buffer_store_format_xyz
;
4166 opcode
= aco_opcode::buffer_store_format_xyzw
;
4169 unreachable(">4 channel buffer image store");
4171 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4172 store
->operands
[0] = Operand(vindex
);
4173 store
->operands
[1] = Operand(rsrc
);
4174 store
->operands
[2] = Operand((uint32_t) 0);
4175 store
->operands
[3] = Operand(data
);
4176 store
->idxen
= true;
4179 store
->disable_wqm
= true;
4180 store
->barrier
= barrier_image
;
4181 ctx
->program
->needs_exact
= true;
4182 ctx
->block
->instructions
.emplace_back(std::move(store
));
4186 assert(data
.type() == RegType::vgpr
);
4187 Temp coords
= get_image_coords(ctx
, instr
, type
);
4188 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4190 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
4191 store
->operands
[0] = Operand(coords
);
4192 store
->operands
[1] = Operand(resource
);
4193 store
->operands
[2] = Operand(s4
);
4194 store
->operands
[3] = Operand(data
);
4197 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4198 store
->dmask
= (1 << data
.size()) - 1;
4200 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4201 store
->disable_wqm
= true;
4202 store
->barrier
= barrier_image
;
4203 ctx
->program
->needs_exact
= true;
4204 ctx
->block
->instructions
.emplace_back(std::move(store
));
4208 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4210 /* return the previous value if dest is ever used */
4211 bool return_previous
= false;
4212 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4213 return_previous
= true;
4216 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4217 return_previous
= true;
4221 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4222 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4223 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4224 bool is_array
= glsl_sampler_type_is_array(type
);
4225 Builder
bld(ctx
->program
, ctx
->block
);
4227 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4228 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4230 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4231 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4233 aco_opcode buf_op
, image_op
;
4234 switch (instr
->intrinsic
) {
4235 case nir_intrinsic_image_deref_atomic_add
:
4236 buf_op
= aco_opcode::buffer_atomic_add
;
4237 image_op
= aco_opcode::image_atomic_add
;
4239 case nir_intrinsic_image_deref_atomic_umin
:
4240 buf_op
= aco_opcode::buffer_atomic_umin
;
4241 image_op
= aco_opcode::image_atomic_umin
;
4243 case nir_intrinsic_image_deref_atomic_imin
:
4244 buf_op
= aco_opcode::buffer_atomic_smin
;
4245 image_op
= aco_opcode::image_atomic_smin
;
4247 case nir_intrinsic_image_deref_atomic_umax
:
4248 buf_op
= aco_opcode::buffer_atomic_umax
;
4249 image_op
= aco_opcode::image_atomic_umax
;
4251 case nir_intrinsic_image_deref_atomic_imax
:
4252 buf_op
= aco_opcode::buffer_atomic_smax
;
4253 image_op
= aco_opcode::image_atomic_smax
;
4255 case nir_intrinsic_image_deref_atomic_and
:
4256 buf_op
= aco_opcode::buffer_atomic_and
;
4257 image_op
= aco_opcode::image_atomic_and
;
4259 case nir_intrinsic_image_deref_atomic_or
:
4260 buf_op
= aco_opcode::buffer_atomic_or
;
4261 image_op
= aco_opcode::image_atomic_or
;
4263 case nir_intrinsic_image_deref_atomic_xor
:
4264 buf_op
= aco_opcode::buffer_atomic_xor
;
4265 image_op
= aco_opcode::image_atomic_xor
;
4267 case nir_intrinsic_image_deref_atomic_exchange
:
4268 buf_op
= aco_opcode::buffer_atomic_swap
;
4269 image_op
= aco_opcode::image_atomic_swap
;
4271 case nir_intrinsic_image_deref_atomic_comp_swap
:
4272 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4273 image_op
= aco_opcode::image_atomic_cmpswap
;
4276 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4279 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4281 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4282 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4283 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4284 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4285 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4286 mubuf
->operands
[0] = Operand(vindex
);
4287 mubuf
->operands
[1] = Operand(resource
);
4288 mubuf
->operands
[2] = Operand((uint32_t)0);
4289 mubuf
->operands
[3] = Operand(data
);
4290 if (return_previous
)
4291 mubuf
->definitions
[0] = Definition(dst
);
4293 mubuf
->idxen
= true;
4294 mubuf
->glc
= return_previous
;
4295 mubuf
->dlc
= false; /* Not needed for atomics */
4296 mubuf
->disable_wqm
= true;
4297 mubuf
->barrier
= barrier_image
;
4298 ctx
->program
->needs_exact
= true;
4299 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4303 Temp coords
= get_image_coords(ctx
, instr
, type
);
4304 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4305 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
4306 mimg
->operands
[0] = Operand(coords
);
4307 mimg
->operands
[1] = Operand(resource
);
4308 mimg
->operands
[2] = Operand(s4
); /* no sampler */
4309 mimg
->operands
[3] = Operand(data
);
4310 if (return_previous
)
4311 mimg
->definitions
[0] = Definition(dst
);
4312 mimg
->glc
= return_previous
;
4313 mimg
->dlc
= false; /* Not needed for atomics */
4314 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4315 mimg
->dmask
= (1 << data
.size()) - 1;
4317 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4318 mimg
->disable_wqm
= true;
4319 mimg
->barrier
= barrier_image
;
4320 ctx
->program
->needs_exact
= true;
4321 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4325 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4327 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4328 Builder
bld(ctx
->program
, ctx
->block
);
4330 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4331 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4332 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
4333 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
4335 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4336 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
4338 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4339 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4340 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4342 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4344 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4345 * in case 12 (or 3?), we have to divide by 3:
4346 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4347 * use v_mul_hi_u32 with magic number to divide
4348 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4350 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4354 emit_extract_vector(ctx
, desc
, 2, dst
);
4358 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4360 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4361 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4362 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4363 bool is_array
= glsl_sampler_type_is_array(type
);
4364 Builder
bld(ctx
->program
, ctx
->block
);
4366 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4367 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4368 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4372 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4375 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4377 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4379 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4380 mimg
->operands
[0] = Operand(lod
);
4381 mimg
->operands
[1] = Operand(resource
);
4382 unsigned& dmask
= mimg
->dmask
;
4383 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4384 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4385 mimg
->da
= glsl_sampler_type_is_array(type
);
4386 mimg
->can_reorder
= true;
4387 Definition
& def
= mimg
->definitions
[0];
4388 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4390 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4391 glsl_sampler_type_is_array(type
)) {
4393 assert(instr
->dest
.ssa
.num_components
== 3);
4394 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4395 def
= Definition(tmp
);
4396 emit_split_vector(ctx
, tmp
, 3);
4398 /* divide 3rd value by 6 by multiplying with magic number */
4399 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4400 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4402 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4403 emit_extract_vector(ctx
, tmp
, 0, v1
),
4404 emit_extract_vector(ctx
, tmp
, 1, v1
),
4407 } else if (ctx
->options
->chip_class
== GFX9
&&
4408 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4409 glsl_sampler_type_is_array(type
)) {
4410 assert(instr
->dest
.ssa
.num_components
== 2);
4411 def
= Definition(dst
);
4414 def
= Definition(dst
);
4417 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4420 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4422 Builder
bld(ctx
->program
, ctx
->block
);
4423 unsigned num_components
= instr
->num_components
;
4425 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4426 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4427 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4429 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4430 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
, false);
4433 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4435 Builder
bld(ctx
->program
, ctx
->block
);
4436 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4437 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4438 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4441 if (ctx
->options
->chip_class
< GFX8
)
4442 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4444 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4446 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4447 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4449 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4450 ctx
->options
->chip_class
>= GFX8
;
4452 offset
= bld
.as_uniform(offset
);
4453 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4457 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4458 if (count
== 3 && smem
) {
4459 writemask
|= 1u << (start
+ 2);
4462 int num_bytes
= count
* elem_size_bytes
;
4464 if (num_bytes
> 16) {
4465 assert(elem_size_bytes
== 8);
4466 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4471 // TODO: check alignment of sub-dword stores
4472 // TODO: split 3 bytes. there is no store instruction for that
4475 if (count
!= instr
->num_components
) {
4476 emit_split_vector(ctx
, data
, instr
->num_components
);
4477 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4478 for (int i
= 0; i
< count
; i
++) {
4479 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4480 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4482 write_data
= bld
.tmp(!smem
? RegType::vgpr
: smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4483 vec
->definitions
[0] = Definition(write_data
);
4484 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4485 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4486 assert(num_bytes
% 4 == 0);
4487 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4488 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4489 assert(num_bytes
% 4 == 0);
4490 write_data
= bld
.as_uniform(data
);
4495 aco_opcode vmem_op
, smem_op
;
4496 switch (num_bytes
) {
4498 vmem_op
= aco_opcode::buffer_store_dword
;
4499 smem_op
= aco_opcode::s_buffer_store_dword
;
4502 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4503 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4506 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4507 smem_op
= aco_opcode::last_opcode
;
4511 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4512 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4515 unreachable("Store SSBO not implemented for this size.");
4517 if (ctx
->stage
== fragment_fs
)
4518 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4521 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4522 store
->operands
[0] = Operand(rsrc
);
4524 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4525 offset
, Operand(start
* elem_size_bytes
));
4526 store
->operands
[1] = Operand(off
);
4528 store
->operands
[1] = Operand(offset
);
4530 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4531 store
->operands
[1].setFixed(m0
);
4532 store
->operands
[2] = Operand(write_data
);
4533 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4535 store
->disable_wqm
= true;
4536 store
->barrier
= barrier_buffer
;
4537 ctx
->block
->instructions
.emplace_back(std::move(store
));
4538 ctx
->program
->wb_smem_l1_on_end
= true;
4539 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4540 ctx
->block
->kind
|= block_kind_needs_lowering
;
4541 ctx
->program
->needs_exact
= true;
4544 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4545 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4546 store
->operands
[1] = Operand(rsrc
);
4547 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4548 store
->operands
[3] = Operand(write_data
);
4549 store
->offset
= start
* elem_size_bytes
;
4550 store
->offen
= (offset
.type() == RegType::vgpr
);
4551 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4553 store
->disable_wqm
= true;
4554 store
->barrier
= barrier_buffer
;
4555 ctx
->program
->needs_exact
= true;
4556 ctx
->block
->instructions
.emplace_back(std::move(store
));
4561 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4563 /* return the previous value if dest is ever used */
4564 bool return_previous
= false;
4565 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4566 return_previous
= true;
4569 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4570 return_previous
= true;
4574 Builder
bld(ctx
->program
, ctx
->block
);
4575 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4577 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4578 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4579 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4582 if (ctx
->options
->chip_class
< GFX8
)
4583 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4585 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4587 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4588 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4590 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4592 aco_opcode op32
, op64
;
4593 switch (instr
->intrinsic
) {
4594 case nir_intrinsic_ssbo_atomic_add
:
4595 op32
= aco_opcode::buffer_atomic_add
;
4596 op64
= aco_opcode::buffer_atomic_add_x2
;
4598 case nir_intrinsic_ssbo_atomic_imin
:
4599 op32
= aco_opcode::buffer_atomic_smin
;
4600 op64
= aco_opcode::buffer_atomic_smin_x2
;
4602 case nir_intrinsic_ssbo_atomic_umin
:
4603 op32
= aco_opcode::buffer_atomic_umin
;
4604 op64
= aco_opcode::buffer_atomic_umin_x2
;
4606 case nir_intrinsic_ssbo_atomic_imax
:
4607 op32
= aco_opcode::buffer_atomic_smax
;
4608 op64
= aco_opcode::buffer_atomic_smax_x2
;
4610 case nir_intrinsic_ssbo_atomic_umax
:
4611 op32
= aco_opcode::buffer_atomic_umax
;
4612 op64
= aco_opcode::buffer_atomic_umax_x2
;
4614 case nir_intrinsic_ssbo_atomic_and
:
4615 op32
= aco_opcode::buffer_atomic_and
;
4616 op64
= aco_opcode::buffer_atomic_and_x2
;
4618 case nir_intrinsic_ssbo_atomic_or
:
4619 op32
= aco_opcode::buffer_atomic_or
;
4620 op64
= aco_opcode::buffer_atomic_or_x2
;
4622 case nir_intrinsic_ssbo_atomic_xor
:
4623 op32
= aco_opcode::buffer_atomic_xor
;
4624 op64
= aco_opcode::buffer_atomic_xor_x2
;
4626 case nir_intrinsic_ssbo_atomic_exchange
:
4627 op32
= aco_opcode::buffer_atomic_swap
;
4628 op64
= aco_opcode::buffer_atomic_swap_x2
;
4630 case nir_intrinsic_ssbo_atomic_comp_swap
:
4631 op32
= aco_opcode::buffer_atomic_cmpswap
;
4632 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4635 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4637 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4638 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4639 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4640 mubuf
->operands
[1] = Operand(rsrc
);
4641 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4642 mubuf
->operands
[3] = Operand(data
);
4643 if (return_previous
)
4644 mubuf
->definitions
[0] = Definition(dst
);
4646 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4647 mubuf
->glc
= return_previous
;
4648 mubuf
->dlc
= false; /* Not needed for atomics */
4649 mubuf
->disable_wqm
= true;
4650 mubuf
->barrier
= barrier_buffer
;
4651 ctx
->program
->needs_exact
= true;
4652 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4655 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4657 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4658 Builder
bld(ctx
->program
, ctx
->block
);
4659 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4660 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4663 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4665 Builder
bld(ctx
->program
, ctx
->block
);
4666 unsigned num_components
= instr
->num_components
;
4667 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4669 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4670 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4672 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4673 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4675 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4676 bool global
= ctx
->options
->chip_class
>= GFX9
;
4678 switch (num_bytes
) {
4680 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4683 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4686 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4689 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4692 unreachable("load_global not implemented for this size.");
4694 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4695 flat
->operands
[0] = Operand(addr
);
4696 flat
->operands
[1] = Operand(s1
);
4699 flat
->barrier
= barrier_buffer
;
4701 if (dst
.type() == RegType::sgpr
) {
4702 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4703 flat
->definitions
[0] = Definition(vec
);
4704 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4705 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4707 flat
->definitions
[0] = Definition(dst
);
4708 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4710 emit_split_vector(ctx
, dst
, num_components
);
4712 switch (num_bytes
) {
4714 op
= aco_opcode::s_load_dword
;
4717 op
= aco_opcode::s_load_dwordx2
;
4721 op
= aco_opcode::s_load_dwordx4
;
4724 unreachable("load_global not implemented for this size.");
4726 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4727 load
->operands
[0] = Operand(addr
);
4728 load
->operands
[1] = Operand(0u);
4729 load
->definitions
[0] = Definition(dst
);
4732 load
->barrier
= barrier_buffer
;
4733 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4735 if (dst
.size() == 3) {
4737 Temp vec
= bld
.tmp(s4
);
4738 load
->definitions
[0] = Definition(vec
);
4739 ctx
->block
->instructions
.emplace_back(std::move(load
));
4740 emit_split_vector(ctx
, vec
, 4);
4742 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4743 emit_extract_vector(ctx
, vec
, 0, s1
),
4744 emit_extract_vector(ctx
, vec
, 1, s1
),
4745 emit_extract_vector(ctx
, vec
, 2, s1
));
4747 ctx
->block
->instructions
.emplace_back(std::move(load
));
4752 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4754 Builder
bld(ctx
->program
, ctx
->block
);
4755 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4757 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4758 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4760 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4763 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4764 unsigned num_bytes
= count
* elem_size_bytes
;
4766 Temp write_data
= data
;
4767 if (count
!= instr
->num_components
) {
4768 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4769 for (int i
= 0; i
< count
; i
++)
4770 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4771 write_data
= bld
.tmp(RegType::vgpr
, count
);
4772 vec
->definitions
[0] = Definition(write_data
);
4773 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4776 unsigned offset
= start
* elem_size_bytes
;
4777 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4778 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4779 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4780 Temp carry
= bld
.tmp(bld
.lm
);
4781 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4783 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4784 Operand(offset
), addr0
);
4785 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(bld
.lm
),
4787 carry
).def(1).setHint(vcc
);
4789 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4794 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4795 bool global
= ctx
->options
->chip_class
>= GFX9
;
4797 switch (num_bytes
) {
4799 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4802 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4805 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4808 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4811 unreachable("store_global not implemented for this size.");
4813 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4814 flat
->operands
[0] = Operand(addr
);
4815 flat
->operands
[1] = Operand(s1
);
4816 flat
->operands
[2] = Operand(data
);
4819 flat
->offset
= offset
;
4820 flat
->disable_wqm
= true;
4821 flat
->barrier
= barrier_buffer
;
4822 ctx
->program
->needs_exact
= true;
4823 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4827 void visit_global_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4829 /* return the previous value if dest is ever used */
4830 bool return_previous
= false;
4831 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4832 return_previous
= true;
4835 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4836 return_previous
= true;
4840 Builder
bld(ctx
->program
, ctx
->block
);
4841 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4842 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4844 if (instr
->intrinsic
== nir_intrinsic_global_atomic_comp_swap
)
4845 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4846 get_ssa_temp(ctx
, instr
->src
[2].ssa
), data
);
4848 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4850 bool global
= ctx
->options
->chip_class
>= GFX9
;
4851 aco_opcode op32
, op64
;
4852 switch (instr
->intrinsic
) {
4853 case nir_intrinsic_global_atomic_add
:
4854 op32
= global
? aco_opcode::global_atomic_add
: aco_opcode::flat_atomic_add
;
4855 op64
= global
? aco_opcode::global_atomic_add_x2
: aco_opcode::flat_atomic_add_x2
;
4857 case nir_intrinsic_global_atomic_imin
:
4858 op32
= global
? aco_opcode::global_atomic_smin
: aco_opcode::flat_atomic_smin
;
4859 op64
= global
? aco_opcode::global_atomic_smin_x2
: aco_opcode::flat_atomic_smin_x2
;
4861 case nir_intrinsic_global_atomic_umin
:
4862 op32
= global
? aco_opcode::global_atomic_umin
: aco_opcode::flat_atomic_umin
;
4863 op64
= global
? aco_opcode::global_atomic_umin_x2
: aco_opcode::flat_atomic_umin_x2
;
4865 case nir_intrinsic_global_atomic_imax
:
4866 op32
= global
? aco_opcode::global_atomic_smax
: aco_opcode::flat_atomic_smax
;
4867 op64
= global
? aco_opcode::global_atomic_smax_x2
: aco_opcode::flat_atomic_smax_x2
;
4869 case nir_intrinsic_global_atomic_umax
:
4870 op32
= global
? aco_opcode::global_atomic_umax
: aco_opcode::flat_atomic_umax
;
4871 op64
= global
? aco_opcode::global_atomic_umax_x2
: aco_opcode::flat_atomic_umax_x2
;
4873 case nir_intrinsic_global_atomic_and
:
4874 op32
= global
? aco_opcode::global_atomic_and
: aco_opcode::flat_atomic_and
;
4875 op64
= global
? aco_opcode::global_atomic_and_x2
: aco_opcode::flat_atomic_and_x2
;
4877 case nir_intrinsic_global_atomic_or
:
4878 op32
= global
? aco_opcode::global_atomic_or
: aco_opcode::flat_atomic_or
;
4879 op64
= global
? aco_opcode::global_atomic_or_x2
: aco_opcode::flat_atomic_or_x2
;
4881 case nir_intrinsic_global_atomic_xor
:
4882 op32
= global
? aco_opcode::global_atomic_xor
: aco_opcode::flat_atomic_xor
;
4883 op64
= global
? aco_opcode::global_atomic_xor_x2
: aco_opcode::flat_atomic_xor_x2
;
4885 case nir_intrinsic_global_atomic_exchange
:
4886 op32
= global
? aco_opcode::global_atomic_swap
: aco_opcode::flat_atomic_swap
;
4887 op64
= global
? aco_opcode::global_atomic_swap_x2
: aco_opcode::flat_atomic_swap_x2
;
4889 case nir_intrinsic_global_atomic_comp_swap
:
4890 op32
= global
? aco_opcode::global_atomic_cmpswap
: aco_opcode::flat_atomic_cmpswap
;
4891 op64
= global
? aco_opcode::global_atomic_cmpswap_x2
: aco_opcode::flat_atomic_cmpswap_x2
;
4894 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
4896 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4897 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, return_previous
? 1 : 0)};
4898 flat
->operands
[0] = Operand(addr
);
4899 flat
->operands
[1] = Operand(s1
);
4900 flat
->operands
[2] = Operand(data
);
4901 if (return_previous
)
4902 flat
->definitions
[0] = Definition(dst
);
4903 flat
->glc
= return_previous
;
4904 flat
->dlc
= false; /* Not needed for atomics */
4906 flat
->disable_wqm
= true;
4907 flat
->barrier
= barrier_buffer
;
4908 ctx
->program
->needs_exact
= true;
4909 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4912 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4913 Builder
bld(ctx
->program
, ctx
->block
);
4914 switch(instr
->intrinsic
) {
4915 case nir_intrinsic_group_memory_barrier
:
4916 case nir_intrinsic_memory_barrier
:
4917 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4919 case nir_intrinsic_memory_barrier_atomic_counter
:
4920 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4922 case nir_intrinsic_memory_barrier_buffer
:
4923 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4925 case nir_intrinsic_memory_barrier_image
:
4926 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4928 case nir_intrinsic_memory_barrier_shared
:
4929 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4932 unreachable("Unimplemented memory barrier intrinsic");
4937 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4939 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4940 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4941 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4942 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4943 Builder
bld(ctx
->program
, ctx
->block
);
4945 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4946 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4947 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
4950 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4952 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4953 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4954 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4955 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4956 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4958 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4959 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
4962 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4964 unsigned offset
= nir_intrinsic_base(instr
);
4965 Operand m
= load_lds_size_m0(ctx
);
4966 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4967 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4969 unsigned num_operands
= 3;
4970 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4971 switch(instr
->intrinsic
) {
4972 case nir_intrinsic_shared_atomic_add
:
4973 op32
= aco_opcode::ds_add_u32
;
4974 op64
= aco_opcode::ds_add_u64
;
4975 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4976 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4978 case nir_intrinsic_shared_atomic_imin
:
4979 op32
= aco_opcode::ds_min_i32
;
4980 op64
= aco_opcode::ds_min_i64
;
4981 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4982 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4984 case nir_intrinsic_shared_atomic_umin
:
4985 op32
= aco_opcode::ds_min_u32
;
4986 op64
= aco_opcode::ds_min_u64
;
4987 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4988 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4990 case nir_intrinsic_shared_atomic_imax
:
4991 op32
= aco_opcode::ds_max_i32
;
4992 op64
= aco_opcode::ds_max_i64
;
4993 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4994 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4996 case nir_intrinsic_shared_atomic_umax
:
4997 op32
= aco_opcode::ds_max_u32
;
4998 op64
= aco_opcode::ds_max_u64
;
4999 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
5000 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
5002 case nir_intrinsic_shared_atomic_and
:
5003 op32
= aco_opcode::ds_and_b32
;
5004 op64
= aco_opcode::ds_and_b64
;
5005 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
5006 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
5008 case nir_intrinsic_shared_atomic_or
:
5009 op32
= aco_opcode::ds_or_b32
;
5010 op64
= aco_opcode::ds_or_b64
;
5011 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
5012 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
5014 case nir_intrinsic_shared_atomic_xor
:
5015 op32
= aco_opcode::ds_xor_b32
;
5016 op64
= aco_opcode::ds_xor_b64
;
5017 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
5018 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
5020 case nir_intrinsic_shared_atomic_exchange
:
5021 op32
= aco_opcode::ds_write_b32
;
5022 op64
= aco_opcode::ds_write_b64
;
5023 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
5024 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
5026 case nir_intrinsic_shared_atomic_comp_swap
:
5027 op32
= aco_opcode::ds_cmpst_b32
;
5028 op64
= aco_opcode::ds_cmpst_b64
;
5029 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
5030 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
5034 unreachable("Unhandled shared atomic intrinsic");
5037 /* return the previous value if dest is ever used */
5038 bool return_previous
= false;
5039 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5040 return_previous
= true;
5043 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5044 return_previous
= true;
5049 if (data
.size() == 1) {
5050 assert(instr
->dest
.ssa
.bit_size
== 32);
5051 op
= return_previous
? op32_rtn
: op32
;
5053 assert(instr
->dest
.ssa
.bit_size
== 64);
5054 op
= return_previous
? op64_rtn
: op64
;
5057 if (offset
> 65535) {
5058 Builder
bld(ctx
->program
, ctx
->block
);
5059 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
5063 aco_ptr
<DS_instruction
> ds
;
5064 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
5065 ds
->operands
[0] = Operand(address
);
5066 ds
->operands
[1] = Operand(data
);
5067 if (num_operands
== 4)
5068 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5069 ds
->operands
[num_operands
- 1] = m
;
5070 ds
->offset0
= offset
;
5071 if (return_previous
)
5072 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5073 ctx
->block
->instructions
.emplace_back(std::move(ds
));
5076 Temp
get_scratch_resource(isel_context
*ctx
)
5078 Builder
bld(ctx
->program
, ctx
->block
);
5079 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
5080 if (ctx
->stage
!= compute_cs
)
5081 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
5083 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
5084 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
5086 if (ctx
->program
->chip_class
>= GFX10
) {
5087 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5088 S_008F0C_OOB_SELECT(3) |
5089 S_008F0C_RESOURCE_LEVEL(1);
5090 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
5091 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5092 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5095 /* older generations need element size = 16 bytes. element size removed in GFX9 */
5096 if (ctx
->program
->chip_class
<= GFX8
)
5097 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
5099 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
5102 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5103 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
5104 Builder
bld(ctx
->program
, ctx
->block
);
5105 Temp rsrc
= get_scratch_resource(ctx
);
5106 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5107 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5110 switch (dst
.size()) {
5112 op
= aco_opcode::buffer_load_dword
;
5115 op
= aco_opcode::buffer_load_dwordx2
;
5118 op
= aco_opcode::buffer_load_dwordx3
;
5121 op
= aco_opcode::buffer_load_dwordx4
;
5125 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
5126 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
5127 bld
.def(v4
), offset
, rsrc
,
5128 ctx
->program
->scratch_offset
, 0, true);
5129 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
5130 aco_opcode::buffer_load_dwordx4
,
5131 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
5132 offset
, rsrc
, ctx
->program
->scratch_offset
, 16, true);
5133 emit_split_vector(ctx
, lower
, 2);
5134 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
5135 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
5136 if (dst
.size() == 8) {
5137 emit_split_vector(ctx
, upper
, 2);
5138 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
5139 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
5144 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
5145 Format::PSEUDO
, dst
.size() / 2, 1)};
5146 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
5147 vec
->operands
[i
] = Operand(elems
[i
]);
5148 vec
->definitions
[0] = Definition(dst
);
5149 bld
.insert(std::move(vec
));
5150 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5154 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5157 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->program
->scratch_offset
, 0, true);
5158 emit_split_vector(ctx
, dst
, instr
->num_components
);
5161 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5162 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
5163 Builder
bld(ctx
->program
, ctx
->block
);
5164 Temp rsrc
= get_scratch_resource(ctx
);
5165 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5166 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5168 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5169 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5173 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5174 int num_bytes
= count
* elem_size_bytes
;
5176 if (num_bytes
> 16) {
5177 assert(elem_size_bytes
== 8);
5178 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5183 // TODO: check alignment of sub-dword stores
5184 // TODO: split 3 bytes. there is no store instruction for that
5187 if (count
!= instr
->num_components
) {
5188 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5189 for (int i
= 0; i
< count
; i
++) {
5190 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
5191 vec
->operands
[i
] = Operand(elem
);
5193 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
5194 vec
->definitions
[0] = Definition(write_data
);
5195 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5201 switch (num_bytes
) {
5203 op
= aco_opcode::buffer_store_dword
;
5206 op
= aco_opcode::buffer_store_dwordx2
;
5209 op
= aco_opcode::buffer_store_dwordx3
;
5212 op
= aco_opcode::buffer_store_dwordx4
;
5215 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5218 bld
.mubuf(op
, offset
, rsrc
, ctx
->program
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
5222 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5223 uint8_t log2_ps_iter_samples
;
5224 if (ctx
->program
->info
->ps
.force_persample
) {
5225 log2_ps_iter_samples
=
5226 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
5228 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
5231 /* The bit pattern matches that used by fixed function fragment
5233 static const unsigned ps_iter_masks
[] = {
5234 0xffff, /* not used */
5240 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
5242 Builder
bld(ctx
->program
, ctx
->block
);
5244 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
5245 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
5246 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
5247 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
5248 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5249 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, get_arg(ctx
, ctx
->args
->ac
.sample_coverage
));
5252 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
5254 Builder
bld(ctx
->program
, ctx
->block
);
5256 if (cluster_size
== 1) {
5258 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5259 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5260 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
5261 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
5262 bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
));
5263 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5264 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5265 return bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
5266 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)));
5267 } else if (op
== nir_op_iand
&& cluster_size
== ctx
->program
->wave_size
) {
5268 //subgroupAnd(val) -> (exec & ~val) == 0
5269 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
5270 return bld
.sop2(Builder::s_cselect
, bld
.def(bld
.lm
), Operand(0u), Operand(-1u), bld
.scc(tmp
));
5271 } else if (op
== nir_op_ior
&& cluster_size
== ctx
->program
->wave_size
) {
5272 //subgroupOr(val) -> (val & exec) != 0
5273 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)).def(1).getTemp();
5274 return bool_to_vector_condition(ctx
, tmp
);
5275 } else if (op
== nir_op_ixor
&& cluster_size
== ctx
->program
->wave_size
) {
5276 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5277 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5278 tmp
= bld
.sop1(Builder::s_bcnt1_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
);
5279 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5280 return bool_to_vector_condition(ctx
, tmp
);
5282 //subgroupClustered{And,Or,Xor}(val, n) ->
5283 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
5284 //cluster_offset = ~(n - 1) & lane_id
5285 //cluster_mask = ((1 << n) - 1)
5286 //subgroupClusteredAnd():
5287 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5288 //subgroupClusteredOr():
5289 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5290 //subgroupClusteredXor():
5291 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5292 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
5293 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5296 if (op
== nir_op_iand
)
5297 tmp
= bld
.sop2(Builder::s_orn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5299 tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5301 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5303 if (ctx
->program
->chip_class
<= GFX7
)
5304 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), tmp
, cluster_offset
);
5305 else if (ctx
->program
->wave_size
== 64)
5306 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5308 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), cluster_offset
, tmp
);
5309 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5310 if (cluster_mask
!= 0xffffffff)
5311 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5313 Definition cmp_def
= Definition();
5314 if (op
== nir_op_iand
) {
5315 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(cluster_mask
), tmp
).def(0);
5316 } else if (op
== nir_op_ior
) {
5317 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
5318 } else if (op
== nir_op_ixor
) {
5319 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5320 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5321 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
5323 cmp_def
.setHint(vcc
);
5324 return cmp_def
.getTemp();
5328 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5330 Builder
bld(ctx
->program
, ctx
->block
);
5332 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5333 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5334 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5336 if (op
== nir_op_iand
)
5337 tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
5339 tmp
= bld
.sop2(Builder::s_and
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5341 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5342 Temp lo
= lohi
.def(0).getTemp();
5343 Temp hi
= lohi
.def(1).getTemp();
5344 Temp mbcnt
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(lo
), Operand(hi
));
5346 Definition cmp_def
= Definition();
5347 if (op
== nir_op_iand
)
5348 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
5349 else if (op
== nir_op_ior
)
5350 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
5351 else if (op
== nir_op_ixor
)
5352 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u),
5353 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5354 cmp_def
.setHint(vcc
);
5355 return cmp_def
.getTemp();
5358 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5360 Builder
bld(ctx
->program
, ctx
->block
);
5362 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5363 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5364 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5365 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5366 if (op
== nir_op_iand
)
5367 return bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
5368 else if (op
== nir_op_ior
)
5369 return bld
.sop2(Builder::s_or
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
5370 else if (op
== nir_op_ixor
)
5371 return bld
.sop2(Builder::s_xor
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
5377 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5379 Builder
bld(ctx
->program
, ctx
->block
);
5380 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5381 if (src
.regClass().type() == RegType::vgpr
) {
5382 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5383 } else if (src
.regClass() == s1
) {
5384 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5385 } else if (src
.regClass() == s2
) {
5386 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5388 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5389 nir_print_instr(&instr
->instr
, stderr
);
5390 fprintf(stderr
, "\n");
5394 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5396 Builder
bld(ctx
->program
, ctx
->block
);
5397 Temp persp_center
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
5398 Temp p1
= emit_extract_vector(ctx
, persp_center
, 0, v1
);
5399 Temp p2
= emit_extract_vector(ctx
, persp_center
, 1, v1
);
5401 Temp ddx_1
, ddx_2
, ddy_1
, ddy_2
;
5402 uint32_t dpp_ctrl0
= dpp_quad_perm(0, 0, 0, 0);
5403 uint32_t dpp_ctrl1
= dpp_quad_perm(1, 1, 1, 1);
5404 uint32_t dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
5407 if (ctx
->program
->chip_class
>= GFX8
) {
5408 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_ctrl0
);
5409 ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl1
);
5410 ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl2
);
5411 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_ctrl0
);
5412 ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl1
);
5413 ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl2
);
5415 Temp tl_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl0
);
5416 ddx_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl1
);
5417 ddx_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_1
, tl_1
);
5418 ddx_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl2
);
5419 ddx_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_2
, tl_1
);
5420 Temp tl_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl0
);
5421 ddy_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl1
);
5422 ddy_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_1
, tl_2
);
5423 ddy_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl2
);
5424 ddy_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_2
, tl_2
);
5427 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5428 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5429 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5430 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5431 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5432 Temp wqm1
= bld
.tmp(v1
);
5433 emit_wqm(ctx
, tmp1
, wqm1
, true);
5434 Temp wqm2
= bld
.tmp(v1
);
5435 emit_wqm(ctx
, tmp2
, wqm2
, true);
5436 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5440 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5442 Builder
bld(ctx
->program
, ctx
->block
);
5443 switch(instr
->intrinsic
) {
5444 case nir_intrinsic_load_barycentric_sample
:
5445 case nir_intrinsic_load_barycentric_pixel
:
5446 case nir_intrinsic_load_barycentric_centroid
: {
5447 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5448 Temp bary
= Temp(0, s2
);
5450 case INTERP_MODE_SMOOTH
:
5451 case INTERP_MODE_NONE
:
5452 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
5453 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
5454 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
5455 bary
= ctx
->persp_centroid
;
5456 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
5457 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_sample
);
5459 case INTERP_MODE_NOPERSPECTIVE
:
5460 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
5461 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_center
);
5462 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
5463 bary
= ctx
->linear_centroid
;
5464 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
5465 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_sample
);
5470 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5471 Temp p1
= emit_extract_vector(ctx
, bary
, 0, v1
);
5472 Temp p2
= emit_extract_vector(ctx
, bary
, 1, v1
);
5473 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5474 Operand(p1
), Operand(p2
));
5475 emit_split_vector(ctx
, dst
, 2);
5478 case nir_intrinsic_load_barycentric_at_sample
: {
5479 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5480 switch (ctx
->options
->key
.fs
.num_samples
) {
5481 case 2: sample_pos_offset
+= 1 << 3; break;
5482 case 4: sample_pos_offset
+= 3 << 3; break;
5483 case 8: sample_pos_offset
+= 7 << 3; break;
5487 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5488 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5489 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
5490 if (addr
.type() == RegType::sgpr
) {
5493 sample_pos_offset
+= const_addr
->u32
<< 3;
5494 offset
= Operand(sample_pos_offset
);
5495 } else if (ctx
->options
->chip_class
>= GFX9
) {
5496 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5498 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5499 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5501 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, Operand(offset
));
5503 } else if (ctx
->options
->chip_class
>= GFX9
) {
5504 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5505 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
5507 /* addr += private_segment_buffer + sample_pos_offset */
5508 Temp tmp0
= bld
.tmp(s1
);
5509 Temp tmp1
= bld
.tmp(s1
);
5510 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
5511 Definition scc_tmp
= bld
.def(s1
, scc
);
5512 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5513 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
5514 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5515 Temp pck0
= bld
.tmp(v1
);
5516 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5517 tmp1
= as_vgpr(ctx
, tmp1
);
5518 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
);
5519 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5521 /* sample_pos = flat_load_dwordx2 addr */
5522 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5525 /* sample_pos -= 0.5 */
5526 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5527 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5528 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5529 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5530 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5532 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5535 case nir_intrinsic_load_barycentric_at_offset
: {
5536 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5537 RegClass rc
= RegClass(offset
.type(), 1);
5538 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5539 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5540 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5543 case nir_intrinsic_load_front_face
: {
5544 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5545 Operand(0u), get_arg(ctx
, ctx
->args
->ac
.front_face
)).def(0).setHint(vcc
);
5548 case nir_intrinsic_load_view_index
:
5549 case nir_intrinsic_load_layer_id
: {
5550 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5551 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5552 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.view_index
)));
5556 unsigned idx
= nir_intrinsic_base(instr
);
5557 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5558 Operand(2u), bld
.m0(get_arg(ctx
, ctx
->args
->ac
.prim_mask
)), idx
, 0);
5561 case nir_intrinsic_load_frag_coord
: {
5562 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5565 case nir_intrinsic_load_sample_pos
: {
5566 Temp posx
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[0]);
5567 Temp posy
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[1]);
5568 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5569 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5570 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5573 case nir_intrinsic_load_interpolated_input
:
5574 visit_load_interpolated_input(ctx
, instr
);
5576 case nir_intrinsic_store_output
:
5577 visit_store_output(ctx
, instr
);
5579 case nir_intrinsic_load_input
:
5580 visit_load_input(ctx
, instr
);
5582 case nir_intrinsic_load_ubo
:
5583 visit_load_ubo(ctx
, instr
);
5585 case nir_intrinsic_load_push_constant
:
5586 visit_load_push_constant(ctx
, instr
);
5588 case nir_intrinsic_load_constant
:
5589 visit_load_constant(ctx
, instr
);
5591 case nir_intrinsic_vulkan_resource_index
:
5592 visit_load_resource(ctx
, instr
);
5594 case nir_intrinsic_discard
:
5595 visit_discard(ctx
, instr
);
5597 case nir_intrinsic_discard_if
:
5598 visit_discard_if(ctx
, instr
);
5600 case nir_intrinsic_load_shared
:
5601 visit_load_shared(ctx
, instr
);
5603 case nir_intrinsic_store_shared
:
5604 visit_store_shared(ctx
, instr
);
5606 case nir_intrinsic_shared_atomic_add
:
5607 case nir_intrinsic_shared_atomic_imin
:
5608 case nir_intrinsic_shared_atomic_umin
:
5609 case nir_intrinsic_shared_atomic_imax
:
5610 case nir_intrinsic_shared_atomic_umax
:
5611 case nir_intrinsic_shared_atomic_and
:
5612 case nir_intrinsic_shared_atomic_or
:
5613 case nir_intrinsic_shared_atomic_xor
:
5614 case nir_intrinsic_shared_atomic_exchange
:
5615 case nir_intrinsic_shared_atomic_comp_swap
:
5616 visit_shared_atomic(ctx
, instr
);
5618 case nir_intrinsic_image_deref_load
:
5619 visit_image_load(ctx
, instr
);
5621 case nir_intrinsic_image_deref_store
:
5622 visit_image_store(ctx
, instr
);
5624 case nir_intrinsic_image_deref_atomic_add
:
5625 case nir_intrinsic_image_deref_atomic_umin
:
5626 case nir_intrinsic_image_deref_atomic_imin
:
5627 case nir_intrinsic_image_deref_atomic_umax
:
5628 case nir_intrinsic_image_deref_atomic_imax
:
5629 case nir_intrinsic_image_deref_atomic_and
:
5630 case nir_intrinsic_image_deref_atomic_or
:
5631 case nir_intrinsic_image_deref_atomic_xor
:
5632 case nir_intrinsic_image_deref_atomic_exchange
:
5633 case nir_intrinsic_image_deref_atomic_comp_swap
:
5634 visit_image_atomic(ctx
, instr
);
5636 case nir_intrinsic_image_deref_size
:
5637 visit_image_size(ctx
, instr
);
5639 case nir_intrinsic_load_ssbo
:
5640 visit_load_ssbo(ctx
, instr
);
5642 case nir_intrinsic_store_ssbo
:
5643 visit_store_ssbo(ctx
, instr
);
5645 case nir_intrinsic_load_global
:
5646 visit_load_global(ctx
, instr
);
5648 case nir_intrinsic_store_global
:
5649 visit_store_global(ctx
, instr
);
5651 case nir_intrinsic_global_atomic_add
:
5652 case nir_intrinsic_global_atomic_imin
:
5653 case nir_intrinsic_global_atomic_umin
:
5654 case nir_intrinsic_global_atomic_imax
:
5655 case nir_intrinsic_global_atomic_umax
:
5656 case nir_intrinsic_global_atomic_and
:
5657 case nir_intrinsic_global_atomic_or
:
5658 case nir_intrinsic_global_atomic_xor
:
5659 case nir_intrinsic_global_atomic_exchange
:
5660 case nir_intrinsic_global_atomic_comp_swap
:
5661 visit_global_atomic(ctx
, instr
);
5663 case nir_intrinsic_ssbo_atomic_add
:
5664 case nir_intrinsic_ssbo_atomic_imin
:
5665 case nir_intrinsic_ssbo_atomic_umin
:
5666 case nir_intrinsic_ssbo_atomic_imax
:
5667 case nir_intrinsic_ssbo_atomic_umax
:
5668 case nir_intrinsic_ssbo_atomic_and
:
5669 case nir_intrinsic_ssbo_atomic_or
:
5670 case nir_intrinsic_ssbo_atomic_xor
:
5671 case nir_intrinsic_ssbo_atomic_exchange
:
5672 case nir_intrinsic_ssbo_atomic_comp_swap
:
5673 visit_atomic_ssbo(ctx
, instr
);
5675 case nir_intrinsic_load_scratch
:
5676 visit_load_scratch(ctx
, instr
);
5678 case nir_intrinsic_store_scratch
:
5679 visit_store_scratch(ctx
, instr
);
5681 case nir_intrinsic_get_buffer_size
:
5682 visit_get_buffer_size(ctx
, instr
);
5684 case nir_intrinsic_barrier
: {
5685 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5686 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5687 if (workgroup_size
> ctx
->program
->wave_size
)
5688 bld
.sopp(aco_opcode::s_barrier
);
5691 case nir_intrinsic_group_memory_barrier
:
5692 case nir_intrinsic_memory_barrier
:
5693 case nir_intrinsic_memory_barrier_atomic_counter
:
5694 case nir_intrinsic_memory_barrier_buffer
:
5695 case nir_intrinsic_memory_barrier_image
:
5696 case nir_intrinsic_memory_barrier_shared
:
5697 emit_memory_barrier(ctx
, instr
);
5699 case nir_intrinsic_load_num_work_groups
: {
5700 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5701 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.num_work_groups
)));
5702 emit_split_vector(ctx
, dst
, 3);
5705 case nir_intrinsic_load_local_invocation_id
: {
5706 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5707 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.local_invocation_ids
)));
5708 emit_split_vector(ctx
, dst
, 3);
5711 case nir_intrinsic_load_work_group_id
: {
5712 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5713 struct ac_arg
*args
= ctx
->args
->ac
.workgroup_ids
;
5714 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5715 args
[0].used
? Operand(get_arg(ctx
, args
[0])) : Operand(0u),
5716 args
[1].used
? Operand(get_arg(ctx
, args
[1])) : Operand(0u),
5717 args
[2].used
? Operand(get_arg(ctx
, args
[2])) : Operand(0u));
5718 emit_split_vector(ctx
, dst
, 3);
5721 case nir_intrinsic_load_local_invocation_index
: {
5722 Temp id
= emit_mbcnt(ctx
, bld
.def(v1
));
5724 /* The tg_size bits [6:11] contain the subgroup id,
5725 * we need this multiplied by the wave size, and then OR the thread id to it.
5727 if (ctx
->program
->wave_size
== 64) {
5728 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
5729 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
5730 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
5731 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5733 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
5734 Temp tg_num
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
5735 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
5736 bld
.vop3(aco_opcode::v_lshl_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, Operand(0x5u
), id
);
5740 case nir_intrinsic_load_subgroup_id
: {
5741 if (ctx
->stage
== compute_cs
) {
5742 bld
.sop2(aco_opcode::s_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
),
5743 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
5745 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5749 case nir_intrinsic_load_subgroup_invocation
: {
5750 emit_mbcnt(ctx
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
5753 case nir_intrinsic_load_num_subgroups
: {
5754 if (ctx
->stage
== compute_cs
)
5755 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
),
5756 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
5758 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5761 case nir_intrinsic_ballot
: {
5762 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5763 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5764 Definition tmp
= bld
.def(dst
.regClass());
5765 Definition lanemask_tmp
= dst
.size() == bld
.lm
.size() ? tmp
: bld
.def(src
.regClass());
5766 if (instr
->src
[0].ssa
->bit_size
== 1) {
5767 assert(src
.regClass() == bld
.lm
);
5768 bld
.sop2(Builder::s_and
, lanemask_tmp
, bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
5769 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5770 bld
.vopc(aco_opcode::v_cmp_lg_u32
, lanemask_tmp
, Operand(0u), src
);
5771 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5772 bld
.vopc(aco_opcode::v_cmp_lg_u64
, lanemask_tmp
, Operand(0u), src
);
5774 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5775 nir_print_instr(&instr
->instr
, stderr
);
5776 fprintf(stderr
, "\n");
5778 if (dst
.size() != bld
.lm
.size()) {
5779 /* Wave32 with ballot size set to 64 */
5780 bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
), lanemask_tmp
.getTemp(), Operand(0u));
5782 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5785 case nir_intrinsic_shuffle
:
5786 case nir_intrinsic_read_invocation
: {
5787 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5788 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
]) {
5789 emit_uniform_subgroup(ctx
, instr
, src
);
5791 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5792 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !ctx
->divergent_vals
[instr
->src
[1].ssa
->index
])
5793 tid
= bld
.as_uniform(tid
);
5794 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5795 if (src
.regClass() == v1
) {
5796 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
5797 } else if (src
.regClass() == v2
) {
5798 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5799 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5800 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
5801 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
5802 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5803 emit_split_vector(ctx
, dst
, 2);
5804 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
5805 assert(src
.regClass() == bld
.lm
);
5806 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
, tid
);
5807 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
5808 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
5809 assert(src
.regClass() == bld
.lm
);
5811 if (ctx
->program
->chip_class
<= GFX7
)
5812 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), src
, tid
);
5813 else if (ctx
->program
->wave_size
== 64)
5814 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5816 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), tid
, src
);
5817 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5818 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5819 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
), dst
);
5821 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5822 nir_print_instr(&instr
->instr
, stderr
);
5823 fprintf(stderr
, "\n");
5828 case nir_intrinsic_load_sample_id
: {
5829 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5830 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
5833 case nir_intrinsic_load_sample_mask_in
: {
5834 visit_load_sample_mask_in(ctx
, instr
);
5837 case nir_intrinsic_read_first_invocation
: {
5838 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5839 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5840 if (src
.regClass() == v1
) {
5842 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
5844 } else if (src
.regClass() == v2
) {
5845 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5846 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5847 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5848 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
5849 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5850 emit_split_vector(ctx
, dst
, 2);
5851 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5852 assert(src
.regClass() == bld
.lm
);
5853 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
,
5854 bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)));
5855 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
5856 } else if (src
.regClass() == s1
) {
5857 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5858 } else if (src
.regClass() == s2
) {
5859 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5861 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5862 nir_print_instr(&instr
->instr
, stderr
);
5863 fprintf(stderr
, "\n");
5867 case nir_intrinsic_vote_all
: {
5868 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5869 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5870 assert(src
.regClass() == bld
.lm
);
5871 assert(dst
.regClass() == bld
.lm
);
5873 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
5874 Temp val
= bld
.sop2(Builder::s_cselect
, bld
.def(bld
.lm
), Operand(0u), Operand(-1u), bld
.scc(tmp
));
5875 emit_wqm(ctx
, val
, dst
);
5878 case nir_intrinsic_vote_any
: {
5879 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5880 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5881 assert(src
.regClass() == bld
.lm
);
5882 assert(dst
.regClass() == bld
.lm
);
5884 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
5885 Temp val
= bld
.sop2(Builder::s_cselect
, bld
.def(bld
.lm
), Operand(-1u), Operand(0u), bld
.scc(tmp
));
5886 emit_wqm(ctx
, val
, dst
);
5889 case nir_intrinsic_reduce
:
5890 case nir_intrinsic_inclusive_scan
:
5891 case nir_intrinsic_exclusive_scan
: {
5892 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5893 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5894 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5895 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5896 nir_intrinsic_cluster_size(instr
) : 0;
5897 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: ctx
->program
->wave_size
, ctx
->program
->wave_size
));
5899 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5900 emit_uniform_subgroup(ctx
, instr
, src
);
5901 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5902 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5904 else if (op
== nir_op_iadd
)
5906 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5908 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5910 switch (instr
->intrinsic
) {
5911 case nir_intrinsic_reduce
:
5912 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5914 case nir_intrinsic_exclusive_scan
:
5915 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5917 case nir_intrinsic_inclusive_scan
:
5918 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5923 } else if (cluster_size
== 1) {
5924 bld
.copy(Definition(dst
), src
);
5926 src
= as_vgpr(ctx
, src
);
5930 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5945 unreachable("unknown reduction op");
5950 switch (instr
->intrinsic
) {
5951 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5952 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5953 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5955 unreachable("unknown reduce intrinsic");
5958 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5959 reduce
->operands
[0] = Operand(src
);
5960 // filled in by aco_reduce_assign.cpp, used internally as part of the
5962 assert(dst
.size() == 1 || dst
.size() == 2);
5963 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5964 reduce
->operands
[2] = Operand(v1
.as_linear());
5966 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5967 reduce
->definitions
[0] = Definition(tmp_dst
);
5968 reduce
->definitions
[1] = bld
.def(ctx
->program
->lane_mask
); // used internally
5969 reduce
->definitions
[2] = Definition();
5970 reduce
->definitions
[3] = Definition(scc
, s1
);
5971 reduce
->definitions
[4] = Definition();
5972 reduce
->reduce_op
= reduce_op
;
5973 reduce
->cluster_size
= cluster_size
;
5974 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5976 emit_wqm(ctx
, tmp_dst
, dst
);
5980 case nir_intrinsic_quad_broadcast
: {
5981 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5982 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5983 emit_uniform_subgroup(ctx
, instr
, src
);
5985 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5986 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5987 uint32_t dpp_ctrl
= dpp_quad_perm(lane
, lane
, lane
, lane
);
5989 if (instr
->dest
.ssa
.bit_size
== 1) {
5990 assert(src
.regClass() == bld
.lm
);
5991 assert(dst
.regClass() == bld
.lm
);
5992 uint32_t half_mask
= 0x11111111u
<< lane
;
5993 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5994 Temp tmp
= bld
.tmp(bld
.lm
);
5995 bld
.sop1(Builder::s_wqm
, Definition(tmp
),
5996 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), mask_tmp
,
5997 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
))));
5998 emit_wqm(ctx
, tmp
, dst
);
5999 } else if (instr
->dest
.ssa
.bit_size
== 32) {
6000 if (ctx
->program
->chip_class
>= GFX8
)
6001 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), dst
);
6003 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), dst
);
6004 } else if (instr
->dest
.ssa
.bit_size
== 64) {
6005 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6006 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6007 if (ctx
->program
->chip_class
>= GFX8
) {
6008 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
6009 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
6011 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, (1 << 15) | dpp_ctrl
));
6012 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, (1 << 15) | dpp_ctrl
));
6014 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6015 emit_split_vector(ctx
, dst
, 2);
6017 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6018 nir_print_instr(&instr
->instr
, stderr
);
6019 fprintf(stderr
, "\n");
6024 case nir_intrinsic_quad_swap_horizontal
:
6025 case nir_intrinsic_quad_swap_vertical
:
6026 case nir_intrinsic_quad_swap_diagonal
:
6027 case nir_intrinsic_quad_swizzle_amd
: {
6028 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6029 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
6030 emit_uniform_subgroup(ctx
, instr
, src
);
6033 uint16_t dpp_ctrl
= 0;
6034 switch (instr
->intrinsic
) {
6035 case nir_intrinsic_quad_swap_horizontal
:
6036 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
6038 case nir_intrinsic_quad_swap_vertical
:
6039 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
6041 case nir_intrinsic_quad_swap_diagonal
:
6042 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
6044 case nir_intrinsic_quad_swizzle_amd
:
6045 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
6050 if (ctx
->program
->chip_class
< GFX8
)
6051 dpp_ctrl
|= (1 << 15);
6053 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6054 if (instr
->dest
.ssa
.bit_size
== 1) {
6055 assert(src
.regClass() == bld
.lm
);
6056 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
6057 if (ctx
->program
->chip_class
>= GFX8
)
6058 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6060 src
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6061 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), src
);
6062 emit_wqm(ctx
, tmp
, dst
);
6063 } else if (instr
->dest
.ssa
.bit_size
== 32) {
6065 if (ctx
->program
->chip_class
>= GFX8
)
6066 tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6068 tmp
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6069 emit_wqm(ctx
, tmp
, dst
);
6070 } else if (instr
->dest
.ssa
.bit_size
== 64) {
6071 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6072 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6073 if (ctx
->program
->chip_class
>= GFX8
) {
6074 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
6075 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
6077 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
6078 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
6080 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6081 emit_split_vector(ctx
, dst
, 2);
6083 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6084 nir_print_instr(&instr
->instr
, stderr
);
6085 fprintf(stderr
, "\n");
6089 case nir_intrinsic_masked_swizzle_amd
: {
6090 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6091 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
6092 emit_uniform_subgroup(ctx
, instr
, src
);
6095 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6096 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
6097 if (dst
.regClass() == v1
) {
6099 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
6101 } else if (dst
.regClass() == v2
) {
6102 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6103 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6104 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
6105 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
6106 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6107 emit_split_vector(ctx
, dst
, 2);
6109 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6110 nir_print_instr(&instr
->instr
, stderr
);
6111 fprintf(stderr
, "\n");
6115 case nir_intrinsic_write_invocation_amd
: {
6116 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6117 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6118 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
6119 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6120 if (dst
.regClass() == v1
) {
6121 /* src2 is ignored for writelane. RA assigns the same reg for dst */
6122 emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val
, lane
, src
), dst
);
6123 } else if (dst
.regClass() == v2
) {
6124 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
6125 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
6126 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
6127 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
6128 Temp lo
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_lo
, lane
, src_hi
));
6129 Temp hi
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_hi
, lane
, src_hi
));
6130 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6131 emit_split_vector(ctx
, dst
, 2);
6133 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6134 nir_print_instr(&instr
->instr
, stderr
);
6135 fprintf(stderr
, "\n");
6139 case nir_intrinsic_mbcnt_amd
: {
6140 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6141 RegClass rc
= RegClass(src
.type(), 1);
6142 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
6143 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
6144 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6145 Temp wqm_tmp
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(mask_lo
), Operand(mask_hi
));
6146 emit_wqm(ctx
, wqm_tmp
, dst
);
6149 case nir_intrinsic_load_helper_invocation
: {
6150 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6151 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
6152 ctx
->block
->kind
|= block_kind_needs_lowering
;
6153 ctx
->program
->needs_exact
= true;
6156 case nir_intrinsic_is_helper_invocation
: {
6157 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6158 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
6159 ctx
->block
->kind
|= block_kind_needs_lowering
;
6160 ctx
->program
->needs_exact
= true;
6163 case nir_intrinsic_demote
:
6164 bld
.pseudo(aco_opcode::p_demote_to_helper
);
6165 ctx
->block
->kind
|= block_kind_uses_demote
;
6166 ctx
->program
->needs_exact
= true;
6168 case nir_intrinsic_demote_if
: {
6169 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6170 assert(src
.regClass() == bld
.lm
);
6171 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6172 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
6173 ctx
->block
->kind
|= block_kind_uses_demote
;
6174 ctx
->program
->needs_exact
= true;
6177 case nir_intrinsic_first_invocation
: {
6178 emit_wqm(ctx
, bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)),
6179 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
6182 case nir_intrinsic_shader_clock
:
6183 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
6184 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
6186 case nir_intrinsic_load_vertex_id_zero_base
: {
6187 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6188 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
6191 case nir_intrinsic_load_first_vertex
: {
6192 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6193 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.base_vertex
));
6196 case nir_intrinsic_load_base_instance
: {
6197 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6198 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.start_instance
));
6201 case nir_intrinsic_load_instance_id
: {
6202 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6203 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.instance_id
));
6206 case nir_intrinsic_load_draw_id
: {
6207 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6208 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.draw_id
));
6212 fprintf(stderr
, "Unimplemented intrinsic instr: ");
6213 nir_print_instr(&instr
->instr
, stderr
);
6214 fprintf(stderr
, "\n");
6222 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
6223 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
6224 enum glsl_base_type
*stype
)
6226 nir_deref_instr
*texture_deref_instr
= NULL
;
6227 nir_deref_instr
*sampler_deref_instr
= NULL
;
6230 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6231 switch (instr
->src
[i
].src_type
) {
6232 case nir_tex_src_texture_deref
:
6233 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6235 case nir_tex_src_sampler_deref
:
6236 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6238 case nir_tex_src_plane
:
6239 plane
= nir_src_as_int(instr
->src
[i
].src
);
6246 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
6248 if (!sampler_deref_instr
)
6249 sampler_deref_instr
= texture_deref_instr
;
6252 assert(instr
->op
!= nir_texop_txf_ms
&&
6253 instr
->op
!= nir_texop_samples_identical
);
6254 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
6255 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
6256 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6257 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
6259 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
6262 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
6264 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
6265 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
6266 Builder
bld(ctx
->program
, ctx
->block
);
6268 /* to avoid unnecessary moves, we split and recombine sampler and image */
6269 Temp img
[8] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
),
6270 bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
6271 Temp samp
[4] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
6272 bld
.pseudo(aco_opcode::p_split_vector
, Definition(img
[0]), Definition(img
[1]),
6273 Definition(img
[2]), Definition(img
[3]), Definition(img
[4]),
6274 Definition(img
[5]), Definition(img
[6]), Definition(img
[7]), *res_ptr
);
6275 bld
.pseudo(aco_opcode::p_split_vector
, Definition(samp
[0]), Definition(samp
[1]),
6276 Definition(samp
[2]), Definition(samp
[3]), *samp_ptr
);
6278 samp
[0] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), samp
[0], img
[7]);
6279 *res_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
6280 img
[0], img
[1], img
[2], img
[3],
6281 img
[4], img
[5], img
[6], img
[7]);
6282 *samp_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
6283 samp
[0], samp
[1], samp
[2], samp
[3]);
6286 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
6287 instr
->op
== nir_texop_samples_identical
))
6288 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6291 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
6292 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
6294 Builder
bld(ctx
->program
, ctx
->block
);
6296 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
6297 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
6298 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
6300 Operand
neg_one(0xbf800000u
);
6301 Operand
one(0x3f800000u
);
6302 Operand
two(0x40000000u
);
6303 Operand
four(0x40800000u
);
6305 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), ma
);
6306 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
6307 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
6309 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), four
, id
);
6310 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
6311 is_ma_y
= bld
.sop2(Builder::s_andn2
, bld
.hint_vcc(bld
.def(bld
.lm
)), is_ma_y
, is_ma_z
);
6312 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
);
6315 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
6316 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6317 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
6319 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6322 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
6323 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
6324 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6327 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6328 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
6330 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
6331 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
6334 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
6336 Builder
bld(ctx
->program
, ctx
->block
);
6337 Temp coord_args
[4], ma
, tc
, sc
, id
;
6338 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
6339 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
6342 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
6344 // see comment in ac_prepare_cube_coords()
6345 if (ctx
->options
->chip_class
<= GFX8
)
6346 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6349 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6351 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6352 vop3a
->operands
[0] = Operand(ma
);
6353 vop3a
->abs
[0] = true;
6354 Temp invma
= bld
.tmp(v1
);
6355 vop3a
->definitions
[0] = Definition(invma
);
6356 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6358 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6360 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6362 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6364 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6366 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6369 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6370 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6372 for (unsigned i
= 0; i
< 2; i
++) {
6373 // see comment in ac_prepare_cube_coords()
6375 Temp deriv_sc
, deriv_tc
;
6376 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6377 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6379 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6381 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6382 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6383 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6384 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6385 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6386 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6387 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6390 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6391 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6395 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6396 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6400 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6403 for (unsigned i
= 0; i
< coords
.size(); i
++)
6404 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6406 Builder
bld(ctx
->program
, ctx
->block
);
6407 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6409 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6410 for (unsigned i
= 0; i
< coords
.size(); i
++)
6411 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6412 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6413 vec
->definitions
[0] = Definition(res
);
6414 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6418 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6420 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6422 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6423 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6426 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6427 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6428 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6432 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6434 Builder
bld(ctx
->program
, ctx
->block
);
6435 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6436 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6437 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6438 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6439 nir_const_value
*sample_index_cv
= NULL
;
6440 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6441 enum glsl_base_type stype
;
6442 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6444 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6445 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6446 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6447 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6449 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6450 switch (instr
->src
[i
].src_type
) {
6451 case nir_tex_src_coord
:
6452 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6454 case nir_tex_src_bias
:
6455 if (instr
->op
== nir_texop_txb
) {
6456 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6460 case nir_tex_src_lod
: {
6461 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6463 if (val
&& val
->f32
<= 0.0) {
6466 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6471 case nir_tex_src_comparator
:
6472 if (instr
->is_shadow
) {
6473 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6477 case nir_tex_src_offset
:
6478 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6479 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6482 case nir_tex_src_ddx
:
6483 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6486 case nir_tex_src_ddy
:
6487 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6490 case nir_tex_src_ms_index
:
6491 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6492 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6493 has_sample_index
= true;
6495 case nir_tex_src_texture_offset
:
6496 case nir_tex_src_sampler_offset
:
6501 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6502 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6503 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6505 if (instr
->op
== nir_texop_texture_samples
) {
6506 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6508 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6509 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6510 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 */));
6511 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6513 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6514 samples
, Operand(1u), bld
.scc(is_msaa
));
6518 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6519 aco_ptr
<Instruction
> tmp_instr
;
6520 Temp acc
, pack
= Temp();
6522 uint32_t pack_const
= 0;
6523 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6524 if (!const_offset
[i
])
6526 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6529 if (offset
.type() == RegType::sgpr
) {
6530 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6531 if (const_offset
[i
])
6534 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6535 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6538 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6541 if (pack
== Temp()) {
6544 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6548 if (pack_const
&& pack
!= Temp())
6549 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6551 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6552 if (const_offset
[i
])
6555 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6556 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6559 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6562 if (pack
== Temp()) {
6565 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6569 if (pack_const
&& pack
!= Temp())
6570 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6572 if (pack_const
&& pack
== Temp())
6573 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6574 else if (pack
== Temp())
6580 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6581 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6583 /* pack derivatives */
6584 if (has_ddx
|| has_ddy
) {
6585 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
6586 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6587 ddx
, Operand(0u), ddy
, Operand(0u));
6589 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6594 if (instr
->coord_components
> 1 &&
6595 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6597 instr
->op
!= nir_texop_txf
)
6598 coords
= apply_round_slice(ctx
, coords
, 1);
6600 if (instr
->coord_components
> 2 &&
6601 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6602 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6603 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6604 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6606 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6607 coords
= apply_round_slice(ctx
, coords
, 2);
6609 if (ctx
->options
->chip_class
== GFX9
&&
6610 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6611 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6612 assert(coords
.size() > 0 && coords
.size() < 3);
6614 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6615 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6616 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6617 if (coords
.size() > 1)
6618 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6619 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6620 vec
->definitions
[0] = Definition(coords
);
6621 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6624 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6626 if (instr
->op
== nir_texop_samples_identical
)
6627 resource
= fmask_ptr
;
6629 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6630 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6631 instr
->op
!= nir_texop_txs
) {
6632 assert(has_sample_index
);
6633 Operand
op(sample_index
);
6634 if (sample_index_cv
)
6635 op
= Operand(sample_index_cv
->u32
);
6636 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6639 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6640 Temp split_coords
[coords
.size()];
6641 emit_split_vector(ctx
, coords
, coords
.size());
6642 for (unsigned i
= 0; i
< coords
.size(); i
++)
6643 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6646 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6647 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6648 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6651 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6652 for (unsigned i
= 0; i
< coords
.size(); i
++)
6653 vec
->operands
[i
] = Operand(split_coords
[i
]);
6654 coords
= bld
.tmp(coords
.regClass());
6655 vec
->definitions
[0] = Definition(coords
);
6656 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6661 /* Build tex instruction */
6662 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6663 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
6664 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
6666 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6669 /* gather4 selects the component by dmask and always returns vec4 */
6670 if (instr
->op
== nir_texop_tg4
) {
6671 assert(instr
->dest
.ssa
.num_components
== 4);
6672 if (instr
->is_shadow
)
6675 dmask
= 1 << instr
->component
;
6676 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6677 tmp_dst
= bld
.tmp(v4
);
6678 } else if (instr
->op
== nir_texop_samples_identical
) {
6679 tmp_dst
= bld
.tmp(v1
);
6680 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6681 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6684 aco_ptr
<MIMG_instruction
> tex
;
6685 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6687 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6689 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6690 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6693 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6694 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6696 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6697 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6698 tex
->operands
[1] = Operand(resource
);
6699 if (ctx
->options
->chip_class
== GFX9
&&
6700 instr
->op
== nir_texop_txs
&&
6701 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6703 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6704 } else if (instr
->op
== nir_texop_query_levels
) {
6705 tex
->dmask
= 1 << 3;
6710 tex
->definitions
[0] = Definition(tmp_dst
);
6712 tex
->can_reorder
= true;
6713 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6716 /* divide 3rd value by 6 by multiplying with magic number */
6717 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6718 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6719 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6720 assert(instr
->dest
.ssa
.num_components
== 3);
6721 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6722 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6723 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6724 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6729 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6733 Temp tg4_compare_cube_wa64
= Temp();
6735 if (tg4_integer_workarounds
) {
6736 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6737 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6738 tex
->operands
[1] = Operand(resource
);
6742 Temp size
= bld
.tmp(v2
);
6743 tex
->definitions
[0] = Definition(size
);
6744 tex
->can_reorder
= true;
6745 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6746 emit_split_vector(ctx
, size
, size
.size());
6749 for (unsigned i
= 0; i
< 2; i
++) {
6750 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6751 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6752 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6753 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6756 Temp orig_coords
[2] = {
6757 emit_extract_vector(ctx
, coords
, 0, v1
),
6758 emit_extract_vector(ctx
, coords
, 1, v1
)};
6759 Temp new_coords
[2] = {
6760 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6761 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6764 if (tg4_integer_cube_workaround
) {
6765 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6766 Temp desc
[resource
.size()];
6767 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6768 Format::PSEUDO
, 1, resource
.size())};
6769 split
->operands
[0] = Operand(resource
);
6770 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6771 desc
[i
] = bld
.tmp(s1
);
6772 split
->definitions
[i
] = Definition(desc
[i
]);
6774 ctx
->block
->instructions
.emplace_back(std::move(split
));
6776 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6777 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6778 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6781 if (stype
== GLSL_TYPE_UINT
) {
6782 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6783 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6784 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6785 bld
.scc(compare_cube_wa
));
6787 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6788 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6789 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6790 bld
.scc(compare_cube_wa
));
6792 tg4_compare_cube_wa64
= bld
.tmp(bld
.lm
);
6793 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
6795 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6797 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6798 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6799 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6801 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6802 Format::PSEUDO
, resource
.size(), 1)};
6803 for (unsigned i
= 0; i
< resource
.size(); i
++)
6804 vec
->operands
[i
] = Operand(desc
[i
]);
6805 resource
= bld
.tmp(resource
.regClass());
6806 vec
->definitions
[0] = Definition(resource
);
6807 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6809 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6810 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6811 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6812 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6815 if (coords
.size() == 3) {
6816 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6817 new_coords
[0], new_coords
[1],
6818 emit_extract_vector(ctx
, coords
, 2, v1
));
6820 assert(coords
.size() == 2);
6821 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6822 new_coords
[0], new_coords
[1]);
6826 std::vector
<Operand
> args
;
6828 args
.emplace_back(Operand(offset
));
6830 args
.emplace_back(Operand(bias
));
6832 args
.emplace_back(Operand(compare
));
6834 args
.emplace_back(Operand(derivs
));
6835 args
.emplace_back(Operand(coords
));
6836 if (has_sample_index
)
6837 args
.emplace_back(Operand(sample_index
));
6839 args
.emplace_back(lod
);
6842 if (args
.size() > 1) {
6843 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6845 for (unsigned i
= 0; i
< args
.size(); i
++) {
6846 size
+= args
[i
].size();
6847 vec
->operands
[i
] = args
[i
];
6849 RegClass rc
= RegClass(RegType::vgpr
, size
);
6850 Temp tmp
= bld
.tmp(rc
);
6851 vec
->definitions
[0] = Definition(tmp
);
6852 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6855 assert(args
[0].isTemp());
6856 arg
= as_vgpr(ctx
, args
[0].getTemp());
6859 /* we don't need the bias, sample index, compare value or offset to be
6860 * computed in WQM but if the p_create_vector copies the coordinates, then it
6861 * needs to be in WQM */
6862 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6863 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6864 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6865 arg
= emit_wqm(ctx
, arg
, bld
.tmp(arg
.regClass()), true);
6867 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6868 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6870 assert(coords
.size() == 1);
6871 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6875 op
= aco_opcode::buffer_load_format_x
; break;
6877 op
= aco_opcode::buffer_load_format_xy
; break;
6879 op
= aco_opcode::buffer_load_format_xyz
; break;
6881 op
= aco_opcode::buffer_load_format_xyzw
; break;
6883 unreachable("Tex instruction loads more than 4 components.");
6886 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6887 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6890 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6892 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6893 mubuf
->operands
[0] = Operand(coords
);
6894 mubuf
->operands
[1] = Operand(resource
);
6895 mubuf
->operands
[2] = Operand((uint32_t) 0);
6896 mubuf
->definitions
[0] = Definition(tmp_dst
);
6897 mubuf
->idxen
= true;
6898 mubuf
->can_reorder
= true;
6899 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6901 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6906 if (instr
->op
== nir_texop_txf
||
6907 instr
->op
== nir_texop_txf_ms
||
6908 instr
->op
== nir_texop_samples_identical
) {
6909 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6910 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6911 tex
->operands
[0] = Operand(arg
);
6912 tex
->operands
[1] = Operand(resource
);
6917 tex
->definitions
[0] = Definition(tmp_dst
);
6918 tex
->can_reorder
= true;
6919 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6921 if (instr
->op
== nir_texop_samples_identical
) {
6922 assert(dmask
== 1 && dst
.regClass() == v1
);
6923 assert(dst
.id() != tmp_dst
.id());
6925 Temp tmp
= bld
.tmp(bld
.lm
);
6926 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6927 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6930 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6935 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6936 aco_opcode opcode
= aco_opcode::image_sample
;
6937 if (has_offset
) { /* image_sample_*_o */
6939 opcode
= aco_opcode::image_sample_c_o
;
6941 opcode
= aco_opcode::image_sample_c_d_o
;
6943 opcode
= aco_opcode::image_sample_c_b_o
;
6945 opcode
= aco_opcode::image_sample_c_lz_o
;
6947 opcode
= aco_opcode::image_sample_c_l_o
;
6949 opcode
= aco_opcode::image_sample_o
;
6951 opcode
= aco_opcode::image_sample_d_o
;
6953 opcode
= aco_opcode::image_sample_b_o
;
6955 opcode
= aco_opcode::image_sample_lz_o
;
6957 opcode
= aco_opcode::image_sample_l_o
;
6959 } else { /* no offset */
6961 opcode
= aco_opcode::image_sample_c
;
6963 opcode
= aco_opcode::image_sample_c_d
;
6965 opcode
= aco_opcode::image_sample_c_b
;
6967 opcode
= aco_opcode::image_sample_c_lz
;
6969 opcode
= aco_opcode::image_sample_c_l
;
6971 opcode
= aco_opcode::image_sample
;
6973 opcode
= aco_opcode::image_sample_d
;
6975 opcode
= aco_opcode::image_sample_b
;
6977 opcode
= aco_opcode::image_sample_lz
;
6979 opcode
= aco_opcode::image_sample_l
;
6983 if (instr
->op
== nir_texop_tg4
) {
6985 opcode
= aco_opcode::image_gather4_lz_o
;
6987 opcode
= aco_opcode::image_gather4_c_lz_o
;
6989 opcode
= aco_opcode::image_gather4_lz
;
6991 opcode
= aco_opcode::image_gather4_c_lz
;
6993 } else if (instr
->op
== nir_texop_lod
) {
6994 opcode
= aco_opcode::image_get_lod
;
6997 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6998 tex
->operands
[0] = Operand(arg
);
6999 tex
->operands
[1] = Operand(resource
);
7000 tex
->operands
[2] = Operand(sampler
);
7004 tex
->definitions
[0] = Definition(tmp_dst
);
7005 tex
->can_reorder
= true;
7006 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7008 if (tg4_integer_cube_workaround
) {
7009 assert(tmp_dst
.id() != dst
.id());
7010 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
7012 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
7014 for (unsigned i
= 0; i
< dst
.size(); i
++) {
7015 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
7017 if (stype
== GLSL_TYPE_UINT
)
7018 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
7020 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
7021 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
7023 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
7024 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
7025 val
[0], val
[1], val
[2], val
[3]);
7027 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
7028 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
7033 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
7035 Temp tmp
= get_ssa_temp(ctx
, ssa
);
7036 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
7037 return Operand(tmp
.regClass());
7039 return Operand(tmp
);
7042 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
7044 aco_ptr
<Pseudo_instruction
> phi
;
7045 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7046 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == ctx
->program
->lane_mask
);
7048 bool logical
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
];
7049 logical
|= ctx
->block
->kind
& block_kind_merge
;
7050 aco_opcode opcode
= logical
? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
7052 /* we want a sorted list of sources, since the predecessor list is also sorted */
7053 std::map
<unsigned, nir_ssa_def
*> phi_src
;
7054 nir_foreach_phi_src(src
, instr
)
7055 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
7057 std::vector
<unsigned>& preds
= logical
? ctx
->block
->logical_preds
: ctx
->block
->linear_preds
;
7058 unsigned num_operands
= 0;
7059 Operand operands
[std::max(exec_list_length(&instr
->srcs
), (unsigned)preds
.size())];
7060 unsigned num_defined
= 0;
7061 unsigned cur_pred_idx
= 0;
7062 for (std::pair
<unsigned, nir_ssa_def
*> src
: phi_src
) {
7063 if (cur_pred_idx
< preds
.size()) {
7064 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
7065 unsigned block
= ctx
->cf_info
.nir_to_aco
[src
.first
];
7066 unsigned skipped
= 0;
7067 while (cur_pred_idx
+ skipped
< preds
.size() && preds
[cur_pred_idx
+ skipped
] != block
)
7069 if (cur_pred_idx
+ skipped
< preds
.size()) {
7070 for (unsigned i
= 0; i
< skipped
; i
++)
7071 operands
[num_operands
++] = Operand(dst
.regClass());
7072 cur_pred_idx
+= skipped
;
7078 Operand op
= get_phi_operand(ctx
, src
.second
);
7079 operands
[num_operands
++] = op
;
7080 num_defined
+= !op
.isUndefined();
7082 /* handle block_kind_continue_or_break at loop exit blocks */
7083 while (cur_pred_idx
++ < preds
.size())
7084 operands
[num_operands
++] = Operand(dst
.regClass());
7086 if (num_defined
== 0) {
7087 Builder
bld(ctx
->program
, ctx
->block
);
7088 if (dst
.regClass() == s1
) {
7089 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
7090 } else if (dst
.regClass() == v1
) {
7091 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
7093 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
7094 for (unsigned i
= 0; i
< dst
.size(); i
++)
7095 vec
->operands
[i
] = Operand(0u);
7096 vec
->definitions
[0] = Definition(dst
);
7097 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7102 /* we can use a linear phi in some cases if one src is undef */
7103 if (dst
.is_linear() && ctx
->block
->kind
& block_kind_merge
&& num_defined
== 1) {
7104 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, num_operands
, 1));
7106 Block
*linear_else
= &ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
7107 Block
*invert
= &ctx
->program
->blocks
[linear_else
->linear_preds
[0]];
7108 assert(invert
->kind
& block_kind_invert
);
7110 unsigned then_block
= invert
->linear_preds
[0];
7112 Block
* insert_block
= NULL
;
7113 for (unsigned i
= 0; i
< num_operands
; i
++) {
7114 Operand op
= operands
[i
];
7115 if (op
.isUndefined())
7117 insert_block
= ctx
->block
->logical_preds
[i
] == then_block
? invert
: ctx
->block
;
7118 phi
->operands
[0] = op
;
7121 assert(insert_block
); /* should be handled by the "num_defined == 0" case above */
7122 phi
->operands
[1] = Operand(dst
.regClass());
7123 phi
->definitions
[0] = Definition(dst
);
7124 insert_block
->instructions
.emplace(insert_block
->instructions
.begin(), std::move(phi
));
7128 /* try to scalarize vector phis */
7129 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
7130 // TODO: scalarize linear phis on divergent ifs
7131 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
7132 std::array
<Temp
, 4> new_vec
;
7133 for (unsigned i
= 0; can_scalarize
&& (i
< num_operands
); i
++) {
7134 Operand src
= operands
[i
];
7135 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end())
7136 can_scalarize
= false;
7138 if (can_scalarize
) {
7139 unsigned num_components
= instr
->dest
.ssa
.num_components
;
7140 assert(dst
.size() % num_components
== 0);
7141 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
7143 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
7144 for (unsigned k
= 0; k
< num_components
; k
++) {
7145 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
7146 for (unsigned i
= 0; i
< num_operands
; i
++) {
7147 Operand src
= operands
[i
];
7148 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
7150 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
7151 phi
->definitions
[0] = Definition(phi_dst
);
7152 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
7153 new_vec
[k
] = phi_dst
;
7154 vec
->operands
[k
] = Operand(phi_dst
);
7156 vec
->definitions
[0] = Definition(dst
);
7157 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7158 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
7163 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
7164 for (unsigned i
= 0; i
< num_operands
; i
++)
7165 phi
->operands
[i
] = operands
[i
];
7166 phi
->definitions
[0] = Definition(dst
);
7167 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
7171 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
7173 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
7175 assert(dst
.type() == RegType::sgpr
);
7177 if (dst
.size() == 1) {
7178 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
7180 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
7181 for (unsigned i
= 0; i
< dst
.size(); i
++)
7182 vec
->operands
[i
] = Operand(0u);
7183 vec
->definitions
[0] = Definition(dst
);
7184 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7188 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
7190 Builder
bld(ctx
->program
, ctx
->block
);
7191 Block
*logical_target
;
7192 append_logical_end(ctx
->block
);
7193 unsigned idx
= ctx
->block
->index
;
7195 switch (instr
->type
) {
7196 case nir_jump_break
:
7197 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
7198 add_logical_edge(idx
, logical_target
);
7199 ctx
->block
->kind
|= block_kind_break
;
7201 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
7202 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
7203 /* uniform break - directly jump out of the loop */
7204 ctx
->block
->kind
|= block_kind_uniform
;
7205 ctx
->cf_info
.has_branch
= true;
7206 bld
.branch(aco_opcode::p_branch
);
7207 add_linear_edge(idx
, logical_target
);
7210 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
7211 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
7213 case nir_jump_continue
:
7214 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
7215 add_logical_edge(idx
, logical_target
);
7216 ctx
->block
->kind
|= block_kind_continue
;
7218 if (ctx
->cf_info
.parent_if
.is_divergent
) {
7219 /* for potential uniform breaks after this continue,
7220 we must ensure that they are handled correctly */
7221 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
7222 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
7223 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
7225 /* uniform continue - directly jump to the loop header */
7226 ctx
->block
->kind
|= block_kind_uniform
;
7227 ctx
->cf_info
.has_branch
= true;
7228 bld
.branch(aco_opcode::p_branch
);
7229 add_linear_edge(idx
, logical_target
);
7234 fprintf(stderr
, "Unknown NIR jump instr: ");
7235 nir_print_instr(&instr
->instr
, stderr
);
7236 fprintf(stderr
, "\n");
7240 /* remove critical edges from linear CFG */
7241 bld
.branch(aco_opcode::p_branch
);
7242 Block
* break_block
= ctx
->program
->create_and_insert_block();
7243 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7244 break_block
->kind
|= block_kind_uniform
;
7245 add_linear_edge(idx
, break_block
);
7246 /* the loop_header pointer might be invalidated by this point */
7247 if (instr
->type
== nir_jump_continue
)
7248 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
7249 add_linear_edge(break_block
->index
, logical_target
);
7250 bld
.reset(break_block
);
7251 bld
.branch(aco_opcode::p_branch
);
7253 Block
* continue_block
= ctx
->program
->create_and_insert_block();
7254 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7255 add_linear_edge(idx
, continue_block
);
7256 append_logical_start(continue_block
);
7257 ctx
->block
= continue_block
;
7261 void visit_block(isel_context
*ctx
, nir_block
*block
)
7263 nir_foreach_instr(instr
, block
) {
7264 switch (instr
->type
) {
7265 case nir_instr_type_alu
:
7266 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
7268 case nir_instr_type_load_const
:
7269 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
7271 case nir_instr_type_intrinsic
:
7272 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
7274 case nir_instr_type_tex
:
7275 visit_tex(ctx
, nir_instr_as_tex(instr
));
7277 case nir_instr_type_phi
:
7278 visit_phi(ctx
, nir_instr_as_phi(instr
));
7280 case nir_instr_type_ssa_undef
:
7281 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
7283 case nir_instr_type_deref
:
7285 case nir_instr_type_jump
:
7286 visit_jump(ctx
, nir_instr_as_jump(instr
));
7289 fprintf(stderr
, "Unknown NIR instr type: ");
7290 nir_print_instr(instr
, stderr
);
7291 fprintf(stderr
, "\n");
7296 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7297 ctx
->cf_info
.nir_to_aco
[block
->index
] = ctx
->block
->index
;
7302 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
7304 append_logical_end(ctx
->block
);
7305 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
7306 Builder
bld(ctx
->program
, ctx
->block
);
7307 bld
.branch(aco_opcode::p_branch
);
7308 unsigned loop_preheader_idx
= ctx
->block
->index
;
7310 Block loop_exit
= Block();
7311 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7312 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
7314 Block
* loop_header
= ctx
->program
->create_and_insert_block();
7315 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
7316 loop_header
->kind
|= block_kind_loop_header
;
7317 add_edge(loop_preheader_idx
, loop_header
);
7318 ctx
->block
= loop_header
;
7320 /* emit loop body */
7321 unsigned loop_header_idx
= loop_header
->index
;
7322 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
7323 append_logical_start(ctx
->block
);
7324 visit_cf_list(ctx
, &loop
->body
);
7326 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7327 if (!ctx
->cf_info
.has_branch
) {
7328 append_logical_end(ctx
->block
);
7329 if (ctx
->cf_info
.exec_potentially_empty
) {
7330 /* Discards can result in code running with an empty exec mask.
7331 * This would result in divergent breaks not ever being taken. As a
7332 * workaround, break the loop when the loop mask is empty instead of
7333 * always continuing. */
7334 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
7335 unsigned block_idx
= ctx
->block
->index
;
7337 /* create helper blocks to avoid critical edges */
7338 Block
*break_block
= ctx
->program
->create_and_insert_block();
7339 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7340 break_block
->kind
= block_kind_uniform
;
7341 bld
.reset(break_block
);
7342 bld
.branch(aco_opcode::p_branch
);
7343 add_linear_edge(block_idx
, break_block
);
7344 add_linear_edge(break_block
->index
, &loop_exit
);
7346 Block
*continue_block
= ctx
->program
->create_and_insert_block();
7347 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7348 continue_block
->kind
= block_kind_uniform
;
7349 bld
.reset(continue_block
);
7350 bld
.branch(aco_opcode::p_branch
);
7351 add_linear_edge(block_idx
, continue_block
);
7352 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7354 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
7355 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
7357 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
7358 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7359 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7361 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7364 bld
.reset(ctx
->block
);
7365 bld
.branch(aco_opcode::p_branch
);
7368 /* fixup phis in loop header from unreachable blocks */
7369 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
7370 bool linear
= ctx
->cf_info
.has_branch
;
7371 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7372 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
7373 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
7374 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
7375 /* the last operand should be the one that needs to be removed */
7376 instr
->operands
.pop_back();
7377 } else if (!is_phi(instr
)) {
7383 ctx
->cf_info
.has_branch
= false;
7385 // TODO: if the loop has not a single exit, we must add one °°
7386 /* emit loop successor block */
7387 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
7388 append_logical_start(ctx
->block
);
7391 // TODO: check if it is beneficial to not branch on continues
7392 /* trim linear phis in loop header */
7393 for (auto&& instr
: loop_entry
->instructions
) {
7394 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
7395 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
7396 new_phi
->definitions
[0] = instr
->definitions
[0];
7397 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
7398 new_phi
->operands
[i
] = instr
->operands
[i
];
7399 /* check that the remaining operands are all the same */
7400 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
7401 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
7402 instr
.swap(new_phi
);
7403 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7412 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7416 append_logical_end(ctx
->block
);
7417 ctx
->block
->kind
|= block_kind_branch
;
7419 /* branch to linear then block */
7420 assert(cond
.regClass() == ctx
->program
->lane_mask
);
7421 aco_ptr
<Pseudo_branch_instruction
> branch
;
7422 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7423 branch
->operands
[0] = Operand(cond
);
7424 ctx
->block
->instructions
.push_back(std::move(branch
));
7426 ic
->BB_if_idx
= ctx
->block
->index
;
7427 ic
->BB_invert
= Block();
7428 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7429 /* Invert blocks are intentionally not marked as top level because they
7430 * are not part of the logical cfg. */
7431 ic
->BB_invert
.kind
|= block_kind_invert
;
7432 ic
->BB_endif
= Block();
7433 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7434 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7436 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7437 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7438 ctx
->cf_info
.parent_if
.is_divergent
= true;
7439 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7441 /** emit logical then block */
7442 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7443 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7444 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7445 ctx
->block
= BB_then_logical
;
7446 append_logical_start(BB_then_logical
);
7449 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7451 Block
*BB_then_logical
= ctx
->block
;
7452 append_logical_end(BB_then_logical
);
7453 /* branch from logical then block to invert block */
7454 aco_ptr
<Pseudo_branch_instruction
> branch
;
7455 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7456 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7457 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7458 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7459 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7460 BB_then_logical
->kind
|= block_kind_uniform
;
7461 assert(!ctx
->cf_info
.has_branch
);
7462 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7463 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7465 /** emit linear then block */
7466 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7467 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7468 BB_then_linear
->kind
|= block_kind_uniform
;
7469 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7470 /* branch from linear then block to invert block */
7471 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7472 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7473 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7475 /** emit invert merge block */
7476 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7477 ic
->invert_idx
= ctx
->block
->index
;
7479 /* branch to linear else block (skip else) */
7480 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7481 branch
->operands
[0] = Operand(ic
->cond
);
7482 ctx
->block
->instructions
.push_back(std::move(branch
));
7484 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7485 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7487 /** emit logical else block */
7488 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7489 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7490 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7491 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7492 ctx
->block
= BB_else_logical
;
7493 append_logical_start(BB_else_logical
);
7496 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7498 Block
*BB_else_logical
= ctx
->block
;
7499 append_logical_end(BB_else_logical
);
7501 /* branch from logical else block to endif block */
7502 aco_ptr
<Pseudo_branch_instruction
> branch
;
7503 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7504 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7505 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7506 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7507 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7508 BB_else_logical
->kind
|= block_kind_uniform
;
7510 assert(!ctx
->cf_info
.has_branch
);
7511 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7514 /** emit linear else block */
7515 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7516 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7517 BB_else_linear
->kind
|= block_kind_uniform
;
7518 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7520 /* branch from linear else block to endif block */
7521 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7522 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7523 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7526 /** emit endif merge block */
7527 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7528 append_logical_start(ctx
->block
);
7531 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7532 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7533 /* uniform control flow never has an empty exec-mask */
7534 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7535 ctx
->cf_info
.exec_potentially_empty
= false;
7538 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7540 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7541 Builder
bld(ctx
->program
, ctx
->block
);
7542 aco_ptr
<Pseudo_branch_instruction
> branch
;
7544 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7546 * Uniform conditionals are represented in the following way*) :
7548 * The linear and logical CFG:
7551 * BB_THEN (logical) BB_ELSE (logical)
7555 * *) Exceptions may be due to break and continue statements within loops
7556 * If a break/continue happens within uniform control flow, it branches
7557 * to the loop exit/entry block. Otherwise, it branches to the next
7560 append_logical_end(ctx
->block
);
7561 ctx
->block
->kind
|= block_kind_uniform
;
7564 assert(cond
.regClass() == bld
.lm
);
7565 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7566 cond
= bool_to_scalar_condition(ctx
, cond
);
7568 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7569 branch
->operands
[0] = Operand(cond
);
7570 branch
->operands
[0].setFixed(scc
);
7571 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7573 unsigned BB_if_idx
= ctx
->block
->index
;
7574 Block BB_endif
= Block();
7575 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7576 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7578 /** emit then block */
7579 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7580 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7581 add_edge(BB_if_idx
, BB_then
);
7582 append_logical_start(BB_then
);
7583 ctx
->block
= BB_then
;
7584 visit_cf_list(ctx
, &if_stmt
->then_list
);
7585 BB_then
= ctx
->block
;
7586 bool then_branch
= ctx
->cf_info
.has_branch
;
7587 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7590 append_logical_end(BB_then
);
7591 /* branch from then block to endif block */
7592 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7593 BB_then
->instructions
.emplace_back(std::move(branch
));
7594 add_linear_edge(BB_then
->index
, &BB_endif
);
7595 if (!then_branch_divergent
)
7596 add_logical_edge(BB_then
->index
, &BB_endif
);
7597 BB_then
->kind
|= block_kind_uniform
;
7600 ctx
->cf_info
.has_branch
= false;
7601 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7603 /** emit else block */
7604 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7605 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7606 add_edge(BB_if_idx
, BB_else
);
7607 append_logical_start(BB_else
);
7608 ctx
->block
= BB_else
;
7609 visit_cf_list(ctx
, &if_stmt
->else_list
);
7610 BB_else
= ctx
->block
;
7612 if (!ctx
->cf_info
.has_branch
) {
7613 append_logical_end(BB_else
);
7614 /* branch from then block to endif block */
7615 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7616 BB_else
->instructions
.emplace_back(std::move(branch
));
7617 add_linear_edge(BB_else
->index
, &BB_endif
);
7618 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7619 add_logical_edge(BB_else
->index
, &BB_endif
);
7620 BB_else
->kind
|= block_kind_uniform
;
7623 ctx
->cf_info
.has_branch
&= then_branch
;
7624 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7626 /** emit endif merge block */
7627 if (!ctx
->cf_info
.has_branch
) {
7628 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7629 append_logical_start(ctx
->block
);
7631 } else { /* non-uniform condition */
7633 * To maintain a logical and linear CFG without critical edges,
7634 * non-uniform conditionals are represented in the following way*) :
7639 * BB_THEN (logical) BB_THEN (linear)
7641 * BB_INVERT (linear)
7643 * BB_ELSE (logical) BB_ELSE (linear)
7650 * BB_THEN (logical) BB_ELSE (logical)
7654 * *) Exceptions may be due to break and continue statements within loops
7659 begin_divergent_if_then(ctx
, &ic
, cond
);
7660 visit_cf_list(ctx
, &if_stmt
->then_list
);
7662 begin_divergent_if_else(ctx
, &ic
);
7663 visit_cf_list(ctx
, &if_stmt
->else_list
);
7665 end_divergent_if(ctx
, &ic
);
7669 static void visit_cf_list(isel_context
*ctx
,
7670 struct exec_list
*list
)
7672 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7673 switch (node
->type
) {
7674 case nir_cf_node_block
:
7675 visit_block(ctx
, nir_cf_node_as_block(node
));
7677 case nir_cf_node_if
:
7678 visit_if(ctx
, nir_cf_node_as_if(node
));
7680 case nir_cf_node_loop
:
7681 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7684 unreachable("unimplemented cf list type");
7689 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7691 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7692 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7693 if (!is_pos
&& !mask
)
7695 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7697 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7698 exp
->enabled_mask
= mask
;
7699 for (unsigned i
= 0; i
< 4; ++i
) {
7700 if (mask
& (1 << i
))
7701 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7703 exp
->operands
[i
] = Operand(v1
);
7705 exp
->valid_mask
= false;
7707 exp
->compressed
= false;
7709 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7711 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7712 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7715 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7717 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7718 exp
->enabled_mask
= 0;
7719 for (unsigned i
= 0; i
< 4; ++i
)
7720 exp
->operands
[i
] = Operand(v1
);
7721 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7722 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7723 exp
->enabled_mask
|= 0x1;
7725 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7726 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7727 exp
->enabled_mask
|= 0x4;
7729 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7730 if (ctx
->options
->chip_class
< GFX9
) {
7731 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7732 exp
->enabled_mask
|= 0x8;
7734 Builder
bld(ctx
->program
, ctx
->block
);
7736 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7737 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7738 if (exp
->operands
[2].isTemp())
7739 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7741 exp
->operands
[2] = Operand(out
);
7742 exp
->enabled_mask
|= 0x4;
7745 exp
->valid_mask
= false;
7747 exp
->compressed
= false;
7748 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7749 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7752 static void create_vs_exports(isel_context
*ctx
)
7754 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7756 if (outinfo
->export_prim_id
) {
7757 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7758 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = get_arg(ctx
, ctx
->args
->vs_prim_id
);
7761 if (ctx
->options
->key
.has_multiview_view_index
) {
7762 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7763 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, get_arg(ctx
, ctx
->args
->ac
.view_index
));
7766 /* the order these position exports are created is important */
7768 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7769 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7770 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7772 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7773 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7774 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7775 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7777 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7778 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7779 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7780 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7781 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7784 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7785 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7786 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7789 export_vs_varying(ctx
, i
, false, NULL
);
7793 static void emit_stream_output(isel_context
*ctx
,
7794 Temp
const *so_buffers
,
7795 Temp
const *so_write_offset
,
7796 const struct radv_stream_output
*output
)
7798 unsigned num_comps
= util_bitcount(output
->component_mask
);
7799 unsigned loc
= output
->location
;
7800 unsigned buf
= output
->buffer
;
7801 unsigned offset
= output
->offset
;
7803 assert(num_comps
&& num_comps
<= 4);
7804 if (!num_comps
|| num_comps
> 4)
7807 unsigned start
= ffs(output
->component_mask
) - 1;
7810 bool all_undef
= true;
7811 assert(ctx
->stage
== vertex_vs
);
7812 for (unsigned i
= 0; i
< num_comps
; i
++) {
7813 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7814 all_undef
= all_undef
&& !out
[i
].id();
7819 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7820 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7821 for (unsigned i
= 0; i
< num_comps
; ++i
)
7822 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7823 vec
->definitions
[0] = Definition(write_data
);
7824 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7827 switch (num_comps
) {
7829 opcode
= aco_opcode::buffer_store_dword
;
7832 opcode
= aco_opcode::buffer_store_dwordx2
;
7835 opcode
= aco_opcode::buffer_store_dwordx3
;
7838 opcode
= aco_opcode::buffer_store_dwordx4
;
7842 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7843 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7844 store
->operands
[1] = Operand(so_buffers
[buf
]);
7845 store
->operands
[2] = Operand((uint32_t) 0);
7846 store
->operands
[3] = Operand(write_data
);
7847 if (offset
> 4095) {
7848 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7849 Builder
bld(ctx
->program
, ctx
->block
);
7850 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7852 store
->offset
= offset
;
7854 store
->offen
= true;
7858 store
->can_reorder
= true;
7859 ctx
->block
->instructions
.emplace_back(std::move(store
));
7862 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7864 Builder
bld(ctx
->program
, ctx
->block
);
7867 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->streamout_buffers
));
7868 for (unsigned i
= 0; i
< 4; i
++) {
7869 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7873 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7876 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7877 get_arg(ctx
, ctx
->args
->streamout_config
), Operand(0x70010u
));
7879 Temp tid
= emit_mbcnt(ctx
, bld
.def(v1
));
7881 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7884 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7886 bld
.reset(ctx
->block
);
7888 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), get_arg(ctx
, ctx
->args
->streamout_write_idx
), tid
);
7890 Temp so_write_offset
[4];
7892 for (unsigned i
= 0; i
< 4; i
++) {
7893 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7898 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7899 get_arg(ctx
, ctx
->args
->streamout_write_idx
),
7900 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
7901 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7903 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7905 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7906 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u),
7907 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
7908 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7912 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7913 struct radv_stream_output
*output
=
7914 &ctx
->program
->info
->so
.outputs
[i
];
7915 if (stream
!= output
->stream
)
7918 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7921 begin_divergent_if_else(ctx
, &ic
);
7922 end_divergent_if(ctx
, &ic
);
7925 } /* end namespace */
7927 void split_arguments(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
7929 /* Split all arguments except for the first (ring_offsets) and the last
7930 * (exec) so that the dead channels don't stay live throughout the program.
7932 for (unsigned i
= 1; i
< startpgm
->definitions
.size() - 1; i
++) {
7933 if (startpgm
->definitions
[i
].regClass().size() > 1) {
7934 emit_split_vector(ctx
, startpgm
->definitions
[i
].getTemp(),
7935 startpgm
->definitions
[i
].regClass().size());
7940 void handle_bc_optimize(isel_context
*ctx
)
7942 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7943 Builder
bld(ctx
->program
, ctx
->block
);
7944 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7945 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7946 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7947 ctx
->persp_centroid
= get_arg(ctx
, ctx
->args
->ac
.persp_centroid
);
7948 ctx
->linear_centroid
= get_arg(ctx
, ctx
->args
->ac
.linear_centroid
);
7949 if (uses_center
&& uses_centroid
) {
7950 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
7951 get_arg(ctx
, ctx
->args
->ac
.prim_mask
), Operand(0u));
7953 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7955 for (unsigned i
= 0; i
< 2; i
++) {
7956 Temp persp_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_centroid
), i
, v1
);
7957 Temp persp_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_center
), i
, v1
);
7958 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7959 persp_centroid
, persp_center
, sel
);
7961 ctx
->persp_centroid
= bld
.tmp(v2
);
7962 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->persp_centroid
),
7963 Operand(new_coord
[0]), Operand(new_coord
[1]));
7964 emit_split_vector(ctx
, ctx
->persp_centroid
, 2);
7967 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7969 for (unsigned i
= 0; i
< 2; i
++) {
7970 Temp linear_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_centroid
), i
, v1
);
7971 Temp linear_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_center
), i
, v1
);
7972 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7973 linear_centroid
, linear_center
, sel
);
7975 ctx
->linear_centroid
= bld
.tmp(v2
);
7976 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->linear_centroid
),
7977 Operand(new_coord
[0]), Operand(new_coord
[1]));
7978 emit_split_vector(ctx
, ctx
->linear_centroid
, 2);
7983 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
7985 Program
*program
= ctx
->program
;
7987 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
7989 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
7990 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
7991 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
7992 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
7993 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
7995 program
->next_fp_mode
.must_flush_denorms32
=
7996 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
7997 program
->next_fp_mode
.must_flush_denorms16_64
=
7998 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
7999 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
8001 program
->next_fp_mode
.care_about_round32
=
8002 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
8004 program
->next_fp_mode
.care_about_round16_64
=
8005 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
8006 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
8008 /* default to preserving fp16 and fp64 denorms, since it's free */
8009 if (program
->next_fp_mode
.must_flush_denorms16_64
)
8010 program
->next_fp_mode
.denorm16_64
= 0;
8012 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
8014 /* preserving fp32 denorms is expensive, so only do it if asked */
8015 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
8016 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
8018 program
->next_fp_mode
.denorm32
= 0;
8020 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
8021 program
->next_fp_mode
.round32
= fp_round_tz
;
8023 program
->next_fp_mode
.round32
= fp_round_ne
;
8025 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
8026 program
->next_fp_mode
.round16_64
= fp_round_tz
;
8028 program
->next_fp_mode
.round16_64
= fp_round_ne
;
8030 ctx
->block
->fp_mode
= program
->next_fp_mode
;
8033 void select_program(Program
*program
,
8034 unsigned shader_count
,
8035 struct nir_shader
*const *shaders
,
8036 ac_shader_config
* config
,
8037 struct radv_shader_args
*args
)
8039 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, args
);
8041 for (unsigned i
= 0; i
< shader_count
; i
++) {
8042 nir_shader
*nir
= shaders
[i
];
8043 init_context(&ctx
, nir
);
8045 setup_fp_mode(&ctx
, nir
);
8048 /* needs to be after init_context() for FS */
8049 Pseudo_instruction
*startpgm
= add_startpgm(&ctx
);
8050 append_logical_start(ctx
.block
);
8051 split_arguments(&ctx
, startpgm
);
8055 if (shader_count
>= 2) {
8056 Builder
bld(ctx
.program
, ctx
.block
);
8057 Temp count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), ctx
.merged_wave_info
, Operand((8u << 16) | (i
* 8u)));
8058 Temp thread_id
= emit_mbcnt(&ctx
, bld
.def(v1
));
8059 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), count
, thread_id
);
8061 begin_divergent_if_then(&ctx
, &ic
, cond
);
8065 Builder
bld(ctx
.program
, ctx
.block
);
8066 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
8067 bld
.sopp(aco_opcode::s_barrier
);
8070 if (ctx
.stage
== fragment_fs
)
8071 handle_bc_optimize(&ctx
);
8073 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
8074 visit_cf_list(&ctx
, &func
->body
);
8076 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
8077 emit_streamout(&ctx
, 0);
8079 if (ctx
.stage
== vertex_vs
)
8080 create_vs_exports(&ctx
);
8082 if (shader_count
>= 2) {
8083 begin_divergent_if_else(&ctx
, &ic
);
8084 end_divergent_if(&ctx
, &ic
);
8087 ralloc_free(ctx
.divergent_vals
);
8090 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
8092 append_logical_end(ctx
.block
);
8093 ctx
.block
->kind
|= block_kind_uniform
;
8094 Builder
bld(ctx
.program
, ctx
.block
);
8095 if (ctx
.program
->wb_smem_l1_on_end
)
8096 bld
.smem(aco_opcode::s_dcache_wb
, false);
8097 bld
.sopp(aco_opcode::s_endpgm
);
8100 for (Block
& BB
: program
->blocks
) {
8101 for (unsigned idx
: BB
.linear_preds
)
8102 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
8103 for (unsigned idx
: BB
.logical_preds
)
8104 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);