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
31 #include "ac_shader_util.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
41 class loop_info_RAII
{
43 unsigned header_idx_old
;
45 bool divergent_cont_old
;
46 bool divergent_branch_old
;
47 bool divergent_if_old
;
50 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
52 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
53 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
54 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
55 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
57 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
58 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
59 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
60 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
61 ctx
->cf_info
.parent_if
.is_divergent
= false;
62 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
67 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
68 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
69 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
70 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
71 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
72 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
73 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
74 ctx
->cf_info
.exec_potentially_empty_discard
= false;
82 bool exec_potentially_empty_discard_old
;
83 bool exec_potentially_empty_break_old
;
84 uint16_t exec_potentially_empty_break_depth_old
;
88 bool then_branch_divergent
;
93 static void visit_cf_list(struct isel_context
*ctx
,
94 struct exec_list
*list
);
96 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
98 succ
->logical_preds
.emplace_back(pred_idx
);
102 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
104 succ
->linear_preds
.emplace_back(pred_idx
);
107 static void add_edge(unsigned pred_idx
, Block
*succ
)
109 add_logical_edge(pred_idx
, succ
);
110 add_linear_edge(pred_idx
, succ
);
113 static void append_logical_start(Block
*b
)
115 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
118 static void append_logical_end(Block
*b
)
120 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
123 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
125 assert(ctx
->allocated
[def
->index
].id());
126 return ctx
->allocated
[def
->index
];
129 Temp
emit_mbcnt(isel_context
*ctx
, Definition dst
,
130 Operand mask_lo
= Operand((uint32_t) -1), Operand mask_hi
= Operand((uint32_t) -1))
132 Builder
bld(ctx
->program
, ctx
->block
);
133 Definition lo_def
= ctx
->program
->wave_size
== 32 ? dst
: bld
.def(v1
);
134 Temp thread_id_lo
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, lo_def
, mask_lo
, Operand(0u));
136 if (ctx
->program
->wave_size
== 32) {
139 Temp thread_id_hi
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, dst
, mask_hi
, thread_id_lo
);
144 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
146 Builder
bld(ctx
->program
, ctx
->block
);
149 dst
= bld
.tmp(src
.regClass());
151 assert(src
.size() == dst
.size());
153 if (ctx
->stage
!= fragment_fs
) {
157 bld
.copy(Definition(dst
), src
);
161 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
162 ctx
->program
->needs_wqm
|= program_needs_wqm
;
166 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
168 if (index
.regClass() == s1
)
169 return bld
.readlane(bld
.def(s1
), data
, index
);
171 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
173 /* Currently not implemented on GFX6-7 */
174 assert(ctx
->options
->chip_class
>= GFX8
);
176 if (ctx
->options
->chip_class
<= GFX9
|| ctx
->program
->wave_size
== 32) {
177 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
180 /* GFX10, wave64 mode:
181 * The bpermute instruction is limited to half-wave operation, which means that it can't
182 * properly support subgroup shuffle like older generations (or wave32 mode), so we
185 if (!ctx
->has_gfx10_wave64_bpermute
) {
186 ctx
->has_gfx10_wave64_bpermute
= true;
187 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
188 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
191 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
192 Temp lane_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), lane_id
);
193 Temp index_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), index
);
194 Temp cmp
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
, vcc
), lane_is_hi
, index_is_hi
);
196 return bld
.reduction(aco_opcode::p_wave64_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
),
197 bld
.vcc(cmp
), Operand(v2
.as_linear()), index_x4
, data
, gfx10_wave64_bpermute
);
200 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
202 if (val
.type() == RegType::sgpr
) {
203 Builder
bld(ctx
->program
, ctx
->block
);
204 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
206 assert(val
.type() == RegType::vgpr
);
210 //assumes a != 0xffffffff
211 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
214 Builder
bld(ctx
->program
, ctx
->block
);
216 if (util_is_power_of_two_or_zero(b
)) {
217 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
221 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
223 assert(info
.multiplier
<= 0xffffffff);
225 bool pre_shift
= info
.pre_shift
!= 0;
226 bool increment
= info
.increment
!= 0;
227 bool multiply
= true;
228 bool post_shift
= info
.post_shift
!= 0;
230 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
231 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
235 Temp pre_shift_dst
= a
;
237 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
238 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
241 Temp increment_dst
= pre_shift_dst
;
243 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
244 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
247 Temp multiply_dst
= increment_dst
;
249 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
250 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
251 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
255 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
259 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
261 Builder
bld(ctx
->program
, ctx
->block
);
262 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
266 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
268 /* no need to extract the whole vector */
269 if (src
.regClass() == dst_rc
) {
273 assert(src
.size() > idx
);
274 Builder
bld(ctx
->program
, ctx
->block
);
275 auto it
= ctx
->allocated_vec
.find(src
.id());
276 /* the size check needs to be early because elements other than 0 may be garbage */
277 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
278 if (it
->second
[idx
].regClass() == dst_rc
) {
279 return it
->second
[idx
];
281 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
282 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
283 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
287 if (src
.size() == dst_rc
.size()) {
289 return bld
.copy(bld
.def(dst_rc
), src
);
291 Temp dst
= bld
.tmp(dst_rc
);
292 emit_extract_vector(ctx
, src
, idx
, dst
);
297 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
299 if (num_components
== 1)
301 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
303 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
304 split
->operands
[0] = Operand(vec_src
);
305 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
306 for (unsigned i
= 0; i
< num_components
; i
++) {
307 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
308 split
->definitions
[i
] = Definition(elems
[i
]);
310 ctx
->block
->instructions
.emplace_back(std::move(split
));
311 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
314 /* This vector expansion uses a mask to determine which elements in the new vector
315 * come from the original vector. The other elements are undefined. */
316 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
318 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
323 Builder
bld(ctx
->program
, ctx
->block
);
324 if (num_components
== 1) {
325 if (dst
.type() == RegType::sgpr
)
326 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
328 bld
.copy(Definition(dst
), vec_src
);
332 unsigned component_size
= dst
.size() / num_components
;
333 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
335 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
336 vec
->definitions
[0] = Definition(dst
);
338 for (unsigned i
= 0; i
< num_components
; i
++) {
339 if (mask
& (1 << i
)) {
340 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
341 if (dst
.type() == RegType::sgpr
)
342 src
= bld
.as_uniform(src
);
343 vec
->operands
[i
] = Operand(src
);
345 vec
->operands
[i
] = Operand(0u);
347 elems
[i
] = vec
->operands
[i
].getTemp();
349 ctx
->block
->instructions
.emplace_back(std::move(vec
));
350 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
353 Temp
bool_to_vector_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s2
))
355 Builder
bld(ctx
->program
, ctx
->block
);
357 dst
= bld
.tmp(bld
.lm
);
359 assert(val
.regClass() == s1
);
360 assert(dst
.regClass() == bld
.lm
);
362 return bld
.sop2(Builder::s_cselect
, Definition(dst
), Operand((uint32_t) -1), Operand(0u), bld
.scc(val
));
365 Temp
bool_to_scalar_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s1
))
367 Builder
bld(ctx
->program
, ctx
->block
);
371 assert(val
.regClass() == bld
.lm
);
372 assert(dst
.regClass() == s1
);
374 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
375 Temp tmp
= bld
.tmp(s1
);
376 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, bld
.lm
));
377 return emit_wqm(ctx
, tmp
, dst
);
380 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
382 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
383 return get_ssa_temp(ctx
, src
.src
.ssa
);
385 if (src
.src
.ssa
->num_components
== size
) {
386 bool identity_swizzle
= true;
387 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
388 if (src
.swizzle
[i
] != i
)
389 identity_swizzle
= false;
391 if (identity_swizzle
)
392 return get_ssa_temp(ctx
, src
.src
.ssa
);
395 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
396 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
397 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
398 assert(vec
.size() % elem_size
== 0);
400 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
402 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
405 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
406 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
407 for (unsigned i
= 0; i
< size
; ++i
) {
408 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
409 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
411 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
412 vec_instr
->definitions
[0] = Definition(dst
);
413 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
414 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
419 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
423 Builder
bld(ctx
->program
, ctx
->block
);
424 if (ptr
.type() == RegType::vgpr
)
425 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
426 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
427 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
430 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
432 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
433 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
434 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
435 sop2
->definitions
[0] = Definition(dst
);
437 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
438 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
441 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
,
442 bool commutative
, bool swap_srcs
=false, bool flush_denorms
= false)
444 Builder
bld(ctx
->program
, ctx
->block
);
445 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
446 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
447 if (src1
.type() == RegType::sgpr
) {
448 if (commutative
&& src0
.type() == RegType::vgpr
) {
452 } else if (src0
.type() == RegType::vgpr
&&
453 op
!= aco_opcode::v_madmk_f32
&&
454 op
!= aco_opcode::v_madak_f32
&&
455 op
!= aco_opcode::v_madmk_f16
&&
456 op
!= aco_opcode::v_madak_f16
) {
457 /* If the instruction is not commutative, we emit a VOP3A instruction */
458 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
461 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
465 if (flush_denorms
&& ctx
->program
->chip_class
< GFX9
) {
466 assert(dst
.size() == 1);
467 Temp tmp
= bld
.vop2(op
, bld
.def(v1
), src0
, src1
);
468 bld
.vop2(aco_opcode::v_mul_f32
, Definition(dst
), Operand(0x3f800000u
), tmp
);
470 bld
.vop2(op
, Definition(dst
), src0
, src1
);
474 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
,
475 bool flush_denorms
= false)
477 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
478 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
479 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
481 /* ensure that the instruction has at most 1 sgpr operand
482 * The optimizer will inline constants for us */
483 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
484 src0
= as_vgpr(ctx
, src0
);
485 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
486 src1
= as_vgpr(ctx
, src1
);
487 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
488 src2
= as_vgpr(ctx
, src2
);
490 Builder
bld(ctx
->program
, ctx
->block
);
491 if (flush_denorms
&& ctx
->program
->chip_class
< GFX9
) {
492 assert(dst
.size() == 1);
493 Temp tmp
= bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
494 bld
.vop2(aco_opcode::v_mul_f32
, Definition(dst
), Operand(0x3f800000u
), tmp
);
496 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
500 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
502 Builder
bld(ctx
->program
, ctx
->block
);
503 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
506 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
508 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
509 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
510 assert(src0
.size() == src1
.size());
512 aco_ptr
<Instruction
> vopc
;
513 if (src1
.type() == RegType::sgpr
) {
514 if (src0
.type() == RegType::vgpr
) {
515 /* to swap the operands, we might also have to change the opcode */
517 case aco_opcode::v_cmp_lt_f32
:
518 op
= aco_opcode::v_cmp_gt_f32
;
520 case aco_opcode::v_cmp_ge_f32
:
521 op
= aco_opcode::v_cmp_le_f32
;
523 case aco_opcode::v_cmp_lt_i32
:
524 op
= aco_opcode::v_cmp_gt_i32
;
526 case aco_opcode::v_cmp_ge_i32
:
527 op
= aco_opcode::v_cmp_le_i32
;
529 case aco_opcode::v_cmp_lt_u32
:
530 op
= aco_opcode::v_cmp_gt_u32
;
532 case aco_opcode::v_cmp_ge_u32
:
533 op
= aco_opcode::v_cmp_le_u32
;
535 case aco_opcode::v_cmp_lt_f64
:
536 op
= aco_opcode::v_cmp_gt_f64
;
538 case aco_opcode::v_cmp_ge_f64
:
539 op
= aco_opcode::v_cmp_le_f64
;
541 case aco_opcode::v_cmp_lt_i64
:
542 op
= aco_opcode::v_cmp_gt_i64
;
544 case aco_opcode::v_cmp_ge_i64
:
545 op
= aco_opcode::v_cmp_le_i64
;
547 case aco_opcode::v_cmp_lt_u64
:
548 op
= aco_opcode::v_cmp_gt_u64
;
550 case aco_opcode::v_cmp_ge_u64
:
551 op
= aco_opcode::v_cmp_le_u64
;
553 default: /* eq and ne are commutative */
560 src1
= as_vgpr(ctx
, src1
);
564 Builder
bld(ctx
->program
, ctx
->block
);
565 bld
.vopc(op
, bld
.hint_vcc(Definition(dst
)), src0
, src1
);
568 void emit_sopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
570 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
571 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
572 Builder
bld(ctx
->program
, ctx
->block
);
574 assert(dst
.regClass() == bld
.lm
);
575 assert(src0
.type() == RegType::sgpr
);
576 assert(src1
.type() == RegType::sgpr
);
577 assert(src0
.regClass() == src1
.regClass());
579 /* Emit the SALU comparison instruction */
580 Temp cmp
= bld
.sopc(op
, bld
.scc(bld
.def(s1
)), src0
, src1
);
581 /* Turn the result into a per-lane bool */
582 bool_to_vector_condition(ctx
, cmp
, dst
);
585 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
,
586 aco_opcode v32_op
, aco_opcode v64_op
, aco_opcode s32_op
= aco_opcode::num_opcodes
, aco_opcode s64_op
= aco_opcode::num_opcodes
)
588 aco_opcode s_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? s64_op
: s32_op
;
589 aco_opcode v_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? v64_op
: v32_op
;
590 bool divergent_vals
= ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
];
591 bool use_valu
= s_op
== aco_opcode::num_opcodes
||
593 ctx
->allocated
[instr
->src
[0].src
.ssa
->index
].type() == RegType::vgpr
||
594 ctx
->allocated
[instr
->src
[1].src
.ssa
->index
].type() == RegType::vgpr
;
595 aco_opcode op
= use_valu
? v_op
: s_op
;
596 assert(op
!= aco_opcode::num_opcodes
);
597 assert(dst
.regClass() == ctx
->program
->lane_mask
);
600 emit_vopc_instruction(ctx
, instr
, op
, dst
);
602 emit_sopc_instruction(ctx
, instr
, op
, dst
);
605 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, Builder::WaveSpecificOpcode op
, Temp dst
)
607 Builder
bld(ctx
->program
, ctx
->block
);
608 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
609 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
611 assert(dst
.regClass() == bld
.lm
);
612 assert(src0
.regClass() == bld
.lm
);
613 assert(src1
.regClass() == bld
.lm
);
615 bld
.sop2(op
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
618 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
620 Builder
bld(ctx
->program
, ctx
->block
);
621 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
622 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
623 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
625 assert(cond
.regClass() == bld
.lm
);
627 if (dst
.type() == RegType::vgpr
) {
628 aco_ptr
<Instruction
> bcsel
;
629 if (dst
.size() == 1) {
630 then
= as_vgpr(ctx
, then
);
631 els
= as_vgpr(ctx
, els
);
633 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
634 } else if (dst
.size() == 2) {
635 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
636 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
637 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
638 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
640 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
641 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
643 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
645 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
646 nir_print_instr(&instr
->instr
, stderr
);
647 fprintf(stderr
, "\n");
652 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
653 assert(dst
.regClass() == bld
.lm
);
654 assert(then
.regClass() == bld
.lm
);
655 assert(els
.regClass() == bld
.lm
);
658 if (!ctx
->divergent_vals
[instr
->src
[0].src
.ssa
->index
]) { /* uniform condition and values in sgpr */
659 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
660 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
661 assert(dst
.size() == then
.size());
662 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
663 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(bool_to_scalar_condition(ctx
, cond
)));
665 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
666 nir_print_instr(&instr
->instr
, stderr
);
667 fprintf(stderr
, "\n");
672 /* divergent boolean bcsel
673 * this implements bcsel on bools: dst = s0 ? s1 : s2
674 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
675 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
677 if (cond
.id() != then
.id())
678 then
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
, then
);
680 if (cond
.id() == els
.id())
681 bld
.sop1(Builder::s_mov
, Definition(dst
), then
);
683 bld
.sop2(Builder::s_or
, Definition(dst
), bld
.def(s1
, scc
), then
,
684 bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), els
, cond
));
687 void emit_scaled_op(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
,
688 aco_opcode op
, uint32_t undo
)
690 /* multiply by 16777216 to handle denormals */
691 Temp is_denormal
= bld
.vopc(aco_opcode::v_cmp_class_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
692 as_vgpr(ctx
, val
), bld
.copy(bld
.def(v1
), Operand((1u << 7) | (1u << 4))));
693 Temp scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x4b800000u
), val
);
694 scaled
= bld
.vop1(op
, bld
.def(v1
), scaled
);
695 scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(undo
), scaled
);
697 Temp not_scaled
= bld
.vop1(op
, bld
.def(v1
), val
);
699 bld
.vop2(aco_opcode::v_cndmask_b32
, dst
, not_scaled
, scaled
, is_denormal
);
702 void emit_rcp(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
704 if (ctx
->block
->fp_mode
.denorm32
== 0) {
705 bld
.vop1(aco_opcode::v_rcp_f32
, dst
, val
);
709 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rcp_f32
, 0x4b800000u
);
712 void emit_rsq(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
714 if (ctx
->block
->fp_mode
.denorm32
== 0) {
715 bld
.vop1(aco_opcode::v_rsq_f32
, dst
, val
);
719 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rsq_f32
, 0x45800000u
);
722 void emit_sqrt(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
724 if (ctx
->block
->fp_mode
.denorm32
== 0) {
725 bld
.vop1(aco_opcode::v_sqrt_f32
, dst
, val
);
729 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_sqrt_f32
, 0x39800000u
);
732 void emit_log2(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
734 if (ctx
->block
->fp_mode
.denorm32
== 0) {
735 bld
.vop1(aco_opcode::v_log_f32
, dst
, val
);
739 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_log_f32
, 0xc1c00000u
);
742 Temp
emit_trunc_f64(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
744 if (ctx
->options
->chip_class
>= GFX7
)
745 return bld
.vop1(aco_opcode::v_trunc_f64
, Definition(dst
), val
);
747 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
748 /* TODO: create more efficient code! */
749 if (val
.type() == RegType::sgpr
)
750 val
= as_vgpr(ctx
, val
);
752 /* Split the input value. */
753 Temp val_lo
= bld
.tmp(v1
), val_hi
= bld
.tmp(v1
);
754 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
756 /* Extract the exponent and compute the unbiased value. */
757 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, bld
.def(v1
), val
);
759 /* Extract the fractional part. */
760 Temp fract_mask
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(-1u), Operand(0x000fffffu
));
761 fract_mask
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), fract_mask
, exponent
);
763 Temp fract_mask_lo
= bld
.tmp(v1
), fract_mask_hi
= bld
.tmp(v1
);
764 bld
.pseudo(aco_opcode::p_split_vector
, Definition(fract_mask_lo
), Definition(fract_mask_hi
), fract_mask
);
766 Temp fract_lo
= bld
.tmp(v1
), fract_hi
= bld
.tmp(v1
);
767 Temp tmp
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), fract_mask_lo
);
768 fract_lo
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), val_lo
, tmp
);
769 tmp
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), fract_mask_hi
);
770 fract_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), val_hi
, tmp
);
772 /* Get the sign bit. */
773 Temp sign
= bld
.vop2(aco_opcode::v_ashr_i32
, bld
.def(v1
), Operand(31u), val_hi
);
775 /* Decide the operation to apply depending on the unbiased exponent. */
776 Temp exp_lt0
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), exponent
, Operand(0u));
777 Temp dst_lo
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), fract_lo
, bld
.copy(bld
.def(v1
), Operand(0u)), exp_lt0
);
778 Temp dst_hi
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), fract_hi
, sign
, exp_lt0
);
779 Temp exp_gt51
= bld
.vopc_e64(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), exponent
, Operand(51u));
780 dst_lo
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), dst_lo
, val_lo
, exp_gt51
);
781 dst_hi
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), dst_hi
, val_hi
, exp_gt51
);
783 return bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst_lo
, dst_hi
);
786 Temp
emit_floor_f64(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
788 if (ctx
->options
->chip_class
>= GFX7
)
789 return bld
.vop1(aco_opcode::v_floor_f64
, Definition(dst
), val
);
791 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
792 Temp src0
= as_vgpr(ctx
, val
);
794 Temp mask
= bld
.copy(bld
.def(s1
), Operand(3u)); /* isnan */
795 Temp min_val
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(-1u), Operand(0x3fefffffu
));
797 Temp isnan
= bld
.vopc_e64(aco_opcode::v_cmp_class_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, mask
);
798 Temp fract
= bld
.vop1(aco_opcode::v_fract_f64
, bld
.def(v2
), src0
);
799 Temp min
= bld
.vop3(aco_opcode::v_min_f64
, bld
.def(v2
), fract
, min_val
);
801 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
802 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), src0
);
803 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
804 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), min
);
806 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, isnan
);
807 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, isnan
);
809 Temp v
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), dst0
, dst1
);
811 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src0
, v
);
812 static_cast<VOP3A_instruction
*>(add
)->neg
[1] = true;
814 return add
->definitions
[0].getTemp();
817 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
819 if (!instr
->dest
.dest
.is_ssa
) {
820 fprintf(stderr
, "nir alu dst not in ssa: ");
821 nir_print_instr(&instr
->instr
, stderr
);
822 fprintf(stderr
, "\n");
825 Builder
bld(ctx
->program
, ctx
->block
);
826 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
831 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
832 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
833 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
834 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
835 vec
->operands
[i
] = Operand
{elems
[i
]};
837 vec
->definitions
[0] = Definition(dst
);
838 ctx
->block
->instructions
.emplace_back(std::move(vec
));
839 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
843 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
844 aco_ptr
<Instruction
> mov
;
845 if (dst
.type() == RegType::sgpr
) {
846 if (src
.type() == RegType::vgpr
)
847 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
848 else if (src
.regClass() == s1
)
849 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
850 else if (src
.regClass() == s2
)
851 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
853 unreachable("wrong src register class for nir_op_imov");
854 } else if (dst
.regClass() == v1
) {
855 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
856 } else if (dst
.regClass() == v2
) {
857 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
859 nir_print_instr(&instr
->instr
, stderr
);
860 unreachable("Should have been lowered to scalar.");
865 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
866 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
867 assert(src
.regClass() == bld
.lm
);
868 assert(dst
.regClass() == bld
.lm
);
869 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
870 Temp tmp
= bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
);
871 bld
.sop2(Builder::s_and
, Definition(dst
), bld
.def(s1
, scc
), tmp
, Operand(exec
, bld
.lm
));
872 } else if (dst
.regClass() == v1
) {
873 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
874 } else if (dst
.type() == RegType::sgpr
) {
875 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
876 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
878 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
879 nir_print_instr(&instr
->instr
, stderr
);
880 fprintf(stderr
, "\n");
885 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
886 if (dst
.regClass() == v1
) {
887 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
888 } else if (dst
.regClass() == s1
) {
889 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
890 } else if (dst
.size() == 2) {
891 Temp src0
= bld
.tmp(dst
.type(), 1);
892 Temp src1
= bld
.tmp(dst
.type(), 1);
893 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
895 if (dst
.regClass() == s2
) {
896 Temp carry
= bld
.tmp(s1
);
897 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
898 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
899 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
901 Temp lower
= bld
.tmp(v1
);
902 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
903 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
904 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
907 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
908 nir_print_instr(&instr
->instr
, stderr
);
909 fprintf(stderr
, "\n");
914 if (dst
.regClass() == s1
) {
915 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
916 } else if (dst
.regClass() == v1
) {
917 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
918 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
920 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
921 nir_print_instr(&instr
->instr
, stderr
);
922 fprintf(stderr
, "\n");
927 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
928 if (dst
.regClass() == s1
) {
929 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
930 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
931 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
932 } else if (dst
.regClass() == s2
) {
933 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
935 if (ctx
->program
->chip_class
>= GFX8
)
936 neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
938 neqz
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(0u)).def(1).getTemp();
939 /* SCC gets zero-extended to 64 bit */
940 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, bld
.scc(neqz
));
941 } else if (dst
.regClass() == v1
) {
942 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
943 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
944 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
945 } else if (dst
.regClass() == v2
) {
946 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
947 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
948 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
949 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
950 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
951 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
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_max_i32
, dst
, true);
962 } else if (dst
.regClass() == s1
) {
963 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
965 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
966 nir_print_instr(&instr
->instr
, stderr
);
967 fprintf(stderr
, "\n");
972 if (dst
.regClass() == v1
) {
973 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
974 } else if (dst
.regClass() == s1
) {
975 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
977 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
978 nir_print_instr(&instr
->instr
, stderr
);
979 fprintf(stderr
, "\n");
984 if (dst
.regClass() == v1
) {
985 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
986 } else if (dst
.regClass() == s1
) {
987 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
989 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
990 nir_print_instr(&instr
->instr
, stderr
);
991 fprintf(stderr
, "\n");
996 if (dst
.regClass() == v1
) {
997 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
998 } else if (dst
.regClass() == s1
) {
999 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
1001 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1002 nir_print_instr(&instr
->instr
, stderr
);
1003 fprintf(stderr
, "\n");
1008 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1009 emit_boolean_logic(ctx
, instr
, Builder::s_or
, dst
);
1010 } else if (dst
.regClass() == v1
) {
1011 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
1012 } else if (dst
.regClass() == s1
) {
1013 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
1014 } else if (dst
.regClass() == s2
) {
1015 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
1017 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1018 nir_print_instr(&instr
->instr
, stderr
);
1019 fprintf(stderr
, "\n");
1024 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1025 emit_boolean_logic(ctx
, instr
, Builder::s_and
, dst
);
1026 } else if (dst
.regClass() == v1
) {
1027 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
1028 } else if (dst
.regClass() == s1
) {
1029 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
1030 } else if (dst
.regClass() == s2
) {
1031 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
1033 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1034 nir_print_instr(&instr
->instr
, stderr
);
1035 fprintf(stderr
, "\n");
1040 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1041 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
1042 } else if (dst
.regClass() == v1
) {
1043 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
1044 } else if (dst
.regClass() == s1
) {
1045 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
1046 } else if (dst
.regClass() == s2
) {
1047 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
1049 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1050 nir_print_instr(&instr
->instr
, stderr
);
1051 fprintf(stderr
, "\n");
1056 if (dst
.regClass() == v1
) {
1057 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
1058 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1059 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
1060 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1061 } else if (dst
.regClass() == v2
) {
1062 bld
.vop3(aco_opcode::v_lshr_b64
, Definition(dst
),
1063 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1064 } else if (dst
.regClass() == s2
) {
1065 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
1066 } else if (dst
.regClass() == s1
) {
1067 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
1069 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1070 nir_print_instr(&instr
->instr
, stderr
);
1071 fprintf(stderr
, "\n");
1076 if (dst
.regClass() == v1
) {
1077 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
1078 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1079 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
1080 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1081 } else if (dst
.regClass() == v2
) {
1082 bld
.vop3(aco_opcode::v_lshl_b64
, Definition(dst
),
1083 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1084 } else if (dst
.regClass() == s1
) {
1085 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
1086 } else if (dst
.regClass() == s2
) {
1087 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
1089 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1090 nir_print_instr(&instr
->instr
, stderr
);
1091 fprintf(stderr
, "\n");
1096 if (dst
.regClass() == v1
) {
1097 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
1098 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1099 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
1100 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1101 } else if (dst
.regClass() == v2
) {
1102 bld
.vop3(aco_opcode::v_ashr_i64
, Definition(dst
),
1103 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1104 } else if (dst
.regClass() == s1
) {
1105 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
1106 } else if (dst
.regClass() == s2
) {
1107 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
1109 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1110 nir_print_instr(&instr
->instr
, stderr
);
1111 fprintf(stderr
, "\n");
1115 case nir_op_find_lsb
: {
1116 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1117 if (src
.regClass() == s1
) {
1118 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
1119 } else if (src
.regClass() == v1
) {
1120 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
1121 } else if (src
.regClass() == s2
) {
1122 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
1124 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1125 nir_print_instr(&instr
->instr
, stderr
);
1126 fprintf(stderr
, "\n");
1130 case nir_op_ufind_msb
:
1131 case nir_op_ifind_msb
: {
1132 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1133 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
1134 aco_opcode op
= src
.regClass() == s2
?
1135 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
1136 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
1137 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
1139 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
1140 Operand(src
.size() * 32u - 1u), msb_rev
);
1141 Temp msb
= sub
.def(0).getTemp();
1142 Temp carry
= sub
.def(1).getTemp();
1144 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, bld
.scc(carry
));
1145 } else if (src
.regClass() == v1
) {
1146 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
1147 Temp msb_rev
= bld
.tmp(v1
);
1148 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
1149 Temp msb
= bld
.tmp(v1
);
1150 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
1151 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
1153 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1154 nir_print_instr(&instr
->instr
, stderr
);
1155 fprintf(stderr
, "\n");
1159 case nir_op_bitfield_reverse
: {
1160 if (dst
.regClass() == s1
) {
1161 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1162 } else if (dst
.regClass() == v1
) {
1163 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1165 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1166 nir_print_instr(&instr
->instr
, stderr
);
1167 fprintf(stderr
, "\n");
1172 if (dst
.regClass() == s1
) {
1173 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1177 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1178 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1179 if (dst
.regClass() == v1
) {
1180 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1184 assert(src0
.size() == 2 && src1
.size() == 2);
1185 Temp src00
= bld
.tmp(src0
.type(), 1);
1186 Temp src01
= bld
.tmp(dst
.type(), 1);
1187 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1188 Temp src10
= bld
.tmp(src1
.type(), 1);
1189 Temp src11
= bld
.tmp(dst
.type(), 1);
1190 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1192 if (dst
.regClass() == s2
) {
1193 Temp carry
= bld
.tmp(s1
);
1194 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1195 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1196 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1197 } else if (dst
.regClass() == v2
) {
1198 Temp dst0
= bld
.tmp(v1
);
1199 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1200 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1201 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1203 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1204 nir_print_instr(&instr
->instr
, stderr
);
1205 fprintf(stderr
, "\n");
1209 case nir_op_uadd_sat
: {
1210 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1211 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1212 if (dst
.regClass() == s1
) {
1213 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1214 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1216 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1217 } else if (dst
.regClass() == v1
) {
1218 if (ctx
->options
->chip_class
>= GFX9
) {
1219 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1220 add
->operands
[0] = Operand(src0
);
1221 add
->operands
[1] = Operand(src1
);
1222 add
->definitions
[0] = Definition(dst
);
1224 ctx
->block
->instructions
.emplace_back(std::move(add
));
1226 if (src1
.regClass() != v1
)
1227 std::swap(src0
, src1
);
1228 assert(src1
.regClass() == v1
);
1229 Temp tmp
= bld
.tmp(v1
);
1230 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1231 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1234 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1235 nir_print_instr(&instr
->instr
, stderr
);
1236 fprintf(stderr
, "\n");
1240 case nir_op_uadd_carry
: {
1241 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1242 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1243 if (dst
.regClass() == s1
) {
1244 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1247 if (dst
.regClass() == v1
) {
1248 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1249 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1253 Temp src00
= bld
.tmp(src0
.type(), 1);
1254 Temp src01
= bld
.tmp(dst
.type(), 1);
1255 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1256 Temp src10
= bld
.tmp(src1
.type(), 1);
1257 Temp src11
= bld
.tmp(dst
.type(), 1);
1258 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1259 if (dst
.regClass() == s2
) {
1260 Temp carry
= bld
.tmp(s1
);
1261 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1262 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1263 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1264 } else if (dst
.regClass() == v2
) {
1265 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1266 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1267 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1268 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1270 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1271 nir_print_instr(&instr
->instr
, stderr
);
1272 fprintf(stderr
, "\n");
1277 if (dst
.regClass() == s1
) {
1278 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1282 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1283 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1284 if (dst
.regClass() == v1
) {
1285 bld
.vsub32(Definition(dst
), src0
, src1
);
1289 Temp src00
= bld
.tmp(src0
.type(), 1);
1290 Temp src01
= bld
.tmp(dst
.type(), 1);
1291 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1292 Temp src10
= bld
.tmp(src1
.type(), 1);
1293 Temp src11
= bld
.tmp(dst
.type(), 1);
1294 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1295 if (dst
.regClass() == s2
) {
1296 Temp carry
= bld
.tmp(s1
);
1297 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1298 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1299 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1300 } else if (dst
.regClass() == v2
) {
1301 Temp lower
= bld
.tmp(v1
);
1302 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1303 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1304 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1306 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1307 nir_print_instr(&instr
->instr
, stderr
);
1308 fprintf(stderr
, "\n");
1312 case nir_op_usub_borrow
: {
1313 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1314 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1315 if (dst
.regClass() == s1
) {
1316 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1318 } else if (dst
.regClass() == v1
) {
1319 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1320 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1324 Temp src00
= bld
.tmp(src0
.type(), 1);
1325 Temp src01
= bld
.tmp(dst
.type(), 1);
1326 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1327 Temp src10
= bld
.tmp(src1
.type(), 1);
1328 Temp src11
= bld
.tmp(dst
.type(), 1);
1329 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1330 if (dst
.regClass() == s2
) {
1331 Temp borrow
= bld
.tmp(s1
);
1332 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1333 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1334 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1335 } else if (dst
.regClass() == v2
) {
1336 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1337 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1338 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1339 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1341 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1342 nir_print_instr(&instr
->instr
, stderr
);
1343 fprintf(stderr
, "\n");
1348 if (dst
.regClass() == v1
) {
1349 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1350 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1351 } else if (dst
.regClass() == s1
) {
1352 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1354 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1355 nir_print_instr(&instr
->instr
, stderr
);
1356 fprintf(stderr
, "\n");
1360 case nir_op_umul_high
: {
1361 if (dst
.regClass() == v1
) {
1362 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1363 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1364 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1365 } else if (dst
.regClass() == s1
) {
1366 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1367 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1368 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1370 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1371 nir_print_instr(&instr
->instr
, stderr
);
1372 fprintf(stderr
, "\n");
1376 case nir_op_imul_high
: {
1377 if (dst
.regClass() == v1
) {
1378 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1379 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1380 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1381 } else if (dst
.regClass() == s1
) {
1382 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1383 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1384 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1386 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1387 nir_print_instr(&instr
->instr
, stderr
);
1388 fprintf(stderr
, "\n");
1393 if (dst
.size() == 1) {
1394 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1395 } else if (dst
.size() == 2) {
1396 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1397 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1399 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1400 nir_print_instr(&instr
->instr
, stderr
);
1401 fprintf(stderr
, "\n");
1406 if (dst
.size() == 1) {
1407 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1408 } else if (dst
.size() == 2) {
1409 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1410 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1412 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1413 nir_print_instr(&instr
->instr
, stderr
);
1414 fprintf(stderr
, "\n");
1419 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1420 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1421 if (dst
.size() == 1) {
1422 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1423 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1425 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1426 } else if (dst
.size() == 2) {
1427 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1428 get_alu_src(ctx
, instr
->src
[0]),
1429 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1430 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1433 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1434 nir_print_instr(&instr
->instr
, stderr
);
1435 fprintf(stderr
, "\n");
1440 if (dst
.size() == 1) {
1441 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true, false, ctx
->block
->fp_mode
.must_flush_denorms32
);
1442 } else if (dst
.size() == 2) {
1443 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
&& ctx
->program
->chip_class
< GFX9
) {
1444 Temp tmp
= bld
.vop3(aco_opcode::v_max_f64
, bld
.def(v2
),
1445 get_alu_src(ctx
, instr
->src
[0]),
1446 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1447 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), Operand(0x3FF0000000000000lu
), tmp
);
1449 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1450 get_alu_src(ctx
, instr
->src
[0]),
1451 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1454 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1455 nir_print_instr(&instr
->instr
, stderr
);
1456 fprintf(stderr
, "\n");
1461 if (dst
.size() == 1) {
1462 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true, false, ctx
->block
->fp_mode
.must_flush_denorms32
);
1463 } else if (dst
.size() == 2) {
1464 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
&& ctx
->program
->chip_class
< GFX9
) {
1465 Temp tmp
= bld
.vop3(aco_opcode::v_min_f64
, bld
.def(v2
),
1466 get_alu_src(ctx
, instr
->src
[0]),
1467 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1468 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), Operand(0x3FF0000000000000lu
), tmp
);
1470 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1471 get_alu_src(ctx
, instr
->src
[0]),
1472 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1475 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1476 nir_print_instr(&instr
->instr
, stderr
);
1477 fprintf(stderr
, "\n");
1481 case nir_op_fmax3
: {
1482 if (dst
.size() == 1) {
1483 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1485 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1486 nir_print_instr(&instr
->instr
, stderr
);
1487 fprintf(stderr
, "\n");
1491 case nir_op_fmin3
: {
1492 if (dst
.size() == 1) {
1493 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1495 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1496 nir_print_instr(&instr
->instr
, stderr
);
1497 fprintf(stderr
, "\n");
1501 case nir_op_fmed3
: {
1502 if (dst
.size() == 1) {
1503 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1505 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1506 nir_print_instr(&instr
->instr
, stderr
);
1507 fprintf(stderr
, "\n");
1511 case nir_op_umax3
: {
1512 if (dst
.size() == 1) {
1513 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1515 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1516 nir_print_instr(&instr
->instr
, stderr
);
1517 fprintf(stderr
, "\n");
1521 case nir_op_umin3
: {
1522 if (dst
.size() == 1) {
1523 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1525 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1526 nir_print_instr(&instr
->instr
, stderr
);
1527 fprintf(stderr
, "\n");
1531 case nir_op_umed3
: {
1532 if (dst
.size() == 1) {
1533 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1535 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1536 nir_print_instr(&instr
->instr
, stderr
);
1537 fprintf(stderr
, "\n");
1541 case nir_op_imax3
: {
1542 if (dst
.size() == 1) {
1543 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1545 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1546 nir_print_instr(&instr
->instr
, stderr
);
1547 fprintf(stderr
, "\n");
1551 case nir_op_imin3
: {
1552 if (dst
.size() == 1) {
1553 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1555 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1556 nir_print_instr(&instr
->instr
, stderr
);
1557 fprintf(stderr
, "\n");
1561 case nir_op_imed3
: {
1562 if (dst
.size() == 1) {
1563 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1565 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1566 nir_print_instr(&instr
->instr
, stderr
);
1567 fprintf(stderr
, "\n");
1571 case nir_op_cube_face_coord
: {
1572 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1573 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1574 emit_extract_vector(ctx
, in
, 1, v1
),
1575 emit_extract_vector(ctx
, in
, 2, v1
) };
1576 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1577 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1578 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1579 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1580 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1581 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1582 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1585 case nir_op_cube_face_index
: {
1586 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1587 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1588 emit_extract_vector(ctx
, in
, 1, v1
),
1589 emit_extract_vector(ctx
, in
, 2, v1
) };
1590 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1593 case nir_op_bcsel
: {
1594 emit_bcsel(ctx
, instr
, dst
);
1598 if (dst
.size() == 1) {
1599 emit_rsq(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1600 } else if (dst
.size() == 2) {
1601 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1603 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1604 nir_print_instr(&instr
->instr
, stderr
);
1605 fprintf(stderr
, "\n");
1610 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1611 if (dst
.size() == 1) {
1612 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1613 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1614 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1615 } else if (dst
.size() == 2) {
1616 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1617 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1618 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1619 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1620 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1621 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1623 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1624 nir_print_instr(&instr
->instr
, stderr
);
1625 fprintf(stderr
, "\n");
1630 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1631 if (dst
.size() == 1) {
1632 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1633 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1634 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1635 } else if (dst
.size() == 2) {
1636 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1637 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1638 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1639 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1640 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1641 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1643 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1644 nir_print_instr(&instr
->instr
, stderr
);
1645 fprintf(stderr
, "\n");
1650 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1651 if (dst
.size() == 1) {
1652 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1653 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1654 // TODO: confirm that this holds under any circumstances
1655 } else if (dst
.size() == 2) {
1656 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1657 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1660 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1661 nir_print_instr(&instr
->instr
, stderr
);
1662 fprintf(stderr
, "\n");
1666 case nir_op_flog2
: {
1667 if (dst
.size() == 1) {
1668 emit_log2(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1670 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1671 nir_print_instr(&instr
->instr
, stderr
);
1672 fprintf(stderr
, "\n");
1677 if (dst
.size() == 1) {
1678 emit_rcp(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1679 } else if (dst
.size() == 2) {
1680 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1682 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1683 nir_print_instr(&instr
->instr
, stderr
);
1684 fprintf(stderr
, "\n");
1688 case nir_op_fexp2
: {
1689 if (dst
.size() == 1) {
1690 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1692 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1693 nir_print_instr(&instr
->instr
, stderr
);
1694 fprintf(stderr
, "\n");
1698 case nir_op_fsqrt
: {
1699 if (dst
.size() == 1) {
1700 emit_sqrt(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1701 } else if (dst
.size() == 2) {
1702 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1704 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1705 nir_print_instr(&instr
->instr
, stderr
);
1706 fprintf(stderr
, "\n");
1710 case nir_op_ffract
: {
1711 if (dst
.size() == 1) {
1712 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1713 } else if (dst
.size() == 2) {
1714 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1716 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1717 nir_print_instr(&instr
->instr
, stderr
);
1718 fprintf(stderr
, "\n");
1722 case nir_op_ffloor
: {
1723 if (dst
.size() == 1) {
1724 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1725 } else if (dst
.size() == 2) {
1726 emit_floor_f64(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1728 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1729 nir_print_instr(&instr
->instr
, stderr
);
1730 fprintf(stderr
, "\n");
1734 case nir_op_fceil
: {
1735 if (dst
.size() == 1) {
1736 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1737 } else if (dst
.size() == 2) {
1738 if (ctx
->options
->chip_class
>= GFX7
) {
1739 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1741 /* GFX6 doesn't support V_CEIL_F64, lower it. */
1742 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1744 /* trunc = trunc(src0)
1745 * if (src0 > 0.0 && src0 != trunc)
1748 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src0
);
1749 Temp tmp0
= bld
.vopc_e64(aco_opcode::v_cmp_gt_f64
, bld
.def(bld
.lm
), src0
, Operand(0u));
1750 Temp tmp1
= bld
.vopc(aco_opcode::v_cmp_lg_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, trunc
);
1751 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.hint_vcc(bld
.def(s2
)), bld
.def(s1
, scc
), tmp0
, tmp1
);
1752 Temp add
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), bld
.copy(bld
.def(v1
), Operand(0u)), bld
.copy(bld
.def(v1
), Operand(0x3ff00000u
)), cond
);
1753 add
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), bld
.copy(bld
.def(v1
), Operand(0u)), add
);
1754 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), trunc
, add
);
1757 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1758 nir_print_instr(&instr
->instr
, stderr
);
1759 fprintf(stderr
, "\n");
1763 case nir_op_ftrunc
: {
1764 if (dst
.size() == 1) {
1765 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1766 } else if (dst
.size() == 2) {
1767 emit_trunc_f64(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1769 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1770 nir_print_instr(&instr
->instr
, stderr
);
1771 fprintf(stderr
, "\n");
1775 case nir_op_fround_even
: {
1776 if (dst
.size() == 1) {
1777 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1778 } else if (dst
.size() == 2) {
1779 if (ctx
->options
->chip_class
>= GFX7
) {
1780 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1782 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
1783 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1785 Temp src0_lo
= bld
.tmp(v1
), src0_hi
= bld
.tmp(v1
);
1786 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0_lo
), Definition(src0_hi
), src0
);
1788 Temp bitmask
= bld
.sop1(aco_opcode::s_brev_b32
, bld
.def(s1
), bld
.copy(bld
.def(s1
), Operand(-2u)));
1789 Temp bfi
= bld
.vop3(aco_opcode::v_bfi_b32
, bld
.def(v1
), bitmask
, bld
.copy(bld
.def(v1
), Operand(0x43300000u
)), as_vgpr(ctx
, src0_hi
));
1790 Temp tmp
= bld
.vop3(aco_opcode::v_add_f64
, bld
.def(v2
), src0
, bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), bfi
));
1791 Instruction
*sub
= bld
.vop3(aco_opcode::v_add_f64
, bld
.def(v2
), tmp
, bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), bfi
));
1792 static_cast<VOP3A_instruction
*>(sub
)->neg
[1] = true;
1793 tmp
= sub
->definitions
[0].getTemp();
1795 Temp v
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(-1u), Operand(0x432fffffu
));
1796 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_gt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, v
);
1797 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
1798 Temp cond
= vop3
->definitions
[0].getTemp();
1800 Temp tmp_lo
= bld
.tmp(v1
), tmp_hi
= bld
.tmp(v1
);
1801 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp_lo
), Definition(tmp_hi
), tmp
);
1802 Temp dst0
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp_lo
, as_vgpr(ctx
, src0_lo
), cond
);
1803 Temp dst1
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp_hi
, as_vgpr(ctx
, src0_hi
), cond
);
1805 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1808 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1809 nir_print_instr(&instr
->instr
, stderr
);
1810 fprintf(stderr
, "\n");
1816 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1817 aco_ptr
<Instruction
> norm
;
1818 if (dst
.size() == 1) {
1819 Temp half_pi
= bld
.copy(bld
.def(s1
), Operand(0x3e22f983u
));
1820 Temp tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, as_vgpr(ctx
, src
));
1822 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1823 if (ctx
->options
->chip_class
< GFX9
)
1824 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1826 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1827 bld
.vop1(opcode
, Definition(dst
), tmp
);
1829 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1830 nir_print_instr(&instr
->instr
, stderr
);
1831 fprintf(stderr
, "\n");
1835 case nir_op_ldexp
: {
1836 if (dst
.size() == 1) {
1837 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1838 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1839 get_alu_src(ctx
, instr
->src
[1]));
1840 } else if (dst
.size() == 2) {
1841 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1842 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1843 get_alu_src(ctx
, instr
->src
[1]));
1845 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1846 nir_print_instr(&instr
->instr
, stderr
);
1847 fprintf(stderr
, "\n");
1851 case nir_op_frexp_sig
: {
1852 if (dst
.size() == 1) {
1853 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1854 get_alu_src(ctx
, instr
->src
[0]));
1855 } else if (dst
.size() == 2) {
1856 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1857 get_alu_src(ctx
, instr
->src
[0]));
1859 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1860 nir_print_instr(&instr
->instr
, stderr
);
1861 fprintf(stderr
, "\n");
1865 case nir_op_frexp_exp
: {
1866 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1867 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1868 get_alu_src(ctx
, instr
->src
[0]));
1869 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1870 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1871 get_alu_src(ctx
, instr
->src
[0]));
1873 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1874 nir_print_instr(&instr
->instr
, stderr
);
1875 fprintf(stderr
, "\n");
1879 case nir_op_fsign
: {
1880 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1881 if (dst
.size() == 1) {
1882 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1883 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1884 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1885 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1886 } else if (dst
.size() == 2) {
1887 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1888 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1889 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, emit_extract_vector(ctx
, src
, 1, v1
), cond
);
1891 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1892 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1893 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1895 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1897 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1898 nir_print_instr(&instr
->instr
, stderr
);
1899 fprintf(stderr
, "\n");
1903 case nir_op_f2f32
: {
1904 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1905 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1907 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1908 nir_print_instr(&instr
->instr
, stderr
);
1909 fprintf(stderr
, "\n");
1913 case nir_op_f2f64
: {
1914 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1915 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1917 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1918 nir_print_instr(&instr
->instr
, stderr
);
1919 fprintf(stderr
, "\n");
1923 case nir_op_i2f32
: {
1924 assert(dst
.size() == 1);
1925 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1928 case nir_op_i2f64
: {
1929 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1930 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1931 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1932 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1933 RegClass rc
= RegClass(src
.type(), 1);
1934 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1935 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1936 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1937 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1938 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1939 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1942 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1943 nir_print_instr(&instr
->instr
, stderr
);
1944 fprintf(stderr
, "\n");
1948 case nir_op_u2f32
: {
1949 assert(dst
.size() == 1);
1950 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1953 case nir_op_u2f64
: {
1954 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1955 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1956 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1957 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1958 RegClass rc
= RegClass(src
.type(), 1);
1959 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1960 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1961 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1962 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1963 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1964 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1966 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1967 nir_print_instr(&instr
->instr
, stderr
);
1968 fprintf(stderr
, "\n");
1972 case nir_op_f2i32
: {
1973 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1974 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1975 if (dst
.type() == RegType::vgpr
)
1976 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1978 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1979 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1981 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1982 if (dst
.type() == RegType::vgpr
)
1983 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1985 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1986 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1989 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1990 nir_print_instr(&instr
->instr
, stderr
);
1991 fprintf(stderr
, "\n");
1995 case nir_op_f2u32
: {
1996 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1997 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1998 if (dst
.type() == RegType::vgpr
)
1999 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
2001 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
2002 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
2004 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2005 if (dst
.type() == RegType::vgpr
)
2006 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
2008 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
2009 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
2012 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2013 nir_print_instr(&instr
->instr
, stderr
);
2014 fprintf(stderr
, "\n");
2018 case nir_op_f2i64
: {
2019 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2020 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
2021 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
2022 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
2023 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
2024 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
2025 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
2026 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
2027 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
2028 Temp new_exponent
= bld
.tmp(v1
);
2029 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
2030 if (ctx
->program
->chip_class
>= GFX8
)
2031 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
2033 mantissa
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), mantissa
, new_exponent
);
2034 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
2035 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
2036 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2037 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
2038 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
2039 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
2040 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
2041 Temp new_lower
= bld
.tmp(v1
);
2042 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
2043 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
2044 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
2046 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
2047 if (src
.type() == RegType::vgpr
)
2048 src
= bld
.as_uniform(src
);
2049 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
2050 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
2051 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
2052 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
2053 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
2054 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
2055 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
2056 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
2057 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
2058 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
2059 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
2060 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
2061 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
2062 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
2063 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
2064 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2065 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
2066 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
2067 Temp borrow
= bld
.tmp(s1
);
2068 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
2069 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
2070 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2072 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2073 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
2074 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src
);
2075 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
2076 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
2077 Temp floor
= emit_floor_f64(ctx
, bld
, bld
.def(v2
), mul
);
2078 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
2079 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
2080 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
2081 if (dst
.type() == RegType::sgpr
) {
2082 lower
= bld
.as_uniform(lower
);
2083 upper
= bld
.as_uniform(upper
);
2085 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2088 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2089 nir_print_instr(&instr
->instr
, stderr
);
2090 fprintf(stderr
, "\n");
2094 case nir_op_f2u64
: {
2095 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2096 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
2097 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
2098 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(64u), exponent
);
2099 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
2100 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
2101 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
2102 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
2103 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
2104 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
2105 Temp new_exponent
= bld
.tmp(v1
);
2106 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
2107 if (ctx
->program
->chip_class
>= GFX8
)
2108 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
2110 mantissa
= bld
.vop3(aco_opcode::v_lshl_b64
, bld
.def(v2
), mantissa
, new_exponent
);
2111 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
2112 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2113 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
2114 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
2115 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
2116 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
2117 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2119 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
2120 if (src
.type() == RegType::vgpr
)
2121 src
= bld
.as_uniform(src
);
2122 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
2123 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
2124 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
2125 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
2126 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
2127 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
2128 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
2129 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
2130 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
2131 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
2132 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
2133 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
2134 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
2135 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2136 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
2137 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
2138 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
2139 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2141 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2142 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
2143 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src
);
2144 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
2145 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
2146 Temp floor
= emit_floor_f64(ctx
, bld
, bld
.def(v2
), mul
);
2147 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
2148 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
2149 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
2150 if (dst
.type() == RegType::sgpr
) {
2151 lower
= bld
.as_uniform(lower
);
2152 upper
= bld
.as_uniform(upper
);
2154 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2157 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2158 nir_print_instr(&instr
->instr
, stderr
);
2159 fprintf(stderr
, "\n");
2163 case nir_op_b2f32
: {
2164 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2165 assert(src
.regClass() == bld
.lm
);
2167 if (dst
.regClass() == s1
) {
2168 src
= bool_to_scalar_condition(ctx
, src
);
2169 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
2170 } else if (dst
.regClass() == v1
) {
2171 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
2173 unreachable("Wrong destination register class for nir_op_b2f32.");
2177 case nir_op_b2f64
: {
2178 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2179 assert(src
.regClass() == bld
.lm
);
2181 if (dst
.regClass() == s2
) {
2182 src
= bool_to_scalar_condition(ctx
, src
);
2183 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
2184 } else if (dst
.regClass() == v2
) {
2185 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
2186 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
, src
);
2187 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
2189 unreachable("Wrong destination register class for nir_op_b2f64.");
2193 case nir_op_i2i32
: {
2194 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2195 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2196 /* we can actually just say dst = src, as it would map the lower register */
2197 emit_extract_vector(ctx
, src
, 0, dst
);
2199 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2200 nir_print_instr(&instr
->instr
, stderr
);
2201 fprintf(stderr
, "\n");
2205 case nir_op_u2u32
: {
2206 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2207 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2208 if (dst
.regClass() == s1
) {
2209 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
2211 // TODO: do better with SDWA
2212 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
2214 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2215 /* we can actually just say dst = src, as it would map the lower register */
2216 emit_extract_vector(ctx
, src
, 0, dst
);
2218 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2219 nir_print_instr(&instr
->instr
, stderr
);
2220 fprintf(stderr
, "\n");
2224 case nir_op_i2i64
: {
2225 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2226 if (src
.regClass() == s1
) {
2227 Temp high
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
2228 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, high
);
2229 } else if (src
.regClass() == v1
) {
2230 Temp high
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
2231 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, high
);
2233 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2234 nir_print_instr(&instr
->instr
, stderr
);
2235 fprintf(stderr
, "\n");
2239 case nir_op_u2u64
: {
2240 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2241 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2242 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
2244 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2245 nir_print_instr(&instr
->instr
, stderr
);
2246 fprintf(stderr
, "\n");
2250 case nir_op_b2i32
: {
2251 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2252 assert(src
.regClass() == bld
.lm
);
2254 if (dst
.regClass() == s1
) {
2255 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2256 bool_to_scalar_condition(ctx
, src
, dst
);
2257 } else if (dst
.regClass() == v1
) {
2258 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2260 unreachable("Invalid register class for b2i32");
2265 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2266 assert(dst
.regClass() == bld
.lm
);
2268 if (src
.type() == RegType::vgpr
) {
2269 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2270 assert(dst
.regClass() == bld
.lm
);
2271 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2272 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2274 assert(src
.regClass() == s1
|| src
.regClass() == s2
);
2276 if (src
.regClass() == s2
&& ctx
->program
->chip_class
<= GFX7
) {
2277 tmp
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0u), src
).def(1).getTemp();
2279 tmp
= bld
.sopc(src
.size() == 2 ? aco_opcode::s_cmp_lg_u64
: aco_opcode::s_cmp_lg_u32
,
2280 bld
.scc(bld
.def(s1
)), Operand(0u), src
);
2282 bool_to_vector_condition(ctx
, tmp
, dst
);
2286 case nir_op_pack_64_2x32_split
: {
2287 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2288 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2290 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2293 case nir_op_unpack_64_2x32_split_x
:
2294 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2296 case nir_op_unpack_64_2x32_split_y
:
2297 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2299 case nir_op_pack_half_2x16
: {
2300 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2302 if (dst
.regClass() == v1
) {
2303 Temp src0
= bld
.tmp(v1
);
2304 Temp src1
= bld
.tmp(v1
);
2305 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2306 if (!ctx
->block
->fp_mode
.care_about_round32
|| ctx
->block
->fp_mode
.round32
== fp_round_tz
)
2307 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2309 bld
.vop3(aco_opcode::v_cvt_pk_u16_u32
, Definition(dst
),
2310 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src0
),
2311 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src1
));
2313 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2314 nir_print_instr(&instr
->instr
, stderr
);
2315 fprintf(stderr
, "\n");
2319 case nir_op_unpack_half_2x16_split_x
: {
2320 if (dst
.regClass() == v1
) {
2321 Builder
bld(ctx
->program
, ctx
->block
);
2322 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2324 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2325 nir_print_instr(&instr
->instr
, stderr
);
2326 fprintf(stderr
, "\n");
2330 case nir_op_unpack_half_2x16_split_y
: {
2331 if (dst
.regClass() == v1
) {
2332 Builder
bld(ctx
->program
, ctx
->block
);
2333 /* TODO: use SDWA here */
2334 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2335 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2337 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2338 nir_print_instr(&instr
->instr
, stderr
);
2339 fprintf(stderr
, "\n");
2343 case nir_op_fquantize2f16
: {
2344 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2345 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), src
);
2348 if (ctx
->program
->chip_class
>= GFX8
) {
2349 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2350 cmp_res
= bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), f16
, mask
);
2351 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2353 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2354 * so compare the result and flush to 0 if it's smaller.
2356 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2357 Temp smallest
= bld
.copy(bld
.def(s1
), Operand(0x38800000u
));
2358 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), f32
, smallest
);
2359 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
2360 cmp_res
= vop3
->definitions
[0].getTemp();
2363 if (ctx
->block
->fp_mode
.preserve_signed_zero_inf_nan32
|| ctx
->program
->chip_class
< GFX8
) {
2364 Temp copysign_0
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0u), as_vgpr(ctx
, src
));
2365 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), copysign_0
, f32
, cmp_res
);
2367 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2372 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2373 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2375 if (dst
.regClass() == s1
) {
2376 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2377 } else if (dst
.regClass() == v1
) {
2378 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2380 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2381 nir_print_instr(&instr
->instr
, stderr
);
2382 fprintf(stderr
, "\n");
2386 case nir_op_bitfield_select
: {
2387 /* (mask & insert) | (~mask & base) */
2388 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2389 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2390 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2392 /* dst = (insert & bitmask) | (base & ~bitmask) */
2393 if (dst
.regClass() == s1
) {
2394 aco_ptr
<Instruction
> sop2
;
2395 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2396 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2398 if (const_insert
&& const_bitmask
) {
2399 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2401 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2402 lhs
= Operand(insert
);
2406 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2407 if (const_base
&& const_bitmask
) {
2408 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2410 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2411 rhs
= Operand(base
);
2414 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2416 } else if (dst
.regClass() == v1
) {
2417 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2418 base
= as_vgpr(ctx
, base
);
2419 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2420 insert
= as_vgpr(ctx
, insert
);
2422 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2425 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2426 nir_print_instr(&instr
->instr
, stderr
);
2427 fprintf(stderr
, "\n");
2433 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2434 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2435 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2437 if (dst
.type() == RegType::sgpr
) {
2439 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2440 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2441 if (const_offset
&& const_bits
) {
2442 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2443 extract
= Operand(const_extract
);
2447 width
= Operand(const_bits
->u32
<< 16);
2449 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2451 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2455 if (dst
.regClass() == s1
) {
2456 if (instr
->op
== nir_op_ubfe
)
2457 opcode
= aco_opcode::s_bfe_u32
;
2459 opcode
= aco_opcode::s_bfe_i32
;
2460 } else if (dst
.regClass() == s2
) {
2461 if (instr
->op
== nir_op_ubfe
)
2462 opcode
= aco_opcode::s_bfe_u64
;
2464 opcode
= aco_opcode::s_bfe_i64
;
2466 unreachable("Unsupported BFE bit size");
2469 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2473 if (dst
.regClass() == v1
) {
2474 if (instr
->op
== nir_op_ubfe
)
2475 opcode
= aco_opcode::v_bfe_u32
;
2477 opcode
= aco_opcode::v_bfe_i32
;
2479 unreachable("Unsupported BFE bit size");
2482 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2486 case nir_op_bit_count
: {
2487 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2488 if (src
.regClass() == s1
) {
2489 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2490 } else if (src
.regClass() == v1
) {
2491 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2492 } else if (src
.regClass() == v2
) {
2493 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2494 emit_extract_vector(ctx
, src
, 1, v1
),
2495 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2496 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2497 } else if (src
.regClass() == s2
) {
2498 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2500 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2501 nir_print_instr(&instr
->instr
, stderr
);
2502 fprintf(stderr
, "\n");
2507 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_f32
, aco_opcode::v_cmp_lt_f64
);
2511 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_f32
, aco_opcode::v_cmp_ge_f64
);
2515 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_f32
, aco_opcode::v_cmp_eq_f64
);
2519 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_neq_f32
, aco_opcode::v_cmp_neq_f64
);
2523 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_i32
, aco_opcode::v_cmp_lt_i64
, aco_opcode::s_cmp_lt_i32
);
2527 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_i32
, aco_opcode::v_cmp_ge_i64
, aco_opcode::s_cmp_ge_i32
);
2531 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2532 emit_boolean_logic(ctx
, instr
, Builder::s_xnor
, dst
);
2534 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_i32
, aco_opcode::v_cmp_eq_i64
, aco_opcode::s_cmp_eq_i32
,
2535 ctx
->program
->chip_class
>= GFX8
? aco_opcode::s_cmp_eq_u64
: aco_opcode::num_opcodes
);
2539 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2540 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
2542 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lg_i32
, aco_opcode::v_cmp_lg_i64
, aco_opcode::s_cmp_lg_i32
,
2543 ctx
->program
->chip_class
>= GFX8
? aco_opcode::s_cmp_lg_u64
: aco_opcode::num_opcodes
);
2547 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_u32
, aco_opcode::v_cmp_lt_u64
, aco_opcode::s_cmp_lt_u32
);
2551 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_u32
, aco_opcode::v_cmp_ge_u64
, aco_opcode::s_cmp_ge_u32
);
2556 case nir_op_fddx_fine
:
2557 case nir_op_fddy_fine
:
2558 case nir_op_fddx_coarse
:
2559 case nir_op_fddy_coarse
: {
2560 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2561 uint16_t dpp_ctrl1
, dpp_ctrl2
;
2562 if (instr
->op
== nir_op_fddx_fine
) {
2563 dpp_ctrl1
= dpp_quad_perm(0, 0, 2, 2);
2564 dpp_ctrl2
= dpp_quad_perm(1, 1, 3, 3);
2565 } else if (instr
->op
== nir_op_fddy_fine
) {
2566 dpp_ctrl1
= dpp_quad_perm(0, 1, 0, 1);
2567 dpp_ctrl2
= dpp_quad_perm(2, 3, 2, 3);
2569 dpp_ctrl1
= dpp_quad_perm(0, 0, 0, 0);
2570 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2571 dpp_ctrl2
= dpp_quad_perm(1, 1, 1, 1);
2573 dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
2577 if (ctx
->program
->chip_class
>= GFX8
) {
2578 Temp tl
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl1
);
2579 tmp
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), src
, tl
, dpp_ctrl2
);
2581 Temp tl
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl1
);
2582 Temp tr
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl2
);
2583 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), tr
, tl
);
2585 emit_wqm(ctx
, tmp
, dst
, true);
2589 fprintf(stderr
, "Unknown NIR ALU instr: ");
2590 nir_print_instr(&instr
->instr
, stderr
);
2591 fprintf(stderr
, "\n");
2595 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2597 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2599 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2600 // which get truncated the lsb if double and msb if int
2601 // for now, we only use s_mov_b64 with 64bit inline constants
2602 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2603 assert(dst
.type() == RegType::sgpr
);
2605 Builder
bld(ctx
->program
, ctx
->block
);
2607 if (instr
->def
.bit_size
== 1) {
2608 assert(dst
.regClass() == bld
.lm
);
2609 int val
= instr
->value
[0].b
? -1 : 0;
2610 Operand op
= bld
.lm
.size() == 1 ? Operand((uint32_t) val
) : Operand((uint64_t) val
);
2611 bld
.sop1(Builder::s_mov
, Definition(dst
), op
);
2612 } else if (dst
.size() == 1) {
2613 bld
.copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2615 assert(dst
.size() != 1);
2616 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2617 if (instr
->def
.bit_size
== 64)
2618 for (unsigned i
= 0; i
< dst
.size(); i
++)
2619 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2621 for (unsigned i
= 0; i
< dst
.size(); i
++)
2622 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2624 vec
->definitions
[0] = Definition(dst
);
2625 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2629 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2631 uint32_t new_mask
= 0;
2632 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2633 if (mask
& (1u << i
))
2634 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2638 Operand
load_lds_size_m0(isel_context
*ctx
)
2640 /* TODO: m0 does not need to be initialized on GFX9+ */
2641 Builder
bld(ctx
->program
, ctx
->block
);
2642 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
2645 void load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
2646 Temp address
, unsigned base_offset
, unsigned align
)
2648 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2650 Builder
bld(ctx
->program
, ctx
->block
);
2652 Operand m
= load_lds_size_m0(ctx
);
2654 unsigned num_components
= dst
.size() * 4u / elem_size_bytes
;
2655 unsigned bytes_read
= 0;
2656 unsigned result_size
= 0;
2657 unsigned total_bytes
= num_components
* elem_size_bytes
;
2658 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> result
;
2659 bool large_ds_read
= ctx
->options
->chip_class
>= GFX7
;
2661 while (bytes_read
< total_bytes
) {
2662 unsigned todo
= total_bytes
- bytes_read
;
2663 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2664 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2666 aco_opcode op
= aco_opcode::last_opcode
;
2668 if (todo
>= 16 && aligned16
&& large_ds_read
) {
2669 op
= aco_opcode::ds_read_b128
;
2671 } else if (todo
>= 16 && aligned8
) {
2672 op
= aco_opcode::ds_read2_b64
;
2675 } else if (todo
>= 12 && aligned16
&& large_ds_read
) {
2676 op
= aco_opcode::ds_read_b96
;
2678 } else if (todo
>= 8 && aligned8
) {
2679 op
= aco_opcode::ds_read_b64
;
2681 } else if (todo
>= 8) {
2682 op
= aco_opcode::ds_read2_b32
;
2685 } else if (todo
>= 4) {
2686 op
= aco_opcode::ds_read_b32
;
2691 assert(todo
% elem_size_bytes
== 0);
2692 unsigned num_elements
= todo
/ elem_size_bytes
;
2693 unsigned offset
= base_offset
+ bytes_read
;
2694 unsigned max_offset
= read2
? 1019 : 65535;
2696 Temp address_offset
= address
;
2697 if (offset
> max_offset
) {
2698 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2699 offset
= bytes_read
;
2701 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2704 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2707 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2710 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
2712 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2714 if (num_components
== 1) {
2715 assert(todo
== total_bytes
);
2716 if (dst
.type() == RegType::sgpr
)
2717 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2721 if (dst
.type() == RegType::sgpr
) {
2722 Temp new_res
= bld
.tmp(RegType::sgpr
, res
.size());
2723 expand_vector(ctx
, res
, new_res
, res
.size(), (1 << res
.size()) - 1);
2727 if (num_elements
== 1) {
2728 result
[result_size
++] = res
;
2730 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2731 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2732 split
->operands
[0] = Operand(res
);
2733 for (unsigned i
= 0; i
< num_elements
; i
++)
2734 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2735 ctx
->block
->instructions
.emplace_back(std::move(split
));
2741 assert(result_size
== num_components
&& result_size
> 1);
2742 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2743 for (unsigned i
= 0; i
< result_size
; i
++)
2744 vec
->operands
[i
] = Operand(result
[i
]);
2745 vec
->definitions
[0] = Definition(dst
);
2746 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2747 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2750 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2752 if (start
== 0 && size
== data
.size())
2753 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2755 unsigned size_hint
= 1;
2756 auto it
= ctx
->allocated_vec
.find(data
.id());
2757 if (it
!= ctx
->allocated_vec
.end())
2758 size_hint
= it
->second
[0].size();
2759 if (size
% size_hint
|| start
% size_hint
)
2766 for (unsigned i
= 0; i
< size
; i
++)
2767 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2770 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2772 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2773 for (unsigned i
= 0; i
< size
; i
++)
2774 vec
->operands
[i
] = Operand(elems
[i
]);
2775 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2776 vec
->definitions
[0] = Definition(res
);
2777 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2781 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
)
2783 Builder
bld(ctx
->program
, ctx
->block
);
2784 unsigned bytes_written
= 0;
2785 bool large_ds_write
= ctx
->options
->chip_class
>= GFX7
;
2787 while (bytes_written
< total_size
* 4) {
2788 unsigned todo
= total_size
* 4 - bytes_written
;
2789 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
2790 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
2792 aco_opcode op
= aco_opcode::last_opcode
;
2793 bool write2
= false;
2795 if (todo
>= 16 && aligned16
&& large_ds_write
) {
2796 op
= aco_opcode::ds_write_b128
;
2798 } else if (todo
>= 16 && aligned8
) {
2799 op
= aco_opcode::ds_write2_b64
;
2802 } else if (todo
>= 12 && aligned16
&& large_ds_write
) {
2803 op
= aco_opcode::ds_write_b96
;
2805 } else if (todo
>= 8 && aligned8
) {
2806 op
= aco_opcode::ds_write_b64
;
2808 } else if (todo
>= 8) {
2809 op
= aco_opcode::ds_write2_b32
;
2812 } else if (todo
>= 4) {
2813 op
= aco_opcode::ds_write_b32
;
2819 unsigned offset
= offset0
+ offset1
+ bytes_written
;
2820 unsigned max_offset
= write2
? 1020 : 65535;
2821 Temp address_offset
= address
;
2822 if (offset
> max_offset
) {
2823 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
2824 offset
= offset1
+ bytes_written
;
2826 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
2829 Temp val0
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
/ 2, RegType::vgpr
);
2830 Temp val1
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2) + 1, size
/ 2, RegType::vgpr
);
2831 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
2833 Temp val
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
, RegType::vgpr
);
2834 bld
.ds(op
, address_offset
, val
, m
, offset
);
2837 bytes_written
+= size
* 4;
2841 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
2842 Temp address
, unsigned base_offset
, unsigned align
)
2844 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2846 Operand m
= load_lds_size_m0(ctx
);
2848 /* we need at most two stores for 32bit variables */
2849 int start
[2], count
[2];
2850 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2851 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2852 assert(wrmask
== 0);
2854 /* one combined store is sufficient */
2855 if (count
[0] == count
[1]) {
2856 Builder
bld(ctx
->program
, ctx
->block
);
2858 Temp address_offset
= address
;
2859 if ((base_offset
>> 2) + start
[1] > 255) {
2860 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2864 assert(count
[0] == 1);
2865 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
2866 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
2867 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2868 base_offset
= base_offset
/ elem_size_bytes
;
2869 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2870 base_offset
+ start
[0], base_offset
+ start
[1]);
2874 for (unsigned i
= 0; i
< 2; i
++) {
2878 unsigned elem_size_words
= elem_size_bytes
/ 4;
2879 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2880 base_offset
, start
[i
] * elem_size_bytes
, align
);
2885 void visit_store_vsgs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2887 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2888 unsigned component
= nir_intrinsic_component(instr
);
2889 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2890 unsigned idx
= (nir_intrinsic_base(instr
) + component
) * 4u;
2892 Builder
bld(ctx
->program
, ctx
->block
);
2894 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2895 if (off_instr
->type
!= nir_instr_type_load_const
)
2896 offset
= bld
.v_mul24_imm(bld
.def(v1
), get_ssa_temp(ctx
, instr
->src
[1].ssa
), 16u);
2898 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 16u;
2900 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8u;
2901 if (ctx
->stage
== vertex_es
) {
2902 Temp esgs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_VS
* 16u));
2904 Temp elems
[NIR_MAX_VEC_COMPONENTS
* 2];
2905 if (elem_size_bytes
== 8) {
2906 for (unsigned i
= 0; i
< src
.size() / 2; i
++) {
2907 Temp elem
= emit_extract_vector(ctx
, src
, i
, v2
);
2908 elems
[i
*2] = bld
.tmp(v1
);
2909 elems
[i
*2+1] = bld
.tmp(v1
);
2910 bld
.pseudo(aco_opcode::p_split_vector
, Definition(elems
[i
*2]), Definition(elems
[i
*2+1]), elem
);
2912 write_mask
= widen_mask(write_mask
, 2);
2913 elem_size_bytes
/= 2u;
2915 for (unsigned i
= 0; i
< src
.size(); i
++)
2916 elems
[i
] = emit_extract_vector(ctx
, src
, i
, v1
);
2919 while (write_mask
) {
2920 unsigned index
= u_bit_scan(&write_mask
);
2921 unsigned offset
= index
* elem_size_bytes
;
2922 Temp elem
= emit_extract_vector(ctx
, src
, index
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
2924 Operand
vaddr_offset(v1
);
2925 unsigned const_offset
= idx
+ offset
;
2926 if (const_offset
>= 4096u) {
2927 vaddr_offset
= bld
.copy(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u));
2928 const_offset
%= 4096u;
2931 aco_ptr
<MTBUF_instruction
> mtbuf
{create_instruction
<MTBUF_instruction
>(aco_opcode::tbuffer_store_format_x
, Format::MTBUF
, 4, 0)};
2932 mtbuf
->operands
[0] = Operand(esgs_ring
);
2933 mtbuf
->operands
[1] = vaddr_offset
;
2934 mtbuf
->operands
[2] = Operand(get_arg(ctx
, ctx
->args
->es2gs_offset
));
2935 mtbuf
->operands
[3] = Operand(elem
);
2936 mtbuf
->offen
= !vaddr_offset
.isUndefined();
2937 mtbuf
->dfmt
= V_008F0C_BUF_DATA_FORMAT_32
;
2938 mtbuf
->nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2939 mtbuf
->offset
= const_offset
;
2942 mtbuf
->barrier
= barrier_none
;
2943 mtbuf
->can_reorder
= true;
2944 bld
.insert(std::move(mtbuf
));
2947 unsigned itemsize
= ctx
->program
->info
->vs
.es_info
.esgs_itemsize
;
2949 Temp vertex_idx
= emit_mbcnt(ctx
, bld
.def(v1
));
2950 Temp wave_idx
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(4u << 16 | 24));
2951 vertex_idx
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), vertex_idx
,
2952 bld
.v_mul24_imm(bld
.def(v1
), as_vgpr(ctx
, wave_idx
), ctx
->program
->wave_size
));
2954 Temp lds_base
= bld
.v_mul24_imm(bld
.def(v1
), vertex_idx
, itemsize
);
2955 if (!offset
.isUndefined())
2956 lds_base
= bld
.vadd32(bld
.def(v1
), offset
, lds_base
);
2958 unsigned align
= 1 << (ffs(itemsize
) - 1);
2960 align
= std::min(align
, 1u << (ffs(idx
) - 1));
2962 store_lds(ctx
, elem_size_bytes
, src
, write_mask
, lds_base
, idx
, align
);
2966 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2968 if (ctx
->stage
== vertex_vs
||
2969 ctx
->stage
== fragment_fs
||
2970 ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
2971 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2972 unsigned component
= nir_intrinsic_component(instr
);
2973 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2974 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2976 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2977 if (off_instr
->type
!= nir_instr_type_load_const
) {
2978 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2979 nir_print_instr(off_instr
, stderr
);
2980 fprintf(stderr
, "\n");
2982 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2984 if (instr
->src
[0].ssa
->bit_size
== 64)
2985 write_mask
= widen_mask(write_mask
, 2);
2987 for (unsigned i
= 0; i
< 8; ++i
) {
2988 if (write_mask
& (1 << i
)) {
2989 ctx
->outputs
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2990 ctx
->outputs
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2994 } else if (ctx
->stage
== vertex_es
||
2995 (ctx
->stage
== vertex_geometry_gs
&& ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
)) {
2996 visit_store_vsgs_output(ctx
, instr
);
2998 unreachable("Shader stage not implemented");
3002 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
3004 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
3005 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
3007 Builder
bld(ctx
->program
, ctx
->block
);
3008 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
3009 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
3012 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
3014 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
3015 for (unsigned i
= 0; i
< num_components
; i
++)
3016 vec
->operands
[i
] = Operand(get_arg(ctx
, ctx
->args
->ac
.frag_pos
[i
]));
3017 if (G_0286CC_POS_W_FLOAT_ENA(ctx
->program
->config
->spi_ps_input_ena
)) {
3018 assert(num_components
== 4);
3019 Builder
bld(ctx
->program
, ctx
->block
);
3020 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->ac
.frag_pos
[3]));
3023 for (Operand
& op
: vec
->operands
)
3024 op
= op
.isUndefined() ? Operand(0u) : op
;
3026 vec
->definitions
[0] = Definition(dst
);
3027 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3028 emit_split_vector(ctx
, dst
, num_components
);
3032 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3034 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3035 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3036 unsigned idx
= nir_intrinsic_base(instr
);
3037 unsigned component
= nir_intrinsic_component(instr
);
3038 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
3040 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
3042 assert(offset
->u32
== 0);
3044 /* the lower 15bit of the prim_mask contain the offset into LDS
3045 * while the upper bits contain the number of prims */
3046 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3047 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3048 Builder
bld(ctx
->program
, ctx
->block
);
3049 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3050 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3051 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3052 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3053 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3056 if (instr
->dest
.ssa
.num_components
== 1) {
3057 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
3059 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
3060 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
3062 Temp tmp
= {ctx
->program
->allocateId(), v1
};
3063 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
3064 vec
->operands
[i
] = Operand(tmp
);
3066 vec
->definitions
[0] = Definition(dst
);
3067 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3071 bool check_vertex_fetch_size(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
3072 unsigned offset
, unsigned stride
, unsigned channels
)
3074 unsigned vertex_byte_size
= vtx_info
->chan_byte_size
* channels
;
3075 if (vtx_info
->chan_byte_size
!= 4 && channels
== 3)
3077 return (ctx
->options
->chip_class
!= GFX6
&& ctx
->options
->chip_class
!= GFX10
) ||
3078 (offset
% vertex_byte_size
== 0 && stride
% vertex_byte_size
== 0);
3081 uint8_t get_fetch_data_format(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
3082 unsigned offset
, unsigned stride
, unsigned *channels
)
3084 if (!vtx_info
->chan_byte_size
) {
3085 *channels
= vtx_info
->num_channels
;
3086 return vtx_info
->chan_format
;
3089 unsigned num_channels
= *channels
;
3090 if (!check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, *channels
)) {
3091 unsigned new_channels
= num_channels
+ 1;
3092 /* first, assume more loads is worse and try using a larger data format */
3093 while (new_channels
<= 4 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
)) {
3095 /* don't make the attribute potentially out-of-bounds */
3096 if (offset
+ new_channels
* vtx_info
->chan_byte_size
> stride
)
3100 if (new_channels
== 5) {
3101 /* then try decreasing load size (at the cost of more loads) */
3102 new_channels
= *channels
;
3103 while (new_channels
> 1 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
))
3107 if (new_channels
< *channels
)
3108 *channels
= new_channels
;
3109 num_channels
= new_channels
;
3112 switch (vtx_info
->chan_format
) {
3113 case V_008F0C_BUF_DATA_FORMAT_8
:
3114 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8
, V_008F0C_BUF_DATA_FORMAT_8_8
,
3115 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_8_8_8_8
}[num_channels
- 1];
3116 case V_008F0C_BUF_DATA_FORMAT_16
:
3117 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16
, V_008F0C_BUF_DATA_FORMAT_16_16
,
3118 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_16_16_16_16
}[num_channels
- 1];
3119 case V_008F0C_BUF_DATA_FORMAT_32
:
3120 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32
, V_008F0C_BUF_DATA_FORMAT_32_32
,
3121 V_008F0C_BUF_DATA_FORMAT_32_32_32
, V_008F0C_BUF_DATA_FORMAT_32_32_32_32
}[num_channels
- 1];
3123 unreachable("shouldn't reach here");
3124 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
3127 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3128 * so we may need to fix it up. */
3129 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
3131 Builder
bld(ctx
->program
, ctx
->block
);
3133 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3134 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3136 /* For the integer-like cases, do a natural sign extension.
3138 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3139 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3142 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3143 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3145 /* Convert back to the right type. */
3146 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3147 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3148 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0xbf800000u
), alpha
);
3149 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3150 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3151 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3157 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3159 Builder
bld(ctx
->program
, ctx
->block
);
3160 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3161 if (ctx
->stage
& sw_vs
) {
3163 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3164 if (off_instr
->type
!= nir_instr_type_load_const
) {
3165 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3166 nir_print_instr(off_instr
, stderr
);
3167 fprintf(stderr
, "\n");
3169 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3171 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->vertex_buffers
));
3173 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3174 unsigned component
= nir_intrinsic_component(instr
);
3175 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3176 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3177 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3178 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3180 unsigned dfmt
= attrib_format
& 0xf;
3181 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3182 const struct ac_data_format_info
*vtx_info
= ac_get_data_format_info(dfmt
);
3184 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3185 unsigned num_channels
= MIN2(util_last_bit(mask
), vtx_info
->num_channels
);
3186 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3187 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3189 num_channels
= MAX2(num_channels
, 3);
3191 Operand off
= bld
.copy(bld
.def(s1
), Operand(attrib_binding
* 16u));
3192 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, off
);
3195 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3196 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3197 Temp start_instance
= get_arg(ctx
, ctx
->args
->ac
.start_instance
);
3199 Temp instance_id
= get_arg(ctx
, ctx
->args
->ac
.instance_id
);
3201 Temp divided
= bld
.tmp(v1
);
3202 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, instance_id
), divisor
);
3203 index
= bld
.vadd32(bld
.def(v1
), start_instance
, divided
);
3205 index
= bld
.vadd32(bld
.def(v1
), start_instance
, instance_id
);
3208 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), start_instance
);
3211 index
= bld
.vadd32(bld
.def(v1
),
3212 get_arg(ctx
, ctx
->args
->ac
.base_vertex
),
3213 get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
3216 Temp channels
[num_channels
];
3217 unsigned channel_start
= 0;
3218 bool direct_fetch
= false;
3220 /* skip unused channels at the start */
3221 if (vtx_info
->chan_byte_size
&& !post_shuffle
) {
3222 channel_start
= ffs(mask
) - 1;
3223 for (unsigned i
= 0; i
< channel_start
; i
++)
3224 channels
[i
] = Temp(0, s1
);
3225 } else if (vtx_info
->chan_byte_size
&& post_shuffle
&& !(mask
& 0x8)) {
3226 num_channels
= 3 - (ffs(mask
) - 1);
3230 while (channel_start
< num_channels
) {
3231 unsigned fetch_size
= num_channels
- channel_start
;
3232 unsigned fetch_offset
= attrib_offset
+ channel_start
* vtx_info
->chan_byte_size
;
3234 /* use MUBUF when possible to avoid possible alignment issues */
3235 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
3236 bool use_mubuf
= (nfmt
== V_008F0C_BUF_NUM_FORMAT_FLOAT
||
3237 nfmt
== V_008F0C_BUF_NUM_FORMAT_UINT
||
3238 nfmt
== V_008F0C_BUF_NUM_FORMAT_SINT
) &&
3239 vtx_info
->chan_byte_size
== 4;
3240 unsigned fetch_dfmt
= V_008F0C_BUF_DATA_FORMAT_INVALID
;
3242 fetch_dfmt
= get_fetch_data_format(ctx
, vtx_info
, fetch_offset
, attrib_stride
, &fetch_size
);
3244 if (fetch_size
== 3 && ctx
->options
->chip_class
== GFX6
) {
3245 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
3250 Temp fetch_index
= index
;
3251 if (attrib_stride
!= 0 && fetch_offset
> attrib_stride
) {
3252 fetch_index
= bld
.vadd32(bld
.def(v1
), Operand(fetch_offset
/ attrib_stride
), fetch_index
);
3253 fetch_offset
= fetch_offset
% attrib_stride
;
3256 Operand
soffset(0u);
3257 if (fetch_offset
>= 4096) {
3258 soffset
= bld
.copy(bld
.def(s1
), Operand(fetch_offset
/ 4096 * 4096));
3259 fetch_offset
%= 4096;
3263 switch (fetch_size
) {
3265 opcode
= use_mubuf
? aco_opcode::buffer_load_dword
: aco_opcode::tbuffer_load_format_x
;
3268 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx2
: aco_opcode::tbuffer_load_format_xy
;
3271 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx3
: aco_opcode::tbuffer_load_format_xyz
;
3274 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx4
: aco_opcode::tbuffer_load_format_xyzw
;
3277 unreachable("Unimplemented load_input vector size");
3281 if (channel_start
== 0 && fetch_size
== dst
.size() && !post_shuffle
&&
3282 (alpha_adjust
== RADV_ALPHA_ADJUST_NONE
|| num_channels
<= 3)) {
3283 direct_fetch
= true;
3286 fetch_dst
= bld
.tmp(RegType::vgpr
, fetch_size
);
3290 Instruction
*mubuf
= bld
.mubuf(opcode
,
3291 Definition(fetch_dst
), list
, fetch_index
, soffset
,
3292 fetch_offset
, false, true).instr
;
3293 static_cast<MUBUF_instruction
*>(mubuf
)->can_reorder
= true;
3295 Instruction
*mtbuf
= bld
.mtbuf(opcode
,
3296 Definition(fetch_dst
), list
, fetch_index
, soffset
,
3297 fetch_dfmt
, nfmt
, fetch_offset
, false, true).instr
;
3298 static_cast<MTBUF_instruction
*>(mtbuf
)->can_reorder
= true;
3301 emit_split_vector(ctx
, fetch_dst
, fetch_dst
.size());
3303 if (fetch_size
== 1) {
3304 channels
[channel_start
] = fetch_dst
;
3306 for (unsigned i
= 0; i
< MIN2(fetch_size
, num_channels
- channel_start
); i
++)
3307 channels
[channel_start
+ i
] = emit_extract_vector(ctx
, fetch_dst
, i
, v1
);
3310 channel_start
+= fetch_size
;
3313 if (!direct_fetch
) {
3314 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3315 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3317 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3318 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3319 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3321 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3322 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3323 unsigned num_temp
= 0;
3324 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3325 unsigned idx
= i
+ component
;
3326 if (swizzle
[idx
] < num_channels
&& channels
[swizzle
[idx
]].id()) {
3327 Temp channel
= channels
[swizzle
[idx
]];
3328 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
)
3329 channel
= adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, channel
);
3330 vec
->operands
[i
] = Operand(channel
);
3334 } else if (is_float
&& idx
== 3) {
3335 vec
->operands
[i
] = Operand(0x3f800000u
);
3336 } else if (!is_float
&& idx
== 3) {
3337 vec
->operands
[i
] = Operand(1u);
3339 vec
->operands
[i
] = Operand(0u);
3342 vec
->definitions
[0] = Definition(dst
);
3343 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3344 emit_split_vector(ctx
, dst
, dst
.size());
3346 if (num_temp
== dst
.size())
3347 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3349 } else if (ctx
->stage
== fragment_fs
) {
3350 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
3351 nir_instr
*off_instr
= instr
->src
[offset_idx
].ssa
->parent_instr
;
3352 if (off_instr
->type
!= nir_instr_type_load_const
||
3353 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3354 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3355 nir_print_instr(off_instr
, stderr
);
3356 fprintf(stderr
, "\n");
3359 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
3360 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
3362 assert(offset
->u32
== 0);
3364 /* the lower 15bit of the prim_mask contain the offset into LDS
3365 * while the upper bits contain the number of prims */
3366 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[offset_idx
].ssa
);
3367 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3368 Builder
bld(ctx
->program
, ctx
->block
);
3369 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3370 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3371 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3372 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3373 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3376 unsigned idx
= nir_intrinsic_base(instr
);
3377 unsigned component
= nir_intrinsic_component(instr
);
3378 unsigned vertex_id
= 2; /* P0 */
3380 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3381 nir_const_value
* src0
= nir_src_as_const_value(instr
->src
[0]);
3382 switch (src0
->u32
) {
3384 vertex_id
= 2; /* P0 */
3387 vertex_id
= 0; /* P10 */
3390 vertex_id
= 1; /* P20 */
3393 unreachable("invalid vertex index");
3397 if (dst
.size() == 1) {
3398 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
);
3400 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3401 for (unsigned i
= 0; i
< dst
.size(); i
++)
3402 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
+ i
);
3403 vec
->definitions
[0] = Definition(dst
);
3404 bld
.insert(std::move(vec
));
3408 unreachable("Shader stage not implemented");
3412 void visit_load_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3414 assert(ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== geometry_gs
);
3415 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3417 Builder
bld(ctx
->program
, ctx
->block
);
3418 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3420 Temp offset
= Temp();
3421 if (instr
->src
[0].ssa
->parent_instr
->type
!= nir_instr_type_load_const
) {
3422 /* better code could be created, but this case probably doesn't happen
3423 * much in practice */
3424 Temp indirect_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3425 for (unsigned i
= 0; i
< ctx
->shader
->info
.gs
.vertices_in
; i
++) {
3427 if (ctx
->stage
== vertex_geometry_gs
) {
3428 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
/ 2u * 2u]);
3430 elem
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), elem
);
3432 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
]);
3435 Temp cond
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(s2
)),
3436 Operand(i
), indirect_vertex
);
3437 offset
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), offset
, elem
, cond
);
3442 if (ctx
->stage
== vertex_geometry_gs
)
3443 offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
), offset
);
3445 unsigned vertex
= nir_src_as_uint(instr
->src
[0]);
3446 if (ctx
->stage
== vertex_geometry_gs
)
3448 aco_opcode::v_bfe_u32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
/ 2u * 2u]),
3449 Operand((vertex
% 2u) * 16u), Operand(16u));
3451 offset
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
]);
3454 unsigned const_offset
= nir_intrinsic_base(instr
);
3455 const_offset
+= nir_intrinsic_component(instr
);
3457 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
3458 if (off_instr
->type
!= nir_instr_type_load_const
) {
3459 Temp indirect_offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3460 offset
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u),
3461 bld
.vadd32(bld
.def(v1
), indirect_offset
, offset
));
3463 const_offset
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
3467 offset
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), offset
);
3469 unsigned itemsize
= ctx
->program
->info
->vs
.es_info
.esgs_itemsize
;
3471 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
3472 if (ctx
->stage
== geometry_gs
) {
3473 Temp esgs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_GS
* 16u));
3475 const_offset
*= ctx
->program
->wave_size
;
3477 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3478 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(
3479 aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1)};
3480 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++) {
3482 for (unsigned j
= 0; j
< elem_size_bytes
/ 4; j
++) {
3483 Operand
soffset(0u);
3484 if (const_offset
>= 4096u)
3485 soffset
= bld
.copy(bld
.def(s1
), Operand(const_offset
/ 4096u * 4096u));
3487 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dword
, Format::MUBUF
, 3, 1)};
3488 mubuf
->definitions
[0] = bld
.def(v1
);
3489 subelems
[j
] = mubuf
->definitions
[0].getTemp();
3490 mubuf
->operands
[0] = Operand(esgs_ring
);
3491 mubuf
->operands
[1] = Operand(offset
);
3492 mubuf
->operands
[2] = Operand(soffset
);
3493 mubuf
->offen
= true;
3494 mubuf
->offset
= const_offset
% 4096u;
3496 mubuf
->dlc
= ctx
->options
->chip_class
>= GFX10
;
3497 mubuf
->barrier
= barrier_none
;
3498 mubuf
->can_reorder
= true;
3499 bld
.insert(std::move(mubuf
));
3501 const_offset
+= ctx
->program
->wave_size
* 4u;
3504 if (elem_size_bytes
== 4)
3505 elems
[i
] = subelems
[0];
3507 elems
[i
] = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), subelems
[0], subelems
[1]);
3508 vec
->operands
[i
] = Operand(elems
[i
]);
3510 vec
->definitions
[0] = Definition(dst
);
3511 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3512 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3514 unsigned align
= 16; /* alignment of indirect offset */
3515 align
= std::min(align
, 1u << (ffs(itemsize
) - 1));
3517 align
= std::min(align
, 1u << (ffs(const_offset
) - 1));
3519 load_lds(ctx
, elem_size_bytes
, dst
, offset
, const_offset
, align
);
3523 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3525 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3526 Builder
bld(ctx
->program
, ctx
->block
);
3527 Temp ptr64
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->descriptor_sets
[0]));
3528 Operand off
= bld
.copy(bld
.def(s1
), Operand(desc_set
<< 2));
3529 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, off
);//, false, false, false);
3532 return get_arg(ctx
, ctx
->args
->descriptor_sets
[desc_set
]);
3536 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3538 Builder
bld(ctx
->program
, ctx
->block
);
3539 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3540 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3541 index
= bld
.as_uniform(index
);
3542 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3543 unsigned binding
= nir_intrinsic_binding(instr
);
3546 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3547 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3548 unsigned offset
= layout
->binding
[binding
].offset
;
3550 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3551 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3552 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3553 desc_ptr
= get_arg(ctx
, ctx
->args
->ac
.push_constants
);
3554 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3557 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3558 stride
= layout
->binding
[binding
].size
;
3561 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3562 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3564 if (nir_const_index
) {
3565 const_index
= const_index
* stride
;
3566 } else if (index
.type() == RegType::vgpr
) {
3567 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3568 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3570 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3574 if (nir_const_index
) {
3575 const_index
= const_index
+ offset
;
3576 } else if (index
.type() == RegType::vgpr
) {
3577 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
3579 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
3583 if (nir_const_index
&& const_index
== 0) {
3585 } else if (index
.type() == RegType::vgpr
) {
3586 index
= bld
.vadd32(bld
.def(v1
),
3587 nir_const_index
? Operand(const_index
) : Operand(index
),
3590 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3591 nir_const_index
? Operand(const_index
) : Operand(index
),
3595 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
3598 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
,
3599 Temp rsrc
, Temp offset
, bool glc
=false, bool readonly
=true)
3601 Builder
bld(ctx
->program
, ctx
->block
);
3603 unsigned num_bytes
= dst
.size() * 4;
3604 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
3607 if (dst
.type() == RegType::vgpr
|| (ctx
->options
->chip_class
< GFX8
&& !readonly
)) {
3608 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3609 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3610 unsigned const_offset
= 0;
3612 Temp lower
= Temp();
3613 if (num_bytes
> 16) {
3614 assert(num_components
== 3 || num_components
== 4);
3615 op
= aco_opcode::buffer_load_dwordx4
;
3616 lower
= bld
.tmp(v4
);
3617 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3618 mubuf
->definitions
[0] = Definition(lower
);
3619 mubuf
->operands
[0] = Operand(rsrc
);
3620 mubuf
->operands
[1] = vaddr
;
3621 mubuf
->operands
[2] = soffset
;
3622 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3625 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3626 mubuf
->can_reorder
= readonly
;
3627 bld
.insert(std::move(mubuf
));
3628 emit_split_vector(ctx
, lower
, 2);
3631 } else if (num_bytes
== 12 && ctx
->options
->chip_class
== GFX6
) {
3632 /* GFX6 doesn't support loading vec3, expand to vec4. */
3636 switch (num_bytes
) {
3638 op
= aco_opcode::buffer_load_dword
;
3641 op
= aco_opcode::buffer_load_dwordx2
;
3644 assert(ctx
->options
->chip_class
> GFX6
);
3645 op
= aco_opcode::buffer_load_dwordx3
;
3648 op
= aco_opcode::buffer_load_dwordx4
;
3651 unreachable("Load SSBO not implemented for this size.");
3653 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3654 mubuf
->operands
[0] = Operand(rsrc
);
3655 mubuf
->operands
[1] = vaddr
;
3656 mubuf
->operands
[2] = soffset
;
3657 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3660 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3661 mubuf
->can_reorder
= readonly
;
3662 mubuf
->offset
= const_offset
;
3663 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3665 if (dst
.size() > 4) {
3666 assert(lower
!= Temp());
3667 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3668 instr
->definitions
[0] = Definition(upper
);
3669 bld
.insert(std::move(instr
));
3670 if (dst
.size() == 8)
3671 emit_split_vector(ctx
, upper
, 2);
3672 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3673 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3674 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3675 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3676 if (dst
.size() == 8)
3677 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3678 } else if (dst
.size() == 3 && ctx
->options
->chip_class
== GFX6
) {
3679 Temp vec
= bld
.tmp(v4
);
3680 instr
->definitions
[0] = Definition(vec
);
3681 bld
.insert(std::move(instr
));
3682 emit_split_vector(ctx
, vec
, 4);
3684 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, 3, 1));
3685 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, vec
, 0, v1
));
3686 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, vec
, 1, v1
));
3687 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, vec
, 2, v1
));
3690 if (dst
.type() == RegType::sgpr
) {
3691 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3692 instr
->definitions
[0] = Definition(vec
);
3693 bld
.insert(std::move(instr
));
3694 expand_vector(ctx
, vec
, dst
, num_components
, (1 << num_components
) - 1);
3696 instr
->definitions
[0] = Definition(dst
);
3697 bld
.insert(std::move(instr
));
3698 emit_split_vector(ctx
, dst
, num_components
);
3701 switch (num_bytes
) {
3703 op
= aco_opcode::s_buffer_load_dword
;
3706 op
= aco_opcode::s_buffer_load_dwordx2
;
3710 op
= aco_opcode::s_buffer_load_dwordx4
;
3714 op
= aco_opcode::s_buffer_load_dwordx8
;
3717 unreachable("Load SSBO not implemented for this size.");
3719 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3720 load
->operands
[0] = Operand(rsrc
);
3721 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3722 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3723 load
->definitions
[0] = Definition(dst
);
3726 load
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3727 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3728 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3731 if (dst
.size() == 3) {
3732 Temp vec
= bld
.tmp(s4
);
3733 load
->definitions
[0] = Definition(vec
);
3734 bld
.insert(std::move(load
));
3735 emit_split_vector(ctx
, vec
, 4);
3737 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3738 emit_extract_vector(ctx
, vec
, 0, s1
),
3739 emit_extract_vector(ctx
, vec
, 1, s1
),
3740 emit_extract_vector(ctx
, vec
, 2, s1
));
3741 } else if (dst
.size() == 6) {
3742 Temp vec
= bld
.tmp(s8
);
3743 load
->definitions
[0] = Definition(vec
);
3744 bld
.insert(std::move(load
));
3745 emit_split_vector(ctx
, vec
, 4);
3747 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3748 emit_extract_vector(ctx
, vec
, 0, s2
),
3749 emit_extract_vector(ctx
, vec
, 1, s2
),
3750 emit_extract_vector(ctx
, vec
, 2, s2
));
3752 bld
.insert(std::move(load
));
3754 emit_split_vector(ctx
, dst
, num_components
);
3758 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3760 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3761 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3763 Builder
bld(ctx
->program
, ctx
->block
);
3765 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3766 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3767 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3768 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3770 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3771 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3772 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3773 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3774 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3775 if (ctx
->options
->chip_class
>= GFX10
) {
3776 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3777 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
3778 S_008F0C_RESOURCE_LEVEL(1);
3780 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3781 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3783 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3784 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3785 Operand(0xFFFFFFFFu
),
3786 Operand(desc_type
));
3787 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3788 rsrc
, upper_dwords
);
3790 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3791 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3794 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3797 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3799 Builder
bld(ctx
->program
, ctx
->block
);
3800 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3802 unsigned offset
= nir_intrinsic_base(instr
);
3803 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3804 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3806 unsigned count
= instr
->dest
.ssa
.num_components
;
3807 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3808 start
-= ctx
->args
->ac
.base_inline_push_consts
;
3809 if (start
+ count
<= ctx
->args
->ac
.num_inline_push_consts
) {
3810 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3811 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3812 for (unsigned i
= 0; i
< count
; ++i
) {
3813 elems
[i
] = get_arg(ctx
, ctx
->args
->ac
.inline_push_consts
[start
+ i
]);
3814 vec
->operands
[i
] = Operand
{elems
[i
]};
3816 vec
->definitions
[0] = Definition(dst
);
3817 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3818 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3823 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3824 if (offset
!= 0) // TODO check if index != 0 as well
3825 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3826 Temp ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->ac
.push_constants
));
3831 switch (dst
.size()) {
3833 op
= aco_opcode::s_load_dword
;
3836 op
= aco_opcode::s_load_dwordx2
;
3842 op
= aco_opcode::s_load_dwordx4
;
3848 op
= aco_opcode::s_load_dwordx8
;
3851 unreachable("unimplemented or forbidden load_push_constant.");
3854 bld
.smem(op
, Definition(vec
), ptr
, index
);
3857 emit_split_vector(ctx
, vec
, 4);
3858 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3859 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3860 emit_extract_vector(ctx
, vec
, 0, rc
),
3861 emit_extract_vector(ctx
, vec
, 1, rc
),
3862 emit_extract_vector(ctx
, vec
, 2, rc
));
3865 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3868 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3870 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3872 Builder
bld(ctx
->program
, ctx
->block
);
3874 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3875 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3876 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3877 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3878 if (ctx
->options
->chip_class
>= GFX10
) {
3879 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3880 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
3881 S_008F0C_RESOURCE_LEVEL(1);
3883 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3884 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3887 unsigned base
= nir_intrinsic_base(instr
);
3888 unsigned range
= nir_intrinsic_range(instr
);
3890 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3891 if (base
&& offset
.type() == RegType::sgpr
)
3892 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3893 else if (base
&& offset
.type() == RegType::vgpr
)
3894 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3896 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3897 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
3898 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
3899 Operand(desc_type
));
3901 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3904 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3906 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3907 ctx
->cf_info
.exec_potentially_empty_discard
= true;
3909 ctx
->program
->needs_exact
= true;
3911 // TODO: optimize uniform conditions
3912 Builder
bld(ctx
->program
, ctx
->block
);
3913 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3914 assert(src
.regClass() == bld
.lm
);
3915 src
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
3916 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3917 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3921 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3923 Builder
bld(ctx
->program
, ctx
->block
);
3925 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3926 ctx
->cf_info
.exec_potentially_empty_discard
= true;
3928 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3929 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3931 if (ctx
->block
->loop_nest_depth
&&
3932 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3933 /* we handle discards the same way as jump instructions */
3934 append_logical_end(ctx
->block
);
3936 /* in loops, discard behaves like break */
3937 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3938 ctx
->block
->kind
|= block_kind_discard
;
3941 /* uniform discard - loop ends here */
3942 assert(nir_instr_is_last(&instr
->instr
));
3943 ctx
->block
->kind
|= block_kind_uniform
;
3944 ctx
->cf_info
.has_branch
= true;
3945 bld
.branch(aco_opcode::p_branch
);
3946 add_linear_edge(ctx
->block
->index
, linear_target
);
3950 /* we add a break right behind the discard() instructions */
3951 ctx
->block
->kind
|= block_kind_break
;
3952 unsigned idx
= ctx
->block
->index
;
3954 /* remove critical edges from linear CFG */
3955 bld
.branch(aco_opcode::p_branch
);
3956 Block
* break_block
= ctx
->program
->create_and_insert_block();
3957 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3958 break_block
->kind
|= block_kind_uniform
;
3959 add_linear_edge(idx
, break_block
);
3960 add_linear_edge(break_block
->index
, linear_target
);
3961 bld
.reset(break_block
);
3962 bld
.branch(aco_opcode::p_branch
);
3964 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3965 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3966 add_linear_edge(idx
, continue_block
);
3967 append_logical_start(continue_block
);
3968 ctx
->block
= continue_block
;
3973 /* it can currently happen that NIR doesn't remove the unreachable code */
3974 if (!nir_instr_is_last(&instr
->instr
)) {
3975 ctx
->program
->needs_exact
= true;
3976 /* save exec somewhere temporarily so that it doesn't get
3977 * overwritten before the discard from outer exec masks */
3978 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, bld
.lm
));
3979 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3980 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3984 /* This condition is incorrect for uniformly branched discards in a loop
3985 * predicated by a divergent condition, but the above code catches that case
3986 * and the discard would end up turning into a discard_if.
3996 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3997 /* program just ends here */
3998 ctx
->block
->kind
|= block_kind_uniform
;
3999 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
4000 0 /* enabled mask */, 9 /* dest */,
4001 false /* compressed */, true/* done */, true /* valid mask */);
4002 bld
.sopp(aco_opcode::s_endpgm
);
4003 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
4005 ctx
->block
->kind
|= block_kind_discard
;
4006 /* branch and linear edge is added by visit_if() */
4010 enum aco_descriptor_type
{
4021 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
4022 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4024 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
4025 return dim
== ac_image_cube
||
4026 dim
== ac_image_1darray
||
4027 dim
== ac_image_2darray
||
4028 dim
== ac_image_2darraymsaa
;
4031 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
4032 enum aco_descriptor_type desc_type
,
4033 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
4035 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
4036 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
4037 if (it != ctx->tex_desc.end())
4040 Temp index
= Temp();
4041 bool index_set
= false;
4042 unsigned constant_index
= 0;
4043 unsigned descriptor_set
;
4044 unsigned base_index
;
4045 Builder
bld(ctx
->program
, ctx
->block
);
4048 assert(tex_instr
&& !image
);
4050 base_index
= tex_instr
->sampler_index
;
4052 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4053 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4057 assert(deref_instr
->deref_type
== nir_deref_type_array
);
4058 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
4060 constant_index
+= array_size
* const_value
->u32
;
4062 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
4063 if (indirect
.type() == RegType::vgpr
)
4064 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
4066 if (array_size
!= 1)
4067 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
4073 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
4077 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4079 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4080 base_index
= deref_instr
->var
->data
.binding
;
4083 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
4084 list
= convert_pointer_to_64_bit(ctx
, list
);
4086 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4087 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4088 unsigned offset
= binding
->offset
;
4089 unsigned stride
= binding
->size
;
4093 assert(base_index
< layout
->binding_count
);
4095 switch (desc_type
) {
4096 case ACO_DESC_IMAGE
:
4098 opcode
= aco_opcode::s_load_dwordx8
;
4100 case ACO_DESC_FMASK
:
4102 opcode
= aco_opcode::s_load_dwordx8
;
4105 case ACO_DESC_SAMPLER
:
4107 opcode
= aco_opcode::s_load_dwordx4
;
4108 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4109 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
4111 case ACO_DESC_BUFFER
:
4113 opcode
= aco_opcode::s_load_dwordx4
;
4115 case ACO_DESC_PLANE_0
:
4116 case ACO_DESC_PLANE_1
:
4118 opcode
= aco_opcode::s_load_dwordx8
;
4119 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
4121 case ACO_DESC_PLANE_2
:
4123 opcode
= aco_opcode::s_load_dwordx4
;
4127 unreachable("invalid desc_type\n");
4130 offset
+= constant_index
* stride
;
4132 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4133 (!index_set
|| binding
->immutable_samplers_equal
)) {
4134 if (binding
->immutable_samplers_equal
)
4137 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4138 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
4139 Operand(samplers
[constant_index
* 4 + 0]),
4140 Operand(samplers
[constant_index
* 4 + 1]),
4141 Operand(samplers
[constant_index
* 4 + 2]),
4142 Operand(samplers
[constant_index
* 4 + 3]));
4147 off
= bld
.copy(bld
.def(s1
), Operand(offset
));
4149 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
4150 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
4153 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
4155 if (desc_type
== ACO_DESC_PLANE_2
) {
4157 for (unsigned i
= 0; i
< 8; i
++)
4158 components
[i
] = bld
.tmp(s1
);
4159 bld
.pseudo(aco_opcode::p_split_vector
,
4160 Definition(components
[0]),
4161 Definition(components
[1]),
4162 Definition(components
[2]),
4163 Definition(components
[3]),
4166 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
4167 bld
.pseudo(aco_opcode::p_split_vector
,
4168 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
4169 Definition(components
[4]),
4170 Definition(components
[5]),
4171 Definition(components
[6]),
4172 Definition(components
[7]),
4175 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
4176 components
[0], components
[1], components
[2], components
[3],
4177 components
[4], components
[5], components
[6], components
[7]);
4183 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
4186 case GLSL_SAMPLER_DIM_BUF
:
4188 case GLSL_SAMPLER_DIM_1D
:
4189 return array
? 2 : 1;
4190 case GLSL_SAMPLER_DIM_2D
:
4191 return array
? 3 : 2;
4192 case GLSL_SAMPLER_DIM_MS
:
4193 return array
? 4 : 3;
4194 case GLSL_SAMPLER_DIM_3D
:
4195 case GLSL_SAMPLER_DIM_CUBE
:
4197 case GLSL_SAMPLER_DIM_RECT
:
4198 case GLSL_SAMPLER_DIM_SUBPASS
:
4200 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
4209 /* Adjust the sample index according to FMASK.
4211 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4212 * which is the identity mapping. Each nibble says which physical sample
4213 * should be fetched to get that sample.
4215 * For example, 0x11111100 means there are only 2 samples stored and
4216 * the second sample covers 3/4 of the pixel. When reading samples 0
4217 * and 1, return physical sample 0 (determined by the first two 0s
4218 * in FMASK), otherwise return physical sample 1.
4220 * The sample index should be adjusted as follows:
4221 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4223 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, std::vector
<Temp
>& coords
, Operand sample_index
, Temp fmask_desc_ptr
)
4225 Builder
bld(ctx
->program
, ctx
->block
);
4226 Temp fmask
= bld
.tmp(v1
);
4227 unsigned dim
= ctx
->options
->chip_class
>= GFX10
4228 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
4231 Temp coord
= da
? bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), coords
[0], coords
[1], coords
[2]) :
4232 bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), coords
[0], coords
[1]);
4233 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 3, 1)};
4234 load
->operands
[0] = Operand(fmask_desc_ptr
);
4235 load
->operands
[1] = Operand(s4
); /* no sampler */
4236 load
->operands
[2] = Operand(coord
);
4237 load
->definitions
[0] = Definition(fmask
);
4244 load
->can_reorder
= true; /* fmask images shouldn't be modified */
4245 ctx
->block
->instructions
.emplace_back(std::move(load
));
4247 Operand sample_index4
;
4248 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
4249 sample_index4
= Operand(sample_index
.constantValue() << 2);
4250 } else if (sample_index
.regClass() == s1
) {
4251 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
4253 assert(sample_index
.regClass() == v1
);
4254 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
4258 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
4259 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
4260 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
4261 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
4263 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
4265 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4266 * resource descriptor is 0 (invalid),
4268 Temp compare
= bld
.tmp(bld
.lm
);
4269 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
4270 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
4272 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
4274 /* Replace the MSAA sample index. */
4275 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
4278 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
4281 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4282 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4283 bool is_array
= glsl_sampler_type_is_array(type
);
4284 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
4285 assert(!add_frag_pos
&& "Input attachments should be lowered.");
4286 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
4287 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
4288 int count
= image_type_to_components_count(dim
, is_array
);
4289 std::vector
<Temp
> coords(count
);
4290 Builder
bld(ctx
->program
, ctx
->block
);
4294 Temp src2
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4295 /* get sample index */
4296 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
4297 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
4298 Operand sample_index
= sample_cv
? Operand(sample_cv
->u32
) : Operand(emit_extract_vector(ctx
, src2
, 0, v1
));
4299 std::vector
<Temp
> fmask_load_address
;
4300 for (unsigned i
= 0; i
< (is_array
? 3 : 2); i
++)
4301 fmask_load_address
.emplace_back(emit_extract_vector(ctx
, src0
, i
, v1
));
4303 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
4304 coords
[count
] = adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
);
4306 coords
[count
] = emit_extract_vector(ctx
, src2
, 0, v1
);
4310 if (count
== 1 && !gfx9_1d
)
4311 return emit_extract_vector(ctx
, src0
, 0, v1
);
4314 coords
[0] = emit_extract_vector(ctx
, src0
, 0, v1
);
4315 coords
.resize(coords
.size() + 1);
4316 coords
[1] = bld
.copy(bld
.def(v1
), Operand(0u));
4318 coords
[2] = emit_extract_vector(ctx
, src0
, 1, v1
);
4320 for (int i
= 0; i
< count
; i
++)
4321 coords
[i
] = emit_extract_vector(ctx
, src0
, i
, v1
);
4324 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
4325 instr
->intrinsic
== nir_intrinsic_image_deref_store
) {
4326 int lod_index
= instr
->intrinsic
== nir_intrinsic_image_deref_load
? 3 : 4;
4327 bool level_zero
= nir_src_is_const(instr
->src
[lod_index
]) && nir_src_as_uint(instr
->src
[lod_index
]) == 0;
4330 coords
.emplace_back(get_ssa_temp(ctx
, instr
->src
[lod_index
].ssa
));
4333 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
4334 for (unsigned i
= 0; i
< coords
.size(); i
++)
4335 vec
->operands
[i
] = Operand(coords
[i
]);
4336 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
4337 vec
->definitions
[0] = Definition(res
);
4338 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4343 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4345 Builder
bld(ctx
->program
, ctx
->block
);
4346 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4347 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4348 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4349 bool is_array
= glsl_sampler_type_is_array(type
);
4350 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4352 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4353 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4354 unsigned num_channels
= util_last_bit(mask
);
4355 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4356 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4359 switch (num_channels
) {
4361 opcode
= aco_opcode::buffer_load_format_x
;
4364 opcode
= aco_opcode::buffer_load_format_xy
;
4367 opcode
= aco_opcode::buffer_load_format_xyz
;
4370 opcode
= aco_opcode::buffer_load_format_xyzw
;
4373 unreachable(">4 channel buffer image load");
4375 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4376 load
->operands
[0] = Operand(rsrc
);
4377 load
->operands
[1] = Operand(vindex
);
4378 load
->operands
[2] = Operand((uint32_t) 0);
4380 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4383 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4384 load
->definitions
[0] = Definition(tmp
);
4386 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4387 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
4388 load
->barrier
= barrier_image
;
4389 ctx
->block
->instructions
.emplace_back(std::move(load
));
4391 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4395 Temp coords
= get_image_coords(ctx
, instr
, type
);
4396 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4398 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4399 unsigned num_components
= util_bitcount(dmask
);
4401 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4404 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4406 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
4407 aco_opcode opcode
= level_zero
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
4409 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1)};
4410 load
->operands
[0] = Operand(resource
);
4411 load
->operands
[1] = Operand(s4
); /* no sampler */
4412 load
->operands
[2] = Operand(coords
);
4413 load
->definitions
[0] = Definition(tmp
);
4414 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4415 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
4416 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4417 load
->dmask
= dmask
;
4419 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4420 load
->barrier
= barrier_image
;
4421 ctx
->block
->instructions
.emplace_back(std::move(load
));
4423 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4427 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4429 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4430 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4431 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4432 bool is_array
= glsl_sampler_type_is_array(type
);
4433 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4435 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4437 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4438 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4439 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4441 switch (data
.size()) {
4443 opcode
= aco_opcode::buffer_store_format_x
;
4446 opcode
= aco_opcode::buffer_store_format_xy
;
4449 opcode
= aco_opcode::buffer_store_format_xyz
;
4452 opcode
= aco_opcode::buffer_store_format_xyzw
;
4455 unreachable(">4 channel buffer image store");
4457 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4458 store
->operands
[0] = Operand(rsrc
);
4459 store
->operands
[1] = Operand(vindex
);
4460 store
->operands
[2] = Operand((uint32_t) 0);
4461 store
->operands
[3] = Operand(data
);
4462 store
->idxen
= true;
4465 store
->disable_wqm
= true;
4466 store
->barrier
= barrier_image
;
4467 ctx
->program
->needs_exact
= true;
4468 ctx
->block
->instructions
.emplace_back(std::move(store
));
4472 assert(data
.type() == RegType::vgpr
);
4473 Temp coords
= get_image_coords(ctx
, instr
, type
);
4474 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4476 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
4477 aco_opcode opcode
= level_zero
? aco_opcode::image_store
: aco_opcode::image_store_mip
;
4479 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 0)};
4480 store
->operands
[0] = Operand(resource
);
4481 store
->operands
[1] = Operand(data
);
4482 store
->operands
[2] = Operand(coords
);
4485 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4486 store
->dmask
= (1 << data
.size()) - 1;
4488 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4489 store
->disable_wqm
= true;
4490 store
->barrier
= barrier_image
;
4491 ctx
->program
->needs_exact
= true;
4492 ctx
->block
->instructions
.emplace_back(std::move(store
));
4496 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4498 /* return the previous value if dest is ever used */
4499 bool return_previous
= false;
4500 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4501 return_previous
= true;
4504 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4505 return_previous
= true;
4509 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4510 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4511 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4512 bool is_array
= glsl_sampler_type_is_array(type
);
4513 Builder
bld(ctx
->program
, ctx
->block
);
4515 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4516 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4518 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4519 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4521 aco_opcode buf_op
, image_op
;
4522 switch (instr
->intrinsic
) {
4523 case nir_intrinsic_image_deref_atomic_add
:
4524 buf_op
= aco_opcode::buffer_atomic_add
;
4525 image_op
= aco_opcode::image_atomic_add
;
4527 case nir_intrinsic_image_deref_atomic_umin
:
4528 buf_op
= aco_opcode::buffer_atomic_umin
;
4529 image_op
= aco_opcode::image_atomic_umin
;
4531 case nir_intrinsic_image_deref_atomic_imin
:
4532 buf_op
= aco_opcode::buffer_atomic_smin
;
4533 image_op
= aco_opcode::image_atomic_smin
;
4535 case nir_intrinsic_image_deref_atomic_umax
:
4536 buf_op
= aco_opcode::buffer_atomic_umax
;
4537 image_op
= aco_opcode::image_atomic_umax
;
4539 case nir_intrinsic_image_deref_atomic_imax
:
4540 buf_op
= aco_opcode::buffer_atomic_smax
;
4541 image_op
= aco_opcode::image_atomic_smax
;
4543 case nir_intrinsic_image_deref_atomic_and
:
4544 buf_op
= aco_opcode::buffer_atomic_and
;
4545 image_op
= aco_opcode::image_atomic_and
;
4547 case nir_intrinsic_image_deref_atomic_or
:
4548 buf_op
= aco_opcode::buffer_atomic_or
;
4549 image_op
= aco_opcode::image_atomic_or
;
4551 case nir_intrinsic_image_deref_atomic_xor
:
4552 buf_op
= aco_opcode::buffer_atomic_xor
;
4553 image_op
= aco_opcode::image_atomic_xor
;
4555 case nir_intrinsic_image_deref_atomic_exchange
:
4556 buf_op
= aco_opcode::buffer_atomic_swap
;
4557 image_op
= aco_opcode::image_atomic_swap
;
4559 case nir_intrinsic_image_deref_atomic_comp_swap
:
4560 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4561 image_op
= aco_opcode::image_atomic_cmpswap
;
4564 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4567 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4569 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4570 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4571 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4572 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4573 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4574 mubuf
->operands
[0] = Operand(resource
);
4575 mubuf
->operands
[1] = Operand(vindex
);
4576 mubuf
->operands
[2] = Operand((uint32_t)0);
4577 mubuf
->operands
[3] = Operand(data
);
4578 if (return_previous
)
4579 mubuf
->definitions
[0] = Definition(dst
);
4581 mubuf
->idxen
= true;
4582 mubuf
->glc
= return_previous
;
4583 mubuf
->dlc
= false; /* Not needed for atomics */
4584 mubuf
->disable_wqm
= true;
4585 mubuf
->barrier
= barrier_image
;
4586 ctx
->program
->needs_exact
= true;
4587 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4591 Temp coords
= get_image_coords(ctx
, instr
, type
);
4592 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4593 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 3, return_previous
? 1 : 0)};
4594 mimg
->operands
[0] = Operand(resource
);
4595 mimg
->operands
[1] = Operand(data
);
4596 mimg
->operands
[2] = Operand(coords
);
4597 if (return_previous
)
4598 mimg
->definitions
[0] = Definition(dst
);
4599 mimg
->glc
= return_previous
;
4600 mimg
->dlc
= false; /* Not needed for atomics */
4601 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4602 mimg
->dmask
= (1 << data
.size()) - 1;
4604 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4605 mimg
->disable_wqm
= true;
4606 mimg
->barrier
= barrier_image
;
4607 ctx
->program
->needs_exact
= true;
4608 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4612 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4614 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4615 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
4616 Builder
bld(ctx
->program
, ctx
->block
);
4618 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4620 Temp size_div3
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), bld
.copy(bld
.def(v1
), Operand(0xaaaaaaabu
)), size
);
4621 size_div3
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.as_uniform(size_div3
), Operand(1u));
4623 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4624 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4626 Temp is12
= bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.def(s1
, scc
), stride
, Operand(12u));
4627 size
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), size_div3
, size
, bld
.scc(is12
));
4629 Temp shr_dst
= dst
.type() == RegType::vgpr
? bld
.tmp(s1
) : dst
;
4630 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(shr_dst
), bld
.def(s1
, scc
),
4631 size
, bld
.sop1(aco_opcode::s_ff1_i32_b32
, bld
.def(s1
), stride
));
4632 if (dst
.type() == RegType::vgpr
)
4633 bld
.copy(Definition(dst
), shr_dst
);
4635 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
4637 emit_extract_vector(ctx
, desc
, 2, dst
);
4641 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4643 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4644 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4645 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4646 bool is_array
= glsl_sampler_type_is_array(type
);
4647 Builder
bld(ctx
->program
, ctx
->block
);
4649 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4650 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4651 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4655 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4658 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4660 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4662 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1)};
4663 mimg
->operands
[0] = Operand(resource
);
4664 mimg
->operands
[1] = Operand(s4
); /* no sampler */
4665 mimg
->operands
[2] = Operand(lod
);
4666 uint8_t& dmask
= mimg
->dmask
;
4667 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4668 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4669 mimg
->da
= glsl_sampler_type_is_array(type
);
4670 mimg
->can_reorder
= true;
4671 Definition
& def
= mimg
->definitions
[0];
4672 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4674 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4675 glsl_sampler_type_is_array(type
)) {
4677 assert(instr
->dest
.ssa
.num_components
== 3);
4678 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4679 def
= Definition(tmp
);
4680 emit_split_vector(ctx
, tmp
, 3);
4682 /* divide 3rd value by 6 by multiplying with magic number */
4683 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4684 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4686 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4687 emit_extract_vector(ctx
, tmp
, 0, v1
),
4688 emit_extract_vector(ctx
, tmp
, 1, v1
),
4691 } else if (ctx
->options
->chip_class
== GFX9
&&
4692 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4693 glsl_sampler_type_is_array(type
)) {
4694 assert(instr
->dest
.ssa
.num_components
== 2);
4695 def
= Definition(dst
);
4698 def
= Definition(dst
);
4701 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4704 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4706 Builder
bld(ctx
->program
, ctx
->block
);
4707 unsigned num_components
= instr
->num_components
;
4709 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4710 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4711 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4713 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4714 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
, false);
4717 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4719 Builder
bld(ctx
->program
, ctx
->block
);
4720 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4721 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4722 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4723 Temp offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4725 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4726 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4728 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4729 ctx
->options
->chip_class
>= GFX8
;
4731 offset
= bld
.as_uniform(offset
);
4732 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4736 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4737 if (count
== 3 && (smem
|| ctx
->options
->chip_class
== GFX6
)) {
4738 /* GFX6 doesn't support storing vec3, split it. */
4739 writemask
|= 1u << (start
+ 2);
4742 int num_bytes
= count
* elem_size_bytes
;
4744 if (num_bytes
> 16) {
4745 assert(elem_size_bytes
== 8);
4746 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4751 // TODO: check alignment of sub-dword stores
4752 // TODO: split 3 bytes. there is no store instruction for that
4755 if (count
!= instr
->num_components
) {
4756 emit_split_vector(ctx
, data
, instr
->num_components
);
4757 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4758 for (int i
= 0; i
< count
; i
++) {
4759 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4760 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4762 write_data
= bld
.tmp(!smem
? RegType::vgpr
: smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4763 vec
->definitions
[0] = Definition(write_data
);
4764 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4765 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4766 assert(num_bytes
% 4 == 0);
4767 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4768 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4769 assert(num_bytes
% 4 == 0);
4770 write_data
= bld
.as_uniform(data
);
4775 aco_opcode vmem_op
, smem_op
;
4776 switch (num_bytes
) {
4778 vmem_op
= aco_opcode::buffer_store_dword
;
4779 smem_op
= aco_opcode::s_buffer_store_dword
;
4782 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4783 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4786 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4787 smem_op
= aco_opcode::last_opcode
;
4788 assert(!smem
&& ctx
->options
->chip_class
> GFX6
);
4791 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4792 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4795 unreachable("Store SSBO not implemented for this size.");
4797 if (ctx
->stage
== fragment_fs
)
4798 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4801 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4802 store
->operands
[0] = Operand(rsrc
);
4804 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4805 offset
, Operand(start
* elem_size_bytes
));
4806 store
->operands
[1] = Operand(off
);
4808 store
->operands
[1] = Operand(offset
);
4810 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4811 store
->operands
[1].setFixed(m0
);
4812 store
->operands
[2] = Operand(write_data
);
4813 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4815 store
->disable_wqm
= true;
4816 store
->barrier
= barrier_buffer
;
4817 ctx
->block
->instructions
.emplace_back(std::move(store
));
4818 ctx
->program
->wb_smem_l1_on_end
= true;
4819 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4820 ctx
->block
->kind
|= block_kind_needs_lowering
;
4821 ctx
->program
->needs_exact
= true;
4824 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4825 store
->operands
[0] = Operand(rsrc
);
4826 store
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4827 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4828 store
->operands
[3] = Operand(write_data
);
4829 store
->offset
= start
* elem_size_bytes
;
4830 store
->offen
= (offset
.type() == RegType::vgpr
);
4831 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4833 store
->disable_wqm
= true;
4834 store
->barrier
= barrier_buffer
;
4835 ctx
->program
->needs_exact
= true;
4836 ctx
->block
->instructions
.emplace_back(std::move(store
));
4841 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4843 /* return the previous value if dest is ever used */
4844 bool return_previous
= false;
4845 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4846 return_previous
= true;
4849 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4850 return_previous
= true;
4854 Builder
bld(ctx
->program
, ctx
->block
);
4855 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4857 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4858 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4859 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4861 Temp offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4862 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4863 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4865 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4867 aco_opcode op32
, op64
;
4868 switch (instr
->intrinsic
) {
4869 case nir_intrinsic_ssbo_atomic_add
:
4870 op32
= aco_opcode::buffer_atomic_add
;
4871 op64
= aco_opcode::buffer_atomic_add_x2
;
4873 case nir_intrinsic_ssbo_atomic_imin
:
4874 op32
= aco_opcode::buffer_atomic_smin
;
4875 op64
= aco_opcode::buffer_atomic_smin_x2
;
4877 case nir_intrinsic_ssbo_atomic_umin
:
4878 op32
= aco_opcode::buffer_atomic_umin
;
4879 op64
= aco_opcode::buffer_atomic_umin_x2
;
4881 case nir_intrinsic_ssbo_atomic_imax
:
4882 op32
= aco_opcode::buffer_atomic_smax
;
4883 op64
= aco_opcode::buffer_atomic_smax_x2
;
4885 case nir_intrinsic_ssbo_atomic_umax
:
4886 op32
= aco_opcode::buffer_atomic_umax
;
4887 op64
= aco_opcode::buffer_atomic_umax_x2
;
4889 case nir_intrinsic_ssbo_atomic_and
:
4890 op32
= aco_opcode::buffer_atomic_and
;
4891 op64
= aco_opcode::buffer_atomic_and_x2
;
4893 case nir_intrinsic_ssbo_atomic_or
:
4894 op32
= aco_opcode::buffer_atomic_or
;
4895 op64
= aco_opcode::buffer_atomic_or_x2
;
4897 case nir_intrinsic_ssbo_atomic_xor
:
4898 op32
= aco_opcode::buffer_atomic_xor
;
4899 op64
= aco_opcode::buffer_atomic_xor_x2
;
4901 case nir_intrinsic_ssbo_atomic_exchange
:
4902 op32
= aco_opcode::buffer_atomic_swap
;
4903 op64
= aco_opcode::buffer_atomic_swap_x2
;
4905 case nir_intrinsic_ssbo_atomic_comp_swap
:
4906 op32
= aco_opcode::buffer_atomic_cmpswap
;
4907 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4910 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4912 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4913 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4914 mubuf
->operands
[0] = Operand(rsrc
);
4915 mubuf
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4916 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4917 mubuf
->operands
[3] = Operand(data
);
4918 if (return_previous
)
4919 mubuf
->definitions
[0] = Definition(dst
);
4921 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4922 mubuf
->glc
= return_previous
;
4923 mubuf
->dlc
= false; /* Not needed for atomics */
4924 mubuf
->disable_wqm
= true;
4925 mubuf
->barrier
= barrier_buffer
;
4926 ctx
->program
->needs_exact
= true;
4927 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4930 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4932 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4933 Builder
bld(ctx
->program
, ctx
->block
);
4934 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4935 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4938 Temp
get_gfx6_global_rsrc(Builder
& bld
, Temp addr
)
4940 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4941 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4943 if (addr
.type() == RegType::vgpr
)
4944 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf
));
4945 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), addr
, Operand(-1u), Operand(rsrc_conf
));
4948 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4950 Builder
bld(ctx
->program
, ctx
->block
);
4951 unsigned num_components
= instr
->num_components
;
4952 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4954 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4955 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4957 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4958 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4960 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4961 bool global
= ctx
->options
->chip_class
>= GFX9
;
4963 if (ctx
->options
->chip_class
>= GFX7
) {
4965 switch (num_bytes
) {
4967 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4970 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4973 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4976 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4979 unreachable("load_global not implemented for this size.");
4982 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4983 flat
->operands
[0] = Operand(addr
);
4984 flat
->operands
[1] = Operand(s1
);
4987 flat
->barrier
= barrier_buffer
;
4989 if (dst
.type() == RegType::sgpr
) {
4990 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4991 flat
->definitions
[0] = Definition(vec
);
4992 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4993 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4995 flat
->definitions
[0] = Definition(dst
);
4996 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4998 emit_split_vector(ctx
, dst
, num_components
);
5000 assert(ctx
->options
->chip_class
== GFX6
);
5002 /* GFX6 doesn't support loading vec3, expand to vec4. */
5003 num_bytes
= num_bytes
== 12 ? 16 : num_bytes
;
5006 switch (num_bytes
) {
5008 op
= aco_opcode::buffer_load_dword
;
5011 op
= aco_opcode::buffer_load_dwordx2
;
5014 op
= aco_opcode::buffer_load_dwordx4
;
5017 unreachable("load_global not implemented for this size.");
5020 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
5022 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
5023 mubuf
->operands
[0] = Operand(rsrc
);
5024 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
5025 mubuf
->operands
[2] = Operand(0u);
5029 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
5030 mubuf
->disable_wqm
= false;
5031 mubuf
->barrier
= barrier_buffer
;
5032 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
5035 if (dst
.size() == 3) {
5036 Temp vec
= bld
.tmp(v4
);
5037 instr
->definitions
[0] = Definition(vec
);
5038 bld
.insert(std::move(instr
));
5039 emit_split_vector(ctx
, vec
, 4);
5041 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, 3, 1));
5042 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, vec
, 0, v1
));
5043 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, vec
, 1, v1
));
5044 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, vec
, 2, v1
));
5047 if (dst
.type() == RegType::sgpr
) {
5048 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
5049 instr
->definitions
[0] = Definition(vec
);
5050 bld
.insert(std::move(instr
));
5051 expand_vector(ctx
, vec
, dst
, num_components
, (1 << num_components
) - 1);
5052 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
5054 instr
->definitions
[0] = Definition(dst
);
5055 bld
.insert(std::move(instr
));
5056 emit_split_vector(ctx
, dst
, num_components
);
5060 switch (num_bytes
) {
5062 op
= aco_opcode::s_load_dword
;
5065 op
= aco_opcode::s_load_dwordx2
;
5069 op
= aco_opcode::s_load_dwordx4
;
5072 unreachable("load_global not implemented for this size.");
5074 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
5075 load
->operands
[0] = Operand(addr
);
5076 load
->operands
[1] = Operand(0u);
5077 load
->definitions
[0] = Definition(dst
);
5080 load
->barrier
= barrier_buffer
;
5081 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
5083 if (dst
.size() == 3) {
5085 Temp vec
= bld
.tmp(s4
);
5086 load
->definitions
[0] = Definition(vec
);
5087 ctx
->block
->instructions
.emplace_back(std::move(load
));
5088 emit_split_vector(ctx
, vec
, 4);
5090 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5091 emit_extract_vector(ctx
, vec
, 0, s1
),
5092 emit_extract_vector(ctx
, vec
, 1, s1
),
5093 emit_extract_vector(ctx
, vec
, 2, s1
));
5095 ctx
->block
->instructions
.emplace_back(std::move(load
));
5100 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5102 Builder
bld(ctx
->program
, ctx
->block
);
5103 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5105 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5106 Temp addr
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5108 if (ctx
->options
->chip_class
>= GFX7
)
5109 addr
= as_vgpr(ctx
, addr
);
5111 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5114 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5115 if (count
== 3 && ctx
->options
->chip_class
== GFX6
) {
5116 /* GFX6 doesn't support storing vec3, split it. */
5117 writemask
|= 1u << (start
+ 2);
5120 unsigned num_bytes
= count
* elem_size_bytes
;
5122 Temp write_data
= data
;
5123 if (count
!= instr
->num_components
) {
5124 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5125 for (int i
= 0; i
< count
; i
++)
5126 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
5127 write_data
= bld
.tmp(RegType::vgpr
, count
);
5128 vec
->definitions
[0] = Definition(write_data
);
5129 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5132 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
5133 unsigned offset
= start
* elem_size_bytes
;
5135 if (ctx
->options
->chip_class
>= GFX7
) {
5136 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
5137 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
5138 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
5139 Temp carry
= bld
.tmp(bld
.lm
);
5140 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
5142 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
5143 Operand(offset
), addr0
);
5144 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(bld
.lm
),
5146 carry
).def(1).setHint(vcc
);
5148 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
5153 bool global
= ctx
->options
->chip_class
>= GFX9
;
5155 switch (num_bytes
) {
5157 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
5160 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
5163 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
5166 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
5169 unreachable("store_global not implemented for this size.");
5172 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
5173 flat
->operands
[0] = Operand(addr
);
5174 flat
->operands
[1] = Operand(s1
);
5175 flat
->operands
[2] = Operand(data
);
5178 flat
->offset
= offset
;
5179 flat
->disable_wqm
= true;
5180 flat
->barrier
= barrier_buffer
;
5181 ctx
->program
->needs_exact
= true;
5182 ctx
->block
->instructions
.emplace_back(std::move(flat
));
5184 assert(ctx
->options
->chip_class
== GFX6
);
5187 switch (num_bytes
) {
5189 op
= aco_opcode::buffer_store_dword
;
5192 op
= aco_opcode::buffer_store_dwordx2
;
5195 op
= aco_opcode::buffer_store_dwordx4
;
5198 unreachable("store_global not implemented for this size.");
5201 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
5203 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, 0)};
5204 mubuf
->operands
[0] = Operand(rsrc
);
5205 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
5206 mubuf
->operands
[2] = Operand(0u);
5207 mubuf
->operands
[3] = Operand(write_data
);
5210 mubuf
->offset
= offset
;
5211 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
5212 mubuf
->disable_wqm
= true;
5213 mubuf
->barrier
= barrier_buffer
;
5214 ctx
->program
->needs_exact
= true;
5215 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
5220 void visit_global_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5222 /* return the previous value if dest is ever used */
5223 bool return_previous
= false;
5224 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5225 return_previous
= true;
5228 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5229 return_previous
= true;
5233 Builder
bld(ctx
->program
, ctx
->block
);
5234 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5235 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5237 if (ctx
->options
->chip_class
>= GFX7
)
5238 addr
= as_vgpr(ctx
, addr
);
5240 if (instr
->intrinsic
== nir_intrinsic_global_atomic_comp_swap
)
5241 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
5242 get_ssa_temp(ctx
, instr
->src
[2].ssa
), data
);
5244 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5246 aco_opcode op32
, op64
;
5248 if (ctx
->options
->chip_class
>= GFX7
) {
5249 bool global
= ctx
->options
->chip_class
>= GFX9
;
5250 switch (instr
->intrinsic
) {
5251 case nir_intrinsic_global_atomic_add
:
5252 op32
= global
? aco_opcode::global_atomic_add
: aco_opcode::flat_atomic_add
;
5253 op64
= global
? aco_opcode::global_atomic_add_x2
: aco_opcode::flat_atomic_add_x2
;
5255 case nir_intrinsic_global_atomic_imin
:
5256 op32
= global
? aco_opcode::global_atomic_smin
: aco_opcode::flat_atomic_smin
;
5257 op64
= global
? aco_opcode::global_atomic_smin_x2
: aco_opcode::flat_atomic_smin_x2
;
5259 case nir_intrinsic_global_atomic_umin
:
5260 op32
= global
? aco_opcode::global_atomic_umin
: aco_opcode::flat_atomic_umin
;
5261 op64
= global
? aco_opcode::global_atomic_umin_x2
: aco_opcode::flat_atomic_umin_x2
;
5263 case nir_intrinsic_global_atomic_imax
:
5264 op32
= global
? aco_opcode::global_atomic_smax
: aco_opcode::flat_atomic_smax
;
5265 op64
= global
? aco_opcode::global_atomic_smax_x2
: aco_opcode::flat_atomic_smax_x2
;
5267 case nir_intrinsic_global_atomic_umax
:
5268 op32
= global
? aco_opcode::global_atomic_umax
: aco_opcode::flat_atomic_umax
;
5269 op64
= global
? aco_opcode::global_atomic_umax_x2
: aco_opcode::flat_atomic_umax_x2
;
5271 case nir_intrinsic_global_atomic_and
:
5272 op32
= global
? aco_opcode::global_atomic_and
: aco_opcode::flat_atomic_and
;
5273 op64
= global
? aco_opcode::global_atomic_and_x2
: aco_opcode::flat_atomic_and_x2
;
5275 case nir_intrinsic_global_atomic_or
:
5276 op32
= global
? aco_opcode::global_atomic_or
: aco_opcode::flat_atomic_or
;
5277 op64
= global
? aco_opcode::global_atomic_or_x2
: aco_opcode::flat_atomic_or_x2
;
5279 case nir_intrinsic_global_atomic_xor
:
5280 op32
= global
? aco_opcode::global_atomic_xor
: aco_opcode::flat_atomic_xor
;
5281 op64
= global
? aco_opcode::global_atomic_xor_x2
: aco_opcode::flat_atomic_xor_x2
;
5283 case nir_intrinsic_global_atomic_exchange
:
5284 op32
= global
? aco_opcode::global_atomic_swap
: aco_opcode::flat_atomic_swap
;
5285 op64
= global
? aco_opcode::global_atomic_swap_x2
: aco_opcode::flat_atomic_swap_x2
;
5287 case nir_intrinsic_global_atomic_comp_swap
:
5288 op32
= global
? aco_opcode::global_atomic_cmpswap
: aco_opcode::flat_atomic_cmpswap
;
5289 op64
= global
? aco_opcode::global_atomic_cmpswap_x2
: aco_opcode::flat_atomic_cmpswap_x2
;
5292 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5295 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
5296 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, return_previous
? 1 : 0)};
5297 flat
->operands
[0] = Operand(addr
);
5298 flat
->operands
[1] = Operand(s1
);
5299 flat
->operands
[2] = Operand(data
);
5300 if (return_previous
)
5301 flat
->definitions
[0] = Definition(dst
);
5302 flat
->glc
= return_previous
;
5303 flat
->dlc
= false; /* Not needed for atomics */
5305 flat
->disable_wqm
= true;
5306 flat
->barrier
= barrier_buffer
;
5307 ctx
->program
->needs_exact
= true;
5308 ctx
->block
->instructions
.emplace_back(std::move(flat
));
5310 assert(ctx
->options
->chip_class
== GFX6
);
5312 switch (instr
->intrinsic
) {
5313 case nir_intrinsic_global_atomic_add
:
5314 op32
= aco_opcode::buffer_atomic_add
;
5315 op64
= aco_opcode::buffer_atomic_add_x2
;
5317 case nir_intrinsic_global_atomic_imin
:
5318 op32
= aco_opcode::buffer_atomic_smin
;
5319 op64
= aco_opcode::buffer_atomic_smin_x2
;
5321 case nir_intrinsic_global_atomic_umin
:
5322 op32
= aco_opcode::buffer_atomic_umin
;
5323 op64
= aco_opcode::buffer_atomic_umin_x2
;
5325 case nir_intrinsic_global_atomic_imax
:
5326 op32
= aco_opcode::buffer_atomic_smax
;
5327 op64
= aco_opcode::buffer_atomic_smax_x2
;
5329 case nir_intrinsic_global_atomic_umax
:
5330 op32
= aco_opcode::buffer_atomic_umax
;
5331 op64
= aco_opcode::buffer_atomic_umax_x2
;
5333 case nir_intrinsic_global_atomic_and
:
5334 op32
= aco_opcode::buffer_atomic_and
;
5335 op64
= aco_opcode::buffer_atomic_and_x2
;
5337 case nir_intrinsic_global_atomic_or
:
5338 op32
= aco_opcode::buffer_atomic_or
;
5339 op64
= aco_opcode::buffer_atomic_or_x2
;
5341 case nir_intrinsic_global_atomic_xor
:
5342 op32
= aco_opcode::buffer_atomic_xor
;
5343 op64
= aco_opcode::buffer_atomic_xor_x2
;
5345 case nir_intrinsic_global_atomic_exchange
:
5346 op32
= aco_opcode::buffer_atomic_swap
;
5347 op64
= aco_opcode::buffer_atomic_swap_x2
;
5349 case nir_intrinsic_global_atomic_comp_swap
:
5350 op32
= aco_opcode::buffer_atomic_cmpswap
;
5351 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
5354 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5357 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
5359 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
5361 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
5362 mubuf
->operands
[0] = Operand(rsrc
);
5363 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
5364 mubuf
->operands
[2] = Operand(0u);
5365 mubuf
->operands
[3] = Operand(data
);
5366 if (return_previous
)
5367 mubuf
->definitions
[0] = Definition(dst
);
5368 mubuf
->glc
= return_previous
;
5371 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
5372 mubuf
->disable_wqm
= true;
5373 mubuf
->barrier
= barrier_buffer
;
5374 ctx
->program
->needs_exact
= true;
5375 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
5379 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5380 Builder
bld(ctx
->program
, ctx
->block
);
5381 switch(instr
->intrinsic
) {
5382 case nir_intrinsic_group_memory_barrier
:
5383 case nir_intrinsic_memory_barrier
:
5384 bld
.barrier(aco_opcode::p_memory_barrier_common
);
5386 case nir_intrinsic_memory_barrier_buffer
:
5387 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
5389 case nir_intrinsic_memory_barrier_image
:
5390 bld
.barrier(aco_opcode::p_memory_barrier_image
);
5392 case nir_intrinsic_memory_barrier_shared
:
5393 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
5396 unreachable("Unimplemented memory barrier intrinsic");
5401 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5403 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
5404 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5405 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
5406 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5407 Builder
bld(ctx
->program
, ctx
->block
);
5409 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
5410 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
5411 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
5414 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5416 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5417 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5418 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5419 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5420 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
5422 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
5423 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
5426 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5428 unsigned offset
= nir_intrinsic_base(instr
);
5429 Operand m
= load_lds_size_m0(ctx
);
5430 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5431 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5433 unsigned num_operands
= 3;
5434 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
5435 switch(instr
->intrinsic
) {
5436 case nir_intrinsic_shared_atomic_add
:
5437 op32
= aco_opcode::ds_add_u32
;
5438 op64
= aco_opcode::ds_add_u64
;
5439 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
5440 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
5442 case nir_intrinsic_shared_atomic_imin
:
5443 op32
= aco_opcode::ds_min_i32
;
5444 op64
= aco_opcode::ds_min_i64
;
5445 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
5446 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
5448 case nir_intrinsic_shared_atomic_umin
:
5449 op32
= aco_opcode::ds_min_u32
;
5450 op64
= aco_opcode::ds_min_u64
;
5451 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
5452 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
5454 case nir_intrinsic_shared_atomic_imax
:
5455 op32
= aco_opcode::ds_max_i32
;
5456 op64
= aco_opcode::ds_max_i64
;
5457 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
5458 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
5460 case nir_intrinsic_shared_atomic_umax
:
5461 op32
= aco_opcode::ds_max_u32
;
5462 op64
= aco_opcode::ds_max_u64
;
5463 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
5464 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
5466 case nir_intrinsic_shared_atomic_and
:
5467 op32
= aco_opcode::ds_and_b32
;
5468 op64
= aco_opcode::ds_and_b64
;
5469 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
5470 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
5472 case nir_intrinsic_shared_atomic_or
:
5473 op32
= aco_opcode::ds_or_b32
;
5474 op64
= aco_opcode::ds_or_b64
;
5475 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
5476 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
5478 case nir_intrinsic_shared_atomic_xor
:
5479 op32
= aco_opcode::ds_xor_b32
;
5480 op64
= aco_opcode::ds_xor_b64
;
5481 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
5482 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
5484 case nir_intrinsic_shared_atomic_exchange
:
5485 op32
= aco_opcode::ds_write_b32
;
5486 op64
= aco_opcode::ds_write_b64
;
5487 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
5488 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
5490 case nir_intrinsic_shared_atomic_comp_swap
:
5491 op32
= aco_opcode::ds_cmpst_b32
;
5492 op64
= aco_opcode::ds_cmpst_b64
;
5493 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
5494 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
5498 unreachable("Unhandled shared atomic intrinsic");
5501 /* return the previous value if dest is ever used */
5502 bool return_previous
= false;
5503 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5504 return_previous
= true;
5507 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5508 return_previous
= true;
5513 if (data
.size() == 1) {
5514 assert(instr
->dest
.ssa
.bit_size
== 32);
5515 op
= return_previous
? op32_rtn
: op32
;
5517 assert(instr
->dest
.ssa
.bit_size
== 64);
5518 op
= return_previous
? op64_rtn
: op64
;
5521 if (offset
> 65535) {
5522 Builder
bld(ctx
->program
, ctx
->block
);
5523 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
5527 aco_ptr
<DS_instruction
> ds
;
5528 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
5529 ds
->operands
[0] = Operand(address
);
5530 ds
->operands
[1] = Operand(data
);
5531 if (num_operands
== 4)
5532 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5533 ds
->operands
[num_operands
- 1] = m
;
5534 ds
->offset0
= offset
;
5535 if (return_previous
)
5536 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5537 ctx
->block
->instructions
.emplace_back(std::move(ds
));
5540 Temp
get_scratch_resource(isel_context
*ctx
)
5542 Builder
bld(ctx
->program
, ctx
->block
);
5543 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
5544 if (ctx
->stage
!= compute_cs
)
5545 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
5547 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
5548 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
5550 if (ctx
->program
->chip_class
>= GFX10
) {
5551 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5552 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
5553 S_008F0C_RESOURCE_LEVEL(1);
5554 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
5555 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5556 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5559 /* older generations need element size = 16 bytes. element size removed in GFX9 */
5560 if (ctx
->program
->chip_class
<= GFX8
)
5561 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
5563 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
5566 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5567 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
5568 Builder
bld(ctx
->program
, ctx
->block
);
5569 Temp rsrc
= get_scratch_resource(ctx
);
5570 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5571 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5574 switch (dst
.size()) {
5576 op
= aco_opcode::buffer_load_dword
;
5579 op
= aco_opcode::buffer_load_dwordx2
;
5582 op
= aco_opcode::buffer_load_dwordx3
;
5585 op
= aco_opcode::buffer_load_dwordx4
;
5589 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
5590 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
5591 bld
.def(v4
), rsrc
, offset
,
5592 ctx
->program
->scratch_offset
, 0, true);
5593 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
5594 aco_opcode::buffer_load_dwordx4
,
5595 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
5596 rsrc
, offset
, ctx
->program
->scratch_offset
, 16, true);
5597 emit_split_vector(ctx
, lower
, 2);
5598 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
5599 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
5600 if (dst
.size() == 8) {
5601 emit_split_vector(ctx
, upper
, 2);
5602 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
5603 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
5608 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
5609 Format::PSEUDO
, dst
.size() / 2, 1)};
5610 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
5611 vec
->operands
[i
] = Operand(elems
[i
]);
5612 vec
->definitions
[0] = Definition(dst
);
5613 bld
.insert(std::move(vec
));
5614 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5618 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5621 bld
.mubuf(op
, Definition(dst
), rsrc
, offset
, ctx
->program
->scratch_offset
, 0, true);
5622 emit_split_vector(ctx
, dst
, instr
->num_components
);
5625 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5626 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
5627 Builder
bld(ctx
->program
, ctx
->block
);
5628 Temp rsrc
= get_scratch_resource(ctx
);
5629 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5630 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5632 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5633 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5637 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5638 int num_bytes
= count
* elem_size_bytes
;
5640 if (num_bytes
> 16) {
5641 assert(elem_size_bytes
== 8);
5642 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5647 // TODO: check alignment of sub-dword stores
5648 // TODO: split 3 bytes. there is no store instruction for that
5651 if (count
!= instr
->num_components
) {
5652 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5653 for (int i
= 0; i
< count
; i
++) {
5654 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
5655 vec
->operands
[i
] = Operand(elem
);
5657 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
5658 vec
->definitions
[0] = Definition(write_data
);
5659 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5665 switch (num_bytes
) {
5667 op
= aco_opcode::buffer_store_dword
;
5670 op
= aco_opcode::buffer_store_dwordx2
;
5673 op
= aco_opcode::buffer_store_dwordx3
;
5676 op
= aco_opcode::buffer_store_dwordx4
;
5679 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5682 bld
.mubuf(op
, rsrc
, offset
, ctx
->program
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
5686 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5687 uint8_t log2_ps_iter_samples
;
5688 if (ctx
->program
->info
->ps
.force_persample
) {
5689 log2_ps_iter_samples
=
5690 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
5692 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
5695 /* The bit pattern matches that used by fixed function fragment
5697 static const unsigned ps_iter_masks
[] = {
5698 0xffff, /* not used */
5704 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
5706 Builder
bld(ctx
->program
, ctx
->block
);
5708 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
5709 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
5710 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
5711 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
5712 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5713 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, get_arg(ctx
, ctx
->args
->ac
.sample_coverage
));
5716 void visit_emit_vertex_with_counter(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5717 Builder
bld(ctx
->program
, ctx
->block
);
5719 unsigned stream
= nir_intrinsic_stream_id(instr
);
5720 Temp next_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5721 next_vertex
= bld
.v_mul_imm(bld
.def(v1
), next_vertex
, 4u);
5722 nir_const_value
*next_vertex_cv
= nir_src_as_const_value(instr
->src
[0]);
5725 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_GSVS_GS
* 16u));
5727 unsigned num_components
=
5728 ctx
->program
->info
->gs
.num_stream_output_components
[stream
];
5729 assert(num_components
);
5731 unsigned stride
= 4u * num_components
* ctx
->shader
->info
.gs
.vertices_out
;
5732 unsigned stream_offset
= 0;
5733 for (unsigned i
= 0; i
< stream
; i
++) {
5734 unsigned prev_stride
= 4u * ctx
->program
->info
->gs
.num_stream_output_components
[i
] * ctx
->shader
->info
.gs
.vertices_out
;
5735 stream_offset
+= prev_stride
* ctx
->program
->wave_size
;
5738 /* Limit on the stride field for <= GFX7. */
5739 assert(stride
< (1 << 14));
5741 Temp gsvs_dwords
[4];
5742 for (unsigned i
= 0; i
< 4; i
++)
5743 gsvs_dwords
[i
] = bld
.tmp(s1
);
5744 bld
.pseudo(aco_opcode::p_split_vector
,
5745 Definition(gsvs_dwords
[0]),
5746 Definition(gsvs_dwords
[1]),
5747 Definition(gsvs_dwords
[2]),
5748 Definition(gsvs_dwords
[3]),
5751 if (stream_offset
) {
5752 Temp stream_offset_tmp
= bld
.copy(bld
.def(s1
), Operand(stream_offset
));
5754 Temp carry
= bld
.tmp(s1
);
5755 gsvs_dwords
[0] = bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), gsvs_dwords
[0], stream_offset_tmp
);
5756 gsvs_dwords
[1] = bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), gsvs_dwords
[1], Operand(0u), bld
.scc(carry
));
5759 gsvs_dwords
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), gsvs_dwords
[1], Operand(S_008F04_STRIDE(stride
)));
5760 gsvs_dwords
[2] = bld
.copy(bld
.def(s1
), Operand((uint32_t)ctx
->program
->wave_size
));
5762 gsvs_ring
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5763 gsvs_dwords
[0], gsvs_dwords
[1], gsvs_dwords
[2], gsvs_dwords
[3]);
5765 unsigned offset
= 0;
5766 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; i
++) {
5767 if (ctx
->program
->info
->gs
.output_streams
[i
] != stream
)
5770 for (unsigned j
= 0; j
< 4; j
++) {
5771 if (!(ctx
->program
->info
->gs
.output_usage_mask
[i
] & (1 << j
)))
5774 if (ctx
->outputs
.mask
[i
] & (1 << j
)) {
5775 Operand vaddr_offset
= next_vertex_cv
? Operand(v1
) : Operand(next_vertex
);
5776 unsigned const_offset
= (offset
+ (next_vertex_cv
? next_vertex_cv
->u32
: 0u)) * 4u;
5777 if (const_offset
>= 4096u) {
5778 if (vaddr_offset
.isUndefined())
5779 vaddr_offset
= bld
.copy(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u));
5781 vaddr_offset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), vaddr_offset
);
5782 const_offset
%= 4096u;
5785 aco_ptr
<MTBUF_instruction
> mtbuf
{create_instruction
<MTBUF_instruction
>(aco_opcode::tbuffer_store_format_x
, Format::MTBUF
, 4, 0)};
5786 mtbuf
->operands
[0] = Operand(gsvs_ring
);
5787 mtbuf
->operands
[1] = vaddr_offset
;
5788 mtbuf
->operands
[2] = Operand(get_arg(ctx
, ctx
->args
->gs2vs_offset
));
5789 mtbuf
->operands
[3] = Operand(ctx
->outputs
.outputs
[i
][j
]);
5790 mtbuf
->offen
= !vaddr_offset
.isUndefined();
5791 mtbuf
->dfmt
= V_008F0C_BUF_DATA_FORMAT_32
;
5792 mtbuf
->nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
5793 mtbuf
->offset
= const_offset
;
5796 mtbuf
->barrier
= barrier_gs_data
;
5797 mtbuf
->can_reorder
= true;
5798 bld
.insert(std::move(mtbuf
));
5801 offset
+= ctx
->shader
->info
.gs
.vertices_out
;
5804 /* outputs for the next vertex are undefined and keeping them around can
5805 * create invalid IR with control flow */
5806 ctx
->outputs
.mask
[i
] = 0;
5809 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(false, true, stream
));
5812 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
5814 Builder
bld(ctx
->program
, ctx
->block
);
5816 if (cluster_size
== 1) {
5818 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5819 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5820 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
5821 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
5822 bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
));
5823 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5824 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5825 return bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
5826 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)));
5827 } else if (op
== nir_op_iand
&& cluster_size
== ctx
->program
->wave_size
) {
5828 //subgroupAnd(val) -> (exec & ~val) == 0
5829 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
5830 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
5831 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
);
5832 } else if (op
== nir_op_ior
&& cluster_size
== ctx
->program
->wave_size
) {
5833 //subgroupOr(val) -> (val & exec) != 0
5834 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)).def(1).getTemp();
5835 return bool_to_vector_condition(ctx
, tmp
);
5836 } else if (op
== nir_op_ixor
&& cluster_size
== ctx
->program
->wave_size
) {
5837 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5838 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5839 tmp
= bld
.sop1(Builder::s_bcnt1_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
);
5840 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5841 return bool_to_vector_condition(ctx
, tmp
);
5843 //subgroupClustered{And,Or,Xor}(val, n) ->
5844 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
5845 //cluster_offset = ~(n - 1) & lane_id
5846 //cluster_mask = ((1 << n) - 1)
5847 //subgroupClusteredAnd():
5848 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5849 //subgroupClusteredOr():
5850 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5851 //subgroupClusteredXor():
5852 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5853 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
5854 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5857 if (op
== nir_op_iand
)
5858 tmp
= bld
.sop2(Builder::s_orn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5860 tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5862 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5864 if (ctx
->program
->chip_class
<= GFX7
)
5865 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), tmp
, cluster_offset
);
5866 else if (ctx
->program
->wave_size
== 64)
5867 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5869 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), cluster_offset
, tmp
);
5870 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5871 if (cluster_mask
!= 0xffffffff)
5872 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5874 Definition cmp_def
= Definition();
5875 if (op
== nir_op_iand
) {
5876 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(cluster_mask
), tmp
).def(0);
5877 } else if (op
== nir_op_ior
) {
5878 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
5879 } else if (op
== nir_op_ixor
) {
5880 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5881 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5882 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
5884 cmp_def
.setHint(vcc
);
5885 return cmp_def
.getTemp();
5889 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5891 Builder
bld(ctx
->program
, ctx
->block
);
5893 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5894 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5895 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5897 if (op
== nir_op_iand
)
5898 tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
5900 tmp
= bld
.sop2(Builder::s_and
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5902 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5903 Temp lo
= lohi
.def(0).getTemp();
5904 Temp hi
= lohi
.def(1).getTemp();
5905 Temp mbcnt
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(lo
), Operand(hi
));
5907 Definition cmp_def
= Definition();
5908 if (op
== nir_op_iand
)
5909 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
5910 else if (op
== nir_op_ior
)
5911 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
5912 else if (op
== nir_op_ixor
)
5913 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u),
5914 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5915 cmp_def
.setHint(vcc
);
5916 return cmp_def
.getTemp();
5919 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5921 Builder
bld(ctx
->program
, ctx
->block
);
5923 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5924 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5925 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5926 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5927 if (op
== nir_op_iand
)
5928 return bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
5929 else if (op
== nir_op_ior
)
5930 return bld
.sop2(Builder::s_or
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
5931 else if (op
== nir_op_ixor
)
5932 return bld
.sop2(Builder::s_xor
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
5938 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5940 Builder
bld(ctx
->program
, ctx
->block
);
5941 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5942 if (src
.regClass().type() == RegType::vgpr
) {
5943 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5944 } else if (src
.regClass() == s1
) {
5945 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5946 } else if (src
.regClass() == s2
) {
5947 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5949 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5950 nir_print_instr(&instr
->instr
, stderr
);
5951 fprintf(stderr
, "\n");
5955 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5957 Builder
bld(ctx
->program
, ctx
->block
);
5958 Temp persp_center
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
5959 Temp p1
= emit_extract_vector(ctx
, persp_center
, 0, v1
);
5960 Temp p2
= emit_extract_vector(ctx
, persp_center
, 1, v1
);
5962 Temp ddx_1
, ddx_2
, ddy_1
, ddy_2
;
5963 uint32_t dpp_ctrl0
= dpp_quad_perm(0, 0, 0, 0);
5964 uint32_t dpp_ctrl1
= dpp_quad_perm(1, 1, 1, 1);
5965 uint32_t dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
5968 if (ctx
->program
->chip_class
>= GFX8
) {
5969 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_ctrl0
);
5970 ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl1
);
5971 ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl2
);
5972 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_ctrl0
);
5973 ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl1
);
5974 ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl2
);
5976 Temp tl_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl0
);
5977 ddx_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl1
);
5978 ddx_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_1
, tl_1
);
5979 ddx_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl2
);
5980 ddx_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_2
, tl_1
);
5981 Temp tl_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl0
);
5982 ddy_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl1
);
5983 ddy_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_1
, tl_2
);
5984 ddy_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl2
);
5985 ddy_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_2
, tl_2
);
5988 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5989 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5990 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5991 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5992 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5993 Temp wqm1
= bld
.tmp(v1
);
5994 emit_wqm(ctx
, tmp1
, wqm1
, true);
5995 Temp wqm2
= bld
.tmp(v1
);
5996 emit_wqm(ctx
, tmp2
, wqm2
, true);
5997 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
6001 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6003 Builder
bld(ctx
->program
, ctx
->block
);
6004 switch(instr
->intrinsic
) {
6005 case nir_intrinsic_load_barycentric_sample
:
6006 case nir_intrinsic_load_barycentric_pixel
:
6007 case nir_intrinsic_load_barycentric_centroid
: {
6008 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
6009 Temp bary
= Temp(0, s2
);
6011 case INTERP_MODE_SMOOTH
:
6012 case INTERP_MODE_NONE
:
6013 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
6014 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
6015 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
6016 bary
= ctx
->persp_centroid
;
6017 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
6018 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_sample
);
6020 case INTERP_MODE_NOPERSPECTIVE
:
6021 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
6022 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_center
);
6023 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
6024 bary
= ctx
->linear_centroid
;
6025 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
6026 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_sample
);
6031 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6032 Temp p1
= emit_extract_vector(ctx
, bary
, 0, v1
);
6033 Temp p2
= emit_extract_vector(ctx
, bary
, 1, v1
);
6034 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6035 Operand(p1
), Operand(p2
));
6036 emit_split_vector(ctx
, dst
, 2);
6039 case nir_intrinsic_load_barycentric_model
: {
6040 Temp model
= get_arg(ctx
, ctx
->args
->ac
.pull_model
);
6042 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6043 Temp p1
= emit_extract_vector(ctx
, model
, 0, v1
);
6044 Temp p2
= emit_extract_vector(ctx
, model
, 1, v1
);
6045 Temp p3
= emit_extract_vector(ctx
, model
, 2, v1
);
6046 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6047 Operand(p1
), Operand(p2
), Operand(p3
));
6048 emit_split_vector(ctx
, dst
, 3);
6051 case nir_intrinsic_load_barycentric_at_sample
: {
6052 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
6053 switch (ctx
->options
->key
.fs
.num_samples
) {
6054 case 2: sample_pos_offset
+= 1 << 3; break;
6055 case 4: sample_pos_offset
+= 3 << 3; break;
6056 case 8: sample_pos_offset
+= 7 << 3; break;
6060 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6061 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
6062 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
6063 if (addr
.type() == RegType::sgpr
) {
6066 sample_pos_offset
+= const_addr
->u32
<< 3;
6067 offset
= Operand(sample_pos_offset
);
6068 } else if (ctx
->options
->chip_class
>= GFX9
) {
6069 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
6071 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
6072 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
6075 Operand off
= bld
.copy(bld
.def(s1
), Operand(offset
));
6076 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, off
);
6078 } else if (ctx
->options
->chip_class
>= GFX9
) {
6079 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
6080 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
6081 } else if (ctx
->options
->chip_class
>= GFX7
) {
6082 /* addr += private_segment_buffer + sample_pos_offset */
6083 Temp tmp0
= bld
.tmp(s1
);
6084 Temp tmp1
= bld
.tmp(s1
);
6085 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
6086 Definition scc_tmp
= bld
.def(s1
, scc
);
6087 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
6088 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
6089 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
6090 Temp pck0
= bld
.tmp(v1
);
6091 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
6092 tmp1
= as_vgpr(ctx
, tmp1
);
6093 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
);
6094 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
6096 /* sample_pos = flat_load_dwordx2 addr */
6097 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
6099 assert(ctx
->options
->chip_class
== GFX6
);
6101 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
6102 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
6103 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), private_segment_buffer
, Operand(0u), Operand(rsrc_conf
));
6105 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
6106 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), addr
, Operand(0u));
6108 sample_pos
= bld
.tmp(v2
);
6110 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dwordx2
, Format::MUBUF
, 3, 1)};
6111 load
->definitions
[0] = Definition(sample_pos
);
6112 load
->operands
[0] = Operand(rsrc
);
6113 load
->operands
[1] = Operand(addr
);
6114 load
->operands
[2] = Operand(0u);
6115 load
->offset
= sample_pos_offset
;
6117 load
->addr64
= true;
6120 load
->disable_wqm
= false;
6121 load
->barrier
= barrier_none
;
6122 load
->can_reorder
= true;
6123 ctx
->block
->instructions
.emplace_back(std::move(load
));
6126 /* sample_pos -= 0.5 */
6127 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
6128 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
6129 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
6130 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
6131 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
6133 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
6136 case nir_intrinsic_load_barycentric_at_offset
: {
6137 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6138 RegClass rc
= RegClass(offset
.type(), 1);
6139 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
6140 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
6141 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
6144 case nir_intrinsic_load_front_face
: {
6145 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6146 Operand(0u), get_arg(ctx
, ctx
->args
->ac
.front_face
)).def(0).setHint(vcc
);
6149 case nir_intrinsic_load_view_index
:
6150 case nir_intrinsic_load_layer_id
: {
6151 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& (sw_vs
| sw_gs
))) {
6152 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6153 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.view_index
)));
6157 unsigned idx
= nir_intrinsic_base(instr
);
6158 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6159 Operand(2u), bld
.m0(get_arg(ctx
, ctx
->args
->ac
.prim_mask
)), idx
, 0);
6162 case nir_intrinsic_load_frag_coord
: {
6163 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
6166 case nir_intrinsic_load_sample_pos
: {
6167 Temp posx
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[0]);
6168 Temp posy
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[1]);
6169 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6170 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
6171 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
6174 case nir_intrinsic_load_interpolated_input
:
6175 visit_load_interpolated_input(ctx
, instr
);
6177 case nir_intrinsic_store_output
:
6178 visit_store_output(ctx
, instr
);
6180 case nir_intrinsic_load_input
:
6181 case nir_intrinsic_load_input_vertex
:
6182 visit_load_input(ctx
, instr
);
6184 case nir_intrinsic_load_per_vertex_input
:
6185 visit_load_per_vertex_input(ctx
, instr
);
6187 case nir_intrinsic_load_ubo
:
6188 visit_load_ubo(ctx
, instr
);
6190 case nir_intrinsic_load_push_constant
:
6191 visit_load_push_constant(ctx
, instr
);
6193 case nir_intrinsic_load_constant
:
6194 visit_load_constant(ctx
, instr
);
6196 case nir_intrinsic_vulkan_resource_index
:
6197 visit_load_resource(ctx
, instr
);
6199 case nir_intrinsic_discard
:
6200 visit_discard(ctx
, instr
);
6202 case nir_intrinsic_discard_if
:
6203 visit_discard_if(ctx
, instr
);
6205 case nir_intrinsic_load_shared
:
6206 visit_load_shared(ctx
, instr
);
6208 case nir_intrinsic_store_shared
:
6209 visit_store_shared(ctx
, instr
);
6211 case nir_intrinsic_shared_atomic_add
:
6212 case nir_intrinsic_shared_atomic_imin
:
6213 case nir_intrinsic_shared_atomic_umin
:
6214 case nir_intrinsic_shared_atomic_imax
:
6215 case nir_intrinsic_shared_atomic_umax
:
6216 case nir_intrinsic_shared_atomic_and
:
6217 case nir_intrinsic_shared_atomic_or
:
6218 case nir_intrinsic_shared_atomic_xor
:
6219 case nir_intrinsic_shared_atomic_exchange
:
6220 case nir_intrinsic_shared_atomic_comp_swap
:
6221 visit_shared_atomic(ctx
, instr
);
6223 case nir_intrinsic_image_deref_load
:
6224 visit_image_load(ctx
, instr
);
6226 case nir_intrinsic_image_deref_store
:
6227 visit_image_store(ctx
, instr
);
6229 case nir_intrinsic_image_deref_atomic_add
:
6230 case nir_intrinsic_image_deref_atomic_umin
:
6231 case nir_intrinsic_image_deref_atomic_imin
:
6232 case nir_intrinsic_image_deref_atomic_umax
:
6233 case nir_intrinsic_image_deref_atomic_imax
:
6234 case nir_intrinsic_image_deref_atomic_and
:
6235 case nir_intrinsic_image_deref_atomic_or
:
6236 case nir_intrinsic_image_deref_atomic_xor
:
6237 case nir_intrinsic_image_deref_atomic_exchange
:
6238 case nir_intrinsic_image_deref_atomic_comp_swap
:
6239 visit_image_atomic(ctx
, instr
);
6241 case nir_intrinsic_image_deref_size
:
6242 visit_image_size(ctx
, instr
);
6244 case nir_intrinsic_load_ssbo
:
6245 visit_load_ssbo(ctx
, instr
);
6247 case nir_intrinsic_store_ssbo
:
6248 visit_store_ssbo(ctx
, instr
);
6250 case nir_intrinsic_load_global
:
6251 visit_load_global(ctx
, instr
);
6253 case nir_intrinsic_store_global
:
6254 visit_store_global(ctx
, instr
);
6256 case nir_intrinsic_global_atomic_add
:
6257 case nir_intrinsic_global_atomic_imin
:
6258 case nir_intrinsic_global_atomic_umin
:
6259 case nir_intrinsic_global_atomic_imax
:
6260 case nir_intrinsic_global_atomic_umax
:
6261 case nir_intrinsic_global_atomic_and
:
6262 case nir_intrinsic_global_atomic_or
:
6263 case nir_intrinsic_global_atomic_xor
:
6264 case nir_intrinsic_global_atomic_exchange
:
6265 case nir_intrinsic_global_atomic_comp_swap
:
6266 visit_global_atomic(ctx
, instr
);
6268 case nir_intrinsic_ssbo_atomic_add
:
6269 case nir_intrinsic_ssbo_atomic_imin
:
6270 case nir_intrinsic_ssbo_atomic_umin
:
6271 case nir_intrinsic_ssbo_atomic_imax
:
6272 case nir_intrinsic_ssbo_atomic_umax
:
6273 case nir_intrinsic_ssbo_atomic_and
:
6274 case nir_intrinsic_ssbo_atomic_or
:
6275 case nir_intrinsic_ssbo_atomic_xor
:
6276 case nir_intrinsic_ssbo_atomic_exchange
:
6277 case nir_intrinsic_ssbo_atomic_comp_swap
:
6278 visit_atomic_ssbo(ctx
, instr
);
6280 case nir_intrinsic_load_scratch
:
6281 visit_load_scratch(ctx
, instr
);
6283 case nir_intrinsic_store_scratch
:
6284 visit_store_scratch(ctx
, instr
);
6286 case nir_intrinsic_get_buffer_size
:
6287 visit_get_buffer_size(ctx
, instr
);
6289 case nir_intrinsic_control_barrier
: {
6290 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
6291 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
6292 if (workgroup_size
> ctx
->program
->wave_size
)
6293 bld
.sopp(aco_opcode::s_barrier
);
6296 case nir_intrinsic_group_memory_barrier
:
6297 case nir_intrinsic_memory_barrier
:
6298 case nir_intrinsic_memory_barrier_buffer
:
6299 case nir_intrinsic_memory_barrier_image
:
6300 case nir_intrinsic_memory_barrier_shared
:
6301 emit_memory_barrier(ctx
, instr
);
6303 case nir_intrinsic_memory_barrier_tcs_patch
:
6305 case nir_intrinsic_load_num_work_groups
: {
6306 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6307 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.num_work_groups
)));
6308 emit_split_vector(ctx
, dst
, 3);
6311 case nir_intrinsic_load_local_invocation_id
: {
6312 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6313 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.local_invocation_ids
)));
6314 emit_split_vector(ctx
, dst
, 3);
6317 case nir_intrinsic_load_work_group_id
: {
6318 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6319 struct ac_arg
*args
= ctx
->args
->ac
.workgroup_ids
;
6320 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6321 args
[0].used
? Operand(get_arg(ctx
, args
[0])) : Operand(0u),
6322 args
[1].used
? Operand(get_arg(ctx
, args
[1])) : Operand(0u),
6323 args
[2].used
? Operand(get_arg(ctx
, args
[2])) : Operand(0u));
6324 emit_split_vector(ctx
, dst
, 3);
6327 case nir_intrinsic_load_local_invocation_index
: {
6328 Temp id
= emit_mbcnt(ctx
, bld
.def(v1
));
6330 /* The tg_size bits [6:11] contain the subgroup id,
6331 * we need this multiplied by the wave size, and then OR the thread id to it.
6333 if (ctx
->program
->wave_size
== 64) {
6334 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
6335 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
6336 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
6337 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
6339 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
6340 Temp tg_num
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
6341 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
6342 bld
.vop3(aco_opcode::v_lshl_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, Operand(0x5u
), id
);
6346 case nir_intrinsic_load_subgroup_id
: {
6347 if (ctx
->stage
== compute_cs
) {
6348 bld
.sop2(aco_opcode::s_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
),
6349 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
6351 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
6355 case nir_intrinsic_load_subgroup_invocation
: {
6356 emit_mbcnt(ctx
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
6359 case nir_intrinsic_load_num_subgroups
: {
6360 if (ctx
->stage
== compute_cs
)
6361 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
),
6362 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
6364 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
6367 case nir_intrinsic_ballot
: {
6368 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6369 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6370 Definition tmp
= bld
.def(dst
.regClass());
6371 Definition lanemask_tmp
= dst
.size() == bld
.lm
.size() ? tmp
: bld
.def(src
.regClass());
6372 if (instr
->src
[0].ssa
->bit_size
== 1) {
6373 assert(src
.regClass() == bld
.lm
);
6374 bld
.sop2(Builder::s_and
, lanemask_tmp
, bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
6375 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
6376 bld
.vopc(aco_opcode::v_cmp_lg_u32
, lanemask_tmp
, Operand(0u), src
);
6377 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
6378 bld
.vopc(aco_opcode::v_cmp_lg_u64
, lanemask_tmp
, Operand(0u), src
);
6380 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6381 nir_print_instr(&instr
->instr
, stderr
);
6382 fprintf(stderr
, "\n");
6384 if (dst
.size() != bld
.lm
.size()) {
6385 /* Wave32 with ballot size set to 64 */
6386 bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
), lanemask_tmp
.getTemp(), Operand(0u));
6388 emit_wqm(ctx
, tmp
.getTemp(), dst
);
6391 case nir_intrinsic_shuffle
:
6392 case nir_intrinsic_read_invocation
: {
6393 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6394 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
]) {
6395 emit_uniform_subgroup(ctx
, instr
, src
);
6397 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
6398 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !ctx
->divergent_vals
[instr
->src
[1].ssa
->index
])
6399 tid
= bld
.as_uniform(tid
);
6400 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6401 if (src
.regClass() == v1
) {
6402 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
6403 } else if (src
.regClass() == v2
) {
6404 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6405 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6406 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
6407 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
6408 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6409 emit_split_vector(ctx
, dst
, 2);
6410 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
6411 assert(src
.regClass() == bld
.lm
);
6412 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
, tid
);
6413 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
6414 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
6415 assert(src
.regClass() == bld
.lm
);
6417 if (ctx
->program
->chip_class
<= GFX7
)
6418 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), src
, tid
);
6419 else if (ctx
->program
->wave_size
== 64)
6420 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
6422 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), tid
, src
);
6423 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
6424 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
6425 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
), dst
);
6427 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6428 nir_print_instr(&instr
->instr
, stderr
);
6429 fprintf(stderr
, "\n");
6434 case nir_intrinsic_load_sample_id
: {
6435 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6436 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
6439 case nir_intrinsic_load_sample_mask_in
: {
6440 visit_load_sample_mask_in(ctx
, instr
);
6443 case nir_intrinsic_read_first_invocation
: {
6444 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6445 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6446 if (src
.regClass() == v1
) {
6448 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
6450 } else if (src
.regClass() == v2
) {
6451 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6452 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6453 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
6454 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
6455 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6456 emit_split_vector(ctx
, dst
, 2);
6457 } else if (instr
->dest
.ssa
.bit_size
== 1) {
6458 assert(src
.regClass() == bld
.lm
);
6459 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
,
6460 bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)));
6461 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
6462 } else if (src
.regClass() == s1
) {
6463 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
6464 } else if (src
.regClass() == s2
) {
6465 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
6467 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6468 nir_print_instr(&instr
->instr
, stderr
);
6469 fprintf(stderr
, "\n");
6473 case nir_intrinsic_vote_all
: {
6474 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6475 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6476 assert(src
.regClass() == bld
.lm
);
6477 assert(dst
.regClass() == bld
.lm
);
6479 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
6480 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
6481 bld
.sop1(Builder::s_not
, Definition(dst
), bld
.def(s1
, scc
), cond
);
6484 case nir_intrinsic_vote_any
: {
6485 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6486 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6487 assert(src
.regClass() == bld
.lm
);
6488 assert(dst
.regClass() == bld
.lm
);
6490 Temp tmp
= bool_to_scalar_condition(ctx
, src
);
6491 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
6494 case nir_intrinsic_reduce
:
6495 case nir_intrinsic_inclusive_scan
:
6496 case nir_intrinsic_exclusive_scan
: {
6497 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6498 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6499 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
6500 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
6501 nir_intrinsic_cluster_size(instr
) : 0;
6502 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: ctx
->program
->wave_size
, ctx
->program
->wave_size
));
6504 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
6505 emit_uniform_subgroup(ctx
, instr
, src
);
6506 } else if (instr
->dest
.ssa
.bit_size
== 1) {
6507 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
6509 else if (op
== nir_op_iadd
)
6511 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
6513 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
6515 switch (instr
->intrinsic
) {
6516 case nir_intrinsic_reduce
:
6517 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
6519 case nir_intrinsic_exclusive_scan
:
6520 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
6522 case nir_intrinsic_inclusive_scan
:
6523 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
6528 } else if (cluster_size
== 1) {
6529 bld
.copy(Definition(dst
), src
);
6531 src
= as_vgpr(ctx
, src
);
6535 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
6550 unreachable("unknown reduction op");
6555 switch (instr
->intrinsic
) {
6556 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
6557 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
6558 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
6560 unreachable("unknown reduce intrinsic");
6563 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
6564 reduce
->operands
[0] = Operand(src
);
6565 // filled in by aco_reduce_assign.cpp, used internally as part of the
6567 assert(dst
.size() == 1 || dst
.size() == 2);
6568 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
6569 reduce
->operands
[2] = Operand(v1
.as_linear());
6571 Temp tmp_dst
= bld
.tmp(dst
.regClass());
6572 reduce
->definitions
[0] = Definition(tmp_dst
);
6573 reduce
->definitions
[1] = bld
.def(ctx
->program
->lane_mask
); // used internally
6574 reduce
->definitions
[2] = Definition();
6575 reduce
->definitions
[3] = Definition(scc
, s1
);
6576 reduce
->definitions
[4] = Definition();
6577 reduce
->reduce_op
= reduce_op
;
6578 reduce
->cluster_size
= cluster_size
;
6579 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
6581 emit_wqm(ctx
, tmp_dst
, dst
);
6585 case nir_intrinsic_quad_broadcast
: {
6586 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6587 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
6588 emit_uniform_subgroup(ctx
, instr
, src
);
6590 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6591 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
6592 uint32_t dpp_ctrl
= dpp_quad_perm(lane
, lane
, lane
, lane
);
6594 if (instr
->dest
.ssa
.bit_size
== 1) {
6595 assert(src
.regClass() == bld
.lm
);
6596 assert(dst
.regClass() == bld
.lm
);
6597 uint32_t half_mask
= 0x11111111u
<< lane
;
6598 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
6599 Temp tmp
= bld
.tmp(bld
.lm
);
6600 bld
.sop1(Builder::s_wqm
, Definition(tmp
),
6601 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), mask_tmp
,
6602 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
))));
6603 emit_wqm(ctx
, tmp
, dst
);
6604 } else if (instr
->dest
.ssa
.bit_size
== 32) {
6605 if (ctx
->program
->chip_class
>= GFX8
)
6606 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), dst
);
6608 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), dst
);
6609 } else if (instr
->dest
.ssa
.bit_size
== 64) {
6610 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6611 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6612 if (ctx
->program
->chip_class
>= GFX8
) {
6613 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
6614 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
6616 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, (1 << 15) | dpp_ctrl
));
6617 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, (1 << 15) | dpp_ctrl
));
6619 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6620 emit_split_vector(ctx
, dst
, 2);
6622 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6623 nir_print_instr(&instr
->instr
, stderr
);
6624 fprintf(stderr
, "\n");
6629 case nir_intrinsic_quad_swap_horizontal
:
6630 case nir_intrinsic_quad_swap_vertical
:
6631 case nir_intrinsic_quad_swap_diagonal
:
6632 case nir_intrinsic_quad_swizzle_amd
: {
6633 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6634 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
6635 emit_uniform_subgroup(ctx
, instr
, src
);
6638 uint16_t dpp_ctrl
= 0;
6639 switch (instr
->intrinsic
) {
6640 case nir_intrinsic_quad_swap_horizontal
:
6641 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
6643 case nir_intrinsic_quad_swap_vertical
:
6644 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
6646 case nir_intrinsic_quad_swap_diagonal
:
6647 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
6649 case nir_intrinsic_quad_swizzle_amd
:
6650 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
6655 if (ctx
->program
->chip_class
< GFX8
)
6656 dpp_ctrl
|= (1 << 15);
6658 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6659 if (instr
->dest
.ssa
.bit_size
== 1) {
6660 assert(src
.regClass() == bld
.lm
);
6661 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
6662 if (ctx
->program
->chip_class
>= GFX8
)
6663 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6665 src
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6666 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), src
);
6667 emit_wqm(ctx
, tmp
, dst
);
6668 } else if (instr
->dest
.ssa
.bit_size
== 32) {
6670 if (ctx
->program
->chip_class
>= GFX8
)
6671 tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6673 tmp
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
6674 emit_wqm(ctx
, tmp
, dst
);
6675 } else if (instr
->dest
.ssa
.bit_size
== 64) {
6676 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6677 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6678 if (ctx
->program
->chip_class
>= GFX8
) {
6679 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
6680 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
6682 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
6683 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
6685 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6686 emit_split_vector(ctx
, dst
, 2);
6688 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6689 nir_print_instr(&instr
->instr
, stderr
);
6690 fprintf(stderr
, "\n");
6694 case nir_intrinsic_masked_swizzle_amd
: {
6695 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6696 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
6697 emit_uniform_subgroup(ctx
, instr
, src
);
6700 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6701 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
6702 if (dst
.regClass() == v1
) {
6704 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
6706 } else if (dst
.regClass() == v2
) {
6707 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6708 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6709 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
6710 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
6711 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6712 emit_split_vector(ctx
, dst
, 2);
6714 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6715 nir_print_instr(&instr
->instr
, stderr
);
6716 fprintf(stderr
, "\n");
6720 case nir_intrinsic_write_invocation_amd
: {
6721 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6722 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6723 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
6724 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6725 if (dst
.regClass() == v1
) {
6726 /* src2 is ignored for writelane. RA assigns the same reg for dst */
6727 emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val
, lane
, src
), dst
);
6728 } else if (dst
.regClass() == v2
) {
6729 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
6730 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
6731 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
6732 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
6733 Temp lo
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_lo
, lane
, src_hi
));
6734 Temp hi
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_hi
, lane
, src_hi
));
6735 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6736 emit_split_vector(ctx
, dst
, 2);
6738 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6739 nir_print_instr(&instr
->instr
, stderr
);
6740 fprintf(stderr
, "\n");
6744 case nir_intrinsic_mbcnt_amd
: {
6745 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6746 RegClass rc
= RegClass(src
.type(), 1);
6747 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
6748 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
6749 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6750 Temp wqm_tmp
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(mask_lo
), Operand(mask_hi
));
6751 emit_wqm(ctx
, wqm_tmp
, dst
);
6754 case nir_intrinsic_load_helper_invocation
: {
6755 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6756 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
6757 ctx
->block
->kind
|= block_kind_needs_lowering
;
6758 ctx
->program
->needs_exact
= true;
6761 case nir_intrinsic_is_helper_invocation
: {
6762 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6763 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
6764 ctx
->block
->kind
|= block_kind_needs_lowering
;
6765 ctx
->program
->needs_exact
= true;
6768 case nir_intrinsic_demote
:
6769 bld
.pseudo(aco_opcode::p_demote_to_helper
, Operand(-1u));
6771 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
6772 ctx
->cf_info
.exec_potentially_empty_discard
= true;
6773 ctx
->block
->kind
|= block_kind_uses_demote
;
6774 ctx
->program
->needs_exact
= true;
6776 case nir_intrinsic_demote_if
: {
6777 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6778 assert(src
.regClass() == bld
.lm
);
6779 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6780 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
6782 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
6783 ctx
->cf_info
.exec_potentially_empty_discard
= true;
6784 ctx
->block
->kind
|= block_kind_uses_demote
;
6785 ctx
->program
->needs_exact
= true;
6788 case nir_intrinsic_first_invocation
: {
6789 emit_wqm(ctx
, bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)),
6790 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
6793 case nir_intrinsic_shader_clock
:
6794 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
6795 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
6797 case nir_intrinsic_load_vertex_id_zero_base
: {
6798 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6799 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
6802 case nir_intrinsic_load_first_vertex
: {
6803 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6804 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.base_vertex
));
6807 case nir_intrinsic_load_base_instance
: {
6808 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6809 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.start_instance
));
6812 case nir_intrinsic_load_instance_id
: {
6813 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6814 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.instance_id
));
6817 case nir_intrinsic_load_draw_id
: {
6818 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6819 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.draw_id
));
6822 case nir_intrinsic_load_invocation_id
: {
6823 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
6824 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6825 if (ctx
->options
->chip_class
>= GFX10
)
6826 bld
.vop2_e64(aco_opcode::v_and_b32
, Definition(dst
), Operand(127u), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
6828 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
6831 case nir_intrinsic_load_primitive_id
: {
6832 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
6833 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6834 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_prim_id
));
6837 case nir_intrinsic_emit_vertex_with_counter
: {
6838 visit_emit_vertex_with_counter(ctx
, instr
);
6841 case nir_intrinsic_end_primitive_with_counter
: {
6842 unsigned stream
= nir_intrinsic_stream_id(instr
);
6843 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(true, false, stream
));
6846 case nir_intrinsic_set_vertex_count
: {
6847 /* unused, the HW keeps track of this for us */
6851 fprintf(stderr
, "Unimplemented intrinsic instr: ");
6852 nir_print_instr(&instr
->instr
, stderr
);
6853 fprintf(stderr
, "\n");
6861 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
6862 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
6863 enum glsl_base_type
*stype
)
6865 nir_deref_instr
*texture_deref_instr
= NULL
;
6866 nir_deref_instr
*sampler_deref_instr
= NULL
;
6869 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6870 switch (instr
->src
[i
].src_type
) {
6871 case nir_tex_src_texture_deref
:
6872 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6874 case nir_tex_src_sampler_deref
:
6875 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6877 case nir_tex_src_plane
:
6878 plane
= nir_src_as_int(instr
->src
[i
].src
);
6885 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
6887 if (!sampler_deref_instr
)
6888 sampler_deref_instr
= texture_deref_instr
;
6891 assert(instr
->op
!= nir_texop_txf_ms
&&
6892 instr
->op
!= nir_texop_samples_identical
);
6893 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
6894 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
6895 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6896 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
6897 } else if (instr
->op
== nir_texop_fragment_mask_fetch
) {
6898 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6900 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
6903 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
6905 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
6906 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
6907 Builder
bld(ctx
->program
, ctx
->block
);
6909 /* to avoid unnecessary moves, we split and recombine sampler and image */
6910 Temp img
[8] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
),
6911 bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
6912 Temp samp
[4] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
6913 bld
.pseudo(aco_opcode::p_split_vector
, Definition(img
[0]), Definition(img
[1]),
6914 Definition(img
[2]), Definition(img
[3]), Definition(img
[4]),
6915 Definition(img
[5]), Definition(img
[6]), Definition(img
[7]), *res_ptr
);
6916 bld
.pseudo(aco_opcode::p_split_vector
, Definition(samp
[0]), Definition(samp
[1]),
6917 Definition(samp
[2]), Definition(samp
[3]), *samp_ptr
);
6919 samp
[0] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), samp
[0], img
[7]);
6920 *res_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
6921 img
[0], img
[1], img
[2], img
[3],
6922 img
[4], img
[5], img
[6], img
[7]);
6923 *samp_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
6924 samp
[0], samp
[1], samp
[2], samp
[3]);
6927 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
6928 instr
->op
== nir_texop_samples_identical
))
6929 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6932 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
6933 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
6935 Builder
bld(ctx
->program
, ctx
->block
);
6937 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
6938 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
6939 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
6941 Operand
neg_one(0xbf800000u
);
6942 Operand
one(0x3f800000u
);
6943 Operand
two(0x40000000u
);
6944 Operand
four(0x40800000u
);
6946 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), ma
);
6947 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
6948 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
6950 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), four
, id
);
6951 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(bld
.lm
), two
, id
);
6952 is_ma_y
= bld
.sop2(Builder::s_andn2
, bld
.hint_vcc(bld
.def(bld
.lm
)), is_ma_y
, is_ma_z
);
6953 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
);
6956 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
6957 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6958 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
6960 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6963 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
6964 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
6965 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6968 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6969 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
6971 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
6972 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
6975 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
6977 Builder
bld(ctx
->program
, ctx
->block
);
6978 Temp coord_args
[4], ma
, tc
, sc
, id
;
6979 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
6980 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
6983 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
6985 // see comment in ac_prepare_cube_coords()
6986 if (ctx
->options
->chip_class
<= GFX8
)
6987 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6990 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6992 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6993 vop3a
->operands
[0] = Operand(ma
);
6994 vop3a
->abs
[0] = true;
6995 Temp invma
= bld
.tmp(v1
);
6996 vop3a
->definitions
[0] = Definition(invma
);
6997 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6999 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
7001 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
7003 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
7005 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
7007 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
7010 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
7011 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
7013 for (unsigned i
= 0; i
< 2; i
++) {
7014 // see comment in ac_prepare_cube_coords()
7016 Temp deriv_sc
, deriv_tc
;
7017 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
7018 &deriv_ma
, &deriv_sc
, &deriv_tc
);
7020 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
7022 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
7023 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
7024 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
7025 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
7026 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
7027 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
7028 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
7031 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
7032 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
7036 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
7037 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
7041 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
7044 for (unsigned i
= 0; i
< coords
.size(); i
++)
7045 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
7047 Builder
bld(ctx
->program
, ctx
->block
);
7048 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
7050 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
7051 for (unsigned i
= 0; i
< coords
.size(); i
++)
7052 vec
->operands
[i
] = Operand(coord_vec
[i
]);
7053 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
7054 vec
->definitions
[0] = Definition(res
);
7055 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7059 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
7061 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
7063 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
7064 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
7067 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
7068 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
7069 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
7073 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
7075 Builder
bld(ctx
->program
, ctx
->block
);
7076 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
7077 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
7078 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
7079 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
7080 nir_const_value
*sample_index_cv
= NULL
;
7081 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
7082 enum glsl_base_type stype
;
7083 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
7085 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
7086 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
7087 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
7088 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
7090 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
7091 switch (instr
->src
[i
].src_type
) {
7092 case nir_tex_src_coord
:
7093 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
7095 case nir_tex_src_bias
:
7096 if (instr
->op
== nir_texop_txb
) {
7097 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7101 case nir_tex_src_lod
: {
7102 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
7104 if (val
&& val
->f32
<= 0.0) {
7107 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7112 case nir_tex_src_comparator
:
7113 if (instr
->is_shadow
) {
7114 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7118 case nir_tex_src_offset
:
7119 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7120 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
7123 case nir_tex_src_ddx
:
7124 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7127 case nir_tex_src_ddy
:
7128 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7131 case nir_tex_src_ms_index
:
7132 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7133 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
7134 has_sample_index
= true;
7136 case nir_tex_src_texture_offset
:
7137 case nir_tex_src_sampler_offset
:
7142 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
7143 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
7144 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
7146 if (instr
->op
== nir_texop_texture_samples
) {
7147 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
7149 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
7150 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
7151 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 */));
7152 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
7154 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7155 samples
, Operand(1u), bld
.scc(is_msaa
));
7159 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
7160 aco_ptr
<Instruction
> tmp_instr
;
7161 Temp acc
, pack
= Temp();
7163 uint32_t pack_const
= 0;
7164 for (unsigned i
= 0; i
< offset
.size(); i
++) {
7165 if (!const_offset
[i
])
7167 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
7170 if (offset
.type() == RegType::sgpr
) {
7171 for (unsigned i
= 0; i
< offset
.size(); i
++) {
7172 if (const_offset
[i
])
7175 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
7176 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
7179 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
7182 if (pack
== Temp()) {
7185 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
7189 if (pack_const
&& pack
!= Temp())
7190 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
7192 for (unsigned i
= 0; i
< offset
.size(); i
++) {
7193 if (const_offset
[i
])
7196 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
7197 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
7200 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
7203 if (pack
== Temp()) {
7206 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
7210 if (pack_const
&& pack
!= Temp())
7211 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
7213 if (pack_const
&& pack
== Temp())
7214 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
7215 else if (pack
== Temp())
7221 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
7222 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
7224 /* pack derivatives */
7225 if (has_ddx
|| has_ddy
) {
7226 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
7227 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
7228 ddx
, Operand(0u), ddy
, Operand(0u));
7230 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
7235 if (instr
->coord_components
> 1 &&
7236 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
7238 instr
->op
!= nir_texop_txf
)
7239 coords
= apply_round_slice(ctx
, coords
, 1);
7241 if (instr
->coord_components
> 2 &&
7242 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
7243 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
7244 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
7245 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
7247 instr
->op
!= nir_texop_txf
&&
7248 instr
->op
!= nir_texop_txf_ms
&&
7249 instr
->op
!= nir_texop_fragment_fetch
&&
7250 instr
->op
!= nir_texop_fragment_mask_fetch
)
7251 coords
= apply_round_slice(ctx
, coords
, 2);
7253 if (ctx
->options
->chip_class
== GFX9
&&
7254 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
7255 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
7256 assert(coords
.size() > 0 && coords
.size() < 3);
7258 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
7259 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
7260 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
7261 if (coords
.size() > 1)
7262 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
7263 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
7264 vec
->definitions
[0] = Definition(coords
);
7265 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7268 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
7270 if (instr
->op
== nir_texop_samples_identical
)
7271 resource
= fmask_ptr
;
7273 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
7274 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
7275 instr
->op
!= nir_texop_txs
&&
7276 instr
->op
!= nir_texop_fragment_fetch
&&
7277 instr
->op
!= nir_texop_fragment_mask_fetch
) {
7278 assert(has_sample_index
);
7279 Operand
op(sample_index
);
7280 if (sample_index_cv
)
7281 op
= Operand(sample_index_cv
->u32
);
7282 std::vector
<Temp
> fmask_load_address
;
7283 for (unsigned i
= 0; i
< coords
.size(); i
++)
7284 fmask_load_address
.emplace_back(emit_extract_vector(ctx
, coords
, i
, v1
));
7285 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, fmask_load_address
, op
, fmask_ptr
);
7288 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
7289 Temp split_coords
[coords
.size()];
7290 emit_split_vector(ctx
, coords
, coords
.size());
7291 for (unsigned i
= 0; i
< coords
.size(); i
++)
7292 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
7295 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
7296 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
7297 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
7300 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
7301 for (unsigned i
= 0; i
< coords
.size(); i
++)
7302 vec
->operands
[i
] = Operand(split_coords
[i
]);
7303 coords
= bld
.tmp(coords
.regClass());
7304 vec
->definitions
[0] = Definition(coords
);
7305 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7310 /* Build tex instruction */
7311 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
7312 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
7313 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
7315 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7318 /* gather4 selects the component by dmask and always returns vec4 */
7319 if (instr
->op
== nir_texop_tg4
) {
7320 assert(instr
->dest
.ssa
.num_components
== 4);
7321 if (instr
->is_shadow
)
7324 dmask
= 1 << instr
->component
;
7325 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
7326 tmp_dst
= bld
.tmp(v4
);
7327 } else if (instr
->op
== nir_texop_samples_identical
) {
7328 tmp_dst
= bld
.tmp(v1
);
7329 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
7330 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
7333 aco_ptr
<MIMG_instruction
> tex
;
7334 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
7336 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
7338 bool div_by_6
= instr
->op
== nir_texop_txs
&&
7339 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
7342 if (tmp_dst
.id() == dst
.id() && div_by_6
)
7343 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
7345 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
7346 tex
->operands
[0] = Operand(resource
);
7347 tex
->operands
[1] = Operand(s4
); /* no sampler */
7348 tex
->operands
[2] = Operand(as_vgpr(ctx
,lod
));
7349 if (ctx
->options
->chip_class
== GFX9
&&
7350 instr
->op
== nir_texop_txs
&&
7351 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
7353 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
7354 } else if (instr
->op
== nir_texop_query_levels
) {
7355 tex
->dmask
= 1 << 3;
7360 tex
->definitions
[0] = Definition(tmp_dst
);
7362 tex
->can_reorder
= true;
7363 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7366 /* divide 3rd value by 6 by multiplying with magic number */
7367 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
7368 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
7369 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
7370 assert(instr
->dest
.ssa
.num_components
== 3);
7371 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
7372 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
7373 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
7374 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
7379 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
7383 Temp tg4_compare_cube_wa64
= Temp();
7385 if (tg4_integer_workarounds
) {
7386 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
7387 tex
->operands
[0] = Operand(resource
);
7388 tex
->operands
[1] = Operand(s4
); /* no sampler */
7389 tex
->operands
[2] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
7393 Temp size
= bld
.tmp(v2
);
7394 tex
->definitions
[0] = Definition(size
);
7395 tex
->can_reorder
= true;
7396 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7397 emit_split_vector(ctx
, size
, size
.size());
7400 for (unsigned i
= 0; i
< 2; i
++) {
7401 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
7402 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
7403 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
7404 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
7407 Temp orig_coords
[2] = {
7408 emit_extract_vector(ctx
, coords
, 0, v1
),
7409 emit_extract_vector(ctx
, coords
, 1, v1
)};
7410 Temp new_coords
[2] = {
7411 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
7412 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
7415 if (tg4_integer_cube_workaround
) {
7416 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
7417 Temp desc
[resource
.size()];
7418 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
7419 Format::PSEUDO
, 1, resource
.size())};
7420 split
->operands
[0] = Operand(resource
);
7421 for (unsigned i
= 0; i
< resource
.size(); i
++) {
7422 desc
[i
] = bld
.tmp(s1
);
7423 split
->definitions
[i
] = Definition(desc
[i
]);
7425 ctx
->block
->instructions
.emplace_back(std::move(split
));
7427 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
7428 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
7429 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
7432 if (stype
== GLSL_TYPE_UINT
) {
7433 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
7434 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
7435 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
7436 bld
.scc(compare_cube_wa
));
7438 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
7439 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
7440 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
7441 bld
.scc(compare_cube_wa
));
7443 tg4_compare_cube_wa64
= bld
.tmp(bld
.lm
);
7444 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
7446 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
7448 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
7449 Operand((uint32_t)C_008F14_NUM_FORMAT
));
7450 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
7452 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
7453 Format::PSEUDO
, resource
.size(), 1)};
7454 for (unsigned i
= 0; i
< resource
.size(); i
++)
7455 vec
->operands
[i
] = Operand(desc
[i
]);
7456 resource
= bld
.tmp(resource
.regClass());
7457 vec
->definitions
[0] = Definition(resource
);
7458 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7460 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7461 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
7462 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7463 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
7466 if (coords
.size() == 3) {
7467 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
7468 new_coords
[0], new_coords
[1],
7469 emit_extract_vector(ctx
, coords
, 2, v1
));
7471 assert(coords
.size() == 2);
7472 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
7473 new_coords
[0], new_coords
[1]);
7477 std::vector
<Operand
> args
;
7479 args
.emplace_back(Operand(offset
));
7481 args
.emplace_back(Operand(bias
));
7483 args
.emplace_back(Operand(compare
));
7485 args
.emplace_back(Operand(derivs
));
7486 args
.emplace_back(Operand(coords
));
7487 if (has_sample_index
)
7488 args
.emplace_back(Operand(sample_index
));
7490 args
.emplace_back(lod
);
7493 if (args
.size() > 1) {
7494 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
7496 for (unsigned i
= 0; i
< args
.size(); i
++) {
7497 size
+= args
[i
].size();
7498 vec
->operands
[i
] = args
[i
];
7500 RegClass rc
= RegClass(RegType::vgpr
, size
);
7501 Temp tmp
= bld
.tmp(rc
);
7502 vec
->definitions
[0] = Definition(tmp
);
7503 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7506 assert(args
[0].isTemp());
7507 arg
= as_vgpr(ctx
, args
[0].getTemp());
7510 /* we don't need the bias, sample index, compare value or offset to be
7511 * computed in WQM but if the p_create_vector copies the coordinates, then it
7512 * needs to be in WQM */
7513 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
7514 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
7515 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
7516 arg
= emit_wqm(ctx
, arg
, bld
.tmp(arg
.regClass()), true);
7518 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
7519 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
7521 assert(coords
.size() == 1);
7522 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
7526 op
= aco_opcode::buffer_load_format_x
; break;
7528 op
= aco_opcode::buffer_load_format_xy
; break;
7530 op
= aco_opcode::buffer_load_format_xyz
; break;
7532 op
= aco_opcode::buffer_load_format_xyzw
; break;
7534 unreachable("Tex instruction loads more than 4 components.");
7537 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
7538 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
7541 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
7543 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
7544 mubuf
->operands
[0] = Operand(resource
);
7545 mubuf
->operands
[1] = Operand(coords
);
7546 mubuf
->operands
[2] = Operand((uint32_t) 0);
7547 mubuf
->definitions
[0] = Definition(tmp_dst
);
7548 mubuf
->idxen
= true;
7549 mubuf
->can_reorder
= true;
7550 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
7552 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
7557 if (instr
->op
== nir_texop_txf
||
7558 instr
->op
== nir_texop_txf_ms
||
7559 instr
->op
== nir_texop_samples_identical
||
7560 instr
->op
== nir_texop_fragment_fetch
||
7561 instr
->op
== nir_texop_fragment_mask_fetch
) {
7562 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
7563 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 3, 1));
7564 tex
->operands
[0] = Operand(resource
);
7565 tex
->operands
[1] = Operand(s4
); /* no sampler */
7566 tex
->operands
[2] = Operand(arg
);
7571 tex
->definitions
[0] = Definition(tmp_dst
);
7572 tex
->can_reorder
= true;
7573 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7575 if (instr
->op
== nir_texop_samples_identical
) {
7576 assert(dmask
== 1 && dst
.regClass() == v1
);
7577 assert(dst
.id() != tmp_dst
.id());
7579 Temp tmp
= bld
.tmp(bld
.lm
);
7580 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
7581 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
7584 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
7589 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
7590 aco_opcode opcode
= aco_opcode::image_sample
;
7591 if (has_offset
) { /* image_sample_*_o */
7593 opcode
= aco_opcode::image_sample_c_o
;
7595 opcode
= aco_opcode::image_sample_c_d_o
;
7597 opcode
= aco_opcode::image_sample_c_b_o
;
7599 opcode
= aco_opcode::image_sample_c_lz_o
;
7601 opcode
= aco_opcode::image_sample_c_l_o
;
7603 opcode
= aco_opcode::image_sample_o
;
7605 opcode
= aco_opcode::image_sample_d_o
;
7607 opcode
= aco_opcode::image_sample_b_o
;
7609 opcode
= aco_opcode::image_sample_lz_o
;
7611 opcode
= aco_opcode::image_sample_l_o
;
7613 } else { /* no offset */
7615 opcode
= aco_opcode::image_sample_c
;
7617 opcode
= aco_opcode::image_sample_c_d
;
7619 opcode
= aco_opcode::image_sample_c_b
;
7621 opcode
= aco_opcode::image_sample_c_lz
;
7623 opcode
= aco_opcode::image_sample_c_l
;
7625 opcode
= aco_opcode::image_sample
;
7627 opcode
= aco_opcode::image_sample_d
;
7629 opcode
= aco_opcode::image_sample_b
;
7631 opcode
= aco_opcode::image_sample_lz
;
7633 opcode
= aco_opcode::image_sample_l
;
7637 if (instr
->op
== nir_texop_tg4
) {
7639 opcode
= aco_opcode::image_gather4_lz_o
;
7641 opcode
= aco_opcode::image_gather4_c_lz_o
;
7643 opcode
= aco_opcode::image_gather4_lz
;
7645 opcode
= aco_opcode::image_gather4_c_lz
;
7647 } else if (instr
->op
== nir_texop_lod
) {
7648 opcode
= aco_opcode::image_get_lod
;
7651 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
7652 tex
->operands
[0] = Operand(resource
);
7653 tex
->operands
[1] = Operand(sampler
);
7654 tex
->operands
[2] = Operand(arg
);
7658 tex
->definitions
[0] = Definition(tmp_dst
);
7659 tex
->can_reorder
= true;
7660 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7662 if (tg4_integer_cube_workaround
) {
7663 assert(tmp_dst
.id() != dst
.id());
7664 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
7666 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
7668 for (unsigned i
= 0; i
< dst
.size(); i
++) {
7669 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
7671 if (stype
== GLSL_TYPE_UINT
)
7672 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
7674 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
7675 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
7677 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
7678 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
7679 val
[0], val
[1], val
[2], val
[3]);
7681 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
7682 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
7687 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
7689 Temp tmp
= get_ssa_temp(ctx
, ssa
);
7690 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
7691 return Operand(tmp
.regClass());
7693 return Operand(tmp
);
7696 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
7698 aco_ptr
<Pseudo_instruction
> phi
;
7699 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7700 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == ctx
->program
->lane_mask
);
7702 bool logical
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
];
7703 logical
|= ctx
->block
->kind
& block_kind_merge
;
7704 aco_opcode opcode
= logical
? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
7706 /* we want a sorted list of sources, since the predecessor list is also sorted */
7707 std::map
<unsigned, nir_ssa_def
*> phi_src
;
7708 nir_foreach_phi_src(src
, instr
)
7709 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
7711 std::vector
<unsigned>& preds
= logical
? ctx
->block
->logical_preds
: ctx
->block
->linear_preds
;
7712 unsigned num_operands
= 0;
7713 Operand operands
[std::max(exec_list_length(&instr
->srcs
), (unsigned)preds
.size())];
7714 unsigned num_defined
= 0;
7715 unsigned cur_pred_idx
= 0;
7716 for (std::pair
<unsigned, nir_ssa_def
*> src
: phi_src
) {
7717 if (cur_pred_idx
< preds
.size()) {
7718 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
7719 unsigned block
= ctx
->cf_info
.nir_to_aco
[src
.first
];
7720 unsigned skipped
= 0;
7721 while (cur_pred_idx
+ skipped
< preds
.size() && preds
[cur_pred_idx
+ skipped
] != block
)
7723 if (cur_pred_idx
+ skipped
< preds
.size()) {
7724 for (unsigned i
= 0; i
< skipped
; i
++)
7725 operands
[num_operands
++] = Operand(dst
.regClass());
7726 cur_pred_idx
+= skipped
;
7732 Operand op
= get_phi_operand(ctx
, src
.second
);
7733 operands
[num_operands
++] = op
;
7734 num_defined
+= !op
.isUndefined();
7736 /* handle block_kind_continue_or_break at loop exit blocks */
7737 while (cur_pred_idx
++ < preds
.size())
7738 operands
[num_operands
++] = Operand(dst
.regClass());
7740 if (num_defined
== 0) {
7741 Builder
bld(ctx
->program
, ctx
->block
);
7742 if (dst
.regClass() == s1
) {
7743 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
7744 } else if (dst
.regClass() == v1
) {
7745 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
7747 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
7748 for (unsigned i
= 0; i
< dst
.size(); i
++)
7749 vec
->operands
[i
] = Operand(0u);
7750 vec
->definitions
[0] = Definition(dst
);
7751 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7756 /* we can use a linear phi in some cases if one src is undef */
7757 if (dst
.is_linear() && ctx
->block
->kind
& block_kind_merge
&& num_defined
== 1) {
7758 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, num_operands
, 1));
7760 Block
*linear_else
= &ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
7761 Block
*invert
= &ctx
->program
->blocks
[linear_else
->linear_preds
[0]];
7762 assert(invert
->kind
& block_kind_invert
);
7764 unsigned then_block
= invert
->linear_preds
[0];
7766 Block
* insert_block
= NULL
;
7767 for (unsigned i
= 0; i
< num_operands
; i
++) {
7768 Operand op
= operands
[i
];
7769 if (op
.isUndefined())
7771 insert_block
= ctx
->block
->logical_preds
[i
] == then_block
? invert
: ctx
->block
;
7772 phi
->operands
[0] = op
;
7775 assert(insert_block
); /* should be handled by the "num_defined == 0" case above */
7776 phi
->operands
[1] = Operand(dst
.regClass());
7777 phi
->definitions
[0] = Definition(dst
);
7778 insert_block
->instructions
.emplace(insert_block
->instructions
.begin(), std::move(phi
));
7782 /* try to scalarize vector phis */
7783 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
7784 // TODO: scalarize linear phis on divergent ifs
7785 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
7786 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> new_vec
;
7787 for (unsigned i
= 0; can_scalarize
&& (i
< num_operands
); i
++) {
7788 Operand src
= operands
[i
];
7789 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end())
7790 can_scalarize
= false;
7792 if (can_scalarize
) {
7793 unsigned num_components
= instr
->dest
.ssa
.num_components
;
7794 assert(dst
.size() % num_components
== 0);
7795 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
7797 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
7798 for (unsigned k
= 0; k
< num_components
; k
++) {
7799 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
7800 for (unsigned i
= 0; i
< num_operands
; i
++) {
7801 Operand src
= operands
[i
];
7802 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
7804 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
7805 phi
->definitions
[0] = Definition(phi_dst
);
7806 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
7807 new_vec
[k
] = phi_dst
;
7808 vec
->operands
[k
] = Operand(phi_dst
);
7810 vec
->definitions
[0] = Definition(dst
);
7811 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7812 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
7817 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
7818 for (unsigned i
= 0; i
< num_operands
; i
++)
7819 phi
->operands
[i
] = operands
[i
];
7820 phi
->definitions
[0] = Definition(dst
);
7821 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
7825 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
7827 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
7829 assert(dst
.type() == RegType::sgpr
);
7831 if (dst
.size() == 1) {
7832 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
7834 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
7835 for (unsigned i
= 0; i
< dst
.size(); i
++)
7836 vec
->operands
[i
] = Operand(0u);
7837 vec
->definitions
[0] = Definition(dst
);
7838 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7842 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
7844 Builder
bld(ctx
->program
, ctx
->block
);
7845 Block
*logical_target
;
7846 append_logical_end(ctx
->block
);
7847 unsigned idx
= ctx
->block
->index
;
7849 switch (instr
->type
) {
7850 case nir_jump_break
:
7851 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
7852 add_logical_edge(idx
, logical_target
);
7853 ctx
->block
->kind
|= block_kind_break
;
7855 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
7856 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
7857 /* uniform break - directly jump out of the loop */
7858 ctx
->block
->kind
|= block_kind_uniform
;
7859 ctx
->cf_info
.has_branch
= true;
7860 bld
.branch(aco_opcode::p_branch
);
7861 add_linear_edge(idx
, logical_target
);
7864 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
7865 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
7867 case nir_jump_continue
:
7868 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
7869 add_logical_edge(idx
, logical_target
);
7870 ctx
->block
->kind
|= block_kind_continue
;
7872 if (ctx
->cf_info
.parent_if
.is_divergent
) {
7873 /* for potential uniform breaks after this continue,
7874 we must ensure that they are handled correctly */
7875 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
7876 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
7877 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
7879 /* uniform continue - directly jump to the loop header */
7880 ctx
->block
->kind
|= block_kind_uniform
;
7881 ctx
->cf_info
.has_branch
= true;
7882 bld
.branch(aco_opcode::p_branch
);
7883 add_linear_edge(idx
, logical_target
);
7888 fprintf(stderr
, "Unknown NIR jump instr: ");
7889 nir_print_instr(&instr
->instr
, stderr
);
7890 fprintf(stderr
, "\n");
7894 if (ctx
->cf_info
.parent_if
.is_divergent
&& !ctx
->cf_info
.exec_potentially_empty_break
) {
7895 ctx
->cf_info
.exec_potentially_empty_break
= true;
7896 ctx
->cf_info
.exec_potentially_empty_break_depth
= ctx
->cf_info
.loop_nest_depth
;
7899 /* remove critical edges from linear CFG */
7900 bld
.branch(aco_opcode::p_branch
);
7901 Block
* break_block
= ctx
->program
->create_and_insert_block();
7902 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7903 break_block
->kind
|= block_kind_uniform
;
7904 add_linear_edge(idx
, break_block
);
7905 /* the loop_header pointer might be invalidated by this point */
7906 if (instr
->type
== nir_jump_continue
)
7907 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
7908 add_linear_edge(break_block
->index
, logical_target
);
7909 bld
.reset(break_block
);
7910 bld
.branch(aco_opcode::p_branch
);
7912 Block
* continue_block
= ctx
->program
->create_and_insert_block();
7913 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7914 add_linear_edge(idx
, continue_block
);
7915 append_logical_start(continue_block
);
7916 ctx
->block
= continue_block
;
7920 void visit_block(isel_context
*ctx
, nir_block
*block
)
7922 nir_foreach_instr(instr
, block
) {
7923 switch (instr
->type
) {
7924 case nir_instr_type_alu
:
7925 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
7927 case nir_instr_type_load_const
:
7928 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
7930 case nir_instr_type_intrinsic
:
7931 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
7933 case nir_instr_type_tex
:
7934 visit_tex(ctx
, nir_instr_as_tex(instr
));
7936 case nir_instr_type_phi
:
7937 visit_phi(ctx
, nir_instr_as_phi(instr
));
7939 case nir_instr_type_ssa_undef
:
7940 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
7942 case nir_instr_type_deref
:
7944 case nir_instr_type_jump
:
7945 visit_jump(ctx
, nir_instr_as_jump(instr
));
7948 fprintf(stderr
, "Unknown NIR instr type: ");
7949 nir_print_instr(instr
, stderr
);
7950 fprintf(stderr
, "\n");
7955 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7956 ctx
->cf_info
.nir_to_aco
[block
->index
] = ctx
->block
->index
;
7961 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
7963 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
7964 append_logical_end(ctx
->block
);
7965 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
7966 Builder
bld(ctx
->program
, ctx
->block
);
7967 bld
.branch(aco_opcode::p_branch
);
7968 unsigned loop_preheader_idx
= ctx
->block
->index
;
7970 Block loop_exit
= Block();
7971 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7972 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
7974 Block
* loop_header
= ctx
->program
->create_and_insert_block();
7975 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
7976 loop_header
->kind
|= block_kind_loop_header
;
7977 add_edge(loop_preheader_idx
, loop_header
);
7978 ctx
->block
= loop_header
;
7980 /* emit loop body */
7981 unsigned loop_header_idx
= loop_header
->index
;
7982 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
7983 append_logical_start(ctx
->block
);
7984 visit_cf_list(ctx
, &loop
->body
);
7986 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7987 if (!ctx
->cf_info
.has_branch
) {
7988 append_logical_end(ctx
->block
);
7989 if (ctx
->cf_info
.exec_potentially_empty_discard
|| ctx
->cf_info
.exec_potentially_empty_break
) {
7990 /* Discards can result in code running with an empty exec mask.
7991 * This would result in divergent breaks not ever being taken. As a
7992 * workaround, break the loop when the loop mask is empty instead of
7993 * always continuing. */
7994 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
7995 unsigned block_idx
= ctx
->block
->index
;
7997 /* create helper blocks to avoid critical edges */
7998 Block
*break_block
= ctx
->program
->create_and_insert_block();
7999 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8000 break_block
->kind
= block_kind_uniform
;
8001 bld
.reset(break_block
);
8002 bld
.branch(aco_opcode::p_branch
);
8003 add_linear_edge(block_idx
, break_block
);
8004 add_linear_edge(break_block
->index
, &loop_exit
);
8006 Block
*continue_block
= ctx
->program
->create_and_insert_block();
8007 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8008 continue_block
->kind
= block_kind_uniform
;
8009 bld
.reset(continue_block
);
8010 bld
.branch(aco_opcode::p_branch
);
8011 add_linear_edge(block_idx
, continue_block
);
8012 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
8014 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8015 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
8016 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
8018 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
8019 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8020 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
8022 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
8025 bld
.reset(ctx
->block
);
8026 bld
.branch(aco_opcode::p_branch
);
8029 /* fixup phis in loop header from unreachable blocks */
8030 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
8031 bool linear
= ctx
->cf_info
.has_branch
;
8032 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
8033 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
8034 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
8035 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
8036 /* the last operand should be the one that needs to be removed */
8037 instr
->operands
.pop_back();
8038 } else if (!is_phi(instr
)) {
8044 ctx
->cf_info
.has_branch
= false;
8046 // TODO: if the loop has not a single exit, we must add one °°
8047 /* emit loop successor block */
8048 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
8049 append_logical_start(ctx
->block
);
8052 // TODO: check if it is beneficial to not branch on continues
8053 /* trim linear phis in loop header */
8054 for (auto&& instr
: loop_entry
->instructions
) {
8055 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
8056 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
8057 new_phi
->definitions
[0] = instr
->definitions
[0];
8058 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
8059 new_phi
->operands
[i
] = instr
->operands
[i
];
8060 /* check that the remaining operands are all the same */
8061 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
8062 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
8063 instr
.swap(new_phi
);
8064 } else if (instr
->opcode
== aco_opcode::p_phi
) {
8073 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
8077 append_logical_end(ctx
->block
);
8078 ctx
->block
->kind
|= block_kind_branch
;
8080 /* branch to linear then block */
8081 assert(cond
.regClass() == ctx
->program
->lane_mask
);
8082 aco_ptr
<Pseudo_branch_instruction
> branch
;
8083 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
8084 branch
->operands
[0] = Operand(cond
);
8085 ctx
->block
->instructions
.push_back(std::move(branch
));
8087 ic
->BB_if_idx
= ctx
->block
->index
;
8088 ic
->BB_invert
= Block();
8089 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8090 /* Invert blocks are intentionally not marked as top level because they
8091 * are not part of the logical cfg. */
8092 ic
->BB_invert
.kind
|= block_kind_invert
;
8093 ic
->BB_endif
= Block();
8094 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8095 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
8097 ic
->exec_potentially_empty_discard_old
= ctx
->cf_info
.exec_potentially_empty_discard
;
8098 ic
->exec_potentially_empty_break_old
= ctx
->cf_info
.exec_potentially_empty_break
;
8099 ic
->exec_potentially_empty_break_depth_old
= ctx
->cf_info
.exec_potentially_empty_break_depth
;
8100 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
8101 ctx
->cf_info
.parent_if
.is_divergent
= true;
8103 /* divergent branches use cbranch_execz */
8104 ctx
->cf_info
.exec_potentially_empty_discard
= false;
8105 ctx
->cf_info
.exec_potentially_empty_break
= false;
8106 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8108 /** emit logical then block */
8109 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
8110 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8111 add_edge(ic
->BB_if_idx
, BB_then_logical
);
8112 ctx
->block
= BB_then_logical
;
8113 append_logical_start(BB_then_logical
);
8116 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
8118 Block
*BB_then_logical
= ctx
->block
;
8119 append_logical_end(BB_then_logical
);
8120 /* branch from logical then block to invert block */
8121 aco_ptr
<Pseudo_branch_instruction
> branch
;
8122 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8123 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
8124 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
8125 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8126 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
8127 BB_then_logical
->kind
|= block_kind_uniform
;
8128 assert(!ctx
->cf_info
.has_branch
);
8129 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
8130 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
8132 /** emit linear then block */
8133 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
8134 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8135 BB_then_linear
->kind
|= block_kind_uniform
;
8136 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
8137 /* branch from linear then block to invert block */
8138 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8139 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
8140 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
8142 /** emit invert merge block */
8143 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
8144 ic
->invert_idx
= ctx
->block
->index
;
8146 /* branch to linear else block (skip else) */
8147 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
8148 branch
->operands
[0] = Operand(ic
->cond
);
8149 ctx
->block
->instructions
.push_back(std::move(branch
));
8151 ic
->exec_potentially_empty_discard_old
|= ctx
->cf_info
.exec_potentially_empty_discard
;
8152 ic
->exec_potentially_empty_break_old
|= ctx
->cf_info
.exec_potentially_empty_break
;
8153 ic
->exec_potentially_empty_break_depth_old
=
8154 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
8155 /* divergent branches use cbranch_execz */
8156 ctx
->cf_info
.exec_potentially_empty_discard
= false;
8157 ctx
->cf_info
.exec_potentially_empty_break
= false;
8158 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8160 /** emit logical else block */
8161 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
8162 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8163 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
8164 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
8165 ctx
->block
= BB_else_logical
;
8166 append_logical_start(BB_else_logical
);
8169 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
8171 Block
*BB_else_logical
= ctx
->block
;
8172 append_logical_end(BB_else_logical
);
8174 /* branch from logical else block to endif block */
8175 aco_ptr
<Pseudo_branch_instruction
> branch
;
8176 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8177 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
8178 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
8179 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8180 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
8181 BB_else_logical
->kind
|= block_kind_uniform
;
8183 assert(!ctx
->cf_info
.has_branch
);
8184 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
8187 /** emit linear else block */
8188 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
8189 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8190 BB_else_linear
->kind
|= block_kind_uniform
;
8191 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
8193 /* branch from linear else block to endif block */
8194 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8195 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
8196 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
8199 /** emit endif merge block */
8200 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
8201 append_logical_start(ctx
->block
);
8204 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
8205 ctx
->cf_info
.exec_potentially_empty_discard
|= ic
->exec_potentially_empty_discard_old
;
8206 ctx
->cf_info
.exec_potentially_empty_break
|= ic
->exec_potentially_empty_break_old
;
8207 ctx
->cf_info
.exec_potentially_empty_break_depth
=
8208 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
8209 if (ctx
->cf_info
.loop_nest_depth
== ctx
->cf_info
.exec_potentially_empty_break_depth
&&
8210 !ctx
->cf_info
.parent_if
.is_divergent
) {
8211 ctx
->cf_info
.exec_potentially_empty_break
= false;
8212 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8214 /* uniform control flow never has an empty exec-mask */
8215 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
) {
8216 ctx
->cf_info
.exec_potentially_empty_discard
= false;
8217 ctx
->cf_info
.exec_potentially_empty_break
= false;
8218 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8222 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
8224 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
8225 Builder
bld(ctx
->program
, ctx
->block
);
8226 aco_ptr
<Pseudo_branch_instruction
> branch
;
8228 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
8230 * Uniform conditionals are represented in the following way*) :
8232 * The linear and logical CFG:
8235 * BB_THEN (logical) BB_ELSE (logical)
8239 * *) Exceptions may be due to break and continue statements within loops
8240 * If a break/continue happens within uniform control flow, it branches
8241 * to the loop exit/entry block. Otherwise, it branches to the next
8244 append_logical_end(ctx
->block
);
8245 ctx
->block
->kind
|= block_kind_uniform
;
8248 assert(cond
.regClass() == bld
.lm
);
8249 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
8250 cond
= bool_to_scalar_condition(ctx
, cond
);
8252 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
8253 branch
->operands
[0] = Operand(cond
);
8254 branch
->operands
[0].setFixed(scc
);
8255 ctx
->block
->instructions
.emplace_back(std::move(branch
));
8257 unsigned BB_if_idx
= ctx
->block
->index
;
8258 Block BB_endif
= Block();
8259 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8260 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
8262 /** emit then block */
8263 Block
* BB_then
= ctx
->program
->create_and_insert_block();
8264 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8265 add_edge(BB_if_idx
, BB_then
);
8266 append_logical_start(BB_then
);
8267 ctx
->block
= BB_then
;
8268 visit_cf_list(ctx
, &if_stmt
->then_list
);
8269 BB_then
= ctx
->block
;
8270 bool then_branch
= ctx
->cf_info
.has_branch
;
8271 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
8274 append_logical_end(BB_then
);
8275 /* branch from then block to endif block */
8276 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8277 BB_then
->instructions
.emplace_back(std::move(branch
));
8278 add_linear_edge(BB_then
->index
, &BB_endif
);
8279 if (!then_branch_divergent
)
8280 add_logical_edge(BB_then
->index
, &BB_endif
);
8281 BB_then
->kind
|= block_kind_uniform
;
8284 ctx
->cf_info
.has_branch
= false;
8285 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
8287 /** emit else block */
8288 Block
* BB_else
= ctx
->program
->create_and_insert_block();
8289 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8290 add_edge(BB_if_idx
, BB_else
);
8291 append_logical_start(BB_else
);
8292 ctx
->block
= BB_else
;
8293 visit_cf_list(ctx
, &if_stmt
->else_list
);
8294 BB_else
= ctx
->block
;
8296 if (!ctx
->cf_info
.has_branch
) {
8297 append_logical_end(BB_else
);
8298 /* branch from then block to endif block */
8299 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8300 BB_else
->instructions
.emplace_back(std::move(branch
));
8301 add_linear_edge(BB_else
->index
, &BB_endif
);
8302 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8303 add_logical_edge(BB_else
->index
, &BB_endif
);
8304 BB_else
->kind
|= block_kind_uniform
;
8307 ctx
->cf_info
.has_branch
&= then_branch
;
8308 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
8310 /** emit endif merge block */
8311 if (!ctx
->cf_info
.has_branch
) {
8312 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
8313 append_logical_start(ctx
->block
);
8315 } else { /* non-uniform condition */
8317 * To maintain a logical and linear CFG without critical edges,
8318 * non-uniform conditionals are represented in the following way*) :
8323 * BB_THEN (logical) BB_THEN (linear)
8325 * BB_INVERT (linear)
8327 * BB_ELSE (logical) BB_ELSE (linear)
8334 * BB_THEN (logical) BB_ELSE (logical)
8338 * *) Exceptions may be due to break and continue statements within loops
8343 begin_divergent_if_then(ctx
, &ic
, cond
);
8344 visit_cf_list(ctx
, &if_stmt
->then_list
);
8346 begin_divergent_if_else(ctx
, &ic
);
8347 visit_cf_list(ctx
, &if_stmt
->else_list
);
8349 end_divergent_if(ctx
, &ic
);
8353 static void visit_cf_list(isel_context
*ctx
,
8354 struct exec_list
*list
)
8356 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
8357 switch (node
->type
) {
8358 case nir_cf_node_block
:
8359 visit_block(ctx
, nir_cf_node_as_block(node
));
8361 case nir_cf_node_if
:
8362 visit_if(ctx
, nir_cf_node_as_if(node
));
8364 case nir_cf_node_loop
:
8365 visit_loop(ctx
, nir_cf_node_as_loop(node
));
8368 unreachable("unimplemented cf list type");
8373 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
8375 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
8376 uint64_t mask
= ctx
->outputs
.mask
[slot
];
8377 if (!is_pos
&& !mask
)
8379 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
8381 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
8382 exp
->enabled_mask
= mask
;
8383 for (unsigned i
= 0; i
< 4; ++i
) {
8384 if (mask
& (1 << i
))
8385 exp
->operands
[i
] = Operand(ctx
->outputs
.outputs
[slot
][i
]);
8387 exp
->operands
[i
] = Operand(v1
);
8389 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
8390 * Setting valid_mask=1 prevents it and has no other effect.
8392 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& is_pos
&& *next_pos
== 0;
8394 exp
->compressed
= false;
8396 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
8398 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
8399 ctx
->block
->instructions
.emplace_back(std::move(exp
));
8402 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
8404 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
8405 exp
->enabled_mask
= 0;
8406 for (unsigned i
= 0; i
< 4; ++i
)
8407 exp
->operands
[i
] = Operand(v1
);
8408 if (ctx
->outputs
.mask
[VARYING_SLOT_PSIZ
]) {
8409 exp
->operands
[0] = Operand(ctx
->outputs
.outputs
[VARYING_SLOT_PSIZ
][0]);
8410 exp
->enabled_mask
|= 0x1;
8412 if (ctx
->outputs
.mask
[VARYING_SLOT_LAYER
]) {
8413 exp
->operands
[2] = Operand(ctx
->outputs
.outputs
[VARYING_SLOT_LAYER
][0]);
8414 exp
->enabled_mask
|= 0x4;
8416 if (ctx
->outputs
.mask
[VARYING_SLOT_VIEWPORT
]) {
8417 if (ctx
->options
->chip_class
< GFX9
) {
8418 exp
->operands
[3] = Operand(ctx
->outputs
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
8419 exp
->enabled_mask
|= 0x8;
8421 Builder
bld(ctx
->program
, ctx
->block
);
8423 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
8424 Operand(ctx
->outputs
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
8425 if (exp
->operands
[2].isTemp())
8426 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
8428 exp
->operands
[2] = Operand(out
);
8429 exp
->enabled_mask
|= 0x4;
8432 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& *next_pos
== 0;
8434 exp
->compressed
= false;
8435 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
8436 ctx
->block
->instructions
.emplace_back(std::move(exp
));
8439 static void create_vs_exports(isel_context
*ctx
)
8441 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
8443 if (outinfo
->export_prim_id
) {
8444 ctx
->outputs
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
8445 ctx
->outputs
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = get_arg(ctx
, ctx
->args
->vs_prim_id
);
8448 if (ctx
->options
->key
.has_multiview_view_index
) {
8449 ctx
->outputs
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
8450 ctx
->outputs
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, get_arg(ctx
, ctx
->args
->ac
.view_index
));
8453 /* the order these position exports are created is important */
8455 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
8456 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
8457 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
8459 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
8460 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
8461 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
8462 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
8464 if (ctx
->export_clip_dists
) {
8465 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
8466 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
8467 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
8468 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
8471 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
8472 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
8473 i
!= VARYING_SLOT_PRIMITIVE_ID
)
8476 export_vs_varying(ctx
, i
, false, NULL
);
8480 static void export_fs_mrt_z(isel_context
*ctx
)
8482 Builder
bld(ctx
->program
, ctx
->block
);
8483 unsigned enabled_channels
= 0;
8487 for (unsigned i
= 0; i
< 4; ++i
) {
8488 values
[i
] = Operand(v1
);
8491 /* Both stencil and sample mask only need 16-bits. */
8492 if (!ctx
->program
->info
->ps
.writes_z
&&
8493 (ctx
->program
->info
->ps
.writes_stencil
||
8494 ctx
->program
->info
->ps
.writes_sample_mask
)) {
8495 compr
= true; /* COMPR flag */
8497 if (ctx
->program
->info
->ps
.writes_stencil
) {
8498 /* Stencil should be in X[23:16]. */
8499 values
[0] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_STENCIL
][0]);
8500 values
[0] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u), values
[0]);
8501 enabled_channels
|= 0x3;
8504 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
8505 /* SampleMask should be in Y[15:0]. */
8506 values
[1] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_SAMPLE_MASK
][0]);
8507 enabled_channels
|= 0xc;
8510 if (ctx
->program
->info
->ps
.writes_z
) {
8511 values
[0] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_DEPTH
][0]);
8512 enabled_channels
|= 0x1;
8515 if (ctx
->program
->info
->ps
.writes_stencil
) {
8516 values
[1] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_STENCIL
][0]);
8517 enabled_channels
|= 0x2;
8520 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
8521 values
[2] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_SAMPLE_MASK
][0]);
8522 enabled_channels
|= 0x4;
8526 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
8527 * writemask component.
8529 if (ctx
->options
->chip_class
== GFX6
&&
8530 ctx
->options
->family
!= CHIP_OLAND
&&
8531 ctx
->options
->family
!= CHIP_HAINAN
) {
8532 enabled_channels
|= 0x1;
8535 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
8536 enabled_channels
, V_008DFC_SQ_EXP_MRTZ
, compr
);
8539 static void export_fs_mrt_color(isel_context
*ctx
, int slot
)
8541 Builder
bld(ctx
->program
, ctx
->block
);
8542 unsigned write_mask
= ctx
->outputs
.mask
[slot
];
8545 for (unsigned i
= 0; i
< 4; ++i
) {
8546 if (write_mask
& (1 << i
)) {
8547 values
[i
] = Operand(ctx
->outputs
.outputs
[slot
][i
]);
8549 values
[i
] = Operand(v1
);
8553 unsigned target
, col_format
;
8554 unsigned enabled_channels
= 0;
8555 aco_opcode compr_op
= (aco_opcode
)0;
8557 slot
-= FRAG_RESULT_DATA0
;
8558 target
= V_008DFC_SQ_EXP_MRT
+ slot
;
8559 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * slot
)) & 0xf;
8561 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> slot
) & 1;
8562 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> slot
) & 1;
8566 case V_028714_SPI_SHADER_ZERO
:
8567 enabled_channels
= 0; /* writemask */
8568 target
= V_008DFC_SQ_EXP_NULL
;
8571 case V_028714_SPI_SHADER_32_R
:
8572 enabled_channels
= 1;
8575 case V_028714_SPI_SHADER_32_GR
:
8576 enabled_channels
= 0x3;
8579 case V_028714_SPI_SHADER_32_AR
:
8580 if (ctx
->options
->chip_class
>= GFX10
) {
8581 /* Special case: on GFX10, the outputs are different for 32_AR */
8582 enabled_channels
= 0x3;
8583 values
[1] = values
[3];
8584 values
[3] = Operand(v1
);
8586 enabled_channels
= 0x9;
8590 case V_028714_SPI_SHADER_FP16_ABGR
:
8591 enabled_channels
= 0x5;
8592 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
8595 case V_028714_SPI_SHADER_UNORM16_ABGR
:
8596 enabled_channels
= 0x5;
8597 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
8600 case V_028714_SPI_SHADER_SNORM16_ABGR
:
8601 enabled_channels
= 0x5;
8602 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
8605 case V_028714_SPI_SHADER_UINT16_ABGR
: {
8606 enabled_channels
= 0x5;
8607 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
8608 if (is_int8
|| is_int10
) {
8610 uint32_t max_rgb
= is_int8
? 255 : is_int10
? 1023 : 0;
8611 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
8613 for (unsigned i
= 0; i
< 4; i
++) {
8614 if ((write_mask
>> i
) & 1) {
8615 values
[i
] = bld
.vop2(aco_opcode::v_min_u32
, bld
.def(v1
),
8616 i
== 3 && is_int10
? Operand(3u) : Operand(max_rgb_val
),
8624 case V_028714_SPI_SHADER_SINT16_ABGR
:
8625 enabled_channels
= 0x5;
8626 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
8627 if (is_int8
|| is_int10
) {
8629 uint32_t max_rgb
= is_int8
? 127 : is_int10
? 511 : 0;
8630 uint32_t min_rgb
= is_int8
? -128 :is_int10
? -512 : 0;
8631 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
8632 Temp min_rgb_val
= bld
.copy(bld
.def(s1
), Operand(min_rgb
));
8634 for (unsigned i
= 0; i
< 4; i
++) {
8635 if ((write_mask
>> i
) & 1) {
8636 values
[i
] = bld
.vop2(aco_opcode::v_min_i32
, bld
.def(v1
),
8637 i
== 3 && is_int10
? Operand(1u) : Operand(max_rgb_val
),
8639 values
[i
] = bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
),
8640 i
== 3 && is_int10
? Operand(-2u) : Operand(min_rgb_val
),
8647 case V_028714_SPI_SHADER_32_ABGR
:
8648 enabled_channels
= 0xF;
8655 if (target
== V_008DFC_SQ_EXP_NULL
)
8658 if ((bool) compr_op
) {
8659 for (int i
= 0; i
< 2; i
++) {
8660 /* check if at least one of the values to be compressed is enabled */
8661 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
8663 enabled_channels
|= enabled
<< (i
*2);
8664 values
[i
] = bld
.vop3(compr_op
, bld
.def(v1
),
8665 values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2],
8666 values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1]);
8668 values
[i
] = Operand(v1
);
8671 values
[2] = Operand(v1
);
8672 values
[3] = Operand(v1
);
8674 for (int i
= 0; i
< 4; i
++)
8675 values
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
8678 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
8679 enabled_channels
, target
, (bool) compr_op
);
8682 static void create_fs_exports(isel_context
*ctx
)
8684 /* Export depth, stencil and sample mask. */
8685 if (ctx
->outputs
.mask
[FRAG_RESULT_DEPTH
] ||
8686 ctx
->outputs
.mask
[FRAG_RESULT_STENCIL
] ||
8687 ctx
->outputs
.mask
[FRAG_RESULT_SAMPLE_MASK
]) {
8688 export_fs_mrt_z(ctx
);
8691 /* Export all color render targets. */
8692 for (unsigned i
= FRAG_RESULT_DATA0
; i
< FRAG_RESULT_DATA7
+ 1; ++i
) {
8693 if (ctx
->outputs
.mask
[i
])
8694 export_fs_mrt_color(ctx
, i
);
8698 static void emit_stream_output(isel_context
*ctx
,
8699 Temp
const *so_buffers
,
8700 Temp
const *so_write_offset
,
8701 const struct radv_stream_output
*output
)
8703 unsigned num_comps
= util_bitcount(output
->component_mask
);
8704 unsigned writemask
= (1 << num_comps
) - 1;
8705 unsigned loc
= output
->location
;
8706 unsigned buf
= output
->buffer
;
8708 assert(num_comps
&& num_comps
<= 4);
8709 if (!num_comps
|| num_comps
> 4)
8712 unsigned start
= ffs(output
->component_mask
) - 1;
8715 bool all_undef
= true;
8716 assert(ctx
->stage
== vertex_vs
|| ctx
->stage
== gs_copy_vs
);
8717 for (unsigned i
= 0; i
< num_comps
; i
++) {
8718 out
[i
] = ctx
->outputs
.outputs
[loc
][start
+ i
];
8719 all_undef
= all_undef
&& !out
[i
].id();
8726 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
8727 if (count
== 3 && ctx
->options
->chip_class
== GFX6
) {
8728 /* GFX6 doesn't support storing vec3, split it. */
8729 writemask
|= 1u << (start
+ 2);
8733 unsigned offset
= output
->offset
+ start
* 4;
8735 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, count
)};
8736 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
8737 for (int i
= 0; i
< count
; ++i
)
8738 vec
->operands
[i
] = (ctx
->outputs
.mask
[loc
] & 1 << (start
+ i
)) ? Operand(out
[start
+ i
]) : Operand(0u);
8739 vec
->definitions
[0] = Definition(write_data
);
8740 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8745 opcode
= aco_opcode::buffer_store_dword
;
8748 opcode
= aco_opcode::buffer_store_dwordx2
;
8751 opcode
= aco_opcode::buffer_store_dwordx3
;
8754 opcode
= aco_opcode::buffer_store_dwordx4
;
8757 unreachable("Unsupported dword count.");
8760 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
8761 store
->operands
[0] = Operand(so_buffers
[buf
]);
8762 store
->operands
[1] = Operand(so_write_offset
[buf
]);
8763 store
->operands
[2] = Operand((uint32_t) 0);
8764 store
->operands
[3] = Operand(write_data
);
8765 if (offset
> 4095) {
8766 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
8767 Builder
bld(ctx
->program
, ctx
->block
);
8768 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
8770 store
->offset
= offset
;
8772 store
->offen
= true;
8776 store
->can_reorder
= true;
8777 ctx
->block
->instructions
.emplace_back(std::move(store
));
8781 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
8783 Builder
bld(ctx
->program
, ctx
->block
);
8786 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->streamout_buffers
));
8787 for (unsigned i
= 0; i
< 4; i
++) {
8788 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
8792 Operand off
= bld
.copy(bld
.def(s1
), Operand(i
* 16u));
8793 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, off
);
8796 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
8797 get_arg(ctx
, ctx
->args
->streamout_config
), Operand(0x70010u
));
8799 Temp tid
= emit_mbcnt(ctx
, bld
.def(v1
));
8801 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(bld
.lm
), so_vtx_count
, tid
);
8804 begin_divergent_if_then(ctx
, &ic
, can_emit
);
8806 bld
.reset(ctx
->block
);
8808 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), get_arg(ctx
, ctx
->args
->streamout_write_idx
), tid
);
8810 Temp so_write_offset
[4];
8812 for (unsigned i
= 0; i
< 4; i
++) {
8813 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
8818 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
8819 get_arg(ctx
, ctx
->args
->streamout_write_idx
),
8820 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
8821 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
8823 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
8825 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
8826 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u),
8827 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
8828 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
8832 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
8833 struct radv_stream_output
*output
=
8834 &ctx
->program
->info
->so
.outputs
[i
];
8835 if (stream
!= output
->stream
)
8838 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
8841 begin_divergent_if_else(ctx
, &ic
);
8842 end_divergent_if(ctx
, &ic
);
8845 } /* end namespace */
8847 void split_arguments(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
8849 /* Split all arguments except for the first (ring_offsets) and the last
8850 * (exec) so that the dead channels don't stay live throughout the program.
8852 for (int i
= 1; i
< startpgm
->definitions
.size() - 1; i
++) {
8853 if (startpgm
->definitions
[i
].regClass().size() > 1) {
8854 emit_split_vector(ctx
, startpgm
->definitions
[i
].getTemp(),
8855 startpgm
->definitions
[i
].regClass().size());
8860 void handle_bc_optimize(isel_context
*ctx
)
8862 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
8863 Builder
bld(ctx
->program
, ctx
->block
);
8864 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
8865 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
8866 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
8867 ctx
->persp_centroid
= get_arg(ctx
, ctx
->args
->ac
.persp_centroid
);
8868 ctx
->linear_centroid
= get_arg(ctx
, ctx
->args
->ac
.linear_centroid
);
8869 if (uses_center
&& uses_centroid
) {
8870 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
8871 get_arg(ctx
, ctx
->args
->ac
.prim_mask
), Operand(0u));
8873 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
8875 for (unsigned i
= 0; i
< 2; i
++) {
8876 Temp persp_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_centroid
), i
, v1
);
8877 Temp persp_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_center
), i
, v1
);
8878 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8879 persp_centroid
, persp_center
, sel
);
8881 ctx
->persp_centroid
= bld
.tmp(v2
);
8882 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->persp_centroid
),
8883 Operand(new_coord
[0]), Operand(new_coord
[1]));
8884 emit_split_vector(ctx
, ctx
->persp_centroid
, 2);
8887 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
8889 for (unsigned i
= 0; i
< 2; i
++) {
8890 Temp linear_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_centroid
), i
, v1
);
8891 Temp linear_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_center
), i
, v1
);
8892 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8893 linear_centroid
, linear_center
, sel
);
8895 ctx
->linear_centroid
= bld
.tmp(v2
);
8896 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->linear_centroid
),
8897 Operand(new_coord
[0]), Operand(new_coord
[1]));
8898 emit_split_vector(ctx
, ctx
->linear_centroid
, 2);
8903 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
8905 Program
*program
= ctx
->program
;
8907 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
8909 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
8910 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
8911 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
8912 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
8913 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
8915 program
->next_fp_mode
.must_flush_denorms32
=
8916 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
8917 program
->next_fp_mode
.must_flush_denorms16_64
=
8918 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
8919 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
8921 program
->next_fp_mode
.care_about_round32
=
8922 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
8924 program
->next_fp_mode
.care_about_round16_64
=
8925 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
8926 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
8928 /* default to preserving fp16 and fp64 denorms, since it's free */
8929 if (program
->next_fp_mode
.must_flush_denorms16_64
)
8930 program
->next_fp_mode
.denorm16_64
= 0;
8932 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
8934 /* preserving fp32 denorms is expensive, so only do it if asked */
8935 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
8936 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
8938 program
->next_fp_mode
.denorm32
= 0;
8940 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
8941 program
->next_fp_mode
.round32
= fp_round_tz
;
8943 program
->next_fp_mode
.round32
= fp_round_ne
;
8945 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
8946 program
->next_fp_mode
.round16_64
= fp_round_tz
;
8948 program
->next_fp_mode
.round16_64
= fp_round_ne
;
8950 ctx
->block
->fp_mode
= program
->next_fp_mode
;
8953 void cleanup_cfg(Program
*program
)
8955 /* create linear_succs/logical_succs */
8956 for (Block
& BB
: program
->blocks
) {
8957 for (unsigned idx
: BB
.linear_preds
)
8958 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
8959 for (unsigned idx
: BB
.logical_preds
)
8960 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);
8964 void select_program(Program
*program
,
8965 unsigned shader_count
,
8966 struct nir_shader
*const *shaders
,
8967 ac_shader_config
* config
,
8968 struct radv_shader_args
*args
)
8970 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, args
, false);
8972 for (unsigned i
= 0; i
< shader_count
; i
++) {
8973 nir_shader
*nir
= shaders
[i
];
8974 init_context(&ctx
, nir
);
8976 setup_fp_mode(&ctx
, nir
);
8979 /* needs to be after init_context() for FS */
8980 Pseudo_instruction
*startpgm
= add_startpgm(&ctx
);
8981 append_logical_start(ctx
.block
);
8982 split_arguments(&ctx
, startpgm
);
8986 if (shader_count
>= 2) {
8987 Builder
bld(ctx
.program
, ctx
.block
);
8988 Temp count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), get_arg(&ctx
, args
->merged_wave_info
), Operand((8u << 16) | (i
* 8u)));
8989 Temp thread_id
= emit_mbcnt(&ctx
, bld
.def(v1
));
8990 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), count
, thread_id
);
8992 begin_divergent_if_then(&ctx
, &ic
, cond
);
8996 Builder
bld(ctx
.program
, ctx
.block
);
8997 assert(ctx
.stage
== vertex_geometry_gs
);
8998 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
8999 bld
.sopp(aco_opcode::s_barrier
);
9001 ctx
.gs_wave_id
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), get_arg(&ctx
, args
->merged_wave_info
), Operand((8u << 16) | 16u));
9002 } else if (ctx
.stage
== geometry_gs
)
9003 ctx
.gs_wave_id
= get_arg(&ctx
, args
->gs_wave_id
);
9005 if (ctx
.stage
== fragment_fs
)
9006 handle_bc_optimize(&ctx
);
9008 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
9009 visit_cf_list(&ctx
, &func
->body
);
9011 if (ctx
.program
->info
->so
.num_outputs
&& ctx
.stage
== vertex_vs
)
9012 emit_streamout(&ctx
, 0);
9014 if (ctx
.stage
== vertex_vs
) {
9015 create_vs_exports(&ctx
);
9016 } else if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
9017 Builder
bld(ctx
.program
, ctx
.block
);
9018 bld
.barrier(aco_opcode::p_memory_barrier_gs_data
);
9019 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
.gs_wave_id
), -1, sendmsg_gs_done(false, false, 0));
9022 if (ctx
.stage
== fragment_fs
)
9023 create_fs_exports(&ctx
);
9025 if (shader_count
>= 2) {
9026 begin_divergent_if_else(&ctx
, &ic
);
9027 end_divergent_if(&ctx
, &ic
);
9030 ralloc_free(ctx
.divergent_vals
);
9033 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
9035 append_logical_end(ctx
.block
);
9036 ctx
.block
->kind
|= block_kind_uniform
| block_kind_export_end
;
9037 Builder
bld(ctx
.program
, ctx
.block
);
9038 if (ctx
.program
->wb_smem_l1_on_end
)
9039 bld
.smem(aco_opcode::s_dcache_wb
, false);
9040 bld
.sopp(aco_opcode::s_endpgm
);
9042 cleanup_cfg(program
);
9045 void select_gs_copy_shader(Program
*program
, struct nir_shader
*gs_shader
,
9046 ac_shader_config
* config
,
9047 struct radv_shader_args
*args
)
9049 isel_context ctx
= setup_isel_context(program
, 1, &gs_shader
, config
, args
, true);
9051 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
= false;
9052 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
= false;
9053 program
->next_fp_mode
.must_flush_denorms32
= false;
9054 program
->next_fp_mode
.must_flush_denorms16_64
= false;
9055 program
->next_fp_mode
.care_about_round32
= false;
9056 program
->next_fp_mode
.care_about_round16_64
= false;
9057 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
9058 program
->next_fp_mode
.denorm32
= 0;
9059 program
->next_fp_mode
.round32
= fp_round_ne
;
9060 program
->next_fp_mode
.round16_64
= fp_round_ne
;
9061 ctx
.block
->fp_mode
= program
->next_fp_mode
;
9064 append_logical_start(ctx
.block
);
9066 Builder
bld(ctx
.program
, ctx
.block
);
9068 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), program
->private_segment_buffer
, Operand(RING_GSVS_VS
* 16u));
9070 Operand
stream_id(0u);
9071 if (args
->shader_info
->so
.num_outputs
)
9072 stream_id
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
9073 get_arg(&ctx
, ctx
.args
->streamout_config
), Operand(0x20018u
));
9075 Temp vtx_offset
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), get_arg(&ctx
, ctx
.args
->ac
.vertex_id
));
9077 std::stack
<Block
> endif_blocks
;
9079 for (unsigned stream
= 0; stream
< 4; stream
++) {
9080 if (stream_id
.isConstant() && stream
!= stream_id
.constantValue())
9083 unsigned num_components
= args
->shader_info
->gs
.num_stream_output_components
[stream
];
9084 if (stream
> 0 && (!num_components
|| !args
->shader_info
->so
.num_outputs
))
9087 memset(ctx
.outputs
.mask
, 0, sizeof(ctx
.outputs
.mask
));
9089 unsigned BB_if_idx
= ctx
.block
->index
;
9090 Block BB_endif
= Block();
9091 if (!stream_id
.isConstant()) {
9093 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), stream_id
, Operand(stream
));
9094 append_logical_end(ctx
.block
);
9095 ctx
.block
->kind
|= block_kind_uniform
;
9096 bld
.branch(aco_opcode::p_cbranch_z
, cond
);
9098 BB_endif
.kind
|= ctx
.block
->kind
& block_kind_top_level
;
9100 ctx
.block
= ctx
.program
->create_and_insert_block();
9101 add_edge(BB_if_idx
, ctx
.block
);
9102 bld
.reset(ctx
.block
);
9103 append_logical_start(ctx
.block
);
9106 unsigned offset
= 0;
9107 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
9108 if (args
->shader_info
->gs
.output_streams
[i
] != stream
)
9111 unsigned output_usage_mask
= args
->shader_info
->gs
.output_usage_mask
[i
];
9112 unsigned length
= util_last_bit(output_usage_mask
);
9113 for (unsigned j
= 0; j
< length
; ++j
) {
9114 if (!(output_usage_mask
& (1 << j
)))
9117 unsigned const_offset
= offset
* args
->shader_info
->gs
.vertices_out
* 16 * 4;
9118 Temp voffset
= vtx_offset
;
9119 if (const_offset
>= 4096u) {
9120 voffset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), voffset
);
9121 const_offset
%= 4096u;
9124 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dword
, Format::MUBUF
, 3, 1)};
9125 mubuf
->definitions
[0] = bld
.def(v1
);
9126 mubuf
->operands
[0] = Operand(gsvs_ring
);
9127 mubuf
->operands
[1] = Operand(voffset
);
9128 mubuf
->operands
[2] = Operand(0u);
9129 mubuf
->offen
= true;
9130 mubuf
->offset
= const_offset
;
9133 mubuf
->dlc
= args
->options
->chip_class
>= GFX10
;
9134 mubuf
->barrier
= barrier_none
;
9135 mubuf
->can_reorder
= true;
9137 ctx
.outputs
.mask
[i
] |= 1 << j
;
9138 ctx
.outputs
.outputs
[i
][j
] = mubuf
->definitions
[0].getTemp();
9140 bld
.insert(std::move(mubuf
));
9146 if (args
->shader_info
->so
.num_outputs
) {
9147 emit_streamout(&ctx
, stream
);
9148 bld
.reset(ctx
.block
);
9152 create_vs_exports(&ctx
);
9153 ctx
.block
->kind
|= block_kind_export_end
;
9156 if (!stream_id
.isConstant()) {
9157 append_logical_end(ctx
.block
);
9159 /* branch from then block to endif block */
9160 bld
.branch(aco_opcode::p_branch
);
9161 add_edge(ctx
.block
->index
, &BB_endif
);
9162 ctx
.block
->kind
|= block_kind_uniform
;
9164 /* emit else block */
9165 ctx
.block
= ctx
.program
->create_and_insert_block();
9166 add_edge(BB_if_idx
, ctx
.block
);
9167 bld
.reset(ctx
.block
);
9168 append_logical_start(ctx
.block
);
9170 endif_blocks
.push(std::move(BB_endif
));
9174 while (!endif_blocks
.empty()) {
9175 Block BB_endif
= std::move(endif_blocks
.top());
9178 Block
*BB_else
= ctx
.block
;
9180 append_logical_end(BB_else
);
9181 /* branch from else block to endif block */
9182 bld
.branch(aco_opcode::p_branch
);
9183 add_edge(BB_else
->index
, &BB_endif
);
9184 BB_else
->kind
|= block_kind_uniform
;
9186 /** emit endif merge block */
9187 ctx
.block
= program
->insert_block(std::move(BB_endif
));
9188 bld
.reset(ctx
.block
);
9189 append_logical_start(ctx
.block
);
9192 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
9194 append_logical_end(ctx
.block
);
9195 ctx
.block
->kind
|= block_kind_uniform
;
9196 bld
.sopp(aco_opcode::s_endpgm
);
9198 cleanup_cfg(program
);