2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "ac_shader_util.h"
32 #include "aco_builder.h"
33 #include "aco_interface.h"
34 #include "aco_instruction_selection_setup.cpp"
35 #include "util/fast_idiv_by_const.h"
40 class loop_info_RAII
{
42 unsigned header_idx_old
;
44 bool divergent_cont_old
;
45 bool divergent_branch_old
;
46 bool divergent_if_old
;
49 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
51 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
52 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
53 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
54 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
56 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
57 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
58 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
59 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
60 ctx
->cf_info
.parent_if
.is_divergent
= false;
61 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
66 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
67 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
68 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
69 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
70 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
71 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
72 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
73 ctx
->cf_info
.exec_potentially_empty
= false;
81 bool exec_potentially_empty_old
;
85 bool then_branch_divergent
;
90 static void visit_cf_list(struct isel_context
*ctx
,
91 struct exec_list
*list
);
93 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
95 succ
->logical_preds
.emplace_back(pred_idx
);
99 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
101 succ
->linear_preds
.emplace_back(pred_idx
);
104 static void add_edge(unsigned pred_idx
, Block
*succ
)
106 add_logical_edge(pred_idx
, succ
);
107 add_linear_edge(pred_idx
, succ
);
110 static void append_logical_start(Block
*b
)
112 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
115 static void append_logical_end(Block
*b
)
117 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
120 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
122 assert(ctx
->allocated
[def
->index
].id());
123 return ctx
->allocated
[def
->index
];
126 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
128 Builder
bld(ctx
->program
, ctx
->block
);
131 dst
= bld
.tmp(src
.regClass());
133 assert(src
.size() == dst
.size());
135 if (ctx
->stage
!= fragment_fs
) {
139 bld
.copy(Definition(dst
), src
);
143 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
144 ctx
->program
->needs_wqm
|= program_needs_wqm
;
148 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
150 if (index
.regClass() == s1
)
151 return bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), data
, index
);
153 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
155 /* Currently not implemented on GFX6-7 */
156 assert(ctx
->options
->chip_class
>= GFX8
);
158 if (ctx
->options
->chip_class
<= GFX9
|| ctx
->program
->wave_size
== 32) {
159 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
162 /* GFX10, wave64 mode:
163 * The bpermute instruction is limited to half-wave operation, which means that it can't
164 * properly support subgroup shuffle like older generations (or wave32 mode), so we
167 if (!ctx
->has_gfx10_wave64_bpermute
) {
168 ctx
->has_gfx10_wave64_bpermute
= true;
169 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
170 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
173 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u));
174 lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), lane_id
);
175 Temp lane_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), lane_id
);
176 Temp index_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), index
);
177 Temp cmp
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
, vcc
), lane_is_hi
, index_is_hi
);
179 return bld
.reduction(aco_opcode::p_wave64_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
),
180 bld
.vcc(cmp
), Operand(v2
.as_linear()), index_x4
, data
, gfx10_wave64_bpermute
);
183 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
185 if (val
.type() == RegType::sgpr
) {
186 Builder
bld(ctx
->program
, ctx
->block
);
187 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
189 assert(val
.type() == RegType::vgpr
);
193 //assumes a != 0xffffffff
194 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
197 Builder
bld(ctx
->program
, ctx
->block
);
199 if (util_is_power_of_two_or_zero(b
)) {
200 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
204 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
206 assert(info
.multiplier
<= 0xffffffff);
208 bool pre_shift
= info
.pre_shift
!= 0;
209 bool increment
= info
.increment
!= 0;
210 bool multiply
= true;
211 bool post_shift
= info
.post_shift
!= 0;
213 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
214 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
218 Temp pre_shift_dst
= a
;
220 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
221 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
224 Temp increment_dst
= pre_shift_dst
;
226 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
227 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
230 Temp multiply_dst
= increment_dst
;
232 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
233 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
234 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
238 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
242 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
244 Builder
bld(ctx
->program
, ctx
->block
);
245 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
249 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
251 /* no need to extract the whole vector */
252 if (src
.regClass() == dst_rc
) {
256 assert(src
.size() > idx
);
257 Builder
bld(ctx
->program
, ctx
->block
);
258 auto it
= ctx
->allocated_vec
.find(src
.id());
259 /* the size check needs to be early because elements other than 0 may be garbage */
260 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
261 if (it
->second
[idx
].regClass() == dst_rc
) {
262 return it
->second
[idx
];
264 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
265 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
266 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
270 if (src
.size() == dst_rc
.size()) {
272 return bld
.copy(bld
.def(dst_rc
), src
);
274 Temp dst
= bld
.tmp(dst_rc
);
275 emit_extract_vector(ctx
, src
, idx
, dst
);
280 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
282 if (num_components
== 1)
284 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
286 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
287 split
->operands
[0] = Operand(vec_src
);
288 std::array
<Temp
,4> elems
;
289 for (unsigned i
= 0; i
< num_components
; i
++) {
290 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
291 split
->definitions
[i
] = Definition(elems
[i
]);
293 ctx
->block
->instructions
.emplace_back(std::move(split
));
294 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
297 /* This vector expansion uses a mask to determine which elements in the new vector
298 * come from the original vector. The other elements are undefined. */
299 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
301 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
306 Builder
bld(ctx
->program
, ctx
->block
);
307 if (num_components
== 1) {
308 if (dst
.type() == RegType::sgpr
)
309 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
311 bld
.copy(Definition(dst
), vec_src
);
315 unsigned component_size
= dst
.size() / num_components
;
316 std::array
<Temp
,4> elems
;
318 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
319 vec
->definitions
[0] = Definition(dst
);
321 for (unsigned i
= 0; i
< num_components
; i
++) {
322 if (mask
& (1 << i
)) {
323 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
324 if (dst
.type() == RegType::sgpr
)
325 src
= bld
.as_uniform(src
);
326 vec
->operands
[i
] = Operand(src
);
328 vec
->operands
[i
] = Operand(0u);
330 elems
[i
] = vec
->operands
[i
].getTemp();
332 ctx
->block
->instructions
.emplace_back(std::move(vec
));
333 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
336 Temp
bool_to_vector_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s2
))
338 Builder
bld(ctx
->program
, ctx
->block
);
342 assert(val
.regClass() == s1
);
343 assert(dst
.regClass() == s2
);
345 return bld
.sop2(aco_opcode::s_cselect_b64
, bld
.hint_vcc(Definition(dst
)), Operand((uint32_t) -1), Operand(0u), bld
.scc(val
));
348 Temp
bool_to_scalar_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s1
))
350 Builder
bld(ctx
->program
, ctx
->block
);
354 assert(val
.regClass() == s2
);
355 assert(dst
.regClass() == s1
);
357 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
358 Temp tmp
= bld
.tmp(s1
);
359 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, s2
));
360 return emit_wqm(ctx
, tmp
, dst
);
363 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
365 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
366 return get_ssa_temp(ctx
, src
.src
.ssa
);
368 if (src
.src
.ssa
->num_components
== size
) {
369 bool identity_swizzle
= true;
370 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
371 if (src
.swizzle
[i
] != i
)
372 identity_swizzle
= false;
374 if (identity_swizzle
)
375 return get_ssa_temp(ctx
, src
.src
.ssa
);
378 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
379 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
380 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
381 assert(vec
.size() % elem_size
== 0);
383 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
385 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
388 std::array
<Temp
,4> elems
;
389 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
390 for (unsigned i
= 0; i
< size
; ++i
) {
391 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
392 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
394 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
395 vec_instr
->definitions
[0] = Definition(dst
);
396 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
397 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
402 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
406 Builder
bld(ctx
->program
, ctx
->block
);
407 if (ptr
.type() == RegType::vgpr
)
408 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
409 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
410 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
413 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
415 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
416 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
417 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
418 sop2
->definitions
[0] = Definition(dst
);
420 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
421 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
424 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool commutative
, bool swap_srcs
=false)
426 Builder
bld(ctx
->program
, ctx
->block
);
427 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
428 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
429 if (src1
.type() == RegType::sgpr
) {
430 if (commutative
&& src0
.type() == RegType::vgpr
) {
434 } else if (src0
.type() == RegType::vgpr
&&
435 op
!= aco_opcode::v_madmk_f32
&&
436 op
!= aco_opcode::v_madak_f32
&&
437 op
!= aco_opcode::v_madmk_f16
&&
438 op
!= aco_opcode::v_madak_f16
) {
439 /* If the instruction is not commutative, we emit a VOP3A instruction */
440 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
443 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
446 bld
.vop2(op
, Definition(dst
), src0
, src1
);
449 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
451 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
452 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
453 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
455 /* ensure that the instruction has at most 1 sgpr operand
456 * The optimizer will inline constants for us */
457 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
458 src0
= as_vgpr(ctx
, src0
);
459 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
460 src1
= as_vgpr(ctx
, src1
);
461 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
462 src2
= as_vgpr(ctx
, src2
);
464 Builder
bld(ctx
->program
, ctx
->block
);
465 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
468 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
470 Builder
bld(ctx
->program
, ctx
->block
);
471 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
474 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
476 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
477 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
478 aco_ptr
<Instruction
> vopc
;
479 if (src1
.type() == RegType::sgpr
) {
480 if (src0
.type() == RegType::vgpr
) {
481 /* to swap the operands, we might also have to change the opcode */
483 case aco_opcode::v_cmp_lt_f32
:
484 op
= aco_opcode::v_cmp_gt_f32
;
486 case aco_opcode::v_cmp_ge_f32
:
487 op
= aco_opcode::v_cmp_le_f32
;
489 case aco_opcode::v_cmp_lt_i32
:
490 op
= aco_opcode::v_cmp_gt_i32
;
492 case aco_opcode::v_cmp_ge_i32
:
493 op
= aco_opcode::v_cmp_le_i32
;
495 case aco_opcode::v_cmp_lt_u32
:
496 op
= aco_opcode::v_cmp_gt_u32
;
498 case aco_opcode::v_cmp_ge_u32
:
499 op
= aco_opcode::v_cmp_le_u32
;
501 case aco_opcode::v_cmp_lt_f64
:
502 op
= aco_opcode::v_cmp_gt_f64
;
504 case aco_opcode::v_cmp_ge_f64
:
505 op
= aco_opcode::v_cmp_le_f64
;
507 case aco_opcode::v_cmp_lt_i64
:
508 op
= aco_opcode::v_cmp_gt_i64
;
510 case aco_opcode::v_cmp_ge_i64
:
511 op
= aco_opcode::v_cmp_le_i64
;
513 case aco_opcode::v_cmp_lt_u64
:
514 op
= aco_opcode::v_cmp_gt_u64
;
516 case aco_opcode::v_cmp_ge_u64
:
517 op
= aco_opcode::v_cmp_le_u64
;
519 default: /* eq and ne are commutative */
526 src1
= as_vgpr(ctx
, src1
);
530 Builder
bld(ctx
->program
, ctx
->block
);
531 bld
.vopc(op
, bld
.hint_vcc(Definition(dst
)), src0
, src1
);
534 void emit_sopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
536 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
537 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
539 assert(dst
.regClass() == s2
);
540 assert(src0
.type() == RegType::sgpr
);
541 assert(src1
.type() == RegType::sgpr
);
543 Builder
bld(ctx
->program
, ctx
->block
);
544 /* Emit the SALU comparison instruction */
545 Temp cmp
= bld
.sopc(op
, bld
.scc(bld
.def(s1
)), src0
, src1
);
546 /* Turn the result into a per-lane bool */
547 bool_to_vector_condition(ctx
, cmp
, dst
);
550 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
,
551 aco_opcode v32_op
, aco_opcode v64_op
, aco_opcode s32_op
= aco_opcode::last_opcode
, aco_opcode s64_op
= aco_opcode::last_opcode
)
553 aco_opcode s_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? s64_op
: s32_op
;
554 aco_opcode v_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? v64_op
: v32_op
;
555 bool divergent_vals
= ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
];
556 bool use_valu
= s_op
== aco_opcode::last_opcode
||
558 ctx
->allocated
[instr
->src
[0].src
.ssa
->index
].type() == RegType::vgpr
||
559 ctx
->allocated
[instr
->src
[1].src
.ssa
->index
].type() == RegType::vgpr
;
560 aco_opcode op
= use_valu
? v_op
: s_op
;
561 assert(op
!= aco_opcode::last_opcode
);
564 emit_vopc_instruction(ctx
, instr
, op
, dst
);
566 emit_sopc_instruction(ctx
, instr
, op
, dst
);
569 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op32
, aco_opcode op64
, Temp dst
)
571 Builder
bld(ctx
->program
, ctx
->block
);
572 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
573 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
575 assert(dst
.regClass() == s2
);
576 assert(src0
.regClass() == s2
);
577 assert(src1
.regClass() == s2
);
579 bld
.sop2(op64
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
582 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
584 Builder
bld(ctx
->program
, ctx
->block
);
585 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
586 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
587 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
589 assert(cond
.regClass() == s2
);
591 if (dst
.type() == RegType::vgpr
) {
592 aco_ptr
<Instruction
> bcsel
;
593 if (dst
.size() == 1) {
594 then
= as_vgpr(ctx
, then
);
595 els
= as_vgpr(ctx
, els
);
597 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
598 } else if (dst
.size() == 2) {
599 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
600 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
601 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
602 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
604 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
605 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
607 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
609 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
610 nir_print_instr(&instr
->instr
, stderr
);
611 fprintf(stderr
, "\n");
616 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
617 assert(dst
.regClass() == s2
);
618 assert(then
.regClass() == s2
);
619 assert(els
.regClass() == s2
);
622 if (!ctx
->divergent_vals
[instr
->src
[0].src
.ssa
->index
]) { /* uniform condition and values in sgpr */
623 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
624 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
625 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
626 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(bool_to_scalar_condition(ctx
, cond
)));
628 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
629 nir_print_instr(&instr
->instr
, stderr
);
630 fprintf(stderr
, "\n");
635 /* divergent boolean bcsel
636 * this implements bcsel on bools: dst = s0 ? s1 : s2
637 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
638 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
640 if (cond
.id() != then
.id())
641 then
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), cond
, then
);
643 if (cond
.id() == els
.id())
644 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), then
);
646 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), then
,
647 bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), els
, cond
));
650 void emit_scaled_op(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
,
651 aco_opcode op
, uint32_t undo
)
653 /* multiply by 16777216 to handle denormals */
654 Temp is_denormal
= bld
.vopc(aco_opcode::v_cmp_class_f32
, bld
.hint_vcc(bld
.def(s2
)),
655 as_vgpr(ctx
, val
), bld
.copy(bld
.def(v1
), Operand((1u << 7) | (1u << 4))));
656 Temp scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x4b800000u
), val
);
657 scaled
= bld
.vop1(op
, bld
.def(v1
), scaled
);
658 scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(undo
), scaled
);
660 Temp not_scaled
= bld
.vop1(op
, bld
.def(v1
), val
);
662 bld
.vop2(aco_opcode::v_cndmask_b32
, dst
, not_scaled
, scaled
, is_denormal
);
665 void emit_rcp(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
667 if (ctx
->block
->fp_mode
.denorm32
== 0) {
668 bld
.vop1(aco_opcode::v_rcp_f32
, dst
, val
);
672 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rcp_f32
, 0x4b800000u
);
675 void emit_rsq(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
677 if (ctx
->block
->fp_mode
.denorm32
== 0) {
678 bld
.vop1(aco_opcode::v_rsq_f32
, dst
, val
);
682 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rsq_f32
, 0x45800000u
);
685 void emit_sqrt(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
687 if (ctx
->block
->fp_mode
.denorm32
== 0) {
688 bld
.vop1(aco_opcode::v_sqrt_f32
, dst
, val
);
692 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_sqrt_f32
, 0x39800000u
);
695 void emit_log2(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
697 if (ctx
->block
->fp_mode
.denorm32
== 0) {
698 bld
.vop1(aco_opcode::v_log_f32
, dst
, val
);
702 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_log_f32
, 0xc1c00000u
);
705 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
707 if (!instr
->dest
.dest
.is_ssa
) {
708 fprintf(stderr
, "nir alu dst not in ssa: ");
709 nir_print_instr(&instr
->instr
, stderr
);
710 fprintf(stderr
, "\n");
713 Builder
bld(ctx
->program
, ctx
->block
);
714 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
719 std::array
<Temp
,4> elems
;
720 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
721 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
722 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
723 vec
->operands
[i
] = Operand
{elems
[i
]};
725 vec
->definitions
[0] = Definition(dst
);
726 ctx
->block
->instructions
.emplace_back(std::move(vec
));
727 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
731 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
732 aco_ptr
<Instruction
> mov
;
733 if (dst
.type() == RegType::sgpr
) {
734 if (src
.type() == RegType::vgpr
)
735 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
736 else if (src
.regClass() == s1
)
737 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
738 else if (src
.regClass() == s2
)
739 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
741 unreachable("wrong src register class for nir_op_imov");
742 } else if (dst
.regClass() == v1
) {
743 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
744 } else if (dst
.regClass() == v2
) {
745 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
747 nir_print_instr(&instr
->instr
, stderr
);
748 unreachable("Should have been lowered to scalar.");
753 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
754 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
755 assert(src
.regClass() == s2
);
756 assert(dst
.regClass() == s2
);
757 bld
.sop2(aco_opcode::s_andn2_b64
, Definition(dst
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
758 } else if (dst
.regClass() == v1
) {
759 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
760 } else if (dst
.type() == RegType::sgpr
) {
761 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
762 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
764 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
765 nir_print_instr(&instr
->instr
, stderr
);
766 fprintf(stderr
, "\n");
771 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
772 if (dst
.regClass() == v1
) {
773 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
774 } else if (dst
.regClass() == s1
) {
775 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
776 } else if (dst
.size() == 2) {
777 Temp src0
= bld
.tmp(dst
.type(), 1);
778 Temp src1
= bld
.tmp(dst
.type(), 1);
779 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
781 if (dst
.regClass() == s2
) {
782 Temp carry
= bld
.tmp(s1
);
783 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
784 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
785 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
787 Temp lower
= bld
.tmp(v1
);
788 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
789 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
790 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
793 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
794 nir_print_instr(&instr
->instr
, stderr
);
795 fprintf(stderr
, "\n");
800 if (dst
.regClass() == s1
) {
801 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
802 } else if (dst
.regClass() == v1
) {
803 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
804 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
806 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
807 nir_print_instr(&instr
->instr
, stderr
);
808 fprintf(stderr
, "\n");
813 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
814 if (dst
.regClass() == s1
) {
815 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
816 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
817 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
818 } else if (dst
.regClass() == s2
) {
819 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
820 Temp neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
821 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, neqz
);
822 } else if (dst
.regClass() == v1
) {
823 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
824 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
825 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
826 } else if (dst
.regClass() == v2
) {
827 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
828 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
829 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
830 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
831 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
832 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
834 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
835 nir_print_instr(&instr
->instr
, stderr
);
836 fprintf(stderr
, "\n");
841 if (dst
.regClass() == v1
) {
842 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
843 } else if (dst
.regClass() == s1
) {
844 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
846 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
847 nir_print_instr(&instr
->instr
, stderr
);
848 fprintf(stderr
, "\n");
853 if (dst
.regClass() == v1
) {
854 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
855 } else if (dst
.regClass() == s1
) {
856 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
858 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
859 nir_print_instr(&instr
->instr
, stderr
);
860 fprintf(stderr
, "\n");
865 if (dst
.regClass() == v1
) {
866 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
867 } else if (dst
.regClass() == s1
) {
868 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
870 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
871 nir_print_instr(&instr
->instr
, stderr
);
872 fprintf(stderr
, "\n");
877 if (dst
.regClass() == v1
) {
878 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
879 } else if (dst
.regClass() == s1
) {
880 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
882 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
883 nir_print_instr(&instr
->instr
, stderr
);
884 fprintf(stderr
, "\n");
889 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
890 emit_boolean_logic(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::s_or_b64
, dst
);
891 } else if (dst
.regClass() == v1
) {
892 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
893 } else if (dst
.regClass() == s1
) {
894 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
895 } else if (dst
.regClass() == s2
) {
896 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
898 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
899 nir_print_instr(&instr
->instr
, stderr
);
900 fprintf(stderr
, "\n");
905 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
906 emit_boolean_logic(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::s_and_b64
, dst
);
907 } else if (dst
.regClass() == v1
) {
908 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
909 } else if (dst
.regClass() == s1
) {
910 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
911 } else if (dst
.regClass() == s2
) {
912 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
914 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
915 nir_print_instr(&instr
->instr
, stderr
);
916 fprintf(stderr
, "\n");
921 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
922 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
923 } else if (dst
.regClass() == v1
) {
924 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
925 } else if (dst
.regClass() == s1
) {
926 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
927 } else if (dst
.regClass() == s2
) {
928 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
930 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
931 nir_print_instr(&instr
->instr
, stderr
);
932 fprintf(stderr
, "\n");
937 if (dst
.regClass() == v1
) {
938 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
939 } else if (dst
.regClass() == v2
) {
940 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
941 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
942 } else if (dst
.regClass() == s2
) {
943 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
944 } else if (dst
.regClass() == s1
) {
945 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
947 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
948 nir_print_instr(&instr
->instr
, stderr
);
949 fprintf(stderr
, "\n");
954 if (dst
.regClass() == v1
) {
955 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
956 } else if (dst
.regClass() == v2
) {
957 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
958 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
959 } else if (dst
.regClass() == s1
) {
960 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
961 } else if (dst
.regClass() == s2
) {
962 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
964 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
965 nir_print_instr(&instr
->instr
, stderr
);
966 fprintf(stderr
, "\n");
971 if (dst
.regClass() == v1
) {
972 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
973 } else if (dst
.regClass() == v2
) {
974 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
975 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
976 } else if (dst
.regClass() == s1
) {
977 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
978 } else if (dst
.regClass() == s2
) {
979 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
981 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
982 nir_print_instr(&instr
->instr
, stderr
);
983 fprintf(stderr
, "\n");
987 case nir_op_find_lsb
: {
988 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
989 if (src
.regClass() == s1
) {
990 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
991 } else if (src
.regClass() == v1
) {
992 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
993 } else if (src
.regClass() == s2
) {
994 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
996 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
997 nir_print_instr(&instr
->instr
, stderr
);
998 fprintf(stderr
, "\n");
1002 case nir_op_ufind_msb
:
1003 case nir_op_ifind_msb
: {
1004 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1005 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
1006 aco_opcode op
= src
.regClass() == s2
?
1007 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
1008 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
1009 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
1011 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
1012 Operand(src
.size() * 32u - 1u), msb_rev
);
1013 Temp msb
= sub
.def(0).getTemp();
1014 Temp carry
= sub
.def(1).getTemp();
1016 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
1017 } else if (src
.regClass() == v1
) {
1018 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
1019 Temp msb_rev
= bld
.tmp(v1
);
1020 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
1021 Temp msb
= bld
.tmp(v1
);
1022 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
1023 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
1025 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1026 nir_print_instr(&instr
->instr
, stderr
);
1027 fprintf(stderr
, "\n");
1031 case nir_op_bitfield_reverse
: {
1032 if (dst
.regClass() == s1
) {
1033 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1034 } else if (dst
.regClass() == v1
) {
1035 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1037 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1038 nir_print_instr(&instr
->instr
, stderr
);
1039 fprintf(stderr
, "\n");
1044 if (dst
.regClass() == s1
) {
1045 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1049 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1050 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1051 if (dst
.regClass() == v1
) {
1052 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1056 assert(src0
.size() == 2 && src1
.size() == 2);
1057 Temp src00
= bld
.tmp(src0
.type(), 1);
1058 Temp src01
= bld
.tmp(dst
.type(), 1);
1059 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1060 Temp src10
= bld
.tmp(src1
.type(), 1);
1061 Temp src11
= bld
.tmp(dst
.type(), 1);
1062 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1064 if (dst
.regClass() == s2
) {
1065 Temp carry
= bld
.tmp(s1
);
1066 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1067 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1068 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1069 } else if (dst
.regClass() == v2
) {
1070 Temp dst0
= bld
.tmp(v1
);
1071 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1072 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1073 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1075 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1076 nir_print_instr(&instr
->instr
, stderr
);
1077 fprintf(stderr
, "\n");
1081 case nir_op_uadd_sat
: {
1082 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1083 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1084 if (dst
.regClass() == s1
) {
1085 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1086 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1088 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1089 } else if (dst
.regClass() == v1
) {
1090 if (ctx
->options
->chip_class
>= GFX9
) {
1091 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1092 add
->operands
[0] = Operand(src0
);
1093 add
->operands
[1] = Operand(src1
);
1094 add
->definitions
[0] = Definition(dst
);
1096 ctx
->block
->instructions
.emplace_back(std::move(add
));
1098 if (src1
.regClass() != v1
)
1099 std::swap(src0
, src1
);
1100 assert(src1
.regClass() == v1
);
1101 Temp tmp
= bld
.tmp(v1
);
1102 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1103 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1106 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1107 nir_print_instr(&instr
->instr
, stderr
);
1108 fprintf(stderr
, "\n");
1112 case nir_op_uadd_carry
: {
1113 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1114 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1115 if (dst
.regClass() == s1
) {
1116 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1119 if (dst
.regClass() == v1
) {
1120 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1121 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1125 Temp src00
= bld
.tmp(src0
.type(), 1);
1126 Temp src01
= bld
.tmp(dst
.type(), 1);
1127 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1128 Temp src10
= bld
.tmp(src1
.type(), 1);
1129 Temp src11
= bld
.tmp(dst
.type(), 1);
1130 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1131 if (dst
.regClass() == s2
) {
1132 Temp carry
= bld
.tmp(s1
);
1133 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1134 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1135 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1136 } else if (dst
.regClass() == v2
) {
1137 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1138 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1139 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1140 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1142 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1143 nir_print_instr(&instr
->instr
, stderr
);
1144 fprintf(stderr
, "\n");
1149 if (dst
.regClass() == s1
) {
1150 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1154 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1155 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1156 if (dst
.regClass() == v1
) {
1157 bld
.vsub32(Definition(dst
), src0
, src1
);
1161 Temp src00
= bld
.tmp(src0
.type(), 1);
1162 Temp src01
= bld
.tmp(dst
.type(), 1);
1163 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1164 Temp src10
= bld
.tmp(src1
.type(), 1);
1165 Temp src11
= bld
.tmp(dst
.type(), 1);
1166 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1167 if (dst
.regClass() == s2
) {
1168 Temp carry
= bld
.tmp(s1
);
1169 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1170 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1171 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1172 } else if (dst
.regClass() == v2
) {
1173 Temp lower
= bld
.tmp(v1
);
1174 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1175 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1176 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1178 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1179 nir_print_instr(&instr
->instr
, stderr
);
1180 fprintf(stderr
, "\n");
1184 case nir_op_usub_borrow
: {
1185 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1186 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1187 if (dst
.regClass() == s1
) {
1188 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1190 } else if (dst
.regClass() == v1
) {
1191 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1192 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1196 Temp src00
= bld
.tmp(src0
.type(), 1);
1197 Temp src01
= bld
.tmp(dst
.type(), 1);
1198 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1199 Temp src10
= bld
.tmp(src1
.type(), 1);
1200 Temp src11
= bld
.tmp(dst
.type(), 1);
1201 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1202 if (dst
.regClass() == s2
) {
1203 Temp borrow
= bld
.tmp(s1
);
1204 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1205 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1206 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1207 } else if (dst
.regClass() == v2
) {
1208 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1209 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1210 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1211 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1213 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1214 nir_print_instr(&instr
->instr
, stderr
);
1215 fprintf(stderr
, "\n");
1220 if (dst
.regClass() == v1
) {
1221 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1222 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1223 } else if (dst
.regClass() == s1
) {
1224 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1226 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1227 nir_print_instr(&instr
->instr
, stderr
);
1228 fprintf(stderr
, "\n");
1232 case nir_op_umul_high
: {
1233 if (dst
.regClass() == v1
) {
1234 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1235 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1236 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1237 } else if (dst
.regClass() == s1
) {
1238 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1239 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1240 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1242 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1243 nir_print_instr(&instr
->instr
, stderr
);
1244 fprintf(stderr
, "\n");
1248 case nir_op_imul_high
: {
1249 if (dst
.regClass() == v1
) {
1250 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1251 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1252 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1253 } else if (dst
.regClass() == s1
) {
1254 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1255 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1256 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1258 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1259 nir_print_instr(&instr
->instr
, stderr
);
1260 fprintf(stderr
, "\n");
1265 if (dst
.size() == 1) {
1266 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1267 } else if (dst
.size() == 2) {
1268 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1269 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1271 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1272 nir_print_instr(&instr
->instr
, stderr
);
1273 fprintf(stderr
, "\n");
1278 if (dst
.size() == 1) {
1279 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1280 } else if (dst
.size() == 2) {
1281 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1282 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1284 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1285 nir_print_instr(&instr
->instr
, stderr
);
1286 fprintf(stderr
, "\n");
1291 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1292 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1293 if (dst
.size() == 1) {
1294 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1295 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1297 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1298 } else if (dst
.size() == 2) {
1299 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1300 get_alu_src(ctx
, instr
->src
[0]),
1301 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1302 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1305 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1306 nir_print_instr(&instr
->instr
, stderr
);
1307 fprintf(stderr
, "\n");
1312 if (dst
.size() == 1) {
1313 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true);
1314 } else if (dst
.size() == 2) {
1315 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1316 get_alu_src(ctx
, instr
->src
[0]),
1317 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1319 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1320 nir_print_instr(&instr
->instr
, stderr
);
1321 fprintf(stderr
, "\n");
1326 if (dst
.size() == 1) {
1327 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1328 } else if (dst
.size() == 2) {
1329 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1330 get_alu_src(ctx
, instr
->src
[0]),
1331 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1333 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1334 nir_print_instr(&instr
->instr
, stderr
);
1335 fprintf(stderr
, "\n");
1339 case nir_op_fmax3
: {
1340 if (dst
.size() == 1) {
1341 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1343 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1344 nir_print_instr(&instr
->instr
, stderr
);
1345 fprintf(stderr
, "\n");
1349 case nir_op_fmin3
: {
1350 if (dst
.size() == 1) {
1351 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1353 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1354 nir_print_instr(&instr
->instr
, stderr
);
1355 fprintf(stderr
, "\n");
1359 case nir_op_fmed3
: {
1360 if (dst
.size() == 1) {
1361 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1363 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1364 nir_print_instr(&instr
->instr
, stderr
);
1365 fprintf(stderr
, "\n");
1369 case nir_op_umax3
: {
1370 if (dst
.size() == 1) {
1371 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1373 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1374 nir_print_instr(&instr
->instr
, stderr
);
1375 fprintf(stderr
, "\n");
1379 case nir_op_umin3
: {
1380 if (dst
.size() == 1) {
1381 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1383 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1384 nir_print_instr(&instr
->instr
, stderr
);
1385 fprintf(stderr
, "\n");
1389 case nir_op_umed3
: {
1390 if (dst
.size() == 1) {
1391 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1393 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1394 nir_print_instr(&instr
->instr
, stderr
);
1395 fprintf(stderr
, "\n");
1399 case nir_op_imax3
: {
1400 if (dst
.size() == 1) {
1401 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1403 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1404 nir_print_instr(&instr
->instr
, stderr
);
1405 fprintf(stderr
, "\n");
1409 case nir_op_imin3
: {
1410 if (dst
.size() == 1) {
1411 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1413 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1414 nir_print_instr(&instr
->instr
, stderr
);
1415 fprintf(stderr
, "\n");
1419 case nir_op_imed3
: {
1420 if (dst
.size() == 1) {
1421 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1423 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1424 nir_print_instr(&instr
->instr
, stderr
);
1425 fprintf(stderr
, "\n");
1429 case nir_op_cube_face_coord
: {
1430 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1431 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1432 emit_extract_vector(ctx
, in
, 1, v1
),
1433 emit_extract_vector(ctx
, in
, 2, v1
) };
1434 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1435 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1436 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1437 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1438 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1439 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1440 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1443 case nir_op_cube_face_index
: {
1444 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1445 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1446 emit_extract_vector(ctx
, in
, 1, v1
),
1447 emit_extract_vector(ctx
, in
, 2, v1
) };
1448 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1451 case nir_op_bcsel
: {
1452 emit_bcsel(ctx
, instr
, dst
);
1456 if (dst
.size() == 1) {
1457 emit_rsq(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1458 } else if (dst
.size() == 2) {
1459 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1461 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1462 nir_print_instr(&instr
->instr
, stderr
);
1463 fprintf(stderr
, "\n");
1468 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1469 if (dst
.size() == 1) {
1470 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1471 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1472 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1473 } else if (dst
.size() == 2) {
1474 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1475 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1476 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1477 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1478 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1479 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1481 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1482 nir_print_instr(&instr
->instr
, stderr
);
1483 fprintf(stderr
, "\n");
1488 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1489 if (dst
.size() == 1) {
1490 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1491 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1492 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1493 } else if (dst
.size() == 2) {
1494 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1495 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1496 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1497 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1498 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1499 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1501 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1502 nir_print_instr(&instr
->instr
, stderr
);
1503 fprintf(stderr
, "\n");
1508 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1509 if (dst
.size() == 1) {
1510 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1511 } else if (dst
.size() == 2) {
1512 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1513 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1516 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1517 nir_print_instr(&instr
->instr
, stderr
);
1518 fprintf(stderr
, "\n");
1522 case nir_op_flog2
: {
1523 if (dst
.size() == 1) {
1524 emit_log2(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1526 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1527 nir_print_instr(&instr
->instr
, stderr
);
1528 fprintf(stderr
, "\n");
1533 if (dst
.size() == 1) {
1534 emit_rcp(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1535 } else if (dst
.size() == 2) {
1536 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1538 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1539 nir_print_instr(&instr
->instr
, stderr
);
1540 fprintf(stderr
, "\n");
1544 case nir_op_fexp2
: {
1545 if (dst
.size() == 1) {
1546 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1548 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1549 nir_print_instr(&instr
->instr
, stderr
);
1550 fprintf(stderr
, "\n");
1554 case nir_op_fsqrt
: {
1555 if (dst
.size() == 1) {
1556 emit_sqrt(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1557 } else if (dst
.size() == 2) {
1558 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1560 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1561 nir_print_instr(&instr
->instr
, stderr
);
1562 fprintf(stderr
, "\n");
1566 case nir_op_ffract
: {
1567 if (dst
.size() == 1) {
1568 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1569 } else if (dst
.size() == 2) {
1570 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1572 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1573 nir_print_instr(&instr
->instr
, stderr
);
1574 fprintf(stderr
, "\n");
1578 case nir_op_ffloor
: {
1579 if (dst
.size() == 1) {
1580 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1581 } else if (dst
.size() == 2) {
1582 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1584 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1585 nir_print_instr(&instr
->instr
, stderr
);
1586 fprintf(stderr
, "\n");
1590 case nir_op_fceil
: {
1591 if (dst
.size() == 1) {
1592 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1593 } else if (dst
.size() == 2) {
1594 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1596 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1597 nir_print_instr(&instr
->instr
, stderr
);
1598 fprintf(stderr
, "\n");
1602 case nir_op_ftrunc
: {
1603 if (dst
.size() == 1) {
1604 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1605 } else if (dst
.size() == 2) {
1606 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1608 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1609 nir_print_instr(&instr
->instr
, stderr
);
1610 fprintf(stderr
, "\n");
1614 case nir_op_fround_even
: {
1615 if (dst
.size() == 1) {
1616 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1617 } else if (dst
.size() == 2) {
1618 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1620 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1621 nir_print_instr(&instr
->instr
, stderr
);
1622 fprintf(stderr
, "\n");
1628 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1629 aco_ptr
<Instruction
> norm
;
1630 if (dst
.size() == 1) {
1632 Operand
half_pi(0x3e22f983u
);
1633 if (src
.type() == RegType::sgpr
)
1634 tmp
= bld
.vop2_e64(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1636 tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1638 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1639 if (ctx
->options
->chip_class
< GFX9
)
1640 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1642 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1643 bld
.vop1(opcode
, Definition(dst
), tmp
);
1645 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1646 nir_print_instr(&instr
->instr
, stderr
);
1647 fprintf(stderr
, "\n");
1651 case nir_op_ldexp
: {
1652 if (dst
.size() == 1) {
1653 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1654 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1655 get_alu_src(ctx
, instr
->src
[1]));
1656 } else if (dst
.size() == 2) {
1657 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1658 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1659 get_alu_src(ctx
, instr
->src
[1]));
1661 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1662 nir_print_instr(&instr
->instr
, stderr
);
1663 fprintf(stderr
, "\n");
1667 case nir_op_frexp_sig
: {
1668 if (dst
.size() == 1) {
1669 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1670 get_alu_src(ctx
, instr
->src
[0]));
1671 } else if (dst
.size() == 2) {
1672 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1673 get_alu_src(ctx
, instr
->src
[0]));
1675 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1676 nir_print_instr(&instr
->instr
, stderr
);
1677 fprintf(stderr
, "\n");
1681 case nir_op_frexp_exp
: {
1682 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1683 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1684 get_alu_src(ctx
, instr
->src
[0]));
1685 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1686 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1687 get_alu_src(ctx
, instr
->src
[0]));
1689 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1690 nir_print_instr(&instr
->instr
, stderr
);
1691 fprintf(stderr
, "\n");
1695 case nir_op_fsign
: {
1696 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1697 if (dst
.size() == 1) {
1698 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1699 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1700 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1701 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1702 } else if (dst
.size() == 2) {
1703 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1704 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1705 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, emit_extract_vector(ctx
, src
, 1, v1
), cond
);
1707 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1708 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1709 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1711 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1713 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1714 nir_print_instr(&instr
->instr
, stderr
);
1715 fprintf(stderr
, "\n");
1719 case nir_op_f2f32
: {
1720 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1721 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1723 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1724 nir_print_instr(&instr
->instr
, stderr
);
1725 fprintf(stderr
, "\n");
1729 case nir_op_f2f64
: {
1730 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1731 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1733 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1734 nir_print_instr(&instr
->instr
, stderr
);
1735 fprintf(stderr
, "\n");
1739 case nir_op_i2f32
: {
1740 assert(dst
.size() == 1);
1741 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1744 case nir_op_i2f64
: {
1745 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1746 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1747 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1748 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1749 RegClass rc
= RegClass(src
.type(), 1);
1750 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1751 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1752 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1753 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1754 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1755 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1758 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1759 nir_print_instr(&instr
->instr
, stderr
);
1760 fprintf(stderr
, "\n");
1764 case nir_op_u2f32
: {
1765 assert(dst
.size() == 1);
1766 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1769 case nir_op_u2f64
: {
1770 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1771 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1772 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1773 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1774 RegClass rc
= RegClass(src
.type(), 1);
1775 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1776 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1777 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1778 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1779 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1780 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1782 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1783 nir_print_instr(&instr
->instr
, stderr
);
1784 fprintf(stderr
, "\n");
1788 case nir_op_f2i32
: {
1789 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1790 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1791 if (dst
.type() == RegType::vgpr
)
1792 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1794 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1795 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1797 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1798 if (dst
.type() == RegType::vgpr
)
1799 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1801 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1802 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1805 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1806 nir_print_instr(&instr
->instr
, stderr
);
1807 fprintf(stderr
, "\n");
1811 case nir_op_f2u32
: {
1812 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1813 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1814 if (dst
.type() == RegType::vgpr
)
1815 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1817 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1818 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1820 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1821 if (dst
.type() == RegType::vgpr
)
1822 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1824 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1825 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1828 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1829 nir_print_instr(&instr
->instr
, stderr
);
1830 fprintf(stderr
, "\n");
1834 case nir_op_f2i64
: {
1835 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1836 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1837 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1838 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1839 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1840 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1841 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1842 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1843 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1844 Temp new_exponent
= bld
.tmp(v1
);
1845 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1846 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1847 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1848 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1849 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1850 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1851 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1852 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1853 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1854 Temp new_lower
= bld
.tmp(v1
);
1855 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1856 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1857 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1859 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1860 if (src
.type() == RegType::vgpr
)
1861 src
= bld
.as_uniform(src
);
1862 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1863 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1864 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1865 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1866 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1867 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1868 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1869 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1870 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1871 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1872 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1873 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1874 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1875 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1876 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1877 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1878 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1879 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1880 Temp borrow
= bld
.tmp(s1
);
1881 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1882 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1883 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1885 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1886 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1887 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1888 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1889 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1890 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1891 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1892 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1893 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1894 if (dst
.type() == RegType::sgpr
) {
1895 lower
= bld
.as_uniform(lower
);
1896 upper
= bld
.as_uniform(upper
);
1898 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1901 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1902 nir_print_instr(&instr
->instr
, stderr
);
1903 fprintf(stderr
, "\n");
1907 case nir_op_f2u64
: {
1908 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1909 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1910 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1911 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(64u), exponent
);
1912 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1913 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1914 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1915 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1916 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1917 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1918 Temp new_exponent
= bld
.tmp(v1
);
1919 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1920 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1921 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1922 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1923 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1924 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1925 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1926 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1927 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1929 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1930 if (src
.type() == RegType::vgpr
)
1931 src
= bld
.as_uniform(src
);
1932 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1933 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1934 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1935 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1936 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1937 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1938 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1939 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1940 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1941 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1942 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1943 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1944 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1945 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1946 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1947 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1948 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1949 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1951 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1952 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1953 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1954 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1955 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1956 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1957 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1958 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1959 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1960 if (dst
.type() == RegType::sgpr
) {
1961 lower
= bld
.as_uniform(lower
);
1962 upper
= bld
.as_uniform(upper
);
1964 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1967 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1968 nir_print_instr(&instr
->instr
, stderr
);
1969 fprintf(stderr
, "\n");
1973 case nir_op_b2f32
: {
1974 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1975 assert(src
.regClass() == s2
);
1977 if (dst
.regClass() == s1
) {
1978 src
= bool_to_scalar_condition(ctx
, src
);
1979 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
1980 } else if (dst
.regClass() == v1
) {
1981 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1983 unreachable("Wrong destination register class for nir_op_b2f32.");
1987 case nir_op_b2f64
: {
1988 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1989 assert(src
.regClass() == s2
);
1991 if (dst
.regClass() == s2
) {
1992 src
= bool_to_scalar_condition(ctx
, src
);
1993 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
1994 } else if (dst
.regClass() == v2
) {
1995 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
1996 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
, src
);
1997 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1999 unreachable("Wrong destination register class for nir_op_b2f64.");
2003 case nir_op_i2i32
: {
2004 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2005 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2006 /* we can actually just say dst = src, as it would map the lower register */
2007 emit_extract_vector(ctx
, src
, 0, dst
);
2009 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2010 nir_print_instr(&instr
->instr
, stderr
);
2011 fprintf(stderr
, "\n");
2015 case nir_op_u2u32
: {
2016 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2017 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2018 if (dst
.regClass() == s1
) {
2019 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
2021 // TODO: do better with SDWA
2022 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
2024 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2025 /* we can actually just say dst = src, as it would map the lower register */
2026 emit_extract_vector(ctx
, src
, 0, dst
);
2028 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2029 nir_print_instr(&instr
->instr
, stderr
);
2030 fprintf(stderr
, "\n");
2034 case nir_op_i2i64
: {
2035 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2036 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2037 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
2039 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2040 nir_print_instr(&instr
->instr
, stderr
);
2041 fprintf(stderr
, "\n");
2045 case nir_op_u2u64
: {
2046 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2047 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2048 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
2050 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2051 nir_print_instr(&instr
->instr
, stderr
);
2052 fprintf(stderr
, "\n");
2056 case nir_op_b2i32
: {
2057 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2058 assert(src
.regClass() == s2
);
2060 if (dst
.regClass() == s1
) {
2061 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2062 bool_to_scalar_condition(ctx
, src
, dst
);
2063 } else if (dst
.regClass() == v1
) {
2064 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2066 unreachable("Invalid register class for b2i32");
2071 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2072 assert(dst
.regClass() == s2
);
2074 if (src
.type() == RegType::vgpr
) {
2075 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2076 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2077 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2079 assert(src
.regClass() == s1
|| src
.regClass() == s2
);
2080 Temp tmp
= bld
.sopc(src
.size() == 2 ? aco_opcode::s_cmp_lg_u64
: aco_opcode::s_cmp_lg_u32
,
2081 bld
.scc(bld
.def(s1
)), Operand(0u), src
);
2082 bool_to_vector_condition(ctx
, tmp
, dst
);
2086 case nir_op_pack_64_2x32_split
: {
2087 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2088 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2090 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2093 case nir_op_unpack_64_2x32_split_x
:
2094 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2096 case nir_op_unpack_64_2x32_split_y
:
2097 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2099 case nir_op_pack_half_2x16
: {
2100 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2102 if (dst
.regClass() == v1
) {
2103 Temp src0
= bld
.tmp(v1
);
2104 Temp src1
= bld
.tmp(v1
);
2105 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2106 if (!ctx
->block
->fp_mode
.care_about_round32
|| ctx
->block
->fp_mode
.round32
== fp_round_tz
)
2107 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2109 bld
.vop3(aco_opcode::v_cvt_pk_u16_u32
, Definition(dst
),
2110 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src0
),
2111 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src1
));
2113 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2114 nir_print_instr(&instr
->instr
, stderr
);
2115 fprintf(stderr
, "\n");
2119 case nir_op_unpack_half_2x16_split_x
: {
2120 if (dst
.regClass() == v1
) {
2121 Builder
bld(ctx
->program
, ctx
->block
);
2122 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2124 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2125 nir_print_instr(&instr
->instr
, stderr
);
2126 fprintf(stderr
, "\n");
2130 case nir_op_unpack_half_2x16_split_y
: {
2131 if (dst
.regClass() == v1
) {
2132 Builder
bld(ctx
->program
, ctx
->block
);
2133 /* TODO: use SDWA here */
2134 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2135 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2137 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2138 nir_print_instr(&instr
->instr
, stderr
);
2139 fprintf(stderr
, "\n");
2143 case nir_op_fquantize2f16
: {
2144 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2145 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), src
);
2147 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2149 Temp cmp_res
= bld
.tmp(s2
);
2150 bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, Definition(cmp_res
), f16
, mask
).def(0).setHint(vcc
);
2152 Temp f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2154 if (ctx
->block
->fp_mode
.preserve_signed_zero_inf_nan32
) {
2155 Temp copysign_0
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0u), as_vgpr(ctx
, src
));
2156 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), copysign_0
, f32
, cmp_res
);
2158 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2163 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2164 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2166 if (dst
.regClass() == s1
) {
2167 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2168 } else if (dst
.regClass() == v1
) {
2169 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2171 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2172 nir_print_instr(&instr
->instr
, stderr
);
2173 fprintf(stderr
, "\n");
2177 case nir_op_bitfield_select
: {
2178 /* (mask & insert) | (~mask & base) */
2179 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2180 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2181 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2183 /* dst = (insert & bitmask) | (base & ~bitmask) */
2184 if (dst
.regClass() == s1
) {
2185 aco_ptr
<Instruction
> sop2
;
2186 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2187 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2189 if (const_insert
&& const_bitmask
) {
2190 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2192 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2193 lhs
= Operand(insert
);
2197 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2198 if (const_base
&& const_bitmask
) {
2199 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2201 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2202 rhs
= Operand(base
);
2205 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2207 } else if (dst
.regClass() == v1
) {
2208 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2209 base
= as_vgpr(ctx
, base
);
2210 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2211 insert
= as_vgpr(ctx
, insert
);
2213 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2216 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2217 nir_print_instr(&instr
->instr
, stderr
);
2218 fprintf(stderr
, "\n");
2224 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2225 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2226 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2228 if (dst
.type() == RegType::sgpr
) {
2230 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2231 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2232 if (const_offset
&& const_bits
) {
2233 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2234 extract
= Operand(const_extract
);
2238 width
= Operand(const_bits
->u32
<< 16);
2240 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2242 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2246 if (dst
.regClass() == s1
) {
2247 if (instr
->op
== nir_op_ubfe
)
2248 opcode
= aco_opcode::s_bfe_u32
;
2250 opcode
= aco_opcode::s_bfe_i32
;
2251 } else if (dst
.regClass() == s2
) {
2252 if (instr
->op
== nir_op_ubfe
)
2253 opcode
= aco_opcode::s_bfe_u64
;
2255 opcode
= aco_opcode::s_bfe_i64
;
2257 unreachable("Unsupported BFE bit size");
2260 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2264 if (dst
.regClass() == v1
) {
2265 if (instr
->op
== nir_op_ubfe
)
2266 opcode
= aco_opcode::v_bfe_u32
;
2268 opcode
= aco_opcode::v_bfe_i32
;
2270 unreachable("Unsupported BFE bit size");
2273 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2277 case nir_op_bit_count
: {
2278 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2279 if (src
.regClass() == s1
) {
2280 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2281 } else if (src
.regClass() == v1
) {
2282 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2283 } else if (src
.regClass() == v2
) {
2284 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2285 emit_extract_vector(ctx
, src
, 1, v1
),
2286 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2287 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2288 } else if (src
.regClass() == s2
) {
2289 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2291 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2292 nir_print_instr(&instr
->instr
, stderr
);
2293 fprintf(stderr
, "\n");
2298 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_f32
, aco_opcode::v_cmp_lt_f64
);
2302 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_f32
, aco_opcode::v_cmp_ge_f64
);
2306 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_f32
, aco_opcode::v_cmp_eq_f64
);
2310 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_neq_f32
, aco_opcode::v_cmp_neq_f64
);
2314 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_i32
, aco_opcode::v_cmp_lt_i64
, aco_opcode::s_cmp_lt_i32
);
2318 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_i32
, aco_opcode::v_cmp_ge_i64
, aco_opcode::s_cmp_ge_i32
);
2322 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2323 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xnor_b32
, aco_opcode::s_xnor_b64
, dst
);
2325 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_i32
, aco_opcode::v_cmp_eq_i64
, aco_opcode::s_cmp_eq_i32
, aco_opcode::s_cmp_eq_u64
);
2329 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2330 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
2332 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lg_i32
, aco_opcode::v_cmp_lg_i64
, aco_opcode::s_cmp_lg_i32
, aco_opcode::s_cmp_lg_u64
);
2336 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_u32
, aco_opcode::v_cmp_lt_u64
, aco_opcode::s_cmp_lt_u32
);
2340 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_u32
, aco_opcode::v_cmp_ge_u64
, aco_opcode::s_cmp_ge_u32
);
2345 case nir_op_fddx_fine
:
2346 case nir_op_fddy_fine
:
2347 case nir_op_fddx_coarse
:
2348 case nir_op_fddy_coarse
: {
2349 Definition tl
= bld
.def(v1
);
2351 if (instr
->op
== nir_op_fddx_fine
) {
2352 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 2, 2));
2353 dpp_ctrl
= dpp_quad_perm(1, 1, 3, 3);
2354 } else if (instr
->op
== nir_op_fddy_fine
) {
2355 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 1, 0, 1));
2356 dpp_ctrl
= dpp_quad_perm(2, 3, 2, 3);
2358 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 0, 0));
2359 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2360 dpp_ctrl
= dpp_quad_perm(1, 1, 1, 1);
2362 dpp_ctrl
= dpp_quad_perm(2, 2, 2, 2);
2365 Definition tmp
= bld
.def(v1
);
2366 bld
.vop2_dpp(aco_opcode::v_sub_f32
, tmp
, get_alu_src(ctx
, instr
->src
[0]), tl
.getTemp(), dpp_ctrl
);
2367 emit_wqm(ctx
, tmp
.getTemp(), dst
, true);
2371 fprintf(stderr
, "Unknown NIR ALU instr: ");
2372 nir_print_instr(&instr
->instr
, stderr
);
2373 fprintf(stderr
, "\n");
2377 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2379 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2381 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2382 // which get truncated the lsb if double and msb if int
2383 // for now, we only use s_mov_b64 with 64bit inline constants
2384 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2385 assert(dst
.type() == RegType::sgpr
);
2387 Builder
bld(ctx
->program
, ctx
->block
);
2389 if (instr
->def
.bit_size
== 1) {
2390 assert(dst
.regClass() == s2
);
2391 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), Operand((uint64_t)(instr
->value
[0].b
? -1 : 0)));
2392 } else if (dst
.size() == 1) {
2393 bld
.copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2395 assert(dst
.size() != 1);
2396 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2397 if (instr
->def
.bit_size
== 64)
2398 for (unsigned i
= 0; i
< dst
.size(); i
++)
2399 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2401 for (unsigned i
= 0; i
< dst
.size(); i
++)
2402 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2404 vec
->definitions
[0] = Definition(dst
);
2405 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2409 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2411 uint32_t new_mask
= 0;
2412 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2413 if (mask
& (1u << i
))
2414 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2418 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2420 /* This wouldn't work inside control flow or with indirect offsets but
2421 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2423 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2424 unsigned component
= nir_intrinsic_component(instr
);
2425 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2426 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2428 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2429 if (off_instr
->type
!= nir_instr_type_load_const
) {
2430 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2431 nir_print_instr(off_instr
, stderr
);
2432 fprintf(stderr
, "\n");
2434 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2436 if (instr
->src
[0].ssa
->bit_size
== 64)
2437 write_mask
= widen_mask(write_mask
, 2);
2439 for (unsigned i
= 0; i
< 8; ++i
) {
2440 if (write_mask
& (1 << i
)) {
2441 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2442 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2448 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2450 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2452 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2453 for (unsigned i
= 0; i
< 4; ++i
) {
2454 if (write_mask
& (1 << i
)) {
2455 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2456 values
[i
] = Operand(tmp
);
2458 values
[i
] = Operand(v1
);
2462 unsigned index
= nir_intrinsic_base(instr
) / 4;
2463 unsigned target
, col_format
;
2464 unsigned enabled_channels
= 0xF;
2465 aco_opcode compr_op
= (aco_opcode
)0;
2467 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2468 assert(offset
&& "Non-const offsets on exports not yet supported");
2469 index
+= offset
->u32
;
2471 assert(index
!= FRAG_RESULT_COLOR
);
2473 /* Unlike vertex shader exports, it's fine to use multiple exports to
2474 * export separate channels of one target. So shaders which export both
2475 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2476 * TODO: combine the exports in those cases and create better code
2479 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2481 if (ctx
->program
->info
->ps
.writes_z
) {
2482 target
= V_008DFC_SQ_EXP_MRTZ
;
2483 enabled_channels
= 0x4;
2484 col_format
= (unsigned) -1;
2486 values
[2] = values
[0];
2487 values
[0] = Operand(v1
);
2489 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2490 exp
->valid_mask
= false;
2492 exp
->compressed
= true;
2493 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2494 exp
->enabled_mask
= 0xc;
2495 for (int i
= 0; i
< 4; i
++)
2496 exp
->operands
[i
] = Operand(v1
);
2497 exp
->operands
[1] = Operand(values
[0]);
2498 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2502 } else if (index
== FRAG_RESULT_DEPTH
) {
2504 target
= V_008DFC_SQ_EXP_MRTZ
;
2505 enabled_channels
= 0x1;
2506 col_format
= (unsigned) -1;
2508 } else if (index
== FRAG_RESULT_STENCIL
) {
2510 if (ctx
->program
->info
->ps
.writes_z
) {
2511 target
= V_008DFC_SQ_EXP_MRTZ
;
2512 enabled_channels
= 0x2;
2513 col_format
= (unsigned) -1;
2515 values
[1] = values
[0];
2516 values
[0] = Operand(v1
);
2518 aco_ptr
<Instruction
> shift
{create_instruction
<VOP2_instruction
>(aco_opcode::v_lshlrev_b32
, Format::VOP2
, 2, 1)};
2519 shift
->operands
[0] = Operand((uint32_t) 16);
2520 shift
->operands
[1] = values
[0];
2521 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2522 shift
->definitions
[0] = Definition(tmp
);
2523 ctx
->block
->instructions
.emplace_back(std::move(shift
));
2525 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2526 exp
->valid_mask
= false;
2528 exp
->compressed
= true;
2529 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2530 exp
->enabled_mask
= 0x3;
2531 exp
->operands
[0] = Operand(tmp
);
2532 for (int i
= 1; i
< 4; i
++)
2533 exp
->operands
[i
] = Operand(v1
);
2534 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2539 index
-= FRAG_RESULT_DATA0
;
2540 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2541 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2543 ASSERTED
bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2544 ASSERTED
bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2545 assert(!is_int8
&& !is_int10
);
2549 case V_028714_SPI_SHADER_ZERO
:
2550 enabled_channels
= 0; /* writemask */
2551 target
= V_008DFC_SQ_EXP_NULL
;
2554 case V_028714_SPI_SHADER_32_R
:
2555 enabled_channels
= 1;
2558 case V_028714_SPI_SHADER_32_GR
:
2559 enabled_channels
= 0x3;
2562 case V_028714_SPI_SHADER_32_AR
:
2563 if (ctx
->options
->chip_class
>= GFX10
) {
2564 /* Special case: on GFX10, the outputs are different for 32_AR */
2565 enabled_channels
= 0x3;
2566 values
[1] = values
[3];
2568 enabled_channels
= 0x9;
2572 case V_028714_SPI_SHADER_FP16_ABGR
:
2573 enabled_channels
= 0x5;
2574 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2577 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2578 enabled_channels
= 0x5;
2579 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2582 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2583 enabled_channels
= 0x5;
2584 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2587 case V_028714_SPI_SHADER_UINT16_ABGR
:
2588 enabled_channels
= 0x5;
2589 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2592 case V_028714_SPI_SHADER_SINT16_ABGR
:
2593 enabled_channels
= 0x5;
2594 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2597 case V_028714_SPI_SHADER_32_ABGR
:
2598 enabled_channels
= 0xF;
2605 if (target
== V_008DFC_SQ_EXP_NULL
)
2610 for (int i
= 0; i
< 2; i
++)
2612 /* check if at least one of the values to be compressed is enabled */
2613 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2615 enabled_channels
|= enabled
<< (i
*2);
2616 aco_ptr
<VOP3A_instruction
> compr
{create_instruction
<VOP3A_instruction
>(compr_op
, Format::VOP3A
, 2, 1)};
2617 Temp tmp
{ctx
->program
->allocateId(), v1
};
2618 compr
->operands
[0] = values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2];
2619 compr
->operands
[1] = values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1];
2620 compr
->definitions
[0] = Definition(tmp
);
2621 values
[i
] = Operand(tmp
);
2622 ctx
->block
->instructions
.emplace_back(std::move(compr
));
2624 values
[i
] = Operand(v1
);
2629 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2630 exp
->valid_mask
= false;
2632 exp
->compressed
= (bool) compr_op
;
2634 exp
->enabled_mask
= enabled_channels
;
2635 if ((bool) compr_op
) {
2636 for (int i
= 0; i
< 2; i
++)
2637 exp
->operands
[i
] = enabled_channels
& (3 << (i
* 2)) ? values
[i
] : Operand(v1
);
2638 exp
->operands
[2] = Operand(v1
);
2639 exp
->operands
[3] = Operand(v1
);
2641 for (int i
= 0; i
< 4; i
++)
2642 exp
->operands
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2645 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2648 Operand
load_lds_size_m0(isel_context
*ctx
)
2650 /* TODO: m0 does not need to be initialized on GFX9+ */
2651 Builder
bld(ctx
->program
, ctx
->block
);
2652 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
2655 void load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
2656 Temp address
, unsigned base_offset
, unsigned align
)
2658 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2660 Builder
bld(ctx
->program
, ctx
->block
);
2662 Operand m
= load_lds_size_m0(ctx
);
2664 unsigned num_components
= dst
.size() * 4u / elem_size_bytes
;
2665 unsigned bytes_read
= 0;
2666 unsigned result_size
= 0;
2667 unsigned total_bytes
= num_components
* elem_size_bytes
;
2668 std::array
<Temp
, 4> result
;
2670 while (bytes_read
< total_bytes
) {
2671 unsigned todo
= total_bytes
- bytes_read
;
2672 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2673 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2675 aco_opcode op
= aco_opcode::last_opcode
;
2677 if (todo
>= 16 && aligned16
) {
2678 op
= aco_opcode::ds_read_b128
;
2680 } else if (todo
>= 16 && aligned8
) {
2681 op
= aco_opcode::ds_read2_b64
;
2684 } else if (todo
>= 12 && aligned16
) {
2685 op
= aco_opcode::ds_read_b96
;
2687 } else if (todo
>= 8 && aligned8
) {
2688 op
= aco_opcode::ds_read_b64
;
2690 } else if (todo
>= 8) {
2691 op
= aco_opcode::ds_read2_b32
;
2694 } else if (todo
>= 4) {
2695 op
= aco_opcode::ds_read_b32
;
2700 assert(todo
% elem_size_bytes
== 0);
2701 unsigned num_elements
= todo
/ elem_size_bytes
;
2702 unsigned offset
= base_offset
+ bytes_read
;
2703 unsigned max_offset
= read2
? 1019 : 65535;
2705 Temp address_offset
= address
;
2706 if (offset
> max_offset
) {
2707 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2708 offset
= bytes_read
;
2710 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2713 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2716 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2719 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
2721 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2723 if (num_components
== 1) {
2724 assert(todo
== total_bytes
);
2725 if (dst
.type() == RegType::sgpr
)
2726 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2730 if (dst
.type() == RegType::sgpr
)
2731 res
= bld
.as_uniform(res
);
2733 if (num_elements
== 1) {
2734 result
[result_size
++] = res
;
2736 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2737 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2738 split
->operands
[0] = Operand(res
);
2739 for (unsigned i
= 0; i
< num_elements
; i
++)
2740 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2741 ctx
->block
->instructions
.emplace_back(std::move(split
));
2747 assert(result_size
== num_components
&& result_size
> 1);
2748 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2749 for (unsigned i
= 0; i
< result_size
; i
++)
2750 vec
->operands
[i
] = Operand(result
[i
]);
2751 vec
->definitions
[0] = Definition(dst
);
2752 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2753 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2756 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2758 if (start
== 0 && size
== data
.size())
2759 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2761 unsigned size_hint
= 1;
2762 auto it
= ctx
->allocated_vec
.find(data
.id());
2763 if (it
!= ctx
->allocated_vec
.end())
2764 size_hint
= it
->second
[0].size();
2765 if (size
% size_hint
|| start
% size_hint
)
2772 for (unsigned i
= 0; i
< size
; i
++)
2773 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2776 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2778 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2779 for (unsigned i
= 0; i
< size
; i
++)
2780 vec
->operands
[i
] = Operand(elems
[i
]);
2781 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2782 vec
->definitions
[0] = Definition(res
);
2783 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2787 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
)
2789 Builder
bld(ctx
->program
, ctx
->block
);
2790 unsigned bytes_written
= 0;
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
) {
2800 op
= aco_opcode::ds_write_b128
;
2802 } else if (todo
>= 16 && aligned8
) {
2803 op
= aco_opcode::ds_write2_b64
;
2806 } else if (todo
>= 12 && aligned16
) {
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) {
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
>> 2, (offset
>> 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);
2850 Operand m
= load_lds_size_m0(ctx
);
2852 /* we need at most two stores for 32bit variables */
2853 int start
[2], count
[2];
2854 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2855 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2856 assert(wrmask
== 0);
2858 /* one combined store is sufficient */
2859 if (count
[0] == count
[1]) {
2860 Builder
bld(ctx
->program
, ctx
->block
);
2862 Temp address_offset
= address
;
2863 if ((base_offset
>> 2) + start
[1] > 255) {
2864 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2868 assert(count
[0] == 1);
2869 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
2870 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
2871 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2872 base_offset
= base_offset
/ elem_size_bytes
;
2873 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2874 base_offset
+ start
[0], base_offset
+ start
[1]);
2878 for (unsigned i
= 0; i
< 2; i
++) {
2882 unsigned elem_size_words
= elem_size_bytes
/ 4;
2883 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2884 base_offset
, start
[i
] * elem_size_bytes
, align
);
2889 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2891 if (ctx
->stage
== vertex_vs
) {
2892 visit_store_vs_output(ctx
, instr
);
2893 } else if (ctx
->stage
== fragment_fs
) {
2894 visit_store_fs_output(ctx
, instr
);
2896 unreachable("Shader stage not implemented");
2900 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2902 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2903 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2905 Builder
bld(ctx
->program
, ctx
->block
);
2906 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2907 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2910 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2912 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2913 for (unsigned i
= 0; i
< num_components
; i
++)
2914 vec
->operands
[i
] = Operand(get_arg(ctx
, ctx
->args
->ac
.frag_pos
[i
]));
2915 if (G_0286CC_POS_W_FLOAT_ENA(ctx
->program
->config
->spi_ps_input_ena
)) {
2916 assert(num_components
== 4);
2917 Builder
bld(ctx
->program
, ctx
->block
);
2918 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->ac
.frag_pos
[3]));
2921 for (Operand
& op
: vec
->operands
)
2922 op
= op
.isUndefined() ? Operand(0u) : op
;
2924 vec
->definitions
[0] = Definition(dst
);
2925 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2926 emit_split_vector(ctx
, dst
, num_components
);
2930 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2932 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2933 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2934 unsigned idx
= nir_intrinsic_base(instr
);
2935 unsigned component
= nir_intrinsic_component(instr
);
2936 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
2938 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2940 assert(offset
->u32
== 0);
2942 /* the lower 15bit of the prim_mask contain the offset into LDS
2943 * while the upper bits contain the number of prims */
2944 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
2945 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2946 Builder
bld(ctx
->program
, ctx
->block
);
2947 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2948 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2949 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2950 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2951 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2954 if (instr
->dest
.ssa
.num_components
== 1) {
2955 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
2957 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
2958 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
2960 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2961 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
2962 vec
->operands
[i
] = Operand(tmp
);
2964 vec
->definitions
[0] = Definition(dst
);
2965 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2969 unsigned get_num_channels_from_data_format(unsigned data_format
)
2971 switch (data_format
) {
2972 case V_008F0C_BUF_DATA_FORMAT_8
:
2973 case V_008F0C_BUF_DATA_FORMAT_16
:
2974 case V_008F0C_BUF_DATA_FORMAT_32
:
2976 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2977 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2978 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2980 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2981 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2982 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2984 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2985 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2986 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2987 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2988 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2997 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2998 * so we may need to fix it up. */
2999 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
3001 Builder
bld(ctx
->program
, ctx
->block
);
3003 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3004 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3006 /* For the integer-like cases, do a natural sign extension.
3008 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3009 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3012 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3013 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3015 /* Convert back to the right type. */
3016 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3017 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3018 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0xbf800000u
), alpha
);
3019 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3020 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3021 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3027 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3029 Builder
bld(ctx
->program
, ctx
->block
);
3030 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3031 if (ctx
->stage
& sw_vs
) {
3033 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3034 if (off_instr
->type
!= nir_instr_type_load_const
) {
3035 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3036 nir_print_instr(off_instr
, stderr
);
3037 fprintf(stderr
, "\n");
3039 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3041 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->vertex_buffers
));
3043 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3044 unsigned component
= nir_intrinsic_component(instr
);
3045 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3046 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3047 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3048 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3050 unsigned dfmt
= attrib_format
& 0xf;
3052 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3053 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
3054 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3055 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
3056 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3057 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3059 num_channels
= MAX2(num_channels
, 3);
3061 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
3064 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3065 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3066 Temp start_instance
= get_arg(ctx
, ctx
->args
->ac
.start_instance
);
3068 ctx
->needs_instance_id
= true;
3069 Temp instance_id
= get_arg(ctx
, ctx
->args
->ac
.instance_id
);
3071 Temp divided
= bld
.tmp(v1
);
3072 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, instance_id
), divisor
);
3073 index
= bld
.vadd32(bld
.def(v1
), start_instance
, divided
);
3075 index
= bld
.vadd32(bld
.def(v1
), start_instance
, instance_id
);
3078 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), start_instance
);
3081 index
= bld
.vadd32(bld
.def(v1
),
3082 get_arg(ctx
, ctx
->args
->ac
.base_vertex
),
3083 get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
3086 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
3087 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
3088 attrib_offset
= attrib_offset
% attrib_stride
;
3091 Operand
soffset(0u);
3092 if (attrib_offset
>= 4096) {
3093 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
3098 switch (num_channels
) {
3100 opcode
= aco_opcode::tbuffer_load_format_x
;
3103 opcode
= aco_opcode::tbuffer_load_format_xy
;
3106 opcode
= aco_opcode::tbuffer_load_format_xyz
;
3109 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
3112 unreachable("Unimplemented load_input vector size");
3115 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
3117 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
3118 mubuf
->operands
[0] = Operand(index
);
3119 mubuf
->operands
[1] = Operand(list
);
3120 mubuf
->operands
[2] = soffset
;
3121 mubuf
->definitions
[0] = Definition(tmp
);
3122 mubuf
->idxen
= true;
3123 mubuf
->can_reorder
= true;
3126 assert(attrib_offset
< 4096);
3127 mubuf
->offset
= attrib_offset
;
3128 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3130 emit_split_vector(ctx
, tmp
, tmp
.size());
3132 if (tmp
.id() != dst
.id()) {
3133 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3134 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3136 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3137 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3138 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3140 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3141 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3142 unsigned idx
= i
+ component
;
3143 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
3144 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
3145 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
3146 } else if (idx
< num_channels
) {
3147 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
3148 } else if (is_float
&& idx
== 3) {
3149 vec
->operands
[i
] = Operand(0x3f800000u
);
3150 } else if (!is_float
&& idx
== 3) {
3151 vec
->operands
[i
] = Operand(1u);
3153 vec
->operands
[i
] = Operand(0u);
3156 vec
->definitions
[0] = Definition(dst
);
3157 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3158 emit_split_vector(ctx
, dst
, dst
.size());
3161 } else if (ctx
->stage
== fragment_fs
) {
3162 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3163 if (off_instr
->type
!= nir_instr_type_load_const
||
3164 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3165 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3166 nir_print_instr(off_instr
, stderr
);
3167 fprintf(stderr
, "\n");
3170 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
3171 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
3173 assert(offset
->u32
== 0);
3175 /* the lower 15bit of the prim_mask contain the offset into LDS
3176 * while the upper bits contain the number of prims */
3177 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3178 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3179 Builder
bld(ctx
->program
, ctx
->block
);
3180 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3181 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3182 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3183 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3184 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3187 unsigned idx
= nir_intrinsic_base(instr
);
3188 unsigned component
= nir_intrinsic_component(instr
);
3190 if (dst
.size() == 1) {
3191 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
3193 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3194 for (unsigned i
= 0; i
< dst
.size(); i
++)
3195 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
3196 vec
->definitions
[0] = Definition(dst
);
3197 bld
.insert(std::move(vec
));
3201 unreachable("Shader stage not implemented");
3205 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3207 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3208 Builder
bld(ctx
->program
, ctx
->block
);
3209 Temp ptr64
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->descriptor_sets
[0]));
3210 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
3213 return get_arg(ctx
, ctx
->args
->descriptor_sets
[desc_set
]);
3217 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3219 Builder
bld(ctx
->program
, ctx
->block
);
3220 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3221 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3222 index
= bld
.as_uniform(index
);
3223 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3224 unsigned binding
= nir_intrinsic_binding(instr
);
3227 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3228 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3229 unsigned offset
= layout
->binding
[binding
].offset
;
3231 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3232 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3233 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3234 desc_ptr
= get_arg(ctx
, ctx
->args
->ac
.push_constants
);
3235 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3238 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3239 stride
= layout
->binding
[binding
].size
;
3242 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3243 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3245 if (nir_const_index
) {
3246 const_index
= const_index
* stride
;
3247 } else if (index
.type() == RegType::vgpr
) {
3248 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3249 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3251 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3255 if (nir_const_index
) {
3256 const_index
= const_index
+ offset
;
3257 } else if (index
.type() == RegType::vgpr
) {
3258 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
3260 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
3264 if (nir_const_index
&& const_index
== 0) {
3266 } else if (index
.type() == RegType::vgpr
) {
3267 index
= bld
.vadd32(bld
.def(v1
),
3268 nir_const_index
? Operand(const_index
) : Operand(index
),
3271 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3272 nir_const_index
? Operand(const_index
) : Operand(index
),
3276 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
3279 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
,
3280 Temp rsrc
, Temp offset
, bool glc
=false, bool readonly
=true)
3282 Builder
bld(ctx
->program
, ctx
->block
);
3284 unsigned num_bytes
= dst
.size() * 4;
3285 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
3288 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
3289 if (ctx
->options
->chip_class
< GFX8
)
3290 offset
= as_vgpr(ctx
, offset
);
3292 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3293 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3294 unsigned const_offset
= 0;
3296 Temp lower
= Temp();
3297 if (num_bytes
> 16) {
3298 assert(num_components
== 3 || num_components
== 4);
3299 op
= aco_opcode::buffer_load_dwordx4
;
3300 lower
= bld
.tmp(v4
);
3301 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3302 mubuf
->definitions
[0] = Definition(lower
);
3303 mubuf
->operands
[0] = vaddr
;
3304 mubuf
->operands
[1] = Operand(rsrc
);
3305 mubuf
->operands
[2] = soffset
;
3306 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3309 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3310 mubuf
->can_reorder
= readonly
;
3311 bld
.insert(std::move(mubuf
));
3312 emit_split_vector(ctx
, lower
, 2);
3317 switch (num_bytes
) {
3319 op
= aco_opcode::buffer_load_dword
;
3322 op
= aco_opcode::buffer_load_dwordx2
;
3325 op
= aco_opcode::buffer_load_dwordx3
;
3328 op
= aco_opcode::buffer_load_dwordx4
;
3331 unreachable("Load SSBO not implemented for this size.");
3333 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3334 mubuf
->operands
[0] = vaddr
;
3335 mubuf
->operands
[1] = Operand(rsrc
);
3336 mubuf
->operands
[2] = soffset
;
3337 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3340 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3341 mubuf
->can_reorder
= readonly
;
3342 mubuf
->offset
= const_offset
;
3343 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3345 if (dst
.size() > 4) {
3346 assert(lower
!= Temp());
3347 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3348 instr
->definitions
[0] = Definition(upper
);
3349 bld
.insert(std::move(instr
));
3350 if (dst
.size() == 8)
3351 emit_split_vector(ctx
, upper
, 2);
3352 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3353 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3354 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3355 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3356 if (dst
.size() == 8)
3357 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3360 if (dst
.type() == RegType::sgpr
) {
3361 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3362 instr
->definitions
[0] = Definition(vec
);
3363 bld
.insert(std::move(instr
));
3364 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3366 instr
->definitions
[0] = Definition(dst
);
3367 bld
.insert(std::move(instr
));
3370 switch (num_bytes
) {
3372 op
= aco_opcode::s_buffer_load_dword
;
3375 op
= aco_opcode::s_buffer_load_dwordx2
;
3379 op
= aco_opcode::s_buffer_load_dwordx4
;
3383 op
= aco_opcode::s_buffer_load_dwordx8
;
3386 unreachable("Load SSBO not implemented for this size.");
3388 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3389 load
->operands
[0] = Operand(rsrc
);
3390 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3391 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3392 load
->definitions
[0] = Definition(dst
);
3395 load
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3396 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3397 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3400 if (dst
.size() == 3) {
3401 Temp vec
= bld
.tmp(s4
);
3402 load
->definitions
[0] = Definition(vec
);
3403 bld
.insert(std::move(load
));
3404 emit_split_vector(ctx
, vec
, 4);
3406 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3407 emit_extract_vector(ctx
, vec
, 0, s1
),
3408 emit_extract_vector(ctx
, vec
, 1, s1
),
3409 emit_extract_vector(ctx
, vec
, 2, s1
));
3410 } else if (dst
.size() == 6) {
3411 Temp vec
= bld
.tmp(s8
);
3412 load
->definitions
[0] = Definition(vec
);
3413 bld
.insert(std::move(load
));
3414 emit_split_vector(ctx
, vec
, 4);
3416 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3417 emit_extract_vector(ctx
, vec
, 0, s2
),
3418 emit_extract_vector(ctx
, vec
, 1, s2
),
3419 emit_extract_vector(ctx
, vec
, 2, s2
));
3421 bld
.insert(std::move(load
));
3425 emit_split_vector(ctx
, dst
, num_components
);
3428 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3430 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3431 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3433 Builder
bld(ctx
->program
, ctx
->block
);
3435 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3436 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3437 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3438 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3440 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3441 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3442 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3443 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3444 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3445 if (ctx
->options
->chip_class
>= GFX10
) {
3446 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3447 S_008F0C_OOB_SELECT(3) |
3448 S_008F0C_RESOURCE_LEVEL(1);
3450 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3451 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3453 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3454 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3455 Operand(0xFFFFFFFFu
),
3456 Operand(desc_type
));
3457 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3458 rsrc
, upper_dwords
);
3460 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3461 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3464 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3467 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3469 Builder
bld(ctx
->program
, ctx
->block
);
3470 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3472 unsigned offset
= nir_intrinsic_base(instr
);
3473 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3474 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3476 unsigned count
= instr
->dest
.ssa
.num_components
;
3477 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3478 start
-= ctx
->args
->ac
.base_inline_push_consts
;
3479 if (start
+ count
<= ctx
->args
->ac
.num_inline_push_consts
) {
3480 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3481 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3482 for (unsigned i
= 0; i
< count
; ++i
) {
3483 elems
[i
] = get_arg(ctx
, ctx
->args
->ac
.inline_push_consts
[start
+ i
]);
3484 vec
->operands
[i
] = Operand
{elems
[i
]};
3486 vec
->definitions
[0] = Definition(dst
);
3487 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3488 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3493 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3494 if (offset
!= 0) // TODO check if index != 0 as well
3495 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3496 Temp ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->ac
.push_constants
));
3501 switch (dst
.size()) {
3503 op
= aco_opcode::s_load_dword
;
3506 op
= aco_opcode::s_load_dwordx2
;
3512 op
= aco_opcode::s_load_dwordx4
;
3518 op
= aco_opcode::s_load_dwordx8
;
3521 unreachable("unimplemented or forbidden load_push_constant.");
3524 bld
.smem(op
, Definition(vec
), ptr
, index
);
3527 emit_split_vector(ctx
, vec
, 4);
3528 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3529 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3530 emit_extract_vector(ctx
, vec
, 0, rc
),
3531 emit_extract_vector(ctx
, vec
, 1, rc
),
3532 emit_extract_vector(ctx
, vec
, 2, rc
));
3535 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3538 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3540 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3542 Builder
bld(ctx
->program
, ctx
->block
);
3544 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3545 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3546 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3547 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3548 if (ctx
->options
->chip_class
>= GFX10
) {
3549 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3550 S_008F0C_OOB_SELECT(3) |
3551 S_008F0C_RESOURCE_LEVEL(1);
3553 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3554 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3557 unsigned base
= nir_intrinsic_base(instr
);
3558 unsigned range
= nir_intrinsic_range(instr
);
3560 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3561 if (base
&& offset
.type() == RegType::sgpr
)
3562 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3563 else if (base
&& offset
.type() == RegType::vgpr
)
3564 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3566 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3567 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
3568 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
3569 Operand(desc_type
));
3571 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3574 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3576 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3577 ctx
->cf_info
.exec_potentially_empty
= true;
3579 ctx
->program
->needs_exact
= true;
3581 // TODO: optimize uniform conditions
3582 Builder
bld(ctx
->program
, ctx
->block
);
3583 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3584 assert(src
.regClass() == s2
);
3585 src
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
3586 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3587 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3591 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3593 Builder
bld(ctx
->program
, ctx
->block
);
3595 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3596 ctx
->cf_info
.exec_potentially_empty
= true;
3598 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3599 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3601 if (ctx
->block
->loop_nest_depth
&&
3602 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3603 /* we handle discards the same way as jump instructions */
3604 append_logical_end(ctx
->block
);
3606 /* in loops, discard behaves like break */
3607 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3608 ctx
->block
->kind
|= block_kind_discard
;
3611 /* uniform discard - loop ends here */
3612 assert(nir_instr_is_last(&instr
->instr
));
3613 ctx
->block
->kind
|= block_kind_uniform
;
3614 ctx
->cf_info
.has_branch
= true;
3615 bld
.branch(aco_opcode::p_branch
);
3616 add_linear_edge(ctx
->block
->index
, linear_target
);
3620 /* we add a break right behind the discard() instructions */
3621 ctx
->block
->kind
|= block_kind_break
;
3622 unsigned idx
= ctx
->block
->index
;
3624 /* remove critical edges from linear CFG */
3625 bld
.branch(aco_opcode::p_branch
);
3626 Block
* break_block
= ctx
->program
->create_and_insert_block();
3627 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3628 break_block
->kind
|= block_kind_uniform
;
3629 add_linear_edge(idx
, break_block
);
3630 add_linear_edge(break_block
->index
, linear_target
);
3631 bld
.reset(break_block
);
3632 bld
.branch(aco_opcode::p_branch
);
3634 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3635 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3636 add_linear_edge(idx
, continue_block
);
3637 append_logical_start(continue_block
);
3638 ctx
->block
= continue_block
;
3643 /* it can currently happen that NIR doesn't remove the unreachable code */
3644 if (!nir_instr_is_last(&instr
->instr
)) {
3645 ctx
->program
->needs_exact
= true;
3646 /* save exec somewhere temporarily so that it doesn't get
3647 * overwritten before the discard from outer exec masks */
3648 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, s2
));
3649 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3650 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3654 /* This condition is incorrect for uniformly branched discards in a loop
3655 * predicated by a divergent condition, but the above code catches that case
3656 * and the discard would end up turning into a discard_if.
3666 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3667 /* program just ends here */
3668 ctx
->block
->kind
|= block_kind_uniform
;
3669 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3670 0 /* enabled mask */, 9 /* dest */,
3671 false /* compressed */, true/* done */, true /* valid mask */);
3672 bld
.sopp(aco_opcode::s_endpgm
);
3673 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3675 ctx
->block
->kind
|= block_kind_discard
;
3676 /* branch and linear edge is added by visit_if() */
3680 enum aco_descriptor_type
{
3691 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3692 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3694 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
3695 return dim
== ac_image_cube
||
3696 dim
== ac_image_1darray
||
3697 dim
== ac_image_2darray
||
3698 dim
== ac_image_2darraymsaa
;
3701 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3702 enum aco_descriptor_type desc_type
,
3703 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3705 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3706 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3707 if (it != ctx->tex_desc.end())
3710 Temp index
= Temp();
3711 bool index_set
= false;
3712 unsigned constant_index
= 0;
3713 unsigned descriptor_set
;
3714 unsigned base_index
;
3715 Builder
bld(ctx
->program
, ctx
->block
);
3718 assert(tex_instr
&& !image
);
3720 base_index
= tex_instr
->sampler_index
;
3722 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3723 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3727 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3728 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3730 constant_index
+= array_size
* const_value
->u32
;
3732 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
3733 if (indirect
.type() == RegType::vgpr
)
3734 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
3736 if (array_size
!= 1)
3737 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3743 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3747 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3749 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3750 base_index
= deref_instr
->var
->data
.binding
;
3753 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3754 list
= convert_pointer_to_64_bit(ctx
, list
);
3756 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3757 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3758 unsigned offset
= binding
->offset
;
3759 unsigned stride
= binding
->size
;
3763 assert(base_index
< layout
->binding_count
);
3765 switch (desc_type
) {
3766 case ACO_DESC_IMAGE
:
3768 opcode
= aco_opcode::s_load_dwordx8
;
3770 case ACO_DESC_FMASK
:
3772 opcode
= aco_opcode::s_load_dwordx8
;
3775 case ACO_DESC_SAMPLER
:
3777 opcode
= aco_opcode::s_load_dwordx4
;
3778 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3779 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3781 case ACO_DESC_BUFFER
:
3783 opcode
= aco_opcode::s_load_dwordx4
;
3785 case ACO_DESC_PLANE_0
:
3786 case ACO_DESC_PLANE_1
:
3788 opcode
= aco_opcode::s_load_dwordx8
;
3789 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3791 case ACO_DESC_PLANE_2
:
3793 opcode
= aco_opcode::s_load_dwordx4
;
3797 unreachable("invalid desc_type\n");
3800 offset
+= constant_index
* stride
;
3802 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3803 (!index_set
|| binding
->immutable_samplers_equal
)) {
3804 if (binding
->immutable_samplers_equal
)
3807 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3808 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3809 Operand(samplers
[constant_index
* 4 + 0]),
3810 Operand(samplers
[constant_index
* 4 + 1]),
3811 Operand(samplers
[constant_index
* 4 + 2]),
3812 Operand(samplers
[constant_index
* 4 + 3]));
3817 off
= Operand(offset
);
3819 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3820 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3823 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3825 if (desc_type
== ACO_DESC_PLANE_2
) {
3827 for (unsigned i
= 0; i
< 8; i
++)
3828 components
[i
] = bld
.tmp(s1
);
3829 bld
.pseudo(aco_opcode::p_split_vector
,
3830 Definition(components
[0]),
3831 Definition(components
[1]),
3832 Definition(components
[2]),
3833 Definition(components
[3]),
3836 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3837 bld
.pseudo(aco_opcode::p_split_vector
,
3838 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3839 Definition(components
[4]),
3840 Definition(components
[5]),
3841 Definition(components
[6]),
3842 Definition(components
[7]),
3845 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3846 components
[0], components
[1], components
[2], components
[3],
3847 components
[4], components
[5], components
[6], components
[7]);
3853 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3856 case GLSL_SAMPLER_DIM_BUF
:
3858 case GLSL_SAMPLER_DIM_1D
:
3859 return array
? 2 : 1;
3860 case GLSL_SAMPLER_DIM_2D
:
3861 return array
? 3 : 2;
3862 case GLSL_SAMPLER_DIM_MS
:
3863 return array
? 4 : 3;
3864 case GLSL_SAMPLER_DIM_3D
:
3865 case GLSL_SAMPLER_DIM_CUBE
:
3867 case GLSL_SAMPLER_DIM_RECT
:
3868 case GLSL_SAMPLER_DIM_SUBPASS
:
3870 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3879 /* Adjust the sample index according to FMASK.
3881 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3882 * which is the identity mapping. Each nibble says which physical sample
3883 * should be fetched to get that sample.
3885 * For example, 0x11111100 means there are only 2 samples stored and
3886 * the second sample covers 3/4 of the pixel. When reading samples 0
3887 * and 1, return physical sample 0 (determined by the first two 0s
3888 * in FMASK), otherwise return physical sample 1.
3890 * The sample index should be adjusted as follows:
3891 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3893 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3895 Builder
bld(ctx
->program
, ctx
->block
);
3896 Temp fmask
= bld
.tmp(v1
);
3897 unsigned dim
= ctx
->options
->chip_class
>= GFX10
3898 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
3901 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3902 load
->operands
[0] = Operand(coords
);
3903 load
->operands
[1] = Operand(fmask_desc_ptr
);
3904 load
->definitions
[0] = Definition(fmask
);
3911 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3912 ctx
->block
->instructions
.emplace_back(std::move(load
));
3914 Operand sample_index4
;
3915 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3916 sample_index4
= Operand(sample_index
.constantValue() << 2);
3917 } else if (sample_index
.regClass() == s1
) {
3918 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3920 assert(sample_index
.regClass() == v1
);
3921 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3925 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3926 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3927 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3928 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3930 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3932 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3933 * resource descriptor is 0 (invalid),
3935 Temp compare
= bld
.tmp(s2
);
3936 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3937 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3939 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3941 /* Replace the MSAA sample index. */
3942 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
3945 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
3948 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3949 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3950 bool is_array
= glsl_sampler_type_is_array(type
);
3951 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3952 assert(!add_frag_pos
&& "Input attachments should be lowered.");
3953 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3954 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3955 int count
= image_type_to_components_count(dim
, is_array
);
3956 std::vector
<Operand
> coords(count
);
3959 Operand sample_index
;
3960 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
3962 sample_index
= Operand(sample_cv
->u32
);
3964 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
3966 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
3967 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
3968 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
3969 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3970 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
3971 vec
->definitions
[0] = Definition(fmask_load_address
);
3972 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3974 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
3975 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
3978 coords
[count
] = sample_index
;
3981 if (count
== 1 && !gfx9_1d
)
3982 return emit_extract_vector(ctx
, src0
, 0, v1
);
3985 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
3986 coords
.resize(coords
.size() + 1);
3987 coords
[1] = Operand((uint32_t) 0);
3989 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
3991 for (int i
= 0; i
< count
; i
++)
3992 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3995 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
3996 for (unsigned i
= 0; i
< coords
.size(); i
++)
3997 vec
->operands
[i
] = coords
[i
];
3998 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
3999 vec
->definitions
[0] = Definition(res
);
4000 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4005 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4007 Builder
bld(ctx
->program
, ctx
->block
);
4008 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4009 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4010 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4011 bool is_array
= glsl_sampler_type_is_array(type
);
4012 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4014 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4015 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4016 unsigned num_channels
= util_last_bit(mask
);
4017 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4018 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4021 switch (num_channels
) {
4023 opcode
= aco_opcode::buffer_load_format_x
;
4026 opcode
= aco_opcode::buffer_load_format_xy
;
4029 opcode
= aco_opcode::buffer_load_format_xyz
;
4032 opcode
= aco_opcode::buffer_load_format_xyzw
;
4035 unreachable(">4 channel buffer image load");
4037 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4038 load
->operands
[0] = Operand(vindex
);
4039 load
->operands
[1] = Operand(rsrc
);
4040 load
->operands
[2] = Operand((uint32_t) 0);
4042 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4045 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4046 load
->definitions
[0] = Definition(tmp
);
4048 load
->barrier
= barrier_image
;
4049 ctx
->block
->instructions
.emplace_back(std::move(load
));
4051 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4055 Temp coords
= get_image_coords(ctx
, instr
, type
);
4056 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4058 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4059 unsigned num_components
= util_bitcount(dmask
);
4061 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4064 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4066 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
4067 load
->operands
[0] = Operand(coords
);
4068 load
->operands
[1] = Operand(resource
);
4069 load
->definitions
[0] = Definition(tmp
);
4070 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4071 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4072 load
->dmask
= dmask
;
4074 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4075 load
->barrier
= barrier_image
;
4076 ctx
->block
->instructions
.emplace_back(std::move(load
));
4078 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4082 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4084 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4085 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4086 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4087 bool is_array
= glsl_sampler_type_is_array(type
);
4088 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4090 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4092 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4093 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4094 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4096 switch (data
.size()) {
4098 opcode
= aco_opcode::buffer_store_format_x
;
4101 opcode
= aco_opcode::buffer_store_format_xy
;
4104 opcode
= aco_opcode::buffer_store_format_xyz
;
4107 opcode
= aco_opcode::buffer_store_format_xyzw
;
4110 unreachable(">4 channel buffer image store");
4112 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4113 store
->operands
[0] = Operand(vindex
);
4114 store
->operands
[1] = Operand(rsrc
);
4115 store
->operands
[2] = Operand((uint32_t) 0);
4116 store
->operands
[3] = Operand(data
);
4117 store
->idxen
= true;
4120 store
->disable_wqm
= true;
4121 store
->barrier
= barrier_image
;
4122 ctx
->program
->needs_exact
= true;
4123 ctx
->block
->instructions
.emplace_back(std::move(store
));
4127 assert(data
.type() == RegType::vgpr
);
4128 Temp coords
= get_image_coords(ctx
, instr
, type
);
4129 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4131 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
4132 store
->operands
[0] = Operand(coords
);
4133 store
->operands
[1] = Operand(resource
);
4134 store
->operands
[2] = Operand(s4
);
4135 store
->operands
[3] = Operand(data
);
4138 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4139 store
->dmask
= (1 << data
.size()) - 1;
4141 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4142 store
->disable_wqm
= true;
4143 store
->barrier
= barrier_image
;
4144 ctx
->program
->needs_exact
= true;
4145 ctx
->block
->instructions
.emplace_back(std::move(store
));
4149 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4151 /* return the previous value if dest is ever used */
4152 bool return_previous
= false;
4153 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4154 return_previous
= true;
4157 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4158 return_previous
= true;
4162 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4163 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4164 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4165 bool is_array
= glsl_sampler_type_is_array(type
);
4166 Builder
bld(ctx
->program
, ctx
->block
);
4168 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4169 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4171 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4172 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4174 aco_opcode buf_op
, image_op
;
4175 switch (instr
->intrinsic
) {
4176 case nir_intrinsic_image_deref_atomic_add
:
4177 buf_op
= aco_opcode::buffer_atomic_add
;
4178 image_op
= aco_opcode::image_atomic_add
;
4180 case nir_intrinsic_image_deref_atomic_umin
:
4181 buf_op
= aco_opcode::buffer_atomic_umin
;
4182 image_op
= aco_opcode::image_atomic_umin
;
4184 case nir_intrinsic_image_deref_atomic_imin
:
4185 buf_op
= aco_opcode::buffer_atomic_smin
;
4186 image_op
= aco_opcode::image_atomic_smin
;
4188 case nir_intrinsic_image_deref_atomic_umax
:
4189 buf_op
= aco_opcode::buffer_atomic_umax
;
4190 image_op
= aco_opcode::image_atomic_umax
;
4192 case nir_intrinsic_image_deref_atomic_imax
:
4193 buf_op
= aco_opcode::buffer_atomic_smax
;
4194 image_op
= aco_opcode::image_atomic_smax
;
4196 case nir_intrinsic_image_deref_atomic_and
:
4197 buf_op
= aco_opcode::buffer_atomic_and
;
4198 image_op
= aco_opcode::image_atomic_and
;
4200 case nir_intrinsic_image_deref_atomic_or
:
4201 buf_op
= aco_opcode::buffer_atomic_or
;
4202 image_op
= aco_opcode::image_atomic_or
;
4204 case nir_intrinsic_image_deref_atomic_xor
:
4205 buf_op
= aco_opcode::buffer_atomic_xor
;
4206 image_op
= aco_opcode::image_atomic_xor
;
4208 case nir_intrinsic_image_deref_atomic_exchange
:
4209 buf_op
= aco_opcode::buffer_atomic_swap
;
4210 image_op
= aco_opcode::image_atomic_swap
;
4212 case nir_intrinsic_image_deref_atomic_comp_swap
:
4213 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4214 image_op
= aco_opcode::image_atomic_cmpswap
;
4217 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4220 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4222 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4223 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4224 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4225 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4226 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4227 mubuf
->operands
[0] = Operand(vindex
);
4228 mubuf
->operands
[1] = Operand(resource
);
4229 mubuf
->operands
[2] = Operand((uint32_t)0);
4230 mubuf
->operands
[3] = Operand(data
);
4231 if (return_previous
)
4232 mubuf
->definitions
[0] = Definition(dst
);
4234 mubuf
->idxen
= true;
4235 mubuf
->glc
= return_previous
;
4236 mubuf
->dlc
= false; /* Not needed for atomics */
4237 mubuf
->disable_wqm
= true;
4238 mubuf
->barrier
= barrier_image
;
4239 ctx
->program
->needs_exact
= true;
4240 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4244 Temp coords
= get_image_coords(ctx
, instr
, type
);
4245 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4246 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
4247 mimg
->operands
[0] = Operand(coords
);
4248 mimg
->operands
[1] = Operand(resource
);
4249 mimg
->operands
[2] = Operand(s4
); /* no sampler */
4250 mimg
->operands
[3] = Operand(data
);
4251 if (return_previous
)
4252 mimg
->definitions
[0] = Definition(dst
);
4253 mimg
->glc
= return_previous
;
4254 mimg
->dlc
= false; /* Not needed for atomics */
4255 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4256 mimg
->dmask
= (1 << data
.size()) - 1;
4258 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4259 mimg
->disable_wqm
= true;
4260 mimg
->barrier
= barrier_image
;
4261 ctx
->program
->needs_exact
= true;
4262 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4266 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4268 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4269 Builder
bld(ctx
->program
, ctx
->block
);
4271 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4272 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4273 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
4274 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
4276 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4277 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
4279 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4280 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4281 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4283 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4285 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4286 * in case 12 (or 3?), we have to divide by 3:
4287 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4288 * use v_mul_hi_u32 with magic number to divide
4289 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4291 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4295 emit_extract_vector(ctx
, desc
, 2, dst
);
4299 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4301 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4302 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4303 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4304 bool is_array
= glsl_sampler_type_is_array(type
);
4305 Builder
bld(ctx
->program
, ctx
->block
);
4307 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4308 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4309 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4313 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4316 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4318 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4320 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4321 mimg
->operands
[0] = Operand(lod
);
4322 mimg
->operands
[1] = Operand(resource
);
4323 unsigned& dmask
= mimg
->dmask
;
4324 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4325 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4326 mimg
->da
= glsl_sampler_type_is_array(type
);
4327 mimg
->can_reorder
= true;
4328 Definition
& def
= mimg
->definitions
[0];
4329 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4331 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4332 glsl_sampler_type_is_array(type
)) {
4334 assert(instr
->dest
.ssa
.num_components
== 3);
4335 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4336 def
= Definition(tmp
);
4337 emit_split_vector(ctx
, tmp
, 3);
4339 /* divide 3rd value by 6 by multiplying with magic number */
4340 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4341 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4343 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4344 emit_extract_vector(ctx
, tmp
, 0, v1
),
4345 emit_extract_vector(ctx
, tmp
, 1, v1
),
4348 } else if (ctx
->options
->chip_class
== GFX9
&&
4349 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4350 glsl_sampler_type_is_array(type
)) {
4351 assert(instr
->dest
.ssa
.num_components
== 2);
4352 def
= Definition(dst
);
4355 def
= Definition(dst
);
4358 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4361 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4363 Builder
bld(ctx
->program
, ctx
->block
);
4364 unsigned num_components
= instr
->num_components
;
4366 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4367 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4368 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4370 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4371 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
, false);
4374 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4376 Builder
bld(ctx
->program
, ctx
->block
);
4377 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4378 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4379 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4382 if (ctx
->options
->chip_class
< GFX8
)
4383 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4385 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4387 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4388 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4390 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4391 ctx
->options
->chip_class
>= GFX8
;
4393 offset
= bld
.as_uniform(offset
);
4394 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4398 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4399 if (count
== 3 && smem
) {
4400 writemask
|= 1u << (start
+ 2);
4403 int num_bytes
= count
* elem_size_bytes
;
4405 if (num_bytes
> 16) {
4406 assert(elem_size_bytes
== 8);
4407 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4412 // TODO: check alignment of sub-dword stores
4413 // TODO: split 3 bytes. there is no store instruction for that
4416 if (count
!= instr
->num_components
) {
4417 emit_split_vector(ctx
, data
, instr
->num_components
);
4418 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4419 for (int i
= 0; i
< count
; i
++) {
4420 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4421 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4423 write_data
= bld
.tmp(smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4424 vec
->definitions
[0] = Definition(write_data
);
4425 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4426 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4427 assert(num_bytes
% 4 == 0);
4428 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4429 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4430 assert(num_bytes
% 4 == 0);
4431 write_data
= bld
.as_uniform(data
);
4436 aco_opcode vmem_op
, smem_op
;
4437 switch (num_bytes
) {
4439 vmem_op
= aco_opcode::buffer_store_dword
;
4440 smem_op
= aco_opcode::s_buffer_store_dword
;
4443 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4444 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4447 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4448 smem_op
= aco_opcode::last_opcode
;
4452 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4453 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4456 unreachable("Store SSBO not implemented for this size.");
4458 if (ctx
->stage
== fragment_fs
)
4459 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4462 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4463 store
->operands
[0] = Operand(rsrc
);
4465 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4466 offset
, Operand(start
* elem_size_bytes
));
4467 store
->operands
[1] = Operand(off
);
4469 store
->operands
[1] = Operand(offset
);
4471 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4472 store
->operands
[1].setFixed(m0
);
4473 store
->operands
[2] = Operand(write_data
);
4474 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4476 store
->disable_wqm
= true;
4477 store
->barrier
= barrier_buffer
;
4478 ctx
->block
->instructions
.emplace_back(std::move(store
));
4479 ctx
->program
->wb_smem_l1_on_end
= true;
4480 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4481 ctx
->block
->kind
|= block_kind_needs_lowering
;
4482 ctx
->program
->needs_exact
= true;
4485 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4486 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4487 store
->operands
[1] = Operand(rsrc
);
4488 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4489 store
->operands
[3] = Operand(write_data
);
4490 store
->offset
= start
* elem_size_bytes
;
4491 store
->offen
= (offset
.type() == RegType::vgpr
);
4492 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4494 store
->disable_wqm
= true;
4495 store
->barrier
= barrier_buffer
;
4496 ctx
->program
->needs_exact
= true;
4497 ctx
->block
->instructions
.emplace_back(std::move(store
));
4502 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4504 /* return the previous value if dest is ever used */
4505 bool return_previous
= false;
4506 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4507 return_previous
= true;
4510 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4511 return_previous
= true;
4515 Builder
bld(ctx
->program
, ctx
->block
);
4516 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4518 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4519 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4520 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4523 if (ctx
->options
->chip_class
< GFX8
)
4524 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4526 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4528 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4529 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4531 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4533 aco_opcode op32
, op64
;
4534 switch (instr
->intrinsic
) {
4535 case nir_intrinsic_ssbo_atomic_add
:
4536 op32
= aco_opcode::buffer_atomic_add
;
4537 op64
= aco_opcode::buffer_atomic_add_x2
;
4539 case nir_intrinsic_ssbo_atomic_imin
:
4540 op32
= aco_opcode::buffer_atomic_smin
;
4541 op64
= aco_opcode::buffer_atomic_smin_x2
;
4543 case nir_intrinsic_ssbo_atomic_umin
:
4544 op32
= aco_opcode::buffer_atomic_umin
;
4545 op64
= aco_opcode::buffer_atomic_umin_x2
;
4547 case nir_intrinsic_ssbo_atomic_imax
:
4548 op32
= aco_opcode::buffer_atomic_smax
;
4549 op64
= aco_opcode::buffer_atomic_smax_x2
;
4551 case nir_intrinsic_ssbo_atomic_umax
:
4552 op32
= aco_opcode::buffer_atomic_umax
;
4553 op64
= aco_opcode::buffer_atomic_umax_x2
;
4555 case nir_intrinsic_ssbo_atomic_and
:
4556 op32
= aco_opcode::buffer_atomic_and
;
4557 op64
= aco_opcode::buffer_atomic_and_x2
;
4559 case nir_intrinsic_ssbo_atomic_or
:
4560 op32
= aco_opcode::buffer_atomic_or
;
4561 op64
= aco_opcode::buffer_atomic_or_x2
;
4563 case nir_intrinsic_ssbo_atomic_xor
:
4564 op32
= aco_opcode::buffer_atomic_xor
;
4565 op64
= aco_opcode::buffer_atomic_xor_x2
;
4567 case nir_intrinsic_ssbo_atomic_exchange
:
4568 op32
= aco_opcode::buffer_atomic_swap
;
4569 op64
= aco_opcode::buffer_atomic_swap_x2
;
4571 case nir_intrinsic_ssbo_atomic_comp_swap
:
4572 op32
= aco_opcode::buffer_atomic_cmpswap
;
4573 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4576 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4578 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4579 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4580 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4581 mubuf
->operands
[1] = Operand(rsrc
);
4582 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4583 mubuf
->operands
[3] = Operand(data
);
4584 if (return_previous
)
4585 mubuf
->definitions
[0] = Definition(dst
);
4587 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4588 mubuf
->glc
= return_previous
;
4589 mubuf
->dlc
= false; /* Not needed for atomics */
4590 mubuf
->disable_wqm
= true;
4591 mubuf
->barrier
= barrier_buffer
;
4592 ctx
->program
->needs_exact
= true;
4593 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4596 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4598 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4599 Builder
bld(ctx
->program
, ctx
->block
);
4600 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4601 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4604 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4606 Builder
bld(ctx
->program
, ctx
->block
);
4607 unsigned num_components
= instr
->num_components
;
4608 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4610 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4611 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4613 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4614 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4616 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4617 bool global
= ctx
->options
->chip_class
>= GFX9
;
4619 switch (num_bytes
) {
4621 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4624 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4627 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4630 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4633 unreachable("load_global not implemented for this size.");
4635 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4636 flat
->operands
[0] = Operand(addr
);
4637 flat
->operands
[1] = Operand(s1
);
4641 if (dst
.type() == RegType::sgpr
) {
4642 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4643 flat
->definitions
[0] = Definition(vec
);
4644 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4645 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4647 flat
->definitions
[0] = Definition(dst
);
4648 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4650 emit_split_vector(ctx
, dst
, num_components
);
4652 switch (num_bytes
) {
4654 op
= aco_opcode::s_load_dword
;
4657 op
= aco_opcode::s_load_dwordx2
;
4661 op
= aco_opcode::s_load_dwordx4
;
4664 unreachable("load_global not implemented for this size.");
4666 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4667 load
->operands
[0] = Operand(addr
);
4668 load
->operands
[1] = Operand(0u);
4669 load
->definitions
[0] = Definition(dst
);
4672 load
->barrier
= barrier_buffer
;
4673 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4675 if (dst
.size() == 3) {
4677 Temp vec
= bld
.tmp(s4
);
4678 load
->definitions
[0] = Definition(vec
);
4679 ctx
->block
->instructions
.emplace_back(std::move(load
));
4680 emit_split_vector(ctx
, vec
, 4);
4682 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4683 emit_extract_vector(ctx
, vec
, 0, s1
),
4684 emit_extract_vector(ctx
, vec
, 1, s1
),
4685 emit_extract_vector(ctx
, vec
, 2, s1
));
4687 ctx
->block
->instructions
.emplace_back(std::move(load
));
4692 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4694 Builder
bld(ctx
->program
, ctx
->block
);
4695 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4697 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4698 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4700 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4703 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4704 unsigned num_bytes
= count
* elem_size_bytes
;
4706 Temp write_data
= data
;
4707 if (count
!= instr
->num_components
) {
4708 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4709 for (int i
= 0; i
< count
; i
++)
4710 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4711 write_data
= bld
.tmp(RegType::vgpr
, count
);
4712 vec
->definitions
[0] = Definition(write_data
);
4713 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4716 unsigned offset
= start
* elem_size_bytes
;
4717 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4718 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4719 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4720 Temp carry
= bld
.tmp(s2
);
4721 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4723 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4724 Operand(offset
), addr0
);
4725 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(s2
),
4727 carry
).def(1).setHint(vcc
);
4729 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4734 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4735 bool global
= ctx
->options
->chip_class
>= GFX9
;
4737 switch (num_bytes
) {
4739 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4742 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4745 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4748 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4751 unreachable("store_global not implemented for this size.");
4753 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4754 flat
->operands
[0] = Operand(addr
);
4755 flat
->operands
[1] = Operand(s1
);
4756 flat
->operands
[2] = Operand(data
);
4759 flat
->offset
= offset
;
4760 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4764 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4765 Builder
bld(ctx
->program
, ctx
->block
);
4766 switch(instr
->intrinsic
) {
4767 case nir_intrinsic_group_memory_barrier
:
4768 case nir_intrinsic_memory_barrier
:
4769 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4771 case nir_intrinsic_memory_barrier_atomic_counter
:
4772 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4774 case nir_intrinsic_memory_barrier_buffer
:
4775 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4777 case nir_intrinsic_memory_barrier_image
:
4778 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4780 case nir_intrinsic_memory_barrier_shared
:
4781 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4784 unreachable("Unimplemented memory barrier intrinsic");
4789 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4791 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4792 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4793 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4794 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4795 Builder
bld(ctx
->program
, ctx
->block
);
4797 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4798 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4799 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
4802 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4804 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4805 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4806 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4807 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4808 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4810 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4811 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
4814 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4816 unsigned offset
= nir_intrinsic_base(instr
);
4817 Operand m
= load_lds_size_m0(ctx
);
4818 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4819 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4821 unsigned num_operands
= 3;
4822 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4823 switch(instr
->intrinsic
) {
4824 case nir_intrinsic_shared_atomic_add
:
4825 op32
= aco_opcode::ds_add_u32
;
4826 op64
= aco_opcode::ds_add_u64
;
4827 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4828 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4830 case nir_intrinsic_shared_atomic_imin
:
4831 op32
= aco_opcode::ds_min_i32
;
4832 op64
= aco_opcode::ds_min_i64
;
4833 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4834 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4836 case nir_intrinsic_shared_atomic_umin
:
4837 op32
= aco_opcode::ds_min_u32
;
4838 op64
= aco_opcode::ds_min_u64
;
4839 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4840 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4842 case nir_intrinsic_shared_atomic_imax
:
4843 op32
= aco_opcode::ds_max_i32
;
4844 op64
= aco_opcode::ds_max_i64
;
4845 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4846 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4848 case nir_intrinsic_shared_atomic_umax
:
4849 op32
= aco_opcode::ds_max_u32
;
4850 op64
= aco_opcode::ds_max_u64
;
4851 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
4852 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
4854 case nir_intrinsic_shared_atomic_and
:
4855 op32
= aco_opcode::ds_and_b32
;
4856 op64
= aco_opcode::ds_and_b64
;
4857 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
4858 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
4860 case nir_intrinsic_shared_atomic_or
:
4861 op32
= aco_opcode::ds_or_b32
;
4862 op64
= aco_opcode::ds_or_b64
;
4863 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
4864 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
4866 case nir_intrinsic_shared_atomic_xor
:
4867 op32
= aco_opcode::ds_xor_b32
;
4868 op64
= aco_opcode::ds_xor_b64
;
4869 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
4870 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
4872 case nir_intrinsic_shared_atomic_exchange
:
4873 op32
= aco_opcode::ds_write_b32
;
4874 op64
= aco_opcode::ds_write_b64
;
4875 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
4876 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
4878 case nir_intrinsic_shared_atomic_comp_swap
:
4879 op32
= aco_opcode::ds_cmpst_b32
;
4880 op64
= aco_opcode::ds_cmpst_b64
;
4881 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
4882 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
4886 unreachable("Unhandled shared atomic intrinsic");
4889 /* return the previous value if dest is ever used */
4890 bool return_previous
= false;
4891 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4892 return_previous
= true;
4895 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4896 return_previous
= true;
4901 if (data
.size() == 1) {
4902 assert(instr
->dest
.ssa
.bit_size
== 32);
4903 op
= return_previous
? op32_rtn
: op32
;
4905 assert(instr
->dest
.ssa
.bit_size
== 64);
4906 op
= return_previous
? op64_rtn
: op64
;
4909 if (offset
> 65535) {
4910 Builder
bld(ctx
->program
, ctx
->block
);
4911 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
4915 aco_ptr
<DS_instruction
> ds
;
4916 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
4917 ds
->operands
[0] = Operand(address
);
4918 ds
->operands
[1] = Operand(data
);
4919 if (num_operands
== 4)
4920 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4921 ds
->operands
[num_operands
- 1] = m
;
4922 ds
->offset0
= offset
;
4923 if (return_previous
)
4924 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
4925 ctx
->block
->instructions
.emplace_back(std::move(ds
));
4928 Temp
get_scratch_resource(isel_context
*ctx
)
4930 Builder
bld(ctx
->program
, ctx
->block
);
4931 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
4932 if (ctx
->stage
!= compute_cs
)
4933 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
4935 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
4936 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
4938 if (ctx
->program
->chip_class
>= GFX10
) {
4939 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
4940 S_008F0C_OOB_SELECT(3) |
4941 S_008F0C_RESOURCE_LEVEL(1);
4942 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
4943 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4944 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4947 /* older generations need element size = 16 bytes. element size removed in GFX9 */
4948 if (ctx
->program
->chip_class
<= GFX8
)
4949 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
4951 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4954 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4955 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
4956 Builder
bld(ctx
->program
, ctx
->block
);
4957 Temp rsrc
= get_scratch_resource(ctx
);
4958 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4959 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4962 switch (dst
.size()) {
4964 op
= aco_opcode::buffer_load_dword
;
4967 op
= aco_opcode::buffer_load_dwordx2
;
4970 op
= aco_opcode::buffer_load_dwordx3
;
4973 op
= aco_opcode::buffer_load_dwordx4
;
4977 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4978 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
4979 bld
.def(v4
), offset
, rsrc
,
4980 ctx
->program
->scratch_offset
, 0, true);
4981 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
4982 aco_opcode::buffer_load_dwordx4
,
4983 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
4984 offset
, rsrc
, ctx
->program
->scratch_offset
, 16, true);
4985 emit_split_vector(ctx
, lower
, 2);
4986 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
4987 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
4988 if (dst
.size() == 8) {
4989 emit_split_vector(ctx
, upper
, 2);
4990 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
4991 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
4996 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
4997 Format::PSEUDO
, dst
.size() / 2, 1)};
4998 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
4999 vec
->operands
[i
] = Operand(elems
[i
]);
5000 vec
->definitions
[0] = Definition(dst
);
5001 bld
.insert(std::move(vec
));
5002 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5006 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5009 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->program
->scratch_offset
, 0, true);
5010 emit_split_vector(ctx
, dst
, instr
->num_components
);
5013 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5014 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
5015 Builder
bld(ctx
->program
, ctx
->block
);
5016 Temp rsrc
= get_scratch_resource(ctx
);
5017 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5018 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5020 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5021 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5025 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5026 int num_bytes
= count
* elem_size_bytes
;
5028 if (num_bytes
> 16) {
5029 assert(elem_size_bytes
== 8);
5030 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5035 // TODO: check alignment of sub-dword stores
5036 // TODO: split 3 bytes. there is no store instruction for that
5039 if (count
!= instr
->num_components
) {
5040 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5041 for (int i
= 0; i
< count
; i
++) {
5042 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
5043 vec
->operands
[i
] = Operand(elem
);
5045 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
5046 vec
->definitions
[0] = Definition(write_data
);
5047 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5053 switch (num_bytes
) {
5055 op
= aco_opcode::buffer_store_dword
;
5058 op
= aco_opcode::buffer_store_dwordx2
;
5061 op
= aco_opcode::buffer_store_dwordx3
;
5064 op
= aco_opcode::buffer_store_dwordx4
;
5067 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5070 bld
.mubuf(op
, offset
, rsrc
, ctx
->program
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
5074 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5075 uint8_t log2_ps_iter_samples
;
5076 if (ctx
->program
->info
->ps
.force_persample
) {
5077 log2_ps_iter_samples
=
5078 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
5080 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
5083 /* The bit pattern matches that used by fixed function fragment
5085 static const unsigned ps_iter_masks
[] = {
5086 0xffff, /* not used */
5092 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
5094 Builder
bld(ctx
->program
, ctx
->block
);
5096 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
5097 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
5098 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
5099 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
5100 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5101 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, get_arg(ctx
, ctx
->args
->ac
.sample_coverage
));
5104 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
5106 Builder
bld(ctx
->program
, ctx
->block
);
5108 if (cluster_size
== 1) {
5110 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5111 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5112 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5113 return bld
.sop1(aco_opcode::s_not_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5114 bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
));
5115 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5116 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5117 return bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5118 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)));
5119 } else if (op
== nir_op_iand
&& cluster_size
== 64) {
5120 //subgroupAnd(val) -> (exec & ~val) == 0
5121 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5122 return bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), Operand(0u), Operand(-1u), bld
.scc(tmp
));
5123 } else if (op
== nir_op_ior
&& cluster_size
== 64) {
5124 //subgroupOr(val) -> (val & exec) != 0
5125 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)).def(1).getTemp();
5126 return bool_to_vector_condition(ctx
, tmp
);
5127 } else if (op
== nir_op_ixor
&& cluster_size
== 64) {
5128 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5129 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5130 tmp
= bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
);
5131 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5132 return bool_to_vector_condition(ctx
, tmp
);
5134 //subgroupClustered{And,Or,Xor}(val, n) ->
5135 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0))
5136 //cluster_offset = ~(n - 1) & lane_id
5137 //cluster_mask = ((1 << n) - 1)
5138 //subgroupClusteredAnd():
5139 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5140 //subgroupClusteredOr():
5141 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5142 //subgroupClusteredXor():
5143 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5144 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5145 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5146 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5149 if (op
== nir_op_iand
)
5150 tmp
= bld
.sop2(aco_opcode::s_orn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5152 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5154 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5155 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5156 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5157 if (cluster_mask
!= 0xffffffff)
5158 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5160 Definition cmp_def
= Definition();
5161 if (op
== nir_op_iand
) {
5162 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(cluster_mask
), tmp
).def(0);
5163 } else if (op
== nir_op_ior
) {
5164 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5165 } else if (op
== nir_op_ixor
) {
5166 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5167 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5168 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5170 cmp_def
.setHint(vcc
);
5171 return cmp_def
.getTemp();
5175 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5177 Builder
bld(ctx
->program
, ctx
->block
);
5179 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5180 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5181 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5183 if (op
== nir_op_iand
)
5184 tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5186 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5188 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5189 Temp lo
= lohi
.def(0).getTemp();
5190 Temp hi
= lohi
.def(1).getTemp();
5191 Temp mbcnt
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), hi
,
5192 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), lo
, Operand(0u)));
5194 Definition cmp_def
= Definition();
5195 if (op
== nir_op_iand
)
5196 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5197 else if (op
== nir_op_ior
)
5198 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5199 else if (op
== nir_op_ixor
)
5200 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u),
5201 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5202 cmp_def
.setHint(vcc
);
5203 return cmp_def
.getTemp();
5206 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5208 Builder
bld(ctx
->program
, ctx
->block
);
5210 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5211 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5212 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5213 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5214 if (op
== nir_op_iand
)
5215 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5216 else if (op
== nir_op_ior
)
5217 return bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5218 else if (op
== nir_op_ixor
)
5219 return bld
.sop2(aco_opcode::s_xor_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5225 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5227 Builder
bld(ctx
->program
, ctx
->block
);
5228 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5229 if (src
.regClass().type() == RegType::vgpr
) {
5230 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5231 } else if (src
.regClass() == s1
) {
5232 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5233 } else if (src
.regClass() == s2
) {
5234 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5236 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5237 nir_print_instr(&instr
->instr
, stderr
);
5238 fprintf(stderr
, "\n");
5242 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5244 Builder
bld(ctx
->program
, ctx
->block
);
5245 Temp persp_center
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
5246 Temp p1
= emit_extract_vector(ctx
, persp_center
, 0, v1
);
5247 Temp p2
= emit_extract_vector(ctx
, persp_center
, 1, v1
);
5250 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_quad_perm(0, 0, 0, 0));
5251 Temp ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(1, 1, 1, 1));
5252 Temp ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(2, 2, 2, 2));
5253 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_quad_perm(0, 0, 0, 0));
5254 Temp ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(1, 1, 1, 1));
5255 Temp ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(2, 2, 2, 2));
5257 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5258 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5259 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5260 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5261 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5262 Temp wqm1
= bld
.tmp(v1
);
5263 emit_wqm(ctx
, tmp1
, wqm1
, true);
5264 Temp wqm2
= bld
.tmp(v1
);
5265 emit_wqm(ctx
, tmp2
, wqm2
, true);
5266 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5270 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5272 Builder
bld(ctx
->program
, ctx
->block
);
5273 switch(instr
->intrinsic
) {
5274 case nir_intrinsic_load_barycentric_sample
:
5275 case nir_intrinsic_load_barycentric_pixel
:
5276 case nir_intrinsic_load_barycentric_centroid
: {
5277 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5278 Temp bary
= Temp(0, s2
);
5280 case INTERP_MODE_SMOOTH
:
5281 case INTERP_MODE_NONE
:
5282 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
5283 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
5284 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
5285 bary
= ctx
->persp_centroid
;
5286 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
5287 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_sample
);
5289 case INTERP_MODE_NOPERSPECTIVE
:
5290 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
5291 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_center
);
5292 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
5293 bary
= ctx
->linear_centroid
;
5294 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
5295 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_sample
);
5300 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5301 Temp p1
= emit_extract_vector(ctx
, bary
, 0, v1
);
5302 Temp p2
= emit_extract_vector(ctx
, bary
, 1, v1
);
5303 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5304 Operand(p1
), Operand(p2
));
5305 emit_split_vector(ctx
, dst
, 2);
5308 case nir_intrinsic_load_barycentric_at_sample
: {
5309 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5310 switch (ctx
->options
->key
.fs
.num_samples
) {
5311 case 2: sample_pos_offset
+= 1 << 3; break;
5312 case 4: sample_pos_offset
+= 3 << 3; break;
5313 case 8: sample_pos_offset
+= 7 << 3; break;
5317 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5318 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5319 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
5320 if (addr
.type() == RegType::sgpr
) {
5323 sample_pos_offset
+= const_addr
->u32
<< 3;
5324 offset
= Operand(sample_pos_offset
);
5325 } else if (ctx
->options
->chip_class
>= GFX9
) {
5326 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5328 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5329 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5331 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, Operand(offset
));
5333 } else if (ctx
->options
->chip_class
>= GFX9
) {
5334 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5335 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
5337 /* addr += private_segment_buffer + sample_pos_offset */
5338 Temp tmp0
= bld
.tmp(s1
);
5339 Temp tmp1
= bld
.tmp(s1
);
5340 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
5341 Definition scc_tmp
= bld
.def(s1
, scc
);
5342 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5343 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
5344 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5345 Temp pck0
= bld
.tmp(v1
);
5346 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5347 tmp1
= as_vgpr(ctx
, tmp1
);
5348 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(s2
)), tmp1
, Operand(0u), carry
);
5349 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5351 /* sample_pos = flat_load_dwordx2 addr */
5352 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5355 /* sample_pos -= 0.5 */
5356 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5357 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5358 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5359 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5360 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5362 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5365 case nir_intrinsic_load_barycentric_at_offset
: {
5366 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5367 RegClass rc
= RegClass(offset
.type(), 1);
5368 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5369 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5370 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5373 case nir_intrinsic_load_front_face
: {
5374 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5375 Operand(0u), get_arg(ctx
, ctx
->args
->ac
.front_face
)).def(0).setHint(vcc
);
5378 case nir_intrinsic_load_view_index
:
5379 case nir_intrinsic_load_layer_id
: {
5380 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5381 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5382 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.view_index
)));
5386 unsigned idx
= nir_intrinsic_base(instr
);
5387 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5388 Operand(2u), bld
.m0(get_arg(ctx
, ctx
->args
->ac
.prim_mask
)), idx
, 0);
5391 case nir_intrinsic_load_frag_coord
: {
5392 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5395 case nir_intrinsic_load_sample_pos
: {
5396 Temp posx
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[0]);
5397 Temp posy
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[1]);
5398 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5399 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5400 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5403 case nir_intrinsic_load_interpolated_input
:
5404 visit_load_interpolated_input(ctx
, instr
);
5406 case nir_intrinsic_store_output
:
5407 visit_store_output(ctx
, instr
);
5409 case nir_intrinsic_load_input
:
5410 visit_load_input(ctx
, instr
);
5412 case nir_intrinsic_load_ubo
:
5413 visit_load_ubo(ctx
, instr
);
5415 case nir_intrinsic_load_push_constant
:
5416 visit_load_push_constant(ctx
, instr
);
5418 case nir_intrinsic_load_constant
:
5419 visit_load_constant(ctx
, instr
);
5421 case nir_intrinsic_vulkan_resource_index
:
5422 visit_load_resource(ctx
, instr
);
5424 case nir_intrinsic_discard
:
5425 visit_discard(ctx
, instr
);
5427 case nir_intrinsic_discard_if
:
5428 visit_discard_if(ctx
, instr
);
5430 case nir_intrinsic_load_shared
:
5431 visit_load_shared(ctx
, instr
);
5433 case nir_intrinsic_store_shared
:
5434 visit_store_shared(ctx
, instr
);
5436 case nir_intrinsic_shared_atomic_add
:
5437 case nir_intrinsic_shared_atomic_imin
:
5438 case nir_intrinsic_shared_atomic_umin
:
5439 case nir_intrinsic_shared_atomic_imax
:
5440 case nir_intrinsic_shared_atomic_umax
:
5441 case nir_intrinsic_shared_atomic_and
:
5442 case nir_intrinsic_shared_atomic_or
:
5443 case nir_intrinsic_shared_atomic_xor
:
5444 case nir_intrinsic_shared_atomic_exchange
:
5445 case nir_intrinsic_shared_atomic_comp_swap
:
5446 visit_shared_atomic(ctx
, instr
);
5448 case nir_intrinsic_image_deref_load
:
5449 visit_image_load(ctx
, instr
);
5451 case nir_intrinsic_image_deref_store
:
5452 visit_image_store(ctx
, instr
);
5454 case nir_intrinsic_image_deref_atomic_add
:
5455 case nir_intrinsic_image_deref_atomic_umin
:
5456 case nir_intrinsic_image_deref_atomic_imin
:
5457 case nir_intrinsic_image_deref_atomic_umax
:
5458 case nir_intrinsic_image_deref_atomic_imax
:
5459 case nir_intrinsic_image_deref_atomic_and
:
5460 case nir_intrinsic_image_deref_atomic_or
:
5461 case nir_intrinsic_image_deref_atomic_xor
:
5462 case nir_intrinsic_image_deref_atomic_exchange
:
5463 case nir_intrinsic_image_deref_atomic_comp_swap
:
5464 visit_image_atomic(ctx
, instr
);
5466 case nir_intrinsic_image_deref_size
:
5467 visit_image_size(ctx
, instr
);
5469 case nir_intrinsic_load_ssbo
:
5470 visit_load_ssbo(ctx
, instr
);
5472 case nir_intrinsic_store_ssbo
:
5473 visit_store_ssbo(ctx
, instr
);
5475 case nir_intrinsic_load_global
:
5476 visit_load_global(ctx
, instr
);
5478 case nir_intrinsic_store_global
:
5479 visit_store_global(ctx
, instr
);
5481 case nir_intrinsic_ssbo_atomic_add
:
5482 case nir_intrinsic_ssbo_atomic_imin
:
5483 case nir_intrinsic_ssbo_atomic_umin
:
5484 case nir_intrinsic_ssbo_atomic_imax
:
5485 case nir_intrinsic_ssbo_atomic_umax
:
5486 case nir_intrinsic_ssbo_atomic_and
:
5487 case nir_intrinsic_ssbo_atomic_or
:
5488 case nir_intrinsic_ssbo_atomic_xor
:
5489 case nir_intrinsic_ssbo_atomic_exchange
:
5490 case nir_intrinsic_ssbo_atomic_comp_swap
:
5491 visit_atomic_ssbo(ctx
, instr
);
5493 case nir_intrinsic_load_scratch
:
5494 visit_load_scratch(ctx
, instr
);
5496 case nir_intrinsic_store_scratch
:
5497 visit_store_scratch(ctx
, instr
);
5499 case nir_intrinsic_get_buffer_size
:
5500 visit_get_buffer_size(ctx
, instr
);
5502 case nir_intrinsic_barrier
: {
5503 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5504 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5505 if (workgroup_size
> 64)
5506 bld
.sopp(aco_opcode::s_barrier
);
5509 case nir_intrinsic_group_memory_barrier
:
5510 case nir_intrinsic_memory_barrier
:
5511 case nir_intrinsic_memory_barrier_atomic_counter
:
5512 case nir_intrinsic_memory_barrier_buffer
:
5513 case nir_intrinsic_memory_barrier_image
:
5514 case nir_intrinsic_memory_barrier_shared
:
5515 emit_memory_barrier(ctx
, instr
);
5517 case nir_intrinsic_load_num_work_groups
: {
5518 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5519 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.num_work_groups
)));
5520 emit_split_vector(ctx
, dst
, 3);
5523 case nir_intrinsic_load_local_invocation_id
: {
5524 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5525 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.local_invocation_ids
)));
5526 emit_split_vector(ctx
, dst
, 3);
5529 case nir_intrinsic_load_work_group_id
: {
5530 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5531 struct ac_arg
*args
= ctx
->args
->ac
.workgroup_ids
;
5532 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5533 args
[0].used
? Operand(get_arg(ctx
, args
[0])) : Operand(1u),
5534 args
[1].used
? Operand(get_arg(ctx
, args
[1])) : Operand(1u),
5535 args
[2].used
? Operand(get_arg(ctx
, args
[2])) : Operand(1u));
5536 emit_split_vector(ctx
, dst
, 3);
5539 case nir_intrinsic_load_local_invocation_index
: {
5540 Temp id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5541 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5542 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
5543 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
5544 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5547 case nir_intrinsic_load_subgroup_id
: {
5548 if (ctx
->stage
== compute_cs
) {
5549 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
5550 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
5551 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), tg_num
, Operand(0x6u
));
5553 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5557 case nir_intrinsic_load_subgroup_invocation
: {
5558 bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand((uint32_t) -1),
5559 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5562 case nir_intrinsic_load_num_subgroups
: {
5563 if (ctx
->stage
== compute_cs
)
5564 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
),
5565 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
5567 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5570 case nir_intrinsic_ballot
: {
5571 Definition tmp
= bld
.def(s2
);
5572 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5573 if (instr
->src
[0].ssa
->bit_size
== 1) {
5574 assert(src
.regClass() == s2
);
5575 bld
.sop2(aco_opcode::s_and_b64
, tmp
, bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5576 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5577 bld
.vopc(aco_opcode::v_cmp_lg_u32
, tmp
, Operand(0u), src
);
5578 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5579 bld
.vopc(aco_opcode::v_cmp_lg_u64
, tmp
, Operand(0u), src
);
5581 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5582 nir_print_instr(&instr
->instr
, stderr
);
5583 fprintf(stderr
, "\n");
5585 emit_wqm(ctx
, tmp
.getTemp(), get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5588 case nir_intrinsic_shuffle
:
5589 case nir_intrinsic_read_invocation
: {
5590 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5591 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
]) {
5592 emit_uniform_subgroup(ctx
, instr
, src
);
5594 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5595 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !ctx
->divergent_vals
[instr
->src
[1].ssa
->index
])
5596 tid
= bld
.as_uniform(tid
);
5597 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5598 if (src
.regClass() == v1
) {
5599 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
5600 } else if (src
.regClass() == v2
) {
5601 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5602 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5603 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
5604 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
5605 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5606 emit_split_vector(ctx
, dst
, 2);
5607 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
5608 assert(src
.regClass() == s2
);
5609 Temp tmp
= bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
, tid
);
5610 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
5611 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
5612 assert(src
.regClass() == s2
);
5613 Temp tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5614 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5615 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5616 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
), dst
);
5618 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5619 nir_print_instr(&instr
->instr
, stderr
);
5620 fprintf(stderr
, "\n");
5625 case nir_intrinsic_load_sample_id
: {
5626 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5627 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
5630 case nir_intrinsic_load_sample_mask_in
: {
5631 visit_load_sample_mask_in(ctx
, instr
);
5634 case nir_intrinsic_read_first_invocation
: {
5635 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5636 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5637 if (src
.regClass() == v1
) {
5639 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
5641 } else if (src
.regClass() == v2
) {
5642 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5643 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5644 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5645 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
5646 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5647 emit_split_vector(ctx
, dst
, 2);
5648 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5649 assert(src
.regClass() == s2
);
5650 Temp tmp
= bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
,
5651 bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)));
5652 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
5653 } else if (src
.regClass() == s1
) {
5654 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5655 } else if (src
.regClass() == s2
) {
5656 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5658 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5659 nir_print_instr(&instr
->instr
, stderr
);
5660 fprintf(stderr
, "\n");
5664 case nir_intrinsic_vote_all
: {
5665 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5666 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5667 assert(src
.regClass() == s2
);
5668 assert(dst
.regClass() == s2
);
5670 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5671 Temp val
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), Operand(0u), Operand(-1u), bld
.scc(tmp
));
5672 emit_wqm(ctx
, val
, dst
);
5675 case nir_intrinsic_vote_any
: {
5676 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5677 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5678 assert(src
.regClass() == s2
);
5679 assert(dst
.regClass() == s2
);
5681 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5682 Temp val
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), Operand(-1u), Operand(0u), bld
.scc(tmp
));
5683 emit_wqm(ctx
, val
, dst
);
5686 case nir_intrinsic_reduce
:
5687 case nir_intrinsic_inclusive_scan
:
5688 case nir_intrinsic_exclusive_scan
: {
5689 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5690 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5691 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5692 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5693 nir_intrinsic_cluster_size(instr
) : 0;
5694 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: 64, 64));
5696 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5697 emit_uniform_subgroup(ctx
, instr
, src
);
5698 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5699 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5701 else if (op
== nir_op_iadd
)
5703 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5705 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5707 switch (instr
->intrinsic
) {
5708 case nir_intrinsic_reduce
:
5709 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5711 case nir_intrinsic_exclusive_scan
:
5712 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5714 case nir_intrinsic_inclusive_scan
:
5715 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5720 } else if (cluster_size
== 1) {
5721 bld
.copy(Definition(dst
), src
);
5723 src
= as_vgpr(ctx
, src
);
5727 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5742 unreachable("unknown reduction op");
5747 switch (instr
->intrinsic
) {
5748 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5749 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5750 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5752 unreachable("unknown reduce intrinsic");
5755 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5756 reduce
->operands
[0] = Operand(src
);
5757 // filled in by aco_reduce_assign.cpp, used internally as part of the
5759 assert(dst
.size() == 1 || dst
.size() == 2);
5760 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5761 reduce
->operands
[2] = Operand(v1
.as_linear());
5763 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5764 reduce
->definitions
[0] = Definition(tmp_dst
);
5765 reduce
->definitions
[1] = bld
.def(s2
); // used internally
5766 reduce
->definitions
[2] = Definition();
5767 reduce
->definitions
[3] = Definition(scc
, s1
);
5768 reduce
->definitions
[4] = Definition();
5769 reduce
->reduce_op
= reduce_op
;
5770 reduce
->cluster_size
= cluster_size
;
5771 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5773 emit_wqm(ctx
, tmp_dst
, dst
);
5777 case nir_intrinsic_quad_broadcast
: {
5778 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5779 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5780 emit_uniform_subgroup(ctx
, instr
, src
);
5782 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5783 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5784 if (instr
->dest
.ssa
.bit_size
== 1) {
5785 assert(src
.regClass() == s2
);
5786 uint32_t half_mask
= 0x11111111u
<< lane
;
5787 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5788 Temp tmp
= bld
.tmp(s2
);
5789 bld
.sop1(aco_opcode::s_wqm_b64
, Definition(tmp
),
5790 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mask_tmp
,
5791 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
))));
5792 emit_wqm(ctx
, tmp
, dst
);
5793 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5795 bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
,
5796 dpp_quad_perm(lane
, lane
, lane
, lane
)),
5798 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5799 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5800 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5801 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5802 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5803 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5804 emit_split_vector(ctx
, dst
, 2);
5806 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5807 nir_print_instr(&instr
->instr
, stderr
);
5808 fprintf(stderr
, "\n");
5813 case nir_intrinsic_quad_swap_horizontal
:
5814 case nir_intrinsic_quad_swap_vertical
:
5815 case nir_intrinsic_quad_swap_diagonal
:
5816 case nir_intrinsic_quad_swizzle_amd
: {
5817 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5818 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5819 emit_uniform_subgroup(ctx
, instr
, src
);
5822 uint16_t dpp_ctrl
= 0;
5823 switch (instr
->intrinsic
) {
5824 case nir_intrinsic_quad_swap_horizontal
:
5825 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
5827 case nir_intrinsic_quad_swap_vertical
:
5828 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
5830 case nir_intrinsic_quad_swap_diagonal
:
5831 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
5833 case nir_intrinsic_quad_swizzle_amd
: {
5834 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
5841 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5842 if (instr
->dest
.ssa
.bit_size
== 1) {
5843 assert(src
.regClass() == s2
);
5844 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
5845 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5846 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), src
);
5847 emit_wqm(ctx
, tmp
, dst
);
5848 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5849 Temp tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5850 emit_wqm(ctx
, tmp
, dst
);
5851 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5852 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5853 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5854 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
5855 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
5856 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5857 emit_split_vector(ctx
, dst
, 2);
5859 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5860 nir_print_instr(&instr
->instr
, stderr
);
5861 fprintf(stderr
, "\n");
5865 case nir_intrinsic_masked_swizzle_amd
: {
5866 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5867 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5868 emit_uniform_subgroup(ctx
, instr
, src
);
5871 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5872 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
5873 if (dst
.regClass() == v1
) {
5875 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
5877 } else if (dst
.regClass() == v2
) {
5878 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5879 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5880 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
5881 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
5882 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5883 emit_split_vector(ctx
, dst
, 2);
5885 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5886 nir_print_instr(&instr
->instr
, stderr
);
5887 fprintf(stderr
, "\n");
5891 case nir_intrinsic_write_invocation_amd
: {
5892 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5893 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5894 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5895 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5896 if (dst
.regClass() == v1
) {
5897 /* src2 is ignored for writelane. RA assigns the same reg for dst */
5898 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val
, lane
, src
), dst
);
5899 } else if (dst
.regClass() == v2
) {
5900 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
5901 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
5902 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
5903 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
5904 Temp lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_lo
, lane
, src_hi
));
5905 Temp hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_hi
, lane
, src_hi
));
5906 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5907 emit_split_vector(ctx
, dst
, 2);
5909 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5910 nir_print_instr(&instr
->instr
, stderr
);
5911 fprintf(stderr
, "\n");
5915 case nir_intrinsic_mbcnt_amd
: {
5916 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5917 RegClass rc
= RegClass(src
.type(), 1);
5918 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
5919 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
5920 Temp tmp
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), mask_lo
, Operand(0u));
5921 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5922 Temp wqm_tmp
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), mask_hi
, tmp
);
5923 emit_wqm(ctx
, wqm_tmp
, dst
);
5926 case nir_intrinsic_load_helper_invocation
: {
5927 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5928 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
5929 ctx
->block
->kind
|= block_kind_needs_lowering
;
5930 ctx
->program
->needs_exact
= true;
5933 case nir_intrinsic_is_helper_invocation
: {
5934 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5935 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
5936 ctx
->block
->kind
|= block_kind_needs_lowering
;
5937 ctx
->program
->needs_exact
= true;
5940 case nir_intrinsic_demote
:
5941 bld
.pseudo(aco_opcode::p_demote_to_helper
);
5942 ctx
->block
->kind
|= block_kind_uses_demote
;
5943 ctx
->program
->needs_exact
= true;
5945 case nir_intrinsic_demote_if
: {
5946 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5947 assert(src
.regClass() == s2
);
5948 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5949 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
5950 ctx
->block
->kind
|= block_kind_uses_demote
;
5951 ctx
->program
->needs_exact
= true;
5954 case nir_intrinsic_first_invocation
: {
5955 emit_wqm(ctx
, bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)),
5956 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5959 case nir_intrinsic_shader_clock
:
5960 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
5961 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
5963 case nir_intrinsic_load_vertex_id_zero_base
: {
5964 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5965 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
5968 case nir_intrinsic_load_first_vertex
: {
5969 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5970 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.base_vertex
));
5973 case nir_intrinsic_load_base_instance
: {
5974 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5975 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.start_instance
));
5978 case nir_intrinsic_load_instance_id
: {
5979 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5980 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.instance_id
));
5983 case nir_intrinsic_load_draw_id
: {
5984 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5985 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.draw_id
));
5989 fprintf(stderr
, "Unimplemented intrinsic instr: ");
5990 nir_print_instr(&instr
->instr
, stderr
);
5991 fprintf(stderr
, "\n");
5999 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
6000 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
6001 enum glsl_base_type
*stype
)
6003 nir_deref_instr
*texture_deref_instr
= NULL
;
6004 nir_deref_instr
*sampler_deref_instr
= NULL
;
6007 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6008 switch (instr
->src
[i
].src_type
) {
6009 case nir_tex_src_texture_deref
:
6010 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6012 case nir_tex_src_sampler_deref
:
6013 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6015 case nir_tex_src_plane
:
6016 plane
= nir_src_as_int(instr
->src
[i
].src
);
6023 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
6025 if (!sampler_deref_instr
)
6026 sampler_deref_instr
= texture_deref_instr
;
6029 assert(instr
->op
!= nir_texop_txf_ms
&&
6030 instr
->op
!= nir_texop_samples_identical
);
6031 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
6032 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
6033 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6034 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
6036 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
6039 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
6040 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
6041 fprintf(stderr
, "Unimplemented sampler descriptor: ");
6042 nir_print_instr(&instr
->instr
, stderr
);
6043 fprintf(stderr
, "\n");
6045 // TODO: build samp_ptr = and(samp_ptr, res_ptr)
6048 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
6049 instr
->op
== nir_texop_samples_identical
))
6050 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6053 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
6054 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
6056 Builder
bld(ctx
->program
, ctx
->block
);
6058 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
6059 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
6060 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
6062 Operand
neg_one(0xbf800000u
);
6063 Operand
one(0x3f800000u
);
6064 Operand
two(0x40000000u
);
6065 Operand
four(0x40800000u
);
6067 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), ma
);
6068 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
6069 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
6071 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), four
, id
);
6072 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
6073 is_ma_y
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.hint_vcc(bld
.def(s2
)), is_ma_y
, is_ma_z
);
6074 Temp is_not_ma_x
= bld
.sop2(aco_opcode::s_or_b64
, bld
.hint_vcc(bld
.def(s2
)), bld
.def(s1
, scc
), is_ma_z
, is_ma_y
);
6077 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
6078 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6079 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
6081 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6084 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
6085 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
6086 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6089 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6090 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
6092 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
6093 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
6096 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
6098 Builder
bld(ctx
->program
, ctx
->block
);
6099 Temp coord_args
[4], ma
, tc
, sc
, id
;
6100 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
6101 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
6104 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
6106 // see comment in ac_prepare_cube_coords()
6107 if (ctx
->options
->chip_class
<= GFX8
)
6108 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6111 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6113 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6114 vop3a
->operands
[0] = Operand(ma
);
6115 vop3a
->abs
[0] = true;
6116 Temp invma
= bld
.tmp(v1
);
6117 vop3a
->definitions
[0] = Definition(invma
);
6118 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6120 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6122 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6124 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6126 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6128 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6131 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6132 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6134 for (unsigned i
= 0; i
< 2; i
++) {
6135 // see comment in ac_prepare_cube_coords()
6137 Temp deriv_sc
, deriv_tc
;
6138 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6139 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6141 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6143 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6144 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6145 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6146 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6147 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6148 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6149 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6152 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6153 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6157 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6158 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6162 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6165 for (unsigned i
= 0; i
< coords
.size(); i
++)
6166 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6168 Builder
bld(ctx
->program
, ctx
->block
);
6169 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6171 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6172 for (unsigned i
= 0; i
< coords
.size(); i
++)
6173 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6174 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6175 vec
->definitions
[0] = Definition(res
);
6176 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6180 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6182 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6184 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6185 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6188 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6189 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6190 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6194 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6196 Builder
bld(ctx
->program
, ctx
->block
);
6197 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6198 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6199 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6200 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6201 nir_const_value
*sample_index_cv
= NULL
;
6202 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6203 enum glsl_base_type stype
;
6204 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6206 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6207 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6208 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6209 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6211 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6212 switch (instr
->src
[i
].src_type
) {
6213 case nir_tex_src_coord
:
6214 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6216 case nir_tex_src_bias
:
6217 if (instr
->op
== nir_texop_txb
) {
6218 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6222 case nir_tex_src_lod
: {
6223 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6225 if (val
&& val
->f32
<= 0.0) {
6228 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6233 case nir_tex_src_comparator
:
6234 if (instr
->is_shadow
) {
6235 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6239 case nir_tex_src_offset
:
6240 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6241 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6244 case nir_tex_src_ddx
:
6245 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6248 case nir_tex_src_ddy
:
6249 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6252 case nir_tex_src_ms_index
:
6253 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6254 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6255 has_sample_index
= true;
6257 case nir_tex_src_texture_offset
:
6258 case nir_tex_src_sampler_offset
:
6263 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6264 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6265 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6267 if (instr
->op
== nir_texop_texture_samples
) {
6268 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6270 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6271 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6272 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 */));
6273 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6275 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6276 samples
, Operand(1u), bld
.scc(is_msaa
));
6280 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6281 aco_ptr
<Instruction
> tmp_instr
;
6282 Temp acc
, pack
= Temp();
6284 uint32_t pack_const
= 0;
6285 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6286 if (!const_offset
[i
])
6288 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6291 if (offset
.type() == RegType::sgpr
) {
6292 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6293 if (const_offset
[i
])
6296 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6297 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6300 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6303 if (pack
== Temp()) {
6306 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6310 if (pack_const
&& pack
!= Temp())
6311 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6313 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6314 if (const_offset
[i
])
6317 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6318 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6321 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6324 if (pack
== Temp()) {
6327 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6331 if (pack_const
&& pack
!= Temp())
6332 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6334 if (pack_const
&& pack
== Temp())
6335 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6336 else if (pack
== Temp())
6342 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6343 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6345 /* pack derivatives */
6346 if (has_ddx
|| has_ddy
) {
6347 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
6348 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6349 ddx
, Operand(0u), ddy
, Operand(0u));
6351 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6356 if (instr
->coord_components
> 1 &&
6357 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6359 instr
->op
!= nir_texop_txf
)
6360 coords
= apply_round_slice(ctx
, coords
, 1);
6362 if (instr
->coord_components
> 2 &&
6363 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6364 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6365 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6366 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6368 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6369 coords
= apply_round_slice(ctx
, coords
, 2);
6371 if (ctx
->options
->chip_class
== GFX9
&&
6372 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6373 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6374 assert(coords
.size() > 0 && coords
.size() < 3);
6376 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6377 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6378 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6379 if (coords
.size() > 1)
6380 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6381 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6382 vec
->definitions
[0] = Definition(coords
);
6383 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6386 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6388 if (instr
->op
== nir_texop_samples_identical
)
6389 resource
= fmask_ptr
;
6391 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6392 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6393 instr
->op
!= nir_texop_txs
) {
6394 assert(has_sample_index
);
6395 Operand
op(sample_index
);
6396 if (sample_index_cv
)
6397 op
= Operand(sample_index_cv
->u32
);
6398 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6401 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6402 Temp split_coords
[coords
.size()];
6403 emit_split_vector(ctx
, coords
, coords
.size());
6404 for (unsigned i
= 0; i
< coords
.size(); i
++)
6405 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6408 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6409 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6410 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6413 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6414 for (unsigned i
= 0; i
< coords
.size(); i
++)
6415 vec
->operands
[i
] = Operand(split_coords
[i
]);
6416 coords
= bld
.tmp(coords
.regClass());
6417 vec
->definitions
[0] = Definition(coords
);
6418 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6423 /* Build tex instruction */
6424 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6425 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
6426 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
6428 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6431 /* gather4 selects the component by dmask and always returns vec4 */
6432 if (instr
->op
== nir_texop_tg4
) {
6433 assert(instr
->dest
.ssa
.num_components
== 4);
6434 if (instr
->is_shadow
)
6437 dmask
= 1 << instr
->component
;
6438 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6439 tmp_dst
= bld
.tmp(v4
);
6440 } else if (instr
->op
== nir_texop_samples_identical
) {
6441 tmp_dst
= bld
.tmp(v1
);
6442 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6443 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6446 aco_ptr
<MIMG_instruction
> tex
;
6447 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6449 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6451 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6452 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6455 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6456 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6458 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6459 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6460 tex
->operands
[1] = Operand(resource
);
6461 if (ctx
->options
->chip_class
== GFX9
&&
6462 instr
->op
== nir_texop_txs
&&
6463 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6465 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6466 } else if (instr
->op
== nir_texop_query_levels
) {
6467 tex
->dmask
= 1 << 3;
6472 tex
->definitions
[0] = Definition(tmp_dst
);
6474 tex
->can_reorder
= true;
6475 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6478 /* divide 3rd value by 6 by multiplying with magic number */
6479 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6480 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6481 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6482 assert(instr
->dest
.ssa
.num_components
== 3);
6483 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6484 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6485 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6486 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6491 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6495 Temp tg4_compare_cube_wa64
= Temp();
6497 if (tg4_integer_workarounds
) {
6498 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6499 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6500 tex
->operands
[1] = Operand(resource
);
6504 Temp size
= bld
.tmp(v2
);
6505 tex
->definitions
[0] = Definition(size
);
6506 tex
->can_reorder
= true;
6507 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6508 emit_split_vector(ctx
, size
, size
.size());
6511 for (unsigned i
= 0; i
< 2; i
++) {
6512 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6513 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6514 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6515 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6518 Temp orig_coords
[2] = {
6519 emit_extract_vector(ctx
, coords
, 0, v1
),
6520 emit_extract_vector(ctx
, coords
, 1, v1
)};
6521 Temp new_coords
[2] = {
6522 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6523 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6526 if (tg4_integer_cube_workaround
) {
6527 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6528 Temp desc
[resource
.size()];
6529 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6530 Format::PSEUDO
, 1, resource
.size())};
6531 split
->operands
[0] = Operand(resource
);
6532 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6533 desc
[i
] = bld
.tmp(s1
);
6534 split
->definitions
[i
] = Definition(desc
[i
]);
6536 ctx
->block
->instructions
.emplace_back(std::move(split
));
6538 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6539 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6540 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6543 if (stype
== GLSL_TYPE_UINT
) {
6544 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6545 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6546 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6547 bld
.scc(compare_cube_wa
));
6549 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6550 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6551 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6552 bld
.scc(compare_cube_wa
));
6554 tg4_compare_cube_wa64
= bld
.tmp(s2
);
6555 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
6557 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6559 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6560 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6561 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6563 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6564 Format::PSEUDO
, resource
.size(), 1)};
6565 for (unsigned i
= 0; i
< resource
.size(); i
++)
6566 vec
->operands
[i
] = Operand(desc
[i
]);
6567 resource
= bld
.tmp(resource
.regClass());
6568 vec
->definitions
[0] = Definition(resource
);
6569 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6571 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6572 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6573 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6574 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6577 if (coords
.size() == 3) {
6578 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6579 new_coords
[0], new_coords
[1],
6580 emit_extract_vector(ctx
, coords
, 2, v1
));
6582 assert(coords
.size() == 2);
6583 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6584 new_coords
[0], new_coords
[1]);
6588 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6589 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6590 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6591 coords
= emit_wqm(ctx
, coords
, bld
.tmp(coords
.regClass()), true);
6593 std::vector
<Operand
> args
;
6595 args
.emplace_back(Operand(offset
));
6597 args
.emplace_back(Operand(bias
));
6599 args
.emplace_back(Operand(compare
));
6601 args
.emplace_back(Operand(derivs
));
6602 args
.emplace_back(Operand(coords
));
6603 if (has_sample_index
)
6604 args
.emplace_back(Operand(sample_index
));
6606 args
.emplace_back(lod
);
6609 if (args
.size() > 1) {
6610 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6612 for (unsigned i
= 0; i
< args
.size(); i
++) {
6613 size
+= args
[i
].size();
6614 vec
->operands
[i
] = args
[i
];
6616 RegClass rc
= RegClass(RegType::vgpr
, size
);
6617 Temp tmp
= bld
.tmp(rc
);
6618 vec
->definitions
[0] = Definition(tmp
);
6619 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6622 assert(args
[0].isTemp());
6623 arg
= Operand(as_vgpr(ctx
, args
[0].getTemp()));
6626 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6627 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6629 assert(coords
.size() == 1);
6630 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6634 op
= aco_opcode::buffer_load_format_x
; break;
6636 op
= aco_opcode::buffer_load_format_xy
; break;
6638 op
= aco_opcode::buffer_load_format_xyz
; break;
6640 op
= aco_opcode::buffer_load_format_xyzw
; break;
6642 unreachable("Tex instruction loads more than 4 components.");
6645 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6646 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6649 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6651 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6652 mubuf
->operands
[0] = Operand(coords
);
6653 mubuf
->operands
[1] = Operand(resource
);
6654 mubuf
->operands
[2] = Operand((uint32_t) 0);
6655 mubuf
->definitions
[0] = Definition(tmp_dst
);
6656 mubuf
->idxen
= true;
6657 mubuf
->can_reorder
= true;
6658 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6660 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6665 if (instr
->op
== nir_texop_txf
||
6666 instr
->op
== nir_texop_txf_ms
||
6667 instr
->op
== nir_texop_samples_identical
) {
6668 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6669 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6670 tex
->operands
[0] = Operand(arg
);
6671 tex
->operands
[1] = Operand(resource
);
6676 tex
->definitions
[0] = Definition(tmp_dst
);
6677 tex
->can_reorder
= true;
6678 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6680 if (instr
->op
== nir_texop_samples_identical
) {
6681 assert(dmask
== 1 && dst
.regClass() == v1
);
6682 assert(dst
.id() != tmp_dst
.id());
6684 Temp tmp
= bld
.tmp(s2
);
6685 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6686 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6689 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6694 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6695 aco_opcode opcode
= aco_opcode::image_sample
;
6696 if (has_offset
) { /* image_sample_*_o */
6698 opcode
= aco_opcode::image_sample_c_o
;
6700 opcode
= aco_opcode::image_sample_c_d_o
;
6702 opcode
= aco_opcode::image_sample_c_b_o
;
6704 opcode
= aco_opcode::image_sample_c_lz_o
;
6706 opcode
= aco_opcode::image_sample_c_l_o
;
6708 opcode
= aco_opcode::image_sample_o
;
6710 opcode
= aco_opcode::image_sample_d_o
;
6712 opcode
= aco_opcode::image_sample_b_o
;
6714 opcode
= aco_opcode::image_sample_lz_o
;
6716 opcode
= aco_opcode::image_sample_l_o
;
6718 } else { /* no offset */
6720 opcode
= aco_opcode::image_sample_c
;
6722 opcode
= aco_opcode::image_sample_c_d
;
6724 opcode
= aco_opcode::image_sample_c_b
;
6726 opcode
= aco_opcode::image_sample_c_lz
;
6728 opcode
= aco_opcode::image_sample_c_l
;
6730 opcode
= aco_opcode::image_sample
;
6732 opcode
= aco_opcode::image_sample_d
;
6734 opcode
= aco_opcode::image_sample_b
;
6736 opcode
= aco_opcode::image_sample_lz
;
6738 opcode
= aco_opcode::image_sample_l
;
6742 if (instr
->op
== nir_texop_tg4
) {
6744 opcode
= aco_opcode::image_gather4_lz_o
;
6746 opcode
= aco_opcode::image_gather4_c_lz_o
;
6748 opcode
= aco_opcode::image_gather4_lz
;
6750 opcode
= aco_opcode::image_gather4_c_lz
;
6752 } else if (instr
->op
== nir_texop_lod
) {
6753 opcode
= aco_opcode::image_get_lod
;
6756 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6757 tex
->operands
[0] = arg
;
6758 tex
->operands
[1] = Operand(resource
);
6759 tex
->operands
[2] = Operand(sampler
);
6763 tex
->definitions
[0] = Definition(tmp_dst
);
6764 tex
->can_reorder
= true;
6765 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6767 if (tg4_integer_cube_workaround
) {
6768 assert(tmp_dst
.id() != dst
.id());
6769 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
6771 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6773 for (unsigned i
= 0; i
< dst
.size(); i
++) {
6774 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
6776 if (stype
== GLSL_TYPE_UINT
)
6777 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
6779 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
6780 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
6782 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
6783 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6784 val
[0], val
[1], val
[2], val
[3]);
6786 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
6787 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
6792 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
6794 Temp tmp
= get_ssa_temp(ctx
, ssa
);
6795 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
6796 return Operand(tmp
.regClass());
6798 return Operand(tmp
);
6801 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
6803 aco_ptr
<Pseudo_instruction
> phi
;
6804 unsigned num_src
= exec_list_length(&instr
->srcs
);
6805 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6806 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == s2
);
6808 aco_opcode opcode
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
] ? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
6810 std::map
<unsigned, nir_ssa_def
*> phi_src
;
6811 bool all_undef
= true;
6812 nir_foreach_phi_src(src
, instr
) {
6813 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
6814 if (src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_ssa_undef
)
6818 Builder
bld(ctx
->program
, ctx
->block
);
6819 if (dst
.regClass() == s1
) {
6820 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
6821 } else if (dst
.regClass() == v1
) {
6822 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
6824 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6825 for (unsigned i
= 0; i
< dst
.size(); i
++)
6826 vec
->operands
[i
] = Operand(0u);
6827 vec
->definitions
[0] = Definition(dst
);
6828 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6833 /* try to scalarize vector phis */
6834 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
6835 // TODO: scalarize linear phis on divergent ifs
6836 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
6837 std::array
<Temp
, 4> new_vec
;
6838 for (std::pair
<const unsigned, nir_ssa_def
*>& pair
: phi_src
) {
6839 Operand src
= get_phi_operand(ctx
, pair
.second
);
6840 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end()) {
6841 can_scalarize
= false;
6845 if (can_scalarize
) {
6846 unsigned num_components
= instr
->dest
.ssa
.num_components
;
6847 assert(dst
.size() % num_components
== 0);
6848 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
6850 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
6851 for (unsigned k
= 0; k
< num_components
; k
++) {
6852 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
, 1));
6853 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6854 for (unsigned i
= 0; i
< num_src
; i
++) {
6855 Operand src
= get_phi_operand(ctx
, it
->second
);
6856 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
6859 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
6860 phi
->definitions
[0] = Definition(phi_dst
);
6861 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6862 new_vec
[k
] = phi_dst
;
6863 vec
->operands
[k
] = Operand(phi_dst
);
6865 vec
->definitions
[0] = Definition(dst
);
6866 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6867 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
6872 unsigned extra_src
= 0;
6873 if (opcode
== aco_opcode::p_linear_phi
&& (ctx
->block
->kind
& block_kind_loop_exit
) &&
6874 ctx
->program
->blocks
[ctx
->block
->index
-2].kind
& block_kind_continue_or_break
) {
6878 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
+ extra_src
, 1));
6880 /* if we have a linear phi on a divergent if, we know that one src is undef */
6881 if (opcode
== aco_opcode::p_linear_phi
&& ctx
->block
->kind
& block_kind_merge
) {
6882 assert(extra_src
== 0);
6884 /* we place the phi either in the invert-block or in the current block */
6885 if (phi_src
.begin()->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
) {
6886 assert((++phi_src
.begin())->second
->parent_instr
->type
== nir_instr_type_ssa_undef
);
6887 Block
& linear_else
= ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
6888 block
= &ctx
->program
->blocks
[linear_else
.linear_preds
[0]];
6889 assert(block
->kind
& block_kind_invert
);
6890 phi
->operands
[0] = get_phi_operand(ctx
, phi_src
.begin()->second
);
6892 assert((++phi_src
.begin())->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
);
6894 phi
->operands
[0] = get_phi_operand(ctx
, (++phi_src
.begin())->second
);
6896 phi
->operands
[1] = Operand(dst
.regClass());
6897 phi
->definitions
[0] = Definition(dst
);
6898 block
->instructions
.emplace(block
->instructions
.begin(), std::move(phi
));
6902 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6903 for (unsigned i
= 0; i
< num_src
; i
++) {
6904 phi
->operands
[i
] = get_phi_operand(ctx
, it
->second
);
6907 for (unsigned i
= 0; i
< extra_src
; i
++)
6908 phi
->operands
[num_src
+ i
] = Operand(dst
.regClass());
6909 phi
->definitions
[0] = Definition(dst
);
6910 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6914 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
6916 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
6918 assert(dst
.type() == RegType::sgpr
);
6920 if (dst
.size() == 1) {
6921 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
6923 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6924 for (unsigned i
= 0; i
< dst
.size(); i
++)
6925 vec
->operands
[i
] = Operand(0u);
6926 vec
->definitions
[0] = Definition(dst
);
6927 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6931 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
6933 Builder
bld(ctx
->program
, ctx
->block
);
6934 Block
*logical_target
;
6935 append_logical_end(ctx
->block
);
6936 unsigned idx
= ctx
->block
->index
;
6938 switch (instr
->type
) {
6939 case nir_jump_break
:
6940 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
6941 add_logical_edge(idx
, logical_target
);
6942 ctx
->block
->kind
|= block_kind_break
;
6944 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
6945 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
6946 /* uniform break - directly jump out of the loop */
6947 ctx
->block
->kind
|= block_kind_uniform
;
6948 ctx
->cf_info
.has_branch
= true;
6949 bld
.branch(aco_opcode::p_branch
);
6950 add_linear_edge(idx
, logical_target
);
6953 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6955 case nir_jump_continue
:
6956 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6957 add_logical_edge(idx
, logical_target
);
6958 ctx
->block
->kind
|= block_kind_continue
;
6960 if (ctx
->cf_info
.parent_if
.is_divergent
) {
6961 /* for potential uniform breaks after this continue,
6962 we must ensure that they are handled correctly */
6963 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
6964 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6966 /* uniform continue - directly jump to the loop header */
6967 ctx
->block
->kind
|= block_kind_uniform
;
6968 ctx
->cf_info
.has_branch
= true;
6969 bld
.branch(aco_opcode::p_branch
);
6970 add_linear_edge(idx
, logical_target
);
6975 fprintf(stderr
, "Unknown NIR jump instr: ");
6976 nir_print_instr(&instr
->instr
, stderr
);
6977 fprintf(stderr
, "\n");
6981 /* remove critical edges from linear CFG */
6982 bld
.branch(aco_opcode::p_branch
);
6983 Block
* break_block
= ctx
->program
->create_and_insert_block();
6984 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6985 break_block
->kind
|= block_kind_uniform
;
6986 add_linear_edge(idx
, break_block
);
6987 /* the loop_header pointer might be invalidated by this point */
6988 if (instr
->type
== nir_jump_continue
)
6989 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6990 add_linear_edge(break_block
->index
, logical_target
);
6991 bld
.reset(break_block
);
6992 bld
.branch(aco_opcode::p_branch
);
6994 Block
* continue_block
= ctx
->program
->create_and_insert_block();
6995 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6996 add_linear_edge(idx
, continue_block
);
6997 append_logical_start(continue_block
);
6998 ctx
->block
= continue_block
;
7002 void visit_block(isel_context
*ctx
, nir_block
*block
)
7004 nir_foreach_instr(instr
, block
) {
7005 switch (instr
->type
) {
7006 case nir_instr_type_alu
:
7007 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
7009 case nir_instr_type_load_const
:
7010 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
7012 case nir_instr_type_intrinsic
:
7013 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
7015 case nir_instr_type_tex
:
7016 visit_tex(ctx
, nir_instr_as_tex(instr
));
7018 case nir_instr_type_phi
:
7019 visit_phi(ctx
, nir_instr_as_phi(instr
));
7021 case nir_instr_type_ssa_undef
:
7022 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
7024 case nir_instr_type_deref
:
7026 case nir_instr_type_jump
:
7027 visit_jump(ctx
, nir_instr_as_jump(instr
));
7030 fprintf(stderr
, "Unknown NIR instr type: ");
7031 nir_print_instr(instr
, stderr
);
7032 fprintf(stderr
, "\n");
7040 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
7042 append_logical_end(ctx
->block
);
7043 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
7044 Builder
bld(ctx
->program
, ctx
->block
);
7045 bld
.branch(aco_opcode::p_branch
);
7046 unsigned loop_preheader_idx
= ctx
->block
->index
;
7048 Block loop_exit
= Block();
7049 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7050 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
7052 Block
* loop_header
= ctx
->program
->create_and_insert_block();
7053 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
7054 loop_header
->kind
|= block_kind_loop_header
;
7055 add_edge(loop_preheader_idx
, loop_header
);
7056 ctx
->block
= loop_header
;
7058 /* emit loop body */
7059 unsigned loop_header_idx
= loop_header
->index
;
7060 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
7061 append_logical_start(ctx
->block
);
7062 visit_cf_list(ctx
, &loop
->body
);
7064 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7065 if (!ctx
->cf_info
.has_branch
) {
7066 append_logical_end(ctx
->block
);
7067 if (ctx
->cf_info
.exec_potentially_empty
) {
7068 /* Discards can result in code running with an empty exec mask.
7069 * This would result in divergent breaks not ever being taken. As a
7070 * workaround, break the loop when the loop mask is empty instead of
7071 * always continuing. */
7072 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
7073 unsigned block_idx
= ctx
->block
->index
;
7075 /* create helper blocks to avoid critical edges */
7076 Block
*break_block
= ctx
->program
->create_and_insert_block();
7077 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7078 break_block
->kind
= block_kind_uniform
;
7079 bld
.reset(break_block
);
7080 bld
.branch(aco_opcode::p_branch
);
7081 add_linear_edge(block_idx
, break_block
);
7082 add_linear_edge(break_block
->index
, &loop_exit
);
7084 Block
*continue_block
= ctx
->program
->create_and_insert_block();
7085 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7086 continue_block
->kind
= block_kind_uniform
;
7087 bld
.reset(continue_block
);
7088 bld
.branch(aco_opcode::p_branch
);
7089 add_linear_edge(block_idx
, continue_block
);
7090 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7092 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
7093 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
7095 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
7096 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7097 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7099 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7102 bld
.reset(ctx
->block
);
7103 bld
.branch(aco_opcode::p_branch
);
7106 /* fixup phis in loop header from unreachable blocks */
7107 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
7108 bool linear
= ctx
->cf_info
.has_branch
;
7109 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7110 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
7111 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
7112 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
7113 /* the last operand should be the one that needs to be removed */
7114 instr
->operands
.pop_back();
7115 } else if (!is_phi(instr
)) {
7121 ctx
->cf_info
.has_branch
= false;
7123 // TODO: if the loop has not a single exit, we must add one °°
7124 /* emit loop successor block */
7125 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
7126 append_logical_start(ctx
->block
);
7129 // TODO: check if it is beneficial to not branch on continues
7130 /* trim linear phis in loop header */
7131 for (auto&& instr
: loop_entry
->instructions
) {
7132 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
7133 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
7134 new_phi
->definitions
[0] = instr
->definitions
[0];
7135 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
7136 new_phi
->operands
[i
] = instr
->operands
[i
];
7137 /* check that the remaining operands are all the same */
7138 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
7139 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
7140 instr
.swap(new_phi
);
7141 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7150 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7154 append_logical_end(ctx
->block
);
7155 ctx
->block
->kind
|= block_kind_branch
;
7157 /* branch to linear then block */
7158 assert(cond
.regClass() == s2
);
7159 aco_ptr
<Pseudo_branch_instruction
> branch
;
7160 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7161 branch
->operands
[0] = Operand(cond
);
7162 ctx
->block
->instructions
.push_back(std::move(branch
));
7164 ic
->BB_if_idx
= ctx
->block
->index
;
7165 ic
->BB_invert
= Block();
7166 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7167 /* Invert blocks are intentionally not marked as top level because they
7168 * are not part of the logical cfg. */
7169 ic
->BB_invert
.kind
|= block_kind_invert
;
7170 ic
->BB_endif
= Block();
7171 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7172 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7174 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7175 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7176 ctx
->cf_info
.parent_if
.is_divergent
= true;
7177 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7179 /** emit logical then block */
7180 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7181 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7182 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7183 ctx
->block
= BB_then_logical
;
7184 append_logical_start(BB_then_logical
);
7187 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7189 Block
*BB_then_logical
= ctx
->block
;
7190 append_logical_end(BB_then_logical
);
7191 /* branch from logical then block to invert block */
7192 aco_ptr
<Pseudo_branch_instruction
> branch
;
7193 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7194 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7195 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7196 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7197 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7198 BB_then_logical
->kind
|= block_kind_uniform
;
7199 assert(!ctx
->cf_info
.has_branch
);
7200 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7201 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7203 /** emit linear then block */
7204 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7205 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7206 BB_then_linear
->kind
|= block_kind_uniform
;
7207 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7208 /* branch from linear then block to invert block */
7209 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7210 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7211 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7213 /** emit invert merge block */
7214 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7215 ic
->invert_idx
= ctx
->block
->index
;
7217 /* branch to linear else block (skip else) */
7218 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7219 branch
->operands
[0] = Operand(ic
->cond
);
7220 ctx
->block
->instructions
.push_back(std::move(branch
));
7222 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7223 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7225 /** emit logical else block */
7226 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7227 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7228 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7229 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7230 ctx
->block
= BB_else_logical
;
7231 append_logical_start(BB_else_logical
);
7234 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7236 Block
*BB_else_logical
= ctx
->block
;
7237 append_logical_end(BB_else_logical
);
7239 /* branch from logical else block to endif block */
7240 aco_ptr
<Pseudo_branch_instruction
> branch
;
7241 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7242 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7243 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7244 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7245 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7246 BB_else_logical
->kind
|= block_kind_uniform
;
7248 assert(!ctx
->cf_info
.has_branch
);
7249 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7252 /** emit linear else block */
7253 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7254 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7255 BB_else_linear
->kind
|= block_kind_uniform
;
7256 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7258 /* branch from linear else block to endif block */
7259 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7260 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7261 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7264 /** emit endif merge block */
7265 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7266 append_logical_start(ctx
->block
);
7269 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7270 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7271 /* uniform control flow never has an empty exec-mask */
7272 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7273 ctx
->cf_info
.exec_potentially_empty
= false;
7276 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7278 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7279 Builder
bld(ctx
->program
, ctx
->block
);
7280 aco_ptr
<Pseudo_branch_instruction
> branch
;
7282 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7284 * Uniform conditionals are represented in the following way*) :
7286 * The linear and logical CFG:
7289 * BB_THEN (logical) BB_ELSE (logical)
7293 * *) Exceptions may be due to break and continue statements within loops
7294 * If a break/continue happens within uniform control flow, it branches
7295 * to the loop exit/entry block. Otherwise, it branches to the next
7298 append_logical_end(ctx
->block
);
7299 ctx
->block
->kind
|= block_kind_uniform
;
7302 assert(cond
.regClass() == s2
);
7303 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7304 cond
= bool_to_scalar_condition(ctx
, cond
);
7306 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7307 branch
->operands
[0] = Operand(cond
);
7308 branch
->operands
[0].setFixed(scc
);
7309 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7311 unsigned BB_if_idx
= ctx
->block
->index
;
7312 Block BB_endif
= Block();
7313 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7314 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7316 /** emit then block */
7317 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7318 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7319 add_edge(BB_if_idx
, BB_then
);
7320 append_logical_start(BB_then
);
7321 ctx
->block
= BB_then
;
7322 visit_cf_list(ctx
, &if_stmt
->then_list
);
7323 BB_then
= ctx
->block
;
7324 bool then_branch
= ctx
->cf_info
.has_branch
;
7325 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7328 append_logical_end(BB_then
);
7329 /* branch from then block to endif block */
7330 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7331 BB_then
->instructions
.emplace_back(std::move(branch
));
7332 add_linear_edge(BB_then
->index
, &BB_endif
);
7333 if (!then_branch_divergent
)
7334 add_logical_edge(BB_then
->index
, &BB_endif
);
7335 BB_then
->kind
|= block_kind_uniform
;
7338 ctx
->cf_info
.has_branch
= false;
7339 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7341 /** emit else block */
7342 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7343 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7344 add_edge(BB_if_idx
, BB_else
);
7345 append_logical_start(BB_else
);
7346 ctx
->block
= BB_else
;
7347 visit_cf_list(ctx
, &if_stmt
->else_list
);
7348 BB_else
= ctx
->block
;
7350 if (!ctx
->cf_info
.has_branch
) {
7351 append_logical_end(BB_else
);
7352 /* branch from then block to endif block */
7353 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7354 BB_else
->instructions
.emplace_back(std::move(branch
));
7355 add_linear_edge(BB_else
->index
, &BB_endif
);
7356 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7357 add_logical_edge(BB_else
->index
, &BB_endif
);
7358 BB_else
->kind
|= block_kind_uniform
;
7361 ctx
->cf_info
.has_branch
&= then_branch
;
7362 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7364 /** emit endif merge block */
7365 if (!ctx
->cf_info
.has_branch
) {
7366 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7367 append_logical_start(ctx
->block
);
7369 } else { /* non-uniform condition */
7371 * To maintain a logical and linear CFG without critical edges,
7372 * non-uniform conditionals are represented in the following way*) :
7377 * BB_THEN (logical) BB_THEN (linear)
7379 * BB_INVERT (linear)
7381 * BB_ELSE (logical) BB_ELSE (linear)
7388 * BB_THEN (logical) BB_ELSE (logical)
7392 * *) Exceptions may be due to break and continue statements within loops
7397 begin_divergent_if_then(ctx
, &ic
, cond
);
7398 visit_cf_list(ctx
, &if_stmt
->then_list
);
7400 begin_divergent_if_else(ctx
, &ic
);
7401 visit_cf_list(ctx
, &if_stmt
->else_list
);
7403 end_divergent_if(ctx
, &ic
);
7407 static void visit_cf_list(isel_context
*ctx
,
7408 struct exec_list
*list
)
7410 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7411 switch (node
->type
) {
7412 case nir_cf_node_block
:
7413 visit_block(ctx
, nir_cf_node_as_block(node
));
7415 case nir_cf_node_if
:
7416 visit_if(ctx
, nir_cf_node_as_if(node
));
7418 case nir_cf_node_loop
:
7419 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7422 unreachable("unimplemented cf list type");
7427 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7429 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7430 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7431 if (!is_pos
&& !mask
)
7433 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7435 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7436 exp
->enabled_mask
= mask
;
7437 for (unsigned i
= 0; i
< 4; ++i
) {
7438 if (mask
& (1 << i
))
7439 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7441 exp
->operands
[i
] = Operand(v1
);
7443 exp
->valid_mask
= false;
7445 exp
->compressed
= false;
7447 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7449 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7450 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7453 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7455 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7456 exp
->enabled_mask
= 0;
7457 for (unsigned i
= 0; i
< 4; ++i
)
7458 exp
->operands
[i
] = Operand(v1
);
7459 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7460 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7461 exp
->enabled_mask
|= 0x1;
7463 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7464 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7465 exp
->enabled_mask
|= 0x4;
7467 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7468 if (ctx
->options
->chip_class
< GFX9
) {
7469 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7470 exp
->enabled_mask
|= 0x8;
7472 Builder
bld(ctx
->program
, ctx
->block
);
7474 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7475 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7476 if (exp
->operands
[2].isTemp())
7477 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7479 exp
->operands
[2] = Operand(out
);
7480 exp
->enabled_mask
|= 0x4;
7483 exp
->valid_mask
= false;
7485 exp
->compressed
= false;
7486 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7487 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7490 static void create_vs_exports(isel_context
*ctx
)
7492 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7494 if (outinfo
->export_prim_id
) {
7495 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7496 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = get_arg(ctx
, ctx
->args
->vs_prim_id
);
7499 if (ctx
->options
->key
.has_multiview_view_index
) {
7500 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7501 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, get_arg(ctx
, ctx
->args
->ac
.view_index
));
7504 /* the order these position exports are created is important */
7506 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7507 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7508 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7510 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7511 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7512 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7513 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7515 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7516 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7517 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7518 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7519 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7522 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7523 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7524 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7527 export_vs_varying(ctx
, i
, false, NULL
);
7531 static void emit_stream_output(isel_context
*ctx
,
7532 Temp
const *so_buffers
,
7533 Temp
const *so_write_offset
,
7534 const struct radv_stream_output
*output
)
7536 unsigned num_comps
= util_bitcount(output
->component_mask
);
7537 unsigned loc
= output
->location
;
7538 unsigned buf
= output
->buffer
;
7539 unsigned offset
= output
->offset
;
7541 assert(num_comps
&& num_comps
<= 4);
7542 if (!num_comps
|| num_comps
> 4)
7545 unsigned start
= ffs(output
->component_mask
) - 1;
7548 bool all_undef
= true;
7549 assert(ctx
->stage
== vertex_vs
);
7550 for (unsigned i
= 0; i
< num_comps
; i
++) {
7551 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7552 all_undef
= all_undef
&& !out
[i
].id();
7557 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7558 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7559 for (unsigned i
= 0; i
< num_comps
; ++i
)
7560 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7561 vec
->definitions
[0] = Definition(write_data
);
7562 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7565 switch (num_comps
) {
7567 opcode
= aco_opcode::buffer_store_dword
;
7570 opcode
= aco_opcode::buffer_store_dwordx2
;
7573 opcode
= aco_opcode::buffer_store_dwordx3
;
7576 opcode
= aco_opcode::buffer_store_dwordx4
;
7580 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7581 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7582 store
->operands
[1] = Operand(so_buffers
[buf
]);
7583 store
->operands
[2] = Operand((uint32_t) 0);
7584 store
->operands
[3] = Operand(write_data
);
7585 if (offset
> 4095) {
7586 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7587 Builder
bld(ctx
->program
, ctx
->block
);
7588 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7590 store
->offset
= offset
;
7592 store
->offen
= true;
7596 store
->can_reorder
= true;
7597 ctx
->block
->instructions
.emplace_back(std::move(store
));
7600 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7602 Builder
bld(ctx
->program
, ctx
->block
);
7605 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->streamout_buffers
));
7606 for (unsigned i
= 0; i
< 4; i
++) {
7607 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7611 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7614 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7615 get_arg(ctx
, ctx
->args
->streamout_config
), Operand(0x70010u
));
7617 Temp tid
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7618 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7620 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7623 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7625 bld
.reset(ctx
->block
);
7627 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), get_arg(ctx
, ctx
->args
->streamout_write_idx
), tid
);
7629 Temp so_write_offset
[4];
7631 for (unsigned i
= 0; i
< 4; i
++) {
7632 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7637 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7638 get_arg(ctx
, ctx
->args
->streamout_write_idx
),
7639 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
7640 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7642 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7644 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7645 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u),
7646 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
7647 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7651 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7652 struct radv_stream_output
*output
=
7653 &ctx
->program
->info
->so
.outputs
[i
];
7654 if (stream
!= output
->stream
)
7657 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7660 begin_divergent_if_else(ctx
, &ic
);
7661 end_divergent_if(ctx
, &ic
);
7664 } /* end namespace */
7666 void split_arguments(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
7668 /* Split all arguments except for the first (ring_offsets) and the last
7669 * (exec) so that the dead channels don't stay live throughout the program.
7671 for (unsigned i
= 1; i
< startpgm
->definitions
.size() - 1; i
++) {
7672 if (startpgm
->definitions
[i
].regClass().size() > 1) {
7673 emit_split_vector(ctx
, startpgm
->definitions
[i
].getTemp(),
7674 startpgm
->definitions
[i
].regClass().size());
7679 void handle_bc_optimize(isel_context
*ctx
)
7681 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7682 Builder
bld(ctx
->program
, ctx
->block
);
7683 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7684 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7685 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7686 ctx
->persp_centroid
= get_arg(ctx
, ctx
->args
->ac
.persp_centroid
);
7687 ctx
->linear_centroid
= get_arg(ctx
, ctx
->args
->ac
.linear_centroid
);
7688 if (uses_center
&& uses_centroid
) {
7689 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(s2
)),
7690 get_arg(ctx
, ctx
->args
->ac
.prim_mask
), Operand(0u));
7692 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7694 for (unsigned i
= 0; i
< 2; i
++) {
7695 Temp persp_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_centroid
), i
, v1
);
7696 Temp persp_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_center
), i
, v1
);
7697 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7698 persp_centroid
, persp_center
, sel
);
7700 ctx
->persp_centroid
= bld
.tmp(v2
);
7701 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->persp_centroid
),
7702 Operand(new_coord
[0]), Operand(new_coord
[1]));
7703 emit_split_vector(ctx
, ctx
->persp_centroid
, 2);
7706 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7708 for (unsigned i
= 0; i
< 2; i
++) {
7709 Temp linear_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_centroid
), i
, v1
);
7710 Temp linear_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_center
), i
, v1
);
7711 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7712 linear_centroid
, linear_center
, sel
);
7714 ctx
->linear_centroid
= bld
.tmp(v2
);
7715 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->linear_centroid
),
7716 Operand(new_coord
[0]), Operand(new_coord
[1]));
7717 emit_split_vector(ctx
, ctx
->linear_centroid
, 2);
7722 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
7724 Program
*program
= ctx
->program
;
7726 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
7728 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
7729 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
7730 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
7731 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
7732 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
7734 program
->next_fp_mode
.must_flush_denorms32
=
7735 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
7736 program
->next_fp_mode
.must_flush_denorms16_64
=
7737 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
7738 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
7740 program
->next_fp_mode
.care_about_round32
=
7741 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
7743 program
->next_fp_mode
.care_about_round16_64
=
7744 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
7745 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
7747 /* default to preserving fp16 and fp64 denorms, since it's free */
7748 if (program
->next_fp_mode
.must_flush_denorms16_64
)
7749 program
->next_fp_mode
.denorm16_64
= 0;
7751 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
7753 /* preserving fp32 denorms is expensive, so only do it if asked */
7754 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
7755 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
7757 program
->next_fp_mode
.denorm32
= 0;
7759 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
7760 program
->next_fp_mode
.round32
= fp_round_tz
;
7762 program
->next_fp_mode
.round32
= fp_round_ne
;
7764 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
7765 program
->next_fp_mode
.round16_64
= fp_round_tz
;
7767 program
->next_fp_mode
.round16_64
= fp_round_ne
;
7769 ctx
->block
->fp_mode
= program
->next_fp_mode
;
7772 void select_program(Program
*program
,
7773 unsigned shader_count
,
7774 struct nir_shader
*const *shaders
,
7775 ac_shader_config
* config
,
7776 struct radv_shader_args
*args
)
7778 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, args
);
7780 for (unsigned i
= 0; i
< shader_count
; i
++) {
7781 nir_shader
*nir
= shaders
[i
];
7782 init_context(&ctx
, nir
);
7784 setup_fp_mode(&ctx
, nir
);
7787 /* needs to be after init_context() for FS */
7788 Pseudo_instruction
*startpgm
= add_startpgm(&ctx
);
7789 append_logical_start(ctx
.block
);
7790 split_arguments(&ctx
, startpgm
);
7794 if (shader_count
>= 2) {
7795 Builder
bld(ctx
.program
, ctx
.block
);
7796 Temp count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), ctx
.merged_wave_info
, Operand((8u << 16) | (i
* 8u)));
7797 Temp thread_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7798 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7799 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(s2
)), count
, thread_id
);
7801 begin_divergent_if_then(&ctx
, &ic
, cond
);
7805 Builder
bld(ctx
.program
, ctx
.block
);
7806 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
7807 bld
.sopp(aco_opcode::s_barrier
);
7810 if (ctx
.stage
== fragment_fs
)
7811 handle_bc_optimize(&ctx
);
7813 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
7814 visit_cf_list(&ctx
, &func
->body
);
7816 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
7817 emit_streamout(&ctx
, 0);
7819 if (ctx
.stage
== vertex_vs
)
7820 create_vs_exports(&ctx
);
7822 if (shader_count
>= 2) {
7823 begin_divergent_if_else(&ctx
, &ic
);
7824 end_divergent_if(&ctx
, &ic
);
7827 ralloc_free(ctx
.divergent_vals
);
7830 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
7832 append_logical_end(ctx
.block
);
7833 ctx
.block
->kind
|= block_kind_uniform
;
7834 Builder
bld(ctx
.program
, ctx
.block
);
7835 if (ctx
.program
->wb_smem_l1_on_end
)
7836 bld
.smem(aco_opcode::s_dcache_wb
, false);
7837 bld
.sopp(aco_opcode::s_endpgm
);
7840 for (Block
& BB
: program
->blocks
) {
7841 for (unsigned idx
: BB
.linear_preds
)
7842 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
7843 for (unsigned idx
: BB
.logical_preds
)
7844 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);