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 if (ctx
->stage
!= fragment_fs
) {
137 if (src
.type() == RegType::vgpr
|| src
.size() > 1)
138 bld
.copy(Definition(dst
), src
);
140 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
144 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
145 ctx
->program
->needs_wqm
|= program_needs_wqm
;
149 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
151 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
153 /* Currently not implemented on GFX6-7 */
154 assert(ctx
->options
->chip_class
>= GFX8
);
156 if (ctx
->options
->chip_class
<= GFX9
|| ctx
->options
->wave_size
== 32) {
157 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
160 /* GFX10, wave64 mode:
161 * The bpermute instruction is limited to half-wave operation, which means that it can't
162 * properly support subgroup shuffle like older generations (or wave32 mode), so we
165 if (!ctx
->has_gfx10_wave64_bpermute
) {
166 ctx
->has_gfx10_wave64_bpermute
= true;
167 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
168 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
171 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u));
172 lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), lane_id
);
173 Temp lane_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), lane_id
);
174 Temp index_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), index
);
175 Temp cmp
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
, vcc
), lane_is_hi
, index_is_hi
);
177 return bld
.reduction(aco_opcode::p_wave64_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
),
178 bld
.vcc(cmp
), Operand(v2
.as_linear()), index_x4
, data
, gfx10_wave64_bpermute
);
181 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
183 if (val
.type() == RegType::sgpr
) {
184 Builder
bld(ctx
->program
, ctx
->block
);
185 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
187 assert(val
.type() == RegType::vgpr
);
191 //assumes a != 0xffffffff
192 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
195 Builder
bld(ctx
->program
, ctx
->block
);
197 if (util_is_power_of_two_or_zero(b
)) {
198 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
202 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
204 assert(info
.multiplier
<= 0xffffffff);
206 bool pre_shift
= info
.pre_shift
!= 0;
207 bool increment
= info
.increment
!= 0;
208 bool multiply
= true;
209 bool post_shift
= info
.post_shift
!= 0;
211 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
212 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
216 Temp pre_shift_dst
= a
;
218 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
219 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
222 Temp increment_dst
= pre_shift_dst
;
224 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
225 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
228 Temp multiply_dst
= increment_dst
;
230 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
231 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
232 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
236 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
240 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
242 Builder
bld(ctx
->program
, ctx
->block
);
243 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
247 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
249 /* no need to extract the whole vector */
250 if (src
.regClass() == dst_rc
) {
254 assert(src
.size() > idx
);
255 Builder
bld(ctx
->program
, ctx
->block
);
256 auto it
= ctx
->allocated_vec
.find(src
.id());
257 /* the size check needs to be early because elements other than 0 may be garbage */
258 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
259 if (it
->second
[idx
].regClass() == dst_rc
) {
260 return it
->second
[idx
];
262 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
263 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
264 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
268 if (src
.size() == dst_rc
.size()) {
270 return bld
.copy(bld
.def(dst_rc
), src
);
272 Temp dst
= bld
.tmp(dst_rc
);
273 emit_extract_vector(ctx
, src
, idx
, dst
);
278 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
280 if (num_components
== 1)
282 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
284 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
285 split
->operands
[0] = Operand(vec_src
);
286 std::array
<Temp
,4> elems
;
287 for (unsigned i
= 0; i
< num_components
; i
++) {
288 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
289 split
->definitions
[i
] = Definition(elems
[i
]);
291 ctx
->block
->instructions
.emplace_back(std::move(split
));
292 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
295 /* This vector expansion uses a mask to determine which elements in the new vector
296 * come from the original vector. The other elements are undefined. */
297 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
299 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
304 Builder
bld(ctx
->program
, ctx
->block
);
305 if (num_components
== 1) {
306 if (dst
.type() == RegType::sgpr
)
307 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
309 bld
.copy(Definition(dst
), vec_src
);
313 unsigned component_size
= dst
.size() / num_components
;
314 std::array
<Temp
,4> elems
;
316 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
317 vec
->definitions
[0] = Definition(dst
);
319 for (unsigned i
= 0; i
< num_components
; i
++) {
320 if (mask
& (1 << i
)) {
321 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
322 if (dst
.type() == RegType::sgpr
)
323 src
= bld
.as_uniform(src
);
324 vec
->operands
[i
] = Operand(src
);
326 vec
->operands
[i
] = Operand(0u);
328 elems
[i
] = vec
->operands
[i
].getTemp();
330 ctx
->block
->instructions
.emplace_back(std::move(vec
));
331 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
334 Temp
as_divergent_bool(isel_context
*ctx
, Temp val
, bool vcc_hint
)
336 if (val
.regClass() == s2
) {
339 assert(val
.regClass() == s1
);
340 Builder
bld(ctx
->program
, ctx
->block
);
341 Definition
& def
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
),
342 Operand((uint32_t) -1), Operand(0u), bld
.scc(val
)).def(0);
345 return def
.getTemp();
349 Temp
as_uniform_bool(isel_context
*ctx
, Temp val
)
351 if (val
.regClass() == s1
) {
354 assert(val
.regClass() == s2
);
355 Builder
bld(ctx
->program
, ctx
->block
);
356 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
357 return bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), Operand(0u), emit_wqm(ctx
, val
));
361 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
363 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
364 return get_ssa_temp(ctx
, src
.src
.ssa
);
366 if (src
.src
.ssa
->num_components
== size
) {
367 bool identity_swizzle
= true;
368 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
369 if (src
.swizzle
[i
] != i
)
370 identity_swizzle
= false;
372 if (identity_swizzle
)
373 return get_ssa_temp(ctx
, src
.src
.ssa
);
376 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
377 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
378 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
379 assert(vec
.size() % elem_size
== 0);
381 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
383 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
386 std::array
<Temp
,4> elems
;
387 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
388 for (unsigned i
= 0; i
< size
; ++i
) {
389 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
390 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
392 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
393 vec_instr
->definitions
[0] = Definition(dst
);
394 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
395 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
400 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
404 Builder
bld(ctx
->program
, ctx
->block
);
405 if (ptr
.type() == RegType::vgpr
)
406 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
407 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
408 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
411 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
413 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
414 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
415 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
416 sop2
->definitions
[0] = Definition(dst
);
418 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
419 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
422 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool commutative
, bool swap_srcs
=false)
424 Builder
bld(ctx
->program
, ctx
->block
);
425 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
426 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
427 if (src1
.type() == RegType::sgpr
) {
428 if (commutative
&& src0
.type() == RegType::vgpr
) {
432 } else if (src0
.type() == RegType::vgpr
&&
433 op
!= aco_opcode::v_madmk_f32
&&
434 op
!= aco_opcode::v_madak_f32
&&
435 op
!= aco_opcode::v_madmk_f16
&&
436 op
!= aco_opcode::v_madak_f16
) {
437 /* If the instruction is not commutative, we emit a VOP3A instruction */
438 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
441 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
444 bld
.vop2(op
, Definition(dst
), src0
, src1
);
447 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
449 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
450 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
451 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
453 /* ensure that the instruction has at most 1 sgpr operand
454 * The optimizer will inline constants for us */
455 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
456 src0
= as_vgpr(ctx
, src0
);
457 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
458 src1
= as_vgpr(ctx
, src1
);
459 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
460 src2
= as_vgpr(ctx
, src2
);
462 Builder
bld(ctx
->program
, ctx
->block
);
463 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
466 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
468 Builder
bld(ctx
->program
, ctx
->block
);
469 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
472 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
474 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
475 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
476 aco_ptr
<Instruction
> vopc
;
477 if (src1
.type() == RegType::sgpr
) {
478 if (src0
.type() == RegType::vgpr
) {
479 /* to swap the operands, we might also have to change the opcode */
481 case aco_opcode::v_cmp_lt_f32
:
482 op
= aco_opcode::v_cmp_gt_f32
;
484 case aco_opcode::v_cmp_ge_f32
:
485 op
= aco_opcode::v_cmp_le_f32
;
487 case aco_opcode::v_cmp_lt_i32
:
488 op
= aco_opcode::v_cmp_gt_i32
;
490 case aco_opcode::v_cmp_ge_i32
:
491 op
= aco_opcode::v_cmp_le_i32
;
493 case aco_opcode::v_cmp_lt_u32
:
494 op
= aco_opcode::v_cmp_gt_u32
;
496 case aco_opcode::v_cmp_ge_u32
:
497 op
= aco_opcode::v_cmp_le_u32
;
499 case aco_opcode::v_cmp_lt_f64
:
500 op
= aco_opcode::v_cmp_gt_f64
;
502 case aco_opcode::v_cmp_ge_f64
:
503 op
= aco_opcode::v_cmp_le_f64
;
505 case aco_opcode::v_cmp_lt_i64
:
506 op
= aco_opcode::v_cmp_gt_i64
;
508 case aco_opcode::v_cmp_ge_i64
:
509 op
= aco_opcode::v_cmp_le_i64
;
511 case aco_opcode::v_cmp_lt_u64
:
512 op
= aco_opcode::v_cmp_gt_u64
;
514 case aco_opcode::v_cmp_ge_u64
:
515 op
= aco_opcode::v_cmp_le_u64
;
517 default: /* eq and ne are commutative */
524 src1
= as_vgpr(ctx
, src1
);
527 Builder
bld(ctx
->program
, ctx
->block
);
528 bld
.vopc(op
, Definition(dst
), src0
, src1
).def(0).setHint(vcc
);
531 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
533 if (dst
.regClass() == s2
) {
534 emit_vopc_instruction(ctx
, instr
, op
, dst
);
535 if (!ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
])
536 emit_split_vector(ctx
, dst
, 2);
537 } else if (dst
.regClass() == s1
) {
538 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
539 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
540 assert(src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
);
542 Builder
bld(ctx
->program
, ctx
->block
);
543 bld
.sopc(op
, bld
.scc(Definition(dst
)), src0
, src1
);
550 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op32
, aco_opcode op64
, Temp dst
)
552 Builder
bld(ctx
->program
, ctx
->block
);
553 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
554 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
555 if (dst
.regClass() == s2
) {
556 bld
.sop2(op64
, Definition(dst
), bld
.def(s1
, scc
),
557 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
559 assert(dst
.regClass() == s1
);
560 bld
.sop2(op32
, bld
.def(s1
), bld
.scc(Definition(dst
)),
561 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
566 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
568 Builder
bld(ctx
->program
, ctx
->block
);
569 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
570 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
571 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
573 if (dst
.type() == RegType::vgpr
) {
574 cond
= as_divergent_bool(ctx
, cond
, true);
576 aco_ptr
<Instruction
> bcsel
;
577 if (dst
.size() == 1) {
578 then
= as_vgpr(ctx
, then
);
579 els
= as_vgpr(ctx
, els
);
581 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
582 } else if (dst
.size() == 2) {
583 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
584 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
585 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
586 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
588 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
589 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
591 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
593 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
594 nir_print_instr(&instr
->instr
, stderr
);
595 fprintf(stderr
, "\n");
600 if (instr
->dest
.dest
.ssa
.bit_size
!= 1) { /* uniform condition and values in sgpr */
601 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
602 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
603 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
604 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(as_uniform_bool(ctx
, cond
)));
606 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
607 nir_print_instr(&instr
->instr
, stderr
);
608 fprintf(stderr
, "\n");
614 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
616 if (dst
.regClass() == s1
)
617 cond
= as_uniform_bool(ctx
, cond
);
619 if (cond
.regClass() == s1
) { /* uniform selection */
621 if (dst
.regClass() == s2
) {
622 op
= aco_opcode::s_cselect_b64
;
623 then
= as_divergent_bool(ctx
, then
, false);
624 els
= as_divergent_bool(ctx
, els
, false);
626 assert(dst
.regClass() == s1
);
627 op
= aco_opcode::s_cselect_b32
;
628 then
= as_uniform_bool(ctx
, then
);
629 els
= as_uniform_bool(ctx
, els
);
631 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(cond
));
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 (dst
.regClass() == s2
);
639 then
= as_divergent_bool(ctx
, then
, false);
640 els
= as_divergent_bool(ctx
, els
, false);
642 if (cond
.id() != then
.id())
643 then
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), cond
, then
);
645 if (cond
.id() == els
.id())
646 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), then
);
648 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), then
,
649 bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), els
, cond
));
652 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
654 if (!instr
->dest
.dest
.is_ssa
) {
655 fprintf(stderr
, "nir alu dst not in ssa: ");
656 nir_print_instr(&instr
->instr
, stderr
);
657 fprintf(stderr
, "\n");
660 Builder
bld(ctx
->program
, ctx
->block
);
661 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
666 std::array
<Temp
,4> elems
;
667 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
668 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
669 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
670 vec
->operands
[i
] = Operand
{elems
[i
]};
672 vec
->definitions
[0] = Definition(dst
);
673 ctx
->block
->instructions
.emplace_back(std::move(vec
));
674 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
678 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
679 aco_ptr
<Instruction
> mov
;
680 if (dst
.type() == RegType::sgpr
) {
681 if (src
.type() == RegType::vgpr
)
682 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
683 else if (src
.regClass() == s1
)
684 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
685 else if (src
.regClass() == s2
)
686 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
688 unreachable("wrong src register class for nir_op_imov");
689 } else if (dst
.regClass() == v1
) {
690 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
691 } else if (dst
.regClass() == v2
) {
692 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
694 nir_print_instr(&instr
->instr
, stderr
);
695 unreachable("Should have been lowered to scalar.");
700 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
701 /* uniform booleans */
702 if (instr
->dest
.dest
.ssa
.bit_size
== 1 && dst
.regClass() == s1
) {
703 if (src
.regClass() == s1
) {
704 /* in this case, src is either 1 or 0 */
705 bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.scc(Definition(dst
)), Operand(1u), src
);
707 /* src is either exec_mask or 0 */
708 assert(src
.regClass() == s2
);
709 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
711 } else if (dst
.regClass() == v1
) {
712 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
713 } else if (dst
.type() == RegType::sgpr
) {
714 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
715 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
717 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
718 nir_print_instr(&instr
->instr
, stderr
);
719 fprintf(stderr
, "\n");
724 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
725 if (dst
.regClass() == v1
) {
726 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
727 } else if (dst
.regClass() == s1
) {
728 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
729 } else if (dst
.size() == 2) {
730 Temp src0
= bld
.tmp(dst
.type(), 1);
731 Temp src1
= bld
.tmp(dst
.type(), 1);
732 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
734 if (dst
.regClass() == s2
) {
735 Temp carry
= bld
.tmp(s1
);
736 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
737 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
738 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
740 Temp lower
= bld
.tmp(v1
);
741 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
742 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
743 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
746 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
747 nir_print_instr(&instr
->instr
, stderr
);
748 fprintf(stderr
, "\n");
753 if (dst
.regClass() == s1
) {
754 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
755 } else if (dst
.regClass() == v1
) {
756 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
757 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
759 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
760 nir_print_instr(&instr
->instr
, stderr
);
761 fprintf(stderr
, "\n");
766 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
767 if (dst
.regClass() == s1
) {
768 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
769 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
770 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
771 } else if (dst
.regClass() == s2
) {
772 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
773 Temp neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
774 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, neqz
);
775 } else if (dst
.regClass() == v1
) {
776 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
777 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
778 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
779 } else if (dst
.regClass() == v2
) {
780 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
781 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
782 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
783 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
784 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
785 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
787 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
788 nir_print_instr(&instr
->instr
, stderr
);
789 fprintf(stderr
, "\n");
794 if (dst
.regClass() == v1
) {
795 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
796 } else if (dst
.regClass() == s1
) {
797 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
799 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
800 nir_print_instr(&instr
->instr
, stderr
);
801 fprintf(stderr
, "\n");
806 if (dst
.regClass() == v1
) {
807 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
808 } else if (dst
.regClass() == s1
) {
809 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
811 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
812 nir_print_instr(&instr
->instr
, stderr
);
813 fprintf(stderr
, "\n");
818 if (dst
.regClass() == v1
) {
819 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
820 } else if (dst
.regClass() == s1
) {
821 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
823 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
824 nir_print_instr(&instr
->instr
, stderr
);
825 fprintf(stderr
, "\n");
830 if (dst
.regClass() == v1
) {
831 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
832 } else if (dst
.regClass() == s1
) {
833 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
835 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
836 nir_print_instr(&instr
->instr
, stderr
);
837 fprintf(stderr
, "\n");
842 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
843 emit_boolean_logic(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::s_or_b64
, dst
);
844 } else if (dst
.regClass() == v1
) {
845 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
846 } else if (dst
.regClass() == s1
) {
847 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
848 } else if (dst
.regClass() == s2
) {
849 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
851 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
852 nir_print_instr(&instr
->instr
, stderr
);
853 fprintf(stderr
, "\n");
858 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
859 emit_boolean_logic(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::s_and_b64
, dst
);
860 } else if (dst
.regClass() == v1
) {
861 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
862 } else if (dst
.regClass() == s1
) {
863 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
864 } else if (dst
.regClass() == s2
) {
865 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
867 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
868 nir_print_instr(&instr
->instr
, stderr
);
869 fprintf(stderr
, "\n");
874 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
875 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
876 } else if (dst
.regClass() == v1
) {
877 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
878 } else if (dst
.regClass() == s1
) {
879 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
880 } else if (dst
.regClass() == s2
) {
881 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
883 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
884 nir_print_instr(&instr
->instr
, stderr
);
885 fprintf(stderr
, "\n");
890 if (dst
.regClass() == v1
) {
891 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
892 } else if (dst
.regClass() == v2
) {
893 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
894 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
895 } else if (dst
.regClass() == s2
) {
896 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
897 } else if (dst
.regClass() == s1
) {
898 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
900 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
901 nir_print_instr(&instr
->instr
, stderr
);
902 fprintf(stderr
, "\n");
907 if (dst
.regClass() == v1
) {
908 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
909 } else if (dst
.regClass() == v2
) {
910 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
911 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
912 } else if (dst
.regClass() == s1
) {
913 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
914 } else if (dst
.regClass() == s2
) {
915 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
917 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
918 nir_print_instr(&instr
->instr
, stderr
);
919 fprintf(stderr
, "\n");
924 if (dst
.regClass() == v1
) {
925 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
926 } else if (dst
.regClass() == v2
) {
927 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
928 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
929 } else if (dst
.regClass() == s1
) {
930 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
931 } else if (dst
.regClass() == s2
) {
932 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
934 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
935 nir_print_instr(&instr
->instr
, stderr
);
936 fprintf(stderr
, "\n");
940 case nir_op_find_lsb
: {
941 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
942 if (src
.regClass() == s1
) {
943 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
944 } else if (src
.regClass() == v1
) {
945 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
946 } else if (src
.regClass() == s2
) {
947 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
949 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
950 nir_print_instr(&instr
->instr
, stderr
);
951 fprintf(stderr
, "\n");
955 case nir_op_ufind_msb
:
956 case nir_op_ifind_msb
: {
957 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
958 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
959 aco_opcode op
= src
.regClass() == s2
?
960 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
961 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
962 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
964 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
965 Operand(src
.size() * 32u - 1u), msb_rev
);
966 Temp msb
= sub
.def(0).getTemp();
967 Temp carry
= sub
.def(1).getTemp();
969 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
970 } else if (src
.regClass() == v1
) {
971 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
972 Temp msb_rev
= bld
.tmp(v1
);
973 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
974 Temp msb
= bld
.tmp(v1
);
975 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
976 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
978 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
979 nir_print_instr(&instr
->instr
, stderr
);
980 fprintf(stderr
, "\n");
984 case nir_op_bitfield_reverse
: {
985 if (dst
.regClass() == s1
) {
986 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
987 } else if (dst
.regClass() == v1
) {
988 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
990 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
991 nir_print_instr(&instr
->instr
, stderr
);
992 fprintf(stderr
, "\n");
997 if (dst
.regClass() == s1
) {
998 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1002 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1003 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1004 if (dst
.regClass() == v1
) {
1005 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1009 assert(src0
.size() == 2 && src1
.size() == 2);
1010 Temp src00
= bld
.tmp(src0
.type(), 1);
1011 Temp src01
= bld
.tmp(dst
.type(), 1);
1012 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1013 Temp src10
= bld
.tmp(src1
.type(), 1);
1014 Temp src11
= bld
.tmp(dst
.type(), 1);
1015 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1017 if (dst
.regClass() == s2
) {
1018 Temp carry
= bld
.tmp(s1
);
1019 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1020 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1021 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1022 } else if (dst
.regClass() == v2
) {
1023 Temp dst0
= bld
.tmp(v1
);
1024 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1025 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1026 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1028 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1029 nir_print_instr(&instr
->instr
, stderr
);
1030 fprintf(stderr
, "\n");
1034 case nir_op_uadd_sat
: {
1035 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1036 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1037 if (dst
.regClass() == s1
) {
1038 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1039 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1041 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1042 } else if (dst
.regClass() == v1
) {
1043 if (ctx
->options
->chip_class
>= GFX9
) {
1044 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1045 add
->operands
[0] = Operand(src0
);
1046 add
->operands
[1] = Operand(src1
);
1047 add
->definitions
[0] = Definition(dst
);
1049 ctx
->block
->instructions
.emplace_back(std::move(add
));
1051 if (src1
.regClass() != v1
)
1052 std::swap(src0
, src1
);
1053 assert(src1
.regClass() == v1
);
1054 Temp tmp
= bld
.tmp(v1
);
1055 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1056 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1059 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1060 nir_print_instr(&instr
->instr
, stderr
);
1061 fprintf(stderr
, "\n");
1065 case nir_op_uadd_carry
: {
1066 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1067 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1068 if (dst
.regClass() == s1
) {
1069 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1072 if (dst
.regClass() == v1
) {
1073 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1074 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1078 Temp src00
= bld
.tmp(src0
.type(), 1);
1079 Temp src01
= bld
.tmp(dst
.type(), 1);
1080 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1081 Temp src10
= bld
.tmp(src1
.type(), 1);
1082 Temp src11
= bld
.tmp(dst
.type(), 1);
1083 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1084 if (dst
.regClass() == s2
) {
1085 Temp carry
= bld
.tmp(s1
);
1086 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1087 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1088 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1089 } else if (dst
.regClass() == v2
) {
1090 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1091 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1092 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1093 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1095 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1096 nir_print_instr(&instr
->instr
, stderr
);
1097 fprintf(stderr
, "\n");
1102 if (dst
.regClass() == s1
) {
1103 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1107 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1108 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1109 if (dst
.regClass() == v1
) {
1110 bld
.vsub32(Definition(dst
), src0
, src1
);
1114 Temp src00
= bld
.tmp(src0
.type(), 1);
1115 Temp src01
= bld
.tmp(dst
.type(), 1);
1116 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1117 Temp src10
= bld
.tmp(src1
.type(), 1);
1118 Temp src11
= bld
.tmp(dst
.type(), 1);
1119 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1120 if (dst
.regClass() == s2
) {
1121 Temp carry
= bld
.tmp(s1
);
1122 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1123 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1124 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1125 } else if (dst
.regClass() == v2
) {
1126 Temp lower
= bld
.tmp(v1
);
1127 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1128 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1129 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1131 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1132 nir_print_instr(&instr
->instr
, stderr
);
1133 fprintf(stderr
, "\n");
1137 case nir_op_usub_borrow
: {
1138 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1139 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1140 if (dst
.regClass() == s1
) {
1141 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1143 } else if (dst
.regClass() == v1
) {
1144 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1145 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1149 Temp src00
= bld
.tmp(src0
.type(), 1);
1150 Temp src01
= bld
.tmp(dst
.type(), 1);
1151 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1152 Temp src10
= bld
.tmp(src1
.type(), 1);
1153 Temp src11
= bld
.tmp(dst
.type(), 1);
1154 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1155 if (dst
.regClass() == s2
) {
1156 Temp borrow
= bld
.tmp(s1
);
1157 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1158 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1159 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1160 } else if (dst
.regClass() == v2
) {
1161 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1162 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1163 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1164 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1166 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1167 nir_print_instr(&instr
->instr
, stderr
);
1168 fprintf(stderr
, "\n");
1173 if (dst
.regClass() == v1
) {
1174 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1175 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1176 } else if (dst
.regClass() == s1
) {
1177 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1179 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1180 nir_print_instr(&instr
->instr
, stderr
);
1181 fprintf(stderr
, "\n");
1185 case nir_op_umul_high
: {
1186 if (dst
.regClass() == v1
) {
1187 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1188 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1189 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1190 } else if (dst
.regClass() == s1
) {
1191 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1192 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1193 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1195 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1196 nir_print_instr(&instr
->instr
, stderr
);
1197 fprintf(stderr
, "\n");
1201 case nir_op_imul_high
: {
1202 if (dst
.regClass() == v1
) {
1203 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1204 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1205 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1206 } else if (dst
.regClass() == s1
) {
1207 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1208 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1209 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1211 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1212 nir_print_instr(&instr
->instr
, stderr
);
1213 fprintf(stderr
, "\n");
1218 if (dst
.size() == 1) {
1219 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1220 } else if (dst
.size() == 2) {
1221 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1222 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1224 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1225 nir_print_instr(&instr
->instr
, stderr
);
1226 fprintf(stderr
, "\n");
1231 if (dst
.size() == 1) {
1232 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1233 } else if (dst
.size() == 2) {
1234 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1235 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1237 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1238 nir_print_instr(&instr
->instr
, stderr
);
1239 fprintf(stderr
, "\n");
1244 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1245 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1246 if (dst
.size() == 1) {
1247 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1248 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1250 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1251 } else if (dst
.size() == 2) {
1252 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1253 get_alu_src(ctx
, instr
->src
[0]),
1254 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1255 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
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_max_f32
, dst
, true);
1267 } else if (dst
.size() == 2) {
1268 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1269 get_alu_src(ctx
, instr
->src
[0]),
1270 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1272 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1273 nir_print_instr(&instr
->instr
, stderr
);
1274 fprintf(stderr
, "\n");
1279 if (dst
.size() == 1) {
1280 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1281 } else if (dst
.size() == 2) {
1282 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1283 get_alu_src(ctx
, instr
->src
[0]),
1284 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1286 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1287 nir_print_instr(&instr
->instr
, stderr
);
1288 fprintf(stderr
, "\n");
1292 case nir_op_fmax3
: {
1293 if (dst
.size() == 1) {
1294 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1296 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1297 nir_print_instr(&instr
->instr
, stderr
);
1298 fprintf(stderr
, "\n");
1302 case nir_op_fmin3
: {
1303 if (dst
.size() == 1) {
1304 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1306 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1307 nir_print_instr(&instr
->instr
, stderr
);
1308 fprintf(stderr
, "\n");
1312 case nir_op_fmed3
: {
1313 if (dst
.size() == 1) {
1314 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1316 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1317 nir_print_instr(&instr
->instr
, stderr
);
1318 fprintf(stderr
, "\n");
1322 case nir_op_umax3
: {
1323 if (dst
.size() == 1) {
1324 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1326 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1327 nir_print_instr(&instr
->instr
, stderr
);
1328 fprintf(stderr
, "\n");
1332 case nir_op_umin3
: {
1333 if (dst
.size() == 1) {
1334 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1336 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1337 nir_print_instr(&instr
->instr
, stderr
);
1338 fprintf(stderr
, "\n");
1342 case nir_op_umed3
: {
1343 if (dst
.size() == 1) {
1344 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1346 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1347 nir_print_instr(&instr
->instr
, stderr
);
1348 fprintf(stderr
, "\n");
1352 case nir_op_imax3
: {
1353 if (dst
.size() == 1) {
1354 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1356 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1357 nir_print_instr(&instr
->instr
, stderr
);
1358 fprintf(stderr
, "\n");
1362 case nir_op_imin3
: {
1363 if (dst
.size() == 1) {
1364 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1366 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1367 nir_print_instr(&instr
->instr
, stderr
);
1368 fprintf(stderr
, "\n");
1372 case nir_op_imed3
: {
1373 if (dst
.size() == 1) {
1374 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1376 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1377 nir_print_instr(&instr
->instr
, stderr
);
1378 fprintf(stderr
, "\n");
1382 case nir_op_cube_face_coord
: {
1383 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1384 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1385 emit_extract_vector(ctx
, in
, 1, v1
),
1386 emit_extract_vector(ctx
, in
, 2, v1
) };
1387 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1388 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1389 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1390 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1391 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1392 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1393 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1396 case nir_op_cube_face_index
: {
1397 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1398 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1399 emit_extract_vector(ctx
, in
, 1, v1
),
1400 emit_extract_vector(ctx
, in
, 2, v1
) };
1401 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1404 case nir_op_bcsel
: {
1405 emit_bcsel(ctx
, instr
, dst
);
1409 if (dst
.size() == 1) {
1410 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f32
, dst
);
1411 } else if (dst
.size() == 2) {
1412 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1414 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1415 nir_print_instr(&instr
->instr
, stderr
);
1416 fprintf(stderr
, "\n");
1421 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1422 if (dst
.size() == 1) {
1423 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1424 } else if (dst
.size() == 2) {
1425 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1426 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1427 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1428 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1430 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1431 nir_print_instr(&instr
->instr
, stderr
);
1432 fprintf(stderr
, "\n");
1437 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1438 if (dst
.size() == 1) {
1439 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1440 } else if (dst
.size() == 2) {
1441 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1442 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1443 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1444 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1446 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1447 nir_print_instr(&instr
->instr
, stderr
);
1448 fprintf(stderr
, "\n");
1453 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1454 if (dst
.size() == 1) {
1455 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1456 } else if (dst
.size() == 2) {
1457 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1458 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1461 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1462 nir_print_instr(&instr
->instr
, stderr
);
1463 fprintf(stderr
, "\n");
1467 case nir_op_flog2
: {
1468 if (dst
.size() == 1) {
1469 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_log_f32
, dst
);
1471 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1472 nir_print_instr(&instr
->instr
, stderr
);
1473 fprintf(stderr
, "\n");
1478 if (dst
.size() == 1) {
1479 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f32
, dst
);
1480 } else if (dst
.size() == 2) {
1481 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1483 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1484 nir_print_instr(&instr
->instr
, stderr
);
1485 fprintf(stderr
, "\n");
1489 case nir_op_fexp2
: {
1490 if (dst
.size() == 1) {
1491 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1493 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1494 nir_print_instr(&instr
->instr
, stderr
);
1495 fprintf(stderr
, "\n");
1499 case nir_op_fsqrt
: {
1500 if (dst
.size() == 1) {
1501 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f32
, dst
);
1502 } else if (dst
.size() == 2) {
1503 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1505 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1506 nir_print_instr(&instr
->instr
, stderr
);
1507 fprintf(stderr
, "\n");
1511 case nir_op_ffract
: {
1512 if (dst
.size() == 1) {
1513 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1514 } else if (dst
.size() == 2) {
1515 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1517 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1518 nir_print_instr(&instr
->instr
, stderr
);
1519 fprintf(stderr
, "\n");
1523 case nir_op_ffloor
: {
1524 if (dst
.size() == 1) {
1525 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1526 } else if (dst
.size() == 2) {
1527 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1529 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1530 nir_print_instr(&instr
->instr
, stderr
);
1531 fprintf(stderr
, "\n");
1535 case nir_op_fceil
: {
1536 if (dst
.size() == 1) {
1537 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1538 } else if (dst
.size() == 2) {
1539 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1541 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1542 nir_print_instr(&instr
->instr
, stderr
);
1543 fprintf(stderr
, "\n");
1547 case nir_op_ftrunc
: {
1548 if (dst
.size() == 1) {
1549 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1550 } else if (dst
.size() == 2) {
1551 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1553 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1554 nir_print_instr(&instr
->instr
, stderr
);
1555 fprintf(stderr
, "\n");
1559 case nir_op_fround_even
: {
1560 if (dst
.size() == 1) {
1561 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1562 } else if (dst
.size() == 2) {
1563 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1565 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1566 nir_print_instr(&instr
->instr
, stderr
);
1567 fprintf(stderr
, "\n");
1573 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1574 aco_ptr
<Instruction
> norm
;
1575 if (dst
.size() == 1) {
1577 Operand
half_pi(0x3e22f983u
);
1578 if (src
.type() == RegType::sgpr
)
1579 tmp
= bld
.vop2_e64(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1581 tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1583 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1584 if (ctx
->options
->chip_class
< GFX9
)
1585 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1587 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1588 bld
.vop1(opcode
, Definition(dst
), tmp
);
1590 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1591 nir_print_instr(&instr
->instr
, stderr
);
1592 fprintf(stderr
, "\n");
1596 case nir_op_ldexp
: {
1597 if (dst
.size() == 1) {
1598 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1599 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1600 get_alu_src(ctx
, instr
->src
[1]));
1601 } else if (dst
.size() == 2) {
1602 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1603 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1604 get_alu_src(ctx
, instr
->src
[1]));
1606 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1607 nir_print_instr(&instr
->instr
, stderr
);
1608 fprintf(stderr
, "\n");
1612 case nir_op_frexp_sig
: {
1613 if (dst
.size() == 1) {
1614 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1615 get_alu_src(ctx
, instr
->src
[0]));
1616 } else if (dst
.size() == 2) {
1617 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1618 get_alu_src(ctx
, instr
->src
[0]));
1620 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1621 nir_print_instr(&instr
->instr
, stderr
);
1622 fprintf(stderr
, "\n");
1626 case nir_op_frexp_exp
: {
1627 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1628 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1629 get_alu_src(ctx
, instr
->src
[0]));
1630 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1631 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1632 get_alu_src(ctx
, instr
->src
[0]));
1634 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1635 nir_print_instr(&instr
->instr
, stderr
);
1636 fprintf(stderr
, "\n");
1640 case nir_op_fsign
: {
1641 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1642 if (dst
.size() == 1) {
1643 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1644 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1645 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1646 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1647 } else if (dst
.size() == 2) {
1648 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1649 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1650 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, src
, cond
);
1652 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1653 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1654 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1656 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1658 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1659 nir_print_instr(&instr
->instr
, stderr
);
1660 fprintf(stderr
, "\n");
1664 case nir_op_f2f32
: {
1665 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1666 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1668 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1669 nir_print_instr(&instr
->instr
, stderr
);
1670 fprintf(stderr
, "\n");
1674 case nir_op_f2f64
: {
1675 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1676 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1678 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1679 nir_print_instr(&instr
->instr
, stderr
);
1680 fprintf(stderr
, "\n");
1684 case nir_op_i2f32
: {
1685 assert(dst
.size() == 1);
1686 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1689 case nir_op_i2f64
: {
1690 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1691 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1692 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1693 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1694 RegClass rc
= RegClass(src
.type(), 1);
1695 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1696 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1697 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1698 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1699 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1700 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1703 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1704 nir_print_instr(&instr
->instr
, stderr
);
1705 fprintf(stderr
, "\n");
1709 case nir_op_u2f32
: {
1710 assert(dst
.size() == 1);
1711 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1714 case nir_op_u2f64
: {
1715 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1716 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1717 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1718 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1719 RegClass rc
= RegClass(src
.type(), 1);
1720 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1721 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1722 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1723 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1724 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1725 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1727 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1728 nir_print_instr(&instr
->instr
, stderr
);
1729 fprintf(stderr
, "\n");
1733 case nir_op_f2i32
: {
1734 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1735 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1736 if (dst
.type() == RegType::vgpr
)
1737 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1739 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1740 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1742 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1743 if (dst
.type() == RegType::vgpr
)
1744 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1746 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1747 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1750 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1751 nir_print_instr(&instr
->instr
, stderr
);
1752 fprintf(stderr
, "\n");
1756 case nir_op_f2u32
: {
1757 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1758 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1759 if (dst
.type() == RegType::vgpr
)
1760 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1762 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1763 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1765 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1766 if (dst
.type() == RegType::vgpr
)
1767 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1769 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1770 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1773 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1774 nir_print_instr(&instr
->instr
, stderr
);
1775 fprintf(stderr
, "\n");
1779 case nir_op_f2i64
: {
1780 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1781 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1782 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1783 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1784 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1785 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1786 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1787 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1788 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1789 Temp new_exponent
= bld
.tmp(v1
);
1790 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1791 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1792 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1793 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1794 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1795 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1796 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1797 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1798 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1799 Temp new_lower
= bld
.tmp(v1
);
1800 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1801 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1802 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1804 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1805 if (src
.type() == RegType::vgpr
)
1806 src
= bld
.as_uniform(src
);
1807 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1808 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1809 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1810 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1811 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1812 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1813 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1814 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1815 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1816 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1817 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1818 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1819 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1820 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1821 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1822 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1823 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1824 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1825 Temp borrow
= bld
.tmp(s1
);
1826 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1827 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1828 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1830 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1831 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1832 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1833 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1834 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1835 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1836 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1837 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1838 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1839 if (dst
.type() == RegType::sgpr
) {
1840 lower
= bld
.as_uniform(lower
);
1841 upper
= bld
.as_uniform(upper
);
1843 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1846 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1847 nir_print_instr(&instr
->instr
, stderr
);
1848 fprintf(stderr
, "\n");
1852 case nir_op_f2u64
: {
1853 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1854 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1855 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1856 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(64u), exponent
);
1857 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1858 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1859 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1860 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1861 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1862 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1863 Temp new_exponent
= bld
.tmp(v1
);
1864 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1865 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1866 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1867 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1868 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1869 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1870 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1871 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1872 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1874 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1875 if (src
.type() == RegType::vgpr
)
1876 src
= bld
.as_uniform(src
);
1877 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1878 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1879 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1880 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1881 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1882 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1883 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1884 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1885 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1886 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1887 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1888 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1889 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1890 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1891 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1892 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1893 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1894 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1896 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1897 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1898 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1899 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1900 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1901 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1902 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1903 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1904 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1905 if (dst
.type() == RegType::sgpr
) {
1906 lower
= bld
.as_uniform(lower
);
1907 upper
= bld
.as_uniform(upper
);
1909 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1912 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1913 nir_print_instr(&instr
->instr
, stderr
);
1914 fprintf(stderr
, "\n");
1918 case nir_op_b2f32
: {
1919 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1920 if (dst
.regClass() == s1
) {
1921 src
= as_uniform_bool(ctx
, src
);
1922 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
1923 } else if (dst
.regClass() == v1
) {
1924 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
),
1925 as_divergent_bool(ctx
, src
, true));
1927 unreachable("Wrong destination register class for nir_op_b2f32.");
1931 case nir_op_b2f64
: {
1932 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1933 if (dst
.regClass() == s2
) {
1934 src
= as_uniform_bool(ctx
, src
);
1935 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
1936 } else if (dst
.regClass() == v2
) {
1937 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
1938 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
,
1939 as_divergent_bool(ctx
, src
, true));
1940 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1942 unreachable("Wrong destination register class for nir_op_b2f64.");
1946 case nir_op_i2i32
: {
1947 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1948 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1949 /* we can actually just say dst = src, as it would map the lower register */
1950 emit_extract_vector(ctx
, src
, 0, dst
);
1952 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1953 nir_print_instr(&instr
->instr
, stderr
);
1954 fprintf(stderr
, "\n");
1958 case nir_op_u2u32
: {
1959 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1960 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
1961 if (dst
.regClass() == s1
) {
1962 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
1964 // TODO: do better with SDWA
1965 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
1967 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1968 /* we can actually just say dst = src, as it would map the lower register */
1969 emit_extract_vector(ctx
, src
, 0, dst
);
1971 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1972 nir_print_instr(&instr
->instr
, stderr
);
1973 fprintf(stderr
, "\n");
1977 case nir_op_i2i64
: {
1978 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1979 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1980 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1982 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1983 nir_print_instr(&instr
->instr
, stderr
);
1984 fprintf(stderr
, "\n");
1988 case nir_op_u2u64
: {
1989 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1990 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1991 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1993 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1994 nir_print_instr(&instr
->instr
, stderr
);
1995 fprintf(stderr
, "\n");
1999 case nir_op_b2i32
: {
2000 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2001 if (dst
.regClass() == s1
) {
2002 if (src
.regClass() == s1
) {
2003 bld
.copy(Definition(dst
), src
);
2005 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2006 assert(src
.regClass() == s2
);
2007 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2010 assert(dst
.regClass() == v1
&& src
.regClass() == s2
);
2011 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2016 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2017 if (dst
.regClass() == s2
) {
2018 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2019 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2020 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2022 assert(src
.regClass() == s1
&& dst
.regClass() == s1
);
2023 bld
.sopc(aco_opcode::s_cmp_lg_u32
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2027 case nir_op_pack_64_2x32_split
: {
2028 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2029 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2031 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2034 case nir_op_unpack_64_2x32_split_x
:
2035 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2037 case nir_op_unpack_64_2x32_split_y
:
2038 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2040 case nir_op_pack_half_2x16
: {
2041 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2043 if (dst
.regClass() == v1
) {
2044 Temp src0
= bld
.tmp(v1
);
2045 Temp src1
= bld
.tmp(v1
);
2046 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2047 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2050 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2051 nir_print_instr(&instr
->instr
, stderr
);
2052 fprintf(stderr
, "\n");
2056 case nir_op_unpack_half_2x16_split_x
: {
2057 if (dst
.regClass() == v1
) {
2058 Builder
bld(ctx
->program
, ctx
->block
);
2059 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2061 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2062 nir_print_instr(&instr
->instr
, stderr
);
2063 fprintf(stderr
, "\n");
2067 case nir_op_unpack_half_2x16_split_y
: {
2068 if (dst
.regClass() == v1
) {
2069 Builder
bld(ctx
->program
, ctx
->block
);
2070 /* TODO: use SDWA here */
2071 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2072 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2074 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2075 nir_print_instr(&instr
->instr
, stderr
);
2076 fprintf(stderr
, "\n");
2080 case nir_op_fquantize2f16
: {
2081 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]));
2083 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2085 Temp cmp_res
= bld
.tmp(s2
);
2086 bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, Definition(cmp_res
), f16
, mask
).def(0).setHint(vcc
);
2088 Temp f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2090 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2094 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2095 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2097 if (dst
.regClass() == s1
) {
2098 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2099 } else if (dst
.regClass() == v1
) {
2100 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2102 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2103 nir_print_instr(&instr
->instr
, stderr
);
2104 fprintf(stderr
, "\n");
2108 case nir_op_bitfield_select
: {
2109 /* (mask & insert) | (~mask & base) */
2110 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2111 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2112 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2114 /* dst = (insert & bitmask) | (base & ~bitmask) */
2115 if (dst
.regClass() == s1
) {
2116 aco_ptr
<Instruction
> sop2
;
2117 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2118 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2120 if (const_insert
&& const_bitmask
) {
2121 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2123 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2124 lhs
= Operand(insert
);
2128 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2129 if (const_base
&& const_bitmask
) {
2130 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2132 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2133 rhs
= Operand(base
);
2136 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2138 } else if (dst
.regClass() == v1
) {
2139 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2140 base
= as_vgpr(ctx
, base
);
2141 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2142 insert
= as_vgpr(ctx
, insert
);
2144 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2147 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2148 nir_print_instr(&instr
->instr
, stderr
);
2149 fprintf(stderr
, "\n");
2155 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2156 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2157 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2159 if (dst
.type() == RegType::sgpr
) {
2161 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2162 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2163 if (const_offset
&& const_bits
) {
2164 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2165 extract
= Operand(const_extract
);
2169 width
= Operand(const_bits
->u32
<< 16);
2171 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2173 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2177 if (dst
.regClass() == s1
) {
2178 if (instr
->op
== nir_op_ubfe
)
2179 opcode
= aco_opcode::s_bfe_u32
;
2181 opcode
= aco_opcode::s_bfe_i32
;
2182 } else if (dst
.regClass() == s2
) {
2183 if (instr
->op
== nir_op_ubfe
)
2184 opcode
= aco_opcode::s_bfe_u64
;
2186 opcode
= aco_opcode::s_bfe_i64
;
2188 unreachable("Unsupported BFE bit size");
2191 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2195 if (dst
.regClass() == v1
) {
2196 if (instr
->op
== nir_op_ubfe
)
2197 opcode
= aco_opcode::v_bfe_u32
;
2199 opcode
= aco_opcode::v_bfe_i32
;
2201 unreachable("Unsupported BFE bit size");
2204 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2208 case nir_op_bit_count
: {
2209 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2210 if (src
.regClass() == s1
) {
2211 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2212 } else if (src
.regClass() == v1
) {
2213 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2214 } else if (src
.regClass() == v2
) {
2215 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2216 emit_extract_vector(ctx
, src
, 1, v1
),
2217 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2218 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2219 } else if (src
.regClass() == s2
) {
2220 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2222 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2223 nir_print_instr(&instr
->instr
, stderr
);
2224 fprintf(stderr
, "\n");
2229 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2230 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f32
, dst
);
2231 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2232 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f64
, dst
);
2236 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2237 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f32
, dst
);
2238 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2239 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f64
, dst
);
2243 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2244 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f32
, dst
);
2245 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2246 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f64
, dst
);
2250 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2251 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f32
, dst
);
2252 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2253 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f64
, dst
);
2257 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2258 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i32
, dst
);
2259 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2260 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_i32
, dst
);
2261 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2262 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i64
, dst
);
2266 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2267 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i32
, dst
);
2268 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2269 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_i32
, dst
);
2270 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2271 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i64
, dst
);
2275 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2276 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i32
, dst
);
2277 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2278 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_i32
, dst
);
2279 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2280 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i64
, dst
);
2281 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2282 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_u64
, dst
);
2283 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2284 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2285 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2286 bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.scc(Definition(dst
)),
2287 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2288 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2289 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2290 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2291 bld
.sop2(aco_opcode::s_xnor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2292 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2294 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2295 nir_print_instr(&instr
->instr
, stderr
);
2296 fprintf(stderr
, "\n");
2301 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2302 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i32
, dst
);
2303 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2304 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i64
, dst
);
2305 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2306 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_i32
, dst
);
2307 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2308 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_u64
, dst
);
2309 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2310 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2311 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2312 bld
.sopc(aco_opcode::s_cmp_lg_i32
, bld
.scc(Definition(dst
)),
2313 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2314 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2315 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2316 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2317 bld
.sop2(aco_opcode::s_xor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2318 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2320 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2321 nir_print_instr(&instr
->instr
, stderr
);
2322 fprintf(stderr
, "\n");
2327 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2328 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u32
, dst
);
2329 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2330 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_u32
, dst
);
2331 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2332 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u64
, dst
);
2336 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2337 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u32
, dst
);
2338 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2339 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_u32
, dst
);
2340 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2341 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u64
, dst
);
2346 case nir_op_fddx_fine
:
2347 case nir_op_fddy_fine
:
2348 case nir_op_fddx_coarse
:
2349 case nir_op_fddy_coarse
: {
2350 Definition tl
= bld
.def(v1
);
2352 if (instr
->op
== nir_op_fddx_fine
) {
2353 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 2, 2));
2354 dpp_ctrl
= dpp_quad_perm(1, 1, 3, 3);
2355 } else if (instr
->op
== nir_op_fddy_fine
) {
2356 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 1, 0, 1));
2357 dpp_ctrl
= dpp_quad_perm(2, 3, 2, 3);
2359 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 0, 0));
2360 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2361 dpp_ctrl
= dpp_quad_perm(1, 1, 1, 1);
2363 dpp_ctrl
= dpp_quad_perm(2, 2, 2, 2);
2366 Definition tmp
= bld
.def(v1
);
2367 bld
.vop2_dpp(aco_opcode::v_sub_f32
, tmp
, get_alu_src(ctx
, instr
->src
[0]), tl
.getTemp(), dpp_ctrl
);
2368 emit_wqm(ctx
, tmp
.getTemp(), dst
, true);
2372 fprintf(stderr
, "Unknown NIR ALU instr: ");
2373 nir_print_instr(&instr
->instr
, stderr
);
2374 fprintf(stderr
, "\n");
2378 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2380 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2382 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2383 // which get truncated the lsb if double and msb if int
2384 // for now, we only use s_mov_b64 with 64bit inline constants
2385 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2386 assert(dst
.type() == RegType::sgpr
);
2388 if (dst
.size() == 1)
2390 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2392 assert(dst
.size() != 1);
2393 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2394 if (instr
->def
.bit_size
== 64)
2395 for (unsigned i
= 0; i
< dst
.size(); i
++)
2396 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2398 for (unsigned i
= 0; i
< dst
.size(); i
++)
2399 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2401 vec
->definitions
[0] = Definition(dst
);
2402 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2406 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2408 uint32_t new_mask
= 0;
2409 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2410 if (mask
& (1u << i
))
2411 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2415 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2417 /* This wouldn't work inside control flow or with indirect offsets but
2418 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2420 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2421 unsigned component
= nir_intrinsic_component(instr
);
2422 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2423 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2425 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2426 if (off_instr
->type
!= nir_instr_type_load_const
) {
2427 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2428 nir_print_instr(off_instr
, stderr
);
2429 fprintf(stderr
, "\n");
2431 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2433 if (instr
->src
[0].ssa
->bit_size
== 64)
2434 write_mask
= widen_mask(write_mask
, 2);
2436 for (unsigned i
= 0; i
< 8; ++i
) {
2437 if (write_mask
& (1 << i
)) {
2438 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2439 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2445 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2447 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2449 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2450 for (unsigned i
= 0; i
< 4; ++i
) {
2451 if (write_mask
& (1 << i
)) {
2452 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2453 values
[i
] = Operand(tmp
);
2455 values
[i
] = Operand(v1
);
2459 unsigned index
= nir_intrinsic_base(instr
) / 4;
2460 unsigned target
, col_format
;
2461 unsigned enabled_channels
= 0xF;
2462 aco_opcode compr_op
= (aco_opcode
)0;
2464 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2465 assert(offset
&& "Non-const offsets on exports not yet supported");
2466 index
+= offset
->u32
;
2468 assert(index
!= FRAG_RESULT_COLOR
);
2470 /* Unlike vertex shader exports, it's fine to use multiple exports to
2471 * export separate channels of one target. So shaders which export both
2472 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2473 * TODO: combine the exports in those cases and create better code
2476 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2478 if (ctx
->program
->info
->ps
.writes_z
) {
2479 target
= V_008DFC_SQ_EXP_MRTZ
;
2480 enabled_channels
= 0x4;
2481 col_format
= (unsigned) -1;
2483 values
[2] = values
[0];
2484 values
[0] = Operand(v1
);
2486 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2487 exp
->valid_mask
= false;
2489 exp
->compressed
= true;
2490 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2491 exp
->enabled_mask
= 0xc;
2492 for (int i
= 0; i
< 4; i
++)
2493 exp
->operands
[i
] = Operand(v1
);
2494 exp
->operands
[1] = Operand(values
[0]);
2495 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2499 } else if (index
== FRAG_RESULT_DEPTH
) {
2501 target
= V_008DFC_SQ_EXP_MRTZ
;
2502 enabled_channels
= 0x1;
2503 col_format
= (unsigned) -1;
2505 } else if (index
== FRAG_RESULT_STENCIL
) {
2507 if (ctx
->program
->info
->ps
.writes_z
) {
2508 target
= V_008DFC_SQ_EXP_MRTZ
;
2509 enabled_channels
= 0x2;
2510 col_format
= (unsigned) -1;
2512 values
[1] = values
[0];
2513 values
[0] = Operand(v1
);
2515 aco_ptr
<Instruction
> shift
{create_instruction
<VOP2_instruction
>(aco_opcode::v_lshlrev_b32
, Format::VOP2
, 2, 1)};
2516 shift
->operands
[0] = Operand((uint32_t) 16);
2517 shift
->operands
[1] = values
[0];
2518 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2519 shift
->definitions
[0] = Definition(tmp
);
2520 ctx
->block
->instructions
.emplace_back(std::move(shift
));
2522 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2523 exp
->valid_mask
= false;
2525 exp
->compressed
= true;
2526 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2527 exp
->enabled_mask
= 0x3;
2528 exp
->operands
[0] = Operand(tmp
);
2529 for (int i
= 1; i
< 4; i
++)
2530 exp
->operands
[i
] = Operand(v1
);
2531 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2536 index
-= FRAG_RESULT_DATA0
;
2537 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2538 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2540 ASSERTED
bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2541 ASSERTED
bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2542 assert(!is_int8
&& !is_int10
);
2546 case V_028714_SPI_SHADER_ZERO
:
2547 enabled_channels
= 0; /* writemask */
2548 target
= V_008DFC_SQ_EXP_NULL
;
2551 case V_028714_SPI_SHADER_32_R
:
2552 enabled_channels
= 1;
2555 case V_028714_SPI_SHADER_32_GR
:
2556 enabled_channels
= 0x3;
2559 case V_028714_SPI_SHADER_32_AR
:
2560 if (ctx
->options
->chip_class
>= GFX10
) {
2561 /* Special case: on GFX10, the outputs are different for 32_AR */
2562 enabled_channels
= 0x3;
2563 values
[1] = values
[3];
2565 enabled_channels
= 0x9;
2569 case V_028714_SPI_SHADER_FP16_ABGR
:
2570 enabled_channels
= 0x5;
2571 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2574 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2575 enabled_channels
= 0x5;
2576 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2579 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2580 enabled_channels
= 0x5;
2581 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2584 case V_028714_SPI_SHADER_UINT16_ABGR
:
2585 enabled_channels
= 0x5;
2586 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2589 case V_028714_SPI_SHADER_SINT16_ABGR
:
2590 enabled_channels
= 0x5;
2591 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2594 case V_028714_SPI_SHADER_32_ABGR
:
2595 enabled_channels
= 0xF;
2602 if (target
== V_008DFC_SQ_EXP_NULL
)
2607 for (int i
= 0; i
< 2; i
++)
2609 /* check if at least one of the values to be compressed is enabled */
2610 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2612 enabled_channels
|= enabled
<< (i
*2);
2613 aco_ptr
<VOP3A_instruction
> compr
{create_instruction
<VOP3A_instruction
>(compr_op
, Format::VOP3A
, 2, 1)};
2614 Temp tmp
{ctx
->program
->allocateId(), v1
};
2615 compr
->operands
[0] = values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2];
2616 compr
->operands
[1] = values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1];
2617 compr
->definitions
[0] = Definition(tmp
);
2618 values
[i
] = Operand(tmp
);
2619 ctx
->block
->instructions
.emplace_back(std::move(compr
));
2621 values
[i
] = Operand(v1
);
2626 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2627 exp
->valid_mask
= false;
2629 exp
->compressed
= (bool) compr_op
;
2631 exp
->enabled_mask
= enabled_channels
;
2632 if ((bool) compr_op
) {
2633 for (int i
= 0; i
< 2; i
++)
2634 exp
->operands
[i
] = enabled_channels
& (3 << (i
* 2)) ? values
[i
] : Operand(v1
);
2635 exp
->operands
[2] = Operand(v1
);
2636 exp
->operands
[3] = Operand(v1
);
2638 for (int i
= 0; i
< 4; i
++)
2639 exp
->operands
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2642 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2645 Operand
load_lds_size_m0(isel_context
*ctx
)
2647 /* TODO: m0 does not need to be initialized on GFX9+ */
2648 Builder
bld(ctx
->program
, ctx
->block
);
2649 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
2652 void load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
2653 Temp address
, unsigned base_offset
, unsigned align
)
2655 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2657 Builder
bld(ctx
->program
, ctx
->block
);
2659 Operand m
= load_lds_size_m0(ctx
);
2661 unsigned num_components
= dst
.size() * 4u / elem_size_bytes
;
2662 unsigned bytes_read
= 0;
2663 unsigned result_size
= 0;
2664 unsigned total_bytes
= num_components
* elem_size_bytes
;
2665 std::array
<Temp
, 4> result
;
2667 while (bytes_read
< total_bytes
) {
2668 unsigned todo
= total_bytes
- bytes_read
;
2669 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2670 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2672 aco_opcode op
= aco_opcode::last_opcode
;
2674 if (todo
>= 16 && aligned16
) {
2675 op
= aco_opcode::ds_read_b128
;
2677 } else if (todo
>= 16 && aligned8
) {
2678 op
= aco_opcode::ds_read2_b64
;
2681 } else if (todo
>= 12 && aligned16
) {
2682 op
= aco_opcode::ds_read_b96
;
2684 } else if (todo
>= 8 && aligned8
) {
2685 op
= aco_opcode::ds_read_b64
;
2687 } else if (todo
>= 8) {
2688 op
= aco_opcode::ds_read2_b32
;
2691 } else if (todo
>= 4) {
2692 op
= aco_opcode::ds_read_b32
;
2697 assert(todo
% elem_size_bytes
== 0);
2698 unsigned num_elements
= todo
/ elem_size_bytes
;
2699 unsigned offset
= base_offset
+ bytes_read
;
2700 unsigned max_offset
= read2
? 1019 : 65535;
2702 Temp address_offset
= address
;
2703 if (offset
> max_offset
) {
2704 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2705 offset
= bytes_read
;
2707 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2710 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2713 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2716 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
2718 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2720 if (num_components
== 1) {
2721 assert(todo
== total_bytes
);
2722 if (dst
.type() == RegType::sgpr
)
2723 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2727 if (dst
.type() == RegType::sgpr
)
2728 res
= bld
.as_uniform(res
);
2730 if (num_elements
== 1) {
2731 result
[result_size
++] = res
;
2733 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2734 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2735 split
->operands
[0] = Operand(res
);
2736 for (unsigned i
= 0; i
< num_elements
; i
++)
2737 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2738 ctx
->block
->instructions
.emplace_back(std::move(split
));
2744 assert(result_size
== num_components
&& result_size
> 1);
2745 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2746 for (unsigned i
= 0; i
< result_size
; i
++)
2747 vec
->operands
[i
] = Operand(result
[i
]);
2748 vec
->definitions
[0] = Definition(dst
);
2749 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2750 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2753 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2755 if (start
== 0 && size
== data
.size())
2756 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2758 unsigned size_hint
= 1;
2759 auto it
= ctx
->allocated_vec
.find(data
.id());
2760 if (it
!= ctx
->allocated_vec
.end())
2761 size_hint
= it
->second
[0].size();
2762 if (size
% size_hint
|| start
% size_hint
)
2769 for (unsigned i
= 0; i
< size
; i
++)
2770 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2773 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2775 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2776 for (unsigned i
= 0; i
< size
; i
++)
2777 vec
->operands
[i
] = Operand(elems
[i
]);
2778 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2779 vec
->definitions
[0] = Definition(res
);
2780 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2784 void ds_write_helper(isel_context
*ctx
, Operand m
, Temp address
, Temp data
, unsigned data_start
, unsigned total_size
, unsigned offset0
, unsigned offset1
, unsigned align
)
2786 Builder
bld(ctx
->program
, ctx
->block
);
2787 unsigned bytes_written
= 0;
2788 while (bytes_written
< total_size
* 4) {
2789 unsigned todo
= total_size
* 4 - bytes_written
;
2790 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
2791 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
2793 aco_opcode op
= aco_opcode::last_opcode
;
2794 bool write2
= false;
2796 if (todo
>= 16 && aligned16
) {
2797 op
= aco_opcode::ds_write_b128
;
2799 } else if (todo
>= 16 && aligned8
) {
2800 op
= aco_opcode::ds_write2_b64
;
2803 } else if (todo
>= 12 && aligned16
) {
2804 op
= aco_opcode::ds_write_b96
;
2806 } else if (todo
>= 8 && aligned8
) {
2807 op
= aco_opcode::ds_write_b64
;
2809 } else if (todo
>= 8) {
2810 op
= aco_opcode::ds_write2_b32
;
2813 } else if (todo
>= 4) {
2814 op
= aco_opcode::ds_write_b32
;
2820 unsigned offset
= offset0
+ offset1
+ bytes_written
;
2821 unsigned max_offset
= write2
? 1020 : 65535;
2822 Temp address_offset
= address
;
2823 if (offset
> max_offset
) {
2824 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
2825 offset
= offset1
+ bytes_written
;
2827 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
2830 Temp val0
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
/ 2, RegType::vgpr
);
2831 Temp val1
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2) + 1, size
/ 2, RegType::vgpr
);
2832 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
2834 Temp val
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
, RegType::vgpr
);
2835 bld
.ds(op
, address_offset
, val
, m
, offset
);
2838 bytes_written
+= size
* 4;
2842 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
2843 Temp address
, unsigned base_offset
, unsigned align
)
2845 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2847 Operand m
= load_lds_size_m0(ctx
);
2849 /* we need at most two stores for 32bit variables */
2850 int start
[2], count
[2];
2851 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2852 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2853 assert(wrmask
== 0);
2855 /* one combined store is sufficient */
2856 if (count
[0] == count
[1]) {
2857 Builder
bld(ctx
->program
, ctx
->block
);
2859 Temp address_offset
= address
;
2860 if ((base_offset
>> 2) + start
[1] > 255) {
2861 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2865 assert(count
[0] == 1);
2866 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
2867 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
2868 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2869 base_offset
= base_offset
/ elem_size_bytes
;
2870 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2871 base_offset
+ start
[0], base_offset
+ start
[1]);
2875 for (unsigned i
= 0; i
< 2; i
++) {
2879 unsigned elem_size_words
= elem_size_bytes
/ 4;
2880 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2881 base_offset
, start
[i
] * elem_size_bytes
, align
);
2886 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2888 if (ctx
->stage
== vertex_vs
) {
2889 visit_store_vs_output(ctx
, instr
);
2890 } else if (ctx
->stage
== fragment_fs
) {
2891 visit_store_fs_output(ctx
, instr
);
2893 unreachable("Shader stage not implemented");
2897 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2899 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2900 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2902 Builder
bld(ctx
->program
, ctx
->block
);
2903 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2904 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2907 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2909 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2910 for (unsigned i
= 0; i
< num_components
; i
++)
2911 vec
->operands
[i
] = Operand(ctx
->fs_inputs
[fs_input::frag_pos_0
+ i
]);
2913 if (ctx
->fs_vgpr_args
[fs_input::frag_pos_3
]) {
2914 assert(num_components
== 4);
2915 Builder
bld(ctx
->program
, ctx
->block
);
2916 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::frag_pos_3
]);
2919 for (Operand
& op
: vec
->operands
)
2920 op
= op
.isUndefined() ? Operand(0u) : op
;
2922 vec
->definitions
[0] = Definition(dst
);
2923 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2924 emit_split_vector(ctx
, dst
, num_components
);
2928 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2930 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2931 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2932 unsigned idx
= nir_intrinsic_base(instr
);
2933 unsigned component
= nir_intrinsic_component(instr
);
2934 Temp prim_mask
= ctx
->prim_mask
;
2936 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2938 assert(offset
->u32
== 0);
2940 /* the lower 15bit of the prim_mask contain the offset into LDS
2941 * while the upper bits contain the number of prims */
2942 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
2943 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2944 Builder
bld(ctx
->program
, ctx
->block
);
2945 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2946 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2947 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2948 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2949 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2952 if (instr
->dest
.ssa
.num_components
== 1) {
2953 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
2955 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
2956 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
2958 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2959 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
2960 vec
->operands
[i
] = Operand(tmp
);
2962 vec
->definitions
[0] = Definition(dst
);
2963 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2967 unsigned get_num_channels_from_data_format(unsigned data_format
)
2969 switch (data_format
) {
2970 case V_008F0C_BUF_DATA_FORMAT_8
:
2971 case V_008F0C_BUF_DATA_FORMAT_16
:
2972 case V_008F0C_BUF_DATA_FORMAT_32
:
2974 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2975 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2976 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2978 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2979 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2980 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2982 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2983 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2984 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2985 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2986 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2995 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2996 * so we may need to fix it up. */
2997 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
2999 Builder
bld(ctx
->program
, ctx
->block
);
3001 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3002 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3004 /* For the integer-like cases, do a natural sign extension.
3006 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3007 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3010 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3011 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3013 /* Convert back to the right type. */
3014 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3015 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3016 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0xbf800000u
), alpha
);
3017 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3018 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3019 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3025 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3027 Builder
bld(ctx
->program
, ctx
->block
);
3028 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3029 if (ctx
->stage
& sw_vs
) {
3031 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3032 if (off_instr
->type
!= nir_instr_type_load_const
) {
3033 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3034 nir_print_instr(off_instr
, stderr
);
3035 fprintf(stderr
, "\n");
3037 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3039 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, ctx
->vertex_buffers
);
3041 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3042 unsigned component
= nir_intrinsic_component(instr
);
3043 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3044 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3045 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3046 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3048 unsigned dfmt
= attrib_format
& 0xf;
3050 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3051 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
3052 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3053 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
3054 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3055 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3057 num_channels
= MAX2(num_channels
, 3);
3059 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
3062 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3063 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3065 ctx
->needs_instance_id
= true;
3068 Temp divided
= bld
.tmp(v1
);
3069 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, ctx
->instance_id
), divisor
);
3070 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, divided
);
3072 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, ctx
->instance_id
);
3075 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), ctx
->start_instance
);
3078 index
= bld
.vadd32(bld
.def(v1
), ctx
->base_vertex
, ctx
->vertex_id
);
3081 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
3082 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
3083 attrib_offset
= attrib_offset
% attrib_stride
;
3086 Operand
soffset(0u);
3087 if (attrib_offset
>= 4096) {
3088 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
3093 switch (num_channels
) {
3095 opcode
= aco_opcode::tbuffer_load_format_x
;
3098 opcode
= aco_opcode::tbuffer_load_format_xy
;
3101 opcode
= aco_opcode::tbuffer_load_format_xyz
;
3104 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
3107 unreachable("Unimplemented load_input vector size");
3110 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
3112 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
3113 mubuf
->operands
[0] = Operand(index
);
3114 mubuf
->operands
[1] = Operand(list
);
3115 mubuf
->operands
[2] = soffset
;
3116 mubuf
->definitions
[0] = Definition(tmp
);
3117 mubuf
->idxen
= true;
3118 mubuf
->can_reorder
= true;
3121 assert(attrib_offset
< 4096);
3122 mubuf
->offset
= attrib_offset
;
3123 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3125 emit_split_vector(ctx
, tmp
, tmp
.size());
3127 if (tmp
.id() != dst
.id()) {
3128 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3129 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3131 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3132 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3133 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3135 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3136 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3137 unsigned idx
= i
+ component
;
3138 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
3139 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
3140 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
3141 } else if (idx
< num_channels
) {
3142 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
3143 } else if (is_float
&& idx
== 3) {
3144 vec
->operands
[i
] = Operand(0x3f800000u
);
3145 } else if (!is_float
&& idx
== 3) {
3146 vec
->operands
[i
] = Operand(1u);
3148 vec
->operands
[i
] = Operand(0u);
3151 vec
->definitions
[0] = Definition(dst
);
3152 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3153 emit_split_vector(ctx
, dst
, dst
.size());
3156 } else if (ctx
->stage
== fragment_fs
) {
3157 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3158 if (off_instr
->type
!= nir_instr_type_load_const
||
3159 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3160 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3161 nir_print_instr(off_instr
, stderr
);
3162 fprintf(stderr
, "\n");
3165 Temp prim_mask
= ctx
->prim_mask
;
3166 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
3168 assert(offset
->u32
== 0);
3170 /* the lower 15bit of the prim_mask contain the offset into LDS
3171 * while the upper bits contain the number of prims */
3172 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3173 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3174 Builder
bld(ctx
->program
, ctx
->block
);
3175 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3176 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3177 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3178 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3179 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3182 unsigned idx
= nir_intrinsic_base(instr
);
3183 unsigned component
= nir_intrinsic_component(instr
);
3185 if (dst
.size() == 1) {
3186 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
3188 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3189 for (unsigned i
= 0; i
< dst
.size(); i
++)
3190 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
3191 vec
->definitions
[0] = Definition(dst
);
3192 bld
.insert(std::move(vec
));
3196 unreachable("Shader stage not implemented");
3200 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3202 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3203 Builder
bld(ctx
->program
, ctx
->block
);
3204 Temp ptr64
= convert_pointer_to_64_bit(ctx
, ctx
->descriptor_sets
[0]);
3205 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
3208 return ctx
->descriptor_sets
[desc_set
];
3212 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3214 Builder
bld(ctx
->program
, ctx
->block
);
3215 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3216 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3217 index
= bld
.as_uniform(index
);
3218 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3219 unsigned binding
= nir_intrinsic_binding(instr
);
3222 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3223 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3224 unsigned offset
= layout
->binding
[binding
].offset
;
3226 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3227 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3228 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3229 desc_ptr
= ctx
->push_constants
;
3230 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3233 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3234 stride
= layout
->binding
[binding
].size
;
3237 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3238 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3240 if (nir_const_index
) {
3241 const_index
= const_index
* stride
;
3242 } else if (index
.type() == RegType::vgpr
) {
3243 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3244 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3246 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3250 if (nir_const_index
) {
3251 const_index
= const_index
+ offset
;
3252 } else if (index
.type() == RegType::vgpr
) {
3253 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
3255 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
3259 if (nir_const_index
&& const_index
== 0) {
3261 } else if (index
.type() == RegType::vgpr
) {
3262 index
= bld
.vadd32(bld
.def(v1
),
3263 nir_const_index
? Operand(const_index
) : Operand(index
),
3266 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3267 nir_const_index
? Operand(const_index
) : Operand(index
),
3271 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
3274 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
, Temp rsrc
, Temp offset
, bool glc
=false)
3276 Builder
bld(ctx
->program
, ctx
->block
);
3278 unsigned num_bytes
= dst
.size() * 4;
3279 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
3282 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
3283 if (ctx
->options
->chip_class
< GFX8
)
3284 offset
= as_vgpr(ctx
, offset
);
3286 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3287 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3288 unsigned const_offset
= 0;
3290 Temp lower
= Temp();
3291 if (num_bytes
> 16) {
3292 assert(num_components
== 3 || num_components
== 4);
3293 op
= aco_opcode::buffer_load_dwordx4
;
3294 lower
= bld
.tmp(v4
);
3295 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3296 mubuf
->definitions
[0] = Definition(lower
);
3297 mubuf
->operands
[0] = vaddr
;
3298 mubuf
->operands
[1] = Operand(rsrc
);
3299 mubuf
->operands
[2] = soffset
;
3300 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3303 mubuf
->barrier
= barrier_buffer
;
3304 bld
.insert(std::move(mubuf
));
3305 emit_split_vector(ctx
, lower
, 2);
3310 switch (num_bytes
) {
3312 op
= aco_opcode::buffer_load_dword
;
3315 op
= aco_opcode::buffer_load_dwordx2
;
3318 op
= aco_opcode::buffer_load_dwordx3
;
3321 op
= aco_opcode::buffer_load_dwordx4
;
3324 unreachable("Load SSBO not implemented for this size.");
3326 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3327 mubuf
->operands
[0] = vaddr
;
3328 mubuf
->operands
[1] = Operand(rsrc
);
3329 mubuf
->operands
[2] = soffset
;
3330 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3333 mubuf
->barrier
= barrier_buffer
;
3334 mubuf
->offset
= const_offset
;
3335 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3337 if (dst
.size() > 4) {
3338 assert(lower
!= Temp());
3339 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3340 instr
->definitions
[0] = Definition(upper
);
3341 bld
.insert(std::move(instr
));
3342 if (dst
.size() == 8)
3343 emit_split_vector(ctx
, upper
, 2);
3344 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3345 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3346 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3347 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3348 if (dst
.size() == 8)
3349 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3352 if (dst
.type() == RegType::sgpr
) {
3353 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3354 instr
->definitions
[0] = Definition(vec
);
3355 bld
.insert(std::move(instr
));
3356 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3358 instr
->definitions
[0] = Definition(dst
);
3359 bld
.insert(std::move(instr
));
3362 switch (num_bytes
) {
3364 op
= aco_opcode::s_buffer_load_dword
;
3367 op
= aco_opcode::s_buffer_load_dwordx2
;
3371 op
= aco_opcode::s_buffer_load_dwordx4
;
3375 op
= aco_opcode::s_buffer_load_dwordx8
;
3378 unreachable("Load SSBO not implemented for this size.");
3380 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3381 load
->operands
[0] = Operand(rsrc
);
3382 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3383 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3384 load
->definitions
[0] = Definition(dst
);
3387 load
->barrier
= barrier_buffer
;
3388 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3391 if (dst
.size() == 3) {
3392 Temp vec
= bld
.tmp(s4
);
3393 load
->definitions
[0] = Definition(vec
);
3394 bld
.insert(std::move(load
));
3395 emit_split_vector(ctx
, vec
, 4);
3397 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3398 emit_extract_vector(ctx
, vec
, 0, s1
),
3399 emit_extract_vector(ctx
, vec
, 1, s1
),
3400 emit_extract_vector(ctx
, vec
, 2, s1
));
3401 } else if (dst
.size() == 6) {
3402 Temp vec
= bld
.tmp(s8
);
3403 load
->definitions
[0] = Definition(vec
);
3404 bld
.insert(std::move(load
));
3405 emit_split_vector(ctx
, vec
, 4);
3407 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3408 emit_extract_vector(ctx
, vec
, 0, s2
),
3409 emit_extract_vector(ctx
, vec
, 1, s2
),
3410 emit_extract_vector(ctx
, vec
, 2, s2
));
3412 bld
.insert(std::move(load
));
3416 emit_split_vector(ctx
, dst
, num_components
);
3419 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3421 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3422 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3424 Builder
bld(ctx
->program
, ctx
->block
);
3426 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3427 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3428 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3429 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3431 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3432 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3433 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3434 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3435 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3436 if (ctx
->options
->chip_class
>= GFX10
) {
3437 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3438 S_008F0C_OOB_SELECT(3) |
3439 S_008F0C_RESOURCE_LEVEL(1);
3441 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3442 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3444 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3445 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3446 Operand(0xFFFFFFFFu
),
3447 Operand(desc_type
));
3448 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3449 rsrc
, upper_dwords
);
3451 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3452 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3455 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3458 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3460 Builder
bld(ctx
->program
, ctx
->block
);
3461 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3463 unsigned offset
= nir_intrinsic_base(instr
);
3464 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3465 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3467 unsigned count
= instr
->dest
.ssa
.num_components
;
3468 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3469 start
-= ctx
->base_inline_push_consts
;
3470 if (start
+ count
<= ctx
->num_inline_push_consts
) {
3471 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3472 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3473 for (unsigned i
= 0; i
< count
; ++i
) {
3474 elems
[i
] = ctx
->inline_push_consts
[start
+ i
];
3475 vec
->operands
[i
] = Operand
{elems
[i
]};
3477 vec
->definitions
[0] = Definition(dst
);
3478 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3479 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3484 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3485 if (offset
!= 0) // TODO check if index != 0 as well
3486 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3487 Temp ptr
= convert_pointer_to_64_bit(ctx
, ctx
->push_constants
);
3492 switch (dst
.size()) {
3494 op
= aco_opcode::s_load_dword
;
3497 op
= aco_opcode::s_load_dwordx2
;
3503 op
= aco_opcode::s_load_dwordx4
;
3509 op
= aco_opcode::s_load_dwordx8
;
3512 unreachable("unimplemented or forbidden load_push_constant.");
3515 bld
.smem(op
, Definition(vec
), ptr
, index
);
3518 emit_split_vector(ctx
, vec
, 4);
3519 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3520 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3521 emit_extract_vector(ctx
, vec
, 0, rc
),
3522 emit_extract_vector(ctx
, vec
, 1, rc
),
3523 emit_extract_vector(ctx
, vec
, 2, rc
));
3526 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3529 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3531 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3533 Builder
bld(ctx
->program
, ctx
->block
);
3535 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3536 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3537 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3538 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3539 if (ctx
->options
->chip_class
>= GFX10
) {
3540 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3541 S_008F0C_OOB_SELECT(3) |
3542 S_008F0C_RESOURCE_LEVEL(1);
3544 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3545 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3548 unsigned base
= nir_intrinsic_base(instr
);
3549 unsigned range
= nir_intrinsic_range(instr
);
3551 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3552 if (base
&& offset
.type() == RegType::sgpr
)
3553 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3554 else if (base
&& offset
.type() == RegType::vgpr
)
3555 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3557 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3558 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
3559 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
3560 Operand(desc_type
));
3562 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3565 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3567 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3568 ctx
->cf_info
.exec_potentially_empty
= true;
3570 ctx
->program
->needs_exact
= true;
3572 // TODO: optimize uniform conditions
3573 Builder
bld(ctx
->program
, ctx
->block
);
3574 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
3575 src
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
3576 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3577 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3581 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3583 Builder
bld(ctx
->program
, ctx
->block
);
3585 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3586 ctx
->cf_info
.exec_potentially_empty
= true;
3588 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3589 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3591 if (ctx
->block
->loop_nest_depth
&&
3592 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3593 /* we handle discards the same way as jump instructions */
3594 append_logical_end(ctx
->block
);
3596 /* in loops, discard behaves like break */
3597 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3598 ctx
->block
->kind
|= block_kind_discard
;
3601 /* uniform discard - loop ends here */
3602 assert(nir_instr_is_last(&instr
->instr
));
3603 ctx
->block
->kind
|= block_kind_uniform
;
3604 ctx
->cf_info
.has_branch
= true;
3605 bld
.branch(aco_opcode::p_branch
);
3606 add_linear_edge(ctx
->block
->index
, linear_target
);
3610 /* we add a break right behind the discard() instructions */
3611 ctx
->block
->kind
|= block_kind_break
;
3612 unsigned idx
= ctx
->block
->index
;
3614 /* remove critical edges from linear CFG */
3615 bld
.branch(aco_opcode::p_branch
);
3616 Block
* break_block
= ctx
->program
->create_and_insert_block();
3617 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3618 break_block
->kind
|= block_kind_uniform
;
3619 add_linear_edge(idx
, break_block
);
3620 add_linear_edge(break_block
->index
, linear_target
);
3621 bld
.reset(break_block
);
3622 bld
.branch(aco_opcode::p_branch
);
3624 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3625 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3626 add_linear_edge(idx
, continue_block
);
3627 append_logical_start(continue_block
);
3628 ctx
->block
= continue_block
;
3633 /* it can currently happen that NIR doesn't remove the unreachable code */
3634 if (!nir_instr_is_last(&instr
->instr
)) {
3635 ctx
->program
->needs_exact
= true;
3636 /* save exec somewhere temporarily so that it doesn't get
3637 * overwritten before the discard from outer exec masks */
3638 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, s2
));
3639 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3640 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3644 /* This condition is incorrect for uniformly branched discards in a loop
3645 * predicated by a divergent condition, but the above code catches that case
3646 * and the discard would end up turning into a discard_if.
3656 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3657 /* program just ends here */
3658 ctx
->block
->kind
|= block_kind_uniform
;
3659 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3660 0 /* enabled mask */, 9 /* dest */,
3661 false /* compressed */, true/* done */, true /* valid mask */);
3662 bld
.sopp(aco_opcode::s_endpgm
);
3663 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3665 ctx
->block
->kind
|= block_kind_discard
;
3666 /* branch and linear edge is added by visit_if() */
3670 enum aco_descriptor_type
{
3681 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3682 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3684 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
3685 return dim
== ac_image_cube
||
3686 dim
== ac_image_1darray
||
3687 dim
== ac_image_2darray
||
3688 dim
== ac_image_2darraymsaa
;
3691 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3692 enum aco_descriptor_type desc_type
,
3693 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3695 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3696 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3697 if (it != ctx->tex_desc.end())
3700 Temp index
= Temp();
3701 bool index_set
= false;
3702 unsigned constant_index
= 0;
3703 unsigned descriptor_set
;
3704 unsigned base_index
;
3705 Builder
bld(ctx
->program
, ctx
->block
);
3708 assert(tex_instr
&& !image
);
3710 base_index
= tex_instr
->sampler_index
;
3712 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3713 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3717 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3718 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3720 constant_index
+= array_size
* const_value
->u32
;
3722 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
3723 if (indirect
.type() == RegType::vgpr
)
3724 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
3726 if (array_size
!= 1)
3727 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3733 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3737 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3739 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3740 base_index
= deref_instr
->var
->data
.binding
;
3743 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3744 list
= convert_pointer_to_64_bit(ctx
, list
);
3746 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3747 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3748 unsigned offset
= binding
->offset
;
3749 unsigned stride
= binding
->size
;
3753 assert(base_index
< layout
->binding_count
);
3755 switch (desc_type
) {
3756 case ACO_DESC_IMAGE
:
3758 opcode
= aco_opcode::s_load_dwordx8
;
3760 case ACO_DESC_FMASK
:
3762 opcode
= aco_opcode::s_load_dwordx8
;
3765 case ACO_DESC_SAMPLER
:
3767 opcode
= aco_opcode::s_load_dwordx4
;
3768 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3769 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3771 case ACO_DESC_BUFFER
:
3773 opcode
= aco_opcode::s_load_dwordx4
;
3775 case ACO_DESC_PLANE_0
:
3776 case ACO_DESC_PLANE_1
:
3778 opcode
= aco_opcode::s_load_dwordx8
;
3779 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3781 case ACO_DESC_PLANE_2
:
3783 opcode
= aco_opcode::s_load_dwordx4
;
3787 unreachable("invalid desc_type\n");
3790 offset
+= constant_index
* stride
;
3792 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3793 (!index_set
|| binding
->immutable_samplers_equal
)) {
3794 if (binding
->immutable_samplers_equal
)
3797 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3798 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3799 Operand(samplers
[constant_index
* 4 + 0]),
3800 Operand(samplers
[constant_index
* 4 + 1]),
3801 Operand(samplers
[constant_index
* 4 + 2]),
3802 Operand(samplers
[constant_index
* 4 + 3]));
3807 off
= Operand(offset
);
3809 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3810 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3813 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3815 if (desc_type
== ACO_DESC_PLANE_2
) {
3817 for (unsigned i
= 0; i
< 8; i
++)
3818 components
[i
] = bld
.tmp(s1
);
3819 bld
.pseudo(aco_opcode::p_split_vector
,
3820 Definition(components
[0]),
3821 Definition(components
[1]),
3822 Definition(components
[2]),
3823 Definition(components
[3]),
3826 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3827 bld
.pseudo(aco_opcode::p_split_vector
,
3828 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3829 Definition(components
[4]),
3830 Definition(components
[5]),
3831 Definition(components
[6]),
3832 Definition(components
[7]),
3835 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3836 components
[0], components
[1], components
[2], components
[3],
3837 components
[4], components
[5], components
[6], components
[7]);
3843 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3846 case GLSL_SAMPLER_DIM_BUF
:
3848 case GLSL_SAMPLER_DIM_1D
:
3849 return array
? 2 : 1;
3850 case GLSL_SAMPLER_DIM_2D
:
3851 return array
? 3 : 2;
3852 case GLSL_SAMPLER_DIM_MS
:
3853 return array
? 4 : 3;
3854 case GLSL_SAMPLER_DIM_3D
:
3855 case GLSL_SAMPLER_DIM_CUBE
:
3857 case GLSL_SAMPLER_DIM_RECT
:
3858 case GLSL_SAMPLER_DIM_SUBPASS
:
3860 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3869 /* Adjust the sample index according to FMASK.
3871 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3872 * which is the identity mapping. Each nibble says which physical sample
3873 * should be fetched to get that sample.
3875 * For example, 0x11111100 means there are only 2 samples stored and
3876 * the second sample covers 3/4 of the pixel. When reading samples 0
3877 * and 1, return physical sample 0 (determined by the first two 0s
3878 * in FMASK), otherwise return physical sample 1.
3880 * The sample index should be adjusted as follows:
3881 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3883 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3885 Builder
bld(ctx
->program
, ctx
->block
);
3886 Temp fmask
= bld
.tmp(v1
);
3887 unsigned dim
= ctx
->options
->chip_class
>= GFX10
3888 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
3891 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3892 load
->operands
[0] = Operand(coords
);
3893 load
->operands
[1] = Operand(fmask_desc_ptr
);
3894 load
->definitions
[0] = Definition(fmask
);
3901 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3902 ctx
->block
->instructions
.emplace_back(std::move(load
));
3904 Operand sample_index4
;
3905 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3906 sample_index4
= Operand(sample_index
.constantValue() << 2);
3907 } else if (sample_index
.regClass() == s1
) {
3908 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3910 assert(sample_index
.regClass() == v1
);
3911 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3915 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3916 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3917 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3918 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3920 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3922 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3923 * resource descriptor is 0 (invalid),
3925 Temp compare
= bld
.tmp(s2
);
3926 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3927 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3929 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3931 /* Replace the MSAA sample index. */
3932 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
3935 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
3938 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3939 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3940 bool is_array
= glsl_sampler_type_is_array(type
);
3941 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3942 assert(!add_frag_pos
&& "Input attachments should be lowered.");
3943 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3944 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3945 int count
= image_type_to_components_count(dim
, is_array
);
3946 std::vector
<Operand
> coords(count
);
3949 Operand sample_index
;
3950 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
3952 sample_index
= Operand(sample_cv
->u32
);
3954 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
3956 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
3957 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
3958 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
3959 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3960 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
3961 vec
->definitions
[0] = Definition(fmask_load_address
);
3962 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3964 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
3965 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
3968 coords
[count
] = sample_index
;
3971 if (count
== 1 && !gfx9_1d
)
3972 return emit_extract_vector(ctx
, src0
, 0, v1
);
3975 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
3976 coords
.resize(coords
.size() + 1);
3977 coords
[1] = Operand((uint32_t) 0);
3979 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
3981 for (int i
= 0; i
< count
; i
++)
3982 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3985 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
3986 for (unsigned i
= 0; i
< coords
.size(); i
++)
3987 vec
->operands
[i
] = coords
[i
];
3988 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
3989 vec
->definitions
[0] = Definition(res
);
3990 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3995 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3997 Builder
bld(ctx
->program
, ctx
->block
);
3998 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3999 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4000 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4001 bool is_array
= glsl_sampler_type_is_array(type
);
4002 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4004 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4005 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4006 unsigned num_channels
= util_last_bit(mask
);
4007 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4008 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4011 switch (num_channels
) {
4013 opcode
= aco_opcode::buffer_load_format_x
;
4016 opcode
= aco_opcode::buffer_load_format_xy
;
4019 opcode
= aco_opcode::buffer_load_format_xyz
;
4022 opcode
= aco_opcode::buffer_load_format_xyzw
;
4025 unreachable(">4 channel buffer image load");
4027 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4028 load
->operands
[0] = Operand(vindex
);
4029 load
->operands
[1] = Operand(rsrc
);
4030 load
->operands
[2] = Operand((uint32_t) 0);
4032 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4035 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4036 load
->definitions
[0] = Definition(tmp
);
4038 load
->barrier
= barrier_image
;
4039 ctx
->block
->instructions
.emplace_back(std::move(load
));
4041 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4045 Temp coords
= get_image_coords(ctx
, instr
, type
);
4046 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4048 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4049 unsigned num_components
= util_bitcount(dmask
);
4051 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4054 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4056 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
4057 load
->operands
[0] = Operand(coords
);
4058 load
->operands
[1] = Operand(resource
);
4059 load
->definitions
[0] = Definition(tmp
);
4060 load
->glc
= var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4061 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4062 load
->dmask
= dmask
;
4064 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4065 load
->barrier
= barrier_image
;
4066 ctx
->block
->instructions
.emplace_back(std::move(load
));
4068 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4072 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4074 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4075 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4076 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4077 bool is_array
= glsl_sampler_type_is_array(type
);
4078 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4080 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4082 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4083 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4084 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4086 switch (data
.size()) {
4088 opcode
= aco_opcode::buffer_store_format_x
;
4091 opcode
= aco_opcode::buffer_store_format_xy
;
4094 opcode
= aco_opcode::buffer_store_format_xyz
;
4097 opcode
= aco_opcode::buffer_store_format_xyzw
;
4100 unreachable(">4 channel buffer image store");
4102 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4103 store
->operands
[0] = Operand(vindex
);
4104 store
->operands
[1] = Operand(rsrc
);
4105 store
->operands
[2] = Operand((uint32_t) 0);
4106 store
->operands
[3] = Operand(data
);
4107 store
->idxen
= true;
4110 store
->disable_wqm
= true;
4111 store
->barrier
= barrier_image
;
4112 ctx
->program
->needs_exact
= true;
4113 ctx
->block
->instructions
.emplace_back(std::move(store
));
4117 assert(data
.type() == RegType::vgpr
);
4118 Temp coords
= get_image_coords(ctx
, instr
, type
);
4119 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4121 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
4122 store
->operands
[0] = Operand(coords
);
4123 store
->operands
[1] = Operand(resource
);
4124 store
->operands
[2] = Operand(s4
);
4125 store
->operands
[3] = Operand(data
);
4128 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4129 store
->dmask
= (1 << data
.size()) - 1;
4131 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4132 store
->disable_wqm
= true;
4133 store
->barrier
= barrier_image
;
4134 ctx
->program
->needs_exact
= true;
4135 ctx
->block
->instructions
.emplace_back(std::move(store
));
4139 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4141 /* return the previous value if dest is ever used */
4142 bool return_previous
= false;
4143 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4144 return_previous
= true;
4147 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4148 return_previous
= true;
4152 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4153 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4154 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4155 bool is_array
= glsl_sampler_type_is_array(type
);
4156 Builder
bld(ctx
->program
, ctx
->block
);
4158 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4159 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4161 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4162 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4164 aco_opcode buf_op
, image_op
;
4165 switch (instr
->intrinsic
) {
4166 case nir_intrinsic_image_deref_atomic_add
:
4167 buf_op
= aco_opcode::buffer_atomic_add
;
4168 image_op
= aco_opcode::image_atomic_add
;
4170 case nir_intrinsic_image_deref_atomic_umin
:
4171 buf_op
= aco_opcode::buffer_atomic_umin
;
4172 image_op
= aco_opcode::image_atomic_umin
;
4174 case nir_intrinsic_image_deref_atomic_imin
:
4175 buf_op
= aco_opcode::buffer_atomic_smin
;
4176 image_op
= aco_opcode::image_atomic_smin
;
4178 case nir_intrinsic_image_deref_atomic_umax
:
4179 buf_op
= aco_opcode::buffer_atomic_umax
;
4180 image_op
= aco_opcode::image_atomic_umax
;
4182 case nir_intrinsic_image_deref_atomic_imax
:
4183 buf_op
= aco_opcode::buffer_atomic_smax
;
4184 image_op
= aco_opcode::image_atomic_smax
;
4186 case nir_intrinsic_image_deref_atomic_and
:
4187 buf_op
= aco_opcode::buffer_atomic_and
;
4188 image_op
= aco_opcode::image_atomic_and
;
4190 case nir_intrinsic_image_deref_atomic_or
:
4191 buf_op
= aco_opcode::buffer_atomic_or
;
4192 image_op
= aco_opcode::image_atomic_or
;
4194 case nir_intrinsic_image_deref_atomic_xor
:
4195 buf_op
= aco_opcode::buffer_atomic_xor
;
4196 image_op
= aco_opcode::image_atomic_xor
;
4198 case nir_intrinsic_image_deref_atomic_exchange
:
4199 buf_op
= aco_opcode::buffer_atomic_swap
;
4200 image_op
= aco_opcode::image_atomic_swap
;
4202 case nir_intrinsic_image_deref_atomic_comp_swap
:
4203 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4204 image_op
= aco_opcode::image_atomic_cmpswap
;
4207 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4210 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4212 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4213 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4214 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4215 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4216 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4217 mubuf
->operands
[0] = Operand(vindex
);
4218 mubuf
->operands
[1] = Operand(resource
);
4219 mubuf
->operands
[2] = Operand((uint32_t)0);
4220 mubuf
->operands
[3] = Operand(data
);
4221 if (return_previous
)
4222 mubuf
->definitions
[0] = Definition(dst
);
4224 mubuf
->idxen
= true;
4225 mubuf
->glc
= return_previous
;
4226 mubuf
->dlc
= false; /* Not needed for atomics */
4227 mubuf
->disable_wqm
= true;
4228 mubuf
->barrier
= barrier_image
;
4229 ctx
->program
->needs_exact
= true;
4230 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4234 Temp coords
= get_image_coords(ctx
, instr
, type
);
4235 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4236 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
4237 mimg
->operands
[0] = Operand(coords
);
4238 mimg
->operands
[1] = Operand(resource
);
4239 mimg
->operands
[2] = Operand(s4
); /* no sampler */
4240 mimg
->operands
[3] = Operand(data
);
4241 if (return_previous
)
4242 mimg
->definitions
[0] = Definition(dst
);
4243 mimg
->glc
= return_previous
;
4244 mimg
->dlc
= false; /* Not needed for atomics */
4245 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4246 mimg
->dmask
= (1 << data
.size()) - 1;
4248 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4249 mimg
->disable_wqm
= true;
4250 mimg
->barrier
= barrier_image
;
4251 ctx
->program
->needs_exact
= true;
4252 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4256 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4258 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4259 Builder
bld(ctx
->program
, ctx
->block
);
4261 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4262 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4263 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
4264 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
4266 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4267 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
4269 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4270 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4271 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4273 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4275 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4276 * in case 12 (or 3?), we have to divide by 3:
4277 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4278 * use v_mul_hi_u32 with magic number to divide
4279 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4281 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4285 emit_extract_vector(ctx
, desc
, 2, dst
);
4289 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4291 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4292 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4293 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4294 bool is_array
= glsl_sampler_type_is_array(type
);
4295 Builder
bld(ctx
->program
, ctx
->block
);
4297 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4298 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4299 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4303 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4306 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4308 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4310 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4311 mimg
->operands
[0] = Operand(lod
);
4312 mimg
->operands
[1] = Operand(resource
);
4313 unsigned& dmask
= mimg
->dmask
;
4314 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4315 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4316 mimg
->da
= glsl_sampler_type_is_array(type
);
4317 mimg
->can_reorder
= true;
4318 Definition
& def
= mimg
->definitions
[0];
4319 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4321 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4322 glsl_sampler_type_is_array(type
)) {
4324 assert(instr
->dest
.ssa
.num_components
== 3);
4325 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4326 def
= Definition(tmp
);
4327 emit_split_vector(ctx
, tmp
, 3);
4329 /* divide 3rd value by 6 by multiplying with magic number */
4330 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4331 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4333 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4334 emit_extract_vector(ctx
, tmp
, 0, v1
),
4335 emit_extract_vector(ctx
, tmp
, 1, v1
),
4338 } else if (ctx
->options
->chip_class
== GFX9
&&
4339 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4340 glsl_sampler_type_is_array(type
)) {
4341 assert(instr
->dest
.ssa
.num_components
== 2);
4342 def
= Definition(dst
);
4345 def
= Definition(dst
);
4348 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4351 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4353 Builder
bld(ctx
->program
, ctx
->block
);
4354 unsigned num_components
= instr
->num_components
;
4356 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4357 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4358 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4360 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4361 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
);
4364 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4366 Builder
bld(ctx
->program
, ctx
->block
);
4367 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4368 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4369 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4372 if (ctx
->options
->chip_class
< GFX8
)
4373 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4375 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4377 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4378 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4380 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4381 ctx
->options
->chip_class
>= GFX8
;
4383 offset
= bld
.as_uniform(offset
);
4384 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4388 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4389 if (count
== 3 && smem
) {
4390 writemask
|= 1u << (start
+ 2);
4393 int num_bytes
= count
* elem_size_bytes
;
4395 if (num_bytes
> 16) {
4396 assert(elem_size_bytes
== 8);
4397 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4402 // TODO: check alignment of sub-dword stores
4403 // TODO: split 3 bytes. there is no store instruction for that
4406 if (count
!= instr
->num_components
) {
4407 emit_split_vector(ctx
, data
, instr
->num_components
);
4408 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4409 for (int i
= 0; i
< count
; i
++) {
4410 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4411 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4413 write_data
= bld
.tmp(smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4414 vec
->definitions
[0] = Definition(write_data
);
4415 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4416 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4417 assert(num_bytes
% 4 == 0);
4418 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4419 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4420 assert(num_bytes
% 4 == 0);
4421 write_data
= bld
.as_uniform(data
);
4426 aco_opcode vmem_op
, smem_op
;
4427 switch (num_bytes
) {
4429 vmem_op
= aco_opcode::buffer_store_dword
;
4430 smem_op
= aco_opcode::s_buffer_store_dword
;
4433 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4434 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4437 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4438 smem_op
= aco_opcode::last_opcode
;
4442 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4443 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4446 unreachable("Store SSBO not implemented for this size.");
4448 if (ctx
->stage
== fragment_fs
)
4449 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4452 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4453 store
->operands
[0] = Operand(rsrc
);
4455 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4456 offset
, Operand(start
* elem_size_bytes
));
4457 store
->operands
[1] = Operand(off
);
4459 store
->operands
[1] = Operand(offset
);
4461 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4462 store
->operands
[1].setFixed(m0
);
4463 store
->operands
[2] = Operand(write_data
);
4464 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4466 store
->disable_wqm
= true;
4467 store
->barrier
= barrier_buffer
;
4468 ctx
->block
->instructions
.emplace_back(std::move(store
));
4469 ctx
->program
->wb_smem_l1_on_end
= true;
4470 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4471 ctx
->block
->kind
|= block_kind_needs_lowering
;
4472 ctx
->program
->needs_exact
= true;
4475 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4476 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4477 store
->operands
[1] = Operand(rsrc
);
4478 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4479 store
->operands
[3] = Operand(write_data
);
4480 store
->offset
= start
* elem_size_bytes
;
4481 store
->offen
= (offset
.type() == RegType::vgpr
);
4482 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4484 store
->disable_wqm
= true;
4485 store
->barrier
= barrier_buffer
;
4486 ctx
->program
->needs_exact
= true;
4487 ctx
->block
->instructions
.emplace_back(std::move(store
));
4492 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4494 /* return the previous value if dest is ever used */
4495 bool return_previous
= false;
4496 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4497 return_previous
= true;
4500 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4501 return_previous
= true;
4505 Builder
bld(ctx
->program
, ctx
->block
);
4506 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4508 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4509 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4510 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4513 if (ctx
->options
->chip_class
< GFX8
)
4514 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4516 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4518 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4519 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4521 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4523 aco_opcode op32
, op64
;
4524 switch (instr
->intrinsic
) {
4525 case nir_intrinsic_ssbo_atomic_add
:
4526 op32
= aco_opcode::buffer_atomic_add
;
4527 op64
= aco_opcode::buffer_atomic_add_x2
;
4529 case nir_intrinsic_ssbo_atomic_imin
:
4530 op32
= aco_opcode::buffer_atomic_smin
;
4531 op64
= aco_opcode::buffer_atomic_smin_x2
;
4533 case nir_intrinsic_ssbo_atomic_umin
:
4534 op32
= aco_opcode::buffer_atomic_umin
;
4535 op64
= aco_opcode::buffer_atomic_umin_x2
;
4537 case nir_intrinsic_ssbo_atomic_imax
:
4538 op32
= aco_opcode::buffer_atomic_smax
;
4539 op64
= aco_opcode::buffer_atomic_smax_x2
;
4541 case nir_intrinsic_ssbo_atomic_umax
:
4542 op32
= aco_opcode::buffer_atomic_umax
;
4543 op64
= aco_opcode::buffer_atomic_umax_x2
;
4545 case nir_intrinsic_ssbo_atomic_and
:
4546 op32
= aco_opcode::buffer_atomic_and
;
4547 op64
= aco_opcode::buffer_atomic_and_x2
;
4549 case nir_intrinsic_ssbo_atomic_or
:
4550 op32
= aco_opcode::buffer_atomic_or
;
4551 op64
= aco_opcode::buffer_atomic_or_x2
;
4553 case nir_intrinsic_ssbo_atomic_xor
:
4554 op32
= aco_opcode::buffer_atomic_xor
;
4555 op64
= aco_opcode::buffer_atomic_xor_x2
;
4557 case nir_intrinsic_ssbo_atomic_exchange
:
4558 op32
= aco_opcode::buffer_atomic_swap
;
4559 op64
= aco_opcode::buffer_atomic_swap_x2
;
4561 case nir_intrinsic_ssbo_atomic_comp_swap
:
4562 op32
= aco_opcode::buffer_atomic_cmpswap
;
4563 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4566 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4568 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4569 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4570 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4571 mubuf
->operands
[1] = Operand(rsrc
);
4572 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4573 mubuf
->operands
[3] = Operand(data
);
4574 if (return_previous
)
4575 mubuf
->definitions
[0] = Definition(dst
);
4577 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4578 mubuf
->glc
= return_previous
;
4579 mubuf
->dlc
= false; /* Not needed for atomics */
4580 mubuf
->disable_wqm
= true;
4581 mubuf
->barrier
= barrier_buffer
;
4582 ctx
->program
->needs_exact
= true;
4583 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4586 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4588 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4589 Builder
bld(ctx
->program
, ctx
->block
);
4590 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4591 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4594 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4596 Builder
bld(ctx
->program
, ctx
->block
);
4597 unsigned num_components
= instr
->num_components
;
4598 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4600 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4601 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4603 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4604 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4606 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4607 bool global
= ctx
->options
->chip_class
>= GFX9
;
4609 switch (num_bytes
) {
4611 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4614 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4617 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4620 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4623 unreachable("load_global not implemented for this size.");
4625 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4626 flat
->operands
[0] = Operand(addr
);
4627 flat
->operands
[1] = Operand(s1
);
4631 if (dst
.type() == RegType::sgpr
) {
4632 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4633 flat
->definitions
[0] = Definition(vec
);
4634 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4635 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4637 flat
->definitions
[0] = Definition(dst
);
4638 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4640 emit_split_vector(ctx
, dst
, num_components
);
4642 switch (num_bytes
) {
4644 op
= aco_opcode::s_load_dword
;
4647 op
= aco_opcode::s_load_dwordx2
;
4651 op
= aco_opcode::s_load_dwordx4
;
4654 unreachable("load_global not implemented for this size.");
4656 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4657 load
->operands
[0] = Operand(addr
);
4658 load
->operands
[1] = Operand(0u);
4659 load
->definitions
[0] = Definition(dst
);
4662 load
->barrier
= barrier_buffer
;
4663 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4665 if (dst
.size() == 3) {
4667 Temp vec
= bld
.tmp(s4
);
4668 load
->definitions
[0] = Definition(vec
);
4669 ctx
->block
->instructions
.emplace_back(std::move(load
));
4670 emit_split_vector(ctx
, vec
, 4);
4672 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4673 emit_extract_vector(ctx
, vec
, 0, s1
),
4674 emit_extract_vector(ctx
, vec
, 1, s1
),
4675 emit_extract_vector(ctx
, vec
, 2, s1
));
4677 ctx
->block
->instructions
.emplace_back(std::move(load
));
4682 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4684 Builder
bld(ctx
->program
, ctx
->block
);
4685 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4687 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4688 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4690 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4693 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4694 unsigned num_bytes
= count
* elem_size_bytes
;
4696 Temp write_data
= data
;
4697 if (count
!= instr
->num_components
) {
4698 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4699 for (int i
= 0; i
< count
; i
++)
4700 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4701 write_data
= bld
.tmp(RegType::vgpr
, count
);
4702 vec
->definitions
[0] = Definition(write_data
);
4703 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4706 unsigned offset
= start
* elem_size_bytes
;
4707 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4708 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4709 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4710 Temp carry
= bld
.tmp(s2
);
4711 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4713 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4714 Operand(offset
), addr0
);
4715 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(s2
),
4717 carry
).def(1).setHint(vcc
);
4719 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4724 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4725 bool global
= ctx
->options
->chip_class
>= GFX9
;
4727 switch (num_bytes
) {
4729 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4732 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4735 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4738 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4741 unreachable("store_global not implemented for this size.");
4743 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4744 flat
->operands
[0] = Operand(addr
);
4745 flat
->operands
[1] = Operand(s1
);
4746 flat
->operands
[2] = Operand(data
);
4749 flat
->offset
= offset
;
4750 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4754 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4755 Builder
bld(ctx
->program
, ctx
->block
);
4756 switch(instr
->intrinsic
) {
4757 case nir_intrinsic_group_memory_barrier
:
4758 case nir_intrinsic_memory_barrier
:
4759 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4761 case nir_intrinsic_memory_barrier_atomic_counter
:
4762 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4764 case nir_intrinsic_memory_barrier_buffer
:
4765 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4767 case nir_intrinsic_memory_barrier_image
:
4768 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4770 case nir_intrinsic_memory_barrier_shared
:
4771 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4774 unreachable("Unimplemented memory barrier intrinsic");
4779 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4781 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4782 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4783 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4784 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4785 Builder
bld(ctx
->program
, ctx
->block
);
4787 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4788 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4789 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
4792 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4794 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4795 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4796 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4797 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4798 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4800 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4801 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
4804 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4806 unsigned offset
= nir_intrinsic_base(instr
);
4807 Operand m
= load_lds_size_m0(ctx
);
4808 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4809 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4811 unsigned num_operands
= 3;
4812 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4813 switch(instr
->intrinsic
) {
4814 case nir_intrinsic_shared_atomic_add
:
4815 op32
= aco_opcode::ds_add_u32
;
4816 op64
= aco_opcode::ds_add_u64
;
4817 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4818 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4820 case nir_intrinsic_shared_atomic_imin
:
4821 op32
= aco_opcode::ds_min_i32
;
4822 op64
= aco_opcode::ds_min_i64
;
4823 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4824 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4826 case nir_intrinsic_shared_atomic_umin
:
4827 op32
= aco_opcode::ds_min_u32
;
4828 op64
= aco_opcode::ds_min_u64
;
4829 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4830 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4832 case nir_intrinsic_shared_atomic_imax
:
4833 op32
= aco_opcode::ds_max_i32
;
4834 op64
= aco_opcode::ds_max_i64
;
4835 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4836 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4838 case nir_intrinsic_shared_atomic_umax
:
4839 op32
= aco_opcode::ds_max_u32
;
4840 op64
= aco_opcode::ds_max_u64
;
4841 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
4842 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
4844 case nir_intrinsic_shared_atomic_and
:
4845 op32
= aco_opcode::ds_and_b32
;
4846 op64
= aco_opcode::ds_and_b64
;
4847 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
4848 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
4850 case nir_intrinsic_shared_atomic_or
:
4851 op32
= aco_opcode::ds_or_b32
;
4852 op64
= aco_opcode::ds_or_b64
;
4853 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
4854 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
4856 case nir_intrinsic_shared_atomic_xor
:
4857 op32
= aco_opcode::ds_xor_b32
;
4858 op64
= aco_opcode::ds_xor_b64
;
4859 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
4860 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
4862 case nir_intrinsic_shared_atomic_exchange
:
4863 op32
= aco_opcode::ds_write_b32
;
4864 op64
= aco_opcode::ds_write_b64
;
4865 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
4866 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
4868 case nir_intrinsic_shared_atomic_comp_swap
:
4869 op32
= aco_opcode::ds_cmpst_b32
;
4870 op64
= aco_opcode::ds_cmpst_b64
;
4871 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
4872 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
4876 unreachable("Unhandled shared atomic intrinsic");
4879 /* return the previous value if dest is ever used */
4880 bool return_previous
= false;
4881 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4882 return_previous
= true;
4885 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4886 return_previous
= true;
4891 if (data
.size() == 1) {
4892 assert(instr
->dest
.ssa
.bit_size
== 32);
4893 op
= return_previous
? op32_rtn
: op32
;
4895 assert(instr
->dest
.ssa
.bit_size
== 64);
4896 op
= return_previous
? op64_rtn
: op64
;
4899 if (offset
> 65535) {
4900 Builder
bld(ctx
->program
, ctx
->block
);
4901 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
4905 aco_ptr
<DS_instruction
> ds
;
4906 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
4907 ds
->operands
[0] = Operand(address
);
4908 ds
->operands
[1] = Operand(data
);
4909 if (num_operands
== 4)
4910 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4911 ds
->operands
[num_operands
- 1] = m
;
4912 ds
->offset0
= offset
;
4913 if (return_previous
)
4914 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
4915 ctx
->block
->instructions
.emplace_back(std::move(ds
));
4918 Temp
get_scratch_resource(isel_context
*ctx
)
4920 Builder
bld(ctx
->program
, ctx
->block
);
4921 Temp scratch_addr
= ctx
->private_segment_buffer
;
4922 if (ctx
->stage
!= compute_cs
)
4923 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), ctx
->private_segment_buffer
, Operand(0u));
4925 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
4926 S_008F0C_INDEX_STRIDE(ctx
->options
->wave_size
== 64 ? 3 : 2);;
4928 if (ctx
->program
->chip_class
>= GFX10
) {
4929 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
4930 S_008F0C_OOB_SELECT(3) |
4931 S_008F0C_RESOURCE_LEVEL(1);
4932 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
4933 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4934 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4937 /* older generations need element size = 16 bytes. element size removed in GFX9 */
4938 if (ctx
->program
->chip_class
<= GFX8
)
4939 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
4941 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4944 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4945 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
4946 Builder
bld(ctx
->program
, ctx
->block
);
4947 Temp rsrc
= get_scratch_resource(ctx
);
4948 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4949 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4952 switch (dst
.size()) {
4954 op
= aco_opcode::buffer_load_dword
;
4957 op
= aco_opcode::buffer_load_dwordx2
;
4960 op
= aco_opcode::buffer_load_dwordx3
;
4963 op
= aco_opcode::buffer_load_dwordx4
;
4967 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4968 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
4969 bld
.def(v4
), offset
, rsrc
,
4970 ctx
->scratch_offset
, 0, true);
4971 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
4972 aco_opcode::buffer_load_dwordx4
,
4973 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
4974 offset
, rsrc
, ctx
->scratch_offset
, 16, true);
4975 emit_split_vector(ctx
, lower
, 2);
4976 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
4977 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
4978 if (dst
.size() == 8) {
4979 emit_split_vector(ctx
, upper
, 2);
4980 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
4981 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
4986 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
4987 Format::PSEUDO
, dst
.size() / 2, 1)};
4988 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
4989 vec
->operands
[i
] = Operand(elems
[i
]);
4990 vec
->definitions
[0] = Definition(dst
);
4991 bld
.insert(std::move(vec
));
4992 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
4996 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
4999 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->scratch_offset
, 0, true);
5000 emit_split_vector(ctx
, dst
, instr
->num_components
);
5003 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5004 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
5005 Builder
bld(ctx
->program
, ctx
->block
);
5006 Temp rsrc
= get_scratch_resource(ctx
);
5007 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5008 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5010 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5011 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5015 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5016 int num_bytes
= count
* elem_size_bytes
;
5018 if (num_bytes
> 16) {
5019 assert(elem_size_bytes
== 8);
5020 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5025 // TODO: check alignment of sub-dword stores
5026 // TODO: split 3 bytes. there is no store instruction for that
5029 if (count
!= instr
->num_components
) {
5030 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5031 for (int i
= 0; i
< count
; i
++) {
5032 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
5033 vec
->operands
[i
] = Operand(elem
);
5035 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
5036 vec
->definitions
[0] = Definition(write_data
);
5037 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5043 switch (num_bytes
) {
5045 op
= aco_opcode::buffer_store_dword
;
5048 op
= aco_opcode::buffer_store_dwordx2
;
5051 op
= aco_opcode::buffer_store_dwordx3
;
5054 op
= aco_opcode::buffer_store_dwordx4
;
5057 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5060 bld
.mubuf(op
, offset
, rsrc
, ctx
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
5064 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5065 uint8_t log2_ps_iter_samples
;
5066 if (ctx
->program
->info
->ps
.force_persample
) {
5067 log2_ps_iter_samples
=
5068 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
5070 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
5073 /* The bit pattern matches that used by fixed function fragment
5075 static const unsigned ps_iter_masks
[] = {
5076 0xffff, /* not used */
5082 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
5084 Builder
bld(ctx
->program
, ctx
->block
);
5086 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::ancillary
], Operand(8u), Operand(4u));
5087 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
5088 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
5089 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5090 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, ctx
->fs_inputs
[fs_input::sample_coverage
]);
5093 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
5095 Builder
bld(ctx
->program
, ctx
->block
);
5097 if (cluster_size
== 1) {
5099 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5100 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5101 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5102 return bld
.sop1(aco_opcode::s_not_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5103 bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
));
5104 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5105 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5106 return bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5107 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)));
5108 } else if (op
== nir_op_iand
&& cluster_size
== 64) {
5109 //subgroupAnd(val) -> (exec & ~val) == 0
5110 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5111 return bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), tmp
, Operand(0u));
5112 } else if (op
== nir_op_ior
&& cluster_size
== 64) {
5113 //subgroupOr(val) -> (val & exec) != 0
5114 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)).def(1).getTemp();
5115 } else if (op
== nir_op_ixor
&& cluster_size
== 64) {
5116 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5117 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5118 tmp
= bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
);
5119 return bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5121 //subgroupClustered{And,Or,Xor}(val, n) ->
5122 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0))
5123 //cluster_offset = ~(n - 1) & lane_id
5124 //cluster_mask = ((1 << n) - 1)
5125 //subgroupClusteredAnd():
5126 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5127 //subgroupClusteredOr():
5128 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5129 //subgroupClusteredXor():
5130 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5131 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5132 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5133 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5136 if (op
== nir_op_iand
)
5137 tmp
= bld
.sop2(aco_opcode::s_orn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5139 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5141 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5142 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5143 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5144 if (cluster_mask
!= 0xffffffff)
5145 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5147 Definition cmp_def
= Definition();
5148 if (op
== nir_op_iand
) {
5149 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(cluster_mask
), tmp
).def(0);
5150 } else if (op
== nir_op_ior
) {
5151 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5152 } else if (op
== nir_op_ixor
) {
5153 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5154 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5155 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5157 cmp_def
.setHint(vcc
);
5158 return cmp_def
.getTemp();
5162 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5164 Builder
bld(ctx
->program
, ctx
->block
);
5166 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5167 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5168 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5170 if (op
== nir_op_iand
)
5171 tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5173 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5175 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5176 Temp lo
= lohi
.def(0).getTemp();
5177 Temp hi
= lohi
.def(1).getTemp();
5178 Temp mbcnt
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), hi
,
5179 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), lo
, Operand(0u)));
5181 Definition cmp_def
= Definition();
5182 if (op
== nir_op_iand
)
5183 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5184 else if (op
== nir_op_ior
)
5185 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5186 else if (op
== nir_op_ixor
)
5187 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u),
5188 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5189 cmp_def
.setHint(vcc
);
5190 return cmp_def
.getTemp();
5193 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5195 Builder
bld(ctx
->program
, ctx
->block
);
5197 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5198 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5199 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5200 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5201 if (op
== nir_op_iand
)
5202 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5203 else if (op
== nir_op_ior
)
5204 return bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5205 else if (op
== nir_op_ixor
)
5206 return bld
.sop2(aco_opcode::s_xor_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5212 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5214 Builder
bld(ctx
->program
, ctx
->block
);
5215 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5216 if (src
.regClass().type() == RegType::vgpr
) {
5217 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5218 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5219 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(dst
), Operand(0u), Operand(src
));
5220 } else if (src
.regClass() == s1
) {
5221 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5222 } else if (src
.regClass() == s2
) {
5223 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5225 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5226 nir_print_instr(&instr
->instr
, stderr
);
5227 fprintf(stderr
, "\n");
5231 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5233 Builder
bld(ctx
->program
, ctx
->block
);
5234 Temp p1
= ctx
->fs_inputs
[fs_input::persp_center_p1
];
5235 Temp p2
= ctx
->fs_inputs
[fs_input::persp_center_p2
];
5238 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_quad_perm(0, 0, 0, 0));
5239 Temp ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(1, 1, 1, 1));
5240 Temp ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(2, 2, 2, 2));
5241 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_quad_perm(0, 0, 0, 0));
5242 Temp ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(1, 1, 1, 1));
5243 Temp ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(2, 2, 2, 2));
5245 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5246 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5247 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5248 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5249 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5250 Temp wqm1
= bld
.tmp(v1
);
5251 emit_wqm(ctx
, tmp1
, wqm1
, true);
5252 Temp wqm2
= bld
.tmp(v1
);
5253 emit_wqm(ctx
, tmp2
, wqm2
, true);
5254 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5258 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5260 Builder
bld(ctx
->program
, ctx
->block
);
5261 switch(instr
->intrinsic
) {
5262 case nir_intrinsic_load_barycentric_sample
:
5263 case nir_intrinsic_load_barycentric_pixel
:
5264 case nir_intrinsic_load_barycentric_centroid
: {
5265 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5266 fs_input input
= get_interp_input(instr
->intrinsic
, mode
);
5268 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5269 if (input
== fs_input::max_inputs
) {
5270 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5271 Operand(0u), Operand(0u));
5273 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5274 ctx
->fs_inputs
[input
],
5275 ctx
->fs_inputs
[input
+ 1]);
5277 emit_split_vector(ctx
, dst
, 2);
5280 case nir_intrinsic_load_barycentric_at_sample
: {
5281 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5282 switch (ctx
->options
->key
.fs
.num_samples
) {
5283 case 2: sample_pos_offset
+= 1 << 3; break;
5284 case 4: sample_pos_offset
+= 3 << 3; break;
5285 case 8: sample_pos_offset
+= 7 << 3; break;
5289 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5290 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5291 if (addr
.type() == RegType::sgpr
) {
5294 sample_pos_offset
+= const_addr
->u32
<< 3;
5295 offset
= Operand(sample_pos_offset
);
5296 } else if (ctx
->options
->chip_class
>= GFX9
) {
5297 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5299 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5300 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5302 addr
= ctx
->private_segment_buffer
;
5303 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), addr
, Operand(offset
));
5305 } else if (ctx
->options
->chip_class
>= GFX9
) {
5306 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5307 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, ctx
->private_segment_buffer
, sample_pos_offset
);
5309 /* addr += ctx->private_segment_buffer + sample_pos_offset */
5310 Temp tmp0
= bld
.tmp(s1
);
5311 Temp tmp1
= bld
.tmp(s1
);
5312 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), ctx
->private_segment_buffer
);
5313 Definition scc_tmp
= bld
.def(s1
, scc
);
5314 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5315 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
5316 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5317 Temp pck0
= bld
.tmp(v1
);
5318 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5319 tmp1
= as_vgpr(ctx
, tmp1
);
5320 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(s2
)), tmp1
, Operand(0u), carry
);
5321 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5323 /* sample_pos = flat_load_dwordx2 addr */
5324 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5327 /* sample_pos -= 0.5 */
5328 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5329 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5330 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5331 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5332 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5334 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5337 case nir_intrinsic_load_barycentric_at_offset
: {
5338 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5339 RegClass rc
= RegClass(offset
.type(), 1);
5340 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5341 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5342 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5345 case nir_intrinsic_load_front_face
: {
5346 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5347 Operand(0u), ctx
->fs_inputs
[fs_input::front_face
]).def(0).setHint(vcc
);
5350 case nir_intrinsic_load_view_index
:
5351 case nir_intrinsic_load_layer_id
: {
5352 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5353 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5354 bld
.copy(Definition(dst
), Operand(ctx
->view_index
));
5358 unsigned idx
= nir_intrinsic_base(instr
);
5359 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5360 Operand(2u), bld
.m0(ctx
->prim_mask
), idx
, 0);
5363 case nir_intrinsic_load_frag_coord
: {
5364 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5367 case nir_intrinsic_load_sample_pos
: {
5368 Temp posx
= ctx
->fs_inputs
[fs_input::frag_pos_0
];
5369 Temp posy
= ctx
->fs_inputs
[fs_input::frag_pos_1
];
5370 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5371 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5372 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5375 case nir_intrinsic_load_interpolated_input
:
5376 visit_load_interpolated_input(ctx
, instr
);
5378 case nir_intrinsic_store_output
:
5379 visit_store_output(ctx
, instr
);
5381 case nir_intrinsic_load_input
:
5382 visit_load_input(ctx
, instr
);
5384 case nir_intrinsic_load_ubo
:
5385 visit_load_ubo(ctx
, instr
);
5387 case nir_intrinsic_load_push_constant
:
5388 visit_load_push_constant(ctx
, instr
);
5390 case nir_intrinsic_load_constant
:
5391 visit_load_constant(ctx
, instr
);
5393 case nir_intrinsic_vulkan_resource_index
:
5394 visit_load_resource(ctx
, instr
);
5396 case nir_intrinsic_discard
:
5397 visit_discard(ctx
, instr
);
5399 case nir_intrinsic_discard_if
:
5400 visit_discard_if(ctx
, instr
);
5402 case nir_intrinsic_load_shared
:
5403 visit_load_shared(ctx
, instr
);
5405 case nir_intrinsic_store_shared
:
5406 visit_store_shared(ctx
, instr
);
5408 case nir_intrinsic_shared_atomic_add
:
5409 case nir_intrinsic_shared_atomic_imin
:
5410 case nir_intrinsic_shared_atomic_umin
:
5411 case nir_intrinsic_shared_atomic_imax
:
5412 case nir_intrinsic_shared_atomic_umax
:
5413 case nir_intrinsic_shared_atomic_and
:
5414 case nir_intrinsic_shared_atomic_or
:
5415 case nir_intrinsic_shared_atomic_xor
:
5416 case nir_intrinsic_shared_atomic_exchange
:
5417 case nir_intrinsic_shared_atomic_comp_swap
:
5418 visit_shared_atomic(ctx
, instr
);
5420 case nir_intrinsic_image_deref_load
:
5421 visit_image_load(ctx
, instr
);
5423 case nir_intrinsic_image_deref_store
:
5424 visit_image_store(ctx
, instr
);
5426 case nir_intrinsic_image_deref_atomic_add
:
5427 case nir_intrinsic_image_deref_atomic_umin
:
5428 case nir_intrinsic_image_deref_atomic_imin
:
5429 case nir_intrinsic_image_deref_atomic_umax
:
5430 case nir_intrinsic_image_deref_atomic_imax
:
5431 case nir_intrinsic_image_deref_atomic_and
:
5432 case nir_intrinsic_image_deref_atomic_or
:
5433 case nir_intrinsic_image_deref_atomic_xor
:
5434 case nir_intrinsic_image_deref_atomic_exchange
:
5435 case nir_intrinsic_image_deref_atomic_comp_swap
:
5436 visit_image_atomic(ctx
, instr
);
5438 case nir_intrinsic_image_deref_size
:
5439 visit_image_size(ctx
, instr
);
5441 case nir_intrinsic_load_ssbo
:
5442 visit_load_ssbo(ctx
, instr
);
5444 case nir_intrinsic_store_ssbo
:
5445 visit_store_ssbo(ctx
, instr
);
5447 case nir_intrinsic_load_global
:
5448 visit_load_global(ctx
, instr
);
5450 case nir_intrinsic_store_global
:
5451 visit_store_global(ctx
, instr
);
5453 case nir_intrinsic_ssbo_atomic_add
:
5454 case nir_intrinsic_ssbo_atomic_imin
:
5455 case nir_intrinsic_ssbo_atomic_umin
:
5456 case nir_intrinsic_ssbo_atomic_imax
:
5457 case nir_intrinsic_ssbo_atomic_umax
:
5458 case nir_intrinsic_ssbo_atomic_and
:
5459 case nir_intrinsic_ssbo_atomic_or
:
5460 case nir_intrinsic_ssbo_atomic_xor
:
5461 case nir_intrinsic_ssbo_atomic_exchange
:
5462 case nir_intrinsic_ssbo_atomic_comp_swap
:
5463 visit_atomic_ssbo(ctx
, instr
);
5465 case nir_intrinsic_load_scratch
:
5466 visit_load_scratch(ctx
, instr
);
5468 case nir_intrinsic_store_scratch
:
5469 visit_store_scratch(ctx
, instr
);
5471 case nir_intrinsic_get_buffer_size
:
5472 visit_get_buffer_size(ctx
, instr
);
5474 case nir_intrinsic_barrier
: {
5475 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5476 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5477 if (workgroup_size
> 64)
5478 bld
.sopp(aco_opcode::s_barrier
);
5481 case nir_intrinsic_group_memory_barrier
:
5482 case nir_intrinsic_memory_barrier
:
5483 case nir_intrinsic_memory_barrier_atomic_counter
:
5484 case nir_intrinsic_memory_barrier_buffer
:
5485 case nir_intrinsic_memory_barrier_image
:
5486 case nir_intrinsic_memory_barrier_shared
:
5487 emit_memory_barrier(ctx
, instr
);
5489 case nir_intrinsic_load_num_work_groups
:
5490 case nir_intrinsic_load_work_group_id
:
5491 case nir_intrinsic_load_local_invocation_id
: {
5492 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5494 if (instr
->intrinsic
== nir_intrinsic_load_num_work_groups
)
5495 ids
= ctx
->num_workgroups
;
5496 else if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
5497 ids
= ctx
->workgroup_ids
;
5499 ids
= ctx
->local_invocation_ids
;
5500 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5501 ids
[0].id() ? Operand(ids
[0]) : Operand(1u),
5502 ids
[1].id() ? Operand(ids
[1]) : Operand(1u),
5503 ids
[2].id() ? Operand(ids
[2]) : Operand(1u));
5504 emit_split_vector(ctx
, dst
, 3);
5507 case nir_intrinsic_load_local_invocation_index
: {
5508 Temp id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5509 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5510 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5511 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5514 case nir_intrinsic_load_subgroup_id
: {
5515 if (ctx
->stage
== compute_cs
) {
5516 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5517 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), tg_num
, Operand(0x6u
));
5519 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5523 case nir_intrinsic_load_subgroup_invocation
: {
5524 bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand((uint32_t) -1),
5525 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5528 case nir_intrinsic_load_num_subgroups
: {
5529 if (ctx
->stage
== compute_cs
)
5530 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
), ctx
->tg_size
);
5532 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5535 case nir_intrinsic_ballot
: {
5536 Definition tmp
= bld
.def(s2
);
5537 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5538 if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s2
) {
5539 bld
.sop2(aco_opcode::s_and_b64
, tmp
, bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5540 } else if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s1
) {
5541 bld
.sop2(aco_opcode::s_cselect_b64
, tmp
, Operand(exec
, s2
), Operand(0u), bld
.scc(src
));
5542 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5543 bld
.vopc(aco_opcode::v_cmp_lg_u32
, tmp
, Operand(0u), src
);
5544 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5545 bld
.vopc(aco_opcode::v_cmp_lg_u64
, tmp
, Operand(0u), src
);
5547 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5548 nir_print_instr(&instr
->instr
, stderr
);
5549 fprintf(stderr
, "\n");
5551 emit_wqm(ctx
, tmp
.getTemp(), get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5554 case nir_intrinsic_shuffle
: {
5555 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5556 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5557 emit_uniform_subgroup(ctx
, instr
, src
);
5559 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5560 assert(tid
.regClass() == v1
);
5561 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5562 if (src
.regClass() == v1
) {
5563 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
5564 } else if (src
.regClass() == v2
) {
5565 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5566 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5567 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
5568 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
5569 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5570 emit_split_vector(ctx
, dst
, 2);
5571 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5572 Temp tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5573 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5574 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5575 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
), dst
);
5577 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5578 nir_print_instr(&instr
->instr
, stderr
);
5579 fprintf(stderr
, "\n");
5584 case nir_intrinsic_load_sample_id
: {
5585 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5586 ctx
->fs_inputs
[ancillary
], Operand(8u), Operand(4u));
5589 case nir_intrinsic_load_sample_mask_in
: {
5590 visit_load_sample_mask_in(ctx
, instr
);
5593 case nir_intrinsic_read_first_invocation
: {
5594 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5595 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5596 if (src
.regClass() == v1
) {
5598 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
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
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5604 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), 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 && src
.regClass() == s2
) {
5609 bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
,
5610 bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
))),
5612 } else if (src
.regClass() == s1
) {
5613 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5614 } else if (src
.regClass() == s2
) {
5615 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5617 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5618 nir_print_instr(&instr
->instr
, stderr
);
5619 fprintf(stderr
, "\n");
5623 case nir_intrinsic_read_invocation
: {
5624 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5625 Temp lane
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5626 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5627 assert(lane
.regClass() == s1
);
5628 if (src
.regClass() == v1
) {
5629 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), src
, lane
), dst
);
5630 } else if (src
.regClass() == v2
) {
5631 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5632 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5633 lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), lo
, lane
));
5634 hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), hi
, lane
));
5635 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5636 emit_split_vector(ctx
, dst
, 2);
5637 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5638 emit_wqm(ctx
, bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
, lane
), dst
);
5639 } else if (src
.regClass() == s1
) {
5640 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5641 } else if (src
.regClass() == s2
) {
5642 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5644 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5645 nir_print_instr(&instr
->instr
, stderr
);
5646 fprintf(stderr
, "\n");
5650 case nir_intrinsic_vote_all
: {
5651 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5652 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5653 assert(src
.regClass() == s2
);
5654 assert(dst
.regClass() == s1
);
5656 Definition tmp
= bld
.def(s1
);
5657 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(tmp
),
5658 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)),
5660 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5663 case nir_intrinsic_vote_any
: {
5664 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5665 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5666 assert(src
.regClass() == s2
);
5667 assert(dst
.regClass() == s1
);
5669 Definition tmp
= bld
.def(s1
);
5670 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(tmp
), src
, Operand(exec
, s2
));
5671 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5674 case nir_intrinsic_reduce
:
5675 case nir_intrinsic_inclusive_scan
:
5676 case nir_intrinsic_exclusive_scan
: {
5677 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5678 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5679 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5680 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5681 nir_intrinsic_cluster_size(instr
) : 0;
5682 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: 64, 64));
5684 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5685 emit_uniform_subgroup(ctx
, instr
, src
);
5686 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5687 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5689 else if (op
== nir_op_iadd
)
5691 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5693 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5695 switch (instr
->intrinsic
) {
5696 case nir_intrinsic_reduce
:
5697 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5699 case nir_intrinsic_exclusive_scan
:
5700 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5702 case nir_intrinsic_inclusive_scan
:
5703 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5708 } else if (cluster_size
== 1) {
5709 bld
.copy(Definition(dst
), src
);
5711 src
= as_vgpr(ctx
, src
);
5715 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5730 unreachable("unknown reduction op");
5735 switch (instr
->intrinsic
) {
5736 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5737 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5738 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5740 unreachable("unknown reduce intrinsic");
5743 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5744 reduce
->operands
[0] = Operand(src
);
5745 // filled in by aco_reduce_assign.cpp, used internally as part of the
5747 assert(dst
.size() == 1 || dst
.size() == 2);
5748 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5749 reduce
->operands
[2] = Operand(v1
.as_linear());
5751 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5752 reduce
->definitions
[0] = Definition(tmp_dst
);
5753 reduce
->definitions
[1] = bld
.def(s2
); // used internally
5754 reduce
->definitions
[2] = Definition();
5755 reduce
->definitions
[3] = Definition(scc
, s1
);
5756 reduce
->definitions
[4] = Definition();
5757 reduce
->reduce_op
= reduce_op
;
5758 reduce
->cluster_size
= cluster_size
;
5759 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5761 emit_wqm(ctx
, tmp_dst
, dst
);
5765 case nir_intrinsic_quad_broadcast
: {
5766 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5767 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5768 emit_uniform_subgroup(ctx
, instr
, src
);
5770 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5771 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5772 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5773 uint32_t half_mask
= 0x11111111u
<< lane
;
5774 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5775 Temp tmp
= bld
.tmp(s2
);
5776 bld
.sop1(aco_opcode::s_wqm_b64
, Definition(tmp
),
5777 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mask_tmp
,
5778 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
))));
5779 emit_wqm(ctx
, tmp
, dst
);
5780 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5782 bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
,
5783 dpp_quad_perm(lane
, lane
, lane
, lane
)),
5785 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5786 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5787 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5788 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5789 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5790 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5791 emit_split_vector(ctx
, dst
, 2);
5793 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5794 nir_print_instr(&instr
->instr
, stderr
);
5795 fprintf(stderr
, "\n");
5800 case nir_intrinsic_quad_swap_horizontal
:
5801 case nir_intrinsic_quad_swap_vertical
:
5802 case nir_intrinsic_quad_swap_diagonal
:
5803 case nir_intrinsic_quad_swizzle_amd
: {
5804 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5805 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5806 emit_uniform_subgroup(ctx
, instr
, src
);
5809 uint16_t dpp_ctrl
= 0;
5810 switch (instr
->intrinsic
) {
5811 case nir_intrinsic_quad_swap_horizontal
:
5812 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
5814 case nir_intrinsic_quad_swap_vertical
:
5815 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
5817 case nir_intrinsic_quad_swap_diagonal
:
5818 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
5820 case nir_intrinsic_quad_swizzle_amd
: {
5821 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
5828 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5829 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5830 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
5831 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5832 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), src
);
5833 emit_wqm(ctx
, tmp
, dst
);
5834 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5835 Temp tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5836 emit_wqm(ctx
, tmp
, dst
);
5837 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5838 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5839 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5840 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
5841 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
5842 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5843 emit_split_vector(ctx
, dst
, 2);
5845 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5846 nir_print_instr(&instr
->instr
, stderr
);
5847 fprintf(stderr
, "\n");
5851 case nir_intrinsic_masked_swizzle_amd
: {
5852 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5853 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5854 emit_uniform_subgroup(ctx
, instr
, src
);
5857 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5858 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
5859 if (dst
.regClass() == v1
) {
5861 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
5863 } else if (dst
.regClass() == v2
) {
5864 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5865 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5866 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
5867 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
5868 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5869 emit_split_vector(ctx
, dst
, 2);
5871 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5872 nir_print_instr(&instr
->instr
, stderr
);
5873 fprintf(stderr
, "\n");
5877 case nir_intrinsic_write_invocation_amd
: {
5878 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5879 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5880 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5881 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5882 if (dst
.regClass() == v1
) {
5883 /* src2 is ignored for writelane. RA assigns the same reg for dst */
5884 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val
, lane
, src
), dst
);
5885 } else if (dst
.regClass() == v2
) {
5886 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
5887 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
5888 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
5889 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
5890 Temp lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_lo
, lane
, src_hi
));
5891 Temp hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_hi
, lane
, src_hi
));
5892 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5893 emit_split_vector(ctx
, dst
, 2);
5895 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5896 nir_print_instr(&instr
->instr
, stderr
);
5897 fprintf(stderr
, "\n");
5901 case nir_intrinsic_mbcnt_amd
: {
5902 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5903 RegClass rc
= RegClass(src
.type(), 1);
5904 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
5905 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
5906 Temp tmp
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), mask_lo
, Operand(0u));
5907 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5908 Temp wqm_tmp
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), mask_hi
, tmp
);
5909 emit_wqm(ctx
, wqm_tmp
, dst
);
5912 case nir_intrinsic_load_helper_invocation
: {
5913 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5914 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
5915 ctx
->block
->kind
|= block_kind_needs_lowering
;
5916 ctx
->program
->needs_exact
= true;
5919 case nir_intrinsic_is_helper_invocation
: {
5920 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5921 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
5922 ctx
->block
->kind
|= block_kind_needs_lowering
;
5923 ctx
->program
->needs_exact
= true;
5926 case nir_intrinsic_demote
:
5927 bld
.pseudo(aco_opcode::p_demote_to_helper
);
5928 ctx
->block
->kind
|= block_kind_uses_demote
;
5929 ctx
->program
->needs_exact
= true;
5931 case nir_intrinsic_demote_if
: {
5932 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5933 as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false),
5935 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
5936 ctx
->block
->kind
|= block_kind_uses_demote
;
5937 ctx
->program
->needs_exact
= true;
5940 case nir_intrinsic_first_invocation
: {
5941 emit_wqm(ctx
, bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)),
5942 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5945 case nir_intrinsic_shader_clock
:
5946 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
5947 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
5949 case nir_intrinsic_load_vertex_id_zero_base
: {
5950 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5951 bld
.copy(Definition(dst
), ctx
->vertex_id
);
5954 case nir_intrinsic_load_first_vertex
: {
5955 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5956 bld
.copy(Definition(dst
), ctx
->base_vertex
);
5959 case nir_intrinsic_load_base_instance
: {
5960 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5961 bld
.copy(Definition(dst
), ctx
->start_instance
);
5964 case nir_intrinsic_load_instance_id
: {
5965 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5966 bld
.copy(Definition(dst
), ctx
->instance_id
);
5969 case nir_intrinsic_load_draw_id
: {
5970 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5971 bld
.copy(Definition(dst
), ctx
->draw_id
);
5975 fprintf(stderr
, "Unimplemented intrinsic instr: ");
5976 nir_print_instr(&instr
->instr
, stderr
);
5977 fprintf(stderr
, "\n");
5985 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
5986 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
5987 enum glsl_base_type
*stype
)
5989 nir_deref_instr
*texture_deref_instr
= NULL
;
5990 nir_deref_instr
*sampler_deref_instr
= NULL
;
5993 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
5994 switch (instr
->src
[i
].src_type
) {
5995 case nir_tex_src_texture_deref
:
5996 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5998 case nir_tex_src_sampler_deref
:
5999 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
6001 case nir_tex_src_plane
:
6002 plane
= nir_src_as_int(instr
->src
[i
].src
);
6009 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
6011 if (!sampler_deref_instr
)
6012 sampler_deref_instr
= texture_deref_instr
;
6015 assert(instr
->op
!= nir_texop_txf_ms
&&
6016 instr
->op
!= nir_texop_samples_identical
);
6017 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
6018 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
6019 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6020 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
6022 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
6025 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
6026 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
6027 fprintf(stderr
, "Unimplemented sampler descriptor: ");
6028 nir_print_instr(&instr
->instr
, stderr
);
6029 fprintf(stderr
, "\n");
6031 // TODO: build samp_ptr = and(samp_ptr, res_ptr)
6034 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
6035 instr
->op
== nir_texop_samples_identical
))
6036 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6039 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
6040 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
6042 Builder
bld(ctx
->program
, ctx
->block
);
6044 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
6045 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
6046 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
6048 Operand
neg_one(0xbf800000u
);
6049 Operand
one(0x3f800000u
);
6050 Operand
two(0x40000000u
);
6051 Operand
four(0x40800000u
);
6053 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), ma
);
6054 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
6055 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
6057 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), four
, id
);
6058 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
6059 is_ma_y
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.hint_vcc(bld
.def(s2
)), is_ma_y
, is_ma_z
);
6060 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
);
6063 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
6064 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6065 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
6067 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6070 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
6071 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
6072 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6075 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6076 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
6078 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
6079 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
6082 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
6084 Builder
bld(ctx
->program
, ctx
->block
);
6085 Temp coord_args
[4], ma
, tc
, sc
, id
;
6086 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
6087 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
6090 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
6092 // see comment in ac_prepare_cube_coords()
6093 if (ctx
->options
->chip_class
<= GFX8
)
6094 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6097 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6099 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6100 vop3a
->operands
[0] = Operand(ma
);
6101 vop3a
->abs
[0] = true;
6102 Temp invma
= bld
.tmp(v1
);
6103 vop3a
->definitions
[0] = Definition(invma
);
6104 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6106 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6108 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6110 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6112 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6114 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6117 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6118 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6120 for (unsigned i
= 0; i
< 2; i
++) {
6121 // see comment in ac_prepare_cube_coords()
6123 Temp deriv_sc
, deriv_tc
;
6124 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6125 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6127 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6129 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6130 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6131 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6132 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6133 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6134 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6135 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6138 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6139 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6143 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6144 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6148 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6151 for (unsigned i
= 0; i
< coords
.size(); i
++)
6152 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6154 Builder
bld(ctx
->program
, ctx
->block
);
6155 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6157 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6158 for (unsigned i
= 0; i
< coords
.size(); i
++)
6159 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6160 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6161 vec
->definitions
[0] = Definition(res
);
6162 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6166 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6168 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6170 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6171 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6174 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6175 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6176 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6180 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6182 Builder
bld(ctx
->program
, ctx
->block
);
6183 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6184 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6185 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6186 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6187 nir_const_value
*sample_index_cv
= NULL
;
6188 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6189 enum glsl_base_type stype
;
6190 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6192 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6193 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6194 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6195 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6197 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6198 switch (instr
->src
[i
].src_type
) {
6199 case nir_tex_src_coord
:
6200 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6202 case nir_tex_src_bias
:
6203 if (instr
->op
== nir_texop_txb
) {
6204 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6208 case nir_tex_src_lod
: {
6209 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6211 if (val
&& val
->f32
<= 0.0) {
6214 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6219 case nir_tex_src_comparator
:
6220 if (instr
->is_shadow
) {
6221 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6225 case nir_tex_src_offset
:
6226 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6227 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6230 case nir_tex_src_ddx
:
6231 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6234 case nir_tex_src_ddy
:
6235 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6238 case nir_tex_src_ms_index
:
6239 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6240 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6241 has_sample_index
= true;
6243 case nir_tex_src_texture_offset
:
6244 case nir_tex_src_sampler_offset
:
6249 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6250 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6251 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6253 if (instr
->op
== nir_texop_texture_samples
) {
6254 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6256 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6257 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6258 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 */));
6259 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6261 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6262 samples
, Operand(1u), bld
.scc(is_msaa
));
6266 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6267 aco_ptr
<Instruction
> tmp_instr
;
6268 Temp acc
, pack
= Temp();
6270 uint32_t pack_const
= 0;
6271 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6272 if (!const_offset
[i
])
6274 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6277 if (offset
.type() == RegType::sgpr
) {
6278 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6279 if (const_offset
[i
])
6282 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6283 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6286 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6289 if (pack
== Temp()) {
6292 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6296 if (pack_const
&& pack
!= Temp())
6297 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6299 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6300 if (const_offset
[i
])
6303 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6304 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6307 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6310 if (pack
== Temp()) {
6313 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6317 if (pack_const
&& pack
!= Temp())
6318 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6320 if (pack_const
&& pack
== Temp())
6321 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6322 else if (pack
== Temp())
6328 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6329 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6331 /* pack derivatives */
6332 if (has_ddx
|| has_ddy
) {
6333 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
6334 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6335 ddx
, Operand(0u), ddy
, Operand(0u));
6337 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6342 if (instr
->coord_components
> 1 &&
6343 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6345 instr
->op
!= nir_texop_txf
)
6346 coords
= apply_round_slice(ctx
, coords
, 1);
6348 if (instr
->coord_components
> 2 &&
6349 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6350 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6351 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6352 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6354 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6355 coords
= apply_round_slice(ctx
, coords
, 2);
6357 if (ctx
->options
->chip_class
== GFX9
&&
6358 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6359 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6360 assert(coords
.size() > 0 && coords
.size() < 3);
6362 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6363 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6364 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6365 if (coords
.size() > 1)
6366 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6367 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6368 vec
->definitions
[0] = Definition(coords
);
6369 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6372 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6374 if (instr
->op
== nir_texop_samples_identical
)
6375 resource
= fmask_ptr
;
6377 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6378 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6379 instr
->op
!= nir_texop_txs
) {
6380 assert(has_sample_index
);
6381 Operand
op(sample_index
);
6382 if (sample_index_cv
)
6383 op
= Operand(sample_index_cv
->u32
);
6384 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6387 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6388 Temp split_coords
[coords
.size()];
6389 emit_split_vector(ctx
, coords
, coords
.size());
6390 for (unsigned i
= 0; i
< coords
.size(); i
++)
6391 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6394 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6395 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6396 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6399 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6400 for (unsigned i
= 0; i
< coords
.size(); i
++)
6401 vec
->operands
[i
] = Operand(split_coords
[i
]);
6402 coords
= bld
.tmp(coords
.regClass());
6403 vec
->definitions
[0] = Definition(coords
);
6404 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6409 /* Build tex instruction */
6410 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6411 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
6412 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
6414 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6417 /* gather4 selects the component by dmask and always returns vec4 */
6418 if (instr
->op
== nir_texop_tg4
) {
6419 assert(instr
->dest
.ssa
.num_components
== 4);
6420 if (instr
->is_shadow
)
6423 dmask
= 1 << instr
->component
;
6424 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6425 tmp_dst
= bld
.tmp(v4
);
6426 } else if (instr
->op
== nir_texop_samples_identical
) {
6427 tmp_dst
= bld
.tmp(v1
);
6428 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6429 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6432 aco_ptr
<MIMG_instruction
> tex
;
6433 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6435 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6437 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6438 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6441 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6442 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6444 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6445 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6446 tex
->operands
[1] = Operand(resource
);
6447 if (ctx
->options
->chip_class
== GFX9
&&
6448 instr
->op
== nir_texop_txs
&&
6449 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6451 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6452 } else if (instr
->op
== nir_texop_query_levels
) {
6453 tex
->dmask
= 1 << 3;
6458 tex
->definitions
[0] = Definition(tmp_dst
);
6460 tex
->can_reorder
= true;
6461 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6464 /* divide 3rd value by 6 by multiplying with magic number */
6465 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6466 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6467 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6468 assert(instr
->dest
.ssa
.num_components
== 3);
6469 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6470 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6471 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6472 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6477 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6481 Temp tg4_compare_cube_wa64
= Temp();
6483 if (tg4_integer_workarounds
) {
6484 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6485 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6486 tex
->operands
[1] = Operand(resource
);
6490 Temp size
= bld
.tmp(v2
);
6491 tex
->definitions
[0] = Definition(size
);
6492 tex
->can_reorder
= true;
6493 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6494 emit_split_vector(ctx
, size
, size
.size());
6497 for (unsigned i
= 0; i
< 2; i
++) {
6498 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6499 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6500 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6501 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6504 Temp orig_coords
[2] = {
6505 emit_extract_vector(ctx
, coords
, 0, v1
),
6506 emit_extract_vector(ctx
, coords
, 1, v1
)};
6507 Temp new_coords
[2] = {
6508 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6509 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6512 if (tg4_integer_cube_workaround
) {
6513 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6514 Temp desc
[resource
.size()];
6515 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6516 Format::PSEUDO
, 1, resource
.size())};
6517 split
->operands
[0] = Operand(resource
);
6518 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6519 desc
[i
] = bld
.tmp(s1
);
6520 split
->definitions
[i
] = Definition(desc
[i
]);
6522 ctx
->block
->instructions
.emplace_back(std::move(split
));
6524 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6525 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6526 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6529 if (stype
== GLSL_TYPE_UINT
) {
6530 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6531 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6532 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6533 bld
.scc(compare_cube_wa
));
6535 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6536 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6537 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6538 bld
.scc(compare_cube_wa
));
6540 tg4_compare_cube_wa64
= as_divergent_bool(ctx
, compare_cube_wa
, true);
6541 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6543 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6544 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6545 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6547 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6548 Format::PSEUDO
, resource
.size(), 1)};
6549 for (unsigned i
= 0; i
< resource
.size(); i
++)
6550 vec
->operands
[i
] = Operand(desc
[i
]);
6551 resource
= bld
.tmp(resource
.regClass());
6552 vec
->definitions
[0] = Definition(resource
);
6553 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6555 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6556 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6557 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6558 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6561 if (coords
.size() == 3) {
6562 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6563 new_coords
[0], new_coords
[1],
6564 emit_extract_vector(ctx
, coords
, 2, v1
));
6566 assert(coords
.size() == 2);
6567 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6568 new_coords
[0], new_coords
[1]);
6572 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6573 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6574 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6575 coords
= emit_wqm(ctx
, coords
, bld
.tmp(coords
.regClass()), true);
6577 std::vector
<Operand
> args
;
6579 args
.emplace_back(Operand(offset
));
6581 args
.emplace_back(Operand(bias
));
6583 args
.emplace_back(Operand(compare
));
6585 args
.emplace_back(Operand(derivs
));
6586 args
.emplace_back(Operand(coords
));
6587 if (has_sample_index
)
6588 args
.emplace_back(Operand(sample_index
));
6590 args
.emplace_back(lod
);
6593 if (args
.size() > 1) {
6594 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6596 for (unsigned i
= 0; i
< args
.size(); i
++) {
6597 size
+= args
[i
].size();
6598 vec
->operands
[i
] = args
[i
];
6600 RegClass rc
= RegClass(RegType::vgpr
, size
);
6601 Temp tmp
= bld
.tmp(rc
);
6602 vec
->definitions
[0] = Definition(tmp
);
6603 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6606 assert(args
[0].isTemp());
6607 arg
= Operand(as_vgpr(ctx
, args
[0].getTemp()));
6610 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6611 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6613 assert(coords
.size() == 1);
6614 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6618 op
= aco_opcode::buffer_load_format_x
; break;
6620 op
= aco_opcode::buffer_load_format_xy
; break;
6622 op
= aco_opcode::buffer_load_format_xyz
; break;
6624 op
= aco_opcode::buffer_load_format_xyzw
; break;
6626 unreachable("Tex instruction loads more than 4 components.");
6629 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6630 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6633 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6635 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6636 mubuf
->operands
[0] = Operand(coords
);
6637 mubuf
->operands
[1] = Operand(resource
);
6638 mubuf
->operands
[2] = Operand((uint32_t) 0);
6639 mubuf
->definitions
[0] = Definition(tmp_dst
);
6640 mubuf
->idxen
= true;
6641 mubuf
->can_reorder
= true;
6642 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6644 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6649 if (instr
->op
== nir_texop_txf
||
6650 instr
->op
== nir_texop_txf_ms
||
6651 instr
->op
== nir_texop_samples_identical
) {
6652 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6653 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6654 tex
->operands
[0] = Operand(arg
);
6655 tex
->operands
[1] = Operand(resource
);
6660 tex
->definitions
[0] = Definition(tmp_dst
);
6661 tex
->can_reorder
= true;
6662 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6664 if (instr
->op
== nir_texop_samples_identical
) {
6665 assert(dmask
== 1 && dst
.regClass() == v1
);
6666 assert(dst
.id() != tmp_dst
.id());
6668 Temp tmp
= bld
.tmp(s2
);
6669 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6670 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6673 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6678 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6679 aco_opcode opcode
= aco_opcode::image_sample
;
6680 if (has_offset
) { /* image_sample_*_o */
6682 opcode
= aco_opcode::image_sample_c_o
;
6684 opcode
= aco_opcode::image_sample_c_d_o
;
6686 opcode
= aco_opcode::image_sample_c_b_o
;
6688 opcode
= aco_opcode::image_sample_c_lz_o
;
6690 opcode
= aco_opcode::image_sample_c_l_o
;
6692 opcode
= aco_opcode::image_sample_o
;
6694 opcode
= aco_opcode::image_sample_d_o
;
6696 opcode
= aco_opcode::image_sample_b_o
;
6698 opcode
= aco_opcode::image_sample_lz_o
;
6700 opcode
= aco_opcode::image_sample_l_o
;
6702 } else { /* no offset */
6704 opcode
= aco_opcode::image_sample_c
;
6706 opcode
= aco_opcode::image_sample_c_d
;
6708 opcode
= aco_opcode::image_sample_c_b
;
6710 opcode
= aco_opcode::image_sample_c_lz
;
6712 opcode
= aco_opcode::image_sample_c_l
;
6714 opcode
= aco_opcode::image_sample
;
6716 opcode
= aco_opcode::image_sample_d
;
6718 opcode
= aco_opcode::image_sample_b
;
6720 opcode
= aco_opcode::image_sample_lz
;
6722 opcode
= aco_opcode::image_sample_l
;
6726 if (instr
->op
== nir_texop_tg4
) {
6728 opcode
= aco_opcode::image_gather4_lz_o
;
6730 opcode
= aco_opcode::image_gather4_c_lz_o
;
6732 opcode
= aco_opcode::image_gather4_lz
;
6734 opcode
= aco_opcode::image_gather4_c_lz
;
6736 } else if (instr
->op
== nir_texop_lod
) {
6737 opcode
= aco_opcode::image_get_lod
;
6740 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6741 tex
->operands
[0] = arg
;
6742 tex
->operands
[1] = Operand(resource
);
6743 tex
->operands
[2] = Operand(sampler
);
6747 tex
->definitions
[0] = Definition(tmp_dst
);
6748 tex
->can_reorder
= true;
6749 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6751 if (tg4_integer_cube_workaround
) {
6752 assert(tmp_dst
.id() != dst
.id());
6753 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
6755 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6757 for (unsigned i
= 0; i
< dst
.size(); i
++) {
6758 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
6760 if (stype
== GLSL_TYPE_UINT
)
6761 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
6763 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
6764 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
6766 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
6767 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6768 val
[0], val
[1], val
[2], val
[3]);
6770 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
6771 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
6776 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
6778 Temp tmp
= get_ssa_temp(ctx
, ssa
);
6779 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
6780 return Operand(tmp
.regClass());
6782 return Operand(tmp
);
6785 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
6787 aco_ptr
<Pseudo_instruction
> phi
;
6788 unsigned num_src
= exec_list_length(&instr
->srcs
);
6789 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6791 aco_opcode opcode
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
] ? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
6793 std::map
<unsigned, nir_ssa_def
*> phi_src
;
6794 bool all_undef
= true;
6795 nir_foreach_phi_src(src
, instr
) {
6796 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
6797 if (src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_ssa_undef
)
6801 Builder
bld(ctx
->program
, ctx
->block
);
6802 if (dst
.regClass() == s1
) {
6803 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
6804 } else if (dst
.regClass() == v1
) {
6805 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
6807 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6808 for (unsigned i
= 0; i
< dst
.size(); i
++)
6809 vec
->operands
[i
] = Operand(0u);
6810 vec
->definitions
[0] = Definition(dst
);
6811 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6816 /* try to scalarize vector phis */
6817 if (dst
.size() > 1) {
6818 // TODO: scalarize linear phis on divergent ifs
6819 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
6820 std::array
<Temp
, 4> new_vec
;
6821 for (std::pair
<const unsigned, nir_ssa_def
*>& pair
: phi_src
) {
6822 Operand src
= get_phi_operand(ctx
, pair
.second
);
6823 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end()) {
6824 can_scalarize
= false;
6828 if (can_scalarize
) {
6829 unsigned num_components
= instr
->dest
.ssa
.num_components
;
6830 assert(dst
.size() % num_components
== 0);
6831 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
6833 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
6834 for (unsigned k
= 0; k
< num_components
; k
++) {
6835 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
, 1));
6836 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6837 for (unsigned i
= 0; i
< num_src
; i
++) {
6838 Operand src
= get_phi_operand(ctx
, it
->second
);
6839 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
6842 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
6843 phi
->definitions
[0] = Definition(phi_dst
);
6844 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6845 new_vec
[k
] = phi_dst
;
6846 vec
->operands
[k
] = Operand(phi_dst
);
6848 vec
->definitions
[0] = Definition(dst
);
6849 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6850 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
6855 unsigned extra_src
= 0;
6856 if (opcode
== aco_opcode::p_linear_phi
&& (ctx
->block
->kind
& block_kind_loop_exit
) &&
6857 ctx
->program
->blocks
[ctx
->block
->index
-2].kind
& block_kind_continue_or_break
) {
6861 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
+ extra_src
, 1));
6863 /* if we have a linear phi on a divergent if, we know that one src is undef */
6864 if (opcode
== aco_opcode::p_linear_phi
&& ctx
->block
->kind
& block_kind_merge
) {
6865 assert(extra_src
== 0);
6867 /* we place the phi either in the invert-block or in the current block */
6868 if (phi_src
.begin()->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
) {
6869 assert((++phi_src
.begin())->second
->parent_instr
->type
== nir_instr_type_ssa_undef
);
6870 Block
& linear_else
= ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
6871 block
= &ctx
->program
->blocks
[linear_else
.linear_preds
[0]];
6872 assert(block
->kind
& block_kind_invert
);
6873 phi
->operands
[0] = get_phi_operand(ctx
, phi_src
.begin()->second
);
6875 assert((++phi_src
.begin())->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
);
6877 phi
->operands
[0] = get_phi_operand(ctx
, (++phi_src
.begin())->second
);
6879 phi
->operands
[1] = Operand(dst
.regClass());
6880 phi
->definitions
[0] = Definition(dst
);
6881 block
->instructions
.emplace(block
->instructions
.begin(), std::move(phi
));
6885 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6886 for (unsigned i
= 0; i
< num_src
; i
++) {
6887 phi
->operands
[i
] = get_phi_operand(ctx
, it
->second
);
6890 for (unsigned i
= 0; i
< extra_src
; i
++)
6891 phi
->operands
[num_src
+ i
] = Operand(dst
.regClass());
6892 phi
->definitions
[0] = Definition(dst
);
6893 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6897 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
6899 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
6901 assert(dst
.type() == RegType::sgpr
);
6903 if (dst
.size() == 1) {
6904 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
6906 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6907 for (unsigned i
= 0; i
< dst
.size(); i
++)
6908 vec
->operands
[i
] = Operand(0u);
6909 vec
->definitions
[0] = Definition(dst
);
6910 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6914 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
6916 Builder
bld(ctx
->program
, ctx
->block
);
6917 Block
*logical_target
;
6918 append_logical_end(ctx
->block
);
6919 unsigned idx
= ctx
->block
->index
;
6921 switch (instr
->type
) {
6922 case nir_jump_break
:
6923 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
6924 add_logical_edge(idx
, logical_target
);
6925 ctx
->block
->kind
|= block_kind_break
;
6927 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
6928 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
6929 /* uniform break - directly jump out of the loop */
6930 ctx
->block
->kind
|= block_kind_uniform
;
6931 ctx
->cf_info
.has_branch
= true;
6932 bld
.branch(aco_opcode::p_branch
);
6933 add_linear_edge(idx
, logical_target
);
6936 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6938 case nir_jump_continue
:
6939 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6940 add_logical_edge(idx
, logical_target
);
6941 ctx
->block
->kind
|= block_kind_continue
;
6943 if (ctx
->cf_info
.parent_if
.is_divergent
) {
6944 /* for potential uniform breaks after this continue,
6945 we must ensure that they are handled correctly */
6946 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
6947 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6949 /* uniform continue - directly jump to the loop header */
6950 ctx
->block
->kind
|= block_kind_uniform
;
6951 ctx
->cf_info
.has_branch
= true;
6952 bld
.branch(aco_opcode::p_branch
);
6953 add_linear_edge(idx
, logical_target
);
6958 fprintf(stderr
, "Unknown NIR jump instr: ");
6959 nir_print_instr(&instr
->instr
, stderr
);
6960 fprintf(stderr
, "\n");
6964 /* remove critical edges from linear CFG */
6965 bld
.branch(aco_opcode::p_branch
);
6966 Block
* break_block
= ctx
->program
->create_and_insert_block();
6967 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6968 break_block
->kind
|= block_kind_uniform
;
6969 add_linear_edge(idx
, break_block
);
6970 /* the loop_header pointer might be invalidated by this point */
6971 if (instr
->type
== nir_jump_continue
)
6972 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6973 add_linear_edge(break_block
->index
, logical_target
);
6974 bld
.reset(break_block
);
6975 bld
.branch(aco_opcode::p_branch
);
6977 Block
* continue_block
= ctx
->program
->create_and_insert_block();
6978 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6979 add_linear_edge(idx
, continue_block
);
6980 append_logical_start(continue_block
);
6981 ctx
->block
= continue_block
;
6985 void visit_block(isel_context
*ctx
, nir_block
*block
)
6987 nir_foreach_instr(instr
, block
) {
6988 switch (instr
->type
) {
6989 case nir_instr_type_alu
:
6990 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
6992 case nir_instr_type_load_const
:
6993 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
6995 case nir_instr_type_intrinsic
:
6996 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
6998 case nir_instr_type_tex
:
6999 visit_tex(ctx
, nir_instr_as_tex(instr
));
7001 case nir_instr_type_phi
:
7002 visit_phi(ctx
, nir_instr_as_phi(instr
));
7004 case nir_instr_type_ssa_undef
:
7005 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
7007 case nir_instr_type_deref
:
7009 case nir_instr_type_jump
:
7010 visit_jump(ctx
, nir_instr_as_jump(instr
));
7013 fprintf(stderr
, "Unknown NIR instr type: ");
7014 nir_print_instr(instr
, stderr
);
7015 fprintf(stderr
, "\n");
7023 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
7025 append_logical_end(ctx
->block
);
7026 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
7027 Builder
bld(ctx
->program
, ctx
->block
);
7028 bld
.branch(aco_opcode::p_branch
);
7029 unsigned loop_preheader_idx
= ctx
->block
->index
;
7031 Block loop_exit
= Block();
7032 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7033 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
7035 Block
* loop_header
= ctx
->program
->create_and_insert_block();
7036 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
7037 loop_header
->kind
|= block_kind_loop_header
;
7038 add_edge(loop_preheader_idx
, loop_header
);
7039 ctx
->block
= loop_header
;
7041 /* emit loop body */
7042 unsigned loop_header_idx
= loop_header
->index
;
7043 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
7044 append_logical_start(ctx
->block
);
7045 visit_cf_list(ctx
, &loop
->body
);
7047 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7048 if (!ctx
->cf_info
.has_branch
) {
7049 append_logical_end(ctx
->block
);
7050 if (ctx
->cf_info
.exec_potentially_empty
) {
7051 /* Discards can result in code running with an empty exec mask.
7052 * This would result in divergent breaks not ever being taken. As a
7053 * workaround, break the loop when the loop mask is empty instead of
7054 * always continuing. */
7055 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
7057 /* create "loop_almost_exit" to avoid critical edges */
7058 unsigned block_idx
= ctx
->block
->index
;
7059 Block
*loop_almost_exit
= ctx
->program
->create_and_insert_block();
7060 loop_almost_exit
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7061 loop_almost_exit
->kind
= block_kind_uniform
;
7062 bld
.reset(loop_almost_exit
);
7063 bld
.branch(aco_opcode::p_branch
);
7065 add_linear_edge(block_idx
, loop_almost_exit
);
7066 add_linear_edge(loop_almost_exit
->index
, &loop_exit
);
7068 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
7070 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
7072 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7073 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7075 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7076 bld
.reset(ctx
->block
);
7077 bld
.branch(aco_opcode::p_branch
);
7080 /* fixup phis in loop header from unreachable blocks */
7081 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
7082 bool linear
= ctx
->cf_info
.has_branch
;
7083 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7084 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
7085 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
7086 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
7087 /* the last operand should be the one that needs to be removed */
7088 instr
->operands
.pop_back();
7089 } else if (!is_phi(instr
)) {
7095 ctx
->cf_info
.has_branch
= false;
7097 // TODO: if the loop has not a single exit, we must add one °°
7098 /* emit loop successor block */
7099 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
7100 append_logical_start(ctx
->block
);
7103 // TODO: check if it is beneficial to not branch on continues
7104 /* trim linear phis in loop header */
7105 for (auto&& instr
: loop_entry
->instructions
) {
7106 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
7107 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
7108 new_phi
->definitions
[0] = instr
->definitions
[0];
7109 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
7110 new_phi
->operands
[i
] = instr
->operands
[i
];
7111 /* check that the remaining operands are all the same */
7112 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
7113 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
7114 instr
.swap(new_phi
);
7115 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7124 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7128 append_logical_end(ctx
->block
);
7129 ctx
->block
->kind
|= block_kind_branch
;
7131 /* branch to linear then block */
7132 assert(cond
.regClass() == s2
);
7133 aco_ptr
<Pseudo_branch_instruction
> branch
;
7134 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7135 branch
->operands
[0] = Operand(cond
);
7136 ctx
->block
->instructions
.push_back(std::move(branch
));
7138 ic
->BB_if_idx
= ctx
->block
->index
;
7139 ic
->BB_invert
= Block();
7140 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7141 /* Invert blocks are intentionally not marked as top level because they
7142 * are not part of the logical cfg. */
7143 ic
->BB_invert
.kind
|= block_kind_invert
;
7144 ic
->BB_endif
= Block();
7145 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7146 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7148 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7149 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7150 ctx
->cf_info
.parent_if
.is_divergent
= true;
7151 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7153 /** emit logical then block */
7154 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7155 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7156 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7157 ctx
->block
= BB_then_logical
;
7158 append_logical_start(BB_then_logical
);
7161 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7163 Block
*BB_then_logical
= ctx
->block
;
7164 append_logical_end(BB_then_logical
);
7165 /* branch from logical then block to invert block */
7166 aco_ptr
<Pseudo_branch_instruction
> branch
;
7167 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7168 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7169 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7170 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7171 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7172 BB_then_logical
->kind
|= block_kind_uniform
;
7173 assert(!ctx
->cf_info
.has_branch
);
7174 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7175 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7177 /** emit linear then block */
7178 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7179 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7180 BB_then_linear
->kind
|= block_kind_uniform
;
7181 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7182 /* branch from linear then block to invert block */
7183 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7184 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7185 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7187 /** emit invert merge block */
7188 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7189 ic
->invert_idx
= ctx
->block
->index
;
7191 /* branch to linear else block (skip else) */
7192 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7193 branch
->operands
[0] = Operand(ic
->cond
);
7194 ctx
->block
->instructions
.push_back(std::move(branch
));
7196 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7197 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7199 /** emit logical else block */
7200 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7201 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7202 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7203 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7204 ctx
->block
= BB_else_logical
;
7205 append_logical_start(BB_else_logical
);
7208 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7210 Block
*BB_else_logical
= ctx
->block
;
7211 append_logical_end(BB_else_logical
);
7213 /* branch from logical else block to endif block */
7214 aco_ptr
<Pseudo_branch_instruction
> branch
;
7215 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7216 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7217 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7218 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7219 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7220 BB_else_logical
->kind
|= block_kind_uniform
;
7222 assert(!ctx
->cf_info
.has_branch
);
7223 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7226 /** emit linear else block */
7227 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7228 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7229 BB_else_linear
->kind
|= block_kind_uniform
;
7230 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7232 /* branch from linear else block to endif block */
7233 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7234 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7235 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7238 /** emit endif merge block */
7239 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7240 append_logical_start(ctx
->block
);
7243 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7244 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7245 /* uniform control flow never has an empty exec-mask */
7246 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7247 ctx
->cf_info
.exec_potentially_empty
= false;
7250 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7252 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7253 Builder
bld(ctx
->program
, ctx
->block
);
7254 aco_ptr
<Pseudo_branch_instruction
> branch
;
7256 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7258 * Uniform conditionals are represented in the following way*) :
7260 * The linear and logical CFG:
7263 * BB_THEN (logical) BB_ELSE (logical)
7267 * *) Exceptions may be due to break and continue statements within loops
7268 * If a break/continue happens within uniform control flow, it branches
7269 * to the loop exit/entry block. Otherwise, it branches to the next
7272 append_logical_end(ctx
->block
);
7273 ctx
->block
->kind
|= block_kind_uniform
;
7276 if (cond
.regClass() == s2
) {
7277 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7278 cond
= as_uniform_bool(ctx
, cond
);
7280 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7281 branch
->operands
[0] = Operand(cond
);
7282 branch
->operands
[0].setFixed(scc
);
7283 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7285 unsigned BB_if_idx
= ctx
->block
->index
;
7286 Block BB_endif
= Block();
7287 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7288 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7290 /** emit then block */
7291 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7292 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7293 add_edge(BB_if_idx
, BB_then
);
7294 append_logical_start(BB_then
);
7295 ctx
->block
= BB_then
;
7296 visit_cf_list(ctx
, &if_stmt
->then_list
);
7297 BB_then
= ctx
->block
;
7298 bool then_branch
= ctx
->cf_info
.has_branch
;
7299 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7302 append_logical_end(BB_then
);
7303 /* branch from then block to endif block */
7304 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7305 BB_then
->instructions
.emplace_back(std::move(branch
));
7306 add_linear_edge(BB_then
->index
, &BB_endif
);
7307 if (!then_branch_divergent
)
7308 add_logical_edge(BB_then
->index
, &BB_endif
);
7309 BB_then
->kind
|= block_kind_uniform
;
7312 ctx
->cf_info
.has_branch
= false;
7313 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7315 /** emit else block */
7316 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7317 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7318 add_edge(BB_if_idx
, BB_else
);
7319 append_logical_start(BB_else
);
7320 ctx
->block
= BB_else
;
7321 visit_cf_list(ctx
, &if_stmt
->else_list
);
7322 BB_else
= ctx
->block
;
7324 if (!ctx
->cf_info
.has_branch
) {
7325 append_logical_end(BB_else
);
7326 /* branch from then block to endif block */
7327 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7328 BB_else
->instructions
.emplace_back(std::move(branch
));
7329 add_linear_edge(BB_else
->index
, &BB_endif
);
7330 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7331 add_logical_edge(BB_else
->index
, &BB_endif
);
7332 BB_else
->kind
|= block_kind_uniform
;
7335 ctx
->cf_info
.has_branch
&= then_branch
;
7336 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7338 /** emit endif merge block */
7339 if (!ctx
->cf_info
.has_branch
) {
7340 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7341 append_logical_start(ctx
->block
);
7343 } else { /* non-uniform condition */
7345 * To maintain a logical and linear CFG without critical edges,
7346 * non-uniform conditionals are represented in the following way*) :
7351 * BB_THEN (logical) BB_THEN (linear)
7353 * BB_INVERT (linear)
7355 * BB_ELSE (logical) BB_ELSE (linear)
7362 * BB_THEN (logical) BB_ELSE (logical)
7366 * *) Exceptions may be due to break and continue statements within loops
7371 begin_divergent_if_then(ctx
, &ic
, cond
);
7372 visit_cf_list(ctx
, &if_stmt
->then_list
);
7374 begin_divergent_if_else(ctx
, &ic
);
7375 visit_cf_list(ctx
, &if_stmt
->else_list
);
7377 end_divergent_if(ctx
, &ic
);
7381 static void visit_cf_list(isel_context
*ctx
,
7382 struct exec_list
*list
)
7384 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7385 switch (node
->type
) {
7386 case nir_cf_node_block
:
7387 visit_block(ctx
, nir_cf_node_as_block(node
));
7389 case nir_cf_node_if
:
7390 visit_if(ctx
, nir_cf_node_as_if(node
));
7392 case nir_cf_node_loop
:
7393 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7396 unreachable("unimplemented cf list type");
7401 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7403 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7404 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7405 if (!is_pos
&& !mask
)
7407 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7409 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7410 exp
->enabled_mask
= mask
;
7411 for (unsigned i
= 0; i
< 4; ++i
) {
7412 if (mask
& (1 << i
))
7413 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7415 exp
->operands
[i
] = Operand(v1
);
7417 exp
->valid_mask
= false;
7419 exp
->compressed
= false;
7421 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7423 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7424 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7427 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7429 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7430 exp
->enabled_mask
= 0;
7431 for (unsigned i
= 0; i
< 4; ++i
)
7432 exp
->operands
[i
] = Operand(v1
);
7433 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7434 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7435 exp
->enabled_mask
|= 0x1;
7437 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7438 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7439 exp
->enabled_mask
|= 0x4;
7441 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7442 if (ctx
->options
->chip_class
< GFX9
) {
7443 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7444 exp
->enabled_mask
|= 0x8;
7446 Builder
bld(ctx
->program
, ctx
->block
);
7448 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7449 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7450 if (exp
->operands
[2].isTemp())
7451 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7453 exp
->operands
[2] = Operand(out
);
7454 exp
->enabled_mask
|= 0x4;
7457 exp
->valid_mask
= false;
7459 exp
->compressed
= false;
7460 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7461 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7464 static void create_vs_exports(isel_context
*ctx
)
7466 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7468 if (outinfo
->export_prim_id
) {
7469 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7470 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = ctx
->vs_prim_id
;
7473 if (ctx
->options
->key
.has_multiview_view_index
) {
7474 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7475 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, ctx
->view_index
);
7478 /* the order these position exports are created is important */
7480 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7481 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7482 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7484 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7485 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7486 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7487 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7489 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7490 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7491 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7492 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7493 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7496 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7497 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7498 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7501 export_vs_varying(ctx
, i
, false, NULL
);
7505 static void emit_stream_output(isel_context
*ctx
,
7506 Temp
const *so_buffers
,
7507 Temp
const *so_write_offset
,
7508 const struct radv_stream_output
*output
)
7510 unsigned num_comps
= util_bitcount(output
->component_mask
);
7511 unsigned loc
= output
->location
;
7512 unsigned buf
= output
->buffer
;
7513 unsigned offset
= output
->offset
;
7515 assert(num_comps
&& num_comps
<= 4);
7516 if (!num_comps
|| num_comps
> 4)
7519 unsigned start
= ffs(output
->component_mask
) - 1;
7522 bool all_undef
= true;
7523 assert(ctx
->stage
== vertex_vs
);
7524 for (unsigned i
= 0; i
< num_comps
; i
++) {
7525 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7526 all_undef
= all_undef
&& !out
[i
].id();
7531 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7532 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7533 for (unsigned i
= 0; i
< num_comps
; ++i
)
7534 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7535 vec
->definitions
[0] = Definition(write_data
);
7536 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7539 switch (num_comps
) {
7541 opcode
= aco_opcode::buffer_store_dword
;
7544 opcode
= aco_opcode::buffer_store_dwordx2
;
7547 opcode
= aco_opcode::buffer_store_dwordx3
;
7550 opcode
= aco_opcode::buffer_store_dwordx4
;
7554 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7555 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7556 store
->operands
[1] = Operand(so_buffers
[buf
]);
7557 store
->operands
[2] = Operand((uint32_t) 0);
7558 store
->operands
[3] = Operand(write_data
);
7559 if (offset
> 4095) {
7560 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7561 Builder
bld(ctx
->program
, ctx
->block
);
7562 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7564 store
->offset
= offset
;
7566 store
->offen
= true;
7570 store
->can_reorder
= true;
7571 ctx
->block
->instructions
.emplace_back(std::move(store
));
7574 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7576 Builder
bld(ctx
->program
, ctx
->block
);
7579 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, ctx
->streamout_buffers
);
7580 for (unsigned i
= 0; i
< 4; i
++) {
7581 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7585 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7588 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7589 ctx
->streamout_config
, Operand(0x70010u
));
7591 Temp tid
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7592 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7594 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7597 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7599 bld
.reset(ctx
->block
);
7601 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), ctx
->streamout_write_idx
, tid
);
7603 Temp so_write_offset
[4];
7605 for (unsigned i
= 0; i
< 4; i
++) {
7606 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7611 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7612 ctx
->streamout_write_idx
, ctx
->streamout_offset
[i
]);
7613 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7615 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7617 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7618 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u), ctx
->streamout_offset
[i
]);
7619 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7623 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7624 struct radv_stream_output
*output
=
7625 &ctx
->program
->info
->so
.outputs
[i
];
7626 if (stream
!= output
->stream
)
7629 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7632 begin_divergent_if_else(ctx
, &ic
);
7633 end_divergent_if(ctx
, &ic
);
7636 } /* end namespace */
7638 void handle_bc_optimize(isel_context
*ctx
)
7640 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7641 Builder
bld(ctx
->program
, ctx
->block
);
7642 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7643 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7644 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7645 if (uses_center
&& uses_centroid
) {
7646 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(s2
)), ctx
->prim_mask
, Operand(0u));
7648 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7649 for (unsigned i
= 0; i
< 2; i
++) {
7650 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7651 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
],
7652 ctx
->fs_inputs
[fs_input::persp_center_p1
+ i
],
7654 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
] = new_coord
;
7658 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7659 for (unsigned i
= 0; i
< 2; i
++) {
7660 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7661 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
],
7662 ctx
->fs_inputs
[fs_input::linear_center_p1
+ i
],
7664 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
] = new_coord
;
7670 void select_program(Program
*program
,
7671 unsigned shader_count
,
7672 struct nir_shader
*const *shaders
,
7673 ac_shader_config
* config
,
7674 struct radv_shader_info
*info
,
7675 struct radv_nir_compiler_options
*options
)
7677 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, info
, options
);
7679 for (unsigned i
= 0; i
< shader_count
; i
++) {
7680 nir_shader
*nir
= shaders
[i
];
7681 init_context(&ctx
, nir
);
7684 add_startpgm(&ctx
); /* needs to be after init_context() for FS */
7685 append_logical_start(ctx
.block
);
7689 if (shader_count
>= 2) {
7690 Builder
bld(ctx
.program
, ctx
.block
);
7691 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)));
7692 Temp thread_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7693 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7694 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(s2
)), count
, thread_id
);
7696 begin_divergent_if_then(&ctx
, &ic
, cond
);
7700 Builder
bld(ctx
.program
, ctx
.block
);
7701 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
7702 bld
.sopp(aco_opcode::s_barrier
);
7705 if (ctx
.stage
== fragment_fs
)
7706 handle_bc_optimize(&ctx
);
7708 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
7709 visit_cf_list(&ctx
, &func
->body
);
7711 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
7712 emit_streamout(&ctx
, 0);
7714 if (ctx
.stage
== vertex_vs
)
7715 create_vs_exports(&ctx
);
7717 if (shader_count
>= 2) {
7718 begin_divergent_if_else(&ctx
, &ic
);
7719 end_divergent_if(&ctx
, &ic
);
7722 ralloc_free(ctx
.divergent_vals
);
7725 append_logical_end(ctx
.block
);
7726 ctx
.block
->kind
|= block_kind_uniform
;
7727 Builder
bld(ctx
.program
, ctx
.block
);
7728 if (ctx
.program
->wb_smem_l1_on_end
)
7729 bld
.smem(aco_opcode::s_dcache_wb
, false);
7730 bld
.sopp(aco_opcode::s_endpgm
);
7733 for (Block
& BB
: program
->blocks
) {
7734 for (unsigned idx
: BB
.linear_preds
)
7735 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
7736 for (unsigned idx
: BB
.logical_preds
)
7737 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);