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 Temp
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
;
2660 bool usable_read2
= ctx
->options
->chip_class
>= GFX7
;
2662 while (bytes_read
< total_bytes
) {
2663 unsigned todo
= total_bytes
- bytes_read
;
2664 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2665 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2667 aco_opcode op
= aco_opcode::last_opcode
;
2669 if (todo
>= 16 && aligned16
&& large_ds_read
) {
2670 op
= aco_opcode::ds_read_b128
;
2672 } else if (todo
>= 16 && aligned8
&& usable_read2
) {
2673 op
= aco_opcode::ds_read2_b64
;
2676 } else if (todo
>= 12 && aligned16
&& large_ds_read
) {
2677 op
= aco_opcode::ds_read_b96
;
2679 } else if (todo
>= 8 && aligned8
) {
2680 op
= aco_opcode::ds_read_b64
;
2682 } else if (todo
>= 8 && usable_read2
) {
2683 op
= aco_opcode::ds_read2_b32
;
2686 } else if (todo
>= 4) {
2687 op
= aco_opcode::ds_read_b32
;
2692 assert(todo
% elem_size_bytes
== 0);
2693 unsigned num_elements
= todo
/ elem_size_bytes
;
2694 unsigned offset
= base_offset
+ bytes_read
;
2695 unsigned max_offset
= read2
? 1019 : 65535;
2697 Temp address_offset
= address
;
2698 if (offset
> max_offset
) {
2699 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2700 offset
= bytes_read
;
2702 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2705 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2708 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2711 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
/ (todo
/ 2), (offset
/ (todo
/ 2)) + 1);
2713 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2715 if (num_components
== 1) {
2716 assert(todo
== total_bytes
);
2717 if (dst
.type() == RegType::sgpr
)
2718 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2722 if (dst
.type() == RegType::sgpr
) {
2723 Temp new_res
= bld
.tmp(RegType::sgpr
, res
.size());
2724 expand_vector(ctx
, res
, new_res
, res
.size(), (1 << res
.size()) - 1);
2728 if (num_elements
== 1) {
2729 result
[result_size
++] = res
;
2731 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2732 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2733 split
->operands
[0] = Operand(res
);
2734 for (unsigned i
= 0; i
< num_elements
; i
++)
2735 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2736 ctx
->block
->instructions
.emplace_back(std::move(split
));
2742 assert(result_size
== num_components
&& result_size
> 1);
2743 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2744 for (unsigned i
= 0; i
< result_size
; i
++)
2745 vec
->operands
[i
] = Operand(result
[i
]);
2746 vec
->definitions
[0] = Definition(dst
);
2747 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2748 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2753 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2755 if (start
== 0 && size
== data
.size())
2756 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2758 unsigned size_hint
= 1;
2759 auto it
= ctx
->allocated_vec
.find(data
.id());
2760 if (it
!= ctx
->allocated_vec
.end())
2761 size_hint
= it
->second
[0].size();
2762 if (size
% size_hint
|| start
% size_hint
)
2769 for (unsigned i
= 0; i
< size
; i
++)
2770 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2773 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2775 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2776 for (unsigned i
= 0; i
< size
; i
++)
2777 vec
->operands
[i
] = Operand(elems
[i
]);
2778 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2779 vec
->definitions
[0] = Definition(res
);
2780 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2784 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
)
2786 Builder
bld(ctx
->program
, ctx
->block
);
2787 unsigned bytes_written
= 0;
2788 bool large_ds_write
= ctx
->options
->chip_class
>= GFX7
;
2789 bool usable_write2
= ctx
->options
->chip_class
>= GFX7
;
2791 while (bytes_written
< total_size
* 4) {
2792 unsigned todo
= total_size
* 4 - bytes_written
;
2793 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
2794 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
2796 aco_opcode op
= aco_opcode::last_opcode
;
2797 bool write2
= false;
2799 if (todo
>= 16 && aligned16
&& large_ds_write
) {
2800 op
= aco_opcode::ds_write_b128
;
2802 } else if (todo
>= 16 && aligned8
&& usable_write2
) {
2803 op
= aco_opcode::ds_write2_b64
;
2806 } else if (todo
>= 12 && aligned16
&& large_ds_write
) {
2807 op
= aco_opcode::ds_write_b96
;
2809 } else if (todo
>= 8 && aligned8
) {
2810 op
= aco_opcode::ds_write_b64
;
2812 } else if (todo
>= 8 && usable_write2
) {
2813 op
= aco_opcode::ds_write2_b32
;
2816 } else if (todo
>= 4) {
2817 op
= aco_opcode::ds_write_b32
;
2823 unsigned offset
= offset0
+ offset1
+ bytes_written
;
2824 unsigned max_offset
= write2
? 1020 : 65535;
2825 Temp address_offset
= address
;
2826 if (offset
> max_offset
) {
2827 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
2828 offset
= offset1
+ bytes_written
;
2830 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
2833 Temp val0
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
/ 2, RegType::vgpr
);
2834 Temp val1
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2) + 1, size
/ 2, RegType::vgpr
);
2835 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
/ size
/ 2, (offset
/ size
/ 2) + 1);
2837 Temp val
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
, RegType::vgpr
);
2838 bld
.ds(op
, address_offset
, val
, m
, offset
);
2841 bytes_written
+= size
* 4;
2845 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
2846 Temp address
, unsigned base_offset
, unsigned align
)
2848 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2849 assert(elem_size_bytes
== 4 || elem_size_bytes
== 8);
2851 Operand m
= load_lds_size_m0(ctx
);
2853 /* we need at most two stores, assuming that the writemask is at most 4 bits wide */
2854 assert(wrmask
<= 0x0f);
2855 int start
[2], count
[2];
2856 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2857 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2858 assert(wrmask
== 0);
2860 /* one combined store is sufficient */
2861 if (count
[0] == count
[1] && (align
% elem_size_bytes
) == 0 && (base_offset
% elem_size_bytes
) == 0) {
2862 Builder
bld(ctx
->program
, ctx
->block
);
2864 Temp address_offset
= address
;
2865 if ((base_offset
/ elem_size_bytes
) + start
[1] > 255) {
2866 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2870 assert(count
[0] == 1);
2871 RegClass
xtract_rc(RegType::vgpr
, elem_size_bytes
/ 4);
2873 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], xtract_rc
);
2874 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], xtract_rc
);
2875 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2876 base_offset
= base_offset
/ elem_size_bytes
;
2877 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2878 base_offset
+ start
[0], base_offset
+ start
[1]);
2882 for (unsigned i
= 0; i
< 2; i
++) {
2886 unsigned elem_size_words
= elem_size_bytes
/ 4;
2887 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2888 base_offset
, start
[i
] * elem_size_bytes
, align
);
2893 unsigned calculate_lds_alignment(isel_context
*ctx
, unsigned const_offset
)
2895 unsigned align
= 16;
2897 align
= std::min(align
, 1u << (ffs(const_offset
) - 1));
2903 Temp
create_vec_from_array(isel_context
*ctx
, Temp arr
[], unsigned cnt
, RegType reg_type
, unsigned split_cnt
= 0u, Temp dst
= Temp())
2905 Builder
bld(ctx
->program
, ctx
->block
);
2908 dst
= bld
.tmp(RegClass(reg_type
, cnt
* arr
[0].size()));
2910 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> allocated_vec
;
2911 aco_ptr
<Pseudo_instruction
> instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, cnt
, 1)};
2912 instr
->definitions
[0] = Definition(dst
);
2914 for (unsigned i
= 0; i
< cnt
; ++i
) {
2915 assert(arr
[i
].size() == arr
[0].size());
2916 allocated_vec
[i
] = arr
[i
];
2917 instr
->operands
[i
] = Operand(arr
[i
]);
2920 bld
.insert(std::move(instr
));
2923 emit_split_vector(ctx
, dst
, split_cnt
);
2925 ctx
->allocated_vec
.emplace(dst
.id(), allocated_vec
); /* emit_split_vector already does this */
2930 inline unsigned resolve_excess_vmem_const_offset(Builder
&bld
, Temp
&voffset
, unsigned const_offset
)
2932 if (const_offset
>= 4096) {
2933 unsigned excess_const_offset
= const_offset
/ 4096u * 4096u;
2934 const_offset
%= 4096u;
2937 voffset
= bld
.copy(bld
.def(v1
), Operand(excess_const_offset
));
2938 else if (unlikely(voffset
.regClass() == s1
))
2939 voffset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(excess_const_offset
), Operand(voffset
));
2940 else if (likely(voffset
.regClass() == v1
))
2941 voffset
= bld
.vadd32(bld
.def(v1
), Operand(voffset
), Operand(excess_const_offset
));
2943 unreachable("Unsupported register class of voffset");
2946 return const_offset
;
2949 void emit_single_mubuf_store(isel_context
*ctx
, Temp descriptor
, Temp voffset
, Temp soffset
, Temp vdata
,
2950 unsigned const_offset
= 0u, bool allow_reorder
= true, bool slc
= false)
2953 assert(vdata
.size() != 3 || ctx
->program
->chip_class
!= GFX6
);
2954 assert(vdata
.size() >= 1 && vdata
.size() <= 4);
2956 Builder
bld(ctx
->program
, ctx
->block
);
2957 aco_opcode op
= (aco_opcode
) ((unsigned) aco_opcode::buffer_store_dword
+ vdata
.size() - 1);
2958 const_offset
= resolve_excess_vmem_const_offset(bld
, voffset
, const_offset
);
2960 Operand voffset_op
= voffset
.id() ? Operand(as_vgpr(ctx
, voffset
)) : Operand(v1
);
2961 Operand soffset_op
= soffset
.id() ? Operand(soffset
) : Operand(0u);
2962 Builder::Result r
= bld
.mubuf(op
, Operand(descriptor
), voffset_op
, soffset_op
, Operand(vdata
), const_offset
,
2963 /* offen */ !voffset_op
.isUndefined(), /* idxen*/ false, /* addr64 */ false,
2964 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc
);
2966 static_cast<MUBUF_instruction
*>(r
.instr
)->can_reorder
= allow_reorder
;
2969 void store_vmem_mubuf(isel_context
*ctx
, Temp src
, Temp descriptor
, Temp voffset
, Temp soffset
,
2970 unsigned base_const_offset
, unsigned elem_size_bytes
, unsigned write_mask
,
2971 bool allow_combining
= true, bool reorder
= true, bool slc
= false)
2973 Builder
bld(ctx
->program
, ctx
->block
);
2974 assert(elem_size_bytes
== 4 || elem_size_bytes
== 8);
2977 if (elem_size_bytes
== 8) {
2978 elem_size_bytes
= 4;
2979 write_mask
= widen_mask(write_mask
, 2);
2982 while (write_mask
) {
2985 u_bit_scan_consecutive_range(&write_mask
, &start
, &count
);
2990 unsigned sub_count
= allow_combining
? MIN2(count
, 4) : 1;
2991 unsigned const_offset
= (unsigned) start
* elem_size_bytes
+ base_const_offset
;
2993 /* GFX6 doesn't have buffer_store_dwordx3, so make sure not to emit that here either. */
2994 if (unlikely(ctx
->program
->chip_class
== GFX6
&& sub_count
== 3))
2997 Temp elem
= extract_subvector(ctx
, src
, start
, sub_count
, RegType::vgpr
);
2998 emit_single_mubuf_store(ctx
, descriptor
, voffset
, soffset
, elem
, const_offset
, reorder
, slc
);
3008 Temp
emit_single_mubuf_load(isel_context
*ctx
, Temp descriptor
, Temp voffset
, Temp soffset
,
3009 unsigned const_offset
, unsigned size_dwords
, bool allow_reorder
= true)
3011 assert(size_dwords
!= 3 || ctx
->program
->chip_class
!= GFX6
);
3012 assert(size_dwords
>= 1 && size_dwords
<= 4);
3014 Builder
bld(ctx
->program
, ctx
->block
);
3015 Temp vdata
= bld
.tmp(RegClass(RegType::vgpr
, size_dwords
));
3016 aco_opcode op
= (aco_opcode
) ((unsigned) aco_opcode::buffer_load_dword
+ size_dwords
- 1);
3017 const_offset
= resolve_excess_vmem_const_offset(bld
, voffset
, const_offset
);
3019 Operand voffset_op
= voffset
.id() ? Operand(as_vgpr(ctx
, voffset
)) : Operand(v1
);
3020 Operand soffset_op
= soffset
.id() ? Operand(soffset
) : Operand(0u);
3021 Builder::Result r
= bld
.mubuf(op
, Definition(vdata
), Operand(descriptor
), voffset_op
, soffset_op
, const_offset
,
3022 /* offen */ !voffset_op
.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3023 /* disable_wqm */ false, /* glc */ true,
3024 /* dlc*/ ctx
->program
->chip_class
>= GFX10
, /* slc */ false);
3026 static_cast<MUBUF_instruction
*>(r
.instr
)->can_reorder
= allow_reorder
;
3031 void load_vmem_mubuf(isel_context
*ctx
, Temp dst
, Temp descriptor
, Temp voffset
, Temp soffset
,
3032 unsigned base_const_offset
, unsigned elem_size_bytes
, unsigned num_components
,
3033 unsigned stride
= 0u, bool allow_combining
= true, bool allow_reorder
= true)
3035 assert(elem_size_bytes
== 4 || elem_size_bytes
== 8);
3036 assert((num_components
* elem_size_bytes
/ 4) == dst
.size());
3037 assert(!!stride
!= allow_combining
);
3039 Builder
bld(ctx
->program
, ctx
->block
);
3040 unsigned split_cnt
= num_components
;
3042 if (elem_size_bytes
== 8) {
3043 elem_size_bytes
= 4;
3044 num_components
*= 2;
3048 stride
= elem_size_bytes
;
3050 unsigned load_size
= 1;
3051 if (allow_combining
) {
3052 if ((num_components
% 4) == 0)
3054 else if ((num_components
% 3) == 0 && ctx
->program
->chip_class
!= GFX6
)
3056 else if ((num_components
% 2) == 0)
3060 unsigned num_loads
= num_components
/ load_size
;
3061 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> elems
;
3063 for (unsigned i
= 0; i
< num_loads
; ++i
) {
3064 unsigned const_offset
= i
* stride
* load_size
+ base_const_offset
;
3065 elems
[i
] = emit_single_mubuf_load(ctx
, descriptor
, voffset
, soffset
, const_offset
, load_size
, allow_reorder
);
3068 create_vec_from_array(ctx
, elems
.data(), num_loads
, RegType::vgpr
, split_cnt
, dst
);
3071 std::pair
<Temp
, unsigned> offset_add_from_nir(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &base_offset
, nir_src
*off_src
, unsigned stride
= 1u)
3073 Builder
bld(ctx
->program
, ctx
->block
);
3074 Temp offset
= base_offset
.first
;
3075 unsigned const_offset
= base_offset
.second
;
3077 if (!nir_src_is_const(*off_src
)) {
3078 Temp indirect_offset_arg
= get_ssa_temp(ctx
, off_src
->ssa
);
3081 /* Calculate indirect offset with stride */
3082 if (likely(indirect_offset_arg
.regClass() == v1
))
3083 with_stride
= bld
.v_mul_imm(bld
.def(v1
), indirect_offset_arg
, stride
);
3084 else if (indirect_offset_arg
.regClass() == s1
)
3085 with_stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), indirect_offset_arg
);
3087 unreachable("Unsupported register class of indirect offset");
3089 /* Add to the supplied base offset */
3090 if (offset
.id() == 0)
3091 offset
= with_stride
;
3092 else if (unlikely(offset
.regClass() == s1
&& with_stride
.regClass() == s1
))
3093 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), with_stride
, offset
);
3094 else if (offset
.size() == 1 && with_stride
.size() == 1)
3095 offset
= bld
.vadd32(bld
.def(v1
), with_stride
, offset
);
3097 unreachable("Unsupported register class of indirect offset");
3099 unsigned const_offset_arg
= nir_src_as_uint(*off_src
);
3100 const_offset
+= const_offset_arg
* stride
;
3103 return std::make_pair(offset
, const_offset
);
3106 std::pair
<Temp
, unsigned> offset_add(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &off1
, const std::pair
<Temp
, unsigned> &off2
)
3108 Builder
bld(ctx
->program
, ctx
->block
);
3111 if (off1
.first
.id() && off2
.first
.id()) {
3112 if (unlikely(off1
.first
.regClass() == s1
&& off2
.first
.regClass() == s1
))
3113 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), off1
.first
, off2
.first
);
3114 else if (off1
.first
.size() == 1 && off2
.first
.size() == 1)
3115 offset
= bld
.vadd32(bld
.def(v1
), off1
.first
, off2
.first
);
3117 unreachable("Unsupported register class of indirect offset");
3119 offset
= off1
.first
.id() ? off1
.first
: off2
.first
;
3122 return std::make_pair(offset
, off1
.second
+ off2
.second
);
3125 std::pair
<Temp
, unsigned> offset_mul(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &offs
, unsigned multiplier
)
3127 Builder
bld(ctx
->program
, ctx
->block
);
3128 unsigned const_offset
= offs
.second
* multiplier
;
3130 if (!offs
.first
.id())
3131 return std::make_pair(offs
.first
, const_offset
);
3133 Temp offset
= unlikely(offs
.first
.regClass() == s1
)
3134 ? bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(multiplier
), offs
.first
)
3135 : bld
.v_mul_imm(bld
.def(v1
), offs
.first
, multiplier
);
3137 return std::make_pair(offset
, const_offset
);
3140 std::pair
<Temp
, unsigned> get_intrinsic_io_basic_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned base_stride
, unsigned component_stride
)
3142 Builder
bld(ctx
->program
, ctx
->block
);
3144 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
3145 unsigned const_offset
= nir_intrinsic_base(instr
) * base_stride
;
3146 /* component is in bytes */
3147 const_offset
+= nir_intrinsic_component(instr
) * component_stride
;
3149 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
3150 nir_src
*off_src
= nir_get_io_offset_src(instr
);
3151 return offset_add_from_nir(ctx
, std::make_pair(Temp(), const_offset
), off_src
, 4u * base_stride
);
3154 std::pair
<Temp
, unsigned> get_intrinsic_io_basic_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned stride
= 1u)
3156 return get_intrinsic_io_basic_offset(ctx
, instr
, stride
, stride
);
3159 Temp
get_tess_rel_patch_id(isel_context
*ctx
)
3161 Builder
bld(ctx
->program
, ctx
->block
);
3163 switch (ctx
->shader
->info
.stage
) {
3164 case MESA_SHADER_TESS_CTRL
:
3165 return bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffu
),
3166 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
));
3167 case MESA_SHADER_TESS_EVAL
:
3168 return get_arg(ctx
, ctx
->args
->tes_rel_patch_id
);
3170 unreachable("Unsupported stage in get_tess_rel_patch_id");
3174 std::pair
<Temp
, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3176 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
3177 Builder
bld(ctx
->program
, ctx
->block
);
3179 uint32_t tcs_in_patch_stride
= ctx
->args
->options
->key
.tcs
.input_vertices
* ctx
->tcs_num_inputs
* 4;
3180 uint32_t tcs_in_vertex_stride
= ctx
->tcs_num_inputs
* 4;
3182 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
);
3184 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
3185 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, tcs_in_vertex_stride
);
3187 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
3188 Temp tcs_in_current_patch_offset
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, tcs_in_patch_stride
);
3189 offs
= offset_add(ctx
, offs
, std::make_pair(tcs_in_current_patch_offset
, 0));
3191 return offset_mul(ctx
, offs
, 4u);
3194 std::pair
<Temp
, unsigned> get_tcs_output_lds_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
= nullptr, bool per_vertex
= false)
3196 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
3197 Builder
bld(ctx
->program
, ctx
->block
);
3199 uint32_t input_patch_size
= ctx
->args
->options
->key
.tcs
.input_vertices
* ctx
->tcs_num_inputs
* 16;
3200 uint32_t num_tcs_outputs
= util_last_bit64(ctx
->args
->shader_info
->tcs
.outputs_written
);
3201 uint32_t num_tcs_patch_outputs
= util_last_bit64(ctx
->args
->shader_info
->tcs
.patch_outputs_written
);
3202 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
3203 uint32_t pervertex_output_patch_size
= ctx
->shader
->info
.tess
.tcs_vertices_out
* output_vertex_size
;
3204 uint32_t output_patch_stride
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
3206 std::pair
<Temp
, unsigned> offs
= instr
3207 ? get_intrinsic_io_basic_offset(ctx
, instr
, 4u)
3208 : std::make_pair(Temp(), 0u);
3210 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
3211 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, output_patch_stride
);
3216 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
3217 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, output_vertex_size
);
3219 uint32_t output_patch0_offset
= (input_patch_size
* ctx
->tcs_num_patches
);
3220 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, output_patch0_offset
));
3222 uint32_t output_patch0_patch_data_offset
= (input_patch_size
* ctx
->tcs_num_patches
+ pervertex_output_patch_size
);
3223 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, output_patch0_patch_data_offset
));
3229 std::pair
<Temp
, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3231 Builder
bld(ctx
->program
, ctx
->block
);
3233 unsigned vertices_per_patch
= ctx
->shader
->info
.tess
.tcs_vertices_out
;
3234 unsigned attr_stride
= vertices_per_patch
* ctx
->tcs_num_patches
;
3236 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, attr_stride
* 4u, 4u);
3238 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
3239 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, vertices_per_patch
* 16u);
3240 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, 0u));
3242 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
3243 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, 16u);
3248 std::pair
<Temp
, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
= nullptr, unsigned const_base_offset
= 0u)
3250 Builder
bld(ctx
->program
, ctx
->block
);
3252 unsigned num_tcs_outputs
= ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
3253 ? util_last_bit64(ctx
->args
->shader_info
->tcs
.outputs_written
)
3254 : ctx
->args
->options
->key
.tes
.tcs_num_outputs
;
3256 unsigned output_vertex_size
= num_tcs_outputs
* 16;
3257 unsigned per_vertex_output_patch_size
= ctx
->shader
->info
.tess
.tcs_vertices_out
* output_vertex_size
;
3258 unsigned per_patch_data_offset
= per_vertex_output_patch_size
* ctx
->tcs_num_patches
;
3259 unsigned attr_stride
= ctx
->tcs_num_patches
;
3261 std::pair
<Temp
, unsigned> offs
= instr
3262 ? get_intrinsic_io_basic_offset(ctx
, instr
, attr_stride
* 4u, 4u)
3263 : std::make_pair(Temp(), 0u);
3265 if (const_base_offset
)
3266 offs
.second
+= const_base_offset
* attr_stride
;
3268 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
3269 Temp patch_off
= bld
.v_mul_imm(bld
.def(v1
), rel_patch_id
, 16u);
3270 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, per_patch_data_offset
));
3275 void visit_store_ls_or_es_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3277 Builder
bld(ctx
->program
, ctx
->block
);
3279 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, 4u);
3280 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3281 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
3282 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8u;
3284 if (ctx
->stage
== vertex_es
) {
3285 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
3286 Temp esgs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_VS
* 16u));
3287 Temp es2gs_offset
= get_arg(ctx
, ctx
->args
->es2gs_offset
);
3288 store_vmem_mubuf(ctx
, src
, esgs_ring
, offs
.first
, es2gs_offset
, offs
.second
, elem_size_bytes
, write_mask
, false, true, true);
3292 if (ctx
->stage
== vertex_geometry_gs
) {
3293 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
3294 unsigned itemsize
= ctx
->program
->info
->vs
.es_info
.esgs_itemsize
;
3295 Temp thread_id
= emit_mbcnt(ctx
, bld
.def(v1
));
3296 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));
3297 Temp vertex_idx
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), thread_id
,
3298 bld
.v_mul24_imm(bld
.def(v1
), as_vgpr(ctx
, wave_idx
), ctx
->program
->wave_size
));
3299 lds_base
= bld
.v_mul24_imm(bld
.def(v1
), vertex_idx
, itemsize
);
3301 unreachable("Invalid LS or ES stage");
3304 offs
= offset_add(ctx
, offs
, std::make_pair(lds_base
, 0u));
3305 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
3306 store_lds(ctx
, elem_size_bytes
, src
, write_mask
, offs
.first
, offs
.second
, lds_align
);
3311 void visit_store_tcs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
3313 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
3314 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
3316 Builder
bld(ctx
->program
, ctx
->block
);
3318 Temp store_val
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3319 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
3320 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
3322 /* TODO: Only write to VMEM if the output is per-vertex or it's per-patch non tess factor */
3323 bool write_to_vmem
= true;
3324 /* TODO: Only write to LDS if the output is read by the shader, or it's per-patch tess factor */
3325 bool write_to_lds
= true;
3327 if (write_to_vmem
) {
3328 std::pair
<Temp
, unsigned> vmem_offs
= per_vertex
3329 ? get_tcs_per_vertex_output_vmem_offset(ctx
, instr
)
3330 : get_tcs_per_patch_output_vmem_offset(ctx
, instr
);
3332 Temp hs_ring_tess_offchip
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
3333 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
3334 store_vmem_mubuf(ctx
, store_val
, hs_ring_tess_offchip
, vmem_offs
.first
, oc_lds
, vmem_offs
.second
, elem_size_bytes
, write_mask
, false, false);
3338 std::pair
<Temp
, unsigned> lds_offs
= get_tcs_output_lds_offset(ctx
, instr
, per_vertex
);
3339 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_offs
.second
);
3340 store_lds(ctx
, elem_size_bytes
, store_val
, write_mask
, lds_offs
.first
, lds_offs
.second
, lds_align
);
3344 void visit_load_tcs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
3346 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
3347 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
3349 Builder
bld(ctx
->program
, ctx
->block
);
3351 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3352 std::pair
<Temp
, unsigned> lds_offs
= get_tcs_output_lds_offset(ctx
, instr
, per_vertex
);
3353 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_offs
.second
);
3354 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
3356 load_lds(ctx
, elem_size_bytes
, dst
, lds_offs
.first
, lds_offs
.second
, lds_align
);
3359 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3361 if (ctx
->stage
== vertex_vs
||
3362 ctx
->stage
== fragment_fs
||
3363 ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
3364 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
3365 unsigned component
= nir_intrinsic_component(instr
);
3366 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3367 unsigned idx
= nir_intrinsic_base(instr
) + component
;
3369 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
3370 if (off_instr
->type
!= nir_instr_type_load_const
) {
3371 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3372 nir_print_instr(off_instr
, stderr
);
3373 fprintf(stderr
, "\n");
3375 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
3377 if (instr
->src
[0].ssa
->bit_size
== 64)
3378 write_mask
= widen_mask(write_mask
, 2);
3380 for (unsigned i
= 0; i
< 8; ++i
) {
3381 if (write_mask
& (1 << i
)) {
3382 ctx
->outputs
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
3383 ctx
->outputs
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
3387 } else if (ctx
->stage
== vertex_es
||
3388 (ctx
->stage
== vertex_geometry_gs
&& ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
)) {
3389 visit_store_ls_or_es_output(ctx
, instr
);
3390 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3391 visit_store_tcs_output(ctx
, instr
, false);
3393 unreachable("Shader stage not implemented");
3397 void visit_load_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3399 visit_load_tcs_output(ctx
, instr
, false);
3402 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
3404 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
3405 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
3407 Builder
bld(ctx
->program
, ctx
->block
);
3408 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
3409 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
3412 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
3414 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
3415 for (unsigned i
= 0; i
< num_components
; i
++)
3416 vec
->operands
[i
] = Operand(get_arg(ctx
, ctx
->args
->ac
.frag_pos
[i
]));
3417 if (G_0286CC_POS_W_FLOAT_ENA(ctx
->program
->config
->spi_ps_input_ena
)) {
3418 assert(num_components
== 4);
3419 Builder
bld(ctx
->program
, ctx
->block
);
3420 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->ac
.frag_pos
[3]));
3423 for (Operand
& op
: vec
->operands
)
3424 op
= op
.isUndefined() ? Operand(0u) : op
;
3426 vec
->definitions
[0] = Definition(dst
);
3427 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3428 emit_split_vector(ctx
, dst
, num_components
);
3432 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3434 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3435 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3436 unsigned idx
= nir_intrinsic_base(instr
);
3437 unsigned component
= nir_intrinsic_component(instr
);
3438 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
3440 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
3442 assert(offset
->u32
== 0);
3444 /* the lower 15bit of the prim_mask contain the offset into LDS
3445 * while the upper bits contain the number of prims */
3446 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3447 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3448 Builder
bld(ctx
->program
, ctx
->block
);
3449 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3450 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3451 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3452 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3453 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3456 if (instr
->dest
.ssa
.num_components
== 1) {
3457 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
3459 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
3460 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
3462 Temp tmp
= {ctx
->program
->allocateId(), v1
};
3463 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
3464 vec
->operands
[i
] = Operand(tmp
);
3466 vec
->definitions
[0] = Definition(dst
);
3467 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3471 bool check_vertex_fetch_size(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
3472 unsigned offset
, unsigned stride
, unsigned channels
)
3474 unsigned vertex_byte_size
= vtx_info
->chan_byte_size
* channels
;
3475 if (vtx_info
->chan_byte_size
!= 4 && channels
== 3)
3477 return (ctx
->options
->chip_class
!= GFX6
&& ctx
->options
->chip_class
!= GFX10
) ||
3478 (offset
% vertex_byte_size
== 0 && stride
% vertex_byte_size
== 0);
3481 uint8_t get_fetch_data_format(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
3482 unsigned offset
, unsigned stride
, unsigned *channels
)
3484 if (!vtx_info
->chan_byte_size
) {
3485 *channels
= vtx_info
->num_channels
;
3486 return vtx_info
->chan_format
;
3489 unsigned num_channels
= *channels
;
3490 if (!check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, *channels
)) {
3491 unsigned new_channels
= num_channels
+ 1;
3492 /* first, assume more loads is worse and try using a larger data format */
3493 while (new_channels
<= 4 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
)) {
3495 /* don't make the attribute potentially out-of-bounds */
3496 if (offset
+ new_channels
* vtx_info
->chan_byte_size
> stride
)
3500 if (new_channels
== 5) {
3501 /* then try decreasing load size (at the cost of more loads) */
3502 new_channels
= *channels
;
3503 while (new_channels
> 1 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
))
3507 if (new_channels
< *channels
)
3508 *channels
= new_channels
;
3509 num_channels
= new_channels
;
3512 switch (vtx_info
->chan_format
) {
3513 case V_008F0C_BUF_DATA_FORMAT_8
:
3514 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8
, V_008F0C_BUF_DATA_FORMAT_8_8
,
3515 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_8_8_8_8
}[num_channels
- 1];
3516 case V_008F0C_BUF_DATA_FORMAT_16
:
3517 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16
, V_008F0C_BUF_DATA_FORMAT_16_16
,
3518 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_16_16_16_16
}[num_channels
- 1];
3519 case V_008F0C_BUF_DATA_FORMAT_32
:
3520 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32
, V_008F0C_BUF_DATA_FORMAT_32_32
,
3521 V_008F0C_BUF_DATA_FORMAT_32_32_32
, V_008F0C_BUF_DATA_FORMAT_32_32_32_32
}[num_channels
- 1];
3523 unreachable("shouldn't reach here");
3524 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
3527 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
3528 * so we may need to fix it up. */
3529 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
3531 Builder
bld(ctx
->program
, ctx
->block
);
3533 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3534 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3536 /* For the integer-like cases, do a natural sign extension.
3538 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3539 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3542 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3543 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3545 /* Convert back to the right type. */
3546 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3547 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3548 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0xbf800000u
), alpha
);
3549 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3550 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3551 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3557 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3559 Builder
bld(ctx
->program
, ctx
->block
);
3560 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3561 if (ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) {
3563 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3564 if (off_instr
->type
!= nir_instr_type_load_const
) {
3565 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3566 nir_print_instr(off_instr
, stderr
);
3567 fprintf(stderr
, "\n");
3569 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3571 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->vertex_buffers
));
3573 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3574 unsigned component
= nir_intrinsic_component(instr
);
3575 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3576 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3577 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3578 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3580 unsigned dfmt
= attrib_format
& 0xf;
3581 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3582 const struct ac_data_format_info
*vtx_info
= ac_get_data_format_info(dfmt
);
3584 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3585 unsigned num_channels
= MIN2(util_last_bit(mask
), vtx_info
->num_channels
);
3586 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3587 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3589 num_channels
= MAX2(num_channels
, 3);
3591 Operand off
= bld
.copy(bld
.def(s1
), Operand(attrib_binding
* 16u));
3592 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, off
);
3595 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3596 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3597 Temp start_instance
= get_arg(ctx
, ctx
->args
->ac
.start_instance
);
3599 Temp instance_id
= get_arg(ctx
, ctx
->args
->ac
.instance_id
);
3601 Temp divided
= bld
.tmp(v1
);
3602 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, instance_id
), divisor
);
3603 index
= bld
.vadd32(bld
.def(v1
), start_instance
, divided
);
3605 index
= bld
.vadd32(bld
.def(v1
), start_instance
, instance_id
);
3608 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), start_instance
);
3611 index
= bld
.vadd32(bld
.def(v1
),
3612 get_arg(ctx
, ctx
->args
->ac
.base_vertex
),
3613 get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
3616 Temp channels
[num_channels
];
3617 unsigned channel_start
= 0;
3618 bool direct_fetch
= false;
3620 /* skip unused channels at the start */
3621 if (vtx_info
->chan_byte_size
&& !post_shuffle
) {
3622 channel_start
= ffs(mask
) - 1;
3623 for (unsigned i
= 0; i
< channel_start
; i
++)
3624 channels
[i
] = Temp(0, s1
);
3625 } else if (vtx_info
->chan_byte_size
&& post_shuffle
&& !(mask
& 0x8)) {
3626 num_channels
= 3 - (ffs(mask
) - 1);
3630 while (channel_start
< num_channels
) {
3631 unsigned fetch_size
= num_channels
- channel_start
;
3632 unsigned fetch_offset
= attrib_offset
+ channel_start
* vtx_info
->chan_byte_size
;
3633 bool expanded
= false;
3635 /* use MUBUF when possible to avoid possible alignment issues */
3636 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
3637 bool use_mubuf
= (nfmt
== V_008F0C_BUF_NUM_FORMAT_FLOAT
||
3638 nfmt
== V_008F0C_BUF_NUM_FORMAT_UINT
||
3639 nfmt
== V_008F0C_BUF_NUM_FORMAT_SINT
) &&
3640 vtx_info
->chan_byte_size
== 4;
3641 unsigned fetch_dfmt
= V_008F0C_BUF_DATA_FORMAT_INVALID
;
3643 fetch_dfmt
= get_fetch_data_format(ctx
, vtx_info
, fetch_offset
, attrib_stride
, &fetch_size
);
3645 if (fetch_size
== 3 && ctx
->options
->chip_class
== GFX6
) {
3646 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
3652 Temp fetch_index
= index
;
3653 if (attrib_stride
!= 0 && fetch_offset
> attrib_stride
) {
3654 fetch_index
= bld
.vadd32(bld
.def(v1
), Operand(fetch_offset
/ attrib_stride
), fetch_index
);
3655 fetch_offset
= fetch_offset
% attrib_stride
;
3658 Operand
soffset(0u);
3659 if (fetch_offset
>= 4096) {
3660 soffset
= bld
.copy(bld
.def(s1
), Operand(fetch_offset
/ 4096 * 4096));
3661 fetch_offset
%= 4096;
3665 switch (fetch_size
) {
3667 opcode
= use_mubuf
? aco_opcode::buffer_load_dword
: aco_opcode::tbuffer_load_format_x
;
3670 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx2
: aco_opcode::tbuffer_load_format_xy
;
3673 assert(ctx
->options
->chip_class
>= GFX7
||
3674 (!use_mubuf
&& ctx
->options
->chip_class
== GFX6
));
3675 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx3
: aco_opcode::tbuffer_load_format_xyz
;
3678 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx4
: aco_opcode::tbuffer_load_format_xyzw
;
3681 unreachable("Unimplemented load_input vector size");
3685 if (channel_start
== 0 && fetch_size
== dst
.size() && !post_shuffle
&&
3686 !expanded
&& (alpha_adjust
== RADV_ALPHA_ADJUST_NONE
||
3687 num_channels
<= 3)) {
3688 direct_fetch
= true;
3691 fetch_dst
= bld
.tmp(RegType::vgpr
, fetch_size
);
3695 Instruction
*mubuf
= bld
.mubuf(opcode
,
3696 Definition(fetch_dst
), list
, fetch_index
, soffset
,
3697 fetch_offset
, false, true).instr
;
3698 static_cast<MUBUF_instruction
*>(mubuf
)->can_reorder
= true;
3700 Instruction
*mtbuf
= bld
.mtbuf(opcode
,
3701 Definition(fetch_dst
), list
, fetch_index
, soffset
,
3702 fetch_dfmt
, nfmt
, fetch_offset
, false, true).instr
;
3703 static_cast<MTBUF_instruction
*>(mtbuf
)->can_reorder
= true;
3706 emit_split_vector(ctx
, fetch_dst
, fetch_dst
.size());
3708 if (fetch_size
== 1) {
3709 channels
[channel_start
] = fetch_dst
;
3711 for (unsigned i
= 0; i
< MIN2(fetch_size
, num_channels
- channel_start
); i
++)
3712 channels
[channel_start
+ i
] = emit_extract_vector(ctx
, fetch_dst
, i
, v1
);
3715 channel_start
+= fetch_size
;
3718 if (!direct_fetch
) {
3719 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3720 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3722 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3723 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3724 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3726 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3727 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3728 unsigned num_temp
= 0;
3729 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3730 unsigned idx
= i
+ component
;
3731 if (swizzle
[idx
] < num_channels
&& channels
[swizzle
[idx
]].id()) {
3732 Temp channel
= channels
[swizzle
[idx
]];
3733 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
)
3734 channel
= adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, channel
);
3735 vec
->operands
[i
] = Operand(channel
);
3739 } else if (is_float
&& idx
== 3) {
3740 vec
->operands
[i
] = Operand(0x3f800000u
);
3741 } else if (!is_float
&& idx
== 3) {
3742 vec
->operands
[i
] = Operand(1u);
3744 vec
->operands
[i
] = Operand(0u);
3747 vec
->definitions
[0] = Definition(dst
);
3748 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3749 emit_split_vector(ctx
, dst
, dst
.size());
3751 if (num_temp
== dst
.size())
3752 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3754 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
3755 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
3756 nir_instr
*off_instr
= instr
->src
[offset_idx
].ssa
->parent_instr
;
3757 if (off_instr
->type
!= nir_instr_type_load_const
||
3758 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3759 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3760 nir_print_instr(off_instr
, stderr
);
3761 fprintf(stderr
, "\n");
3764 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
3765 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
3767 assert(offset
->u32
== 0);
3769 /* the lower 15bit of the prim_mask contain the offset into LDS
3770 * while the upper bits contain the number of prims */
3771 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[offset_idx
].ssa
);
3772 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3773 Builder
bld(ctx
->program
, ctx
->block
);
3774 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3775 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3776 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3777 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3778 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3781 unsigned idx
= nir_intrinsic_base(instr
);
3782 unsigned component
= nir_intrinsic_component(instr
);
3783 unsigned vertex_id
= 2; /* P0 */
3785 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3786 nir_const_value
* src0
= nir_src_as_const_value(instr
->src
[0]);
3787 switch (src0
->u32
) {
3789 vertex_id
= 2; /* P0 */
3792 vertex_id
= 0; /* P10 */
3795 vertex_id
= 1; /* P20 */
3798 unreachable("invalid vertex index");
3802 if (dst
.size() == 1) {
3803 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
);
3805 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3806 for (unsigned i
= 0; i
< dst
.size(); i
++)
3807 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
+ i
);
3808 vec
->definitions
[0] = Definition(dst
);
3809 bld
.insert(std::move(vec
));
3813 unreachable("Shader stage not implemented");
3817 std::pair
<Temp
, unsigned> get_gs_per_vertex_input_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned base_stride
= 1u)
3819 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3821 Builder
bld(ctx
->program
, ctx
->block
);
3822 nir_src
*vertex_src
= nir_get_io_vertex_index_src(instr
);
3825 if (!nir_src_is_const(*vertex_src
)) {
3826 /* better code could be created, but this case probably doesn't happen
3827 * much in practice */
3828 Temp indirect_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, vertex_src
->ssa
));
3829 for (unsigned i
= 0; i
< ctx
->shader
->info
.gs
.vertices_in
; i
++) {
3832 if (ctx
->stage
== vertex_geometry_gs
) {
3833 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
/ 2u * 2u]);
3835 elem
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), elem
);
3837 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
]);
3840 if (vertex_offset
.id()) {
3841 Temp cond
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
3842 Operand(i
), indirect_vertex
);
3843 vertex_offset
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), vertex_offset
, elem
, cond
);
3845 vertex_offset
= elem
;
3849 if (ctx
->stage
== vertex_geometry_gs
)
3850 vertex_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
), vertex_offset
);
3852 unsigned vertex
= nir_src_as_uint(*vertex_src
);
3853 if (ctx
->stage
== vertex_geometry_gs
)
3854 vertex_offset
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
3855 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
/ 2u * 2u]),
3856 Operand((vertex
% 2u) * 16u), Operand(16u));
3858 vertex_offset
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
]);
3861 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, base_stride
);
3862 offs
= offset_add(ctx
, offs
, std::make_pair(vertex_offset
, 0u));
3863 return offset_mul(ctx
, offs
, 4u);
3866 void visit_load_gs_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3868 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3870 Builder
bld(ctx
->program
, ctx
->block
);
3871 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3872 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
3874 if (ctx
->stage
== geometry_gs
) {
3875 std::pair
<Temp
, unsigned> offs
= get_gs_per_vertex_input_offset(ctx
, instr
, ctx
->program
->wave_size
);
3876 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_GS
* 16u));
3877 load_vmem_mubuf(ctx
, dst
, ring
, offs
.first
, Temp(), offs
.second
, elem_size_bytes
, instr
->dest
.ssa
.num_components
, 4u * ctx
->program
->wave_size
, false, true);
3878 } else if (ctx
->stage
== vertex_geometry_gs
) {
3879 std::pair
<Temp
, unsigned> offs
= get_gs_per_vertex_input_offset(ctx
, instr
);
3880 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
3881 load_lds(ctx
, elem_size_bytes
, dst
, offs
.first
, offs
.second
, lds_align
);
3883 unreachable("Unsupported GS stage.");
3887 void visit_load_tcs_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3889 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
3891 Builder
bld(ctx
->program
, ctx
->block
);
3892 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3893 std::pair
<Temp
, unsigned> offs
= get_tcs_per_vertex_input_lds_offset(ctx
, instr
);
3894 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
3895 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
3897 load_lds(ctx
, elem_size_bytes
, dst
, offs
.first
, offs
.second
, lds_align
);
3900 void visit_load_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3902 switch (ctx
->shader
->info
.stage
) {
3903 case MESA_SHADER_GEOMETRY
:
3904 visit_load_gs_per_vertex_input(ctx
, instr
);
3906 case MESA_SHADER_TESS_CTRL
:
3907 visit_load_tcs_per_vertex_input(ctx
, instr
);
3910 unreachable("Unimplemented shader stage");
3914 void visit_load_per_vertex_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3916 visit_load_tcs_output(ctx
, instr
, true);
3919 void visit_store_per_vertex_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3921 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
3922 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
3924 visit_store_tcs_output(ctx
, instr
, true);
3927 void visit_load_tess_coord(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3929 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3931 Builder
bld(ctx
->program
, ctx
->block
);
3932 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3934 Operand
tes_u(get_arg(ctx
, ctx
->args
->tes_u
));
3935 Operand
tes_v(get_arg(ctx
, ctx
->args
->tes_v
));
3938 if (ctx
->shader
->info
.tess
.primitive_mode
== GL_TRIANGLES
) {
3939 Temp tmp
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), tes_u
, tes_v
);
3940 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0x3f800000u
/* 1.0f */), tmp
);
3941 tes_w
= Operand(tmp
);
3944 Temp tess_coord
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tes_u
, tes_v
, tes_w
);
3945 emit_split_vector(ctx
, tess_coord
, 3);
3948 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3950 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3951 Builder
bld(ctx
->program
, ctx
->block
);
3952 Temp ptr64
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->descriptor_sets
[0]));
3953 Operand off
= bld
.copy(bld
.def(s1
), Operand(desc_set
<< 2));
3954 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, off
);//, false, false, false);
3957 return get_arg(ctx
, ctx
->args
->descriptor_sets
[desc_set
]);
3961 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3963 Builder
bld(ctx
->program
, ctx
->block
);
3964 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3965 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3966 index
= bld
.as_uniform(index
);
3967 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3968 unsigned binding
= nir_intrinsic_binding(instr
);
3971 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3972 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3973 unsigned offset
= layout
->binding
[binding
].offset
;
3975 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3976 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3977 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3978 desc_ptr
= get_arg(ctx
, ctx
->args
->ac
.push_constants
);
3979 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3982 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3983 stride
= layout
->binding
[binding
].size
;
3986 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3987 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3989 if (nir_const_index
) {
3990 const_index
= const_index
* stride
;
3991 } else if (index
.type() == RegType::vgpr
) {
3992 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3993 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3995 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3999 if (nir_const_index
) {
4000 const_index
= const_index
+ offset
;
4001 } else if (index
.type() == RegType::vgpr
) {
4002 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
4004 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
4008 if (nir_const_index
&& const_index
== 0) {
4010 } else if (index
.type() == RegType::vgpr
) {
4011 index
= bld
.vadd32(bld
.def(v1
),
4012 nir_const_index
? Operand(const_index
) : Operand(index
),
4015 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4016 nir_const_index
? Operand(const_index
) : Operand(index
),
4020 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
4023 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
,
4024 Temp rsrc
, Temp offset
, bool glc
=false, bool readonly
=true)
4026 Builder
bld(ctx
->program
, ctx
->block
);
4028 unsigned num_bytes
= dst
.size() * 4;
4029 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4032 if (dst
.type() == RegType::vgpr
|| (ctx
->options
->chip_class
< GFX8
&& !readonly
)) {
4033 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4034 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4035 unsigned const_offset
= 0;
4037 Temp lower
= Temp();
4038 if (num_bytes
> 16) {
4039 assert(num_components
== 3 || num_components
== 4);
4040 op
= aco_opcode::buffer_load_dwordx4
;
4041 lower
= bld
.tmp(v4
);
4042 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
4043 mubuf
->definitions
[0] = Definition(lower
);
4044 mubuf
->operands
[0] = Operand(rsrc
);
4045 mubuf
->operands
[1] = vaddr
;
4046 mubuf
->operands
[2] = soffset
;
4047 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4050 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
4051 mubuf
->can_reorder
= readonly
;
4052 bld
.insert(std::move(mubuf
));
4053 emit_split_vector(ctx
, lower
, 2);
4056 } else if (num_bytes
== 12 && ctx
->options
->chip_class
== GFX6
) {
4057 /* GFX6 doesn't support loading vec3, expand to vec4. */
4061 switch (num_bytes
) {
4063 op
= aco_opcode::buffer_load_dword
;
4066 op
= aco_opcode::buffer_load_dwordx2
;
4069 assert(ctx
->options
->chip_class
> GFX6
);
4070 op
= aco_opcode::buffer_load_dwordx3
;
4073 op
= aco_opcode::buffer_load_dwordx4
;
4076 unreachable("Load SSBO not implemented for this size.");
4078 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
4079 mubuf
->operands
[0] = Operand(rsrc
);
4080 mubuf
->operands
[1] = vaddr
;
4081 mubuf
->operands
[2] = soffset
;
4082 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4085 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
4086 mubuf
->can_reorder
= readonly
;
4087 mubuf
->offset
= const_offset
;
4088 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
4090 if (dst
.size() > 4) {
4091 assert(lower
!= Temp());
4092 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
4093 instr
->definitions
[0] = Definition(upper
);
4094 bld
.insert(std::move(instr
));
4095 if (dst
.size() == 8)
4096 emit_split_vector(ctx
, upper
, 2);
4097 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
4098 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
4099 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
4100 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
4101 if (dst
.size() == 8)
4102 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
4103 } else if (dst
.size() == 3 && ctx
->options
->chip_class
== GFX6
) {
4104 Temp vec
= bld
.tmp(v4
);
4105 instr
->definitions
[0] = Definition(vec
);
4106 bld
.insert(std::move(instr
));
4107 emit_split_vector(ctx
, vec
, 4);
4109 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, 3, 1));
4110 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, vec
, 0, v1
));
4111 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, vec
, 1, v1
));
4112 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, vec
, 2, v1
));
4115 if (dst
.type() == RegType::sgpr
) {
4116 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4117 instr
->definitions
[0] = Definition(vec
);
4118 bld
.insert(std::move(instr
));
4119 expand_vector(ctx
, vec
, dst
, num_components
, (1 << num_components
) - 1);
4121 instr
->definitions
[0] = Definition(dst
);
4122 bld
.insert(std::move(instr
));
4123 emit_split_vector(ctx
, dst
, num_components
);
4126 switch (num_bytes
) {
4128 op
= aco_opcode::s_buffer_load_dword
;
4131 op
= aco_opcode::s_buffer_load_dwordx2
;
4135 op
= aco_opcode::s_buffer_load_dwordx4
;
4139 op
= aco_opcode::s_buffer_load_dwordx8
;
4142 unreachable("Load SSBO not implemented for this size.");
4144 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4145 load
->operands
[0] = Operand(rsrc
);
4146 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
4147 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
4148 load
->definitions
[0] = Definition(dst
);
4151 load
->barrier
= readonly
? barrier_none
: barrier_buffer
;
4152 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
4153 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4156 if (dst
.size() == 3) {
4157 Temp vec
= bld
.tmp(s4
);
4158 load
->definitions
[0] = Definition(vec
);
4159 bld
.insert(std::move(load
));
4160 emit_split_vector(ctx
, vec
, 4);
4162 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4163 emit_extract_vector(ctx
, vec
, 0, s1
),
4164 emit_extract_vector(ctx
, vec
, 1, s1
),
4165 emit_extract_vector(ctx
, vec
, 2, s1
));
4166 } else if (dst
.size() == 6) {
4167 Temp vec
= bld
.tmp(s8
);
4168 load
->definitions
[0] = Definition(vec
);
4169 bld
.insert(std::move(load
));
4170 emit_split_vector(ctx
, vec
, 4);
4172 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4173 emit_extract_vector(ctx
, vec
, 0, s2
),
4174 emit_extract_vector(ctx
, vec
, 1, s2
),
4175 emit_extract_vector(ctx
, vec
, 2, s2
));
4177 bld
.insert(std::move(load
));
4179 emit_split_vector(ctx
, dst
, num_components
);
4183 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4185 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4186 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4188 Builder
bld(ctx
->program
, ctx
->block
);
4190 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
4191 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
4192 unsigned binding
= nir_intrinsic_binding(idx_instr
);
4193 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
4195 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
4196 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
4197 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
4198 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
4199 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
4200 if (ctx
->options
->chip_class
>= GFX10
) {
4201 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
4202 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
4203 S_008F0C_RESOURCE_LEVEL(1);
4205 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4206 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4208 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
4209 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
4210 Operand(0xFFFFFFFFu
),
4211 Operand(desc_type
));
4212 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
4213 rsrc
, upper_dwords
);
4215 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
4216 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4219 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4222 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4224 Builder
bld(ctx
->program
, ctx
->block
);
4225 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4227 unsigned offset
= nir_intrinsic_base(instr
);
4228 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
4229 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
4231 unsigned count
= instr
->dest
.ssa
.num_components
;
4232 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
4233 start
-= ctx
->args
->ac
.base_inline_push_consts
;
4234 if (start
+ count
<= ctx
->args
->ac
.num_inline_push_consts
) {
4235 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4236 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4237 for (unsigned i
= 0; i
< count
; ++i
) {
4238 elems
[i
] = get_arg(ctx
, ctx
->args
->ac
.inline_push_consts
[start
+ i
]);
4239 vec
->operands
[i
] = Operand
{elems
[i
]};
4241 vec
->definitions
[0] = Definition(dst
);
4242 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4243 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
4248 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4249 if (offset
!= 0) // TODO check if index != 0 as well
4250 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
4251 Temp ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->ac
.push_constants
));
4256 switch (dst
.size()) {
4258 op
= aco_opcode::s_load_dword
;
4261 op
= aco_opcode::s_load_dwordx2
;
4267 op
= aco_opcode::s_load_dwordx4
;
4273 op
= aco_opcode::s_load_dwordx8
;
4276 unreachable("unimplemented or forbidden load_push_constant.");
4279 bld
.smem(op
, Definition(vec
), ptr
, index
);
4282 emit_split_vector(ctx
, vec
, 4);
4283 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
4284 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4285 emit_extract_vector(ctx
, vec
, 0, rc
),
4286 emit_extract_vector(ctx
, vec
, 1, rc
),
4287 emit_extract_vector(ctx
, vec
, 2, rc
));
4290 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4293 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4295 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4297 Builder
bld(ctx
->program
, ctx
->block
);
4299 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
4300 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
4301 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
4302 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
4303 if (ctx
->options
->chip_class
>= GFX10
) {
4304 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
4305 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
4306 S_008F0C_RESOURCE_LEVEL(1);
4308 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4309 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4312 unsigned base
= nir_intrinsic_base(instr
);
4313 unsigned range
= nir_intrinsic_range(instr
);
4315 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4316 if (base
&& offset
.type() == RegType::sgpr
)
4317 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
4318 else if (base
&& offset
.type() == RegType::vgpr
)
4319 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
4321 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
4322 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
4323 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
4324 Operand(desc_type
));
4326 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
4329 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4331 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
4332 ctx
->cf_info
.exec_potentially_empty_discard
= true;
4334 ctx
->program
->needs_exact
= true;
4336 // TODO: optimize uniform conditions
4337 Builder
bld(ctx
->program
, ctx
->block
);
4338 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4339 assert(src
.regClass() == bld
.lm
);
4340 src
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
4341 bld
.pseudo(aco_opcode::p_discard_if
, src
);
4342 ctx
->block
->kind
|= block_kind_uses_discard_if
;
4346 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
4348 Builder
bld(ctx
->program
, ctx
->block
);
4350 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
4351 ctx
->cf_info
.exec_potentially_empty_discard
= true;
4353 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
4354 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
4356 if (ctx
->block
->loop_nest_depth
&&
4357 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
4358 /* we handle discards the same way as jump instructions */
4359 append_logical_end(ctx
->block
);
4361 /* in loops, discard behaves like break */
4362 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
4363 ctx
->block
->kind
|= block_kind_discard
;
4366 /* uniform discard - loop ends here */
4367 assert(nir_instr_is_last(&instr
->instr
));
4368 ctx
->block
->kind
|= block_kind_uniform
;
4369 ctx
->cf_info
.has_branch
= true;
4370 bld
.branch(aco_opcode::p_branch
);
4371 add_linear_edge(ctx
->block
->index
, linear_target
);
4375 /* we add a break right behind the discard() instructions */
4376 ctx
->block
->kind
|= block_kind_break
;
4377 unsigned idx
= ctx
->block
->index
;
4379 /* remove critical edges from linear CFG */
4380 bld
.branch(aco_opcode::p_branch
);
4381 Block
* break_block
= ctx
->program
->create_and_insert_block();
4382 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
4383 break_block
->kind
|= block_kind_uniform
;
4384 add_linear_edge(idx
, break_block
);
4385 add_linear_edge(break_block
->index
, linear_target
);
4386 bld
.reset(break_block
);
4387 bld
.branch(aco_opcode::p_branch
);
4389 Block
* continue_block
= ctx
->program
->create_and_insert_block();
4390 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
4391 add_linear_edge(idx
, continue_block
);
4392 append_logical_start(continue_block
);
4393 ctx
->block
= continue_block
;
4398 /* it can currently happen that NIR doesn't remove the unreachable code */
4399 if (!nir_instr_is_last(&instr
->instr
)) {
4400 ctx
->program
->needs_exact
= true;
4401 /* save exec somewhere temporarily so that it doesn't get
4402 * overwritten before the discard from outer exec masks */
4403 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, bld
.lm
));
4404 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
4405 ctx
->block
->kind
|= block_kind_uses_discard_if
;
4409 /* This condition is incorrect for uniformly branched discards in a loop
4410 * predicated by a divergent condition, but the above code catches that case
4411 * and the discard would end up turning into a discard_if.
4421 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
4422 /* program just ends here */
4423 ctx
->block
->kind
|= block_kind_uniform
;
4424 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
4425 0 /* enabled mask */, 9 /* dest */,
4426 false /* compressed */, true/* done */, true /* valid mask */);
4427 bld
.sopp(aco_opcode::s_endpgm
);
4428 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
4430 ctx
->block
->kind
|= block_kind_discard
;
4431 /* branch and linear edge is added by visit_if() */
4435 enum aco_descriptor_type
{
4446 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
4447 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4449 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
4450 return dim
== ac_image_cube
||
4451 dim
== ac_image_1darray
||
4452 dim
== ac_image_2darray
||
4453 dim
== ac_image_2darraymsaa
;
4456 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
4457 enum aco_descriptor_type desc_type
,
4458 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
4460 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
4461 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
4462 if (it != ctx->tex_desc.end())
4465 Temp index
= Temp();
4466 bool index_set
= false;
4467 unsigned constant_index
= 0;
4468 unsigned descriptor_set
;
4469 unsigned base_index
;
4470 Builder
bld(ctx
->program
, ctx
->block
);
4473 assert(tex_instr
&& !image
);
4475 base_index
= tex_instr
->sampler_index
;
4477 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4478 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4482 assert(deref_instr
->deref_type
== nir_deref_type_array
);
4483 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
4485 constant_index
+= array_size
* const_value
->u32
;
4487 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
4488 if (indirect
.type() == RegType::vgpr
)
4489 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
4491 if (array_size
!= 1)
4492 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
4498 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
4502 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4504 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4505 base_index
= deref_instr
->var
->data
.binding
;
4508 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
4509 list
= convert_pointer_to_64_bit(ctx
, list
);
4511 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4512 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4513 unsigned offset
= binding
->offset
;
4514 unsigned stride
= binding
->size
;
4518 assert(base_index
< layout
->binding_count
);
4520 switch (desc_type
) {
4521 case ACO_DESC_IMAGE
:
4523 opcode
= aco_opcode::s_load_dwordx8
;
4525 case ACO_DESC_FMASK
:
4527 opcode
= aco_opcode::s_load_dwordx8
;
4530 case ACO_DESC_SAMPLER
:
4532 opcode
= aco_opcode::s_load_dwordx4
;
4533 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4534 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
4536 case ACO_DESC_BUFFER
:
4538 opcode
= aco_opcode::s_load_dwordx4
;
4540 case ACO_DESC_PLANE_0
:
4541 case ACO_DESC_PLANE_1
:
4543 opcode
= aco_opcode::s_load_dwordx8
;
4544 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
4546 case ACO_DESC_PLANE_2
:
4548 opcode
= aco_opcode::s_load_dwordx4
;
4552 unreachable("invalid desc_type\n");
4555 offset
+= constant_index
* stride
;
4557 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4558 (!index_set
|| binding
->immutable_samplers_equal
)) {
4559 if (binding
->immutable_samplers_equal
)
4562 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4563 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
4564 Operand(samplers
[constant_index
* 4 + 0]),
4565 Operand(samplers
[constant_index
* 4 + 1]),
4566 Operand(samplers
[constant_index
* 4 + 2]),
4567 Operand(samplers
[constant_index
* 4 + 3]));
4572 off
= bld
.copy(bld
.def(s1
), Operand(offset
));
4574 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
4575 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
4578 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
4580 if (desc_type
== ACO_DESC_PLANE_2
) {
4582 for (unsigned i
= 0; i
< 8; i
++)
4583 components
[i
] = bld
.tmp(s1
);
4584 bld
.pseudo(aco_opcode::p_split_vector
,
4585 Definition(components
[0]),
4586 Definition(components
[1]),
4587 Definition(components
[2]),
4588 Definition(components
[3]),
4591 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
4592 bld
.pseudo(aco_opcode::p_split_vector
,
4593 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
4594 Definition(components
[4]),
4595 Definition(components
[5]),
4596 Definition(components
[6]),
4597 Definition(components
[7]),
4600 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
4601 components
[0], components
[1], components
[2], components
[3],
4602 components
[4], components
[5], components
[6], components
[7]);
4608 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
4611 case GLSL_SAMPLER_DIM_BUF
:
4613 case GLSL_SAMPLER_DIM_1D
:
4614 return array
? 2 : 1;
4615 case GLSL_SAMPLER_DIM_2D
:
4616 return array
? 3 : 2;
4617 case GLSL_SAMPLER_DIM_MS
:
4618 return array
? 4 : 3;
4619 case GLSL_SAMPLER_DIM_3D
:
4620 case GLSL_SAMPLER_DIM_CUBE
:
4622 case GLSL_SAMPLER_DIM_RECT
:
4623 case GLSL_SAMPLER_DIM_SUBPASS
:
4625 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
4634 /* Adjust the sample index according to FMASK.
4636 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4637 * which is the identity mapping. Each nibble says which physical sample
4638 * should be fetched to get that sample.
4640 * For example, 0x11111100 means there are only 2 samples stored and
4641 * the second sample covers 3/4 of the pixel. When reading samples 0
4642 * and 1, return physical sample 0 (determined by the first two 0s
4643 * in FMASK), otherwise return physical sample 1.
4645 * The sample index should be adjusted as follows:
4646 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4648 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, std::vector
<Temp
>& coords
, Operand sample_index
, Temp fmask_desc_ptr
)
4650 Builder
bld(ctx
->program
, ctx
->block
);
4651 Temp fmask
= bld
.tmp(v1
);
4652 unsigned dim
= ctx
->options
->chip_class
>= GFX10
4653 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
4656 Temp coord
= da
? bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), coords
[0], coords
[1], coords
[2]) :
4657 bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), coords
[0], coords
[1]);
4658 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 3, 1)};
4659 load
->operands
[0] = Operand(fmask_desc_ptr
);
4660 load
->operands
[1] = Operand(s4
); /* no sampler */
4661 load
->operands
[2] = Operand(coord
);
4662 load
->definitions
[0] = Definition(fmask
);
4669 load
->can_reorder
= true; /* fmask images shouldn't be modified */
4670 ctx
->block
->instructions
.emplace_back(std::move(load
));
4672 Operand sample_index4
;
4673 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
4674 sample_index4
= Operand(sample_index
.constantValue() << 2);
4675 } else if (sample_index
.regClass() == s1
) {
4676 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
4678 assert(sample_index
.regClass() == v1
);
4679 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
4683 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
4684 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
4685 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
4686 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
4688 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
4690 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4691 * resource descriptor is 0 (invalid),
4693 Temp compare
= bld
.tmp(bld
.lm
);
4694 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
4695 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
4697 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
4699 /* Replace the MSAA sample index. */
4700 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
4703 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
4706 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4707 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4708 bool is_array
= glsl_sampler_type_is_array(type
);
4709 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
4710 assert(!add_frag_pos
&& "Input attachments should be lowered.");
4711 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
4712 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
4713 int count
= image_type_to_components_count(dim
, is_array
);
4714 std::vector
<Temp
> coords(count
);
4715 Builder
bld(ctx
->program
, ctx
->block
);
4719 Temp src2
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4720 /* get sample index */
4721 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
4722 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
4723 Operand sample_index
= sample_cv
? Operand(sample_cv
->u32
) : Operand(emit_extract_vector(ctx
, src2
, 0, v1
));
4724 std::vector
<Temp
> fmask_load_address
;
4725 for (unsigned i
= 0; i
< (is_array
? 3 : 2); i
++)
4726 fmask_load_address
.emplace_back(emit_extract_vector(ctx
, src0
, i
, v1
));
4728 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
4729 coords
[count
] = adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
);
4731 coords
[count
] = emit_extract_vector(ctx
, src2
, 0, v1
);
4736 coords
[0] = emit_extract_vector(ctx
, src0
, 0, v1
);
4737 coords
.resize(coords
.size() + 1);
4738 coords
[1] = bld
.copy(bld
.def(v1
), Operand(0u));
4740 coords
[2] = emit_extract_vector(ctx
, src0
, 1, v1
);
4742 for (int i
= 0; i
< count
; i
++)
4743 coords
[i
] = emit_extract_vector(ctx
, src0
, i
, v1
);
4746 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
4747 instr
->intrinsic
== nir_intrinsic_image_deref_store
) {
4748 int lod_index
= instr
->intrinsic
== nir_intrinsic_image_deref_load
? 3 : 4;
4749 bool level_zero
= nir_src_is_const(instr
->src
[lod_index
]) && nir_src_as_uint(instr
->src
[lod_index
]) == 0;
4752 coords
.emplace_back(get_ssa_temp(ctx
, instr
->src
[lod_index
].ssa
));
4755 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
4756 for (unsigned i
= 0; i
< coords
.size(); i
++)
4757 vec
->operands
[i
] = Operand(coords
[i
]);
4758 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
4759 vec
->definitions
[0] = Definition(res
);
4760 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4765 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4767 Builder
bld(ctx
->program
, ctx
->block
);
4768 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4769 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4770 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4771 bool is_array
= glsl_sampler_type_is_array(type
);
4772 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4774 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4775 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4776 unsigned num_channels
= util_last_bit(mask
);
4777 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4778 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4781 switch (num_channels
) {
4783 opcode
= aco_opcode::buffer_load_format_x
;
4786 opcode
= aco_opcode::buffer_load_format_xy
;
4789 opcode
= aco_opcode::buffer_load_format_xyz
;
4792 opcode
= aco_opcode::buffer_load_format_xyzw
;
4795 unreachable(">4 channel buffer image load");
4797 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4798 load
->operands
[0] = Operand(rsrc
);
4799 load
->operands
[1] = Operand(vindex
);
4800 load
->operands
[2] = Operand((uint32_t) 0);
4802 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4805 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4806 load
->definitions
[0] = Definition(tmp
);
4808 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4809 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
4810 load
->barrier
= barrier_image
;
4811 ctx
->block
->instructions
.emplace_back(std::move(load
));
4813 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4817 Temp coords
= get_image_coords(ctx
, instr
, type
);
4818 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4820 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4821 unsigned num_components
= util_bitcount(dmask
);
4823 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4826 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4828 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
4829 aco_opcode opcode
= level_zero
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
4831 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1)};
4832 load
->operands
[0] = Operand(resource
);
4833 load
->operands
[1] = Operand(s4
); /* no sampler */
4834 load
->operands
[2] = Operand(coords
);
4835 load
->definitions
[0] = Definition(tmp
);
4836 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4837 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
4838 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4839 load
->dmask
= dmask
;
4841 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4842 load
->barrier
= barrier_image
;
4843 ctx
->block
->instructions
.emplace_back(std::move(load
));
4845 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4849 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4851 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4852 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4853 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4854 bool is_array
= glsl_sampler_type_is_array(type
);
4855 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4857 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4859 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4860 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4861 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4863 switch (data
.size()) {
4865 opcode
= aco_opcode::buffer_store_format_x
;
4868 opcode
= aco_opcode::buffer_store_format_xy
;
4871 opcode
= aco_opcode::buffer_store_format_xyz
;
4874 opcode
= aco_opcode::buffer_store_format_xyzw
;
4877 unreachable(">4 channel buffer image store");
4879 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4880 store
->operands
[0] = Operand(rsrc
);
4881 store
->operands
[1] = Operand(vindex
);
4882 store
->operands
[2] = Operand((uint32_t) 0);
4883 store
->operands
[3] = Operand(data
);
4884 store
->idxen
= true;
4887 store
->disable_wqm
= true;
4888 store
->barrier
= barrier_image
;
4889 ctx
->program
->needs_exact
= true;
4890 ctx
->block
->instructions
.emplace_back(std::move(store
));
4894 assert(data
.type() == RegType::vgpr
);
4895 Temp coords
= get_image_coords(ctx
, instr
, type
);
4896 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4898 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
4899 aco_opcode opcode
= level_zero
? aco_opcode::image_store
: aco_opcode::image_store_mip
;
4901 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 0)};
4902 store
->operands
[0] = Operand(resource
);
4903 store
->operands
[1] = Operand(data
);
4904 store
->operands
[2] = Operand(coords
);
4907 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4908 store
->dmask
= (1 << data
.size()) - 1;
4910 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4911 store
->disable_wqm
= true;
4912 store
->barrier
= barrier_image
;
4913 ctx
->program
->needs_exact
= true;
4914 ctx
->block
->instructions
.emplace_back(std::move(store
));
4918 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4920 /* return the previous value if dest is ever used */
4921 bool return_previous
= false;
4922 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4923 return_previous
= true;
4926 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4927 return_previous
= true;
4931 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4932 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4933 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4934 bool is_array
= glsl_sampler_type_is_array(type
);
4935 Builder
bld(ctx
->program
, ctx
->block
);
4937 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4938 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4940 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4941 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4943 aco_opcode buf_op
, image_op
;
4944 switch (instr
->intrinsic
) {
4945 case nir_intrinsic_image_deref_atomic_add
:
4946 buf_op
= aco_opcode::buffer_atomic_add
;
4947 image_op
= aco_opcode::image_atomic_add
;
4949 case nir_intrinsic_image_deref_atomic_umin
:
4950 buf_op
= aco_opcode::buffer_atomic_umin
;
4951 image_op
= aco_opcode::image_atomic_umin
;
4953 case nir_intrinsic_image_deref_atomic_imin
:
4954 buf_op
= aco_opcode::buffer_atomic_smin
;
4955 image_op
= aco_opcode::image_atomic_smin
;
4957 case nir_intrinsic_image_deref_atomic_umax
:
4958 buf_op
= aco_opcode::buffer_atomic_umax
;
4959 image_op
= aco_opcode::image_atomic_umax
;
4961 case nir_intrinsic_image_deref_atomic_imax
:
4962 buf_op
= aco_opcode::buffer_atomic_smax
;
4963 image_op
= aco_opcode::image_atomic_smax
;
4965 case nir_intrinsic_image_deref_atomic_and
:
4966 buf_op
= aco_opcode::buffer_atomic_and
;
4967 image_op
= aco_opcode::image_atomic_and
;
4969 case nir_intrinsic_image_deref_atomic_or
:
4970 buf_op
= aco_opcode::buffer_atomic_or
;
4971 image_op
= aco_opcode::image_atomic_or
;
4973 case nir_intrinsic_image_deref_atomic_xor
:
4974 buf_op
= aco_opcode::buffer_atomic_xor
;
4975 image_op
= aco_opcode::image_atomic_xor
;
4977 case nir_intrinsic_image_deref_atomic_exchange
:
4978 buf_op
= aco_opcode::buffer_atomic_swap
;
4979 image_op
= aco_opcode::image_atomic_swap
;
4981 case nir_intrinsic_image_deref_atomic_comp_swap
:
4982 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4983 image_op
= aco_opcode::image_atomic_cmpswap
;
4986 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4989 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4991 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4992 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4993 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4994 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4995 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4996 mubuf
->operands
[0] = Operand(resource
);
4997 mubuf
->operands
[1] = Operand(vindex
);
4998 mubuf
->operands
[2] = Operand((uint32_t)0);
4999 mubuf
->operands
[3] = Operand(data
);
5000 if (return_previous
)
5001 mubuf
->definitions
[0] = Definition(dst
);
5003 mubuf
->idxen
= true;
5004 mubuf
->glc
= return_previous
;
5005 mubuf
->dlc
= false; /* Not needed for atomics */
5006 mubuf
->disable_wqm
= true;
5007 mubuf
->barrier
= barrier_image
;
5008 ctx
->program
->needs_exact
= true;
5009 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
5013 Temp coords
= get_image_coords(ctx
, instr
, type
);
5014 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
5015 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 3, return_previous
? 1 : 0)};
5016 mimg
->operands
[0] = Operand(resource
);
5017 mimg
->operands
[1] = Operand(data
);
5018 mimg
->operands
[2] = Operand(coords
);
5019 if (return_previous
)
5020 mimg
->definitions
[0] = Definition(dst
);
5021 mimg
->glc
= return_previous
;
5022 mimg
->dlc
= false; /* Not needed for atomics */
5023 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
5024 mimg
->dmask
= (1 << data
.size()) - 1;
5026 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
5027 mimg
->disable_wqm
= true;
5028 mimg
->barrier
= barrier_image
;
5029 ctx
->program
->needs_exact
= true;
5030 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
5034 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
5036 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
5037 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
5038 Builder
bld(ctx
->program
, ctx
->block
);
5040 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
5042 Temp size_div3
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), bld
.copy(bld
.def(v1
), Operand(0xaaaaaaabu
)), size
);
5043 size_div3
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.as_uniform(size_div3
), Operand(1u));
5045 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
5046 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
5048 Temp is12
= bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.def(s1
, scc
), stride
, Operand(12u));
5049 size
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), size_div3
, size
, bld
.scc(is12
));
5051 Temp shr_dst
= dst
.type() == RegType::vgpr
? bld
.tmp(s1
) : dst
;
5052 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(shr_dst
), bld
.def(s1
, scc
),
5053 size
, bld
.sop1(aco_opcode::s_ff1_i32_b32
, bld
.def(s1
), stride
));
5054 if (dst
.type() == RegType::vgpr
)
5055 bld
.copy(Definition(dst
), shr_dst
);
5057 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
5059 emit_extract_vector(ctx
, desc
, 2, dst
);
5063 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5065 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5066 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5067 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5068 bool is_array
= glsl_sampler_type_is_array(type
);
5069 Builder
bld(ctx
->program
, ctx
->block
);
5071 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
5072 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
5073 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
5077 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
5080 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
5082 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5084 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1)};
5085 mimg
->operands
[0] = Operand(resource
);
5086 mimg
->operands
[1] = Operand(s4
); /* no sampler */
5087 mimg
->operands
[2] = Operand(lod
);
5088 uint8_t& dmask
= mimg
->dmask
;
5089 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
5090 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
5091 mimg
->da
= glsl_sampler_type_is_array(type
);
5092 mimg
->can_reorder
= true;
5093 Definition
& def
= mimg
->definitions
[0];
5094 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
5096 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
5097 glsl_sampler_type_is_array(type
)) {
5099 assert(instr
->dest
.ssa
.num_components
== 3);
5100 Temp tmp
= {ctx
->program
->allocateId(), v3
};
5101 def
= Definition(tmp
);
5102 emit_split_vector(ctx
, tmp
, 3);
5104 /* divide 3rd value by 6 by multiplying with magic number */
5105 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
5106 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
5108 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5109 emit_extract_vector(ctx
, tmp
, 0, v1
),
5110 emit_extract_vector(ctx
, tmp
, 1, v1
),
5113 } else if (ctx
->options
->chip_class
== GFX9
&&
5114 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
5115 glsl_sampler_type_is_array(type
)) {
5116 assert(instr
->dest
.ssa
.num_components
== 2);
5117 def
= Definition(dst
);
5120 def
= Definition(dst
);
5123 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
5126 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5128 Builder
bld(ctx
->program
, ctx
->block
);
5129 unsigned num_components
= instr
->num_components
;
5131 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5132 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5133 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
5135 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
5136 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
, false);
5139 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5141 Builder
bld(ctx
->program
, ctx
->block
);
5142 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5143 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5144 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5145 Temp offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
5147 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5148 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
5150 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
5151 ctx
->options
->chip_class
>= GFX8
;
5153 offset
= bld
.as_uniform(offset
);
5154 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
5158 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5159 if (count
== 3 && (smem
|| ctx
->options
->chip_class
== GFX6
)) {
5160 /* GFX6 doesn't support storing vec3, split it. */
5161 writemask
|= 1u << (start
+ 2);
5164 int num_bytes
= count
* elem_size_bytes
;
5166 if (num_bytes
> 16) {
5167 assert(elem_size_bytes
== 8);
5168 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5173 // TODO: check alignment of sub-dword stores
5174 // TODO: split 3 bytes. there is no store instruction for that
5177 if (count
!= instr
->num_components
) {
5178 emit_split_vector(ctx
, data
, instr
->num_components
);
5179 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5180 for (int i
= 0; i
< count
; i
++) {
5181 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
5182 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
5184 write_data
= bld
.tmp(!smem
? RegType::vgpr
: smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
5185 vec
->definitions
[0] = Definition(write_data
);
5186 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5187 } else if (!smem
&& data
.type() != RegType::vgpr
) {
5188 assert(num_bytes
% 4 == 0);
5189 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
5190 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
5191 assert(num_bytes
% 4 == 0);
5192 write_data
= bld
.as_uniform(data
);
5197 aco_opcode vmem_op
, smem_op
;
5198 switch (num_bytes
) {
5200 vmem_op
= aco_opcode::buffer_store_dword
;
5201 smem_op
= aco_opcode::s_buffer_store_dword
;
5204 vmem_op
= aco_opcode::buffer_store_dwordx2
;
5205 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
5208 vmem_op
= aco_opcode::buffer_store_dwordx3
;
5209 smem_op
= aco_opcode::last_opcode
;
5210 assert(!smem
&& ctx
->options
->chip_class
> GFX6
);
5213 vmem_op
= aco_opcode::buffer_store_dwordx4
;
5214 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
5217 unreachable("Store SSBO not implemented for this size.");
5219 if (ctx
->stage
== fragment_fs
)
5220 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
5223 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
5224 store
->operands
[0] = Operand(rsrc
);
5226 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
5227 offset
, Operand(start
* elem_size_bytes
));
5228 store
->operands
[1] = Operand(off
);
5230 store
->operands
[1] = Operand(offset
);
5232 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
5233 store
->operands
[1].setFixed(m0
);
5234 store
->operands
[2] = Operand(write_data
);
5235 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
5237 store
->disable_wqm
= true;
5238 store
->barrier
= barrier_buffer
;
5239 ctx
->block
->instructions
.emplace_back(std::move(store
));
5240 ctx
->program
->wb_smem_l1_on_end
= true;
5241 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
5242 ctx
->block
->kind
|= block_kind_needs_lowering
;
5243 ctx
->program
->needs_exact
= true;
5246 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
5247 store
->operands
[0] = Operand(rsrc
);
5248 store
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
5249 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
5250 store
->operands
[3] = Operand(write_data
);
5251 store
->offset
= start
* elem_size_bytes
;
5252 store
->offen
= (offset
.type() == RegType::vgpr
);
5253 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
5255 store
->disable_wqm
= true;
5256 store
->barrier
= barrier_buffer
;
5257 ctx
->program
->needs_exact
= true;
5258 ctx
->block
->instructions
.emplace_back(std::move(store
));
5263 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5265 /* return the previous value if dest is ever used */
5266 bool return_previous
= false;
5267 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5268 return_previous
= true;
5271 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5272 return_previous
= true;
5276 Builder
bld(ctx
->program
, ctx
->block
);
5277 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5279 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
5280 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
5281 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
5283 Temp offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5284 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5285 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
5287 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5289 aco_opcode op32
, op64
;
5290 switch (instr
->intrinsic
) {
5291 case nir_intrinsic_ssbo_atomic_add
:
5292 op32
= aco_opcode::buffer_atomic_add
;
5293 op64
= aco_opcode::buffer_atomic_add_x2
;
5295 case nir_intrinsic_ssbo_atomic_imin
:
5296 op32
= aco_opcode::buffer_atomic_smin
;
5297 op64
= aco_opcode::buffer_atomic_smin_x2
;
5299 case nir_intrinsic_ssbo_atomic_umin
:
5300 op32
= aco_opcode::buffer_atomic_umin
;
5301 op64
= aco_opcode::buffer_atomic_umin_x2
;
5303 case nir_intrinsic_ssbo_atomic_imax
:
5304 op32
= aco_opcode::buffer_atomic_smax
;
5305 op64
= aco_opcode::buffer_atomic_smax_x2
;
5307 case nir_intrinsic_ssbo_atomic_umax
:
5308 op32
= aco_opcode::buffer_atomic_umax
;
5309 op64
= aco_opcode::buffer_atomic_umax_x2
;
5311 case nir_intrinsic_ssbo_atomic_and
:
5312 op32
= aco_opcode::buffer_atomic_and
;
5313 op64
= aco_opcode::buffer_atomic_and_x2
;
5315 case nir_intrinsic_ssbo_atomic_or
:
5316 op32
= aco_opcode::buffer_atomic_or
;
5317 op64
= aco_opcode::buffer_atomic_or_x2
;
5319 case nir_intrinsic_ssbo_atomic_xor
:
5320 op32
= aco_opcode::buffer_atomic_xor
;
5321 op64
= aco_opcode::buffer_atomic_xor_x2
;
5323 case nir_intrinsic_ssbo_atomic_exchange
:
5324 op32
= aco_opcode::buffer_atomic_swap
;
5325 op64
= aco_opcode::buffer_atomic_swap_x2
;
5327 case nir_intrinsic_ssbo_atomic_comp_swap
:
5328 op32
= aco_opcode::buffer_atomic_cmpswap
;
5329 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
5332 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
5334 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
5335 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
5336 mubuf
->operands
[0] = Operand(rsrc
);
5337 mubuf
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
5338 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
5339 mubuf
->operands
[3] = Operand(data
);
5340 if (return_previous
)
5341 mubuf
->definitions
[0] = Definition(dst
);
5343 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
5344 mubuf
->glc
= return_previous
;
5345 mubuf
->dlc
= false; /* Not needed for atomics */
5346 mubuf
->disable_wqm
= true;
5347 mubuf
->barrier
= barrier_buffer
;
5348 ctx
->program
->needs_exact
= true;
5349 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
5352 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5354 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5355 Builder
bld(ctx
->program
, ctx
->block
);
5356 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
5357 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
5360 Temp
get_gfx6_global_rsrc(Builder
& bld
, Temp addr
)
5362 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5363 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5365 if (addr
.type() == RegType::vgpr
)
5366 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf
));
5367 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), addr
, Operand(-1u), Operand(rsrc_conf
));
5370 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5372 Builder
bld(ctx
->program
, ctx
->block
);
5373 unsigned num_components
= instr
->num_components
;
5374 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
5376 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5377 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5379 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
5380 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
5382 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
5383 bool global
= ctx
->options
->chip_class
>= GFX9
;
5385 if (ctx
->options
->chip_class
>= GFX7
) {
5387 switch (num_bytes
) {
5389 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
5392 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
5395 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
5398 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
5401 unreachable("load_global not implemented for this size.");
5404 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
5405 flat
->operands
[0] = Operand(addr
);
5406 flat
->operands
[1] = Operand(s1
);
5409 flat
->barrier
= barrier_buffer
;
5411 if (dst
.type() == RegType::sgpr
) {
5412 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
5413 flat
->definitions
[0] = Definition(vec
);
5414 ctx
->block
->instructions
.emplace_back(std::move(flat
));
5415 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
5417 flat
->definitions
[0] = Definition(dst
);
5418 ctx
->block
->instructions
.emplace_back(std::move(flat
));
5420 emit_split_vector(ctx
, dst
, num_components
);
5422 assert(ctx
->options
->chip_class
== GFX6
);
5424 /* GFX6 doesn't support loading vec3, expand to vec4. */
5425 num_bytes
= num_bytes
== 12 ? 16 : num_bytes
;
5428 switch (num_bytes
) {
5430 op
= aco_opcode::buffer_load_dword
;
5433 op
= aco_opcode::buffer_load_dwordx2
;
5436 op
= aco_opcode::buffer_load_dwordx4
;
5439 unreachable("load_global not implemented for this size.");
5442 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
5444 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
5445 mubuf
->operands
[0] = Operand(rsrc
);
5446 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
5447 mubuf
->operands
[2] = Operand(0u);
5451 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
5452 mubuf
->disable_wqm
= false;
5453 mubuf
->barrier
= barrier_buffer
;
5454 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
5457 if (dst
.size() == 3) {
5458 Temp vec
= bld
.tmp(v4
);
5459 instr
->definitions
[0] = Definition(vec
);
5460 bld
.insert(std::move(instr
));
5461 emit_split_vector(ctx
, vec
, 4);
5463 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, 3, 1));
5464 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, vec
, 0, v1
));
5465 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, vec
, 1, v1
));
5466 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, vec
, 2, v1
));
5469 if (dst
.type() == RegType::sgpr
) {
5470 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
5471 instr
->definitions
[0] = Definition(vec
);
5472 bld
.insert(std::move(instr
));
5473 expand_vector(ctx
, vec
, dst
, num_components
, (1 << num_components
) - 1);
5474 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
5476 instr
->definitions
[0] = Definition(dst
);
5477 bld
.insert(std::move(instr
));
5478 emit_split_vector(ctx
, dst
, num_components
);
5482 switch (num_bytes
) {
5484 op
= aco_opcode::s_load_dword
;
5487 op
= aco_opcode::s_load_dwordx2
;
5491 op
= aco_opcode::s_load_dwordx4
;
5494 unreachable("load_global not implemented for this size.");
5496 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
5497 load
->operands
[0] = Operand(addr
);
5498 load
->operands
[1] = Operand(0u);
5499 load
->definitions
[0] = Definition(dst
);
5502 load
->barrier
= barrier_buffer
;
5503 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
5505 if (dst
.size() == 3) {
5507 Temp vec
= bld
.tmp(s4
);
5508 load
->definitions
[0] = Definition(vec
);
5509 ctx
->block
->instructions
.emplace_back(std::move(load
));
5510 emit_split_vector(ctx
, vec
, 4);
5512 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5513 emit_extract_vector(ctx
, vec
, 0, s1
),
5514 emit_extract_vector(ctx
, vec
, 1, s1
),
5515 emit_extract_vector(ctx
, vec
, 2, s1
));
5517 ctx
->block
->instructions
.emplace_back(std::move(load
));
5522 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5524 Builder
bld(ctx
->program
, ctx
->block
);
5525 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5527 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5528 Temp addr
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5530 if (ctx
->options
->chip_class
>= GFX7
)
5531 addr
= as_vgpr(ctx
, addr
);
5533 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5536 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5537 if (count
== 3 && ctx
->options
->chip_class
== GFX6
) {
5538 /* GFX6 doesn't support storing vec3, split it. */
5539 writemask
|= 1u << (start
+ 2);
5542 unsigned num_bytes
= count
* elem_size_bytes
;
5544 Temp write_data
= data
;
5545 if (count
!= instr
->num_components
) {
5546 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5547 for (int i
= 0; i
< count
; i
++)
5548 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
5549 write_data
= bld
.tmp(RegType::vgpr
, count
);
5550 vec
->definitions
[0] = Definition(write_data
);
5551 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5554 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
5555 unsigned offset
= start
* elem_size_bytes
;
5557 if (ctx
->options
->chip_class
>= GFX7
) {
5558 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
5559 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
5560 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
5561 Temp carry
= bld
.tmp(bld
.lm
);
5562 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
5564 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
5565 Operand(offset
), addr0
);
5566 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(bld
.lm
),
5568 carry
).def(1).setHint(vcc
);
5570 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
5575 bool global
= ctx
->options
->chip_class
>= GFX9
;
5577 switch (num_bytes
) {
5579 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
5582 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
5585 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
5588 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
5591 unreachable("store_global not implemented for this size.");
5594 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
5595 flat
->operands
[0] = Operand(addr
);
5596 flat
->operands
[1] = Operand(s1
);
5597 flat
->operands
[2] = Operand(data
);
5600 flat
->offset
= offset
;
5601 flat
->disable_wqm
= true;
5602 flat
->barrier
= barrier_buffer
;
5603 ctx
->program
->needs_exact
= true;
5604 ctx
->block
->instructions
.emplace_back(std::move(flat
));
5606 assert(ctx
->options
->chip_class
== GFX6
);
5609 switch (num_bytes
) {
5611 op
= aco_opcode::buffer_store_dword
;
5614 op
= aco_opcode::buffer_store_dwordx2
;
5617 op
= aco_opcode::buffer_store_dwordx4
;
5620 unreachable("store_global not implemented for this size.");
5623 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
5625 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, 0)};
5626 mubuf
->operands
[0] = Operand(rsrc
);
5627 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
5628 mubuf
->operands
[2] = Operand(0u);
5629 mubuf
->operands
[3] = Operand(write_data
);
5632 mubuf
->offset
= offset
;
5633 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
5634 mubuf
->disable_wqm
= true;
5635 mubuf
->barrier
= barrier_buffer
;
5636 ctx
->program
->needs_exact
= true;
5637 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
5642 void visit_global_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5644 /* return the previous value if dest is ever used */
5645 bool return_previous
= false;
5646 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5647 return_previous
= true;
5650 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5651 return_previous
= true;
5655 Builder
bld(ctx
->program
, ctx
->block
);
5656 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5657 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5659 if (ctx
->options
->chip_class
>= GFX7
)
5660 addr
= as_vgpr(ctx
, addr
);
5662 if (instr
->intrinsic
== nir_intrinsic_global_atomic_comp_swap
)
5663 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
5664 get_ssa_temp(ctx
, instr
->src
[2].ssa
), data
);
5666 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5668 aco_opcode op32
, op64
;
5670 if (ctx
->options
->chip_class
>= GFX7
) {
5671 bool global
= ctx
->options
->chip_class
>= GFX9
;
5672 switch (instr
->intrinsic
) {
5673 case nir_intrinsic_global_atomic_add
:
5674 op32
= global
? aco_opcode::global_atomic_add
: aco_opcode::flat_atomic_add
;
5675 op64
= global
? aco_opcode::global_atomic_add_x2
: aco_opcode::flat_atomic_add_x2
;
5677 case nir_intrinsic_global_atomic_imin
:
5678 op32
= global
? aco_opcode::global_atomic_smin
: aco_opcode::flat_atomic_smin
;
5679 op64
= global
? aco_opcode::global_atomic_smin_x2
: aco_opcode::flat_atomic_smin_x2
;
5681 case nir_intrinsic_global_atomic_umin
:
5682 op32
= global
? aco_opcode::global_atomic_umin
: aco_opcode::flat_atomic_umin
;
5683 op64
= global
? aco_opcode::global_atomic_umin_x2
: aco_opcode::flat_atomic_umin_x2
;
5685 case nir_intrinsic_global_atomic_imax
:
5686 op32
= global
? aco_opcode::global_atomic_smax
: aco_opcode::flat_atomic_smax
;
5687 op64
= global
? aco_opcode::global_atomic_smax_x2
: aco_opcode::flat_atomic_smax_x2
;
5689 case nir_intrinsic_global_atomic_umax
:
5690 op32
= global
? aco_opcode::global_atomic_umax
: aco_opcode::flat_atomic_umax
;
5691 op64
= global
? aco_opcode::global_atomic_umax_x2
: aco_opcode::flat_atomic_umax_x2
;
5693 case nir_intrinsic_global_atomic_and
:
5694 op32
= global
? aco_opcode::global_atomic_and
: aco_opcode::flat_atomic_and
;
5695 op64
= global
? aco_opcode::global_atomic_and_x2
: aco_opcode::flat_atomic_and_x2
;
5697 case nir_intrinsic_global_atomic_or
:
5698 op32
= global
? aco_opcode::global_atomic_or
: aco_opcode::flat_atomic_or
;
5699 op64
= global
? aco_opcode::global_atomic_or_x2
: aco_opcode::flat_atomic_or_x2
;
5701 case nir_intrinsic_global_atomic_xor
:
5702 op32
= global
? aco_opcode::global_atomic_xor
: aco_opcode::flat_atomic_xor
;
5703 op64
= global
? aco_opcode::global_atomic_xor_x2
: aco_opcode::flat_atomic_xor_x2
;
5705 case nir_intrinsic_global_atomic_exchange
:
5706 op32
= global
? aco_opcode::global_atomic_swap
: aco_opcode::flat_atomic_swap
;
5707 op64
= global
? aco_opcode::global_atomic_swap_x2
: aco_opcode::flat_atomic_swap_x2
;
5709 case nir_intrinsic_global_atomic_comp_swap
:
5710 op32
= global
? aco_opcode::global_atomic_cmpswap
: aco_opcode::flat_atomic_cmpswap
;
5711 op64
= global
? aco_opcode::global_atomic_cmpswap_x2
: aco_opcode::flat_atomic_cmpswap_x2
;
5714 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5717 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
5718 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, return_previous
? 1 : 0)};
5719 flat
->operands
[0] = Operand(addr
);
5720 flat
->operands
[1] = Operand(s1
);
5721 flat
->operands
[2] = Operand(data
);
5722 if (return_previous
)
5723 flat
->definitions
[0] = Definition(dst
);
5724 flat
->glc
= return_previous
;
5725 flat
->dlc
= false; /* Not needed for atomics */
5727 flat
->disable_wqm
= true;
5728 flat
->barrier
= barrier_buffer
;
5729 ctx
->program
->needs_exact
= true;
5730 ctx
->block
->instructions
.emplace_back(std::move(flat
));
5732 assert(ctx
->options
->chip_class
== GFX6
);
5734 switch (instr
->intrinsic
) {
5735 case nir_intrinsic_global_atomic_add
:
5736 op32
= aco_opcode::buffer_atomic_add
;
5737 op64
= aco_opcode::buffer_atomic_add_x2
;
5739 case nir_intrinsic_global_atomic_imin
:
5740 op32
= aco_opcode::buffer_atomic_smin
;
5741 op64
= aco_opcode::buffer_atomic_smin_x2
;
5743 case nir_intrinsic_global_atomic_umin
:
5744 op32
= aco_opcode::buffer_atomic_umin
;
5745 op64
= aco_opcode::buffer_atomic_umin_x2
;
5747 case nir_intrinsic_global_atomic_imax
:
5748 op32
= aco_opcode::buffer_atomic_smax
;
5749 op64
= aco_opcode::buffer_atomic_smax_x2
;
5751 case nir_intrinsic_global_atomic_umax
:
5752 op32
= aco_opcode::buffer_atomic_umax
;
5753 op64
= aco_opcode::buffer_atomic_umax_x2
;
5755 case nir_intrinsic_global_atomic_and
:
5756 op32
= aco_opcode::buffer_atomic_and
;
5757 op64
= aco_opcode::buffer_atomic_and_x2
;
5759 case nir_intrinsic_global_atomic_or
:
5760 op32
= aco_opcode::buffer_atomic_or
;
5761 op64
= aco_opcode::buffer_atomic_or_x2
;
5763 case nir_intrinsic_global_atomic_xor
:
5764 op32
= aco_opcode::buffer_atomic_xor
;
5765 op64
= aco_opcode::buffer_atomic_xor_x2
;
5767 case nir_intrinsic_global_atomic_exchange
:
5768 op32
= aco_opcode::buffer_atomic_swap
;
5769 op64
= aco_opcode::buffer_atomic_swap_x2
;
5771 case nir_intrinsic_global_atomic_comp_swap
:
5772 op32
= aco_opcode::buffer_atomic_cmpswap
;
5773 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
5776 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
5779 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
5781 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
5783 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
5784 mubuf
->operands
[0] = Operand(rsrc
);
5785 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
5786 mubuf
->operands
[2] = Operand(0u);
5787 mubuf
->operands
[3] = Operand(data
);
5788 if (return_previous
)
5789 mubuf
->definitions
[0] = Definition(dst
);
5790 mubuf
->glc
= return_previous
;
5793 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
5794 mubuf
->disable_wqm
= true;
5795 mubuf
->barrier
= barrier_buffer
;
5796 ctx
->program
->needs_exact
= true;
5797 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
5801 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5802 Builder
bld(ctx
->program
, ctx
->block
);
5803 switch(instr
->intrinsic
) {
5804 case nir_intrinsic_group_memory_barrier
:
5805 case nir_intrinsic_memory_barrier
:
5806 bld
.barrier(aco_opcode::p_memory_barrier_common
);
5808 case nir_intrinsic_memory_barrier_buffer
:
5809 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
5811 case nir_intrinsic_memory_barrier_image
:
5812 bld
.barrier(aco_opcode::p_memory_barrier_image
);
5814 case nir_intrinsic_memory_barrier_tcs_patch
:
5815 case nir_intrinsic_memory_barrier_shared
:
5816 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
5819 unreachable("Unimplemented memory barrier intrinsic");
5824 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5826 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
5827 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5828 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
5829 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5830 Builder
bld(ctx
->program
, ctx
->block
);
5832 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
5833 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
5834 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
5837 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5839 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5840 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5841 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5842 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5843 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
5845 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
5846 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
5849 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5851 unsigned offset
= nir_intrinsic_base(instr
);
5852 Operand m
= load_lds_size_m0(ctx
);
5853 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5854 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5856 unsigned num_operands
= 3;
5857 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
5858 switch(instr
->intrinsic
) {
5859 case nir_intrinsic_shared_atomic_add
:
5860 op32
= aco_opcode::ds_add_u32
;
5861 op64
= aco_opcode::ds_add_u64
;
5862 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
5863 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
5865 case nir_intrinsic_shared_atomic_imin
:
5866 op32
= aco_opcode::ds_min_i32
;
5867 op64
= aco_opcode::ds_min_i64
;
5868 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
5869 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
5871 case nir_intrinsic_shared_atomic_umin
:
5872 op32
= aco_opcode::ds_min_u32
;
5873 op64
= aco_opcode::ds_min_u64
;
5874 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
5875 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
5877 case nir_intrinsic_shared_atomic_imax
:
5878 op32
= aco_opcode::ds_max_i32
;
5879 op64
= aco_opcode::ds_max_i64
;
5880 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
5881 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
5883 case nir_intrinsic_shared_atomic_umax
:
5884 op32
= aco_opcode::ds_max_u32
;
5885 op64
= aco_opcode::ds_max_u64
;
5886 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
5887 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
5889 case nir_intrinsic_shared_atomic_and
:
5890 op32
= aco_opcode::ds_and_b32
;
5891 op64
= aco_opcode::ds_and_b64
;
5892 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
5893 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
5895 case nir_intrinsic_shared_atomic_or
:
5896 op32
= aco_opcode::ds_or_b32
;
5897 op64
= aco_opcode::ds_or_b64
;
5898 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
5899 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
5901 case nir_intrinsic_shared_atomic_xor
:
5902 op32
= aco_opcode::ds_xor_b32
;
5903 op64
= aco_opcode::ds_xor_b64
;
5904 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
5905 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
5907 case nir_intrinsic_shared_atomic_exchange
:
5908 op32
= aco_opcode::ds_write_b32
;
5909 op64
= aco_opcode::ds_write_b64
;
5910 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
5911 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
5913 case nir_intrinsic_shared_atomic_comp_swap
:
5914 op32
= aco_opcode::ds_cmpst_b32
;
5915 op64
= aco_opcode::ds_cmpst_b64
;
5916 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
5917 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
5921 unreachable("Unhandled shared atomic intrinsic");
5924 /* return the previous value if dest is ever used */
5925 bool return_previous
= false;
5926 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5927 return_previous
= true;
5930 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5931 return_previous
= true;
5936 if (data
.size() == 1) {
5937 assert(instr
->dest
.ssa
.bit_size
== 32);
5938 op
= return_previous
? op32_rtn
: op32
;
5940 assert(instr
->dest
.ssa
.bit_size
== 64);
5941 op
= return_previous
? op64_rtn
: op64
;
5944 if (offset
> 65535) {
5945 Builder
bld(ctx
->program
, ctx
->block
);
5946 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
5950 aco_ptr
<DS_instruction
> ds
;
5951 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
5952 ds
->operands
[0] = Operand(address
);
5953 ds
->operands
[1] = Operand(data
);
5954 if (num_operands
== 4)
5955 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5956 ds
->operands
[num_operands
- 1] = m
;
5957 ds
->offset0
= offset
;
5958 if (return_previous
)
5959 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5960 ctx
->block
->instructions
.emplace_back(std::move(ds
));
5963 Temp
get_scratch_resource(isel_context
*ctx
)
5965 Builder
bld(ctx
->program
, ctx
->block
);
5966 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
5967 if (ctx
->stage
!= compute_cs
)
5968 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
5970 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
5971 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
5973 if (ctx
->program
->chip_class
>= GFX10
) {
5974 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5975 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
5976 S_008F0C_RESOURCE_LEVEL(1);
5977 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
5978 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5979 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5982 /* older generations need element size = 16 bytes. element size removed in GFX9 */
5983 if (ctx
->program
->chip_class
<= GFX8
)
5984 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
5986 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
5989 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5990 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
5991 Builder
bld(ctx
->program
, ctx
->block
);
5992 Temp rsrc
= get_scratch_resource(ctx
);
5993 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5994 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5997 switch (dst
.size()) {
5999 op
= aco_opcode::buffer_load_dword
;
6002 op
= aco_opcode::buffer_load_dwordx2
;
6005 op
= aco_opcode::buffer_load_dwordx3
;
6008 op
= aco_opcode::buffer_load_dwordx4
;
6012 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
6013 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
6014 bld
.def(v4
), rsrc
, offset
,
6015 ctx
->program
->scratch_offset
, 0, true);
6016 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
6017 aco_opcode::buffer_load_dwordx4
,
6018 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
6019 rsrc
, offset
, ctx
->program
->scratch_offset
, 16, true);
6020 emit_split_vector(ctx
, lower
, 2);
6021 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
6022 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
6023 if (dst
.size() == 8) {
6024 emit_split_vector(ctx
, upper
, 2);
6025 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
6026 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
6031 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6032 Format::PSEUDO
, dst
.size() / 2, 1)};
6033 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
6034 vec
->operands
[i
] = Operand(elems
[i
]);
6035 vec
->definitions
[0] = Definition(dst
);
6036 bld
.insert(std::move(vec
));
6037 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
6041 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
6044 bld
.mubuf(op
, Definition(dst
), rsrc
, offset
, ctx
->program
->scratch_offset
, 0, true);
6045 emit_split_vector(ctx
, dst
, instr
->num_components
);
6048 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6049 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
6050 Builder
bld(ctx
->program
, ctx
->block
);
6051 Temp rsrc
= get_scratch_resource(ctx
);
6052 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6053 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6055 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6056 unsigned writemask
= nir_intrinsic_write_mask(instr
);
6060 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
6061 int num_bytes
= count
* elem_size_bytes
;
6063 if (num_bytes
> 16) {
6064 assert(elem_size_bytes
== 8);
6065 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
6070 // TODO: check alignment of sub-dword stores
6071 // TODO: split 3 bytes. there is no store instruction for that
6074 if (count
!= instr
->num_components
) {
6075 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
6076 for (int i
= 0; i
< count
; i
++) {
6077 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
6078 vec
->operands
[i
] = Operand(elem
);
6080 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
6081 vec
->definitions
[0] = Definition(write_data
);
6082 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6088 switch (num_bytes
) {
6090 op
= aco_opcode::buffer_store_dword
;
6093 op
= aco_opcode::buffer_store_dwordx2
;
6096 op
= aco_opcode::buffer_store_dwordx3
;
6099 op
= aco_opcode::buffer_store_dwordx4
;
6102 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
6105 bld
.mubuf(op
, rsrc
, offset
, ctx
->program
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
6109 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6110 uint8_t log2_ps_iter_samples
;
6111 if (ctx
->program
->info
->ps
.force_persample
) {
6112 log2_ps_iter_samples
=
6113 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
6115 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
6118 /* The bit pattern matches that used by fixed function fragment
6120 static const unsigned ps_iter_masks
[] = {
6121 0xffff, /* not used */
6127 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
6129 Builder
bld(ctx
->program
, ctx
->block
);
6131 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
6132 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
6133 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
6134 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
6135 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6136 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, get_arg(ctx
, ctx
->args
->ac
.sample_coverage
));
6139 void visit_emit_vertex_with_counter(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6140 Builder
bld(ctx
->program
, ctx
->block
);
6142 unsigned stream
= nir_intrinsic_stream_id(instr
);
6143 Temp next_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6144 next_vertex
= bld
.v_mul_imm(bld
.def(v1
), next_vertex
, 4u);
6145 nir_const_value
*next_vertex_cv
= nir_src_as_const_value(instr
->src
[0]);
6148 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_GSVS_GS
* 16u));
6150 unsigned num_components
=
6151 ctx
->program
->info
->gs
.num_stream_output_components
[stream
];
6152 assert(num_components
);
6154 unsigned stride
= 4u * num_components
* ctx
->shader
->info
.gs
.vertices_out
;
6155 unsigned stream_offset
= 0;
6156 for (unsigned i
= 0; i
< stream
; i
++) {
6157 unsigned prev_stride
= 4u * ctx
->program
->info
->gs
.num_stream_output_components
[i
] * ctx
->shader
->info
.gs
.vertices_out
;
6158 stream_offset
+= prev_stride
* ctx
->program
->wave_size
;
6161 /* Limit on the stride field for <= GFX7. */
6162 assert(stride
< (1 << 14));
6164 Temp gsvs_dwords
[4];
6165 for (unsigned i
= 0; i
< 4; i
++)
6166 gsvs_dwords
[i
] = bld
.tmp(s1
);
6167 bld
.pseudo(aco_opcode::p_split_vector
,
6168 Definition(gsvs_dwords
[0]),
6169 Definition(gsvs_dwords
[1]),
6170 Definition(gsvs_dwords
[2]),
6171 Definition(gsvs_dwords
[3]),
6174 if (stream_offset
) {
6175 Temp stream_offset_tmp
= bld
.copy(bld
.def(s1
), Operand(stream_offset
));
6177 Temp carry
= bld
.tmp(s1
);
6178 gsvs_dwords
[0] = bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), gsvs_dwords
[0], stream_offset_tmp
);
6179 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
));
6182 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
)));
6183 gsvs_dwords
[2] = bld
.copy(bld
.def(s1
), Operand((uint32_t)ctx
->program
->wave_size
));
6185 gsvs_ring
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
6186 gsvs_dwords
[0], gsvs_dwords
[1], gsvs_dwords
[2], gsvs_dwords
[3]);
6188 unsigned offset
= 0;
6189 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; i
++) {
6190 if (ctx
->program
->info
->gs
.output_streams
[i
] != stream
)
6193 for (unsigned j
= 0; j
< 4; j
++) {
6194 if (!(ctx
->program
->info
->gs
.output_usage_mask
[i
] & (1 << j
)))
6197 if (ctx
->outputs
.mask
[i
] & (1 << j
)) {
6198 Operand vaddr_offset
= next_vertex_cv
? Operand(v1
) : Operand(next_vertex
);
6199 unsigned const_offset
= (offset
+ (next_vertex_cv
? next_vertex_cv
->u32
: 0u)) * 4u;
6200 if (const_offset
>= 4096u) {
6201 if (vaddr_offset
.isUndefined())
6202 vaddr_offset
= bld
.copy(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u));
6204 vaddr_offset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), vaddr_offset
);
6205 const_offset
%= 4096u;
6208 aco_ptr
<MTBUF_instruction
> mtbuf
{create_instruction
<MTBUF_instruction
>(aco_opcode::tbuffer_store_format_x
, Format::MTBUF
, 4, 0)};
6209 mtbuf
->operands
[0] = Operand(gsvs_ring
);
6210 mtbuf
->operands
[1] = vaddr_offset
;
6211 mtbuf
->operands
[2] = Operand(get_arg(ctx
, ctx
->args
->gs2vs_offset
));
6212 mtbuf
->operands
[3] = Operand(ctx
->outputs
.outputs
[i
][j
]);
6213 mtbuf
->offen
= !vaddr_offset
.isUndefined();
6214 mtbuf
->dfmt
= V_008F0C_BUF_DATA_FORMAT_32
;
6215 mtbuf
->nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
6216 mtbuf
->offset
= const_offset
;
6219 mtbuf
->barrier
= barrier_gs_data
;
6220 mtbuf
->can_reorder
= true;
6221 bld
.insert(std::move(mtbuf
));
6224 offset
+= ctx
->shader
->info
.gs
.vertices_out
;
6227 /* outputs for the next vertex are undefined and keeping them around can
6228 * create invalid IR with control flow */
6229 ctx
->outputs
.mask
[i
] = 0;
6232 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(false, true, stream
));
6235 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
6237 Builder
bld(ctx
->program
, ctx
->block
);
6239 if (cluster_size
== 1) {
6241 } if (op
== nir_op_iand
&& cluster_size
== 4) {
6242 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6243 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
6244 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
6245 bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
));
6246 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
6247 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6248 return bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
6249 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)));
6250 } else if (op
== nir_op_iand
&& cluster_size
== ctx
->program
->wave_size
) {
6251 //subgroupAnd(val) -> (exec & ~val) == 0
6252 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
6253 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
6254 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
);
6255 } else if (op
== nir_op_ior
&& cluster_size
== ctx
->program
->wave_size
) {
6256 //subgroupOr(val) -> (val & exec) != 0
6257 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)).def(1).getTemp();
6258 return bool_to_vector_condition(ctx
, tmp
);
6259 } else if (op
== nir_op_ixor
&& cluster_size
== ctx
->program
->wave_size
) {
6260 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6261 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6262 tmp
= bld
.sop1(Builder::s_bcnt1_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
);
6263 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
6264 return bool_to_vector_condition(ctx
, tmp
);
6266 //subgroupClustered{And,Or,Xor}(val, n) ->
6267 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
6268 //cluster_offset = ~(n - 1) & lane_id
6269 //cluster_mask = ((1 << n) - 1)
6270 //subgroupClusteredAnd():
6271 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
6272 //subgroupClusteredOr():
6273 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
6274 //subgroupClusteredXor():
6275 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
6276 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
6277 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
6280 if (op
== nir_op_iand
)
6281 tmp
= bld
.sop2(Builder::s_orn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6283 tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6285 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
6287 if (ctx
->program
->chip_class
<= GFX7
)
6288 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), tmp
, cluster_offset
);
6289 else if (ctx
->program
->wave_size
== 64)
6290 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
6292 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), cluster_offset
, tmp
);
6293 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
6294 if (cluster_mask
!= 0xffffffff)
6295 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
6297 Definition cmp_def
= Definition();
6298 if (op
== nir_op_iand
) {
6299 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(cluster_mask
), tmp
).def(0);
6300 } else if (op
== nir_op_ior
) {
6301 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
6302 } else if (op
== nir_op_ixor
) {
6303 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
6304 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
6305 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
6307 cmp_def
.setHint(vcc
);
6308 return cmp_def
.getTemp();
6312 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
6314 Builder
bld(ctx
->program
, ctx
->block
);
6316 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
6317 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
6318 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
6320 if (op
== nir_op_iand
)
6321 tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
6323 tmp
= bld
.sop2(Builder::s_and
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6325 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
6326 Temp lo
= lohi
.def(0).getTemp();
6327 Temp hi
= lohi
.def(1).getTemp();
6328 Temp mbcnt
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(lo
), Operand(hi
));
6330 Definition cmp_def
= Definition();
6331 if (op
== nir_op_iand
)
6332 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
6333 else if (op
== nir_op_ior
)
6334 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
6335 else if (op
== nir_op_ixor
)
6336 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u),
6337 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
6338 cmp_def
.setHint(vcc
);
6339 return cmp_def
.getTemp();
6342 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
6344 Builder
bld(ctx
->program
, ctx
->block
);
6346 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
6347 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
6348 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
6349 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
6350 if (op
== nir_op_iand
)
6351 return bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
6352 else if (op
== nir_op_ior
)
6353 return bld
.sop2(Builder::s_or
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
6354 else if (op
== nir_op_ixor
)
6355 return bld
.sop2(Builder::s_xor
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
6361 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
6363 Builder
bld(ctx
->program
, ctx
->block
);
6364 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
6365 if (src
.regClass().type() == RegType::vgpr
) {
6366 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
6367 } else if (src
.regClass() == s1
) {
6368 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
6369 } else if (src
.regClass() == s2
) {
6370 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
6372 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6373 nir_print_instr(&instr
->instr
, stderr
);
6374 fprintf(stderr
, "\n");
6378 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
6380 Builder
bld(ctx
->program
, ctx
->block
);
6381 Temp persp_center
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
6382 Temp p1
= emit_extract_vector(ctx
, persp_center
, 0, v1
);
6383 Temp p2
= emit_extract_vector(ctx
, persp_center
, 1, v1
);
6385 Temp ddx_1
, ddx_2
, ddy_1
, ddy_2
;
6386 uint32_t dpp_ctrl0
= dpp_quad_perm(0, 0, 0, 0);
6387 uint32_t dpp_ctrl1
= dpp_quad_perm(1, 1, 1, 1);
6388 uint32_t dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
6391 if (ctx
->program
->chip_class
>= GFX8
) {
6392 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_ctrl0
);
6393 ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl1
);
6394 ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl2
);
6395 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_ctrl0
);
6396 ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl1
);
6397 ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl2
);
6399 Temp tl_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl0
);
6400 ddx_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl1
);
6401 ddx_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_1
, tl_1
);
6402 ddx_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl2
);
6403 ddx_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_2
, tl_1
);
6404 Temp tl_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl0
);
6405 ddy_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl1
);
6406 ddy_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_1
, tl_2
);
6407 ddy_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl2
);
6408 ddy_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_2
, tl_2
);
6411 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
6412 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
6413 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
6414 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
6415 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
6416 Temp wqm1
= bld
.tmp(v1
);
6417 emit_wqm(ctx
, tmp1
, wqm1
, true);
6418 Temp wqm2
= bld
.tmp(v1
);
6419 emit_wqm(ctx
, tmp2
, wqm2
, true);
6420 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
6424 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6426 Builder
bld(ctx
->program
, ctx
->block
);
6427 switch(instr
->intrinsic
) {
6428 case nir_intrinsic_load_barycentric_sample
:
6429 case nir_intrinsic_load_barycentric_pixel
:
6430 case nir_intrinsic_load_barycentric_centroid
: {
6431 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
6432 Temp bary
= Temp(0, s2
);
6434 case INTERP_MODE_SMOOTH
:
6435 case INTERP_MODE_NONE
:
6436 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
6437 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
6438 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
6439 bary
= ctx
->persp_centroid
;
6440 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
6441 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_sample
);
6443 case INTERP_MODE_NOPERSPECTIVE
:
6444 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
6445 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_center
);
6446 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
6447 bary
= ctx
->linear_centroid
;
6448 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
6449 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_sample
);
6454 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6455 Temp p1
= emit_extract_vector(ctx
, bary
, 0, v1
);
6456 Temp p2
= emit_extract_vector(ctx
, bary
, 1, v1
);
6457 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6458 Operand(p1
), Operand(p2
));
6459 emit_split_vector(ctx
, dst
, 2);
6462 case nir_intrinsic_load_barycentric_model
: {
6463 Temp model
= get_arg(ctx
, ctx
->args
->ac
.pull_model
);
6465 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6466 Temp p1
= emit_extract_vector(ctx
, model
, 0, v1
);
6467 Temp p2
= emit_extract_vector(ctx
, model
, 1, v1
);
6468 Temp p3
= emit_extract_vector(ctx
, model
, 2, v1
);
6469 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6470 Operand(p1
), Operand(p2
), Operand(p3
));
6471 emit_split_vector(ctx
, dst
, 3);
6474 case nir_intrinsic_load_barycentric_at_sample
: {
6475 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
6476 switch (ctx
->options
->key
.fs
.num_samples
) {
6477 case 2: sample_pos_offset
+= 1 << 3; break;
6478 case 4: sample_pos_offset
+= 3 << 3; break;
6479 case 8: sample_pos_offset
+= 7 << 3; break;
6483 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6484 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
6485 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
6486 if (addr
.type() == RegType::sgpr
) {
6489 sample_pos_offset
+= const_addr
->u32
<< 3;
6490 offset
= Operand(sample_pos_offset
);
6491 } else if (ctx
->options
->chip_class
>= GFX9
) {
6492 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
6494 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
6495 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
6498 Operand off
= bld
.copy(bld
.def(s1
), Operand(offset
));
6499 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, off
);
6501 } else if (ctx
->options
->chip_class
>= GFX9
) {
6502 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
6503 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
6504 } else if (ctx
->options
->chip_class
>= GFX7
) {
6505 /* addr += private_segment_buffer + sample_pos_offset */
6506 Temp tmp0
= bld
.tmp(s1
);
6507 Temp tmp1
= bld
.tmp(s1
);
6508 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
6509 Definition scc_tmp
= bld
.def(s1
, scc
);
6510 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
6511 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
6512 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
6513 Temp pck0
= bld
.tmp(v1
);
6514 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
6515 tmp1
= as_vgpr(ctx
, tmp1
);
6516 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
);
6517 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
6519 /* sample_pos = flat_load_dwordx2 addr */
6520 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
6522 assert(ctx
->options
->chip_class
== GFX6
);
6524 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
6525 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
6526 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), private_segment_buffer
, Operand(0u), Operand(rsrc_conf
));
6528 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
6529 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), addr
, Operand(0u));
6531 sample_pos
= bld
.tmp(v2
);
6533 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dwordx2
, Format::MUBUF
, 3, 1)};
6534 load
->definitions
[0] = Definition(sample_pos
);
6535 load
->operands
[0] = Operand(rsrc
);
6536 load
->operands
[1] = Operand(addr
);
6537 load
->operands
[2] = Operand(0u);
6538 load
->offset
= sample_pos_offset
;
6540 load
->addr64
= true;
6543 load
->disable_wqm
= false;
6544 load
->barrier
= barrier_none
;
6545 load
->can_reorder
= true;
6546 ctx
->block
->instructions
.emplace_back(std::move(load
));
6549 /* sample_pos -= 0.5 */
6550 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
6551 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
6552 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
6553 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
6554 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
6556 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
6559 case nir_intrinsic_load_barycentric_at_offset
: {
6560 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6561 RegClass rc
= RegClass(offset
.type(), 1);
6562 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
6563 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
6564 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
6567 case nir_intrinsic_load_front_face
: {
6568 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6569 Operand(0u), get_arg(ctx
, ctx
->args
->ac
.front_face
)).def(0).setHint(vcc
);
6572 case nir_intrinsic_load_view_index
: {
6573 if (ctx
->stage
& (sw_vs
| sw_gs
| sw_tcs
| sw_tes
)) {
6574 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6575 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.view_index
)));
6581 case nir_intrinsic_load_layer_id
: {
6582 unsigned idx
= nir_intrinsic_base(instr
);
6583 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6584 Operand(2u), bld
.m0(get_arg(ctx
, ctx
->args
->ac
.prim_mask
)), idx
, 0);
6587 case nir_intrinsic_load_frag_coord
: {
6588 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
6591 case nir_intrinsic_load_sample_pos
: {
6592 Temp posx
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[0]);
6593 Temp posy
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[1]);
6594 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6595 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
6596 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
6599 case nir_intrinsic_load_tess_coord
:
6600 visit_load_tess_coord(ctx
, instr
);
6602 case nir_intrinsic_load_interpolated_input
:
6603 visit_load_interpolated_input(ctx
, instr
);
6605 case nir_intrinsic_store_output
:
6606 visit_store_output(ctx
, instr
);
6608 case nir_intrinsic_load_input
:
6609 case nir_intrinsic_load_input_vertex
:
6610 visit_load_input(ctx
, instr
);
6612 case nir_intrinsic_load_output
:
6613 visit_load_output(ctx
, instr
);
6615 case nir_intrinsic_load_per_vertex_input
:
6616 visit_load_per_vertex_input(ctx
, instr
);
6618 case nir_intrinsic_load_per_vertex_output
:
6619 visit_load_per_vertex_output(ctx
, instr
);
6621 case nir_intrinsic_store_per_vertex_output
:
6622 visit_store_per_vertex_output(ctx
, instr
);
6624 case nir_intrinsic_load_ubo
:
6625 visit_load_ubo(ctx
, instr
);
6627 case nir_intrinsic_load_push_constant
:
6628 visit_load_push_constant(ctx
, instr
);
6630 case nir_intrinsic_load_constant
:
6631 visit_load_constant(ctx
, instr
);
6633 case nir_intrinsic_vulkan_resource_index
:
6634 visit_load_resource(ctx
, instr
);
6636 case nir_intrinsic_discard
:
6637 visit_discard(ctx
, instr
);
6639 case nir_intrinsic_discard_if
:
6640 visit_discard_if(ctx
, instr
);
6642 case nir_intrinsic_load_shared
:
6643 visit_load_shared(ctx
, instr
);
6645 case nir_intrinsic_store_shared
:
6646 visit_store_shared(ctx
, instr
);
6648 case nir_intrinsic_shared_atomic_add
:
6649 case nir_intrinsic_shared_atomic_imin
:
6650 case nir_intrinsic_shared_atomic_umin
:
6651 case nir_intrinsic_shared_atomic_imax
:
6652 case nir_intrinsic_shared_atomic_umax
:
6653 case nir_intrinsic_shared_atomic_and
:
6654 case nir_intrinsic_shared_atomic_or
:
6655 case nir_intrinsic_shared_atomic_xor
:
6656 case nir_intrinsic_shared_atomic_exchange
:
6657 case nir_intrinsic_shared_atomic_comp_swap
:
6658 visit_shared_atomic(ctx
, instr
);
6660 case nir_intrinsic_image_deref_load
:
6661 visit_image_load(ctx
, instr
);
6663 case nir_intrinsic_image_deref_store
:
6664 visit_image_store(ctx
, instr
);
6666 case nir_intrinsic_image_deref_atomic_add
:
6667 case nir_intrinsic_image_deref_atomic_umin
:
6668 case nir_intrinsic_image_deref_atomic_imin
:
6669 case nir_intrinsic_image_deref_atomic_umax
:
6670 case nir_intrinsic_image_deref_atomic_imax
:
6671 case nir_intrinsic_image_deref_atomic_and
:
6672 case nir_intrinsic_image_deref_atomic_or
:
6673 case nir_intrinsic_image_deref_atomic_xor
:
6674 case nir_intrinsic_image_deref_atomic_exchange
:
6675 case nir_intrinsic_image_deref_atomic_comp_swap
:
6676 visit_image_atomic(ctx
, instr
);
6678 case nir_intrinsic_image_deref_size
:
6679 visit_image_size(ctx
, instr
);
6681 case nir_intrinsic_load_ssbo
:
6682 visit_load_ssbo(ctx
, instr
);
6684 case nir_intrinsic_store_ssbo
:
6685 visit_store_ssbo(ctx
, instr
);
6687 case nir_intrinsic_load_global
:
6688 visit_load_global(ctx
, instr
);
6690 case nir_intrinsic_store_global
:
6691 visit_store_global(ctx
, instr
);
6693 case nir_intrinsic_global_atomic_add
:
6694 case nir_intrinsic_global_atomic_imin
:
6695 case nir_intrinsic_global_atomic_umin
:
6696 case nir_intrinsic_global_atomic_imax
:
6697 case nir_intrinsic_global_atomic_umax
:
6698 case nir_intrinsic_global_atomic_and
:
6699 case nir_intrinsic_global_atomic_or
:
6700 case nir_intrinsic_global_atomic_xor
:
6701 case nir_intrinsic_global_atomic_exchange
:
6702 case nir_intrinsic_global_atomic_comp_swap
:
6703 visit_global_atomic(ctx
, instr
);
6705 case nir_intrinsic_ssbo_atomic_add
:
6706 case nir_intrinsic_ssbo_atomic_imin
:
6707 case nir_intrinsic_ssbo_atomic_umin
:
6708 case nir_intrinsic_ssbo_atomic_imax
:
6709 case nir_intrinsic_ssbo_atomic_umax
:
6710 case nir_intrinsic_ssbo_atomic_and
:
6711 case nir_intrinsic_ssbo_atomic_or
:
6712 case nir_intrinsic_ssbo_atomic_xor
:
6713 case nir_intrinsic_ssbo_atomic_exchange
:
6714 case nir_intrinsic_ssbo_atomic_comp_swap
:
6715 visit_atomic_ssbo(ctx
, instr
);
6717 case nir_intrinsic_load_scratch
:
6718 visit_load_scratch(ctx
, instr
);
6720 case nir_intrinsic_store_scratch
:
6721 visit_store_scratch(ctx
, instr
);
6723 case nir_intrinsic_get_buffer_size
:
6724 visit_get_buffer_size(ctx
, instr
);
6726 case nir_intrinsic_control_barrier
: {
6727 if (ctx
->program
->chip_class
== GFX6
&& ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6728 /* GFX6 only (thanks to a hw bug workaround):
6729 * The real barrier instruction isn’t needed, because an entire patch
6730 * always fits into a single wave.
6735 if (ctx
->shader
->info
.stage
== MESA_SHADER_COMPUTE
) {
6736 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
6737 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
6738 if (workgroup_size
> ctx
->program
->wave_size
)
6739 bld
.sopp(aco_opcode::s_barrier
);
6740 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6741 /* For each patch provided during rendering, n TCS shader invocations will be processed,
6742 * where n is the number of vertices in the output patch.
6744 unsigned workgroup_size
= ctx
->tcs_num_patches
* ctx
->shader
->info
.tess
.tcs_vertices_out
;
6745 if (workgroup_size
> ctx
->program
->wave_size
)
6746 bld
.sopp(aco_opcode::s_barrier
);
6748 /* We don't know the workgroup size, so always emit the s_barrier. */
6749 bld
.sopp(aco_opcode::s_barrier
);
6754 case nir_intrinsic_memory_barrier_tcs_patch
:
6755 case nir_intrinsic_group_memory_barrier
:
6756 case nir_intrinsic_memory_barrier
:
6757 case nir_intrinsic_memory_barrier_buffer
:
6758 case nir_intrinsic_memory_barrier_image
:
6759 case nir_intrinsic_memory_barrier_shared
:
6760 emit_memory_barrier(ctx
, instr
);
6762 case nir_intrinsic_load_num_work_groups
: {
6763 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6764 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.num_work_groups
)));
6765 emit_split_vector(ctx
, dst
, 3);
6768 case nir_intrinsic_load_local_invocation_id
: {
6769 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6770 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.local_invocation_ids
)));
6771 emit_split_vector(ctx
, dst
, 3);
6774 case nir_intrinsic_load_work_group_id
: {
6775 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6776 struct ac_arg
*args
= ctx
->args
->ac
.workgroup_ids
;
6777 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6778 args
[0].used
? Operand(get_arg(ctx
, args
[0])) : Operand(0u),
6779 args
[1].used
? Operand(get_arg(ctx
, args
[1])) : Operand(0u),
6780 args
[2].used
? Operand(get_arg(ctx
, args
[2])) : Operand(0u));
6781 emit_split_vector(ctx
, dst
, 3);
6784 case nir_intrinsic_load_local_invocation_index
: {
6785 Temp id
= emit_mbcnt(ctx
, bld
.def(v1
));
6787 /* The tg_size bits [6:11] contain the subgroup id,
6788 * we need this multiplied by the wave size, and then OR the thread id to it.
6790 if (ctx
->program
->wave_size
== 64) {
6791 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
6792 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
6793 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
6794 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
6796 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
6797 Temp tg_num
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
6798 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
6799 bld
.vop3(aco_opcode::v_lshl_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, Operand(0x5u
), id
);
6803 case nir_intrinsic_load_subgroup_id
: {
6804 if (ctx
->stage
== compute_cs
) {
6805 bld
.sop2(aco_opcode::s_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
),
6806 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
6808 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
6812 case nir_intrinsic_load_subgroup_invocation
: {
6813 emit_mbcnt(ctx
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
6816 case nir_intrinsic_load_num_subgroups
: {
6817 if (ctx
->stage
== compute_cs
)
6818 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
),
6819 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
6821 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
6824 case nir_intrinsic_ballot
: {
6825 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6826 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6827 Definition tmp
= bld
.def(dst
.regClass());
6828 Definition lanemask_tmp
= dst
.size() == bld
.lm
.size() ? tmp
: bld
.def(src
.regClass());
6829 if (instr
->src
[0].ssa
->bit_size
== 1) {
6830 assert(src
.regClass() == bld
.lm
);
6831 bld
.sop2(Builder::s_and
, lanemask_tmp
, bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
6832 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
6833 bld
.vopc(aco_opcode::v_cmp_lg_u32
, lanemask_tmp
, Operand(0u), src
);
6834 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
6835 bld
.vopc(aco_opcode::v_cmp_lg_u64
, lanemask_tmp
, Operand(0u), src
);
6837 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6838 nir_print_instr(&instr
->instr
, stderr
);
6839 fprintf(stderr
, "\n");
6841 if (dst
.size() != bld
.lm
.size()) {
6842 /* Wave32 with ballot size set to 64 */
6843 bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
), lanemask_tmp
.getTemp(), Operand(0u));
6845 emit_wqm(ctx
, tmp
.getTemp(), dst
);
6848 case nir_intrinsic_shuffle
:
6849 case nir_intrinsic_read_invocation
: {
6850 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6851 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
]) {
6852 emit_uniform_subgroup(ctx
, instr
, src
);
6854 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
6855 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !ctx
->divergent_vals
[instr
->src
[1].ssa
->index
])
6856 tid
= bld
.as_uniform(tid
);
6857 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6858 if (src
.regClass() == v1
) {
6859 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
6860 } else if (src
.regClass() == v2
) {
6861 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6862 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6863 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
6864 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
6865 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6866 emit_split_vector(ctx
, dst
, 2);
6867 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
6868 assert(src
.regClass() == bld
.lm
);
6869 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
, tid
);
6870 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
6871 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
6872 assert(src
.regClass() == bld
.lm
);
6874 if (ctx
->program
->chip_class
<= GFX7
)
6875 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), src
, tid
);
6876 else if (ctx
->program
->wave_size
== 64)
6877 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
6879 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), tid
, src
);
6880 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
6881 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
6882 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
), dst
);
6884 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6885 nir_print_instr(&instr
->instr
, stderr
);
6886 fprintf(stderr
, "\n");
6891 case nir_intrinsic_load_sample_id
: {
6892 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6893 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
6896 case nir_intrinsic_load_sample_mask_in
: {
6897 visit_load_sample_mask_in(ctx
, instr
);
6900 case nir_intrinsic_read_first_invocation
: {
6901 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6902 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6903 if (src
.regClass() == v1
) {
6905 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
6907 } else if (src
.regClass() == v2
) {
6908 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
6909 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
6910 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
6911 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
6912 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
6913 emit_split_vector(ctx
, dst
, 2);
6914 } else if (instr
->dest
.ssa
.bit_size
== 1) {
6915 assert(src
.regClass() == bld
.lm
);
6916 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
,
6917 bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)));
6918 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
6919 } else if (src
.regClass() == s1
) {
6920 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
6921 } else if (src
.regClass() == s2
) {
6922 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
6924 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
6925 nir_print_instr(&instr
->instr
, stderr
);
6926 fprintf(stderr
, "\n");
6930 case nir_intrinsic_vote_all
: {
6931 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6932 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6933 assert(src
.regClass() == bld
.lm
);
6934 assert(dst
.regClass() == bld
.lm
);
6936 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
6937 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
6938 bld
.sop1(Builder::s_not
, Definition(dst
), bld
.def(s1
, scc
), cond
);
6941 case nir_intrinsic_vote_any
: {
6942 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6943 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6944 assert(src
.regClass() == bld
.lm
);
6945 assert(dst
.regClass() == bld
.lm
);
6947 Temp tmp
= bool_to_scalar_condition(ctx
, src
);
6948 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
6951 case nir_intrinsic_reduce
:
6952 case nir_intrinsic_inclusive_scan
:
6953 case nir_intrinsic_exclusive_scan
: {
6954 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6955 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6956 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
6957 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
6958 nir_intrinsic_cluster_size(instr
) : 0;
6959 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: ctx
->program
->wave_size
, ctx
->program
->wave_size
));
6961 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
6962 emit_uniform_subgroup(ctx
, instr
, src
);
6963 } else if (instr
->dest
.ssa
.bit_size
== 1) {
6964 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
6966 else if (op
== nir_op_iadd
)
6968 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
6970 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
6972 switch (instr
->intrinsic
) {
6973 case nir_intrinsic_reduce
:
6974 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
6976 case nir_intrinsic_exclusive_scan
:
6977 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
6979 case nir_intrinsic_inclusive_scan
:
6980 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
6985 } else if (cluster_size
== 1) {
6986 bld
.copy(Definition(dst
), src
);
6988 src
= as_vgpr(ctx
, src
);
6992 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
7007 unreachable("unknown reduction op");
7012 switch (instr
->intrinsic
) {
7013 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
7014 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
7015 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
7017 unreachable("unknown reduce intrinsic");
7020 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
7021 reduce
->operands
[0] = Operand(src
);
7022 // filled in by aco_reduce_assign.cpp, used internally as part of the
7024 assert(dst
.size() == 1 || dst
.size() == 2);
7025 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
7026 reduce
->operands
[2] = Operand(v1
.as_linear());
7028 Temp tmp_dst
= bld
.tmp(dst
.regClass());
7029 reduce
->definitions
[0] = Definition(tmp_dst
);
7030 reduce
->definitions
[1] = bld
.def(ctx
->program
->lane_mask
); // used internally
7031 reduce
->definitions
[2] = Definition();
7032 reduce
->definitions
[3] = Definition(scc
, s1
);
7033 reduce
->definitions
[4] = Definition();
7034 reduce
->reduce_op
= reduce_op
;
7035 reduce
->cluster_size
= cluster_size
;
7036 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
7038 emit_wqm(ctx
, tmp_dst
, dst
);
7042 case nir_intrinsic_quad_broadcast
: {
7043 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7044 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
7045 emit_uniform_subgroup(ctx
, instr
, src
);
7047 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7048 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
7049 uint32_t dpp_ctrl
= dpp_quad_perm(lane
, lane
, lane
, lane
);
7051 if (instr
->dest
.ssa
.bit_size
== 1) {
7052 assert(src
.regClass() == bld
.lm
);
7053 assert(dst
.regClass() == bld
.lm
);
7054 uint32_t half_mask
= 0x11111111u
<< lane
;
7055 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
7056 Temp tmp
= bld
.tmp(bld
.lm
);
7057 bld
.sop1(Builder::s_wqm
, Definition(tmp
),
7058 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), mask_tmp
,
7059 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
))));
7060 emit_wqm(ctx
, tmp
, dst
);
7061 } else if (instr
->dest
.ssa
.bit_size
== 32) {
7062 if (ctx
->program
->chip_class
>= GFX8
)
7063 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), dst
);
7065 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), dst
);
7066 } else if (instr
->dest
.ssa
.bit_size
== 64) {
7067 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7068 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7069 if (ctx
->program
->chip_class
>= GFX8
) {
7070 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7071 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7073 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, (1 << 15) | dpp_ctrl
));
7074 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, (1 << 15) | dpp_ctrl
));
7076 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7077 emit_split_vector(ctx
, dst
, 2);
7079 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7080 nir_print_instr(&instr
->instr
, stderr
);
7081 fprintf(stderr
, "\n");
7086 case nir_intrinsic_quad_swap_horizontal
:
7087 case nir_intrinsic_quad_swap_vertical
:
7088 case nir_intrinsic_quad_swap_diagonal
:
7089 case nir_intrinsic_quad_swizzle_amd
: {
7090 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7091 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
7092 emit_uniform_subgroup(ctx
, instr
, src
);
7095 uint16_t dpp_ctrl
= 0;
7096 switch (instr
->intrinsic
) {
7097 case nir_intrinsic_quad_swap_horizontal
:
7098 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
7100 case nir_intrinsic_quad_swap_vertical
:
7101 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
7103 case nir_intrinsic_quad_swap_diagonal
:
7104 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
7106 case nir_intrinsic_quad_swizzle_amd
:
7107 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
7112 if (ctx
->program
->chip_class
< GFX8
)
7113 dpp_ctrl
|= (1 << 15);
7115 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7116 if (instr
->dest
.ssa
.bit_size
== 1) {
7117 assert(src
.regClass() == bld
.lm
);
7118 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
7119 if (ctx
->program
->chip_class
>= GFX8
)
7120 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7122 src
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7123 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), src
);
7124 emit_wqm(ctx
, tmp
, dst
);
7125 } else if (instr
->dest
.ssa
.bit_size
== 32) {
7127 if (ctx
->program
->chip_class
>= GFX8
)
7128 tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7130 tmp
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7131 emit_wqm(ctx
, tmp
, dst
);
7132 } else if (instr
->dest
.ssa
.bit_size
== 64) {
7133 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7134 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7135 if (ctx
->program
->chip_class
>= GFX8
) {
7136 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7137 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7139 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7140 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7142 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7143 emit_split_vector(ctx
, dst
, 2);
7145 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7146 nir_print_instr(&instr
->instr
, stderr
);
7147 fprintf(stderr
, "\n");
7151 case nir_intrinsic_masked_swizzle_amd
: {
7152 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7153 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
7154 emit_uniform_subgroup(ctx
, instr
, src
);
7157 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7158 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
7159 if (dst
.regClass() == v1
) {
7161 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
7163 } else if (dst
.regClass() == v2
) {
7164 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7165 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7166 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
7167 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
7168 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7169 emit_split_vector(ctx
, dst
, 2);
7171 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7172 nir_print_instr(&instr
->instr
, stderr
);
7173 fprintf(stderr
, "\n");
7177 case nir_intrinsic_write_invocation_amd
: {
7178 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
7179 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
7180 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
7181 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7182 if (dst
.regClass() == v1
) {
7183 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7184 emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val
, lane
, src
), dst
);
7185 } else if (dst
.regClass() == v2
) {
7186 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
7187 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
7188 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
7189 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
7190 Temp lo
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_lo
, lane
, src_hi
));
7191 Temp hi
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_hi
, lane
, src_hi
));
7192 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7193 emit_split_vector(ctx
, dst
, 2);
7195 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7196 nir_print_instr(&instr
->instr
, stderr
);
7197 fprintf(stderr
, "\n");
7201 case nir_intrinsic_mbcnt_amd
: {
7202 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7203 RegClass rc
= RegClass(src
.type(), 1);
7204 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
7205 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
7206 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7207 Temp wqm_tmp
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(mask_lo
), Operand(mask_hi
));
7208 emit_wqm(ctx
, wqm_tmp
, dst
);
7211 case nir_intrinsic_load_helper_invocation
: {
7212 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7213 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
7214 ctx
->block
->kind
|= block_kind_needs_lowering
;
7215 ctx
->program
->needs_exact
= true;
7218 case nir_intrinsic_is_helper_invocation
: {
7219 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7220 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
7221 ctx
->block
->kind
|= block_kind_needs_lowering
;
7222 ctx
->program
->needs_exact
= true;
7225 case nir_intrinsic_demote
:
7226 bld
.pseudo(aco_opcode::p_demote_to_helper
, Operand(-1u));
7228 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
7229 ctx
->cf_info
.exec_potentially_empty_discard
= true;
7230 ctx
->block
->kind
|= block_kind_uses_demote
;
7231 ctx
->program
->needs_exact
= true;
7233 case nir_intrinsic_demote_if
: {
7234 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7235 assert(src
.regClass() == bld
.lm
);
7236 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7237 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
7239 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
7240 ctx
->cf_info
.exec_potentially_empty_discard
= true;
7241 ctx
->block
->kind
|= block_kind_uses_demote
;
7242 ctx
->program
->needs_exact
= true;
7245 case nir_intrinsic_first_invocation
: {
7246 emit_wqm(ctx
, bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)),
7247 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
7250 case nir_intrinsic_shader_clock
:
7251 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
7252 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
7254 case nir_intrinsic_load_vertex_id_zero_base
: {
7255 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7256 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
7259 case nir_intrinsic_load_first_vertex
: {
7260 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7261 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.base_vertex
));
7264 case nir_intrinsic_load_base_instance
: {
7265 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7266 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.start_instance
));
7269 case nir_intrinsic_load_instance_id
: {
7270 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7271 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.instance_id
));
7274 case nir_intrinsic_load_draw_id
: {
7275 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7276 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.draw_id
));
7279 case nir_intrinsic_load_invocation_id
: {
7280 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7282 if (ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
7283 if (ctx
->options
->chip_class
>= GFX10
)
7284 bld
.vop2_e64(aco_opcode::v_and_b32
, Definition(dst
), Operand(127u), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
7286 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
7287 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
7288 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(dst
),
7289 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
), Operand(8u), Operand(5u));
7291 unreachable("Unsupported stage for load_invocation_id");
7296 case nir_intrinsic_load_primitive_id
: {
7297 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7299 switch (ctx
->shader
->info
.stage
) {
7300 case MESA_SHADER_GEOMETRY
:
7301 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_prim_id
));
7303 case MESA_SHADER_TESS_CTRL
:
7304 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.tcs_patch_id
));
7306 case MESA_SHADER_TESS_EVAL
:
7307 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.tes_patch_id
));
7310 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
7315 case nir_intrinsic_load_patch_vertices_in
: {
7316 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
7317 ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
7319 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7320 bld
.copy(Definition(dst
), Operand(ctx
->args
->options
->key
.tcs
.input_vertices
));
7323 case nir_intrinsic_emit_vertex_with_counter
: {
7324 visit_emit_vertex_with_counter(ctx
, instr
);
7327 case nir_intrinsic_end_primitive_with_counter
: {
7328 unsigned stream
= nir_intrinsic_stream_id(instr
);
7329 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(true, false, stream
));
7332 case nir_intrinsic_set_vertex_count
: {
7333 /* unused, the HW keeps track of this for us */
7337 fprintf(stderr
, "Unimplemented intrinsic instr: ");
7338 nir_print_instr(&instr
->instr
, stderr
);
7339 fprintf(stderr
, "\n");
7347 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
7348 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
7349 enum glsl_base_type
*stype
)
7351 nir_deref_instr
*texture_deref_instr
= NULL
;
7352 nir_deref_instr
*sampler_deref_instr
= NULL
;
7355 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
7356 switch (instr
->src
[i
].src_type
) {
7357 case nir_tex_src_texture_deref
:
7358 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
7360 case nir_tex_src_sampler_deref
:
7361 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
7363 case nir_tex_src_plane
:
7364 plane
= nir_src_as_int(instr
->src
[i
].src
);
7371 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
7373 if (!sampler_deref_instr
)
7374 sampler_deref_instr
= texture_deref_instr
;
7377 assert(instr
->op
!= nir_texop_txf_ms
&&
7378 instr
->op
!= nir_texop_samples_identical
);
7379 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
7380 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
7381 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
7382 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
7383 } else if (instr
->op
== nir_texop_fragment_mask_fetch
) {
7384 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
7386 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
7389 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
7391 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
7392 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
7393 Builder
bld(ctx
->program
, ctx
->block
);
7395 /* to avoid unnecessary moves, we split and recombine sampler and image */
7396 Temp img
[8] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
),
7397 bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
7398 Temp samp
[4] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
7399 bld
.pseudo(aco_opcode::p_split_vector
, Definition(img
[0]), Definition(img
[1]),
7400 Definition(img
[2]), Definition(img
[3]), Definition(img
[4]),
7401 Definition(img
[5]), Definition(img
[6]), Definition(img
[7]), *res_ptr
);
7402 bld
.pseudo(aco_opcode::p_split_vector
, Definition(samp
[0]), Definition(samp
[1]),
7403 Definition(samp
[2]), Definition(samp
[3]), *samp_ptr
);
7405 samp
[0] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), samp
[0], img
[7]);
7406 *res_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
7407 img
[0], img
[1], img
[2], img
[3],
7408 img
[4], img
[5], img
[6], img
[7]);
7409 *samp_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
7410 samp
[0], samp
[1], samp
[2], samp
[3]);
7413 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
7414 instr
->op
== nir_texop_samples_identical
))
7415 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
7418 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
7419 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
7421 Builder
bld(ctx
->program
, ctx
->block
);
7423 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
7424 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
7425 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
7427 Operand
neg_one(0xbf800000u
);
7428 Operand
one(0x3f800000u
);
7429 Operand
two(0x40000000u
);
7430 Operand
four(0x40800000u
);
7432 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), ma
);
7433 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
7434 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
7436 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), four
, id
);
7437 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(bld
.lm
), two
, id
);
7438 is_ma_y
= bld
.sop2(Builder::s_andn2
, bld
.hint_vcc(bld
.def(bld
.lm
)), is_ma_y
, is_ma_z
);
7439 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
);
7442 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
7443 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7444 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
7446 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
7449 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
7450 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
7451 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
7454 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7455 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
7457 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
7458 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
7461 void prepare_cube_coords(isel_context
*ctx
, std::vector
<Temp
>& coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
7463 Builder
bld(ctx
->program
, ctx
->block
);
7464 Temp ma
, tc
, sc
, id
;
7467 coords
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[3]);
7469 // see comment in ac_prepare_cube_coords()
7470 if (ctx
->options
->chip_class
<= GFX8
)
7471 coords
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coords
[3]);
7474 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
7476 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
7477 vop3a
->operands
[0] = Operand(ma
);
7478 vop3a
->abs
[0] = true;
7479 Temp invma
= bld
.tmp(v1
);
7480 vop3a
->definitions
[0] = Definition(invma
);
7481 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
7483 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
7485 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
7487 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
7489 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
7491 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
7494 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
7495 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
7497 for (unsigned i
= 0; i
< 2; i
++) {
7498 // see comment in ac_prepare_cube_coords()
7500 Temp deriv_sc
, deriv_tc
;
7501 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
7502 &deriv_ma
, &deriv_sc
, &deriv_tc
);
7504 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
7506 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
7507 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
7508 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
7509 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
7510 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
7511 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
7512 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
7515 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
7516 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
7520 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coords
[3], id
, Operand(0x41000000u
/*8.0*/));
7527 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
7529 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
7531 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
7532 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
7535 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
7536 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
7537 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
7541 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
7543 Builder
bld(ctx
->program
, ctx
->block
);
7544 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
7545 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
7546 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), compare
= Temp(), sample_index
= Temp(),
7547 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp();
7548 std::vector
<Temp
> coords
;
7549 std::vector
<Temp
> derivs
;
7550 nir_const_value
*sample_index_cv
= NULL
;
7551 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
7552 enum glsl_base_type stype
;
7553 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
7555 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
7556 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
7557 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
7558 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
7560 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
7561 switch (instr
->src
[i
].src_type
) {
7562 case nir_tex_src_coord
: {
7563 Temp coord
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7564 for (unsigned i
= 0; i
< coord
.size(); i
++)
7565 coords
.emplace_back(emit_extract_vector(ctx
, coord
, i
, v1
));
7568 case nir_tex_src_bias
:
7569 if (instr
->op
== nir_texop_txb
) {
7570 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7574 case nir_tex_src_lod
: {
7575 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
7577 if (val
&& val
->f32
<= 0.0) {
7580 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7585 case nir_tex_src_comparator
:
7586 if (instr
->is_shadow
) {
7587 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7591 case nir_tex_src_offset
:
7592 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7593 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
7596 case nir_tex_src_ddx
:
7597 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7600 case nir_tex_src_ddy
:
7601 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7604 case nir_tex_src_ms_index
:
7605 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
7606 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
7607 has_sample_index
= true;
7609 case nir_tex_src_texture_offset
:
7610 case nir_tex_src_sampler_offset
:
7616 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
7617 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
7619 if (instr
->op
== nir_texop_texture_samples
) {
7620 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
7622 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
7623 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
7624 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 */));
7625 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
7627 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7628 samples
, Operand(1u), bld
.scc(is_msaa
));
7632 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
7633 aco_ptr
<Instruction
> tmp_instr
;
7634 Temp acc
, pack
= Temp();
7636 uint32_t pack_const
= 0;
7637 for (unsigned i
= 0; i
< offset
.size(); i
++) {
7638 if (!const_offset
[i
])
7640 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
7643 if (offset
.type() == RegType::sgpr
) {
7644 for (unsigned i
= 0; i
< offset
.size(); i
++) {
7645 if (const_offset
[i
])
7648 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
7649 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
7652 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
7655 if (pack
== Temp()) {
7658 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
7662 if (pack_const
&& pack
!= Temp())
7663 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
7665 for (unsigned i
= 0; i
< offset
.size(); i
++) {
7666 if (const_offset
[i
])
7669 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
7670 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
7673 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
7676 if (pack
== Temp()) {
7679 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
7683 if (pack_const
&& pack
!= Temp())
7684 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
7686 if (pack_const
&& pack
== Temp())
7687 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
7688 else if (pack
== Temp())
7694 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
7695 prepare_cube_coords(ctx
, coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
7697 /* pack derivatives */
7698 if (has_ddx
|| has_ddy
) {
7699 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
7700 assert(has_ddx
&& has_ddy
&& ddx
.size() == 1 && ddy
.size() == 1);
7701 Temp zero
= bld
.copy(bld
.def(v1
), Operand(0u));
7702 derivs
= {ddy
, zero
, ddy
, zero
};
7704 for (unsigned i
= 0; has_ddx
&& i
< ddx
.size(); i
++)
7705 derivs
.emplace_back(emit_extract_vector(ctx
, ddx
, i
, v1
));
7706 for (unsigned i
= 0; has_ddy
&& i
< ddy
.size(); i
++)
7707 derivs
.emplace_back(emit_extract_vector(ctx
, ddy
, i
, v1
));
7712 if (instr
->coord_components
> 1 &&
7713 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
7715 instr
->op
!= nir_texop_txf
)
7716 coords
[1] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[1]);
7718 if (instr
->coord_components
> 2 &&
7719 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
7720 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
7721 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
7722 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
7724 instr
->op
!= nir_texop_txf
&&
7725 instr
->op
!= nir_texop_txf_ms
&&
7726 instr
->op
!= nir_texop_fragment_fetch
&&
7727 instr
->op
!= nir_texop_fragment_mask_fetch
)
7728 coords
[2] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[2]);
7730 if (ctx
->options
->chip_class
== GFX9
&&
7731 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
7732 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
7733 assert(coords
.size() > 0 && coords
.size() < 3);
7735 coords
.insert(std::next(coords
.begin()), bld
.copy(bld
.def(v1
), instr
->op
== nir_texop_txf
?
7736 Operand((uint32_t) 0) :
7737 Operand((uint32_t) 0x3f000000)));
7740 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
7742 if (instr
->op
== nir_texop_samples_identical
)
7743 resource
= fmask_ptr
;
7745 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
7746 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
7747 instr
->op
!= nir_texop_txs
&&
7748 instr
->op
!= nir_texop_fragment_fetch
&&
7749 instr
->op
!= nir_texop_fragment_mask_fetch
) {
7750 assert(has_sample_index
);
7751 Operand
op(sample_index
);
7752 if (sample_index_cv
)
7753 op
= Operand(sample_index_cv
->u32
);
7754 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
7757 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
7758 for (unsigned i
= 0; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
7759 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
7760 coords
[i
] = bld
.vadd32(bld
.def(v1
), coords
[i
], off
);
7765 /* Build tex instruction */
7766 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
7767 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
7768 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
7770 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7773 /* gather4 selects the component by dmask and always returns vec4 */
7774 if (instr
->op
== nir_texop_tg4
) {
7775 assert(instr
->dest
.ssa
.num_components
== 4);
7776 if (instr
->is_shadow
)
7779 dmask
= 1 << instr
->component
;
7780 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
7781 tmp_dst
= bld
.tmp(v4
);
7782 } else if (instr
->op
== nir_texop_samples_identical
) {
7783 tmp_dst
= bld
.tmp(v1
);
7784 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
7785 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
7788 aco_ptr
<MIMG_instruction
> tex
;
7789 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
7791 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
7793 bool div_by_6
= instr
->op
== nir_texop_txs
&&
7794 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
7797 if (tmp_dst
.id() == dst
.id() && div_by_6
)
7798 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
7800 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
7801 tex
->operands
[0] = Operand(resource
);
7802 tex
->operands
[1] = Operand(s4
); /* no sampler */
7803 tex
->operands
[2] = Operand(as_vgpr(ctx
,lod
));
7804 if (ctx
->options
->chip_class
== GFX9
&&
7805 instr
->op
== nir_texop_txs
&&
7806 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
7808 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
7809 } else if (instr
->op
== nir_texop_query_levels
) {
7810 tex
->dmask
= 1 << 3;
7815 tex
->definitions
[0] = Definition(tmp_dst
);
7817 tex
->can_reorder
= true;
7818 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7821 /* divide 3rd value by 6 by multiplying with magic number */
7822 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
7823 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
7824 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
7825 assert(instr
->dest
.ssa
.num_components
== 3);
7826 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
7827 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
7828 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
7829 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
7834 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
7838 Temp tg4_compare_cube_wa64
= Temp();
7840 if (tg4_integer_workarounds
) {
7841 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
7842 tex
->operands
[0] = Operand(resource
);
7843 tex
->operands
[1] = Operand(s4
); /* no sampler */
7844 tex
->operands
[2] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
7848 Temp size
= bld
.tmp(v2
);
7849 tex
->definitions
[0] = Definition(size
);
7850 tex
->can_reorder
= true;
7851 ctx
->block
->instructions
.emplace_back(std::move(tex
));
7852 emit_split_vector(ctx
, size
, size
.size());
7855 for (unsigned i
= 0; i
< 2; i
++) {
7856 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
7857 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
7858 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
7859 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
7862 Temp new_coords
[2] = {
7863 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), coords
[0], half_texel
[0]),
7864 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), coords
[1], half_texel
[1])
7867 if (tg4_integer_cube_workaround
) {
7868 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
7869 Temp desc
[resource
.size()];
7870 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
7871 Format::PSEUDO
, 1, resource
.size())};
7872 split
->operands
[0] = Operand(resource
);
7873 for (unsigned i
= 0; i
< resource
.size(); i
++) {
7874 desc
[i
] = bld
.tmp(s1
);
7875 split
->definitions
[i
] = Definition(desc
[i
]);
7877 ctx
->block
->instructions
.emplace_back(std::move(split
));
7879 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
7880 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
7881 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
7884 if (stype
== GLSL_TYPE_UINT
) {
7885 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
7886 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
7887 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
7888 bld
.scc(compare_cube_wa
));
7890 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
7891 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
7892 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
7893 bld
.scc(compare_cube_wa
));
7895 tg4_compare_cube_wa64
= bld
.tmp(bld
.lm
);
7896 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
7898 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
7900 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
7901 Operand((uint32_t)C_008F14_NUM_FORMAT
));
7902 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
7904 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
7905 Format::PSEUDO
, resource
.size(), 1)};
7906 for (unsigned i
= 0; i
< resource
.size(); i
++)
7907 vec
->operands
[i
] = Operand(desc
[i
]);
7908 resource
= bld
.tmp(resource
.regClass());
7909 vec
->definitions
[0] = Definition(resource
);
7910 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7912 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7913 new_coords
[0], coords
[0], tg4_compare_cube_wa64
);
7914 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7915 new_coords
[1], coords
[1], tg4_compare_cube_wa64
);
7917 coords
[0] = new_coords
[0];
7918 coords
[1] = new_coords
[1];
7921 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
7922 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
7924 assert(coords
.size() == 1);
7925 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
7929 op
= aco_opcode::buffer_load_format_x
; break;
7931 op
= aco_opcode::buffer_load_format_xy
; break;
7933 op
= aco_opcode::buffer_load_format_xyz
; break;
7935 op
= aco_opcode::buffer_load_format_xyzw
; break;
7937 unreachable("Tex instruction loads more than 4 components.");
7940 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
7941 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
7944 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
7946 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
7947 mubuf
->operands
[0] = Operand(resource
);
7948 mubuf
->operands
[1] = Operand(coords
[0]);
7949 mubuf
->operands
[2] = Operand((uint32_t) 0);
7950 mubuf
->definitions
[0] = Definition(tmp_dst
);
7951 mubuf
->idxen
= true;
7952 mubuf
->can_reorder
= true;
7953 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
7955 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
7959 /* gather MIMG address components */
7960 std::vector
<Temp
> args
;
7962 args
.emplace_back(offset
);
7964 args
.emplace_back(bias
);
7966 args
.emplace_back(compare
);
7968 args
.insert(args
.end(), derivs
.begin(), derivs
.end());
7970 args
.insert(args
.end(), coords
.begin(), coords
.end());
7971 if (has_sample_index
)
7972 args
.emplace_back(sample_index
);
7974 args
.emplace_back(lod
);
7976 Temp arg
= bld
.tmp(RegClass(RegType::vgpr
, args
.size()));
7977 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
7978 vec
->definitions
[0] = Definition(arg
);
7979 for (unsigned i
= 0; i
< args
.size(); i
++)
7980 vec
->operands
[i
] = Operand(args
[i
]);
7981 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7984 if (instr
->op
== nir_texop_txf
||
7985 instr
->op
== nir_texop_txf_ms
||
7986 instr
->op
== nir_texop_samples_identical
||
7987 instr
->op
== nir_texop_fragment_fetch
||
7988 instr
->op
== nir_texop_fragment_mask_fetch
) {
7989 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
;
7990 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 3, 1));
7991 tex
->operands
[0] = Operand(resource
);
7992 tex
->operands
[1] = Operand(s4
); /* no sampler */
7993 tex
->operands
[2] = Operand(arg
);
7998 tex
->definitions
[0] = Definition(tmp_dst
);
7999 tex
->can_reorder
= true;
8000 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8002 if (instr
->op
== nir_texop_samples_identical
) {
8003 assert(dmask
== 1 && dst
.regClass() == v1
);
8004 assert(dst
.id() != tmp_dst
.id());
8006 Temp tmp
= bld
.tmp(bld
.lm
);
8007 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
8008 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
8011 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
8016 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8017 aco_opcode opcode
= aco_opcode::image_sample
;
8018 if (has_offset
) { /* image_sample_*_o */
8020 opcode
= aco_opcode::image_sample_c_o
;
8022 opcode
= aco_opcode::image_sample_c_d_o
;
8024 opcode
= aco_opcode::image_sample_c_b_o
;
8026 opcode
= aco_opcode::image_sample_c_lz_o
;
8028 opcode
= aco_opcode::image_sample_c_l_o
;
8030 opcode
= aco_opcode::image_sample_o
;
8032 opcode
= aco_opcode::image_sample_d_o
;
8034 opcode
= aco_opcode::image_sample_b_o
;
8036 opcode
= aco_opcode::image_sample_lz_o
;
8038 opcode
= aco_opcode::image_sample_l_o
;
8040 } else { /* no offset */
8042 opcode
= aco_opcode::image_sample_c
;
8044 opcode
= aco_opcode::image_sample_c_d
;
8046 opcode
= aco_opcode::image_sample_c_b
;
8048 opcode
= aco_opcode::image_sample_c_lz
;
8050 opcode
= aco_opcode::image_sample_c_l
;
8052 opcode
= aco_opcode::image_sample
;
8054 opcode
= aco_opcode::image_sample_d
;
8056 opcode
= aco_opcode::image_sample_b
;
8058 opcode
= aco_opcode::image_sample_lz
;
8060 opcode
= aco_opcode::image_sample_l
;
8064 if (instr
->op
== nir_texop_tg4
) {
8066 opcode
= aco_opcode::image_gather4_lz_o
;
8068 opcode
= aco_opcode::image_gather4_c_lz_o
;
8070 opcode
= aco_opcode::image_gather4_lz
;
8072 opcode
= aco_opcode::image_gather4_c_lz
;
8074 } else if (instr
->op
== nir_texop_lod
) {
8075 opcode
= aco_opcode::image_get_lod
;
8078 /* we don't need the bias, sample index, compare value or offset to be
8079 * computed in WQM but if the p_create_vector copies the coordinates, then it
8080 * needs to be in WQM */
8081 if (ctx
->stage
== fragment_fs
&&
8082 !has_derivs
&& !has_lod
&& !level_zero
&&
8083 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
8084 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
8085 arg
= emit_wqm(ctx
, arg
, bld
.tmp(arg
.regClass()), true);
8087 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
8088 tex
->operands
[0] = Operand(resource
);
8089 tex
->operands
[1] = Operand(sampler
);
8090 tex
->operands
[2] = Operand(arg
);
8094 tex
->definitions
[0] = Definition(tmp_dst
);
8095 tex
->can_reorder
= true;
8096 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8098 if (tg4_integer_cube_workaround
) {
8099 assert(tmp_dst
.id() != dst
.id());
8100 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
8102 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
8104 for (unsigned i
= 0; i
< dst
.size(); i
++) {
8105 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
8107 if (stype
== GLSL_TYPE_UINT
)
8108 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
8110 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
8111 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
8113 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
8114 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
8115 val
[0], val
[1], val
[2], val
[3]);
8117 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
8118 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
8123 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
8125 Temp tmp
= get_ssa_temp(ctx
, ssa
);
8126 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
8127 return Operand(tmp
.regClass());
8129 return Operand(tmp
);
8132 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
8134 aco_ptr
<Pseudo_instruction
> phi
;
8135 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8136 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == ctx
->program
->lane_mask
);
8138 bool logical
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
];
8139 logical
|= ctx
->block
->kind
& block_kind_merge
;
8140 aco_opcode opcode
= logical
? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
8142 /* we want a sorted list of sources, since the predecessor list is also sorted */
8143 std::map
<unsigned, nir_ssa_def
*> phi_src
;
8144 nir_foreach_phi_src(src
, instr
)
8145 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
8147 std::vector
<unsigned>& preds
= logical
? ctx
->block
->logical_preds
: ctx
->block
->linear_preds
;
8148 unsigned num_operands
= 0;
8149 Operand operands
[std::max(exec_list_length(&instr
->srcs
), (unsigned)preds
.size())];
8150 unsigned num_defined
= 0;
8151 unsigned cur_pred_idx
= 0;
8152 for (std::pair
<unsigned, nir_ssa_def
*> src
: phi_src
) {
8153 if (cur_pred_idx
< preds
.size()) {
8154 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8155 unsigned block
= ctx
->cf_info
.nir_to_aco
[src
.first
];
8156 unsigned skipped
= 0;
8157 while (cur_pred_idx
+ skipped
< preds
.size() && preds
[cur_pred_idx
+ skipped
] != block
)
8159 if (cur_pred_idx
+ skipped
< preds
.size()) {
8160 for (unsigned i
= 0; i
< skipped
; i
++)
8161 operands
[num_operands
++] = Operand(dst
.regClass());
8162 cur_pred_idx
+= skipped
;
8168 Operand op
= get_phi_operand(ctx
, src
.second
);
8169 operands
[num_operands
++] = op
;
8170 num_defined
+= !op
.isUndefined();
8172 /* handle block_kind_continue_or_break at loop exit blocks */
8173 while (cur_pred_idx
++ < preds
.size())
8174 operands
[num_operands
++] = Operand(dst
.regClass());
8176 if (num_defined
== 0) {
8177 Builder
bld(ctx
->program
, ctx
->block
);
8178 if (dst
.regClass() == s1
) {
8179 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
8180 } else if (dst
.regClass() == v1
) {
8181 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
8183 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
8184 for (unsigned i
= 0; i
< dst
.size(); i
++)
8185 vec
->operands
[i
] = Operand(0u);
8186 vec
->definitions
[0] = Definition(dst
);
8187 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8192 /* we can use a linear phi in some cases if one src is undef */
8193 if (dst
.is_linear() && ctx
->block
->kind
& block_kind_merge
&& num_defined
== 1) {
8194 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, num_operands
, 1));
8196 Block
*linear_else
= &ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
8197 Block
*invert
= &ctx
->program
->blocks
[linear_else
->linear_preds
[0]];
8198 assert(invert
->kind
& block_kind_invert
);
8200 unsigned then_block
= invert
->linear_preds
[0];
8202 Block
* insert_block
= NULL
;
8203 for (unsigned i
= 0; i
< num_operands
; i
++) {
8204 Operand op
= operands
[i
];
8205 if (op
.isUndefined())
8207 insert_block
= ctx
->block
->logical_preds
[i
] == then_block
? invert
: ctx
->block
;
8208 phi
->operands
[0] = op
;
8211 assert(insert_block
); /* should be handled by the "num_defined == 0" case above */
8212 phi
->operands
[1] = Operand(dst
.regClass());
8213 phi
->definitions
[0] = Definition(dst
);
8214 insert_block
->instructions
.emplace(insert_block
->instructions
.begin(), std::move(phi
));
8218 /* try to scalarize vector phis */
8219 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
8220 // TODO: scalarize linear phis on divergent ifs
8221 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
8222 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> new_vec
;
8223 for (unsigned i
= 0; can_scalarize
&& (i
< num_operands
); i
++) {
8224 Operand src
= operands
[i
];
8225 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end())
8226 can_scalarize
= false;
8228 if (can_scalarize
) {
8229 unsigned num_components
= instr
->dest
.ssa
.num_components
;
8230 assert(dst
.size() % num_components
== 0);
8231 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
8233 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
8234 for (unsigned k
= 0; k
< num_components
; k
++) {
8235 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
8236 for (unsigned i
= 0; i
< num_operands
; i
++) {
8237 Operand src
= operands
[i
];
8238 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
8240 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
8241 phi
->definitions
[0] = Definition(phi_dst
);
8242 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
8243 new_vec
[k
] = phi_dst
;
8244 vec
->operands
[k
] = Operand(phi_dst
);
8246 vec
->definitions
[0] = Definition(dst
);
8247 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8248 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
8253 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
8254 for (unsigned i
= 0; i
< num_operands
; i
++)
8255 phi
->operands
[i
] = operands
[i
];
8256 phi
->definitions
[0] = Definition(dst
);
8257 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
8261 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
8263 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
8265 assert(dst
.type() == RegType::sgpr
);
8267 if (dst
.size() == 1) {
8268 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
8270 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
8271 for (unsigned i
= 0; i
< dst
.size(); i
++)
8272 vec
->operands
[i
] = Operand(0u);
8273 vec
->definitions
[0] = Definition(dst
);
8274 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8278 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
8280 Builder
bld(ctx
->program
, ctx
->block
);
8281 Block
*logical_target
;
8282 append_logical_end(ctx
->block
);
8283 unsigned idx
= ctx
->block
->index
;
8285 switch (instr
->type
) {
8286 case nir_jump_break
:
8287 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
8288 add_logical_edge(idx
, logical_target
);
8289 ctx
->block
->kind
|= block_kind_break
;
8291 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
8292 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
8293 /* uniform break - directly jump out of the loop */
8294 ctx
->block
->kind
|= block_kind_uniform
;
8295 ctx
->cf_info
.has_branch
= true;
8296 bld
.branch(aco_opcode::p_branch
);
8297 add_linear_edge(idx
, logical_target
);
8300 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
8301 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
8303 case nir_jump_continue
:
8304 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
8305 add_logical_edge(idx
, logical_target
);
8306 ctx
->block
->kind
|= block_kind_continue
;
8308 if (ctx
->cf_info
.parent_if
.is_divergent
) {
8309 /* for potential uniform breaks after this continue,
8310 we must ensure that they are handled correctly */
8311 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
8312 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
8313 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
8315 /* uniform continue - directly jump to the loop header */
8316 ctx
->block
->kind
|= block_kind_uniform
;
8317 ctx
->cf_info
.has_branch
= true;
8318 bld
.branch(aco_opcode::p_branch
);
8319 add_linear_edge(idx
, logical_target
);
8324 fprintf(stderr
, "Unknown NIR jump instr: ");
8325 nir_print_instr(&instr
->instr
, stderr
);
8326 fprintf(stderr
, "\n");
8330 if (ctx
->cf_info
.parent_if
.is_divergent
&& !ctx
->cf_info
.exec_potentially_empty_break
) {
8331 ctx
->cf_info
.exec_potentially_empty_break
= true;
8332 ctx
->cf_info
.exec_potentially_empty_break_depth
= ctx
->cf_info
.loop_nest_depth
;
8335 /* remove critical edges from linear CFG */
8336 bld
.branch(aco_opcode::p_branch
);
8337 Block
* break_block
= ctx
->program
->create_and_insert_block();
8338 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8339 break_block
->kind
|= block_kind_uniform
;
8340 add_linear_edge(idx
, break_block
);
8341 /* the loop_header pointer might be invalidated by this point */
8342 if (instr
->type
== nir_jump_continue
)
8343 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
8344 add_linear_edge(break_block
->index
, logical_target
);
8345 bld
.reset(break_block
);
8346 bld
.branch(aco_opcode::p_branch
);
8348 Block
* continue_block
= ctx
->program
->create_and_insert_block();
8349 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8350 add_linear_edge(idx
, continue_block
);
8351 append_logical_start(continue_block
);
8352 ctx
->block
= continue_block
;
8356 void visit_block(isel_context
*ctx
, nir_block
*block
)
8358 nir_foreach_instr(instr
, block
) {
8359 switch (instr
->type
) {
8360 case nir_instr_type_alu
:
8361 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
8363 case nir_instr_type_load_const
:
8364 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
8366 case nir_instr_type_intrinsic
:
8367 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
8369 case nir_instr_type_tex
:
8370 visit_tex(ctx
, nir_instr_as_tex(instr
));
8372 case nir_instr_type_phi
:
8373 visit_phi(ctx
, nir_instr_as_phi(instr
));
8375 case nir_instr_type_ssa_undef
:
8376 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
8378 case nir_instr_type_deref
:
8380 case nir_instr_type_jump
:
8381 visit_jump(ctx
, nir_instr_as_jump(instr
));
8384 fprintf(stderr
, "Unknown NIR instr type: ");
8385 nir_print_instr(instr
, stderr
);
8386 fprintf(stderr
, "\n");
8391 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8392 ctx
->cf_info
.nir_to_aco
[block
->index
] = ctx
->block
->index
;
8397 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
8399 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
8400 append_logical_end(ctx
->block
);
8401 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
8402 Builder
bld(ctx
->program
, ctx
->block
);
8403 bld
.branch(aco_opcode::p_branch
);
8404 unsigned loop_preheader_idx
= ctx
->block
->index
;
8406 Block loop_exit
= Block();
8407 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8408 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
8410 Block
* loop_header
= ctx
->program
->create_and_insert_block();
8411 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
8412 loop_header
->kind
|= block_kind_loop_header
;
8413 add_edge(loop_preheader_idx
, loop_header
);
8414 ctx
->block
= loop_header
;
8416 /* emit loop body */
8417 unsigned loop_header_idx
= loop_header
->index
;
8418 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
8419 append_logical_start(ctx
->block
);
8420 visit_cf_list(ctx
, &loop
->body
);
8422 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
8423 if (!ctx
->cf_info
.has_branch
) {
8424 append_logical_end(ctx
->block
);
8425 if (ctx
->cf_info
.exec_potentially_empty_discard
|| ctx
->cf_info
.exec_potentially_empty_break
) {
8426 /* Discards can result in code running with an empty exec mask.
8427 * This would result in divergent breaks not ever being taken. As a
8428 * workaround, break the loop when the loop mask is empty instead of
8429 * always continuing. */
8430 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
8431 unsigned block_idx
= ctx
->block
->index
;
8433 /* create helper blocks to avoid critical edges */
8434 Block
*break_block
= ctx
->program
->create_and_insert_block();
8435 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8436 break_block
->kind
= block_kind_uniform
;
8437 bld
.reset(break_block
);
8438 bld
.branch(aco_opcode::p_branch
);
8439 add_linear_edge(block_idx
, break_block
);
8440 add_linear_edge(break_block
->index
, &loop_exit
);
8442 Block
*continue_block
= ctx
->program
->create_and_insert_block();
8443 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8444 continue_block
->kind
= block_kind_uniform
;
8445 bld
.reset(continue_block
);
8446 bld
.branch(aco_opcode::p_branch
);
8447 add_linear_edge(block_idx
, continue_block
);
8448 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
8450 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8451 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
8452 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
8454 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
8455 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8456 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
8458 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
8461 bld
.reset(ctx
->block
);
8462 bld
.branch(aco_opcode::p_branch
);
8465 /* fixup phis in loop header from unreachable blocks */
8466 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
8467 bool linear
= ctx
->cf_info
.has_branch
;
8468 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
8469 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
8470 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
8471 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
8472 /* the last operand should be the one that needs to be removed */
8473 instr
->operands
.pop_back();
8474 } else if (!is_phi(instr
)) {
8480 ctx
->cf_info
.has_branch
= false;
8482 // TODO: if the loop has not a single exit, we must add one °°
8483 /* emit loop successor block */
8484 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
8485 append_logical_start(ctx
->block
);
8488 // TODO: check if it is beneficial to not branch on continues
8489 /* trim linear phis in loop header */
8490 for (auto&& instr
: loop_entry
->instructions
) {
8491 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
8492 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
8493 new_phi
->definitions
[0] = instr
->definitions
[0];
8494 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
8495 new_phi
->operands
[i
] = instr
->operands
[i
];
8496 /* check that the remaining operands are all the same */
8497 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
8498 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
8499 instr
.swap(new_phi
);
8500 } else if (instr
->opcode
== aco_opcode::p_phi
) {
8509 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
8513 append_logical_end(ctx
->block
);
8514 ctx
->block
->kind
|= block_kind_branch
;
8516 /* branch to linear then block */
8517 assert(cond
.regClass() == ctx
->program
->lane_mask
);
8518 aco_ptr
<Pseudo_branch_instruction
> branch
;
8519 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
8520 branch
->operands
[0] = Operand(cond
);
8521 ctx
->block
->instructions
.push_back(std::move(branch
));
8523 ic
->BB_if_idx
= ctx
->block
->index
;
8524 ic
->BB_invert
= Block();
8525 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8526 /* Invert blocks are intentionally not marked as top level because they
8527 * are not part of the logical cfg. */
8528 ic
->BB_invert
.kind
|= block_kind_invert
;
8529 ic
->BB_endif
= Block();
8530 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8531 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
8533 ic
->exec_potentially_empty_discard_old
= ctx
->cf_info
.exec_potentially_empty_discard
;
8534 ic
->exec_potentially_empty_break_old
= ctx
->cf_info
.exec_potentially_empty_break
;
8535 ic
->exec_potentially_empty_break_depth_old
= ctx
->cf_info
.exec_potentially_empty_break_depth
;
8536 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
8537 ctx
->cf_info
.parent_if
.is_divergent
= true;
8539 /* divergent branches use cbranch_execz */
8540 ctx
->cf_info
.exec_potentially_empty_discard
= false;
8541 ctx
->cf_info
.exec_potentially_empty_break
= false;
8542 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8544 /** emit logical then block */
8545 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
8546 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8547 add_edge(ic
->BB_if_idx
, BB_then_logical
);
8548 ctx
->block
= BB_then_logical
;
8549 append_logical_start(BB_then_logical
);
8552 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
8554 Block
*BB_then_logical
= ctx
->block
;
8555 append_logical_end(BB_then_logical
);
8556 /* branch from logical then block to invert block */
8557 aco_ptr
<Pseudo_branch_instruction
> branch
;
8558 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8559 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
8560 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
8561 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8562 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
8563 BB_then_logical
->kind
|= block_kind_uniform
;
8564 assert(!ctx
->cf_info
.has_branch
);
8565 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
8566 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
8568 /** emit linear then block */
8569 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
8570 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8571 BB_then_linear
->kind
|= block_kind_uniform
;
8572 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
8573 /* branch from linear then block to invert block */
8574 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8575 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
8576 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
8578 /** emit invert merge block */
8579 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
8580 ic
->invert_idx
= ctx
->block
->index
;
8582 /* branch to linear else block (skip else) */
8583 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
8584 branch
->operands
[0] = Operand(ic
->cond
);
8585 ctx
->block
->instructions
.push_back(std::move(branch
));
8587 ic
->exec_potentially_empty_discard_old
|= ctx
->cf_info
.exec_potentially_empty_discard
;
8588 ic
->exec_potentially_empty_break_old
|= ctx
->cf_info
.exec_potentially_empty_break
;
8589 ic
->exec_potentially_empty_break_depth_old
=
8590 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
8591 /* divergent branches use cbranch_execz */
8592 ctx
->cf_info
.exec_potentially_empty_discard
= false;
8593 ctx
->cf_info
.exec_potentially_empty_break
= false;
8594 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8596 /** emit logical else block */
8597 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
8598 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8599 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
8600 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
8601 ctx
->block
= BB_else_logical
;
8602 append_logical_start(BB_else_logical
);
8605 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
8607 Block
*BB_else_logical
= ctx
->block
;
8608 append_logical_end(BB_else_logical
);
8610 /* branch from logical else block to endif block */
8611 aco_ptr
<Pseudo_branch_instruction
> branch
;
8612 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8613 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
8614 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
8615 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8616 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
8617 BB_else_logical
->kind
|= block_kind_uniform
;
8619 assert(!ctx
->cf_info
.has_branch
);
8620 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
8623 /** emit linear else block */
8624 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
8625 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8626 BB_else_linear
->kind
|= block_kind_uniform
;
8627 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
8629 /* branch from linear else block to endif block */
8630 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8631 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
8632 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
8635 /** emit endif merge block */
8636 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
8637 append_logical_start(ctx
->block
);
8640 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
8641 ctx
->cf_info
.exec_potentially_empty_discard
|= ic
->exec_potentially_empty_discard_old
;
8642 ctx
->cf_info
.exec_potentially_empty_break
|= ic
->exec_potentially_empty_break_old
;
8643 ctx
->cf_info
.exec_potentially_empty_break_depth
=
8644 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
8645 if (ctx
->cf_info
.loop_nest_depth
== ctx
->cf_info
.exec_potentially_empty_break_depth
&&
8646 !ctx
->cf_info
.parent_if
.is_divergent
) {
8647 ctx
->cf_info
.exec_potentially_empty_break
= false;
8648 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8650 /* uniform control flow never has an empty exec-mask */
8651 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
) {
8652 ctx
->cf_info
.exec_potentially_empty_discard
= false;
8653 ctx
->cf_info
.exec_potentially_empty_break
= false;
8654 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
8658 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
8660 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
8661 Builder
bld(ctx
->program
, ctx
->block
);
8662 aco_ptr
<Pseudo_branch_instruction
> branch
;
8664 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
8666 * Uniform conditionals are represented in the following way*) :
8668 * The linear and logical CFG:
8671 * BB_THEN (logical) BB_ELSE (logical)
8675 * *) Exceptions may be due to break and continue statements within loops
8676 * If a break/continue happens within uniform control flow, it branches
8677 * to the loop exit/entry block. Otherwise, it branches to the next
8680 append_logical_end(ctx
->block
);
8681 ctx
->block
->kind
|= block_kind_uniform
;
8684 assert(cond
.regClass() == bld
.lm
);
8685 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
8686 cond
= bool_to_scalar_condition(ctx
, cond
);
8688 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
8689 branch
->operands
[0] = Operand(cond
);
8690 branch
->operands
[0].setFixed(scc
);
8691 ctx
->block
->instructions
.emplace_back(std::move(branch
));
8693 unsigned BB_if_idx
= ctx
->block
->index
;
8694 Block BB_endif
= Block();
8695 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8696 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
8698 /** emit then block */
8699 Block
* BB_then
= ctx
->program
->create_and_insert_block();
8700 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8701 add_edge(BB_if_idx
, BB_then
);
8702 append_logical_start(BB_then
);
8703 ctx
->block
= BB_then
;
8704 visit_cf_list(ctx
, &if_stmt
->then_list
);
8705 BB_then
= ctx
->block
;
8706 bool then_branch
= ctx
->cf_info
.has_branch
;
8707 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
8710 append_logical_end(BB_then
);
8711 /* branch from then block to endif block */
8712 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8713 BB_then
->instructions
.emplace_back(std::move(branch
));
8714 add_linear_edge(BB_then
->index
, &BB_endif
);
8715 if (!then_branch_divergent
)
8716 add_logical_edge(BB_then
->index
, &BB_endif
);
8717 BB_then
->kind
|= block_kind_uniform
;
8720 ctx
->cf_info
.has_branch
= false;
8721 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
8723 /** emit else block */
8724 Block
* BB_else
= ctx
->program
->create_and_insert_block();
8725 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
8726 add_edge(BB_if_idx
, BB_else
);
8727 append_logical_start(BB_else
);
8728 ctx
->block
= BB_else
;
8729 visit_cf_list(ctx
, &if_stmt
->else_list
);
8730 BB_else
= ctx
->block
;
8732 if (!ctx
->cf_info
.has_branch
) {
8733 append_logical_end(BB_else
);
8734 /* branch from then block to endif block */
8735 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
8736 BB_else
->instructions
.emplace_back(std::move(branch
));
8737 add_linear_edge(BB_else
->index
, &BB_endif
);
8738 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
8739 add_logical_edge(BB_else
->index
, &BB_endif
);
8740 BB_else
->kind
|= block_kind_uniform
;
8743 ctx
->cf_info
.has_branch
&= then_branch
;
8744 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
8746 /** emit endif merge block */
8747 if (!ctx
->cf_info
.has_branch
) {
8748 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
8749 append_logical_start(ctx
->block
);
8751 } else { /* non-uniform condition */
8753 * To maintain a logical and linear CFG without critical edges,
8754 * non-uniform conditionals are represented in the following way*) :
8759 * BB_THEN (logical) BB_THEN (linear)
8761 * BB_INVERT (linear)
8763 * BB_ELSE (logical) BB_ELSE (linear)
8770 * BB_THEN (logical) BB_ELSE (logical)
8774 * *) Exceptions may be due to break and continue statements within loops
8779 begin_divergent_if_then(ctx
, &ic
, cond
);
8780 visit_cf_list(ctx
, &if_stmt
->then_list
);
8782 begin_divergent_if_else(ctx
, &ic
);
8783 visit_cf_list(ctx
, &if_stmt
->else_list
);
8785 end_divergent_if(ctx
, &ic
);
8789 static void visit_cf_list(isel_context
*ctx
,
8790 struct exec_list
*list
)
8792 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
8793 switch (node
->type
) {
8794 case nir_cf_node_block
:
8795 visit_block(ctx
, nir_cf_node_as_block(node
));
8797 case nir_cf_node_if
:
8798 visit_if(ctx
, nir_cf_node_as_if(node
));
8800 case nir_cf_node_loop
:
8801 visit_loop(ctx
, nir_cf_node_as_loop(node
));
8804 unreachable("unimplemented cf list type");
8809 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
8811 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
8812 uint64_t mask
= ctx
->outputs
.mask
[slot
];
8813 if (!is_pos
&& !mask
)
8815 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
8817 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
8818 exp
->enabled_mask
= mask
;
8819 for (unsigned i
= 0; i
< 4; ++i
) {
8820 if (mask
& (1 << i
))
8821 exp
->operands
[i
] = Operand(ctx
->outputs
.outputs
[slot
][i
]);
8823 exp
->operands
[i
] = Operand(v1
);
8825 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
8826 * Setting valid_mask=1 prevents it and has no other effect.
8828 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& is_pos
&& *next_pos
== 0;
8830 exp
->compressed
= false;
8832 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
8834 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
8835 ctx
->block
->instructions
.emplace_back(std::move(exp
));
8838 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
8840 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
8841 exp
->enabled_mask
= 0;
8842 for (unsigned i
= 0; i
< 4; ++i
)
8843 exp
->operands
[i
] = Operand(v1
);
8844 if (ctx
->outputs
.mask
[VARYING_SLOT_PSIZ
]) {
8845 exp
->operands
[0] = Operand(ctx
->outputs
.outputs
[VARYING_SLOT_PSIZ
][0]);
8846 exp
->enabled_mask
|= 0x1;
8848 if (ctx
->outputs
.mask
[VARYING_SLOT_LAYER
]) {
8849 exp
->operands
[2] = Operand(ctx
->outputs
.outputs
[VARYING_SLOT_LAYER
][0]);
8850 exp
->enabled_mask
|= 0x4;
8852 if (ctx
->outputs
.mask
[VARYING_SLOT_VIEWPORT
]) {
8853 if (ctx
->options
->chip_class
< GFX9
) {
8854 exp
->operands
[3] = Operand(ctx
->outputs
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
8855 exp
->enabled_mask
|= 0x8;
8857 Builder
bld(ctx
->program
, ctx
->block
);
8859 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
8860 Operand(ctx
->outputs
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
8861 if (exp
->operands
[2].isTemp())
8862 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
8864 exp
->operands
[2] = Operand(out
);
8865 exp
->enabled_mask
|= 0x4;
8868 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& *next_pos
== 0;
8870 exp
->compressed
= false;
8871 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
8872 ctx
->block
->instructions
.emplace_back(std::move(exp
));
8875 static void create_vs_exports(isel_context
*ctx
)
8877 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
8879 if (outinfo
->export_prim_id
) {
8880 ctx
->outputs
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
8881 ctx
->outputs
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = get_arg(ctx
, ctx
->args
->vs_prim_id
);
8884 if (ctx
->options
->key
.has_multiview_view_index
) {
8885 ctx
->outputs
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
8886 ctx
->outputs
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, get_arg(ctx
, ctx
->args
->ac
.view_index
));
8889 /* the order these position exports are created is important */
8891 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
8892 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
8893 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
8895 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
8896 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
8897 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
8898 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
8900 if (ctx
->export_clip_dists
) {
8901 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
8902 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
8903 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
8904 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
8907 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
8908 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
8909 i
!= VARYING_SLOT_PRIMITIVE_ID
)
8912 export_vs_varying(ctx
, i
, false, NULL
);
8916 static void export_fs_mrt_z(isel_context
*ctx
)
8918 Builder
bld(ctx
->program
, ctx
->block
);
8919 unsigned enabled_channels
= 0;
8923 for (unsigned i
= 0; i
< 4; ++i
) {
8924 values
[i
] = Operand(v1
);
8927 /* Both stencil and sample mask only need 16-bits. */
8928 if (!ctx
->program
->info
->ps
.writes_z
&&
8929 (ctx
->program
->info
->ps
.writes_stencil
||
8930 ctx
->program
->info
->ps
.writes_sample_mask
)) {
8931 compr
= true; /* COMPR flag */
8933 if (ctx
->program
->info
->ps
.writes_stencil
) {
8934 /* Stencil should be in X[23:16]. */
8935 values
[0] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_STENCIL
][0]);
8936 values
[0] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u), values
[0]);
8937 enabled_channels
|= 0x3;
8940 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
8941 /* SampleMask should be in Y[15:0]. */
8942 values
[1] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_SAMPLE_MASK
][0]);
8943 enabled_channels
|= 0xc;
8946 if (ctx
->program
->info
->ps
.writes_z
) {
8947 values
[0] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_DEPTH
][0]);
8948 enabled_channels
|= 0x1;
8951 if (ctx
->program
->info
->ps
.writes_stencil
) {
8952 values
[1] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_STENCIL
][0]);
8953 enabled_channels
|= 0x2;
8956 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
8957 values
[2] = Operand(ctx
->outputs
.outputs
[FRAG_RESULT_SAMPLE_MASK
][0]);
8958 enabled_channels
|= 0x4;
8962 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
8963 * writemask component.
8965 if (ctx
->options
->chip_class
== GFX6
&&
8966 ctx
->options
->family
!= CHIP_OLAND
&&
8967 ctx
->options
->family
!= CHIP_HAINAN
) {
8968 enabled_channels
|= 0x1;
8971 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
8972 enabled_channels
, V_008DFC_SQ_EXP_MRTZ
, compr
);
8975 static void export_fs_mrt_color(isel_context
*ctx
, int slot
)
8977 Builder
bld(ctx
->program
, ctx
->block
);
8978 unsigned write_mask
= ctx
->outputs
.mask
[slot
];
8981 for (unsigned i
= 0; i
< 4; ++i
) {
8982 if (write_mask
& (1 << i
)) {
8983 values
[i
] = Operand(ctx
->outputs
.outputs
[slot
][i
]);
8985 values
[i
] = Operand(v1
);
8989 unsigned target
, col_format
;
8990 unsigned enabled_channels
= 0;
8991 aco_opcode compr_op
= (aco_opcode
)0;
8993 slot
-= FRAG_RESULT_DATA0
;
8994 target
= V_008DFC_SQ_EXP_MRT
+ slot
;
8995 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * slot
)) & 0xf;
8997 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> slot
) & 1;
8998 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> slot
) & 1;
9002 case V_028714_SPI_SHADER_ZERO
:
9003 enabled_channels
= 0; /* writemask */
9004 target
= V_008DFC_SQ_EXP_NULL
;
9007 case V_028714_SPI_SHADER_32_R
:
9008 enabled_channels
= 1;
9011 case V_028714_SPI_SHADER_32_GR
:
9012 enabled_channels
= 0x3;
9015 case V_028714_SPI_SHADER_32_AR
:
9016 if (ctx
->options
->chip_class
>= GFX10
) {
9017 /* Special case: on GFX10, the outputs are different for 32_AR */
9018 enabled_channels
= 0x3;
9019 values
[1] = values
[3];
9020 values
[3] = Operand(v1
);
9022 enabled_channels
= 0x9;
9026 case V_028714_SPI_SHADER_FP16_ABGR
:
9027 enabled_channels
= 0x5;
9028 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
9031 case V_028714_SPI_SHADER_UNORM16_ABGR
:
9032 enabled_channels
= 0x5;
9033 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
9036 case V_028714_SPI_SHADER_SNORM16_ABGR
:
9037 enabled_channels
= 0x5;
9038 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
9041 case V_028714_SPI_SHADER_UINT16_ABGR
: {
9042 enabled_channels
= 0x5;
9043 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
9044 if (is_int8
|| is_int10
) {
9046 uint32_t max_rgb
= is_int8
? 255 : is_int10
? 1023 : 0;
9047 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
9049 for (unsigned i
= 0; i
< 4; i
++) {
9050 if ((write_mask
>> i
) & 1) {
9051 values
[i
] = bld
.vop2(aco_opcode::v_min_u32
, bld
.def(v1
),
9052 i
== 3 && is_int10
? Operand(3u) : Operand(max_rgb_val
),
9060 case V_028714_SPI_SHADER_SINT16_ABGR
:
9061 enabled_channels
= 0x5;
9062 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
9063 if (is_int8
|| is_int10
) {
9065 uint32_t max_rgb
= is_int8
? 127 : is_int10
? 511 : 0;
9066 uint32_t min_rgb
= is_int8
? -128 :is_int10
? -512 : 0;
9067 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
9068 Temp min_rgb_val
= bld
.copy(bld
.def(s1
), Operand(min_rgb
));
9070 for (unsigned i
= 0; i
< 4; i
++) {
9071 if ((write_mask
>> i
) & 1) {
9072 values
[i
] = bld
.vop2(aco_opcode::v_min_i32
, bld
.def(v1
),
9073 i
== 3 && is_int10
? Operand(1u) : Operand(max_rgb_val
),
9075 values
[i
] = bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
),
9076 i
== 3 && is_int10
? Operand(-2u) : Operand(min_rgb_val
),
9083 case V_028714_SPI_SHADER_32_ABGR
:
9084 enabled_channels
= 0xF;
9091 if (target
== V_008DFC_SQ_EXP_NULL
)
9094 if ((bool) compr_op
) {
9095 for (int i
= 0; i
< 2; i
++) {
9096 /* check if at least one of the values to be compressed is enabled */
9097 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
9099 enabled_channels
|= enabled
<< (i
*2);
9100 values
[i
] = bld
.vop3(compr_op
, bld
.def(v1
),
9101 values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2],
9102 values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1]);
9104 values
[i
] = Operand(v1
);
9107 values
[2] = Operand(v1
);
9108 values
[3] = Operand(v1
);
9110 for (int i
= 0; i
< 4; i
++)
9111 values
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
9114 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
9115 enabled_channels
, target
, (bool) compr_op
);
9118 static void create_fs_exports(isel_context
*ctx
)
9120 /* Export depth, stencil and sample mask. */
9121 if (ctx
->outputs
.mask
[FRAG_RESULT_DEPTH
] ||
9122 ctx
->outputs
.mask
[FRAG_RESULT_STENCIL
] ||
9123 ctx
->outputs
.mask
[FRAG_RESULT_SAMPLE_MASK
]) {
9124 export_fs_mrt_z(ctx
);
9127 /* Export all color render targets. */
9128 for (unsigned i
= FRAG_RESULT_DATA0
; i
< FRAG_RESULT_DATA7
+ 1; ++i
) {
9129 if (ctx
->outputs
.mask
[i
])
9130 export_fs_mrt_color(ctx
, i
);
9134 static void write_tcs_tess_factors(isel_context
*ctx
)
9136 unsigned outer_comps
;
9137 unsigned inner_comps
;
9139 switch (ctx
->args
->options
->key
.tcs
.primitive_mode
) {
9156 const unsigned tess_index_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
9157 const unsigned tess_index_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
9159 Builder
bld(ctx
->program
, ctx
->block
);
9161 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
9162 unsigned workgroup_size
= ctx
->tcs_num_patches
* ctx
->shader
->info
.tess
.tcs_vertices_out
;
9163 if (unlikely(ctx
->program
->chip_class
!= GFX6
&& workgroup_size
> ctx
->program
->wave_size
))
9164 bld
.sopp(aco_opcode::s_barrier
);
9166 Temp tcs_rel_ids
= get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
);
9167 Temp invocation_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), tcs_rel_ids
, Operand(8u), Operand(5u));
9169 Temp invocation_id_is_zero
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), invocation_id
);
9170 if_context ic_invocation_id_is_zero
;
9171 begin_divergent_if_then(ctx
, &ic_invocation_id_is_zero
, invocation_id_is_zero
);
9172 bld
.reset(ctx
->block
);
9174 Temp hs_ring_tess_factor
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_FACTOR
* 16u));
9176 std::pair
<Temp
, unsigned> lds_base
= get_tcs_output_lds_offset(ctx
);
9177 unsigned stride
= inner_comps
+ outer_comps
;
9181 assert(inner_comps
<= (sizeof(inner
) / sizeof(Temp
)));
9182 assert(outer_comps
<= (sizeof(outer
) / sizeof(Temp
)));
9183 assert(stride
<= (sizeof(out
) / sizeof(Temp
)));
9185 if (ctx
->args
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
9187 outer
[0] = out
[1] = load_lds(ctx
, 4, bld
.tmp(v1
), lds_base
.first
, lds_base
.second
+ tess_index_outer
* 16 + 0 * 4, 4);
9188 outer
[1] = out
[0] = load_lds(ctx
, 4, bld
.tmp(v1
), lds_base
.first
, lds_base
.second
+ tess_index_outer
* 16 + 1 * 4, 4);
9190 for (unsigned i
= 0; i
< outer_comps
; ++i
)
9191 outer
[i
] = out
[i
] = load_lds(ctx
, 4, bld
.tmp(v1
), lds_base
.first
, lds_base
.second
+ tess_index_outer
* 16 + i
* 4, 4);
9193 for (unsigned i
= 0; i
< inner_comps
; ++i
)
9194 inner
[i
] = out
[outer_comps
+ i
] = load_lds(ctx
, 4, bld
.tmp(v1
), lds_base
.first
, lds_base
.second
+ tess_index_inner
* 16 + i
* 4, 4);
9197 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
9198 Temp tf_base
= get_arg(ctx
, ctx
->args
->tess_factor_offset
);
9199 Temp byte_offset
= bld
.v_mul_imm(bld
.def(v1
), rel_patch_id
, stride
* 4u);
9200 unsigned tf_const_offset
= 0;
9202 if (ctx
->program
->chip_class
<= GFX8
) {
9203 Temp rel_patch_id_is_zero
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), rel_patch_id
);
9204 if_context ic_rel_patch_id_is_zero
;
9205 begin_divergent_if_then(ctx
, &ic_rel_patch_id_is_zero
, rel_patch_id_is_zero
);
9206 bld
.reset(ctx
->block
);
9208 /* Store the dynamic HS control word. */
9209 Temp control_word
= bld
.copy(bld
.def(v1
), Operand(0x80000000u
));
9210 bld
.mubuf(aco_opcode::buffer_store_dword
,
9211 /* SRSRC */ hs_ring_tess_factor
, /* VADDR */ Operand(v1
), /* SOFFSET */ tf_base
, /* VDATA */ control_word
,
9212 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
9213 /* disable_wqm */ false, /* glc */ true);
9214 tf_const_offset
+= 4;
9216 begin_divergent_if_else(ctx
, &ic_rel_patch_id_is_zero
);
9217 end_divergent_if(ctx
, &ic_rel_patch_id_is_zero
);
9218 bld
.reset(ctx
->block
);
9221 assert(stride
== 2 || stride
== 4 || stride
== 6);
9222 Temp tf_vec
= create_vec_from_array(ctx
, out
, stride
, RegType::vgpr
);
9223 store_vmem_mubuf(ctx
, tf_vec
, hs_ring_tess_factor
, byte_offset
, tf_base
, tf_const_offset
, 4, (1 << stride
) - 1, true, false);
9225 begin_divergent_if_else(ctx
, &ic_invocation_id_is_zero
);
9226 end_divergent_if(ctx
, &ic_invocation_id_is_zero
);
9229 static void emit_stream_output(isel_context
*ctx
,
9230 Temp
const *so_buffers
,
9231 Temp
const *so_write_offset
,
9232 const struct radv_stream_output
*output
)
9234 unsigned num_comps
= util_bitcount(output
->component_mask
);
9235 unsigned writemask
= (1 << num_comps
) - 1;
9236 unsigned loc
= output
->location
;
9237 unsigned buf
= output
->buffer
;
9239 assert(num_comps
&& num_comps
<= 4);
9240 if (!num_comps
|| num_comps
> 4)
9243 unsigned start
= ffs(output
->component_mask
) - 1;
9246 bool all_undef
= true;
9247 assert(ctx
->stage
== vertex_vs
|| ctx
->stage
== gs_copy_vs
);
9248 for (unsigned i
= 0; i
< num_comps
; i
++) {
9249 out
[i
] = ctx
->outputs
.outputs
[loc
][start
+ i
];
9250 all_undef
= all_undef
&& !out
[i
].id();
9257 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
9258 if (count
== 3 && ctx
->options
->chip_class
== GFX6
) {
9259 /* GFX6 doesn't support storing vec3, split it. */
9260 writemask
|= 1u << (start
+ 2);
9264 unsigned offset
= output
->offset
+ start
* 4;
9266 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, count
)};
9267 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
9268 for (int i
= 0; i
< count
; ++i
)
9269 vec
->operands
[i
] = (ctx
->outputs
.mask
[loc
] & 1 << (start
+ i
)) ? Operand(out
[start
+ i
]) : Operand(0u);
9270 vec
->definitions
[0] = Definition(write_data
);
9271 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9276 opcode
= aco_opcode::buffer_store_dword
;
9279 opcode
= aco_opcode::buffer_store_dwordx2
;
9282 opcode
= aco_opcode::buffer_store_dwordx3
;
9285 opcode
= aco_opcode::buffer_store_dwordx4
;
9288 unreachable("Unsupported dword count.");
9291 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
9292 store
->operands
[0] = Operand(so_buffers
[buf
]);
9293 store
->operands
[1] = Operand(so_write_offset
[buf
]);
9294 store
->operands
[2] = Operand((uint32_t) 0);
9295 store
->operands
[3] = Operand(write_data
);
9296 if (offset
> 4095) {
9297 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
9298 Builder
bld(ctx
->program
, ctx
->block
);
9299 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
9301 store
->offset
= offset
;
9303 store
->offen
= true;
9307 store
->can_reorder
= true;
9308 ctx
->block
->instructions
.emplace_back(std::move(store
));
9312 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
9314 Builder
bld(ctx
->program
, ctx
->block
);
9317 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->streamout_buffers
));
9318 for (unsigned i
= 0; i
< 4; i
++) {
9319 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
9323 Operand off
= bld
.copy(bld
.def(s1
), Operand(i
* 16u));
9324 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, off
);
9327 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
9328 get_arg(ctx
, ctx
->args
->streamout_config
), Operand(0x70010u
));
9330 Temp tid
= emit_mbcnt(ctx
, bld
.def(v1
));
9332 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(bld
.lm
), so_vtx_count
, tid
);
9335 begin_divergent_if_then(ctx
, &ic
, can_emit
);
9337 bld
.reset(ctx
->block
);
9339 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), get_arg(ctx
, ctx
->args
->streamout_write_idx
), tid
);
9341 Temp so_write_offset
[4];
9343 for (unsigned i
= 0; i
< 4; i
++) {
9344 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
9349 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
9350 get_arg(ctx
, ctx
->args
->streamout_write_idx
),
9351 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
9352 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
9354 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
9356 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
9357 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u),
9358 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
9359 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
9363 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
9364 struct radv_stream_output
*output
=
9365 &ctx
->program
->info
->so
.outputs
[i
];
9366 if (stream
!= output
->stream
)
9369 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
9372 begin_divergent_if_else(ctx
, &ic
);
9373 end_divergent_if(ctx
, &ic
);
9376 } /* end namespace */
9378 void split_arguments(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
9380 /* Split all arguments except for the first (ring_offsets) and the last
9381 * (exec) so that the dead channels don't stay live throughout the program.
9383 for (int i
= 1; i
< startpgm
->definitions
.size() - 1; i
++) {
9384 if (startpgm
->definitions
[i
].regClass().size() > 1) {
9385 emit_split_vector(ctx
, startpgm
->definitions
[i
].getTemp(),
9386 startpgm
->definitions
[i
].regClass().size());
9391 void handle_bc_optimize(isel_context
*ctx
)
9393 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
9394 Builder
bld(ctx
->program
, ctx
->block
);
9395 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
9396 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
9397 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
9398 ctx
->persp_centroid
= get_arg(ctx
, ctx
->args
->ac
.persp_centroid
);
9399 ctx
->linear_centroid
= get_arg(ctx
, ctx
->args
->ac
.linear_centroid
);
9400 if (uses_center
&& uses_centroid
) {
9401 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
9402 get_arg(ctx
, ctx
->args
->ac
.prim_mask
), Operand(0u));
9404 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
9406 for (unsigned i
= 0; i
< 2; i
++) {
9407 Temp persp_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_centroid
), i
, v1
);
9408 Temp persp_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_center
), i
, v1
);
9409 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
9410 persp_centroid
, persp_center
, sel
);
9412 ctx
->persp_centroid
= bld
.tmp(v2
);
9413 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->persp_centroid
),
9414 Operand(new_coord
[0]), Operand(new_coord
[1]));
9415 emit_split_vector(ctx
, ctx
->persp_centroid
, 2);
9418 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
9420 for (unsigned i
= 0; i
< 2; i
++) {
9421 Temp linear_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_centroid
), i
, v1
);
9422 Temp linear_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_center
), i
, v1
);
9423 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
9424 linear_centroid
, linear_center
, sel
);
9426 ctx
->linear_centroid
= bld
.tmp(v2
);
9427 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->linear_centroid
),
9428 Operand(new_coord
[0]), Operand(new_coord
[1]));
9429 emit_split_vector(ctx
, ctx
->linear_centroid
, 2);
9434 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
9436 Program
*program
= ctx
->program
;
9438 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
9440 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
9441 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
9442 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
9443 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
9444 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
9446 program
->next_fp_mode
.must_flush_denorms32
=
9447 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
9448 program
->next_fp_mode
.must_flush_denorms16_64
=
9449 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
9450 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
9452 program
->next_fp_mode
.care_about_round32
=
9453 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
9455 program
->next_fp_mode
.care_about_round16_64
=
9456 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
9457 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
9459 /* default to preserving fp16 and fp64 denorms, since it's free */
9460 if (program
->next_fp_mode
.must_flush_denorms16_64
)
9461 program
->next_fp_mode
.denorm16_64
= 0;
9463 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
9465 /* preserving fp32 denorms is expensive, so only do it if asked */
9466 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
9467 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
9469 program
->next_fp_mode
.denorm32
= 0;
9471 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
9472 program
->next_fp_mode
.round32
= fp_round_tz
;
9474 program
->next_fp_mode
.round32
= fp_round_ne
;
9476 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
9477 program
->next_fp_mode
.round16_64
= fp_round_tz
;
9479 program
->next_fp_mode
.round16_64
= fp_round_ne
;
9481 ctx
->block
->fp_mode
= program
->next_fp_mode
;
9484 void cleanup_cfg(Program
*program
)
9486 /* create linear_succs/logical_succs */
9487 for (Block
& BB
: program
->blocks
) {
9488 for (unsigned idx
: BB
.linear_preds
)
9489 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
9490 for (unsigned idx
: BB
.logical_preds
)
9491 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);
9495 void select_program(Program
*program
,
9496 unsigned shader_count
,
9497 struct nir_shader
*const *shaders
,
9498 ac_shader_config
* config
,
9499 struct radv_shader_args
*args
)
9501 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, args
, false);
9503 for (unsigned i
= 0; i
< shader_count
; i
++) {
9504 nir_shader
*nir
= shaders
[i
];
9505 init_context(&ctx
, nir
);
9507 setup_fp_mode(&ctx
, nir
);
9510 /* needs to be after init_context() for FS */
9511 Pseudo_instruction
*startpgm
= add_startpgm(&ctx
);
9512 append_logical_start(ctx
.block
);
9513 split_arguments(&ctx
, startpgm
);
9517 if (shader_count
>= 2) {
9518 Builder
bld(ctx
.program
, ctx
.block
);
9519 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)));
9520 Temp thread_id
= emit_mbcnt(&ctx
, bld
.def(v1
));
9521 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), count
, thread_id
);
9523 begin_divergent_if_then(&ctx
, &ic
, cond
);
9527 Builder
bld(ctx
.program
, ctx
.block
);
9529 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
9530 bld
.sopp(aco_opcode::s_barrier
);
9532 if (ctx
.stage
== vertex_geometry_gs
) {
9533 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));
9535 } else if (ctx
.stage
== geometry_gs
)
9536 ctx
.gs_wave_id
= get_arg(&ctx
, args
->gs_wave_id
);
9538 if (ctx
.stage
== fragment_fs
)
9539 handle_bc_optimize(&ctx
);
9541 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
9542 visit_cf_list(&ctx
, &func
->body
);
9544 if (ctx
.program
->info
->so
.num_outputs
&& ctx
.stage
== vertex_vs
)
9545 emit_streamout(&ctx
, 0);
9547 if (ctx
.stage
== vertex_vs
) {
9548 create_vs_exports(&ctx
);
9549 } else if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
9550 Builder
bld(ctx
.program
, ctx
.block
);
9551 bld
.barrier(aco_opcode::p_memory_barrier_gs_data
);
9552 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
.gs_wave_id
), -1, sendmsg_gs_done(false, false, 0));
9553 } else if (nir
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
9554 write_tcs_tess_factors(&ctx
);
9557 if (ctx
.stage
== fragment_fs
)
9558 create_fs_exports(&ctx
);
9560 if (shader_count
>= 2) {
9561 begin_divergent_if_else(&ctx
, &ic
);
9562 end_divergent_if(&ctx
, &ic
);
9565 ralloc_free(ctx
.divergent_vals
);
9568 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
9570 append_logical_end(ctx
.block
);
9571 ctx
.block
->kind
|= block_kind_uniform
| block_kind_export_end
;
9572 Builder
bld(ctx
.program
, ctx
.block
);
9573 if (ctx
.program
->wb_smem_l1_on_end
)
9574 bld
.smem(aco_opcode::s_dcache_wb
, false);
9575 bld
.sopp(aco_opcode::s_endpgm
);
9577 cleanup_cfg(program
);
9580 void select_gs_copy_shader(Program
*program
, struct nir_shader
*gs_shader
,
9581 ac_shader_config
* config
,
9582 struct radv_shader_args
*args
)
9584 isel_context ctx
= setup_isel_context(program
, 1, &gs_shader
, config
, args
, true);
9586 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
= false;
9587 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
= false;
9588 program
->next_fp_mode
.must_flush_denorms32
= false;
9589 program
->next_fp_mode
.must_flush_denorms16_64
= false;
9590 program
->next_fp_mode
.care_about_round32
= false;
9591 program
->next_fp_mode
.care_about_round16_64
= false;
9592 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
9593 program
->next_fp_mode
.denorm32
= 0;
9594 program
->next_fp_mode
.round32
= fp_round_ne
;
9595 program
->next_fp_mode
.round16_64
= fp_round_ne
;
9596 ctx
.block
->fp_mode
= program
->next_fp_mode
;
9599 append_logical_start(ctx
.block
);
9601 Builder
bld(ctx
.program
, ctx
.block
);
9603 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), program
->private_segment_buffer
, Operand(RING_GSVS_VS
* 16u));
9605 Operand
stream_id(0u);
9606 if (args
->shader_info
->so
.num_outputs
)
9607 stream_id
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
9608 get_arg(&ctx
, ctx
.args
->streamout_config
), Operand(0x20018u
));
9610 Temp vtx_offset
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), get_arg(&ctx
, ctx
.args
->ac
.vertex_id
));
9612 std::stack
<Block
> endif_blocks
;
9614 for (unsigned stream
= 0; stream
< 4; stream
++) {
9615 if (stream_id
.isConstant() && stream
!= stream_id
.constantValue())
9618 unsigned num_components
= args
->shader_info
->gs
.num_stream_output_components
[stream
];
9619 if (stream
> 0 && (!num_components
|| !args
->shader_info
->so
.num_outputs
))
9622 memset(ctx
.outputs
.mask
, 0, sizeof(ctx
.outputs
.mask
));
9624 unsigned BB_if_idx
= ctx
.block
->index
;
9625 Block BB_endif
= Block();
9626 if (!stream_id
.isConstant()) {
9628 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), stream_id
, Operand(stream
));
9629 append_logical_end(ctx
.block
);
9630 ctx
.block
->kind
|= block_kind_uniform
;
9631 bld
.branch(aco_opcode::p_cbranch_z
, cond
);
9633 BB_endif
.kind
|= ctx
.block
->kind
& block_kind_top_level
;
9635 ctx
.block
= ctx
.program
->create_and_insert_block();
9636 add_edge(BB_if_idx
, ctx
.block
);
9637 bld
.reset(ctx
.block
);
9638 append_logical_start(ctx
.block
);
9641 unsigned offset
= 0;
9642 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
9643 if (args
->shader_info
->gs
.output_streams
[i
] != stream
)
9646 unsigned output_usage_mask
= args
->shader_info
->gs
.output_usage_mask
[i
];
9647 unsigned length
= util_last_bit(output_usage_mask
);
9648 for (unsigned j
= 0; j
< length
; ++j
) {
9649 if (!(output_usage_mask
& (1 << j
)))
9652 unsigned const_offset
= offset
* args
->shader_info
->gs
.vertices_out
* 16 * 4;
9653 Temp voffset
= vtx_offset
;
9654 if (const_offset
>= 4096u) {
9655 voffset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), voffset
);
9656 const_offset
%= 4096u;
9659 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dword
, Format::MUBUF
, 3, 1)};
9660 mubuf
->definitions
[0] = bld
.def(v1
);
9661 mubuf
->operands
[0] = Operand(gsvs_ring
);
9662 mubuf
->operands
[1] = Operand(voffset
);
9663 mubuf
->operands
[2] = Operand(0u);
9664 mubuf
->offen
= true;
9665 mubuf
->offset
= const_offset
;
9668 mubuf
->dlc
= args
->options
->chip_class
>= GFX10
;
9669 mubuf
->barrier
= barrier_none
;
9670 mubuf
->can_reorder
= true;
9672 ctx
.outputs
.mask
[i
] |= 1 << j
;
9673 ctx
.outputs
.outputs
[i
][j
] = mubuf
->definitions
[0].getTemp();
9675 bld
.insert(std::move(mubuf
));
9681 if (args
->shader_info
->so
.num_outputs
) {
9682 emit_streamout(&ctx
, stream
);
9683 bld
.reset(ctx
.block
);
9687 create_vs_exports(&ctx
);
9688 ctx
.block
->kind
|= block_kind_export_end
;
9691 if (!stream_id
.isConstant()) {
9692 append_logical_end(ctx
.block
);
9694 /* branch from then block to endif block */
9695 bld
.branch(aco_opcode::p_branch
);
9696 add_edge(ctx
.block
->index
, &BB_endif
);
9697 ctx
.block
->kind
|= block_kind_uniform
;
9699 /* emit else block */
9700 ctx
.block
= ctx
.program
->create_and_insert_block();
9701 add_edge(BB_if_idx
, ctx
.block
);
9702 bld
.reset(ctx
.block
);
9703 append_logical_start(ctx
.block
);
9705 endif_blocks
.push(std::move(BB_endif
));
9709 while (!endif_blocks
.empty()) {
9710 Block BB_endif
= std::move(endif_blocks
.top());
9713 Block
*BB_else
= ctx
.block
;
9715 append_logical_end(BB_else
);
9716 /* branch from else block to endif block */
9717 bld
.branch(aco_opcode::p_branch
);
9718 add_edge(BB_else
->index
, &BB_endif
);
9719 BB_else
->kind
|= block_kind_uniform
;
9721 /** emit endif merge block */
9722 ctx
.block
= program
->insert_block(std::move(BB_endif
));
9723 bld
.reset(ctx
.block
);
9724 append_logical_start(ctx
.block
);
9727 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
9729 append_logical_end(ctx
.block
);
9730 ctx
.block
->kind
|= block_kind_uniform
;
9731 bld
.sopp(aco_opcode::s_endpgm
);
9733 cleanup_cfg(program
);